bsg-lib.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /**
  31. * bsg_teardown_job - routine to teardown a bsg job
  32. * @job: bsg_job that is to be torn down
  33. */
  34. static void bsg_teardown_job(struct kref *kref)
  35. {
  36. struct bsg_job *job = container_of(kref, struct bsg_job, kref);
  37. struct request *rq = job->req;
  38. put_device(job->dev); /* release reference for the request */
  39. kfree(job->request_payload.sg_list);
  40. kfree(job->reply_payload.sg_list);
  41. blk_end_request_all(rq, BLK_STS_OK);
  42. }
  43. void bsg_job_put(struct bsg_job *job)
  44. {
  45. kref_put(&job->kref, bsg_teardown_job);
  46. }
  47. EXPORT_SYMBOL_GPL(bsg_job_put);
  48. int bsg_job_get(struct bsg_job *job)
  49. {
  50. return kref_get_unless_zero(&job->kref);
  51. }
  52. EXPORT_SYMBOL_GPL(bsg_job_get);
  53. /**
  54. * bsg_job_done - completion routine for bsg requests
  55. * @job: bsg_job that is complete
  56. * @result: job reply result
  57. * @reply_payload_rcv_len: length of payload recvd
  58. *
  59. * The LLD should call this when the bsg job has completed.
  60. */
  61. void bsg_job_done(struct bsg_job *job, int result,
  62. unsigned int reply_payload_rcv_len)
  63. {
  64. struct request *req = job->req;
  65. struct request *rsp = req->next_rq;
  66. struct scsi_request *rq = scsi_req(req);
  67. int err;
  68. err = scsi_req(job->req)->result = result;
  69. if (err < 0)
  70. /* we're only returning the result field in the reply */
  71. rq->sense_len = sizeof(u32);
  72. else
  73. rq->sense_len = job->reply_len;
  74. /* we assume all request payload was transferred, residual == 0 */
  75. rq->resid_len = 0;
  76. if (rsp) {
  77. WARN_ON(reply_payload_rcv_len > scsi_req(rsp)->resid_len);
  78. /* set reply (bidi) residual */
  79. scsi_req(rsp)->resid_len -=
  80. min(reply_payload_rcv_len, scsi_req(rsp)->resid_len);
  81. }
  82. blk_complete_request(req);
  83. }
  84. EXPORT_SYMBOL_GPL(bsg_job_done);
  85. /**
  86. * bsg_softirq_done - softirq done routine for destroying the bsg requests
  87. * @rq: BSG request that holds the job to be destroyed
  88. */
  89. static void bsg_softirq_done(struct request *rq)
  90. {
  91. struct bsg_job *job = blk_mq_rq_to_pdu(rq);
  92. bsg_job_put(job);
  93. }
  94. static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
  95. {
  96. size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
  97. BUG_ON(!req->nr_phys_segments);
  98. buf->sg_list = kzalloc(sz, GFP_KERNEL);
  99. if (!buf->sg_list)
  100. return -ENOMEM;
  101. sg_init_table(buf->sg_list, req->nr_phys_segments);
  102. scsi_req(req)->resid_len = blk_rq_bytes(req);
  103. buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
  104. buf->payload_len = blk_rq_bytes(req);
  105. return 0;
  106. }
  107. /**
  108. * bsg_prepare_job - create the bsg_job structure for the bsg request
  109. * @dev: device that is being sent the bsg request
  110. * @req: BSG request that needs a job structure
  111. */
  112. static int bsg_prepare_job(struct device *dev, struct request *req)
  113. {
  114. struct request *rsp = req->next_rq;
  115. struct scsi_request *rq = scsi_req(req);
  116. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  117. int ret;
  118. job->request = rq->cmd;
  119. job->request_len = rq->cmd_len;
  120. if (req->bio) {
  121. ret = bsg_map_buffer(&job->request_payload, req);
  122. if (ret)
  123. goto failjob_rls_job;
  124. }
  125. if (rsp && rsp->bio) {
  126. ret = bsg_map_buffer(&job->reply_payload, rsp);
  127. if (ret)
  128. goto failjob_rls_rqst_payload;
  129. }
  130. job->dev = dev;
  131. /* take a reference for the request */
  132. get_device(job->dev);
  133. kref_init(&job->kref);
  134. return 0;
  135. failjob_rls_rqst_payload:
  136. kfree(job->request_payload.sg_list);
  137. failjob_rls_job:
  138. return -ENOMEM;
  139. }
  140. /**
  141. * bsg_request_fn - generic handler for bsg requests
  142. * @q: request queue to manage
  143. *
  144. * On error the create_bsg_job function should return a -Exyz error value
  145. * that will be set to ->result.
  146. *
  147. * Drivers/subsys should pass this to the queue init function.
  148. */
  149. static void bsg_request_fn(struct request_queue *q)
  150. __releases(q->queue_lock)
  151. __acquires(q->queue_lock)
  152. {
  153. struct device *dev = q->queuedata;
  154. struct request *req;
  155. int ret;
  156. if (!get_device(dev))
  157. return;
  158. while (1) {
  159. req = blk_fetch_request(q);
  160. if (!req)
  161. break;
  162. spin_unlock_irq(q->queue_lock);
  163. ret = bsg_prepare_job(dev, req);
  164. if (ret) {
  165. scsi_req(req)->result = ret;
  166. blk_end_request_all(req, BLK_STS_OK);
  167. spin_lock_irq(q->queue_lock);
  168. continue;
  169. }
  170. ret = q->bsg_job_fn(blk_mq_rq_to_pdu(req));
  171. spin_lock_irq(q->queue_lock);
  172. if (ret)
  173. break;
  174. }
  175. spin_unlock_irq(q->queue_lock);
  176. put_device(dev);
  177. spin_lock_irq(q->queue_lock);
  178. }
  179. static int bsg_init_rq(struct request_queue *q, struct request *req, gfp_t gfp)
  180. {
  181. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  182. struct scsi_request *sreq = &job->sreq;
  183. /* called right after the request is allocated for the request_queue */
  184. sreq->sense = kzalloc(SCSI_SENSE_BUFFERSIZE, gfp);
  185. if (!sreq->sense)
  186. return -ENOMEM;
  187. return 0;
  188. }
  189. static void bsg_initialize_rq(struct request *req)
  190. {
  191. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  192. struct scsi_request *sreq = &job->sreq;
  193. void *sense = sreq->sense;
  194. /* called right before the request is given to the request_queue user */
  195. memset(job, 0, sizeof(*job));
  196. scsi_req_init(sreq);
  197. sreq->sense = sense;
  198. sreq->sense_len = SCSI_SENSE_BUFFERSIZE;
  199. job->req = req;
  200. job->reply = sense;
  201. job->reply_len = sreq->sense_len;
  202. job->dd_data = job + 1;
  203. }
  204. static void bsg_exit_rq(struct request_queue *q, struct request *req)
  205. {
  206. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  207. struct scsi_request *sreq = &job->sreq;
  208. kfree(sreq->sense);
  209. }
  210. /**
  211. * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
  212. * @dev: device to attach bsg device to
  213. * @name: device to give bsg device
  214. * @job_fn: bsg job handler
  215. * @dd_job_size: size of LLD data needed for each job
  216. */
  217. struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
  218. bsg_job_fn *job_fn, int dd_job_size,
  219. void (*release)(struct device *))
  220. {
  221. struct request_queue *q;
  222. int ret;
  223. q = blk_alloc_queue(GFP_KERNEL);
  224. if (!q)
  225. return ERR_PTR(-ENOMEM);
  226. q->cmd_size = sizeof(struct bsg_job) + dd_job_size;
  227. q->init_rq_fn = bsg_init_rq;
  228. q->exit_rq_fn = bsg_exit_rq;
  229. q->initialize_rq_fn = bsg_initialize_rq;
  230. q->request_fn = bsg_request_fn;
  231. ret = blk_init_allocated_queue(q);
  232. if (ret)
  233. goto out_cleanup_queue;
  234. q->queuedata = dev;
  235. q->bsg_job_fn = job_fn;
  236. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
  237. queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
  238. blk_queue_softirq_done(q, bsg_softirq_done);
  239. blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
  240. ret = bsg_register_queue(q, dev, name, release);
  241. if (ret) {
  242. printk(KERN_ERR "%s: bsg interface failed to "
  243. "initialize - register queue\n", dev->kobj.name);
  244. goto out_cleanup_queue;
  245. }
  246. return q;
  247. out_cleanup_queue:
  248. blk_cleanup_queue(q);
  249. return ERR_PTR(ret);
  250. }
  251. EXPORT_SYMBOL_GPL(bsg_setup_queue);