1 /* Copyright (c) 2007 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>
13 static void dummy_timer(ulong);
14 static void aoedev_freedev(struct aoedev *);
15 static void freetgt(struct aoedev *d, struct aoetgt *t);
16 static void skbpoolfree(struct aoedev *d);
18 static struct aoedev *devlist;
19 static DEFINE_SPINLOCK(devlist_lock);
22 aoedev_by_aoeaddr(int maj, int min)
27 spin_lock_irqsave(&devlist_lock, flags);
29 for (d=devlist; d; d=d->next)
30 if (d->aoemajor == maj && d->aoeminor == min)
33 spin_unlock_irqrestore(&devlist_lock, flags);
42 d = (struct aoedev *)vp;
43 if (d->flags & DEVFL_TKILL)
45 d->timer.expires = jiffies + HZ;
50 aoedev_downdev(struct aoedev *d)
52 struct aoetgt **t, **te;
59 for (; t < te && *t; t++) {
61 e = f + (*t)->nframes;
62 for (; f < e; f->tag = FREETAG, f->buf = NULL, f++) {
63 if (f->tag == FREETAG || f->buf == NULL)
67 if (--buf->nframesout == 0
68 && buf != d->inprocess) {
69 mempool_free(buf, d->bufpool);
73 (*t)->maxout = (*t)->nframes;
79 mempool_free(buf, d->bufpool);
85 while (!list_empty(&d->bufq)) {
86 buf = container_of(d->bufq.next, struct buf, bufs);
87 list_del(d->bufq.next);
89 mempool_free(buf, d->bufpool);
96 d->flags &= ~DEVFL_UP;
100 aoedev_freedev(struct aoedev *d)
102 struct aoetgt **t, **e;
111 for (; t < e && *t; t++)
114 mempool_destroy(d->bufpool);
120 aoedev_flush(const char __user *str, size_t cnt)
123 struct aoedev *d, **dd;
124 struct aoedev *rmd = NULL;
129 if (cnt > sizeof buf)
131 if (copy_from_user(buf, str, cnt))
133 all = !strncmp(buf, "all", 3);
136 flush_scheduled_work();
137 spin_lock_irqsave(&devlist_lock, flags);
141 if ((!all && (d->flags & DEVFL_UP))
142 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
144 spin_unlock(&d->lock);
150 d->flags |= DEVFL_TKILL;
151 spin_unlock(&d->lock);
155 spin_unlock_irqrestore(&devlist_lock, flags);
158 del_timer_sync(&d->timer);
159 aoedev_freedev(d); /* must be able to sleep */
164 /* I'm not really sure that this is a realistic problem, but if the
165 network driver goes gonzo let's just leak memory after complaining. */
167 skbfree(struct sk_buff *skb)
169 enum { Sms = 100, Tms = 3*1000};
174 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
178 "aoe: %s holds ref: %s\n",
179 skb->dev ? skb->dev->name : "netif",
180 "cannot free skb -- memory leaked.");
183 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
189 skbpoolfree(struct aoedev *d)
193 while ((skb = d->skbpool_hd)) {
194 d->skbpool_hd = skb->next;
198 d->skbpool_tl = NULL;
201 /* find it or malloc it */
203 aoedev_by_sysminor_m(ulong sysminor)
208 spin_lock_irqsave(&devlist_lock, flags);
210 for (d=devlist; d; d=d->next)
211 if (d->sysminor == sysminor)
215 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
218 INIT_WORK(&d->work, aoecmd_sleepwork);
219 spin_lock_init(&d->lock);
220 init_timer(&d->timer);
221 d->timer.data = (ulong) d;
222 d->timer.function = dummy_timer;
223 d->timer.expires = jiffies + HZ;
224 add_timer(&d->timer);
225 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
227 INIT_LIST_HEAD(&d->bufq);
228 d->sysminor = sysminor;
229 d->aoemajor = AOEMAJOR(sysminor);
230 d->aoeminor = AOEMINOR(sysminor);
231 d->mintimer = MINTIMER;
235 spin_unlock_irqrestore(&devlist_lock, flags);
240 freetgt(struct aoedev *d, struct aoetgt *t)
258 flush_scheduled_work();
260 while ((d = devlist)) {
263 spin_lock_irqsave(&d->lock, flags);
265 d->flags |= DEVFL_TKILL;
266 spin_unlock_irqrestore(&d->lock, flags);
268 del_timer_sync(&d->timer);