ide-scsi.c 30 KB

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