ide-scsi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 transform; /* SCSI cmd translation layer */
  77. unsigned long log; /* log flags */
  78. } idescsi_scsi_t;
  79. static DEFINE_MUTEX(idescsi_ref_mutex);
  80. /* Set by module param to skip cd */
  81. static int idescsi_nocd;
  82. #define ide_scsi_g(disk) \
  83. container_of((disk)->private_data, struct ide_scsi_obj, driver)
  84. static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
  85. {
  86. struct ide_scsi_obj *scsi = NULL;
  87. mutex_lock(&idescsi_ref_mutex);
  88. scsi = ide_scsi_g(disk);
  89. if (scsi) {
  90. scsi_host_get(scsi->host);
  91. if (ide_device_get(scsi->drive)) {
  92. scsi_host_put(scsi->host);
  93. scsi = NULL;
  94. }
  95. }
  96. mutex_unlock(&idescsi_ref_mutex);
  97. return scsi;
  98. }
  99. static void ide_scsi_put(struct ide_scsi_obj *scsi)
  100. {
  101. mutex_lock(&idescsi_ref_mutex);
  102. ide_device_put(scsi->drive);
  103. scsi_host_put(scsi->host);
  104. mutex_unlock(&idescsi_ref_mutex);
  105. }
  106. static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
  107. {
  108. return (idescsi_scsi_t*) (&host[1]);
  109. }
  110. static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
  111. {
  112. return scsihost_to_idescsi(ide_drive->driver_data);
  113. }
  114. /*
  115. * PIO data transfer routine using the scatter gather table.
  116. */
  117. static void ide_scsi_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  118. unsigned int bcount, int write)
  119. {
  120. ide_hwif_t *hwif = drive->hwif;
  121. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  122. xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
  123. char *buf;
  124. int count;
  125. while (bcount) {
  126. count = min(pc->sg->length - pc->b_count, bcount);
  127. if (PageHighMem(sg_page(pc->sg))) {
  128. unsigned long flags;
  129. local_irq_save(flags);
  130. buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
  131. pc->sg->offset;
  132. xf(drive, NULL, buf + pc->b_count, count);
  133. kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
  134. local_irq_restore(flags);
  135. } else {
  136. buf = sg_virt(pc->sg);
  137. xf(drive, NULL, buf + pc->b_count, count);
  138. }
  139. bcount -= count; pc->b_count += count;
  140. if (pc->b_count == pc->sg->length) {
  141. if (!--pc->sg_cnt)
  142. break;
  143. pc->sg = sg_next(pc->sg);
  144. pc->b_count = 0;
  145. }
  146. }
  147. if (bcount) {
  148. printk(KERN_ERR "%s: scatter gather table too small, %s\n",
  149. drive->name, write ? "padding with zeros"
  150. : "discarding data");
  151. ide_pad_transfer(drive, write, bcount);
  152. }
  153. }
  154. static void ide_scsi_hex_dump(u8 *data, int len)
  155. {
  156. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
  157. }
  158. static int idescsi_end_request(ide_drive_t *, int, int);
  159. static void ide_scsi_callback(ide_drive_t *drive)
  160. {
  161. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  162. struct ide_atapi_pc *pc = scsi->pc;
  163. if (pc->flags & PC_FLAG_TIMEDOUT)
  164. debug_log("%s: got timed out packet %lu at %lu\n", __func__,
  165. pc->scsi_cmd->serial_number, jiffies);
  166. /* end this request now - scsi should retry it*/
  167. else if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  168. printk(KERN_INFO "Packet command completed, %d bytes"
  169. " transferred\n", pc->xferred);
  170. idescsi_end_request(drive, 1, 0);
  171. }
  172. static int idescsi_check_condition(ide_drive_t *drive,
  173. struct request *failed_cmd)
  174. {
  175. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  176. struct ide_atapi_pc *pc;
  177. struct request *rq;
  178. u8 *buf;
  179. /* stuff a sense request in front of our current request */
  180. pc = kzalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
  181. rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
  182. buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
  183. if (!pc || !rq || !buf) {
  184. kfree(buf);
  185. kfree(rq);
  186. kfree(pc);
  187. return -ENOMEM;
  188. }
  189. blk_rq_init(NULL, rq);
  190. rq->special = (char *) pc;
  191. pc->rq = rq;
  192. pc->buf = buf;
  193. pc->c[0] = REQUEST_SENSE;
  194. pc->c[4] = pc->req_xfer = pc->buf_size = SCSI_SENSE_BUFFERSIZE;
  195. rq->cmd_type = REQ_TYPE_SENSE;
  196. rq->cmd_flags |= REQ_PREEMPT;
  197. pc->timeout = jiffies + WAIT_READY;
  198. /* NOTE! Save the failed packet command in "rq->buffer" */
  199. rq->buffer = (void *) failed_cmd->special;
  200. pc->scsi_cmd = ((struct ide_atapi_pc *) failed_cmd->special)->scsi_cmd;
  201. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  202. printk ("ide-scsi: %s: queue cmd = ", drive->name);
  203. ide_scsi_hex_dump(pc->c, 6);
  204. }
  205. rq->rq_disk = scsi->disk;
  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) & (BUSY_STAT | DRQ_STAT))
  215. /* force an abort */
  216. hwif->tp_ops->exec_command(hwif, WIN_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. kfree(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. kfree(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. static void idescsi_add_settings(ide_drive_t *drive)
  371. {
  372. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  373. /*
  374. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  375. */
  376. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  377. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  378. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  379. ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  380. ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  381. }
  382. #else
  383. static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
  384. #endif
  385. /*
  386. * Driver initialization.
  387. */
  388. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  389. {
  390. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  391. set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags);
  392. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  393. #if IDESCSI_DEBUG_LOG
  394. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  395. #endif /* IDESCSI_DEBUG_LOG */
  396. drive->pc_callback = ide_scsi_callback;
  397. idescsi_add_settings(drive);
  398. }
  399. static void ide_scsi_remove(ide_drive_t *drive)
  400. {
  401. struct Scsi_Host *scsihost = drive->driver_data;
  402. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  403. struct gendisk *g = scsi->disk;
  404. scsi_remove_host(scsihost);
  405. ide_proc_unregister_driver(drive, scsi->driver);
  406. ide_unregister_region(g);
  407. drive->driver_data = NULL;
  408. g->private_data = NULL;
  409. put_disk(g);
  410. ide_scsi_put(scsi);
  411. drive->scsi = 0;
  412. }
  413. static int ide_scsi_probe(ide_drive_t *);
  414. #ifdef CONFIG_IDE_PROC_FS
  415. static ide_proc_entry_t idescsi_proc[] = {
  416. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  417. { NULL, 0, NULL, NULL }
  418. };
  419. #endif
  420. static ide_driver_t idescsi_driver = {
  421. .gen_driver = {
  422. .owner = THIS_MODULE,
  423. .name = "ide-scsi",
  424. .bus = &ide_bus_type,
  425. },
  426. .probe = ide_scsi_probe,
  427. .remove = ide_scsi_remove,
  428. .version = IDESCSI_VERSION,
  429. .media = ide_scsi,
  430. .supports_dsc_overlap = 0,
  431. .do_request = idescsi_do_request,
  432. .end_request = idescsi_end_request,
  433. .error = idescsi_atapi_error,
  434. #ifdef CONFIG_IDE_PROC_FS
  435. .proc = idescsi_proc,
  436. #endif
  437. };
  438. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  439. {
  440. struct gendisk *disk = inode->i_bdev->bd_disk;
  441. struct ide_scsi_obj *scsi;
  442. if (!(scsi = ide_scsi_get(disk)))
  443. return -ENXIO;
  444. return 0;
  445. }
  446. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  447. {
  448. struct gendisk *disk = inode->i_bdev->bd_disk;
  449. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  450. ide_scsi_put(scsi);
  451. return 0;
  452. }
  453. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  454. unsigned int cmd, unsigned long arg)
  455. {
  456. struct block_device *bdev = inode->i_bdev;
  457. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  458. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  459. }
  460. static struct block_device_operations idescsi_ops = {
  461. .owner = THIS_MODULE,
  462. .open = idescsi_ide_open,
  463. .release = idescsi_ide_release,
  464. .ioctl = idescsi_ide_ioctl,
  465. };
  466. static int idescsi_slave_configure(struct scsi_device * sdp)
  467. {
  468. /* Configure detected device */
  469. sdp->use_10_for_rw = 1;
  470. sdp->use_10_for_ms = 1;
  471. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  472. return 0;
  473. }
  474. static const char *idescsi_info (struct Scsi_Host *host)
  475. {
  476. return "SCSI host adapter emulation for IDE ATAPI devices";
  477. }
  478. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  479. {
  480. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  481. if (cmd == SG_SET_TRANSFORM) {
  482. if (arg)
  483. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  484. else
  485. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  486. return 0;
  487. } else if (cmd == SG_GET_TRANSFORM)
  488. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  489. return -EINVAL;
  490. }
  491. static int idescsi_queue (struct scsi_cmnd *cmd,
  492. void (*done)(struct scsi_cmnd *))
  493. {
  494. struct Scsi_Host *host = cmd->device->host;
  495. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  496. ide_drive_t *drive = scsi->drive;
  497. struct request *rq = NULL;
  498. struct ide_atapi_pc *pc = NULL;
  499. if (!drive) {
  500. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  501. goto abort;
  502. }
  503. scsi = drive_to_idescsi(drive);
  504. pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
  505. rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
  506. if (rq == NULL || pc == NULL) {
  507. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  508. goto abort;
  509. }
  510. memset (pc->c, 0, 12);
  511. pc->flags = 0;
  512. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  513. pc->flags |= PC_FLAG_WRITING;
  514. pc->rq = rq;
  515. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  516. pc->buf = NULL;
  517. pc->sg = scsi_sglist(cmd);
  518. pc->sg_cnt = scsi_sg_count(cmd);
  519. pc->b_count = 0;
  520. pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
  521. pc->scsi_cmd = cmd;
  522. pc->done = done;
  523. pc->timeout = jiffies + cmd->timeout_per_command;
  524. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  525. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  526. ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
  527. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  528. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  529. ide_scsi_hex_dump(pc->c, 12);
  530. }
  531. }
  532. blk_rq_init(NULL, rq);
  533. rq->special = (char *) pc;
  534. rq->cmd_type = REQ_TYPE_SPECIAL;
  535. spin_unlock_irq(host->host_lock);
  536. memcpy(rq->cmd, pc->c, 12);
  537. blk_execute_rq_nowait(drive->queue, scsi->disk, rq, 0, NULL);
  538. spin_lock_irq(host->host_lock);
  539. return 0;
  540. abort:
  541. kfree (pc);
  542. kfree (rq);
  543. cmd->result = DID_ERROR << 16;
  544. done(cmd);
  545. return 0;
  546. }
  547. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  548. {
  549. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  550. ide_drive_t *drive = scsi->drive;
  551. int busy;
  552. int ret = FAILED;
  553. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  554. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  555. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  556. if (!drive) {
  557. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  558. WARN_ON(1);
  559. goto no_drive;
  560. }
  561. /* First give it some more time, how much is "right" is hard to say :-( */
  562. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  563. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  564. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  565. spin_lock_irq(&ide_lock);
  566. /* If there is no pc running we're done (our interrupt took care of it) */
  567. if (!scsi->pc) {
  568. ret = SUCCESS;
  569. goto ide_unlock;
  570. }
  571. /* It's somewhere in flight. Does ide subsystem agree? */
  572. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  573. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  574. /*
  575. * FIXME - not sure this condition can ever occur
  576. */
  577. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  578. if (blk_sense_request(scsi->pc->rq))
  579. kfree(scsi->pc->buf);
  580. kfree(scsi->pc->rq);
  581. kfree(scsi->pc);
  582. scsi->pc = NULL;
  583. ret = SUCCESS;
  584. }
  585. ide_unlock:
  586. spin_unlock_irq(&ide_lock);
  587. no_drive:
  588. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  589. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  590. return ret;
  591. }
  592. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  593. {
  594. struct request *req;
  595. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  596. ide_drive_t *drive = scsi->drive;
  597. int ready = 0;
  598. int ret = SUCCESS;
  599. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  600. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  601. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  602. if (!drive) {
  603. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  604. WARN_ON(1);
  605. return FAILED;
  606. }
  607. spin_lock_irq(cmd->device->host->host_lock);
  608. spin_lock(&ide_lock);
  609. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  610. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  611. spin_unlock(&ide_lock);
  612. spin_unlock_irq(cmd->device->host->host_lock);
  613. return FAILED;
  614. }
  615. /* kill current request */
  616. if (__blk_end_request(req, -EIO, 0))
  617. BUG();
  618. if (blk_sense_request(req))
  619. kfree(scsi->pc->buf);
  620. kfree(scsi->pc);
  621. scsi->pc = NULL;
  622. kfree(req);
  623. /* now nuke the drive queue */
  624. while ((req = elv_next_request(drive->queue))) {
  625. if (__blk_end_request(req, -EIO, 0))
  626. BUG();
  627. }
  628. HWGROUP(drive)->rq = NULL;
  629. HWGROUP(drive)->handler = NULL;
  630. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  631. spin_unlock(&ide_lock);
  632. ide_do_reset(drive);
  633. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  634. do {
  635. spin_unlock_irq(cmd->device->host->host_lock);
  636. msleep(50);
  637. spin_lock_irq(cmd->device->host->host_lock);
  638. } while ( HWGROUP(drive)->handler );
  639. ready = drive_is_ready(drive);
  640. HWGROUP(drive)->busy--;
  641. if (!ready) {
  642. printk (KERN_ERR "ide-scsi: reset failed!\n");
  643. ret = FAILED;
  644. }
  645. spin_unlock_irq(cmd->device->host->host_lock);
  646. return ret;
  647. }
  648. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  649. sector_t capacity, int *parm)
  650. {
  651. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  652. ide_drive_t *drive = idescsi->drive;
  653. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  654. parm[0] = drive->bios_head;
  655. parm[1] = drive->bios_sect;
  656. parm[2] = drive->bios_cyl;
  657. }
  658. return 0;
  659. }
  660. static struct scsi_host_template idescsi_template = {
  661. .module = THIS_MODULE,
  662. .name = "idescsi",
  663. .info = idescsi_info,
  664. .slave_configure = idescsi_slave_configure,
  665. .ioctl = idescsi_ioctl,
  666. .queuecommand = idescsi_queue,
  667. .eh_abort_handler = idescsi_eh_abort,
  668. .eh_host_reset_handler = idescsi_eh_reset,
  669. .bios_param = idescsi_bios,
  670. .can_queue = 40,
  671. .this_id = -1,
  672. .sg_tablesize = 256,
  673. .cmd_per_lun = 5,
  674. .max_sectors = 128,
  675. .use_clustering = DISABLE_CLUSTERING,
  676. .emulated = 1,
  677. .proc_name = "ide-scsi",
  678. };
  679. static int ide_scsi_probe(ide_drive_t *drive)
  680. {
  681. idescsi_scsi_t *idescsi;
  682. struct Scsi_Host *host;
  683. struct gendisk *g;
  684. static int warned;
  685. int err = -ENOMEM;
  686. if (!warned && drive->media == ide_cdrom) {
  687. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  688. warned = 1;
  689. }
  690. if (idescsi_nocd && drive->media == ide_cdrom)
  691. return -ENODEV;
  692. if (!strstr("ide-scsi", drive->driver_req) ||
  693. !drive->present ||
  694. drive->media == ide_disk ||
  695. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  696. return -ENODEV;
  697. drive->scsi = 1;
  698. g = alloc_disk(1 << PARTN_BITS);
  699. if (!g)
  700. goto out_host_put;
  701. ide_init_disk(g, drive);
  702. host->max_id = 1;
  703. if (drive->id->last_lun)
  704. debug_log("%s: id->last_lun=%u\n", drive->name,
  705. drive->id->last_lun);
  706. if ((drive->id->last_lun & 0x7) != 7)
  707. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  708. else
  709. host->max_lun = 1;
  710. drive->driver_data = host;
  711. idescsi = scsihost_to_idescsi(host);
  712. idescsi->drive = drive;
  713. idescsi->driver = &idescsi_driver;
  714. idescsi->host = host;
  715. idescsi->disk = g;
  716. g->private_data = &idescsi->driver;
  717. ide_proc_register_driver(drive, &idescsi_driver);
  718. err = 0;
  719. idescsi_setup(drive, idescsi);
  720. g->fops = &idescsi_ops;
  721. ide_register_region(g);
  722. err = scsi_add_host(host, &drive->gendev);
  723. if (!err) {
  724. scsi_scan_host(host);
  725. return 0;
  726. }
  727. /* fall through on error */
  728. ide_unregister_region(g);
  729. ide_proc_unregister_driver(drive, &idescsi_driver);
  730. put_disk(g);
  731. out_host_put:
  732. drive->scsi = 0;
  733. scsi_host_put(host);
  734. return err;
  735. }
  736. static int __init init_idescsi_module(void)
  737. {
  738. return driver_register(&idescsi_driver.gen_driver);
  739. }
  740. static void __exit exit_idescsi_module(void)
  741. {
  742. driver_unregister(&idescsi_driver.gen_driver);
  743. }
  744. module_param(idescsi_nocd, int, 0600);
  745. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  746. module_init(init_idescsi_module);
  747. module_exit(exit_idescsi_module);
  748. MODULE_LICENSE("GPL");