ide-scsi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
  3. * Copyright (C) 2004-2005 Bartlomiej Zolnierkiewicz
  4. */
  5. /*
  6. * Emulation of a SCSI host adapter for IDE ATAPI devices.
  7. *
  8. * With this driver, one can use the Linux SCSI drivers instead of the
  9. * native IDE ATAPI drivers.
  10. *
  11. * Ver 0.1 Dec 3 96 Initial version.
  12. * Ver 0.2 Jan 26 97 Fixed bug in cleanup_module() and added emulation
  13. * of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
  14. * to Janos Farkas for pointing this out.
  15. * Avoid using bitfields in structures for m68k.
  16. * Added Scatter/Gather and DMA support.
  17. * Ver 0.4 Dec 7 97 Add support for ATAPI PD/CD drives.
  18. * Use variable timeout for each command.
  19. * Ver 0.5 Jan 2 98 Fix previous PD/CD support.
  20. * Allow disabling of SCSI-6 to SCSI-10 transformation.
  21. * Ver 0.6 Jan 27 98 Allow disabling of SCSI command translation layer
  22. * for access through /dev/sg.
  23. * Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
  24. * Ver 0.7 Dec 04 98 Ignore commands where lun != 0 to avoid multiple
  25. * detection of devices with CONFIG_SCSI_MULTI_LUN
  26. * Ver 0.8 Feb 05 99 Optical media need translation too. Reverse 0.7.
  27. * Ver 0.9 Jul 04 99 Fix a bug in SG_SET_TRANSFORM.
  28. * Ver 0.91 Jun 10 02 Fix "off by one" error in transforms
  29. * Ver 0.92 Dec 31 02 Implement new SCSI mid level API
  30. */
  31. #define IDESCSI_VERSION "0.92"
  32. #include <linux/module.h>
  33. #include <linux/types.h>
  34. #include <linux/string.h>
  35. #include <linux/kernel.h>
  36. #include <linux/mm.h>
  37. #include <linux/ioport.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/errno.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/slab.h>
  42. #include <linux/ide.h>
  43. #include <linux/scatterlist.h>
  44. #include <linux/delay.h>
  45. #include <linux/mutex.h>
  46. #include <linux/bitops.h>
  47. #include <asm/io.h>
  48. #include <asm/uaccess.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_tcq.h>
  54. #include <scsi/sg.h>
  55. #define IDESCSI_DEBUG_LOG 0
  56. typedef struct idescsi_pc_s {
  57. u8 c[12]; /* Actual packet bytes */
  58. int request_transfer; /* Bytes to transfer */
  59. int actually_transferred; /* Bytes actually transferred */
  60. int buffer_size; /* Size of our data buffer */
  61. struct request *rq; /* The corresponding request */
  62. u8 *buffer; /* Data buffer */
  63. u8 *current_position; /* Pointer into the above buffer */
  64. struct scatterlist *sg; /* Scatter gather table */
  65. unsigned int sg_cnt; /* Number of entries in sg */
  66. int b_count; /* Bytes transferred from current entry */
  67. struct scsi_cmnd *scsi_cmd; /* SCSI command */
  68. void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
  69. unsigned long flags; /* Status/Action flags */
  70. unsigned long timeout; /* Command timeout */
  71. } idescsi_pc_t;
  72. /*
  73. * Packet command status bits.
  74. */
  75. #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
  76. #define PC_WRITING 1 /* Data direction */
  77. #define PC_TIMEDOUT 3 /* command timed out */
  78. #define PC_DMA_OK 4 /* Use DMA */
  79. /*
  80. * SCSI command transformation layer
  81. */
  82. #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
  83. /*
  84. * Log flags
  85. */
  86. #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
  87. typedef struct ide_scsi_obj {
  88. ide_drive_t *drive;
  89. ide_driver_t *driver;
  90. struct gendisk *disk;
  91. struct Scsi_Host *host;
  92. idescsi_pc_t *pc; /* Current packet command */
  93. unsigned long flags; /* Status/Action flags */
  94. unsigned long transform; /* SCSI cmd translation layer */
  95. unsigned long log; /* log flags */
  96. } idescsi_scsi_t;
  97. static DEFINE_MUTEX(idescsi_ref_mutex);
  98. static int idescsi_nocd; /* Set by module param to skip cd */
  99. #define ide_scsi_g(disk) \
  100. container_of((disk)->private_data, struct ide_scsi_obj, driver)
  101. static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
  102. {
  103. struct ide_scsi_obj *scsi = NULL;
  104. mutex_lock(&idescsi_ref_mutex);
  105. scsi = ide_scsi_g(disk);
  106. if (scsi)
  107. scsi_host_get(scsi->host);
  108. mutex_unlock(&idescsi_ref_mutex);
  109. return scsi;
  110. }
  111. static void ide_scsi_put(struct ide_scsi_obj *scsi)
  112. {
  113. mutex_lock(&idescsi_ref_mutex);
  114. scsi_host_put(scsi->host);
  115. mutex_unlock(&idescsi_ref_mutex);
  116. }
  117. static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
  118. {
  119. return (idescsi_scsi_t*) (&host[1]);
  120. }
  121. static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
  122. {
  123. return scsihost_to_idescsi(ide_drive->driver_data);
  124. }
  125. /*
  126. * Per ATAPI device status bits.
  127. */
  128. #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
  129. /*
  130. * ide-scsi requests.
  131. */
  132. #define IDESCSI_PC_RQ 90
  133. static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
  134. {
  135. while (bcount--)
  136. (void) HWIF(drive)->INB(IDE_DATA_REG);
  137. }
  138. static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
  139. {
  140. while (bcount--)
  141. HWIF(drive)->OUTB(0, IDE_DATA_REG);
  142. }
  143. /*
  144. * PIO data transfer routines using the scatter gather table.
  145. */
  146. static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
  147. {
  148. int count;
  149. char *buf;
  150. while (bcount) {
  151. count = min(pc->sg->length - pc->b_count, bcount);
  152. if (PageHighMem(sg_page(pc->sg))) {
  153. unsigned long flags;
  154. local_irq_save(flags);
  155. buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
  156. pc->sg->offset;
  157. drive->hwif->atapi_input_bytes(drive,
  158. buf + pc->b_count, count);
  159. kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
  160. local_irq_restore(flags);
  161. } else {
  162. buf = sg_virt(pc->sg);
  163. drive->hwif->atapi_input_bytes(drive,
  164. buf + pc->b_count, count);
  165. }
  166. bcount -= count; pc->b_count += count;
  167. if (pc->b_count == pc->sg->length) {
  168. if (!--pc->sg_cnt)
  169. break;
  170. pc->sg = sg_next(pc->sg);
  171. pc->b_count = 0;
  172. }
  173. }
  174. if (bcount) {
  175. printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
  176. idescsi_discard_data (drive, bcount);
  177. }
  178. }
  179. static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
  180. {
  181. int count;
  182. char *buf;
  183. while (bcount) {
  184. count = min(pc->sg->length - pc->b_count, bcount);
  185. if (PageHighMem(sg_page(pc->sg))) {
  186. unsigned long flags;
  187. local_irq_save(flags);
  188. buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
  189. pc->sg->offset;
  190. drive->hwif->atapi_output_bytes(drive,
  191. buf + pc->b_count, count);
  192. kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
  193. local_irq_restore(flags);
  194. } else {
  195. buf = sg_virt(pc->sg);
  196. drive->hwif->atapi_output_bytes(drive,
  197. buf + pc->b_count, count);
  198. }
  199. bcount -= count; pc->b_count += count;
  200. if (pc->b_count == pc->sg->length) {
  201. if (!--pc->sg_cnt)
  202. break;
  203. pc->sg = sg_next(pc->sg);
  204. pc->b_count = 0;
  205. }
  206. }
  207. if (bcount) {
  208. printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
  209. idescsi_output_zeros (drive, bcount);
  210. }
  211. }
  212. static void ide_scsi_hex_dump(u8 *data, int len)
  213. {
  214. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
  215. }
  216. static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
  217. {
  218. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  219. idescsi_pc_t *pc;
  220. struct request *rq;
  221. u8 *buf;
  222. /* stuff a sense request in front of our current request */
  223. pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
  224. rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
  225. buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
  226. if (!pc || !rq || !buf) {
  227. kfree(buf);
  228. kfree(rq);
  229. kfree(pc);
  230. return -ENOMEM;
  231. }
  232. ide_init_drive_cmd(rq);
  233. rq->special = (char *) pc;
  234. pc->rq = rq;
  235. pc->buffer = buf;
  236. pc->c[0] = REQUEST_SENSE;
  237. pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
  238. rq->cmd_type = REQ_TYPE_SENSE;
  239. pc->timeout = jiffies + WAIT_READY;
  240. /* NOTE! Save the failed packet command in "rq->buffer" */
  241. rq->buffer = (void *) failed_command->special;
  242. pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
  243. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  244. printk ("ide-scsi: %s: queue cmd = ", drive->name);
  245. ide_scsi_hex_dump(pc->c, 6);
  246. }
  247. rq->rq_disk = scsi->disk;
  248. return ide_do_drive_cmd(drive, rq, ide_preempt);
  249. }
  250. static int idescsi_end_request(ide_drive_t *, int, int);
  251. static ide_startstop_t
  252. idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  253. {
  254. if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
  255. /* force an abort */
  256. HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
  257. rq->errors++;
  258. idescsi_end_request(drive, 0, 0);
  259. return ide_stopped;
  260. }
  261. static ide_startstop_t
  262. idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
  263. {
  264. #if IDESCSI_DEBUG_LOG
  265. printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
  266. ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
  267. #endif
  268. rq->errors |= ERROR_MAX;
  269. idescsi_end_request(drive, 0, 0);
  270. return ide_stopped;
  271. }
  272. static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
  273. {
  274. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  275. struct request *rq = HWGROUP(drive)->rq;
  276. idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
  277. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  278. struct Scsi_Host *host;
  279. int errors = rq->errors;
  280. unsigned long flags;
  281. if (!blk_special_request(rq) && !blk_sense_request(rq)) {
  282. ide_end_request(drive, uptodate, nrsecs);
  283. return 0;
  284. }
  285. ide_end_drive_cmd (drive, 0, 0);
  286. if (blk_sense_request(rq)) {
  287. idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
  288. if (log) {
  289. printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
  290. ide_scsi_hex_dump(pc->buffer, 16);
  291. }
  292. memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
  293. kfree(pc->buffer);
  294. kfree(pc);
  295. kfree(rq);
  296. pc = opc;
  297. rq = pc->rq;
  298. pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
  299. ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
  300. } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
  301. if (log)
  302. printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
  303. drive->name, pc->scsi_cmd->serial_number);
  304. pc->scsi_cmd->result = DID_TIME_OUT << 16;
  305. } else if (errors >= ERROR_MAX) {
  306. pc->scsi_cmd->result = DID_ERROR << 16;
  307. if (log)
  308. printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  309. } else if (errors) {
  310. if (log)
  311. printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  312. if (!idescsi_check_condition(drive, rq))
  313. /* we started a request sense, so we'll be back, exit for now */
  314. return 0;
  315. pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
  316. } else {
  317. pc->scsi_cmd->result = DID_OK << 16;
  318. }
  319. host = pc->scsi_cmd->device->host;
  320. spin_lock_irqsave(host->host_lock, flags);
  321. pc->done(pc->scsi_cmd);
  322. spin_unlock_irqrestore(host->host_lock, flags);
  323. kfree(pc);
  324. kfree(rq);
  325. scsi->pc = NULL;
  326. return 0;
  327. }
  328. static inline unsigned long get_timeout(idescsi_pc_t *pc)
  329. {
  330. return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
  331. }
  332. static int idescsi_expiry(ide_drive_t *drive)
  333. {
  334. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  335. idescsi_pc_t *pc = scsi->pc;
  336. #if IDESCSI_DEBUG_LOG
  337. printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
  338. #endif
  339. set_bit(PC_TIMEDOUT, &pc->flags);
  340. return 0; /* we do not want the ide subsystem to retry */
  341. }
  342. /*
  343. * Our interrupt handler.
  344. */
  345. static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
  346. {
  347. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  348. ide_hwif_t *hwif = drive->hwif;
  349. idescsi_pc_t *pc = scsi->pc;
  350. struct request *rq = pc->rq;
  351. unsigned int temp;
  352. u16 bcount;
  353. u8 stat, ireason;
  354. #if IDESCSI_DEBUG_LOG
  355. printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
  356. #endif /* IDESCSI_DEBUG_LOG */
  357. if (test_bit(PC_TIMEDOUT, &pc->flags)){
  358. #if IDESCSI_DEBUG_LOG
  359. printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
  360. pc->scsi_cmd->serial_number, jiffies);
  361. #endif
  362. /* end this request now - scsi should retry it*/
  363. idescsi_end_request (drive, 1, 0);
  364. return ide_stopped;
  365. }
  366. if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
  367. #if IDESCSI_DEBUG_LOG
  368. printk ("ide-scsi: %s: DMA complete\n", drive->name);
  369. #endif /* IDESCSI_DEBUG_LOG */
  370. pc->actually_transferred=pc->request_transfer;
  371. (void) HWIF(drive)->ide_dma_end(drive);
  372. }
  373. /* Clear the interrupt */
  374. stat = drive->hwif->INB(IDE_STATUS_REG);
  375. if ((stat & DRQ_STAT) == 0) {
  376. /* No more interrupts */
  377. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  378. printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
  379. local_irq_enable_in_hardirq();
  380. if (stat & ERR_STAT)
  381. rq->errors++;
  382. idescsi_end_request (drive, 1, 0);
  383. return ide_stopped;
  384. }
  385. bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
  386. hwif->INB(IDE_BCOUNTL_REG);
  387. ireason = hwif->INB(IDE_IREASON_REG);
  388. if (ireason & CD) {
  389. printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
  390. return ide_do_reset (drive);
  391. }
  392. if (ireason & IO) {
  393. temp = pc->actually_transferred + bcount;
  394. if (temp > pc->request_transfer) {
  395. if (temp > pc->buffer_size) {
  396. printk(KERN_ERR "ide-scsi: The scsi wants to "
  397. "send us more data than expected "
  398. "- discarding data\n");
  399. temp = pc->buffer_size - pc->actually_transferred;
  400. if (temp) {
  401. clear_bit(PC_WRITING, &pc->flags);
  402. if (pc->sg)
  403. idescsi_input_buffers(drive, pc, temp);
  404. else
  405. drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
  406. printk(KERN_ERR "ide-scsi: transferred"
  407. " %d of %d bytes\n",
  408. temp, bcount);
  409. }
  410. pc->actually_transferred += temp;
  411. pc->current_position += temp;
  412. idescsi_discard_data(drive, bcount - temp);
  413. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  414. return ide_started;
  415. }
  416. #if IDESCSI_DEBUG_LOG
  417. printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
  418. #endif /* IDESCSI_DEBUG_LOG */
  419. }
  420. }
  421. if (ireason & IO) {
  422. clear_bit(PC_WRITING, &pc->flags);
  423. if (pc->sg)
  424. idescsi_input_buffers(drive, pc, bcount);
  425. else
  426. hwif->atapi_input_bytes(drive, pc->current_position,
  427. bcount);
  428. } else {
  429. set_bit(PC_WRITING, &pc->flags);
  430. if (pc->sg)
  431. idescsi_output_buffers(drive, pc, bcount);
  432. else
  433. hwif->atapi_output_bytes(drive, pc->current_position,
  434. bcount);
  435. }
  436. /* Update the current position */
  437. pc->actually_transferred += bcount;
  438. pc->current_position += bcount;
  439. /* And set the interrupt handler again */
  440. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  441. return ide_started;
  442. }
  443. static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
  444. {
  445. ide_hwif_t *hwif = drive->hwif;
  446. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  447. idescsi_pc_t *pc = scsi->pc;
  448. ide_startstop_t startstop;
  449. u8 ireason;
  450. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  451. printk(KERN_ERR "ide-scsi: Strange, packet command "
  452. "initiated yet DRQ isn't asserted\n");
  453. return startstop;
  454. }
  455. ireason = hwif->INB(IDE_IREASON_REG);
  456. if ((ireason & CD) == 0 || (ireason & IO)) {
  457. printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
  458. "issuing a packet command\n");
  459. return ide_do_reset (drive);
  460. }
  461. BUG_ON(HWGROUP(drive)->handler != NULL);
  462. /* Set the interrupt routine */
  463. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  464. /* Send the actual packet */
  465. drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
  466. if (test_bit (PC_DMA_OK, &pc->flags)) {
  467. set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  468. hwif->dma_start(drive);
  469. }
  470. return ide_started;
  471. }
  472. static inline int idescsi_set_direction(idescsi_pc_t *pc)
  473. {
  474. switch (pc->c[0]) {
  475. case READ_6: case READ_10: case READ_12:
  476. clear_bit(PC_WRITING, &pc->flags);
  477. return 0;
  478. case WRITE_6: case WRITE_10: case WRITE_12:
  479. set_bit(PC_WRITING, &pc->flags);
  480. return 0;
  481. default:
  482. return 1;
  483. }
  484. }
  485. static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
  486. {
  487. ide_hwif_t *hwif = drive->hwif;
  488. struct scatterlist *sg, *scsi_sg;
  489. int segments;
  490. if (!pc->request_transfer || pc->request_transfer % 1024)
  491. return 1;
  492. if (idescsi_set_direction(pc))
  493. return 1;
  494. sg = hwif->sg_table;
  495. scsi_sg = scsi_sglist(pc->scsi_cmd);
  496. segments = scsi_sg_count(pc->scsi_cmd);
  497. if (segments > hwif->sg_max_nents)
  498. return 1;
  499. hwif->sg_nents = segments;
  500. memcpy(sg, scsi_sg, sizeof(*sg) * segments);
  501. return 0;
  502. }
  503. /*
  504. * Issue a packet command
  505. */
  506. static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
  507. {
  508. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  509. ide_hwif_t *hwif = drive->hwif;
  510. u16 bcount;
  511. u8 dma = 0;
  512. scsi->pc=pc; /* Set the current packet command */
  513. pc->actually_transferred=0; /* We haven't transferred any data yet */
  514. pc->current_position=pc->buffer;
  515. /* Request to transfer the entire buffer at once */
  516. bcount = min(pc->request_transfer, 63 * 1024);
  517. if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
  518. hwif->sg_mapped = 1;
  519. dma = !hwif->dma_setup(drive);
  520. hwif->sg_mapped = 0;
  521. }
  522. SELECT_DRIVE(drive);
  523. ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK, bcount, dma);
  524. if (dma)
  525. set_bit(PC_DMA_OK, &pc->flags);
  526. if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
  527. ide_execute_command(drive, WIN_PACKETCMD, &idescsi_transfer_pc,
  528. get_timeout(pc), idescsi_expiry);
  529. return ide_started;
  530. } else {
  531. /* Issue the packet command */
  532. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  533. return idescsi_transfer_pc(drive);
  534. }
  535. }
  536. /*
  537. * idescsi_do_request is our request handling function.
  538. */
  539. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
  540. {
  541. #if IDESCSI_DEBUG_LOG
  542. printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
  543. printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
  544. #endif /* IDESCSI_DEBUG_LOG */
  545. if (blk_sense_request(rq) || blk_special_request(rq)) {
  546. return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
  547. }
  548. blk_dump_rq_flags(rq, "ide-scsi: unsup command");
  549. idescsi_end_request (drive, 0, 0);
  550. return ide_stopped;
  551. }
  552. #ifdef CONFIG_IDE_PROC_FS
  553. static void idescsi_add_settings(ide_drive_t *drive)
  554. {
  555. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  556. /*
  557. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  558. */
  559. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  560. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  561. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  562. ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  563. ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  564. }
  565. #else
  566. static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
  567. #endif
  568. /*
  569. * Driver initialization.
  570. */
  571. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  572. {
  573. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  574. set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
  575. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  576. #if IDESCSI_DEBUG_LOG
  577. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  578. #endif /* IDESCSI_DEBUG_LOG */
  579. idescsi_add_settings(drive);
  580. }
  581. static void ide_scsi_remove(ide_drive_t *drive)
  582. {
  583. struct Scsi_Host *scsihost = drive->driver_data;
  584. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  585. struct gendisk *g = scsi->disk;
  586. scsi_remove_host(scsihost);
  587. ide_proc_unregister_driver(drive, scsi->driver);
  588. ide_unregister_region(g);
  589. drive->driver_data = NULL;
  590. g->private_data = NULL;
  591. put_disk(g);
  592. ide_scsi_put(scsi);
  593. }
  594. static int ide_scsi_probe(ide_drive_t *);
  595. #ifdef CONFIG_IDE_PROC_FS
  596. static ide_proc_entry_t idescsi_proc[] = {
  597. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  598. { NULL, 0, NULL, NULL }
  599. };
  600. #endif
  601. static ide_driver_t idescsi_driver = {
  602. .gen_driver = {
  603. .owner = THIS_MODULE,
  604. .name = "ide-scsi",
  605. .bus = &ide_bus_type,
  606. },
  607. .probe = ide_scsi_probe,
  608. .remove = ide_scsi_remove,
  609. .version = IDESCSI_VERSION,
  610. .media = ide_scsi,
  611. .supports_dsc_overlap = 0,
  612. .do_request = idescsi_do_request,
  613. .end_request = idescsi_end_request,
  614. .error = idescsi_atapi_error,
  615. .abort = idescsi_atapi_abort,
  616. #ifdef CONFIG_IDE_PROC_FS
  617. .proc = idescsi_proc,
  618. #endif
  619. };
  620. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  621. {
  622. struct gendisk *disk = inode->i_bdev->bd_disk;
  623. struct ide_scsi_obj *scsi;
  624. if (!(scsi = ide_scsi_get(disk)))
  625. return -ENXIO;
  626. return 0;
  627. }
  628. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  629. {
  630. struct gendisk *disk = inode->i_bdev->bd_disk;
  631. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  632. ide_scsi_put(scsi);
  633. return 0;
  634. }
  635. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  636. unsigned int cmd, unsigned long arg)
  637. {
  638. struct block_device *bdev = inode->i_bdev;
  639. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  640. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  641. }
  642. static struct block_device_operations idescsi_ops = {
  643. .owner = THIS_MODULE,
  644. .open = idescsi_ide_open,
  645. .release = idescsi_ide_release,
  646. .ioctl = idescsi_ide_ioctl,
  647. };
  648. static int idescsi_slave_configure(struct scsi_device * sdp)
  649. {
  650. /* Configure detected device */
  651. sdp->use_10_for_rw = 1;
  652. sdp->use_10_for_ms = 1;
  653. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  654. return 0;
  655. }
  656. static const char *idescsi_info (struct Scsi_Host *host)
  657. {
  658. return "SCSI host adapter emulation for IDE ATAPI devices";
  659. }
  660. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  661. {
  662. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  663. if (cmd == SG_SET_TRANSFORM) {
  664. if (arg)
  665. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  666. else
  667. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  668. return 0;
  669. } else if (cmd == SG_GET_TRANSFORM)
  670. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  671. return -EINVAL;
  672. }
  673. static int idescsi_queue (struct scsi_cmnd *cmd,
  674. void (*done)(struct scsi_cmnd *))
  675. {
  676. struct Scsi_Host *host = cmd->device->host;
  677. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  678. ide_drive_t *drive = scsi->drive;
  679. struct request *rq = NULL;
  680. idescsi_pc_t *pc = NULL;
  681. if (!drive) {
  682. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  683. goto abort;
  684. }
  685. scsi = drive_to_idescsi(drive);
  686. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  687. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  688. if (rq == NULL || pc == NULL) {
  689. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  690. goto abort;
  691. }
  692. memset (pc->c, 0, 12);
  693. pc->flags = 0;
  694. pc->rq = rq;
  695. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  696. pc->buffer = NULL;
  697. pc->sg = scsi_sglist(cmd);
  698. pc->sg_cnt = scsi_sg_count(cmd);
  699. pc->b_count = 0;
  700. pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
  701. pc->scsi_cmd = cmd;
  702. pc->done = done;
  703. pc->timeout = jiffies + cmd->timeout_per_command;
  704. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  705. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  706. ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
  707. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  708. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  709. ide_scsi_hex_dump(pc->c, 12);
  710. }
  711. }
  712. ide_init_drive_cmd (rq);
  713. rq->special = (char *) pc;
  714. rq->cmd_type = REQ_TYPE_SPECIAL;
  715. spin_unlock_irq(host->host_lock);
  716. rq->rq_disk = scsi->disk;
  717. (void) ide_do_drive_cmd (drive, rq, ide_end);
  718. spin_lock_irq(host->host_lock);
  719. return 0;
  720. abort:
  721. kfree (pc);
  722. kfree (rq);
  723. cmd->result = DID_ERROR << 16;
  724. done(cmd);
  725. return 0;
  726. }
  727. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  728. {
  729. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  730. ide_drive_t *drive = scsi->drive;
  731. int busy;
  732. int ret = FAILED;
  733. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  734. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  735. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  736. if (!drive) {
  737. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  738. WARN_ON(1);
  739. goto no_drive;
  740. }
  741. /* First give it some more time, how much is "right" is hard to say :-( */
  742. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  743. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  744. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  745. spin_lock_irq(&ide_lock);
  746. /* If there is no pc running we're done (our interrupt took care of it) */
  747. if (!scsi->pc) {
  748. ret = SUCCESS;
  749. goto ide_unlock;
  750. }
  751. /* It's somewhere in flight. Does ide subsystem agree? */
  752. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  753. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  754. /*
  755. * FIXME - not sure this condition can ever occur
  756. */
  757. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  758. if (blk_sense_request(scsi->pc->rq))
  759. kfree(scsi->pc->buffer);
  760. kfree(scsi->pc->rq);
  761. kfree(scsi->pc);
  762. scsi->pc = NULL;
  763. ret = SUCCESS;
  764. }
  765. ide_unlock:
  766. spin_unlock_irq(&ide_lock);
  767. no_drive:
  768. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  769. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  770. return ret;
  771. }
  772. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  773. {
  774. struct request *req;
  775. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  776. ide_drive_t *drive = scsi->drive;
  777. int ready = 0;
  778. int ret = SUCCESS;
  779. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  780. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  781. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  782. if (!drive) {
  783. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  784. WARN_ON(1);
  785. return FAILED;
  786. }
  787. spin_lock_irq(cmd->device->host->host_lock);
  788. spin_lock(&ide_lock);
  789. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  790. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  791. spin_unlock(&ide_lock);
  792. spin_unlock_irq(cmd->device->host->host_lock);
  793. return FAILED;
  794. }
  795. /* kill current request */
  796. if (__blk_end_request(req, -EIO, 0))
  797. BUG();
  798. if (blk_sense_request(req))
  799. kfree(scsi->pc->buffer);
  800. kfree(scsi->pc);
  801. scsi->pc = NULL;
  802. kfree(req);
  803. /* now nuke the drive queue */
  804. while ((req = elv_next_request(drive->queue))) {
  805. if (__blk_end_request(req, -EIO, 0))
  806. BUG();
  807. }
  808. HWGROUP(drive)->rq = NULL;
  809. HWGROUP(drive)->handler = NULL;
  810. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  811. spin_unlock(&ide_lock);
  812. ide_do_reset(drive);
  813. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  814. do {
  815. spin_unlock_irq(cmd->device->host->host_lock);
  816. msleep(50);
  817. spin_lock_irq(cmd->device->host->host_lock);
  818. } while ( HWGROUP(drive)->handler );
  819. ready = drive_is_ready(drive);
  820. HWGROUP(drive)->busy--;
  821. if (!ready) {
  822. printk (KERN_ERR "ide-scsi: reset failed!\n");
  823. ret = FAILED;
  824. }
  825. spin_unlock_irq(cmd->device->host->host_lock);
  826. return ret;
  827. }
  828. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  829. sector_t capacity, int *parm)
  830. {
  831. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  832. ide_drive_t *drive = idescsi->drive;
  833. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  834. parm[0] = drive->bios_head;
  835. parm[1] = drive->bios_sect;
  836. parm[2] = drive->bios_cyl;
  837. }
  838. return 0;
  839. }
  840. static struct scsi_host_template idescsi_template = {
  841. .module = THIS_MODULE,
  842. .name = "idescsi",
  843. .info = idescsi_info,
  844. .slave_configure = idescsi_slave_configure,
  845. .ioctl = idescsi_ioctl,
  846. .queuecommand = idescsi_queue,
  847. .eh_abort_handler = idescsi_eh_abort,
  848. .eh_host_reset_handler = idescsi_eh_reset,
  849. .bios_param = idescsi_bios,
  850. .can_queue = 40,
  851. .this_id = -1,
  852. .sg_tablesize = 256,
  853. .cmd_per_lun = 5,
  854. .max_sectors = 128,
  855. .use_clustering = DISABLE_CLUSTERING,
  856. .emulated = 1,
  857. .proc_name = "ide-scsi",
  858. };
  859. static int ide_scsi_probe(ide_drive_t *drive)
  860. {
  861. idescsi_scsi_t *idescsi;
  862. struct Scsi_Host *host;
  863. struct gendisk *g;
  864. static int warned;
  865. int err = -ENOMEM;
  866. if (!warned && drive->media == ide_cdrom) {
  867. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  868. warned = 1;
  869. }
  870. if (idescsi_nocd && drive->media == ide_cdrom)
  871. return -ENODEV;
  872. if (!strstr("ide-scsi", drive->driver_req) ||
  873. !drive->present ||
  874. drive->media == ide_disk ||
  875. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  876. return -ENODEV;
  877. g = alloc_disk(1 << PARTN_BITS);
  878. if (!g)
  879. goto out_host_put;
  880. ide_init_disk(g, drive);
  881. host->max_id = 1;
  882. #if IDESCSI_DEBUG_LOG
  883. if (drive->id->last_lun)
  884. printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
  885. #endif
  886. if ((drive->id->last_lun & 0x7) != 7)
  887. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  888. else
  889. host->max_lun = 1;
  890. drive->driver_data = host;
  891. idescsi = scsihost_to_idescsi(host);
  892. idescsi->drive = drive;
  893. idescsi->driver = &idescsi_driver;
  894. idescsi->host = host;
  895. idescsi->disk = g;
  896. g->private_data = &idescsi->driver;
  897. ide_proc_register_driver(drive, &idescsi_driver);
  898. err = 0;
  899. idescsi_setup(drive, idescsi);
  900. g->fops = &idescsi_ops;
  901. ide_register_region(g);
  902. err = scsi_add_host(host, &drive->gendev);
  903. if (!err) {
  904. scsi_scan_host(host);
  905. return 0;
  906. }
  907. /* fall through on error */
  908. ide_unregister_region(g);
  909. ide_proc_unregister_driver(drive, &idescsi_driver);
  910. put_disk(g);
  911. out_host_put:
  912. scsi_host_put(host);
  913. return err;
  914. }
  915. static int __init init_idescsi_module(void)
  916. {
  917. return driver_register(&idescsi_driver.gen_driver);
  918. }
  919. static void __exit exit_idescsi_module(void)
  920. {
  921. driver_unregister(&idescsi_driver.gen_driver);
  922. }
  923. module_param(idescsi_nocd, int, 0600);
  924. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  925. module_init(init_idescsi_module);
  926. module_exit(exit_idescsi_module);
  927. MODULE_LICENSE("GPL");