xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 1eb0da4cea28ae8f1bbe61822a2cc04e6d074e03)
12611464dSEd L. Cashin /* Copyright (c) 2006 Coraid, Inc.  See COPYING for GPL terms. */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * aoecmd.c
41da177e4SLinus Torvalds  * Filesystem request handling methods
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/hdreg.h>
81da177e4SLinus Torvalds #include <linux/blkdev.h>
91da177e4SLinus Torvalds #include <linux/skbuff.h>
101da177e4SLinus Torvalds #include <linux/netdevice.h>
113ae1c24eSEd L. Cashin #include <linux/genhd.h>
1268e0d42fSEd L. Cashin #include <linux/moduleparam.h>
13881d966bSEric W. Biederman #include <net/net_namespace.h>
14475172fbSEd L. Cashin #include <asm/unaligned.h>
151da177e4SLinus Torvalds #include "aoe.h"
161da177e4SLinus Torvalds 
17b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3;
18b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644);
19b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
201da177e4SLinus Torvalds 
2168e0d42fSEd L. Cashin static struct sk_buff *
22e407a7f6SEd L. Cashin new_skb(ulong len)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	struct sk_buff *skb;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
271da177e4SLinus Torvalds 	if (skb) {
28459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
29c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
301da177e4SLinus Torvalds 		skb->protocol = __constant_htons(ETH_P_AOE);
311da177e4SLinus Torvalds 		skb->priority = 0;
321da177e4SLinus Torvalds 		skb->next = skb->prev = NULL;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 		/* tell the network layer not to perform IP checksums
351da177e4SLinus Torvalds 		 * or to get the NIC to do it
361da177e4SLinus Torvalds 		 */
371da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_NONE;
381da177e4SLinus Torvalds 	}
391da177e4SLinus Torvalds 	return skb;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static struct frame *
4368e0d42fSEd L. Cashin getframe(struct aoetgt *t, int tag)
441da177e4SLinus Torvalds {
451da177e4SLinus Torvalds 	struct frame *f, *e;
461da177e4SLinus Torvalds 
4768e0d42fSEd L. Cashin 	f = t->frames;
4868e0d42fSEd L. Cashin 	e = f + t->nframes;
491da177e4SLinus Torvalds 	for (; f<e; f++)
501da177e4SLinus Torvalds 		if (f->tag == tag)
511da177e4SLinus Torvalds 			return f;
521da177e4SLinus Torvalds 	return NULL;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * Leave the top bit clear so we have tagspace for userland.
571da177e4SLinus Torvalds  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
581da177e4SLinus Torvalds  * This driver reserves tag -1 to mean "unused frame."
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds static int
6168e0d42fSEd L. Cashin newtag(struct aoetgt *t)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	register ulong n;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	n = jiffies & 0xffff;
6668e0d42fSEd L. Cashin 	return n |= (++t->lasttag & 0x7fff) << 16;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static int
7068e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
711da177e4SLinus Torvalds {
7268e0d42fSEd L. Cashin 	u32 host_tag = newtag(t);
731da177e4SLinus Torvalds 
7468e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
7568e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
7663e9cc5dSecashin@coraid.com 	h->type = __constant_cpu_to_be16(ETH_P_AOE);
771da177e4SLinus Torvalds 	h->verfl = AOE_HVER;
7863e9cc5dSecashin@coraid.com 	h->major = cpu_to_be16(d->aoemajor);
791da177e4SLinus Torvalds 	h->minor = d->aoeminor;
801da177e4SLinus Torvalds 	h->cmd = AOECMD_ATA;
8163e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(host_tag);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	return host_tag;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
8619bf2635SEd L. Cashin static inline void
8719bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba)
8819bf2635SEd L. Cashin {
8919bf2635SEd L. Cashin 	ah->lba0 = lba;
9019bf2635SEd L. Cashin 	ah->lba1 = lba >>= 8;
9119bf2635SEd L. Cashin 	ah->lba2 = lba >>= 8;
9219bf2635SEd L. Cashin 	ah->lba3 = lba >>= 8;
9319bf2635SEd L. Cashin 	ah->lba4 = lba >>= 8;
9419bf2635SEd L. Cashin 	ah->lba5 = lba >>= 8;
9519bf2635SEd L. Cashin }
9619bf2635SEd L. Cashin 
971da177e4SLinus Torvalds static void
9868e0d42fSEd L. Cashin ifrotate(struct aoetgt *t)
991da177e4SLinus Torvalds {
10068e0d42fSEd L. Cashin 	t->ifp++;
10168e0d42fSEd L. Cashin 	if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
10268e0d42fSEd L. Cashin 		t->ifp = t->ifs;
10368e0d42fSEd L. Cashin 	if (t->ifp->nd == NULL) {
10468e0d42fSEd L. Cashin 		printk(KERN_INFO "aoe: no interface to rotate to\n");
10568e0d42fSEd L. Cashin 		BUG();
10668e0d42fSEd L. Cashin 	}
10768e0d42fSEd L. Cashin }
10868e0d42fSEd L. Cashin 
10968e0d42fSEd L. Cashin static struct frame *
11068e0d42fSEd L. Cashin freeframe(struct aoedev *d)
11168e0d42fSEd L. Cashin {
11268e0d42fSEd L. Cashin 	struct frame *f, *e;
11368e0d42fSEd L. Cashin 	struct aoetgt **t;
11468e0d42fSEd L. Cashin 	ulong n;
11568e0d42fSEd L. Cashin 
11668e0d42fSEd L. Cashin 	if (d->targets[0] == NULL) {	/* shouldn't happen, but I'm paranoid */
11768e0d42fSEd L. Cashin 		printk(KERN_ERR "aoe: NULL TARGETS!\n");
11868e0d42fSEd L. Cashin 		return NULL;
11968e0d42fSEd L. Cashin 	}
12068e0d42fSEd L. Cashin 	t = d->targets;
12168e0d42fSEd L. Cashin 	do {
12268e0d42fSEd L. Cashin 		if (t != d->htgt
12368e0d42fSEd L. Cashin 		&& (*t)->ifp->nd
12468e0d42fSEd L. Cashin 		&& (*t)->nout < (*t)->maxout) {
12568e0d42fSEd L. Cashin 			n = (*t)->nframes;
12668e0d42fSEd L. Cashin 			f = (*t)->frames;
12768e0d42fSEd L. Cashin 			e = f + n;
12868e0d42fSEd L. Cashin 			for (; f < e; f++) {
12968e0d42fSEd L. Cashin 				if (f->tag != FREETAG)
13068e0d42fSEd L. Cashin 					continue;
13168e0d42fSEd L. Cashin 				if (atomic_read(&skb_shinfo(f->skb)->dataref)
13268e0d42fSEd L. Cashin 					!= 1) {
13368e0d42fSEd L. Cashin 					n--;
13468e0d42fSEd L. Cashin 					continue;
13568e0d42fSEd L. Cashin 				}
13668e0d42fSEd L. Cashin 				skb_shinfo(f->skb)->nr_frags = 0;
13768e0d42fSEd L. Cashin 				f->skb->data_len = 0;
13868e0d42fSEd L. Cashin 				skb_trim(f->skb, 0);
13968e0d42fSEd L. Cashin 				d->tgt = t;
14068e0d42fSEd L. Cashin 				ifrotate(*t);
14168e0d42fSEd L. Cashin 				return f;
14268e0d42fSEd L. Cashin 			}
14368e0d42fSEd L. Cashin 			if (n == 0)	/* slow polling network card */
14468e0d42fSEd L. Cashin 				d->flags |= DEVFL_KICKME;
14568e0d42fSEd L. Cashin 		}
14668e0d42fSEd L. Cashin 		t++;
14768e0d42fSEd L. Cashin 	} while (t < &d->targets[NTARGETS] && *t);
14868e0d42fSEd L. Cashin 	return NULL;
14968e0d42fSEd L. Cashin }
15068e0d42fSEd L. Cashin 
15168e0d42fSEd L. Cashin static int
15268e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d)
15368e0d42fSEd L. Cashin {
15468e0d42fSEd L. Cashin 	struct frame *f;
1551da177e4SLinus Torvalds 	struct aoe_hdr *h;
1561da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
1571da177e4SLinus Torvalds 	struct buf *buf;
15868e0d42fSEd L. Cashin 	struct bio_vec *bv;
15968e0d42fSEd L. Cashin 	struct aoetgt *t;
1601da177e4SLinus Torvalds 	struct sk_buff *skb;
1611da177e4SLinus Torvalds 	ulong bcnt;
1621da177e4SLinus Torvalds 	char writebit, extbit;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	writebit = 0x10;
1651da177e4SLinus Torvalds 	extbit = 0x4;
1661da177e4SLinus Torvalds 
16768e0d42fSEd L. Cashin 	f = freeframe(d);
16868e0d42fSEd L. Cashin 	if (f == NULL)
16968e0d42fSEd L. Cashin 		return 0;
17068e0d42fSEd L. Cashin 	t = *d->tgt;
1711da177e4SLinus Torvalds 	buf = d->inprocess;
17268e0d42fSEd L. Cashin 	bv = buf->bv;
17368e0d42fSEd L. Cashin 	bcnt = t->ifp->maxbcnt;
17468e0d42fSEd L. Cashin 	if (bcnt == 0)
17568e0d42fSEd L. Cashin 		bcnt = DEFAULTBCNT;
17668e0d42fSEd L. Cashin 	if (bcnt > buf->bv_resid)
1771da177e4SLinus Torvalds 		bcnt = buf->bv_resid;
1781da177e4SLinus Torvalds 	/* initialize the headers & frame */
179e407a7f6SEd L. Cashin 	skb = f->skb;
180abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
1811da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
18219900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
18319900cdeSEd L. Cashin 	memset(h, 0, skb->len);
18468e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
18568e0d42fSEd L. Cashin 	t->nout++;
1861da177e4SLinus Torvalds 	f->waited = 0;
1871da177e4SLinus Torvalds 	f->buf = buf;
18868e0d42fSEd L. Cashin 	f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
18919bf2635SEd L. Cashin 	f->bcnt = bcnt;
19068e0d42fSEd L. Cashin 	f->lba = buf->sector;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* set up ata header */
1931da177e4SLinus Torvalds 	ah->scnt = bcnt >> 9;
19468e0d42fSEd L. Cashin 	put_lba(ah, buf->sector);
1951da177e4SLinus Torvalds 	if (d->flags & DEVFL_EXT) {
1961da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_EXT;
1971da177e4SLinus Torvalds 	} else {
1981da177e4SLinus Torvalds 		extbit = 0;
1991da177e4SLinus Torvalds 		ah->lba3 &= 0x0f;
2001da177e4SLinus Torvalds 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 	if (bio_data_dir(buf->bio) == WRITE) {
20368e0d42fSEd L. Cashin 		skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
2041da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_WRITE;
2054f51dc5eSEd L. Cashin 		skb->len += bcnt;
2064f51dc5eSEd L. Cashin 		skb->data_len = bcnt;
20768e0d42fSEd L. Cashin 		t->wpkts++;
2081da177e4SLinus Torvalds 	} else {
20968e0d42fSEd L. Cashin 		t->rpkts++;
2101da177e4SLinus Torvalds 		writebit = 0;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	ah->cmdstat = WIN_READ | writebit | extbit;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* mark all tracking fields and load out */
2161da177e4SLinus Torvalds 	buf->nframesout += 1;
21768e0d42fSEd L. Cashin 	buf->bv_off += bcnt;
2181da177e4SLinus Torvalds 	buf->bv_resid -= bcnt;
2191da177e4SLinus Torvalds 	buf->resid -= bcnt;
2201da177e4SLinus Torvalds 	buf->sector += bcnt >> 9;
2211da177e4SLinus Torvalds 	if (buf->resid == 0) {
2221da177e4SLinus Torvalds 		d->inprocess = NULL;
2231da177e4SLinus Torvalds 	} else if (buf->bv_resid == 0) {
22468e0d42fSEd L. Cashin 		buf->bv = ++bv;
22568e0d42fSEd L. Cashin 		buf->bv_resid = bv->bv_len;
22668e0d42fSEd L. Cashin 		WARN_ON(buf->bv_resid == 0);
22768e0d42fSEd L. Cashin 		buf->bv_off = bv->bv_offset;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
23068e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
2314f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
23268e0d42fSEd L. Cashin 	if (skb) {
233a4b38364Secashin@coraid.com 		if (d->sendq_hd)
234a4b38364Secashin@coraid.com 			d->sendq_tl->next = skb;
235a4b38364Secashin@coraid.com 		else
236a4b38364Secashin@coraid.com 			d->sendq_hd = skb;
237a4b38364Secashin@coraid.com 		d->sendq_tl = skb;
2381da177e4SLinus Torvalds 	}
23968e0d42fSEd L. Cashin 	return 1;
24068e0d42fSEd L. Cashin }
2411da177e4SLinus Torvalds 
2423ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function,
2433ae1c24eSEd L. Cashin  * transmitting the packets later, when interrupts are on
2443ae1c24eSEd L. Cashin  */
2453ae1c24eSEd L. Cashin static struct sk_buff *
2463ae1c24eSEd L. Cashin aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
2473ae1c24eSEd L. Cashin {
2483ae1c24eSEd L. Cashin 	struct aoe_hdr *h;
2493ae1c24eSEd L. Cashin 	struct aoe_cfghdr *ch;
2503ae1c24eSEd L. Cashin 	struct sk_buff *skb, *sl, *sl_tail;
2513ae1c24eSEd L. Cashin 	struct net_device *ifp;
2523ae1c24eSEd L. Cashin 
2533ae1c24eSEd L. Cashin 	sl = sl_tail = NULL;
2543ae1c24eSEd L. Cashin 
2553ae1c24eSEd L. Cashin 	read_lock(&dev_base_lock);
256881d966bSEric W. Biederman 	for_each_netdev(&init_net, ifp) {
2573ae1c24eSEd L. Cashin 		dev_hold(ifp);
2583ae1c24eSEd L. Cashin 		if (!is_aoe_netif(ifp))
2597562f876SPavel Emelianov 			goto cont;
2603ae1c24eSEd L. Cashin 
261e407a7f6SEd L. Cashin 		skb = new_skb(sizeof *h + sizeof *ch);
2623ae1c24eSEd L. Cashin 		if (skb == NULL) {
263a12c93f0SEd L. Cashin 			printk(KERN_INFO "aoe: skb alloc failure\n");
2647562f876SPavel Emelianov 			goto cont;
2653ae1c24eSEd L. Cashin 		}
26619900cdeSEd L. Cashin 		skb_put(skb, sizeof *h + sizeof *ch);
267e407a7f6SEd L. Cashin 		skb->dev = ifp;
2683ae1c24eSEd L. Cashin 		if (sl_tail == NULL)
2693ae1c24eSEd L. Cashin 			sl_tail = skb;
270abdbf94dSEd L. Cashin 		h = (struct aoe_hdr *) skb_mac_header(skb);
2713ae1c24eSEd L. Cashin 		memset(h, 0, sizeof *h + sizeof *ch);
2723ae1c24eSEd L. Cashin 
2733ae1c24eSEd L. Cashin 		memset(h->dst, 0xff, sizeof h->dst);
2743ae1c24eSEd L. Cashin 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
2753ae1c24eSEd L. Cashin 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
2763ae1c24eSEd L. Cashin 		h->verfl = AOE_HVER;
2773ae1c24eSEd L. Cashin 		h->major = cpu_to_be16(aoemajor);
2783ae1c24eSEd L. Cashin 		h->minor = aoeminor;
2793ae1c24eSEd L. Cashin 		h->cmd = AOECMD_CFG;
2803ae1c24eSEd L. Cashin 
2813ae1c24eSEd L. Cashin 		skb->next = sl;
2823ae1c24eSEd L. Cashin 		sl = skb;
2837562f876SPavel Emelianov cont:
2847562f876SPavel Emelianov 		dev_put(ifp);
2853ae1c24eSEd L. Cashin 	}
2863ae1c24eSEd L. Cashin 	read_unlock(&dev_base_lock);
2873ae1c24eSEd L. Cashin 
2883ae1c24eSEd L. Cashin 	if (tail != NULL)
2893ae1c24eSEd L. Cashin 		*tail = sl_tail;
2903ae1c24eSEd L. Cashin 	return sl;
2913ae1c24eSEd L. Cashin }
2923ae1c24eSEd L. Cashin 
2931da177e4SLinus Torvalds static void
29468e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	struct sk_buff *skb;
2971da177e4SLinus Torvalds 	struct aoe_hdr *h;
29819bf2635SEd L. Cashin 	struct aoe_atahdr *ah;
2991da177e4SLinus Torvalds 	char buf[128];
3001da177e4SLinus Torvalds 	u32 n;
3011da177e4SLinus Torvalds 
30268e0d42fSEd L. Cashin 	ifrotate(t);
30368e0d42fSEd L. Cashin 	n = newtag(t);
304e407a7f6SEd L. Cashin 	skb = f->skb;
305abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
30619bf2635SEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
30768e0d42fSEd L. Cashin 
30868e0d42fSEd L. Cashin 	snprintf(buf, sizeof buf,
30968e0d42fSEd L. Cashin 		"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x "
31068e0d42fSEd L. Cashin 		"s=%012llx d=%012llx nout=%d\n",
31168e0d42fSEd L. Cashin 		"retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
312*1eb0da4cSEd L. Cashin 		mac_addr(h->src),
313*1eb0da4cSEd L. Cashin 		mac_addr(h->dst), t->nout);
31468e0d42fSEd L. Cashin 	aoechr_error(buf);
31568e0d42fSEd L. Cashin 
3161da177e4SLinus Torvalds 	f->tag = n;
31763e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(n);
31868e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
31968e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
3201da177e4SLinus Torvalds 
32168e0d42fSEd L. Cashin 	switch (ah->cmdstat) {
32268e0d42fSEd L. Cashin 	default:
32368e0d42fSEd L. Cashin 		break;
32468e0d42fSEd L. Cashin 	case WIN_READ:
32568e0d42fSEd L. Cashin 	case WIN_READ_EXT:
32668e0d42fSEd L. Cashin 	case WIN_WRITE:
32768e0d42fSEd L. Cashin 	case WIN_WRITE_EXT:
32868e0d42fSEd L. Cashin 		put_lba(ah, f->lba);
32968e0d42fSEd L. Cashin 
33068e0d42fSEd L. Cashin 		n = f->bcnt;
33168e0d42fSEd L. Cashin 		if (n > DEFAULTBCNT)
33268e0d42fSEd L. Cashin 			n = DEFAULTBCNT;
33368e0d42fSEd L. Cashin 		ah->scnt = n >> 9;
3344f51dc5eSEd L. Cashin 		if (ah->aflags & AOEAFL_WRITE) {
33519bf2635SEd L. Cashin 			skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
33668e0d42fSEd L. Cashin 				offset_in_page(f->bufaddr), n);
33768e0d42fSEd L. Cashin 			skb->len = sizeof *h + sizeof *ah + n;
33868e0d42fSEd L. Cashin 			skb->data_len = n;
33919bf2635SEd L. Cashin 		}
34019bf2635SEd L. Cashin 	}
34168e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
3424f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
3434f51dc5eSEd L. Cashin 	if (skb == NULL)
3444f51dc5eSEd L. Cashin 		return;
345a4b38364Secashin@coraid.com 	if (d->sendq_hd)
346a4b38364Secashin@coraid.com 		d->sendq_tl->next = skb;
347a4b38364Secashin@coraid.com 	else
348a4b38364Secashin@coraid.com 		d->sendq_hd = skb;
349a4b38364Secashin@coraid.com 	d->sendq_tl = skb;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static int
3531da177e4SLinus Torvalds tsince(int tag)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	int n;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	n = jiffies & 0xffff;
3581da177e4SLinus Torvalds 	n -= tag & 0xffff;
3591da177e4SLinus Torvalds 	if (n < 0)
3601da177e4SLinus Torvalds 		n += 1<<16;
3611da177e4SLinus Torvalds 	return n;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
36468e0d42fSEd L. Cashin static struct aoeif *
36568e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd)
36668e0d42fSEd L. Cashin {
36768e0d42fSEd L. Cashin 	struct aoeif *p, *e;
36868e0d42fSEd L. Cashin 
36968e0d42fSEd L. Cashin 	p = t->ifs;
37068e0d42fSEd L. Cashin 	e = p + NAOEIFS;
37168e0d42fSEd L. Cashin 	for (; p < e; p++)
37268e0d42fSEd L. Cashin 		if (p->nd == nd)
37368e0d42fSEd L. Cashin 			return p;
37468e0d42fSEd L. Cashin 	return NULL;
37568e0d42fSEd L. Cashin }
37668e0d42fSEd L. Cashin 
37768e0d42fSEd L. Cashin static struct aoeif *
37868e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd)
37968e0d42fSEd L. Cashin {
38068e0d42fSEd L. Cashin 	struct aoeif *p;
38168e0d42fSEd L. Cashin 
38268e0d42fSEd L. Cashin 	p = getif(t, NULL);
38368e0d42fSEd L. Cashin 	if (!p)
38468e0d42fSEd L. Cashin 		return NULL;
38568e0d42fSEd L. Cashin 	p->nd = nd;
38668e0d42fSEd L. Cashin 	p->maxbcnt = DEFAULTBCNT;
38768e0d42fSEd L. Cashin 	p->lost = 0;
38868e0d42fSEd L. Cashin 	p->lostjumbo = 0;
38968e0d42fSEd L. Cashin 	return p;
39068e0d42fSEd L. Cashin }
39168e0d42fSEd L. Cashin 
39268e0d42fSEd L. Cashin static void
39368e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp)
39468e0d42fSEd L. Cashin {
39568e0d42fSEd L. Cashin 	struct aoeif *e;
39668e0d42fSEd L. Cashin 	ulong n;
39768e0d42fSEd L. Cashin 
39868e0d42fSEd L. Cashin 	e = t->ifs + NAOEIFS - 1;
39968e0d42fSEd L. Cashin 	n = (e - ifp) * sizeof *ifp;
40068e0d42fSEd L. Cashin 	memmove(ifp, ifp+1, n);
40168e0d42fSEd L. Cashin 	e->nd = NULL;
40268e0d42fSEd L. Cashin }
40368e0d42fSEd L. Cashin 
40468e0d42fSEd L. Cashin static int
40568e0d42fSEd L. Cashin sthtith(struct aoedev *d)
40668e0d42fSEd L. Cashin {
40768e0d42fSEd L. Cashin 	struct frame *f, *e, *nf;
40868e0d42fSEd L. Cashin 	struct sk_buff *skb;
40968e0d42fSEd L. Cashin 	struct aoetgt *ht = *d->htgt;
41068e0d42fSEd L. Cashin 
41168e0d42fSEd L. Cashin 	f = ht->frames;
41268e0d42fSEd L. Cashin 	e = f + ht->nframes;
41368e0d42fSEd L. Cashin 	for (; f < e; f++) {
41468e0d42fSEd L. Cashin 		if (f->tag == FREETAG)
41568e0d42fSEd L. Cashin 			continue;
41668e0d42fSEd L. Cashin 		nf = freeframe(d);
41768e0d42fSEd L. Cashin 		if (!nf)
41868e0d42fSEd L. Cashin 			return 0;
41968e0d42fSEd L. Cashin 		skb = nf->skb;
42068e0d42fSEd L. Cashin 		*nf = *f;
42168e0d42fSEd L. Cashin 		f->skb = skb;
42268e0d42fSEd L. Cashin 		f->tag = FREETAG;
42368e0d42fSEd L. Cashin 		nf->waited = 0;
42468e0d42fSEd L. Cashin 		ht->nout--;
42568e0d42fSEd L. Cashin 		(*d->tgt)->nout++;
42668e0d42fSEd L. Cashin 		resend(d, *d->tgt, nf);
42768e0d42fSEd L. Cashin 	}
42868e0d42fSEd L. Cashin 	/* he's clean, he's useless.  take away his interfaces */
42968e0d42fSEd L. Cashin 	memset(ht->ifs, 0, sizeof ht->ifs);
43068e0d42fSEd L. Cashin 	d->htgt = NULL;
43168e0d42fSEd L. Cashin 	return 1;
43268e0d42fSEd L. Cashin }
43368e0d42fSEd L. Cashin 
43468e0d42fSEd L. Cashin static inline unsigned char
43568e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) {
43668e0d42fSEd L. Cashin 	struct aoe_hdr *h;
43768e0d42fSEd L. Cashin 	struct aoe_atahdr *ah;
43868e0d42fSEd L. Cashin 
43968e0d42fSEd L. Cashin 	h = (struct aoe_hdr *) packet;
44068e0d42fSEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
44168e0d42fSEd L. Cashin 	return ah->scnt;
44268e0d42fSEd L. Cashin }
44368e0d42fSEd L. Cashin 
4441da177e4SLinus Torvalds static void
4451da177e4SLinus Torvalds rexmit_timer(ulong vp)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct aoedev *d;
44868e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
44968e0d42fSEd L. Cashin 	struct aoeif *ifp;
4501da177e4SLinus Torvalds 	struct frame *f, *e;
4511da177e4SLinus Torvalds 	struct sk_buff *sl;
4521da177e4SLinus Torvalds 	register long timeout;
4531da177e4SLinus Torvalds 	ulong flags, n;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	d = (struct aoedev *) vp;
4561da177e4SLinus Torvalds 	sl = NULL;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/* timeout is always ~150% of the moving average */
4591da177e4SLinus Torvalds 	timeout = d->rttavg;
4601da177e4SLinus Torvalds 	timeout += timeout >> 1;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (d->flags & DEVFL_TKILL) {
4651c6f3fcaSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
4661da177e4SLinus Torvalds 		return;
4671da177e4SLinus Torvalds 	}
46868e0d42fSEd L. Cashin 	tt = d->targets;
46968e0d42fSEd L. Cashin 	te = tt + NTARGETS;
47068e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++) {
47168e0d42fSEd L. Cashin 		t = *tt;
47268e0d42fSEd L. Cashin 		f = t->frames;
47368e0d42fSEd L. Cashin 		e = f + t->nframes;
4741da177e4SLinus Torvalds 		for (; f < e; f++) {
47568e0d42fSEd L. Cashin 			if (f->tag == FREETAG
47668e0d42fSEd L. Cashin 			|| tsince(f->tag) < timeout)
47768e0d42fSEd L. Cashin 				continue;
4781da177e4SLinus Torvalds 			n = f->waited += timeout;
4791da177e4SLinus Torvalds 			n /= HZ;
48068e0d42fSEd L. Cashin 			if (n > aoe_deadsecs) {
48168e0d42fSEd L. Cashin 				/* waited too long.  device failure. */
4821da177e4SLinus Torvalds 				aoedev_downdev(d);
4831c6f3fcaSEd L. Cashin 				break;
4841da177e4SLinus Torvalds 			}
48568e0d42fSEd L. Cashin 
48668e0d42fSEd L. Cashin 			if (n > HELPWAIT /* see if another target can help */
48768e0d42fSEd L. Cashin 			&& (tt != d->targets || d->targets[1]))
48868e0d42fSEd L. Cashin 				d->htgt = tt;
48968e0d42fSEd L. Cashin 
49068e0d42fSEd L. Cashin 			if (t->nout == t->maxout) {
49168e0d42fSEd L. Cashin 				if (t->maxout > 1)
49268e0d42fSEd L. Cashin 					t->maxout--;
49368e0d42fSEd L. Cashin 				t->lastwadj = jiffies;
49468e0d42fSEd L. Cashin 			}
49568e0d42fSEd L. Cashin 
49668e0d42fSEd L. Cashin 			ifp = getif(t, f->skb->dev);
49768e0d42fSEd L. Cashin 			if (ifp && ++ifp->lost > (t->nframes << 1)
49868e0d42fSEd L. Cashin 			&& (ifp != t->ifs || t->ifs[1].nd)) {
49968e0d42fSEd L. Cashin 				ejectif(t, ifp);
50068e0d42fSEd L. Cashin 				ifp = NULL;
50168e0d42fSEd L. Cashin 			}
50268e0d42fSEd L. Cashin 
50368e0d42fSEd L. Cashin 			if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
50468e0d42fSEd L. Cashin 			&& ifp && ++ifp->lostjumbo > (t->nframes << 1)
50568e0d42fSEd L. Cashin 			&& ifp->maxbcnt != DEFAULTBCNT) {
50668e0d42fSEd L. Cashin 				printk(KERN_INFO
50768e0d42fSEd L. Cashin 					"aoe: e%ld.%d: "
50868e0d42fSEd L. Cashin 					"too many lost jumbo on "
50968e0d42fSEd L. Cashin 					"%s:%012llx - "
51068e0d42fSEd L. Cashin 					"falling back to %d frames.\n",
51168e0d42fSEd L. Cashin 					d->aoemajor, d->aoeminor,
51268e0d42fSEd L. Cashin 					ifp->nd->name, mac_addr(t->addr),
51368e0d42fSEd L. Cashin 					DEFAULTBCNT);
51468e0d42fSEd L. Cashin 				ifp->maxbcnt = 0;
51568e0d42fSEd L. Cashin 			}
51668e0d42fSEd L. Cashin 			resend(d, t, f);
51768e0d42fSEd L. Cashin 		}
51868e0d42fSEd L. Cashin 
51968e0d42fSEd L. Cashin 		/* window check */
52068e0d42fSEd L. Cashin 		if (t->nout == t->maxout
52168e0d42fSEd L. Cashin 		&& t->maxout < t->nframes
52268e0d42fSEd L. Cashin 		&& (jiffies - t->lastwadj)/HZ > 10) {
52368e0d42fSEd L. Cashin 			t->maxout++;
52468e0d42fSEd L. Cashin 			t->lastwadj = jiffies;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 	}
52768e0d42fSEd L. Cashin 
52868e0d42fSEd L. Cashin 	if (d->sendq_hd) {
52968e0d42fSEd L. Cashin 		n = d->rttavg <<= 1;
53068e0d42fSEd L. Cashin 		if (n > MAXTIMER)
53168e0d42fSEd L. Cashin 			d->rttavg = MAXTIMER;
53268e0d42fSEd L. Cashin 	}
53368e0d42fSEd L. Cashin 
53468e0d42fSEd L. Cashin 	if (d->flags & DEVFL_KICKME || d->htgt) {
5354f51dc5eSEd L. Cashin 		d->flags &= ~DEVFL_KICKME;
5364f51dc5eSEd L. Cashin 		aoecmd_work(d);
5374f51dc5eSEd L. Cashin 	}
5381da177e4SLinus Torvalds 
539a4b38364Secashin@coraid.com 	sl = d->sendq_hd;
540a4b38364Secashin@coraid.com 	d->sendq_hd = d->sendq_tl = NULL;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	d->timer.expires = jiffies + TIMERTICK;
5431da177e4SLinus Torvalds 	add_timer(&d->timer);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	aoenet_xmit(sl);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
55068e0d42fSEd L. Cashin /* enters with d->lock held */
55168e0d42fSEd L. Cashin void
55268e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d)
55368e0d42fSEd L. Cashin {
55468e0d42fSEd L. Cashin 	struct buf *buf;
55568e0d42fSEd L. Cashin loop:
55668e0d42fSEd L. Cashin 	if (d->htgt && !sthtith(d))
55768e0d42fSEd L. Cashin 		return;
55868e0d42fSEd L. Cashin 	if (d->inprocess == NULL) {
55968e0d42fSEd L. Cashin 		if (list_empty(&d->bufq))
56068e0d42fSEd L. Cashin 			return;
56168e0d42fSEd L. Cashin 		buf = container_of(d->bufq.next, struct buf, bufs);
56268e0d42fSEd L. Cashin 		list_del(d->bufq.next);
56368e0d42fSEd L. Cashin 		d->inprocess = buf;
56468e0d42fSEd L. Cashin 	}
56568e0d42fSEd L. Cashin 	if (aoecmd_ata_rw(d))
56668e0d42fSEd L. Cashin 		goto loop;
56768e0d42fSEd L. Cashin }
56868e0d42fSEd L. Cashin 
5693ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK
5703ae1c24eSEd L. Cashin  */
5713ae1c24eSEd L. Cashin void
572c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work)
5733ae1c24eSEd L. Cashin {
574c4028958SDavid Howells 	struct aoedev *d = container_of(work, struct aoedev, work);
5753ae1c24eSEd L. Cashin 
5763ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_GDALLOC)
5773ae1c24eSEd L. Cashin 		aoeblk_gdalloc(d);
5783ae1c24eSEd L. Cashin 
5793ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_NEWSIZE) {
5803ae1c24eSEd L. Cashin 		struct block_device *bd;
5813ae1c24eSEd L. Cashin 		unsigned long flags;
5823ae1c24eSEd L. Cashin 		u64 ssize;
5833ae1c24eSEd L. Cashin 
5843ae1c24eSEd L. Cashin 		ssize = d->gd->capacity;
5853ae1c24eSEd L. Cashin 		bd = bdget_disk(d->gd, 0);
5863ae1c24eSEd L. Cashin 
5873ae1c24eSEd L. Cashin 		if (bd) {
5883ae1c24eSEd L. Cashin 			mutex_lock(&bd->bd_inode->i_mutex);
5893ae1c24eSEd L. Cashin 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
5903ae1c24eSEd L. Cashin 			mutex_unlock(&bd->bd_inode->i_mutex);
5913ae1c24eSEd L. Cashin 			bdput(bd);
5923ae1c24eSEd L. Cashin 		}
5933ae1c24eSEd L. Cashin 		spin_lock_irqsave(&d->lock, flags);
5943ae1c24eSEd L. Cashin 		d->flags |= DEVFL_UP;
5953ae1c24eSEd L. Cashin 		d->flags &= ~DEVFL_NEWSIZE;
5963ae1c24eSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
5973ae1c24eSEd L. Cashin 	}
5983ae1c24eSEd L. Cashin }
5993ae1c24eSEd L. Cashin 
6001da177e4SLinus Torvalds static void
60168e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	u64 ssize;
6041da177e4SLinus Torvalds 	u16 n;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	/* word 83: command set supported */
607475172fbSEd L. Cashin 	n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1]));
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/* word 86: command set/feature enabled */
610475172fbSEd L. Cashin 	n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1]));
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
6131da177e4SLinus Torvalds 		d->flags |= DEVFL_EXT;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		/* word 100: number lba48 sectors */
616475172fbSEd L. Cashin 		ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1]));
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		/* set as in ide-disk.c:init_idedisk_capacity */
6191da177e4SLinus Torvalds 		d->geo.cylinders = ssize;
6201da177e4SLinus Torvalds 		d->geo.cylinders /= (255 * 63);
6211da177e4SLinus Torvalds 		d->geo.heads = 255;
6221da177e4SLinus Torvalds 		d->geo.sectors = 63;
6231da177e4SLinus Torvalds 	} else {
6241da177e4SLinus Torvalds 		d->flags &= ~DEVFL_EXT;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		/* number lba28 sectors */
627475172fbSEd L. Cashin 		ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1]));
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		/* NOTE: obsolete in ATA 6 */
630475172fbSEd L. Cashin 		d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1]));
631475172fbSEd L. Cashin 		d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1]));
632475172fbSEd L. Cashin 		d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1]));
6331da177e4SLinus Torvalds 	}
6343ae1c24eSEd L. Cashin 
6353ae1c24eSEd L. Cashin 	if (d->ssize != ssize)
636a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu sectors\n",
637*1eb0da4cSEd L. Cashin 			mac_addr(t->addr),
6383ae1c24eSEd L. Cashin 			d->aoemajor, d->aoeminor,
6393ae1c24eSEd L. Cashin 			d->fw_ver, (long long)ssize);
6401da177e4SLinus Torvalds 	d->ssize = ssize;
6411da177e4SLinus Torvalds 	d->geo.start = 0;
6421da177e4SLinus Torvalds 	if (d->gd != NULL) {
6431da177e4SLinus Torvalds 		d->gd->capacity = ssize;
6443ae1c24eSEd L. Cashin 		d->flags |= DEVFL_NEWSIZE;
64568e0d42fSEd L. Cashin 	} else
6463ae1c24eSEd L. Cashin 		d->flags |= DEVFL_GDALLOC;
6471da177e4SLinus Torvalds 	schedule_work(&d->work);
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds static void
6511da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	register long n;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	n = rtt;
656dced3a05SEd L. Cashin 	if (n < 0) {
657dced3a05SEd L. Cashin 		n = -rtt;
6581da177e4SLinus Torvalds 		if (n < MINTIMER)
6591da177e4SLinus Torvalds 			n = MINTIMER;
6601da177e4SLinus Torvalds 		else if (n > MAXTIMER)
6611da177e4SLinus Torvalds 			n = MAXTIMER;
662dced3a05SEd L. Cashin 		d->mintimer += (n - d->mintimer) >> 1;
663dced3a05SEd L. Cashin 	} else if (n < d->mintimer)
664dced3a05SEd L. Cashin 		n = d->mintimer;
665dced3a05SEd L. Cashin 	else if (n > MAXTIMER)
666dced3a05SEd L. Cashin 		n = MAXTIMER;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	/* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
6691da177e4SLinus Torvalds 	n -= d->rttavg;
6701da177e4SLinus Torvalds 	d->rttavg += n >> 2;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
67368e0d42fSEd L. Cashin static struct aoetgt *
67468e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr)
67568e0d42fSEd L. Cashin {
67668e0d42fSEd L. Cashin 	struct aoetgt **t, **e;
67768e0d42fSEd L. Cashin 
67868e0d42fSEd L. Cashin 	t = d->targets;
67968e0d42fSEd L. Cashin 	e = t + NTARGETS;
68068e0d42fSEd L. Cashin 	for (; t < e && *t; t++)
68168e0d42fSEd L. Cashin 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
68268e0d42fSEd L. Cashin 			return *t;
68368e0d42fSEd L. Cashin 	return NULL;
68468e0d42fSEd L. Cashin }
68568e0d42fSEd L. Cashin 
68668e0d42fSEd L. Cashin static inline void
68768e0d42fSEd L. Cashin diskstats(struct gendisk *disk, struct bio *bio, ulong duration)
68868e0d42fSEd L. Cashin {
68968e0d42fSEd L. Cashin 	unsigned long n_sect = bio->bi_size >> 9;
69068e0d42fSEd L. Cashin 	const int rw = bio_data_dir(bio);
69168e0d42fSEd L. Cashin 
69268e0d42fSEd L. Cashin 	disk_stat_inc(disk, ios[rw]);
69368e0d42fSEd L. Cashin 	disk_stat_add(disk, ticks[rw], duration);
69468e0d42fSEd L. Cashin 	disk_stat_add(disk, sectors[rw], n_sect);
69568e0d42fSEd L. Cashin 	disk_stat_add(disk, io_ticks, duration);
69668e0d42fSEd L. Cashin }
69768e0d42fSEd L. Cashin 
6981da177e4SLinus Torvalds void
6991da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct aoedev *d;
702ddec63e8SEd L. Cashin 	struct aoe_hdr *hin, *hout;
7031da177e4SLinus Torvalds 	struct aoe_atahdr *ahin, *ahout;
7041da177e4SLinus Torvalds 	struct frame *f;
7051da177e4SLinus Torvalds 	struct buf *buf;
7061da177e4SLinus Torvalds 	struct sk_buff *sl;
70768e0d42fSEd L. Cashin 	struct aoetgt *t;
70868e0d42fSEd L. Cashin 	struct aoeif *ifp;
7091da177e4SLinus Torvalds 	register long n;
7101da177e4SLinus Torvalds 	ulong flags;
7111da177e4SLinus Torvalds 	char ebuf[128];
71232465c65Secashin@coraid.com 	u16 aoemajor;
7131da177e4SLinus Torvalds 
714abdbf94dSEd L. Cashin 	hin = (struct aoe_hdr *) skb_mac_header(skb);
71543ecf529SDavid S. Miller 	aoemajor = be16_to_cpu(get_unaligned(&hin->major));
71632465c65Secashin@coraid.com 	d = aoedev_by_aoeaddr(aoemajor, hin->minor);
7171da177e4SLinus Torvalds 	if (d == NULL) {
7181da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
7191da177e4SLinus Torvalds 			"for unknown device %d.%d\n",
72032465c65Secashin@coraid.com 			 aoemajor, hin->minor);
7211da177e4SLinus Torvalds 		aoechr_error(ebuf);
7221da177e4SLinus Torvalds 		return;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
7261da177e4SLinus Torvalds 
72743ecf529SDavid S. Miller 	n = be32_to_cpu(get_unaligned(&hin->tag));
72868e0d42fSEd L. Cashin 	t = gettgt(d, hin->src);
72968e0d42fSEd L. Cashin 	if (t == NULL) {
73068e0d42fSEd L. Cashin 		printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n",
731*1eb0da4cSEd L. Cashin 			d->aoemajor, d->aoeminor, mac_addr(hin->src));
73268e0d42fSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
73368e0d42fSEd L. Cashin 		return;
73468e0d42fSEd L. Cashin 	}
73568e0d42fSEd L. Cashin 	f = getframe(t, n);
7361da177e4SLinus Torvalds 	if (f == NULL) {
737dced3a05SEd L. Cashin 		calc_rttavg(d, -tsince(n));
7381da177e4SLinus Torvalds 		spin_unlock_irqrestore(&d->lock, flags);
7391da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf,
7401da177e4SLinus Torvalds 			"%15s e%d.%d    tag=%08x@%08lx\n",
7411da177e4SLinus Torvalds 			"unexpected rsp",
74243ecf529SDavid S. Miller 			be16_to_cpu(get_unaligned(&hin->major)),
7431da177e4SLinus Torvalds 			hin->minor,
74443ecf529SDavid S. Miller 			be32_to_cpu(get_unaligned(&hin->tag)),
7451da177e4SLinus Torvalds 			jiffies);
7461da177e4SLinus Torvalds 		aoechr_error(ebuf);
7471da177e4SLinus Torvalds 		return;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	calc_rttavg(d, tsince(f->tag));
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	ahin = (struct aoe_atahdr *) (hin+1);
753abdbf94dSEd L. Cashin 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
754ddec63e8SEd L. Cashin 	ahout = (struct aoe_atahdr *) (hout+1);
7551da177e4SLinus Torvalds 	buf = f->buf;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
758a12c93f0SEd L. Cashin 		printk(KERN_ERR
759a12c93f0SEd L. Cashin 			"aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n",
7601da177e4SLinus Torvalds 			ahout->cmdstat, ahin->cmdstat,
7611da177e4SLinus Torvalds 			d->aoemajor, d->aoeminor);
7621da177e4SLinus Torvalds 		if (buf)
7631da177e4SLinus Torvalds 			buf->flags |= BUFFL_FAIL;
7641da177e4SLinus Torvalds 	} else {
76568e0d42fSEd L. Cashin 		if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
76668e0d42fSEd L. Cashin 			d->htgt = NULL;
76719bf2635SEd L. Cashin 		n = ahout->scnt << 9;
7681da177e4SLinus Torvalds 		switch (ahout->cmdstat) {
7691da177e4SLinus Torvalds 		case WIN_READ:
7701da177e4SLinus Torvalds 		case WIN_READ_EXT:
7711da177e4SLinus Torvalds 			if (skb->len - sizeof *hin - sizeof *ahin < n) {
772a12c93f0SEd L. Cashin 				printk(KERN_ERR
77368e0d42fSEd L. Cashin 					"aoe: %s.  skb->len=%d need=%ld\n",
77468e0d42fSEd L. Cashin 					"runt data size in read", skb->len, n);
7751da177e4SLinus Torvalds 				/* fail frame f?  just returning will rexmit. */
7761da177e4SLinus Torvalds 				spin_unlock_irqrestore(&d->lock, flags);
7771da177e4SLinus Torvalds 				return;
7781da177e4SLinus Torvalds 			}
7791da177e4SLinus Torvalds 			memcpy(f->bufaddr, ahin+1, n);
7801da177e4SLinus Torvalds 		case WIN_WRITE:
7811da177e4SLinus Torvalds 		case WIN_WRITE_EXT:
78268e0d42fSEd L. Cashin 			ifp = getif(t, skb->dev);
78368e0d42fSEd L. Cashin 			if (ifp) {
78468e0d42fSEd L. Cashin 				ifp->lost = 0;
78568e0d42fSEd L. Cashin 				if (n > DEFAULTBCNT)
78668e0d42fSEd L. Cashin 					ifp->lostjumbo = 0;
78768e0d42fSEd L. Cashin 			}
78819bf2635SEd L. Cashin 			if (f->bcnt -= n) {
78968e0d42fSEd L. Cashin 				f->lba += n >> 9;
79019bf2635SEd L. Cashin 				f->bufaddr += n;
79168e0d42fSEd L. Cashin 				resend(d, t, f);
79268e0d42fSEd L. Cashin 				goto xmit;
7934f51dc5eSEd L. Cashin 			}
7941da177e4SLinus Torvalds 			break;
7951da177e4SLinus Torvalds 		case WIN_IDENTIFY:
7961da177e4SLinus Torvalds 			if (skb->len - sizeof *hin - sizeof *ahin < 512) {
797a12c93f0SEd L. Cashin 				printk(KERN_INFO
798a12c93f0SEd L. Cashin 					"aoe: runt data size in ataid.  skb->len=%d\n",
7996bb6285fSEd L. Cashin 					skb->len);
8001da177e4SLinus Torvalds 				spin_unlock_irqrestore(&d->lock, flags);
8011da177e4SLinus Torvalds 				return;
8021da177e4SLinus Torvalds 			}
80368e0d42fSEd L. Cashin 			ataid_complete(d, t, (char *) (ahin+1));
8041da177e4SLinus Torvalds 			break;
8051da177e4SLinus Torvalds 		default:
806a12c93f0SEd L. Cashin 			printk(KERN_INFO
807a12c93f0SEd L. Cashin 				"aoe: unrecognized ata command %2.2Xh for %d.%d\n",
8081da177e4SLinus Torvalds 				ahout->cmdstat,
80943ecf529SDavid S. Miller 				be16_to_cpu(get_unaligned(&hin->major)),
8101da177e4SLinus Torvalds 				hin->minor);
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
81468e0d42fSEd L. Cashin 	if (buf && --buf->nframesout == 0 && buf->resid == 0) {
81568e0d42fSEd L. Cashin 		diskstats(d->gd, buf->bio, jiffies - buf->stime);
8161da177e4SLinus Torvalds 		n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
8176712ecf8SNeilBrown 		bio_endio(buf->bio, n);
8181da177e4SLinus Torvalds 		mempool_free(buf, d->bufpool);
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	f->buf = NULL;
8221da177e4SLinus Torvalds 	f->tag = FREETAG;
82368e0d42fSEd L. Cashin 	t->nout--;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	aoecmd_work(d);
82668e0d42fSEd L. Cashin xmit:
827a4b38364Secashin@coraid.com 	sl = d->sendq_hd;
828a4b38364Secashin@coraid.com 	d->sendq_hd = d->sendq_tl = NULL;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
8311da177e4SLinus Torvalds 	aoenet_xmit(sl);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds void
8351da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
8361da177e4SLinus Torvalds {
8373ae1c24eSEd L. Cashin 	struct sk_buff *sl;
8381da177e4SLinus Torvalds 
8393ae1c24eSEd L. Cashin 	sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	aoenet_xmit(sl);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
84468e0d42fSEd L. Cashin struct sk_buff *
8451da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	struct aoe_hdr *h;
8481da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
8491da177e4SLinus Torvalds 	struct frame *f;
8501da177e4SLinus Torvalds 	struct sk_buff *skb;
85168e0d42fSEd L. Cashin 	struct aoetgt *t;
8521da177e4SLinus Torvalds 
8534f51dc5eSEd L. Cashin 	f = freeframe(d);
85468e0d42fSEd L. Cashin 	if (f == NULL)
8551da177e4SLinus Torvalds 		return NULL;
85668e0d42fSEd L. Cashin 
85768e0d42fSEd L. Cashin 	t = *d->tgt;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	/* initialize the headers & frame */
860e407a7f6SEd L. Cashin 	skb = f->skb;
861abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
8621da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
86319900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
86419900cdeSEd L. Cashin 	memset(h, 0, skb->len);
86568e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
86668e0d42fSEd L. Cashin 	t->nout++;
8671da177e4SLinus Torvalds 	f->waited = 0;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	/* set up ata header */
8701da177e4SLinus Torvalds 	ah->scnt = 1;
8711da177e4SLinus Torvalds 	ah->cmdstat = WIN_IDENTIFY;
8721da177e4SLinus Torvalds 	ah->lba3 = 0xa0;
8731da177e4SLinus Torvalds 
87468e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
8751da177e4SLinus Torvalds 
8763ae1c24eSEd L. Cashin 	d->rttavg = MAXTIMER;
8771da177e4SLinus Torvalds 	d->timer.function = rexmit_timer;
8781da177e4SLinus Torvalds 
8794f51dc5eSEd L. Cashin 	return skb_clone(skb, GFP_ATOMIC);
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
88268e0d42fSEd L. Cashin static struct aoetgt *
88368e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes)
88468e0d42fSEd L. Cashin {
88568e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
88668e0d42fSEd L. Cashin 	struct frame *f, *e;
88768e0d42fSEd L. Cashin 
88868e0d42fSEd L. Cashin 	tt = d->targets;
88968e0d42fSEd L. Cashin 	te = tt + NTARGETS;
89068e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++)
89168e0d42fSEd L. Cashin 		;
89268e0d42fSEd L. Cashin 
89368e0d42fSEd L. Cashin 	if (tt == te)
89468e0d42fSEd L. Cashin 		return NULL;
89568e0d42fSEd L. Cashin 
89668e0d42fSEd L. Cashin 	t = kcalloc(1, sizeof *t, GFP_ATOMIC);
89768e0d42fSEd L. Cashin 	f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
89868e0d42fSEd L. Cashin 	if (!t || !f)
89968e0d42fSEd L. Cashin 		goto bail;
90068e0d42fSEd L. Cashin 	t->nframes = nframes;
90168e0d42fSEd L. Cashin 	t->frames = f;
90268e0d42fSEd L. Cashin 	e = f + nframes;
90368e0d42fSEd L. Cashin 	for (; f < e; f++) {
90468e0d42fSEd L. Cashin 		f->tag = FREETAG;
90568e0d42fSEd L. Cashin 		f->skb = new_skb(ETH_ZLEN);
90668e0d42fSEd L. Cashin 		if (!f->skb)
90768e0d42fSEd L. Cashin 			break;
90868e0d42fSEd L. Cashin 	}
90968e0d42fSEd L. Cashin 	if (f != e) {
91068e0d42fSEd L. Cashin 		while (f > t->frames) {
91168e0d42fSEd L. Cashin 			f--;
91268e0d42fSEd L. Cashin 			dev_kfree_skb(f->skb);
91368e0d42fSEd L. Cashin 		}
91468e0d42fSEd L. Cashin 		goto bail;
91568e0d42fSEd L. Cashin 	}
91668e0d42fSEd L. Cashin 	memcpy(t->addr, addr, sizeof t->addr);
91768e0d42fSEd L. Cashin 	t->ifp = t->ifs;
91868e0d42fSEd L. Cashin 	t->maxout = t->nframes;
91968e0d42fSEd L. Cashin 	return *tt = t;
92068e0d42fSEd L. Cashin bail:
92168e0d42fSEd L. Cashin 	kfree(t);
92268e0d42fSEd L. Cashin 	kfree(f);
92368e0d42fSEd L. Cashin 	return NULL;
92468e0d42fSEd L. Cashin }
92568e0d42fSEd L. Cashin 
9261da177e4SLinus Torvalds void
9271da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	struct aoedev *d;
9301da177e4SLinus Torvalds 	struct aoe_hdr *h;
9311da177e4SLinus Torvalds 	struct aoe_cfghdr *ch;
93268e0d42fSEd L. Cashin 	struct aoetgt *t;
93368e0d42fSEd L. Cashin 	struct aoeif *ifp;
93463e9cc5dSecashin@coraid.com 	ulong flags, sysminor, aoemajor;
9351da177e4SLinus Torvalds 	struct sk_buff *sl;
936eaf0a3cbSEd L. Cashin 	enum { MAXFRAMES = 16 };
93719bf2635SEd L. Cashin 	u16 n;
9381da177e4SLinus Torvalds 
939abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
9401da177e4SLinus Torvalds 	ch = (struct aoe_cfghdr *) (h+1);
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/*
9431da177e4SLinus Torvalds 	 * Enough people have their dip switches set backwards to
9441da177e4SLinus Torvalds 	 * warrant a loud message for this special case.
9451da177e4SLinus Torvalds 	 */
94643ecf529SDavid S. Miller 	aoemajor = be16_to_cpu(get_unaligned(&h->major));
9471da177e4SLinus Torvalds 	if (aoemajor == 0xfff) {
948a12c93f0SEd L. Cashin 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
9496bb6285fSEd L. Cashin 			"Check shelf dip switches.\n");
9501da177e4SLinus Torvalds 		return;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	sysminor = SYSMINOR(aoemajor, h->minor);
954fc458dcdSecashin@coraid.com 	if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
955a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
956fc458dcdSecashin@coraid.com 			aoemajor, (int) h->minor);
9571da177e4SLinus Torvalds 		return;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
96019bf2635SEd L. Cashin 	n = be16_to_cpu(ch->bufcnt);
96119bf2635SEd L. Cashin 	if (n > MAXFRAMES)	/* keep it reasonable */
96219bf2635SEd L. Cashin 		n = MAXFRAMES;
9631da177e4SLinus Torvalds 
96468e0d42fSEd L. Cashin 	d = aoedev_by_sysminor_m(sysminor);
9651da177e4SLinus Torvalds 	if (d == NULL) {
966a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: device sysminor_m failure\n");
9671da177e4SLinus Torvalds 		return;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
9711da177e4SLinus Torvalds 
97268e0d42fSEd L. Cashin 	t = gettgt(d, h->src);
97368e0d42fSEd L. Cashin 	if (!t) {
97468e0d42fSEd L. Cashin 		t = addtgt(d, h->src, n);
97568e0d42fSEd L. Cashin 		if (!t) {
97668e0d42fSEd L. Cashin 			printk(KERN_INFO
97768e0d42fSEd L. Cashin 				"aoe: device addtgt failure; "
97868e0d42fSEd L. Cashin 				"too many targets?\n");
97968e0d42fSEd L. Cashin 			spin_unlock_irqrestore(&d->lock, flags);
98068e0d42fSEd L. Cashin 			return;
98168e0d42fSEd L. Cashin 		}
98268e0d42fSEd L. Cashin 	}
98368e0d42fSEd L. Cashin 	ifp = getif(t, skb->dev);
98468e0d42fSEd L. Cashin 	if (!ifp) {
98568e0d42fSEd L. Cashin 		ifp = addif(t, skb->dev);
98668e0d42fSEd L. Cashin 		if (!ifp) {
98768e0d42fSEd L. Cashin 			printk(KERN_INFO
98868e0d42fSEd L. Cashin 				"aoe: device addif failure; "
98968e0d42fSEd L. Cashin 				"too many interfaces?\n");
99068e0d42fSEd L. Cashin 			spin_unlock_irqrestore(&d->lock, flags);
99168e0d42fSEd L. Cashin 			return;
99268e0d42fSEd L. Cashin 		}
99368e0d42fSEd L. Cashin 	}
99468e0d42fSEd L. Cashin 	if (ifp->maxbcnt) {
99568e0d42fSEd L. Cashin 		n = ifp->nd->mtu;
99619bf2635SEd L. Cashin 		n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
99719bf2635SEd L. Cashin 		n /= 512;
99819bf2635SEd L. Cashin 		if (n > ch->scnt)
99919bf2635SEd L. Cashin 			n = ch->scnt;
10004f51dc5eSEd L. Cashin 		n = n ? n * 512 : DEFAULTBCNT;
100168e0d42fSEd L. Cashin 		if (n != ifp->maxbcnt) {
1002a12c93f0SEd L. Cashin 			printk(KERN_INFO
100368e0d42fSEd L. Cashin 				"aoe: e%ld.%d: setting %d%s%s:%012llx\n",
100468e0d42fSEd L. Cashin 				d->aoemajor, d->aoeminor, n,
100568e0d42fSEd L. Cashin 				" byte data frames on ", ifp->nd->name,
1006*1eb0da4cSEd L. Cashin 				mac_addr(t->addr));
100768e0d42fSEd L. Cashin 			ifp->maxbcnt = n;
10084f51dc5eSEd L. Cashin 		}
100919bf2635SEd L. Cashin 	}
10103ae1c24eSEd L. Cashin 
10113ae1c24eSEd L. Cashin 	/* don't change users' perspective */
101268e0d42fSEd L. Cashin 	if (d->nopen) {
10131da177e4SLinus Torvalds 		spin_unlock_irqrestore(&d->lock, flags);
10141da177e4SLinus Torvalds 		return;
10151da177e4SLinus Torvalds 	}
101663e9cc5dSecashin@coraid.com 	d->fw_ver = be16_to_cpu(ch->fwver);
10171da177e4SLinus Torvalds 
101868e0d42fSEd L. Cashin 	sl = aoecmd_ata_id(d);
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	aoenet_xmit(sl);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
102568e0d42fSEd L. Cashin void
102668e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d)
102768e0d42fSEd L. Cashin {
102868e0d42fSEd L. Cashin 	struct aoetgt **t, **te;
102968e0d42fSEd L. Cashin 	struct aoeif *p, *e;
103068e0d42fSEd L. Cashin 
103168e0d42fSEd L. Cashin 	d->mintimer = MINTIMER;
103268e0d42fSEd L. Cashin 
103368e0d42fSEd L. Cashin 	t = d->targets;
103468e0d42fSEd L. Cashin 	te = t + NTARGETS;
103568e0d42fSEd L. Cashin 	for (; t < te && *t; t++) {
103668e0d42fSEd L. Cashin 		(*t)->maxout = (*t)->nframes;
103768e0d42fSEd L. Cashin 		p = (*t)->ifs;
103868e0d42fSEd L. Cashin 		e = p + NAOEIFS;
103968e0d42fSEd L. Cashin 		for (; p < e; p++) {
104068e0d42fSEd L. Cashin 			p->lostjumbo = 0;
104168e0d42fSEd L. Cashin 			p->lost = 0;
104268e0d42fSEd L. Cashin 			p->maxbcnt = DEFAULTBCNT;
104368e0d42fSEd L. Cashin 		}
104468e0d42fSEd L. Cashin 	}
104568e0d42fSEd L. Cashin }
1046