Back to home page

LXR

 
 

    


0001 /*
0002  * Functions related to tagged command queuing
0003  */
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/bio.h>
0007 #include <linux/blkdev.h>
0008 #include <linux/slab.h>
0009 
0010 #include "blk.h"
0011 
0012 /**
0013  * blk_queue_find_tag - find a request by its tag and queue
0014  * @q:   The request queue for the device
0015  * @tag: The tag of the request
0016  *
0017  * Notes:
0018  *    Should be used when a device returns a tag and you want to match
0019  *    it with a request.
0020  *
0021  *    no locks need be held.
0022  **/
0023 struct request *blk_queue_find_tag(struct request_queue *q, int tag)
0024 {
0025     return blk_map_queue_find_tag(q->queue_tags, tag);
0026 }
0027 EXPORT_SYMBOL(blk_queue_find_tag);
0028 
0029 /**
0030  * blk_free_tags - release a given set of tag maintenance info
0031  * @bqt:    the tag map to free
0032  *
0033  * Drop the reference count on @bqt and frees it when the last reference
0034  * is dropped.
0035  */
0036 void blk_free_tags(struct blk_queue_tag *bqt)
0037 {
0038     if (atomic_dec_and_test(&bqt->refcnt)) {
0039         BUG_ON(find_first_bit(bqt->tag_map, bqt->max_depth) <
0040                             bqt->max_depth);
0041 
0042         kfree(bqt->tag_index);
0043         bqt->tag_index = NULL;
0044 
0045         kfree(bqt->tag_map);
0046         bqt->tag_map = NULL;
0047 
0048         kfree(bqt);
0049     }
0050 }
0051 EXPORT_SYMBOL(blk_free_tags);
0052 
0053 /**
0054  * __blk_queue_free_tags - release tag maintenance info
0055  * @q:  the request queue for the device
0056  *
0057  *  Notes:
0058  *    blk_cleanup_queue() will take care of calling this function, if tagging
0059  *    has been used. So there's no need to call this directly.
0060  **/
0061 void __blk_queue_free_tags(struct request_queue *q)
0062 {
0063     struct blk_queue_tag *bqt = q->queue_tags;
0064 
0065     if (!bqt)
0066         return;
0067 
0068     blk_free_tags(bqt);
0069 
0070     q->queue_tags = NULL;
0071     queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
0072 }
0073 
0074 /**
0075  * blk_queue_free_tags - release tag maintenance info
0076  * @q:  the request queue for the device
0077  *
0078  *  Notes:
0079  *  This is used to disable tagged queuing to a device, yet leave
0080  *  queue in function.
0081  **/
0082 void blk_queue_free_tags(struct request_queue *q)
0083 {
0084     queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
0085 }
0086 EXPORT_SYMBOL(blk_queue_free_tags);
0087 
0088 static int
0089 init_tag_map(struct request_queue *q, struct blk_queue_tag *tags, int depth)
0090 {
0091     struct request **tag_index;
0092     unsigned long *tag_map;
0093     int nr_ulongs;
0094 
0095     if (q && depth > q->nr_requests * 2) {
0096         depth = q->nr_requests * 2;
0097         printk(KERN_ERR "%s: adjusted depth to %d\n",
0098                __func__, depth);
0099     }
0100 
0101     tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
0102     if (!tag_index)
0103         goto fail;
0104 
0105     nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
0106     tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
0107     if (!tag_map)
0108         goto fail;
0109 
0110     tags->real_max_depth = depth;
0111     tags->max_depth = depth;
0112     tags->tag_index = tag_index;
0113     tags->tag_map = tag_map;
0114 
0115     return 0;
0116 fail:
0117     kfree(tag_index);
0118     return -ENOMEM;
0119 }
0120 
0121 static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
0122                         int depth, int alloc_policy)
0123 {
0124     struct blk_queue_tag *tags;
0125 
0126     tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
0127     if (!tags)
0128         goto fail;
0129 
0130     if (init_tag_map(q, tags, depth))
0131         goto fail;
0132 
0133     atomic_set(&tags->refcnt, 1);
0134     tags->alloc_policy = alloc_policy;
0135     tags->next_tag = 0;
0136     return tags;
0137 fail:
0138     kfree(tags);
0139     return NULL;
0140 }
0141 
0142 /**
0143  * blk_init_tags - initialize the tag info for an external tag map
0144  * @depth:  the maximum queue depth supported
0145  * @alloc_policy: tag allocation policy
0146  **/
0147 struct blk_queue_tag *blk_init_tags(int depth, int alloc_policy)
0148 {
0149     return __blk_queue_init_tags(NULL, depth, alloc_policy);
0150 }
0151 EXPORT_SYMBOL(blk_init_tags);
0152 
0153 /**
0154  * blk_queue_init_tags - initialize the queue tag info
0155  * @q:  the request queue for the device
0156  * @depth:  the maximum queue depth supported
0157  * @tags: the tag to use
0158  * @alloc_policy: tag allocation policy
0159  *
0160  * Queue lock must be held here if the function is called to resize an
0161  * existing map.
0162  **/
0163 int blk_queue_init_tags(struct request_queue *q, int depth,
0164             struct blk_queue_tag *tags, int alloc_policy)
0165 {
0166     int rc;
0167 
0168     BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
0169 
0170     if (!tags && !q->queue_tags) {
0171         tags = __blk_queue_init_tags(q, depth, alloc_policy);
0172 
0173         if (!tags)
0174             return -ENOMEM;
0175 
0176     } else if (q->queue_tags) {
0177         rc = blk_queue_resize_tags(q, depth);
0178         if (rc)
0179             return rc;
0180         queue_flag_set(QUEUE_FLAG_QUEUED, q);
0181         return 0;
0182     } else
0183         atomic_inc(&tags->refcnt);
0184 
0185     /*
0186      * assign it, all done
0187      */
0188     q->queue_tags = tags;
0189     queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, q);
0190     INIT_LIST_HEAD(&q->tag_busy_list);
0191     return 0;
0192 }
0193 EXPORT_SYMBOL(blk_queue_init_tags);
0194 
0195 /**
0196  * blk_queue_resize_tags - change the queueing depth
0197  * @q:  the request queue for the device
0198  * @new_depth: the new max command queueing depth
0199  *
0200  *  Notes:
0201  *    Must be called with the queue lock held.
0202  **/
0203 int blk_queue_resize_tags(struct request_queue *q, int new_depth)
0204 {
0205     struct blk_queue_tag *bqt = q->queue_tags;
0206     struct request **tag_index;
0207     unsigned long *tag_map;
0208     int max_depth, nr_ulongs;
0209 
0210     if (!bqt)
0211         return -ENXIO;
0212 
0213     /*
0214      * if we already have large enough real_max_depth.  just
0215      * adjust max_depth.  *NOTE* as requests with tag value
0216      * between new_depth and real_max_depth can be in-flight, tag
0217      * map can not be shrunk blindly here.
0218      */
0219     if (new_depth <= bqt->real_max_depth) {
0220         bqt->max_depth = new_depth;
0221         return 0;
0222     }
0223 
0224     /*
0225      * Currently cannot replace a shared tag map with a new
0226      * one, so error out if this is the case
0227      */
0228     if (atomic_read(&bqt->refcnt) != 1)
0229         return -EBUSY;
0230 
0231     /*
0232      * save the old state info, so we can copy it back
0233      */
0234     tag_index = bqt->tag_index;
0235     tag_map = bqt->tag_map;
0236     max_depth = bqt->real_max_depth;
0237 
0238     if (init_tag_map(q, bqt, new_depth))
0239         return -ENOMEM;
0240 
0241     memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
0242     nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
0243     memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
0244 
0245     kfree(tag_index);
0246     kfree(tag_map);
0247     return 0;
0248 }
0249 EXPORT_SYMBOL(blk_queue_resize_tags);
0250 
0251 /**
0252  * blk_queue_end_tag - end tag operations for a request
0253  * @q:  the request queue for the device
0254  * @rq: the request that has completed
0255  *
0256  *  Description:
0257  *    Typically called when end_that_request_first() returns %0, meaning
0258  *    all transfers have been done for a request. It's important to call
0259  *    this function before end_that_request_last(), as that will put the
0260  *    request back on the free list thus corrupting the internal tag list.
0261  *
0262  *  Notes:
0263  *   queue lock must be held.
0264  **/
0265 void blk_queue_end_tag(struct request_queue *q, struct request *rq)
0266 {
0267     struct blk_queue_tag *bqt = q->queue_tags;
0268     unsigned tag = rq->tag; /* negative tags invalid */
0269 
0270     BUG_ON(tag >= bqt->real_max_depth);
0271 
0272     list_del_init(&rq->queuelist);
0273     rq->rq_flags &= ~RQF_QUEUED;
0274     rq->tag = -1;
0275 
0276     if (unlikely(bqt->tag_index[tag] == NULL))
0277         printk(KERN_ERR "%s: tag %d is missing\n",
0278                __func__, tag);
0279 
0280     bqt->tag_index[tag] = NULL;
0281 
0282     if (unlikely(!test_bit(tag, bqt->tag_map))) {
0283         printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
0284                __func__, tag);
0285         return;
0286     }
0287     /*
0288      * The tag_map bit acts as a lock for tag_index[bit], so we need
0289      * unlock memory barrier semantics.
0290      */
0291     clear_bit_unlock(tag, bqt->tag_map);
0292 }
0293 EXPORT_SYMBOL(blk_queue_end_tag);
0294 
0295 /**
0296  * blk_queue_start_tag - find a free tag and assign it
0297  * @q:  the request queue for the device
0298  * @rq:  the block request that needs tagging
0299  *
0300  *  Description:
0301  *    This can either be used as a stand-alone helper, or possibly be
0302  *    assigned as the queue &prep_rq_fn (in which case &struct request
0303  *    automagically gets a tag assigned). Note that this function
0304  *    assumes that any type of request can be queued! if this is not
0305  *    true for your device, you must check the request type before
0306  *    calling this function.  The request will also be removed from
0307  *    the request queue, so it's the drivers responsibility to readd
0308  *    it if it should need to be restarted for some reason.
0309  *
0310  *  Notes:
0311  *   queue lock must be held.
0312  **/
0313 int blk_queue_start_tag(struct request_queue *q, struct request *rq)
0314 {
0315     struct blk_queue_tag *bqt = q->queue_tags;
0316     unsigned max_depth;
0317     int tag;
0318 
0319     if (unlikely((rq->rq_flags & RQF_QUEUED))) {
0320         printk(KERN_ERR
0321                "%s: request %p for device [%s] already tagged %d",
0322                __func__, rq,
0323                rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
0324         BUG();
0325     }
0326 
0327     /*
0328      * Protect against shared tag maps, as we may not have exclusive
0329      * access to the tag map.
0330      *
0331      * We reserve a few tags just for sync IO, since we don't want
0332      * to starve sync IO on behalf of flooding async IO.
0333      */
0334     max_depth = bqt->max_depth;
0335     if (!rq_is_sync(rq) && max_depth > 1) {
0336         switch (max_depth) {
0337         case 2:
0338             max_depth = 1;
0339             break;
0340         case 3:
0341             max_depth = 2;
0342             break;
0343         default:
0344             max_depth -= 2;
0345         }
0346         if (q->in_flight[BLK_RW_ASYNC] > max_depth)
0347             return 1;
0348     }
0349 
0350     do {
0351         if (bqt->alloc_policy == BLK_TAG_ALLOC_FIFO) {
0352             tag = find_first_zero_bit(bqt->tag_map, max_depth);
0353             if (tag >= max_depth)
0354                 return 1;
0355         } else {
0356             int start = bqt->next_tag;
0357             int size = min_t(int, bqt->max_depth, max_depth + start);
0358             tag = find_next_zero_bit(bqt->tag_map, size, start);
0359             if (tag >= size && start + size > bqt->max_depth) {
0360                 size = start + size - bqt->max_depth;
0361                 tag = find_first_zero_bit(bqt->tag_map, size);
0362             }
0363             if (tag >= size)
0364                 return 1;
0365         }
0366 
0367     } while (test_and_set_bit_lock(tag, bqt->tag_map));
0368     /*
0369      * We need lock ordering semantics given by test_and_set_bit_lock.
0370      * See blk_queue_end_tag for details.
0371      */
0372 
0373     bqt->next_tag = (tag + 1) % bqt->max_depth;
0374     rq->rq_flags |= RQF_QUEUED;
0375     rq->tag = tag;
0376     bqt->tag_index[tag] = rq;
0377     blk_start_request(rq);
0378     list_add(&rq->queuelist, &q->tag_busy_list);
0379     return 0;
0380 }
0381 EXPORT_SYMBOL(blk_queue_start_tag);
0382 
0383 /**
0384  * blk_queue_invalidate_tags - invalidate all pending tags
0385  * @q:  the request queue for the device
0386  *
0387  *  Description:
0388  *   Hardware conditions may dictate a need to stop all pending requests.
0389  *   In this case, we will safely clear the block side of the tag queue and
0390  *   readd all requests to the request queue in the right order.
0391  *
0392  *  Notes:
0393  *   queue lock must be held.
0394  **/
0395 void blk_queue_invalidate_tags(struct request_queue *q)
0396 {
0397     struct list_head *tmp, *n;
0398 
0399     list_for_each_safe(tmp, n, &q->tag_busy_list)
0400         blk_requeue_request(q, list_entry_rq(tmp));
0401 }
0402 EXPORT_SYMBOL(blk_queue_invalidate_tags);