ide-scsi.c 29 KB

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