ide-scsi.c 34 KB

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