ide-scsi.c 24 KB

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