ide-scsi.c 28 KB

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