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 on all targets */
109 for (; tt < te && (t = *tt); tt++) {
110 for (i = 0; i < NFACTIVE; i++) {
111 head = &t->factive[i];
112 list_for_each_safe(pos, nx, head) {
114 f = list_entry(pos, struct frame, head);
116 f->buf->nframesout--;
117 aoe_failbuf(d, f->buf);
122 t->maxout = t->nframes;
126 /* clean out the in-process request (if any) */
130 /* fast fail all pending I/O */
132 while ((rq = blk_peek_request(d->blkq))) {
133 blk_start_request(rq);
134 aoe_end_request(d, rq, 1);
139 set_capacity(d->gd, 0);
143 aoedev_freedev(struct aoedev *d)
145 struct aoetgt **t, **e;
147 cancel_work_sync(&d->work);
152 blk_cleanup_queue(d->blkq);
156 for (; t < e && *t; t++)
159 mempool_destroy(d->bufpool);
165 aoedev_flush(const char __user *str, size_t cnt)
168 struct aoedev *d, **dd;
169 struct aoedev *rmd = NULL;
174 if (cnt > sizeof buf)
176 if (copy_from_user(buf, str, cnt))
178 all = !strncmp(buf, "all", 3);
181 spin_lock_irqsave(&devlist_lock, flags);
185 if ((!all && (d->flags & DEVFL_UP))
186 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
189 spin_unlock(&d->lock);
195 d->flags |= DEVFL_TKILL;
196 spin_unlock(&d->lock);
200 spin_unlock_irqrestore(&devlist_lock, flags);
203 del_timer_sync(&d->timer);
204 aoedev_freedev(d); /* must be able to sleep */
209 /* This has been confirmed to occur once with Tms=3*1000 due to the
210 * driver changing link and not processing its transmit ring. The
211 * problem is hard enough to solve by returning an error that I'm
212 * still punting on "solving" this.
215 skbfree(struct sk_buff *skb)
217 enum { Sms = 250, Tms = 30 * 1000};
222 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
226 "aoe: %s holds ref: %s\n",
227 skb->dev ? skb->dev->name : "netif",
228 "cannot free skb -- memory leaked.");
231 skb->truesize -= skb->data_len;
232 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
238 skbpoolfree(struct aoedev *d)
240 struct sk_buff *skb, *tmp;
242 skb_queue_walk_safe(&d->skbpool, skb, tmp)
245 __skb_queue_head_init(&d->skbpool);
248 /* find it or malloc it */
250 aoedev_by_sysminor_m(ulong sysminor)
255 spin_lock_irqsave(&devlist_lock, flags);
257 for (d=devlist; d; d=d->next)
258 if (d->sysminor == sysminor) {
264 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
267 INIT_WORK(&d->work, aoecmd_sleepwork);
268 spin_lock_init(&d->lock);
269 skb_queue_head_init(&d->skbpool);
270 init_timer(&d->timer);
271 d->timer.data = (ulong) d;
272 d->timer.function = dummy_timer;
273 d->timer.expires = jiffies + HZ;
274 add_timer(&d->timer);
275 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
278 d->sysminor = sysminor;
279 d->aoemajor = AOEMAJOR(sysminor);
280 d->aoeminor = AOEMINOR(sysminor);
281 d->mintimer = MINTIMER;
285 spin_unlock_irqrestore(&devlist_lock, flags);
290 freetgt(struct aoedev *d, struct aoetgt *t)
293 struct list_head *pos, *nx, *head;
296 list_for_each_safe(pos, nx, head) {
298 f = list_entry(pos, struct frame, head);
312 while ((d = devlist)) {
315 spin_lock_irqsave(&d->lock, flags);
317 d->flags |= DEVFL_TKILL;
318 spin_unlock_irqrestore(&d->lock, flags);
320 del_timer_sync(&d->timer);