152e112b3SEd L. Cashin /* Copyright (c) 2007 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 217df620d8SEd L. Cashin static int aoe_maxout = 16; 227df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644); 237df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout, 247df620d8SEd L. Cashin "Only aoe_maxout outstanding packets for every MAC on eX.Y."); 257df620d8SEd L. Cashin 2668e0d42fSEd L. Cashin static struct sk_buff * 27e407a7f6SEd L. Cashin new_skb(ulong len) 281da177e4SLinus Torvalds { 291da177e4SLinus Torvalds struct sk_buff *skb; 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 321da177e4SLinus Torvalds if (skb) { 33459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 34c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 351da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 361da177e4SLinus Torvalds skb->priority = 0; 371da177e4SLinus Torvalds skb->next = skb->prev = NULL; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* tell the network layer not to perform IP checksums 401da177e4SLinus Torvalds * or to get the NIC to do it 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds return skb; 451da177e4SLinus Torvalds } 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds static struct frame * 4868e0d42fSEd L. Cashin getframe(struct aoetgt *t, int tag) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds struct frame *f, *e; 511da177e4SLinus Torvalds 5268e0d42fSEd L. Cashin f = t->frames; 5368e0d42fSEd L. Cashin e = f + t->nframes; 541da177e4SLinus Torvalds for (; f<e; f++) 551da177e4SLinus Torvalds if (f->tag == tag) 561da177e4SLinus Torvalds return f; 571da177e4SLinus Torvalds return NULL; 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* 611da177e4SLinus Torvalds * Leave the top bit clear so we have tagspace for userland. 621da177e4SLinus Torvalds * The bottom 16 bits are the xmit tick for rexmit/rttavg processing. 631da177e4SLinus Torvalds * This driver reserves tag -1 to mean "unused frame." 641da177e4SLinus Torvalds */ 651da177e4SLinus Torvalds static int 6668e0d42fSEd L. Cashin newtag(struct aoetgt *t) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds register ulong n; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds n = jiffies & 0xffff; 7168e0d42fSEd L. Cashin return n |= (++t->lasttag & 0x7fff) << 16; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds static int 7568e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) 761da177e4SLinus Torvalds { 7768e0d42fSEd L. Cashin u32 host_tag = newtag(t); 781da177e4SLinus Torvalds 7968e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 8068e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 8163e9cc5dSecashin@coraid.com h->type = __constant_cpu_to_be16(ETH_P_AOE); 821da177e4SLinus Torvalds h->verfl = AOE_HVER; 8363e9cc5dSecashin@coraid.com h->major = cpu_to_be16(d->aoemajor); 841da177e4SLinus Torvalds h->minor = d->aoeminor; 851da177e4SLinus Torvalds h->cmd = AOECMD_ATA; 8663e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(host_tag); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds return host_tag; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 9119bf2635SEd L. Cashin static inline void 9219bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba) 9319bf2635SEd L. Cashin { 9419bf2635SEd L. Cashin ah->lba0 = lba; 9519bf2635SEd L. Cashin ah->lba1 = lba >>= 8; 9619bf2635SEd L. Cashin ah->lba2 = lba >>= 8; 9719bf2635SEd L. Cashin ah->lba3 = lba >>= 8; 9819bf2635SEd L. Cashin ah->lba4 = lba >>= 8; 9919bf2635SEd L. Cashin ah->lba5 = lba >>= 8; 10019bf2635SEd L. Cashin } 10119bf2635SEd L. Cashin 1021da177e4SLinus Torvalds static void 10368e0d42fSEd L. Cashin ifrotate(struct aoetgt *t) 1041da177e4SLinus Torvalds { 10568e0d42fSEd L. Cashin t->ifp++; 10668e0d42fSEd L. Cashin if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL) 10768e0d42fSEd L. Cashin t->ifp = t->ifs; 10868e0d42fSEd L. Cashin if (t->ifp->nd == NULL) { 10968e0d42fSEd L. Cashin printk(KERN_INFO "aoe: no interface to rotate to\n"); 11068e0d42fSEd L. Cashin BUG(); 11168e0d42fSEd L. Cashin } 11268e0d42fSEd L. Cashin } 11368e0d42fSEd L. Cashin 1149bb237b6SEd L. Cashin static void 1159bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 1169bb237b6SEd L. Cashin { 117*e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->skbpool, skb); 1189bb237b6SEd L. Cashin } 1199bb237b6SEd L. Cashin 1209bb237b6SEd L. Cashin static struct sk_buff * 1219bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 1229bb237b6SEd L. Cashin { 123*e9bb8fb0SDavid S. Miller struct sk_buff *skb = skb_peek(&d->skbpool); 1249bb237b6SEd L. Cashin 1259bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 126*e9bb8fb0SDavid S. Miller __skb_unlink(skb, &d->skbpool); 1279bb237b6SEd L. Cashin return skb; 1289bb237b6SEd L. Cashin } 129*e9bb8fb0SDavid S. Miller if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 130*e9bb8fb0SDavid S. Miller (skb = new_skb(ETH_ZLEN))) 1319bb237b6SEd L. Cashin return skb; 132*e9bb8fb0SDavid S. Miller 1339bb237b6SEd L. Cashin return NULL; 1349bb237b6SEd L. Cashin } 1359bb237b6SEd L. Cashin 1369bb237b6SEd L. Cashin /* freeframe is where we do our load balancing so it's a little hairy. */ 13768e0d42fSEd L. Cashin static struct frame * 13868e0d42fSEd L. Cashin freeframe(struct aoedev *d) 13968e0d42fSEd L. Cashin { 1409bb237b6SEd L. Cashin struct frame *f, *e, *rf; 14168e0d42fSEd L. Cashin struct aoetgt **t; 1429bb237b6SEd L. Cashin struct sk_buff *skb; 14368e0d42fSEd L. Cashin 14468e0d42fSEd L. Cashin if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */ 14568e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 14668e0d42fSEd L. Cashin return NULL; 14768e0d42fSEd L. Cashin } 1489bb237b6SEd L. Cashin t = d->tgt; 1499bb237b6SEd L. Cashin t++; 1509bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 15168e0d42fSEd L. Cashin t = d->targets; 1529bb237b6SEd L. Cashin for (;;) { 1539bb237b6SEd L. Cashin if ((*t)->nout < (*t)->maxout 1549bb237b6SEd L. Cashin && t != d->htgt 1559bb237b6SEd L. Cashin && (*t)->ifp->nd) { 1569bb237b6SEd L. Cashin rf = NULL; 15768e0d42fSEd L. Cashin f = (*t)->frames; 1589bb237b6SEd L. Cashin e = f + (*t)->nframes; 15968e0d42fSEd L. Cashin for (; f < e; f++) { 16068e0d42fSEd L. Cashin if (f->tag != FREETAG) 16168e0d42fSEd L. Cashin continue; 1629bb237b6SEd L. Cashin skb = f->skb; 1639bb237b6SEd L. Cashin if (!skb 1649bb237b6SEd L. Cashin && !(f->skb = skb = new_skb(ETH_ZLEN))) 1659bb237b6SEd L. Cashin continue; 1669bb237b6SEd L. Cashin if (atomic_read(&skb_shinfo(skb)->dataref) 16768e0d42fSEd L. Cashin != 1) { 1689bb237b6SEd L. Cashin if (!rf) 1699bb237b6SEd L. Cashin rf = f; 17068e0d42fSEd L. Cashin continue; 17168e0d42fSEd L. Cashin } 1729bb237b6SEd L. Cashin gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0; 1739bb237b6SEd L. Cashin skb_trim(skb, 0); 17468e0d42fSEd L. Cashin d->tgt = t; 17568e0d42fSEd L. Cashin ifrotate(*t); 17668e0d42fSEd L. Cashin return f; 17768e0d42fSEd L. Cashin } 1789bb237b6SEd L. Cashin /* Work can be done, but the network layer is 1799bb237b6SEd L. Cashin holding our precious packets. Try to grab 1809bb237b6SEd L. Cashin one from the pool. */ 1819bb237b6SEd L. Cashin f = rf; 1829bb237b6SEd L. Cashin if (f == NULL) { /* more paranoia */ 1839bb237b6SEd L. Cashin printk(KERN_ERR 1849bb237b6SEd L. Cashin "aoe: freeframe: %s.\n", 1859bb237b6SEd L. Cashin "unexpected null rf"); 1869bb237b6SEd L. Cashin d->flags |= DEVFL_KICKME; 1879bb237b6SEd L. Cashin return NULL; 1889bb237b6SEd L. Cashin } 1899bb237b6SEd L. Cashin skb = skb_pool_get(d); 1909bb237b6SEd L. Cashin if (skb) { 1919bb237b6SEd L. Cashin skb_pool_put(d, f->skb); 1929bb237b6SEd L. Cashin f->skb = skb; 1939bb237b6SEd L. Cashin goto gotone; 1949bb237b6SEd L. Cashin } 1959bb237b6SEd L. Cashin (*t)->dataref++; 1969bb237b6SEd L. Cashin if ((*t)->nout == 0) 19768e0d42fSEd L. Cashin d->flags |= DEVFL_KICKME; 19868e0d42fSEd L. Cashin } 1999bb237b6SEd L. Cashin if (t == d->tgt) /* we've looped and found nada */ 2009bb237b6SEd L. Cashin break; 20168e0d42fSEd L. Cashin t++; 2029bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 2039bb237b6SEd L. Cashin t = d->targets; 2049bb237b6SEd L. Cashin } 20568e0d42fSEd L. Cashin return NULL; 20668e0d42fSEd L. Cashin } 20768e0d42fSEd L. Cashin 20868e0d42fSEd L. Cashin static int 20968e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 21068e0d42fSEd L. Cashin { 21168e0d42fSEd L. Cashin struct frame *f; 2121da177e4SLinus Torvalds struct aoe_hdr *h; 2131da177e4SLinus Torvalds struct aoe_atahdr *ah; 2141da177e4SLinus Torvalds struct buf *buf; 21568e0d42fSEd L. Cashin struct bio_vec *bv; 21668e0d42fSEd L. Cashin struct aoetgt *t; 2171da177e4SLinus Torvalds struct sk_buff *skb; 2181da177e4SLinus Torvalds ulong bcnt; 2191da177e4SLinus Torvalds char writebit, extbit; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds writebit = 0x10; 2221da177e4SLinus Torvalds extbit = 0x4; 2231da177e4SLinus Torvalds 22468e0d42fSEd L. Cashin f = freeframe(d); 22568e0d42fSEd L. Cashin if (f == NULL) 22668e0d42fSEd L. Cashin return 0; 22768e0d42fSEd L. Cashin t = *d->tgt; 2281da177e4SLinus Torvalds buf = d->inprocess; 22968e0d42fSEd L. Cashin bv = buf->bv; 23068e0d42fSEd L. Cashin bcnt = t->ifp->maxbcnt; 23168e0d42fSEd L. Cashin if (bcnt == 0) 23268e0d42fSEd L. Cashin bcnt = DEFAULTBCNT; 23368e0d42fSEd L. Cashin if (bcnt > buf->bv_resid) 2341da177e4SLinus Torvalds bcnt = buf->bv_resid; 2351da177e4SLinus Torvalds /* initialize the headers & frame */ 236e407a7f6SEd L. Cashin skb = f->skb; 237abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 2381da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 23919900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 24019900cdeSEd L. Cashin memset(h, 0, skb->len); 24168e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 24268e0d42fSEd L. Cashin t->nout++; 2431da177e4SLinus Torvalds f->waited = 0; 2441da177e4SLinus Torvalds f->buf = buf; 24568e0d42fSEd L. Cashin f->bufaddr = page_address(bv->bv_page) + buf->bv_off; 24619bf2635SEd L. Cashin f->bcnt = bcnt; 24768e0d42fSEd L. Cashin f->lba = buf->sector; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* set up ata header */ 2501da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 25168e0d42fSEd L. Cashin put_lba(ah, buf->sector); 2521da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 2531da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 2541da177e4SLinus Torvalds } else { 2551da177e4SLinus Torvalds extbit = 0; 2561da177e4SLinus Torvalds ah->lba3 &= 0x0f; 2571da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 26068e0d42fSEd L. Cashin skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt); 2611da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 2624f51dc5eSEd L. Cashin skb->len += bcnt; 2634f51dc5eSEd L. Cashin skb->data_len = bcnt; 26468e0d42fSEd L. Cashin t->wpkts++; 2651da177e4SLinus Torvalds } else { 26668e0d42fSEd L. Cashin t->rpkts++; 2671da177e4SLinus Torvalds writebit = 0; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds ah->cmdstat = WIN_READ | writebit | extbit; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* mark all tracking fields and load out */ 2731da177e4SLinus Torvalds buf->nframesout += 1; 27468e0d42fSEd L. Cashin buf->bv_off += bcnt; 2751da177e4SLinus Torvalds buf->bv_resid -= bcnt; 2761da177e4SLinus Torvalds buf->resid -= bcnt; 2771da177e4SLinus Torvalds buf->sector += bcnt >> 9; 2781da177e4SLinus Torvalds if (buf->resid == 0) { 2791da177e4SLinus Torvalds d->inprocess = NULL; 2801da177e4SLinus Torvalds } else if (buf->bv_resid == 0) { 28168e0d42fSEd L. Cashin buf->bv = ++bv; 28268e0d42fSEd L. Cashin buf->bv_resid = bv->bv_len; 28368e0d42fSEd L. Cashin WARN_ON(buf->bv_resid == 0); 28468e0d42fSEd L. Cashin buf->bv_off = bv->bv_offset; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 28768e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 2884f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 289*e9bb8fb0SDavid S. Miller if (skb) 290*e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 29168e0d42fSEd L. Cashin return 1; 29268e0d42fSEd L. Cashin } 2931da177e4SLinus Torvalds 2943ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 2953ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 2963ae1c24eSEd L. Cashin */ 297*e9bb8fb0SDavid S. Miller static void 298*e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 2993ae1c24eSEd L. Cashin { 3003ae1c24eSEd L. Cashin struct aoe_hdr *h; 3013ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 302*e9bb8fb0SDavid S. Miller struct sk_buff *skb; 3033ae1c24eSEd L. Cashin struct net_device *ifp; 3043ae1c24eSEd L. Cashin 3053ae1c24eSEd L. Cashin read_lock(&dev_base_lock); 306881d966bSEric W. Biederman for_each_netdev(&init_net, ifp) { 3073ae1c24eSEd L. Cashin dev_hold(ifp); 3083ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 3097562f876SPavel Emelianov goto cont; 3103ae1c24eSEd L. Cashin 311e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 3123ae1c24eSEd L. Cashin if (skb == NULL) { 313a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 3147562f876SPavel Emelianov goto cont; 3153ae1c24eSEd L. Cashin } 31619900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 317e407a7f6SEd L. Cashin skb->dev = ifp; 318*e9bb8fb0SDavid S. Miller __skb_queue_tail(queue, skb); 319abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 3203ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 3213ae1c24eSEd L. Cashin 3223ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 3233ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 3243ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 3253ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 3263ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 3273ae1c24eSEd L. Cashin h->minor = aoeminor; 3283ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 3293ae1c24eSEd L. Cashin 3307562f876SPavel Emelianov cont: 3317562f876SPavel Emelianov dev_put(ifp); 3323ae1c24eSEd L. Cashin } 3333ae1c24eSEd L. Cashin read_unlock(&dev_base_lock); 3343ae1c24eSEd L. Cashin } 3353ae1c24eSEd L. Cashin 3361da177e4SLinus Torvalds static void 33768e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct sk_buff *skb; 3401da177e4SLinus Torvalds struct aoe_hdr *h; 34119bf2635SEd L. Cashin struct aoe_atahdr *ah; 3421da177e4SLinus Torvalds char buf[128]; 3431da177e4SLinus Torvalds u32 n; 3441da177e4SLinus Torvalds 34568e0d42fSEd L. Cashin ifrotate(t); 34668e0d42fSEd L. Cashin n = newtag(t); 347e407a7f6SEd L. Cashin skb = f->skb; 348abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 34919bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 35068e0d42fSEd L. Cashin 35168e0d42fSEd L. Cashin snprintf(buf, sizeof buf, 35268e0d42fSEd L. Cashin "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x " 35368e0d42fSEd L. Cashin "s=%012llx d=%012llx nout=%d\n", 35468e0d42fSEd L. Cashin "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n, 3551eb0da4cSEd L. Cashin mac_addr(h->src), 3561eb0da4cSEd L. Cashin mac_addr(h->dst), t->nout); 35768e0d42fSEd L. Cashin aoechr_error(buf); 35868e0d42fSEd L. Cashin 3591da177e4SLinus Torvalds f->tag = n; 36063e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 36168e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 36268e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 3631da177e4SLinus Torvalds 36468e0d42fSEd L. Cashin switch (ah->cmdstat) { 36568e0d42fSEd L. Cashin default: 36668e0d42fSEd L. Cashin break; 36768e0d42fSEd L. Cashin case WIN_READ: 36868e0d42fSEd L. Cashin case WIN_READ_EXT: 36968e0d42fSEd L. Cashin case WIN_WRITE: 37068e0d42fSEd L. Cashin case WIN_WRITE_EXT: 37168e0d42fSEd L. Cashin put_lba(ah, f->lba); 37268e0d42fSEd L. Cashin 37368e0d42fSEd L. Cashin n = f->bcnt; 37468e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 37568e0d42fSEd L. Cashin n = DEFAULTBCNT; 37668e0d42fSEd L. Cashin ah->scnt = n >> 9; 3774f51dc5eSEd L. Cashin if (ah->aflags & AOEAFL_WRITE) { 37819bf2635SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 37968e0d42fSEd L. Cashin offset_in_page(f->bufaddr), n); 38068e0d42fSEd L. Cashin skb->len = sizeof *h + sizeof *ah + n; 38168e0d42fSEd L. Cashin skb->data_len = n; 38219bf2635SEd L. Cashin } 38319bf2635SEd L. Cashin } 38468e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 3854f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 3864f51dc5eSEd L. Cashin if (skb == NULL) 3874f51dc5eSEd L. Cashin return; 388*e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds static int 3921da177e4SLinus Torvalds tsince(int tag) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds int n; 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds n = jiffies & 0xffff; 3971da177e4SLinus Torvalds n -= tag & 0xffff; 3981da177e4SLinus Torvalds if (n < 0) 3991da177e4SLinus Torvalds n += 1<<16; 4001da177e4SLinus Torvalds return n; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 40368e0d42fSEd L. Cashin static struct aoeif * 40468e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 40568e0d42fSEd L. Cashin { 40668e0d42fSEd L. Cashin struct aoeif *p, *e; 40768e0d42fSEd L. Cashin 40868e0d42fSEd L. Cashin p = t->ifs; 40968e0d42fSEd L. Cashin e = p + NAOEIFS; 41068e0d42fSEd L. Cashin for (; p < e; p++) 41168e0d42fSEd L. Cashin if (p->nd == nd) 41268e0d42fSEd L. Cashin return p; 41368e0d42fSEd L. Cashin return NULL; 41468e0d42fSEd L. Cashin } 41568e0d42fSEd L. Cashin 41668e0d42fSEd L. Cashin static struct aoeif * 41768e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd) 41868e0d42fSEd L. Cashin { 41968e0d42fSEd L. Cashin struct aoeif *p; 42068e0d42fSEd L. Cashin 42168e0d42fSEd L. Cashin p = getif(t, NULL); 42268e0d42fSEd L. Cashin if (!p) 42368e0d42fSEd L. Cashin return NULL; 42468e0d42fSEd L. Cashin p->nd = nd; 42568e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 42668e0d42fSEd L. Cashin p->lost = 0; 42768e0d42fSEd L. Cashin p->lostjumbo = 0; 42868e0d42fSEd L. Cashin return p; 42968e0d42fSEd L. Cashin } 43068e0d42fSEd L. Cashin 43168e0d42fSEd L. Cashin static void 43268e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 43368e0d42fSEd L. Cashin { 43468e0d42fSEd L. Cashin struct aoeif *e; 43568e0d42fSEd L. Cashin ulong n; 43668e0d42fSEd L. Cashin 43768e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 43868e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 43968e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 44068e0d42fSEd L. Cashin e->nd = NULL; 44168e0d42fSEd L. Cashin } 44268e0d42fSEd L. Cashin 44368e0d42fSEd L. Cashin static int 44468e0d42fSEd L. Cashin sthtith(struct aoedev *d) 44568e0d42fSEd L. Cashin { 44668e0d42fSEd L. Cashin struct frame *f, *e, *nf; 44768e0d42fSEd L. Cashin struct sk_buff *skb; 44868e0d42fSEd L. Cashin struct aoetgt *ht = *d->htgt; 44968e0d42fSEd L. Cashin 45068e0d42fSEd L. Cashin f = ht->frames; 45168e0d42fSEd L. Cashin e = f + ht->nframes; 45268e0d42fSEd L. Cashin for (; f < e; f++) { 45368e0d42fSEd L. Cashin if (f->tag == FREETAG) 45468e0d42fSEd L. Cashin continue; 45568e0d42fSEd L. Cashin nf = freeframe(d); 45668e0d42fSEd L. Cashin if (!nf) 45768e0d42fSEd L. Cashin return 0; 45868e0d42fSEd L. Cashin skb = nf->skb; 45968e0d42fSEd L. Cashin *nf = *f; 46068e0d42fSEd L. Cashin f->skb = skb; 46168e0d42fSEd L. Cashin f->tag = FREETAG; 46268e0d42fSEd L. Cashin nf->waited = 0; 46368e0d42fSEd L. Cashin ht->nout--; 46468e0d42fSEd L. Cashin (*d->tgt)->nout++; 46568e0d42fSEd L. Cashin resend(d, *d->tgt, nf); 46668e0d42fSEd L. Cashin } 46768e0d42fSEd L. Cashin /* he's clean, he's useless. take away his interfaces */ 46868e0d42fSEd L. Cashin memset(ht->ifs, 0, sizeof ht->ifs); 46968e0d42fSEd L. Cashin d->htgt = NULL; 47068e0d42fSEd L. Cashin return 1; 47168e0d42fSEd L. Cashin } 47268e0d42fSEd L. Cashin 47368e0d42fSEd L. Cashin static inline unsigned char 47468e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) { 47568e0d42fSEd L. Cashin struct aoe_hdr *h; 47668e0d42fSEd L. Cashin struct aoe_atahdr *ah; 47768e0d42fSEd L. Cashin 47868e0d42fSEd L. Cashin h = (struct aoe_hdr *) packet; 47968e0d42fSEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 48068e0d42fSEd L. Cashin return ah->scnt; 48168e0d42fSEd L. Cashin } 48268e0d42fSEd L. Cashin 4831da177e4SLinus Torvalds static void 4841da177e4SLinus Torvalds rexmit_timer(ulong vp) 4851da177e4SLinus Torvalds { 486*e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 4871da177e4SLinus Torvalds struct aoedev *d; 48868e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 48968e0d42fSEd L. Cashin struct aoeif *ifp; 4901da177e4SLinus Torvalds struct frame *f, *e; 4911da177e4SLinus Torvalds register long timeout; 4921da177e4SLinus Torvalds ulong flags, n; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds d = (struct aoedev *) vp; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* timeout is always ~150% of the moving average */ 4971da177e4SLinus Torvalds timeout = d->rttavg; 4981da177e4SLinus Torvalds timeout += timeout >> 1; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 5031c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 5041da177e4SLinus Torvalds return; 5051da177e4SLinus Torvalds } 50668e0d42fSEd L. Cashin tt = d->targets; 50768e0d42fSEd L. Cashin te = tt + NTARGETS; 50868e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) { 50968e0d42fSEd L. Cashin t = *tt; 51068e0d42fSEd L. Cashin f = t->frames; 51168e0d42fSEd L. Cashin e = f + t->nframes; 5121da177e4SLinus Torvalds for (; f < e; f++) { 51368e0d42fSEd L. Cashin if (f->tag == FREETAG 51468e0d42fSEd L. Cashin || tsince(f->tag) < timeout) 51568e0d42fSEd L. Cashin continue; 5161da177e4SLinus Torvalds n = f->waited += timeout; 5171da177e4SLinus Torvalds n /= HZ; 51868e0d42fSEd L. Cashin if (n > aoe_deadsecs) { 51968e0d42fSEd L. Cashin /* waited too long. device failure. */ 5201da177e4SLinus Torvalds aoedev_downdev(d); 5211c6f3fcaSEd L. Cashin break; 5221da177e4SLinus Torvalds } 52368e0d42fSEd L. Cashin 52468e0d42fSEd L. Cashin if (n > HELPWAIT /* see if another target can help */ 52568e0d42fSEd L. Cashin && (tt != d->targets || d->targets[1])) 52668e0d42fSEd L. Cashin d->htgt = tt; 52768e0d42fSEd L. Cashin 52868e0d42fSEd L. Cashin if (t->nout == t->maxout) { 52968e0d42fSEd L. Cashin if (t->maxout > 1) 53068e0d42fSEd L. Cashin t->maxout--; 53168e0d42fSEd L. Cashin t->lastwadj = jiffies; 53268e0d42fSEd L. Cashin } 53368e0d42fSEd L. Cashin 53468e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 53568e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 53668e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 53768e0d42fSEd L. Cashin ejectif(t, ifp); 53868e0d42fSEd L. Cashin ifp = NULL; 53968e0d42fSEd L. Cashin } 54068e0d42fSEd L. Cashin 54168e0d42fSEd L. Cashin if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512 54268e0d42fSEd L. Cashin && ifp && ++ifp->lostjumbo > (t->nframes << 1) 54368e0d42fSEd L. Cashin && ifp->maxbcnt != DEFAULTBCNT) { 54468e0d42fSEd L. Cashin printk(KERN_INFO 54568e0d42fSEd L. Cashin "aoe: e%ld.%d: " 54668e0d42fSEd L. Cashin "too many lost jumbo on " 54768e0d42fSEd L. Cashin "%s:%012llx - " 54868e0d42fSEd L. Cashin "falling back to %d frames.\n", 54968e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, 55068e0d42fSEd L. Cashin ifp->nd->name, mac_addr(t->addr), 55168e0d42fSEd L. Cashin DEFAULTBCNT); 55268e0d42fSEd L. Cashin ifp->maxbcnt = 0; 55368e0d42fSEd L. Cashin } 55468e0d42fSEd L. Cashin resend(d, t, f); 55568e0d42fSEd L. Cashin } 55668e0d42fSEd L. Cashin 55768e0d42fSEd L. Cashin /* window check */ 55868e0d42fSEd L. Cashin if (t->nout == t->maxout 55968e0d42fSEd L. Cashin && t->maxout < t->nframes 56068e0d42fSEd L. Cashin && (jiffies - t->lastwadj)/HZ > 10) { 56168e0d42fSEd L. Cashin t->maxout++; 56268e0d42fSEd L. Cashin t->lastwadj = jiffies; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds } 56568e0d42fSEd L. Cashin 566*e9bb8fb0SDavid S. Miller if (!skb_queue_empty(&d->sendq)) { 56768e0d42fSEd L. Cashin n = d->rttavg <<= 1; 56868e0d42fSEd L. Cashin if (n > MAXTIMER) 56968e0d42fSEd L. Cashin d->rttavg = MAXTIMER; 57068e0d42fSEd L. Cashin } 57168e0d42fSEd L. Cashin 57268e0d42fSEd L. Cashin if (d->flags & DEVFL_KICKME || d->htgt) { 5734f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 5744f51dc5eSEd L. Cashin aoecmd_work(d); 5754f51dc5eSEd L. Cashin } 5761da177e4SLinus Torvalds 577*e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 578*e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 5811da177e4SLinus Torvalds add_timer(&d->timer); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5841da177e4SLinus Torvalds 585*e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 58868e0d42fSEd L. Cashin /* enters with d->lock held */ 58968e0d42fSEd L. Cashin void 59068e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 59168e0d42fSEd L. Cashin { 59268e0d42fSEd L. Cashin struct buf *buf; 59368e0d42fSEd L. Cashin loop: 59468e0d42fSEd L. Cashin if (d->htgt && !sthtith(d)) 59568e0d42fSEd L. Cashin return; 59668e0d42fSEd L. Cashin if (d->inprocess == NULL) { 59768e0d42fSEd L. Cashin if (list_empty(&d->bufq)) 59868e0d42fSEd L. Cashin return; 59968e0d42fSEd L. Cashin buf = container_of(d->bufq.next, struct buf, bufs); 60068e0d42fSEd L. Cashin list_del(d->bufq.next); 60168e0d42fSEd L. Cashin d->inprocess = buf; 60268e0d42fSEd L. Cashin } 60368e0d42fSEd L. Cashin if (aoecmd_ata_rw(d)) 60468e0d42fSEd L. Cashin goto loop; 60568e0d42fSEd L. Cashin } 60668e0d42fSEd L. Cashin 6073ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 6083ae1c24eSEd L. Cashin */ 6093ae1c24eSEd L. Cashin void 610c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 6113ae1c24eSEd L. Cashin { 612c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 6133ae1c24eSEd L. Cashin 6143ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 6153ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 6163ae1c24eSEd L. Cashin 6173ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 6183ae1c24eSEd L. Cashin struct block_device *bd; 6193ae1c24eSEd L. Cashin unsigned long flags; 6203ae1c24eSEd L. Cashin u64 ssize; 6213ae1c24eSEd L. Cashin 6223ae1c24eSEd L. Cashin ssize = d->gd->capacity; 6233ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 6243ae1c24eSEd L. Cashin 6253ae1c24eSEd L. Cashin if (bd) { 6263ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 6273ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 6283ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 6293ae1c24eSEd L. Cashin bdput(bd); 6303ae1c24eSEd L. Cashin } 6313ae1c24eSEd L. Cashin spin_lock_irqsave(&d->lock, flags); 6323ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 6333ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 6343ae1c24eSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 6353ae1c24eSEd L. Cashin } 6363ae1c24eSEd L. Cashin } 6373ae1c24eSEd L. Cashin 6381da177e4SLinus Torvalds static void 63968e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 6401da177e4SLinus Torvalds { 6411da177e4SLinus Torvalds u64 ssize; 6421da177e4SLinus Torvalds u16 n; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* word 83: command set supported */ 645f885f8d1SHarvey Harrison n = get_unaligned_le16(&id[83 << 1]); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 648f885f8d1SHarvey Harrison n |= get_unaligned_le16(&id[86 << 1]); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 6511da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 654f885f8d1SHarvey Harrison ssize = get_unaligned_le64(&id[100 << 1]); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 6571da177e4SLinus Torvalds d->geo.cylinders = ssize; 6581da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 6591da177e4SLinus Torvalds d->geo.heads = 255; 6601da177e4SLinus Torvalds d->geo.sectors = 63; 6611da177e4SLinus Torvalds } else { 6621da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* number lba28 sectors */ 665f885f8d1SHarvey Harrison ssize = get_unaligned_le32(&id[60 << 1]); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 668f885f8d1SHarvey Harrison d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 669f885f8d1SHarvey Harrison d->geo.heads = get_unaligned_le16(&id[55 << 1]); 670f885f8d1SHarvey Harrison d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 6711da177e4SLinus Torvalds } 6723ae1c24eSEd L. Cashin 6733ae1c24eSEd L. Cashin if (d->ssize != ssize) 6741d75981aSEd L. Cashin printk(KERN_INFO 6751d75981aSEd L. Cashin "aoe: %012llx e%ld.%d v%04x has %llu sectors\n", 6761eb0da4cSEd L. Cashin mac_addr(t->addr), 6773ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 6783ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 6791da177e4SLinus Torvalds d->ssize = ssize; 6801da177e4SLinus Torvalds d->geo.start = 0; 6816b9699bbSEd L. Cashin if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 6826b9699bbSEd L. Cashin return; 6831da177e4SLinus Torvalds if (d->gd != NULL) { 6841da177e4SLinus Torvalds d->gd->capacity = ssize; 6853ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 68668e0d42fSEd L. Cashin } else 6873ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 6881da177e4SLinus Torvalds schedule_work(&d->work); 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds static void 6921da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds register long n; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds n = rtt; 697dced3a05SEd L. Cashin if (n < 0) { 698dced3a05SEd L. Cashin n = -rtt; 6991da177e4SLinus Torvalds if (n < MINTIMER) 7001da177e4SLinus Torvalds n = MINTIMER; 7011da177e4SLinus Torvalds else if (n > MAXTIMER) 7021da177e4SLinus Torvalds n = MAXTIMER; 703dced3a05SEd L. Cashin d->mintimer += (n - d->mintimer) >> 1; 704dced3a05SEd L. Cashin } else if (n < d->mintimer) 705dced3a05SEd L. Cashin n = d->mintimer; 706dced3a05SEd L. Cashin else if (n > MAXTIMER) 707dced3a05SEd L. Cashin n = MAXTIMER; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */ 7101da177e4SLinus Torvalds n -= d->rttavg; 7111da177e4SLinus Torvalds d->rttavg += n >> 2; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 71468e0d42fSEd L. Cashin static struct aoetgt * 71568e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 71668e0d42fSEd L. Cashin { 71768e0d42fSEd L. Cashin struct aoetgt **t, **e; 71868e0d42fSEd L. Cashin 71968e0d42fSEd L. Cashin t = d->targets; 72068e0d42fSEd L. Cashin e = t + NTARGETS; 72168e0d42fSEd L. Cashin for (; t < e && *t; t++) 72268e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 72368e0d42fSEd L. Cashin return *t; 72468e0d42fSEd L. Cashin return NULL; 72568e0d42fSEd L. Cashin } 72668e0d42fSEd L. Cashin 72768e0d42fSEd L. Cashin static inline void 72803054de1SLinus Torvalds diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector) 72968e0d42fSEd L. Cashin { 73068e0d42fSEd L. Cashin unsigned long n_sect = bio->bi_size >> 9; 73168e0d42fSEd L. Cashin const int rw = bio_data_dir(bio); 73228f13702SJens Axboe struct hd_struct *part; 73368e0d42fSEd L. Cashin 73428f13702SJens Axboe part = get_part(disk, sector); 73528f13702SJens Axboe all_stat_inc(disk, part, ios[rw], sector); 73628f13702SJens Axboe all_stat_add(disk, part, ticks[rw], duration, sector); 73728f13702SJens Axboe all_stat_add(disk, part, sectors[rw], n_sect, sector); 73828f13702SJens Axboe all_stat_add(disk, part, io_ticks, duration, sector); 73968e0d42fSEd L. Cashin } 74068e0d42fSEd L. Cashin 7411da177e4SLinus Torvalds void 7421da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb) 7431da177e4SLinus Torvalds { 744*e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 7451da177e4SLinus Torvalds struct aoedev *d; 746ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 7471da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 7481da177e4SLinus Torvalds struct frame *f; 7491da177e4SLinus Torvalds struct buf *buf; 75068e0d42fSEd L. Cashin struct aoetgt *t; 75168e0d42fSEd L. Cashin struct aoeif *ifp; 7521da177e4SLinus Torvalds register long n; 7531da177e4SLinus Torvalds ulong flags; 7541da177e4SLinus Torvalds char ebuf[128]; 75532465c65Secashin@coraid.com u16 aoemajor; 7561da177e4SLinus Torvalds 757abdbf94dSEd L. Cashin hin = (struct aoe_hdr *) skb_mac_header(skb); 758f885f8d1SHarvey Harrison aoemajor = get_unaligned_be16(&hin->major); 75932465c65Secashin@coraid.com d = aoedev_by_aoeaddr(aoemajor, hin->minor); 7601da177e4SLinus Torvalds if (d == NULL) { 7611da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 7621da177e4SLinus Torvalds "for unknown device %d.%d\n", 76332465c65Secashin@coraid.com aoemajor, hin->minor); 7641da177e4SLinus Torvalds aoechr_error(ebuf); 7651da177e4SLinus Torvalds return; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 7691da177e4SLinus Torvalds 770f885f8d1SHarvey Harrison n = get_unaligned_be32(&hin->tag); 77168e0d42fSEd L. Cashin t = gettgt(d, hin->src); 77268e0d42fSEd L. Cashin if (t == NULL) { 77368e0d42fSEd L. Cashin printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n", 7741eb0da4cSEd L. Cashin d->aoemajor, d->aoeminor, mac_addr(hin->src)); 77568e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 77668e0d42fSEd L. Cashin return; 77768e0d42fSEd L. Cashin } 77868e0d42fSEd L. Cashin f = getframe(t, n); 7791da177e4SLinus Torvalds if (f == NULL) { 780dced3a05SEd L. Cashin calc_rttavg(d, -tsince(n)); 7811da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7821da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, 7831da177e4SLinus Torvalds "%15s e%d.%d tag=%08x@%08lx\n", 7841da177e4SLinus Torvalds "unexpected rsp", 785f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 7861da177e4SLinus Torvalds hin->minor, 787f885f8d1SHarvey Harrison get_unaligned_be32(&hin->tag), 7881da177e4SLinus Torvalds jiffies); 7891da177e4SLinus Torvalds aoechr_error(ebuf); 7901da177e4SLinus Torvalds return; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds calc_rttavg(d, tsince(f->tag)); 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds ahin = (struct aoe_atahdr *) (hin+1); 796abdbf94dSEd L. Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 797ddec63e8SEd L. Cashin ahout = (struct aoe_atahdr *) (hout+1); 7981da177e4SLinus Torvalds buf = f->buf; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 801a12c93f0SEd L. Cashin printk(KERN_ERR 8021d75981aSEd L. Cashin "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 8031da177e4SLinus Torvalds ahout->cmdstat, ahin->cmdstat, 8041da177e4SLinus Torvalds d->aoemajor, d->aoeminor); 8051da177e4SLinus Torvalds if (buf) 8061da177e4SLinus Torvalds buf->flags |= BUFFL_FAIL; 8071da177e4SLinus Torvalds } else { 80868e0d42fSEd L. Cashin if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */ 80968e0d42fSEd L. Cashin d->htgt = NULL; 81019bf2635SEd L. Cashin n = ahout->scnt << 9; 8111da177e4SLinus Torvalds switch (ahout->cmdstat) { 8121da177e4SLinus Torvalds case WIN_READ: 8131da177e4SLinus Torvalds case WIN_READ_EXT: 8141da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < n) { 815a12c93f0SEd L. Cashin printk(KERN_ERR 81668e0d42fSEd L. Cashin "aoe: %s. skb->len=%d need=%ld\n", 81768e0d42fSEd L. Cashin "runt data size in read", skb->len, n); 8181da177e4SLinus Torvalds /* fail frame f? just returning will rexmit. */ 8191da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8201da177e4SLinus Torvalds return; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds memcpy(f->bufaddr, ahin+1, n); 8231da177e4SLinus Torvalds case WIN_WRITE: 8241da177e4SLinus Torvalds case WIN_WRITE_EXT: 82568e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 82668e0d42fSEd L. Cashin if (ifp) { 82768e0d42fSEd L. Cashin ifp->lost = 0; 82868e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 82968e0d42fSEd L. Cashin ifp->lostjumbo = 0; 83068e0d42fSEd L. Cashin } 83119bf2635SEd L. Cashin if (f->bcnt -= n) { 83268e0d42fSEd L. Cashin f->lba += n >> 9; 83319bf2635SEd L. Cashin f->bufaddr += n; 83468e0d42fSEd L. Cashin resend(d, t, f); 83568e0d42fSEd L. Cashin goto xmit; 8364f51dc5eSEd L. Cashin } 8371da177e4SLinus Torvalds break; 8381da177e4SLinus Torvalds case WIN_IDENTIFY: 8391da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < 512) { 840a12c93f0SEd L. Cashin printk(KERN_INFO 841a12c93f0SEd L. Cashin "aoe: runt data size in ataid. skb->len=%d\n", 8426bb6285fSEd L. Cashin skb->len); 8431da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8441da177e4SLinus Torvalds return; 8451da177e4SLinus Torvalds } 84668e0d42fSEd L. Cashin ataid_complete(d, t, (char *) (ahin+1)); 8471da177e4SLinus Torvalds break; 8481da177e4SLinus Torvalds default: 849a12c93f0SEd L. Cashin printk(KERN_INFO 850a12c93f0SEd L. Cashin "aoe: unrecognized ata command %2.2Xh for %d.%d\n", 8511da177e4SLinus Torvalds ahout->cmdstat, 852f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 8531da177e4SLinus Torvalds hin->minor); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 85768e0d42fSEd L. Cashin if (buf && --buf->nframesout == 0 && buf->resid == 0) { 85803054de1SLinus Torvalds diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector); 8591da177e4SLinus Torvalds n = (buf->flags & BUFFL_FAIL) ? -EIO : 0; 8606712ecf8SNeilBrown bio_endio(buf->bio, n); 8611da177e4SLinus Torvalds mempool_free(buf, d->bufpool); 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds f->buf = NULL; 8651da177e4SLinus Torvalds f->tag = FREETAG; 86668e0d42fSEd L. Cashin t->nout--; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds aoecmd_work(d); 86968e0d42fSEd L. Cashin xmit: 870*e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 871*e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 874*e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds void 8781da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 8791da177e4SLinus Torvalds { 880*e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 8811da177e4SLinus Torvalds 882*e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 883*e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 884*e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds 88768e0d42fSEd L. Cashin struct sk_buff * 8881da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds struct aoe_hdr *h; 8911da177e4SLinus Torvalds struct aoe_atahdr *ah; 8921da177e4SLinus Torvalds struct frame *f; 8931da177e4SLinus Torvalds struct sk_buff *skb; 89468e0d42fSEd L. Cashin struct aoetgt *t; 8951da177e4SLinus Torvalds 8964f51dc5eSEd L. Cashin f = freeframe(d); 89768e0d42fSEd L. Cashin if (f == NULL) 8981da177e4SLinus Torvalds return NULL; 89968e0d42fSEd L. Cashin 90068e0d42fSEd L. Cashin t = *d->tgt; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* initialize the headers & frame */ 903e407a7f6SEd L. Cashin skb = f->skb; 904abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9051da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 90619900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 90719900cdeSEd L. Cashin memset(h, 0, skb->len); 90868e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 90968e0d42fSEd L. Cashin t->nout++; 9101da177e4SLinus Torvalds f->waited = 0; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* set up ata header */ 9131da177e4SLinus Torvalds ah->scnt = 1; 9141da177e4SLinus Torvalds ah->cmdstat = WIN_IDENTIFY; 9151da177e4SLinus Torvalds ah->lba3 = 0xa0; 9161da177e4SLinus Torvalds 91768e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 9181da177e4SLinus Torvalds 9193ae1c24eSEd L. Cashin d->rttavg = MAXTIMER; 9201da177e4SLinus Torvalds d->timer.function = rexmit_timer; 9211da177e4SLinus Torvalds 9224f51dc5eSEd L. Cashin return skb_clone(skb, GFP_ATOMIC); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 92568e0d42fSEd L. Cashin static struct aoetgt * 92668e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes) 92768e0d42fSEd L. Cashin { 92868e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 92968e0d42fSEd L. Cashin struct frame *f, *e; 93068e0d42fSEd L. Cashin 93168e0d42fSEd L. Cashin tt = d->targets; 93268e0d42fSEd L. Cashin te = tt + NTARGETS; 93368e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) 93468e0d42fSEd L. Cashin ; 93568e0d42fSEd L. Cashin 936578c4aa0SEd L. Cashin if (tt == te) { 937578c4aa0SEd L. Cashin printk(KERN_INFO 938578c4aa0SEd L. Cashin "aoe: device addtgt failure; too many targets\n"); 93968e0d42fSEd L. Cashin return NULL; 940578c4aa0SEd L. Cashin } 94168e0d42fSEd L. Cashin t = kcalloc(1, sizeof *t, GFP_ATOMIC); 94268e0d42fSEd L. Cashin f = kcalloc(nframes, sizeof *f, GFP_ATOMIC); 943578c4aa0SEd L. Cashin if (!t || !f) { 944578c4aa0SEd L. Cashin kfree(f); 9459bb237b6SEd L. Cashin kfree(t); 946578c4aa0SEd L. Cashin printk(KERN_INFO "aoe: cannot allocate memory to add target\n"); 9479bb237b6SEd L. Cashin return NULL; 9489bb237b6SEd L. Cashin } 9499bb237b6SEd L. Cashin 95068e0d42fSEd L. Cashin t->nframes = nframes; 95168e0d42fSEd L. Cashin t->frames = f; 95268e0d42fSEd L. Cashin e = f + nframes; 9539bb237b6SEd L. Cashin for (; f < e; f++) 95468e0d42fSEd L. Cashin f->tag = FREETAG; 95568e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 95668e0d42fSEd L. Cashin t->ifp = t->ifs; 95768e0d42fSEd L. Cashin t->maxout = t->nframes; 95868e0d42fSEd L. Cashin return *tt = t; 95968e0d42fSEd L. Cashin } 96068e0d42fSEd L. Cashin 9611da177e4SLinus Torvalds void 9621da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds struct aoedev *d; 9651da177e4SLinus Torvalds struct aoe_hdr *h; 9661da177e4SLinus Torvalds struct aoe_cfghdr *ch; 96768e0d42fSEd L. Cashin struct aoetgt *t; 96868e0d42fSEd L. Cashin struct aoeif *ifp; 96963e9cc5dSecashin@coraid.com ulong flags, sysminor, aoemajor; 9701da177e4SLinus Torvalds struct sk_buff *sl; 97119bf2635SEd L. Cashin u16 n; 9721da177e4SLinus Torvalds 973abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9741da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 9781da177e4SLinus Torvalds * warrant a loud message for this special case. 9791da177e4SLinus Torvalds */ 980823ed72eSHarvey Harrison aoemajor = get_unaligned_be16(&h->major); 9811da177e4SLinus Torvalds if (aoemajor == 0xfff) { 982a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 9836bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 9841da177e4SLinus Torvalds return; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds sysminor = SYSMINOR(aoemajor, h->minor); 988fc458dcdSecashin@coraid.com if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) { 989a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n", 990fc458dcdSecashin@coraid.com aoemajor, (int) h->minor); 9911da177e4SLinus Torvalds return; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 99419bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 9957df620d8SEd L. Cashin if (n > aoe_maxout) /* keep it reasonable */ 9967df620d8SEd L. Cashin n = aoe_maxout; 9971da177e4SLinus Torvalds 99868e0d42fSEd L. Cashin d = aoedev_by_sysminor_m(sysminor); 9991da177e4SLinus Torvalds if (d == NULL) { 1000a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: device sysminor_m failure\n"); 10011da177e4SLinus Torvalds return; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 10051da177e4SLinus Torvalds 100668e0d42fSEd L. Cashin t = gettgt(d, h->src); 100768e0d42fSEd L. Cashin if (!t) { 100868e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 100968e0d42fSEd L. Cashin if (!t) { 101068e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 101168e0d42fSEd L. Cashin return; 101268e0d42fSEd L. Cashin } 101368e0d42fSEd L. Cashin } 101468e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 101568e0d42fSEd L. Cashin if (!ifp) { 101668e0d42fSEd L. Cashin ifp = addif(t, skb->dev); 101768e0d42fSEd L. Cashin if (!ifp) { 101868e0d42fSEd L. Cashin printk(KERN_INFO 101968e0d42fSEd L. Cashin "aoe: device addif failure; " 102068e0d42fSEd L. Cashin "too many interfaces?\n"); 102168e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 102268e0d42fSEd L. Cashin return; 102368e0d42fSEd L. Cashin } 102468e0d42fSEd L. Cashin } 102568e0d42fSEd L. Cashin if (ifp->maxbcnt) { 102668e0d42fSEd L. Cashin n = ifp->nd->mtu; 102719bf2635SEd L. Cashin n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr); 102819bf2635SEd L. Cashin n /= 512; 102919bf2635SEd L. Cashin if (n > ch->scnt) 103019bf2635SEd L. Cashin n = ch->scnt; 10314f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 103268e0d42fSEd L. Cashin if (n != ifp->maxbcnt) { 1033a12c93f0SEd L. Cashin printk(KERN_INFO 103468e0d42fSEd L. Cashin "aoe: e%ld.%d: setting %d%s%s:%012llx\n", 103568e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, n, 103668e0d42fSEd L. Cashin " byte data frames on ", ifp->nd->name, 10371eb0da4cSEd L. Cashin mac_addr(t->addr)); 103868e0d42fSEd L. Cashin ifp->maxbcnt = n; 10394f51dc5eSEd L. Cashin } 104019bf2635SEd L. Cashin } 10413ae1c24eSEd L. Cashin 10423ae1c24eSEd L. Cashin /* don't change users' perspective */ 104368e0d42fSEd L. Cashin if (d->nopen) { 10441da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10451da177e4SLinus Torvalds return; 10461da177e4SLinus Torvalds } 104763e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 10481da177e4SLinus Torvalds 104968e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10521da177e4SLinus Torvalds 1053*e9bb8fb0SDavid S. Miller if (sl) { 1054*e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 1055*e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1056*e9bb8fb0SDavid S. Miller __skb_queue_tail(&queue, sl); 1057*e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 1058*e9bb8fb0SDavid S. Miller } 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 106168e0d42fSEd L. Cashin void 106268e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 106368e0d42fSEd L. Cashin { 106468e0d42fSEd L. Cashin struct aoetgt **t, **te; 106568e0d42fSEd L. Cashin struct aoeif *p, *e; 106668e0d42fSEd L. Cashin 106768e0d42fSEd L. Cashin d->mintimer = MINTIMER; 106868e0d42fSEd L. Cashin 106968e0d42fSEd L. Cashin t = d->targets; 107068e0d42fSEd L. Cashin te = t + NTARGETS; 107168e0d42fSEd L. Cashin for (; t < te && *t; t++) { 107268e0d42fSEd L. Cashin (*t)->maxout = (*t)->nframes; 107368e0d42fSEd L. Cashin p = (*t)->ifs; 107468e0d42fSEd L. Cashin e = p + NAOEIFS; 107568e0d42fSEd L. Cashin for (; p < e; p++) { 107668e0d42fSEd L. Cashin p->lostjumbo = 0; 107768e0d42fSEd L. Cashin p->lost = 0; 107868e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 107968e0d42fSEd L. Cashin } 108068e0d42fSEd L. Cashin } 108168e0d42fSEd L. Cashin } 1082