ide-scsi.c 33 KB

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