bsg-lib.c 6.7 KB

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