1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE device utility functions; maintains device list.
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/netdevice.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/bitmap.h>
13 #include <linux/kdev_t.h>
14 #include <linux/moduleparam.h>
17 static void dummy_timer(ulong);
18 static void aoedev_freedev(struct aoedev *);
19 static void freetgt(struct aoedev *d, struct aoetgt *t);
20 static void skbpoolfree(struct aoedev *d);
22 static int aoe_dyndevs = 1;
23 module_param(aoe_dyndevs, int, 0644);
24 MODULE_PARM_DESC(aoe_dyndevs, "Use dynamic minor numbers for devices.");
26 static struct aoedev *devlist;
27 static DEFINE_SPINLOCK(devlist_lock);
29 /* Because some systems will have one, many, or no
33 * we need some flexibility in the way the minor numbers
34 * are allocated. So they are dynamic.
36 #define N_DEVS ((1U<<MINORBITS)/AOE_PARTITIONS)
38 static DEFINE_SPINLOCK(used_minors_lock);
39 static DECLARE_BITMAP(used_minors, N_DEVS);
42 minor_get_dyn(ulong *sysminor)
48 spin_lock_irqsave(&used_minors_lock, flags);
49 n = find_first_zero_bit(used_minors, N_DEVS);
51 set_bit(n, used_minors);
54 spin_unlock_irqrestore(&used_minors_lock, flags);
56 *sysminor = n * AOE_PARTITIONS;
61 minor_get_static(ulong *sysminor, ulong aoemaj, int aoemin)
67 /* for backwards compatibility when !aoe_dyndevs,
68 * a static number of supported slots per shelf */
72 n = aoemaj * NPERSHELF + aoemin;
73 if (aoemin >= NPERSHELF || n >= N_DEVS) {
74 pr_err("aoe: %s with e%ld.%d\n",
75 "cannot use static minor device numbers",
79 spin_lock_irqsave(&used_minors_lock, flags);
80 if (test_bit(n, used_minors)) {
81 pr_err("aoe: %s %lu\n",
82 "existing device already has static minor number",
86 set_bit(n, used_minors);
87 spin_unlock_irqrestore(&used_minors_lock, flags);
95 minor_get(ulong *sysminor, ulong aoemaj, int aoemin)
98 return minor_get_dyn(sysminor);
100 return minor_get_static(sysminor, aoemaj, aoemin);
104 minor_free(ulong minor)
108 minor /= AOE_PARTITIONS;
109 BUG_ON(minor >= N_DEVS);
111 spin_lock_irqsave(&used_minors_lock, flags);
112 BUG_ON(!test_bit(minor, used_minors));
113 clear_bit(minor, used_minors);
114 spin_unlock_irqrestore(&used_minors_lock, flags);
118 * Users who grab a pointer to the device with aoedev_by_aoeaddr
119 * automatically get a reference count and must be responsible
120 * for performing a aoedev_put. With the addition of async
121 * kthread processing I'm no longer confident that we can
122 * guarantee consistency in the face of device flushes.
124 * For the time being, we only bother to add extra references for
125 * frames sitting on the iocq. When the kthreads finish processing
126 * these frames, they will aoedev_put the device.
130 aoedev_put(struct aoedev *d)
134 spin_lock_irqsave(&devlist_lock, flags);
136 spin_unlock_irqrestore(&devlist_lock, flags);
140 dummy_timer(ulong vp)
144 d = (struct aoedev *)vp;
145 if (d->flags & DEVFL_TKILL)
147 d->timer.expires = jiffies + HZ;
148 add_timer(&d->timer);
152 aoe_failip(struct aoedev *d)
158 aoe_failbuf(d, d->ip.buf);
163 while ((bio = d->ip.nxbio)) {
164 clear_bit(BIO_UPTODATE, &bio->bi_flags);
165 d->ip.nxbio = bio->bi_next;
166 n = (unsigned long) rq->special;
167 rq->special = (void *) --n;
169 if ((unsigned long) rq->special == 0)
170 aoe_end_request(d, rq, 0);
174 downdev_frame(struct list_head *pos)
178 f = list_entry(pos, struct frame, head);
181 f->buf->nframesout--;
182 aoe_failbuf(f->t->d, f->buf);
188 aoedev_downdev(struct aoedev *d)
190 struct aoetgt *t, **tt, **te;
191 struct list_head *head, *pos, *nx;
195 d->flags &= ~DEVFL_UP;
197 /* clean out active and to-be-retransmitted buffers */
198 for (i = 0; i < NFACTIVE; i++) {
199 head = &d->factive[i];
200 list_for_each_safe(pos, nx, head)
204 list_for_each_safe(pos, nx, head)
207 /* reset window dressings */
210 for (; tt < te && (t = *tt); tt++) {
215 /* clean out the in-process request (if any) */
219 /* fast fail all pending I/O */
221 while ((rq = blk_peek_request(d->blkq))) {
222 blk_start_request(rq);
223 aoe_end_request(d, rq, 1);
228 set_capacity(d->gd, 0);
232 aoedev_freedev(struct aoedev *d)
234 struct aoetgt **t, **e;
236 cancel_work_sync(&d->work);
241 blk_cleanup_queue(d->blkq);
245 for (; t < e && *t; t++)
248 mempool_destroy(d->bufpool);
250 minor_free(d->sysminor);
254 /* return whether the user asked for this particular
255 * device to be flushed
258 user_req(char *s, size_t slen, struct aoedev *d)
265 p = strrchr(d->gd->disk_name, '/');
267 p = d->gd->disk_name;
270 lim = sizeof(d->gd->disk_name);
271 lim -= p - d->gd->disk_name;
275 return !strncmp(s, p, lim);
279 aoedev_flush(const char __user *str, size_t cnt)
282 struct aoedev *d, **dd;
283 struct aoedev *rmd = NULL;
286 int specified = 0; /* flush a specific device */
289 if (cnt > sizeof buf)
291 if (copy_from_user(buf, str, cnt))
293 all = !strncmp(buf, "all", 3);
298 spin_lock_irqsave(&devlist_lock, flags);
303 if (!user_req(buf, cnt, d))
305 } else if ((!all && (d->flags & DEVFL_UP))
306 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
313 d->flags |= DEVFL_TKILL;
314 spin_unlock(&d->lock);
319 spin_unlock(&d->lock);
322 spin_unlock_irqrestore(&devlist_lock, flags);
325 del_timer_sync(&d->timer);
326 aoedev_freedev(d); /* must be able to sleep */
331 /* This has been confirmed to occur once with Tms=3*1000 due to the
332 * driver changing link and not processing its transmit ring. The
333 * problem is hard enough to solve by returning an error that I'm
334 * still punting on "solving" this.
337 skbfree(struct sk_buff *skb)
339 enum { Sms = 250, Tms = 30 * 1000};
344 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
348 "aoe: %s holds ref: %s\n",
349 skb->dev ? skb->dev->name : "netif",
350 "cannot free skb -- memory leaked.");
353 skb->truesize -= skb->data_len;
354 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
360 skbpoolfree(struct aoedev *d)
362 struct sk_buff *skb, *tmp;
364 skb_queue_walk_safe(&d->skbpool, skb, tmp)
367 __skb_queue_head_init(&d->skbpool);
370 /* find it or allocate it */
372 aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
379 spin_lock_irqsave(&devlist_lock, flags);
381 for (d=devlist; d; d=d->next)
382 if (d->aoemajor == maj && d->aoeminor == min) {
386 if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
388 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
391 INIT_WORK(&d->work, aoecmd_sleepwork);
392 spin_lock_init(&d->lock);
393 skb_queue_head_init(&d->skbpool);
394 init_timer(&d->timer);
395 d->timer.data = (ulong) d;
396 d->timer.function = dummy_timer;
397 d->timer.expires = jiffies + HZ;
398 add_timer(&d->timer);
399 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
402 for (i = 0; i < NFACTIVE; i++)
403 INIT_LIST_HEAD(&d->factive[i]);
404 INIT_LIST_HEAD(&d->rexmitq);
405 d->sysminor = sysminor;
408 d->rttavg = RTTAVG_INIT;
409 d->rttdev = RTTDEV_INIT;
413 spin_unlock_irqrestore(&devlist_lock, flags);
418 freetgt(struct aoedev *d, struct aoetgt *t)
421 struct list_head *pos, *nx, *head;
424 for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
431 list_for_each_safe(pos, nx, head) {
433 f = list_entry(pos, struct frame, head);
447 while ((d = devlist)) {
450 spin_lock_irqsave(&d->lock, flags);
452 d->flags |= DEVFL_TKILL;
453 spin_unlock_irqrestore(&d->lock, flags);
455 del_timer_sync(&d->timer);