ide-scsi.c 30 KB

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