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 704b3ab52SBartlomiej Zolnierkiewicz #include <linux/ata.h> 8*5a0e3ad6STejun Heo #include <linux/slab.h> 91da177e4SLinus Torvalds #include <linux/hdreg.h> 101da177e4SLinus Torvalds #include <linux/blkdev.h> 111da177e4SLinus Torvalds #include <linux/skbuff.h> 121da177e4SLinus Torvalds #include <linux/netdevice.h> 133ae1c24eSEd L. Cashin #include <linux/genhd.h> 1468e0d42fSEd L. Cashin #include <linux/moduleparam.h> 15881d966bSEric W. Biederman #include <net/net_namespace.h> 16475172fbSEd L. Cashin #include <asm/unaligned.h> 171da177e4SLinus Torvalds #include "aoe.h" 181da177e4SLinus Torvalds 19b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3; 20b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644); 21b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev."); 221da177e4SLinus Torvalds 237df620d8SEd L. Cashin static int aoe_maxout = 16; 247df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644); 257df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout, 267df620d8SEd L. Cashin "Only aoe_maxout outstanding packets for every MAC on eX.Y."); 277df620d8SEd L. Cashin 2868e0d42fSEd L. Cashin static struct sk_buff * 29e407a7f6SEd L. Cashin new_skb(ulong len) 301da177e4SLinus Torvalds { 311da177e4SLinus Torvalds struct sk_buff *skb; 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 341da177e4SLinus Torvalds if (skb) { 35459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 36c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 371da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 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 1099bb237b6SEd L. Cashin static void 1109bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 1119bb237b6SEd L. Cashin { 112e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->skbpool, skb); 1139bb237b6SEd L. Cashin } 1149bb237b6SEd L. Cashin 1159bb237b6SEd L. Cashin static struct sk_buff * 1169bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 1179bb237b6SEd L. Cashin { 118e9bb8fb0SDavid S. Miller struct sk_buff *skb = skb_peek(&d->skbpool); 1199bb237b6SEd L. Cashin 1209bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 121e9bb8fb0SDavid S. Miller __skb_unlink(skb, &d->skbpool); 1229bb237b6SEd L. Cashin return skb; 1239bb237b6SEd L. Cashin } 124e9bb8fb0SDavid S. Miller if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 125e9bb8fb0SDavid S. Miller (skb = new_skb(ETH_ZLEN))) 1269bb237b6SEd L. Cashin return skb; 127e9bb8fb0SDavid S. Miller 1289bb237b6SEd L. Cashin return NULL; 1299bb237b6SEd L. Cashin } 1309bb237b6SEd L. Cashin 1319bb237b6SEd L. Cashin /* freeframe is where we do our load balancing so it's a little hairy. */ 13268e0d42fSEd L. Cashin static struct frame * 13368e0d42fSEd L. Cashin freeframe(struct aoedev *d) 13468e0d42fSEd L. Cashin { 1359bb237b6SEd L. Cashin struct frame *f, *e, *rf; 13668e0d42fSEd L. Cashin struct aoetgt **t; 1379bb237b6SEd L. Cashin struct sk_buff *skb; 13868e0d42fSEd L. Cashin 13968e0d42fSEd L. Cashin if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */ 14068e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 14168e0d42fSEd L. Cashin return NULL; 14268e0d42fSEd L. Cashin } 1439bb237b6SEd L. Cashin t = d->tgt; 1449bb237b6SEd L. Cashin t++; 1459bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 14668e0d42fSEd L. Cashin t = d->targets; 1479bb237b6SEd L. Cashin for (;;) { 1489bb237b6SEd L. Cashin if ((*t)->nout < (*t)->maxout 1499bb237b6SEd L. Cashin && t != d->htgt 1509bb237b6SEd L. Cashin && (*t)->ifp->nd) { 1519bb237b6SEd L. Cashin rf = NULL; 15268e0d42fSEd L. Cashin f = (*t)->frames; 1539bb237b6SEd L. Cashin e = f + (*t)->nframes; 15468e0d42fSEd L. Cashin for (; f < e; f++) { 15568e0d42fSEd L. Cashin if (f->tag != FREETAG) 15668e0d42fSEd L. Cashin continue; 1579bb237b6SEd L. Cashin skb = f->skb; 1589bb237b6SEd L. Cashin if (!skb 1599bb237b6SEd L. Cashin && !(f->skb = skb = new_skb(ETH_ZLEN))) 1609bb237b6SEd L. Cashin continue; 1619bb237b6SEd L. Cashin if (atomic_read(&skb_shinfo(skb)->dataref) 16268e0d42fSEd L. Cashin != 1) { 1639bb237b6SEd L. Cashin if (!rf) 1649bb237b6SEd L. Cashin rf = f; 16568e0d42fSEd L. Cashin continue; 16668e0d42fSEd L. Cashin } 1679bb237b6SEd L. Cashin gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0; 1689bb237b6SEd L. Cashin skb_trim(skb, 0); 16968e0d42fSEd L. Cashin d->tgt = t; 17068e0d42fSEd L. Cashin ifrotate(*t); 17168e0d42fSEd L. Cashin return f; 17268e0d42fSEd L. Cashin } 1739bb237b6SEd L. Cashin /* Work can be done, but the network layer is 1749bb237b6SEd L. Cashin holding our precious packets. Try to grab 1759bb237b6SEd L. Cashin one from the pool. */ 1769bb237b6SEd L. Cashin f = rf; 1779bb237b6SEd L. Cashin if (f == NULL) { /* more paranoia */ 1789bb237b6SEd L. Cashin printk(KERN_ERR 1799bb237b6SEd L. Cashin "aoe: freeframe: %s.\n", 1809bb237b6SEd L. Cashin "unexpected null rf"); 1819bb237b6SEd L. Cashin d->flags |= DEVFL_KICKME; 1829bb237b6SEd L. Cashin return NULL; 1839bb237b6SEd L. Cashin } 1849bb237b6SEd L. Cashin skb = skb_pool_get(d); 1859bb237b6SEd L. Cashin if (skb) { 1869bb237b6SEd L. Cashin skb_pool_put(d, f->skb); 1879bb237b6SEd L. Cashin f->skb = skb; 1889bb237b6SEd L. Cashin goto gotone; 1899bb237b6SEd L. Cashin } 1909bb237b6SEd L. Cashin (*t)->dataref++; 1919bb237b6SEd L. Cashin if ((*t)->nout == 0) 19268e0d42fSEd L. Cashin d->flags |= DEVFL_KICKME; 19368e0d42fSEd L. Cashin } 1949bb237b6SEd L. Cashin if (t == d->tgt) /* we've looped and found nada */ 1959bb237b6SEd L. Cashin break; 19668e0d42fSEd L. Cashin t++; 1979bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 1989bb237b6SEd L. Cashin t = d->targets; 1999bb237b6SEd L. Cashin } 20068e0d42fSEd L. Cashin return NULL; 20168e0d42fSEd L. Cashin } 20268e0d42fSEd L. Cashin 20368e0d42fSEd L. Cashin static int 20468e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 20568e0d42fSEd L. Cashin { 20668e0d42fSEd L. Cashin struct frame *f; 2071da177e4SLinus Torvalds struct aoe_hdr *h; 2081da177e4SLinus Torvalds struct aoe_atahdr *ah; 2091da177e4SLinus Torvalds struct buf *buf; 21068e0d42fSEd L. Cashin struct bio_vec *bv; 21168e0d42fSEd L. Cashin struct aoetgt *t; 2121da177e4SLinus Torvalds struct sk_buff *skb; 2131da177e4SLinus Torvalds ulong bcnt; 2141da177e4SLinus Torvalds char writebit, extbit; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds writebit = 0x10; 2171da177e4SLinus Torvalds extbit = 0x4; 2181da177e4SLinus Torvalds 21968e0d42fSEd L. Cashin f = freeframe(d); 22068e0d42fSEd L. Cashin if (f == NULL) 22168e0d42fSEd L. Cashin return 0; 22268e0d42fSEd L. Cashin t = *d->tgt; 2231da177e4SLinus Torvalds buf = d->inprocess; 22468e0d42fSEd L. Cashin bv = buf->bv; 22568e0d42fSEd L. Cashin bcnt = t->ifp->maxbcnt; 22668e0d42fSEd L. Cashin if (bcnt == 0) 22768e0d42fSEd L. Cashin bcnt = DEFAULTBCNT; 22868e0d42fSEd L. Cashin if (bcnt > buf->bv_resid) 2291da177e4SLinus Torvalds bcnt = buf->bv_resid; 2301da177e4SLinus Torvalds /* initialize the headers & frame */ 231e407a7f6SEd L. Cashin skb = f->skb; 232abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 2331da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 23419900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 23519900cdeSEd L. Cashin memset(h, 0, skb->len); 23668e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 23768e0d42fSEd L. Cashin t->nout++; 2381da177e4SLinus Torvalds f->waited = 0; 2391da177e4SLinus Torvalds f->buf = buf; 24068e0d42fSEd L. Cashin f->bufaddr = page_address(bv->bv_page) + buf->bv_off; 24119bf2635SEd L. Cashin f->bcnt = bcnt; 24268e0d42fSEd L. Cashin f->lba = buf->sector; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds /* set up ata header */ 2451da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 24668e0d42fSEd L. Cashin put_lba(ah, buf->sector); 2471da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 2481da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 2491da177e4SLinus Torvalds } else { 2501da177e4SLinus Torvalds extbit = 0; 2511da177e4SLinus Torvalds ah->lba3 &= 0x0f; 2521da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 25568e0d42fSEd L. Cashin skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt); 2561da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 2574f51dc5eSEd L. Cashin skb->len += bcnt; 2584f51dc5eSEd L. Cashin skb->data_len = bcnt; 25968e0d42fSEd L. Cashin t->wpkts++; 2601da177e4SLinus Torvalds } else { 26168e0d42fSEd L. Cashin t->rpkts++; 2621da177e4SLinus Torvalds writebit = 0; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 26504b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* mark all tracking fields and load out */ 2681da177e4SLinus Torvalds buf->nframesout += 1; 26968e0d42fSEd L. Cashin buf->bv_off += bcnt; 2701da177e4SLinus Torvalds buf->bv_resid -= bcnt; 2711da177e4SLinus Torvalds buf->resid -= bcnt; 2721da177e4SLinus Torvalds buf->sector += bcnt >> 9; 2731da177e4SLinus Torvalds if (buf->resid == 0) { 2741da177e4SLinus Torvalds d->inprocess = NULL; 2751da177e4SLinus Torvalds } else if (buf->bv_resid == 0) { 27668e0d42fSEd L. Cashin buf->bv = ++bv; 27768e0d42fSEd L. Cashin buf->bv_resid = bv->bv_len; 27868e0d42fSEd L. Cashin WARN_ON(buf->bv_resid == 0); 27968e0d42fSEd L. Cashin buf->bv_off = bv->bv_offset; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 28268e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 2834f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 284e9bb8fb0SDavid S. Miller if (skb) 285e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 28668e0d42fSEd L. Cashin return 1; 28768e0d42fSEd L. Cashin } 2881da177e4SLinus Torvalds 2893ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 2903ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 2913ae1c24eSEd L. Cashin */ 292e9bb8fb0SDavid S. Miller static void 293e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 2943ae1c24eSEd L. Cashin { 2953ae1c24eSEd L. Cashin struct aoe_hdr *h; 2963ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 297e9bb8fb0SDavid S. Miller struct sk_buff *skb; 2983ae1c24eSEd L. Cashin struct net_device *ifp; 2993ae1c24eSEd L. Cashin 3003ae1c24eSEd L. Cashin read_lock(&dev_base_lock); 301881d966bSEric W. Biederman for_each_netdev(&init_net, ifp) { 3023ae1c24eSEd L. Cashin dev_hold(ifp); 3033ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 3047562f876SPavel Emelianov goto cont; 3053ae1c24eSEd L. Cashin 306e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 3073ae1c24eSEd L. Cashin if (skb == NULL) { 308a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 3097562f876SPavel Emelianov goto cont; 3103ae1c24eSEd L. Cashin } 31119900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 312e407a7f6SEd L. Cashin skb->dev = ifp; 313e9bb8fb0SDavid S. Miller __skb_queue_tail(queue, skb); 314abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 3153ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 3163ae1c24eSEd L. Cashin 3173ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 3183ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 3193ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 3203ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 3213ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 3223ae1c24eSEd L. Cashin h->minor = aoeminor; 3233ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 3243ae1c24eSEd L. Cashin 3257562f876SPavel Emelianov cont: 3267562f876SPavel Emelianov dev_put(ifp); 3273ae1c24eSEd L. Cashin } 3283ae1c24eSEd L. Cashin read_unlock(&dev_base_lock); 3293ae1c24eSEd L. Cashin } 3303ae1c24eSEd L. Cashin 3311da177e4SLinus Torvalds static void 33268e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds struct sk_buff *skb; 3351da177e4SLinus Torvalds struct aoe_hdr *h; 33619bf2635SEd L. Cashin struct aoe_atahdr *ah; 3371da177e4SLinus Torvalds char buf[128]; 3381da177e4SLinus Torvalds u32 n; 3391da177e4SLinus Torvalds 34068e0d42fSEd L. Cashin ifrotate(t); 34168e0d42fSEd L. Cashin n = newtag(t); 342e407a7f6SEd L. Cashin skb = f->skb; 343abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 34419bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 34568e0d42fSEd L. Cashin 34668e0d42fSEd L. Cashin snprintf(buf, sizeof buf, 347411c41eeSHarvey Harrison "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", 34868e0d42fSEd L. Cashin "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n, 349411c41eeSHarvey Harrison h->src, h->dst, t->nout); 35068e0d42fSEd L. Cashin aoechr_error(buf); 35168e0d42fSEd L. Cashin 3521da177e4SLinus Torvalds f->tag = n; 35363e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 35468e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 35568e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 3561da177e4SLinus Torvalds 35768e0d42fSEd L. Cashin switch (ah->cmdstat) { 35868e0d42fSEd L. Cashin default: 35968e0d42fSEd L. Cashin break; 36004b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ: 36104b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ_EXT: 36204b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE: 36304b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE_EXT: 36468e0d42fSEd L. Cashin put_lba(ah, f->lba); 36568e0d42fSEd L. Cashin 36668e0d42fSEd L. Cashin n = f->bcnt; 36768e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 36868e0d42fSEd L. Cashin n = DEFAULTBCNT; 36968e0d42fSEd L. Cashin ah->scnt = n >> 9; 3704f51dc5eSEd L. Cashin if (ah->aflags & AOEAFL_WRITE) { 37119bf2635SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 37268e0d42fSEd L. Cashin offset_in_page(f->bufaddr), n); 37368e0d42fSEd L. Cashin skb->len = sizeof *h + sizeof *ah + n; 37468e0d42fSEd L. Cashin skb->data_len = n; 37519bf2635SEd L. Cashin } 37619bf2635SEd L. Cashin } 37768e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 3784f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 3794f51dc5eSEd L. Cashin if (skb == NULL) 3804f51dc5eSEd L. Cashin return; 381e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds static int 3851da177e4SLinus Torvalds tsince(int tag) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds int n; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds n = jiffies & 0xffff; 3901da177e4SLinus Torvalds n -= tag & 0xffff; 3911da177e4SLinus Torvalds if (n < 0) 3921da177e4SLinus Torvalds n += 1<<16; 3931da177e4SLinus Torvalds return n; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 39668e0d42fSEd L. Cashin static struct aoeif * 39768e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 39868e0d42fSEd L. Cashin { 39968e0d42fSEd L. Cashin struct aoeif *p, *e; 40068e0d42fSEd L. Cashin 40168e0d42fSEd L. Cashin p = t->ifs; 40268e0d42fSEd L. Cashin e = p + NAOEIFS; 40368e0d42fSEd L. Cashin for (; p < e; p++) 40468e0d42fSEd L. Cashin if (p->nd == nd) 40568e0d42fSEd L. Cashin return p; 40668e0d42fSEd L. Cashin return NULL; 40768e0d42fSEd L. Cashin } 40868e0d42fSEd L. Cashin 40968e0d42fSEd L. Cashin static struct aoeif * 41068e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd) 41168e0d42fSEd L. Cashin { 41268e0d42fSEd L. Cashin struct aoeif *p; 41368e0d42fSEd L. Cashin 41468e0d42fSEd L. Cashin p = getif(t, NULL); 41568e0d42fSEd L. Cashin if (!p) 41668e0d42fSEd L. Cashin return NULL; 41768e0d42fSEd L. Cashin p->nd = nd; 41868e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 41968e0d42fSEd L. Cashin p->lost = 0; 42068e0d42fSEd L. Cashin p->lostjumbo = 0; 42168e0d42fSEd L. Cashin return p; 42268e0d42fSEd L. Cashin } 42368e0d42fSEd L. Cashin 42468e0d42fSEd L. Cashin static void 42568e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 42668e0d42fSEd L. Cashin { 42768e0d42fSEd L. Cashin struct aoeif *e; 42868e0d42fSEd L. Cashin ulong n; 42968e0d42fSEd L. Cashin 43068e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 43168e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 43268e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 43368e0d42fSEd L. Cashin e->nd = NULL; 43468e0d42fSEd L. Cashin } 43568e0d42fSEd L. Cashin 43668e0d42fSEd L. Cashin static int 43768e0d42fSEd L. Cashin sthtith(struct aoedev *d) 43868e0d42fSEd L. Cashin { 43968e0d42fSEd L. Cashin struct frame *f, *e, *nf; 44068e0d42fSEd L. Cashin struct sk_buff *skb; 44168e0d42fSEd L. Cashin struct aoetgt *ht = *d->htgt; 44268e0d42fSEd L. Cashin 44368e0d42fSEd L. Cashin f = ht->frames; 44468e0d42fSEd L. Cashin e = f + ht->nframes; 44568e0d42fSEd L. Cashin for (; f < e; f++) { 44668e0d42fSEd L. Cashin if (f->tag == FREETAG) 44768e0d42fSEd L. Cashin continue; 44868e0d42fSEd L. Cashin nf = freeframe(d); 44968e0d42fSEd L. Cashin if (!nf) 45068e0d42fSEd L. Cashin return 0; 45168e0d42fSEd L. Cashin skb = nf->skb; 45268e0d42fSEd L. Cashin *nf = *f; 45368e0d42fSEd L. Cashin f->skb = skb; 45468e0d42fSEd L. Cashin f->tag = FREETAG; 45568e0d42fSEd L. Cashin nf->waited = 0; 45668e0d42fSEd L. Cashin ht->nout--; 45768e0d42fSEd L. Cashin (*d->tgt)->nout++; 45868e0d42fSEd L. Cashin resend(d, *d->tgt, nf); 45968e0d42fSEd L. Cashin } 46068e0d42fSEd L. Cashin /* he's clean, he's useless. take away his interfaces */ 46168e0d42fSEd L. Cashin memset(ht->ifs, 0, sizeof ht->ifs); 46268e0d42fSEd L. Cashin d->htgt = NULL; 46368e0d42fSEd L. Cashin return 1; 46468e0d42fSEd L. Cashin } 46568e0d42fSEd L. Cashin 46668e0d42fSEd L. Cashin static inline unsigned char 46768e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) { 46868e0d42fSEd L. Cashin struct aoe_hdr *h; 46968e0d42fSEd L. Cashin struct aoe_atahdr *ah; 47068e0d42fSEd L. Cashin 47168e0d42fSEd L. Cashin h = (struct aoe_hdr *) packet; 47268e0d42fSEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 47368e0d42fSEd L. Cashin return ah->scnt; 47468e0d42fSEd L. Cashin } 47568e0d42fSEd L. Cashin 4761da177e4SLinus Torvalds static void 4771da177e4SLinus Torvalds rexmit_timer(ulong vp) 4781da177e4SLinus Torvalds { 479e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 4801da177e4SLinus Torvalds struct aoedev *d; 48168e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 48268e0d42fSEd L. Cashin struct aoeif *ifp; 4831da177e4SLinus Torvalds struct frame *f, *e; 4841da177e4SLinus Torvalds register long timeout; 4851da177e4SLinus Torvalds ulong flags, n; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds d = (struct aoedev *) vp; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* timeout is always ~150% of the moving average */ 4901da177e4SLinus Torvalds timeout = d->rttavg; 4911da177e4SLinus Torvalds timeout += timeout >> 1; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 4961c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 4971da177e4SLinus Torvalds return; 4981da177e4SLinus Torvalds } 49968e0d42fSEd L. Cashin tt = d->targets; 50068e0d42fSEd L. Cashin te = tt + NTARGETS; 50168e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) { 50268e0d42fSEd L. Cashin t = *tt; 50368e0d42fSEd L. Cashin f = t->frames; 50468e0d42fSEd L. Cashin e = f + t->nframes; 5051da177e4SLinus Torvalds for (; f < e; f++) { 50668e0d42fSEd L. Cashin if (f->tag == FREETAG 50768e0d42fSEd L. Cashin || tsince(f->tag) < timeout) 50868e0d42fSEd L. Cashin continue; 5091da177e4SLinus Torvalds n = f->waited += timeout; 5101da177e4SLinus Torvalds n /= HZ; 51168e0d42fSEd L. Cashin if (n > aoe_deadsecs) { 51268e0d42fSEd L. Cashin /* waited too long. device failure. */ 5131da177e4SLinus Torvalds aoedev_downdev(d); 5141c6f3fcaSEd L. Cashin break; 5151da177e4SLinus Torvalds } 51668e0d42fSEd L. Cashin 51768e0d42fSEd L. Cashin if (n > HELPWAIT /* see if another target can help */ 51868e0d42fSEd L. Cashin && (tt != d->targets || d->targets[1])) 51968e0d42fSEd L. Cashin d->htgt = tt; 52068e0d42fSEd L. Cashin 52168e0d42fSEd L. Cashin if (t->nout == t->maxout) { 52268e0d42fSEd L. Cashin if (t->maxout > 1) 52368e0d42fSEd L. Cashin t->maxout--; 52468e0d42fSEd L. Cashin t->lastwadj = jiffies; 52568e0d42fSEd L. Cashin } 52668e0d42fSEd L. Cashin 52768e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 52868e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 52968e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 53068e0d42fSEd L. Cashin ejectif(t, ifp); 53168e0d42fSEd L. Cashin ifp = NULL; 53268e0d42fSEd L. Cashin } 53368e0d42fSEd L. Cashin 53468e0d42fSEd L. Cashin if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512 53568e0d42fSEd L. Cashin && ifp && ++ifp->lostjumbo > (t->nframes << 1) 53668e0d42fSEd L. Cashin && ifp->maxbcnt != DEFAULTBCNT) { 53768e0d42fSEd L. Cashin printk(KERN_INFO 53868e0d42fSEd L. Cashin "aoe: e%ld.%d: " 53968e0d42fSEd L. Cashin "too many lost jumbo on " 540411c41eeSHarvey Harrison "%s:%pm - " 54168e0d42fSEd L. Cashin "falling back to %d frames.\n", 54268e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, 543411c41eeSHarvey Harrison ifp->nd->name, t->addr, 54468e0d42fSEd L. Cashin DEFAULTBCNT); 54568e0d42fSEd L. Cashin ifp->maxbcnt = 0; 54668e0d42fSEd L. Cashin } 54768e0d42fSEd L. Cashin resend(d, t, f); 54868e0d42fSEd L. Cashin } 54968e0d42fSEd L. Cashin 55068e0d42fSEd L. Cashin /* window check */ 55168e0d42fSEd L. Cashin if (t->nout == t->maxout 55268e0d42fSEd L. Cashin && t->maxout < t->nframes 55368e0d42fSEd L. Cashin && (jiffies - t->lastwadj)/HZ > 10) { 55468e0d42fSEd L. Cashin t->maxout++; 55568e0d42fSEd L. Cashin t->lastwadj = jiffies; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds } 55868e0d42fSEd L. Cashin 559e9bb8fb0SDavid S. Miller if (!skb_queue_empty(&d->sendq)) { 56068e0d42fSEd L. Cashin n = d->rttavg <<= 1; 56168e0d42fSEd L. Cashin if (n > MAXTIMER) 56268e0d42fSEd L. Cashin d->rttavg = MAXTIMER; 56368e0d42fSEd L. Cashin } 56468e0d42fSEd L. Cashin 56568e0d42fSEd L. Cashin if (d->flags & DEVFL_KICKME || d->htgt) { 5664f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 5674f51dc5eSEd L. Cashin aoecmd_work(d); 5684f51dc5eSEd L. Cashin } 5691da177e4SLinus Torvalds 570e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 571e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 5741da177e4SLinus Torvalds add_timer(&d->timer); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5771da177e4SLinus Torvalds 578e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 58168e0d42fSEd L. Cashin /* enters with d->lock held */ 58268e0d42fSEd L. Cashin void 58368e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 58468e0d42fSEd L. Cashin { 58568e0d42fSEd L. Cashin struct buf *buf; 58668e0d42fSEd L. Cashin loop: 58768e0d42fSEd L. Cashin if (d->htgt && !sthtith(d)) 58868e0d42fSEd L. Cashin return; 58968e0d42fSEd L. Cashin if (d->inprocess == NULL) { 59068e0d42fSEd L. Cashin if (list_empty(&d->bufq)) 59168e0d42fSEd L. Cashin return; 59268e0d42fSEd L. Cashin buf = container_of(d->bufq.next, struct buf, bufs); 59368e0d42fSEd L. Cashin list_del(d->bufq.next); 59468e0d42fSEd L. Cashin d->inprocess = buf; 59568e0d42fSEd L. Cashin } 59668e0d42fSEd L. Cashin if (aoecmd_ata_rw(d)) 59768e0d42fSEd L. Cashin goto loop; 59868e0d42fSEd L. Cashin } 59968e0d42fSEd L. Cashin 6003ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 6013ae1c24eSEd L. Cashin */ 6023ae1c24eSEd L. Cashin void 603c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 6043ae1c24eSEd L. Cashin { 605c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 6063ae1c24eSEd L. Cashin 6073ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 6083ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 6093ae1c24eSEd L. Cashin 6103ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 6113ae1c24eSEd L. Cashin struct block_device *bd; 6123ae1c24eSEd L. Cashin unsigned long flags; 6133ae1c24eSEd L. Cashin u64 ssize; 6143ae1c24eSEd L. Cashin 61580795aefSTejun Heo ssize = get_capacity(d->gd); 6163ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 6173ae1c24eSEd L. Cashin 6183ae1c24eSEd L. Cashin if (bd) { 6193ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 6203ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 6213ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 6223ae1c24eSEd L. Cashin bdput(bd); 6233ae1c24eSEd L. Cashin } 6243ae1c24eSEd L. Cashin spin_lock_irqsave(&d->lock, flags); 6253ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 6263ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 6273ae1c24eSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 6283ae1c24eSEd L. Cashin } 6293ae1c24eSEd L. Cashin } 6303ae1c24eSEd L. Cashin 6311da177e4SLinus Torvalds static void 63268e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds u64 ssize; 6351da177e4SLinus Torvalds u16 n; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds /* word 83: command set supported */ 638f885f8d1SHarvey Harrison n = get_unaligned_le16(&id[83 << 1]); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 641f885f8d1SHarvey Harrison n |= get_unaligned_le16(&id[86 << 1]); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 6441da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 647f885f8d1SHarvey Harrison ssize = get_unaligned_le64(&id[100 << 1]); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 6501da177e4SLinus Torvalds d->geo.cylinders = ssize; 6511da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 6521da177e4SLinus Torvalds d->geo.heads = 255; 6531da177e4SLinus Torvalds d->geo.sectors = 63; 6541da177e4SLinus Torvalds } else { 6551da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* number lba28 sectors */ 658f885f8d1SHarvey Harrison ssize = get_unaligned_le32(&id[60 << 1]); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 661f885f8d1SHarvey Harrison d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 662f885f8d1SHarvey Harrison d->geo.heads = get_unaligned_le16(&id[55 << 1]); 663f885f8d1SHarvey Harrison d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 6641da177e4SLinus Torvalds } 6653ae1c24eSEd L. Cashin 6663ae1c24eSEd L. Cashin if (d->ssize != ssize) 6671d75981aSEd L. Cashin printk(KERN_INFO 668411c41eeSHarvey Harrison "aoe: %pm e%ld.%d v%04x has %llu sectors\n", 669411c41eeSHarvey Harrison t->addr, 6703ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 6713ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 6721da177e4SLinus Torvalds d->ssize = ssize; 6731da177e4SLinus Torvalds d->geo.start = 0; 6746b9699bbSEd L. Cashin if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 6756b9699bbSEd L. Cashin return; 6761da177e4SLinus Torvalds if (d->gd != NULL) { 67780795aefSTejun Heo set_capacity(d->gd, ssize); 6783ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 67968e0d42fSEd L. Cashin } else 6803ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 6811da177e4SLinus Torvalds schedule_work(&d->work); 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds static void 6851da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt) 6861da177e4SLinus Torvalds { 6871da177e4SLinus Torvalds register long n; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds n = rtt; 690dced3a05SEd L. Cashin if (n < 0) { 691dced3a05SEd L. Cashin n = -rtt; 6921da177e4SLinus Torvalds if (n < MINTIMER) 6931da177e4SLinus Torvalds n = MINTIMER; 6941da177e4SLinus Torvalds else if (n > MAXTIMER) 6951da177e4SLinus Torvalds n = MAXTIMER; 696dced3a05SEd L. Cashin d->mintimer += (n - d->mintimer) >> 1; 697dced3a05SEd L. Cashin } else if (n < d->mintimer) 698dced3a05SEd L. Cashin n = d->mintimer; 699dced3a05SEd L. Cashin else if (n > MAXTIMER) 700dced3a05SEd L. Cashin n = MAXTIMER; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */ 7031da177e4SLinus Torvalds n -= d->rttavg; 7041da177e4SLinus Torvalds d->rttavg += n >> 2; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 70768e0d42fSEd L. Cashin static struct aoetgt * 70868e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 70968e0d42fSEd L. Cashin { 71068e0d42fSEd L. Cashin struct aoetgt **t, **e; 71168e0d42fSEd L. Cashin 71268e0d42fSEd L. Cashin t = d->targets; 71368e0d42fSEd L. Cashin e = t + NTARGETS; 71468e0d42fSEd L. Cashin for (; t < e && *t; t++) 71568e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 71668e0d42fSEd L. Cashin return *t; 71768e0d42fSEd L. Cashin return NULL; 71868e0d42fSEd L. Cashin } 71968e0d42fSEd L. Cashin 72068e0d42fSEd L. Cashin static inline void 72103054de1SLinus Torvalds diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector) 72268e0d42fSEd L. Cashin { 72368e0d42fSEd L. Cashin unsigned long n_sect = bio->bi_size >> 9; 72468e0d42fSEd L. Cashin const int rw = bio_data_dir(bio); 72528f13702SJens Axboe struct hd_struct *part; 726c9959059STejun Heo int cpu; 72768e0d42fSEd L. Cashin 728074a7acaSTejun Heo cpu = part_stat_lock(); 729e71bf0d0STejun Heo part = disk_map_sector_rcu(disk, sector); 730e71bf0d0STejun Heo 731074a7acaSTejun Heo part_stat_inc(cpu, part, ios[rw]); 732074a7acaSTejun Heo part_stat_add(cpu, part, ticks[rw], duration); 733074a7acaSTejun Heo part_stat_add(cpu, part, sectors[rw], n_sect); 734074a7acaSTejun Heo part_stat_add(cpu, part, io_ticks, duration); 735c9959059STejun Heo 736074a7acaSTejun Heo part_stat_unlock(); 73768e0d42fSEd L. Cashin } 73868e0d42fSEd L. Cashin 7391da177e4SLinus Torvalds void 7401da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb) 7411da177e4SLinus Torvalds { 742e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 7431da177e4SLinus Torvalds struct aoedev *d; 744ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 7451da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 7461da177e4SLinus Torvalds struct frame *f; 7471da177e4SLinus Torvalds struct buf *buf; 74868e0d42fSEd L. Cashin struct aoetgt *t; 74968e0d42fSEd L. Cashin struct aoeif *ifp; 7501da177e4SLinus Torvalds register long n; 7511da177e4SLinus Torvalds ulong flags; 7521da177e4SLinus Torvalds char ebuf[128]; 75332465c65Secashin@coraid.com u16 aoemajor; 7541da177e4SLinus Torvalds 755abdbf94dSEd L. Cashin hin = (struct aoe_hdr *) skb_mac_header(skb); 756f885f8d1SHarvey Harrison aoemajor = get_unaligned_be16(&hin->major); 75732465c65Secashin@coraid.com d = aoedev_by_aoeaddr(aoemajor, hin->minor); 7581da177e4SLinus Torvalds if (d == NULL) { 7591da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 7601da177e4SLinus Torvalds "for unknown device %d.%d\n", 76132465c65Secashin@coraid.com aoemajor, hin->minor); 7621da177e4SLinus Torvalds aoechr_error(ebuf); 7631da177e4SLinus Torvalds return; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 7671da177e4SLinus Torvalds 768f885f8d1SHarvey Harrison n = get_unaligned_be32(&hin->tag); 76968e0d42fSEd L. Cashin t = gettgt(d, hin->src); 77068e0d42fSEd L. Cashin if (t == NULL) { 771411c41eeSHarvey Harrison printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n", 772411c41eeSHarvey Harrison d->aoemajor, d->aoeminor, hin->src); 77368e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 77468e0d42fSEd L. Cashin return; 77568e0d42fSEd L. Cashin } 77668e0d42fSEd L. Cashin f = getframe(t, n); 7771da177e4SLinus Torvalds if (f == NULL) { 778dced3a05SEd L. Cashin calc_rttavg(d, -tsince(n)); 7791da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7801da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, 7811da177e4SLinus Torvalds "%15s e%d.%d tag=%08x@%08lx\n", 7821da177e4SLinus Torvalds "unexpected rsp", 783f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 7841da177e4SLinus Torvalds hin->minor, 785f885f8d1SHarvey Harrison get_unaligned_be32(&hin->tag), 7861da177e4SLinus Torvalds jiffies); 7871da177e4SLinus Torvalds aoechr_error(ebuf); 7881da177e4SLinus Torvalds return; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds calc_rttavg(d, tsince(f->tag)); 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds ahin = (struct aoe_atahdr *) (hin+1); 794abdbf94dSEd L. Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 795ddec63e8SEd L. Cashin ahout = (struct aoe_atahdr *) (hout+1); 7961da177e4SLinus Torvalds buf = f->buf; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 799a12c93f0SEd L. Cashin printk(KERN_ERR 8001d75981aSEd L. Cashin "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 8011da177e4SLinus Torvalds ahout->cmdstat, ahin->cmdstat, 8021da177e4SLinus Torvalds d->aoemajor, d->aoeminor); 8031da177e4SLinus Torvalds if (buf) 8041da177e4SLinus Torvalds buf->flags |= BUFFL_FAIL; 8051da177e4SLinus Torvalds } else { 80668e0d42fSEd L. Cashin if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */ 80768e0d42fSEd L. Cashin d->htgt = NULL; 80819bf2635SEd L. Cashin n = ahout->scnt << 9; 8091da177e4SLinus Torvalds switch (ahout->cmdstat) { 81004b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ: 81104b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ_EXT: 8121da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < n) { 813a12c93f0SEd L. Cashin printk(KERN_ERR 81468e0d42fSEd L. Cashin "aoe: %s. skb->len=%d need=%ld\n", 81568e0d42fSEd L. Cashin "runt data size in read", skb->len, n); 8161da177e4SLinus Torvalds /* fail frame f? just returning will rexmit. */ 8171da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8181da177e4SLinus Torvalds return; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds memcpy(f->bufaddr, ahin+1, n); 82104b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE: 82204b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE_EXT: 82368e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 82468e0d42fSEd L. Cashin if (ifp) { 82568e0d42fSEd L. Cashin ifp->lost = 0; 82668e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 82768e0d42fSEd L. Cashin ifp->lostjumbo = 0; 82868e0d42fSEd L. Cashin } 82919bf2635SEd L. Cashin if (f->bcnt -= n) { 83068e0d42fSEd L. Cashin f->lba += n >> 9; 83119bf2635SEd L. Cashin f->bufaddr += n; 83268e0d42fSEd L. Cashin resend(d, t, f); 83368e0d42fSEd L. Cashin goto xmit; 8344f51dc5eSEd L. Cashin } 8351da177e4SLinus Torvalds break; 83604b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_ID_ATA: 8371da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < 512) { 838a12c93f0SEd L. Cashin printk(KERN_INFO 839a12c93f0SEd L. Cashin "aoe: runt data size in ataid. skb->len=%d\n", 8406bb6285fSEd L. Cashin skb->len); 8411da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8421da177e4SLinus Torvalds return; 8431da177e4SLinus Torvalds } 84468e0d42fSEd L. Cashin ataid_complete(d, t, (char *) (ahin+1)); 8451da177e4SLinus Torvalds break; 8461da177e4SLinus Torvalds default: 847a12c93f0SEd L. Cashin printk(KERN_INFO 848a12c93f0SEd L. Cashin "aoe: unrecognized ata command %2.2Xh for %d.%d\n", 8491da177e4SLinus Torvalds ahout->cmdstat, 850f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 8511da177e4SLinus Torvalds hin->minor); 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 85568e0d42fSEd L. Cashin if (buf && --buf->nframesout == 0 && buf->resid == 0) { 85603054de1SLinus Torvalds diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector); 8570a1f127aSPeter Horton if (buf->flags & BUFFL_FAIL) 8580a1f127aSPeter Horton bio_endio(buf->bio, -EIO); 8590a1f127aSPeter Horton else { 8606ec1480dSAndrew Morton bio_flush_dcache_pages(buf->bio); 8610a1f127aSPeter Horton bio_endio(buf->bio, 0); 8620a1f127aSPeter Horton } 8631da177e4SLinus Torvalds mempool_free(buf, d->bufpool); 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds f->buf = NULL; 8671da177e4SLinus Torvalds f->tag = FREETAG; 86868e0d42fSEd L. Cashin t->nout--; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds aoecmd_work(d); 87168e0d42fSEd L. Cashin xmit: 872e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 873e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 876e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds void 8801da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 8811da177e4SLinus Torvalds { 882e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 8831da177e4SLinus Torvalds 884e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 885e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 886e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 88968e0d42fSEd L. Cashin struct sk_buff * 8901da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 8911da177e4SLinus Torvalds { 8921da177e4SLinus Torvalds struct aoe_hdr *h; 8931da177e4SLinus Torvalds struct aoe_atahdr *ah; 8941da177e4SLinus Torvalds struct frame *f; 8951da177e4SLinus Torvalds struct sk_buff *skb; 89668e0d42fSEd L. Cashin struct aoetgt *t; 8971da177e4SLinus Torvalds 8984f51dc5eSEd L. Cashin f = freeframe(d); 89968e0d42fSEd L. Cashin if (f == NULL) 9001da177e4SLinus Torvalds return NULL; 90168e0d42fSEd L. Cashin 90268e0d42fSEd L. Cashin t = *d->tgt; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* initialize the headers & frame */ 905e407a7f6SEd L. Cashin skb = f->skb; 906abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9071da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 90819900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 90919900cdeSEd L. Cashin memset(h, 0, skb->len); 91068e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 91168e0d42fSEd L. Cashin t->nout++; 9121da177e4SLinus Torvalds f->waited = 0; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* set up ata header */ 9151da177e4SLinus Torvalds ah->scnt = 1; 91604b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_ID_ATA; 9171da177e4SLinus Torvalds ah->lba3 = 0xa0; 9181da177e4SLinus Torvalds 91968e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 9201da177e4SLinus Torvalds 9213ae1c24eSEd L. Cashin d->rttavg = MAXTIMER; 9221da177e4SLinus Torvalds d->timer.function = rexmit_timer; 9231da177e4SLinus Torvalds 9244f51dc5eSEd L. Cashin return skb_clone(skb, GFP_ATOMIC); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 92768e0d42fSEd L. Cashin static struct aoetgt * 92868e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes) 92968e0d42fSEd L. Cashin { 93068e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 93168e0d42fSEd L. Cashin struct frame *f, *e; 93268e0d42fSEd L. Cashin 93368e0d42fSEd L. Cashin tt = d->targets; 93468e0d42fSEd L. Cashin te = tt + NTARGETS; 93568e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) 93668e0d42fSEd L. Cashin ; 93768e0d42fSEd L. Cashin 938578c4aa0SEd L. Cashin if (tt == te) { 939578c4aa0SEd L. Cashin printk(KERN_INFO 940578c4aa0SEd L. Cashin "aoe: device addtgt failure; too many targets\n"); 94168e0d42fSEd L. Cashin return NULL; 942578c4aa0SEd L. Cashin } 94368e0d42fSEd L. Cashin t = kcalloc(1, sizeof *t, GFP_ATOMIC); 94468e0d42fSEd L. Cashin f = kcalloc(nframes, sizeof *f, GFP_ATOMIC); 945578c4aa0SEd L. Cashin if (!t || !f) { 946578c4aa0SEd L. Cashin kfree(f); 9479bb237b6SEd L. Cashin kfree(t); 948578c4aa0SEd L. Cashin printk(KERN_INFO "aoe: cannot allocate memory to add target\n"); 9499bb237b6SEd L. Cashin return NULL; 9509bb237b6SEd L. Cashin } 9519bb237b6SEd L. Cashin 95268e0d42fSEd L. Cashin t->nframes = nframes; 95368e0d42fSEd L. Cashin t->frames = f; 95468e0d42fSEd L. Cashin e = f + nframes; 9559bb237b6SEd L. Cashin for (; f < e; f++) 95668e0d42fSEd L. Cashin f->tag = FREETAG; 95768e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 95868e0d42fSEd L. Cashin t->ifp = t->ifs; 95968e0d42fSEd L. Cashin t->maxout = t->nframes; 96068e0d42fSEd L. Cashin return *tt = t; 96168e0d42fSEd L. Cashin } 96268e0d42fSEd L. Cashin 9631da177e4SLinus Torvalds void 9641da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds struct aoedev *d; 9671da177e4SLinus Torvalds struct aoe_hdr *h; 9681da177e4SLinus Torvalds struct aoe_cfghdr *ch; 96968e0d42fSEd L. Cashin struct aoetgt *t; 97068e0d42fSEd L. Cashin struct aoeif *ifp; 97163e9cc5dSecashin@coraid.com ulong flags, sysminor, aoemajor; 9721da177e4SLinus Torvalds struct sk_buff *sl; 97319bf2635SEd L. Cashin u16 n; 9741da177e4SLinus Torvalds 975abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9761da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds /* 9791da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 9801da177e4SLinus Torvalds * warrant a loud message for this special case. 9811da177e4SLinus Torvalds */ 982823ed72eSHarvey Harrison aoemajor = get_unaligned_be16(&h->major); 9831da177e4SLinus Torvalds if (aoemajor == 0xfff) { 984a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 9856bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 9861da177e4SLinus Torvalds return; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds sysminor = SYSMINOR(aoemajor, h->minor); 990fc458dcdSecashin@coraid.com if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) { 991a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n", 992fc458dcdSecashin@coraid.com aoemajor, (int) h->minor); 9931da177e4SLinus Torvalds return; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 99619bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 9977df620d8SEd L. Cashin if (n > aoe_maxout) /* keep it reasonable */ 9987df620d8SEd L. Cashin n = aoe_maxout; 9991da177e4SLinus Torvalds 100068e0d42fSEd L. Cashin d = aoedev_by_sysminor_m(sysminor); 10011da177e4SLinus Torvalds if (d == NULL) { 1002a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: device sysminor_m failure\n"); 10031da177e4SLinus Torvalds return; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 10071da177e4SLinus Torvalds 100868e0d42fSEd L. Cashin t = gettgt(d, h->src); 100968e0d42fSEd L. Cashin if (!t) { 101068e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 101168e0d42fSEd L. Cashin if (!t) { 101268e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 101368e0d42fSEd L. Cashin return; 101468e0d42fSEd L. Cashin } 101568e0d42fSEd L. Cashin } 101668e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 101768e0d42fSEd L. Cashin if (!ifp) { 101868e0d42fSEd L. Cashin ifp = addif(t, skb->dev); 101968e0d42fSEd L. Cashin if (!ifp) { 102068e0d42fSEd L. Cashin printk(KERN_INFO 102168e0d42fSEd L. Cashin "aoe: device addif failure; " 102268e0d42fSEd L. Cashin "too many interfaces?\n"); 102368e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 102468e0d42fSEd L. Cashin return; 102568e0d42fSEd L. Cashin } 102668e0d42fSEd L. Cashin } 102768e0d42fSEd L. Cashin if (ifp->maxbcnt) { 102868e0d42fSEd L. Cashin n = ifp->nd->mtu; 102919bf2635SEd L. Cashin n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr); 103019bf2635SEd L. Cashin n /= 512; 103119bf2635SEd L. Cashin if (n > ch->scnt) 103219bf2635SEd L. Cashin n = ch->scnt; 10334f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 103468e0d42fSEd L. Cashin if (n != ifp->maxbcnt) { 1035a12c93f0SEd L. Cashin printk(KERN_INFO 1036411c41eeSHarvey Harrison "aoe: e%ld.%d: setting %d%s%s:%pm\n", 103768e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, n, 103868e0d42fSEd L. Cashin " byte data frames on ", ifp->nd->name, 1039411c41eeSHarvey Harrison t->addr); 104068e0d42fSEd L. Cashin ifp->maxbcnt = n; 10414f51dc5eSEd L. Cashin } 104219bf2635SEd L. Cashin } 10433ae1c24eSEd L. Cashin 10443ae1c24eSEd L. Cashin /* don't change users' perspective */ 104568e0d42fSEd L. Cashin if (d->nopen) { 10461da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10471da177e4SLinus Torvalds return; 10481da177e4SLinus Torvalds } 104963e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 10501da177e4SLinus Torvalds 105168e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10541da177e4SLinus Torvalds 1055e9bb8fb0SDavid S. Miller if (sl) { 1056e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 1057e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1058e9bb8fb0SDavid S. Miller __skb_queue_tail(&queue, sl); 1059e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 1060e9bb8fb0SDavid S. Miller } 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106368e0d42fSEd L. Cashin void 106468e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 106568e0d42fSEd L. Cashin { 106668e0d42fSEd L. Cashin struct aoetgt **t, **te; 106768e0d42fSEd L. Cashin struct aoeif *p, *e; 106868e0d42fSEd L. Cashin 106968e0d42fSEd L. Cashin d->mintimer = MINTIMER; 107068e0d42fSEd L. Cashin 107168e0d42fSEd L. Cashin t = d->targets; 107268e0d42fSEd L. Cashin te = t + NTARGETS; 107368e0d42fSEd L. Cashin for (; t < te && *t; t++) { 107468e0d42fSEd L. Cashin (*t)->maxout = (*t)->nframes; 107568e0d42fSEd L. Cashin p = (*t)->ifs; 107668e0d42fSEd L. Cashin e = p + NAOEIFS; 107768e0d42fSEd L. Cashin for (; p < e; p++) { 107868e0d42fSEd L. Cashin p->lostjumbo = 0; 107968e0d42fSEd L. Cashin p->lost = 0; 108068e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 108168e0d42fSEd L. Cashin } 108268e0d42fSEd L. Cashin } 108368e0d42fSEd L. Cashin } 1084