ide-scsi.c 28 KB

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