ide-scsi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. BUG_ON(HWGROUP(drive)->handler != NULL);
  528. ide_set_handler(drive, &idescsi_transfer_pc,
  529. get_timeout(pc), idescsi_expiry);
  530. /* Issue the packet command */
  531. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  532. return ide_started;
  533. } else {
  534. /* Issue the packet command */
  535. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  536. return idescsi_transfer_pc(drive);
  537. }
  538. }
  539. /*
  540. * idescsi_do_request is our request handling function.
  541. */
  542. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
  543. {
  544. #if IDESCSI_DEBUG_LOG
  545. printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
  546. printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
  547. #endif /* IDESCSI_DEBUG_LOG */
  548. if (blk_sense_request(rq) || blk_special_request(rq)) {
  549. return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
  550. }
  551. blk_dump_rq_flags(rq, "ide-scsi: unsup command");
  552. idescsi_end_request (drive, 0, 0);
  553. return ide_stopped;
  554. }
  555. #ifdef CONFIG_IDE_PROC_FS
  556. static void idescsi_add_settings(ide_drive_t *drive)
  557. {
  558. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  559. /*
  560. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  561. */
  562. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  563. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  564. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  565. ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  566. ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  567. }
  568. #else
  569. static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
  570. #endif
  571. /*
  572. * Driver initialization.
  573. */
  574. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  575. {
  576. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  577. set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
  578. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  579. #if IDESCSI_DEBUG_LOG
  580. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  581. #endif /* IDESCSI_DEBUG_LOG */
  582. idescsi_add_settings(drive);
  583. }
  584. static void ide_scsi_remove(ide_drive_t *drive)
  585. {
  586. struct Scsi_Host *scsihost = drive->driver_data;
  587. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  588. struct gendisk *g = scsi->disk;
  589. scsi_remove_host(scsihost);
  590. ide_proc_unregister_driver(drive, scsi->driver);
  591. ide_unregister_region(g);
  592. drive->driver_data = NULL;
  593. g->private_data = NULL;
  594. put_disk(g);
  595. ide_scsi_put(scsi);
  596. }
  597. static int ide_scsi_probe(ide_drive_t *);
  598. #ifdef CONFIG_IDE_PROC_FS
  599. static ide_proc_entry_t idescsi_proc[] = {
  600. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  601. { NULL, 0, NULL, NULL }
  602. };
  603. #endif
  604. static ide_driver_t idescsi_driver = {
  605. .gen_driver = {
  606. .owner = THIS_MODULE,
  607. .name = "ide-scsi",
  608. .bus = &ide_bus_type,
  609. },
  610. .probe = ide_scsi_probe,
  611. .remove = ide_scsi_remove,
  612. .version = IDESCSI_VERSION,
  613. .media = ide_scsi,
  614. .supports_dsc_overlap = 0,
  615. .do_request = idescsi_do_request,
  616. .end_request = idescsi_end_request,
  617. .error = idescsi_atapi_error,
  618. .abort = idescsi_atapi_abort,
  619. #ifdef CONFIG_IDE_PROC_FS
  620. .proc = idescsi_proc,
  621. #endif
  622. };
  623. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  624. {
  625. struct gendisk *disk = inode->i_bdev->bd_disk;
  626. struct ide_scsi_obj *scsi;
  627. if (!(scsi = ide_scsi_get(disk)))
  628. return -ENXIO;
  629. return 0;
  630. }
  631. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  632. {
  633. struct gendisk *disk = inode->i_bdev->bd_disk;
  634. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  635. ide_scsi_put(scsi);
  636. return 0;
  637. }
  638. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  639. unsigned int cmd, unsigned long arg)
  640. {
  641. struct block_device *bdev = inode->i_bdev;
  642. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  643. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  644. }
  645. static struct block_device_operations idescsi_ops = {
  646. .owner = THIS_MODULE,
  647. .open = idescsi_ide_open,
  648. .release = idescsi_ide_release,
  649. .ioctl = idescsi_ide_ioctl,
  650. };
  651. static int idescsi_slave_configure(struct scsi_device * sdp)
  652. {
  653. /* Configure detected device */
  654. sdp->use_10_for_rw = 1;
  655. sdp->use_10_for_ms = 1;
  656. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  657. return 0;
  658. }
  659. static const char *idescsi_info (struct Scsi_Host *host)
  660. {
  661. return "SCSI host adapter emulation for IDE ATAPI devices";
  662. }
  663. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  664. {
  665. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  666. if (cmd == SG_SET_TRANSFORM) {
  667. if (arg)
  668. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  669. else
  670. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  671. return 0;
  672. } else if (cmd == SG_GET_TRANSFORM)
  673. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  674. return -EINVAL;
  675. }
  676. static int idescsi_queue (struct scsi_cmnd *cmd,
  677. void (*done)(struct scsi_cmnd *))
  678. {
  679. struct Scsi_Host *host = cmd->device->host;
  680. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  681. ide_drive_t *drive = scsi->drive;
  682. struct request *rq = NULL;
  683. idescsi_pc_t *pc = NULL;
  684. if (!drive) {
  685. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  686. goto abort;
  687. }
  688. scsi = drive_to_idescsi(drive);
  689. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  690. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  691. if (rq == NULL || pc == NULL) {
  692. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  693. goto abort;
  694. }
  695. memset (pc->c, 0, 12);
  696. pc->flags = 0;
  697. pc->rq = rq;
  698. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  699. pc->buffer = NULL;
  700. pc->sg = scsi_sglist(cmd);
  701. pc->sg_cnt = scsi_sg_count(cmd);
  702. pc->b_count = 0;
  703. pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
  704. pc->scsi_cmd = cmd;
  705. pc->done = done;
  706. pc->timeout = jiffies + cmd->timeout_per_command;
  707. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  708. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  709. ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
  710. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  711. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  712. ide_scsi_hex_dump(pc->c, 12);
  713. }
  714. }
  715. ide_init_drive_cmd (rq);
  716. rq->special = (char *) pc;
  717. rq->cmd_type = REQ_TYPE_SPECIAL;
  718. spin_unlock_irq(host->host_lock);
  719. rq->rq_disk = scsi->disk;
  720. (void) ide_do_drive_cmd (drive, rq, ide_end);
  721. spin_lock_irq(host->host_lock);
  722. return 0;
  723. abort:
  724. kfree (pc);
  725. kfree (rq);
  726. cmd->result = DID_ERROR << 16;
  727. done(cmd);
  728. return 0;
  729. }
  730. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  731. {
  732. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  733. ide_drive_t *drive = scsi->drive;
  734. int busy;
  735. int ret = FAILED;
  736. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  737. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  738. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  739. if (!drive) {
  740. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  741. WARN_ON(1);
  742. goto no_drive;
  743. }
  744. /* First give it some more time, how much is "right" is hard to say :-( */
  745. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  746. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  747. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  748. spin_lock_irq(&ide_lock);
  749. /* If there is no pc running we're done (our interrupt took care of it) */
  750. if (!scsi->pc) {
  751. ret = SUCCESS;
  752. goto ide_unlock;
  753. }
  754. /* It's somewhere in flight. Does ide subsystem agree? */
  755. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  756. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  757. /*
  758. * FIXME - not sure this condition can ever occur
  759. */
  760. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  761. if (blk_sense_request(scsi->pc->rq))
  762. kfree(scsi->pc->buffer);
  763. kfree(scsi->pc->rq);
  764. kfree(scsi->pc);
  765. scsi->pc = NULL;
  766. ret = SUCCESS;
  767. }
  768. ide_unlock:
  769. spin_unlock_irq(&ide_lock);
  770. no_drive:
  771. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  772. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  773. return ret;
  774. }
  775. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  776. {
  777. struct request *req;
  778. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  779. ide_drive_t *drive = scsi->drive;
  780. int ready = 0;
  781. int ret = SUCCESS;
  782. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  783. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  784. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  785. if (!drive) {
  786. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  787. WARN_ON(1);
  788. return FAILED;
  789. }
  790. spin_lock_irq(cmd->device->host->host_lock);
  791. spin_lock(&ide_lock);
  792. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  793. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  794. spin_unlock(&ide_lock);
  795. spin_unlock_irq(cmd->device->host->host_lock);
  796. return FAILED;
  797. }
  798. /* kill current request */
  799. if (__blk_end_request(req, -EIO, 0))
  800. BUG();
  801. if (blk_sense_request(req))
  802. kfree(scsi->pc->buffer);
  803. kfree(scsi->pc);
  804. scsi->pc = NULL;
  805. kfree(req);
  806. /* now nuke the drive queue */
  807. while ((req = elv_next_request(drive->queue))) {
  808. if (__blk_end_request(req, -EIO, 0))
  809. BUG();
  810. }
  811. HWGROUP(drive)->rq = NULL;
  812. HWGROUP(drive)->handler = NULL;
  813. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  814. spin_unlock(&ide_lock);
  815. ide_do_reset(drive);
  816. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  817. do {
  818. spin_unlock_irq(cmd->device->host->host_lock);
  819. msleep(50);
  820. spin_lock_irq(cmd->device->host->host_lock);
  821. } while ( HWGROUP(drive)->handler );
  822. ready = drive_is_ready(drive);
  823. HWGROUP(drive)->busy--;
  824. if (!ready) {
  825. printk (KERN_ERR "ide-scsi: reset failed!\n");
  826. ret = FAILED;
  827. }
  828. spin_unlock_irq(cmd->device->host->host_lock);
  829. return ret;
  830. }
  831. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  832. sector_t capacity, int *parm)
  833. {
  834. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  835. ide_drive_t *drive = idescsi->drive;
  836. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  837. parm[0] = drive->bios_head;
  838. parm[1] = drive->bios_sect;
  839. parm[2] = drive->bios_cyl;
  840. }
  841. return 0;
  842. }
  843. static struct scsi_host_template idescsi_template = {
  844. .module = THIS_MODULE,
  845. .name = "idescsi",
  846. .info = idescsi_info,
  847. .slave_configure = idescsi_slave_configure,
  848. .ioctl = idescsi_ioctl,
  849. .queuecommand = idescsi_queue,
  850. .eh_abort_handler = idescsi_eh_abort,
  851. .eh_host_reset_handler = idescsi_eh_reset,
  852. .bios_param = idescsi_bios,
  853. .can_queue = 40,
  854. .this_id = -1,
  855. .sg_tablesize = 256,
  856. .cmd_per_lun = 5,
  857. .max_sectors = 128,
  858. .use_clustering = DISABLE_CLUSTERING,
  859. .emulated = 1,
  860. .proc_name = "ide-scsi",
  861. };
  862. static int ide_scsi_probe(ide_drive_t *drive)
  863. {
  864. idescsi_scsi_t *idescsi;
  865. struct Scsi_Host *host;
  866. struct gendisk *g;
  867. static int warned;
  868. int err = -ENOMEM;
  869. if (!warned && drive->media == ide_cdrom) {
  870. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  871. warned = 1;
  872. }
  873. if (idescsi_nocd && drive->media == ide_cdrom)
  874. return -ENODEV;
  875. if (!strstr("ide-scsi", drive->driver_req) ||
  876. !drive->present ||
  877. drive->media == ide_disk ||
  878. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  879. return -ENODEV;
  880. g = alloc_disk(1 << PARTN_BITS);
  881. if (!g)
  882. goto out_host_put;
  883. ide_init_disk(g, drive);
  884. host->max_id = 1;
  885. #if IDESCSI_DEBUG_LOG
  886. if (drive->id->last_lun)
  887. printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
  888. #endif
  889. if ((drive->id->last_lun & 0x7) != 7)
  890. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  891. else
  892. host->max_lun = 1;
  893. drive->driver_data = host;
  894. idescsi = scsihost_to_idescsi(host);
  895. idescsi->drive = drive;
  896. idescsi->driver = &idescsi_driver;
  897. idescsi->host = host;
  898. idescsi->disk = g;
  899. g->private_data = &idescsi->driver;
  900. ide_proc_register_driver(drive, &idescsi_driver);
  901. err = 0;
  902. idescsi_setup(drive, idescsi);
  903. g->fops = &idescsi_ops;
  904. ide_register_region(g);
  905. err = scsi_add_host(host, &drive->gendev);
  906. if (!err) {
  907. scsi_scan_host(host);
  908. return 0;
  909. }
  910. /* fall through on error */
  911. ide_unregister_region(g);
  912. ide_proc_unregister_driver(drive, &idescsi_driver);
  913. put_disk(g);
  914. out_host_put:
  915. scsi_host_put(host);
  916. return err;
  917. }
  918. static int __init init_idescsi_module(void)
  919. {
  920. return driver_register(&idescsi_driver.gen_driver);
  921. }
  922. static void __exit exit_idescsi_module(void)
  923. {
  924. driver_unregister(&idescsi_driver.gen_driver);
  925. }
  926. module_param(idescsi_nocd, int, 0600);
  927. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  928. module_init(init_idescsi_module);
  929. module_exit(exit_idescsi_module);
  930. MODULE_LICENSE("GPL");