ide-tape.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:217k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. rq.cmd = IDETAPE_PC_RQ1;
  2. return ide_do_drive_cmd (drive, &rq, ide_wait);
  3. }
  4. static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
  5. {
  6. idetape_tape_t *tape = drive->driver_data;
  7. idetape_init_pc (pc);
  8. pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
  9. pc->c[4] = cmd;
  10. if (tape->onstream) {
  11. pc->c[1] = 1;
  12. if (cmd == !IDETAPE_LU_LOAD_MASK)
  13. pc->c[4] = 4;
  14. }
  15. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  16. pc->callback = &idetape_pc_callback;
  17. }
  18. static int idetape_wait_ready (ide_drive_t *drive, unsigned long long timeout)
  19. {
  20. idetape_tape_t *tape = drive->driver_data;
  21. idetape_pc_t pc;
  22. /*
  23.  * Wait for the tape to become ready
  24.  */
  25. timeout += jiffies;
  26. while (time_before(jiffies, timeout)) {
  27. idetape_create_test_unit_ready_cmd(&pc);
  28. if (!__idetape_queue_pc_tail(drive, &pc))
  29. return 0;
  30. if (tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) {
  31. idetape_create_load_unload_cmd (drive, &pc, IDETAPE_LU_LOAD_MASK);
  32. __idetape_queue_pc_tail(drive, &pc);
  33. idetape_create_test_unit_ready_cmd(&pc);
  34. if (!__idetape_queue_pc_tail(drive, &pc))
  35. return 0;
  36. }
  37. if (!(tape->sense_key == 2 && tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8)))
  38. break;
  39. current->state = TASK_INTERRUPTIBLE;
  40.    schedule_timeout(HZ / 10);
  41. }
  42. return -EIO;
  43. }
  44. static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
  45. {
  46. idetape_tape_t *tape = drive->driver_data;
  47. int rc;
  48. rc = __idetape_queue_pc_tail(drive, pc);
  49. if (rc)
  50. return rc;
  51. if (tape->onstream && test_bit(PC_WAIT_FOR_DSC, &pc->flags))
  52. rc = idetape_wait_ready(drive, 60 * 10 * HZ);   /* AJN-4: Changed from 5 to 10 minutes;
  53.                           because retension takes approx. 8:20 with Onstream 30GB tape */
  54. return rc;
  55. }
  56. static int idetape_flush_tape_buffers (ide_drive_t *drive)
  57. {
  58. idetape_pc_t pc;
  59. int rc;
  60. idetape_create_write_filemark_cmd(drive, &pc, 0);
  61. if ((rc = idetape_queue_pc_tail (drive, &pc)))
  62. return rc;
  63. idetape_wait_ready(drive, 60 * 5 * HZ);
  64. return 0;
  65. }
  66. static void idetape_create_read_position_cmd (idetape_pc_t *pc)
  67. {
  68. idetape_init_pc (pc);
  69. pc->c[0] = IDETAPE_READ_POSITION_CMD;
  70. pc->request_transfer = 20;
  71. pc->callback = &idetape_read_position_callback;
  72. }
  73. static int idetape_read_position (ide_drive_t *drive)
  74. {
  75. idetape_tape_t *tape = drive->driver_data;
  76. idetape_pc_t pc;
  77. int position;
  78. #if IDETAPE_DEBUG_LOG
  79.         if (tape->debug_level >= 4)
  80. printk (KERN_INFO "ide-tape: Reached idetape_read_positionn");
  81. #endif /* IDETAPE_DEBUG_LOG */
  82. #ifdef NO_LONGER_REQUIRED
  83. idetape_flush_tape_buffers(drive);
  84. #endif
  85. idetape_create_read_position_cmd(&pc);
  86. if (idetape_queue_pc_tail (drive, &pc))
  87. return -1;
  88. position = tape->first_frame_position;
  89. #ifdef NO_LONGER_REQUIRED
  90. if (tape->onstream) {
  91. if ((position != tape->last_frame_position - tape->blocks_in_buffer) &&
  92.     (position != tape->last_frame_position + tape->blocks_in_buffer)) {
  93. if (tape->blocks_in_buffer == 0) {
  94. printk("ide-tape: %s: correcting read position %d, %d, %dn", tape->name, position, tape->last_frame_position, tape->blocks_in_buffer);
  95. position = tape->last_frame_position;
  96. tape->first_frame_position = position;
  97. }
  98. }
  99. }
  100. #endif
  101. return position;
  102. }
  103. static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, byte partition, int skip)
  104. {
  105. idetape_tape_t *tape = drive->driver_data;
  106. idetape_init_pc (pc);
  107. pc->c[0] = IDETAPE_LOCATE_CMD;
  108. if (tape->onstream)
  109. pc->c[1] = 1; /* Immediate bit */
  110. else
  111. pc->c[1] = 2;
  112. put_unaligned (htonl (block), (unsigned int *) &pc->c[3]);
  113. pc->c[8] = partition;
  114. if (tape->onstream)
  115.                 /*
  116.                  * Set SKIP bit.
  117.                  * In case of write error this will write buffered
  118.                  * data in the drive to this new position!
  119.                  */
  120. pc->c[9] = skip << 7;
  121. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  122. pc->callback = &idetape_pc_callback;
  123. }
  124. static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
  125. {
  126. idetape_tape_t *tape = drive->driver_data;
  127. if (!tape->capabilities.lock)
  128. return 0;
  129. idetape_init_pc(pc);
  130. pc->c[0] = IDETAPE_PREVENT_CMD;
  131. pc->c[4] = prevent;
  132. pc->callback = &idetape_pc_callback;
  133. return 1;
  134. }
  135. static int __idetape_discard_read_pipeline (ide_drive_t *drive)
  136. {
  137. idetape_tape_t *tape = drive->driver_data;
  138. unsigned long flags;
  139. int cnt;
  140. if (tape->chrdev_direction != idetape_direction_read)
  141. return 0;
  142. tape->merge_stage_size = 0;
  143. if (tape->merge_stage != NULL) {
  144. __idetape_kfree_stage (tape->merge_stage);
  145. tape->merge_stage = NULL;
  146. }
  147. tape->chrdev_direction = idetape_direction_none;
  148. if (tape->first_stage == NULL)
  149. return 0;
  150. spin_lock_irqsave(&tape->spinlock, flags);
  151. tape->next_stage = NULL;
  152. if (idetape_pipeline_active (tape))
  153. idetape_wait_for_request(drive, tape->active_data_request);
  154. spin_unlock_irqrestore(&tape->spinlock, flags);
  155. cnt = tape->nr_stages - tape->nr_pending_stages;
  156. while (tape->first_stage != NULL)
  157. idetape_remove_stage_head (drive);
  158. tape->nr_pending_stages = 0;
  159. tape->max_stages = tape->min_pipeline;
  160. return cnt;
  161. }
  162. /*
  163.  * idetape_position_tape positions the tape to the requested block
  164.  * using the LOCATE packet command. A READ POSITION command is then
  165.  * issued to check where we are positioned.
  166.  *
  167.  * Like all higher level operations, we queue the commands at the tail
  168.  * of the request queue and wait for their completion.
  169.  *
  170.  */
  171. static int idetape_position_tape (ide_drive_t *drive, unsigned int block, byte partition, int skip)
  172. {
  173. idetape_tape_t *tape = drive->driver_data;
  174. int retval;
  175. idetape_pc_t pc;
  176. if (tape->chrdev_direction == idetape_direction_read)
  177. __idetape_discard_read_pipeline(drive);
  178. idetape_wait_ready(drive, 60 * 5 * HZ);
  179. idetape_create_locate_cmd (drive, &pc, block, partition, skip);
  180. retval = idetape_queue_pc_tail (drive, &pc);
  181. if (retval)
  182. return (retval);
  183. idetape_create_read_position_cmd (&pc);
  184. return (idetape_queue_pc_tail (drive, &pc));
  185. }
  186. static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
  187. {
  188. idetape_tape_t *tape = drive->driver_data;
  189. int cnt;
  190. int seek, position;
  191. cnt = __idetape_discard_read_pipeline(drive);
  192. if (restore_position) {
  193. position = idetape_read_position(drive);
  194. #if ONSTREAM_DEBUG
  195. if (tape->debug_level >= 2)
  196. printk(KERN_INFO "ide-tape: address %u, nr_stages %dn", position, cnt);
  197. #endif
  198. seek = position > cnt ? position - cnt : 0;
  199. if (idetape_position_tape(drive, seek, 0, 0)) {
  200. printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()n", tape->name);
  201. return;
  202. }
  203. }
  204. }
  205. static void idetape_update_stats (ide_drive_t *drive)
  206. {
  207. idetape_pc_t pc;
  208. idetape_create_mode_sense_cmd (&pc, IDETAPE_BUFFER_FILLING_PAGE);
  209. pc.callback = idetape_onstream_buffer_fill_callback;
  210. (void) idetape_queue_pc_tail(drive, &pc);
  211. }
  212. /*
  213.  * idetape_queue_rw_tail generates a read/write request for the block
  214.  * device interface and wait for it to be serviced.
  215.  */
  216. static int idetape_queue_rw_tail (ide_drive_t *drive, int cmd, int blocks, struct buffer_head *bh)
  217. {
  218. idetape_tape_t *tape = drive->driver_data;
  219. struct request rq;
  220. #if IDETAPE_DEBUG_LOG
  221. if (tape->debug_level >= 2)
  222. printk (KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%dn",cmd);
  223. #endif /* IDETAPE_DEBUG_LOG */
  224. #if IDETAPE_DEBUG_BUGS
  225. if (idetape_pipeline_active (tape)) {
  226. printk (KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tailn");
  227. return (0);
  228. }
  229. #endif /* IDETAPE_DEBUG_BUGS */
  230. ide_init_drive_cmd (&rq);
  231. rq.bh = bh;
  232. rq.cmd = cmd;
  233. rq.sector = tape->first_frame_position;
  234. rq.nr_sectors = rq.current_nr_sectors = blocks;
  235. if (tape->onstream)
  236. tape->postpone_cnt = 600;
  237. (void) ide_do_drive_cmd (drive, &rq, ide_wait);
  238. if (cmd != IDETAPE_READ_RQ && cmd != IDETAPE_WRITE_RQ)
  239. return 0;
  240. if (tape->merge_stage)
  241. idetape_init_merge_stage (tape);
  242. if (rq.errors == IDETAPE_ERROR_GENERAL)
  243. return -EIO;
  244. return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
  245. }
  246. /*
  247.  * Read back the drive's internal buffer contents, as a part
  248.  * of the write error recovery mechanism for old OnStream
  249.  * firmware revisions.
  250.  */
  251. static void idetape_onstream_read_back_buffer (ide_drive_t *drive)
  252. {
  253. idetape_tape_t *tape = drive->driver_data;
  254. int frames, i, logical_blk_num;
  255. idetape_stage_t *stage, *first = NULL, *last = NULL;
  256. os_aux_t *aux;
  257. struct request *rq;
  258. unsigned char *p;
  259. unsigned long flags;
  260. idetape_update_stats(drive);
  261. frames = tape->cur_frames;
  262. logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num) - frames;
  263. printk(KERN_INFO "ide-tape: %s: reading back %d frames from the drive's internal buffern", tape->name, frames);
  264. for (i = 0; i < frames; i++) {
  265. stage = __idetape_kmalloc_stage(tape, 0, 0);
  266. if (!first)
  267. first = stage;
  268. aux = stage->aux;
  269. p = stage->bh->b_data;
  270. idetape_queue_rw_tail(drive, IDETAPE_READ_BUFFER_RQ, tape->capabilities.ctl, stage->bh);
  271. #if ONSTREAM_DEBUG
  272. if (tape->debug_level >= 2)
  273. printk(KERN_INFO "ide-tape: %s: read back logical block %d, data %x %x %x %xn", tape->name, logical_blk_num, *p++, *p++, *p++, *p++);
  274. #endif
  275. rq = &stage->rq;
  276. ide_init_drive_cmd (rq);
  277. rq->cmd = IDETAPE_WRITE_RQ;
  278. rq->sector = tape->first_frame_position;
  279. rq->nr_sectors = rq->current_nr_sectors = tape->capabilities.ctl;
  280. idetape_init_stage(drive, stage, OS_FRAME_TYPE_DATA, logical_blk_num++);
  281. stage->next = NULL;
  282. if (last)
  283. last->next = stage;
  284. last = stage;
  285. }
  286. if (frames) {
  287. spin_lock_irqsave(&tape->spinlock, flags);
  288. last->next = tape->first_stage;
  289. tape->next_stage = tape->first_stage = first;
  290. tape->nr_stages += frames;
  291. tape->nr_pending_stages += frames;
  292. spin_unlock_irqrestore(&tape->spinlock, flags);
  293. }
  294. idetape_update_stats(drive);
  295. #if ONSTREAM_DEBUG
  296. if (tape->debug_level >= 2)
  297. printk(KERN_INFO "ide-tape: %s: frames left in buffer: %dn", tape->name, tape->cur_frames);
  298. #endif
  299. }
  300. /*
  301.  * Error recovery algorithm for the OnStream tape.
  302.  */
  303. static void idetape_onstream_write_error_recovery (ide_drive_t *drive)
  304. {
  305. idetape_tape_t *tape = drive->driver_data;
  306. unsigned int block;
  307. if (tape->onstream_write_error == OS_WRITE_ERROR) {
  308. printk(KERN_ERR "ide-tape: %s: onstream_write_error_recovery: detected physical bad block at %u, logical %u first frame %u last_frame %u bufblocks %u stages %u skipping %u framesn",
  309. tape->name, ntohl(tape->sense.information), tape->logical_blk_num,
  310. tape->first_frame_position, tape->last_frame_position,
  311. tape->blocks_in_buffer, tape->nr_stages,
  312.   (ntohl(tape->sense.command_specific) >> 16) & 0xff );
  313. block = ntohl(tape->sense.information) + ((ntohl(tape->sense.command_specific) >> 16) & 0xff);
  314. idetape_update_stats(drive);
  315. printk(KERN_ERR "ide-tape: %s: relocating %d buffered logical blocks to physical block %un", tape->name, tape->cur_frames, block);
  316. #if 0  /* isn't once enough ??? MM */
  317. idetape_update_stats(drive);
  318. #endif
  319. if (tape->firmware_revision_num >= 106)
  320. idetape_position_tape(drive, block, 0, 1);
  321. else {
  322. idetape_onstream_read_back_buffer(drive);
  323. idetape_position_tape(drive, block, 0, 0);
  324. }
  325. #if 0     /* already done in idetape_position_tape MM */
  326. idetape_read_position(drive);
  327. #endif
  328. #if ONSTREAM_DEBUG
  329. if (tape->debug_level >= 1)
  330. printk(KERN_ERR "ide-tape: %s: positioning complete, cur_frames %d, pos %d, tape pos %dn", tape->name, tape->cur_frames, tape->first_frame_position, tape->last_frame_position);
  331. #endif
  332. } else if (tape->onstream_write_error == OS_PART_ERROR) {
  333. #if ONSTREAM_DEBUG
  334. if (tape->debug_level >= 1)
  335. printk(KERN_INFO "ide-tape: %s: skipping over config partitionn", tape->name);
  336. #endif
  337. idetape_flush_tape_buffers(drive);
  338. block = idetape_read_position(drive);
  339. if (block != OS_DATA_ENDFRAME1)  
  340. printk(KERN_ERR "ide-tape: warning, current position %d, expected %dn", block, OS_DATA_ENDFRAME1);
  341. idetape_position_tape(drive, 0xbb8, 0, 0); /* 3000 */
  342. }
  343. tape->onstream_write_error = 0;
  344. }
  345. /*
  346.  * idetape_insert_pipeline_into_queue is used to start servicing the
  347.  * pipeline stages, starting from tape->next_stage.
  348.  */
  349. static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
  350. {
  351. idetape_tape_t *tape = drive->driver_data;
  352. if (tape->next_stage == NULL)
  353. return;
  354. if (!idetape_pipeline_active (tape)) {
  355. if (tape->onstream_write_error)
  356. idetape_onstream_write_error_recovery(drive);
  357. set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  358. idetape_active_next_stage (drive);
  359. (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
  360. }
  361. }
  362. static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
  363. {
  364. idetape_init_pc(pc);
  365. pc->c[0] = IDETAPE_INQUIRY_CMD;
  366. pc->c[4] = pc->request_transfer = 254;
  367. pc->callback = &idetape_pc_callback;
  368. }
  369. static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
  370. {
  371. idetape_tape_t *tape = drive->driver_data;
  372. idetape_init_pc (pc);
  373. pc->c[0] = IDETAPE_REWIND_CMD;
  374. if (tape->onstream)
  375. pc->c[1] = 1;
  376. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  377. pc->callback = &idetape_pc_callback;
  378. }
  379. static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
  380. {
  381. idetape_init_pc (pc);
  382. set_bit (PC_WRITING, &pc->flags);
  383. pc->c[0] = IDETAPE_MODE_SELECT_CMD;
  384. pc->c[1] = 0x10;
  385. put_unaligned (htons(length), (unsigned short *) &pc->c[3]);
  386. pc->request_transfer = 255;
  387. pc->callback = &idetape_pc_callback;
  388. }
  389. static void idetape_create_erase_cmd (idetape_pc_t *pc)
  390. {
  391. idetape_init_pc (pc);
  392. pc->c[0] = IDETAPE_ERASE_CMD;
  393. pc->c[1] = 1;
  394. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  395. pc->callback = &idetape_pc_callback;
  396. }
  397. static void idetape_create_space_cmd (idetape_pc_t *pc,int count,byte cmd)
  398. {
  399. idetape_init_pc (pc);
  400. pc->c[0] = IDETAPE_SPACE_CMD;
  401. put_unaligned (htonl (count), (unsigned int *) &pc->c[1]);
  402. pc->c[1] = cmd;
  403. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  404. pc->callback = &idetape_pc_callback;
  405. }
  406. /*
  407.  * Verify that we have the correct tape frame
  408.  */
  409. static int idetape_verify_stage (ide_drive_t *drive, idetape_stage_t *stage, int logical_blk_num, int quiet)
  410. {
  411. idetape_tape_t *tape = drive->driver_data;
  412. os_aux_t *aux = stage->aux;
  413. os_partition_t *par = &aux->partition;
  414. struct request *rq = &stage->rq;
  415. struct buffer_head *bh;
  416. if (!tape->onstream)
  417. return 1;
  418. if (tape->raw) {
  419. if (rq->errors) {
  420. bh = stage->bh;
  421. while (bh) {
  422. memset(bh->b_data, 0, bh->b_size);
  423. bh = bh->b_reqnext;
  424. }
  425. strcpy(stage->bh->b_data, "READ ERROR ON FRAME");
  426. }
  427. return 1;
  428. }
  429. if (rq->errors == IDETAPE_ERROR_GENERAL) {
  430. printk(KERN_INFO "ide-tape: %s: skipping frame %d, read errorn", tape->name, tape->first_frame_position);
  431. return 0;
  432. }
  433. if (rq->errors == IDETAPE_ERROR_EOD) {
  434. printk(KERN_INFO "ide-tape: %s: skipping frame %d, eodn", tape->name, tape->first_frame_position);
  435. return 0;
  436. }
  437. if (ntohl(aux->format_id) != 0) {
  438. printk(KERN_INFO "ide-tape: %s: skipping frame %d, format_id %un", tape->name, tape->first_frame_position, ntohl(aux->format_id));
  439. return 0;
  440. }
  441. if (memcmp(aux->application_sig, tape->application_sig, 4) != 0) {
  442. printk(KERN_INFO "ide-tape: %s: skipping frame %d, incorrect application signaturen", tape->name, tape->first_frame_position);
  443. return 0;
  444. }
  445. if (aux->frame_type != OS_FRAME_TYPE_DATA &&
  446.     aux->frame_type != OS_FRAME_TYPE_EOD &&
  447.     aux->frame_type != OS_FRAME_TYPE_MARKER) {
  448. printk(KERN_INFO "ide-tape: %s: skipping frame %d, frame type %xn", tape->name, tape->first_frame_position, aux->frame_type);
  449. return 0;
  450. }
  451. if (par->partition_num != OS_DATA_PARTITION) {
  452. if (!tape->linux_media || tape->linux_media_version != 2) {
  453. printk(KERN_INFO "ide-tape: %s: skipping frame %d, partition num %dn", tape->name, tape->first_frame_position, par->partition_num);
  454. return 0;
  455. }
  456. }
  457. if (par->par_desc_ver != OS_PARTITION_VERSION) {
  458. printk(KERN_INFO "ide-tape: %s: skipping frame %d, partition version %dn", tape->name, tape->first_frame_position, par->par_desc_ver);
  459. return 0;
  460. }
  461. if (ntohs(par->wrt_pass_cntr) != tape->wrt_pass_cntr) {
  462. printk(KERN_INFO "ide-tape: %s: skipping frame %d, wrt_pass_cntr %d (expected %d)(logical_blk_num %u)n", tape->name, tape->first_frame_position, ntohs(par->wrt_pass_cntr), tape->wrt_pass_cntr, ntohl(aux->logical_blk_num));
  463. return 0;
  464. }
  465. if (aux->frame_seq_num != aux->logical_blk_num) {
  466. printk(KERN_INFO "ide-tape: %s: skipping frame %d, seq != logicaln", tape->name, tape->first_frame_position);
  467. return 0;
  468. }
  469. if (logical_blk_num != -1 && ntohl(aux->logical_blk_num) != logical_blk_num) {
  470. if (!quiet)
  471. printk(KERN_INFO "ide-tape: %s: skipping frame %d, logical_blk_num %u (expected %d)n", tape->name, tape->first_frame_position, ntohl(aux->logical_blk_num), logical_blk_num);
  472. return 0;
  473. }
  474. if (aux->frame_type == OS_FRAME_TYPE_MARKER) {
  475. rq->errors = IDETAPE_ERROR_FILEMARK;
  476. rq->current_nr_sectors = rq->nr_sectors;
  477. }
  478. return 1;
  479. }
  480. static void idetape_wait_first_stage (ide_drive_t *drive)
  481. {
  482. idetape_tape_t *tape = drive->driver_data;
  483. unsigned long flags;
  484. if (tape->first_stage == NULL)
  485. return;
  486. spin_lock_irqsave(&tape->spinlock, flags);
  487. if (tape->active_stage == tape->first_stage)
  488. idetape_wait_for_request(drive, tape->active_data_request);
  489. spin_unlock_irqrestore(&tape->spinlock, flags);
  490. }
  491. /*
  492.  * idetape_add_chrdev_write_request tries to add a character device
  493.  * originated write request to our pipeline. In case we don't succeed,
  494.  * we revert to non-pipelined operation mode for this request.
  495.  *
  496.  * 1. Try to allocate a new pipeline stage.
  497.  * 2. If we can't, wait for more and more requests to be serviced
  498.  * and try again each time.
  499.  * 3. If we still can't allocate a stage, fallback to
  500.  * non-pipelined operation mode for this request.
  501.  */
  502. static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
  503. {
  504. idetape_tape_t *tape = drive->driver_data;
  505. idetape_stage_t *new_stage;
  506. unsigned long flags;
  507. struct request *rq;
  508. #if IDETAPE_DEBUG_LOG
  509. if (tape->debug_level >= 3)
  510. printk (KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_requestn");
  511. #endif /* IDETAPE_DEBUG_LOG */
  512.       /*
  513.        * Attempt to allocate a new stage.
  514.  * Pay special attention to possible race conditions.
  515.  */
  516. while ((new_stage = idetape_kmalloc_stage (tape)) == NULL) {
  517. spin_lock_irqsave(&tape->spinlock, flags);
  518. if (idetape_pipeline_active (tape)) {
  519. idetape_wait_for_request(drive, tape->active_data_request);
  520. spin_unlock_irqrestore(&tape->spinlock, flags);
  521. } else {
  522. spin_unlock_irqrestore(&tape->spinlock, flags);
  523. idetape_insert_pipeline_into_queue (drive);
  524. if (idetape_pipeline_active (tape))
  525. continue;
  526. /*
  527.  * Linux is short on memory. Fallback to
  528.  * non-pipelined operation mode for this request.
  529.  */
  530. return idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
  531. }
  532. }
  533. rq = &new_stage->rq;
  534. ide_init_drive_cmd (rq);
  535. rq->cmd = IDETAPE_WRITE_RQ;
  536. rq->sector = tape->first_frame_position; /* Doesn't actually matter - We always assume sequential access */
  537. rq->nr_sectors = rq->current_nr_sectors = blocks;
  538. idetape_switch_buffers (tape, new_stage);
  539. idetape_init_stage(drive, new_stage, OS_FRAME_TYPE_DATA, tape->logical_blk_num);
  540. tape->logical_blk_num++;
  541. idetape_add_stage_tail (drive, new_stage);
  542. tape->pipeline_head++;
  543. #if USE_IOTRACE
  544. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  545. #endif
  546. calculate_speeds(drive);
  547. /*
  548.  * Estimate whether the tape has stopped writing by checking
  549.  * if our write pipeline is currently empty. If we are not
  550.  * writing anymore, wait for the pipeline to be full enough
  551.  * (90%) before starting to service requests, so that we will
  552.  * be able to keep up with the higher speeds of the tape.
  553.  *
  554.  * For the OnStream drive, we can query the number of pending
  555.  * frames in the drive's internal buffer. As long as the tape
  556.  * is still writing, it is better to write frames immediately
  557.  * rather than gather them in the pipeline. This will give the
  558.  * tape's firmware the ability to sense the current incoming
  559.  * data rate more accurately, and since the OnStream tape
  560.  * supports variable speeds, it can try to adjust itself to the
  561.  * incoming data rate.
  562.  */
  563. if (!idetape_pipeline_active(tape)) {
  564. if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
  565.     tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
  566. tape->measure_insert_time = 1;
  567. tape->insert_time = jiffies;
  568. tape->insert_size = 0;
  569. tape->insert_speed = 0;
  570. idetape_insert_pipeline_into_queue (drive);
  571. } else if (tape->onstream) {
  572. idetape_update_stats(drive);
  573. if (tape->cur_frames > 5)
  574. idetape_insert_pipeline_into_queue (drive);
  575. }
  576. }
  577. if (test_and_clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags)) /* Return a deferred error */
  578. return -EIO;
  579. return blocks;
  580. }
  581. /*
  582.  * idetape_wait_for_pipeline will wait until all pending pipeline
  583.  * requests are serviced. Typically called on device close.
  584.  */
  585. static void idetape_wait_for_pipeline (ide_drive_t *drive)
  586. {
  587. idetape_tape_t *tape = drive->driver_data;
  588. unsigned long flags;
  589. while (tape->next_stage || idetape_pipeline_active(tape)) {
  590. idetape_insert_pipeline_into_queue (drive);
  591. spin_lock_irqsave(&tape->spinlock, flags);
  592. if (idetape_pipeline_active(tape))
  593. idetape_wait_for_request(drive, tape->active_data_request);
  594. spin_unlock_irqrestore(&tape->spinlock, flags);
  595. }
  596. }
  597. static void idetape_empty_write_pipeline (ide_drive_t *drive)
  598. {
  599. idetape_tape_t *tape = drive->driver_data;
  600. int blocks, i, min;
  601. struct buffer_head *bh;
  602. #if IDETAPE_DEBUG_BUGS
  603. if (tape->chrdev_direction != idetape_direction_write) {
  604. printk (KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.n");
  605. return;
  606. }
  607. if (tape->merge_stage_size > tape->stage_size) {
  608. printk (KERN_ERR "ide-tape: bug: merge_buffer too bign");
  609. tape->merge_stage_size = tape->stage_size;
  610. }
  611. #endif /* IDETAPE_DEBUG_BUGS */
  612. if (tape->merge_stage_size) {
  613. blocks = tape->merge_stage_size / tape->tape_block_size;
  614. if (tape->merge_stage_size % tape->tape_block_size) {
  615. blocks++;
  616. i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
  617. bh = tape->bh->b_reqnext;
  618. while (bh) {
  619. atomic_set(&bh->b_count, 0);
  620. bh = bh->b_reqnext;
  621. }
  622. bh = tape->bh;
  623. while (i) {
  624. if (bh == NULL) {
  625. printk(KERN_INFO "ide-tape: bug, bh NULLn");
  626. break;
  627. }
  628. min = IDE_MIN(i, bh->b_size - atomic_read(&bh->b_count));
  629. memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
  630. atomic_add(min, &bh->b_count);
  631. i -= min;
  632. bh = bh->b_reqnext;
  633. }
  634. }
  635. (void) idetape_add_chrdev_write_request (drive, blocks);
  636. tape->merge_stage_size = 0;
  637. }
  638. idetape_wait_for_pipeline (drive);
  639. if (tape->merge_stage != NULL) {
  640. __idetape_kfree_stage (tape->merge_stage);
  641. tape->merge_stage = NULL;
  642. }
  643. clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
  644. tape->chrdev_direction = idetape_direction_none;
  645. /*
  646.  * On the next backup, perform the feedback loop again.
  647.  * (I don't want to keep sense information between backups,
  648.  *  as some systems are constantly on, and the system load
  649.  *  can be totally different on the next backup).
  650.  */
  651. tape->max_stages = tape->min_pipeline;
  652. #if IDETAPE_DEBUG_BUGS
  653. if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->nr_stages != 0) {
  654. printk (KERN_ERR "ide-tape: ide-tape pipeline bug, "
  655. "first_stage %p, next_stage %p, last_stage %p, nr_stages %dn",
  656. tape->first_stage, tape->next_stage, tape->last_stage, tape->nr_stages);
  657. }
  658. #endif /* IDETAPE_DEBUG_BUGS */
  659. }
  660. static void idetape_restart_speed_control (ide_drive_t *drive)
  661. {
  662. idetape_tape_t *tape = drive->driver_data;
  663. tape->restart_speed_control_req = 0;
  664. tape->pipeline_head = 0;
  665. tape->buffer_head = tape->tape_head = tape->cur_frames;
  666. tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
  667. tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
  668. tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
  669. tape->uncontrolled_pipeline_head_speed = 0;
  670. tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
  671. tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
  672. }
  673. static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
  674. {
  675. idetape_tape_t *tape = drive->driver_data;
  676. idetape_stage_t *new_stage;
  677. struct request rq;
  678. int bytes_read;
  679. int blocks = tape->capabilities.ctl;
  680. if (tape->chrdev_direction != idetape_direction_read) { /* Initialize read operation */
  681. if (tape->chrdev_direction == idetape_direction_write) {
  682. idetape_empty_write_pipeline (drive);
  683. idetape_flush_tape_buffers (drive);
  684. }
  685. #if IDETAPE_DEBUG_BUGS
  686. if (tape->merge_stage || tape->merge_stage_size) {
  687. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 nown");
  688. tape->merge_stage_size = 0;
  689. }
  690. #endif /* IDETAPE_DEBUG_BUGS */
  691. if ((tape->merge_stage = __idetape_kmalloc_stage (tape, 0, 0)) == NULL)
  692. return -ENOMEM;
  693. tape->chrdev_direction = idetape_direction_read;
  694. tape->logical_blk_num = 0;
  695. /*
  696.  * Issue a read 0 command to ensure that DSC handshake
  697.  * is switched from completion mode to buffer available
  698.  * mode.
  699.  */
  700. bytes_read = idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 0, tape->merge_stage->bh);
  701. if (bytes_read < 0) {
  702. kfree (tape->merge_stage);
  703. tape->merge_stage = NULL;
  704. tape->chrdev_direction = idetape_direction_none;
  705. return bytes_read;
  706. }
  707. }
  708. if (tape->restart_speed_control_req)
  709. idetape_restart_speed_control(drive);
  710. ide_init_drive_cmd (&rq);
  711. rq.cmd = IDETAPE_READ_RQ;
  712. rq.sector = tape->first_frame_position;
  713. rq.nr_sectors = rq.current_nr_sectors = blocks;
  714. if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) && tape->nr_stages <= max_stages) {
  715. new_stage = idetape_kmalloc_stage (tape);
  716. while (new_stage != NULL) {
  717. new_stage->rq = rq;
  718. idetape_add_stage_tail (drive, new_stage);
  719. if (tape->nr_stages >= max_stages)
  720. break;
  721. new_stage = idetape_kmalloc_stage (tape);
  722. }
  723. }
  724. if (!idetape_pipeline_active(tape)) {
  725. if (tape->nr_pending_stages >= 3 * max_stages / 4) {
  726. tape->measure_insert_time = 1;
  727. tape->insert_time = jiffies;
  728. tape->insert_size = 0;
  729. tape->insert_speed = 0;
  730. idetape_insert_pipeline_into_queue (drive);
  731. } else if (tape->onstream) {
  732. idetape_update_stats(drive);
  733. if (tape->cur_frames < tape->max_frames - 5)
  734. idetape_insert_pipeline_into_queue (drive);
  735. }
  736. }
  737. return 0;
  738. }
  739. static int idetape_get_logical_blk (ide_drive_t *drive, int logical_blk_num, int max_stages, int quiet)
  740. {
  741. idetape_tape_t *tape = drive->driver_data;
  742. unsigned long flags;
  743. int cnt = 0, x, position;
  744. /*
  745.  * Search and wait for the next logical tape block
  746.  */
  747. while (1) {
  748. if (cnt++ > 1000) {   /* AJN: was 100 */
  749. printk(KERN_INFO "ide-tape: %s: couldn't find logical block %d, abortingn", tape->name, logical_blk_num);
  750. return 0;
  751. }
  752. idetape_initiate_read(drive, max_stages);
  753. if (tape->first_stage == NULL) {
  754. if (tape->onstream) {
  755. #if ONSTREAM_DEBUG
  756. if (tape->debug_level >= 1)
  757. printk(KERN_INFO "ide-tape: %s: first_stage == NULL, pipeline error %ldn", tape->name, (long)test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags));
  758. #endif
  759. clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
  760. position = idetape_read_position(drive);
  761. printk(KERN_INFO "ide-tape: %s: blank block detected at %dn", tape->name, position);
  762. if (position >= 3000 && position < 3080)
  763. position += 32;  /* Why is this check and number ??? MM */
  764. if (position >= OS_DATA_ENDFRAME1 && position < 3000)
  765. position = 3000;
  766. else
  767. /*
  768.  * compensate for write errors that generally skip 80 frames,
  769.  * expect around 20 read errors in a row...
  770.  */
  771. position += 60;
  772. if (position >= OS_DATA_ENDFRAME1 && position < 3000)
  773. position = 3000;
  774. printk(KERN_INFO "ide-tape: %s: positioning tape to block %dn", tape->name, position);
  775. if (position == 3000)  /* seems to be needed to correctly position at block 3000 MM */
  776. idetape_position_tape(drive, 0, 0, 0);
  777. idetape_position_tape(drive, position, 0, 0);
  778. cnt += 40;
  779. continue;
  780. } else
  781. return 0;
  782. }
  783. idetape_wait_first_stage(drive);
  784. if (idetape_verify_stage(drive, tape->first_stage, logical_blk_num, quiet))
  785. break;
  786. if (tape->first_stage->rq.errors == IDETAPE_ERROR_EOD)
  787. cnt--;
  788. if (idetape_verify_stage(drive, tape->first_stage, -1, quiet)) {
  789. x = ntohl(tape->first_stage->aux->logical_blk_num);
  790. if (x > logical_blk_num) {
  791. printk(KERN_ERR "ide-tape: %s: couldn't find logical block %d, aborting (block %d found)n", tape->name, logical_blk_num, x);
  792. return 0;
  793. }
  794. }
  795. spin_lock_irqsave(&tape->spinlock, flags);
  796. idetape_remove_stage_head(drive);
  797. spin_unlock_irqrestore(&tape->spinlock, flags);
  798. }
  799. if (tape->onstream)
  800. tape->logical_blk_num = ntohl(tape->first_stage->aux->logical_blk_num);
  801. return 1;
  802. }
  803. /*
  804.  * idetape_add_chrdev_read_request is called from idetape_chrdev_read
  805.  * to service a character device read request and add read-ahead
  806.  * requests to our pipeline.
  807.  */
  808. static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
  809. {
  810. idetape_tape_t *tape = drive->driver_data;
  811. unsigned long flags;
  812. struct request *rq_ptr;
  813. int bytes_read;
  814. #if IDETAPE_DEBUG_LOG
  815. if (tape->debug_level >= 4)
  816. printk (KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocksn", blocks);
  817. #endif /* IDETAPE_DEBUG_LOG */
  818. /*
  819.  * Wait for the next logical block to be available at the head
  820.  * of the pipeline
  821.  */
  822. if (!idetape_get_logical_blk(drive, tape->logical_blk_num, tape->max_stages, 0)) {
  823. if (tape->onstream) {
  824. set_bit(IDETAPE_READ_ERROR, &tape->flags);
  825. return 0;
  826. }
  827. if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
  828.   return 0;
  829. return idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, blocks, tape->merge_stage->bh);
  830. }
  831. rq_ptr = &tape->first_stage->rq;
  832. bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
  833. rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
  834. if (tape->onstream && !tape->raw && tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
  835. #if ONSTREAM_DEBUG
  836. if (tape->debug_level >= 2)
  837. printk(KERN_INFO "ide-tape: %s: EOD reachedn", tape->name);
  838. #endif
  839. return 0;
  840. }
  841. if (rq_ptr->errors == IDETAPE_ERROR_EOD)
  842. return 0;
  843. if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK) {
  844. idetape_switch_buffers (tape, tape->first_stage);
  845. set_bit (IDETAPE_FILEMARK, &tape->flags);
  846. #if USE_IOTRACE
  847. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  848. #endif
  849. calculate_speeds(drive);
  850. } else {
  851. idetape_switch_buffers (tape, tape->first_stage);
  852. if (rq_ptr->errors == IDETAPE_ERROR_GENERAL) {
  853. #if ONSTREAM_DEBUG
  854. if (tape->debug_level >= 1)
  855. printk(KERN_INFO "ide-tape: error detected, bytes_read %dn", bytes_read);
  856. #endif
  857. }
  858. clear_bit (IDETAPE_FILEMARK, &tape->flags);
  859. spin_lock_irqsave(&tape->spinlock, flags);
  860. idetape_remove_stage_head (drive);
  861. spin_unlock_irqrestore(&tape->spinlock, flags);
  862. tape->logical_blk_num++;
  863. tape->pipeline_head++;
  864. #if USE_IOTRACE
  865. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  866. #endif
  867. calculate_speeds(drive);
  868. }
  869. #if IDETAPE_DEBUG_BUGS
  870. if (bytes_read > blocks*tape->tape_block_size) {
  871. printk (KERN_ERR "ide-tape: bug: trying to return more bytes than requestedn");
  872. bytes_read=blocks*tape->tape_block_size;
  873. }
  874. #endif /* IDETAPE_DEBUG_BUGS */
  875. return (bytes_read);
  876. }
  877. static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
  878. {
  879. idetape_tape_t *tape = drive->driver_data;
  880. struct buffer_head *bh;
  881. int count, blocks;
  882. while (bcount) {
  883. bh = tape->merge_stage->bh;
  884. count = IDE_MIN (tape->stage_size, bcount);
  885. bcount -= count;
  886. blocks = count / tape->tape_block_size;
  887. while (count) {
  888. atomic_set(&bh->b_count, IDE_MIN (count, bh->b_size));
  889. memset (bh->b_data, 0, atomic_read(&bh->b_count));
  890. count -= atomic_read(&bh->b_count);
  891. bh = bh->b_reqnext;
  892. }
  893. idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, blocks, tape->merge_stage->bh);
  894. }
  895. }
  896. static int idetape_pipeline_size (ide_drive_t *drive)
  897. {
  898. idetape_tape_t *tape = drive->driver_data;
  899. idetape_stage_t *stage;
  900. struct request *rq;
  901. int size = 0;
  902. idetape_wait_for_pipeline (drive);
  903. stage = tape->first_stage;
  904. while (stage != NULL) {
  905. rq = &stage->rq;
  906. size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
  907. if (rq->errors == IDETAPE_ERROR_FILEMARK)
  908. size += tape->tape_block_size;
  909. stage = stage->next;
  910. }
  911. size += tape->merge_stage_size;
  912. return size;
  913. }
  914. /*
  915.  * Rewinds the tape to the Beginning Of the current Partition (BOP).
  916.  *
  917.  * We currently support only one partition.
  918.  */ 
  919. static int idetape_rewind_tape (ide_drive_t *drive)
  920. {
  921. int retval;
  922. idetape_pc_t pc;
  923. idetape_tape_t *tape = drive->driver_data;
  924. #if IDETAPE_DEBUG_LOG
  925. if (tape->debug_level >= 2)
  926. printk (KERN_INFO "ide-tape: Reached idetape_rewind_tapen");
  927. #endif /* IDETAPE_DEBUG_LOG */
  928. idetape_create_rewind_cmd (drive, &pc);
  929. retval = idetape_queue_pc_tail (drive, &pc);
  930. if (retval)
  931. return retval;
  932. idetape_create_read_position_cmd (&pc);
  933. retval = idetape_queue_pc_tail (drive, &pc);
  934. if (retval)
  935. return retval;
  936. tape->logical_blk_num = 0;
  937. return 0;
  938. }
  939. /*
  940.  * Our special ide-tape ioctl's.
  941.  *
  942.  * Currently there aren't any ioctl's.
  943.  * mtio.h compatible commands should be issued to the character device
  944.  * interface.
  945.  */
  946. static int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
  947.  unsigned int cmd, unsigned long arg)
  948. {
  949. idetape_tape_t *tape = drive->driver_data;
  950. idetape_config_t config;
  951. #if IDETAPE_DEBUG_LOG
  952. if (tape->debug_level >= 4)
  953. printk (KERN_INFO "ide-tape: Reached idetape_blkdev_ioctln");
  954. #endif /* IDETAPE_DEBUG_LOG */
  955. switch (cmd) {
  956. case 0x0340:
  957. if (copy_from_user ((char *) &config, (char *) arg, sizeof (idetape_config_t)))
  958. return -EFAULT;
  959. tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
  960. tape->max_stages = config.nr_stages;
  961. break;
  962. case 0x0350:
  963. config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
  964. config.nr_stages = tape->max_stages; 
  965. if (copy_to_user ((char *) arg, (char *) &config, sizeof (idetape_config_t)))
  966. return -EFAULT;
  967. break;
  968. default:
  969. return -EIO;
  970. }
  971. return 0;
  972. }
  973. /*
  974.  * The block device interface should not be used for data transfers.
  975.  * However, we still allow opening it so that we can issue general
  976.  * ide driver configuration ioctl's, such as the interrupt unmask feature.
  977.  */
  978. static int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
  979. {
  980. MOD_INC_USE_COUNT;
  981. #if ONSTREAM_DEBUG
  982.         printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_blkdev_openn");
  983. #endif
  984. return 0;
  985. }
  986. static void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
  987. {
  988. MOD_DEC_USE_COUNT;
  989. #if ONSTREAM_DEBUG
  990.         printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_blkdev_releasen");
  991. #endif
  992. }
  993. /*
  994.  * idetape_pre_reset is called before an ATAPI/ATA software reset.
  995.  */
  996. static void idetape_pre_reset (ide_drive_t *drive)
  997. {
  998. idetape_tape_t *tape = drive->driver_data;
  999. if (tape != NULL)
  1000. set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
  1001. }
  1002. /*
  1003.  * Character device interface functions
  1004.  */
  1005. static ide_drive_t *get_drive_ptr (kdev_t i_rdev)
  1006. {
  1007. unsigned int i = MINOR(i_rdev) & ~0xc0;
  1008. if (i >= MAX_HWIFS * MAX_DRIVES)
  1009. return NULL;
  1010. return (idetape_chrdevs[i].drive);
  1011. }
  1012. static int idetape_onstream_space_over_filemarks_backward (ide_drive_t *drive,short mt_op,int mt_count)
  1013. {
  1014. idetape_tape_t *tape = drive->driver_data;
  1015. int cnt = 0;
  1016. int last_mark_addr;
  1017. unsigned long flags;
  1018. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1019. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_bwdn", tape->name);
  1020. return -EIO;
  1021. }
  1022. while (cnt != mt_count) {
  1023. last_mark_addr = ntohl(tape->first_stage->aux->last_mark_addr);
  1024. if (last_mark_addr == -1)
  1025. return -EIO;
  1026. #if ONSTREAM_DEBUG
  1027. if (tape->debug_level >= 2)
  1028. printk(KERN_INFO "ide-tape: positioning to last mark at %dn", last_mark_addr);
  1029. #endif
  1030. idetape_position_tape(drive, last_mark_addr, 0, 0);
  1031. cnt++;
  1032. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1033. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarksn", tape->name);
  1034. return -EIO;
  1035. }
  1036. if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1037. printk(KERN_INFO "ide-tape: %s: expected to find marker at block %d, not foundn", tape->name, last_mark_addr);
  1038. return -EIO;
  1039. }
  1040. }
  1041. if (mt_op == MTBSFM) {
  1042. spin_lock_irqsave(&tape->spinlock, flags);
  1043. idetape_remove_stage_head (drive);
  1044. tape->logical_blk_num++;
  1045. spin_unlock_irqrestore(&tape->spinlock, flags);
  1046. }
  1047. return 0;
  1048. }
  1049. /*
  1050.  * ADRL 1.1 compatible "slow" space filemarks fwd version
  1051.  *
  1052.  * Just scans for the filemark sequentially.
  1053.  */
  1054. static int idetape_onstream_space_over_filemarks_forward_slow (ide_drive_t *drive,short mt_op,int mt_count)
  1055. {
  1056. idetape_tape_t *tape = drive->driver_data;
  1057. int cnt = 0;
  1058. unsigned long flags;
  1059. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1060. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwdn", tape->name);
  1061. return -EIO;
  1062. }
  1063. while (1) {
  1064. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1065. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarksn", tape->name);
  1066. return -EIO;
  1067. }
  1068. if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
  1069. cnt++;
  1070. if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
  1071. #if ONSTREAM_DEBUG
  1072. if (tape->debug_level >= 2)
  1073. printk(KERN_INFO "ide-tape: %s: space_fwd: EOD reachedn", tape->name);
  1074. #endif
  1075. return -EIO;
  1076. }
  1077. if (cnt == mt_count)
  1078. break;
  1079. spin_lock_irqsave(&tape->spinlock, flags);
  1080. idetape_remove_stage_head (drive);
  1081. spin_unlock_irqrestore(&tape->spinlock, flags);
  1082. }
  1083. if (mt_op == MTFSF) {
  1084. spin_lock_irqsave(&tape->spinlock, flags);
  1085. idetape_remove_stage_head (drive);
  1086. tape->logical_blk_num++;
  1087. spin_unlock_irqrestore(&tape->spinlock, flags);
  1088. }
  1089. return 0;
  1090. }
  1091. /*
  1092.  * Fast linux specific version of OnStream FSF
  1093.  */
  1094. static int idetape_onstream_space_over_filemarks_forward_fast (ide_drive_t *drive,short mt_op,int mt_count)
  1095. {
  1096. idetape_tape_t *tape = drive->driver_data;
  1097. int cnt = 0, next_mark_addr;
  1098. unsigned long flags;
  1099. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1100. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwdn", tape->name);
  1101. return -EIO;
  1102. }
  1103. /*
  1104.  * Find nearest (usually previous) marker
  1105.  */
  1106. while (1) {
  1107. if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_MARKER)
  1108. break;
  1109. if (tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD) {
  1110. #if ONSTREAM_DEBUG
  1111. if (tape->debug_level >= 2)
  1112. printk(KERN_INFO "ide-tape: %s: space_fwd: EOD reachedn", tape->name);
  1113. #endif
  1114. return -EIO;
  1115. }
  1116. if (ntohl(tape->first_stage->aux->filemark_cnt) == 0) {
  1117. if (tape->first_mark_addr == -1) {
  1118. printk(KERN_INFO "ide-tape: %s: reverting to slow filemark spacen", tape->name);
  1119. return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
  1120. }
  1121. idetape_position_tape(drive, tape->first_mark_addr, 0, 0);
  1122. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1123. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarks_fwd_fastn", tape->name);
  1124. return -EIO;
  1125. }
  1126. if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1127. printk(KERN_INFO "ide-tape: %s: expected to find filemark at %dn", tape->name, tape->first_mark_addr);
  1128. return -EIO;
  1129. }
  1130. } else {
  1131. if (idetape_onstream_space_over_filemarks_backward(drive, MTBSF, 1) < 0)
  1132. return -EIO;
  1133. mt_count++;
  1134. }
  1135. }
  1136. cnt++;
  1137. while (cnt != mt_count) {
  1138. next_mark_addr = ntohl(tape->first_stage->aux->next_mark_addr);
  1139. if (!next_mark_addr || next_mark_addr > tape->eod_frame_addr) {
  1140. printk(KERN_INFO "ide-tape: %s: reverting to slow filemark spacen", tape->name);
  1141. return idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count - cnt);
  1142. #if ONSTREAM_DEBUG
  1143. } else if (tape->debug_level >= 2) {
  1144.      printk(KERN_INFO "ide-tape: positioning to next mark at %dn", next_mark_addr);
  1145. #endif
  1146. }
  1147. idetape_position_tape(drive, next_mark_addr, 0, 0);
  1148. cnt++;
  1149. if (!idetape_get_logical_blk(drive, -1, 10, 0)) {
  1150. printk(KERN_INFO "ide-tape: %s: couldn't get logical blk num in space_filemarksn", tape->name);
  1151. return -EIO;
  1152. }
  1153. if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1154. printk(KERN_INFO "ide-tape: %s: expected to find marker at block %d, not foundn", tape->name, next_mark_addr);
  1155. return -EIO;
  1156. }
  1157. }
  1158. if (mt_op == MTFSF) {
  1159. spin_lock_irqsave(&tape->spinlock, flags);
  1160. idetape_remove_stage_head (drive);
  1161. tape->logical_blk_num++;
  1162. spin_unlock_irqrestore(&tape->spinlock, flags);
  1163. }
  1164. return 0;
  1165. }
  1166. /*
  1167.  * idetape_space_over_filemarks is now a bit more complicated than just
  1168.  * passing the command to the tape since we may have crossed some
  1169.  * filemarks during our pipelined read-ahead mode.
  1170.  *
  1171.  * As a minor side effect, the pipeline enables us to support MTFSFM when
  1172.  * the filemark is in our internal pipeline even if the tape doesn't
  1173.  * support spacing over filemarks in the reverse direction.
  1174.  */
  1175. static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
  1176. {
  1177. idetape_tape_t *tape = drive->driver_data;
  1178. idetape_pc_t pc;
  1179. unsigned long flags;
  1180. int retval,count=0;
  1181. int speed_control;
  1182. if (tape->onstream) {
  1183. if (tape->raw)
  1184. return -EIO;
  1185. speed_control = tape->speed_control;
  1186. tape->speed_control = 0;
  1187. if (mt_op == MTFSF || mt_op == MTFSFM) {
  1188. if (tape->linux_media)
  1189. retval = idetape_onstream_space_over_filemarks_forward_fast(drive, mt_op, mt_count);
  1190. else
  1191. retval = idetape_onstream_space_over_filemarks_forward_slow(drive, mt_op, mt_count);
  1192. } else
  1193. retval = idetape_onstream_space_over_filemarks_backward(drive, mt_op, mt_count);
  1194. tape->speed_control = speed_control;
  1195. tape->restart_speed_control_req = 1;
  1196. return retval;
  1197. }
  1198. if (tape->chrdev_direction == idetape_direction_read) {
  1199. /*
  1200.  * We have a read-ahead buffer. Scan it for crossed
  1201.  * filemarks.
  1202.  */
  1203. tape->merge_stage_size = 0;
  1204. clear_bit (IDETAPE_FILEMARK, &tape->flags);
  1205. while (tape->first_stage != NULL) {
  1206. idetape_wait_first_stage(drive);
  1207. if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
  1208. count++;
  1209. if (count == mt_count) {
  1210. switch (mt_op) {
  1211. case MTFSF:
  1212. spin_lock_irqsave(&tape->spinlock, flags);
  1213. idetape_remove_stage_head (drive);
  1214. spin_unlock_irqrestore(&tape->spinlock, flags);
  1215. case MTFSFM:
  1216. return (0);
  1217. default:
  1218. break;
  1219. }
  1220. }
  1221. spin_lock_irqsave(&tape->spinlock, flags);
  1222. idetape_remove_stage_head (drive);
  1223. spin_unlock_irqrestore(&tape->spinlock, flags);
  1224. }
  1225. idetape_discard_read_pipeline (drive, 1);
  1226. }
  1227. /*
  1228.  * The filemark was not found in our internal pipeline.
  1229.  * Now we can issue the space command.
  1230.  */
  1231. switch (mt_op) {
  1232. case MTFSF:
  1233. idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
  1234. return (idetape_queue_pc_tail (drive, &pc));
  1235. case MTFSFM:
  1236. if (!tape->capabilities.sprev)
  1237. return (-EIO);
  1238. retval = idetape_space_over_filemarks (drive, MTFSF, mt_count-count);
  1239. if (retval) return (retval);
  1240. return (idetape_space_over_filemarks (drive, MTBSF, 1));
  1241. case MTBSF:
  1242. if (!tape->capabilities.sprev)
  1243. return (-EIO);
  1244. idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
  1245. return (idetape_queue_pc_tail (drive, &pc));
  1246. case MTBSFM:
  1247. if (!tape->capabilities.sprev)
  1248. return (-EIO);
  1249. retval = idetape_space_over_filemarks (drive, MTBSF, mt_count+count);
  1250. if (retval) return (retval);
  1251. return (idetape_space_over_filemarks (drive, MTFSF, 1));
  1252. default:
  1253. printk (KERN_ERR "ide-tape: MTIO operation %d not supportedn",mt_op);
  1254. return (-EIO);
  1255. }
  1256. }
  1257. /*
  1258.  * Our character device read / write functions.
  1259.  *
  1260.  * The tape is optimized to maximize throughput when it is transferring
  1261.  * an integral number of the "continuous transfer limit", which is
  1262.  * a parameter of the specific tape (26 KB on my particular tape).
  1263.  *      (32 kB for Onstream)
  1264.  *
  1265.  * As of version 1.3 of the driver, the character device provides an
  1266.  * abstract continuous view of the media - any mix of block sizes (even 1
  1267.  * byte) on the same backup/restore procedure is supported. The driver
  1268.  * will internally convert the requests to the recommended transfer unit,
  1269.  * so that an unmatch between the user's block size to the recommended
  1270.  * size will only result in a (slightly) increased driver overhead, but
  1271.  * will no longer hit performance.
  1272.  *      This is not applicable to Onstream.
  1273.  */
  1274. static ssize_t idetape_chrdev_read (struct file *file, char *buf,
  1275.     size_t count, loff_t *ppos)
  1276. {
  1277. struct inode *inode = file->f_dentry->d_inode;
  1278. ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
  1279. idetape_tape_t *tape = drive->driver_data;
  1280. ssize_t bytes_read,temp, actually_read = 0, rc;
  1281. if (ppos != &file->f_pos) {
  1282. /* "A request was outside the capabilities of the device." */
  1283. return -ENXIO;
  1284. }
  1285. if (tape->onstream && (count != tape->tape_block_size)) {
  1286. printk(KERN_ERR "ide-tape: %s: use %d bytes as block size (%Zd used)n", tape->name, tape->tape_block_size, count);
  1287. return -EINVAL;
  1288. }
  1289. #if IDETAPE_DEBUG_LOG
  1290. if (tape->debug_level >= 3)
  1291. printk (KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zdn", count);
  1292. #endif /* IDETAPE_DEBUG_LOG */
  1293. if (tape->chrdev_direction != idetape_direction_read) {
  1294. if (test_bit (IDETAPE_DETECT_BS, &tape->flags))
  1295. if (count > tape->tape_block_size && (count % tape->tape_block_size) == 0)
  1296. tape->user_bs_factor = count / tape->tape_block_size;
  1297. }
  1298. if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
  1299. return rc;
  1300. if (count == 0)
  1301. return (0);
  1302. if (tape->merge_stage_size) {
  1303. actually_read = IDE_MIN (tape->merge_stage_size, count);
  1304. idetape_copy_stage_to_user (tape, buf, tape->merge_stage, actually_read);
  1305. buf += actually_read;
  1306. tape->merge_stage_size -= actually_read;
  1307. count -= actually_read;
  1308. }
  1309. while (count >= tape->stage_size) {
  1310. bytes_read = idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
  1311. if (bytes_read <= 0)
  1312. goto finish;
  1313. idetape_copy_stage_to_user (tape, buf, tape->merge_stage, bytes_read);
  1314. buf += bytes_read;
  1315. count -= bytes_read;
  1316. actually_read += bytes_read;
  1317. }
  1318. if (count) {
  1319. bytes_read=idetape_add_chrdev_read_request (drive, tape->capabilities.ctl);
  1320. if (bytes_read <= 0)
  1321. goto finish;
  1322. temp = IDE_MIN (count, bytes_read);
  1323. idetape_copy_stage_to_user (tape, buf, tape->merge_stage, temp);
  1324. actually_read += temp;
  1325. tape->merge_stage_size = bytes_read-temp;
  1326. }
  1327. finish:
  1328. if (!actually_read && test_bit (IDETAPE_FILEMARK, &tape->flags)) {
  1329. #if IDETAPE_DEBUG_LOG
  1330. if (tape->debug_level >= 2)
  1331. printk(KERN_INFO "ide-tape: %s: spacing over filemarkn", tape->name);
  1332. #endif
  1333. idetape_space_over_filemarks (drive, MTFSF, 1);
  1334. return 0;
  1335. }
  1336. if (tape->onstream && !actually_read && test_and_clear_bit(IDETAPE_READ_ERROR, &tape->flags)) {
  1337. printk(KERN_ERR "ide-tape: %s: unrecovered read error on logical block number %d, skippingn",
  1338. tape->name, tape->logical_blk_num);
  1339. tape->logical_blk_num++;
  1340. return -EIO;
  1341. }
  1342. return actually_read;
  1343. }
  1344. static void idetape_update_last_marker (ide_drive_t *drive, int last_mark_addr, int next_mark_addr)
  1345. {
  1346. idetape_tape_t *tape = drive->driver_data;
  1347. idetape_stage_t *stage;
  1348. os_aux_t *aux;
  1349. int position;
  1350. if (!tape->onstream || tape->raw)
  1351. return;
  1352. if (last_mark_addr == -1)
  1353. return;
  1354. stage = __idetape_kmalloc_stage(tape, 0, 0);
  1355. if (stage == NULL)
  1356. return;
  1357. idetape_flush_tape_buffers(drive);
  1358. position = idetape_read_position(drive);
  1359. #if ONSTREAM_DEBUG
  1360. if (tape->debug_level >= 2)
  1361. printk(KERN_INFO "ide-tape: current position (2) %d, lblk %dn", position, tape->logical_blk_num);
  1362. if (tape->debug_level >= 2)
  1363. printk(KERN_INFO "ide-tape: current position (2) tape block %dn", tape->last_frame_position);
  1364. #endif
  1365. idetape_position_tape(drive, last_mark_addr, 0, 0);
  1366. if (!idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 1, stage->bh)) {
  1367. printk(KERN_INFO "ide-tape: %s: couldn't read last markern", tape->name);
  1368. __idetape_kfree_stage (stage);
  1369. idetape_position_tape(drive, position, 0, 0);
  1370. return;
  1371. }
  1372. aux = stage->aux;
  1373. if (aux->frame_type != OS_FRAME_TYPE_MARKER) {
  1374. printk(KERN_INFO "ide-tape: %s: expected to find marker at addr %dn", tape->name, last_mark_addr);
  1375. __idetape_kfree_stage (stage);
  1376. idetape_position_tape(drive, position, 0, 0);
  1377. return;
  1378. }
  1379. #if ONSTREAM_DEBUG
  1380. if (tape->debug_level >= 2)
  1381. printk(KERN_INFO "ide-tape: writing back markern");
  1382. #endif
  1383. aux->next_mark_addr = htonl(next_mark_addr);
  1384. idetape_position_tape(drive, last_mark_addr, 0, 0);
  1385. if (!idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
  1386. printk(KERN_INFO "ide-tape: %s: couldn't write back marker frame at %dn", tape->name, last_mark_addr);
  1387. __idetape_kfree_stage (stage);
  1388. idetape_position_tape(drive, position, 0, 0);
  1389. return;
  1390. }
  1391. __idetape_kfree_stage (stage);
  1392. idetape_flush_tape_buffers (drive);
  1393. idetape_position_tape(drive, position, 0, 0);
  1394. return;
  1395. }
  1396. static void idetape_write_filler (ide_drive_t *drive, int block, int cnt)
  1397. {
  1398. idetape_tape_t *tape = drive->driver_data;
  1399. idetape_stage_t *stage;
  1400. int rc;
  1401. if (!tape->onstream || tape->raw)
  1402. return;
  1403. stage = __idetape_kmalloc_stage(tape, 1, 1);
  1404. if (stage == NULL)
  1405. return;
  1406. idetape_init_stage(drive, stage, OS_FRAME_TYPE_FILL, 0);
  1407. idetape_wait_ready(drive, 60 * 5 * HZ);
  1408. rc = idetape_position_tape(drive, block, 0, 0);
  1409. #if ONSTREAM_DEBUG
  1410. printk(KERN_INFO "write_filler: positioning failed it returned %dn", rc);
  1411. #endif
  1412. if (rc != 0) 
  1413. return; /* don't write fillers if we cannot position the tape. */
  1414. strcpy(stage->bh->b_data, "Filler");
  1415. while (cnt--) {
  1416. if (!idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
  1417. printk(KERN_INFO "ide-tape: %s: write_filler: couldn't write header framen", tape->name);
  1418. __idetape_kfree_stage (stage);
  1419. return;
  1420. }
  1421. }
  1422. __idetape_kfree_stage (stage);
  1423. }
  1424. static void __idetape_write_header (ide_drive_t *drive, int block, int cnt)
  1425. {
  1426. idetape_tape_t *tape = drive->driver_data;
  1427. idetape_stage_t *stage;
  1428. os_header_t header;
  1429. stage = __idetape_kmalloc_stage(tape, 1, 1);
  1430. if (stage == NULL)
  1431. return;
  1432. idetape_init_stage(drive, stage, OS_FRAME_TYPE_HEADER, tape->logical_blk_num);
  1433. idetape_wait_ready(drive, 60 * 5 * HZ);
  1434. idetape_position_tape(drive, block, 0, 0);
  1435. memset(&header, 0, sizeof(header));
  1436. strcpy(header.ident_str, "ADR_SEQ");
  1437. header.major_rev = 1;
  1438. header.minor_rev = OS_ADR_MINREV;
  1439. header.par_num = 1;
  1440. header.partition.partition_num = OS_DATA_PARTITION;
  1441. header.partition.par_desc_ver = OS_PARTITION_VERSION;
  1442. header.partition.first_frame_addr = htonl(OS_DATA_STARTFRAME1);
  1443. header.partition.last_frame_addr = htonl(tape->capacity);
  1444. header.partition.wrt_pass_cntr = htons(tape->wrt_pass_cntr);
  1445. header.partition.eod_frame_addr = htonl(tape->eod_frame_addr);
  1446. memcpy(stage->bh->b_data, &header, sizeof(header));
  1447. while (cnt--) {
  1448. if (!idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 1, stage->bh)) {
  1449. printk(KERN_INFO "ide-tape: %s: couldn't write header framen", tape->name);
  1450. __idetape_kfree_stage (stage);
  1451. return;
  1452. }
  1453. }
  1454. __idetape_kfree_stage (stage);
  1455. idetape_flush_tape_buffers (drive);
  1456. }
  1457. static void idetape_write_header (ide_drive_t *drive, int locate_eod)
  1458. {
  1459. idetape_tape_t *tape = drive->driver_data;
  1460. #if ONSTREAM_DEBUG
  1461. if (tape->debug_level >= 2)
  1462. printk(KERN_INFO "ide-tape: %s: writing tape headern", tape->name);
  1463. #endif
  1464. if (!tape->onstream || tape->raw)
  1465. return;
  1466. tape->update_frame_cntr++;
  1467. __idetape_write_header(drive, 5, 5);
  1468. __idetape_write_header(drive, 0xbae, 5); /* 2990 */
  1469. if (locate_eod) {
  1470. #if ONSTREAM_DEBUG
  1471. if (tape->debug_level >= 2)
  1472. printk(KERN_INFO "ide-tape: %s: locating back to eod frame addr %dn", tape->name, tape->eod_frame_addr);
  1473. #endif
  1474. idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
  1475. }
  1476. }
  1477. static ssize_t idetape_chrdev_write (struct file *file, const char *buf,
  1478.      size_t count, loff_t *ppos)
  1479. {
  1480. struct inode *inode = file->f_dentry->d_inode;
  1481. ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
  1482. idetape_tape_t *tape = drive->driver_data;
  1483. ssize_t retval, actually_written = 0;
  1484. int position;
  1485. if (ppos != &file->f_pos) {
  1486. /* "A request was outside the capabilities of the device." */
  1487. return -ENXIO;
  1488. }
  1489. #if IDETAPE_DEBUG_LOG
  1490. if (tape->debug_level >= 3)
  1491. printk (KERN_INFO "ide-tape: Reached idetape_chrdev_write, count %Zdn", count);
  1492. #endif /* IDETAPE_DEBUG_LOG */
  1493. if (tape->onstream) {
  1494. if (count != tape->tape_block_size) {
  1495. printk(KERN_ERR "ide-tape: %s: chrdev_write: use %d bytes as block size (%Zd used)n",
  1496. tape->name, tape->tape_block_size, count);
  1497. return -EINVAL;
  1498. }
  1499. /*
  1500.  * Check if we reach the end of the tape. Just assume the whole pipeline
  1501.  * is filled with write requests!
  1502.  */
  1503. if (tape->first_frame_position + tape->nr_stages >= tape->capacity - OS_EW)  {
  1504. #if ONSTREAM_DEBUG
  1505. printk(KERN_INFO, "chrdev_write: Write truncated at EOM early warning");
  1506. #endif
  1507. if (tape->chrdev_direction == idetape_direction_write)
  1508. idetape_write_release(inode);
  1509. return -ENOSPC;
  1510. }
  1511. }
  1512. if (tape->chrdev_direction != idetape_direction_write) { /* Initialize write operation */
  1513. if (tape->chrdev_direction == idetape_direction_read)
  1514. idetape_discard_read_pipeline (drive, 1);
  1515. #if IDETAPE_DEBUG_BUGS
  1516. if (tape->merge_stage || tape->merge_stage_size) {
  1517. printk (KERN_ERR "ide-tape: merge_stage_size should be 0 nown");
  1518. tape->merge_stage_size = 0;
  1519. }
  1520. #endif /* IDETAPE_DEBUG_BUGS */
  1521. if ((tape->merge_stage = __idetape_kmalloc_stage (tape, 0, 0)) == NULL)
  1522. return -ENOMEM;
  1523. tape->chrdev_direction = idetape_direction_write;
  1524. idetape_init_merge_stage (tape);
  1525. if (tape->onstream) {
  1526. position = idetape_read_position(drive);
  1527. if (position <= OS_DATA_STARTFRAME1) {
  1528. tape->logical_blk_num = 0;
  1529. tape->wrt_pass_cntr++;
  1530. #if ONSTREAM_DEBUG
  1531. if (tape->debug_level >= 2)
  1532. printk(KERN_INFO "ide-tape: %s: logical block num 0, setting eod to %dn", tape->name, OS_DATA_STARTFRAME1);
  1533. if (tape->debug_level >= 2)
  1534. printk(KERN_INFO "ide-tape: %s: allocating new write pass counter %dn", tape->name, tape->wrt_pass_cntr);
  1535. #endif
  1536. tape->filemark_cnt = 0;
  1537. tape->eod_frame_addr = OS_DATA_STARTFRAME1;
  1538. tape->first_mark_addr = tape->last_mark_addr = -1;
  1539. idetape_write_header(drive, 1);
  1540. }
  1541. #if ONSTREAM_DEBUG
  1542. if (tape->debug_level >= 2)
  1543. printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %dn", tape->name, tape->eod_frame_addr);
  1544. #endif
  1545. position = idetape_read_position(drive);
  1546. if (position != tape->eod_frame_addr)
  1547. idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
  1548. #if ONSTREAM_DEBUG
  1549. if (tape->debug_level >= 2)
  1550. printk(KERN_INFO "ide-tape: %s: first_frame_position %dn", tape->name, tape->first_frame_position);
  1551. #endif
  1552. }
  1553. /*
  1554.  * Issue a write 0 command to ensure that DSC handshake
  1555.  * is switched from completion mode to buffer available
  1556.  * mode.
  1557.  */
  1558. retval = idetape_queue_rw_tail (drive, IDETAPE_WRITE_RQ, 0, tape->merge_stage->bh);
  1559. if (retval < 0) {
  1560. kfree (tape->merge_stage);
  1561. tape->merge_stage = NULL;
  1562. tape->chrdev_direction = idetape_direction_none;
  1563. return retval;
  1564. }
  1565. #if ONSTREAM_DEBUG
  1566. if (tape->debug_level >= 2)
  1567. printk("ide-tape: first_frame_position %dn", tape->first_frame_position);
  1568. #endif
  1569. }
  1570. if (count == 0)
  1571. return (0);
  1572. if (tape->restart_speed_control_req)
  1573. idetape_restart_speed_control(drive);
  1574. if (tape->merge_stage_size) {
  1575. #if IDETAPE_DEBUG_BUGS
  1576. if (tape->merge_stage_size >= tape->stage_size) {
  1577. printk (KERN_ERR "ide-tape: bug: merge buffer too bign");
  1578. tape->merge_stage_size = 0;
  1579. }
  1580. #endif /* IDETAPE_DEBUG_BUGS */
  1581. actually_written = IDE_MIN (tape->stage_size - tape->merge_stage_size, count);
  1582. idetape_copy_stage_from_user (tape, tape->merge_stage, buf, actually_written);
  1583. buf += actually_written;
  1584. tape->merge_stage_size += actually_written;
  1585. count -= actually_written;
  1586. if (tape->merge_stage_size == tape->stage_size) {
  1587. tape->merge_stage_size = 0;
  1588. retval = idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
  1589. if (retval <= 0)
  1590. return (retval);
  1591. }
  1592. }
  1593. while (count >= tape->stage_size) {
  1594. idetape_copy_stage_from_user (tape, tape->merge_stage, buf, tape->stage_size);
  1595. buf += tape->stage_size;
  1596. count -= tape->stage_size;
  1597. retval = idetape_add_chrdev_write_request (drive, tape->capabilities.ctl);
  1598. actually_written += tape->stage_size;
  1599. if (retval <= 0)
  1600. return (retval);
  1601. }
  1602. if (count) {
  1603. actually_written+=count;
  1604. idetape_copy_stage_from_user (tape, tape->merge_stage, buf, count);
  1605. tape->merge_stage_size += count;
  1606. }
  1607. return (actually_written);
  1608. }
  1609. static int idetape_write_filemark (ide_drive_t *drive)
  1610. {
  1611. idetape_tape_t *tape = drive->driver_data;
  1612. int last_mark_addr;
  1613. idetape_pc_t pc;
  1614. if (!tape->onstream) {
  1615. idetape_create_write_filemark_cmd(drive, &pc, 1); /* Write a filemark */
  1616. if (idetape_queue_pc_tail (drive, &pc)) {
  1617. printk (KERN_ERR "ide-tape: Couldn't write a filemarkn");
  1618. return -EIO;
  1619. }
  1620. } else if (!tape->raw) {
  1621. last_mark_addr = idetape_read_position(drive);
  1622. tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
  1623. if (tape->merge_stage != NULL) {
  1624. idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_MARKER, tape->logical_blk_num);
  1625. idetape_pad_zeros (drive, tape->stage_size);
  1626. tape->logical_blk_num++;
  1627. __idetape_kfree_stage (tape->merge_stage);
  1628. tape->merge_stage = NULL;
  1629. }
  1630. if (tape->filemark_cnt)
  1631. idetape_update_last_marker(drive, tape->last_mark_addr, last_mark_addr);
  1632. tape->last_mark_addr = last_mark_addr;
  1633. if (tape->filemark_cnt++ == 0)
  1634. tape->first_mark_addr = last_mark_addr;
  1635. }
  1636. return 0;
  1637. }
  1638. static void idetape_write_eod (ide_drive_t *drive)
  1639. {
  1640. idetape_tape_t *tape = drive->driver_data;
  1641. if (!tape->onstream || tape->raw)
  1642. return;
  1643. tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
  1644. if (tape->merge_stage != NULL) {
  1645. tape->eod_frame_addr = idetape_read_position(drive);
  1646. idetape_init_stage(drive, tape->merge_stage, OS_FRAME_TYPE_EOD, tape->logical_blk_num);
  1647. idetape_pad_zeros (drive, tape->stage_size);
  1648. __idetape_kfree_stage (tape->merge_stage);
  1649. tape->merge_stage = NULL;
  1650. }
  1651. return;
  1652. }
  1653. int idetape_seek_logical_blk (ide_drive_t *drive, int logical_blk_num)
  1654. {
  1655. idetape_tape_t *tape = drive->driver_data;
  1656. int estimated_address = logical_blk_num + 20;
  1657. int retries = 0;
  1658. int speed_control;
  1659. speed_control = tape->speed_control;
  1660. tape->speed_control = 0;
  1661. if (logical_blk_num < 0)
  1662. logical_blk_num = 0;
  1663. if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
  1664. goto ok;
  1665. while (++retries < 10) {
  1666. idetape_discard_read_pipeline(drive, 0);
  1667. idetape_position_tape(drive, estimated_address, 0, 0);
  1668. if (idetape_get_logical_blk(drive, logical_blk_num, 10, 1))
  1669. goto ok;
  1670. if (!idetape_get_logical_blk(drive, -1, 10, 1))
  1671. goto error;
  1672. if (tape->logical_blk_num < logical_blk_num)
  1673. estimated_address += logical_blk_num - tape->logical_blk_num;
  1674. else
  1675. break;
  1676. }
  1677. error:
  1678. tape->speed_control = speed_control;
  1679. tape->restart_speed_control_req = 1;
  1680. printk(KERN_INFO "ide-tape: %s: couldn't seek to logical block %d (at %d), %d retriesn", tape->name, logical_blk_num, tape->logical_blk_num, retries);
  1681. return -EIO;
  1682. ok:
  1683. tape->speed_control = speed_control;
  1684. tape->restart_speed_control_req = 1;
  1685. return 0;
  1686. }
  1687. /*
  1688.  * idetape_mtioctop is called from idetape_chrdev_ioctl when
  1689.  * the general mtio MTIOCTOP ioctl is requested.
  1690.  *
  1691.  * We currently support the following mtio.h operations:
  1692.  *
  1693.  * MTFSF - Space over mt_count filemarks in the positive direction.
  1694.  * The tape is positioned after the last spaced filemark.
  1695.  *
  1696.  * MTFSFM - Same as MTFSF, but the tape is positioned before the
  1697.  * last filemark.
  1698.  *
  1699.  * MTBSF - Steps background over mt_count filemarks, tape is
  1700.  * positioned before the last filemark.
  1701.  *
  1702.  * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
  1703.  *
  1704.  * Note:
  1705.  *
  1706.  * MTBSF and MTBSFM are not supported when the tape doesn't
  1707.  * supports spacing over filemarks in the reverse direction.
  1708.  * In this case, MTFSFM is also usually not supported (it is
  1709.  * supported in the rare case in which we crossed the filemark
  1710.  * during our read-ahead pipelined operation mode).
  1711.  *
  1712.  * MTWEOF - Writes mt_count filemarks. Tape is positioned after
  1713.  * the last written filemark.
  1714.  *
  1715.  * MTREW - Rewinds tape.
  1716.  *
  1717.  * MTLOAD - Loads the tape.
  1718.  *
  1719.  * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
  1720.  * MTUNLOAD prevents further access until the media is replaced.
  1721.  *
  1722.  * MTNOP - Flushes tape buffers.
  1723.  *
  1724.  * MTRETEN - Retension media. This typically consists of one end
  1725.  * to end pass on the media.
  1726.  *
  1727.  * MTEOM - Moves to the end of recorded data.
  1728.  *
  1729.  * MTERASE - Erases tape.
  1730.  *
  1731.  * MTSETBLK -  Sets the user block size to mt_count bytes. If
  1732.  * mt_count is 0, we will attempt to autodetect
  1733.  * the block size.
  1734.  *
  1735.  * MTSEEK - Positions the tape in a specific block number, where
  1736.  * each block is assumed to contain which user_block_size
  1737.  * bytes.
  1738.  *
  1739.  * MTSETPART -  Switches to another tape partition.
  1740.  *
  1741.  * MTLOCK -  Locks the tape door.
  1742.  *
  1743.  * MTUNLOCK -  Unlocks the tape door.
  1744.  *
  1745.  * The following commands are currently not supported:
  1746.  *
  1747.  * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
  1748.  * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
  1749.  */
  1750. static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
  1751. {
  1752. idetape_tape_t *tape = drive->driver_data;
  1753. idetape_pc_t pc;
  1754. int i,retval;
  1755. #if IDETAPE_DEBUG_LOG
  1756. if (tape->debug_level >= 1)
  1757. printk (KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%dn",mt_op,mt_count);
  1758. #endif /* IDETAPE_DEBUG_LOG */
  1759. /*
  1760.  * Commands which need our pipelined read-ahead stages.
  1761.  */
  1762. switch (mt_op) {
  1763. case MTFSF:
  1764. case MTFSFM:
  1765. case MTBSF:
  1766. case MTBSFM:
  1767. if (!mt_count)
  1768. return (0);
  1769. return (idetape_space_over_filemarks (drive,mt_op,mt_count));
  1770. default:
  1771. break;
  1772. }
  1773. switch (mt_op) {
  1774. case MTWEOF:
  1775. idetape_discard_read_pipeline (drive, 1);
  1776. for (i = 0; i < mt_count; i++) {
  1777. retval = idetape_write_filemark(drive);
  1778. if (retval) return retval;
  1779. }
  1780. return (0);
  1781. case MTREW:
  1782. idetape_discard_read_pipeline (drive, 0);
  1783. if (idetape_rewind_tape(drive))
  1784. return -EIO;
  1785. if (tape->onstream && !tape->raw)
  1786. return idetape_position_tape(drive, OS_DATA_STARTFRAME1, 0, 0);
  1787. return 0;
  1788. case MTLOAD:
  1789. idetape_discard_read_pipeline (drive, 0);
  1790. idetape_create_load_unload_cmd (drive, &pc, IDETAPE_LU_LOAD_MASK);
  1791. return (idetape_queue_pc_tail (drive, &pc));
  1792. case MTUNLOAD:
  1793. case MTOFFL:
  1794. idetape_discard_read_pipeline (drive, 0);
  1795. idetape_create_load_unload_cmd (drive, &pc,!IDETAPE_LU_LOAD_MASK);
  1796. return (idetape_queue_pc_tail (drive, &pc));
  1797. case MTNOP:
  1798. idetape_discard_read_pipeline (drive, 0);
  1799. return (idetape_flush_tape_buffers (drive));
  1800. case MTRETEN:
  1801. idetape_discard_read_pipeline (drive, 0);
  1802. idetape_create_load_unload_cmd (drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
  1803. return (idetape_queue_pc_tail (drive, &pc));
  1804. case MTEOM:
  1805. if (tape->onstream) {
  1806. #if ONSTREAM_DEBUG
  1807. if (tape->debug_level >= 2)
  1808. printk(KERN_INFO "ide-tape: %s: positioning tape to eod at %dn", tape->name, tape->eod_frame_addr);
  1809. #endif
  1810. idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
  1811. if (!idetape_get_logical_blk(drive, -1, 10, 0))
  1812. return -EIO;
  1813. if (tape->first_stage->aux->frame_type != OS_FRAME_TYPE_EOD)
  1814. return -EIO;
  1815. return 0;
  1816. }
  1817. idetape_create_space_cmd (&pc, 0, IDETAPE_SPACE_TO_EOD);
  1818. return (idetape_queue_pc_tail (drive, &pc));
  1819. case MTERASE:
  1820. if (tape->onstream) {
  1821. tape->eod_frame_addr = OS_DATA_STARTFRAME1;
  1822. tape->logical_blk_num = 0;
  1823. tape->first_mark_addr = tape->last_mark_addr = -1;
  1824. idetape_position_tape(drive, tape->eod_frame_addr, 0, 0);
  1825. idetape_write_eod(drive);
  1826. idetape_flush_tape_buffers (drive);
  1827. idetape_write_header(drive, 0);
  1828. /*
  1829.  * write filler frames to the unused frames...
  1830.  * REMOVE WHEN going to LIN4 application type...
  1831.  */
  1832. idetape_write_filler(drive, OS_DATA_STARTFRAME1 - 10, 10);
  1833. idetape_write_filler(drive, OS_DATA_ENDFRAME1, 10);
  1834. idetape_flush_tape_buffers (drive);
  1835. (void) idetape_rewind_tape (drive);
  1836. return 0;
  1837. }
  1838. (void) idetape_rewind_tape (drive);
  1839. idetape_create_erase_cmd (&pc);
  1840. return (idetape_queue_pc_tail (drive, &pc));
  1841. case MTSETBLK:
  1842. if (tape->onstream) {
  1843. if (mt_count != tape->tape_block_size) {
  1844. printk(KERN_INFO "ide-tape: %s: MTSETBLK %d -- only %d bytes block size supportedn", tape->name, mt_count, tape->tape_block_size);
  1845. return -EINVAL;
  1846. }
  1847. return 0;
  1848. }
  1849. if (mt_count) {
  1850. if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
  1851. return -EIO;
  1852. tape->user_bs_factor = mt_count / tape->tape_block_size;
  1853. clear_bit (IDETAPE_DETECT_BS, &tape->flags);
  1854. } else
  1855. set_bit (IDETAPE_DETECT_BS, &tape->flags);
  1856. return 0;
  1857. case MTSEEK:
  1858. if (!tape->onstream || tape->raw) {
  1859. idetape_discard_read_pipeline (drive, 0);
  1860. return idetape_position_tape (drive, mt_count * tape->user_bs_factor, tape->partition, 0);
  1861. }
  1862. return idetape_seek_logical_blk(drive, mt_count);
  1863. case MTSETPART:
  1864. idetape_discard_read_pipeline (drive, 0);
  1865. if (tape->onstream)
  1866. return -EIO;
  1867. return (idetape_position_tape (drive, 0, mt_count, 0));
  1868. case MTFSR:
  1869. case MTBSR:
  1870. if (tape->onstream) {
  1871. if (!idetape_get_logical_blk(drive, -1, 10, 0))
  1872. return -EIO;
  1873. if (mt_op == MTFSR)
  1874. return idetape_seek_logical_blk(drive, tape->logical_blk_num + mt_count);
  1875. else {
  1876. idetape_discard_read_pipeline (drive, 0);
  1877. return idetape_seek_logical_blk(drive, tape->logical_blk_num - mt_count);
  1878. }
  1879. }
  1880. case MTLOCK:
  1881. if (!idetape_create_prevent_cmd(drive, &pc, 1))
  1882. return 0;
  1883. retval = idetape_queue_pc_tail (drive, &pc);
  1884. if (retval) return retval;
  1885. tape->door_locked = DOOR_EXPLICITLY_LOCKED;
  1886. return 0;
  1887. case MTUNLOCK:
  1888. if (!idetape_create_prevent_cmd(drive, &pc, 0))
  1889. return 0;
  1890. retval = idetape_queue_pc_tail (drive, &pc);
  1891. if (retval) return retval;
  1892. tape->door_locked = DOOR_UNLOCKED;
  1893. return 0;
  1894. default:
  1895. printk (KERN_ERR "ide-tape: MTIO operation %d not supportedn",mt_op);
  1896. return (-EIO);
  1897. }
  1898. }
  1899. /*
  1900.  * Our character device ioctls.
  1901.  *
  1902.  * General mtio.h magnetic io commands are supported here, and not in
  1903.  * the corresponding block interface.
  1904.  *
  1905.  * The following ioctls are supported:
  1906.  *
  1907.  * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
  1908.  *
  1909.  * MTIOCGET -  The mt_dsreg field in the returned mtget structure
  1910.  * will be set to (user block size in bytes <<
  1911.  * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
  1912.  *
  1913.  * The mt_blkno is set to the current user block number.
  1914.  * The other mtget fields are not supported.
  1915.  *
  1916.  * MTIOCPOS - The current tape "block position" is returned. We
  1917.  * assume that each block contains user_block_size
  1918.  * bytes.
  1919.  *
  1920.  * Our own ide-tape ioctls are supported on both interfaces.
  1921.  */
  1922. static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1923. {
  1924. ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
  1925. idetape_tape_t *tape = drive->driver_data;
  1926. struct mtop mtop;
  1927. struct mtget mtget;
  1928. struct mtpos mtpos;
  1929. int block_offset = 0, position = tape->first_frame_position;
  1930. #if IDETAPE_DEBUG_LOG
  1931. if (tape->debug_level >= 3)
  1932. printk (KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, cmd=%un",cmd);
  1933. #endif /* IDETAPE_DEBUG_LOG */
  1934. tape->restart_speed_control_req = 1;
  1935. if (tape->chrdev_direction == idetape_direction_write) {
  1936. idetape_empty_write_pipeline (drive);
  1937. idetape_flush_tape_buffers (drive);
  1938. }
  1939. if (cmd == MTIOCGET || cmd == MTIOCPOS) {
  1940. block_offset = idetape_pipeline_size (drive) / (tape->tape_block_size * tape->user_bs_factor);
  1941. if ((position = idetape_read_position(drive)) < 0)
  1942. return -EIO;
  1943. }
  1944. switch (cmd) {
  1945. case MTIOCTOP:
  1946. if (copy_from_user ((char *) &mtop, (char *) arg, sizeof (struct mtop)))
  1947. return -EFAULT;
  1948. return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
  1949. case MTIOCGET:
  1950. memset (&mtget, 0, sizeof (struct mtget));
  1951. mtget.mt_type = MT_ISSCSI2;
  1952. if (!tape->onstream || tape->raw)
  1953. mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
  1954. else {
  1955. if (!idetape_get_logical_blk(drive, -1, 10, 0))
  1956. mtget.mt_blkno = -1;
  1957. else
  1958. mtget.mt_blkno = tape->logical_blk_num;
  1959. }
  1960. mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
  1961. if (tape->onstream) {
  1962. mtget.mt_gstat |= GMT_ONLINE(0xffffffff);
  1963. if (tape->first_stage && tape->first_stage->aux->frame_type == OS_FRAME_TYPE_EOD)
  1964. mtget.mt_gstat |= GMT_EOD(0xffffffff);
  1965. if (position <= OS_DATA_STARTFRAME1)
  1966. mtget.mt_gstat |= GMT_BOT(0xffffffff);
  1967. }
  1968. if (copy_to_user ((char *) arg,(char *) &mtget, sizeof (struct mtget)))
  1969. return -EFAULT;
  1970. return 0;
  1971. case MTIOCPOS:
  1972. if (tape->onstream && !tape->raw) {
  1973. if (!idetape_get_logical_blk(drive, -1, 10, 0))
  1974. return -EIO;
  1975. mtpos.mt_blkno = tape->logical_blk_num;
  1976. } else
  1977. mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
  1978. if (copy_to_user ((char *) arg,(char *) &mtpos, sizeof (struct mtpos)))
  1979. return -EFAULT;
  1980. return 0;
  1981. default:
  1982. if (tape->chrdev_direction == idetape_direction_read)
  1983. idetape_discard_read_pipeline (drive, 1);
  1984. return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
  1985. }
  1986. }
  1987. static int __idetape_analyze_headers (ide_drive_t *drive, int block)
  1988. {
  1989. idetape_tape_t *tape = drive->driver_data;
  1990. idetape_stage_t *stage;
  1991. os_header_t *header;
  1992. os_aux_t *aux;
  1993. if (!tape->onstream || tape->raw) {
  1994. tape->header_ok = tape->linux_media = 1;
  1995. return 1;
  1996. }
  1997. tape->header_ok = tape->linux_media = 0;
  1998. tape->update_frame_cntr = 0;
  1999. tape->wrt_pass_cntr = 0;
  2000. tape->eod_frame_addr = OS_DATA_STARTFRAME1;
  2001. tape->first_mark_addr = tape->last_mark_addr = -1;
  2002. stage = __idetape_kmalloc_stage (tape, 0, 0);
  2003. if (stage == NULL)
  2004. return 0;
  2005. #if ONSTREAM_DEBUG
  2006. if (tape->debug_level >= 2)
  2007. printk(KERN_INFO "ide-tape: %s: reading headern", tape->name);
  2008. #endif
  2009. idetape_position_tape(drive, block, 0, 0);
  2010. if (!idetape_queue_rw_tail (drive, IDETAPE_READ_RQ, 1, stage->bh)) {
  2011. printk(KERN_INFO "ide-tape: %s: couldn't read header framen", tape->name);
  2012. __idetape_kfree_stage (stage);
  2013. return 0;
  2014. }
  2015. header = (os_header_t *) stage->bh->b_data;
  2016. aux = stage->aux;
  2017. if (strncmp(header->ident_str, "ADR_SEQ", 7) != 0) {
  2018. printk(KERN_INFO "ide-tape: %s: invalid header identification stringn", tape->name);
  2019. __idetape_kfree_stage (stage);
  2020. return 0;
  2021. }
  2022. if (header->major_rev != 1 || (header->minor_rev > OS_ADR_MINREV))
  2023. printk(KERN_INFO "ide-tape: warning: revision %d.%d detected (up to 1.%d supported)n", header->major_rev, header->minor_rev, OS_ADR_MINREV);
  2024. if (header->par_num != 1)
  2025. printk(KERN_INFO "ide-tape: warning: %d partitions defined, only one supportedn", header->par_num);
  2026. tape->wrt_pass_cntr = ntohs(header->partition.wrt_pass_cntr);
  2027. tape->eod_frame_addr = ntohl(header->partition.eod_frame_addr);
  2028. tape->filemark_cnt = ntohl(aux->filemark_cnt);
  2029. tape->first_mark_addr = ntohl(aux->next_mark_addr);
  2030. tape->last_mark_addr = ntohl(aux->last_mark_addr);
  2031. tape->update_frame_cntr = ntohl(aux->update_frame_cntr);
  2032. memcpy(tape->application_sig, aux->application_sig, 4);
  2033. tape->application_sig[4] = 0;
  2034. if (memcmp(tape->application_sig, "LIN", 3) == 0) {
  2035. tape->linux_media = 1;
  2036. tape->linux_media_version = tape->application_sig[3] - '0';
  2037. if (tape->linux_media_version != 3)
  2038. printk(KERN_INFO "ide-tape: %s: Linux media version %d detected (current 3)n",
  2039.  tape->name, tape->linux_media_version);
  2040. } else {
  2041. printk(KERN_INFO "ide-tape: %s: non Linux media detected (%s)n", tape->name, tape->application_sig);
  2042. tape->linux_media = 0;
  2043. }
  2044. #if ONSTREAM_DEBUG
  2045. if (tape->debug_level >= 2)
  2046. printk(KERN_INFO "ide-tape: %s: detected write pass counter %d, eod frame addr %dn", tape->name, tape->wrt_pass_cntr, tape->eod_frame_addr);
  2047. #endif
  2048. __idetape_kfree_stage (stage);
  2049. return 1;
  2050. }
  2051. static int idetape_analyze_headers (ide_drive_t *drive)
  2052. {
  2053. idetape_tape_t *tape = drive->driver_data;
  2054. int position, block;
  2055. if (!tape->onstream || tape->raw) {
  2056. tape->header_ok = tape->linux_media = 1;
  2057. return 1;
  2058. }
  2059. tape->header_ok = tape->linux_media = 0;
  2060. position = idetape_read_position(drive);
  2061. for (block = 5; block < 10; block++)
  2062. if (__idetape_analyze_headers(drive, block))
  2063. goto ok;
  2064. for (block = 0xbae; block < 0xbb3; block++) /* 2990 - 2994 */
  2065. if (__idetape_analyze_headers(drive, block))
  2066. goto ok;
  2067. printk(KERN_ERR "ide-tape: %s: failed to find valid ADRL headern", tape->name);
  2068. return 0;
  2069. ok:
  2070. if (position < OS_DATA_STARTFRAME1)
  2071. position = OS_DATA_STARTFRAME1;
  2072. idetape_position_tape(drive, position, 0, 0);
  2073. tape->header_ok = 1;
  2074. return 1;
  2075. }
  2076. /*
  2077.  * Our character device open function.
  2078.  */
  2079. static int idetape_chrdev_open (struct inode *inode, struct file *filp)
  2080. {
  2081. ide_drive_t *drive;
  2082. idetape_tape_t *tape;
  2083. idetape_pc_t pc;
  2084. unsigned int minor=MINOR (inode->i_rdev);
  2085. #if IDETAPE_DEBUG_LOG
  2086. printk (KERN_INFO "ide-tape: Reached idetape_chrdev_openn");
  2087. #endif /* IDETAPE_DEBUG_LOG */
  2088. if ((drive = get_drive_ptr (inode->i_rdev)) == NULL)
  2089. return -ENXIO;
  2090. tape = drive->driver_data;
  2091. if (test_and_set_bit (IDETAPE_BUSY, &tape->flags))
  2092. return -EBUSY;
  2093. MOD_INC_USE_COUNT;
  2094. if (!tape->onstream) {
  2095. idetape_read_position(drive);
  2096. if (!test_bit (IDETAPE_ADDRESS_VALID, &tape->flags))
  2097. (void) idetape_rewind_tape (drive);
  2098. } else {
  2099. if (minor & 64) {
  2100. tape->tape_block_size = tape->stage_size = 32768 + 512;
  2101. tape->raw = 1;
  2102. } else {
  2103. tape->tape_block_size = tape->stage_size = 32768;
  2104. tape->raw = 0;
  2105. }
  2106.                 idetape_onstream_mode_sense_tape_parameter_page(drive, tape->debug_level);
  2107. }
  2108. if (idetape_wait_ready(drive, 60 * HZ)) {
  2109. clear_bit(IDETAPE_BUSY, &tape->flags);
  2110. printk(KERN_ERR "ide-tape: %s: drive not readyn", tape->name);
  2111. MOD_DEC_USE_COUNT;
  2112. return -EBUSY;
  2113. }
  2114. idetape_read_position(drive);
  2115. MOD_DEC_USE_COUNT;
  2116. clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
  2117. if (tape->chrdev_direction == idetape_direction_none) {
  2118. MOD_INC_USE_COUNT;
  2119. if (idetape_create_prevent_cmd(drive, &pc, 1)) {
  2120. if (!idetape_queue_pc_tail (drive, &pc)) {
  2121. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
  2122. tape->door_locked = DOOR_LOCKED;
  2123. }
  2124. }
  2125. idetape_analyze_headers(drive);
  2126. }
  2127. tape->max_frames = tape->cur_frames = tape->req_buffer_fill = 0;
  2128. idetape_restart_speed_control(drive);
  2129. tape->restart_speed_control_req = 0;
  2130. return 0;
  2131. }
  2132. static void idetape_write_release (struct inode *inode)
  2133. {
  2134. ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
  2135. idetape_tape_t *tape = drive->driver_data;
  2136. unsigned int minor=MINOR (inode->i_rdev);
  2137. idetape_empty_write_pipeline (drive);
  2138. tape->merge_stage = __idetape_kmalloc_stage (tape, 1, 0);
  2139. if (tape->merge_stage != NULL) {
  2140. idetape_pad_zeros (drive, tape->tape_block_size * (tape->user_bs_factor - 1));
  2141. __idetape_kfree_stage (tape->merge_stage);
  2142. tape->merge_stage = NULL;
  2143. }
  2144. idetape_write_filemark(drive);
  2145. idetape_write_eod(drive);
  2146. idetape_flush_tape_buffers (drive);
  2147. idetape_write_header(drive, minor >= 128);
  2148. idetape_flush_tape_buffers (drive);
  2149. return;
  2150. }
  2151. /*
  2152.  * Our character device release function.
  2153.  */
  2154. static int idetape_chrdev_release (struct inode *inode, struct file *filp)
  2155. {
  2156. ide_drive_t *drive = get_drive_ptr (inode->i_rdev);
  2157. idetape_tape_t *tape;
  2158. idetape_pc_t pc;
  2159. unsigned int minor=MINOR (inode->i_rdev);
  2160. lock_kernel();
  2161. tape = drive->driver_data;
  2162. #if IDETAPE_DEBUG_LOG
  2163. if (tape->debug_level >= 3)
  2164. printk (KERN_INFO "ide-tape: Reached idetape_chrdev_releasen");
  2165. #endif /* IDETAPE_DEBUG_LOG */
  2166. if (tape->chrdev_direction == idetape_direction_write) {
  2167. idetape_write_release(inode);
  2168. }
  2169. if (tape->chrdev_direction == idetape_direction_read) {
  2170. if (minor < 128)
  2171. idetape_discard_read_pipeline (drive, 1);
  2172. else
  2173. idetape_wait_for_pipeline (drive);
  2174. }
  2175. if (tape->cache_stage != NULL) {
  2176. __idetape_kfree_stage (tape->cache_stage);
  2177. tape->cache_stage = NULL;
  2178. }
  2179. if (minor < 128)
  2180. (void) idetape_rewind_tape (drive);
  2181. if (tape->chrdev_direction == idetape_direction_none) {
  2182. if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) {
  2183. if (idetape_create_prevent_cmd(drive, &pc, 0))
  2184. if (!idetape_queue_pc_tail (drive, &pc))
  2185. tape->door_locked = DOOR_UNLOCKED;
  2186. }
  2187. MOD_DEC_USE_COUNT;
  2188. }
  2189. clear_bit (IDETAPE_BUSY, &tape->flags);
  2190. unlock_kernel();
  2191. return 0;
  2192. }
  2193. /*
  2194.  * idetape_identify_device is called to check the contents of the
  2195.  * ATAPI IDENTIFY command results. We return:
  2196.  *
  2197.  * 1 If the tape can be supported by us, based on the information
  2198.  * we have so far.
  2199.  *
  2200.  * 0  If this tape driver is not currently supported by us.
  2201.  */
  2202. static int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
  2203. {
  2204. struct idetape_id_gcw gcw;
  2205. #if IDETAPE_DEBUG_INFO
  2206. unsigned short mask,i;
  2207. #endif /* IDETAPE_DEBUG_INFO */
  2208. if (!id)
  2209. return 0;
  2210. *((unsigned short *) &gcw) = id->config;
  2211. #if IDETAPE_DEBUG_INFO
  2212. printk (KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parametersn");
  2213. printk (KERN_INFO "ide-tape: Protocol Type: ");
  2214. switch (gcw.protocol) {
  2215. case 0: case 1: printk (KERN_INFO "ATAn");break;
  2216. case 2: printk (KERN_INFO "ATAPIn");break;
  2217. case 3: printk (KERN_INFO "Reserved (Unknown to ide-tape)n");break;
  2218. }
  2219. printk (KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
  2220. switch (gcw.device_type) {
  2221. case 0: printk (KERN_INFO "Direct-access Devicen");break;
  2222. case 1: printk (KERN_INFO "Streaming Tape Devicen");break;
  2223. case 2: case 3: case 4: printk (KERN_INFO "Reservedn");break;
  2224. case 5: printk (KERN_INFO "CD-ROM Devicen");break;
  2225. case 6: printk (KERN_INFO "Reservedn");
  2226. case 7: printk (KERN_INFO "Optical memory Devicen");break;
  2227. case 0x1f: printk (KERN_INFO "Unknown or no Device typen");break;
  2228. default: printk (KERN_INFO "Reservedn");
  2229. }
  2230. printk (KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yesn":"Non");
  2231. printk (KERN_INFO "ide-tape: Command Packet DRQ Type: ");
  2232. switch (gcw.drq_type) {
  2233. case 0: printk (KERN_INFO "Microprocessor DRQn");break;
  2234. case 1: printk (KERN_INFO "Interrupt DRQn");break;
  2235. case 2: printk (KERN_INFO "Accelerated DRQn");break;
  2236. case 3: printk (KERN_INFO "Reservedn");break;
  2237. }
  2238. printk (KERN_INFO "ide-tape: Command Packet Size: ");
  2239. switch (gcw.packet_size) {
  2240. case 0: printk (KERN_INFO "12 bytesn");break;
  2241. case 1: printk (KERN_INFO "16 bytesn");break;
  2242. default: printk (KERN_INFO "Reservedn");break;
  2243. }
  2244. printk (KERN_INFO "ide-tape: Model: %.40sn",id->model);
  2245. printk (KERN_INFO "ide-tape: Firmware Revision: %.8sn",id->fw_rev);
  2246. printk (KERN_INFO "ide-tape: Serial Number: %.20sn",id->serial_no);
  2247. printk (KERN_INFO "ide-tape: Write buffer size: %d bytesn",id->buf_size*512);
  2248. printk (KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yesn":"Non");
  2249. printk (KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yesn":"Non");
  2250. printk (KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yesn":"Non");
  2251. printk (KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yesn":"Unknownn");
  2252. printk (KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yesn":"Non");
  2253. printk (KERN_INFO "ide-tape: PIO Cycle Timing Category: %dn",id->tPIO);
  2254. printk (KERN_INFO "ide-tape: DMA Cycle Timing Category: %dn",id->tDMA);
  2255. printk (KERN_INFO "ide-tape: Single Word DMA supported modes: ");
  2256. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  2257. if (id->dma_1word & mask)
  2258. printk (KERN_INFO "%d ",i);
  2259. if (id->dma_1word & (mask << 8))
  2260. printk (KERN_INFO "(active) ");
  2261. }
  2262. printk (KERN_INFO "n");
  2263. printk (KERN_INFO "ide-tape: Multi Word DMA supported modes: ");
  2264. for (i=0,mask=1;i<8;i++,mask=mask << 1) {
  2265. if (id->dma_mword & mask)
  2266. printk (KERN_INFO "%d ",i);
  2267. if (id->dma_mword & (mask << 8))
  2268. printk (KERN_INFO "(active) ");
  2269. }
  2270. printk (KERN_INFO "n");
  2271. if (id->field_valid & 0x0002) {
  2272. printk (KERN_INFO "ide-tape: Enhanced PIO Modes: %sn",id->eide_pio_modes & 1 ? "Mode 3":"None");
  2273. printk (KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: ");
  2274. if (id->eide_dma_min == 0)
  2275. printk (KERN_INFO "Not supportedn");
  2276. else
  2277. printk (KERN_INFO "%d nsn",id->eide_dma_min);
  2278. printk (KERN_INFO "ide-tape: Manufacturer's Recommended Multi-word cycle: ");
  2279. if (id->eide_dma_time == 0)
  2280. printk (KERN_INFO "Not supportedn");
  2281. else
  2282. printk (KERN_INFO "%d nsn",id->eide_dma_time);
  2283. printk (KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: ");
  2284. if (id->eide_pio == 0)
  2285. printk (KERN_INFO "Not supportedn");
  2286. else
  2287. printk (KERN_INFO "%d nsn",id->eide_pio);
  2288. printk (KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: ");
  2289. if (id->eide_pio_iordy == 0)
  2290. printk (KERN_INFO "Not supportedn");
  2291. else
  2292. printk (KERN_INFO "%d nsn",id->eide_pio_iordy);
  2293. } else
  2294. printk (KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.n");
  2295. #endif /* IDETAPE_DEBUG_INFO */
  2296. /* Check that we can support this device */
  2297. if (gcw.protocol !=2 )
  2298. printk (KERN_ERR "ide-tape: Protocol is not ATAPIn");
  2299. else if (gcw.device_type != 1)
  2300. printk (KERN_ERR "ide-tape: Device type is not set to tapen");
  2301. else if (!gcw.removable)
  2302. printk (KERN_ERR "ide-tape: The removable flag is not setn");
  2303. else if (gcw.packet_size != 0) {
  2304. printk (KERN_ERR "ide-tape: Packet size is not 12 bytes longn");
  2305. if (gcw.packet_size == 1)
  2306. printk (KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supportedn");
  2307. } else
  2308. return 1;
  2309. return 0;
  2310. }
  2311. /*
  2312.  * Notify vendor ID to the OnStream tape drive
  2313.  */
  2314. static void idetape_onstream_set_vendor (ide_drive_t *drive, char *vendor)
  2315. {
  2316. idetape_pc_t pc;
  2317. idetape_mode_parameter_header_t *header;
  2318. idetape_create_mode_select_cmd(&pc, sizeof(*header) + 8);
  2319. pc.buffer[0] = 3 + 8; /* Mode Data Length */
  2320. pc.buffer[1] = 0; /* Medium Type - ignoring */
  2321. pc.buffer[2] = 0; /* Reserved */
  2322. pc.buffer[3] = 0; /* Block Descriptor Length */
  2323. pc.buffer[4 + 0] = 0x36 | (1 << 7);
  2324. pc.buffer[4 + 1] = 6;
  2325. pc.buffer[4 + 2] = vendor[0];
  2326. pc.buffer[4 + 3] = vendor[1];
  2327. pc.buffer[4 + 4] = vendor[2];
  2328. pc.buffer[4 + 5] = vendor[3];
  2329. pc.buffer[4 + 6] = 0;
  2330. pc.buffer[4 + 7] = 0;
  2331. if (idetape_queue_pc_tail (drive, &pc))
  2332. printk (KERN_ERR "ide-tape: Couldn't set vendor name to %sn", vendor);
  2333. }
  2334. /*
  2335.  * Various unused OnStream commands
  2336.  */
  2337. #if ONSTREAM_DEBUG
  2338. static void idetape_onstream_set_retries (ide_drive_t *drive, int retries)
  2339. {
  2340. idetape_pc_t pc;
  2341. idetape_create_mode_select_cmd(&pc, sizeof(idetape_mode_parameter_header_t) + 4);
  2342. pc.buffer[0] = 3 + 4;
  2343. pc.buffer[1] = 0; /* Medium Type - ignoring */
  2344. pc.buffer[2] = 0; /* Reserved */
  2345. pc.buffer[3] = 0; /* Block Descriptor Length */
  2346. pc.buffer[4 + 0] = 0x2f | (1 << 7);
  2347. pc.buffer[4 + 1] = 2;
  2348. pc.buffer[4 + 2] = 4;
  2349. pc.buffer[4 + 3] = retries;
  2350. if (idetape_queue_pc_tail (drive, &pc))
  2351. printk (KERN_ERR "ide-tape: Couldn't set retries to %dn", retries);
  2352. }
  2353. #endif
  2354. /*
  2355.  * Configure 32.5KB block size.
  2356.  */
  2357. static void idetape_onstream_configure_block_size (ide_drive_t *drive)
  2358. {
  2359. idetape_pc_t pc;
  2360. idetape_mode_parameter_header_t *header;
  2361. idetape_block_size_page_t *bs;
  2362. /*
  2363.  * Get the current block size from the block size mode page
  2364.  */
  2365. idetape_create_mode_sense_cmd (&pc, IDETAPE_BLOCK_SIZE_PAGE);
  2366. if (idetape_queue_pc_tail (drive, &pc))
  2367. printk (KERN_ERR "ide-tape: can't get tape block size mode pagen");
  2368. header = (idetape_mode_parameter_header_t *) pc.buffer;
  2369. bs = (idetape_block_size_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
  2370. #if IDETAPE_DEBUG_INFO
  2371. printk(KERN_INFO "ide-tape: 32KB play back: %sn", bs->play32 ? "Yes" : "No");
  2372. printk(KERN_INFO "ide-tape: 32.5KB play back: %sn", bs->play32_5 ? "Yes" : "No");
  2373. printk(KERN_INFO "ide-tape: 32KB record: %sn", bs->record32 ? "Yes" : "No");
  2374. printk(KERN_INFO "ide-tape: 32.5KB record: %sn", bs->record32_5 ? "Yes" : "No");
  2375. #endif /* IDETAPE_DEBUG_INFO */
  2376. /*
  2377.  * Configure default auto columns mode, 32.5KB block size
  2378.  */ 
  2379. bs->one = 1;
  2380. bs->play32 = 0;
  2381. bs->play32_5 = 1;
  2382. bs->record32 = 0;
  2383. bs->record32_5 = 1;
  2384. idetape_create_mode_select_cmd(&pc, sizeof(*header) + sizeof(*bs));
  2385. if (idetape_queue_pc_tail (drive, &pc))
  2386. printk (KERN_ERR "ide-tape: Couldn't set tape block size mode pagen");
  2387. #if ONSTREAM_DEBUG
  2388. /*
  2389.  * In debug mode, we want to see as many errors as possible
  2390.  * to test the error recovery mechanism.
  2391.  */
  2392. idetape_onstream_set_retries(drive, 0);
  2393. #endif
  2394. }
  2395. /*
  2396.  * Use INQUIRY to get the firmware revision
  2397.  */
  2398. static void idetape_get_inquiry_results (ide_drive_t *drive)
  2399. {
  2400. char *r;
  2401. idetape_tape_t *tape = drive->driver_data;
  2402. idetape_pc_t pc;
  2403. idetape_inquiry_result_t *inquiry;
  2404. idetape_create_inquiry_cmd(&pc);
  2405. if (idetape_queue_pc_tail (drive, &pc)) {
  2406. printk (KERN_ERR "ide-tape: %s: can't get INQUIRY resultsn", tape->name);
  2407. return;
  2408. }
  2409. inquiry = (idetape_inquiry_result_t *) pc.buffer;
  2410. memcpy(tape->vendor_id, inquiry->vendor_id, 8);
  2411. memcpy(tape->product_id, inquiry->product_id, 16);
  2412. memcpy(tape->firmware_revision, inquiry->revision_level, 4);
  2413. ide_fixstring(tape->vendor_id, 10, 0);
  2414. ide_fixstring(tape->product_id, 18, 0);
  2415. ide_fixstring(tape->firmware_revision, 6, 0);
  2416. r = tape->firmware_revision;
  2417. if (*(r + 1) == '.')
  2418. tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
  2419. else if (tape->onstream)
  2420. tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 1) - '0') * 10 + *(r + 2) - '0';
  2421. printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %sn", drive->name, tape->name, tape->vendor_id, tape->product_id, tape->firmware_revision);
  2422. }
  2423. /*
  2424.  * Configure the OnStream ATAPI tape drive for default operation
  2425.  */
  2426. static void idetape_configure_onstream (ide_drive_t *drive)
  2427. {
  2428. idetape_tape_t *tape = drive->driver_data;
  2429. if (tape->firmware_revision_num < 105) {
  2430. printk(KERN_INFO "ide-tape: %s: Old OnStream firmware revision detected (%s)n", tape->name, tape->firmware_revision);
  2431. printk(KERN_INFO "ide-tape: %s: An upgrade to version 1.05 or above is recommendedn", tape->name);
  2432. }
  2433. /*
  2434.  * Configure 32.5KB (data+aux) block size.
  2435.  */
  2436. idetape_onstream_configure_block_size(drive);
  2437. /*
  2438.  * Set vendor name to 'LIN3' for "Linux support version 3".
  2439.  */
  2440. idetape_onstream_set_vendor(drive, "LIN3");
  2441. }
  2442. /*
  2443.  * idetape_get_mode_sense_parameters asks the tape about its various
  2444.  * parameters. This may work for other drives to???
  2445.  */
  2446. static void idetape_onstream_mode_sense_tape_parameter_page(ide_drive_t *drive, int debug)
  2447. {
  2448. idetape_tape_t *tape = drive->driver_data;
  2449. idetape_pc_t pc;
  2450. idetape_mode_parameter_header_t *header;
  2451. onstream_tape_paramtr_page_t *prm;
  2452. idetape_create_mode_sense_cmd (&pc, IDETAPE_PARAMTR_PAGE);
  2453. if (idetape_queue_pc_tail (drive, &pc)) {
  2454. printk (KERN_ERR "ide-tape: Can't get tape parameters page - probably no tape inserted in onstream driven");
  2455. return;
  2456. }
  2457. header = (idetape_mode_parameter_header_t *) pc.buffer;
  2458. prm = (onstream_tape_paramtr_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
  2459.         tape->capacity = ntohs(prm->segtrk) * ntohs(prm->trks);
  2460.         if (debug) {
  2461.     printk (KERN_INFO "ide-tape: %s <-> %s: Tape length %dMB (%d frames/track, %d tracks = %d blocks, density: %dKbpi)n",
  2462.                drive->name, tape->name, tape->capacity/32, ntohs(prm->segtrk), ntohs(prm->trks), tape->capacity, prm->density);
  2463.         }
  2464.         return;
  2465. }
  2466. /*
  2467.  * idetape_get_mode_sense_results asks the tape about its various
  2468.  * parameters. In particular, we will adjust our data transfer buffer
  2469.  * size to the recommended value as returned by the tape.
  2470.  */
  2471. static void idetape_get_mode_sense_results (ide_drive_t *drive)
  2472. {
  2473. idetape_tape_t *tape = drive->driver_data;
  2474. idetape_pc_t pc;
  2475. idetape_mode_parameter_header_t *header;
  2476. idetape_capabilities_page_t *capabilities;
  2477. idetape_create_mode_sense_cmd (&pc, IDETAPE_CAPABILITIES_PAGE);
  2478. if (idetape_queue_pc_tail (drive, &pc)) {
  2479. printk (KERN_ERR "ide-tape: Can't get tape parameters - assuming some default valuesn");
  2480. tape->tape_block_size = 512; tape->capabilities.ctl = 52;
  2481. tape->capabilities.speed = 450; tape->capabilities.buffer_size = 6 * 52;
  2482. return;
  2483. }
  2484. header = (idetape_mode_parameter_header_t *) pc.buffer;
  2485. capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
  2486. capabilities->max_speed = ntohs (capabilities->max_speed);
  2487. capabilities->ctl = ntohs (capabilities->ctl);
  2488. capabilities->speed = ntohs (capabilities->speed);
  2489. capabilities->buffer_size = ntohs (capabilities->buffer_size);
  2490. if (!capabilities->speed) {
  2491. printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)n", drive->name);
  2492. capabilities->speed = 650;
  2493. }
  2494. if (!capabilities->max_speed) {
  2495. printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)n", drive->name);
  2496. capabilities->max_speed = 650;
  2497. }
  2498. tape->capabilities = *capabilities; /* Save us a copy */
  2499. if (capabilities->blk512)
  2500. tape->tape_block_size = 512;
  2501. else if (capabilities->blk1024)
  2502. tape->tape_block_size = 1024;
  2503. else if (tape->onstream && capabilities->blk32768)
  2504. tape->tape_block_size = 32768;
  2505. #if IDETAPE_DEBUG_INFO
  2506. printk (KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet commandn");
  2507. printk (KERN_INFO "ide-tape: Mode Parameter Header:n");
  2508. printk (KERN_INFO "ide-tape: Mode Data Length - %dn",header->mode_data_length);
  2509. printk (KERN_INFO "ide-tape: Medium Type - %dn",header->medium_type);
  2510. printk (KERN_INFO "ide-tape: Device Specific Parameter - %dn",header->dsp);
  2511. printk (KERN_INFO "ide-tape: Block Descriptor Length - %dn",header->bdl);
  2512. printk (KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:n");
  2513. printk (KERN_INFO "ide-tape: Page code - %dn",capabilities->page_code);
  2514. printk (KERN_INFO "ide-tape: Page length - %dn",capabilities->page_length);
  2515. printk (KERN_INFO "ide-tape: Read only - %sn",capabilities->ro ? "Yes":"No");
  2516. printk (KERN_INFO "ide-tape: Supports reverse space - %sn",capabilities->sprev ? "Yes":"No");
  2517. printk (KERN_INFO "ide-tape: Supports erase initiated formatting - %sn",capabilities->efmt ? "Yes":"No");
  2518. printk (KERN_INFO "ide-tape: Supports QFA two Partition format - %sn",capabilities->qfa ? "Yes":"No");
  2519. printk (KERN_INFO "ide-tape: Supports locking the medium - %sn",capabilities->lock ? "Yes":"No");
  2520. printk (KERN_INFO "ide-tape: The volume is currently locked - %sn",capabilities->locked ? "Yes":"No");
  2521. printk (KERN_INFO "ide-tape: The device defaults in the prevent state - %sn",capabilities->prevent ? "Yes":"No");
  2522. printk (KERN_INFO "ide-tape: Supports ejecting the medium - %sn",capabilities->eject ? "Yes":"No");
  2523. printk (KERN_INFO "ide-tape: Supports error correction - %sn",capabilities->ecc ? "Yes":"No");
  2524. printk (KERN_INFO "ide-tape: Supports data compression - %sn",capabilities->cmprs ? "Yes":"No");
  2525. printk (KERN_INFO "ide-tape: Supports 512 bytes block size - %sn",capabilities->blk512 ? "Yes":"No");
  2526. printk (KERN_INFO "ide-tape: Supports 1024 bytes block size - %sn",capabilities->blk1024 ? "Yes":"No");
  2527. printk (KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %sn",capabilities->blk32768 ? "Yes":"No");
  2528. printk (KERN_INFO "ide-tape: Maximum supported speed in KBps - %dn",capabilities->max_speed);
  2529. printk (KERN_INFO "ide-tape: Continuous transfer limits in blocks - %dn",capabilities->ctl);
  2530. printk (KERN_INFO "ide-tape: Current speed in KBps - %dn",capabilities->speed);
  2531. printk (KERN_INFO "ide-tape: Buffer size - %dn",capabilities->buffer_size*512);
  2532. #endif /* IDETAPE_DEBUG_INFO */
  2533. }
  2534. /*
  2535.  * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
  2536.  * and if it succeeds sets the tape block size with the reported value
  2537.  */
  2538. static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
  2539. {
  2540. idetape_tape_t *tape = drive->driver_data;
  2541. idetape_pc_t pc;
  2542. idetape_mode_parameter_header_t *header;
  2543. idetape_parameter_block_descriptor_t *block_descrp;
  2544. idetape_create_mode_sense_cmd (&pc, IDETAPE_BLOCK_DESCRIPTOR);
  2545. if (idetape_queue_pc_tail (drive, &pc)) {
  2546. printk (KERN_ERR "ide-tape: Can't get block descriptorn");
  2547. if (tape->tape_block_size == 0) {
  2548. printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32kn");
  2549. tape->tape_block_size =  32768;
  2550. }
  2551. return;
  2552. }
  2553. header = (idetape_mode_parameter_header_t *) pc.buffer;
  2554. block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
  2555. tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
  2556. #if IDETAPE_DEBUG_INFO
  2557. printk (KERN_INFO "ide-tape: Adjusted block size - %dn", tape->tape_block_size);
  2558. #endif /* IDETAPE_DEBUG_INFO */
  2559. }
  2560. static void idetape_add_settings (ide_drive_t *drive)
  2561. {
  2562. idetape_tape_t *tape = drive->driver_data;
  2563. /*
  2564.  * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
  2565.  */
  2566. ide_add_setting(drive, "buffer", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
  2567. ide_add_setting(drive, "pipeline_min", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
  2568. ide_add_setting(drive, "pipeline", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
  2569. ide_add_setting(drive, "pipeline_max", SETTING_RW, -1, -1, TYPE_INT, 2, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
  2570. ide_add_setting(drive, "pipeline_used",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
  2571. ide_add_setting(drive, "pipeline_pending",SETTING_READ,-1, -1, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
  2572. ide_add_setting(drive, "speed", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
  2573. ide_add_setting(drive, "stage", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
  2574. ide_add_setting(drive, "tdsc", SETTING_RW, -1, -1, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
  2575. ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  2576. ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
  2577. ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed, NULL);
  2578. ide_add_setting(drive, "avg_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
  2579. ide_add_setting(drive, "debug_level",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
  2580. if (tape->onstream) {
  2581. ide_add_setting(drive, "cur_frames", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->cur_frames, NULL);
  2582. ide_add_setting(drive, "max_frames", SETTING_READ, -1, -1, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->max_frames, NULL);
  2583. ide_add_setting(drive, "insert_speed", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->insert_speed, NULL);
  2584. ide_add_setting(drive, "speed_control",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->speed_control, NULL);
  2585. ide_add_setting(drive, "tape_still_time",SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->tape_still_time, NULL);
  2586. ide_add_setting(drive, "max_insert_speed",SETTING_RW, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->max_insert_speed, NULL);
  2587. ide_add_setting(drive, "insert_size", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->insert_size, NULL);
  2588. ide_add_setting(drive, "capacity", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->capacity, NULL);
  2589. ide_add_setting(drive, "first_frame", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->first_frame_position, NULL);
  2590. ide_add_setting(drive, "logical_blk", SETTING_READ, -1, -1, TYPE_INT, 0, 0xffff, 1, 1, &tape->logical_blk_num, NULL);
  2591. }
  2592. }
  2593. /*
  2594.  * ide_setup is called to:
  2595.  *
  2596.  * 1. Initialize our various state variables.
  2597.  * 2. Ask the tape for its capabilities.
  2598.  * 3. Allocate a buffer which will be used for data
  2599.  * transfer. The buffer size is chosen based on
  2600.  * the recommendation which we received in step (2).
  2601.  *
  2602.  * Note that at this point ide.c already assigned us an irq, so that
  2603.  * we can queue requests here and wait for their completion.
  2604.  */
  2605. static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
  2606. {
  2607. unsigned long t1, tmid, tn, t;
  2608. int speed;
  2609. struct idetape_id_gcw gcw;
  2610. int stage_size;
  2611. struct sysinfo si;
  2612. memset (tape, 0, sizeof (idetape_tape_t));
  2613. spin_lock_init(&tape->spinlock);
  2614. drive->driver_data = tape;
  2615. drive->ready_stat = 0; /* An ATAPI device ignores DRDY */
  2616. if (strstr(drive->id->model, "OnStream DI-"))
  2617. tape->onstream = 1;
  2618. drive->dsc_overlap = 1;
  2619. #ifdef CONFIG_BLK_DEV_IDEPCI
  2620. if (!tape->onstream && HWIF(drive)->pci_dev != NULL) {
  2621. /*
  2622.  * These two ide-pci host adapters appear to need DSC overlap disabled.
  2623.  * This probably needs further analysis.
  2624.  */
  2625. if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) ||
  2626.     (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) {
  2627. printk(KERN_INFO "ide-tape: %s: disabling DSC overlapn", tape->name);
  2628.      drive->dsc_overlap = 0;
  2629. }
  2630. }
  2631. #endif /* CONFIG_BLK_DEV_IDEPCI */
  2632. tape->drive = drive;
  2633. tape->minor = minor;
  2634. tape->name[0] = 'h'; tape->name[1] = 't'; tape->name[2] = '0' + minor;
  2635. tape->chrdev_direction = idetape_direction_none;
  2636. tape->pc = tape->pc_stack;
  2637. tape->max_insert_speed = 10000;
  2638. tape->speed_control = 1;
  2639. *((unsigned short *) &gcw) = drive->id->config;
  2640. if (gcw.drq_type == 1)
  2641. set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
  2642. tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
  2643. idetape_get_inquiry_results(drive);
  2644. idetape_get_mode_sense_results(drive);
  2645. idetape_get_blocksize_from_block_descriptor(drive);
  2646. if (tape->onstream) {
  2647. idetape_onstream_mode_sense_tape_parameter_page(drive, 1);
  2648. idetape_configure_onstream(drive);
  2649. }
  2650. tape->user_bs_factor = 1;
  2651. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  2652. while (tape->stage_size > 0xffff) {
  2653. printk (KERN_NOTICE "ide-tape: decreasing stage sizen");
  2654. tape->capabilities.ctl /= 2;
  2655. tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
  2656. }
  2657. stage_size = tape->stage_size;
  2658. if (tape->onstream)
  2659. stage_size = 32768 + 512;
  2660. tape->pages_per_stage = stage_size / PAGE_SIZE;
  2661. if (stage_size % PAGE_SIZE) {
  2662. tape->pages_per_stage++;
  2663. tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
  2664. }
  2665. /*
  2666.  * Select the "best" DSC read/write polling frequency
  2667.  * and pipeline size.
  2668.  */
  2669. speed = IDE_MAX (tape->capabilities.speed, tape->capabilities.max_speed);
  2670. tape->max_stages = speed * 1000 * 10 / tape->stage_size;
  2671. /*
  2672.  *  Limit memory use for pipeline to 10% of physical memory
  2673.  */
  2674. si_meminfo(&si);
  2675. if ( tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
  2676. tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
  2677. tape->min_pipeline = tape->max_stages;
  2678. tape->max_pipeline = tape->max_stages * 2;
  2679. t1 = (tape->stage_size * HZ) / (speed * 1000);
  2680. tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
  2681. tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
  2682. if (tape->max_stages)
  2683. t = tn;
  2684. else
  2685. t = t1;
  2686. /*
  2687.  * Ensure that the number we got makes sense; limit
  2688.  * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
  2689.  */
  2690. tape->best_dsc_rw_frequency = IDE_MAX (IDE_MIN (t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
  2691. printk (KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, %dkB pipeline, %lums tDSC%sn",
  2692. drive->name, tape->name, tape->capabilities.speed, (tape->capabilities.buffer_size * 512) / tape->stage_size,
  2693. tape->stage_size / 1024, tape->max_stages * tape->stage_size / 1024,
  2694. tape->best_dsc_rw_frequency * 1000 / HZ, drive->using_dma ? ", DMA":"");
  2695. idetape_add_settings(drive);
  2696. }
  2697. static int idetape_cleanup (ide_drive_t *drive)
  2698. {
  2699. idetape_tape_t *tape = drive->driver_data;
  2700. int minor = tape->minor;
  2701. unsigned long flags;
  2702. save_flags (flags); /* all CPUs (overkill?) */
  2703. cli(); /* all CPUs (overkill?) */
  2704. if (test_bit (IDETAPE_BUSY, &tape->flags) || tape->first_stage != NULL || tape->merge_stage_size || drive->usage) {
  2705. restore_flags(flags); /* all CPUs (overkill?) */
  2706. return 1;
  2707. }
  2708. idetape_chrdevs[minor].drive = NULL;
  2709. restore_flags (flags); /* all CPUs (overkill?) */
  2710. DRIVER(drive)->busy = 0;
  2711. (void) ide_unregister_subdriver (drive);
  2712. drive->driver_data = NULL;
  2713. devfs_unregister (tape->de_r);
  2714. devfs_unregister (tape->de_n);
  2715. kfree (tape);
  2716. for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++)
  2717. if (idetape_chrdevs[minor].drive != NULL)
  2718. return 0;
  2719. devfs_unregister_chrdev (IDETAPE_MAJOR, "ht");
  2720. idetape_chrdev_present = 0;
  2721. return 0;
  2722. }
  2723. #ifdef CONFIG_PROC_FS
  2724. static int proc_idetape_read_name
  2725. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2726. {
  2727. ide_drive_t *drive = (ide_drive_t *) data;
  2728. idetape_tape_t *tape = drive->driver_data;
  2729. char *out = page;
  2730. int len;
  2731. len = sprintf(out, "%sn", tape->name);
  2732. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  2733. }
  2734. static ide_proc_entry_t idetape_proc[] = {
  2735. { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
  2736. { NULL, 0, NULL, NULL }
  2737. };
  2738. #else
  2739. #define idetape_proc NULL
  2740. #endif
  2741. int idetape_reinit(ide_drive_t *drive);
  2742. /*
  2743.  * IDE subdriver functions, registered with ide.c
  2744.  */
  2745. static ide_driver_t idetape_driver = {
  2746. name: "ide-tape",
  2747. version: IDETAPE_VERSION,
  2748. media: ide_tape,
  2749. busy: 1,
  2750. supports_dma: 1,
  2751. supports_dsc_overlap:  1,
  2752. cleanup: idetape_cleanup,
  2753. standby: NULL,
  2754. flushcache: NULL,
  2755. do_request: idetape_do_request,
  2756. end_request: idetape_end_request,
  2757. ioctl: idetape_blkdev_ioctl,
  2758. open: idetape_blkdev_open,
  2759. release: idetape_blkdev_release,
  2760. media_change: NULL,
  2761. revalidate: NULL,
  2762. pre_reset: idetape_pre_reset,
  2763. capacity: NULL,
  2764. proc: idetape_proc,
  2765. reinit: idetape_reinit,
  2766. ata_prebuilder: NULL,
  2767. atapi_prebuilder: NULL,
  2768. };
  2769. int idetape_init (void);
  2770. static ide_module_t idetape_module = {
  2771. IDE_DRIVER_MODULE,
  2772. idetape_init,
  2773. &idetape_driver,
  2774. NULL
  2775. };
  2776. /*
  2777.  * Our character device supporting functions, passed to register_chrdev.
  2778.  */
  2779. static struct file_operations idetape_fops = {
  2780. owner: THIS_MODULE,
  2781. read: idetape_chrdev_read,
  2782. write: idetape_chrdev_write,
  2783. ioctl: idetape_chrdev_ioctl,
  2784. open: idetape_chrdev_open,
  2785. release: idetape_chrdev_release,
  2786. };
  2787. int idetape_reinit (ide_drive_t *drive)
  2788. {
  2789. #if 0
  2790. idetape_tape_t *tape;
  2791. int minor, failed = 0, supported = 0;
  2792. /* DRIVER(drive)->busy++; */
  2793. MOD_INC_USE_COUNT;
  2794. #if ONSTREAM_DEBUG
  2795.         printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_initn");
  2796. #endif
  2797. if (!idetape_chrdev_present)
  2798. for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
  2799. idetape_chrdevs[minor].drive = NULL;
  2800. if ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) == NULL) {
  2801. ide_register_module (&idetape_module);
  2802. MOD_DEC_USE_COUNT;
  2803. #if ONSTREAM_DEBUG
  2804. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2805. #endif
  2806. return 0;
  2807. }
  2808. if (!idetape_chrdev_present &&
  2809.     devfs_register_chrdev (IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2810. printk (KERN_ERR "ide-tape: Failed to register character device interfacen");
  2811. MOD_DEC_USE_COUNT;
  2812. #if ONSTREAM_DEBUG
  2813. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2814. #endif
  2815. return -EBUSY;
  2816. }
  2817. do {
  2818. if (!idetape_identify_device (drive, drive->id)) {
  2819. printk (KERN_ERR "ide-tape: %s: not supported by this version of ide-tapen", drive->name);
  2820. continue;
  2821. }
  2822. if (drive->scsi) {
  2823. if (strstr(drive->id->model, "OnStream DI-30")) {
  2824. printk("ide-tape: ide-scsi emulation is not supported for %s.n", drive->id->model);
  2825. } else {
  2826. printk("ide-tape: passing drive %s to ide-scsi emulation.n", drive->name);
  2827. continue;
  2828. }
  2829. }
  2830. tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
  2831. if (tape == NULL) {
  2832. printk (KERN_ERR "ide-tape: %s: Can't allocate a tape structuren", drive->name);
  2833. continue;
  2834. }
  2835. if (ide_register_subdriver (drive, &idetape_driver, IDE_SUBDRIVER_VERSION)) {
  2836. printk (KERN_ERR "ide-tape: %s: Failed to register the driver with ide.cn", drive->name);
  2837. kfree (tape);
  2838. continue;
  2839. }
  2840. for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
  2841. idetape_setup (drive, tape, minor);
  2842. idetape_chrdevs[minor].drive = drive;
  2843. tape->de_r =
  2844.     devfs_register (drive->de, "mt", DEVFS_FL_DEFAULT,
  2845.     HWIF(drive)->major, minor,
  2846.     S_IFCHR | S_IRUGO | S_IWUGO,
  2847.     &idetape_fops, NULL);
  2848. tape->de_n =
  2849.     devfs_register (drive->de, "mtn", DEVFS_FL_DEFAULT,
  2850.     HWIF(drive)->major, minor + 128,
  2851.     S_IFCHR | S_IRUGO | S_IWUGO,
  2852.     &idetape_fops, NULL);
  2853. devfs_register_tape (tape->de_r);
  2854. supported++; failed--;
  2855. } while ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) != NULL);
  2856. if (!idetape_chrdev_present && !supported) {
  2857. devfs_unregister_chrdev (IDETAPE_MAJOR, "ht");
  2858. } else
  2859. idetape_chrdev_present = 1;
  2860. ide_register_module (&idetape_module);
  2861. MOD_DEC_USE_COUNT;
  2862. #if ONSTREAM_DEBUG
  2863. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2864. #endif
  2865. return 0;
  2866. #else
  2867. return 1;
  2868. #endif
  2869. }
  2870. MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
  2871. MODULE_LICENSE("GPL");
  2872. static void __exit idetape_exit (void)
  2873. {
  2874. ide_drive_t *drive;
  2875. int minor;
  2876. for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++) {
  2877. drive = idetape_chrdevs[minor].drive;
  2878. if (drive != NULL && idetape_cleanup (drive))
  2879. printk (KERN_ERR "ide-tape: %s: cleanup_module() called while still busyn", drive->name);
  2880. }
  2881. ide_unregister_module(&idetape_module);
  2882. }
  2883. /*
  2884.  * idetape_init will register the driver for each tape.
  2885.  */
  2886. int idetape_init (void)
  2887. {
  2888. ide_drive_t *drive;
  2889. idetape_tape_t *tape;
  2890. int minor, failed = 0, supported = 0;
  2891. /* DRIVER(drive)->busy++; */
  2892. MOD_INC_USE_COUNT;
  2893. #if ONSTREAM_DEBUG
  2894.         printk(KERN_INFO "ide-tape: MOD_INC_USE_COUNT in idetape_initn");
  2895. #endif
  2896. if (!idetape_chrdev_present)
  2897. for (minor = 0; minor < MAX_HWIFS * MAX_DRIVES; minor++ )
  2898. idetape_chrdevs[minor].drive = NULL;
  2899. if ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) == NULL) {
  2900. ide_register_module (&idetape_module);
  2901. MOD_DEC_USE_COUNT;
  2902. #if ONSTREAM_DEBUG
  2903. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2904. #endif
  2905. return 0;
  2906. }
  2907. if (!idetape_chrdev_present &&
  2908.     devfs_register_chrdev (IDETAPE_MAJOR, "ht", &idetape_fops)) {
  2909. printk (KERN_ERR "ide-tape: Failed to register character device interfacen");
  2910. MOD_DEC_USE_COUNT;
  2911. #if ONSTREAM_DEBUG
  2912. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2913. #endif
  2914. return -EBUSY;
  2915. }
  2916. do {
  2917. if (!idetape_identify_device (drive, drive->id)) {
  2918. printk (KERN_ERR "ide-tape: %s: not supported by this version of ide-tapen", drive->name);
  2919. continue;
  2920. }
  2921. if (drive->scsi) {
  2922. if (strstr(drive->id->model, "OnStream DI-")) {
  2923. printk("ide-tape: ide-scsi emulation is not supported for %s.n", drive->id->model);
  2924. } else {
  2925. printk("ide-tape: passing drive %s to ide-scsi emulation.n", drive->name);
  2926. continue;
  2927. }
  2928. }
  2929. tape = (idetape_tape_t *) kmalloc (sizeof (idetape_tape_t), GFP_KERNEL);
  2930. if (tape == NULL) {
  2931. printk (KERN_ERR "ide-tape: %s: Can't allocate a tape structuren", drive->name);
  2932. continue;
  2933. }
  2934. if (ide_register_subdriver (drive, &idetape_driver, IDE_SUBDRIVER_VERSION)) {
  2935. printk (KERN_ERR "ide-tape: %s: Failed to register the driver with ide.cn", drive->name);
  2936. kfree (tape);
  2937. continue;
  2938. }
  2939. for (minor = 0; idetape_chrdevs[minor].drive != NULL; minor++);
  2940. idetape_setup (drive, tape, minor);
  2941. idetape_chrdevs[minor].drive = drive;
  2942. tape->de_r =
  2943.     devfs_register (drive->de, "mt", DEVFS_FL_DEFAULT,
  2944.     HWIF(drive)->major, minor,
  2945.     S_IFCHR | S_IRUGO | S_IWUGO,
  2946.     &idetape_fops, NULL);
  2947. tape->de_n =
  2948.     devfs_register (drive->de, "mtn", DEVFS_FL_DEFAULT,
  2949.     HWIF(drive)->major, minor + 128,
  2950.     S_IFCHR | S_IRUGO | S_IWUGO,
  2951.     &idetape_fops, NULL);
  2952. devfs_register_tape (tape->de_r);
  2953. supported++; failed--;
  2954. } while ((drive = ide_scan_devices (ide_tape, idetape_driver.name, NULL, failed++)) != NULL);
  2955. if (!idetape_chrdev_present && !supported) {
  2956. devfs_unregister_chrdev (IDETAPE_MAJOR, "ht");
  2957. } else
  2958. idetape_chrdev_present = 1;
  2959. ide_register_module (&idetape_module);
  2960. MOD_DEC_USE_COUNT;
  2961. #if ONSTREAM_DEBUG
  2962. printk(KERN_INFO "ide-tape: MOD_DEC_USE_COUNT in idetape_initn");
  2963. #endif
  2964. return 0;
  2965. }
  2966. module_init(idetape_init);
  2967. module_exit(idetape_exit);