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 109*9bb237b6SEd L. Cashin static void 110*9bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 111*9bb237b6SEd L. Cashin { 112*9bb237b6SEd L. Cashin if (!d->skbpool_hd) 113*9bb237b6SEd L. Cashin d->skbpool_hd = skb; 114*9bb237b6SEd L. Cashin else 115*9bb237b6SEd L. Cashin d->skbpool_tl->next = skb; 116*9bb237b6SEd L. Cashin d->skbpool_tl = skb; 117*9bb237b6SEd L. Cashin } 118*9bb237b6SEd L. Cashin 119*9bb237b6SEd L. Cashin static struct sk_buff * 120*9bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 121*9bb237b6SEd L. Cashin { 122*9bb237b6SEd L. Cashin struct sk_buff *skb; 123*9bb237b6SEd L. Cashin 124*9bb237b6SEd L. Cashin skb = d->skbpool_hd; 125*9bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 126*9bb237b6SEd L. Cashin d->skbpool_hd = skb->next; 127*9bb237b6SEd L. Cashin skb->next = NULL; 128*9bb237b6SEd L. Cashin return skb; 129*9bb237b6SEd L. Cashin } 130*9bb237b6SEd L. Cashin if (d->nskbpool < NSKBPOOLMAX 131*9bb237b6SEd L. Cashin && (skb = new_skb(ETH_ZLEN))) { 132*9bb237b6SEd L. Cashin d->nskbpool++; 133*9bb237b6SEd L. Cashin return skb; 134*9bb237b6SEd L. Cashin } 135*9bb237b6SEd L. Cashin return NULL; 136*9bb237b6SEd L. Cashin } 137*9bb237b6SEd L. Cashin 138*9bb237b6SEd L. Cashin /* freeframe is where we do our load balancing so it's a little hairy. */ 13968e0d42fSEd L. Cashin static struct frame * 14068e0d42fSEd L. Cashin freeframe(struct aoedev *d) 14168e0d42fSEd L. Cashin { 142*9bb237b6SEd L. Cashin struct frame *f, *e, *rf; 14368e0d42fSEd L. Cashin struct aoetgt **t; 144*9bb237b6SEd L. Cashin struct sk_buff *skb; 14568e0d42fSEd L. Cashin 14668e0d42fSEd L. Cashin if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */ 14768e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 14868e0d42fSEd L. Cashin return NULL; 14968e0d42fSEd L. Cashin } 150*9bb237b6SEd L. Cashin t = d->tgt; 151*9bb237b6SEd L. Cashin t++; 152*9bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 15368e0d42fSEd L. Cashin t = d->targets; 154*9bb237b6SEd L. Cashin for (;;) { 155*9bb237b6SEd L. Cashin if ((*t)->nout < (*t)->maxout 156*9bb237b6SEd L. Cashin && t != d->htgt 157*9bb237b6SEd L. Cashin && (*t)->ifp->nd) { 158*9bb237b6SEd L. Cashin rf = NULL; 15968e0d42fSEd L. Cashin f = (*t)->frames; 160*9bb237b6SEd L. Cashin e = f + (*t)->nframes; 16168e0d42fSEd L. Cashin for (; f < e; f++) { 16268e0d42fSEd L. Cashin if (f->tag != FREETAG) 16368e0d42fSEd L. Cashin continue; 164*9bb237b6SEd L. Cashin skb = f->skb; 165*9bb237b6SEd L. Cashin if (!skb 166*9bb237b6SEd L. Cashin && !(f->skb = skb = new_skb(ETH_ZLEN))) 167*9bb237b6SEd L. Cashin continue; 168*9bb237b6SEd L. Cashin if (atomic_read(&skb_shinfo(skb)->dataref) 16968e0d42fSEd L. Cashin != 1) { 170*9bb237b6SEd L. Cashin if (!rf) 171*9bb237b6SEd L. Cashin rf = f; 17268e0d42fSEd L. Cashin continue; 17368e0d42fSEd L. Cashin } 174*9bb237b6SEd L. Cashin gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0; 175*9bb237b6SEd L. Cashin skb_trim(skb, 0); 17668e0d42fSEd L. Cashin d->tgt = t; 17768e0d42fSEd L. Cashin ifrotate(*t); 17868e0d42fSEd L. Cashin return f; 17968e0d42fSEd L. Cashin } 180*9bb237b6SEd L. Cashin /* Work can be done, but the network layer is 181*9bb237b6SEd L. Cashin holding our precious packets. Try to grab 182*9bb237b6SEd L. Cashin one from the pool. */ 183*9bb237b6SEd L. Cashin f = rf; 184*9bb237b6SEd L. Cashin if (f == NULL) { /* more paranoia */ 185*9bb237b6SEd L. Cashin printk(KERN_ERR 186*9bb237b6SEd L. Cashin "aoe: freeframe: %s.\n", 187*9bb237b6SEd L. Cashin "unexpected null rf"); 188*9bb237b6SEd L. Cashin d->flags |= DEVFL_KICKME; 189*9bb237b6SEd L. Cashin return NULL; 190*9bb237b6SEd L. Cashin } 191*9bb237b6SEd L. Cashin skb = skb_pool_get(d); 192*9bb237b6SEd L. Cashin if (skb) { 193*9bb237b6SEd L. Cashin skb_pool_put(d, f->skb); 194*9bb237b6SEd L. Cashin f->skb = skb; 195*9bb237b6SEd L. Cashin goto gotone; 196*9bb237b6SEd L. Cashin } 197*9bb237b6SEd L. Cashin (*t)->dataref++; 198*9bb237b6SEd L. Cashin if ((*t)->nout == 0) 19968e0d42fSEd L. Cashin d->flags |= DEVFL_KICKME; 20068e0d42fSEd L. Cashin } 201*9bb237b6SEd L. Cashin if (t == d->tgt) /* we've looped and found nada */ 202*9bb237b6SEd L. Cashin break; 20368e0d42fSEd L. Cashin t++; 204*9bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 205*9bb237b6SEd L. Cashin t = d->targets; 206*9bb237b6SEd L. Cashin } 20768e0d42fSEd L. Cashin return NULL; 20868e0d42fSEd L. Cashin } 20968e0d42fSEd L. Cashin 21068e0d42fSEd L. Cashin static int 21168e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 21268e0d42fSEd L. Cashin { 21368e0d42fSEd L. Cashin struct frame *f; 2141da177e4SLinus Torvalds struct aoe_hdr *h; 2151da177e4SLinus Torvalds struct aoe_atahdr *ah; 2161da177e4SLinus Torvalds struct buf *buf; 21768e0d42fSEd L. Cashin struct bio_vec *bv; 21868e0d42fSEd L. Cashin struct aoetgt *t; 2191da177e4SLinus Torvalds struct sk_buff *skb; 2201da177e4SLinus Torvalds ulong bcnt; 2211da177e4SLinus Torvalds char writebit, extbit; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds writebit = 0x10; 2241da177e4SLinus Torvalds extbit = 0x4; 2251da177e4SLinus Torvalds 22668e0d42fSEd L. Cashin f = freeframe(d); 22768e0d42fSEd L. Cashin if (f == NULL) 22868e0d42fSEd L. Cashin return 0; 22968e0d42fSEd L. Cashin t = *d->tgt; 2301da177e4SLinus Torvalds buf = d->inprocess; 23168e0d42fSEd L. Cashin bv = buf->bv; 23268e0d42fSEd L. Cashin bcnt = t->ifp->maxbcnt; 23368e0d42fSEd L. Cashin if (bcnt == 0) 23468e0d42fSEd L. Cashin bcnt = DEFAULTBCNT; 23568e0d42fSEd L. Cashin if (bcnt > buf->bv_resid) 2361da177e4SLinus Torvalds bcnt = buf->bv_resid; 2371da177e4SLinus Torvalds /* initialize the headers & frame */ 238e407a7f6SEd L. Cashin skb = f->skb; 239abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 2401da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 24119900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 24219900cdeSEd L. Cashin memset(h, 0, skb->len); 24368e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 24468e0d42fSEd L. Cashin t->nout++; 2451da177e4SLinus Torvalds f->waited = 0; 2461da177e4SLinus Torvalds f->buf = buf; 24768e0d42fSEd L. Cashin f->bufaddr = page_address(bv->bv_page) + buf->bv_off; 24819bf2635SEd L. Cashin f->bcnt = bcnt; 24968e0d42fSEd L. Cashin f->lba = buf->sector; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /* set up ata header */ 2521da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 25368e0d42fSEd L. Cashin put_lba(ah, buf->sector); 2541da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 2551da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 2561da177e4SLinus Torvalds } else { 2571da177e4SLinus Torvalds extbit = 0; 2581da177e4SLinus Torvalds ah->lba3 &= 0x0f; 2591da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 26268e0d42fSEd L. Cashin skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt); 2631da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 2644f51dc5eSEd L. Cashin skb->len += bcnt; 2654f51dc5eSEd L. Cashin skb->data_len = bcnt; 26668e0d42fSEd L. Cashin t->wpkts++; 2671da177e4SLinus Torvalds } else { 26868e0d42fSEd L. Cashin t->rpkts++; 2691da177e4SLinus Torvalds writebit = 0; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds ah->cmdstat = WIN_READ | writebit | extbit; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* mark all tracking fields and load out */ 2751da177e4SLinus Torvalds buf->nframesout += 1; 27668e0d42fSEd L. Cashin buf->bv_off += bcnt; 2771da177e4SLinus Torvalds buf->bv_resid -= bcnt; 2781da177e4SLinus Torvalds buf->resid -= bcnt; 2791da177e4SLinus Torvalds buf->sector += bcnt >> 9; 2801da177e4SLinus Torvalds if (buf->resid == 0) { 2811da177e4SLinus Torvalds d->inprocess = NULL; 2821da177e4SLinus Torvalds } else if (buf->bv_resid == 0) { 28368e0d42fSEd L. Cashin buf->bv = ++bv; 28468e0d42fSEd L. Cashin buf->bv_resid = bv->bv_len; 28568e0d42fSEd L. Cashin WARN_ON(buf->bv_resid == 0); 28668e0d42fSEd L. Cashin buf->bv_off = bv->bv_offset; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 28968e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 2904f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 29168e0d42fSEd L. Cashin if (skb) { 292a4b38364Secashin@coraid.com if (d->sendq_hd) 293a4b38364Secashin@coraid.com d->sendq_tl->next = skb; 294a4b38364Secashin@coraid.com else 295a4b38364Secashin@coraid.com d->sendq_hd = skb; 296a4b38364Secashin@coraid.com d->sendq_tl = skb; 2971da177e4SLinus Torvalds } 29868e0d42fSEd L. Cashin return 1; 29968e0d42fSEd L. Cashin } 3001da177e4SLinus Torvalds 3013ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 3023ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 3033ae1c24eSEd L. Cashin */ 3043ae1c24eSEd L. Cashin static struct sk_buff * 3053ae1c24eSEd L. Cashin aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail) 3063ae1c24eSEd L. Cashin { 3073ae1c24eSEd L. Cashin struct aoe_hdr *h; 3083ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 3093ae1c24eSEd L. Cashin struct sk_buff *skb, *sl, *sl_tail; 3103ae1c24eSEd L. Cashin struct net_device *ifp; 3113ae1c24eSEd L. Cashin 3123ae1c24eSEd L. Cashin sl = sl_tail = NULL; 3133ae1c24eSEd L. Cashin 3143ae1c24eSEd L. Cashin read_lock(&dev_base_lock); 315881d966bSEric W. Biederman for_each_netdev(&init_net, ifp) { 3163ae1c24eSEd L. Cashin dev_hold(ifp); 3173ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 3187562f876SPavel Emelianov goto cont; 3193ae1c24eSEd L. Cashin 320e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 3213ae1c24eSEd L. Cashin if (skb == NULL) { 322a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 3237562f876SPavel Emelianov goto cont; 3243ae1c24eSEd L. Cashin } 32519900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 326e407a7f6SEd L. Cashin skb->dev = ifp; 3273ae1c24eSEd L. Cashin if (sl_tail == NULL) 3283ae1c24eSEd L. Cashin sl_tail = skb; 329abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 3303ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 3313ae1c24eSEd L. Cashin 3323ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 3333ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 3343ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 3353ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 3363ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 3373ae1c24eSEd L. Cashin h->minor = aoeminor; 3383ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 3393ae1c24eSEd L. Cashin 3403ae1c24eSEd L. Cashin skb->next = sl; 3413ae1c24eSEd L. Cashin sl = skb; 3427562f876SPavel Emelianov cont: 3437562f876SPavel Emelianov dev_put(ifp); 3443ae1c24eSEd L. Cashin } 3453ae1c24eSEd L. Cashin read_unlock(&dev_base_lock); 3463ae1c24eSEd L. Cashin 3473ae1c24eSEd L. Cashin if (tail != NULL) 3483ae1c24eSEd L. Cashin *tail = sl_tail; 3493ae1c24eSEd L. Cashin return sl; 3503ae1c24eSEd L. Cashin } 3513ae1c24eSEd L. Cashin 3521da177e4SLinus Torvalds static void 35368e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds struct sk_buff *skb; 3561da177e4SLinus Torvalds struct aoe_hdr *h; 35719bf2635SEd L. Cashin struct aoe_atahdr *ah; 3581da177e4SLinus Torvalds char buf[128]; 3591da177e4SLinus Torvalds u32 n; 3601da177e4SLinus Torvalds 36168e0d42fSEd L. Cashin ifrotate(t); 36268e0d42fSEd L. Cashin n = newtag(t); 363e407a7f6SEd L. Cashin skb = f->skb; 364abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 36519bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 36668e0d42fSEd L. Cashin 36768e0d42fSEd L. Cashin snprintf(buf, sizeof buf, 36868e0d42fSEd L. Cashin "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x " 36968e0d42fSEd L. Cashin "s=%012llx d=%012llx nout=%d\n", 37068e0d42fSEd L. Cashin "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n, 3711eb0da4cSEd L. Cashin mac_addr(h->src), 3721eb0da4cSEd L. Cashin mac_addr(h->dst), t->nout); 37368e0d42fSEd L. Cashin aoechr_error(buf); 37468e0d42fSEd L. Cashin 3751da177e4SLinus Torvalds f->tag = n; 37663e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 37768e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 37868e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 3791da177e4SLinus Torvalds 38068e0d42fSEd L. Cashin switch (ah->cmdstat) { 38168e0d42fSEd L. Cashin default: 38268e0d42fSEd L. Cashin break; 38368e0d42fSEd L. Cashin case WIN_READ: 38468e0d42fSEd L. Cashin case WIN_READ_EXT: 38568e0d42fSEd L. Cashin case WIN_WRITE: 38668e0d42fSEd L. Cashin case WIN_WRITE_EXT: 38768e0d42fSEd L. Cashin put_lba(ah, f->lba); 38868e0d42fSEd L. Cashin 38968e0d42fSEd L. Cashin n = f->bcnt; 39068e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 39168e0d42fSEd L. Cashin n = DEFAULTBCNT; 39268e0d42fSEd L. Cashin ah->scnt = n >> 9; 3934f51dc5eSEd L. Cashin if (ah->aflags & AOEAFL_WRITE) { 39419bf2635SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 39568e0d42fSEd L. Cashin offset_in_page(f->bufaddr), n); 39668e0d42fSEd L. Cashin skb->len = sizeof *h + sizeof *ah + n; 39768e0d42fSEd L. Cashin skb->data_len = n; 39819bf2635SEd L. Cashin } 39919bf2635SEd L. Cashin } 40068e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 4014f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 4024f51dc5eSEd L. Cashin if (skb == NULL) 4034f51dc5eSEd L. Cashin return; 404a4b38364Secashin@coraid.com if (d->sendq_hd) 405a4b38364Secashin@coraid.com d->sendq_tl->next = skb; 406a4b38364Secashin@coraid.com else 407a4b38364Secashin@coraid.com d->sendq_hd = skb; 408a4b38364Secashin@coraid.com d->sendq_tl = skb; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds static int 4121da177e4SLinus Torvalds tsince(int tag) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds int n; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds n = jiffies & 0xffff; 4171da177e4SLinus Torvalds n -= tag & 0xffff; 4181da177e4SLinus Torvalds if (n < 0) 4191da177e4SLinus Torvalds n += 1<<16; 4201da177e4SLinus Torvalds return n; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 42368e0d42fSEd L. Cashin static struct aoeif * 42468e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 42568e0d42fSEd L. Cashin { 42668e0d42fSEd L. Cashin struct aoeif *p, *e; 42768e0d42fSEd L. Cashin 42868e0d42fSEd L. Cashin p = t->ifs; 42968e0d42fSEd L. Cashin e = p + NAOEIFS; 43068e0d42fSEd L. Cashin for (; p < e; p++) 43168e0d42fSEd L. Cashin if (p->nd == nd) 43268e0d42fSEd L. Cashin return p; 43368e0d42fSEd L. Cashin return NULL; 43468e0d42fSEd L. Cashin } 43568e0d42fSEd L. Cashin 43668e0d42fSEd L. Cashin static struct aoeif * 43768e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd) 43868e0d42fSEd L. Cashin { 43968e0d42fSEd L. Cashin struct aoeif *p; 44068e0d42fSEd L. Cashin 44168e0d42fSEd L. Cashin p = getif(t, NULL); 44268e0d42fSEd L. Cashin if (!p) 44368e0d42fSEd L. Cashin return NULL; 44468e0d42fSEd L. Cashin p->nd = nd; 44568e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 44668e0d42fSEd L. Cashin p->lost = 0; 44768e0d42fSEd L. Cashin p->lostjumbo = 0; 44868e0d42fSEd L. Cashin return p; 44968e0d42fSEd L. Cashin } 45068e0d42fSEd L. Cashin 45168e0d42fSEd L. Cashin static void 45268e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 45368e0d42fSEd L. Cashin { 45468e0d42fSEd L. Cashin struct aoeif *e; 45568e0d42fSEd L. Cashin ulong n; 45668e0d42fSEd L. Cashin 45768e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 45868e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 45968e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 46068e0d42fSEd L. Cashin e->nd = NULL; 46168e0d42fSEd L. Cashin } 46268e0d42fSEd L. Cashin 46368e0d42fSEd L. Cashin static int 46468e0d42fSEd L. Cashin sthtith(struct aoedev *d) 46568e0d42fSEd L. Cashin { 46668e0d42fSEd L. Cashin struct frame *f, *e, *nf; 46768e0d42fSEd L. Cashin struct sk_buff *skb; 46868e0d42fSEd L. Cashin struct aoetgt *ht = *d->htgt; 46968e0d42fSEd L. Cashin 47068e0d42fSEd L. Cashin f = ht->frames; 47168e0d42fSEd L. Cashin e = f + ht->nframes; 47268e0d42fSEd L. Cashin for (; f < e; f++) { 47368e0d42fSEd L. Cashin if (f->tag == FREETAG) 47468e0d42fSEd L. Cashin continue; 47568e0d42fSEd L. Cashin nf = freeframe(d); 47668e0d42fSEd L. Cashin if (!nf) 47768e0d42fSEd L. Cashin return 0; 47868e0d42fSEd L. Cashin skb = nf->skb; 47968e0d42fSEd L. Cashin *nf = *f; 48068e0d42fSEd L. Cashin f->skb = skb; 48168e0d42fSEd L. Cashin f->tag = FREETAG; 48268e0d42fSEd L. Cashin nf->waited = 0; 48368e0d42fSEd L. Cashin ht->nout--; 48468e0d42fSEd L. Cashin (*d->tgt)->nout++; 48568e0d42fSEd L. Cashin resend(d, *d->tgt, nf); 48668e0d42fSEd L. Cashin } 48768e0d42fSEd L. Cashin /* he's clean, he's useless. take away his interfaces */ 48868e0d42fSEd L. Cashin memset(ht->ifs, 0, sizeof ht->ifs); 48968e0d42fSEd L. Cashin d->htgt = NULL; 49068e0d42fSEd L. Cashin return 1; 49168e0d42fSEd L. Cashin } 49268e0d42fSEd L. Cashin 49368e0d42fSEd L. Cashin static inline unsigned char 49468e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) { 49568e0d42fSEd L. Cashin struct aoe_hdr *h; 49668e0d42fSEd L. Cashin struct aoe_atahdr *ah; 49768e0d42fSEd L. Cashin 49868e0d42fSEd L. Cashin h = (struct aoe_hdr *) packet; 49968e0d42fSEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 50068e0d42fSEd L. Cashin return ah->scnt; 50168e0d42fSEd L. Cashin } 50268e0d42fSEd L. Cashin 5031da177e4SLinus Torvalds static void 5041da177e4SLinus Torvalds rexmit_timer(ulong vp) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds struct aoedev *d; 50768e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 50868e0d42fSEd L. Cashin struct aoeif *ifp; 5091da177e4SLinus Torvalds struct frame *f, *e; 5101da177e4SLinus Torvalds struct sk_buff *sl; 5111da177e4SLinus Torvalds register long timeout; 5121da177e4SLinus Torvalds ulong flags, n; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds d = (struct aoedev *) vp; 5151da177e4SLinus Torvalds sl = NULL; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* timeout is always ~150% of the moving average */ 5181da177e4SLinus Torvalds timeout = d->rttavg; 5191da177e4SLinus Torvalds timeout += timeout >> 1; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 5241c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 5251da177e4SLinus Torvalds return; 5261da177e4SLinus Torvalds } 52768e0d42fSEd L. Cashin tt = d->targets; 52868e0d42fSEd L. Cashin te = tt + NTARGETS; 52968e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) { 53068e0d42fSEd L. Cashin t = *tt; 53168e0d42fSEd L. Cashin f = t->frames; 53268e0d42fSEd L. Cashin e = f + t->nframes; 5331da177e4SLinus Torvalds for (; f < e; f++) { 53468e0d42fSEd L. Cashin if (f->tag == FREETAG 53568e0d42fSEd L. Cashin || tsince(f->tag) < timeout) 53668e0d42fSEd L. Cashin continue; 5371da177e4SLinus Torvalds n = f->waited += timeout; 5381da177e4SLinus Torvalds n /= HZ; 53968e0d42fSEd L. Cashin if (n > aoe_deadsecs) { 54068e0d42fSEd L. Cashin /* waited too long. device failure. */ 5411da177e4SLinus Torvalds aoedev_downdev(d); 5421c6f3fcaSEd L. Cashin break; 5431da177e4SLinus Torvalds } 54468e0d42fSEd L. Cashin 54568e0d42fSEd L. Cashin if (n > HELPWAIT /* see if another target can help */ 54668e0d42fSEd L. Cashin && (tt != d->targets || d->targets[1])) 54768e0d42fSEd L. Cashin d->htgt = tt; 54868e0d42fSEd L. Cashin 54968e0d42fSEd L. Cashin if (t->nout == t->maxout) { 55068e0d42fSEd L. Cashin if (t->maxout > 1) 55168e0d42fSEd L. Cashin t->maxout--; 55268e0d42fSEd L. Cashin t->lastwadj = jiffies; 55368e0d42fSEd L. Cashin } 55468e0d42fSEd L. Cashin 55568e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 55668e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 55768e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 55868e0d42fSEd L. Cashin ejectif(t, ifp); 55968e0d42fSEd L. Cashin ifp = NULL; 56068e0d42fSEd L. Cashin } 56168e0d42fSEd L. Cashin 56268e0d42fSEd L. Cashin if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512 56368e0d42fSEd L. Cashin && ifp && ++ifp->lostjumbo > (t->nframes << 1) 56468e0d42fSEd L. Cashin && ifp->maxbcnt != DEFAULTBCNT) { 56568e0d42fSEd L. Cashin printk(KERN_INFO 56668e0d42fSEd L. Cashin "aoe: e%ld.%d: " 56768e0d42fSEd L. Cashin "too many lost jumbo on " 56868e0d42fSEd L. Cashin "%s:%012llx - " 56968e0d42fSEd L. Cashin "falling back to %d frames.\n", 57068e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, 57168e0d42fSEd L. Cashin ifp->nd->name, mac_addr(t->addr), 57268e0d42fSEd L. Cashin DEFAULTBCNT); 57368e0d42fSEd L. Cashin ifp->maxbcnt = 0; 57468e0d42fSEd L. Cashin } 57568e0d42fSEd L. Cashin resend(d, t, f); 57668e0d42fSEd L. Cashin } 57768e0d42fSEd L. Cashin 57868e0d42fSEd L. Cashin /* window check */ 57968e0d42fSEd L. Cashin if (t->nout == t->maxout 58068e0d42fSEd L. Cashin && t->maxout < t->nframes 58168e0d42fSEd L. Cashin && (jiffies - t->lastwadj)/HZ > 10) { 58268e0d42fSEd L. Cashin t->maxout++; 58368e0d42fSEd L. Cashin t->lastwadj = jiffies; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds } 58668e0d42fSEd L. Cashin 58768e0d42fSEd L. Cashin if (d->sendq_hd) { 58868e0d42fSEd L. Cashin n = d->rttavg <<= 1; 58968e0d42fSEd L. Cashin if (n > MAXTIMER) 59068e0d42fSEd L. Cashin d->rttavg = MAXTIMER; 59168e0d42fSEd L. Cashin } 59268e0d42fSEd L. Cashin 59368e0d42fSEd L. Cashin if (d->flags & DEVFL_KICKME || d->htgt) { 5944f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 5954f51dc5eSEd L. Cashin aoecmd_work(d); 5964f51dc5eSEd L. Cashin } 5971da177e4SLinus Torvalds 598a4b38364Secashin@coraid.com sl = d->sendq_hd; 599a4b38364Secashin@coraid.com d->sendq_hd = d->sendq_tl = NULL; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 6021da177e4SLinus Torvalds add_timer(&d->timer); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds aoenet_xmit(sl); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 60968e0d42fSEd L. Cashin /* enters with d->lock held */ 61068e0d42fSEd L. Cashin void 61168e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 61268e0d42fSEd L. Cashin { 61368e0d42fSEd L. Cashin struct buf *buf; 61468e0d42fSEd L. Cashin loop: 61568e0d42fSEd L. Cashin if (d->htgt && !sthtith(d)) 61668e0d42fSEd L. Cashin return; 61768e0d42fSEd L. Cashin if (d->inprocess == NULL) { 61868e0d42fSEd L. Cashin if (list_empty(&d->bufq)) 61968e0d42fSEd L. Cashin return; 62068e0d42fSEd L. Cashin buf = container_of(d->bufq.next, struct buf, bufs); 62168e0d42fSEd L. Cashin list_del(d->bufq.next); 62268e0d42fSEd L. Cashin d->inprocess = buf; 62368e0d42fSEd L. Cashin } 62468e0d42fSEd L. Cashin if (aoecmd_ata_rw(d)) 62568e0d42fSEd L. Cashin goto loop; 62668e0d42fSEd L. Cashin } 62768e0d42fSEd L. Cashin 6283ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 6293ae1c24eSEd L. Cashin */ 6303ae1c24eSEd L. Cashin void 631c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 6323ae1c24eSEd L. Cashin { 633c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 6343ae1c24eSEd L. Cashin 6353ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 6363ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 6373ae1c24eSEd L. Cashin 6383ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 6393ae1c24eSEd L. Cashin struct block_device *bd; 6403ae1c24eSEd L. Cashin unsigned long flags; 6413ae1c24eSEd L. Cashin u64 ssize; 6423ae1c24eSEd L. Cashin 6433ae1c24eSEd L. Cashin ssize = d->gd->capacity; 6443ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 6453ae1c24eSEd L. Cashin 6463ae1c24eSEd L. Cashin if (bd) { 6473ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 6483ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 6493ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 6503ae1c24eSEd L. Cashin bdput(bd); 6513ae1c24eSEd L. Cashin } 6523ae1c24eSEd L. Cashin spin_lock_irqsave(&d->lock, flags); 6533ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 6543ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 6553ae1c24eSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 6563ae1c24eSEd L. Cashin } 6573ae1c24eSEd L. Cashin } 6583ae1c24eSEd L. Cashin 6591da177e4SLinus Torvalds static void 66068e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds u64 ssize; 6631da177e4SLinus Torvalds u16 n; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* word 83: command set supported */ 666475172fbSEd L. Cashin n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1])); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 669475172fbSEd L. Cashin n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1])); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 6721da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 675475172fbSEd L. Cashin ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1])); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 6781da177e4SLinus Torvalds d->geo.cylinders = ssize; 6791da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 6801da177e4SLinus Torvalds d->geo.heads = 255; 6811da177e4SLinus Torvalds d->geo.sectors = 63; 6821da177e4SLinus Torvalds } else { 6831da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* number lba28 sectors */ 686475172fbSEd L. Cashin ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1])); 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 689475172fbSEd L. Cashin d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1])); 690475172fbSEd L. Cashin d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1])); 691475172fbSEd L. Cashin d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1])); 6921da177e4SLinus Torvalds } 6933ae1c24eSEd L. Cashin 6943ae1c24eSEd L. Cashin if (d->ssize != ssize) 695a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu sectors\n", 6961eb0da4cSEd L. Cashin mac_addr(t->addr), 6973ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 6983ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 6991da177e4SLinus Torvalds d->ssize = ssize; 7001da177e4SLinus Torvalds d->geo.start = 0; 7011da177e4SLinus Torvalds if (d->gd != NULL) { 7021da177e4SLinus Torvalds d->gd->capacity = ssize; 7033ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 70468e0d42fSEd L. Cashin } else 7053ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 7061da177e4SLinus Torvalds schedule_work(&d->work); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static void 7101da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds register long n; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds n = rtt; 715dced3a05SEd L. Cashin if (n < 0) { 716dced3a05SEd L. Cashin n = -rtt; 7171da177e4SLinus Torvalds if (n < MINTIMER) 7181da177e4SLinus Torvalds n = MINTIMER; 7191da177e4SLinus Torvalds else if (n > MAXTIMER) 7201da177e4SLinus Torvalds n = MAXTIMER; 721dced3a05SEd L. Cashin d->mintimer += (n - d->mintimer) >> 1; 722dced3a05SEd L. Cashin } else if (n < d->mintimer) 723dced3a05SEd L. Cashin n = d->mintimer; 724dced3a05SEd L. Cashin else if (n > MAXTIMER) 725dced3a05SEd L. Cashin n = MAXTIMER; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */ 7281da177e4SLinus Torvalds n -= d->rttavg; 7291da177e4SLinus Torvalds d->rttavg += n >> 2; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 73268e0d42fSEd L. Cashin static struct aoetgt * 73368e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 73468e0d42fSEd L. Cashin { 73568e0d42fSEd L. Cashin struct aoetgt **t, **e; 73668e0d42fSEd L. Cashin 73768e0d42fSEd L. Cashin t = d->targets; 73868e0d42fSEd L. Cashin e = t + NTARGETS; 73968e0d42fSEd L. Cashin for (; t < e && *t; t++) 74068e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 74168e0d42fSEd L. Cashin return *t; 74268e0d42fSEd L. Cashin return NULL; 74368e0d42fSEd L. Cashin } 74468e0d42fSEd L. Cashin 74568e0d42fSEd L. Cashin static inline void 74668e0d42fSEd L. Cashin diskstats(struct gendisk *disk, struct bio *bio, ulong duration) 74768e0d42fSEd L. Cashin { 74868e0d42fSEd L. Cashin unsigned long n_sect = bio->bi_size >> 9; 74968e0d42fSEd L. Cashin const int rw = bio_data_dir(bio); 75068e0d42fSEd L. Cashin 75168e0d42fSEd L. Cashin disk_stat_inc(disk, ios[rw]); 75268e0d42fSEd L. Cashin disk_stat_add(disk, ticks[rw], duration); 75368e0d42fSEd L. Cashin disk_stat_add(disk, sectors[rw], n_sect); 75468e0d42fSEd L. Cashin disk_stat_add(disk, io_ticks, duration); 75568e0d42fSEd L. Cashin } 75668e0d42fSEd L. Cashin 7571da177e4SLinus Torvalds void 7581da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds struct aoedev *d; 761ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 7621da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 7631da177e4SLinus Torvalds struct frame *f; 7641da177e4SLinus Torvalds struct buf *buf; 7651da177e4SLinus Torvalds struct sk_buff *sl; 76668e0d42fSEd L. Cashin struct aoetgt *t; 76768e0d42fSEd L. Cashin struct aoeif *ifp; 7681da177e4SLinus Torvalds register long n; 7691da177e4SLinus Torvalds ulong flags; 7701da177e4SLinus Torvalds char ebuf[128]; 77132465c65Secashin@coraid.com u16 aoemajor; 7721da177e4SLinus Torvalds 773abdbf94dSEd L. Cashin hin = (struct aoe_hdr *) skb_mac_header(skb); 77443ecf529SDavid S. Miller aoemajor = be16_to_cpu(get_unaligned(&hin->major)); 77532465c65Secashin@coraid.com d = aoedev_by_aoeaddr(aoemajor, hin->minor); 7761da177e4SLinus Torvalds if (d == NULL) { 7771da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 7781da177e4SLinus Torvalds "for unknown device %d.%d\n", 77932465c65Secashin@coraid.com aoemajor, hin->minor); 7801da177e4SLinus Torvalds aoechr_error(ebuf); 7811da177e4SLinus Torvalds return; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 7851da177e4SLinus Torvalds 78643ecf529SDavid S. Miller n = be32_to_cpu(get_unaligned(&hin->tag)); 78768e0d42fSEd L. Cashin t = gettgt(d, hin->src); 78868e0d42fSEd L. Cashin if (t == NULL) { 78968e0d42fSEd L. Cashin printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n", 7901eb0da4cSEd L. Cashin d->aoemajor, d->aoeminor, mac_addr(hin->src)); 79168e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 79268e0d42fSEd L. Cashin return; 79368e0d42fSEd L. Cashin } 79468e0d42fSEd L. Cashin f = getframe(t, n); 7951da177e4SLinus Torvalds if (f == NULL) { 796dced3a05SEd L. Cashin calc_rttavg(d, -tsince(n)); 7971da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7981da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, 7991da177e4SLinus Torvalds "%15s e%d.%d tag=%08x@%08lx\n", 8001da177e4SLinus Torvalds "unexpected rsp", 80143ecf529SDavid S. Miller be16_to_cpu(get_unaligned(&hin->major)), 8021da177e4SLinus Torvalds hin->minor, 80343ecf529SDavid S. Miller be32_to_cpu(get_unaligned(&hin->tag)), 8041da177e4SLinus Torvalds jiffies); 8051da177e4SLinus Torvalds aoechr_error(ebuf); 8061da177e4SLinus Torvalds return; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds calc_rttavg(d, tsince(f->tag)); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds ahin = (struct aoe_atahdr *) (hin+1); 812abdbf94dSEd L. Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 813ddec63e8SEd L. Cashin ahout = (struct aoe_atahdr *) (hout+1); 8141da177e4SLinus Torvalds buf = f->buf; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 817a12c93f0SEd L. Cashin printk(KERN_ERR 818a12c93f0SEd L. Cashin "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n", 8191da177e4SLinus Torvalds ahout->cmdstat, ahin->cmdstat, 8201da177e4SLinus Torvalds d->aoemajor, d->aoeminor); 8211da177e4SLinus Torvalds if (buf) 8221da177e4SLinus Torvalds buf->flags |= BUFFL_FAIL; 8231da177e4SLinus Torvalds } else { 82468e0d42fSEd L. Cashin if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */ 82568e0d42fSEd L. Cashin d->htgt = NULL; 82619bf2635SEd L. Cashin n = ahout->scnt << 9; 8271da177e4SLinus Torvalds switch (ahout->cmdstat) { 8281da177e4SLinus Torvalds case WIN_READ: 8291da177e4SLinus Torvalds case WIN_READ_EXT: 8301da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < n) { 831a12c93f0SEd L. Cashin printk(KERN_ERR 83268e0d42fSEd L. Cashin "aoe: %s. skb->len=%d need=%ld\n", 83368e0d42fSEd L. Cashin "runt data size in read", skb->len, n); 8341da177e4SLinus Torvalds /* fail frame f? just returning will rexmit. */ 8351da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8361da177e4SLinus Torvalds return; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds memcpy(f->bufaddr, ahin+1, n); 8391da177e4SLinus Torvalds case WIN_WRITE: 8401da177e4SLinus Torvalds case WIN_WRITE_EXT: 84168e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 84268e0d42fSEd L. Cashin if (ifp) { 84368e0d42fSEd L. Cashin ifp->lost = 0; 84468e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 84568e0d42fSEd L. Cashin ifp->lostjumbo = 0; 84668e0d42fSEd L. Cashin } 84719bf2635SEd L. Cashin if (f->bcnt -= n) { 84868e0d42fSEd L. Cashin f->lba += n >> 9; 84919bf2635SEd L. Cashin f->bufaddr += n; 85068e0d42fSEd L. Cashin resend(d, t, f); 85168e0d42fSEd L. Cashin goto xmit; 8524f51dc5eSEd L. Cashin } 8531da177e4SLinus Torvalds break; 8541da177e4SLinus Torvalds case WIN_IDENTIFY: 8551da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < 512) { 856a12c93f0SEd L. Cashin printk(KERN_INFO 857a12c93f0SEd L. Cashin "aoe: runt data size in ataid. skb->len=%d\n", 8586bb6285fSEd L. Cashin skb->len); 8591da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8601da177e4SLinus Torvalds return; 8611da177e4SLinus Torvalds } 86268e0d42fSEd L. Cashin ataid_complete(d, t, (char *) (ahin+1)); 8631da177e4SLinus Torvalds break; 8641da177e4SLinus Torvalds default: 865a12c93f0SEd L. Cashin printk(KERN_INFO 866a12c93f0SEd L. Cashin "aoe: unrecognized ata command %2.2Xh for %d.%d\n", 8671da177e4SLinus Torvalds ahout->cmdstat, 86843ecf529SDavid S. Miller be16_to_cpu(get_unaligned(&hin->major)), 8691da177e4SLinus Torvalds hin->minor); 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87368e0d42fSEd L. Cashin if (buf && --buf->nframesout == 0 && buf->resid == 0) { 87468e0d42fSEd L. Cashin diskstats(d->gd, buf->bio, jiffies - buf->stime); 8751da177e4SLinus Torvalds n = (buf->flags & BUFFL_FAIL) ? -EIO : 0; 8766712ecf8SNeilBrown bio_endio(buf->bio, n); 8771da177e4SLinus Torvalds mempool_free(buf, d->bufpool); 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds f->buf = NULL; 8811da177e4SLinus Torvalds f->tag = FREETAG; 88268e0d42fSEd L. Cashin t->nout--; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds aoecmd_work(d); 88568e0d42fSEd L. Cashin xmit: 886a4b38364Secashin@coraid.com sl = d->sendq_hd; 887a4b38364Secashin@coraid.com d->sendq_hd = d->sendq_tl = NULL; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8901da177e4SLinus Torvalds aoenet_xmit(sl); 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds void 8941da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 8951da177e4SLinus Torvalds { 8963ae1c24eSEd L. Cashin struct sk_buff *sl; 8971da177e4SLinus Torvalds 8983ae1c24eSEd L. Cashin sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds aoenet_xmit(sl); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 90368e0d42fSEd L. Cashin struct sk_buff * 9041da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds struct aoe_hdr *h; 9071da177e4SLinus Torvalds struct aoe_atahdr *ah; 9081da177e4SLinus Torvalds struct frame *f; 9091da177e4SLinus Torvalds struct sk_buff *skb; 91068e0d42fSEd L. Cashin struct aoetgt *t; 9111da177e4SLinus Torvalds 9124f51dc5eSEd L. Cashin f = freeframe(d); 91368e0d42fSEd L. Cashin if (f == NULL) 9141da177e4SLinus Torvalds return NULL; 91568e0d42fSEd L. Cashin 91668e0d42fSEd L. Cashin t = *d->tgt; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* initialize the headers & frame */ 919e407a7f6SEd L. Cashin skb = f->skb; 920abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9211da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 92219900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 92319900cdeSEd L. Cashin memset(h, 0, skb->len); 92468e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 92568e0d42fSEd L. Cashin t->nout++; 9261da177e4SLinus Torvalds f->waited = 0; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* set up ata header */ 9291da177e4SLinus Torvalds ah->scnt = 1; 9301da177e4SLinus Torvalds ah->cmdstat = WIN_IDENTIFY; 9311da177e4SLinus Torvalds ah->lba3 = 0xa0; 9321da177e4SLinus Torvalds 93368e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 9341da177e4SLinus Torvalds 9353ae1c24eSEd L. Cashin d->rttavg = MAXTIMER; 9361da177e4SLinus Torvalds d->timer.function = rexmit_timer; 9371da177e4SLinus Torvalds 9384f51dc5eSEd L. Cashin return skb_clone(skb, GFP_ATOMIC); 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 94168e0d42fSEd L. Cashin static struct aoetgt * 94268e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes) 94368e0d42fSEd L. Cashin { 94468e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 94568e0d42fSEd L. Cashin struct frame *f, *e; 94668e0d42fSEd L. Cashin 94768e0d42fSEd L. Cashin tt = d->targets; 94868e0d42fSEd L. Cashin te = tt + NTARGETS; 94968e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) 95068e0d42fSEd L. Cashin ; 95168e0d42fSEd L. Cashin 95268e0d42fSEd L. Cashin if (tt == te) 95368e0d42fSEd L. Cashin return NULL; 95468e0d42fSEd L. Cashin 95568e0d42fSEd L. Cashin t = kcalloc(1, sizeof *t, GFP_ATOMIC); 956*9bb237b6SEd L. Cashin if (!t) 957*9bb237b6SEd L. Cashin return NULL; 95868e0d42fSEd L. Cashin f = kcalloc(nframes, sizeof *f, GFP_ATOMIC); 959*9bb237b6SEd L. Cashin if (!f) { 960*9bb237b6SEd L. Cashin kfree(t); 961*9bb237b6SEd L. Cashin return NULL; 962*9bb237b6SEd L. Cashin } 963*9bb237b6SEd L. Cashin 96468e0d42fSEd L. Cashin t->nframes = nframes; 96568e0d42fSEd L. Cashin t->frames = f; 96668e0d42fSEd L. Cashin e = f + nframes; 967*9bb237b6SEd L. Cashin for (; f < e; f++) 96868e0d42fSEd L. Cashin f->tag = FREETAG; 96968e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 97068e0d42fSEd L. Cashin t->ifp = t->ifs; 97168e0d42fSEd L. Cashin t->maxout = t->nframes; 97268e0d42fSEd L. Cashin return *tt = t; 97368e0d42fSEd L. Cashin } 97468e0d42fSEd L. Cashin 9751da177e4SLinus Torvalds void 9761da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 9771da177e4SLinus Torvalds { 9781da177e4SLinus Torvalds struct aoedev *d; 9791da177e4SLinus Torvalds struct aoe_hdr *h; 9801da177e4SLinus Torvalds struct aoe_cfghdr *ch; 98168e0d42fSEd L. Cashin struct aoetgt *t; 98268e0d42fSEd L. Cashin struct aoeif *ifp; 98363e9cc5dSecashin@coraid.com ulong flags, sysminor, aoemajor; 9841da177e4SLinus Torvalds struct sk_buff *sl; 985eaf0a3cbSEd L. Cashin enum { MAXFRAMES = 16 }; 98619bf2635SEd L. Cashin u16 n; 9871da177e4SLinus Torvalds 988abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9891da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 9931da177e4SLinus Torvalds * warrant a loud message for this special case. 9941da177e4SLinus Torvalds */ 99543ecf529SDavid S. Miller aoemajor = be16_to_cpu(get_unaligned(&h->major)); 9961da177e4SLinus Torvalds if (aoemajor == 0xfff) { 997a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 9986bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 9991da177e4SLinus Torvalds return; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds sysminor = SYSMINOR(aoemajor, h->minor); 1003fc458dcdSecashin@coraid.com if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) { 1004a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n", 1005fc458dcdSecashin@coraid.com aoemajor, (int) h->minor); 10061da177e4SLinus Torvalds return; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 100919bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 101019bf2635SEd L. Cashin if (n > MAXFRAMES) /* keep it reasonable */ 101119bf2635SEd L. Cashin n = MAXFRAMES; 10121da177e4SLinus Torvalds 101368e0d42fSEd L. Cashin d = aoedev_by_sysminor_m(sysminor); 10141da177e4SLinus Torvalds if (d == NULL) { 1015a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: device sysminor_m failure\n"); 10161da177e4SLinus Torvalds return; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 10201da177e4SLinus Torvalds 102168e0d42fSEd L. Cashin t = gettgt(d, h->src); 102268e0d42fSEd L. Cashin if (!t) { 102368e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 102468e0d42fSEd L. Cashin if (!t) { 102568e0d42fSEd L. Cashin printk(KERN_INFO 102668e0d42fSEd L. Cashin "aoe: device addtgt failure; " 102768e0d42fSEd L. Cashin "too many targets?\n"); 102868e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 102968e0d42fSEd L. Cashin return; 103068e0d42fSEd L. Cashin } 103168e0d42fSEd L. Cashin } 103268e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 103368e0d42fSEd L. Cashin if (!ifp) { 103468e0d42fSEd L. Cashin ifp = addif(t, skb->dev); 103568e0d42fSEd L. Cashin if (!ifp) { 103668e0d42fSEd L. Cashin printk(KERN_INFO 103768e0d42fSEd L. Cashin "aoe: device addif failure; " 103868e0d42fSEd L. Cashin "too many interfaces?\n"); 103968e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 104068e0d42fSEd L. Cashin return; 104168e0d42fSEd L. Cashin } 104268e0d42fSEd L. Cashin } 104368e0d42fSEd L. Cashin if (ifp->maxbcnt) { 104468e0d42fSEd L. Cashin n = ifp->nd->mtu; 104519bf2635SEd L. Cashin n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr); 104619bf2635SEd L. Cashin n /= 512; 104719bf2635SEd L. Cashin if (n > ch->scnt) 104819bf2635SEd L. Cashin n = ch->scnt; 10494f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 105068e0d42fSEd L. Cashin if (n != ifp->maxbcnt) { 1051a12c93f0SEd L. Cashin printk(KERN_INFO 105268e0d42fSEd L. Cashin "aoe: e%ld.%d: setting %d%s%s:%012llx\n", 105368e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, n, 105468e0d42fSEd L. Cashin " byte data frames on ", ifp->nd->name, 10551eb0da4cSEd L. Cashin mac_addr(t->addr)); 105668e0d42fSEd L. Cashin ifp->maxbcnt = n; 10574f51dc5eSEd L. Cashin } 105819bf2635SEd L. Cashin } 10593ae1c24eSEd L. Cashin 10603ae1c24eSEd L. Cashin /* don't change users' perspective */ 106168e0d42fSEd L. Cashin if (d->nopen) { 10621da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10631da177e4SLinus Torvalds return; 10641da177e4SLinus Torvalds } 106563e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 10661da177e4SLinus Torvalds 106768e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds aoenet_xmit(sl); 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 107468e0d42fSEd L. Cashin void 107568e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 107668e0d42fSEd L. Cashin { 107768e0d42fSEd L. Cashin struct aoetgt **t, **te; 107868e0d42fSEd L. Cashin struct aoeif *p, *e; 107968e0d42fSEd L. Cashin 108068e0d42fSEd L. Cashin d->mintimer = MINTIMER; 108168e0d42fSEd L. Cashin 108268e0d42fSEd L. Cashin t = d->targets; 108368e0d42fSEd L. Cashin te = t + NTARGETS; 108468e0d42fSEd L. Cashin for (; t < te && *t; t++) { 108568e0d42fSEd L. Cashin (*t)->maxout = (*t)->nframes; 108668e0d42fSEd L. Cashin p = (*t)->ifs; 108768e0d42fSEd L. Cashin e = p + NAOEIFS; 108868e0d42fSEd L. Cashin for (; p < e; p++) { 108968e0d42fSEd L. Cashin p->lostjumbo = 0; 109068e0d42fSEd L. Cashin p->lost = 0; 109168e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 109268e0d42fSEd L. Cashin } 109368e0d42fSEd L. Cashin } 109468e0d42fSEd L. Cashin } 1095