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