ide-scsi.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  295. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  296. buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
  297. if (pc == NULL || rq == NULL || buf == NULL) {
  298. kfree(buf);
  299. kfree(rq);
  300. kfree(pc);
  301. return -ENOMEM;
  302. }
  303. memset (pc, 0, sizeof (idescsi_pc_t));
  304. memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
  305. ide_init_drive_cmd(rq);
  306. rq->special = (char *) pc;
  307. pc->rq = rq;
  308. pc->buffer = buf;
  309. pc->c[0] = REQUEST_SENSE;
  310. pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
  311. rq->cmd_type = REQ_TYPE_SENSE;
  312. pc->timeout = jiffies + WAIT_READY;
  313. /* NOTE! Save the failed packet command in "rq->buffer" */
  314. rq->buffer = (void *) failed_command->special;
  315. pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
  316. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  317. printk ("ide-scsi: %s: queue cmd = ", drive->name);
  318. hexdump(pc->c, 6);
  319. }
  320. rq->rq_disk = scsi->disk;
  321. return ide_do_drive_cmd(drive, rq, ide_preempt);
  322. }
  323. static int idescsi_end_request(ide_drive_t *, int, int);
  324. static ide_startstop_t
  325. idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  326. {
  327. if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
  328. /* force an abort */
  329. HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
  330. rq->errors++;
  331. idescsi_end_request(drive, 0, 0);
  332. return ide_stopped;
  333. }
  334. static ide_startstop_t
  335. idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
  336. {
  337. #if IDESCSI_DEBUG_LOG
  338. printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
  339. ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
  340. #endif
  341. rq->errors |= ERROR_MAX;
  342. idescsi_end_request(drive, 0, 0);
  343. return ide_stopped;
  344. }
  345. static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
  346. {
  347. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  348. struct request *rq = HWGROUP(drive)->rq;
  349. idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
  350. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  351. struct Scsi_Host *host;
  352. u8 *scsi_buf;
  353. int errors = rq->errors;
  354. unsigned long flags;
  355. if (!blk_special_request(rq) && !blk_sense_request(rq)) {
  356. ide_end_request(drive, uptodate, nrsecs);
  357. return 0;
  358. }
  359. ide_end_drive_cmd (drive, 0, 0);
  360. if (blk_sense_request(rq)) {
  361. idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
  362. if (log) {
  363. printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
  364. hexdump(pc->buffer,16);
  365. }
  366. memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
  367. kfree(pc->buffer);
  368. kfree(pc);
  369. kfree(rq);
  370. pc = opc;
  371. rq = pc->rq;
  372. pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
  373. ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
  374. } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
  375. if (log)
  376. printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
  377. drive->name, pc->scsi_cmd->serial_number);
  378. pc->scsi_cmd->result = DID_TIME_OUT << 16;
  379. } else if (errors >= ERROR_MAX) {
  380. pc->scsi_cmd->result = DID_ERROR << 16;
  381. if (log)
  382. printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  383. } else if (errors) {
  384. if (log)
  385. printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
  386. if (!idescsi_check_condition(drive, rq))
  387. /* we started a request sense, so we'll be back, exit for now */
  388. return 0;
  389. pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
  390. } else {
  391. pc->scsi_cmd->result = DID_OK << 16;
  392. idescsi_transform_pc2 (drive, pc);
  393. if (log) {
  394. printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
  395. if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
  396. printk(", rst = ");
  397. scsi_buf = pc->scsi_cmd->request_buffer;
  398. hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
  399. } else printk("\n");
  400. }
  401. }
  402. host = pc->scsi_cmd->device->host;
  403. spin_lock_irqsave(host->host_lock, flags);
  404. pc->done(pc->scsi_cmd);
  405. spin_unlock_irqrestore(host->host_lock, flags);
  406. kfree(pc);
  407. kfree(rq);
  408. scsi->pc = NULL;
  409. return 0;
  410. }
  411. static inline unsigned long get_timeout(idescsi_pc_t *pc)
  412. {
  413. return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
  414. }
  415. static int idescsi_expiry(ide_drive_t *drive)
  416. {
  417. idescsi_scsi_t *scsi = drive->driver_data;
  418. idescsi_pc_t *pc = scsi->pc;
  419. #if IDESCSI_DEBUG_LOG
  420. printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
  421. #endif
  422. set_bit(PC_TIMEDOUT, &pc->flags);
  423. return 0; /* we do not want the ide subsystem to retry */
  424. }
  425. /*
  426. * Our interrupt handler.
  427. */
  428. static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
  429. {
  430. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  431. idescsi_pc_t *pc=scsi->pc;
  432. struct request *rq = pc->rq;
  433. atapi_bcount_t bcount;
  434. atapi_status_t status;
  435. atapi_ireason_t ireason;
  436. atapi_feature_t feature;
  437. unsigned int temp;
  438. #if IDESCSI_DEBUG_LOG
  439. printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
  440. #endif /* IDESCSI_DEBUG_LOG */
  441. if (test_bit(PC_TIMEDOUT, &pc->flags)){
  442. #if IDESCSI_DEBUG_LOG
  443. printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
  444. pc->scsi_cmd->serial_number, jiffies);
  445. #endif
  446. /* end this request now - scsi should retry it*/
  447. idescsi_end_request (drive, 1, 0);
  448. return ide_stopped;
  449. }
  450. if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
  451. #if IDESCSI_DEBUG_LOG
  452. printk ("ide-scsi: %s: DMA complete\n", drive->name);
  453. #endif /* IDESCSI_DEBUG_LOG */
  454. pc->actually_transferred=pc->request_transfer;
  455. (void) HWIF(drive)->ide_dma_end(drive);
  456. }
  457. feature.all = 0;
  458. /* Clear the interrupt */
  459. status.all = HWIF(drive)->INB(IDE_STATUS_REG);
  460. if (!status.b.drq) {
  461. /* No more interrupts */
  462. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  463. printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
  464. local_irq_enable_in_hardirq();
  465. if (status.b.check)
  466. rq->errors++;
  467. idescsi_end_request (drive, 1, 0);
  468. return ide_stopped;
  469. }
  470. bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  471. bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  472. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  473. if (ireason.b.cod) {
  474. printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
  475. return ide_do_reset (drive);
  476. }
  477. if (ireason.b.io) {
  478. temp = pc->actually_transferred + bcount.all;
  479. if (temp > pc->request_transfer) {
  480. if (temp > pc->buffer_size) {
  481. printk(KERN_ERR "ide-scsi: The scsi wants to "
  482. "send us more data than expected "
  483. "- discarding data\n");
  484. temp = pc->buffer_size - pc->actually_transferred;
  485. if (temp) {
  486. clear_bit(PC_WRITING, &pc->flags);
  487. if (pc->sg)
  488. idescsi_input_buffers(drive, pc, temp);
  489. else
  490. drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
  491. printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
  492. }
  493. pc->actually_transferred += temp;
  494. pc->current_position += temp;
  495. idescsi_discard_data(drive, bcount.all - temp);
  496. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  497. return ide_started;
  498. }
  499. #if IDESCSI_DEBUG_LOG
  500. printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
  501. #endif /* IDESCSI_DEBUG_LOG */
  502. }
  503. }
  504. if (ireason.b.io) {
  505. clear_bit(PC_WRITING, &pc->flags);
  506. if (pc->sg)
  507. idescsi_input_buffers(drive, pc, bcount.all);
  508. else
  509. HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
  510. } else {
  511. set_bit(PC_WRITING, &pc->flags);
  512. if (pc->sg)
  513. idescsi_output_buffers (drive, pc, bcount.all);
  514. else
  515. HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
  516. }
  517. /* Update the current position */
  518. pc->actually_transferred += bcount.all;
  519. pc->current_position += bcount.all;
  520. /* And set the interrupt handler again */
  521. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  522. return ide_started;
  523. }
  524. static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
  525. {
  526. ide_hwif_t *hwif = drive->hwif;
  527. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  528. idescsi_pc_t *pc = scsi->pc;
  529. atapi_ireason_t ireason;
  530. ide_startstop_t startstop;
  531. if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  532. printk(KERN_ERR "ide-scsi: Strange, packet command "
  533. "initiated yet DRQ isn't asserted\n");
  534. return startstop;
  535. }
  536. ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
  537. if (!ireason.b.cod || ireason.b.io) {
  538. printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
  539. "issuing a packet command\n");
  540. return ide_do_reset (drive);
  541. }
  542. BUG_ON(HWGROUP(drive)->handler != NULL);
  543. /* Set the interrupt routine */
  544. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
  545. /* Send the actual packet */
  546. drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
  547. if (test_bit (PC_DMA_OK, &pc->flags)) {
  548. set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  549. hwif->dma_start(drive);
  550. }
  551. return ide_started;
  552. }
  553. static inline int idescsi_set_direction(idescsi_pc_t *pc)
  554. {
  555. switch (pc->c[0]) {
  556. case READ_6: case READ_10: case READ_12:
  557. clear_bit(PC_WRITING, &pc->flags);
  558. return 0;
  559. case WRITE_6: case WRITE_10: case WRITE_12:
  560. set_bit(PC_WRITING, &pc->flags);
  561. return 0;
  562. default:
  563. return 1;
  564. }
  565. }
  566. static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
  567. {
  568. ide_hwif_t *hwif = drive->hwif;
  569. struct scatterlist *sg, *scsi_sg;
  570. int segments;
  571. if (!pc->request_transfer || pc->request_transfer % 1024)
  572. return 1;
  573. if (idescsi_set_direction(pc))
  574. return 1;
  575. sg = hwif->sg_table;
  576. scsi_sg = pc->scsi_cmd->request_buffer;
  577. segments = pc->scsi_cmd->use_sg;
  578. if (segments > hwif->sg_max_nents)
  579. return 1;
  580. if (!segments) {
  581. hwif->sg_nents = 1;
  582. sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
  583. } else {
  584. hwif->sg_nents = segments;
  585. memcpy(sg, scsi_sg, sizeof(*sg) * segments);
  586. }
  587. return 0;
  588. }
  589. /*
  590. * Issue a packet command
  591. */
  592. static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
  593. {
  594. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  595. ide_hwif_t *hwif = drive->hwif;
  596. atapi_feature_t feature;
  597. atapi_bcount_t bcount;
  598. scsi->pc=pc; /* Set the current packet command */
  599. pc->actually_transferred=0; /* We haven't transferred any data yet */
  600. pc->current_position=pc->buffer;
  601. bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
  602. feature.all = 0;
  603. if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
  604. hwif->sg_mapped = 1;
  605. feature.b.dma = !hwif->dma_setup(drive);
  606. hwif->sg_mapped = 0;
  607. }
  608. SELECT_DRIVE(drive);
  609. if (IDE_CONTROL_REG)
  610. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  611. HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
  612. HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
  613. HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
  614. if (feature.b.dma)
  615. set_bit(PC_DMA_OK, &pc->flags);
  616. if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
  617. BUG_ON(HWGROUP(drive)->handler != NULL);
  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 "dev: %s, cmd: %x, errors: %d\n", 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 (blk_sense_request(rq) || blk_special_request(rq)) {
  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 data type min max mul_factor div_factor data pointer set function
  650. */
  651. ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  652. ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  653. ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  654. ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  655. ide_add_setting(drive, "log", SETTING_RW, 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 void ide_scsi_remove(ide_drive_t *drive)
  672. {
  673. struct Scsi_Host *scsihost = drive->driver_data;
  674. struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
  675. struct gendisk *g = scsi->disk;
  676. ide_unregister_subdriver(drive, scsi->driver);
  677. ide_unregister_region(g);
  678. drive->driver_data = NULL;
  679. g->private_data = NULL;
  680. put_disk(g);
  681. scsi_remove_host(scsihost);
  682. ide_scsi_put(scsi);
  683. }
  684. static int ide_scsi_probe(ide_drive_t *);
  685. #ifdef CONFIG_IDE_PROC_FS
  686. static ide_proc_entry_t idescsi_proc[] = {
  687. { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
  688. { NULL, 0, NULL, NULL }
  689. };
  690. #else
  691. # define idescsi_proc NULL
  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. .proc = idescsi_proc,
  705. .do_request = idescsi_do_request,
  706. .end_request = idescsi_end_request,
  707. .error = idescsi_atapi_error,
  708. .abort = idescsi_atapi_abort,
  709. };
  710. static int idescsi_ide_open(struct inode *inode, struct file *filp)
  711. {
  712. struct gendisk *disk = inode->i_bdev->bd_disk;
  713. struct ide_scsi_obj *scsi;
  714. if (!(scsi = ide_scsi_get(disk)))
  715. return -ENXIO;
  716. return 0;
  717. }
  718. static int idescsi_ide_release(struct inode *inode, struct file *filp)
  719. {
  720. struct gendisk *disk = inode->i_bdev->bd_disk;
  721. struct ide_scsi_obj *scsi = ide_scsi_g(disk);
  722. ide_scsi_put(scsi);
  723. return 0;
  724. }
  725. static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
  726. unsigned int cmd, unsigned long arg)
  727. {
  728. struct block_device *bdev = inode->i_bdev;
  729. struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
  730. return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
  731. }
  732. static struct block_device_operations idescsi_ops = {
  733. .owner = THIS_MODULE,
  734. .open = idescsi_ide_open,
  735. .release = idescsi_ide_release,
  736. .ioctl = idescsi_ide_ioctl,
  737. };
  738. static int idescsi_slave_configure(struct scsi_device * sdp)
  739. {
  740. /* Configure detected device */
  741. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
  742. return 0;
  743. }
  744. static const char *idescsi_info (struct Scsi_Host *host)
  745. {
  746. return "SCSI host adapter emulation for IDE ATAPI devices";
  747. }
  748. static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
  749. {
  750. idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
  751. if (cmd == SG_SET_TRANSFORM) {
  752. if (arg)
  753. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  754. else
  755. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  756. return 0;
  757. } else if (cmd == SG_GET_TRANSFORM)
  758. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
  759. return -EINVAL;
  760. }
  761. static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
  762. {
  763. idescsi_scsi_t *scsi = drive_to_idescsi(drive);
  764. /* this was a layering violation and we can't support it
  765. anymore, sorry. */
  766. #if 0
  767. struct gendisk *disk = cmd->request->rq_disk;
  768. if (disk) {
  769. struct struct scsi_device_Template **p = disk->private_data;
  770. if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
  771. return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  772. }
  773. #endif
  774. return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
  775. }
  776. static int idescsi_queue (struct scsi_cmnd *cmd,
  777. void (*done)(struct scsi_cmnd *))
  778. {
  779. struct Scsi_Host *host = cmd->device->host;
  780. idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
  781. ide_drive_t *drive = scsi->drive;
  782. struct request *rq = NULL;
  783. idescsi_pc_t *pc = NULL;
  784. if (!drive) {
  785. scmd_printk (KERN_ERR, cmd, "drive not present\n");
  786. goto abort;
  787. }
  788. scsi = drive_to_idescsi(drive);
  789. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  790. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  791. if (rq == NULL || pc == NULL) {
  792. printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
  793. goto abort;
  794. }
  795. memset (pc->c, 0, 12);
  796. pc->flags = 0;
  797. pc->rq = rq;
  798. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  799. if (cmd->use_sg) {
  800. pc->buffer = NULL;
  801. pc->sg = cmd->request_buffer;
  802. } else {
  803. pc->buffer = cmd->request_buffer;
  804. pc->sg = NULL;
  805. }
  806. pc->b_count = 0;
  807. pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
  808. pc->scsi_cmd = cmd;
  809. pc->done = done;
  810. pc->timeout = jiffies + cmd->timeout_per_command;
  811. if (should_transform(drive, cmd))
  812. set_bit(PC_TRANSFORM, &pc->flags);
  813. idescsi_transform_pc1 (drive, pc);
  814. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  815. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  816. hexdump(cmd->cmnd, cmd->cmd_len);
  817. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  818. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  819. hexdump(pc->c, 12);
  820. }
  821. }
  822. ide_init_drive_cmd (rq);
  823. rq->special = (char *) pc;
  824. rq->cmd_type = REQ_TYPE_SPECIAL;
  825. spin_unlock_irq(host->host_lock);
  826. rq->rq_disk = scsi->disk;
  827. (void) ide_do_drive_cmd (drive, rq, ide_end);
  828. spin_lock_irq(host->host_lock);
  829. return 0;
  830. abort:
  831. kfree (pc);
  832. kfree (rq);
  833. cmd->result = DID_ERROR << 16;
  834. done(cmd);
  835. return 0;
  836. }
  837. static int idescsi_eh_abort (struct scsi_cmnd *cmd)
  838. {
  839. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  840. ide_drive_t *drive = scsi->drive;
  841. int busy;
  842. int ret = FAILED;
  843. /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
  844. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  845. printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
  846. if (!drive) {
  847. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
  848. WARN_ON(1);
  849. goto no_drive;
  850. }
  851. /* First give it some more time, how much is "right" is hard to say :-( */
  852. busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
  853. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  854. printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
  855. spin_lock_irq(&ide_lock);
  856. /* If there is no pc running we're done (our interrupt took care of it) */
  857. if (!scsi->pc) {
  858. ret = SUCCESS;
  859. goto ide_unlock;
  860. }
  861. /* It's somewhere in flight. Does ide subsystem agree? */
  862. if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
  863. elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
  864. /*
  865. * FIXME - not sure this condition can ever occur
  866. */
  867. printk (KERN_ERR "ide-scsi: cmd aborted!\n");
  868. if (blk_sense_request(scsi->pc->rq))
  869. kfree(scsi->pc->buffer);
  870. kfree(scsi->pc->rq);
  871. kfree(scsi->pc);
  872. scsi->pc = NULL;
  873. ret = SUCCESS;
  874. }
  875. ide_unlock:
  876. spin_unlock_irq(&ide_lock);
  877. no_drive:
  878. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  879. printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
  880. return ret;
  881. }
  882. static int idescsi_eh_reset (struct scsi_cmnd *cmd)
  883. {
  884. struct request *req;
  885. idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
  886. ide_drive_t *drive = scsi->drive;
  887. int ready = 0;
  888. int ret = SUCCESS;
  889. /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
  890. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  891. printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
  892. if (!drive) {
  893. printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
  894. WARN_ON(1);
  895. return FAILED;
  896. }
  897. spin_lock_irq(cmd->device->host->host_lock);
  898. spin_lock(&ide_lock);
  899. if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
  900. printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
  901. spin_unlock(&ide_lock);
  902. spin_unlock_irq(cmd->device->host->host_lock);
  903. return FAILED;
  904. }
  905. /* kill current request */
  906. blkdev_dequeue_request(req);
  907. end_that_request_last(req, 0);
  908. if (blk_sense_request(req))
  909. kfree(scsi->pc->buffer);
  910. kfree(scsi->pc);
  911. scsi->pc = NULL;
  912. kfree(req);
  913. /* now nuke the drive queue */
  914. while ((req = elv_next_request(drive->queue))) {
  915. blkdev_dequeue_request(req);
  916. end_that_request_last(req, 0);
  917. }
  918. HWGROUP(drive)->rq = NULL;
  919. HWGROUP(drive)->handler = NULL;
  920. HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
  921. spin_unlock(&ide_lock);
  922. ide_do_reset(drive);
  923. /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
  924. do {
  925. spin_unlock_irq(cmd->device->host->host_lock);
  926. msleep(50);
  927. spin_lock_irq(cmd->device->host->host_lock);
  928. } while ( HWGROUP(drive)->handler );
  929. ready = drive_is_ready(drive);
  930. HWGROUP(drive)->busy--;
  931. if (!ready) {
  932. printk (KERN_ERR "ide-scsi: reset failed!\n");
  933. ret = FAILED;
  934. }
  935. spin_unlock_irq(cmd->device->host->host_lock);
  936. return ret;
  937. }
  938. static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
  939. sector_t capacity, int *parm)
  940. {
  941. idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
  942. ide_drive_t *drive = idescsi->drive;
  943. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  944. parm[0] = drive->bios_head;
  945. parm[1] = drive->bios_sect;
  946. parm[2] = drive->bios_cyl;
  947. }
  948. return 0;
  949. }
  950. static struct scsi_host_template idescsi_template = {
  951. .module = THIS_MODULE,
  952. .name = "idescsi",
  953. .info = idescsi_info,
  954. .slave_configure = idescsi_slave_configure,
  955. .ioctl = idescsi_ioctl,
  956. .queuecommand = idescsi_queue,
  957. .eh_abort_handler = idescsi_eh_abort,
  958. .eh_host_reset_handler = idescsi_eh_reset,
  959. .bios_param = idescsi_bios,
  960. .can_queue = 40,
  961. .this_id = -1,
  962. .sg_tablesize = 256,
  963. .cmd_per_lun = 5,
  964. .max_sectors = 128,
  965. .use_clustering = DISABLE_CLUSTERING,
  966. .emulated = 1,
  967. .proc_name = "ide-scsi",
  968. };
  969. static int ide_scsi_probe(ide_drive_t *drive)
  970. {
  971. idescsi_scsi_t *idescsi;
  972. struct Scsi_Host *host;
  973. struct gendisk *g;
  974. static int warned;
  975. int err = -ENOMEM;
  976. if (!warned && drive->media == ide_cdrom) {
  977. printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
  978. warned = 1;
  979. }
  980. if (idescsi_nocd && drive->media == ide_cdrom)
  981. return -ENODEV;
  982. if (!strstr("ide-scsi", drive->driver_req) ||
  983. !drive->present ||
  984. drive->media == ide_disk ||
  985. !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
  986. return -ENODEV;
  987. g = alloc_disk(1 << PARTN_BITS);
  988. if (!g)
  989. goto out_host_put;
  990. ide_init_disk(g, drive);
  991. host->max_id = 1;
  992. #if IDESCSI_DEBUG_LOG
  993. if (drive->id->last_lun)
  994. printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
  995. #endif
  996. if ((drive->id->last_lun & 0x7) != 7)
  997. host->max_lun = (drive->id->last_lun & 0x7) + 1;
  998. else
  999. host->max_lun = 1;
  1000. drive->driver_data = host;
  1001. idescsi = scsihost_to_idescsi(host);
  1002. idescsi->drive = drive;
  1003. idescsi->driver = &idescsi_driver;
  1004. idescsi->host = host;
  1005. idescsi->disk = g;
  1006. g->private_data = &idescsi->driver;
  1007. ide_register_subdriver(drive, &idescsi_driver);
  1008. err = 0;
  1009. idescsi_setup(drive, idescsi);
  1010. g->fops = &idescsi_ops;
  1011. ide_register_region(g);
  1012. err = scsi_add_host(host, &drive->gendev);
  1013. if (!err) {
  1014. scsi_scan_host(host);
  1015. return 0;
  1016. }
  1017. /* fall through on error */
  1018. ide_unregister_region(g);
  1019. ide_unregister_subdriver(drive, &idescsi_driver);
  1020. put_disk(g);
  1021. out_host_put:
  1022. scsi_host_put(host);
  1023. return err;
  1024. }
  1025. static int __init init_idescsi_module(void)
  1026. {
  1027. return driver_register(&idescsi_driver.gen_driver);
  1028. }
  1029. static void __exit exit_idescsi_module(void)
  1030. {
  1031. driver_unregister(&idescsi_driver.gen_driver);
  1032. }
  1033. module_param(idescsi_nocd, int, 0600);
  1034. MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
  1035. module_init(init_idescsi_module);
  1036. module_exit(exit_idescsi_module);
  1037. MODULE_LICENSE("GPL");