ide-scsi.c 29 KB

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