ide-scsi.c 28 KB

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