scsi_ioctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/module.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/capability.h>
  25. #include <linux/completion.h>
  26. #include <linux/cdrom.h>
  27. #include <linux/ratelimit.h>
  28. #include <linux/slab.h>
  29. #include <linux/times.h>
  30. #include <linux/uio.h>
  31. #include <linux/uaccess.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_ioctl.h>
  34. #include <scsi/scsi_cmnd.h>
  35. /*
  36. * MTK PATCH: Include UFS ioctl code definition.
  37. */
  38. #ifdef CONFIG_MTK_UFS_SUPPORT
  39. #include <scsi/ufs/ufs-mtk-ioctl.h>
  40. #endif
  41. struct blk_cmd_filter {
  42. unsigned long read_ok[BLK_SCSI_CMD_PER_LONG];
  43. unsigned long write_ok[BLK_SCSI_CMD_PER_LONG];
  44. };
  45. static struct blk_cmd_filter blk_default_cmd_filter;
  46. /* Command group 3 is reserved and should never be used. */
  47. const unsigned char scsi_command_size_tbl[8] =
  48. {
  49. 6, 10, 10, 12,
  50. 16, 12, 10, 10
  51. };
  52. EXPORT_SYMBOL(scsi_command_size_tbl);
  53. #include <scsi/sg.h>
  54. static int sg_get_version(int __user *p)
  55. {
  56. static const int sg_version_num = 30527;
  57. return put_user(sg_version_num, p);
  58. }
  59. static int scsi_get_idlun(struct request_queue *q, int __user *p)
  60. {
  61. return put_user(0, p);
  62. }
  63. static int scsi_get_bus(struct request_queue *q, int __user *p)
  64. {
  65. return put_user(0, p);
  66. }
  67. static int sg_get_timeout(struct request_queue *q)
  68. {
  69. return jiffies_to_clock_t(q->sg_timeout);
  70. }
  71. static int sg_set_timeout(struct request_queue *q, int __user *p)
  72. {
  73. int timeout, err = get_user(timeout, p);
  74. if (!err)
  75. q->sg_timeout = clock_t_to_jiffies(timeout);
  76. return err;
  77. }
  78. static int max_sectors_bytes(struct request_queue *q)
  79. {
  80. unsigned int max_sectors = queue_max_sectors(q);
  81. max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
  82. return max_sectors << 9;
  83. }
  84. static int sg_get_reserved_size(struct request_queue *q, int __user *p)
  85. {
  86. int val = min_t(int, q->sg_reserved_size, max_sectors_bytes(q));
  87. return put_user(val, p);
  88. }
  89. static int sg_set_reserved_size(struct request_queue *q, int __user *p)
  90. {
  91. int size, err = get_user(size, p);
  92. if (err)
  93. return err;
  94. if (size < 0)
  95. return -EINVAL;
  96. q->sg_reserved_size = min(size, max_sectors_bytes(q));
  97. return 0;
  98. }
  99. /*
  100. * will always return that we are ATAPI even for a real SCSI drive, I'm not
  101. * so sure this is worth doing anything about (why would you care??)
  102. */
  103. static int sg_emulated_host(struct request_queue *q, int __user *p)
  104. {
  105. return put_user(1, p);
  106. }
  107. static void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
  108. {
  109. /* Basic read-only commands */
  110. __set_bit(TEST_UNIT_READY, filter->read_ok);
  111. __set_bit(REQUEST_SENSE, filter->read_ok);
  112. __set_bit(READ_6, filter->read_ok);
  113. __set_bit(READ_10, filter->read_ok);
  114. __set_bit(READ_12, filter->read_ok);
  115. __set_bit(READ_16, filter->read_ok);
  116. __set_bit(READ_BUFFER, filter->read_ok);
  117. __set_bit(READ_DEFECT_DATA, filter->read_ok);
  118. __set_bit(READ_CAPACITY, filter->read_ok);
  119. __set_bit(READ_LONG, filter->read_ok);
  120. __set_bit(INQUIRY, filter->read_ok);
  121. __set_bit(MODE_SENSE, filter->read_ok);
  122. __set_bit(MODE_SENSE_10, filter->read_ok);
  123. __set_bit(LOG_SENSE, filter->read_ok);
  124. __set_bit(START_STOP, filter->read_ok);
  125. __set_bit(GPCMD_VERIFY_10, filter->read_ok);
  126. __set_bit(VERIFY_16, filter->read_ok);
  127. __set_bit(REPORT_LUNS, filter->read_ok);
  128. __set_bit(SERVICE_ACTION_IN_16, filter->read_ok);
  129. __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
  130. __set_bit(MAINTENANCE_IN, filter->read_ok);
  131. __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
  132. /* Audio CD commands */
  133. __set_bit(GPCMD_PLAY_CD, filter->read_ok);
  134. __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
  135. __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
  136. __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
  137. __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
  138. /* CD/DVD data reading */
  139. __set_bit(GPCMD_READ_CD, filter->read_ok);
  140. __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
  141. __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
  142. __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
  143. __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
  144. __set_bit(GPCMD_READ_HEADER, filter->read_ok);
  145. __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
  146. __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
  147. __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
  148. __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
  149. __set_bit(GPCMD_SCAN, filter->read_ok);
  150. __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
  151. __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
  152. __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
  153. __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
  154. __set_bit(GPCMD_SEEK, filter->read_ok);
  155. __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
  156. /* Basic writing commands */
  157. __set_bit(WRITE_6, filter->write_ok);
  158. __set_bit(WRITE_10, filter->write_ok);
  159. __set_bit(WRITE_VERIFY, filter->write_ok);
  160. __set_bit(WRITE_12, filter->write_ok);
  161. __set_bit(WRITE_VERIFY_12, filter->write_ok);
  162. __set_bit(WRITE_16, filter->write_ok);
  163. __set_bit(WRITE_LONG, filter->write_ok);
  164. __set_bit(WRITE_LONG_2, filter->write_ok);
  165. __set_bit(WRITE_SAME, filter->write_ok);
  166. __set_bit(WRITE_SAME_16, filter->write_ok);
  167. __set_bit(WRITE_SAME_32, filter->write_ok);
  168. __set_bit(ERASE, filter->write_ok);
  169. __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
  170. __set_bit(MODE_SELECT, filter->write_ok);
  171. __set_bit(LOG_SELECT, filter->write_ok);
  172. __set_bit(GPCMD_BLANK, filter->write_ok);
  173. __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
  174. __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
  175. __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
  176. __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
  177. __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
  178. __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
  179. __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
  180. __set_bit(GPCMD_SEND_KEY, filter->write_ok);
  181. __set_bit(GPCMD_SEND_OPC, filter->write_ok);
  182. __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
  183. __set_bit(GPCMD_SET_SPEED, filter->write_ok);
  184. __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
  185. __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
  186. __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
  187. __set_bit(GPCMD_SET_READ_AHEAD, filter->write_ok);
  188. }
  189. int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm)
  190. {
  191. struct blk_cmd_filter *filter = &blk_default_cmd_filter;
  192. /* root can do any command. */
  193. if (capable(CAP_SYS_RAWIO))
  194. return 0;
  195. /* Anybody who can open the device can do a read-safe command */
  196. if (test_bit(cmd[0], filter->read_ok))
  197. return 0;
  198. /* Write-safe commands require a writable open */
  199. if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
  200. return 0;
  201. return -EPERM;
  202. }
  203. EXPORT_SYMBOL(blk_verify_command);
  204. static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
  205. struct sg_io_hdr *hdr, fmode_t mode)
  206. {
  207. struct scsi_request *req = scsi_req(rq);
  208. if (copy_from_user(req->cmd, hdr->cmdp, hdr->cmd_len))
  209. return -EFAULT;
  210. if (blk_verify_command(req->cmd, mode & FMODE_WRITE))
  211. return -EPERM;
  212. /*
  213. * fill in request structure
  214. */
  215. req->cmd_len = hdr->cmd_len;
  216. rq->timeout = msecs_to_jiffies(hdr->timeout);
  217. if (!rq->timeout)
  218. rq->timeout = q->sg_timeout;
  219. if (!rq->timeout)
  220. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  221. if (rq->timeout < BLK_MIN_SG_TIMEOUT)
  222. rq->timeout = BLK_MIN_SG_TIMEOUT;
  223. return 0;
  224. }
  225. static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
  226. struct bio *bio)
  227. {
  228. struct scsi_request *req = scsi_req(rq);
  229. int r, ret = 0;
  230. /*
  231. * fill in all the output members
  232. */
  233. hdr->status = req->result & 0xff;
  234. hdr->masked_status = status_byte(req->result);
  235. hdr->msg_status = msg_byte(req->result);
  236. hdr->host_status = host_byte(req->result);
  237. hdr->driver_status = driver_byte(req->result);
  238. hdr->info = 0;
  239. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  240. hdr->info |= SG_INFO_CHECK;
  241. hdr->resid = req->resid_len;
  242. hdr->sb_len_wr = 0;
  243. if (req->sense_len && hdr->sbp) {
  244. int len = min((unsigned int) hdr->mx_sb_len, req->sense_len);
  245. if (!copy_to_user(hdr->sbp, req->sense, len))
  246. hdr->sb_len_wr = len;
  247. else
  248. ret = -EFAULT;
  249. }
  250. r = blk_rq_unmap_user(bio);
  251. if (!ret)
  252. ret = r;
  253. return ret;
  254. }
  255. static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
  256. struct sg_io_hdr *hdr, fmode_t mode)
  257. {
  258. unsigned long start_time;
  259. ssize_t ret = 0;
  260. int writing = 0;
  261. int at_head = 0;
  262. struct request *rq;
  263. struct scsi_request *req;
  264. struct bio *bio;
  265. if (hdr->interface_id != 'S')
  266. return -EINVAL;
  267. if (hdr->dxfer_len > (queue_max_hw_sectors(q) << 9))
  268. return -EIO;
  269. if (hdr->dxfer_len)
  270. switch (hdr->dxfer_direction) {
  271. default:
  272. return -EINVAL;
  273. case SG_DXFER_TO_DEV:
  274. writing = 1;
  275. break;
  276. case SG_DXFER_TO_FROM_DEV:
  277. case SG_DXFER_FROM_DEV:
  278. break;
  279. }
  280. if (hdr->flags & SG_FLAG_Q_AT_HEAD)
  281. at_head = 1;
  282. ret = -ENOMEM;
  283. rq = blk_get_request(q, writing ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN,
  284. GFP_KERNEL);
  285. if (IS_ERR(rq))
  286. return PTR_ERR(rq);
  287. req = scsi_req(rq);
  288. /* MTK PATCH for SPOH */
  289. #ifdef MTK_UFS_HQA
  290. if (hdr->flags & SG_FLAG_POWER_LOSS)
  291. rq->cmd_flags |= REQ_POWER_LOSS;
  292. #endif
  293. if (hdr->cmd_len > BLK_MAX_CDB) {
  294. req->cmd = kzalloc(hdr->cmd_len, GFP_KERNEL);
  295. if (!req->cmd)
  296. goto out_put_request;
  297. }
  298. ret = blk_fill_sghdr_rq(q, rq, hdr, mode);
  299. if (ret < 0)
  300. goto out_free_cdb;
  301. ret = 0;
  302. if (hdr->iovec_count) {
  303. struct iov_iter i;
  304. struct iovec *iov = NULL;
  305. ret = import_iovec(rq_data_dir(rq),
  306. hdr->dxferp, hdr->iovec_count,
  307. 0, &iov, &i);
  308. if (ret < 0)
  309. goto out_free_cdb;
  310. /* SG_IO howto says that the shorter of the two wins */
  311. iov_iter_truncate(&i, hdr->dxfer_len);
  312. ret = blk_rq_map_user_iov(q, rq, NULL, &i, GFP_KERNEL);
  313. kfree(iov);
  314. } else if (hdr->dxfer_len)
  315. ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
  316. GFP_KERNEL);
  317. if (ret)
  318. goto out_free_cdb;
  319. bio = rq->bio;
  320. req->retries = 0;
  321. start_time = jiffies;
  322. /* ignore return value. All information is passed back to caller
  323. * (if he doesn't check that is his problem).
  324. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  325. */
  326. blk_execute_rq(q, bd_disk, rq, at_head);
  327. hdr->duration = jiffies_to_msecs(jiffies - start_time);
  328. ret = blk_complete_sghdr_rq(rq, hdr, bio);
  329. out_free_cdb:
  330. scsi_req_free_cmd(req);
  331. out_put_request:
  332. blk_put_request(rq);
  333. return ret;
  334. }
  335. /**
  336. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  337. * @file: file this ioctl operates on (optional)
  338. * @q: request queue to send scsi commands down
  339. * @disk: gendisk to operate on (option)
  340. * @sic: userspace structure describing the command to perform
  341. *
  342. * Send down the scsi command described by @sic to the device below
  343. * the request queue @q. If @file is non-NULL it's used to perform
  344. * fine-grained permission checks that allow users to send down
  345. * non-destructive SCSI commands. If the caller has a struct gendisk
  346. * available it should be passed in as @disk to allow the low level
  347. * driver to use the information contained in it. A non-NULL @disk
  348. * is only allowed if the caller knows that the low level driver doesn't
  349. * need it (e.g. in the scsi subsystem).
  350. *
  351. * Notes:
  352. * - This interface is deprecated - users should use the SG_IO
  353. * interface instead, as this is a more flexible approach to
  354. * performing SCSI commands on a device.
  355. * - The SCSI command length is determined by examining the 1st byte
  356. * of the given command. There is no way to override this.
  357. * - Data transfers are limited to PAGE_SIZE
  358. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  359. * accommodate the sense buffer when an error occurs.
  360. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  361. * old code will not be surprised.
  362. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  363. * a negative return and the Unix error code in 'errno'.
  364. * If the SCSI command succeeds then 0 is returned.
  365. * Positive numbers returned are the compacted SCSI error codes (4
  366. * bytes in one int) where the lowest byte is the SCSI status.
  367. */
  368. #define OMAX_SB_LEN 16 /* For backward compatibility */
  369. int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
  370. struct scsi_ioctl_command __user *sic)
  371. {
  372. struct request *rq;
  373. struct scsi_request *req;
  374. int err;
  375. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  376. char *buffer = NULL;
  377. if (!sic)
  378. return -EINVAL;
  379. /*
  380. * get in an out lengths, verify they don't exceed a page worth of data
  381. */
  382. if (get_user(in_len, &sic->inlen))
  383. return -EFAULT;
  384. if (get_user(out_len, &sic->outlen))
  385. return -EFAULT;
  386. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  387. return -EINVAL;
  388. if (get_user(opcode, sic->data))
  389. return -EFAULT;
  390. bytes = max(in_len, out_len);
  391. if (bytes) {
  392. buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  393. if (!buffer)
  394. return -ENOMEM;
  395. }
  396. rq = blk_get_request(q, in_len ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN,
  397. __GFP_RECLAIM);
  398. if (IS_ERR(rq)) {
  399. err = PTR_ERR(rq);
  400. goto error_free_buffer;
  401. }
  402. req = scsi_req(rq);
  403. cmdlen = COMMAND_SIZE(opcode);
  404. /*
  405. * get command and data to send to device, if any
  406. */
  407. err = -EFAULT;
  408. req->cmd_len = cmdlen;
  409. if (copy_from_user(req->cmd, sic->data, cmdlen))
  410. goto error;
  411. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  412. goto error;
  413. err = blk_verify_command(req->cmd, mode & FMODE_WRITE);
  414. if (err)
  415. goto error;
  416. /* default. possible overriden later */
  417. req->retries = 5;
  418. switch (opcode) {
  419. case SEND_DIAGNOSTIC:
  420. case FORMAT_UNIT:
  421. rq->timeout = FORMAT_UNIT_TIMEOUT;
  422. req->retries = 1;
  423. break;
  424. case START_STOP:
  425. rq->timeout = START_STOP_TIMEOUT;
  426. break;
  427. case MOVE_MEDIUM:
  428. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  429. break;
  430. case READ_ELEMENT_STATUS:
  431. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  432. break;
  433. case READ_DEFECT_DATA:
  434. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  435. req->retries = 1;
  436. break;
  437. default:
  438. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  439. break;
  440. }
  441. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_RECLAIM)) {
  442. err = DRIVER_ERROR << 24;
  443. goto error;
  444. }
  445. blk_execute_rq(q, disk, rq, 0);
  446. err = req->result & 0xff; /* only 8 bit SCSI status */
  447. if (err) {
  448. if (req->sense_len && req->sense) {
  449. bytes = (OMAX_SB_LEN > req->sense_len) ?
  450. req->sense_len : OMAX_SB_LEN;
  451. if (copy_to_user(sic->data, req->sense, bytes))
  452. err = -EFAULT;
  453. }
  454. } else {
  455. if (copy_to_user(sic->data, buffer, out_len))
  456. err = -EFAULT;
  457. }
  458. error:
  459. blk_put_request(rq);
  460. error_free_buffer:
  461. kfree(buffer);
  462. return err;
  463. }
  464. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  465. /* Send basic block requests */
  466. static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
  467. int cmd, int data)
  468. {
  469. struct request *rq;
  470. int err;
  471. rq = blk_get_request(q, REQ_OP_SCSI_OUT, __GFP_RECLAIM);
  472. if (IS_ERR(rq))
  473. return PTR_ERR(rq);
  474. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  475. scsi_req(rq)->cmd[0] = cmd;
  476. scsi_req(rq)->cmd[4] = data;
  477. scsi_req(rq)->cmd_len = 6;
  478. blk_execute_rq(q, bd_disk, rq, 0);
  479. err = scsi_req(rq)->result ? -EIO : 0;
  480. blk_put_request(rq);
  481. return err;
  482. }
  483. static inline int blk_send_start_stop(struct request_queue *q,
  484. struct gendisk *bd_disk, int data)
  485. {
  486. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  487. }
  488. int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mode,
  489. unsigned int cmd, void __user *arg)
  490. {
  491. int err;
  492. if (!q)
  493. return -ENXIO;
  494. switch (cmd) {
  495. /*
  496. * new sgv3 interface
  497. */
  498. case SG_GET_VERSION_NUM:
  499. err = sg_get_version(arg);
  500. break;
  501. case SCSI_IOCTL_GET_IDLUN:
  502. err = scsi_get_idlun(q, arg);
  503. break;
  504. case SCSI_IOCTL_GET_BUS_NUMBER:
  505. err = scsi_get_bus(q, arg);
  506. break;
  507. case SG_SET_TIMEOUT:
  508. err = sg_set_timeout(q, arg);
  509. break;
  510. case SG_GET_TIMEOUT:
  511. err = sg_get_timeout(q);
  512. break;
  513. case SG_GET_RESERVED_SIZE:
  514. err = sg_get_reserved_size(q, arg);
  515. break;
  516. case SG_SET_RESERVED_SIZE:
  517. err = sg_set_reserved_size(q, arg);
  518. break;
  519. case SG_EMULATED_HOST:
  520. err = sg_emulated_host(q, arg);
  521. break;
  522. case SG_IO: {
  523. struct sg_io_hdr hdr;
  524. err = -EFAULT;
  525. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  526. break;
  527. err = sg_io(q, bd_disk, &hdr, mode);
  528. if (err == -EFAULT)
  529. break;
  530. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  531. err = -EFAULT;
  532. break;
  533. }
  534. case CDROM_SEND_PACKET: {
  535. struct cdrom_generic_command cgc;
  536. struct sg_io_hdr hdr;
  537. err = -EFAULT;
  538. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  539. break;
  540. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  541. memset(&hdr, 0, sizeof(hdr));
  542. hdr.interface_id = 'S';
  543. hdr.cmd_len = sizeof(cgc.cmd);
  544. hdr.dxfer_len = cgc.buflen;
  545. err = 0;
  546. switch (cgc.data_direction) {
  547. case CGC_DATA_UNKNOWN:
  548. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  549. break;
  550. case CGC_DATA_WRITE:
  551. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  552. break;
  553. case CGC_DATA_READ:
  554. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  555. break;
  556. case CGC_DATA_NONE:
  557. hdr.dxfer_direction = SG_DXFER_NONE;
  558. break;
  559. default:
  560. err = -EINVAL;
  561. }
  562. if (err)
  563. break;
  564. hdr.dxferp = cgc.buffer;
  565. hdr.sbp = cgc.sense;
  566. if (hdr.sbp)
  567. hdr.mx_sb_len = sizeof(struct request_sense);
  568. hdr.timeout = jiffies_to_msecs(cgc.timeout);
  569. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  570. hdr.cmd_len = sizeof(cgc.cmd);
  571. err = sg_io(q, bd_disk, &hdr, mode);
  572. if (err == -EFAULT)
  573. break;
  574. if (hdr.status)
  575. err = -EIO;
  576. cgc.stat = err;
  577. cgc.buflen = hdr.resid;
  578. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  579. err = -EFAULT;
  580. break;
  581. }
  582. /*
  583. * old junk scsi send command ioctl
  584. */
  585. case SCSI_IOCTL_SEND_COMMAND:
  586. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  587. err = -EINVAL;
  588. if (!arg)
  589. break;
  590. err = sg_scsi_ioctl(q, bd_disk, mode, arg);
  591. break;
  592. case CDROMCLOSETRAY:
  593. err = blk_send_start_stop(q, bd_disk, 0x03);
  594. break;
  595. case CDROMEJECT:
  596. err = blk_send_start_stop(q, bd_disk, 0x02);
  597. break;
  598. default:
  599. err = -ENOTTY;
  600. }
  601. return err;
  602. }
  603. EXPORT_SYMBOL(scsi_cmd_ioctl);
  604. int scsi_verify_blk_ioctl(struct block_device *bd, unsigned int cmd)
  605. {
  606. if (bd && bd == bd->bd_contains)
  607. return 0;
  608. /* Actually none of these is particularly useful on a partition,
  609. * but they are safe.
  610. */
  611. switch (cmd) {
  612. case SCSI_IOCTL_GET_IDLUN:
  613. case SCSI_IOCTL_GET_BUS_NUMBER:
  614. case SCSI_IOCTL_GET_PCI:
  615. case SCSI_IOCTL_PROBE_HOST:
  616. case SG_GET_VERSION_NUM:
  617. case SG_SET_TIMEOUT:
  618. case SG_GET_TIMEOUT:
  619. case SG_GET_RESERVED_SIZE:
  620. case SG_SET_RESERVED_SIZE:
  621. case SG_EMULATED_HOST:
  622. return 0;
  623. case CDROM_GET_CAPABILITY:
  624. /* Keep this until we remove the printk below. udev sends it
  625. * and we do not want to spam dmesg about it. CD-ROMs do
  626. * not have partitions, so we get here only for disks.
  627. */
  628. return -ENOIOCTLCMD;
  629. /* MTK PATCH */
  630. #ifdef CONFIG_MTK_UFS_SUPPORT
  631. /*
  632. * MTK PATCH: bypass CAP_SYS_RAWIO checking for UFS ioctl facility.
  633. */
  634. case UFS_IOCTL_FFU:
  635. return 0;
  636. case UFS_IOCTL_QUERY:
  637. return 0;
  638. case UFS_IOCTL_GET_FW_VER:
  639. return 0;
  640. #endif
  641. default:
  642. break;
  643. }
  644. if (capable(CAP_SYS_RAWIO))
  645. return 0;
  646. /* In particular, rule out all resets and host-specific ioctls. */
  647. printk_ratelimited(KERN_WARNING
  648. "%s: sending ioctl %x to a partition!\n", current->comm, cmd);
  649. return -ENOIOCTLCMD;
  650. }
  651. EXPORT_SYMBOL(scsi_verify_blk_ioctl);
  652. int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode,
  653. unsigned int cmd, void __user *arg)
  654. {
  655. int ret;
  656. ret = scsi_verify_blk_ioctl(bd, cmd);
  657. if (ret < 0)
  658. return ret;
  659. return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg);
  660. }
  661. EXPORT_SYMBOL(scsi_cmd_blk_ioctl);
  662. /**
  663. * scsi_req_init - initialize certain fields of a scsi_request structure
  664. * @req: Pointer to a scsi_request structure.
  665. * Initializes .__cmd[], .cmd, .cmd_len and .sense_len but no other members
  666. * of struct scsi_request.
  667. */
  668. void scsi_req_init(struct scsi_request *req)
  669. {
  670. memset(req->__cmd, 0, sizeof(req->__cmd));
  671. req->cmd = req->__cmd;
  672. req->cmd_len = BLK_MAX_CDB;
  673. req->sense_len = 0;
  674. }
  675. EXPORT_SYMBOL(scsi_req_init);
  676. static int __init blk_scsi_ioctl_init(void)
  677. {
  678. blk_set_cmd_filter_defaults(&blk_default_cmd_filter);
  679. return 0;
  680. }
  681. fs_initcall(blk_scsi_ioctl_init);