bsg-lib.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * BSG helper library
  3. *
  4. * Copyright (C) 2008 James Smart, Emulex Corporation
  5. * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
  6. * Copyright (C) 2011 Mike Christie
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/delay.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/bsg-lib.h>
  28. #include <linux/export.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/sg.h>
  31. #define uptr64(val) ((void __user *)(uintptr_t)(val))
  32. static int bsg_transport_check_proto(struct sg_io_v4 *hdr)
  33. {
  34. if (hdr->protocol != BSG_PROTOCOL_SCSI ||
  35. hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_TRANSPORT)
  36. return -EINVAL;
  37. if (!capable(CAP_SYS_RAWIO))
  38. return -EPERM;
  39. return 0;
  40. }
  41. static int bsg_transport_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
  42. fmode_t mode)
  43. {
  44. struct bsg_job *job = blk_mq_rq_to_pdu(rq);
  45. job->request_len = hdr->request_len;
  46. job->request = memdup_user(uptr64(hdr->request), hdr->request_len);
  47. return PTR_ERR_OR_ZERO(job->request);
  48. }
  49. static int bsg_transport_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
  50. {
  51. struct bsg_job *job = blk_mq_rq_to_pdu(rq);
  52. int ret = 0;
  53. /*
  54. * The assignments below don't make much sense, but are kept for
  55. * bug by bug backwards compatibility:
  56. */
  57. hdr->device_status = job->result & 0xff;
  58. hdr->transport_status = host_byte(job->result);
  59. hdr->driver_status = driver_byte(job->result);
  60. hdr->info = 0;
  61. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  62. hdr->info |= SG_INFO_CHECK;
  63. hdr->response_len = 0;
  64. if (job->result < 0) {
  65. /* we're only returning the result field in the reply */
  66. job->reply_len = sizeof(u32);
  67. ret = job->result;
  68. }
  69. if (job->reply_len && hdr->response) {
  70. int len = min(hdr->max_response_len, job->reply_len);
  71. if (copy_to_user(uptr64(hdr->response), job->reply, len))
  72. ret = -EFAULT;
  73. else
  74. hdr->response_len = len;
  75. }
  76. /* we assume all request payload was transferred, residual == 0 */
  77. hdr->dout_resid = 0;
  78. if (rq->next_rq) {
  79. unsigned int rsp_len = job->reply_payload.payload_len;
  80. if (WARN_ON(job->reply_payload_rcv_len > rsp_len))
  81. hdr->din_resid = 0;
  82. else
  83. hdr->din_resid = rsp_len - job->reply_payload_rcv_len;
  84. } else {
  85. hdr->din_resid = 0;
  86. }
  87. return ret;
  88. }
  89. static void bsg_transport_free_rq(struct request *rq)
  90. {
  91. struct bsg_job *job = blk_mq_rq_to_pdu(rq);
  92. kfree(job->request);
  93. }
  94. static const struct bsg_ops bsg_transport_ops = {
  95. .check_proto = bsg_transport_check_proto,
  96. .fill_hdr = bsg_transport_fill_hdr,
  97. .complete_rq = bsg_transport_complete_rq,
  98. .free_rq = bsg_transport_free_rq,
  99. };
  100. /**
  101. * bsg_teardown_job - routine to teardown a bsg job
  102. * @kref: kref inside bsg_job that is to be torn down
  103. */
  104. static void bsg_teardown_job(struct kref *kref)
  105. {
  106. struct bsg_job *job = container_of(kref, struct bsg_job, kref);
  107. struct request *rq = blk_mq_rq_from_pdu(job);
  108. put_device(job->dev); /* release reference for the request */
  109. kfree(job->request_payload.sg_list);
  110. kfree(job->reply_payload.sg_list);
  111. blk_end_request_all(rq, BLK_STS_OK);
  112. }
  113. void bsg_job_put(struct bsg_job *job)
  114. {
  115. kref_put(&job->kref, bsg_teardown_job);
  116. }
  117. EXPORT_SYMBOL_GPL(bsg_job_put);
  118. int bsg_job_get(struct bsg_job *job)
  119. {
  120. return kref_get_unless_zero(&job->kref);
  121. }
  122. EXPORT_SYMBOL_GPL(bsg_job_get);
  123. /**
  124. * bsg_job_done - completion routine for bsg requests
  125. * @job: bsg_job that is complete
  126. * @result: job reply result
  127. * @reply_payload_rcv_len: length of payload recvd
  128. *
  129. * The LLD should call this when the bsg job has completed.
  130. */
  131. void bsg_job_done(struct bsg_job *job, int result,
  132. unsigned int reply_payload_rcv_len)
  133. {
  134. job->result = result;
  135. job->reply_payload_rcv_len = reply_payload_rcv_len;
  136. blk_complete_request(blk_mq_rq_from_pdu(job));
  137. }
  138. EXPORT_SYMBOL_GPL(bsg_job_done);
  139. /**
  140. * bsg_softirq_done - softirq done routine for destroying the bsg requests
  141. * @rq: BSG request that holds the job to be destroyed
  142. */
  143. static void bsg_softirq_done(struct request *rq)
  144. {
  145. struct bsg_job *job = blk_mq_rq_to_pdu(rq);
  146. bsg_job_put(job);
  147. }
  148. static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
  149. {
  150. size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
  151. BUG_ON(!req->nr_phys_segments);
  152. buf->sg_list = kzalloc(sz, GFP_KERNEL);
  153. if (!buf->sg_list)
  154. return -ENOMEM;
  155. sg_init_table(buf->sg_list, req->nr_phys_segments);
  156. buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
  157. buf->payload_len = blk_rq_bytes(req);
  158. return 0;
  159. }
  160. /**
  161. * bsg_prepare_job - create the bsg_job structure for the bsg request
  162. * @dev: device that is being sent the bsg request
  163. * @req: BSG request that needs a job structure
  164. */
  165. static bool bsg_prepare_job(struct device *dev, struct request *req)
  166. {
  167. struct request *rsp = req->next_rq;
  168. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  169. int ret;
  170. job->timeout = req->timeout;
  171. if (req->bio) {
  172. ret = bsg_map_buffer(&job->request_payload, req);
  173. if (ret)
  174. goto failjob_rls_job;
  175. }
  176. if (rsp && rsp->bio) {
  177. ret = bsg_map_buffer(&job->reply_payload, rsp);
  178. if (ret)
  179. goto failjob_rls_rqst_payload;
  180. }
  181. job->dev = dev;
  182. /* take a reference for the request */
  183. get_device(job->dev);
  184. kref_init(&job->kref);
  185. return true;
  186. failjob_rls_rqst_payload:
  187. kfree(job->request_payload.sg_list);
  188. failjob_rls_job:
  189. job->result = -ENOMEM;
  190. return false;
  191. }
  192. /**
  193. * bsg_request_fn - generic handler for bsg requests
  194. * @q: request queue to manage
  195. *
  196. * On error the create_bsg_job function should return a -Exyz error value
  197. * that will be set to ->result.
  198. *
  199. * Drivers/subsys should pass this to the queue init function.
  200. */
  201. static void bsg_request_fn(struct request_queue *q)
  202. __releases(q->queue_lock)
  203. __acquires(q->queue_lock)
  204. {
  205. struct device *dev = q->queuedata;
  206. struct request *req;
  207. int ret;
  208. if (!get_device(dev))
  209. return;
  210. while (1) {
  211. req = blk_fetch_request(q);
  212. if (!req)
  213. break;
  214. spin_unlock_irq(q->queue_lock);
  215. if (!bsg_prepare_job(dev, req)) {
  216. blk_end_request_all(req, BLK_STS_OK);
  217. spin_lock_irq(q->queue_lock);
  218. continue;
  219. }
  220. ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req));
  221. spin_lock_irq(q->queue_lock);
  222. if (ret)
  223. break;
  224. }
  225. spin_unlock_irq(q->queue_lock);
  226. put_device(dev);
  227. spin_lock_irq(q->queue_lock);
  228. }
  229. /* called right after the request is allocated for the request_queue */
  230. static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
  231. {
  232. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  233. job->reply = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp);
  234. if (!job->reply)
  235. return -ENOMEM;
  236. return 0;
  237. }
  238. /* called right before the request is given to the request_queue user */
  239. static void bsg_initialize_rq(struct request *req)
  240. {
  241. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  242. void *reply = job->reply;
  243. memset(job, 0, sizeof(*job));
  244. job->reply = reply;
  245. job->reply_len = SCSI_SENSE_BUFFERSIZE;
  246. job->dd_data = job + 1;
  247. }
  248. static void bsg_exit_rq(struct request_queue *q, struct request *req)
  249. {
  250. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  251. kfree(job->reply);
  252. }
  253. /**
  254. * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
  255. * @dev: device to attach bsg device to
  256. * @name: device to give bsg device
  257. * @job_fn: bsg job handler
  258. * @dd_job_size: size of LLD data needed for each job
  259. */
  260. struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
  261. bsg_job_fn *job_fn, int dd_job_size)
  262. {
  263. struct request_queue *q;
  264. int ret;
  265. q = blk_alloc_queue(GFP_KERNEL);
  266. if (!q)
  267. return ERR_PTR(-ENOMEM);
  268. q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
  269. q->init_rq_fn = bsg_init_rq;
  270. q->exit_rq_fn = bsg_exit_rq;
  271. q->initialize_rq_fn = bsg_initialize_rq;
  272. q->request_fn = bsg_request_fn;
  273. ret = blk_init_allocated_queue(q);
  274. if (ret)
  275. goto out_cleanup_queue;
  276. q->queuedata = dev;
  277. q->bsg_job_fn = job_fn;
  278. blk_queue_flag_set(QUEUE_FLAG_BIDI, q);
  279. blk_queue_softirq_done(q, bsg_softirq_done);
  280. blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
  281. ret = bsg_register_queue(q, dev, name, &bsg_transport_ops);
  282. if (ret) {
  283. printk(KERN_ERR "%s: bsg interface failed to "
  284. "initialize - register queue\n", dev->kobj.name);
  285. goto out_cleanup_queue;
  286. }
  287. return q;
  288. out_cleanup_queue:
  289. blk_cleanup_queue(q);
  290. return ERR_PTR(ret);
  291. }
  292. EXPORT_SYMBOL_GPL(bsg_setup_queue);