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>
11 #include <linux/slab.h>
14 static void dummy_timer(ulong);
15 static void aoedev_freedev(struct aoedev *);
16 static void freetgt(struct aoedev *d, struct aoetgt *t);
17 static void skbpoolfree(struct aoedev *d);
19 static struct aoedev *devlist;
20 static DEFINE_SPINLOCK(devlist_lock);
23 * Users who grab a pointer to the device with aoedev_by_aoeaddr or
24 * aoedev_by_sysminor_m automatically get a reference count and must
25 * be responsible for performing a aoedev_put. With the addition of
26 * async kthread processing I'm no longer confident that we can
27 * guarantee consistency in the face of device flushes.
29 * For the time being, we only bother to add extra references for
30 * frames sitting on the iocq. When the kthreads finish processing
31 * these frames, they will aoedev_put the device.
34 aoedev_by_aoeaddr(int maj, int min)
39 spin_lock_irqsave(&devlist_lock, flags);
41 for (d=devlist; d; d=d->next)
42 if (d->aoemajor == maj && d->aoeminor == min) {
47 spin_unlock_irqrestore(&devlist_lock, flags);
52 aoedev_put(struct aoedev *d)
56 spin_lock_irqsave(&devlist_lock, flags);
58 spin_unlock_irqrestore(&devlist_lock, flags);
66 d = (struct aoedev *)vp;
67 if (d->flags & DEVFL_TKILL)
69 d->timer.expires = jiffies + HZ;
74 aoe_failip(struct aoedev *d)
80 aoe_failbuf(d, d->ip.buf);
85 while ((bio = d->ip.nxbio)) {
86 clear_bit(BIO_UPTODATE, &bio->bi_flags);
87 d->ip.nxbio = bio->bi_next;
88 n = (unsigned long) rq->special;
89 rq->special = (void *) --n;
91 if ((unsigned long) rq->special == 0)
92 aoe_end_request(d, rq, 0);
96 aoedev_downdev(struct aoedev *d)
98 struct aoetgt *t, **tt, **te;
100 struct list_head *head, *pos, *nx;
104 d->flags &= ~DEVFL_UP;
106 /* clean out active buffers */
107 for (i = 0; i < NFACTIVE; i++) {
108 head = &d->factive[i];
109 list_for_each_safe(pos, nx, head) {
110 f = list_entry(pos, struct frame, head);
113 f->buf->nframesout--;
114 aoe_failbuf(d, f->buf);
119 /* reset window dressings */
122 for (; tt < te && (t = *tt); tt++) {
123 t->maxout = t->nframes;
127 /* clean out the in-process request (if any) */
131 /* fast fail all pending I/O */
133 while ((rq = blk_peek_request(d->blkq))) {
134 blk_start_request(rq);
135 aoe_end_request(d, rq, 1);
140 set_capacity(d->gd, 0);
144 aoedev_freedev(struct aoedev *d)
146 struct aoetgt **t, **e;
148 cancel_work_sync(&d->work);
153 blk_cleanup_queue(d->blkq);
157 for (; t < e && *t; t++)
160 mempool_destroy(d->bufpool);
166 aoedev_flush(const char __user *str, size_t cnt)
169 struct aoedev *d, **dd;
170 struct aoedev *rmd = NULL;
175 if (cnt > sizeof buf)
177 if (copy_from_user(buf, str, cnt))
179 all = !strncmp(buf, "all", 3);
182 spin_lock_irqsave(&devlist_lock, flags);
186 if ((!all && (d->flags & DEVFL_UP))
187 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
190 spin_unlock(&d->lock);
196 d->flags |= DEVFL_TKILL;
197 spin_unlock(&d->lock);
201 spin_unlock_irqrestore(&devlist_lock, flags);
204 del_timer_sync(&d->timer);
205 aoedev_freedev(d); /* must be able to sleep */
210 /* This has been confirmed to occur once with Tms=3*1000 due to the
211 * driver changing link and not processing its transmit ring. The
212 * problem is hard enough to solve by returning an error that I'm
213 * still punting on "solving" this.
216 skbfree(struct sk_buff *skb)
218 enum { Sms = 250, Tms = 30 * 1000};
223 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
227 "aoe: %s holds ref: %s\n",
228 skb->dev ? skb->dev->name : "netif",
229 "cannot free skb -- memory leaked.");
232 skb->truesize -= skb->data_len;
233 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
239 skbpoolfree(struct aoedev *d)
241 struct sk_buff *skb, *tmp;
243 skb_queue_walk_safe(&d->skbpool, skb, tmp)
246 __skb_queue_head_init(&d->skbpool);
249 /* find it or malloc it */
251 aoedev_by_sysminor_m(ulong sysminor)
257 spin_lock_irqsave(&devlist_lock, flags);
259 for (d=devlist; d; d=d->next)
260 if (d->sysminor == sysminor) {
266 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
269 INIT_WORK(&d->work, aoecmd_sleepwork);
270 spin_lock_init(&d->lock);
271 skb_queue_head_init(&d->skbpool);
272 init_timer(&d->timer);
273 d->timer.data = (ulong) d;
274 d->timer.function = dummy_timer;
275 d->timer.expires = jiffies + HZ;
276 add_timer(&d->timer);
277 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
280 for (i = 0; i < NFACTIVE; i++)
281 INIT_LIST_HEAD(&d->factive[i]);
282 d->sysminor = sysminor;
283 d->aoemajor = AOEMAJOR(sysminor);
284 d->aoeminor = AOEMINOR(sysminor);
285 d->mintimer = MINTIMER;
289 spin_unlock_irqrestore(&devlist_lock, flags);
294 freetgt(struct aoedev *d, struct aoetgt *t)
297 struct list_head *pos, *nx, *head;
300 for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
307 list_for_each_safe(pos, nx, head) {
309 f = list_entry(pos, struct frame, head);
323 while ((d = devlist)) {
326 spin_lock_irqsave(&d->lock, flags);
328 d->flags |= DEVFL_TKILL;
329 spin_unlock_irqrestore(&d->lock, flags);
331 del_timer_sync(&d->timer);