ide-scsi.c 28 KB

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