ide-scsi.c 22 KB

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