ide-scsi.c 30 KB

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