ide-scsi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. if (PageHighMem(pc->sg->page)) {
  159. unsigned long flags;
  160. local_irq_save(flags);
  161. buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
  162. pc->sg->offset;
  163. drive->hwif->atapi_input_bytes(drive,
  164. buf + pc->b_count, count);
  165. kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
  166. local_irq_restore(flags);
  167. } else {
  168. buf = page_address(pc->sg->page) + pc->sg->offset;
  169. drive->hwif->atapi_input_bytes(drive,
  170. buf + pc->b_count, count);
  171. }
  172. bcount -= count; pc->b_count += count;
  173. if (pc->b_count == pc->sg->length) {
  174. pc->sg++;
  175. pc->b_count = 0;
  176. }
  177. }
  178. }
  179. static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
  180. {
  181. int count;
  182. char *buf;
  183. while (bcount) {
  184. if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
  185. printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
  186. idescsi_output_zeros (drive, bcount);
  187. return;
  188. }
  189. count = min(pc->sg->length - pc->b_count, bcount);
  190. if (PageHighMem(pc->sg->page)) {
  191. unsigned long flags;
  192. local_irq_save(flags);
  193. buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
  194. pc->sg->offset;
  195. drive->hwif->atapi_output_bytes(drive,
  196. buf + pc->b_count, count);
  197. kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
  198. local_irq_restore(flags);
  199. } else {
  200. buf = page_address(pc->sg->page) + pc->sg->offset;
  201. drive->hwif->atapi_output_bytes(drive,
  202. buf + pc->b_count, count);
  203. }
  204. bcount -= count; pc->b_count += count;
  205. if (pc->b_count == pc->sg->length) {
  206. pc->sg++;
  207. pc->b_count = 0;
  208. }
  209. }
  210. }
  211. /*
  212. * Most of the SCSI commands are supported directly by ATAPI devices.
  213. * idescsi_transform_pc handles the few exceptions.
  214. */
  215. static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
  216. {
  217. u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
  218. char *atapi_buf;
  219. if (!test_bit(PC_TRANSFORM, &pc->flags))
  220. return;
  221. if (drive->media == ide_cdrom || drive->media == ide_optical) {
  222. if (c[0] == READ_6 || c[0] == WRITE_6) {
  223. c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
  224. c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
  225. c[0] += (READ_10 - READ_6);
  226. }
  227. if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
  228. unsigned short new_len;
  229. if (!scsi_buf)
  230. return;
  231. if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
  232. return;
  233. memset(atapi_buf, 0, pc->buffer_size + 4);
  234. memset (c, 0, 12);
  235. c[0] = sc[0] | 0x40;
  236. c[1] = sc[1];
  237. c[2] = sc[2];
  238. new_len = sc[4] + 4;
  239. c[8] = new_len;
  240. c[7] = new_len >> 8;
  241. c[9] = sc[5];
  242. if (c[0] == MODE_SELECT_10) {
  243. atapi_buf[1] = scsi_buf[0]; /* Mode data length */
  244. atapi_buf[2] = scsi_buf[1]; /* Medium type */
  245. atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
  246. atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
  247. memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
  248. }
  249. pc->buffer = atapi_buf;
  250. pc->request_transfer += 4;
  251. pc->buffer_size += 4;
  252. }
  253. }
  254. }
  255. static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
  256. {
  257. u8 *atapi_buf = pc->buffer;
  258. u8 *sc = pc->scsi_cmd->cmnd;
  259. u8 *scsi_buf = pc->scsi_cmd->request_buffer;
  260. if (!test_bit(PC_TRANSFORM, &pc->flags))
  261. return;
  262. if (drive->media == ide_cdrom || drive->media == ide_optical) {
  263. if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
  264. scsi_buf[0] = atapi_buf[1]; /* Mode data length */
  265. scsi_buf[1] = atapi_buf[2]; /* Medium type */
  266. scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
  267. scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
  268. memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
  269. }
  270. if (pc->c[0] == INQUIRY) {
  271. scsi_buf[2] |= 2; /* ansi_revision */
  272. scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
  273. }
  274. }
  275. if (atapi_buf && atapi_buf != scsi_buf)
  276. kfree(atapi_buf);
  277. }
  278. static void hexdump(u8 *x, int len)
  279. {
  280. int i;
  281. printk("[ ");
  282. for (i = 0; i < len; i++)
  283. printk("%x ", x[i]);
  284. printk("]\n");
  285. }
  286. static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
  287. {
  288. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  289. idescsi_pc_t *pc;
  290. struct request *rq;
  291. u8 *buf;
  292. /* stuff a sense request in front of our current request */
  293. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  294. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  295. buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
  296. if (pc == NULL || rq == NULL || buf == NULL) {
  297. if (pc) kfree(pc);
  298. if (rq) kfree(rq);
  299. if (buf) kfree(buf);
  300. return -ENOMEM;
  301. }
  302. memset (pc, 0, sizeof (idescsi_pc_t));
  303. memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
  304. ide_init_drive_cmd(rq);
  305. rq->special = (char *) pc;
  306. pc->rq = rq;
  307. pc->buffer = buf;
  308. pc->c[0] = REQUEST_SENSE;
  309. pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
  310. rq->flags = REQ_SENSE;
  311. pc->timeout = jiffies + WAIT_READY;
  312. /* NOTE! Save the failed packet command in "rq->buffer" */
  313. rq->buffer = (void *) failed_command->special;
  314. pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
  315. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  316. printk ("ide-scsi: %s: queue cmd = ", drive->name);
  317. hexdump(pc->c, 6);
  318. }
  319. rq->rq_disk = scsi->disk;
  320. return ide_do_drive_cmd(drive, rq, ide_preempt);
  321. }
  322. static int idescsi_end_request(ide_drive_t *, int, int);
  323. static ide_startstop_t
  324. idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  325. {
  326. if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
  327. /* force an abort */
  328. HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
  329. rq->errors++;
  330. idescsi_end_request(drive, 0, 0);
  331. return ide_stopped;
  332. }
  333. static ide_startstop_t
  334. idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
  335. {
  336. #if IDESCSI_DEBUG_LOG
  337. printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
  338. ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
  339. #endif
  340. rq->errors |= ERROR_MAX;
  341. idescsi_end_request(drive, 0, 0);
  342. return ide_stopped;
  343. }
  344. static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
  345. {
  346. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  347. struct request *rq = HWGROUP(drive)->rq;
  348. idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
  349. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  350. struct Scsi_Host *host;
  351. u8 *scsi_buf;
  352. unsigned long flags;
  353. if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) {
  354. ide_end_request(drive, uptodate, nrsecs);
  355. return 0;
  356. }
  357. ide_end_drive_cmd (drive, 0, 0);
  358. if (rq->flags & REQ_SENSE) {
  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 (rq->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 (rq->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->driver_data;
  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();
  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. if (HWGROUP(drive)->handler != NULL)
  541. BUG();
  542. /* Set the interrupt routine */
  543. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  544. /* Send the actual packet */
  545. drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
  546. if (test_bit (PC_DMA_OK, &pc->flags)) {
  547. set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  548. hwif->dma_start(drive);
  549. }
  550. return ide_started;
  551. }
  552. static inline int idescsi_set_direction(idescsi_pc_t *pc)
  553. {
  554. switch (pc->c[0]) {
  555. case READ_6: case READ_10: case READ_12:
  556. clear_bit(PC_WRITING, &pc->flags);
  557. return 0;
  558. case WRITE_6: case WRITE_10: case WRITE_12:
  559. set_bit(PC_WRITING, &pc->flags);
  560. return 0;
  561. default:
  562. return 1;
  563. }
  564. }
  565. static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
  566. {
  567. ide_hwif_t *hwif = drive->hwif;
  568. struct scatterlist *sg, *scsi_sg;
  569. int segments;
  570. if (!pc->request_transfer || pc->request_transfer % 1024)
  571. return 1;
  572. if (idescsi_set_direction(pc))
  573. return 1;
  574. sg = hwif->sg_table;
  575. scsi_sg = pc->scsi_cmd->request_buffer;
  576. segments = pc->scsi_cmd->use_sg;
  577. if (segments > hwif->sg_max_nents)
  578. return 1;
  579. if (!segments) {
  580. hwif->sg_nents = 1;
  581. sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
  582. } else {
  583. hwif->sg_nents = segments;
  584. memcpy(sg, scsi_sg, sizeof(*sg) * segments);
  585. }
  586. return 0;
  587. }
  588. /*
  589. * Issue a packet command
  590. */
  591. static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
  592. {
  593. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  594. ide_hwif_t *hwif = drive->hwif;
  595. atapi_feature_t feature;
  596. atapi_bcount_t bcount;
  597. scsi->pc=pc; /* Set the current packet command */
  598. pc->actually_transferred=0; /* We haven't transferred any data yet */
  599. pc->current_position=pc->buffer;
  600. bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
  601. feature.all = 0;
  602. if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
  603. hwif->sg_mapped = 1;
  604. feature.b.dma = !hwif->dma_setup(drive);
  605. hwif->sg_mapped = 0;
  606. }
  607. SELECT_DRIVE(drive);
  608. if (IDE_CONTROL_REG)
  609. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  610. HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
  611. HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
  612. HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
  613. if (feature.b.dma)
  614. set_bit(PC_DMA_OK, &pc->flags);
  615. if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
  616. if (HWGROUP(drive)->handler != NULL)
  617. BUG();
  618. ide_set_handler(drive, &idescsi_transfer_pc,
  619. get_timeout(pc), idescsi_expiry);
  620. /* Issue the packet command */
  621. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  622. return ide_started;
  623. } else {
  624. /* Issue the packet command */
  625. HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
  626. return idescsi_transfer_pc(drive);
  627. }
  628. }
  629. /*
  630. * idescsi_do_request is our request handling function.
  631. */
  632. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
  633. {
  634. #if IDESCSI_DEBUG_LOG
  635. 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);
  636. printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
  637. #endif /* IDESCSI_DEBUG_LOG */
  638. if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) {
  639. return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
  640. }
  641. blk_dump_rq_flags(rq, "ide-scsi: unsup command");
  642. idescsi_end_request (drive, 0, 0);
  643. return ide_stopped;
  644. }
  645. static void idescsi_add_settings(ide_drive_t *drive)
  646. {
  647. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  648. /*
  649. * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
  650. */
  651. ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  652. ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  653. ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  654. ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  655. ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  656. }
  657. /*
  658. * Driver initialization.
  659. */
  660. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
  661. {
  662. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  663. set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
  664. set_bit(IDESCSI_TRANSFORM, &scsi->transform);
  665. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  666. #if IDESCSI_DEBUG_LOG
  667. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  668. #endif /* IDESCSI_DEBUG_LOG */
  669. idescsi_add_settings(drive);
  670. }
  671. static int ide_scsi_remove(struct device *dev)
  672. {
  673. ide_drive_t *drive = to_ide_device(dev);
  674. struct Scsi_Host *scsihost = drive->driver_data;
  675. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  676. struct gendisk *g = scsi->disk;
  677. ide_unregister_subdriver(drive, scsi->driver);
  678. ide_unregister_region(g);
  679. drive->driver_data = NULL;
  680. g->private_data = NULL;
  681. put_disk(g);
  682. scsi_remove_host(scsihost);
  683. ide_scsi_put(scsi);
  684. return 0;
  685. }
  686. static int ide_scsi_probe(struct device *);
  687. #ifdef CONFIG_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. #else
  693. # define idescsi_proc NULL
  694. #endif
  695. static ide_driver_t idescsi_driver = {
  696. .owner = THIS_MODULE,
  697. .gen_driver = {
  698. .name = "ide-scsi",
  699. .bus = &ide_bus_type,
  700. .probe = ide_scsi_probe,
  701. .remove = ide_scsi_remove,
  702. },
  703. .version = IDESCSI_VERSION,
  704. .media = ide_scsi,
  705. .supports_dsc_overlap = 0,
  706. .proc = idescsi_proc,
  707. .do_request = idescsi_do_request,
  708. .end_request = idescsi_end_request,
  709. .error = idescsi_atapi_error,
  710. .abort = idescsi_atapi_abort,
  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. ide_drive_t *drive;
  717. if (!(scsi = ide_scsi_get(disk)))
  718. return -ENXIO;
  719. drive = scsi->drive;
  720. drive->usage++;
  721. return 0;
  722. }
  723. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  724. {
  725. struct gendisk *disk = inode->i_bdev->bd_disk;
  726. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  727. ide_drive_t *drive = scsi->drive;
  728. drive->usage--;
  729. ide_scsi_put(scsi);
  730. return 0;
  731. }
  732. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  733. unsigned int cmd, unsigned long arg)
  734. {
  735. struct block_device *bdev = inode->i_bdev;
  736. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  737. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  738. }
  739. static struct block_device_operations idescsi_ops = {
  740. .owner = THIS_MODULE,
  741. .open = idescsi_ide_open,
  742. .release = idescsi_ide_release,
  743. .ioctl = idescsi_ide_ioctl,
  744. };
  745. static int idescsi_slave_configure(struct scsi_device * sdp)
  746. {
  747. /* Configure detected device */
  748. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  749. return 0;
  750. }
  751. static const char *idescsi_info (struct Scsi_Host *host)
  752. {
  753. return "SCSI host adapter emulation for IDE ATAPI devices";
  754. }
  755. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  756. {
  757. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  758. if (cmd == SG_SET_TRANSFORM) {
  759. if (arg)
  760. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  761. else
  762. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  763. return 0;
  764. } else if (cmd == SG_GET_TRANSFORM)
  765. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  766. return -EINVAL;
  767. }
  768. static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
  769. {
  770. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  771. /* this was a layering violation and we can't support it
  772. anymore, sorry. */
  773. #if 0
  774. struct gendisk *disk = cmd->request->rq_disk;
  775. if (disk) {
  776. struct Scsi_Device_Template **p = disk->private_data;
  777. if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
  778. return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  779. }
  780. #endif
  781. return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
  782. }
  783. static int idescsi_queue (struct scsi_cmnd *cmd,
  784. void (*done)(struct scsi_cmnd *))
  785. {
  786. struct Scsi_Host *host = cmd->device->host;
  787. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  788. ide_drive_t *drive = scsi->drive;
  789. struct request *rq = NULL;
  790. idescsi_pc_t *pc = NULL;
  791. if (!drive) {
  792. printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->device->id);
  793. goto abort;
  794. }
  795. scsi = drive_to_idescsi(drive);
  796. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  797. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  798. if (rq == NULL || pc == NULL) {
  799. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  800. goto abort;
  801. }
  802. memset (pc->c, 0, 12);
  803. pc->flags = 0;
  804. pc->rq = rq;
  805. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  806. if (cmd->use_sg) {
  807. pc->buffer = NULL;
  808. pc->sg = cmd->request_buffer;
  809. } else {
  810. pc->buffer = cmd->request_buffer;
  811. pc->sg = NULL;
  812. }
  813. pc->b_count = 0;
  814. pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
  815. pc->scsi_cmd = cmd;
  816. pc->done = done;
  817. pc->timeout = jiffies + cmd->timeout_per_command;
  818. if (should_transform(drive, cmd))
  819. set_bit(PC_TRANSFORM, &pc->flags);
  820. idescsi_transform_pc1 (drive, pc);
  821. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  822. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  823. hexdump(cmd->cmnd, cmd->cmd_len);
  824. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  825. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  826. hexdump(pc->c, 12);
  827. }
  828. }
  829. ide_init_drive_cmd (rq);
  830. rq->special = (char *) pc;
  831. rq->flags = REQ_SPECIAL;
  832. spin_unlock_irq(host->host_lock);
  833. rq->rq_disk = scsi->disk;
  834. (void) ide_do_drive_cmd (drive, rq, ide_end);
  835. spin_lock_irq(host->host_lock);
  836. return 0;
  837. abort:
  838. if (pc) kfree (pc);
  839. if (rq) kfree (rq);
  840. cmd->result = DID_ERROR << 16;
  841. done(cmd);
  842. return 0;
  843. }
  844. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  845. {
  846. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  847. ide_drive_t *drive = scsi->drive;
  848. int busy;
  849. int ret = FAILED;
  850. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  851. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  852. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  853. if (!drive) {
  854. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  855. WARN_ON(1);
  856. goto no_drive;
  857. }
  858. /* First give it some more time, how much is "right" is hard to say :-( */
  859. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  860. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  861. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  862. spin_lock_irq(&ide_lock);
  863. /* If there is no pc running we're done (our interrupt took care of it) */
  864. if (!scsi->pc) {
  865. ret = SUCCESS;
  866. goto ide_unlock;
  867. }
  868. /* It's somewhere in flight. Does ide subsystem agree? */
  869. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  870. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  871. /*
  872. * FIXME - not sure this condition can ever occur
  873. */
  874. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  875. if (scsi->pc->rq->flags & REQ_SENSE)
  876. kfree(scsi->pc->buffer);
  877. kfree(scsi->pc->rq);
  878. kfree(scsi->pc);
  879. scsi->pc = NULL;
  880. ret = SUCCESS;
  881. }
  882. ide_unlock:
  883. spin_unlock_irq(&ide_lock);
  884. no_drive:
  885. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  886. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  887. return ret;
  888. }
  889. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  890. {
  891. struct request *req;
  892. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  893. ide_drive_t *drive = scsi->drive;
  894. int ready = 0;
  895. int ret = SUCCESS;
  896. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  897. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  898. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  899. if (!drive) {
  900. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  901. WARN_ON(1);
  902. return FAILED;
  903. }
  904. spin_lock_irq(cmd->device->host->host_lock);
  905. spin_lock(&ide_lock);
  906. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  907. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  908. spin_unlock(&ide_lock);
  909. spin_unlock_irq(cmd->device->host->host_lock);
  910. return FAILED;
  911. }
  912. /* kill current request */
  913. blkdev_dequeue_request(req);
  914. end_that_request_last(req);
  915. if (req->flags & REQ_SENSE)
  916. kfree(scsi->pc->buffer);
  917. kfree(scsi->pc);
  918. scsi->pc = NULL;
  919. kfree(req);
  920. /* now nuke the drive queue */
  921. while ((req = elv_next_request(drive->queue))) {
  922. blkdev_dequeue_request(req);
  923. end_that_request_last(req);
  924. }
  925. HWGROUP(drive)->rq = NULL;
  926. HWGROUP(drive)->handler = NULL;
  927. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  928. spin_unlock(&ide_lock);
  929. ide_do_reset(drive);
  930. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  931. do {
  932. spin_unlock_irq(cmd->device->host->host_lock);
  933. msleep(50);
  934. spin_lock_irq(cmd->device->host->host_lock);
  935. } while ( HWGROUP(drive)->handler );
  936. ready = drive_is_ready(drive);
  937. HWGROUP(drive)->busy--;
  938. if (!ready) {
  939. printk (KERN_ERR "ide-scsi: reset failed!\n");
  940. ret = FAILED;
  941. }
  942. spin_unlock_irq(cmd->device->host->host_lock);
  943. return ret;
  944. }
  945. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  946. sector_t capacity, int *parm)
  947. {
  948. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  949. ide_drive_t *drive = idescsi->drive;
  950. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  951. parm[0] = drive->bios_head;
  952. parm[1] = drive->bios_sect;
  953. parm[2] = drive->bios_cyl;
  954. }
  955. return 0;
  956. }
  957. static struct scsi_host_template idescsi_template = {
  958. .module = THIS_MODULE,
  959. .name = "idescsi",
  960. .info = idescsi_info,
  961. .slave_configure = idescsi_slave_configure,
  962. .ioctl = idescsi_ioctl,
  963. .queuecommand = idescsi_queue,
  964. .eh_abort_handler = idescsi_eh_abort,
  965. .eh_host_reset_handler = idescsi_eh_reset,
  966. .bios_param = idescsi_bios,
  967. .can_queue = 40,
  968. .this_id = -1,
  969. .sg_tablesize = 256,
  970. .cmd_per_lun = 5,
  971. .max_sectors = 128,
  972. .use_clustering = DISABLE_CLUSTERING,
  973. .emulated = 1,
  974. .proc_name = "ide-scsi",
  975. };
  976. static int ide_scsi_probe(struct device *dev)
  977. {
  978. ide_drive_t *drive = to_ide_device(dev);
  979. idescsi_scsi_t *idescsi;
  980. struct Scsi_Host *host;
  981. struct gendisk *g;
  982. static int warned;
  983. int err = -ENOMEM;
  984. if (!warned && drive->media == ide_cdrom) {
  985. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  986. warned = 1;
  987. }
  988. if (!strstr("ide-scsi", drive->driver_req) ||
  989. !drive->present ||
  990. drive->media == ide_disk ||
  991. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  992. return -ENODEV;
  993. g = alloc_disk(1 << PARTN_BITS);
  994. if (!g)
  995. goto out_host_put;
  996. ide_init_disk(g, drive);
  997. host->max_id = 1;
  998. #if IDESCSI_DEBUG_LOG
  999. if (drive->id->last_lun)
  1000. printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
  1001. #endif
  1002. if ((drive->id->last_lun & 0x7) != 7)
  1003. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  1004. else
  1005. host->max_lun = 1;
  1006. drive->driver_data = host;
  1007. idescsi = scsihost_to_idescsi(host);
  1008. idescsi->drive = drive;
  1009. idescsi->driver = &idescsi_driver;
  1010. idescsi->host = host;
  1011. idescsi->disk = g;
  1012. g->private_data = &idescsi->driver;
  1013. ide_register_subdriver(drive, &idescsi_driver);
  1014. err = 0;
  1015. idescsi_setup(drive, idescsi);
  1016. g->fops = &idescsi_ops;
  1017. ide_register_region(g);
  1018. err = scsi_add_host(host, &drive->gendev);
  1019. if (!err) {
  1020. scsi_scan_host(host);
  1021. return 0;
  1022. }
  1023. /* fall through on error */
  1024. ide_unregister_region(g);
  1025. ide_unregister_subdriver(drive, &idescsi_driver);
  1026. put_disk(g);
  1027. out_host_put:
  1028. scsi_host_put(host);
  1029. return err;
  1030. }
  1031. static int __init init_idescsi_module(void)
  1032. {
  1033. return driver_register(&idescsi_driver.gen_driver);
  1034. }
  1035. static void __exit exit_idescsi_module(void)
  1036. {
  1037. driver_unregister(&idescsi_driver.gen_driver);
  1038. }
  1039. module_init(init_idescsi_module);
  1040. module_exit(exit_idescsi_module);
  1041. MODULE_LICENSE("GPL");