ide-scsi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 ide_startstop_t
  225. idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
  226. {
  227. debug_log("%s called for %lu\n", __func__,
  228. ((struct ide_atapi_pc *) rq->special)->scsi_cmd->serial_number);
  229. rq->errors |= ERROR_MAX;
  230. idescsi_end_request(drive, 0, 0);
  231. return ide_stopped;
  232. }
  233. static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
  234. {
  235. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  236. struct request *rq = HWGROUP(drive)->rq;
  237. struct ide_atapi_pc *pc = (struct ide_atapi_pc *) rq->special;
  238. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  239. struct Scsi_Host *host;
  240. int errors = rq->errors;
  241. unsigned long flags;
  242. if (!blk_special_request(rq) && !blk_sense_request(rq)) {
  243. ide_end_request(drive, uptodate, nrsecs);
  244. return 0;
  245. }
  246. ide_end_drive_cmd (drive, 0, 0);
  247. if (blk_sense_request(rq)) {
  248. struct ide_atapi_pc *opc = (struct ide_atapi_pc *) rq->buffer;
  249. if (log) {
  250. printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
  251. ide_scsi_hex_dump(pc->buf, 16);
  252. }
  253. memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buf,
  254. SCSI_SENSE_BUFFERSIZE);
  255. kfree(pc->buf);
  256. kfree(pc);
  257. kfree(rq);
  258. pc = opc;
  259. rq = pc->rq;
  260. pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
  261. (((pc->flags & PC_FLAG_TIMEDOUT) ?
  262. DID_TIME_OUT :
  263. DID_OK) << 16);
  264. } else if (pc->flags & PC_FLAG_TIMEDOUT) {
  265. if (log)
  266. printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
  267. drive->name, pc->scsi_cmd->serial_number);
  268. pc->scsi_cmd->result = DID_TIME_OUT << 16;
  269. } else if (errors >= ERROR_MAX) {
  270. pc->scsi_cmd->result = DID_ERROR << 16;
  271. if (log)
  272. printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  273. } else if (errors) {
  274. if (log)
  275. printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  276. if (!idescsi_check_condition(drive, rq))
  277. /* we started a request sense, so we'll be back, exit for now */
  278. return 0;
  279. pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
  280. } else {
  281. pc->scsi_cmd->result = DID_OK << 16;
  282. }
  283. host = pc->scsi_cmd->device->host;
  284. spin_lock_irqsave(host->host_lock, flags);
  285. pc->done(pc->scsi_cmd);
  286. spin_unlock_irqrestore(host->host_lock, flags);
  287. kfree(pc);
  288. kfree(rq);
  289. scsi->pc = NULL;
  290. return 0;
  291. }
  292. static inline unsigned long get_timeout(struct ide_atapi_pc *pc)
  293. {
  294. return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
  295. }
  296. static int idescsi_expiry(ide_drive_t *drive)
  297. {
  298. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  299. struct ide_atapi_pc *pc = scsi->pc;
  300. debug_log("%s called for %lu at %lu\n", __func__,
  301. pc->scsi_cmd->serial_number, jiffies);
  302. pc->flags |= PC_FLAG_TIMEDOUT;
  303. return 0; /* we do not want the ide subsystem to retry */
  304. }
  305. /*
  306. * Our interrupt handler.
  307. */
  308. static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
  309. {
  310. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  311. ide_hwif_t *hwif = drive->hwif;
  312. struct ide_atapi_pc *pc = scsi->pc;
  313. struct request *rq = pc->rq;
  314. xfer_func_t *xferfunc;
  315. unsigned int temp;
  316. u16 bcount;
  317. u8 stat, ireason;
  318. debug_log("Reached %s interrupt handler\n", __func__);
  319. if (pc->flags & PC_FLAG_TIMEDOUT) {
  320. pc->callback(drive);
  321. return ide_stopped;
  322. }
  323. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  324. if (hwif->dma_ops->dma_end(drive))
  325. pc->flags |= PC_FLAG_DMA_ERROR;
  326. else
  327. pc->xferred = pc->req_xfer;
  328. debug_log("%s: DMA finished\n", drive->name);
  329. }
  330. /* Clear the interrupt */
  331. stat = ide_read_status(drive);
  332. if ((stat & DRQ_STAT) == 0) {
  333. /* No more interrupts */
  334. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  335. local_irq_enable_in_hardirq();
  336. if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR))
  337. rq->errors++;
  338. pc->callback(drive);
  339. return ide_stopped;
  340. }
  341. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  342. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  343. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  344. "in DMA mode\n", drive->name);
  345. ide_dma_off(drive);
  346. return ide_do_reset(drive);
  347. }
  348. bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
  349. hwif->INB(hwif->io_ports.lbam_addr);
  350. ireason = hwif->INB(hwif->io_ports.nsect_addr);
  351. if (ireason & CD) {
  352. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  353. return ide_do_reset (drive);
  354. }
  355. if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
  356. /* Hopefully, we will never get here */
  357. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  358. "to %s!\n", drive->name,
  359. (ireason & IO) ? "Write" : "Read",
  360. (ireason & IO) ? "Read" : "Write");
  361. return ide_do_reset(drive);
  362. }
  363. if (!(pc->flags & PC_FLAG_WRITING)) {
  364. temp = pc->xferred + bcount;
  365. if (temp > pc->req_xfer) {
  366. if (temp > pc->buf_size) {
  367. printk(KERN_ERR "%s: The device wants to send "
  368. "us more data than expected - "
  369. "discarding data\n",
  370. drive->name);
  371. temp = pc->buf_size - pc->xferred;
  372. if (temp) {
  373. if (pc->sg)
  374. ide_scsi_io_buffers(drive, pc,
  375. temp, 0);
  376. else
  377. hwif->input_data(drive, NULL,
  378. pc->cur_pos, temp);
  379. printk(KERN_ERR "%s: transferred %d of "
  380. "%d bytes\n",
  381. drive->name,
  382. temp, bcount);
  383. }
  384. pc->xferred += temp;
  385. pc->cur_pos += temp;
  386. ide_pad_transfer(drive, 0, bcount - temp);
  387. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  388. return ide_started;
  389. }
  390. debug_log("The device wants to send us more data than "
  391. "expected - allowing transfer\n");
  392. }
  393. xferfunc = hwif->input_data;
  394. } else
  395. xferfunc = hwif->output_data;
  396. if (pc->sg)
  397. ide_scsi_io_buffers(drive, pc, bcount,
  398. !!(pc->flags & PC_FLAG_WRITING));
  399. else
  400. xferfunc(drive, NULL, pc->cur_pos, bcount);
  401. /* Update the current position */
  402. pc->xferred += bcount;
  403. pc->cur_pos += bcount;
  404. /* And set the interrupt handler again */
  405. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  406. return ide_started;
  407. }
  408. static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
  409. {
  410. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  411. return ide_transfer_pc(drive, scsi->pc, idescsi_pc_intr,
  412. get_timeout(scsi->pc), idescsi_expiry);
  413. }
  414. static inline int idescsi_set_direction(struct ide_atapi_pc *pc)
  415. {
  416. switch (pc->c[0]) {
  417. case READ_6: case READ_10: case READ_12:
  418. pc->flags &= ~PC_FLAG_WRITING;
  419. return 0;
  420. case WRITE_6: case WRITE_10: case WRITE_12:
  421. pc->flags |= PC_FLAG_WRITING;
  422. return 0;
  423. default:
  424. return 1;
  425. }
  426. }
  427. static int idescsi_map_sg(ide_drive_t *drive, struct ide_atapi_pc *pc)
  428. {
  429. ide_hwif_t *hwif = drive->hwif;
  430. struct scatterlist *sg, *scsi_sg;
  431. int segments;
  432. if (!pc->req_xfer || pc->req_xfer % 1024)
  433. return 1;
  434. if (idescsi_set_direction(pc))
  435. return 1;
  436. sg = hwif->sg_table;
  437. scsi_sg = scsi_sglist(pc->scsi_cmd);
  438. segments = scsi_sg_count(pc->scsi_cmd);
  439. if (segments > hwif->sg_max_nents)
  440. return 1;
  441. hwif->sg_nents = segments;
  442. memcpy(sg, scsi_sg, sizeof(*sg) * segments);
  443. return 0;
  444. }
  445. static ide_startstop_t idescsi_issue_pc(ide_drive_t *drive,
  446. struct ide_atapi_pc *pc)
  447. {
  448. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  449. /* Set the current packet command */
  450. scsi->pc = pc;
  451. return ide_issue_pc(drive, pc, idescsi_transfer_pc,
  452. get_timeout(pc), idescsi_expiry);
  453. }
  454. /*
  455. * idescsi_do_request is our request handling function.
  456. */
  457. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
  458. {
  459. debug_log("dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,
  460. rq->cmd[0], rq->errors);
  461. debug_log("sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
  462. rq->sector, rq->nr_sectors, rq->current_nr_sectors);
  463. if (blk_sense_request(rq) || blk_special_request(rq)) {
  464. struct ide_atapi_pc *pc = (struct ide_atapi_pc *)rq->special;
  465. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  466. if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags))
  467. pc->flags |= PC_FLAG_DRQ_INTERRUPT;
  468. if (drive->using_dma && !idescsi_map_sg(drive, pc))
  469. pc->flags |= PC_FLAG_DMA_OK;
  470. return idescsi_issue_pc(drive, pc);
  471. }
  472. blk_dump_rq_flags(rq, "ide-scsi: unsup command");
  473. idescsi_end_request (drive, 0, 0);
  474. return ide_stopped;
  475. }
  476. #ifdef CONFIG_IDE_PROC_FS
  477. static void idescsi_add_settings(ide_drive_t *drive)
  478. {
  479. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  480. /*
  481. * drive setting name read/write data type min max mul_factor div_factor data pointer set function
  482. */
  483. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  484. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  485. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  486. ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  487. ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  488. }
  489. #else
  490. static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
  491. #endif
  492. /*
  493. * Driver initialization.
  494. */
  495. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  496. {
  497. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  498. set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
  499. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  500. #if IDESCSI_DEBUG_LOG
  501. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  502. #endif /* IDESCSI_DEBUG_LOG */
  503. idescsi_add_settings(drive);
  504. }
  505. static void ide_scsi_remove(ide_drive_t *drive)
  506. {
  507. struct Scsi_Host *scsihost = drive->driver_data;
  508. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  509. struct gendisk *g = scsi->disk;
  510. scsi_remove_host(scsihost);
  511. ide_proc_unregister_driver(drive, scsi->driver);
  512. ide_unregister_region(g);
  513. drive->driver_data = NULL;
  514. g->private_data = NULL;
  515. put_disk(g);
  516. ide_scsi_put(scsi);
  517. drive->scsi = 0;
  518. }
  519. static int ide_scsi_probe(ide_drive_t *);
  520. #ifdef CONFIG_IDE_PROC_FS
  521. static ide_proc_entry_t idescsi_proc[] = {
  522. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  523. { NULL, 0, NULL, NULL }
  524. };
  525. #endif
  526. static ide_driver_t idescsi_driver = {
  527. .gen_driver = {
  528. .owner = THIS_MODULE,
  529. .name = "ide-scsi",
  530. .bus = &ide_bus_type,
  531. },
  532. .probe = ide_scsi_probe,
  533. .remove = ide_scsi_remove,
  534. .version = IDESCSI_VERSION,
  535. .media = ide_scsi,
  536. .supports_dsc_overlap = 0,
  537. .do_request = idescsi_do_request,
  538. .end_request = idescsi_end_request,
  539. .error = idescsi_atapi_error,
  540. .abort = idescsi_atapi_abort,
  541. #ifdef CONFIG_IDE_PROC_FS
  542. .proc = idescsi_proc,
  543. #endif
  544. };
  545. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  546. {
  547. struct gendisk *disk = inode->i_bdev->bd_disk;
  548. struct ide_scsi_obj *scsi;
  549. if (!(scsi = ide_scsi_get(disk)))
  550. return -ENXIO;
  551. return 0;
  552. }
  553. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  554. {
  555. struct gendisk *disk = inode->i_bdev->bd_disk;
  556. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  557. ide_scsi_put(scsi);
  558. return 0;
  559. }
  560. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  561. unsigned int cmd, unsigned long arg)
  562. {
  563. struct block_device *bdev = inode->i_bdev;
  564. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  565. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  566. }
  567. static struct block_device_operations idescsi_ops = {
  568. .owner = THIS_MODULE,
  569. .open = idescsi_ide_open,
  570. .release = idescsi_ide_release,
  571. .ioctl = idescsi_ide_ioctl,
  572. };
  573. static int idescsi_slave_configure(struct scsi_device * sdp)
  574. {
  575. /* Configure detected device */
  576. sdp->use_10_for_rw = 1;
  577. sdp->use_10_for_ms = 1;
  578. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  579. return 0;
  580. }
  581. static const char *idescsi_info (struct Scsi_Host *host)
  582. {
  583. return "SCSI host adapter emulation for IDE ATAPI devices";
  584. }
  585. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  586. {
  587. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  588. if (cmd == SG_SET_TRANSFORM) {
  589. if (arg)
  590. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  591. else
  592. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  593. return 0;
  594. } else if (cmd == SG_GET_TRANSFORM)
  595. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  596. return -EINVAL;
  597. }
  598. static int idescsi_queue (struct scsi_cmnd *cmd,
  599. void (*done)(struct scsi_cmnd *))
  600. {
  601. struct Scsi_Host *host = cmd->device->host;
  602. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  603. ide_drive_t *drive = scsi->drive;
  604. struct request *rq = NULL;
  605. struct ide_atapi_pc *pc = NULL;
  606. if (!drive) {
  607. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  608. goto abort;
  609. }
  610. scsi = drive_to_idescsi(drive);
  611. pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
  612. rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
  613. if (rq == NULL || pc == NULL) {
  614. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  615. goto abort;
  616. }
  617. memset (pc->c, 0, 12);
  618. pc->flags = 0;
  619. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  620. pc->flags |= PC_FLAG_WRITING;
  621. pc->rq = rq;
  622. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  623. pc->buf = NULL;
  624. pc->sg = scsi_sglist(cmd);
  625. pc->sg_cnt = scsi_sg_count(cmd);
  626. pc->b_count = 0;
  627. pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
  628. pc->scsi_cmd = cmd;
  629. pc->done = done;
  630. pc->timeout = jiffies + cmd->timeout_per_command;
  631. pc->callback = ide_scsi_callback;
  632. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  633. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  634. ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
  635. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  636. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  637. ide_scsi_hex_dump(pc->c, 12);
  638. }
  639. }
  640. blk_rq_init(NULL, rq);
  641. rq->special = (char *) pc;
  642. rq->cmd_type = REQ_TYPE_SPECIAL;
  643. spin_unlock_irq(host->host_lock);
  644. blk_execute_rq_nowait(drive->queue, scsi->disk, rq, 0, NULL);
  645. spin_lock_irq(host->host_lock);
  646. return 0;
  647. abort:
  648. kfree (pc);
  649. kfree (rq);
  650. cmd->result = DID_ERROR << 16;
  651. done(cmd);
  652. return 0;
  653. }
  654. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  655. {
  656. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  657. ide_drive_t *drive = scsi->drive;
  658. int busy;
  659. int ret = FAILED;
  660. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  661. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  662. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  663. if (!drive) {
  664. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  665. WARN_ON(1);
  666. goto no_drive;
  667. }
  668. /* First give it some more time, how much is "right" is hard to say :-( */
  669. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  670. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  671. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  672. spin_lock_irq(&ide_lock);
  673. /* If there is no pc running we're done (our interrupt took care of it) */
  674. if (!scsi->pc) {
  675. ret = SUCCESS;
  676. goto ide_unlock;
  677. }
  678. /* It's somewhere in flight. Does ide subsystem agree? */
  679. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  680. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  681. /*
  682. * FIXME - not sure this condition can ever occur
  683. */
  684. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  685. if (blk_sense_request(scsi->pc->rq))
  686. kfree(scsi->pc->buf);
  687. kfree(scsi->pc->rq);
  688. kfree(scsi->pc);
  689. scsi->pc = NULL;
  690. ret = SUCCESS;
  691. }
  692. ide_unlock:
  693. spin_unlock_irq(&ide_lock);
  694. no_drive:
  695. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  696. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  697. return ret;
  698. }
  699. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  700. {
  701. struct request *req;
  702. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  703. ide_drive_t *drive = scsi->drive;
  704. int ready = 0;
  705. int ret = SUCCESS;
  706. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  707. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  708. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  709. if (!drive) {
  710. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  711. WARN_ON(1);
  712. return FAILED;
  713. }
  714. spin_lock_irq(cmd->device->host->host_lock);
  715. spin_lock(&ide_lock);
  716. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  717. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  718. spin_unlock(&ide_lock);
  719. spin_unlock_irq(cmd->device->host->host_lock);
  720. return FAILED;
  721. }
  722. /* kill current request */
  723. if (__blk_end_request(req, -EIO, 0))
  724. BUG();
  725. if (blk_sense_request(req))
  726. kfree(scsi->pc->buf);
  727. kfree(scsi->pc);
  728. scsi->pc = NULL;
  729. kfree(req);
  730. /* now nuke the drive queue */
  731. while ((req = elv_next_request(drive->queue))) {
  732. if (__blk_end_request(req, -EIO, 0))
  733. BUG();
  734. }
  735. HWGROUP(drive)->rq = NULL;
  736. HWGROUP(drive)->handler = NULL;
  737. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  738. spin_unlock(&ide_lock);
  739. ide_do_reset(drive);
  740. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  741. do {
  742. spin_unlock_irq(cmd->device->host->host_lock);
  743. msleep(50);
  744. spin_lock_irq(cmd->device->host->host_lock);
  745. } while ( HWGROUP(drive)->handler );
  746. ready = drive_is_ready(drive);
  747. HWGROUP(drive)->busy--;
  748. if (!ready) {
  749. printk (KERN_ERR "ide-scsi: reset failed!\n");
  750. ret = FAILED;
  751. }
  752. spin_unlock_irq(cmd->device->host->host_lock);
  753. return ret;
  754. }
  755. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  756. sector_t capacity, int *parm)
  757. {
  758. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  759. ide_drive_t *drive = idescsi->drive;
  760. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  761. parm[0] = drive->bios_head;
  762. parm[1] = drive->bios_sect;
  763. parm[2] = drive->bios_cyl;
  764. }
  765. return 0;
  766. }
  767. static struct scsi_host_template idescsi_template = {
  768. .module = THIS_MODULE,
  769. .name = "idescsi",
  770. .info = idescsi_info,
  771. .slave_configure = idescsi_slave_configure,
  772. .ioctl = idescsi_ioctl,
  773. .queuecommand = idescsi_queue,
  774. .eh_abort_handler = idescsi_eh_abort,
  775. .eh_host_reset_handler = idescsi_eh_reset,
  776. .bios_param = idescsi_bios,
  777. .can_queue = 40,
  778. .this_id = -1,
  779. .sg_tablesize = 256,
  780. .cmd_per_lun = 5,
  781. .max_sectors = 128,
  782. .use_clustering = DISABLE_CLUSTERING,
  783. .emulated = 1,
  784. .proc_name = "ide-scsi",
  785. };
  786. static int ide_scsi_probe(ide_drive_t *drive)
  787. {
  788. idescsi_scsi_t *idescsi;
  789. struct Scsi_Host *host;
  790. struct gendisk *g;
  791. static int warned;
  792. int err = -ENOMEM;
  793. if (!warned && drive->media == ide_cdrom) {
  794. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  795. warned = 1;
  796. }
  797. if (idescsi_nocd && drive->media == ide_cdrom)
  798. return -ENODEV;
  799. if (!strstr("ide-scsi", drive->driver_req) ||
  800. !drive->present ||
  801. drive->media == ide_disk ||
  802. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  803. return -ENODEV;
  804. drive->scsi = 1;
  805. g = alloc_disk(1 << PARTN_BITS);
  806. if (!g)
  807. goto out_host_put;
  808. ide_init_disk(g, drive);
  809. host->max_id = 1;
  810. if (drive->id->last_lun)
  811. debug_log("%s: id->last_lun=%u\n", drive->name,
  812. drive->id->last_lun);
  813. if ((drive->id->last_lun & 0x7) != 7)
  814. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  815. else
  816. host->max_lun = 1;
  817. drive->driver_data = host;
  818. idescsi = scsihost_to_idescsi(host);
  819. idescsi->drive = drive;
  820. idescsi->driver = &idescsi_driver;
  821. idescsi->host = host;
  822. idescsi->disk = g;
  823. g->private_data = &idescsi->driver;
  824. ide_proc_register_driver(drive, &idescsi_driver);
  825. err = 0;
  826. idescsi_setup(drive, idescsi);
  827. g->fops = &idescsi_ops;
  828. ide_register_region(g);
  829. err = scsi_add_host(host, &drive->gendev);
  830. if (!err) {
  831. scsi_scan_host(host);
  832. return 0;
  833. }
  834. /* fall through on error */
  835. ide_unregister_region(g);
  836. ide_proc_unregister_driver(drive, &idescsi_driver);
  837. put_disk(g);
  838. out_host_put:
  839. drive->scsi = 0;
  840. scsi_host_put(host);
  841. return err;
  842. }
  843. static int __init init_idescsi_module(void)
  844. {
  845. return driver_register(&idescsi_driver.gen_driver);
  846. }
  847. static void __exit exit_idescsi_module(void)
  848. {
  849. driver_unregister(&idescsi_driver.gen_driver);
  850. }
  851. module_param(idescsi_nocd, int, 0600);
  852. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  853. module_init(init_idescsi_module);
  854. module_exit(exit_idescsi_module);
  855. MODULE_LICENSE("GPL");