ide-scsi.c 24 KB

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