ide-scsi.c 28 KB

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