12611464dSEd L. Cashin /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * aoecmd.c 41da177e4SLinus Torvalds * Filesystem request handling methods 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds #include <linux/hdreg.h> 81da177e4SLinus Torvalds #include <linux/blkdev.h> 91da177e4SLinus Torvalds #include <linux/skbuff.h> 101da177e4SLinus Torvalds #include <linux/netdevice.h> 113ae1c24eSEd L. Cashin #include <linux/genhd.h> 12475172fbSEd L. Cashin #include <asm/unaligned.h> 131da177e4SLinus Torvalds #include "aoe.h" 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #define TIMERTICK (HZ / 10) 161da177e4SLinus Torvalds #define MINTIMER (2 * TIMERTICK) 171da177e4SLinus Torvalds #define MAXTIMER (HZ << 1) 181da177e4SLinus Torvalds #define MAXWAIT (60 * 3) /* After MAXWAIT seconds, give up and fail dev */ 191da177e4SLinus Torvalds 20e407a7f6SEd L. Cashin struct sk_buff * 21e407a7f6SEd L. Cashin new_skb(ulong len) 221da177e4SLinus Torvalds { 231da177e4SLinus Torvalds struct sk_buff *skb; 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 261da177e4SLinus Torvalds if (skb) { 271da177e4SLinus Torvalds skb->nh.raw = skb->mac.raw = skb->data; 281da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 291da177e4SLinus Torvalds skb->priority = 0; 301da177e4SLinus Torvalds skb_put(skb, len); 3150bba752SEd L. Cashin memset(skb->head, 0, len); 321da177e4SLinus Torvalds skb->next = skb->prev = NULL; 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* tell the network layer not to perform IP checksums 351da177e4SLinus Torvalds * or to get the NIC to do it 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 381da177e4SLinus Torvalds } 391da177e4SLinus Torvalds return skb; 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static struct frame * 431da177e4SLinus Torvalds getframe(struct aoedev *d, int tag) 441da177e4SLinus Torvalds { 451da177e4SLinus Torvalds struct frame *f, *e; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds f = d->frames; 481da177e4SLinus Torvalds e = f + d->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 611da177e4SLinus Torvalds newtag(struct aoedev *d) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds register ulong n; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds n = jiffies & 0xffff; 661da177e4SLinus Torvalds return n |= (++d->lasttag & 0x7fff) << 16; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds static int 701da177e4SLinus Torvalds aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h) 711da177e4SLinus Torvalds { 721da177e4SLinus Torvalds u32 host_tag = newtag(d); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds memcpy(h->src, d->ifp->dev_addr, sizeof h->src); 751da177e4SLinus Torvalds memcpy(h->dst, d->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 981da177e4SLinus Torvalds aoecmd_ata_rw(struct aoedev *d, struct frame *f) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds struct aoe_hdr *h; 1011da177e4SLinus Torvalds struct aoe_atahdr *ah; 1021da177e4SLinus Torvalds struct buf *buf; 1031da177e4SLinus Torvalds struct sk_buff *skb; 1041da177e4SLinus Torvalds ulong bcnt; 1051da177e4SLinus Torvalds register sector_t sector; 1061da177e4SLinus Torvalds char writebit, extbit; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds writebit = 0x10; 1091da177e4SLinus Torvalds extbit = 0x4; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds buf = d->inprocess; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds sector = buf->sector; 1141da177e4SLinus Torvalds bcnt = buf->bv_resid; 11519bf2635SEd L. Cashin if (bcnt > d->maxbcnt) 11619bf2635SEd L. Cashin bcnt = d->maxbcnt; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* initialize the headers & frame */ 119e407a7f6SEd L. Cashin skb = f->skb; 120e407a7f6SEd L. Cashin h = (struct aoe_hdr *) skb->mac.raw; 1211da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 122e407a7f6SEd L. Cashin skb->len = sizeof *h + sizeof *ah; 123e407a7f6SEd L. Cashin memset(h, 0, skb->len); 1241da177e4SLinus Torvalds f->tag = aoehdr_atainit(d, h); 1251da177e4SLinus Torvalds f->waited = 0; 1261da177e4SLinus Torvalds f->buf = buf; 1271da177e4SLinus Torvalds f->bufaddr = buf->bufaddr; 12819bf2635SEd L. Cashin f->bcnt = bcnt; 12919bf2635SEd L. Cashin f->lba = sector; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* set up ata header */ 1321da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 13319bf2635SEd L. Cashin put_lba(ah, sector); 1341da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 1351da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 1361da177e4SLinus Torvalds } else { 1371da177e4SLinus Torvalds extbit = 0; 1381da177e4SLinus Torvalds ah->lba3 &= 0x0f; 1391da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 143e407a7f6SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 144e407a7f6SEd L. Cashin offset_in_page(f->bufaddr), bcnt); 1451da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 1461da177e4SLinus Torvalds } else { 147e407a7f6SEd L. Cashin skb_shinfo(skb)->nr_frags = 0; 148e407a7f6SEd L. Cashin skb->len = ETH_ZLEN; 1491da177e4SLinus Torvalds writebit = 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds ah->cmdstat = WIN_READ | writebit | extbit; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* mark all tracking fields and load out */ 1551da177e4SLinus Torvalds buf->nframesout += 1; 1561da177e4SLinus Torvalds buf->bufaddr += bcnt; 1571da177e4SLinus Torvalds buf->bv_resid -= bcnt; 1586bb6285fSEd L. Cashin /* dprintk("bv_resid=%ld\n", buf->bv_resid); */ 1591da177e4SLinus Torvalds buf->resid -= bcnt; 1601da177e4SLinus Torvalds buf->sector += bcnt >> 9; 1611da177e4SLinus Torvalds if (buf->resid == 0) { 1621da177e4SLinus Torvalds d->inprocess = NULL; 1631da177e4SLinus Torvalds } else if (buf->bv_resid == 0) { 1641da177e4SLinus Torvalds buf->bv++; 1651da177e4SLinus Torvalds buf->bv_resid = buf->bv->bv_len; 1661da177e4SLinus Torvalds buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 169e407a7f6SEd L. Cashin skb->dev = d->ifp; 170e407a7f6SEd L. Cashin skb_get(skb); 171a4b38364Secashin@coraid.com skb->next = NULL; 172a4b38364Secashin@coraid.com if (d->sendq_hd) 173a4b38364Secashin@coraid.com d->sendq_tl->next = skb; 174a4b38364Secashin@coraid.com else 175a4b38364Secashin@coraid.com d->sendq_hd = skb; 176a4b38364Secashin@coraid.com d->sendq_tl = skb; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1793ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 1803ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 1813ae1c24eSEd L. Cashin */ 1823ae1c24eSEd L. Cashin static struct sk_buff * 1833ae1c24eSEd L. Cashin aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail) 1843ae1c24eSEd L. Cashin { 1853ae1c24eSEd L. Cashin struct aoe_hdr *h; 1863ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 1873ae1c24eSEd L. Cashin struct sk_buff *skb, *sl, *sl_tail; 1883ae1c24eSEd L. Cashin struct net_device *ifp; 1893ae1c24eSEd L. Cashin 1903ae1c24eSEd L. Cashin sl = sl_tail = NULL; 1913ae1c24eSEd L. Cashin 1923ae1c24eSEd L. Cashin read_lock(&dev_base_lock); 1933ae1c24eSEd L. Cashin for (ifp = dev_base; ifp; dev_put(ifp), ifp = ifp->next) { 1943ae1c24eSEd L. Cashin dev_hold(ifp); 1953ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 1963ae1c24eSEd L. Cashin continue; 1973ae1c24eSEd L. Cashin 198e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 1993ae1c24eSEd L. Cashin if (skb == NULL) { 2006bb6285fSEd L. Cashin iprintk("skb alloc failure\n"); 2013ae1c24eSEd L. Cashin continue; 2023ae1c24eSEd L. Cashin } 203e407a7f6SEd L. Cashin skb->dev = ifp; 2043ae1c24eSEd L. Cashin if (sl_tail == NULL) 2053ae1c24eSEd L. Cashin sl_tail = skb; 2063ae1c24eSEd L. Cashin h = (struct aoe_hdr *) skb->mac.raw; 2073ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 2083ae1c24eSEd L. Cashin 2093ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 2103ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 2113ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 2123ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 2133ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 2143ae1c24eSEd L. Cashin h->minor = aoeminor; 2153ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 2163ae1c24eSEd L. Cashin 2173ae1c24eSEd L. Cashin skb->next = sl; 2183ae1c24eSEd L. Cashin sl = skb; 2193ae1c24eSEd L. Cashin } 2203ae1c24eSEd L. Cashin read_unlock(&dev_base_lock); 2213ae1c24eSEd L. Cashin 2223ae1c24eSEd L. Cashin if (tail != NULL) 2233ae1c24eSEd L. Cashin *tail = sl_tail; 2243ae1c24eSEd L. Cashin return sl; 2253ae1c24eSEd L. Cashin } 2263ae1c24eSEd L. Cashin 2271da177e4SLinus Torvalds /* enters with d->lock held */ 2281da177e4SLinus Torvalds void 2291da177e4SLinus Torvalds aoecmd_work(struct aoedev *d) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds struct frame *f; 2321da177e4SLinus Torvalds struct buf *buf; 2333ae1c24eSEd L. Cashin 2343ae1c24eSEd L. Cashin if (d->flags & DEVFL_PAUSE) { 2353ae1c24eSEd L. Cashin if (!aoedev_isbusy(d)) 2363ae1c24eSEd L. Cashin d->sendq_hd = aoecmd_cfg_pkts(d->aoemajor, 2373ae1c24eSEd L. Cashin d->aoeminor, &d->sendq_tl); 2383ae1c24eSEd L. Cashin return; 2393ae1c24eSEd L. Cashin } 2403ae1c24eSEd L. Cashin 2411da177e4SLinus Torvalds loop: 2421da177e4SLinus Torvalds f = getframe(d, FREETAG); 2431da177e4SLinus Torvalds if (f == NULL) 2441da177e4SLinus Torvalds return; 2451da177e4SLinus Torvalds if (d->inprocess == NULL) { 2461da177e4SLinus Torvalds if (list_empty(&d->bufq)) 2471da177e4SLinus Torvalds return; 2481da177e4SLinus Torvalds buf = container_of(d->bufq.next, struct buf, bufs); 2491da177e4SLinus Torvalds list_del(d->bufq.next); 2506bb6285fSEd L. Cashin /*dprintk("bi_size=%ld\n", buf->bio->bi_size); */ 2511da177e4SLinus Torvalds d->inprocess = buf; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds aoecmd_ata_rw(d, f); 2541da177e4SLinus Torvalds goto loop; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static void 2581da177e4SLinus Torvalds rexmit(struct aoedev *d, struct frame *f) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds struct sk_buff *skb; 2611da177e4SLinus Torvalds struct aoe_hdr *h; 26219bf2635SEd L. Cashin struct aoe_atahdr *ah; 2631da177e4SLinus Torvalds char buf[128]; 2641da177e4SLinus Torvalds u32 n; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds n = newtag(d); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds snprintf(buf, sizeof buf, 2691da177e4SLinus Torvalds "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n", 2701da177e4SLinus Torvalds "retransmit", 2711da177e4SLinus Torvalds d->aoemajor, d->aoeminor, f->tag, jiffies, n); 2721da177e4SLinus Torvalds aoechr_error(buf); 2731da177e4SLinus Torvalds 274e407a7f6SEd L. Cashin skb = f->skb; 275e407a7f6SEd L. Cashin h = (struct aoe_hdr *) skb->mac.raw; 27619bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 2771da177e4SLinus Torvalds f->tag = n; 27863e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 2792dd5e422SEd L. Cashin memcpy(h->dst, d->addr, sizeof h->dst); 2802dd5e422SEd L. Cashin memcpy(h->src, d->ifp->dev_addr, sizeof h->src); 2811da177e4SLinus Torvalds 28219bf2635SEd L. Cashin n = DEFAULTBCNT / 512; 28319bf2635SEd L. Cashin if (ah->scnt > n) { 28419bf2635SEd L. Cashin ah->scnt = n; 28519bf2635SEd L. Cashin if (ah->aflags & AOEAFL_WRITE) 28619bf2635SEd L. Cashin skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr), 28719bf2635SEd L. Cashin offset_in_page(f->bufaddr), DEFAULTBCNT); 28819bf2635SEd L. Cashin if (++d->lostjumbo > (d->nframes << 1)) 28919bf2635SEd L. Cashin if (d->maxbcnt != DEFAULTBCNT) { 2906bb6285fSEd L. Cashin iprintk("too many lost jumbo - using 1KB frames.\n"); 29119bf2635SEd L. Cashin d->maxbcnt = DEFAULTBCNT; 29219bf2635SEd L. Cashin d->flags |= DEVFL_MAXBCNT; 29319bf2635SEd L. Cashin } 29419bf2635SEd L. Cashin } 29519bf2635SEd L. Cashin 296e407a7f6SEd L. Cashin skb->dev = d->ifp; 297e407a7f6SEd L. Cashin skb_get(skb); 298a4b38364Secashin@coraid.com skb->next = NULL; 299a4b38364Secashin@coraid.com if (d->sendq_hd) 300a4b38364Secashin@coraid.com d->sendq_tl->next = skb; 301a4b38364Secashin@coraid.com else 302a4b38364Secashin@coraid.com d->sendq_hd = skb; 303a4b38364Secashin@coraid.com d->sendq_tl = skb; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds static int 3071da177e4SLinus Torvalds tsince(int tag) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds int n; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds n = jiffies & 0xffff; 3121da177e4SLinus Torvalds n -= tag & 0xffff; 3131da177e4SLinus Torvalds if (n < 0) 3141da177e4SLinus Torvalds n += 1<<16; 3151da177e4SLinus Torvalds return n; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static void 3191da177e4SLinus Torvalds rexmit_timer(ulong vp) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct aoedev *d; 3221da177e4SLinus Torvalds struct frame *f, *e; 3231da177e4SLinus Torvalds struct sk_buff *sl; 3241da177e4SLinus Torvalds register long timeout; 3251da177e4SLinus Torvalds ulong flags, n; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds d = (struct aoedev *) vp; 3281da177e4SLinus Torvalds sl = NULL; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* timeout is always ~150% of the moving average */ 3311da177e4SLinus Torvalds timeout = d->rttavg; 3321da177e4SLinus Torvalds timeout += timeout >> 1; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 3371c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 3381da177e4SLinus Torvalds return; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds f = d->frames; 3411da177e4SLinus Torvalds e = f + d->nframes; 3421da177e4SLinus Torvalds for (; f<e; f++) { 3431da177e4SLinus Torvalds if (f->tag != FREETAG && tsince(f->tag) >= timeout) { 3441da177e4SLinus Torvalds n = f->waited += timeout; 3451da177e4SLinus Torvalds n /= HZ; 3461da177e4SLinus Torvalds if (n > MAXWAIT) { /* waited too long. device failure. */ 3471da177e4SLinus Torvalds aoedev_downdev(d); 3481c6f3fcaSEd L. Cashin break; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds rexmit(d, f); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 354a4b38364Secashin@coraid.com sl = d->sendq_hd; 355a4b38364Secashin@coraid.com d->sendq_hd = d->sendq_tl = NULL; 3561da177e4SLinus Torvalds if (sl) { 3571da177e4SLinus Torvalds n = d->rttavg <<= 1; 3581da177e4SLinus Torvalds if (n > MAXTIMER) 3591da177e4SLinus Torvalds d->rttavg = MAXTIMER; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 3631da177e4SLinus Torvalds add_timer(&d->timer); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds aoenet_xmit(sl); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3703ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 3713ae1c24eSEd L. Cashin */ 3723ae1c24eSEd L. Cashin void 3733ae1c24eSEd L. Cashin aoecmd_sleepwork(void *vp) 3743ae1c24eSEd L. Cashin { 3753ae1c24eSEd L. Cashin struct aoedev *d = (struct aoedev *) vp; 3763ae1c24eSEd L. Cashin 3773ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 3783ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 3793ae1c24eSEd L. Cashin 3803ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 3813ae1c24eSEd L. Cashin struct block_device *bd; 3823ae1c24eSEd L. Cashin unsigned long flags; 3833ae1c24eSEd L. Cashin u64 ssize; 3843ae1c24eSEd L. Cashin 3853ae1c24eSEd L. Cashin ssize = d->gd->capacity; 3863ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 3873ae1c24eSEd L. Cashin 3883ae1c24eSEd L. Cashin if (bd) { 3893ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 3903ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 3913ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 3923ae1c24eSEd L. Cashin bdput(bd); 3933ae1c24eSEd L. Cashin } 3943ae1c24eSEd L. Cashin spin_lock_irqsave(&d->lock, flags); 3953ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 3963ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 3973ae1c24eSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 3983ae1c24eSEd L. Cashin } 3993ae1c24eSEd L. Cashin } 4003ae1c24eSEd L. Cashin 4011da177e4SLinus Torvalds static void 4021da177e4SLinus Torvalds ataid_complete(struct aoedev *d, unsigned char *id) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds u64 ssize; 4051da177e4SLinus Torvalds u16 n; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* word 83: command set supported */ 408475172fbSEd L. Cashin n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1])); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 411475172fbSEd L. Cashin n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1])); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 4141da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 417475172fbSEd L. Cashin ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1])); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 4201da177e4SLinus Torvalds d->geo.cylinders = ssize; 4211da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 4221da177e4SLinus Torvalds d->geo.heads = 255; 4231da177e4SLinus Torvalds d->geo.sectors = 63; 4241da177e4SLinus Torvalds } else { 4251da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* number lba28 sectors */ 428475172fbSEd L. Cashin ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1])); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 431475172fbSEd L. Cashin d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1])); 432475172fbSEd L. Cashin d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1])); 433475172fbSEd L. Cashin d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1])); 4341da177e4SLinus Torvalds } 4353ae1c24eSEd L. Cashin 4363ae1c24eSEd L. Cashin if (d->ssize != ssize) 4376bb6285fSEd L. Cashin iprintk("%012llx e%lu.%lu v%04x has %llu sectors\n", 4386bb6285fSEd L. Cashin (unsigned long long)mac_addr(d->addr), 4393ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 4403ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 4411da177e4SLinus Torvalds d->ssize = ssize; 4421da177e4SLinus Torvalds d->geo.start = 0; 4431da177e4SLinus Torvalds if (d->gd != NULL) { 4441da177e4SLinus Torvalds d->gd->capacity = ssize; 4453ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 4463ae1c24eSEd L. Cashin } else { 4473ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) { 4486bb6285fSEd L. Cashin eprintk("can't schedule work for e%lu.%lu, %s\n", 4493ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 4506bb6285fSEd L. Cashin "it's already on! This shouldn't happen.\n"); 4511da177e4SLinus Torvalds return; 4521da177e4SLinus Torvalds } 4533ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds schedule_work(&d->work); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static void 4591da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds register long n; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds n = rtt; 4641da177e4SLinus Torvalds if (n < MINTIMER) 4651da177e4SLinus Torvalds n = MINTIMER; 4661da177e4SLinus Torvalds else if (n > MAXTIMER) 4671da177e4SLinus Torvalds n = MAXTIMER; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */ 4701da177e4SLinus Torvalds n -= d->rttavg; 4711da177e4SLinus Torvalds d->rttavg += n >> 2; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds void 4751da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct aoedev *d; 478*ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 4791da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 4801da177e4SLinus Torvalds struct frame *f; 4811da177e4SLinus Torvalds struct buf *buf; 4821da177e4SLinus Torvalds struct sk_buff *sl; 4831da177e4SLinus Torvalds register long n; 4841da177e4SLinus Torvalds ulong flags; 4851da177e4SLinus Torvalds char ebuf[128]; 48632465c65Secashin@coraid.com u16 aoemajor; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds hin = (struct aoe_hdr *) skb->mac.raw; 48963e9cc5dSecashin@coraid.com aoemajor = be16_to_cpu(hin->major); 49032465c65Secashin@coraid.com d = aoedev_by_aoeaddr(aoemajor, hin->minor); 4911da177e4SLinus Torvalds if (d == NULL) { 4921da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 4931da177e4SLinus Torvalds "for unknown device %d.%d\n", 49432465c65Secashin@coraid.com aoemajor, hin->minor); 4951da177e4SLinus Torvalds aoechr_error(ebuf); 4961da177e4SLinus Torvalds return; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 5001da177e4SLinus Torvalds 50163e9cc5dSecashin@coraid.com f = getframe(d, be32_to_cpu(hin->tag)); 5021da177e4SLinus Torvalds if (f == NULL) { 5031da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5041da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, 5051da177e4SLinus Torvalds "%15s e%d.%d tag=%08x@%08lx\n", 5061da177e4SLinus Torvalds "unexpected rsp", 50763e9cc5dSecashin@coraid.com be16_to_cpu(hin->major), 5081da177e4SLinus Torvalds hin->minor, 50963e9cc5dSecashin@coraid.com be32_to_cpu(hin->tag), 5101da177e4SLinus Torvalds jiffies); 5111da177e4SLinus Torvalds aoechr_error(ebuf); 5121da177e4SLinus Torvalds return; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds calc_rttavg(d, tsince(f->tag)); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds ahin = (struct aoe_atahdr *) (hin+1); 518*ddec63e8SEd L. Cashin hout = (struct aoe_hdr *) f->skb->mac.raw; 519*ddec63e8SEd L. Cashin ahout = (struct aoe_atahdr *) (hout+1); 5201da177e4SLinus Torvalds buf = f->buf; 5211da177e4SLinus Torvalds 5229d41965bSEd L. Cashin if (ahout->cmdstat == WIN_IDENTIFY) 5239d41965bSEd L. Cashin d->flags &= ~DEVFL_PAUSE; 5241da177e4SLinus Torvalds if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 5256bb6285fSEd L. Cashin eprintk("ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n", 5261da177e4SLinus Torvalds ahout->cmdstat, ahin->cmdstat, 5271da177e4SLinus Torvalds d->aoemajor, d->aoeminor); 5281da177e4SLinus Torvalds if (buf) 5291da177e4SLinus Torvalds buf->flags |= BUFFL_FAIL; 5301da177e4SLinus Torvalds } else { 53119bf2635SEd L. Cashin n = ahout->scnt << 9; 5321da177e4SLinus Torvalds switch (ahout->cmdstat) { 5331da177e4SLinus Torvalds case WIN_READ: 5341da177e4SLinus Torvalds case WIN_READ_EXT: 5351da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < n) { 5366bb6285fSEd L. Cashin eprintk("runt data size in read. skb->len=%d\n", 5371da177e4SLinus Torvalds skb->len); 5381da177e4SLinus Torvalds /* fail frame f? just returning will rexmit. */ 5391da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5401da177e4SLinus Torvalds return; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds memcpy(f->bufaddr, ahin+1, n); 5431da177e4SLinus Torvalds case WIN_WRITE: 5441da177e4SLinus Torvalds case WIN_WRITE_EXT: 54519bf2635SEd L. Cashin if (f->bcnt -= n) { 54619bf2635SEd L. Cashin f->bufaddr += n; 54719bf2635SEd L. Cashin put_lba(ahout, f->lba += ahout->scnt); 5486bb6285fSEd L. Cashin n = f->bcnt; 5496bb6285fSEd L. Cashin if (n > DEFAULTBCNT) 5506bb6285fSEd L. Cashin n = DEFAULTBCNT; 55119bf2635SEd L. Cashin ahout->scnt = n >> 9; 55219bf2635SEd L. Cashin if (ahout->aflags & AOEAFL_WRITE) 5536bb6285fSEd L. Cashin skb_fill_page_desc(f->skb, 0, 5546bb6285fSEd L. Cashin virt_to_page(f->bufaddr), 55519bf2635SEd L. Cashin offset_in_page(f->bufaddr), n); 556*ddec63e8SEd L. Cashin f->tag = newtag(d); 557*ddec63e8SEd L. Cashin hout->tag = cpu_to_be32(f->tag); 558*ddec63e8SEd L. Cashin skb->dev = d->ifp; 55919bf2635SEd L. Cashin skb_get(f->skb); 56019bf2635SEd L. Cashin f->skb->next = NULL; 56119bf2635SEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 56219bf2635SEd L. Cashin aoenet_xmit(f->skb); 56319bf2635SEd L. Cashin return; 56419bf2635SEd L. Cashin } 56519bf2635SEd L. Cashin if (n > DEFAULTBCNT) 56619bf2635SEd L. Cashin d->lostjumbo = 0; 5671da177e4SLinus Torvalds break; 5681da177e4SLinus Torvalds case WIN_IDENTIFY: 5691da177e4SLinus Torvalds if (skb->len - sizeof *hin - sizeof *ahin < 512) { 5706bb6285fSEd L. Cashin iprintk("runt data size in ataid. skb->len=%d\n", 5716bb6285fSEd L. Cashin skb->len); 5721da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 5731da177e4SLinus Torvalds return; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds ataid_complete(d, (char *) (ahin+1)); 5761da177e4SLinus Torvalds break; 5771da177e4SLinus Torvalds default: 5786bb6285fSEd L. Cashin iprintk("unrecognized ata command %2.2Xh for %d.%d\n", 5791da177e4SLinus Torvalds ahout->cmdstat, 58063e9cc5dSecashin@coraid.com be16_to_cpu(hin->major), 5811da177e4SLinus Torvalds hin->minor); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (buf) { 5861da177e4SLinus Torvalds buf->nframesout -= 1; 5871da177e4SLinus Torvalds if (buf->nframesout == 0 && buf->resid == 0) { 5880c6f0e79Secashin@coraid.com unsigned long duration = jiffies - buf->start_time; 5890c6f0e79Secashin@coraid.com unsigned long n_sect = buf->bio->bi_size >> 9; 5900c6f0e79Secashin@coraid.com struct gendisk *disk = d->gd; 591496456c2SJens Axboe const int rw = bio_data_dir(buf->bio); 5920c6f0e79Secashin@coraid.com 593496456c2SJens Axboe disk_stat_inc(disk, ios[rw]); 594496456c2SJens Axboe disk_stat_add(disk, ticks[rw], duration); 595496456c2SJens Axboe disk_stat_add(disk, sectors[rw], n_sect); 5960c6f0e79Secashin@coraid.com disk_stat_add(disk, io_ticks, duration); 5971da177e4SLinus Torvalds n = (buf->flags & BUFFL_FAIL) ? -EIO : 0; 5981da177e4SLinus Torvalds bio_endio(buf->bio, buf->bio->bi_size, n); 5991da177e4SLinus Torvalds mempool_free(buf, d->bufpool); 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds f->buf = NULL; 6041da177e4SLinus Torvalds f->tag = FREETAG; 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds aoecmd_work(d); 607a4b38364Secashin@coraid.com sl = d->sendq_hd; 608a4b38364Secashin@coraid.com d->sendq_hd = d->sendq_tl = NULL; 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 6111da177e4SLinus Torvalds aoenet_xmit(sl); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds void 6151da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 6161da177e4SLinus Torvalds { 6173ae1c24eSEd L. Cashin struct sk_buff *sl; 6181da177e4SLinus Torvalds 6193ae1c24eSEd L. Cashin sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds aoenet_xmit(sl); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * Since we only call this in one place (and it only prepares one frame) 626a4b38364Secashin@coraid.com * we just return the skb. Usually we'd chain it up to the aoedev sendq. 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds static struct sk_buff * 6291da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds struct aoe_hdr *h; 6321da177e4SLinus Torvalds struct aoe_atahdr *ah; 6331da177e4SLinus Torvalds struct frame *f; 6341da177e4SLinus Torvalds struct sk_buff *skb; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds f = getframe(d, FREETAG); 6371da177e4SLinus Torvalds if (f == NULL) { 6386bb6285fSEd L. Cashin eprintk("can't get a frame. This shouldn't happen.\n"); 6391da177e4SLinus Torvalds return NULL; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* initialize the headers & frame */ 643e407a7f6SEd L. Cashin skb = f->skb; 644e407a7f6SEd L. Cashin h = (struct aoe_hdr *) skb->mac.raw; 6451da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 646e407a7f6SEd L. Cashin skb->len = sizeof *h + sizeof *ah; 647e407a7f6SEd L. Cashin memset(h, 0, skb->len); 6481da177e4SLinus Torvalds f->tag = aoehdr_atainit(d, h); 6491da177e4SLinus Torvalds f->waited = 0; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* set up ata header */ 6521da177e4SLinus Torvalds ah->scnt = 1; 6531da177e4SLinus Torvalds ah->cmdstat = WIN_IDENTIFY; 6541da177e4SLinus Torvalds ah->lba3 = 0xa0; 6551da177e4SLinus Torvalds 656e407a7f6SEd L. Cashin skb->dev = d->ifp; 657e407a7f6SEd L. Cashin skb_get(skb); 6581da177e4SLinus Torvalds 6593ae1c24eSEd L. Cashin d->rttavg = MAXTIMER; 6601da177e4SLinus Torvalds d->timer.function = rexmit_timer; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds return skb; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds void 6661da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds struct aoedev *d; 6691da177e4SLinus Torvalds struct aoe_hdr *h; 6701da177e4SLinus Torvalds struct aoe_cfghdr *ch; 67163e9cc5dSecashin@coraid.com ulong flags, sysminor, aoemajor; 6721da177e4SLinus Torvalds struct sk_buff *sl; 673eaf0a3cbSEd L. Cashin enum { MAXFRAMES = 16 }; 67419bf2635SEd L. Cashin u16 n; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds h = (struct aoe_hdr *) skb->mac.raw; 6771da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /* 6801da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 6811da177e4SLinus Torvalds * warrant a loud message for this special case. 6821da177e4SLinus Torvalds */ 68363e9cc5dSecashin@coraid.com aoemajor = be16_to_cpu(h->major); 6841da177e4SLinus Torvalds if (aoemajor == 0xfff) { 6856bb6285fSEd L. Cashin eprintk("Warning: shelf address is all ones. " 6866bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 6871da177e4SLinus Torvalds return; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds sysminor = SYSMINOR(aoemajor, h->minor); 691fc458dcdSecashin@coraid.com if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) { 6926bb6285fSEd L. Cashin iprintk("e%ld.%d: minor number too large\n", 693fc458dcdSecashin@coraid.com aoemajor, (int) h->minor); 6941da177e4SLinus Torvalds return; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 69719bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 69819bf2635SEd L. Cashin if (n > MAXFRAMES) /* keep it reasonable */ 69919bf2635SEd L. Cashin n = MAXFRAMES; 7001da177e4SLinus Torvalds 70119bf2635SEd L. Cashin d = aoedev_by_sysminor_m(sysminor, n); 7021da177e4SLinus Torvalds if (d == NULL) { 7036bb6285fSEd L. Cashin iprintk("device sysminor_m failure\n"); 7041da177e4SLinus Torvalds return; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 7081da177e4SLinus Torvalds 7093ae1c24eSEd L. Cashin /* permit device to migrate mac and network interface */ 7103ae1c24eSEd L. Cashin d->ifp = skb->dev; 7113ae1c24eSEd L. Cashin memcpy(d->addr, h->src, sizeof d->addr); 71219bf2635SEd L. Cashin if (!(d->flags & DEVFL_MAXBCNT)) { 71319bf2635SEd L. Cashin n = d->ifp->mtu; 71419bf2635SEd L. Cashin n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr); 71519bf2635SEd L. Cashin n /= 512; 71619bf2635SEd L. Cashin if (n > ch->scnt) 71719bf2635SEd L. Cashin n = ch->scnt; 71819bf2635SEd L. Cashin d->maxbcnt = n ? n * 512 : DEFAULTBCNT; 71919bf2635SEd L. Cashin } 7203ae1c24eSEd L. Cashin 7213ae1c24eSEd L. Cashin /* don't change users' perspective */ 7223ae1c24eSEd L. Cashin if (d->nopen && !(d->flags & DEVFL_PAUSE)) { 7231da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7241da177e4SLinus Torvalds return; 7251da177e4SLinus Torvalds } 7263ae1c24eSEd L. Cashin d->flags |= DEVFL_PAUSE; /* force pause */ 72763e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 7281da177e4SLinus Torvalds 7293ae1c24eSEd L. Cashin /* check for already outstanding ataid */ 7303ae1c24eSEd L. Cashin sl = aoedev_isbusy(d) == 0 ? aoecmd_ata_id(d) : NULL; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds aoenet_xmit(sl); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 737