/drivers/scsi/osst.c

http://github.com/mirrors/linux · C · 6096 lines · 5156 code · 631 blank · 309 comment · 1505 complexity · a9b3cc6e7aa97c372afc28aeafce0596 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
  3. file Documentation/scsi/st.txt for more information.
  4. History:
  5. OnStream SCSI Tape support (osst) cloned from st.c by
  6. Willem Riede (osst@riede.org) Feb 2000
  7. Fixes ... Kurt Garloff <garloff@suse.de> Mar 2000
  8. Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
  9. Contribution and ideas from several people including (in alphabetical
  10. order) Klaus Ehrenfried, Wolfgang Denk, Steve Hirsch, Andreas Koppenh"ofer,
  11. Michael Leodolter, Eyal Lebedinsky, J"org Weule, and Eric Youngdale.
  12. Copyright 1992 - 2002 Kai Makisara / 2000 - 2006 Willem Riede
  13. email osst@riede.org
  14. $Header: /cvsroot/osst/Driver/osst.c,v 1.73 2005/01/01 21:13:34 wriede Exp $
  15. Microscopic alterations - Rik Ling, 2000/12/21
  16. Last st.c sync: Tue Oct 15 22:01:04 2002 by makisara
  17. Some small formal changes - aeb, 950809
  18. */
  19. static const char * cvsid = "$Id: osst.c,v 1.73 2005/01/01 21:13:34 wriede Exp $";
  20. static const char * osst_version = "0.99.4";
  21. /* The "failure to reconnect" firmware bug */
  22. #define OSST_FW_NEED_POLL_MIN 10601 /*(107A)*/
  23. #define OSST_FW_NEED_POLL_MAX 10704 /*(108D)*/
  24. #define OSST_FW_NEED_POLL(x,d) ((x) >= OSST_FW_NEED_POLL_MIN && (x) <= OSST_FW_NEED_POLL_MAX && d->host->this_id != 7)
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/kernel.h>
  28. #include <linux/sched.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/string.h>
  34. #include <linux/errno.h>
  35. #include <linux/mtio.h>
  36. #include <linux/ioctl.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/blkdev.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/delay.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/mutex.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/dma.h>
  47. /* The driver prints some debugging information on the console if DEBUG
  48. is defined and non-zero. */
  49. #define DEBUG 0
  50. /* The message level for the debug messages is currently set to KERN_NOTICE
  51. so that people can easily see the messages. Later when the debugging messages
  52. in the drivers are more widely classified, this may be changed to KERN_DEBUG. */
  53. #define OSST_DEB_MSG KERN_NOTICE
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_driver.h>
  58. #include <scsi/scsi_eh.h>
  59. #include <scsi/scsi_host.h>
  60. #include <scsi/scsi_ioctl.h>
  61. #define ST_KILOBYTE 1024
  62. #include "st.h"
  63. #include "osst.h"
  64. #include "osst_options.h"
  65. #include "osst_detect.h"
  66. static DEFINE_MUTEX(osst_int_mutex);
  67. static int max_dev = 0;
  68. static int write_threshold_kbs = 0;
  69. static int max_sg_segs = 0;
  70. #ifdef MODULE
  71. MODULE_AUTHOR("Willem Riede");
  72. MODULE_DESCRIPTION("OnStream {DI-|FW-|SC-|USB}{30|50} Tape Driver");
  73. MODULE_LICENSE("GPL");
  74. MODULE_ALIAS_CHARDEV_MAJOR(OSST_MAJOR);
  75. MODULE_ALIAS_SCSI_DEVICE(TYPE_TAPE);
  76. module_param(max_dev, int, 0444);
  77. MODULE_PARM_DESC(max_dev, "Maximum number of OnStream Tape Drives to attach (4)");
  78. module_param(write_threshold_kbs, int, 0644);
  79. MODULE_PARM_DESC(write_threshold_kbs, "Asynchronous write threshold (KB; 32)");
  80. module_param(max_sg_segs, int, 0644);
  81. MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (9)");
  82. #else
  83. static struct osst_dev_parm {
  84. char *name;
  85. int *val;
  86. } parms[] __initdata = {
  87. { "max_dev", &max_dev },
  88. { "write_threshold_kbs", &write_threshold_kbs },
  89. { "max_sg_segs", &max_sg_segs }
  90. };
  91. #endif
  92. /* Some default definitions have been moved to osst_options.h */
  93. #define OSST_BUFFER_SIZE (OSST_BUFFER_BLOCKS * ST_KILOBYTE)
  94. #define OSST_WRITE_THRESHOLD (OSST_WRITE_THRESHOLD_BLOCKS * ST_KILOBYTE)
  95. /* The buffer size should fit into the 24 bits for length in the
  96. 6-byte SCSI read and write commands. */
  97. #if OSST_BUFFER_SIZE >= (2 << 24 - 1)
  98. #error "Buffer size should not exceed (2 << 24 - 1) bytes!"
  99. #endif
  100. #if DEBUG
  101. static int debugging = 1;
  102. /* uncomment define below to test error recovery */
  103. // #define OSST_INJECT_ERRORS 1
  104. #endif
  105. /* Do not retry! The drive firmware already retries when appropriate,
  106. and when it tries to tell us something, we had better listen... */
  107. #define MAX_RETRIES 0
  108. #define NO_TAPE NOT_READY
  109. #define OSST_WAIT_POSITION_COMPLETE (HZ > 200 ? HZ / 200 : 1)
  110. #define OSST_WAIT_WRITE_COMPLETE (HZ / 12)
  111. #define OSST_WAIT_LONG_WRITE_COMPLETE (HZ / 2)
  112. #define OSST_TIMEOUT (200 * HZ)
  113. #define OSST_LONG_TIMEOUT (1800 * HZ)
  114. #define TAPE_NR(x) (iminor(x) & ~(-1 << ST_MODE_SHIFT))
  115. #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
  116. #define TAPE_REWIND(x) ((iminor(x) & 0x80) == 0)
  117. #define TAPE_IS_RAW(x) (TAPE_MODE(x) & (ST_NBR_MODES >> 1))
  118. /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower
  119. 24 bits) */
  120. #define SET_DENS_AND_BLK 0x10001
  121. static int osst_buffer_size = OSST_BUFFER_SIZE;
  122. static int osst_write_threshold = OSST_WRITE_THRESHOLD;
  123. static int osst_max_sg_segs = OSST_MAX_SG;
  124. static int osst_max_dev = OSST_MAX_TAPES;
  125. static int osst_nr_dev;
  126. static struct osst_tape **os_scsi_tapes = NULL;
  127. static DEFINE_RWLOCK(os_scsi_tapes_lock);
  128. static int modes_defined = 0;
  129. static struct osst_buffer *new_tape_buffer(int, int, int);
  130. static int enlarge_buffer(struct osst_buffer *, int);
  131. static void normalize_buffer(struct osst_buffer *);
  132. static int append_to_buffer(const char __user *, struct osst_buffer *, int);
  133. static int from_buffer(struct osst_buffer *, char __user *, int);
  134. static int osst_zero_buffer_tail(struct osst_buffer *);
  135. static int osst_copy_to_buffer(struct osst_buffer *, unsigned char *);
  136. static int osst_copy_from_buffer(struct osst_buffer *, unsigned char *);
  137. static int osst_probe(struct device *);
  138. static int osst_remove(struct device *);
  139. static struct scsi_driver osst_template = {
  140. .gendrv = {
  141. .name = "osst",
  142. .owner = THIS_MODULE,
  143. .probe = osst_probe,
  144. .remove = osst_remove,
  145. }
  146. };
  147. static int osst_int_ioctl(struct osst_tape *STp, struct osst_request ** aSRpnt,
  148. unsigned int cmd_in, unsigned long arg);
  149. static int osst_set_frame_position(struct osst_tape *STp, struct osst_request ** aSRpnt, int frame, int skip);
  150. static int osst_get_frame_position(struct osst_tape *STp, struct osst_request ** aSRpnt);
  151. static int osst_flush_write_buffer(struct osst_tape *STp, struct osst_request ** aSRpnt);
  152. static int osst_write_error_recovery(struct osst_tape * STp, struct osst_request ** aSRpnt, int pending);
  153. static inline char *tape_name(struct osst_tape *tape)
  154. {
  155. return tape->drive->disk_name;
  156. }
  157. /* Routines that handle the interaction with mid-layer SCSI routines */
  158. /* Normalize Sense */
  159. static void osst_analyze_sense(struct osst_request *SRpnt, struct st_cmdstatus *s)
  160. {
  161. const u8 *ucp;
  162. const u8 *sense = SRpnt->sense;
  163. s->have_sense = scsi_normalize_sense(SRpnt->sense,
  164. SCSI_SENSE_BUFFERSIZE, &s->sense_hdr);
  165. s->flags = 0;
  166. if (s->have_sense) {
  167. s->deferred = 0;
  168. s->remainder_valid =
  169. scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64);
  170. switch (sense[0] & 0x7f) {
  171. case 0x71:
  172. s->deferred = 1;
  173. case 0x70:
  174. s->fixed_format = 1;
  175. s->flags = sense[2] & 0xe0;
  176. break;
  177. case 0x73:
  178. s->deferred = 1;
  179. case 0x72:
  180. s->fixed_format = 0;
  181. ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
  182. s->flags = ucp ? (ucp[3] & 0xe0) : 0;
  183. break;
  184. }
  185. }
  186. }
  187. /* Convert the result to success code */
  188. static int osst_chk_result(struct osst_tape * STp, struct osst_request * SRpnt)
  189. {
  190. char *name = tape_name(STp);
  191. int result = SRpnt->result;
  192. u8 * sense = SRpnt->sense, scode;
  193. #if DEBUG
  194. const char *stp;
  195. #endif
  196. struct st_cmdstatus *cmdstatp;
  197. if (!result)
  198. return 0;
  199. cmdstatp = &STp->buffer->cmdstat;
  200. osst_analyze_sense(SRpnt, cmdstatp);
  201. if (cmdstatp->have_sense)
  202. scode = STp->buffer->cmdstat.sense_hdr.sense_key;
  203. else
  204. scode = 0;
  205. #if DEBUG
  206. if (debugging) {
  207. printk(OSST_DEB_MSG "%s:D: Error: %x, cmd: %x %x %x %x %x %x\n",
  208. name, result,
  209. SRpnt->cmd[0], SRpnt->cmd[1], SRpnt->cmd[2],
  210. SRpnt->cmd[3], SRpnt->cmd[4], SRpnt->cmd[5]);
  211. if (scode) printk(OSST_DEB_MSG "%s:D: Sense: %02x, ASC: %02x, ASCQ: %02x\n",
  212. name, scode, sense[12], sense[13]);
  213. if (cmdstatp->have_sense)
  214. __scsi_print_sense(STp->device, name,
  215. SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
  216. }
  217. else
  218. #endif
  219. if (cmdstatp->have_sense && (
  220. scode != NO_SENSE &&
  221. scode != RECOVERED_ERROR &&
  222. /* scode != UNIT_ATTENTION && */
  223. scode != BLANK_CHECK &&
  224. scode != VOLUME_OVERFLOW &&
  225. SRpnt->cmd[0] != MODE_SENSE &&
  226. SRpnt->cmd[0] != TEST_UNIT_READY)) { /* Abnormal conditions for tape */
  227. if (cmdstatp->have_sense) {
  228. printk(KERN_WARNING "%s:W: Command with sense data:\n", name);
  229. __scsi_print_sense(STp->device, name,
  230. SRpnt->sense, SCSI_SENSE_BUFFERSIZE);
  231. }
  232. else {
  233. static int notyetprinted = 1;
  234. printk(KERN_WARNING
  235. "%s:W: Warning %x (driver bt 0x%x, host bt 0x%x).\n",
  236. name, result, driver_byte(result),
  237. host_byte(result));
  238. if (notyetprinted) {
  239. notyetprinted = 0;
  240. printk(KERN_INFO
  241. "%s:I: This warning may be caused by your scsi controller,\n", name);
  242. printk(KERN_INFO
  243. "%s:I: it has been reported with some Buslogic cards.\n", name);
  244. }
  245. }
  246. }
  247. STp->pos_unknown |= STp->device->was_reset;
  248. if (cmdstatp->have_sense && scode == RECOVERED_ERROR) {
  249. STp->recover_count++;
  250. STp->recover_erreg++;
  251. #if DEBUG
  252. if (debugging) {
  253. if (SRpnt->cmd[0] == READ_6)
  254. stp = "read";
  255. else if (SRpnt->cmd[0] == WRITE_6)
  256. stp = "write";
  257. else
  258. stp = "ioctl";
  259. printk(OSST_DEB_MSG "%s:D: Recovered %s error (%d).\n", name, stp,
  260. STp->recover_count);
  261. }
  262. #endif
  263. if ((sense[2] & 0xe0) == 0)
  264. return 0;
  265. }
  266. return (-EIO);
  267. }
  268. /* Wakeup from interrupt */
  269. static void osst_end_async(struct request *req, int update)
  270. {
  271. struct osst_request *SRpnt = req->end_io_data;
  272. struct osst_tape *STp = SRpnt->stp;
  273. struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
  274. STp->buffer->cmdstat.midlevel_result = SRpnt->result = req->errors;
  275. #if DEBUG
  276. STp->write_pending = 0;
  277. #endif
  278. if (SRpnt->waiting)
  279. complete(SRpnt->waiting);
  280. if (SRpnt->bio) {
  281. kfree(mdata->pages);
  282. blk_rq_unmap_user(SRpnt->bio);
  283. }
  284. __blk_put_request(req->q, req);
  285. }
  286. /* osst_request memory management */
  287. static struct osst_request *osst_allocate_request(void)
  288. {
  289. return kzalloc(sizeof(struct osst_request), GFP_KERNEL);
  290. }
  291. static void osst_release_request(struct osst_request *streq)
  292. {
  293. kfree(streq);
  294. }
  295. static int osst_execute(struct osst_request *SRpnt, const unsigned char *cmd,
  296. int cmd_len, int data_direction, void *buffer, unsigned bufflen,
  297. int use_sg, int timeout, int retries)
  298. {
  299. struct request *req;
  300. struct page **pages = NULL;
  301. struct rq_map_data *mdata = &SRpnt->stp->buffer->map_data;
  302. int err = 0;
  303. int write = (data_direction == DMA_TO_DEVICE);
  304. req = blk_get_request(SRpnt->stp->device->request_queue, write, GFP_KERNEL);
  305. if (IS_ERR(req))
  306. return DRIVER_ERROR << 24;
  307. blk_rq_set_block_pc(req);
  308. req->cmd_flags |= REQ_QUIET;
  309. SRpnt->bio = NULL;
  310. if (use_sg) {
  311. struct scatterlist *sg, *sgl = (struct scatterlist *)buffer;
  312. int i;
  313. pages = kzalloc(use_sg * sizeof(struct page *), GFP_KERNEL);
  314. if (!pages)
  315. goto free_req;
  316. for_each_sg(sgl, sg, use_sg, i)
  317. pages[i] = sg_page(sg);
  318. mdata->null_mapped = 1;
  319. mdata->page_order = get_order(sgl[0].length);
  320. mdata->nr_entries =
  321. DIV_ROUND_UP(bufflen, PAGE_SIZE << mdata->page_order);
  322. mdata->offset = 0;
  323. err = blk_rq_map_user(req->q, req, mdata, NULL, bufflen, GFP_KERNEL);
  324. if (err) {
  325. kfree(pages);
  326. goto free_req;
  327. }
  328. SRpnt->bio = req->bio;
  329. mdata->pages = pages;
  330. } else if (bufflen) {
  331. err = blk_rq_map_kern(req->q, req, buffer, bufflen, GFP_KERNEL);
  332. if (err)
  333. goto free_req;
  334. }
  335. req->cmd_len = cmd_len;
  336. memset(req->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  337. memcpy(req->cmd, cmd, req->cmd_len);
  338. req->sense = SRpnt->sense;
  339. req->sense_len = 0;
  340. req->timeout = timeout;
  341. req->retries = retries;
  342. req->end_io_data = SRpnt;
  343. blk_execute_rq_nowait(req->q, NULL, req, 1, osst_end_async);
  344. return 0;
  345. free_req:
  346. blk_put_request(req);
  347. return DRIVER_ERROR << 24;
  348. }
  349. /* Do the scsi command. Waits until command performed if do_wait is true.
  350. Otherwise osst_write_behind_check() is used to check that the command
  351. has finished. */
  352. static struct osst_request * osst_do_scsi(struct osst_request *SRpnt, struct osst_tape *STp,
  353. unsigned char *cmd, int bytes, int direction, int timeout, int retries, int do_wait)
  354. {
  355. unsigned char *bp;
  356. unsigned short use_sg;
  357. #ifdef OSST_INJECT_ERRORS
  358. static int inject = 0;
  359. static int repeat = 0;
  360. #endif
  361. struct completion *waiting;
  362. /* if async, make sure there's no command outstanding */
  363. if (!do_wait && ((STp->buffer)->last_SRpnt)) {
  364. printk(KERN_ERR "%s: Async command already active.\n",
  365. tape_name(STp));
  366. if (signal_pending(current))
  367. (STp->buffer)->syscall_result = (-EINTR);
  368. else
  369. (STp->buffer)->syscall_result = (-EBUSY);
  370. return NULL;
  371. }
  372. if (SRpnt == NULL) {
  373. SRpnt = osst_allocate_request();
  374. if (SRpnt == NULL) {
  375. printk(KERN_ERR "%s: Can't allocate SCSI request.\n",
  376. tape_name(STp));
  377. if (signal_pending(current))
  378. (STp->buffer)->syscall_result = (-EINTR);
  379. else
  380. (STp->buffer)->syscall_result = (-EBUSY);
  381. return NULL;
  382. }
  383. SRpnt->stp = STp;
  384. }
  385. /* If async IO, set last_SRpnt. This ptr tells write_behind_check
  386. which IO is outstanding. It's nulled out when the IO completes. */
  387. if (!do_wait)
  388. (STp->buffer)->last_SRpnt = SRpnt;
  389. waiting = &STp->wait;
  390. init_completion(waiting);
  391. SRpnt->waiting = waiting;
  392. use_sg = (bytes > STp->buffer->sg[0].length) ? STp->buffer->use_sg : 0;
  393. if (use_sg) {
  394. bp = (char *)&(STp->buffer->sg[0]);
  395. if (STp->buffer->sg_segs < use_sg)
  396. use_sg = STp->buffer->sg_segs;
  397. }
  398. else
  399. bp = (STp->buffer)->b_data;
  400. memcpy(SRpnt->cmd, cmd, sizeof(SRpnt->cmd));
  401. STp->buffer->cmdstat.have_sense = 0;
  402. STp->buffer->syscall_result = 0;
  403. if (osst_execute(SRpnt, cmd, COMMAND_SIZE(cmd[0]), direction, bp, bytes,
  404. use_sg, timeout, retries))
  405. /* could not allocate the buffer or request was too large */
  406. (STp->buffer)->syscall_result = (-EBUSY);
  407. else if (do_wait) {
  408. wait_for_completion(waiting);
  409. SRpnt->waiting = NULL;
  410. STp->buffer->syscall_result = osst_chk_result(STp, SRpnt);
  411. #ifdef OSST_INJECT_ERRORS
  412. if (STp->buffer->syscall_result == 0 &&
  413. cmd[0] == READ_6 &&
  414. cmd[4] &&
  415. ( (++ inject % 83) == 29 ||
  416. (STp->first_frame_position == 240
  417. /* or STp->read_error_frame to fail again on the block calculated above */ &&
  418. ++repeat < 3))) {
  419. printk(OSST_DEB_MSG "%s:D: Injecting read error\n", tape_name(STp));
  420. STp->buffer->last_result_fatal = 1;
  421. }
  422. #endif
  423. }
  424. return SRpnt;
  425. }
  426. /* Handle the write-behind checking (downs the semaphore) */
  427. static void osst_write_behind_check(struct osst_tape *STp)
  428. {
  429. struct osst_buffer * STbuffer;
  430. STbuffer = STp->buffer;
  431. #if DEBUG
  432. if (STp->write_pending)
  433. STp->nbr_waits++;
  434. else
  435. STp->nbr_finished++;
  436. #endif
  437. wait_for_completion(&(STp->wait));
  438. STp->buffer->last_SRpnt->waiting = NULL;
  439. STp->buffer->syscall_result = osst_chk_result(STp, STp->buffer->last_SRpnt);
  440. if (STp->buffer->syscall_result)
  441. STp->buffer->syscall_result =
  442. osst_write_error_recovery(STp, &(STp->buffer->last_SRpnt), 1);
  443. else
  444. STp->first_frame_position++;
  445. osst_release_request(STp->buffer->last_SRpnt);
  446. if (STbuffer->writing < STbuffer->buffer_bytes)
  447. printk(KERN_WARNING "osst :A: write_behind_check: something left in buffer!\n");
  448. STbuffer->last_SRpnt = NULL;
  449. STbuffer->buffer_bytes -= STbuffer->writing;
  450. STbuffer->writing = 0;
  451. return;
  452. }
  453. /* Onstream specific Routines */
  454. /*
  455. * Initialize the OnStream AUX
  456. */
  457. static void osst_init_aux(struct osst_tape * STp, int frame_type, int frame_seq_number,
  458. int logical_blk_num, int blk_sz, int blk_cnt)
  459. {
  460. os_aux_t *aux = STp->buffer->aux;
  461. os_partition_t *par = &aux->partition;
  462. os_dat_t *dat = &aux->dat;
  463. if (STp->raw) return;
  464. memset(aux, 0, sizeof(*aux));
  465. aux->format_id = htonl(0);
  466. memcpy(aux->application_sig, "LIN4", 4);
  467. aux->hdwr = htonl(0);
  468. aux->frame_type = frame_type;
  469. switch (frame_type) {
  470. case OS_FRAME_TYPE_HEADER:
  471. aux->update_frame_cntr = htonl(STp->update_frame_cntr);
  472. par->partition_num = OS_CONFIG_PARTITION;
  473. par->par_desc_ver = OS_PARTITION_VERSION;
  474. par->wrt_pass_cntr = htons(0xffff);
  475. /* 0-4 = reserved, 5-9 = header, 2990-2994 = header, 2995-2999 = reserved */
  476. par->first_frame_ppos = htonl(0);
  477. par->last_frame_ppos = htonl(0xbb7);
  478. aux->frame_seq_num = htonl(0);
  479. aux->logical_blk_num_high = htonl(0);
  480. aux->logical_blk_num = htonl(0);
  481. aux->next_mark_ppos = htonl(STp->first_mark_ppos);
  482. break;
  483. case OS_FRAME_TYPE_DATA:
  484. case OS_FRAME_TYPE_MARKER:
  485. dat->dat_sz = 8;
  486. dat->reserved1 = 0;
  487. dat->entry_cnt = 1;
  488. dat->reserved3 = 0;
  489. dat->dat_list[0].blk_sz = htonl(blk_sz);
  490. dat->dat_list[0].blk_cnt = htons(blk_cnt);
  491. dat->dat_list[0].flags = frame_type==OS_FRAME_TYPE_MARKER?
  492. OS_DAT_FLAGS_MARK:OS_DAT_FLAGS_DATA;
  493. dat->dat_list[0].reserved = 0;
  494. case OS_FRAME_TYPE_EOD:
  495. aux->update_frame_cntr = htonl(0);
  496. par->partition_num = OS_DATA_PARTITION;
  497. par->par_desc_ver = OS_PARTITION_VERSION;
  498. par->wrt_pass_cntr = htons(STp->wrt_pass_cntr);
  499. par->first_frame_ppos = htonl(STp->first_data_ppos);
  500. par->last_frame_ppos = htonl(STp->capacity);
  501. aux->frame_seq_num = htonl(frame_seq_number);
  502. aux->logical_blk_num_high = htonl(0);
  503. aux->logical_blk_num = htonl(logical_blk_num);
  504. break;
  505. default: ; /* probably FILL */
  506. }
  507. aux->filemark_cnt = htonl(STp->filemark_cnt);
  508. aux->phys_fm = htonl(0xffffffff);
  509. aux->last_mark_ppos = htonl(STp->last_mark_ppos);
  510. aux->last_mark_lbn = htonl(STp->last_mark_lbn);
  511. }
  512. /*
  513. * Verify that we have the correct tape frame
  514. */
  515. static int osst_verify_frame(struct osst_tape * STp, int frame_seq_number, int quiet)
  516. {
  517. char * name = tape_name(STp);
  518. os_aux_t * aux = STp->buffer->aux;
  519. os_partition_t * par = &(aux->partition);
  520. struct st_partstat * STps = &(STp->ps[STp->partition]);
  521. int blk_cnt, blk_sz, i;
  522. if (STp->raw) {
  523. if (STp->buffer->syscall_result) {
  524. for (i=0; i < STp->buffer->sg_segs; i++)
  525. memset(page_address(sg_page(&STp->buffer->sg[i])),
  526. 0, STp->buffer->sg[i].length);
  527. strcpy(STp->buffer->b_data, "READ ERROR ON FRAME");
  528. } else
  529. STp->buffer->buffer_bytes = OS_FRAME_SIZE;
  530. return 1;
  531. }
  532. if (STp->buffer->syscall_result) {
  533. #if DEBUG
  534. printk(OSST_DEB_MSG "%s:D: Skipping frame, read error\n", name);
  535. #endif
  536. return 0;
  537. }
  538. if (ntohl(aux->format_id) != 0) {
  539. #if DEBUG
  540. printk(OSST_DEB_MSG "%s:D: Skipping frame, format_id %u\n", name, ntohl(aux->format_id));
  541. #endif
  542. goto err_out;
  543. }
  544. if (memcmp(aux->application_sig, STp->application_sig, 4) != 0 &&
  545. (memcmp(aux->application_sig, "LIN3", 4) != 0 || STp->linux_media_version != 4)) {
  546. #if DEBUG
  547. printk(OSST_DEB_MSG "%s:D: Skipping frame, incorrect application signature\n", name);
  548. #endif
  549. goto err_out;
  550. }
  551. if (par->partition_num != OS_DATA_PARTITION) {
  552. if (!STp->linux_media || STp->linux_media_version != 2) {
  553. #if DEBUG
  554. printk(OSST_DEB_MSG "%s:D: Skipping frame, partition num %d\n",
  555. name, par->partition_num);
  556. #endif
  557. goto err_out;
  558. }
  559. }
  560. if (par->par_desc_ver != OS_PARTITION_VERSION) {
  561. #if DEBUG
  562. printk(OSST_DEB_MSG "%s:D: Skipping frame, partition version %d\n", name, par->par_desc_ver);
  563. #endif
  564. goto err_out;
  565. }
  566. if (ntohs(par->wrt_pass_cntr) != STp->wrt_pass_cntr) {
  567. #if DEBUG
  568. printk(OSST_DEB_MSG "%s:D: Skipping frame, wrt_pass_cntr %d (expected %d)\n",
  569. name, ntohs(par->wrt_pass_cntr), STp->wrt_pass_cntr);
  570. #endif
  571. goto err_out;
  572. }
  573. if (aux->frame_type != OS_FRAME_TYPE_DATA &&
  574. aux->frame_type != OS_FRAME_TYPE_EOD &&
  575. aux->frame_type != OS_FRAME_TYPE_MARKER) {
  576. if (!quiet) {
  577. #if DEBUG
  578. printk(OSST_DEB_MSG "%s:D: Skipping frame, frame type %x\n", name, aux->frame_type);
  579. #endif
  580. }
  581. goto err_out;
  582. }
  583. if (aux->frame_type == OS_FRAME_TYPE_EOD &&
  584. STp->first_frame_position < STp->eod_frame_ppos) {
  585. printk(KERN_INFO "%s:I: Skipping premature EOD frame %d\n", name,
  586. STp->first_frame_position);
  587. goto err_out;
  588. }
  589. if (frame_seq_number != -1 && ntohl(aux->frame_seq_num) != frame_seq_number) {
  590. if (!quiet) {
  591. #if DEBUG
  592. printk(OSST_DEB_MSG "%s:D: Skipping frame, sequence number %u (expected %d)\n",
  593. name, ntohl(aux->frame_seq_num), frame_seq_number);
  594. #endif
  595. }
  596. goto err_out;
  597. }
  598. if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
  599. STps->eof = ST_FM_HIT;
  600. i = ntohl(aux->filemark_cnt);
  601. if (STp->header_cache != NULL && i < OS_FM_TAB_MAX && (i > STp->filemark_cnt ||
  602. STp->first_frame_position - 1 != ntohl(STp->header_cache->dat_fm_tab.fm_tab_ent[i]))) {
  603. #if DEBUG
  604. printk(OSST_DEB_MSG "%s:D: %s filemark %d at frame pos %d\n", name,
  605. STp->header_cache->dat_fm_tab.fm_tab_ent[i] == 0?"Learned":"Corrected",
  606. i, STp->first_frame_position - 1);
  607. #endif
  608. STp->header_cache->dat_fm_tab.fm_tab_ent[i] = htonl(STp->first_frame_position - 1);
  609. if (i >= STp->filemark_cnt)
  610. STp->filemark_cnt = i+1;
  611. }
  612. }
  613. if (aux->frame_type == OS_FRAME_TYPE_EOD) {
  614. STps->eof = ST_EOD_1;
  615. STp->frame_in_buffer = 1;
  616. }
  617. if (aux->frame_type == OS_FRAME_TYPE_DATA) {
  618. blk_cnt = ntohs(aux->dat.dat_list[0].blk_cnt);
  619. blk_sz = ntohl(aux->dat.dat_list[0].blk_sz);
  620. STp->buffer->buffer_bytes = blk_cnt * blk_sz;
  621. STp->buffer->read_pointer = 0;
  622. STp->frame_in_buffer = 1;
  623. /* See what block size was used to write file */
  624. if (STp->block_size != blk_sz && blk_sz > 0) {
  625. printk(KERN_INFO
  626. "%s:I: File was written with block size %d%c, currently %d%c, adjusted to match.\n",
  627. name, blk_sz<1024?blk_sz:blk_sz/1024,blk_sz<1024?'b':'k',
  628. STp->block_size<1024?STp->block_size:STp->block_size/1024,
  629. STp->block_size<1024?'b':'k');
  630. STp->block_size = blk_sz;
  631. STp->buffer->buffer_blocks = OS_DATA_SIZE / blk_sz;
  632. }
  633. STps->eof = ST_NOEOF;
  634. }
  635. STp->frame_seq_number = ntohl(aux->frame_seq_num);
  636. STp->logical_blk_num = ntohl(aux->logical_blk_num);
  637. return 1;
  638. err_out:
  639. if (STp->read_error_frame == 0)
  640. STp->read_error_frame = STp->first_frame_position - 1;
  641. return 0;
  642. }
  643. /*
  644. * Wait for the unit to become Ready
  645. */
  646. static int osst_wait_ready(struct osst_tape * STp, struct osst_request ** aSRpnt,
  647. unsigned timeout, int initial_delay)
  648. {
  649. unsigned char cmd[MAX_COMMAND_SIZE];
  650. struct osst_request * SRpnt;
  651. unsigned long startwait = jiffies;
  652. #if DEBUG
  653. int dbg = debugging;
  654. char * name = tape_name(STp);
  655. printk(OSST_DEB_MSG "%s:D: Reached onstream wait ready\n", name);
  656. #endif
  657. if (initial_delay > 0)
  658. msleep(jiffies_to_msecs(initial_delay));
  659. memset(cmd, 0, MAX_COMMAND_SIZE);
  660. cmd[0] = TEST_UNIT_READY;
  661. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  662. *aSRpnt = SRpnt;
  663. if (!SRpnt) return (-EBUSY);
  664. while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
  665. (( SRpnt->sense[2] == 2 && SRpnt->sense[12] == 4 &&
  666. (SRpnt->sense[13] == 1 || SRpnt->sense[13] == 8) ) ||
  667. ( SRpnt->sense[2] == 6 && SRpnt->sense[12] == 0x28 &&
  668. SRpnt->sense[13] == 0 ) )) {
  669. #if DEBUG
  670. if (debugging) {
  671. printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait ready\n", name);
  672. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  673. debugging = 0;
  674. }
  675. #endif
  676. msleep(100);
  677. memset(cmd, 0, MAX_COMMAND_SIZE);
  678. cmd[0] = TEST_UNIT_READY;
  679. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  680. }
  681. *aSRpnt = SRpnt;
  682. #if DEBUG
  683. debugging = dbg;
  684. #endif
  685. if ( STp->buffer->syscall_result &&
  686. osst_write_error_recovery(STp, aSRpnt, 0) ) {
  687. #if DEBUG
  688. printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait ready\n", name);
  689. printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
  690. STp->buffer->syscall_result, SRpnt->sense[0], SRpnt->sense[2],
  691. SRpnt->sense[12], SRpnt->sense[13]);
  692. #endif
  693. return (-EIO);
  694. }
  695. #if DEBUG
  696. printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait ready\n", name);
  697. #endif
  698. return 0;
  699. }
  700. /*
  701. * Wait for a tape to be inserted in the unit
  702. */
  703. static int osst_wait_for_medium(struct osst_tape * STp, struct osst_request ** aSRpnt, unsigned timeout)
  704. {
  705. unsigned char cmd[MAX_COMMAND_SIZE];
  706. struct osst_request * SRpnt;
  707. unsigned long startwait = jiffies;
  708. #if DEBUG
  709. int dbg = debugging;
  710. char * name = tape_name(STp);
  711. printk(OSST_DEB_MSG "%s:D: Reached onstream wait for medium\n", name);
  712. #endif
  713. memset(cmd, 0, MAX_COMMAND_SIZE);
  714. cmd[0] = TEST_UNIT_READY;
  715. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  716. *aSRpnt = SRpnt;
  717. if (!SRpnt) return (-EBUSY);
  718. while ( STp->buffer->syscall_result && time_before(jiffies, startwait + timeout*HZ) &&
  719. SRpnt->sense[2] == 2 && SRpnt->sense[12] == 0x3a && SRpnt->sense[13] == 0 ) {
  720. #if DEBUG
  721. if (debugging) {
  722. printk(OSST_DEB_MSG "%s:D: Sleeping in onstream wait medium\n", name);
  723. printk(OSST_DEB_MSG "%s:D: Turning off debugging for a while\n", name);
  724. debugging = 0;
  725. }
  726. #endif
  727. msleep(100);
  728. memset(cmd, 0, MAX_COMMAND_SIZE);
  729. cmd[0] = TEST_UNIT_READY;
  730. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  731. }
  732. *aSRpnt = SRpnt;
  733. #if DEBUG
  734. debugging = dbg;
  735. #endif
  736. if ( STp->buffer->syscall_result && SRpnt->sense[2] != 2 &&
  737. SRpnt->sense[12] != 4 && SRpnt->sense[13] == 1) {
  738. #if DEBUG
  739. printk(OSST_DEB_MSG "%s:D: Abnormal exit from onstream wait medium\n", name);
  740. printk(OSST_DEB_MSG "%s:D: Result = %d, Sense: 0=%02x, 2=%02x, 12=%02x, 13=%02x\n", name,
  741. STp->buffer->syscall_result, SRpnt->sense[0], SRpnt->sense[2],
  742. SRpnt->sense[12], SRpnt->sense[13]);
  743. #endif
  744. return 0;
  745. }
  746. #if DEBUG
  747. printk(OSST_DEB_MSG "%s:D: Normal exit from onstream wait medium\n", name);
  748. #endif
  749. return 1;
  750. }
  751. static int osst_position_tape_and_confirm(struct osst_tape * STp, struct osst_request ** aSRpnt, int frame)
  752. {
  753. int retval;
  754. osst_wait_ready(STp, aSRpnt, 15 * 60, 0); /* TODO - can this catch a write error? */
  755. retval = osst_set_frame_position(STp, aSRpnt, frame, 0);
  756. if (retval) return (retval);
  757. osst_wait_ready(STp, aSRpnt, 15 * 60, OSST_WAIT_POSITION_COMPLETE);
  758. return (osst_get_frame_position(STp, aSRpnt));
  759. }
  760. /*
  761. * Wait for write(s) to complete
  762. */
  763. static int osst_flush_drive_buffer(struct osst_tape * STp, struct osst_request ** aSRpnt)
  764. {
  765. unsigned char cmd[MAX_COMMAND_SIZE];
  766. struct osst_request * SRpnt;
  767. int result = 0;
  768. int delay = OSST_WAIT_WRITE_COMPLETE;
  769. #if DEBUG
  770. char * name = tape_name(STp);
  771. printk(OSST_DEB_MSG "%s:D: Reached onstream flush drive buffer (write filemark)\n", name);
  772. #endif
  773. memset(cmd, 0, MAX_COMMAND_SIZE);
  774. cmd[0] = WRITE_FILEMARKS;
  775. cmd[1] = 1;
  776. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  777. *aSRpnt = SRpnt;
  778. if (!SRpnt) return (-EBUSY);
  779. if (STp->buffer->syscall_result) {
  780. if ((SRpnt->sense[2] & 0x0f) == 2 && SRpnt->sense[12] == 4) {
  781. if (SRpnt->sense[13] == 8) {
  782. delay = OSST_WAIT_LONG_WRITE_COMPLETE;
  783. }
  784. } else
  785. result = osst_write_error_recovery(STp, aSRpnt, 0);
  786. }
  787. result |= osst_wait_ready(STp, aSRpnt, 5 * 60, delay);
  788. STp->ps[STp->partition].rw = OS_WRITING_COMPLETE;
  789. return (result);
  790. }
  791. #define OSST_POLL_PER_SEC 10
  792. static int osst_wait_frame(struct osst_tape * STp, struct osst_request ** aSRpnt, int curr, int minlast, int to)
  793. {
  794. unsigned long startwait = jiffies;
  795. char * name = tape_name(STp);
  796. #if DEBUG
  797. char notyetprinted = 1;
  798. #endif
  799. if (minlast >= 0 && STp->ps[STp->partition].rw != ST_READING)
  800. printk(KERN_ERR "%s:A: Waiting for frame without having initialized read!\n", name);
  801. while (time_before (jiffies, startwait + to*HZ))
  802. {
  803. int result;
  804. result = osst_get_frame_position(STp, aSRpnt);
  805. if (result == -EIO)
  806. if ((result = osst_write_error_recovery(STp, aSRpnt, 0)) == 0)
  807. return 0; /* successful recovery leaves drive ready for frame */
  808. if (result < 0) break;
  809. if (STp->first_frame_position == curr &&
  810. ((minlast < 0 &&
  811. (signed)STp->last_frame_position > (signed)curr + minlast) ||
  812. (minlast >= 0 && STp->cur_frames > minlast)
  813. ) && result >= 0)
  814. {
  815. #if DEBUG
  816. if (debugging || time_after_eq(jiffies, startwait + 2*HZ/OSST_POLL_PER_SEC))
  817. printk (OSST_DEB_MSG
  818. "%s:D: Succ wait f fr %i (>%i): %i-%i %i (%i): %3li.%li s\n",
  819. name, curr, curr+minlast, STp->first_frame_position,
  820. STp->last_frame_position, STp->cur_frames,
  821. result, (jiffies-startwait)/HZ,
  822. (((jiffies-startwait)%HZ)*10)/HZ);
  823. #endif
  824. return 0;
  825. }
  826. #if DEBUG
  827. if (time_after_eq(jiffies, startwait + 2*HZ/OSST_POLL_PER_SEC) && notyetprinted)
  828. {
  829. printk (OSST_DEB_MSG "%s:D: Wait for frame %i (>%i): %i-%i %i (%i)\n",
  830. name, curr, curr+minlast, STp->first_frame_position,
  831. STp->last_frame_position, STp->cur_frames, result);
  832. notyetprinted--;
  833. }
  834. #endif
  835. msleep(1000 / OSST_POLL_PER_SEC);
  836. }
  837. #if DEBUG
  838. printk (OSST_DEB_MSG "%s:D: Fail wait f fr %i (>%i): %i-%i %i: %3li.%li s\n",
  839. name, curr, curr+minlast, STp->first_frame_position,
  840. STp->last_frame_position, STp->cur_frames,
  841. (jiffies-startwait)/HZ, (((jiffies-startwait)%HZ)*10)/HZ);
  842. #endif
  843. return -EBUSY;
  844. }
  845. static int osst_recover_wait_frame(struct osst_tape * STp, struct osst_request ** aSRpnt, int writing)
  846. {
  847. struct osst_request * SRpnt;
  848. unsigned char cmd[MAX_COMMAND_SIZE];
  849. unsigned long startwait = jiffies;
  850. int retval = 1;
  851. char * name = tape_name(STp);
  852. if (writing) {
  853. char mybuf[24];
  854. char * olddata = STp->buffer->b_data;
  855. int oldsize = STp->buffer->buffer_size;
  856. /* write zero fm then read pos - if shows write error, try to recover - if no progress, wait */
  857. memset(cmd, 0, MAX_COMMAND_SIZE);
  858. cmd[0] = WRITE_FILEMARKS;
  859. cmd[1] = 1;
  860. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout,
  861. MAX_RETRIES, 1);
  862. while (retval && time_before (jiffies, startwait + 5*60*HZ)) {
  863. if (STp->buffer->syscall_result && (SRpnt->sense[2] & 0x0f) != 2) {
  864. /* some failure - not just not-ready */
  865. retval = osst_write_error_recovery(STp, aSRpnt, 0);
  866. break;
  867. }
  868. schedule_timeout_interruptible(HZ / OSST_POLL_PER_SEC);
  869. STp->buffer->b_data = mybuf; STp->buffer->buffer_size = 24;
  870. memset(cmd, 0, MAX_COMMAND_SIZE);
  871. cmd[0] = READ_POSITION;
  872. SRpnt = osst_do_scsi(SRpnt, STp, cmd, 20, DMA_FROM_DEVICE, STp->timeout,
  873. MAX_RETRIES, 1);
  874. retval = ( STp->buffer->syscall_result || (STp->buffer)->b_data[15] > 25 );
  875. STp->buffer->b_data = olddata; STp->buffer->buffer_size = oldsize;
  876. }
  877. if (retval)
  878. printk(KERN_ERR "%s:E: Device did not succeed to write buffered data\n", name);
  879. } else
  880. /* TODO - figure out which error conditions can be handled */
  881. if (STp->buffer->syscall_result)
  882. printk(KERN_WARNING
  883. "%s:W: Recover_wait_frame(read) cannot handle %02x:%02x:%02x\n", name,
  884. (*aSRpnt)->sense[ 2] & 0x0f,
  885. (*aSRpnt)->sense[12],
  886. (*aSRpnt)->sense[13]);
  887. return retval;
  888. }
  889. /*
  890. * Read the next OnStream tape frame at the current location
  891. */
  892. static int osst_read_frame(struct osst_tape * STp, struct osst_request ** aSRpnt, int timeout)
  893. {
  894. unsigned char cmd[MAX_COMMAND_SIZE];
  895. struct osst_request * SRpnt;
  896. int retval = 0;
  897. #if DEBUG
  898. os_aux_t * aux = STp->buffer->aux;
  899. char * name = tape_name(STp);
  900. #endif
  901. if (STp->poll)
  902. if (osst_wait_frame (STp, aSRpnt, STp->first_frame_position, 0, timeout))
  903. retval = osst_recover_wait_frame(STp, aSRpnt, 0);
  904. memset(cmd, 0, MAX_COMMAND_SIZE);
  905. cmd[0] = READ_6;
  906. cmd[1] = 1;
  907. cmd[4] = 1;
  908. #if DEBUG
  909. if (debugging)
  910. printk(OSST_DEB_MSG "%s:D: Reading frame from OnStream tape\n", name);
  911. #endif
  912. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, OS_FRAME_SIZE, DMA_FROM_DEVICE,
  913. STp->timeout, MAX_RETRIES, 1);
  914. *aSRpnt = SRpnt;
  915. if (!SRpnt)
  916. return (-EBUSY);
  917. if ((STp->buffer)->syscall_result) {
  918. retval = 1;
  919. if (STp->read_error_frame == 0) {
  920. STp->read_error_frame = STp->first_frame_position;
  921. #if DEBUG
  922. printk(OSST_DEB_MSG "%s:D: Recording read error at %d\n", name, STp->read_error_frame);
  923. #endif
  924. }
  925. #if DEBUG
  926. if (debugging)
  927. printk(OSST_DEB_MSG "%s:D: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n",
  928. name,
  929. SRpnt->sense[0], SRpnt->sense[1],
  930. SRpnt->sense[2], SRpnt->sense[3],
  931. SRpnt->sense[4], SRpnt->sense[5],
  932. SRpnt->sense[6], SRpnt->sense[7]);
  933. #endif
  934. }
  935. else
  936. STp->first_frame_position++;
  937. #if DEBUG
  938. if (debugging) {
  939. char sig[8]; int i;
  940. for (i=0;i<4;i++)
  941. sig[i] = aux->application_sig[i]<32?'^':aux->application_sig[i];
  942. sig[4] = '\0';
  943. printk(OSST_DEB_MSG
  944. "%s:D: AUX: %s UpdFrCt#%d Wpass#%d %s FrSeq#%d LogBlk#%d Qty=%d Sz=%d\n", name, sig,
  945. ntohl(aux->update_frame_cntr), ntohs(aux->partition.wrt_pass_cntr),
  946. aux->frame_type==1?"EOD":aux->frame_type==2?"MARK":
  947. aux->frame_type==8?"HEADR":aux->frame_type==0x80?"DATA":"FILL",
  948. ntohl(aux->frame_seq_num), ntohl(aux->logical_blk_num),
  949. ntohs(aux->dat.dat_list[0].blk_cnt), ntohl(aux->dat.dat_list[0].blk_sz) );
  950. if (aux->frame_type==2)
  951. printk(OSST_DEB_MSG "%s:D: mark_cnt=%d, last_mark_ppos=%d, last_mark_lbn=%d\n", name,
  952. ntohl(aux->filemark_cnt), ntohl(aux->last_mark_ppos), ntohl(aux->last_mark_lbn));
  953. printk(OSST_DEB_MSG "%s:D: Exit read frame from OnStream tape with code %d\n", name, retval);
  954. }
  955. #endif
  956. return (retval);
  957. }
  958. static int osst_initiate_read(struct osst_tape * STp, struct osst_request ** aSRpnt)
  959. {
  960. struct st_partstat * STps = &(STp->ps[STp->partition]);
  961. struct osst_request * SRpnt ;
  962. unsigned char cmd[MAX_COMMAND_SIZE];
  963. int retval = 0;
  964. char * name = tape_name(STp);
  965. if (STps->rw != ST_READING) { /* Initialize read operation */
  966. if (STps->rw == ST_WRITING || STp->dirty) {
  967. STp->write_type = OS_WRITE_DATA;
  968. osst_flush_write_buffer(STp, aSRpnt);
  969. osst_flush_drive_buffer(STp, aSRpnt);
  970. }
  971. STps->rw = ST_READING;
  972. STp->frame_in_buffer = 0;
  973. /*
  974. * Issue a read 0 command to get the OnStream drive
  975. * read frames into its buffer.
  976. */
  977. memset(cmd, 0, MAX_COMMAND_SIZE);
  978. cmd[0] = READ_6;
  979. cmd[1] = 1;
  980. #if DEBUG
  981. printk(OSST_DEB_MSG "%s:D: Start Read Ahead on OnStream tape\n", name);
  982. #endif
  983. SRpnt = osst_do_scsi(*aSRpnt, STp, cmd, 0, DMA_NONE, STp->timeout, MAX_RETRIES, 1);
  984. *aSRpnt = SRpnt;
  985. if ((retval = STp->buffer->syscall_result))
  986. printk(KERN_WARNING "%s:W: Error starting read ahead\n", name);
  987. }
  988. return retval;
  989. }
  990. static int osst_get_logical_frame(struct osst_tape * STp, struct osst_request ** aSRpnt,
  991. int frame_seq_number, int quiet)
  992. {
  993. struct st_partstat * STps = &(STp->ps[STp->partition]);
  994. char * name = tape_name(STp);
  995. int cnt = 0,
  996. bad = 0,
  997. past = 0,
  998. x,
  999. position;
  1000. /*
  1001. * If we want just any frame (-1) and there is a frame in the buffer, return it
  1002. */
  1003. if (frame_seq_number == -1 && STp->frame_in_buffer) {
  1004. #if DEBUG
  1005. printk(OSST_DEB_MSG "%s:D: Frame %d still in buffer\n", name, STp->frame_seq_number);
  1006. #endif
  1007. return (STps->eof);
  1008. }
  1009. /*
  1010. * Search and wait for the next logical tape frame
  1011. */
  1012. while (1) {
  1013. if (cnt++ > 400) {
  1014. printk(KERN_ERR "%s:E: Couldn't find logical frame %d, aborting\n",
  1015. name, frame_seq_number);
  1016. if (STp->read_error_frame) {
  1017. osst_set_frame_position(STp, aSRpnt, STp->read_error_frame, 0);
  1018. #if DEBUG
  1019. printk(OSST_DEB_MSG "%s:D: Repositioning tape to bad frame %d\n",
  1020. name, STp->read_error_frame);
  1021. #endif
  1022. STp->read_error_frame = 0;
  1023. STp->abort_count++;
  1024. }
  1025. return (-EIO);
  1026. }
  1027. #if DEBUG
  1028. if (debugging)
  1029. printk(OSST_DEB_MSG "%s:D: Looking for frame %d, attempt %d\n",
  1030. name, frame_seq_number, cnt);
  1031. #endif
  1032. if ( osst_initiate_read(STp, aSRpnt)
  1033. || ( (!STp->frame_in_buffer) && osst_read_frame(STp, aSRpnt, 30) ) ) {
  1034. if (STp->raw)
  1035. return (-EIO);
  1036. position = osst_get_frame_position(STp, aSRpnt);
  1037. if (position >= 0xbae && position < 0xbb8)
  1038. position = 0xbb8;
  1039. else if (position > STp->eod_frame_ppos || ++bad == 10) {
  1040. position = STp->read_error_frame - 1;
  1041. bad = 0;
  1042. }
  1043. else {
  1044. position += 29;
  1045. cnt += 19;
  1046. }
  1047. #if DEBUG
  1048. printk(OSST_DEB_MSG "%s:D: Bad frame detected, positioning tape to block %d\n",
  1049. name, position);
  1050. #endif
  1051. osst_set_frame_position(STp, aSRpnt, position, 0);
  1052. continue;
  1053. }
  1054. if (osst_verify_frame(STp, frame_seq_number, quiet))
  1055. break;
  1056. if (osst_verify_frame(STp, -1, quiet)) {
  1057. x = ntohl(STp->buffer->aux->frame_seq_num);
  1058. if (STp->fast_open) {
  1059. printk(KERN_WARNING
  1060. "%s:W: Found logical frame %d instead of %d after fast open\n",
  1061. name, x, frame_seq_number);
  1062. STp->header_ok = 0;
  1063. STp->read_error_frame = 0;
  1064. return (-EIO);
  1065. }
  1066. if (x > frame_seq_number) {
  1067. if (++past > 3) {
  1068. /* positioning backwards did not bring us to the desired frame */
  1069. position = STp->read_error_frame - 1;
  1070. }
  1071. else {
  1072. position = osst_get_frame_position(STp, aSRpnt)
  1073. + frame_seq_number - x - 1;
  1074. if (STp->first_frame_position >= 3000 && position < 3000)
  1075. position -= 10;
  1076. }
  1077. #if DEBUG
  1078. printk(OSST_DEB_MSG
  1079. "%s:D: Found logical frame %d while looking for %d: back up %d\n",
  1080. name, x, frame_seq_number,
  1081. STp->first_frame_position - position);
  1082. #endif
  1083. osst_set_frame_position(STp, aSRpnt, position, 0);
  1084. cnt += 10;
  1085. }
  1086. else
  1087. past = 0;
  1088. }
  1089. if (osst_get_frame_position(STp, aSRpnt) == 0xbaf) {
  1090. #if DEBUG
  1091. printk(OSST_DEB_MSG "%s:D: Skipping config partition\n", name);
  1092. #endif
  1093. osst_set_frame_position(STp, aSRpnt, 0xbb8, 0);
  1094. cnt--;
  1095. }
  1096. STp->frame_in_buffer = 0;
  1097. }
  1098. if (cnt > 1) {
  1099. STp->recover_count++;
  1100. STp->recover_erreg++;
  1101. printk(KERN_WARNING "%s:I: Don't worry, Read error at position %d recovered\n",
  1102. name, STp->read_error_frame);
  1103. }
  1104. STp->read_count++;
  1105. #if DEBUG
  1106. if (debugging || STps->eof)
  1107. printk(OSST_DEB_MSG
  1108. "%s:D: Exit get logical frame (%d=>%d) from OnStream tape with code %d\n",
  1109. name, frame_seq_number, STp->frame_seq_number, STps->eof);
  1110. #endif
  1111. STp->fast_open = 0;
  1112. STp->read_error_frame = 0;
  1113. return (STps->eof);
  1114. }
  1115. static int osst_seek_logical_blk(struct osst_tape * STp, struct osst_request ** aSRpnt, int logical_blk_num)
  1116. {
  1117. struct st_partstat * STps = &(STp->ps[STp->partition]);
  1118. char * name = tape_name(STp);
  1119. int retries = 0;
  1120. int frame_seq_estimate, ppos_estimate, move;
  1121. if (logical_blk_num < 0) logical_blk_num = 0;
  1122. #if DEBUG
  1123. printk(OSST_DEB_MSG "%s:D: Seeking logical block %d (now at %d, size %d%c)\n",
  1124. name, logical_blk_num, STp->logical_blk_num,
  1125. STp->block_size<1024?STp->block_size:STp->block_size/1024,
  1126. STp->block_size<1024?'b':'k');
  1127. #endif
  1128. /* Do we know where we are? */
  1129. if (STps->drv_block >= 0) {
  1130. move = logical_blk_num - STp->logical_blk_num;
  1131. if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
  1132. move /= (OS_DATA_SIZE / STp->block_size);
  1133. frame_seq_estimate = STp->frame_seq_number + move;
  1134. } else
  1135. frame_seq_estimate = logical_blk_num * STp->block_size / OS_DATA_SIZE;
  1136. if (frame_seq_estimate < 2980) ppos_estimate = frame_seq_estimate + 10;
  1137. else ppos_estimate = frame_seq_estimate + 20;
  1138. while (++retries < 10) {
  1139. if (ppos_estimate > STp->eod_frame_ppos-2) {
  1140. frame_seq_estimate += STp->eod_frame_ppos - 2 - ppos_estimate;
  1141. ppos_estimate = STp->eod_frame_ppos - 2;
  1142. }
  1143. if (frame_seq_estimate < 0) {
  1144. frame_seq_estimate = 0;
  1145. ppos_estimate = 10;
  1146. }
  1147. osst_set_frame_position(STp, aSRpnt, ppos_estimate, 0);
  1148. if (osst_get_logical_frame(STp, aSRpnt, frame_seq_estimate, 1) >= 0) {
  1149. /* we've located the estimated frame, now does it have our block? */
  1150. if (logical_blk_num < STp->logical_blk_num ||
  1151. logical_blk_num >= STp->logical_blk_num + ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt)) {
  1152. if (STps->eof == ST_FM_HIT)
  1153. move = logical_blk_num < STp->logical_blk_num? -2 : 1;
  1154. else {
  1155. move = logical_blk_num - STp->logical_blk_num;
  1156. if (move < 0) move -= (OS_DATA_SIZE / STp->block_size) - 1;
  1157. move /= (OS_DATA_SIZE / STp->block_size);
  1158. }
  1159. if (!move) move = logical_blk_num > STp->logical_blk_num ? 1 : -1;
  1160. #if DEBUG
  1161. printk(OSST_DEB_MSG
  1162. "%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d) move %d\n",
  1163. name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
  1164. STp->logical_blk_num, logical_blk_num, move);
  1165. #endif
  1166. frame_seq_estimate += move;
  1167. ppos_estimate += move;
  1168. continue;
  1169. } else {
  1170. STp->buffer->read_pointer = (logical_blk_num - STp->logical_blk_num) * STp->block_size;
  1171. STp->buffer->buffer_bytes -= STp->buffer->read_pointer;
  1172. STp->logical_blk_num = logical_blk_num;
  1173. #if DEBUG
  1174. printk(OSST_DEB_MSG
  1175. "%s:D: Seek success at ppos %d fsq %d in_buf %d, bytes %d, ptr %d*%d\n",
  1176. name, ppos_estimate, STp->frame_seq_number, STp->frame_in_buffer,
  1177. STp->buffer->buffer_bytes, STp->buffer->read_pointer / STp->block_size,
  1178. STp->block_size);
  1179. #endif
  1180. STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
  1181. if (STps->eof == ST_FM_HIT) {
  1182. STps->drv_file++;
  1183. STps->drv_block = 0;
  1184. } else {
  1185. STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
  1186. STp->logical_blk_num -
  1187. (STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
  1188. -1;
  1189. }
  1190. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
  1191. return 0;
  1192. }
  1193. }
  1194. if (osst_get_logical_frame(STp, aSRpnt, -1, 1) < 0)
  1195. goto error;
  1196. /* we are not yet at the estimated frame, adjust our estimate of its physical position */
  1197. #if DEBUG
  1198. printk(OSST_DEB_MSG "%s:D: Seek retry %d at ppos %d fsq %d (est %d) lbn %d (need %d)\n",
  1199. name, retries, ppos_estimate, STp->frame_seq_number, frame_seq_estimate,
  1200. STp->logical_blk_num, logical_blk_num);
  1201. #endif
  1202. if (frame_seq_estimate != STp->frame_seq_number)
  1203. ppos_estimate += frame_seq_estimate - STp->frame_seq_number;
  1204. else
  1205. break;
  1206. }
  1207. error:
  1208. printk(KERN_ERR "%s:E: Couldn't seek to logical block %d (at %d), %d retries\n",
  1209. name, logical_blk_num, STp->logical_blk_num, retries);
  1210. return (-EIO);
  1211. }
  1212. /* The values below are based on the OnStream frame payload size of 32K == 2**15,
  1213. * that is, OSST_FRAME_SHIFT + OSST_SECTOR_SHIFT must be 15. With a minimum block
  1214. * size of 512 bytes, we need to be able to resolve 32K/512 == 64 == 2**6 positions
  1215. * inside each frame. Finally, OSST_SECTOR_MASK == 2**OSST_FRAME_SHIFT - 1.
  1216. */
  1217. #define OSST_FRAME_SHIFT 6
  1218. #define OSST_SECTOR_SHIFT 9
  1219. #define OSST_SECTOR_MASK 0x03F
  1220. static int osst_get_sector(struct osst_tape * STp, struct osst_request ** aSRpnt)
  1221. {
  1222. int sector;
  1223. #if DEBUG
  1224. char * name = tape_name(STp);
  1225. printk(OSST_DEB_MSG
  1226. "%s:D: Positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, %cptr %d, eof %d\n",
  1227. name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
  1228. STp->ps[STp->partition].drv_file, STp->ps[STp->partition].drv_block,
  1229. STp->ps[STp->partition].rw == ST_WRITING?'w':'r',
  1230. STp->ps[STp->partition].rw == ST_WRITING?STp->buffer->buffer_bytes:
  1231. STp->buffer->read_pointer, STp->ps[STp->partition].eof);
  1232. #endif
  1233. /* do we know where we are inside a file? */
  1234. if (STp->ps[STp->partition].drv_block >= 0) {
  1235. sector = (STp->frame_in_buffer ? STp->first_frame_position-1 :
  1236. STp->first_frame_position) << OSST_FRAME_SHIFT;
  1237. if (STp->ps[STp->partition].rw == ST_WRITING)
  1238. sector |= (STp->buffer->buffer_bytes >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
  1239. else
  1240. sector |= (STp->buffer->read_pointer >> OSST_SECTOR_SHIFT) & OSST_SECTOR_MASK;
  1241. } else {
  1242. sector = osst_get_frame_position(STp, aSRpnt);
  1243. if (sector > 0)
  1244. sector <<= OSST_FRAME_SHIFT;
  1245. }
  1246. return sector;
  1247. }
  1248. static int osst_seek_sector(struct osst_tape * STp, struct osst_request ** aSRpnt, int sector)
  1249. {
  1250. struct st_partstat * STps = &(STp->ps[STp->partition]);
  1251. int frame = sector >> OSST_FRAME_SHIFT,
  1252. offset = (sector & OSST_SECTOR_MASK) << OSST_SECTOR_SHIFT,
  1253. r;
  1254. #if DEBUG
  1255. char * name = tape_name(STp);
  1256. printk(OSST_DEB_MSG "%s:D: Seeking sector %d in frame %d at offset %d\n",
  1257. name, sector, frame, offset);
  1258. #endif
  1259. if (frame < 0 || frame >= STp->capacity) return (-ENXIO);
  1260. if (frame <= STp->first_data_ppos) {
  1261. STp->frame_seq_number = STp->logical_blk_num = STps->drv_file = STps->drv_block = 0;
  1262. return (osst_set_frame_position(STp, aSRpnt, frame, 0));
  1263. }
  1264. r = osst_set_frame_position(STp, aSRpnt, offset?frame:frame-1, 0);
  1265. if (r < 0) return r;
  1266. r = osst_get_logical_frame(STp, aSRpnt, -1, 1);
  1267. if (r < 0) return r;
  1268. if (osst_get_frame_position(STp, aSRpnt) != (offset?frame+1:frame)) return (-EIO);
  1269. if (offset) {
  1270. STp->logical_blk_num += offset / STp->block_size;
  1271. STp->buffer->read_pointer = offset;
  1272. STp->buffer->buffer_bytes -= offset;
  1273. } else {
  1274. STp->frame_seq_number++;
  1275. STp->frame_in_buffer = 0;
  1276. STp->logical_blk_num += ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1277. STp->buffer->buffer_bytes = STp->buffer->read_pointer = 0;
  1278. }
  1279. STps->drv_file = ntohl(STp->buffer->aux->filemark_cnt);
  1280. if (STps->eof == ST_FM_HIT) {
  1281. STps->drv_file++;
  1282. STps->drv_block = 0;
  1283. } else {
  1284. STps->drv_block = ntohl(STp->buffer->aux->last_mark_lbn)?
  1285. STp->logical_blk_num -
  1286. (STps->drv_file ? ntohl(STp->buffer->aux->last_mark_lbn) + 1 : 0):
  1287. -1;
  1288. }
  1289. STps->eof = (STp->first_frame_position >= STp->eod_frame_ppos)?ST_EOD:ST_NOEOF;
  1290. #if DEBUG
  1291. printk(OSST_DEB_MSG
  1292. "%s:D: Now positioned at ppos %d, frame %d, lbn %d, file %d, blk %d, rptr %d, eof %d\n",
  1293. name, STp->first_frame_position, STp->frame_seq_number, STp->logical_blk_num,
  1294. STps->drv_file, STps->drv_block, STp->buffer->read_pointer, STps->eof);
  1295. #endif
  1296. return 0;
  1297. }
  1298. /*
  1299. * Read back the drive's internal buffer contents, as a part
  1300. * of the write error recovery mechanism for old OnStream
  1301. * firmware revisions.
  1302. * Precondition for this function to work: all frames in the
  1303. * drive's buffer must be of one type (DATA, MARK or EOD)!
  1304. */
  1305. static int osst_read_back_buffer_and_rewrite(struct osst_tape * STp, struct osst_request ** aSRpnt,
  1306. unsigned int frame, unsigned int skip, int pending)
  1307. {
  1308. struct osst_request * SRpnt = * aSRpnt;
  1309. unsigned char * buffer, * p;
  1310. unsigned char cmd[MAX_COMMAND_SIZE];
  1311. int flag, new_frame, i;
  1312. int nframes = STp->cur_frames;
  1313. int blks_per_frame = ntohs(STp->buffer->aux->dat.dat_list[0].blk_cnt);
  1314. int frame_seq_number = ntohl(STp->buffer->aux->frame_seq_num)
  1315. - (nframes + pending - 1);
  1316. int logical_blk_num = ntohl(STp->buffer->aux->logical_blk_num)
  1317. - (nframes + pending - 1) * blks_per_frame;
  1318. char * name = tape_name(STp);
  1319. unsigned long startwait = jiffies;
  1320. #if DEBUG
  1321. int dbg = debugging;
  1322. #endif
  1323. if ((buffer = vmalloc((nframes + 1) * OS_DATA_SIZE)) == NULL)
  1324. return (-EIO);
  1325. printk(KERN_INFO "%s:I: Reading back %d frames from drive buffer%s\n",
  1326. name, nframes, pending?" and one that was pending":"");
  1327. osst_copy_from_buffer(STp->buffer, (p = &buffer[nframes * OS_DATA_SIZE]));
  1328. #if DEBUG
  1329. if (pending && debugging)
  1330. printk(OSST_DEB_MSG "%s:D: Pending frame %d (lblk %d), data %02x %02x %02x %02x\n",
  1331. name, frame_seq_number + nframes,
  1332. logical_blk_num + nframes * blks_per_frame,
  1333. p[0], p[1], p[2], p[3]);
  1334. #endif
  1335. for (i = 0, p = buffer; i < nframes; i++, p += OS_DATA_SIZE) {
  1336. memset(cmd, 0, MAX_COMMAND_SIZE);
  1337. cmd[0] = 0x3C; /* Buffer Read */
  1338. cmd[1] = 6; /* Retrieve Faulty Block */
  1339. cmd[7] = 32768 >> 8;
  1340. cmd[8] = 32768 & 0xff;
  1341. SRpnt = osst_do_scsi(SRpnt, STp, cmd, OS_FRAME_SIZE, DMA_FROM_DEVICE,
  1342. STp->timeout, MAX_RETRIES, 1);
  1343. if ((STp->buffer)->syscall_result || !SRpnt) {
  1344. printk(KERN_ERR "%s:E: Failed to read frame back from OnStream buffer\n", name);
  1345. vfree(buffer);
  1346. *aSRpnt = SRpnt;
  1347. return (-EIO);
  1348. }
  1349. osst_copy_from_buffer(STp->buffer, p);
  1350. #if DEBUG
  1351. if (debugging)
  1352. printk(OSST_DEB_MSG "%s:D: Read back logical frame %d, data %02x %02x %02x %02x\n",
  1353. name, frame_seq_number + i, p[0], p[1], p[2], p[3]);
  1354. #endif
  1355. }
  1356. *aSRpnt = SRpnt;
  1357. osst_get_frame_position(STp, aSRpnt);
  1358. #if DEBUG
  1359. printk(OSST_DEB_MSG "%s:D: Frames left in buffer: %d\n", name, STp->cur_frames);
  1360. #endif
  1361. /* Write synchronously so we can be sure we're OK again and don't have to recover recursively */
  1362. /* In the header we don't actually re-write the frames that fail, just the ones after them */
  1363. for (flag=1, new_frame=frame, p=buffer, i=0; i < nframes + pending; ) {
  1364. if (flag) {
  1365. if (STp->write_type == OS_WRITE_HEADER) {
  1366. i += skip;
  1367. p += skip * OS_DATA_SIZE;
  1368. }
  1369. else if (new_frame < 2990 && new_frame+skip+nframes+pending >= 2990)
  1370. new_frame = 3000-i;
  1371. else
  1372. new_frame += skip;
  1373. #if DEBUG
  1374. printk(OSST_DEB_MSG "%s:D: Position to frame %d, write fseq %d\n",
  1375. name, new_frame+i, frame_seq_number+i);
  1376. #endif
  1377. osst_set_frame_position(STp, aSRpnt, new_frame + i, 0);
  1378. osst_wait_ready(STp, aSRpnt, 60, OSST_WAIT_POSITION_COMPLETE);
  1379. osst_get_frame_position(STp, aSRpnt);
  1380. SRpnt = * aSRpnt;
  1381. if (new_frame > frame + 1000) {
  1382. printk(KERN_ERR "%s:E: Failed to find writable tape media\n", nam