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> 81da177e4SLinus Torvalds #include <linux/hdreg.h> 91da177e4SLinus Torvalds #include <linux/blkdev.h> 101da177e4SLinus Torvalds #include <linux/skbuff.h> 111da177e4SLinus Torvalds #include <linux/netdevice.h> 123ae1c24eSEd L. Cashin #include <linux/genhd.h> 1368e0d42fSEd L. Cashin #include <linux/moduleparam.h> 14881d966bSEric W. Biederman #include <net/net_namespace.h> 15475172fbSEd L. Cashin #include <asm/unaligned.h> 161da177e4SLinus Torvalds #include "aoe.h" 171da177e4SLinus Torvalds 18b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3; 19b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644); 20b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev."); 211da177e4SLinus Torvalds 227df620d8SEd L. Cashin static int aoe_maxout = 16; 237df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644); 247df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout, 257df620d8SEd L. Cashin "Only aoe_maxout outstanding packets for every MAC on eX.Y."); 267df620d8SEd L. Cashin 2768e0d42fSEd L. Cashin static struct sk_buff * 28e407a7f6SEd L. Cashin new_skb(ulong len) 291da177e4SLinus Torvalds { 301da177e4SLinus Torvalds struct sk_buff *skb; 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 331da177e4SLinus Torvalds if (skb) { 34459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 35c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 361da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 371da177e4SLinus Torvalds } 381da177e4SLinus Torvalds return skb; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static struct frame * 4268e0d42fSEd L. Cashin getframe(struct aoetgt *t, int tag) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds struct frame *f, *e; 451da177e4SLinus Torvalds 4668e0d42fSEd L. Cashin f = t->frames; 4768e0d42fSEd L. Cashin e = f + t->nframes; 481da177e4SLinus Torvalds for (; f<e; f++) 491da177e4SLinus Torvalds if (f->tag == tag) 501da177e4SLinus Torvalds return f; 511da177e4SLinus Torvalds return NULL; 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * Leave the top bit clear so we have tagspace for userland. 561da177e4SLinus Torvalds * The bottom 16 bits are the xmit tick for rexmit/rttavg processing. 571da177e4SLinus Torvalds * This driver reserves tag -1 to mean "unused frame." 581da177e4SLinus Torvalds */ 591da177e4SLinus Torvalds static int 6068e0d42fSEd L. Cashin newtag(struct aoetgt *t) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds register ulong n; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds n = jiffies & 0xffff; 6568e0d42fSEd L. Cashin return n |= (++t->lasttag & 0x7fff) << 16; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds static int 6968e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) 701da177e4SLinus Torvalds { 7168e0d42fSEd L. Cashin u32 host_tag = newtag(t); 721da177e4SLinus Torvalds 7368e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 7468e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 7563e9cc5dSecashin@coraid.com h->type = __constant_cpu_to_be16(ETH_P_AOE); 761da177e4SLinus Torvalds h->verfl = AOE_HVER; 7763e9cc5dSecashin@coraid.com h->major = cpu_to_be16(d->aoemajor); 781da177e4SLinus Torvalds h->minor = d->aoeminor; 791da177e4SLinus Torvalds h->cmd = AOECMD_ATA; 8063e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(host_tag); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds return host_tag; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 8519bf2635SEd L. Cashin static inline void 8619bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba) 8719bf2635SEd L. Cashin { 8819bf2635SEd L. Cashin ah->lba0 = lba; 8919bf2635SEd L. Cashin ah->lba1 = lba >>= 8; 9019bf2635SEd L. Cashin ah->lba2 = lba >>= 8; 9119bf2635SEd L. Cashin ah->lba3 = lba >>= 8; 9219bf2635SEd L. Cashin ah->lba4 = lba >>= 8; 9319bf2635SEd L. Cashin ah->lba5 = lba >>= 8; 9419bf2635SEd L. Cashin } 9519bf2635SEd L. Cashin 961da177e4SLinus Torvalds static void 9768e0d42fSEd L. Cashin ifrotate(struct aoetgt *t) 981da177e4SLinus Torvalds { 9968e0d42fSEd L. Cashin t->ifp++; 10068e0d42fSEd L. Cashin if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL) 10168e0d42fSEd L. Cashin t->ifp = t->ifs; 10268e0d42fSEd L. Cashin if (t->ifp->nd == NULL) { 10368e0d42fSEd L. Cashin printk(KERN_INFO "aoe: no interface to rotate to\n"); 10468e0d42fSEd L. Cashin BUG(); 10568e0d42fSEd L. Cashin } 10668e0d42fSEd L. Cashin } 10768e0d42fSEd L. Cashin 1089bb237b6SEd L. Cashin static void 1099bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 1109bb237b6SEd L. Cashin { 111e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->skbpool, skb); 1129bb237b6SEd L. Cashin } 1139bb237b6SEd L. Cashin 1149bb237b6SEd L. Cashin static struct sk_buff * 1159bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 1169bb237b6SEd L. Cashin { 117e9bb8fb0SDavid S. Miller struct sk_buff *skb = skb_peek(&d->skbpool); 1189bb237b6SEd L. Cashin 1199bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 120e9bb8fb0SDavid S. Miller __skb_unlink(skb, &d->skbpool); 1219bb237b6SEd L. Cashin return skb; 1229bb237b6SEd L. Cashin } 123e9bb8fb0SDavid S. Miller if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 124e9bb8fb0SDavid S. Miller (skb = new_skb(ETH_ZLEN))) 1259bb237b6SEd L. Cashin return skb; 126e9bb8fb0SDavid S. Miller 1279bb237b6SEd L. Cashin return NULL; 1289bb237b6SEd L. Cashin } 1299bb237b6SEd L. Cashin 1309bb237b6SEd L. Cashin /* freeframe is where we do our load balancing so it's a little hairy. */ 13168e0d42fSEd L. Cashin static struct frame * 13268e0d42fSEd L. Cashin freeframe(struct aoedev *d) 13368e0d42fSEd L. Cashin { 1349bb237b6SEd L. Cashin struct frame *f, *e, *rf; 13568e0d42fSEd L. Cashin struct aoetgt **t; 1369bb237b6SEd L. Cashin struct sk_buff *skb; 13768e0d42fSEd L. Cashin 13868e0d42fSEd L. Cashin if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */ 13968e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 14068e0d42fSEd L. Cashin return NULL; 14168e0d42fSEd L. Cashin } 1429bb237b6SEd L. Cashin t = d->tgt; 1439bb237b6SEd L. Cashin t++; 1449bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 14568e0d42fSEd L. Cashin t = d->targets; 1469bb237b6SEd L. Cashin for (;;) { 1479bb237b6SEd L. Cashin if ((*t)->nout < (*t)->maxout 1489bb237b6SEd L. Cashin && t != d->htgt 1499bb237b6SEd L. Cashin && (*t)->ifp->nd) { 1509bb237b6SEd L. Cashin rf = NULL; 15168e0d42fSEd L. Cashin f = (*t)->frames; 1529bb237b6SEd L. Cashin e = f + (*t)->nframes; 15368e0d42fSEd L. Cashin for (; f < e; f++) { 15468e0d42fSEd L. Cashin if (f->tag != FREETAG) 15568e0d42fSEd L. Cashin continue; 1569bb237b6SEd L. Cashin skb = f->skb; 1579bb237b6SEd L. Cashin if (!skb 1589bb237b6SEd L. Cashin && !(f->skb = skb = new_skb(ETH_ZLEN))) 1599bb237b6SEd L. Cashin continue; 1609bb237b6SEd L. Cashin if (atomic_read(&skb_shinfo(skb)->dataref) 16168e0d42fSEd L. Cashin != 1) { 1629bb237b6SEd L. Cashin if (!rf) 1639bb237b6SEd L. Cashin rf = f; 16468e0d42fSEd L. Cashin continue; 16568e0d42fSEd L. Cashin } 1669bb237b6SEd L. Cashin gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0; 1679bb237b6SEd L. Cashin skb_trim(skb, 0); 16868e0d42fSEd L. Cashin d->tgt = t; 16968e0d42fSEd L. Cashin ifrotate(*t); 17068e0d42fSEd L. Cashin return f; 17168e0d42fSEd L. Cashin } 1729bb237b6SEd L. Cashin /* Work can be done, but the network layer is 1739bb237b6SEd L. Cashin holding our precious packets. Try to grab 1749bb237b6SEd L. Cashin one from the pool. */ 1759bb237b6SEd L. Cashin f = rf; 1769bb237b6SEd L. Cashin if (f == NULL) { /* more paranoia */ 1779bb237b6SEd L. Cashin printk(KERN_ERR 1789bb237b6SEd L. Cashin "aoe: freeframe: %s.\n", 1799bb237b6SEd L. Cashin "unexpected null rf"); 1809bb237b6SEd L. Cashin d->flags |= DEVFL_KICKME; 1819bb237b6SEd L. Cashin return NULL; 1829bb237b6SEd L. Cashin } 1839bb237b6SEd L. Cashin skb = skb_pool_get(d); 1849bb237b6SEd L. Cashin if (skb) { 1859bb237b6SEd L. Cashin skb_pool_put(d, f->skb); 1869bb237b6SEd L. Cashin f->skb = skb; 1879bb237b6SEd L. Cashin goto gotone; 1889bb237b6SEd L. Cashin } 1899bb237b6SEd L. Cashin (*t)->dataref++; 1909bb237b6SEd L. Cashin if ((*t)->nout == 0) 19168e0d42fSEd L. Cashin d->flags |= DEVFL_KICKME; 19268e0d42fSEd L. Cashin } 1939bb237b6SEd L. Cashin if (t == d->tgt) /* we've looped and found nada */ 1949bb237b6SEd L. Cashin break; 19568e0d42fSEd L. Cashin t++; 1969bb237b6SEd L. Cashin if (t >= &d->targets[NTARGETS] || !*t) 1979bb237b6SEd L. Cashin t = d->targets; 1989bb237b6SEd L. Cashin } 19968e0d42fSEd L. Cashin return NULL; 20068e0d42fSEd L. Cashin } 20168e0d42fSEd L. Cashin 20268e0d42fSEd L. Cashin static int 20368e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 20468e0d42fSEd L. Cashin { 20568e0d42fSEd L. Cashin struct frame *f; 2061da177e4SLinus Torvalds struct aoe_hdr *h; 2071da177e4SLinus Torvalds struct aoe_atahdr *ah; 2081da177e4SLinus Torvalds struct buf *buf; 20968e0d42fSEd L. Cashin struct bio_vec *bv; 21068e0d42fSEd L. Cashin struct aoetgt *t; 2111da177e4SLinus Torvalds struct sk_buff *skb; 2121da177e4SLinus Torvalds ulong bcnt; 2131da177e4SLinus Torvalds char writebit, extbit; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds writebit = 0x10; 2161da177e4SLinus Torvalds extbit = 0x4; 2171da177e4SLinus Torvalds 21868e0d42fSEd L. Cashin f = freeframe(d); 21968e0d42fSEd L. Cashin if (f == NULL) 22068e0d42fSEd L. Cashin return 0; 22168e0d42fSEd L. Cashin t = *d->tgt; 2221da177e4SLinus Torvalds buf = d->inprocess; 22368e0d42fSEd L. Cashin bv = buf->bv; 22468e0d42fSEd L. Cashin bcnt = t->ifp->maxbcnt; 22568e0d42fSEd L. Cashin if (bcnt == 0) 22668e0d42fSEd L. Cashin bcnt = DEFAULTBCNT; 22768e0d42fSEd L. Cashin if (bcnt > buf->bv_resid) 2281da177e4SLinus Torvalds bcnt = buf->bv_resid; 2291da177e4SLinus Torvalds /* initialize the headers & frame */ 230e407a7f6SEd L. Cashin skb = f->skb; 231abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 2321da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 23319900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 23419900cdeSEd L. Cashin memset(h, 0, skb->len); 23568e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 23668e0d42fSEd L. Cashin t->nout++; 2371da177e4SLinus Torvalds f->waited = 0; 2381da177e4SLinus Torvalds f->buf = buf; 23968e0d42fSEd L. Cashin f->bufaddr = page_address(bv->bv_page) + buf->bv_off; 24019bf2635SEd L. Cashin f->bcnt = bcnt; 24168e0d42fSEd L. Cashin f->lba = buf->sector; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* set up ata header */ 2441da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 24568e0d42fSEd L. Cashin put_lba(ah, buf->sector); 2461da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 2471da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 2481da177e4SLinus Torvalds } else { 2491da177e4SLinus Torvalds extbit = 0; 2501da177e4SLinus Torvalds ah->lba3 &= 0x0f; 2511da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 25468e0d42fSEd L. Cashin skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt); 2551da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 2564f51dc5eSEd L. Cashin skb->len += bcnt; 2574f51dc5eSEd L. Cashin skb->data_len = bcnt; 25868e0d42fSEd L. Cashin t->wpkts++; 2591da177e4SLinus Torvalds } else { 26068e0d42fSEd L. Cashin t->rpkts++; 2611da177e4SLinus Torvalds writebit = 0; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 26404b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* mark all tracking fields and load out */ 2671da177e4SLinus Torvalds buf->nframesout += 1; 26868e0d42fSEd L. Cashin buf->bv_off += bcnt; 2691da177e4SLinus Torvalds buf->bv_resid -= bcnt; 2701da177e4SLinus Torvalds buf->resid -= bcnt; 2711da177e4SLinus Torvalds buf->sector += bcnt >> 9; 2721da177e4SLinus Torvalds if (buf->resid == 0) { 2731da177e4SLinus Torvalds d->inprocess = NULL; 2741da177e4SLinus Torvalds } else if (buf->bv_resid == 0) { 27568e0d42fSEd L. Cashin buf->bv = ++bv; 27668e0d42fSEd L. Cashin buf->bv_resid = bv->bv_len; 27768e0d42fSEd L. Cashin WARN_ON(buf->bv_resid == 0); 27868e0d42fSEd L. Cashin buf->bv_off = bv->bv_offset; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 28168e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 2824f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 283e9bb8fb0SDavid S. Miller if (skb) 284e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 28568e0d42fSEd L. Cashin return 1; 28668e0d42fSEd L. Cashin } 2871da177e4SLinus Torvalds 2883ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 2893ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 2903ae1c24eSEd L. Cashin */ 291e9bb8fb0SDavid S. Miller static void 292e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 2933ae1c24eSEd L. Cashin { 2943ae1c24eSEd L. Cashin struct aoe_hdr *h; 2953ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 296e9bb8fb0SDavid S. Miller struct sk_buff *skb; 2973ae1c24eSEd L. Cashin struct net_device *ifp; 2983ae1c24eSEd L. Cashin 2993ae1c24eSEd L. Cashin read_lock(&dev_base_lock); 300881d966bSEric W. Biederman for_each_netdev(&init_net, ifp) { 3013ae1c24eSEd L. Cashin dev_hold(ifp); 3023ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 3037562f876SPavel Emelianov goto cont; 3043ae1c24eSEd L. Cashin 305e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 3063ae1c24eSEd L. Cashin if (skb == NULL) { 307a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 3087562f876SPavel Emelianov goto cont; 3093ae1c24eSEd L. Cashin } 31019900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 311e407a7f6SEd L. Cashin skb->dev = ifp; 312e9bb8fb0SDavid S. Miller __skb_queue_tail(queue, skb); 313abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 3143ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 3153ae1c24eSEd L. Cashin 3163ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 3173ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 3183ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 3193ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 3203ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 3213ae1c24eSEd L. Cashin h->minor = aoeminor; 3223ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 3233ae1c24eSEd L. Cashin 3247562f876SPavel Emelianov cont: 3257562f876SPavel Emelianov dev_put(ifp); 3263ae1c24eSEd L. Cashin } 3273ae1c24eSEd L. Cashin read_unlock(&dev_base_lock); 3283ae1c24eSEd L. Cashin } 3293ae1c24eSEd L. Cashin 3301da177e4SLinus Torvalds static void 33168e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds struct sk_buff *skb; 3341da177e4SLinus Torvalds struct aoe_hdr *h; 33519bf2635SEd L. Cashin struct aoe_atahdr *ah; 3361da177e4SLinus Torvalds char buf[128]; 3371da177e4SLinus Torvalds u32 n; 3381da177e4SLinus Torvalds 33968e0d42fSEd L. Cashin ifrotate(t); 34068e0d42fSEd L. Cashin n = newtag(t); 341e407a7f6SEd L. Cashin skb = f->skb; 342abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 34319bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 34468e0d42fSEd L. Cashin 34568e0d42fSEd L. Cashin snprintf(buf, sizeof buf, 346411c41eeSHarvey Harrison "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", 34768e0d42fSEd L. Cashin "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n, 348411c41eeSHarvey Harrison h->src, h->dst, t->nout); 34968e0d42fSEd L. Cashin aoechr_error(buf); 35068e0d42fSEd L. Cashin 3511da177e4SLinus Torvalds f->tag = n; 35263e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 35368e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 35468e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 3551da177e4SLinus Torvalds 35668e0d42fSEd L. Cashin switch (ah->cmdstat) { 35768e0d42fSEd L. Cashin default: 35868e0d42fSEd L. Cashin break; 35904b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ: 36004b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ_EXT: 36104b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE: 36204b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE_EXT: 36368e0d42fSEd L. Cashin put_lba(ah, f->lba); 36468e0d42fSEd L. Cashin 36568e0d42fSEd L. Cashin n = f->bcnt; 36668e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 36768e0d42fSEd L. Cashin n = DEFAULTBCNT; 36868e0d42fSEd L. Cashin ah->scnt = n >> 9; 3694f51dc5eSEd L. Cashin if (ah->aflags & AOEAFL_WRITE) { 37019bf2635SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 37168e0d42fSEd L. Cashin offset_in_page(f->bufaddr), n); 37268e0d42fSEd L. Cashin skb->len = sizeof *h + sizeof *ah + n; 37368e0d42fSEd L. Cashin skb->data_len = n; 37419bf2635SEd L. Cashin } 37519bf2635SEd L. Cashin } 37668e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 3774f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 3784f51dc5eSEd L. Cashin if (skb == NULL) 3794f51dc5eSEd L. Cashin return; 380e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->sendq, skb); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds static int 3841da177e4SLinus Torvalds tsince(int tag) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds int n; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds n = jiffies & 0xffff; 3891da177e4SLinus Torvalds n -= tag & 0xffff; 3901da177e4SLinus Torvalds if (n < 0) 3911da177e4SLinus Torvalds n += 1<<16; 3921da177e4SLinus Torvalds return n; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 39568e0d42fSEd L. Cashin static struct aoeif * 39668e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 39768e0d42fSEd L. Cashin { 39868e0d42fSEd L. Cashin struct aoeif *p, *e; 39968e0d42fSEd L. Cashin 40068e0d42fSEd L. Cashin p = t->ifs; 40168e0d42fSEd L. Cashin e = p + NAOEIFS; 40268e0d42fSEd L. Cashin for (; p < e; p++) 40368e0d42fSEd L. Cashin if (p->nd == nd) 40468e0d42fSEd L. Cashin return p; 40568e0d42fSEd L. Cashin return NULL; 40668e0d42fSEd L. Cashin } 40768e0d42fSEd L. Cashin 40868e0d42fSEd L. Cashin static struct aoeif * 40968e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd) 41068e0d42fSEd L. Cashin { 41168e0d42fSEd L. Cashin struct aoeif *p; 41268e0d42fSEd L. Cashin 41368e0d42fSEd L. Cashin p = getif(t, NULL); 41468e0d42fSEd L. Cashin if (!p) 41568e0d42fSEd L. Cashin return NULL; 41668e0d42fSEd L. Cashin p->nd = nd; 41768e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 41868e0d42fSEd L. Cashin p->lost = 0; 41968e0d42fSEd L. Cashin p->lostjumbo = 0; 42068e0d42fSEd L. Cashin return p; 42168e0d42fSEd L. Cashin } 42268e0d42fSEd L. Cashin 42368e0d42fSEd L. Cashin static void 42468e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 42568e0d42fSEd L. Cashin { 42668e0d42fSEd L. Cashin struct aoeif *e; 42768e0d42fSEd L. Cashin ulong n; 42868e0d42fSEd L. Cashin 42968e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 43068e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 43168e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 43268e0d42fSEd L. Cashin e->nd = NULL; 43368e0d42fSEd L. Cashin } 43468e0d42fSEd L. Cashin 43568e0d42fSEd L. Cashin static int 43668e0d42fSEd L. Cashin sthtith(struct aoedev *d) 43768e0d42fSEd L. Cashin { 43868e0d42fSEd L. Cashin struct frame *f, *e, *nf; 43968e0d42fSEd L. Cashin struct sk_buff *skb; 44068e0d42fSEd L. Cashin struct aoetgt *ht = *d->htgt; 44168e0d42fSEd L. Cashin 44268e0d42fSEd L. Cashin f = ht->frames; 44368e0d42fSEd L. Cashin e = f + ht->nframes; 44468e0d42fSEd L. Cashin for (; f < e; f++) { 44568e0d42fSEd L. Cashin if (f->tag == FREETAG) 44668e0d42fSEd L. Cashin continue; 44768e0d42fSEd L. Cashin nf = freeframe(d); 44868e0d42fSEd L. Cashin if (!nf) 44968e0d42fSEd L. Cashin return 0; 45068e0d42fSEd L. Cashin skb = nf->skb; 45168e0d42fSEd L. Cashin *nf = *f; 45268e0d42fSEd L. Cashin f->skb = skb; 45368e0d42fSEd L. Cashin f->tag = FREETAG; 45468e0d42fSEd L. Cashin nf->waited = 0; 45568e0d42fSEd L. Cashin ht->nout--; 45668e0d42fSEd L. Cashin (*d->tgt)->nout++; 45768e0d42fSEd L. Cashin resend(d, *d->tgt, nf); 45868e0d42fSEd L. Cashin } 45968e0d42fSEd L. Cashin /* he's clean, he's useless. take away his interfaces */ 46068e0d42fSEd L. Cashin memset(ht->ifs, 0, sizeof ht->ifs); 46168e0d42fSEd L. Cashin d->htgt = NULL; 46268e0d42fSEd L. Cashin return 1; 46368e0d42fSEd L. Cashin } 46468e0d42fSEd L. Cashin 46568e0d42fSEd L. Cashin static inline unsigned char 46668e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) { 46768e0d42fSEd L. Cashin struct aoe_hdr *h; 46868e0d42fSEd L. Cashin struct aoe_atahdr *ah; 46968e0d42fSEd L. Cashin 47068e0d42fSEd L. Cashin h = (struct aoe_hdr *) packet; 47168e0d42fSEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 47268e0d42fSEd L. Cashin return ah->scnt; 47368e0d42fSEd L. Cashin } 47468e0d42fSEd L. Cashin 4751da177e4SLinus Torvalds static void 4761da177e4SLinus Torvalds rexmit_timer(ulong vp) 4771da177e4SLinus Torvalds { 478e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 4791da177e4SLinus Torvalds struct aoedev *d; 48068e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 48168e0d42fSEd L. Cashin struct aoeif *ifp; 4821da177e4SLinus Torvalds struct frame *f, *e; 4831da177e4SLinus Torvalds register long timeout; 4841da177e4SLinus Torvalds ulong flags, n; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds d = (struct aoedev *) vp; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* timeout is always ~150% of the moving average */ 4891da177e4SLinus Torvalds timeout = d->rttavg; 4901da177e4SLinus Torvalds timeout += timeout >> 1; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 4951c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 4961da177e4SLinus Torvalds return; 4971da177e4SLinus Torvalds } 49868e0d42fSEd L. Cashin tt = d->targets; 49968e0d42fSEd L. Cashin te = tt + NTARGETS; 50068e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) { 50168e0d42fSEd L. Cashin t = *tt; 50268e0d42fSEd L. Cashin f = t->frames; 50368e0d42fSEd L. Cashin e = f + t->nframes; 5041da177e4SLinus Torvalds for (; f < e; f++) { 50568e0d42fSEd L. Cashin if (f->tag == FREETAG 50668e0d42fSEd L. Cashin || tsince(f->tag) < timeout) 50768e0d42fSEd L. Cashin continue; 5081da177e4SLinus Torvalds n = f->waited += timeout; 5091da177e4SLinus Torvalds n /= HZ; 51068e0d42fSEd L. Cashin if (n > aoe_deadsecs) { 51168e0d42fSEd L. Cashin /* waited too long. device failure. */ 5121da177e4SLinus Torvalds aoedev_downdev(d); 5131c6f3fcaSEd L. Cashin break; 5141da177e4SLinus Torvalds } 51568e0d42fSEd L. Cashin 51668e0d42fSEd L. Cashin if (n > HELPWAIT /* see if another target can help */ 51768e0d42fSEd L. Cashin && (tt != d->targets || d->targets[1])) 51868e0d42fSEd L. Cashin d->htgt = tt; 51968e0d42fSEd L. Cashin 52068e0d42fSEd L. Cashin if (t->nout == t->maxout) { 52168e0d42fSEd L. Cashin if (t->maxout > 1) 52268e0d42fSEd L. Cashin t->maxout--; 52368e0d42fSEd L. Cashin t->lastwadj = jiffies; 52468e0d42fSEd L. Cashin } 52568e0d42fSEd L. Cashin 52668e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 52768e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 52868e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 52968e0d42fSEd L. Cashin ejectif(t, ifp); 53068e0d42fSEd L. Cashin ifp = NULL; 53168e0d42fSEd L. Cashin } 53268e0d42fSEd L. Cashin 53368e0d42fSEd L. Cashin if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512 53468e0d42fSEd L. Cashin && ifp && ++ifp->lostjumbo > (t->nframes << 1) 53568e0d42fSEd L. Cashin && ifp->maxbcnt != DEFAULTBCNT) { 53668e0d42fSEd L. Cashin printk(KERN_INFO 53768e0d42fSEd L. Cashin "aoe: e%ld.%d: " 53868e0d42fSEd L. Cashin "too many lost jumbo on " 539411c41eeSHarvey Harrison "%s:%pm - " 54068e0d42fSEd L. Cashin "falling back to %d frames.\n", 54168e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, 542411c41eeSHarvey Harrison ifp->nd->name, t->addr, 54368e0d42fSEd L. Cashin DEFAULTBCNT); 54468e0d42fSEd L. Cashin ifp->maxbcnt = 0; 54568e0d42fSEd L. Cashin } 54668e0d42fSEd L. Cashin resend(d, t, f); 54768e0d42fSEd L. Cashin } 54868e0d42fSEd L. Cashin 54968e0d42fSEd L. Cashin /* window check */ 55068e0d42fSEd L. Cashin if (t->nout == t->maxout 55168e0d42fSEd L. Cashin && t->maxout < t->nframes 55268e0d42fSEd L. Cashin && (jiffies - t->lastwadj)/HZ > 10) { 55368e0d42fSEd L. Cashin t->maxout++; 55468e0d42fSEd L. Cashin t->lastwadj = jiffies; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds } 55768e0d42fSEd L. Cashin 558e9bb8fb0SDavid S. Miller if (!skb_queue_empty(&d->sendq)) { 55968e0d42fSEd L. Cashin n = d->rttavg <<= 1; 56068e0d42fSEd L. Cashin if (n > MAXTIMER) 56168e0d42fSEd L. Cashin d->rttavg = MAXTIMER; 56268e0d42fSEd L. Cashin } 56368e0d42fSEd L. Cashin 56468e0d42fSEd L. Cashin if (d->flags & DEVFL_KICKME || d->htgt) { 5654f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 5664f51dc5eSEd L. Cashin aoecmd_work(d); 5674f51dc5eSEd L. Cashin } 5681da177e4SLinus Torvalds 569e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 570e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 5731da177e4SLinus Torvalds add_timer(&d->timer); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5761da177e4SLinus Torvalds 577e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 58068e0d42fSEd L. Cashin /* enters with d->lock held */ 58168e0d42fSEd L. Cashin void 58268e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 58368e0d42fSEd L. Cashin { 58468e0d42fSEd L. Cashin struct buf *buf; 58568e0d42fSEd L. Cashin loop: 58668e0d42fSEd L. Cashin if (d->htgt && !sthtith(d)) 58768e0d42fSEd L. Cashin return; 58868e0d42fSEd L. Cashin if (d->inprocess == NULL) { 58968e0d42fSEd L. Cashin if (list_empty(&d->bufq)) 59068e0d42fSEd L. Cashin return; 59168e0d42fSEd L. Cashin buf = container_of(d->bufq.next, struct buf, bufs); 59268e0d42fSEd L. Cashin list_del(d->bufq.next); 59368e0d42fSEd L. Cashin d->inprocess = buf; 59468e0d42fSEd L. Cashin } 59568e0d42fSEd L. Cashin if (aoecmd_ata_rw(d)) 59668e0d42fSEd L. Cashin goto loop; 59768e0d42fSEd L. Cashin } 59868e0d42fSEd L. Cashin 5993ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 6003ae1c24eSEd L. Cashin */ 6013ae1c24eSEd L. Cashin void 602c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 6033ae1c24eSEd L. Cashin { 604c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 6053ae1c24eSEd L. Cashin 6063ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 6073ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 6083ae1c24eSEd L. Cashin 6093ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 6103ae1c24eSEd L. Cashin struct block_device *bd; 6113ae1c24eSEd L. Cashin unsigned long flags; 6123ae1c24eSEd L. Cashin u64 ssize; 6133ae1c24eSEd L. Cashin 61480795aefSTejun Heo ssize = get_capacity(d->gd); 6153ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 6163ae1c24eSEd L. Cashin 6173ae1c24eSEd L. Cashin if (bd) { 6183ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 6193ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 6203ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 6213ae1c24eSEd L. Cashin bdput(bd); 6223ae1c24eSEd L. Cashin } 6233ae1c24eSEd L. Cashin spin_lock_irqsave(&d->lock, flags); 6243ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 6253ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 6263ae1c24eSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 6273ae1c24eSEd L. Cashin } 6283ae1c24eSEd L. Cashin } 6293ae1c24eSEd L. Cashin 6301da177e4SLinus Torvalds static void 63168e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 6321da177e4SLinus Torvalds { 6331da177e4SLinus Torvalds u64 ssize; 6341da177e4SLinus Torvalds u16 n; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* word 83: command set supported */ 637f885f8d1SHarvey Harrison n = get_unaligned_le16(&id[83 << 1]); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 640f885f8d1SHarvey Harrison n |= get_unaligned_le16(&id[86 << 1]); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 6431da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 646f885f8d1SHarvey Harrison ssize = get_unaligned_le64(&id[100 << 1]); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 6491da177e4SLinus Torvalds d->geo.cylinders = ssize; 6501da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 6511da177e4SLinus Torvalds d->geo.heads = 255; 6521da177e4SLinus Torvalds d->geo.sectors = 63; 6531da177e4SLinus Torvalds } else { 6541da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* number lba28 sectors */ 657f885f8d1SHarvey Harrison ssize = get_unaligned_le32(&id[60 << 1]); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 660f885f8d1SHarvey Harrison d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 661f885f8d1SHarvey Harrison d->geo.heads = get_unaligned_le16(&id[55 << 1]); 662f885f8d1SHarvey Harrison d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 6631da177e4SLinus Torvalds } 6643ae1c24eSEd L. Cashin 6653ae1c24eSEd L. Cashin if (d->ssize != ssize) 6661d75981aSEd L. Cashin printk(KERN_INFO 667411c41eeSHarvey Harrison "aoe: %pm e%ld.%d v%04x has %llu sectors\n", 668411c41eeSHarvey Harrison t->addr, 6693ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 6703ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 6711da177e4SLinus Torvalds d->ssize = ssize; 6721da177e4SLinus Torvalds d->geo.start = 0; 6736b9699bbSEd L. Cashin if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 6746b9699bbSEd L. Cashin return; 6751da177e4SLinus Torvalds if (d->gd != NULL) { 67680795aefSTejun Heo set_capacity(d->gd, ssize); 6773ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 67868e0d42fSEd L. Cashin } else 6793ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 6801da177e4SLinus Torvalds schedule_work(&d->work); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds static void 6841da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds register long n; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds n = rtt; 689dced3a05SEd L. Cashin if (n < 0) { 690dced3a05SEd L. Cashin n = -rtt; 6911da177e4SLinus Torvalds if (n < MINTIMER) 6921da177e4SLinus Torvalds n = MINTIMER; 6931da177e4SLinus Torvalds else if (n > MAXTIMER) 6941da177e4SLinus Torvalds n = MAXTIMER; 695dced3a05SEd L. Cashin d->mintimer += (n - d->mintimer) >> 1; 696dced3a05SEd L. Cashin } else if (n < d->mintimer) 697dced3a05SEd L. Cashin n = d->mintimer; 698dced3a05SEd L. Cashin else if (n > MAXTIMER) 699dced3a05SEd L. Cashin n = MAXTIMER; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */ 7021da177e4SLinus Torvalds n -= d->rttavg; 7031da177e4SLinus Torvalds d->rttavg += n >> 2; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 70668e0d42fSEd L. Cashin static struct aoetgt * 70768e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 70868e0d42fSEd L. Cashin { 70968e0d42fSEd L. Cashin struct aoetgt **t, **e; 71068e0d42fSEd L. Cashin 71168e0d42fSEd L. Cashin t = d->targets; 71268e0d42fSEd L. Cashin e = t + NTARGETS; 71368e0d42fSEd L. Cashin for (; t < e && *t; t++) 71468e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 71568e0d42fSEd L. Cashin return *t; 71668e0d42fSEd L. Cashin return NULL; 71768e0d42fSEd L. Cashin } 71868e0d42fSEd L. Cashin 71968e0d42fSEd L. Cashin static inline void 72003054de1SLinus Torvalds diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector) 72168e0d42fSEd L. Cashin { 72268e0d42fSEd L. Cashin unsigned long n_sect = bio->bi_size >> 9; 72368e0d42fSEd L. Cashin const int rw = bio_data_dir(bio); 72428f13702SJens Axboe struct hd_struct *part; 725c9959059STejun Heo int cpu; 72668e0d42fSEd L. Cashin 727074a7acaSTejun Heo cpu = part_stat_lock(); 728e71bf0d0STejun Heo part = disk_map_sector_rcu(disk, sector); 729e71bf0d0STejun Heo 730074a7acaSTejun Heo part_stat_inc(cpu, part, ios[rw]); 731074a7acaSTejun Heo part_stat_add(cpu, part, ticks[rw], duration); 732074a7acaSTejun Heo part_stat_add(cpu, part, sectors[rw], n_sect); 733074a7acaSTejun Heo part_stat_add(cpu, part, io_ticks, duration); 734c9959059STejun Heo 735074a7acaSTejun Heo part_stat_unlock(); 73668e0d42fSEd L. Cashin } 73768e0d42fSEd L. Cashin 738*0a1f127aSPeter Horton /* 739*0a1f127aSPeter Horton * Ensure we don't create aliases in VI caches 740*0a1f127aSPeter Horton */ 741*0a1f127aSPeter Horton static inline void 742*0a1f127aSPeter Horton killalias(struct bio *bio) 743*0a1f127aSPeter Horton { 744*0a1f127aSPeter Horton struct bio_vec *bv; 745*0a1f127aSPeter Horton int i; 746*0a1f127aSPeter Horton 747*0a1f127aSPeter Horton if (bio_data_dir(bio) == READ) 748*0a1f127aSPeter Horton __bio_for_each_segment(bv, bio, i, 0) { 749*0a1f127aSPeter Horton flush_dcache_page(bv->bv_page); 750*0a1f127aSPeter Horton } 751*0a1f127aSPeter Horton } 752*0a1f127aSPeter Horton 7531da177e4SLinus Torvalds void 7541da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb) 7551da177e4SLinus Torvalds { 756e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 7571da177e4SLinus Torvalds struct aoedev *d; 758ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 7591da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 7601da177e4SLinus Torvalds struct frame *f; 7611da177e4SLinus Torvalds struct buf *buf; 76268e0d42fSEd L. Cashin struct aoetgt *t; 76368e0d42fSEd L. Cashin struct aoeif *ifp; 7641da177e4SLinus Torvalds register long n; 7651da177e4SLinus Torvalds ulong flags; 7661da177e4SLinus Torvalds char ebuf[128]; 76732465c65Secashin@coraid.com u16 aoemajor; 7681da177e4SLinus Torvalds 769abdbf94dSEd L. Cashin hin = (struct aoe_hdr *) skb_mac_header(skb); 770f885f8d1SHarvey Harrison aoemajor = get_unaligned_be16(&hin->major); 77132465c65Secashin@coraid.com d = aoedev_by_aoeaddr(aoemajor, hin->minor); 7721da177e4SLinus Torvalds if (d == NULL) { 7731da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 7741da177e4SLinus Torvalds "for unknown device %d.%d\n", 77532465c65Secashin@coraid.com aoemajor, hin->minor); 7761da177e4SLinus Torvalds aoechr_error(ebuf); 7771da177e4SLinus Torvalds return; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 7811da177e4SLinus Torvalds 782f885f8d1SHarvey Harrison n = get_unaligned_be32(&hin->tag); 78368e0d42fSEd L. Cashin t = gettgt(d, hin->src); 78468e0d42fSEd L. Cashin if (t == NULL) { 785411c41eeSHarvey Harrison printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n", 786411c41eeSHarvey Harrison d->aoemajor, d->aoeminor, hin->src); 78768e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 78868e0d42fSEd L. Cashin return; 78968e0d42fSEd L. Cashin } 79068e0d42fSEd L. Cashin f = getframe(t, n); 7911da177e4SLinus Torvalds if (f == NULL) { 792dced3a05SEd L. Cashin calc_rttavg(d, -tsince(n)); 7931da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7941da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, 7951da177e4SLinus Torvalds "%15s e%d.%d tag=%08x@%08lx\n", 7961da177e4SLinus Torvalds "unexpected rsp", 797f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 7981da177e4SLinus Torvalds hin->minor, 799f885f8d1SHarvey Harrison get_unaligned_be32(&hin->tag), 8001da177e4SLinus Torvalds jiffies); 8011da177e4SLinus Torvalds aoechr_error(ebuf); 8021da177e4SLinus Torvalds return; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds calc_rttavg(d, tsince(f->tag)); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds ahin = (struct aoe_atahdr *) (hin+1); 808abdbf94dSEd L. Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 809ddec63e8SEd L. Cashin ahout = (struct aoe_atahdr *) (hout+1); 8101da177e4SLinus Torvalds buf = f->buf; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 813a12c93f0SEd L. Cashin printk(KERN_ERR 8141d75981aSEd L. Cashin "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 8151da177e4SLinus Torvalds ahout->cmdstat, ahin->cmdstat, 8161da177e4SLinus Torvalds d->aoemajor, d->aoeminor); 8171da177e4SLinus Torvalds if (buf) 8181da177e4SLinus Torvalds buf->flags |= BUFFL_FAIL; 8191da177e4SLinus Torvalds } else { 82068e0d42fSEd L. Cashin if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */ 82168e0d42fSEd L. Cashin d->htgt = NULL; 82219bf2635SEd L. Cashin n = ahout->scnt << 9; 8231da177e4SLinus Torvalds switch (ahout->cmdstat) { 82404b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ: 82504b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_READ_EXT: 8261da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < n) { 827a12c93f0SEd L. Cashin printk(KERN_ERR 82868e0d42fSEd L. Cashin "aoe: %s. skb->len=%d need=%ld\n", 82968e0d42fSEd L. Cashin "runt data size in read", skb->len, n); 8301da177e4SLinus Torvalds /* fail frame f? just returning will rexmit. */ 8311da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8321da177e4SLinus Torvalds return; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds memcpy(f->bufaddr, ahin+1, n); 83504b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE: 83604b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_PIO_WRITE_EXT: 83768e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 83868e0d42fSEd L. Cashin if (ifp) { 83968e0d42fSEd L. Cashin ifp->lost = 0; 84068e0d42fSEd L. Cashin if (n > DEFAULTBCNT) 84168e0d42fSEd L. Cashin ifp->lostjumbo = 0; 84268e0d42fSEd L. Cashin } 84319bf2635SEd L. Cashin if (f->bcnt -= n) { 84468e0d42fSEd L. Cashin f->lba += n >> 9; 84519bf2635SEd L. Cashin f->bufaddr += n; 84668e0d42fSEd L. Cashin resend(d, t, f); 84768e0d42fSEd L. Cashin goto xmit; 8484f51dc5eSEd L. Cashin } 8491da177e4SLinus Torvalds break; 85004b3ab52SBartlomiej Zolnierkiewicz case ATA_CMD_ID_ATA: 8511da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < 512) { 852a12c93f0SEd L. Cashin printk(KERN_INFO 853a12c93f0SEd L. Cashin "aoe: runt data size in ataid. skb->len=%d\n", 8546bb6285fSEd L. Cashin skb->len); 8551da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 8561da177e4SLinus Torvalds return; 8571da177e4SLinus Torvalds } 85868e0d42fSEd L. Cashin ataid_complete(d, t, (char *) (ahin+1)); 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds default: 861a12c93f0SEd L. Cashin printk(KERN_INFO 862a12c93f0SEd L. Cashin "aoe: unrecognized ata command %2.2Xh for %d.%d\n", 8631da177e4SLinus Torvalds ahout->cmdstat, 864f885f8d1SHarvey Harrison get_unaligned_be16(&hin->major), 8651da177e4SLinus Torvalds hin->minor); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86968e0d42fSEd L. Cashin if (buf && --buf->nframesout == 0 && buf->resid == 0) { 87003054de1SLinus Torvalds diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector); 871*0a1f127aSPeter Horton if (buf->flags & BUFFL_FAIL) 872*0a1f127aSPeter Horton bio_endio(buf->bio, -EIO); 873*0a1f127aSPeter Horton else { 874*0a1f127aSPeter Horton killalias(buf->bio); 875*0a1f127aSPeter Horton bio_endio(buf->bio, 0); 876*0a1f127aSPeter Horton } 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: 886e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 887e9bb8fb0SDavid S. Miller skb_queue_splice_init(&d->sendq, &queue); 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 890e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds void 8941da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 8951da177e4SLinus Torvalds { 896e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 8971da177e4SLinus Torvalds 898e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 899e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 900e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 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; 93004b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_ID_ATA; 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 952578c4aa0SEd L. Cashin if (tt == te) { 953578c4aa0SEd L. Cashin printk(KERN_INFO 954578c4aa0SEd L. Cashin "aoe: device addtgt failure; too many targets\n"); 95568e0d42fSEd L. Cashin return NULL; 956578c4aa0SEd L. Cashin } 95768e0d42fSEd L. Cashin t = kcalloc(1, sizeof *t, GFP_ATOMIC); 95868e0d42fSEd L. Cashin f = kcalloc(nframes, sizeof *f, GFP_ATOMIC); 959578c4aa0SEd L. Cashin if (!t || !f) { 960578c4aa0SEd L. Cashin kfree(f); 9619bb237b6SEd L. Cashin kfree(t); 962578c4aa0SEd L. Cashin printk(KERN_INFO "aoe: cannot allocate memory to add target\n"); 9639bb237b6SEd L. Cashin return NULL; 9649bb237b6SEd L. Cashin } 9659bb237b6SEd L. Cashin 96668e0d42fSEd L. Cashin t->nframes = nframes; 96768e0d42fSEd L. Cashin t->frames = f; 96868e0d42fSEd L. Cashin e = f + nframes; 9699bb237b6SEd L. Cashin for (; f < e; f++) 97068e0d42fSEd L. Cashin f->tag = FREETAG; 97168e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 97268e0d42fSEd L. Cashin t->ifp = t->ifs; 97368e0d42fSEd L. Cashin t->maxout = t->nframes; 97468e0d42fSEd L. Cashin return *tt = t; 97568e0d42fSEd L. Cashin } 97668e0d42fSEd L. Cashin 9771da177e4SLinus Torvalds void 9781da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 9791da177e4SLinus Torvalds { 9801da177e4SLinus Torvalds struct aoedev *d; 9811da177e4SLinus Torvalds struct aoe_hdr *h; 9821da177e4SLinus Torvalds struct aoe_cfghdr *ch; 98368e0d42fSEd L. Cashin struct aoetgt *t; 98468e0d42fSEd L. Cashin struct aoeif *ifp; 98563e9cc5dSecashin@coraid.com ulong flags, sysminor, aoemajor; 9861da177e4SLinus Torvalds struct sk_buff *sl; 98719bf2635SEd L. Cashin u16 n; 9881da177e4SLinus Torvalds 989abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 9901da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /* 9931da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 9941da177e4SLinus Torvalds * warrant a loud message for this special case. 9951da177e4SLinus Torvalds */ 996823ed72eSHarvey Harrison aoemajor = get_unaligned_be16(&h->major); 9971da177e4SLinus Torvalds if (aoemajor == 0xfff) { 998a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 9996bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 10001da177e4SLinus Torvalds return; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds sysminor = SYSMINOR(aoemajor, h->minor); 1004fc458dcdSecashin@coraid.com if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) { 1005a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n", 1006fc458dcdSecashin@coraid.com aoemajor, (int) h->minor); 10071da177e4SLinus Torvalds return; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 101019bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 10117df620d8SEd L. Cashin if (n > aoe_maxout) /* keep it reasonable */ 10127df620d8SEd L. Cashin n = aoe_maxout; 10131da177e4SLinus Torvalds 101468e0d42fSEd L. Cashin d = aoedev_by_sysminor_m(sysminor); 10151da177e4SLinus Torvalds if (d == NULL) { 1016a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: device sysminor_m failure\n"); 10171da177e4SLinus Torvalds return; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 10211da177e4SLinus Torvalds 102268e0d42fSEd L. Cashin t = gettgt(d, h->src); 102368e0d42fSEd L. Cashin if (!t) { 102468e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 102568e0d42fSEd L. Cashin if (!t) { 102668e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 102768e0d42fSEd L. Cashin return; 102868e0d42fSEd L. Cashin } 102968e0d42fSEd L. Cashin } 103068e0d42fSEd L. Cashin ifp = getif(t, skb->dev); 103168e0d42fSEd L. Cashin if (!ifp) { 103268e0d42fSEd L. Cashin ifp = addif(t, skb->dev); 103368e0d42fSEd L. Cashin if (!ifp) { 103468e0d42fSEd L. Cashin printk(KERN_INFO 103568e0d42fSEd L. Cashin "aoe: device addif failure; " 103668e0d42fSEd L. Cashin "too many interfaces?\n"); 103768e0d42fSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 103868e0d42fSEd L. Cashin return; 103968e0d42fSEd L. Cashin } 104068e0d42fSEd L. Cashin } 104168e0d42fSEd L. Cashin if (ifp->maxbcnt) { 104268e0d42fSEd L. Cashin n = ifp->nd->mtu; 104319bf2635SEd L. Cashin n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr); 104419bf2635SEd L. Cashin n /= 512; 104519bf2635SEd L. Cashin if (n > ch->scnt) 104619bf2635SEd L. Cashin n = ch->scnt; 10474f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 104868e0d42fSEd L. Cashin if (n != ifp->maxbcnt) { 1049a12c93f0SEd L. Cashin printk(KERN_INFO 1050411c41eeSHarvey Harrison "aoe: e%ld.%d: setting %d%s%s:%pm\n", 105168e0d42fSEd L. Cashin d->aoemajor, d->aoeminor, n, 105268e0d42fSEd L. Cashin " byte data frames on ", ifp->nd->name, 1053411c41eeSHarvey Harrison t->addr); 105468e0d42fSEd L. Cashin ifp->maxbcnt = n; 10554f51dc5eSEd L. Cashin } 105619bf2635SEd L. Cashin } 10573ae1c24eSEd L. Cashin 10583ae1c24eSEd L. Cashin /* don't change users' perspective */ 105968e0d42fSEd L. Cashin if (d->nopen) { 10601da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10611da177e4SLinus Torvalds return; 10621da177e4SLinus Torvalds } 106363e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 10641da177e4SLinus Torvalds 106568e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 10681da177e4SLinus Torvalds 1069e9bb8fb0SDavid S. Miller if (sl) { 1070e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 1071e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1072e9bb8fb0SDavid S. Miller __skb_queue_tail(&queue, sl); 1073e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 1074e9bb8fb0SDavid S. Miller } 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 107768e0d42fSEd L. Cashin void 107868e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 107968e0d42fSEd L. Cashin { 108068e0d42fSEd L. Cashin struct aoetgt **t, **te; 108168e0d42fSEd L. Cashin struct aoeif *p, *e; 108268e0d42fSEd L. Cashin 108368e0d42fSEd L. Cashin d->mintimer = MINTIMER; 108468e0d42fSEd L. Cashin 108568e0d42fSEd L. Cashin t = d->targets; 108668e0d42fSEd L. Cashin te = t + NTARGETS; 108768e0d42fSEd L. Cashin for (; t < te && *t; t++) { 108868e0d42fSEd L. Cashin (*t)->maxout = (*t)->nframes; 108968e0d42fSEd L. Cashin p = (*t)->ifs; 109068e0d42fSEd L. Cashin e = p + NAOEIFS; 109168e0d42fSEd L. Cashin for (; p < e; p++) { 109268e0d42fSEd L. Cashin p->lostjumbo = 0; 109368e0d42fSEd L. Cashin p->lost = 0; 109468e0d42fSEd L. Cashin p->maxbcnt = DEFAULTBCNT; 109568e0d42fSEd L. Cashin } 109668e0d42fSEd L. Cashin } 109768e0d42fSEd L. Cashin } 1098