ide-scsi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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/slab.h>
  41. #include <linux/ide.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/delay.h>
  44. #include <linux/mutex.h>
  45. #include <linux/bitops.h>
  46. #include <asm/io.h>
  47. #include <asm/uaccess.h>
  48. #include <scsi/scsi.h>
  49. #include <scsi/scsi_cmnd.h>
  50. #include <scsi/scsi_device.h>
  51. #include <scsi/scsi_host.h>
  52. #include <scsi/scsi_tcq.h>
  53. #include <scsi/sg.h>
  54. #define IDESCSI_DEBUG_LOG 0
  55. #if IDESCSI_DEBUG_LOG
  56. #define debug_log(fmt, args...) \
  57. printk(KERN_INFO "ide-scsi: " fmt, ## args)
  58. #else
  59. #define debug_log(fmt, args...) do {} while (0)
  60. #endif
  61. /*
  62. * SCSI command transformation layer
  63. */
  64. #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
  65. /*
  66. * Log flags
  67. */
  68. #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
  69. typedef struct ide_scsi_obj {
  70. ide_drive_t *drive;
  71. ide_driver_t *driver;
  72. struct gendisk *disk;
  73. struct Scsi_Host *host;
  74. struct ide_atapi_pc *pc; /* Current packet command */
  75. unsigned long transform; /* SCSI cmd translation layer */
  76. unsigned long log; /* log flags */
  77. } idescsi_scsi_t;
  78. static DEFINE_MUTEX(idescsi_ref_mutex);
  79. /* Set by module param to skip cd */
  80. static int idescsi_nocd;
  81. #define ide_scsi_g(disk) \
  82. container_of((disk)->private_data, struct ide_scsi_obj, driver)
  83. static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
  84. {
  85. struct ide_scsi_obj *scsi = NULL;
  86. mutex_lock(&idescsi_ref_mutex);
  87. scsi = ide_scsi_g(disk);
  88. if (scsi) {
  89. if (ide_device_get(scsi->drive))
  90. scsi = NULL;
  91. else
  92. scsi_host_get(scsi->host);
  93. }
  94. mutex_unlock(&idescsi_ref_mutex);
  95. return scsi;
  96. }
  97. static void ide_scsi_put(struct ide_scsi_obj *scsi)
  98. {
  99. ide_drive_t *drive = scsi->drive;
  100. mutex_lock(&idescsi_ref_mutex);
  101. scsi_host_put(scsi->host);
  102. ide_device_put(drive);
  103. mutex_unlock(&idescsi_ref_mutex);
  104. }
  105. static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
  106. {
  107. return (idescsi_scsi_t*) (&host[1]);
  108. }
  109. static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
  110. {
  111. return scsihost_to_idescsi(ide_drive->driver_data);
  112. }
  113. static void ide_scsi_hex_dump(u8 *data, int len)
  114. {
  115. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
  116. }
  117. static int idescsi_end_request(ide_drive_t *, int, int);
  118. static void ide_scsi_callback(ide_drive_t *drive)
  119. {
  120. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  121. struct ide_atapi_pc *pc = scsi->pc;
  122. if (pc->flags & PC_FLAG_TIMEDOUT)
  123. debug_log("%s: got timed out packet %lu at %lu\n", __func__,
  124. pc->scsi_cmd->serial_number, jiffies);
  125. /* end this request now - scsi should retry it*/
  126. else if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  127. printk(KERN_INFO "Packet command completed, %d bytes"
  128. " transferred\n", pc->xferred);
  129. idescsi_end_request(drive, 1, 0);
  130. }
  131. static int idescsi_check_condition(ide_drive_t *drive,
  132. struct request *failed_cmd)
  133. {
  134. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  135. struct ide_atapi_pc *pc;
  136. struct request *rq;
  137. u8 *buf;
  138. /* stuff a sense request in front of our current request */
  139. pc = kzalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
  140. rq = blk_get_request(drive->queue, READ, GFP_ATOMIC);
  141. buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
  142. if (!pc || !rq || !buf) {
  143. kfree(buf);
  144. if (rq)
  145. blk_put_request(rq);
  146. kfree(pc);
  147. return -ENOMEM;
  148. }
  149. rq->special = (char *) pc;
  150. pc->rq = rq;
  151. pc->buf = buf;
  152. pc->c[0] = REQUEST_SENSE;
  153. pc->c[4] = pc->req_xfer = pc->buf_size = SCSI_SENSE_BUFFERSIZE;
  154. rq->cmd_type = REQ_TYPE_SENSE;
  155. rq->cmd_flags |= REQ_PREEMPT;
  156. pc->timeout = jiffies + WAIT_READY;
  157. /* NOTE! Save the failed packet command in "rq->buffer" */
  158. rq->buffer = (void *) failed_cmd->special;
  159. pc->scsi_cmd = ((struct ide_atapi_pc *) failed_cmd->special)->scsi_cmd;
  160. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  161. printk ("ide-scsi: %s: queue cmd = ", drive->name);
  162. ide_scsi_hex_dump(pc->c, 6);
  163. }
  164. rq->rq_disk = scsi->disk;
  165. rq->ref_count++;
  166. memcpy(rq->cmd, pc->c, 12);
  167. ide_do_drive_cmd(drive, rq);
  168. return 0;
  169. }
  170. static ide_startstop_t
  171. idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  172. {
  173. ide_hwif_t *hwif = drive->hwif;
  174. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  175. /* force an abort */
  176. hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
  177. rq->errors++;
  178. idescsi_end_request(drive, 0, 0);
  179. return ide_stopped;
  180. }
  181. static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
  182. {
  183. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  184. struct request *rq = HWGROUP(drive)->rq;
  185. struct ide_atapi_pc *pc = (struct ide_atapi_pc *) rq->special;
  186. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  187. struct Scsi_Host *host;
  188. int errors = rq->errors;
  189. unsigned long flags;
  190. if (!blk_special_request(rq) && !blk_sense_request(rq)) {
  191. ide_end_request(drive, uptodate, nrsecs);
  192. return 0;
  193. }
  194. ide_end_drive_cmd (drive, 0, 0);
  195. if (blk_sense_request(rq)) {
  196. struct ide_atapi_pc *opc = (struct ide_atapi_pc *) rq->buffer;
  197. if (log) {
  198. printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
  199. ide_scsi_hex_dump(pc->buf, 16);
  200. }
  201. memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buf,
  202. SCSI_SENSE_BUFFERSIZE);
  203. kfree(pc->buf);
  204. kfree(pc);
  205. blk_put_request(rq);
  206. pc = opc;
  207. rq = pc->rq;
  208. pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
  209. (((pc->flags & PC_FLAG_TIMEDOUT) ?
  210. DID_TIME_OUT :
  211. DID_OK) << 16);
  212. } else if (pc->flags & PC_FLAG_TIMEDOUT) {
  213. if (log)
  214. printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
  215. drive->name, pc->scsi_cmd->serial_number);
  216. pc->scsi_cmd->result = DID_TIME_OUT << 16;
  217. } else if (errors >= ERROR_MAX) {
  218. pc->scsi_cmd->result = DID_ERROR << 16;
  219. if (log)
  220. printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  221. } else if (errors) {
  222. if (log)
  223. printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  224. if (!idescsi_check_condition(drive, rq))
  225. /* we started a request sense, so we'll be back, exit for now */
  226. return 0;
  227. pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
  228. } else {
  229. pc->scsi_cmd->result = DID_OK << 16;
  230. }
  231. host = pc->scsi_cmd->device->host;
  232. spin_lock_irqsave(host->host_lock, flags);
  233. pc->done(pc->scsi_cmd);
  234. spin_unlock_irqrestore(host->host_lock, flags);
  235. kfree(pc);
  236. blk_put_request(rq);
  237. scsi->pc = NULL;
  238. return 0;
  239. }
  240. static inline unsigned long get_timeout(struct ide_atapi_pc *pc)
  241. {
  242. return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
  243. }
  244. static int idescsi_expiry(ide_drive_t *drive)
  245. {
  246. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  247. struct ide_atapi_pc *pc = scsi->pc;
  248. debug_log("%s called for %lu at %lu\n", __func__,
  249. pc->scsi_cmd->serial_number, jiffies);
  250. pc->flags |= PC_FLAG_TIMEDOUT;
  251. return 0; /* we do not want the ide subsystem to retry */
  252. }
  253. /*
  254. * Our interrupt handler.
  255. */
  256. static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
  257. {
  258. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  259. struct ide_atapi_pc *pc = scsi->pc;
  260. return ide_pc_intr(drive, pc, idescsi_pc_intr, get_timeout(pc),
  261. idescsi_expiry, NULL, NULL, NULL,
  262. ide_io_buffers);
  263. }
  264. static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
  265. {
  266. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  267. return ide_transfer_pc(drive, scsi->pc, idescsi_pc_intr,
  268. get_timeout(scsi->pc), idescsi_expiry);
  269. }
  270. static inline int idescsi_set_direction(struct ide_atapi_pc *pc)
  271. {
  272. switch (pc->c[0]) {
  273. case READ_6: case READ_10: case READ_12:
  274. pc->flags &= ~PC_FLAG_WRITING;
  275. return 0;
  276. case WRITE_6: case WRITE_10: case WRITE_12:
  277. pc->flags |= PC_FLAG_WRITING;
  278. return 0;
  279. default:
  280. return 1;
  281. }
  282. }
  283. static int idescsi_map_sg(ide_drive_t *drive, struct ide_atapi_pc *pc)
  284. {
  285. ide_hwif_t *hwif = drive->hwif;
  286. struct scatterlist *sg, *scsi_sg;
  287. int segments;
  288. if (!pc->req_xfer || pc->req_xfer % 1024)
  289. return 1;
  290. if (idescsi_set_direction(pc))
  291. return 1;
  292. sg = hwif->sg_table;
  293. scsi_sg = scsi_sglist(pc->scsi_cmd);
  294. segments = scsi_sg_count(pc->scsi_cmd);
  295. if (segments > hwif->sg_max_nents)
  296. return 1;
  297. hwif->sg_nents = segments;
  298. memcpy(sg, scsi_sg, sizeof(*sg) * segments);
  299. return 0;
  300. }
  301. static ide_startstop_t idescsi_issue_pc(ide_drive_t *drive,
  302. struct ide_atapi_pc *pc)
  303. {
  304. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  305. /* Set the current packet command */
  306. scsi->pc = pc;
  307. return ide_issue_pc(drive, pc, idescsi_transfer_pc,
  308. get_timeout(pc), idescsi_expiry);
  309. }
  310. /*
  311. * idescsi_do_request is our request handling function.
  312. */
  313. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
  314. {
  315. debug_log("dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,
  316. rq->cmd[0], rq->errors);
  317. debug_log("sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
  318. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  319. if (blk_sense_request(rq) || blk_special_request(rq)) {
  320. struct ide_atapi_pc *pc = (struct ide_atapi_pc *)rq->special;
  321. if (drive->using_dma && !idescsi_map_sg(drive, pc))
  322. pc->flags |= PC_FLAG_DMA_OK;
  323. return idescsi_issue_pc(drive, pc);
  324. }
  325. blk_dump_rq_flags(rq, "ide-scsi: unsup command");
  326. idescsi_end_request (drive, 0, 0);
  327. return ide_stopped;
  328. }
  329. #ifdef CONFIG_IDE_PROC_FS
  330. #define ide_scsi_devset_get(name, field) \
  331. static int get_##name(ide_drive_t *drive) \
  332. { \
  333. idescsi_scsi_t *scsi = drive_to_idescsi(drive); \
  334. return scsi->field; \
  335. }
  336. #define ide_scsi_devset_set(name, field) \
  337. static int set_##name(ide_drive_t *drive, int arg) \
  338. { \
  339. idescsi_scsi_t *scsi = drive_to_idescsi(drive); \
  340. scsi->field = arg; \
  341. return 0; \
  342. }
  343. #define ide_scsi_devset_rw(_name, _min, _max, _field) \
  344. ide_scsi_devset_get(_name, _field); \
  345. ide_scsi_devset_set(_name, _field); \
  346. IDE_DEVSET(_name, S_RW, _min, _max, get_##_name, set_##_name)
  347. ide_devset_rw(bios_cyl, 0, 1023, bios_cyl);
  348. ide_devset_rw(bios_head, 0, 255, bios_head);
  349. ide_devset_rw(bios_sect, 0, 63, bios_sect);
  350. ide_scsi_devset_rw(transform, 0, 3, transform);
  351. ide_scsi_devset_rw(log, 0, 1, log);
  352. static const struct ide_devset *idescsi_settings[] = {
  353. &ide_devset_bios_cyl,
  354. &ide_devset_bios_head,
  355. &ide_devset_bios_sect,
  356. &ide_devset_log,
  357. &ide_devset_transform,
  358. NULL
  359. };
  360. #endif
  361. /*
  362. * Driver initialization.
  363. */
  364. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  365. {
  366. if ((drive->id[ATA_ID_CONFIG] & 0x0060) == 0x20)
  367. set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
  368. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  369. #if IDESCSI_DEBUG_LOG
  370. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  371. #endif /* IDESCSI_DEBUG_LOG */
  372. drive->pc_callback = ide_scsi_callback;
  373. ide_proc_register_driver(drive, scsi->driver);
  374. }
  375. static void ide_scsi_remove(ide_drive_t *drive)
  376. {
  377. struct Scsi_Host *scsihost = drive->driver_data;
  378. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  379. struct gendisk *g = scsi->disk;
  380. scsi_remove_host(scsihost);
  381. ide_proc_unregister_driver(drive, scsi->driver);
  382. ide_unregister_region(g);
  383. drive->driver_data = NULL;
  384. g->private_data = NULL;
  385. put_disk(g);
  386. ide_scsi_put(scsi);
  387. drive->scsi = 0;
  388. }
  389. static int ide_scsi_probe(ide_drive_t *);
  390. #ifdef CONFIG_IDE_PROC_FS
  391. static ide_proc_entry_t idescsi_proc[] = {
  392. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  393. { NULL, 0, NULL, NULL }
  394. };
  395. #endif
  396. static ide_driver_t idescsi_driver = {
  397. .gen_driver = {
  398. .owner = THIS_MODULE,
  399. .name = "ide-scsi",
  400. .bus = &ide_bus_type,
  401. },
  402. .probe = ide_scsi_probe,
  403. .remove = ide_scsi_remove,
  404. .version = IDESCSI_VERSION,
  405. .media = ide_scsi,
  406. .do_request = idescsi_do_request,
  407. .end_request = idescsi_end_request,
  408. .error = idescsi_atapi_error,
  409. #ifdef CONFIG_IDE_PROC_FS
  410. .proc = idescsi_proc,
  411. .settings = idescsi_settings,
  412. #endif
  413. };
  414. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  415. {
  416. struct gendisk *disk = inode->i_bdev->bd_disk;
  417. struct ide_scsi_obj *scsi;
  418. if (!(scsi = ide_scsi_get(disk)))
  419. return -ENXIO;
  420. return 0;
  421. }
  422. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  423. {
  424. struct gendisk *disk = inode->i_bdev->bd_disk;
  425. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  426. ide_scsi_put(scsi);
  427. return 0;
  428. }
  429. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  430. unsigned int cmd, unsigned long arg)
  431. {
  432. struct block_device *bdev = inode->i_bdev;
  433. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  434. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  435. }
  436. static struct block_device_operations idescsi_ops = {
  437. .owner = THIS_MODULE,
  438. .open = idescsi_ide_open,
  439. .release = idescsi_ide_release,
  440. .ioctl = idescsi_ide_ioctl,
  441. };
  442. static int idescsi_slave_configure(struct scsi_device * sdp)
  443. {
  444. /* Configure detected device */
  445. sdp->use_10_for_rw = 1;
  446. sdp->use_10_for_ms = 1;
  447. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  448. return 0;
  449. }
  450. static const char *idescsi_info (struct Scsi_Host *host)
  451. {
  452. return "SCSI host adapter emulation for IDE ATAPI devices";
  453. }
  454. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  455. {
  456. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  457. if (cmd == SG_SET_TRANSFORM) {
  458. if (arg)
  459. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  460. else
  461. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  462. return 0;
  463. } else if (cmd == SG_GET_TRANSFORM)
  464. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  465. return -EINVAL;
  466. }
  467. static int idescsi_queue (struct scsi_cmnd *cmd,
  468. void (*done)(struct scsi_cmnd *))
  469. {
  470. struct Scsi_Host *host = cmd->device->host;
  471. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  472. ide_drive_t *drive = scsi->drive;
  473. struct request *rq = NULL;
  474. struct ide_atapi_pc *pc = NULL;
  475. int write = cmd->sc_data_direction == DMA_TO_DEVICE;
  476. if (!drive) {
  477. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  478. goto abort;
  479. }
  480. scsi = drive_to_idescsi(drive);
  481. pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
  482. rq = blk_get_request(drive->queue, write, GFP_ATOMIC);
  483. if (rq == NULL || pc == NULL) {
  484. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  485. goto abort;
  486. }
  487. memset (pc->c, 0, 12);
  488. pc->flags = 0;
  489. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  490. pc->flags |= PC_FLAG_WRITING;
  491. pc->rq = rq;
  492. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  493. pc->buf = NULL;
  494. pc->sg = scsi_sglist(cmd);
  495. pc->sg_cnt = scsi_sg_count(cmd);
  496. pc->b_count = 0;
  497. pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
  498. pc->scsi_cmd = cmd;
  499. pc->done = done;
  500. pc->timeout = jiffies + cmd->request->timeout;
  501. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  502. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  503. ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
  504. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  505. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  506. ide_scsi_hex_dump(pc->c, 12);
  507. }
  508. }
  509. rq->special = (char *) pc;
  510. rq->cmd_type = REQ_TYPE_SPECIAL;
  511. spin_unlock_irq(host->host_lock);
  512. rq->ref_count++;
  513. memcpy(rq->cmd, pc->c, 12);
  514. blk_execute_rq_nowait(drive->queue, scsi->disk, rq, 0, NULL);
  515. spin_lock_irq(host->host_lock);
  516. return 0;
  517. abort:
  518. kfree (pc);
  519. if (rq)
  520. blk_put_request(rq);
  521. cmd->result = DID_ERROR << 16;
  522. done(cmd);
  523. return 0;
  524. }
  525. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  526. {
  527. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  528. ide_drive_t *drive = scsi->drive;
  529. int busy;
  530. int ret = FAILED;
  531. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  532. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  533. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  534. if (!drive) {
  535. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  536. WARN_ON(1);
  537. goto no_drive;
  538. }
  539. /* First give it some more time, how much is "right" is hard to say :-( */
  540. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  541. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  542. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  543. spin_lock_irq(&ide_lock);
  544. /* If there is no pc running we're done (our interrupt took care of it) */
  545. if (!scsi->pc) {
  546. ret = SUCCESS;
  547. goto ide_unlock;
  548. }
  549. /* It's somewhere in flight. Does ide subsystem agree? */
  550. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  551. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  552. /*
  553. * FIXME - not sure this condition can ever occur
  554. */
  555. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  556. if (blk_sense_request(scsi->pc->rq))
  557. kfree(scsi->pc->buf);
  558. /* we need to call blk_put_request twice. */
  559. blk_put_request(scsi->pc->rq);
  560. blk_put_request(scsi->pc->rq);
  561. kfree(scsi->pc);
  562. scsi->pc = NULL;
  563. ret = SUCCESS;
  564. }
  565. ide_unlock:
  566. spin_unlock_irq(&ide_lock);
  567. no_drive:
  568. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  569. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  570. return ret;
  571. }
  572. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  573. {
  574. struct request *req;
  575. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  576. ide_drive_t *drive = scsi->drive;
  577. int ready = 0;
  578. int ret = SUCCESS;
  579. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  580. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  581. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  582. if (!drive) {
  583. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  584. WARN_ON(1);
  585. return FAILED;
  586. }
  587. spin_lock_irq(cmd->device->host->host_lock);
  588. spin_lock(&ide_lock);
  589. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  590. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  591. spin_unlock(&ide_lock);
  592. spin_unlock_irq(cmd->device->host->host_lock);
  593. return FAILED;
  594. }
  595. /* kill current request */
  596. if (__blk_end_request(req, -EIO, 0))
  597. BUG();
  598. if (blk_sense_request(req))
  599. kfree(scsi->pc->buf);
  600. kfree(scsi->pc);
  601. scsi->pc = NULL;
  602. blk_put_request(req);
  603. /* now nuke the drive queue */
  604. while ((req = elv_next_request(drive->queue))) {
  605. if (__blk_end_request(req, -EIO, 0))
  606. BUG();
  607. }
  608. HWGROUP(drive)->rq = NULL;
  609. HWGROUP(drive)->handler = NULL;
  610. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  611. spin_unlock(&ide_lock);
  612. ide_do_reset(drive);
  613. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  614. do {
  615. spin_unlock_irq(cmd->device->host->host_lock);
  616. msleep(50);
  617. spin_lock_irq(cmd->device->host->host_lock);
  618. } while ( HWGROUP(drive)->handler );
  619. ready = drive_is_ready(drive);
  620. HWGROUP(drive)->busy--;
  621. if (!ready) {
  622. printk (KERN_ERR "ide-scsi: reset failed!\n");
  623. ret = FAILED;
  624. }
  625. spin_unlock_irq(cmd->device->host->host_lock);
  626. return ret;
  627. }
  628. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  629. sector_t capacity, int *parm)
  630. {
  631. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  632. ide_drive_t *drive = idescsi->drive;
  633. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  634. parm[0] = drive->bios_head;
  635. parm[1] = drive->bios_sect;
  636. parm[2] = drive->bios_cyl;
  637. }
  638. return 0;
  639. }
  640. static struct scsi_host_template idescsi_template = {
  641. .module = THIS_MODULE,
  642. .name = "idescsi",
  643. .info = idescsi_info,
  644. .slave_configure = idescsi_slave_configure,
  645. .ioctl = idescsi_ioctl,
  646. .queuecommand = idescsi_queue,
  647. .eh_abort_handler = idescsi_eh_abort,
  648. .eh_host_reset_handler = idescsi_eh_reset,
  649. .bios_param = idescsi_bios,
  650. .can_queue = 40,
  651. .this_id = -1,
  652. .sg_tablesize = 256,
  653. .cmd_per_lun = 5,
  654. .max_sectors = 128,
  655. .use_clustering = DISABLE_CLUSTERING,
  656. .emulated = 1,
  657. .proc_name = "ide-scsi",
  658. };
  659. static int ide_scsi_probe(ide_drive_t *drive)
  660. {
  661. idescsi_scsi_t *idescsi;
  662. struct Scsi_Host *host;
  663. struct gendisk *g;
  664. static int warned;
  665. int err = -ENOMEM;
  666. u16 last_lun;
  667. if (!warned && drive->media == ide_cdrom) {
  668. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  669. warned = 1;
  670. }
  671. if (idescsi_nocd && drive->media == ide_cdrom)
  672. return -ENODEV;
  673. if (!strstr("ide-scsi", drive->driver_req) ||
  674. drive->media == ide_disk ||
  675. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  676. return -ENODEV;
  677. drive->scsi = 1;
  678. g = alloc_disk(1 << PARTN_BITS);
  679. if (!g)
  680. goto out_host_put;
  681. ide_init_disk(g, drive);
  682. host->max_id = 1;
  683. last_lun = drive->id[ATA_ID_LAST_LUN];
  684. if (last_lun)
  685. debug_log("%s: last_lun=%u\n", drive->name, last_lun);
  686. if ((last_lun & 7) != 7)
  687. host->max_lun = (last_lun & 7) + 1;
  688. else
  689. host->max_lun = 1;
  690. drive->driver_data = host;
  691. idescsi = scsihost_to_idescsi(host);
  692. idescsi->drive = drive;
  693. idescsi->driver = &idescsi_driver;
  694. idescsi->host = host;
  695. idescsi->disk = g;
  696. g->private_data = &idescsi->driver;
  697. err = 0;
  698. idescsi_setup(drive, idescsi);
  699. g->fops = &idescsi_ops;
  700. ide_register_region(g);
  701. err = scsi_add_host(host, &drive->gendev);
  702. if (!err) {
  703. scsi_scan_host(host);
  704. return 0;
  705. }
  706. /* fall through on error */
  707. ide_unregister_region(g);
  708. ide_proc_unregister_driver(drive, &idescsi_driver);
  709. put_disk(g);
  710. out_host_put:
  711. drive->scsi = 0;
  712. scsi_host_put(host);
  713. return err;
  714. }
  715. static int __init init_idescsi_module(void)
  716. {
  717. return driver_register(&idescsi_driver.gen_driver);
  718. }
  719. static void __exit exit_idescsi_module(void)
  720. {
  721. driver_unregister(&idescsi_driver.gen_driver);
  722. }
  723. module_param(idescsi_nocd, int, 0600);
  724. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  725. module_init(init_idescsi_module);
  726. module_exit(exit_idescsi_module);
  727. MODULE_LICENSE("GPL");