Back to home page

LXR

 
 

    


0001 /*
0002  *  BSG helper library
0003  *
0004  *  Copyright (C) 2008   James Smart, Emulex Corporation
0005  *  Copyright (C) 2011   Red Hat, Inc.  All rights reserved.
0006  *  Copyright (C) 2011   Mike Christie
0007  *
0008  *  This program is free software; you can redistribute it and/or modify
0009  *  it under the terms of the GNU General Public License as published by
0010  *  the Free Software Foundation; either version 2 of the License, or
0011  *  (at your option) any later version.
0012  *
0013  *  This program is distributed in the hope that it will be useful,
0014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  *  GNU General Public License for more details.
0017  *
0018  *  You should have received a copy of the GNU General Public License
0019  *  along with this program; if not, write to the Free Software
0020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0021  *
0022  */
0023 #include <linux/slab.h>
0024 #include <linux/blkdev.h>
0025 #include <linux/delay.h>
0026 #include <linux/scatterlist.h>
0027 #include <linux/bsg-lib.h>
0028 #include <linux/export.h>
0029 #include <scsi/scsi_cmnd.h>
0030 
0031 /**
0032  * bsg_destroy_job - routine to teardown/delete a bsg job
0033  * @job: bsg_job that is to be torn down
0034  */
0035 static void bsg_destroy_job(struct kref *kref)
0036 {
0037     struct bsg_job *job = container_of(kref, struct bsg_job, kref);
0038     struct request *rq = job->req;
0039 
0040     blk_end_request_all(rq, rq->errors);
0041 
0042     put_device(job->dev);   /* release reference for the request */
0043 
0044     kfree(job->request_payload.sg_list);
0045     kfree(job->reply_payload.sg_list);
0046     kfree(job);
0047 }
0048 
0049 void bsg_job_put(struct bsg_job *job)
0050 {
0051     kref_put(&job->kref, bsg_destroy_job);
0052 }
0053 EXPORT_SYMBOL_GPL(bsg_job_put);
0054 
0055 int bsg_job_get(struct bsg_job *job)
0056 {
0057     return kref_get_unless_zero(&job->kref);
0058 }
0059 EXPORT_SYMBOL_GPL(bsg_job_get);
0060 
0061 /**
0062  * bsg_job_done - completion routine for bsg requests
0063  * @job: bsg_job that is complete
0064  * @result: job reply result
0065  * @reply_payload_rcv_len: length of payload recvd
0066  *
0067  * The LLD should call this when the bsg job has completed.
0068  */
0069 void bsg_job_done(struct bsg_job *job, int result,
0070           unsigned int reply_payload_rcv_len)
0071 {
0072     struct request *req = job->req;
0073     struct request *rsp = req->next_rq;
0074     int err;
0075 
0076     err = job->req->errors = result;
0077     if (err < 0)
0078         /* we're only returning the result field in the reply */
0079         job->req->sense_len = sizeof(u32);
0080     else
0081         job->req->sense_len = job->reply_len;
0082     /* we assume all request payload was transferred, residual == 0 */
0083     req->resid_len = 0;
0084 
0085     if (rsp) {
0086         WARN_ON(reply_payload_rcv_len > rsp->resid_len);
0087 
0088         /* set reply (bidi) residual */
0089         rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
0090     }
0091     blk_complete_request(req);
0092 }
0093 EXPORT_SYMBOL_GPL(bsg_job_done);
0094 
0095 /**
0096  * bsg_softirq_done - softirq done routine for destroying the bsg requests
0097  * @rq: BSG request that holds the job to be destroyed
0098  */
0099 static void bsg_softirq_done(struct request *rq)
0100 {
0101     struct bsg_job *job = rq->special;
0102 
0103     bsg_job_put(job);
0104 }
0105 
0106 static int bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
0107 {
0108     size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
0109 
0110     BUG_ON(!req->nr_phys_segments);
0111 
0112     buf->sg_list = kzalloc(sz, GFP_KERNEL);
0113     if (!buf->sg_list)
0114         return -ENOMEM;
0115     sg_init_table(buf->sg_list, req->nr_phys_segments);
0116     buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
0117     buf->payload_len = blk_rq_bytes(req);
0118     return 0;
0119 }
0120 
0121 /**
0122  * bsg_create_job - create the bsg_job structure for the bsg request
0123  * @dev: device that is being sent the bsg request
0124  * @req: BSG request that needs a job structure
0125  */
0126 static int bsg_create_job(struct device *dev, struct request *req)
0127 {
0128     struct request *rsp = req->next_rq;
0129     struct request_queue *q = req->q;
0130     struct bsg_job *job;
0131     int ret;
0132 
0133     BUG_ON(req->special);
0134 
0135     job = kzalloc(sizeof(struct bsg_job) + q->bsg_job_size, GFP_KERNEL);
0136     if (!job)
0137         return -ENOMEM;
0138 
0139     req->special = job;
0140     job->req = req;
0141     if (q->bsg_job_size)
0142         job->dd_data = (void *)&job[1];
0143     job->request = req->cmd;
0144     job->request_len = req->cmd_len;
0145     job->reply = req->sense;
0146     job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
0147                          * allocated */
0148     if (req->bio) {
0149         ret = bsg_map_buffer(&job->request_payload, req);
0150         if (ret)
0151             goto failjob_rls_job;
0152     }
0153     if (rsp && rsp->bio) {
0154         ret = bsg_map_buffer(&job->reply_payload, rsp);
0155         if (ret)
0156             goto failjob_rls_rqst_payload;
0157     }
0158     job->dev = dev;
0159     /* take a reference for the request */
0160     get_device(job->dev);
0161     kref_init(&job->kref);
0162     return 0;
0163 
0164 failjob_rls_rqst_payload:
0165     kfree(job->request_payload.sg_list);
0166 failjob_rls_job:
0167     kfree(job);
0168     return -ENOMEM;
0169 }
0170 
0171 /**
0172  * bsg_request_fn - generic handler for bsg requests
0173  * @q: request queue to manage
0174  *
0175  * On error the create_bsg_job function should return a -Exyz error value
0176  * that will be set to the req->errors.
0177  *
0178  * Drivers/subsys should pass this to the queue init function.
0179  */
0180 void bsg_request_fn(struct request_queue *q)
0181     __releases(q->queue_lock)
0182     __acquires(q->queue_lock)
0183 {
0184     struct device *dev = q->queuedata;
0185     struct request *req;
0186     struct bsg_job *job;
0187     int ret;
0188 
0189     if (!get_device(dev))
0190         return;
0191 
0192     while (1) {
0193         req = blk_fetch_request(q);
0194         if (!req)
0195             break;
0196         spin_unlock_irq(q->queue_lock);
0197 
0198         ret = bsg_create_job(dev, req);
0199         if (ret) {
0200             req->errors = ret;
0201             blk_end_request_all(req, ret);
0202             spin_lock_irq(q->queue_lock);
0203             continue;
0204         }
0205 
0206         job = req->special;
0207         ret = q->bsg_job_fn(job);
0208         spin_lock_irq(q->queue_lock);
0209         if (ret)
0210             break;
0211     }
0212 
0213     spin_unlock_irq(q->queue_lock);
0214     put_device(dev);
0215     spin_lock_irq(q->queue_lock);
0216 }
0217 EXPORT_SYMBOL_GPL(bsg_request_fn);
0218 
0219 /**
0220  * bsg_setup_queue - Create and add the bsg hooks so we can receive requests
0221  * @dev: device to attach bsg device to
0222  * @q: request queue setup by caller
0223  * @name: device to give bsg device
0224  * @job_fn: bsg job handler
0225  * @dd_job_size: size of LLD data needed for each job
0226  *
0227  * The caller should have setup the reuqest queue with bsg_request_fn
0228  * as the request_fn.
0229  */
0230 int bsg_setup_queue(struct device *dev, struct request_queue *q,
0231             char *name, bsg_job_fn *job_fn, int dd_job_size)
0232 {
0233     int ret;
0234 
0235     q->queuedata = dev;
0236     q->bsg_job_size = dd_job_size;
0237     q->bsg_job_fn = job_fn;
0238     queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
0239     blk_queue_softirq_done(q, bsg_softirq_done);
0240     blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
0241 
0242     ret = bsg_register_queue(q, dev, name, NULL);
0243     if (ret) {
0244         printk(KERN_ERR "%s: bsg interface failed to "
0245                "initialize - register queue\n", dev->kobj.name);
0246         return ret;
0247     }
0248 
0249     return 0;
0250 }
0251 EXPORT_SYMBOL_GPL(bsg_setup_queue);