ide-scsi.c 29 KB

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