ide-scsi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:26k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  */
  30. #define IDESCSI_VERSION "0.9"
  31. #include <linux/module.h>
  32. #include <linux/types.h>
  33. #include <linux/string.h>
  34. #include <linux/kernel.h>
  35. #include <linux/mm.h>
  36. #include <linux/ioport.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/errno.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/slab.h>
  41. #include <linux/ide.h>
  42. #include <asm/io.h>
  43. #include <asm/bitops.h>
  44. #include <asm/uaccess.h>
  45. #include "scsi.h"
  46. #include "hosts.h"
  47. #include "sd.h"
  48. #include "ide-scsi.h"
  49. #include <scsi/sg.h>
  50. #define IDESCSI_DEBUG_LOG 0
  51. typedef struct idescsi_pc_s {
  52. u8 c[12]; /* Actual packet bytes */
  53. int request_transfer; /* Bytes to transfer */
  54. int actually_transferred; /* Bytes actually transferred */
  55. int buffer_size; /* Size of our data buffer */
  56. struct request *rq; /* The corresponding request */
  57. byte *buffer; /* Data buffer */
  58. byte *current_position; /* Pointer into the above buffer */
  59. struct scatterlist *sg; /* Scatter gather table */
  60. int b_count; /* Bytes transferred from current entry */
  61. Scsi_Cmnd *scsi_cmd; /* SCSI command */
  62. void (*done)(Scsi_Cmnd *); /* Scsi completion routine */
  63. unsigned long flags; /* Status/Action flags */
  64. unsigned long timeout; /* Command timeout */
  65. } idescsi_pc_t;
  66. /*
  67.  * Packet command status bits.
  68.  */
  69. #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
  70. #define PC_WRITING 1 /* Data direction */
  71. #define PC_TRANSFORM 2 /* transform SCSI commands */
  72. /*
  73.  * SCSI command transformation layer
  74.  */
  75. #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
  76. #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
  77. /*
  78.  * Log flags
  79.  */
  80. #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
  81. typedef struct {
  82. ide_drive_t *drive;
  83. idescsi_pc_t *pc; /* Current packet command */
  84. unsigned long flags; /* Status/Action flags */
  85. unsigned long transform; /* SCSI cmd translation layer */
  86. unsigned long log; /* log flags */
  87. } idescsi_scsi_t;
  88. /*
  89.  * Per ATAPI device status bits.
  90.  */
  91. #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
  92. /*
  93.  * ide-scsi requests.
  94.  */
  95. #define IDESCSI_PC_RQ 90
  96. /*
  97.  * Bits of the interrupt reason register.
  98.  */
  99. #define IDESCSI_IREASON_COD 0x1 /* Information transferred is command */
  100. #define IDESCSI_IREASON_IO 0x2 /* The device requests us to read */
  101. static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
  102. {
  103. while (bcount--)
  104. IN_BYTE (IDE_DATA_REG);
  105. }
  106. static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
  107. {
  108. while (bcount--)
  109. OUT_BYTE (0, IDE_DATA_REG);
  110. }
  111. /*
  112.  * PIO data transfer routines using the scatter gather table.
  113.  */
  114. static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
  115. {
  116. int count;
  117. while (bcount) {
  118. if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
  119. printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding datan");
  120. idescsi_discard_data (drive, bcount);
  121. return;
  122. }
  123. count = IDE_MIN (pc->sg->length - pc->b_count, bcount);
  124. atapi_input_bytes (drive, pc->sg->address + pc->b_count, count);
  125. bcount -= count; pc->b_count += count;
  126. if (pc->b_count == pc->sg->length) {
  127. pc->sg++;
  128. pc->b_count = 0;
  129. }
  130. }
  131. }
  132. static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
  133. {
  134. int count;
  135. while (bcount) {
  136. if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
  137. printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zerosn");
  138. idescsi_output_zeros (drive, bcount);
  139. return;
  140. }
  141. count = IDE_MIN (pc->sg->length - pc->b_count, bcount);
  142. atapi_output_bytes (drive, pc->sg->address + pc->b_count, count);
  143. bcount -= count; pc->b_count += count;
  144. if (pc->b_count == pc->sg->length) {
  145. pc->sg++;
  146. pc->b_count = 0;
  147. }
  148. }
  149. }
  150. /*
  151.  * Most of the SCSI commands are supported directly by ATAPI devices.
  152.  * idescsi_transform_pc handles the few exceptions.
  153.  */
  154. static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
  155. {
  156. u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
  157. char *atapi_buf;
  158. if (!test_bit(PC_TRANSFORM, &pc->flags))
  159. return;
  160. if (drive->media == ide_cdrom || drive->media == ide_optical) {
  161. if (c[0] == READ_6 || c[0] == WRITE_6) {
  162. c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
  163. c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
  164. c[0] += (READ_10 - READ_6);
  165. }
  166. if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
  167. if (!scsi_buf)
  168. return;
  169. if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
  170. return;
  171. memset(atapi_buf, 0, pc->buffer_size + 4);
  172. memset (c, 0, 12);
  173. c[0] = sc[0] | 0x40; c[1] = sc[1]; c[2] = sc[2];
  174. c[8] = sc[4] + 4; c[9] = sc[5];
  175. if (sc[4] + 4 > 255)
  176. c[7] = sc[4] + 4 - 255;
  177. if (c[0] == MODE_SELECT_10) {
  178. atapi_buf[1] = scsi_buf[0]; /* Mode data length */
  179. atapi_buf[2] = scsi_buf[1]; /* Medium type */
  180. atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
  181. atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
  182. memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
  183. }
  184. pc->buffer = atapi_buf;
  185. pc->request_transfer += 4;
  186. pc->buffer_size += 4;
  187. }
  188. }
  189. }
  190. static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
  191. {
  192. u8 *atapi_buf = pc->buffer;
  193. u8 *sc = pc->scsi_cmd->cmnd;
  194. u8 *scsi_buf = pc->scsi_cmd->request_buffer;
  195. if (!test_bit(PC_TRANSFORM, &pc->flags))
  196. return;
  197. if (drive->media == ide_cdrom || drive->media == ide_optical) {
  198. if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
  199. scsi_buf[0] = atapi_buf[1]; /* Mode data length */
  200. scsi_buf[1] = atapi_buf[2]; /* Medium type */
  201. scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
  202. scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
  203. memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
  204. }
  205. if (pc->c[0] == INQUIRY) {
  206. scsi_buf[2] |= 2; /* ansi_revision */
  207. scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
  208. }
  209. }
  210. if (atapi_buf && atapi_buf != scsi_buf)
  211. kfree(atapi_buf);
  212. }
  213. static inline void idescsi_free_bh (struct buffer_head *bh)
  214. {
  215. struct buffer_head *bhp;
  216. while (bh) {
  217. bhp = bh;
  218. bh = bh->b_reqnext;
  219. kfree (bhp);
  220. }
  221. }
  222. static void hexdump(u8 *x, int len)
  223. {
  224. int i;
  225. printk("[ ");
  226. for (i = 0; i < len; i++)
  227. printk("%x ", x[i]);
  228. printk("]n");
  229. }
  230. static void idescsi_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
  231. {
  232. ide_drive_t *drive = hwgroup->drive;
  233. idescsi_scsi_t *scsi = drive->driver_data;
  234. struct request *rq = hwgroup->rq;
  235. idescsi_pc_t *pc = (idescsi_pc_t *) rq->buffer;
  236. int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
  237. u8 *scsi_buf;
  238. unsigned long flags;
  239. if (rq->cmd != IDESCSI_PC_RQ) {
  240. ide_end_request (uptodate, hwgroup);
  241. return;
  242. }
  243. ide_end_drive_cmd (drive, 0, 0);
  244. if (rq->errors >= ERROR_MAX) {
  245. pc->scsi_cmd->result = DID_ERROR << 16;
  246. if (log)
  247. printk ("ide-scsi: %s: I/O error for %lun", drive->name, pc->scsi_cmd->serial_number);
  248. } else if (rq->errors) {
  249. pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
  250. if (log)
  251. printk ("ide-scsi: %s: check condition for %lun", drive->name, pc->scsi_cmd->serial_number);
  252. } else {
  253. pc->scsi_cmd->result = DID_OK << 16;
  254. idescsi_transform_pc2 (drive, pc);
  255. if (log) {
  256. printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
  257. if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
  258. printk(", rst = ");
  259. scsi_buf = pc->scsi_cmd->request_buffer;
  260. hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen));
  261. } else printk("n");
  262. }
  263. }
  264. spin_lock_irqsave(&io_request_lock,flags);
  265. pc->done(pc->scsi_cmd);
  266. spin_unlock_irqrestore(&io_request_lock,flags);
  267. idescsi_free_bh (rq->bh);
  268. kfree(pc); kfree(rq);
  269. scsi->pc = NULL;
  270. }
  271. static inline unsigned long get_timeout(idescsi_pc_t *pc)
  272. {
  273. return IDE_MAX(WAIT_CMD, pc->timeout - jiffies);
  274. }
  275. /*
  276.  * Our interrupt handler.
  277.  */
  278. static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
  279. {
  280. idescsi_scsi_t *scsi = drive->driver_data;
  281. byte status, ireason;
  282. int bcount;
  283. idescsi_pc_t *pc=scsi->pc;
  284. struct request *rq = pc->rq;
  285. unsigned int temp;
  286. #if IDESCSI_DEBUG_LOG
  287. printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handlern");
  288. #endif /* IDESCSI_DEBUG_LOG */
  289. if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
  290. #if IDESCSI_DEBUG_LOG
  291. printk ("ide-scsi: %s: DMA completen", drive->name);
  292. #endif /* IDESCSI_DEBUG_LOG */
  293. pc->actually_transferred=pc->request_transfer;
  294. (void) (HWIF(drive)->dmaproc(ide_dma_end, drive));
  295. }
  296. status = GET_STAT(); /* Clear the interrupt */
  297. if ((status & DRQ_STAT) == 0) { /* No more interrupts */
  298. if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
  299. printk (KERN_INFO "Packet command completed, %d bytes transferredn", pc->actually_transferred);
  300. ide__sti();
  301. if (status & ERR_STAT)
  302. rq->errors++;
  303. idescsi_end_request (1, HWGROUP(drive));
  304. return ide_stopped;
  305. }
  306. bcount = IN_BYTE (IDE_BCOUNTH_REG) << 8 | IN_BYTE (IDE_BCOUNTL_REG);
  307. ireason = IN_BYTE (IDE_IREASON_REG);
  308. if (ireason & IDESCSI_IREASON_COD) {
  309. printk (KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intrn");
  310. return ide_do_reset (drive);
  311. }
  312. if (ireason & IDESCSI_IREASON_IO) {
  313. temp = pc->actually_transferred + bcount;
  314. if ( temp > pc->request_transfer) {
  315. if (temp > pc->buffer_size) {
  316. printk (KERN_ERR "ide-scsi: The scsi wants to send us more data than expected - discarding datan");
  317. temp = pc->buffer_size - pc->actually_transferred;
  318. if (temp) {
  319. clear_bit(PC_WRITING, &pc->flags);
  320. if (pc->sg)
  321. idescsi_input_buffers(drive, pc, temp);
  322. else
  323. atapi_input_bytes(drive, pc->current_position, temp);
  324. printk(KERN_ERR "ide-scsi: transferred %d of %d bytesn", temp, bcount);
  325. }
  326. pc->actually_transferred += temp;
  327. pc->current_position += temp;
  328. idescsi_discard_data (drive,bcount - temp);
  329. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
  330. return ide_started;
  331. }
  332. #if IDESCSI_DEBUG_LOG
  333. printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfern");
  334. #endif /* IDESCSI_DEBUG_LOG */
  335. }
  336. }
  337. if (ireason & IDESCSI_IREASON_IO) {
  338. clear_bit(PC_WRITING, &pc->flags);
  339. if (pc->sg)
  340. idescsi_input_buffers (drive, pc, bcount);
  341. else
  342. atapi_input_bytes (drive,pc->current_position,bcount);
  343. } else {
  344. set_bit(PC_WRITING, &pc->flags);
  345. if (pc->sg)
  346. idescsi_output_buffers (drive, pc, bcount);
  347. else
  348. atapi_output_bytes (drive,pc->current_position,bcount);
  349. }
  350. pc->actually_transferred+=bcount; /* Update the current position */
  351. pc->current_position+=bcount;
  352. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* And set the interrupt handler again */
  353. return ide_started;
  354. }
  355. static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive)
  356. {
  357. idescsi_scsi_t *scsi = drive->driver_data;
  358. idescsi_pc_t *pc = scsi->pc;
  359. byte ireason;
  360. ide_startstop_t startstop;
  361. if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  362. printk (KERN_ERR "ide-scsi: Strange, packet command initiated yet DRQ isn't assertedn");
  363. return startstop;
  364. }
  365. ireason = IN_BYTE (IDE_IREASON_REG);
  366. if ((ireason & (IDESCSI_IREASON_IO | IDESCSI_IREASON_COD)) != IDESCSI_IREASON_COD) {
  367. printk (KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while issuing a packet commandn");
  368. return ide_do_reset (drive);
  369. }
  370. ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* Set the interrupt routine */
  371. atapi_output_bytes (drive, scsi->pc->c, 12); /* Send the actual packet */
  372. return ide_started;
  373. }
  374. /*
  375.  * Issue a packet command
  376.  */
  377. static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
  378. {
  379. idescsi_scsi_t *scsi = drive->driver_data;
  380. int bcount;
  381. struct request *rq = pc->rq;
  382. int dma_ok = 0;
  383. scsi->pc=pc; /* Set the current packet command */
  384. pc->actually_transferred=0; /* We haven't transferred any data yet */
  385. pc->current_position=pc->buffer;
  386. bcount = IDE_MIN (pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
  387. if (drive->using_dma && rq->bh)
  388. dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
  389. SELECT_DRIVE(HWIF(drive), drive);
  390. if (IDE_CONTROL_REG)
  391. OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
  392. OUT_BYTE (dma_ok,IDE_FEATURE_REG);
  393. OUT_BYTE (bcount >> 8,IDE_BCOUNTH_REG);
  394. OUT_BYTE (bcount & 0xff,IDE_BCOUNTL_REG);
  395. if (dma_ok) {
  396. set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  397. (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
  398. }
  399. if (test_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
  400. ide_set_handler (drive, &idescsi_transfer_pc, get_timeout(pc), NULL);
  401. OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* Issue the packet command */
  402. return ide_started;
  403. } else {
  404. OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
  405. return idescsi_transfer_pc (drive);
  406. }
  407. }
  408. /*
  409.  * idescsi_do_request is our request handling function.
  410.  */
  411. static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
  412. {
  413. #if IDESCSI_DEBUG_LOG
  414. printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %dn",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
  415. printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ldn",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
  416. #endif /* IDESCSI_DEBUG_LOG */
  417. if (rq->cmd == IDESCSI_PC_RQ) {
  418. return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->buffer);
  419. }
  420. printk (KERN_ERR "ide-scsi: %s: unsupported command in request queue (%x)n", drive->name, rq->cmd);
  421. idescsi_end_request (0,HWGROUP (drive));
  422. return ide_stopped;
  423. }
  424. static int idescsi_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
  425. {
  426. MOD_INC_USE_COUNT;
  427. return 0;
  428. }
  429. static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
  430. {
  431. MOD_DEC_USE_COUNT;
  432. }
  433. static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
  434. static int idescsi_initialized = 0;
  435. static void idescsi_add_settings(ide_drive_t *drive)
  436. {
  437. idescsi_scsi_t *scsi = drive->driver_data;
  438. /*
  439.  * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
  440.  */
  441. ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
  442. ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
  443. ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
  444. ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
  445. ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
  446. }
  447. /*
  448.  * Driver initialization.
  449.  */
  450. static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id)
  451. {
  452. DRIVER(drive)->busy++;
  453. idescsi_drives[id] = drive;
  454. drive->driver_data = scsi;
  455. drive->ready_stat = 0;
  456. memset (scsi, 0, sizeof (idescsi_scsi_t));
  457. scsi->drive = drive;
  458. if (drive->id && (drive->id->config & 0x0060) == 0x20)
  459. set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
  460. set_bit(IDESCSI_TRANSFORM, &scsi->transform);
  461. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  462. #if IDESCSI_DEBUG_LOG
  463. set_bit(IDESCSI_LOG_CMD, &scsi->log);
  464. #endif /* IDESCSI_DEBUG_LOG */
  465. idescsi_add_settings(drive);
  466. }
  467. static int idescsi_cleanup (ide_drive_t *drive)
  468. {
  469. idescsi_scsi_t *scsi = drive->driver_data;
  470. if (ide_unregister_subdriver (drive))
  471. return 1;
  472. drive->driver_data = NULL;
  473. kfree (scsi);
  474. return 0;
  475. }
  476. int idescsi_reinit(ide_drive_t *drive);
  477. /*
  478.  * IDE subdriver functions, registered with ide.c
  479.  */
  480. static ide_driver_t idescsi_driver = {
  481. name: "ide-scsi",
  482. version: IDESCSI_VERSION,
  483. media: ide_scsi,
  484. busy: 0,
  485. supports_dma: 1,
  486. supports_dsc_overlap: 0,
  487. cleanup: idescsi_cleanup,
  488. standby: NULL,
  489. flushcache: NULL,
  490. do_request: idescsi_do_request,
  491. end_request: idescsi_end_request,
  492. ioctl: NULL,
  493. open: idescsi_open,
  494. release: idescsi_ide_release,
  495. media_change: NULL,
  496. revalidate: NULL,
  497. pre_reset: NULL,
  498. capacity: NULL,
  499. special: NULL,
  500. proc: NULL,
  501. reinit: idescsi_reinit,
  502. ata_prebuilder: NULL,
  503. atapi_prebuilder: NULL,
  504. };
  505. int idescsi_init (void);
  506. static ide_module_t idescsi_module = {
  507. IDE_DRIVER_MODULE,
  508. idescsi_init,
  509. &idescsi_driver,
  510. NULL
  511. };
  512. int idescsi_reinit (ide_drive_t *drive)
  513. {
  514. #if 0
  515. idescsi_scsi_t *scsi;
  516. byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
  517. int i, failed, id;
  518. if (!idescsi_initialized)
  519. return 0;
  520. for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
  521. idescsi_drives[i] = NULL;
  522. MOD_INC_USE_COUNT;
  523. for (i = 0; media[i] != 255; i++) {
  524. failed = 0;
  525. while ((drive = ide_scan_devices (media[i], idescsi_driver.name, NULL, failed++)) != NULL) {
  526. if ((scsi = (idescsi_scsi_t *) kmalloc (sizeof (idescsi_scsi_t), GFP_KERNEL)) == NULL) {
  527. printk (KERN_ERR "ide-scsi: %s: Can't allocate a scsi structuren", drive->name);
  528. continue;
  529. }
  530. if (ide_register_subdriver (drive, &idescsi_driver, IDE_SUBDRIVER_VERSION)) {
  531. printk (KERN_ERR "ide-scsi: %s: Failed to register the driver with ide.cn", drive->name);
  532. kfree (scsi);
  533. continue;
  534. }
  535. for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++);
  536. idescsi_setup (drive, scsi, id);
  537. failed--;
  538. }
  539. }
  540. ide_register_module(&idescsi_module);
  541. MOD_DEC_USE_COUNT;
  542. #endif
  543. return 0;
  544. }
  545. /*
  546.  * idescsi_init will register the driver for each scsi.
  547.  */
  548. int idescsi_init (void)
  549. {
  550. ide_drive_t *drive;
  551. idescsi_scsi_t *scsi;
  552. byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
  553. int i, failed, id;
  554. if (idescsi_initialized)
  555. return 0;
  556. idescsi_initialized = 1;
  557. for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
  558. idescsi_drives[i] = NULL;
  559. MOD_INC_USE_COUNT;
  560. for (i = 0; media[i] != 255; i++) {
  561. failed = 0;
  562. while ((drive = ide_scan_devices (media[i], idescsi_driver.name, NULL, failed++)) != NULL) {
  563. if ((scsi = (idescsi_scsi_t *) kmalloc (sizeof (idescsi_scsi_t), GFP_KERNEL)) == NULL) {
  564. printk (KERN_ERR "ide-scsi: %s: Can't allocate a scsi structuren", drive->name);
  565. continue;
  566. }
  567. if (ide_register_subdriver (drive, &idescsi_driver, IDE_SUBDRIVER_VERSION)) {
  568. printk (KERN_ERR "ide-scsi: %s: Failed to register the driver with ide.cn", drive->name);
  569. kfree (scsi);
  570. continue;
  571. }
  572. for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++);
  573. idescsi_setup (drive, scsi, id);
  574. failed--;
  575. }
  576. }
  577. ide_register_module(&idescsi_module);
  578. MOD_DEC_USE_COUNT;
  579. return 0;
  580. }
  581. int idescsi_detect (Scsi_Host_Template *host_template)
  582. {
  583. struct Scsi_Host *host;
  584. int id;
  585. int last_lun = 0;
  586. host_template->proc_name = "ide-scsi";
  587. host = scsi_register(host_template, 0);
  588. if(host == NULL)
  589. return 0;
  590. for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
  591. last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
  592. host->max_id = id;
  593. host->max_lun = last_lun + 1;
  594. host->can_queue = host->cmd_per_lun * id;
  595. return 1;
  596. }
  597. int idescsi_release (struct Scsi_Host *host)
  598. {
  599. ide_drive_t *drive;
  600. int id;
  601. for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) {
  602. drive = idescsi_drives[id];
  603. if (drive)
  604. DRIVER(drive)->busy--;
  605. }
  606. return 0;
  607. }
  608. const char *idescsi_info (struct Scsi_Host *host)
  609. {
  610. return "SCSI host adapter emulation for IDE ATAPI devices";
  611. }
  612. int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
  613. {
  614. ide_drive_t *drive = idescsi_drives[dev->id];
  615. idescsi_scsi_t *scsi = drive->driver_data;
  616. if (cmd == SG_SET_TRANSFORM) {
  617. if (arg)
  618. set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  619. else
  620. clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  621. return 0;
  622. } else if (cmd == SG_GET_TRANSFORM)
  623. return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
  624. return -EINVAL;
  625. }
  626. static inline struct buffer_head *idescsi_kmalloc_bh (int count)
  627. {
  628. struct buffer_head *bh, *bhp, *first_bh;
  629. if ((first_bh = bhp = bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
  630. goto abort;
  631. memset (bh, 0, sizeof (struct buffer_head));
  632. bh->b_reqnext = NULL;
  633. while (--count) {
  634. if ((bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
  635. goto abort;
  636. memset (bh, 0, sizeof (struct buffer_head));
  637. bhp->b_reqnext = bh;
  638. bhp = bh;
  639. bh->b_reqnext = NULL;
  640. }
  641. return first_bh;
  642. abort:
  643. idescsi_free_bh (first_bh);
  644. return NULL;
  645. }
  646. static inline int idescsi_set_direction (idescsi_pc_t *pc)
  647. {
  648. switch (pc->c[0]) {
  649. case READ_6: case READ_10: case READ_12:
  650. clear_bit (PC_WRITING, &pc->flags);
  651. return 0;
  652. case WRITE_6: case WRITE_10: case WRITE_12:
  653. set_bit (PC_WRITING, &pc->flags);
  654. return 0;
  655. default:
  656. return 1;
  657. }
  658. }
  659. static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc)
  660. {
  661. struct buffer_head *bh = NULL, *first_bh = NULL;
  662. int segments = pc->scsi_cmd->use_sg;
  663. struct scatterlist *sg = pc->scsi_cmd->request_buffer;
  664. if (!drive->using_dma || !pc->request_transfer || pc->request_transfer & 1023)
  665. return NULL;
  666. if (idescsi_set_direction(pc))
  667. return NULL;
  668. if (segments) {
  669. if ((first_bh = bh = idescsi_kmalloc_bh (segments)) == NULL)
  670. return NULL;
  671. #if IDESCSI_DEBUG_LOG
  672. printk ("ide-scsi: %s: building DMA table, %d segments, %dkB totaln", drive->name, segments, pc->request_transfer >> 10);
  673. #endif /* IDESCSI_DEBUG_LOG */
  674. while (segments--) {
  675. if (sg->address) {
  676. bh->b_page = virt_to_page(sg->address);
  677. bh->b_data = (char *) ((unsigned long) sg->address & ~PAGE_MASK);
  678. } else if (sg->page) {
  679. bh->b_page = sg->page;
  680. bh->b_data = (char *) sg->offset;
  681. }
  682. bh->b_size = sg->length;
  683. bh = bh->b_reqnext;
  684. sg++;
  685. }
  686. } else {
  687. /*
  688.  * non-sg requests are guarenteed not to reside in highmem /jens
  689.  */
  690. if ((first_bh = bh = idescsi_kmalloc_bh (1)) == NULL)
  691. return NULL;
  692. #if IDESCSI_DEBUG_LOG
  693. printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)n", drive->name, pc->request_transfer >> 10);
  694. #endif /* IDESCSI_DEBUG_LOG */
  695. bh->b_data = pc->scsi_cmd->request_buffer;
  696. bh->b_size = pc->request_transfer;
  697. }
  698. return first_bh;
  699. }
  700. static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
  701. {
  702. idescsi_scsi_t *scsi = drive->driver_data;
  703. if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
  704. return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
  705. return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
  706. }
  707. int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
  708. {
  709. ide_drive_t *drive = idescsi_drives[cmd->target];
  710. idescsi_scsi_t *scsi;
  711. struct request *rq = NULL;
  712. idescsi_pc_t *pc = NULL;
  713. if (!drive) {
  714. printk (KERN_ERR "ide-scsi: drive id %d not presentn", cmd->target);
  715. goto abort;
  716. }
  717. scsi = drive->driver_data;
  718. pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
  719. rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
  720. if (rq == NULL || pc == NULL) {
  721. printk (KERN_ERR "ide-scsi: %s: out of memoryn", drive->name);
  722. goto abort;
  723. }
  724. memset (pc->c, 0, 12);
  725. pc->flags = 0;
  726. pc->rq = rq;
  727. memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
  728. if (cmd->use_sg) {
  729. pc->buffer = NULL;
  730. pc->sg = cmd->request_buffer;
  731. } else {
  732. pc->buffer = cmd->request_buffer;
  733. pc->sg = NULL;
  734. }
  735. pc->b_count = 0;
  736. pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
  737. pc->scsi_cmd = cmd;
  738. pc->done = done;
  739. pc->timeout = jiffies + cmd->timeout_per_command;
  740. if (should_transform(drive, cmd))
  741. set_bit(PC_TRANSFORM, &pc->flags);
  742. idescsi_transform_pc1 (drive, pc);
  743. if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
  744. printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
  745. hexdump(cmd->cmnd, cmd->cmd_len);
  746. if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
  747. printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
  748. hexdump(pc->c, 12);
  749. }
  750. }
  751. ide_init_drive_cmd (rq);
  752. rq->buffer = (char *) pc;
  753. rq->bh = idescsi_dma_bh (drive, pc);
  754. rq->cmd = IDESCSI_PC_RQ;
  755. spin_unlock_irq(&io_request_lock);
  756. (void) ide_do_drive_cmd (drive, rq, ide_end);
  757. spin_lock_irq(&io_request_lock);
  758. return 0;
  759. abort:
  760. if (pc) kfree (pc);
  761. if (rq) kfree (rq);
  762. cmd->result = DID_ERROR << 16;
  763. done(cmd);
  764. return 0;
  765. }
  766. int idescsi_abort (Scsi_Cmnd *cmd)
  767. {
  768. return SCSI_ABORT_SNOOZE;
  769. }
  770. int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags)
  771. {
  772. return SCSI_RESET_SUCCESS;
  773. }
  774. int idescsi_bios (Disk *disk, kdev_t dev, int *parm)
  775. {
  776. ide_drive_t *drive = idescsi_drives[disk->device->id];
  777. if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
  778. parm[0] = drive->bios_head;
  779. parm[1] = drive->bios_sect;
  780. parm[2] = drive->bios_cyl;
  781. }
  782. return 0;
  783. }
  784. static Scsi_Host_Template idescsi_template = IDESCSI;
  785. static int __init init_idescsi_module(void)
  786. {
  787. idescsi_init();
  788. idescsi_template.module = THIS_MODULE;
  789. scsi_register_module (MODULE_SCSI_HA, &idescsi_template);
  790. return 0;
  791. }
  792. static void __exit exit_idescsi_module(void)
  793. {
  794. ide_drive_t *drive;
  795. byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
  796. int i, failed;
  797. scsi_unregister_module (MODULE_SCSI_HA, &idescsi_template);
  798. for (i = 0; media[i] != 255; i++) {
  799. failed = 0;
  800. while ((drive = ide_scan_devices (media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
  801. if (idescsi_cleanup (drive)) {
  802. printk ("%s: exit_idescsi_module() called while still busyn", drive->name);
  803. failed++;
  804. }
  805. }
  806. ide_unregister_module(&idescsi_module);
  807. }
  808. module_init(init_idescsi_module);
  809. module_exit(exit_idescsi_module);
  810. MODULE_LICENSE("GPL");