xref: /openbmc/linux/drivers/block/aoe/aoedev.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1ca47bbd9SEd Cashin /* Copyright (c) 2013 Coraid, Inc.  See COPYING for GPL terms. */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * aoedev.c
41da177e4SLinus Torvalds  * AoE device utility functions; maintains device list.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/hdreg.h>
83582dd29SJens Axboe #include <linux/blk-mq.h>
91da177e4SLinus Torvalds #include <linux/netdevice.h>
109bb237b6SEd L. Cashin #include <linux/delay.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
120c966214SEd Cashin #include <linux/bitmap.h>
130c966214SEd Cashin #include <linux/kdev_t.h>
144bcce1a3SEd Cashin #include <linux/moduleparam.h>
15a88c1f0cSAndy Shevchenko #include <linux/string.h>
161da177e4SLinus Torvalds #include "aoe.h"
171da177e4SLinus Torvalds 
189bb237b6SEd L. Cashin static void freetgt(struct aoedev *d, struct aoetgt *t);
199bb237b6SEd L. Cashin static void skbpoolfree(struct aoedev *d);
20262bf541SEd L. Cashin 
2108b60623SEd Cashin static int aoe_dyndevs = 1;
224bcce1a3SEd Cashin module_param(aoe_dyndevs, int, 0644);
234bcce1a3SEd Cashin MODULE_PARM_DESC(aoe_dyndevs, "Use dynamic minor numbers for devices.");
244bcce1a3SEd Cashin 
251da177e4SLinus Torvalds static struct aoedev *devlist;
26476aed38SAndrew Morton static DEFINE_SPINLOCK(devlist_lock);
271da177e4SLinus Torvalds 
280c966214SEd Cashin /* Because some systems will have one, many, or no
290c966214SEd Cashin  *   - partitions,
300c966214SEd Cashin  *   - slots per shelf,
310c966214SEd Cashin  *   - or shelves,
320c966214SEd Cashin  * we need some flexibility in the way the minor numbers
330c966214SEd Cashin  * are allocated.  So they are dynamic.
340c966214SEd Cashin  */
350c966214SEd Cashin #define N_DEVS ((1U<<MINORBITS)/AOE_PARTITIONS)
360c966214SEd Cashin 
370c966214SEd Cashin static DEFINE_SPINLOCK(used_minors_lock);
380c966214SEd Cashin static DECLARE_BITMAP(used_minors, N_DEVS);
390c966214SEd Cashin 
400c966214SEd Cashin static int
minor_get_dyn(ulong * sysminor)414bcce1a3SEd Cashin minor_get_dyn(ulong *sysminor)
420c966214SEd Cashin {
430c966214SEd Cashin 	ulong flags;
440c966214SEd Cashin 	ulong n;
450c966214SEd Cashin 	int error = 0;
460c966214SEd Cashin 
470c966214SEd Cashin 	spin_lock_irqsave(&used_minors_lock, flags);
480c966214SEd Cashin 	n = find_first_zero_bit(used_minors, N_DEVS);
490c966214SEd Cashin 	if (n < N_DEVS)
500c966214SEd Cashin 		set_bit(n, used_minors);
510c966214SEd Cashin 	else
520c966214SEd Cashin 		error = -1;
530c966214SEd Cashin 	spin_unlock_irqrestore(&used_minors_lock, flags);
540c966214SEd Cashin 
554bcce1a3SEd Cashin 	*sysminor = n * AOE_PARTITIONS;
560c966214SEd Cashin 	return error;
570c966214SEd Cashin }
580c966214SEd Cashin 
594bcce1a3SEd Cashin static int
minor_get_static(ulong * sysminor,ulong aoemaj,int aoemin)604bcce1a3SEd Cashin minor_get_static(ulong *sysminor, ulong aoemaj, int aoemin)
614bcce1a3SEd Cashin {
624bcce1a3SEd Cashin 	ulong flags;
634bcce1a3SEd Cashin 	ulong n;
644bcce1a3SEd Cashin 	int error = 0;
654bcce1a3SEd Cashin 	enum {
664bcce1a3SEd Cashin 		/* for backwards compatibility when !aoe_dyndevs,
674bcce1a3SEd Cashin 		 * a static number of supported slots per shelf */
684bcce1a3SEd Cashin 		NPERSHELF = 16,
694bcce1a3SEd Cashin 	};
704bcce1a3SEd Cashin 
71e0b2bbabSEd Cashin 	if (aoemin >= NPERSHELF) {
72e0b2bbabSEd Cashin 		pr_err("aoe: %s %d slots per shelf\n",
73e0b2bbabSEd Cashin 			"static minor device numbers support only",
74e0b2bbabSEd Cashin 			NPERSHELF);
75e0b2bbabSEd Cashin 		error = -1;
76e0b2bbabSEd Cashin 		goto out;
77e0b2bbabSEd Cashin 	}
78e0b2bbabSEd Cashin 
794bcce1a3SEd Cashin 	n = aoemaj * NPERSHELF + aoemin;
80e0b2bbabSEd Cashin 	if (n >= N_DEVS) {
814bcce1a3SEd Cashin 		pr_err("aoe: %s with e%ld.%d\n",
824bcce1a3SEd Cashin 			"cannot use static minor device numbers",
834bcce1a3SEd Cashin 			aoemaj, aoemin);
844bcce1a3SEd Cashin 		error = -1;
85e0b2bbabSEd Cashin 		goto out;
86e0b2bbabSEd Cashin 	}
87e0b2bbabSEd Cashin 
884bcce1a3SEd Cashin 	spin_lock_irqsave(&used_minors_lock, flags);
894bcce1a3SEd Cashin 	if (test_bit(n, used_minors)) {
904bcce1a3SEd Cashin 		pr_err("aoe: %s %lu\n",
914bcce1a3SEd Cashin 			"existing device already has static minor number",
924bcce1a3SEd Cashin 			n);
934bcce1a3SEd Cashin 		error = -1;
944bcce1a3SEd Cashin 	} else
954bcce1a3SEd Cashin 		set_bit(n, used_minors);
964bcce1a3SEd Cashin 	spin_unlock_irqrestore(&used_minors_lock, flags);
97b91316f2SEd Cashin 	*sysminor = n * AOE_PARTITIONS;
98e0b2bbabSEd Cashin out:
994bcce1a3SEd Cashin 	return error;
1004bcce1a3SEd Cashin }
1014bcce1a3SEd Cashin 
1024bcce1a3SEd Cashin static int
minor_get(ulong * sysminor,ulong aoemaj,int aoemin)1034bcce1a3SEd Cashin minor_get(ulong *sysminor, ulong aoemaj, int aoemin)
1044bcce1a3SEd Cashin {
1054bcce1a3SEd Cashin 	if (aoe_dyndevs)
1064bcce1a3SEd Cashin 		return minor_get_dyn(sysminor);
1074bcce1a3SEd Cashin 	else
1084bcce1a3SEd Cashin 		return minor_get_static(sysminor, aoemaj, aoemin);
1094bcce1a3SEd Cashin }
1104bcce1a3SEd Cashin 
1110c966214SEd Cashin static void
minor_free(ulong minor)1120c966214SEd Cashin minor_free(ulong minor)
1130c966214SEd Cashin {
1140c966214SEd Cashin 	ulong flags;
1150c966214SEd Cashin 
1160c966214SEd Cashin 	minor /= AOE_PARTITIONS;
1170c966214SEd Cashin 	BUG_ON(minor >= N_DEVS);
1180c966214SEd Cashin 
1190c966214SEd Cashin 	spin_lock_irqsave(&used_minors_lock, flags);
1200c966214SEd Cashin 	BUG_ON(!test_bit(minor, used_minors));
1210c966214SEd Cashin 	clear_bit(minor, used_minors);
1220c966214SEd Cashin 	spin_unlock_irqrestore(&used_minors_lock, flags);
1230c966214SEd Cashin }
1240c966214SEd Cashin 
12569cf2d85SEd Cashin /*
1260c966214SEd Cashin  * Users who grab a pointer to the device with aoedev_by_aoeaddr
1270c966214SEd Cashin  * automatically get a reference count and must be responsible
1280c966214SEd Cashin  * for performing a aoedev_put.  With the addition of async
1290c966214SEd Cashin  * kthread processing I'm no longer confident that we can
13069cf2d85SEd Cashin  * guarantee consistency in the face of device flushes.
13169cf2d85SEd Cashin  *
13269cf2d85SEd Cashin  * For the time being, we only bother to add extra references for
13369cf2d85SEd Cashin  * frames sitting on the iocq.  When the kthreads finish processing
13469cf2d85SEd Cashin  * these frames, they will aoedev_put the device.
13569cf2d85SEd Cashin  */
1361da177e4SLinus Torvalds 
13769cf2d85SEd Cashin void
aoedev_put(struct aoedev * d)13869cf2d85SEd Cashin aoedev_put(struct aoedev *d)
13969cf2d85SEd Cashin {
14069cf2d85SEd Cashin 	ulong flags;
14169cf2d85SEd Cashin 
14269cf2d85SEd Cashin 	spin_lock_irqsave(&devlist_lock, flags);
14369cf2d85SEd Cashin 	d->ref--;
14469cf2d85SEd Cashin 	spin_unlock_irqrestore(&devlist_lock, flags);
14569cf2d85SEd Cashin }
14669cf2d85SEd Cashin 
1473ae1c24eSEd L. Cashin static void
dummy_timer(struct timer_list * t)1480e0cc9dfSKees Cook dummy_timer(struct timer_list *t)
1493ae1c24eSEd L. Cashin {
1503ae1c24eSEd L. Cashin 	struct aoedev *d;
1513ae1c24eSEd L. Cashin 
1520e0cc9dfSKees Cook 	d = from_timer(d, t, timer);
1533ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_TKILL)
1543ae1c24eSEd L. Cashin 		return;
1553ae1c24eSEd L. Cashin 	d->timer.expires = jiffies + HZ;
1563ae1c24eSEd L. Cashin 	add_timer(&d->timer);
1573ae1c24eSEd L. Cashin }
1583ae1c24eSEd L. Cashin 
15969cf2d85SEd Cashin static void
aoe_failip(struct aoedev * d)16069cf2d85SEd Cashin aoe_failip(struct aoedev *d)
1611da177e4SLinus Torvalds {
16269cf2d85SEd Cashin 	struct request *rq;
16361e7712eSChristoph Hellwig 	struct aoe_req *req;
1641da177e4SLinus Torvalds 	struct bio *bio;
1651da177e4SLinus Torvalds 
16669cf2d85SEd Cashin 	aoe_failbuf(d, d->ip.buf);
16769cf2d85SEd Cashin 	rq = d->ip.rq;
16869cf2d85SEd Cashin 	if (rq == NULL)
169896831f5SEd Cashin 		return;
17061e7712eSChristoph Hellwig 
17161e7712eSChristoph Hellwig 	req = blk_mq_rq_to_pdu(rq);
17269cf2d85SEd Cashin 	while ((bio = d->ip.nxbio)) {
1734e4cbee9SChristoph Hellwig 		bio->bi_status = BLK_STS_IOERR;
17469cf2d85SEd Cashin 		d->ip.nxbio = bio->bi_next;
17561e7712eSChristoph Hellwig 		req->nr_bios--;
1761da177e4SLinus Torvalds 	}
17761e7712eSChristoph Hellwig 
17861e7712eSChristoph Hellwig 	if (!req->nr_bios)
17969cf2d85SEd Cashin 		aoe_end_request(d, rq, 0);
180896831f5SEd Cashin }
181896831f5SEd Cashin 
1823fc9b032SEd Cashin static void
downdev_frame(struct list_head * pos)1833fc9b032SEd Cashin downdev_frame(struct list_head *pos)
1843fc9b032SEd Cashin {
1853fc9b032SEd Cashin 	struct frame *f;
1863fc9b032SEd Cashin 
1873fc9b032SEd Cashin 	f = list_entry(pos, struct frame, head);
1883fc9b032SEd Cashin 	list_del(pos);
1893fc9b032SEd Cashin 	if (f->buf) {
1903fc9b032SEd Cashin 		f->buf->nframesout--;
1913fc9b032SEd Cashin 		aoe_failbuf(f->t->d, f->buf);
1923fc9b032SEd Cashin 	}
1933fc9b032SEd Cashin 	aoe_freetframe(f);
1943fc9b032SEd Cashin }
1953fc9b032SEd Cashin 
196896831f5SEd Cashin void
aoedev_downdev(struct aoedev * d)197896831f5SEd Cashin aoedev_downdev(struct aoedev *d)
198896831f5SEd Cashin {
199896831f5SEd Cashin 	struct aoetgt *t, **tt, **te;
200896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
201896831f5SEd Cashin 	int i;
202896831f5SEd Cashin 
20369cf2d85SEd Cashin 	d->flags &= ~DEVFL_UP;
20469cf2d85SEd Cashin 
2053fc9b032SEd Cashin 	/* clean out active and to-be-retransmitted buffers */
206896831f5SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
20764a80f5aSEd Cashin 		head = &d->factive[i];
2083fc9b032SEd Cashin 		list_for_each_safe(pos, nx, head)
2093fc9b032SEd Cashin 			downdev_frame(pos);
210896831f5SEd Cashin 	}
2113fc9b032SEd Cashin 	head = &d->rexmitq;
2123fc9b032SEd Cashin 	list_for_each_safe(pos, nx, head)
2133fc9b032SEd Cashin 		downdev_frame(pos);
2143fc9b032SEd Cashin 
21564a80f5aSEd Cashin 	/* reset window dressings */
21664a80f5aSEd Cashin 	tt = d->targets;
21771114ec4SEd Cashin 	te = tt + d->ntargets;
21864a80f5aSEd Cashin 	for (; tt < te && (t = *tt); tt++) {
2193a0c40d2SEd Cashin 		aoecmd_wreset(t);
220896831f5SEd Cashin 		t->nout = 0;
221896831f5SEd Cashin 	}
222896831f5SEd Cashin 
22369cf2d85SEd Cashin 	/* clean out the in-process request (if any) */
22469cf2d85SEd Cashin 	aoe_failip(d);
2251da177e4SLinus Torvalds 
22669cf2d85SEd Cashin 	/* fast fail all pending I/O */
22769cf2d85SEd Cashin 	if (d->blkq) {
2283582dd29SJens Axboe 		/* UP is cleared, freeze+quiesce to insure all are errored */
2293582dd29SJens Axboe 		blk_mq_freeze_queue(d->blkq);
2303582dd29SJens Axboe 		blk_mq_quiesce_queue(d->blkq);
2313582dd29SJens Axboe 		blk_mq_unquiesce_queue(d->blkq);
2323582dd29SJens Axboe 		blk_mq_unfreeze_queue(d->blkq);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (d->gd)
23680795aefSTejun Heo 		set_capacity(d->gd, 0);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2394ba9aa7fSEd Cashin /* return whether the user asked for this particular
2404ba9aa7fSEd Cashin  * device to be flushed
2414ba9aa7fSEd Cashin  */
2424ba9aa7fSEd Cashin static int
user_req(char * s,size_t slen,struct aoedev * d)2434ba9aa7fSEd Cashin user_req(char *s, size_t slen, struct aoedev *d)
2444ba9aa7fSEd Cashin {
245a88c1f0cSAndy Shevchenko 	const char *p;
2464ba9aa7fSEd Cashin 	size_t lim;
2474ba9aa7fSEd Cashin 
2484ba9aa7fSEd Cashin 	if (!d->gd)
2494ba9aa7fSEd Cashin 		return 0;
250a88c1f0cSAndy Shevchenko 	p = kbasename(d->gd->disk_name);
2514ba9aa7fSEd Cashin 	lim = sizeof(d->gd->disk_name);
2524ba9aa7fSEd Cashin 	lim -= p - d->gd->disk_name;
2534ba9aa7fSEd Cashin 	if (slen < lim)
2544ba9aa7fSEd Cashin 		lim = slen;
2554ba9aa7fSEd Cashin 
2564ba9aa7fSEd Cashin 	return !strncmp(s, p, lim);
2574ba9aa7fSEd Cashin }
2584ba9aa7fSEd Cashin 
259e52a2932SEd Cashin static void
freedev(struct aoedev * d)260e52a2932SEd Cashin freedev(struct aoedev *d)
261e52a2932SEd Cashin {
262e52a2932SEd Cashin 	struct aoetgt **t, **e;
263e52a2932SEd Cashin 	int freeing = 0;
264e52a2932SEd Cashin 	unsigned long flags;
265e52a2932SEd Cashin 
266e52a2932SEd Cashin 	spin_lock_irqsave(&d->lock, flags);
267e52a2932SEd Cashin 	if (d->flags & DEVFL_TKILL
268e52a2932SEd Cashin 	&& !(d->flags & DEVFL_FREEING)) {
269e52a2932SEd Cashin 		d->flags |= DEVFL_FREEING;
270e52a2932SEd Cashin 		freeing = 1;
271e52a2932SEd Cashin 	}
272e52a2932SEd Cashin 	spin_unlock_irqrestore(&d->lock, flags);
273e52a2932SEd Cashin 	if (!freeing)
274e52a2932SEd Cashin 		return;
275e52a2932SEd Cashin 
276e52a2932SEd Cashin 	del_timer_sync(&d->timer);
277e52a2932SEd Cashin 	if (d->gd) {
278e8866cf2SEd Cashin 		aoedisk_rm_debugfs(d);
279e52a2932SEd Cashin 		del_gendisk(d->gd);
280*8b9ab626SChristoph Hellwig 		put_disk(d->gd);
2813582dd29SJens Axboe 		blk_mq_free_tag_set(&d->tag_set);
282e52a2932SEd Cashin 	}
283e52a2932SEd Cashin 	t = d->targets;
28471114ec4SEd Cashin 	e = t + d->ntargets;
285e52a2932SEd Cashin 	for (; t < e && *t; t++)
286e52a2932SEd Cashin 		freetgt(d, *t);
28769daf897Szhong jiang 
288e52a2932SEd Cashin 	mempool_destroy(d->bufpool);
289e52a2932SEd Cashin 	skbpoolfree(d);
290e52a2932SEd Cashin 	minor_free(d->sysminor);
291e52a2932SEd Cashin 
292e52a2932SEd Cashin 	spin_lock_irqsave(&d->lock, flags);
293e52a2932SEd Cashin 	d->flags |= DEVFL_FREED;
294e52a2932SEd Cashin 	spin_unlock_irqrestore(&d->lock, flags);
295e52a2932SEd Cashin }
296e52a2932SEd Cashin 
297e52a2932SEd Cashin enum flush_parms {
298e52a2932SEd Cashin 	NOT_EXITING = 0,
299e52a2932SEd Cashin 	EXITING = 1,
300e52a2932SEd Cashin };
301e52a2932SEd Cashin 
302e52a2932SEd Cashin static int
flush(const char __user * str,size_t cnt,int exiting)303e52a2932SEd Cashin flush(const char __user *str, size_t cnt, int exiting)
304262bf541SEd L. Cashin {
305262bf541SEd L. Cashin 	ulong flags;
306262bf541SEd L. Cashin 	struct aoedev *d, **dd;
307262bf541SEd L. Cashin 	char buf[16];
308262bf541SEd L. Cashin 	int all = 0;
3094ba9aa7fSEd Cashin 	int specified = 0;	/* flush a specific device */
310e52a2932SEd Cashin 	unsigned int skipflags;
311262bf541SEd L. Cashin 
312e52a2932SEd Cashin 	skipflags = DEVFL_GDALLOC | DEVFL_NEWSIZE | DEVFL_TKILL;
313e52a2932SEd Cashin 
314e52a2932SEd Cashin 	if (!exiting && cnt >= 3) {
315262bf541SEd L. Cashin 		if (cnt > sizeof buf)
316262bf541SEd L. Cashin 			cnt = sizeof buf;
317262bf541SEd L. Cashin 		if (copy_from_user(buf, str, cnt))
318262bf541SEd L. Cashin 			return -EFAULT;
319262bf541SEd L. Cashin 		all = !strncmp(buf, "all", 3);
3204ba9aa7fSEd Cashin 		if (!all)
3214ba9aa7fSEd Cashin 			specified = 1;
322262bf541SEd L. Cashin 	}
323262bf541SEd L. Cashin 
3240b8d7622STetsuo Handa 	flush_workqueue(aoe_wq);
325430380b4SHe Zhe 	/* pass one: do aoedev_downdev, which might sleep */
326430380b4SHe Zhe restart1:
327262bf541SEd L. Cashin 	spin_lock_irqsave(&devlist_lock, flags);
328e52a2932SEd Cashin 	for (d = devlist; d; d = d->next) {
329262bf541SEd L. Cashin 		spin_lock(&d->lock);
330430380b4SHe Zhe 		if (d->flags & DEVFL_TKILL)
331430380b4SHe Zhe 			goto cont;
332430380b4SHe Zhe 
333e52a2932SEd Cashin 		if (exiting) {
334e52a2932SEd Cashin 			/* unconditionally take each device down */
335e52a2932SEd Cashin 		} else if (specified) {
3364ba9aa7fSEd Cashin 			if (!user_req(buf, cnt, d))
337e52a2932SEd Cashin 				goto cont;
3384ba9aa7fSEd Cashin 		} else if ((!all && (d->flags & DEVFL_UP))
339e52a2932SEd Cashin 		|| d->flags & skipflags
34069cf2d85SEd Cashin 		|| d->nopen
3414ba9aa7fSEd Cashin 		|| d->ref)
342e52a2932SEd Cashin 			goto cont;
3434ba9aa7fSEd Cashin 
344430380b4SHe Zhe 		spin_unlock(&d->lock);
345430380b4SHe Zhe 		spin_unlock_irqrestore(&devlist_lock, flags);
346262bf541SEd L. Cashin 		aoedev_downdev(d);
347262bf541SEd L. Cashin 		d->flags |= DEVFL_TKILL;
348430380b4SHe Zhe 		goto restart1;
349e52a2932SEd Cashin cont:
350262bf541SEd L. Cashin 		spin_unlock(&d->lock);
351262bf541SEd L. Cashin 	}
352262bf541SEd L. Cashin 	spin_unlock_irqrestore(&devlist_lock, flags);
353e52a2932SEd Cashin 
354e52a2932SEd Cashin 	/* pass two: call freedev, which might sleep,
355e52a2932SEd Cashin 	 * for aoedevs marked with DEVFL_TKILL
356e52a2932SEd Cashin 	 */
357430380b4SHe Zhe restart2:
358e52a2932SEd Cashin 	spin_lock_irqsave(&devlist_lock, flags);
359e52a2932SEd Cashin 	for (d = devlist; d; d = d->next) {
360e52a2932SEd Cashin 		spin_lock(&d->lock);
361e52a2932SEd Cashin 		if (d->flags & DEVFL_TKILL
362e52a2932SEd Cashin 		&& !(d->flags & DEVFL_FREEING)) {
363e52a2932SEd Cashin 			spin_unlock(&d->lock);
364e52a2932SEd Cashin 			spin_unlock_irqrestore(&devlist_lock, flags);
365e52a2932SEd Cashin 			freedev(d);
366430380b4SHe Zhe 			goto restart2;
367262bf541SEd L. Cashin 		}
368e52a2932SEd Cashin 		spin_unlock(&d->lock);
369e52a2932SEd Cashin 	}
370e52a2932SEd Cashin 
371e52a2932SEd Cashin 	/* pass three: remove aoedevs marked with DEVFL_FREED */
372e52a2932SEd Cashin 	for (dd = &devlist, d = *dd; d; d = *dd) {
373e52a2932SEd Cashin 		struct aoedev *doomed = NULL;
374e52a2932SEd Cashin 
375e52a2932SEd Cashin 		spin_lock(&d->lock);
376e52a2932SEd Cashin 		if (d->flags & DEVFL_FREED) {
377e52a2932SEd Cashin 			*dd = d->next;
378e52a2932SEd Cashin 			doomed = d;
379e52a2932SEd Cashin 		} else {
380e52a2932SEd Cashin 			dd = &d->next;
381e52a2932SEd Cashin 		}
382e52a2932SEd Cashin 		spin_unlock(&d->lock);
38371114ec4SEd Cashin 		if (doomed)
38471114ec4SEd Cashin 			kfree(doomed->targets);
385e52a2932SEd Cashin 		kfree(doomed);
386e52a2932SEd Cashin 	}
387e52a2932SEd Cashin 	spin_unlock_irqrestore(&devlist_lock, flags);
388e52a2932SEd Cashin 
389262bf541SEd L. Cashin 	return 0;
390262bf541SEd L. Cashin }
391262bf541SEd L. Cashin 
392e52a2932SEd Cashin int
aoedev_flush(const char __user * str,size_t cnt)393e52a2932SEd Cashin aoedev_flush(const char __user *str, size_t cnt)
394e52a2932SEd Cashin {
395e52a2932SEd Cashin 	return flush(str, cnt, NOT_EXITING);
396e52a2932SEd Cashin }
397e52a2932SEd Cashin 
39869cf2d85SEd Cashin /* This has been confirmed to occur once with Tms=3*1000 due to the
39969cf2d85SEd Cashin  * driver changing link and not processing its transmit ring.  The
40069cf2d85SEd Cashin  * problem is hard enough to solve by returning an error that I'm
40169cf2d85SEd Cashin  * still punting on "solving" this.
40269cf2d85SEd Cashin  */
4039bb237b6SEd L. Cashin static void
skbfree(struct sk_buff * skb)4049bb237b6SEd L. Cashin skbfree(struct sk_buff *skb)
4059bb237b6SEd L. Cashin {
40669cf2d85SEd Cashin 	enum { Sms = 250, Tms = 30 * 1000};
4079bb237b6SEd L. Cashin 	int i = Tms / Sms;
4089bb237b6SEd L. Cashin 
4099bb237b6SEd L. Cashin 	if (skb == NULL)
4109bb237b6SEd L. Cashin 		return;
4119bb237b6SEd L. Cashin 	while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
4129bb237b6SEd L. Cashin 		msleep(Sms);
41394873111SRoel Kluin 	if (i < 0) {
4149bb237b6SEd L. Cashin 		printk(KERN_ERR
4159bb237b6SEd L. Cashin 			"aoe: %s holds ref: %s\n",
4169bb237b6SEd L. Cashin 			skb->dev ? skb->dev->name : "netif",
4179bb237b6SEd L. Cashin 			"cannot free skb -- memory leaked.");
4189bb237b6SEd L. Cashin 		return;
4199bb237b6SEd L. Cashin 	}
4203d5b0605SEd Cashin 	skb->truesize -= skb->data_len;
4219bb237b6SEd L. Cashin 	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
4229bb237b6SEd L. Cashin 	skb_trim(skb, 0);
4239bb237b6SEd L. Cashin 	dev_kfree_skb(skb);
4249bb237b6SEd L. Cashin }
4259bb237b6SEd L. Cashin 
4269bb237b6SEd L. Cashin static void
skbpoolfree(struct aoedev * d)4279bb237b6SEd L. Cashin skbpoolfree(struct aoedev *d)
4289bb237b6SEd L. Cashin {
429e9bb8fb0SDavid S. Miller 	struct sk_buff *skb, *tmp;
4309bb237b6SEd L. Cashin 
431e9bb8fb0SDavid S. Miller 	skb_queue_walk_safe(&d->skbpool, skb, tmp)
4329bb237b6SEd L. Cashin 		skbfree(skb);
433e9bb8fb0SDavid S. Miller 
434e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&d->skbpool);
4359bb237b6SEd L. Cashin }
4369bb237b6SEd L. Cashin 
4370c966214SEd Cashin /* find it or allocate it */
4381da177e4SLinus Torvalds struct aoedev *
aoedev_by_aoeaddr(ulong maj,int min,int do_alloc)4390c966214SEd Cashin aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct aoedev *d;
44264a80f5aSEd Cashin 	int i;
4431da177e4SLinus Torvalds 	ulong flags;
44410935d05SEd Cashin 	ulong sysminor = 0;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	spin_lock_irqsave(&devlist_lock, flags);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	for (d=devlist; d; d=d->next)
4490c966214SEd Cashin 		if (d->aoemajor == maj && d->aoeminor == min) {
450e52a2932SEd Cashin 			spin_lock(&d->lock);
451e52a2932SEd Cashin 			if (d->flags & DEVFL_TKILL) {
452e52a2932SEd Cashin 				spin_unlock(&d->lock);
453e52a2932SEd Cashin 				d = NULL;
454e52a2932SEd Cashin 				goto out;
455e52a2932SEd Cashin 			}
45669cf2d85SEd Cashin 			d->ref++;
457e52a2932SEd Cashin 			spin_unlock(&d->lock);
4581da177e4SLinus Torvalds 			break;
45969cf2d85SEd Cashin 		}
4604bcce1a3SEd Cashin 	if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
46168e0d42fSEd L. Cashin 		goto out;
46268e0d42fSEd L. Cashin 	d = kcalloc(1, sizeof *d, GFP_ATOMIC);
46368e0d42fSEd L. Cashin 	if (!d)
46468e0d42fSEd L. Cashin 		goto out;
46571114ec4SEd Cashin 	d->targets = kcalloc(NTARGETS, sizeof(*d->targets), GFP_ATOMIC);
46671114ec4SEd Cashin 	if (!d->targets) {
46771114ec4SEd Cashin 		kfree(d);
46831279b14SDan Carpenter 		d = NULL;
46971114ec4SEd Cashin 		goto out;
47071114ec4SEd Cashin 	}
47171114ec4SEd Cashin 	d->ntargets = NTARGETS;
47268e0d42fSEd L. Cashin 	INIT_WORK(&d->work, aoecmd_sleepwork);
47368e0d42fSEd L. Cashin 	spin_lock_init(&d->lock);
4743582dd29SJens Axboe 	INIT_LIST_HEAD(&d->rq_list);
475e9bb8fb0SDavid S. Miller 	skb_queue_head_init(&d->skbpool);
4760e0cc9dfSKees Cook 	timer_setup(&d->timer, dummy_timer, 0);
47768e0d42fSEd L. Cashin 	d->timer.expires = jiffies + HZ;
47868e0d42fSEd L. Cashin 	add_timer(&d->timer);
47968e0d42fSEd L. Cashin 	d->bufpool = NULL;	/* defer to aoeblk_gdalloc */
48068e0d42fSEd L. Cashin 	d->tgt = d->targets;
48169cf2d85SEd Cashin 	d->ref = 1;
48264a80f5aSEd Cashin 	for (i = 0; i < NFACTIVE; i++)
48364a80f5aSEd Cashin 		INIT_LIST_HEAD(&d->factive[i]);
4843a0c40d2SEd Cashin 	INIT_LIST_HEAD(&d->rexmitq);
4851da177e4SLinus Torvalds 	d->sysminor = sysminor;
4860c966214SEd Cashin 	d->aoemajor = maj;
4870c966214SEd Cashin 	d->aoeminor = min;
4883a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
4893a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
49068e0d42fSEd L. Cashin 	d->next = devlist;
49168e0d42fSEd L. Cashin 	devlist = d;
49268e0d42fSEd L. Cashin  out:
4933ae1c24eSEd L. Cashin 	spin_unlock_irqrestore(&devlist_lock, flags);
4941da177e4SLinus Torvalds 	return d;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds static void
freetgt(struct aoedev * d,struct aoetgt * t)4989bb237b6SEd L. Cashin freetgt(struct aoedev *d, struct aoetgt *t)
4991da177e4SLinus Torvalds {
500896831f5SEd Cashin 	struct frame *f;
501896831f5SEd Cashin 	struct list_head *pos, *nx, *head;
5021b86fda9SEd Cashin 	struct aoeif *ifp;
5031b86fda9SEd Cashin 
5041b86fda9SEd Cashin 	for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
5051b86fda9SEd Cashin 		if (!ifp->nd)
5061b86fda9SEd Cashin 			break;
5071b86fda9SEd Cashin 		dev_put(ifp->nd);
5081b86fda9SEd Cashin 	}
509e407a7f6SEd L. Cashin 
510896831f5SEd Cashin 	head = &t->ffree;
511896831f5SEd Cashin 	list_for_each_safe(pos, nx, head) {
512896831f5SEd Cashin 		list_del(pos);
513896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
5149bb237b6SEd L. Cashin 		skbfree(f->skb);
515896831f5SEd Cashin 		kfree(f);
516896831f5SEd Cashin 	}
51768e0d42fSEd L. Cashin 	kfree(t);
51868e0d42fSEd L. Cashin }
51968e0d42fSEd L. Cashin 
5201da177e4SLinus Torvalds void
aoedev_exit(void)5211da177e4SLinus Torvalds aoedev_exit(void)
5221da177e4SLinus Torvalds {
5230b8d7622STetsuo Handa 	flush_workqueue(aoe_wq);
524e52a2932SEd Cashin 	flush(NULL, 0, EXITING);
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds int __init
aoedev_init(void)5281da177e4SLinus Torvalds aoedev_init(void)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	return 0;
5311da177e4SLinus Torvalds }
532