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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Changes:
  3.  * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
  4.  * - get rid of some verify_areas and use __copy*user and __get/put_user
  5.  *   for the ones that remain
  6.  */
  7. #define __NO_VERSION__
  8. #include <linux/module.h>
  9. #include <asm/io.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/system.h>
  12. #include <asm/page.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/blk.h>
  20. #include "scsi.h"
  21. #include "hosts.h"
  22. #include <scsi/scsi_ioctl.h>
  23. #define NORMAL_RETRIES 5
  24. #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
  25. #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
  26. #define START_STOP_TIMEOUT (60 * HZ)
  27. #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
  28. #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
  29. #define READ_DEFECT_DATA_TIMEOUT (60 * HZ )  /* ZIP-250 on parallel port takes as long! */
  30. #define MAX_BUF PAGE_SIZE
  31. /*
  32.  * If we are told to probe a host, we will return 0 if  the host is not
  33.  * present, 1 if the host is present, and will return an identifying
  34.  * string at *arg, if arg is non null, filling to the length stored at
  35.  * (int *) arg
  36.  */
  37. static int ioctl_probe(struct Scsi_Host *host, void *buffer)
  38. {
  39. unsigned int len, slen;
  40. const char *string;
  41. int temp = host->hostt->present;
  42. if (temp && buffer) {
  43. if (get_user(len, (unsigned int *) buffer))
  44. return -EFAULT;
  45. if (host->hostt->info)
  46. string = host->hostt->info(host);
  47. else
  48. string = host->hostt->name;
  49. if (string) {
  50. slen = strlen(string);
  51. if (len > slen)
  52. len = slen + 1;
  53. if (copy_to_user(buffer, string, len))
  54. return -EFAULT;
  55. }
  56. }
  57. return temp;
  58. }
  59. /*
  60.  * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  61.  * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES  variables are used.  
  62.  * 
  63.  * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  64.  * input data, if any, not including the command string & counts, 
  65.  * *((int *)arg + 1) is the output buffer size in bytes.
  66.  * 
  67.  * *(char *) ((int *) arg)[2] the actual command byte.   
  68.  * 
  69.  * Note that if more than MAX_BUF bytes are requested to be transferred,
  70.  * the ioctl will fail with error EINVAL.  MAX_BUF can be increased in
  71.  * the future by increasing the size that scsi_malloc will accept.
  72.  * 
  73.  * This size *does not* include the initial lengths that were passed.
  74.  * 
  75.  * The SCSI command is read from the memory location immediately after the
  76.  * length words, and the input data is right after the command.  The SCSI
  77.  * routines know the command size based on the opcode decode.  
  78.  * 
  79.  * The output area is then filled in starting from the command byte. 
  80.  */
  81. static int ioctl_internal_command(Scsi_Device * dev, char *cmd,
  82.   int timeout, int retries)
  83. {
  84. int result;
  85. Scsi_Request *SRpnt;
  86. Scsi_Device *SDpnt;
  87. SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %dn", cmd[0]));
  88. if (NULL == (SRpnt = scsi_allocate_request(dev))) {
  89. printk("SCSI internal ioctl failed, no memoryn");
  90. return -ENOMEM;
  91. }
  92. SRpnt->sr_data_direction = SCSI_DATA_NONE;
  93.         scsi_wait_req(SRpnt, cmd, NULL, 0, timeout, retries);
  94. SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%xn", SRpnt->sr_result));
  95. if (driver_byte(SRpnt->sr_result) != 0)
  96. switch (SRpnt->sr_sense_buffer[2] & 0xf) {
  97. case ILLEGAL_REQUEST:
  98. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  99. dev->lockable = 0;
  100. else
  101. printk("SCSI device (ioctl) reports ILLEGAL REQUEST.n");
  102. break;
  103. case NOT_READY: /* This happens if there is no disc in drive */
  104. if (dev->removable && (cmd[0] != TEST_UNIT_READY)) {
  105. printk(KERN_INFO "Device not ready.  Make sure there is a disc in the drive.n");
  106. break;
  107. }
  108. case UNIT_ATTENTION:
  109. if (dev->removable) {
  110. dev->changed = 1;
  111. SRpnt->sr_result = 0; /* This is no longer considered an error */
  112. /* gag this error, VFS will log it anyway /axboe */
  113. /* printk(KERN_INFO "Disc change detected.n"); */
  114. break;
  115. };
  116. default: /* Fall through for non-removable media */
  117. printk("SCSI error: host %d id %d lun %d return code = %xn",
  118.        dev->host->host_no,
  119.        dev->id,
  120.        dev->lun,
  121.        SRpnt->sr_result);
  122. printk("tSense class %x, sense error %x, extended sense %xn",
  123.        sense_class(SRpnt->sr_sense_buffer[0]),
  124.        sense_error(SRpnt->sr_sense_buffer[0]),
  125.        SRpnt->sr_sense_buffer[2] & 0xf);
  126. };
  127. result = SRpnt->sr_result;
  128. SCSI_LOG_IOCTL(2, printk("IOCTL Releasing commandn"));
  129. SDpnt = SRpnt->sr_device;
  130. scsi_release_request(SRpnt);
  131. SRpnt = NULL;
  132. return result;
  133. }
  134. /*
  135.  * This interface is depreciated - users should use the scsi generic (sg)
  136.  * interface instead, as this is a more flexible approach to performing
  137.  * generic SCSI commands on a device.
  138.  *
  139.  * The structure that we are passed should look like:
  140.  *
  141.  * struct sdata {
  142.  *  unsigned int inlen;      [i] Length of data to be written to device 
  143.  *  unsigned int outlen;     [i] Length of data to be read from device 
  144.  *  unsigned char cmd[x];    [i] SCSI command (6 <= x <= 12).
  145.  *                           [o] Data read from device starts here.
  146.  *                           [o] On error, sense buffer starts here.
  147.  *  unsigned char wdata[y];  [i] Data written to device starts here.
  148.  * };
  149.  * Notes:
  150.  *   -  The SCSI command length is determined by examining the 1st byte
  151.  *      of the given command. There is no way to override this.
  152.  *   -  Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
  153.  *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
  154.  *      accomodate the sense buffer when an error occurs.
  155.  *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  156.  *      old code will not be surprised.
  157.  *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
  158.  *      a negative return and the Unix error code in 'errno'. 
  159.  *      If the SCSI command succeeds then 0 is returned.
  160.  *      Positive numbers returned are the compacted SCSI error codes (4 
  161.  *      bytes in one int) where the lowest byte is the SCSI status.
  162.  *      See the drivers/scsi/scsi.h file for more information on this.
  163.  *
  164.  */
  165. #define OMAX_SB_LEN 16 /* Old sense buffer length */
  166. int scsi_ioctl_send_command(Scsi_Device * dev, Scsi_Ioctl_Command * sic)
  167. {
  168. char *buf;
  169. unsigned char cmd[MAX_COMMAND_SIZE];
  170. char *cmd_in;
  171. Scsi_Request *SRpnt;
  172. Scsi_Device *SDpnt;
  173. unsigned char opcode;
  174. unsigned int inlen, outlen, cmdlen;
  175. unsigned int needed, buf_needed;
  176. int timeout, retries, result;
  177. int data_direction;
  178. if (!sic)
  179. return -EINVAL;
  180. /*
  181.  * Verify that we can read at least this much.
  182.  */
  183. if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
  184. return -EFAULT;
  185. if(__get_user(inlen, &sic->inlen))
  186. return -EFAULT;
  187. if(__get_user(outlen, &sic->outlen))
  188. return -EFAULT;
  189. /*
  190.  * We do not transfer more than MAX_BUF with this interface.
  191.  * If the user needs to transfer more data than this, they
  192.  * should use scsi_generics (sg) instead.
  193.  */
  194. if (inlen > MAX_BUF)
  195. return -EINVAL;
  196. if (outlen > MAX_BUF)
  197. return -EINVAL;
  198. cmd_in = sic->data;
  199. if(get_user(opcode, cmd_in))
  200. return -EFAULT;
  201. needed = buf_needed = (inlen > outlen ? inlen : outlen);
  202. if (buf_needed) {
  203. buf_needed = (buf_needed + 511) & ~511;
  204. if (buf_needed > MAX_BUF)
  205. buf_needed = MAX_BUF;
  206. buf = (char *) scsi_malloc(buf_needed);
  207. if (!buf)
  208. return -ENOMEM;
  209. memset(buf, 0, buf_needed);
  210. if( inlen == 0 ) {
  211. data_direction = SCSI_DATA_READ;
  212. } else if (outlen == 0 ) {
  213. data_direction = SCSI_DATA_WRITE;
  214. } else {
  215. /*
  216.  * Can this ever happen?
  217.  */
  218. data_direction = SCSI_DATA_UNKNOWN;
  219. }
  220. } else {
  221. buf = NULL;
  222. data_direction = SCSI_DATA_NONE;
  223. }
  224. /*
  225.  * Obtain the command from the user's address space.
  226.  */
  227. cmdlen = COMMAND_SIZE(opcode);
  228. result = -EFAULT;
  229. if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
  230. goto error;
  231. if(__copy_from_user(cmd, cmd_in, cmdlen))
  232. goto error;
  233. /*
  234.  * Obtain the data to be sent to the device (if any).
  235.  */
  236. if(copy_from_user(buf, cmd_in + cmdlen, inlen))
  237. goto error;
  238. /*
  239.  * Set the lun field to the correct value.
  240.  */
  241. if (dev->scsi_level <= SCSI_2)
  242. cmd[1] = (cmd[1] & 0x1f) | (dev->lun << 5);
  243. switch (opcode) {
  244. case FORMAT_UNIT:
  245. timeout = FORMAT_UNIT_TIMEOUT;
  246. retries = 1;
  247. break;
  248. case START_STOP:
  249. timeout = START_STOP_TIMEOUT;
  250. retries = NORMAL_RETRIES;
  251. break;
  252. case MOVE_MEDIUM:
  253. timeout = MOVE_MEDIUM_TIMEOUT;
  254. retries = NORMAL_RETRIES;
  255. break;
  256. case READ_ELEMENT_STATUS:
  257. timeout = READ_ELEMENT_STATUS_TIMEOUT;
  258. retries = NORMAL_RETRIES;
  259. break;
  260. case READ_DEFECT_DATA:
  261. timeout = READ_DEFECT_DATA_TIMEOUT;
  262. retries = 1;
  263. break;
  264. default:
  265. timeout = IOCTL_NORMAL_TIMEOUT;
  266. retries = NORMAL_RETRIES;
  267. break;
  268. }
  269. #ifndef DEBUG_NO_CMD
  270. SRpnt = scsi_allocate_request(dev);
  271.         if( SRpnt == NULL )
  272.         {
  273.                 result = -EINTR;
  274.                 goto error;
  275.         }
  276. SRpnt->sr_data_direction = data_direction;
  277.         scsi_wait_req(SRpnt, cmd, buf, needed, timeout, retries);
  278. /* 
  279.  * If there was an error condition, pass the info back to the user. 
  280.  */
  281. result = SRpnt->sr_result;
  282. if (SRpnt->sr_result) {
  283. int sb_len = sizeof(SRpnt->sr_sense_buffer);
  284. sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
  285. if (copy_to_user(cmd_in, SRpnt->sr_sense_buffer, sb_len))
  286. result = -EFAULT;
  287. } else {
  288. if (copy_to_user(cmd_in, buf, outlen))
  289. result = -EFAULT;
  290. }
  291. SDpnt = SRpnt->sr_device;
  292. scsi_release_request(SRpnt);
  293. SRpnt = NULL;
  294. error:
  295. if (buf)
  296. scsi_free(buf, buf_needed);
  297. return result;
  298. #else
  299. {
  300. int i;
  301. printk("scsi_ioctl : device %d.  command = ", dev->id);
  302. for (i = 0; i < cmdlen; ++i)
  303. printk("%02x ", cmd[i]);
  304. printk("nbuffer =");
  305. for (i = 0; i < 20; ++i)
  306. printk("%02x ", buf[i]);
  307. printk("n");
  308. printk("inlen = %d, outlen = %d, cmdlen = %dn",
  309.        inlen, outlen, cmdlen);
  310. printk("buffer = %d, cmd_in = %dn", buffer, cmd_in);
  311. }
  312. return 0;
  313. #endif
  314. }
  315. /*
  316.  * The scsi_ioctl_get_pci() function places into arg the value
  317.  * pci_dev::slot_name (8 characters) for the PCI device (if any).
  318.  * Returns: 0 on success
  319.  *          -ENXIO if there isn't a PCI device pointer
  320.  *                 (could be because the SCSI driver hasn't been
  321.  *                  updated yet, or because it isn't a SCSI
  322.  *                  device)
  323.  *          any copy_to_user() error on failure there
  324.  */
  325. static int
  326. scsi_ioctl_get_pci(Scsi_Device * dev, void *arg)
  327. {
  328.         if (!dev->host->pci_dev)
  329.          return -ENXIO;
  330.         if(copy_to_user(arg, dev->host->pci_dev->slot_name,
  331.                             sizeof(dev->host->pci_dev->slot_name)))
  332. return -EFAULT;
  333. return 0;
  334. }
  335. /*
  336.  * the scsi_ioctl() function differs from most ioctls in that it does
  337.  * not take a major/minor number as the dev field.  Rather, it takes
  338.  * a pointer to a scsi_devices[] element, a structure. 
  339.  */
  340. int scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
  341. {
  342. char scsi_cmd[MAX_COMMAND_SIZE];
  343. char cmd_byte1;
  344. /* No idea how this happens.... */
  345. if (!dev)
  346. return -ENXIO;
  347. /*
  348.  * If we are in the middle of error recovery, don't let anyone
  349.  * else try and use this device.  Also, if error recovery fails, it
  350.  * may try and take the device offline, in which case all further
  351.  * access to the device is prohibited.
  352.  */
  353. if (!scsi_block_when_processing_errors(dev)) {
  354. return -ENODEV;
  355. }
  356. cmd_byte1 = (dev->scsi_level <= SCSI_2) ? (dev->lun << 5) : 0;
  357. switch (cmd) {
  358. case SCSI_IOCTL_GET_IDLUN:
  359. if (verify_area(VERIFY_WRITE, arg, sizeof(Scsi_Idlun)))
  360. return -EFAULT;
  361. __put_user((dev->id & 0xff)
  362.  + ((dev->lun & 0xff) << 8)
  363.  + ((dev->channel & 0xff) << 16)
  364.  + ((dev->host->host_no & 0xff) << 24),
  365.  &((Scsi_Idlun *) arg)->dev_id);
  366. __put_user(dev->host->unique_id, &((Scsi_Idlun *) arg)->host_unique_id);
  367. return 0;
  368. case SCSI_IOCTL_GET_BUS_NUMBER:
  369. return put_user(dev->host->host_no, (int *) arg);
  370. case SCSI_IOCTL_TAGGED_ENABLE:
  371. if (!capable(CAP_SYS_ADMIN))
  372. return -EACCES;
  373. if (!dev->tagged_supported)
  374. return -EINVAL;
  375. dev->tagged_queue = 1;
  376. dev->current_tag = 1;
  377. return 0;
  378. case SCSI_IOCTL_TAGGED_DISABLE:
  379. if (!capable(CAP_SYS_ADMIN))
  380. return -EACCES;
  381. if (!dev->tagged_supported)
  382. return -EINVAL;
  383. dev->tagged_queue = 0;
  384. dev->current_tag = 0;
  385. return 0;
  386. case SCSI_IOCTL_PROBE_HOST:
  387. return ioctl_probe(dev->host, arg);
  388. case SCSI_IOCTL_SEND_COMMAND:
  389. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  390. return -EACCES;
  391. return scsi_ioctl_send_command((Scsi_Device *) dev,
  392.      (Scsi_Ioctl_Command *) arg);
  393. case SCSI_IOCTL_DOORLOCK:
  394. if (!dev->removable || !dev->lockable)
  395. return 0;
  396. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  397. scsi_cmd[1] = cmd_byte1;
  398. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  399. scsi_cmd[4] = SCSI_REMOVAL_PREVENT;
  400. return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
  401.    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  402. break;
  403. case SCSI_IOCTL_DOORUNLOCK:
  404. if (!dev->removable || !dev->lockable)
  405. return 0;
  406. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  407. scsi_cmd[1] = cmd_byte1;
  408. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  409. scsi_cmd[4] = SCSI_REMOVAL_ALLOW;
  410. return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
  411.    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  412. case SCSI_IOCTL_TEST_UNIT_READY:
  413. scsi_cmd[0] = TEST_UNIT_READY;
  414. scsi_cmd[1] = cmd_byte1;
  415. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  416. scsi_cmd[4] = 0;
  417. return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
  418.    IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  419. break;
  420. case SCSI_IOCTL_START_UNIT:
  421. scsi_cmd[0] = START_STOP;
  422. scsi_cmd[1] = cmd_byte1;
  423. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  424. scsi_cmd[4] = 1;
  425. return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
  426.      START_STOP_TIMEOUT, NORMAL_RETRIES);
  427. break;
  428. case SCSI_IOCTL_STOP_UNIT:
  429. scsi_cmd[0] = START_STOP;
  430. scsi_cmd[1] = cmd_byte1;
  431. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  432. scsi_cmd[4] = 0;
  433. return ioctl_internal_command((Scsi_Device *) dev, scsi_cmd,
  434.      START_STOP_TIMEOUT, NORMAL_RETRIES);
  435. break;
  436.         case SCSI_IOCTL_GET_PCI:
  437.                 return scsi_ioctl_get_pci(dev, arg);
  438.                 break;
  439. default:
  440. if (dev->host->hostt->ioctl)
  441. return dev->host->hostt->ioctl(dev, cmd, arg);
  442. return -EINVAL;
  443. }
  444. return -EINVAL;
  445. }
  446. /*
  447.  * Just like scsi_ioctl, only callable from kernel space with no 
  448.  * fs segment fiddling.
  449.  */
  450. int kernel_scsi_ioctl(Scsi_Device * dev, int cmd, void *arg)
  451. {
  452. mm_segment_t oldfs;
  453. int tmp;
  454. oldfs = get_fs();
  455. set_fs(get_ds());
  456. tmp = scsi_ioctl(dev, cmd, arg);
  457. set_fs(oldfs);
  458. return tmp;
  459. }
  460. /*
  461.  * Overrides for Emacs so that we almost follow Linus's tabbing style.
  462.  * Emacs will notice this stuff at the end of the file and automatically
  463.  * adjust the settings for this buffer only.  This must remain at the end
  464.  * of the file.
  465.  * ---------------------------------------------------------------------------
  466.  * Local variables:
  467.  * c-indent-level: 4
  468.  * c-brace-imaginary-offset: 0
  469.  * c-brace-offset: -4
  470.  * c-argdecl-indent: 4
  471.  * c-label-offset: -4
  472.  * c-continued-statement-offset: 4
  473.  * c-continued-brace-offset: 0
  474.  * indent-tabs-mode: nil
  475.  * tab-width: 8
  476.  * End:
  477.  */