bsg-lib.c 6.0 KB

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