ide-scsi.c 28 KB

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