ide-scsi.c 30 KB

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