1fea05a26SEd Cashin /* Copyright (c) 2012 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> 85a0e3ad6STejun 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> 15896831f5SEd Cashin #include <linux/workqueue.h> 16896831f5SEd Cashin #include <linux/kthread.h> 17881d966bSEric W. Biederman #include <net/net_namespace.h> 18475172fbSEd L. Cashin #include <asm/unaligned.h> 19896831f5SEd Cashin #include <linux/uio.h> 201da177e4SLinus Torvalds #include "aoe.h" 211da177e4SLinus Torvalds 22896831f5SEd Cashin #define MAXIOC (8192) /* default meant to avoid most soft lockups */ 23896831f5SEd Cashin 24896831f5SEd Cashin static void ktcomplete(struct frame *, struct sk_buff *); 25896831f5SEd Cashin 2669cf2d85SEd Cashin static struct buf *nextbuf(struct aoedev *); 2769cf2d85SEd Cashin 28b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3; 29b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644); 30b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev."); 311da177e4SLinus Torvalds 327df620d8SEd L. Cashin static int aoe_maxout = 16; 337df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644); 347df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout, 357df620d8SEd L. Cashin "Only aoe_maxout outstanding packets for every MAC on eX.Y."); 367df620d8SEd L. Cashin 37896831f5SEd Cashin static wait_queue_head_t ktiowq; 38896831f5SEd Cashin static struct ktstate kts; 39896831f5SEd Cashin 40896831f5SEd Cashin /* io completion queue */ 41896831f5SEd Cashin static struct { 42896831f5SEd Cashin struct list_head head; 43896831f5SEd Cashin spinlock_t lock; 44896831f5SEd Cashin } iocq; 45896831f5SEd Cashin 4668e0d42fSEd L. Cashin static struct sk_buff * 47e407a7f6SEd L. Cashin new_skb(ulong len) 481da177e4SLinus Torvalds { 491da177e4SLinus Torvalds struct sk_buff *skb; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds skb = alloc_skb(len, GFP_ATOMIC); 521da177e4SLinus Torvalds if (skb) { 53459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 54c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 551da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 568babe8ccSEd Cashin skb_checksum_none_assert(skb); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds return skb; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static struct frame * 623a0c40d2SEd Cashin getframe_deferred(struct aoedev *d, u32 tag) 633a0c40d2SEd Cashin { 643a0c40d2SEd Cashin struct list_head *head, *pos, *nx; 653a0c40d2SEd Cashin struct frame *f; 663a0c40d2SEd Cashin 673a0c40d2SEd Cashin head = &d->rexmitq; 683a0c40d2SEd Cashin list_for_each_safe(pos, nx, head) { 693a0c40d2SEd Cashin f = list_entry(pos, struct frame, head); 703a0c40d2SEd Cashin if (f->tag == tag) { 713a0c40d2SEd Cashin list_del(pos); 723a0c40d2SEd Cashin return f; 733a0c40d2SEd Cashin } 743a0c40d2SEd Cashin } 753a0c40d2SEd Cashin return NULL; 763a0c40d2SEd Cashin } 773a0c40d2SEd Cashin 783a0c40d2SEd Cashin static struct frame * 7964a80f5aSEd Cashin getframe(struct aoedev *d, u32 tag) 801da177e4SLinus Torvalds { 81896831f5SEd Cashin struct frame *f; 82896831f5SEd Cashin struct list_head *head, *pos, *nx; 83896831f5SEd Cashin u32 n; 841da177e4SLinus Torvalds 85896831f5SEd Cashin n = tag % NFACTIVE; 8664a80f5aSEd Cashin head = &d->factive[n]; 87896831f5SEd Cashin list_for_each_safe(pos, nx, head) { 88896831f5SEd Cashin f = list_entry(pos, struct frame, head); 89896831f5SEd Cashin if (f->tag == tag) { 90896831f5SEd Cashin list_del(pos); 911da177e4SLinus Torvalds return f; 92896831f5SEd Cashin } 93896831f5SEd Cashin } 941da177e4SLinus Torvalds return NULL; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * Leave the top bit clear so we have tagspace for userland. 991da177e4SLinus Torvalds * The bottom 16 bits are the xmit tick for rexmit/rttavg processing. 1001da177e4SLinus Torvalds * This driver reserves tag -1 to mean "unused frame." 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds static int 10364a80f5aSEd Cashin newtag(struct aoedev *d) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds register ulong n; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds n = jiffies & 0xffff; 10864a80f5aSEd Cashin return n |= (++d->lasttag & 0x7fff) << 16; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 111896831f5SEd Cashin static u32 11268e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) 1131da177e4SLinus Torvalds { 11464a80f5aSEd Cashin u32 host_tag = newtag(d); 1151da177e4SLinus Torvalds 11668e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 11768e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 11863e9cc5dSecashin@coraid.com h->type = __constant_cpu_to_be16(ETH_P_AOE); 1191da177e4SLinus Torvalds h->verfl = AOE_HVER; 12063e9cc5dSecashin@coraid.com h->major = cpu_to_be16(d->aoemajor); 1211da177e4SLinus Torvalds h->minor = d->aoeminor; 1221da177e4SLinus Torvalds h->cmd = AOECMD_ATA; 12363e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(host_tag); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds return host_tag; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 12819bf2635SEd L. Cashin static inline void 12919bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba) 13019bf2635SEd L. Cashin { 13119bf2635SEd L. Cashin ah->lba0 = lba; 13219bf2635SEd L. Cashin ah->lba1 = lba >>= 8; 13319bf2635SEd L. Cashin ah->lba2 = lba >>= 8; 13419bf2635SEd L. Cashin ah->lba3 = lba >>= 8; 13519bf2635SEd L. Cashin ah->lba4 = lba >>= 8; 13619bf2635SEd L. Cashin ah->lba5 = lba >>= 8; 13719bf2635SEd L. Cashin } 13819bf2635SEd L. Cashin 1393f0f0133SEd Cashin static struct aoeif * 14068e0d42fSEd L. Cashin ifrotate(struct aoetgt *t) 1411da177e4SLinus Torvalds { 1423f0f0133SEd Cashin struct aoeif *ifp; 1433f0f0133SEd Cashin 1443f0f0133SEd Cashin ifp = t->ifp; 1453f0f0133SEd Cashin ifp++; 1463f0f0133SEd Cashin if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL) 1473f0f0133SEd Cashin ifp = t->ifs; 1483f0f0133SEd Cashin if (ifp->nd == NULL) 1493f0f0133SEd Cashin return NULL; 1503f0f0133SEd Cashin return t->ifp = ifp; 15168e0d42fSEd L. Cashin } 15268e0d42fSEd L. Cashin 1539bb237b6SEd L. Cashin static void 1549bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 1559bb237b6SEd L. Cashin { 156e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->skbpool, skb); 1579bb237b6SEd L. Cashin } 1589bb237b6SEd L. Cashin 1599bb237b6SEd L. Cashin static struct sk_buff * 1609bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 1619bb237b6SEd L. Cashin { 162e9bb8fb0SDavid S. Miller struct sk_buff *skb = skb_peek(&d->skbpool); 1639bb237b6SEd L. Cashin 1649bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 165e9bb8fb0SDavid S. Miller __skb_unlink(skb, &d->skbpool); 1669bb237b6SEd L. Cashin return skb; 1679bb237b6SEd L. Cashin } 168e9bb8fb0SDavid S. Miller if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 169e9bb8fb0SDavid S. Miller (skb = new_skb(ETH_ZLEN))) 1709bb237b6SEd L. Cashin return skb; 171e9bb8fb0SDavid S. Miller 1729bb237b6SEd L. Cashin return NULL; 1739bb237b6SEd L. Cashin } 1749bb237b6SEd L. Cashin 175896831f5SEd Cashin void 176896831f5SEd Cashin aoe_freetframe(struct frame *f) 17768e0d42fSEd L. Cashin { 178896831f5SEd Cashin struct aoetgt *t; 179896831f5SEd Cashin 180896831f5SEd Cashin t = f->t; 181896831f5SEd Cashin f->buf = NULL; 182896831f5SEd Cashin f->bv = NULL; 183896831f5SEd Cashin f->r_skb = NULL; 184896831f5SEd Cashin list_add(&f->head, &t->ffree); 185896831f5SEd Cashin } 186896831f5SEd Cashin 187896831f5SEd Cashin static struct frame * 188896831f5SEd Cashin newtframe(struct aoedev *d, struct aoetgt *t) 189896831f5SEd Cashin { 190896831f5SEd Cashin struct frame *f; 1919bb237b6SEd L. Cashin struct sk_buff *skb; 192896831f5SEd Cashin struct list_head *pos; 193896831f5SEd Cashin 194896831f5SEd Cashin if (list_empty(&t->ffree)) { 195896831f5SEd Cashin if (t->falloc >= NSKBPOOLMAX*2) 196896831f5SEd Cashin return NULL; 197896831f5SEd Cashin f = kcalloc(1, sizeof(*f), GFP_ATOMIC); 198896831f5SEd Cashin if (f == NULL) 199896831f5SEd Cashin return NULL; 200896831f5SEd Cashin t->falloc++; 201896831f5SEd Cashin f->t = t; 202896831f5SEd Cashin } else { 203896831f5SEd Cashin pos = t->ffree.next; 204896831f5SEd Cashin list_del(pos); 205896831f5SEd Cashin f = list_entry(pos, struct frame, head); 206896831f5SEd Cashin } 207896831f5SEd Cashin 208896831f5SEd Cashin skb = f->skb; 209896831f5SEd Cashin if (skb == NULL) { 210896831f5SEd Cashin f->skb = skb = new_skb(ETH_ZLEN); 211896831f5SEd Cashin if (!skb) { 212896831f5SEd Cashin bail: aoe_freetframe(f); 213896831f5SEd Cashin return NULL; 214896831f5SEd Cashin } 215896831f5SEd Cashin } 216896831f5SEd Cashin 217896831f5SEd Cashin if (atomic_read(&skb_shinfo(skb)->dataref) != 1) { 218896831f5SEd Cashin skb = skb_pool_get(d); 219896831f5SEd Cashin if (skb == NULL) 220896831f5SEd Cashin goto bail; 221896831f5SEd Cashin skb_pool_put(d, f->skb); 222896831f5SEd Cashin f->skb = skb; 223896831f5SEd Cashin } 224896831f5SEd Cashin 225896831f5SEd Cashin skb->truesize -= skb->data_len; 226896831f5SEd Cashin skb_shinfo(skb)->nr_frags = skb->data_len = 0; 227896831f5SEd Cashin skb_trim(skb, 0); 228896831f5SEd Cashin return f; 229896831f5SEd Cashin } 230896831f5SEd Cashin 231896831f5SEd Cashin static struct frame * 232896831f5SEd Cashin newframe(struct aoedev *d) 233896831f5SEd Cashin { 234896831f5SEd Cashin struct frame *f; 235896831f5SEd Cashin struct aoetgt *t, **tt; 236896831f5SEd Cashin int totout = 0; 23768e0d42fSEd L. Cashin 23868e0d42fSEd L. Cashin if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */ 23968e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 24068e0d42fSEd L. Cashin return NULL; 24168e0d42fSEd L. Cashin } 242896831f5SEd Cashin tt = d->tgt; /* last used target */ 2439bb237b6SEd L. Cashin for (;;) { 244896831f5SEd Cashin tt++; 245896831f5SEd Cashin if (tt >= &d->targets[NTARGETS] || !*tt) 246896831f5SEd Cashin tt = d->targets; 247896831f5SEd Cashin t = *tt; 248896831f5SEd Cashin totout += t->nout; 249896831f5SEd Cashin if (t->nout < t->maxout 2509bb237b6SEd L. Cashin && t != d->htgt 251896831f5SEd Cashin && t->ifp->nd) { 252896831f5SEd Cashin f = newtframe(d, t); 253896831f5SEd Cashin if (f) { 254896831f5SEd Cashin ifrotate(t); 2553f0f0133SEd Cashin d->tgt = tt; 25668e0d42fSEd L. Cashin return f; 25768e0d42fSEd L. Cashin } 2589bb237b6SEd L. Cashin } 259896831f5SEd Cashin if (tt == d->tgt) /* we've looped and found nada */ 2609bb237b6SEd L. Cashin break; 261896831f5SEd Cashin } 262896831f5SEd Cashin if (totout == 0) { 263896831f5SEd Cashin d->kicked++; 264896831f5SEd Cashin d->flags |= DEVFL_KICKME; 2659bb237b6SEd L. Cashin } 26668e0d42fSEd L. Cashin return NULL; 26768e0d42fSEd L. Cashin } 26868e0d42fSEd L. Cashin 2693d5b0605SEd Cashin static void 2703d5b0605SEd Cashin skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt) 2713d5b0605SEd Cashin { 2723d5b0605SEd Cashin int frag = 0; 2733d5b0605SEd Cashin ulong fcnt; 2743d5b0605SEd Cashin loop: 2753d5b0605SEd Cashin fcnt = bv->bv_len - (off - bv->bv_offset); 2763d5b0605SEd Cashin if (fcnt > cnt) 2773d5b0605SEd Cashin fcnt = cnt; 2783d5b0605SEd Cashin skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt); 2793d5b0605SEd Cashin cnt -= fcnt; 2803d5b0605SEd Cashin if (cnt <= 0) 2813d5b0605SEd Cashin return; 2823d5b0605SEd Cashin bv++; 2833d5b0605SEd Cashin off = bv->bv_offset; 2843d5b0605SEd Cashin goto loop; 2853d5b0605SEd Cashin } 2863d5b0605SEd Cashin 287896831f5SEd Cashin static void 288896831f5SEd Cashin fhash(struct frame *f) 289896831f5SEd Cashin { 29064a80f5aSEd Cashin struct aoedev *d = f->t->d; 291896831f5SEd Cashin u32 n; 292896831f5SEd Cashin 293896831f5SEd Cashin n = f->tag % NFACTIVE; 29464a80f5aSEd Cashin list_add_tail(&f->head, &d->factive[n]); 295896831f5SEd Cashin } 296896831f5SEd Cashin 29768e0d42fSEd L. Cashin static int 29868e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 29968e0d42fSEd L. Cashin { 30068e0d42fSEd L. Cashin struct frame *f; 3011da177e4SLinus Torvalds struct aoe_hdr *h; 3021da177e4SLinus Torvalds struct aoe_atahdr *ah; 3031da177e4SLinus Torvalds struct buf *buf; 30468e0d42fSEd L. Cashin struct aoetgt *t; 3051da177e4SLinus Torvalds struct sk_buff *skb; 30669cf2d85SEd Cashin struct sk_buff_head queue; 3073d5b0605SEd Cashin ulong bcnt, fbcnt; 3081da177e4SLinus Torvalds char writebit, extbit; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds writebit = 0x10; 3111da177e4SLinus Torvalds extbit = 0x4; 3121da177e4SLinus Torvalds 31369cf2d85SEd Cashin buf = nextbuf(d); 31469cf2d85SEd Cashin if (buf == NULL) 31569cf2d85SEd Cashin return 0; 316896831f5SEd Cashin f = newframe(d); 31768e0d42fSEd L. Cashin if (f == NULL) 31868e0d42fSEd L. Cashin return 0; 31968e0d42fSEd L. Cashin t = *d->tgt; 3203f0f0133SEd Cashin bcnt = d->maxbcnt; 32168e0d42fSEd L. Cashin if (bcnt == 0) 32268e0d42fSEd L. Cashin bcnt = DEFAULTBCNT; 3233d5b0605SEd Cashin if (bcnt > buf->resid) 3243d5b0605SEd Cashin bcnt = buf->resid; 3253d5b0605SEd Cashin fbcnt = bcnt; 3263d5b0605SEd Cashin f->bv = buf->bv; 3273d5b0605SEd Cashin f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid); 3283d5b0605SEd Cashin do { 3293d5b0605SEd Cashin if (fbcnt < buf->bv_resid) { 3303d5b0605SEd Cashin buf->bv_resid -= fbcnt; 3313d5b0605SEd Cashin buf->resid -= fbcnt; 3323d5b0605SEd Cashin break; 3333d5b0605SEd Cashin } 3343d5b0605SEd Cashin fbcnt -= buf->bv_resid; 3353d5b0605SEd Cashin buf->resid -= buf->bv_resid; 3363d5b0605SEd Cashin if (buf->resid == 0) { 33769cf2d85SEd Cashin d->ip.buf = NULL; 3383d5b0605SEd Cashin break; 3393d5b0605SEd Cashin } 3403d5b0605SEd Cashin buf->bv++; 3413d5b0605SEd Cashin buf->bv_resid = buf->bv->bv_len; 3423d5b0605SEd Cashin WARN_ON(buf->bv_resid == 0); 3433d5b0605SEd Cashin } while (fbcnt); 3443d5b0605SEd Cashin 3451da177e4SLinus Torvalds /* initialize the headers & frame */ 346e407a7f6SEd L. Cashin skb = f->skb; 347abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 3481da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 34919900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 35019900cdeSEd L. Cashin memset(h, 0, skb->len); 35168e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 352896831f5SEd Cashin fhash(f); 35368e0d42fSEd L. Cashin t->nout++; 3541da177e4SLinus Torvalds f->waited = 0; 3551da177e4SLinus Torvalds f->buf = buf; 35619bf2635SEd L. Cashin f->bcnt = bcnt; 35768e0d42fSEd L. Cashin f->lba = buf->sector; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* set up ata header */ 3601da177e4SLinus Torvalds ah->scnt = bcnt >> 9; 36168e0d42fSEd L. Cashin put_lba(ah, buf->sector); 3621da177e4SLinus Torvalds if (d->flags & DEVFL_EXT) { 3631da177e4SLinus Torvalds ah->aflags |= AOEAFL_EXT; 3641da177e4SLinus Torvalds } else { 3651da177e4SLinus Torvalds extbit = 0; 3661da177e4SLinus Torvalds ah->lba3 &= 0x0f; 3671da177e4SLinus Torvalds ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds if (bio_data_dir(buf->bio) == WRITE) { 3703d5b0605SEd Cashin skb_fillup(skb, f->bv, f->bv_off, bcnt); 3711da177e4SLinus Torvalds ah->aflags |= AOEAFL_WRITE; 3724f51dc5eSEd L. Cashin skb->len += bcnt; 3734f51dc5eSEd L. Cashin skb->data_len = bcnt; 3743d5b0605SEd Cashin skb->truesize += bcnt; 37568e0d42fSEd L. Cashin t->wpkts++; 3761da177e4SLinus Torvalds } else { 37768e0d42fSEd L. Cashin t->rpkts++; 3781da177e4SLinus Torvalds writebit = 0; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 38104b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* mark all tracking fields and load out */ 3841da177e4SLinus Torvalds buf->nframesout += 1; 3851da177e4SLinus Torvalds buf->sector += bcnt >> 9; 3861da177e4SLinus Torvalds 38768e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 3884f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 38969cf2d85SEd Cashin if (skb) { 390*5f0c9c48SEd Cashin do_gettimeofday(&f->sent); 391*5f0c9c48SEd Cashin f->sent_jiffs = (u32) jiffies; 39269cf2d85SEd Cashin __skb_queue_head_init(&queue); 39369cf2d85SEd Cashin __skb_queue_tail(&queue, skb); 39469cf2d85SEd Cashin aoenet_xmit(&queue); 39569cf2d85SEd Cashin } 39668e0d42fSEd L. Cashin return 1; 39768e0d42fSEd L. Cashin } 3981da177e4SLinus Torvalds 3993ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 4003ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 4013ae1c24eSEd L. Cashin */ 402e9bb8fb0SDavid S. Miller static void 403e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 4043ae1c24eSEd L. Cashin { 4053ae1c24eSEd L. Cashin struct aoe_hdr *h; 4063ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 407e9bb8fb0SDavid S. Miller struct sk_buff *skb; 4083ae1c24eSEd L. Cashin struct net_device *ifp; 4093ae1c24eSEd L. Cashin 410840a185dSEric Dumazet rcu_read_lock(); 411840a185dSEric Dumazet for_each_netdev_rcu(&init_net, ifp) { 4123ae1c24eSEd L. Cashin dev_hold(ifp); 4133ae1c24eSEd L. Cashin if (!is_aoe_netif(ifp)) 4147562f876SPavel Emelianov goto cont; 4153ae1c24eSEd L. Cashin 416e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 4173ae1c24eSEd L. Cashin if (skb == NULL) { 418a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 4197562f876SPavel Emelianov goto cont; 4203ae1c24eSEd L. Cashin } 42119900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 422e407a7f6SEd L. Cashin skb->dev = ifp; 423e9bb8fb0SDavid S. Miller __skb_queue_tail(queue, skb); 424abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 4253ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 4263ae1c24eSEd L. Cashin 4273ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 4283ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 4293ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 4303ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 4313ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 4323ae1c24eSEd L. Cashin h->minor = aoeminor; 4333ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 4343ae1c24eSEd L. Cashin 4357562f876SPavel Emelianov cont: 4367562f876SPavel Emelianov dev_put(ifp); 4373ae1c24eSEd L. Cashin } 438840a185dSEric Dumazet rcu_read_unlock(); 4393ae1c24eSEd L. Cashin } 4403ae1c24eSEd L. Cashin 4411da177e4SLinus Torvalds static void 442896831f5SEd Cashin resend(struct aoedev *d, struct frame *f) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds struct sk_buff *skb; 44569cf2d85SEd Cashin struct sk_buff_head queue; 4461da177e4SLinus Torvalds struct aoe_hdr *h; 44719bf2635SEd L. Cashin struct aoe_atahdr *ah; 448896831f5SEd Cashin struct aoetgt *t; 4491da177e4SLinus Torvalds char buf[128]; 4501da177e4SLinus Torvalds u32 n; 4511da177e4SLinus Torvalds 452896831f5SEd Cashin t = f->t; 45364a80f5aSEd Cashin n = newtag(d); 454e407a7f6SEd L. Cashin skb = f->skb; 4553f0f0133SEd Cashin if (ifrotate(t) == NULL) { 4563f0f0133SEd Cashin /* probably can't happen, but set it up to fail anyway */ 4573f0f0133SEd Cashin pr_info("aoe: resend: no interfaces to rotate to.\n"); 4583f0f0133SEd Cashin ktcomplete(f, NULL); 4593f0f0133SEd Cashin return; 4603f0f0133SEd Cashin } 461abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 46219bf2635SEd L. Cashin ah = (struct aoe_atahdr *) (h+1); 46368e0d42fSEd L. Cashin 46468e0d42fSEd L. Cashin snprintf(buf, sizeof buf, 465411c41eeSHarvey Harrison "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", 46668e0d42fSEd L. Cashin "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n, 467411c41eeSHarvey Harrison h->src, h->dst, t->nout); 46868e0d42fSEd L. Cashin aoechr_error(buf); 46968e0d42fSEd L. Cashin 4701da177e4SLinus Torvalds f->tag = n; 471896831f5SEd Cashin fhash(f); 47263e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 47368e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 47468e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 4751da177e4SLinus Torvalds 47668e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 4774f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 4784f51dc5eSEd L. Cashin if (skb == NULL) 4794f51dc5eSEd L. Cashin return; 480*5f0c9c48SEd Cashin do_gettimeofday(&f->sent); 481*5f0c9c48SEd Cashin f->sent_jiffs = (u32) jiffies; 48269cf2d85SEd Cashin __skb_queue_head_init(&queue); 48369cf2d85SEd Cashin __skb_queue_tail(&queue, skb); 48469cf2d85SEd Cashin aoenet_xmit(&queue); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds static int 488*5f0c9c48SEd Cashin tsince_hr(struct frame *f) 489*5f0c9c48SEd Cashin { 490*5f0c9c48SEd Cashin struct timeval now; 491*5f0c9c48SEd Cashin int n; 492*5f0c9c48SEd Cashin 493*5f0c9c48SEd Cashin do_gettimeofday(&now); 494*5f0c9c48SEd Cashin n = now.tv_usec - f->sent.tv_usec; 495*5f0c9c48SEd Cashin n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC; 496*5f0c9c48SEd Cashin 497*5f0c9c48SEd Cashin if (n < 0) 498*5f0c9c48SEd Cashin n = -n; 499*5f0c9c48SEd Cashin 500*5f0c9c48SEd Cashin /* For relatively long periods, use jiffies to avoid 501*5f0c9c48SEd Cashin * discrepancies caused by updates to the system time. 502*5f0c9c48SEd Cashin * 503*5f0c9c48SEd Cashin * On system with HZ of 1000, 32-bits is over 49 days 504*5f0c9c48SEd Cashin * worth of jiffies, or over 71 minutes worth of usecs. 505*5f0c9c48SEd Cashin * 506*5f0c9c48SEd Cashin * Jiffies overflow is handled by subtraction of unsigned ints: 507*5f0c9c48SEd Cashin * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe 508*5f0c9c48SEd Cashin * $3 = 4 509*5f0c9c48SEd Cashin * (gdb) 510*5f0c9c48SEd Cashin */ 511*5f0c9c48SEd Cashin if (n > USEC_PER_SEC / 4) { 512*5f0c9c48SEd Cashin n = ((u32) jiffies) - f->sent_jiffs; 513*5f0c9c48SEd Cashin n *= USEC_PER_SEC / HZ; 514*5f0c9c48SEd Cashin } 515*5f0c9c48SEd Cashin 516*5f0c9c48SEd Cashin return n; 517*5f0c9c48SEd Cashin } 518*5f0c9c48SEd Cashin 519*5f0c9c48SEd Cashin static int 520896831f5SEd Cashin tsince(u32 tag) 5211da177e4SLinus Torvalds { 5221da177e4SLinus Torvalds int n; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds n = jiffies & 0xffff; 5251da177e4SLinus Torvalds n -= tag & 0xffff; 5261da177e4SLinus Torvalds if (n < 0) 5271da177e4SLinus Torvalds n += 1<<16; 528*5f0c9c48SEd Cashin return jiffies_to_usecs(n + 1); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 53168e0d42fSEd L. Cashin static struct aoeif * 53268e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 53368e0d42fSEd L. Cashin { 53468e0d42fSEd L. Cashin struct aoeif *p, *e; 53568e0d42fSEd L. Cashin 53668e0d42fSEd L. Cashin p = t->ifs; 53768e0d42fSEd L. Cashin e = p + NAOEIFS; 53868e0d42fSEd L. Cashin for (; p < e; p++) 53968e0d42fSEd L. Cashin if (p->nd == nd) 54068e0d42fSEd L. Cashin return p; 54168e0d42fSEd L. Cashin return NULL; 54268e0d42fSEd L. Cashin } 54368e0d42fSEd L. Cashin 54468e0d42fSEd L. Cashin static void 54568e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 54668e0d42fSEd L. Cashin { 54768e0d42fSEd L. Cashin struct aoeif *e; 5481b86fda9SEd Cashin struct net_device *nd; 54968e0d42fSEd L. Cashin ulong n; 55068e0d42fSEd L. Cashin 5511b86fda9SEd Cashin nd = ifp->nd; 55268e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 55368e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 55468e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 55568e0d42fSEd L. Cashin e->nd = NULL; 5561b86fda9SEd Cashin dev_put(nd); 55768e0d42fSEd L. Cashin } 55868e0d42fSEd L. Cashin 55968e0d42fSEd L. Cashin static int 56068e0d42fSEd L. Cashin sthtith(struct aoedev *d) 56168e0d42fSEd L. Cashin { 562896831f5SEd Cashin struct frame *f, *nf; 563896831f5SEd Cashin struct list_head *nx, *pos, *head; 56468e0d42fSEd L. Cashin struct sk_buff *skb; 565896831f5SEd Cashin struct aoetgt *ht = d->htgt; 566896831f5SEd Cashin int i; 56768e0d42fSEd L. Cashin 568896831f5SEd Cashin for (i = 0; i < NFACTIVE; i++) { 56964a80f5aSEd Cashin head = &d->factive[i]; 570896831f5SEd Cashin list_for_each_safe(pos, nx, head) { 571896831f5SEd Cashin f = list_entry(pos, struct frame, head); 57264a80f5aSEd Cashin if (f->t != ht) 57364a80f5aSEd Cashin continue; 57464a80f5aSEd Cashin 575896831f5SEd Cashin nf = newframe(d); 57668e0d42fSEd L. Cashin if (!nf) 57768e0d42fSEd L. Cashin return 0; 578896831f5SEd Cashin 579896831f5SEd Cashin /* remove frame from active list */ 580896831f5SEd Cashin list_del(pos); 581896831f5SEd Cashin 582896831f5SEd Cashin /* reassign all pertinent bits to new outbound frame */ 58368e0d42fSEd L. Cashin skb = nf->skb; 584896831f5SEd Cashin nf->skb = f->skb; 585896831f5SEd Cashin nf->buf = f->buf; 586896831f5SEd Cashin nf->bcnt = f->bcnt; 587896831f5SEd Cashin nf->lba = f->lba; 588896831f5SEd Cashin nf->bv = f->bv; 589896831f5SEd Cashin nf->bv_off = f->bv_off; 59068e0d42fSEd L. Cashin nf->waited = 0; 591*5f0c9c48SEd Cashin nf->sent_jiffs = f->sent_jiffs; 592896831f5SEd Cashin f->skb = skb; 593896831f5SEd Cashin aoe_freetframe(f); 59468e0d42fSEd L. Cashin ht->nout--; 595896831f5SEd Cashin nf->t->nout++; 596896831f5SEd Cashin resend(d, nf); 597896831f5SEd Cashin } 59868e0d42fSEd L. Cashin } 5993f0f0133SEd Cashin /* We've cleaned up the outstanding so take away his 6003f0f0133SEd Cashin * interfaces so he won't be used. We should remove him from 6013f0f0133SEd Cashin * the target array here, but cleaning up a target is 6023f0f0133SEd Cashin * involved. PUNT! 6033f0f0133SEd Cashin */ 60468e0d42fSEd L. Cashin memset(ht->ifs, 0, sizeof ht->ifs); 60568e0d42fSEd L. Cashin d->htgt = NULL; 60668e0d42fSEd L. Cashin return 1; 60768e0d42fSEd L. Cashin } 60868e0d42fSEd L. Cashin 6091da177e4SLinus Torvalds static void 6103a0c40d2SEd Cashin rexmit_deferred(struct aoedev *d) 6113a0c40d2SEd Cashin { 6123a0c40d2SEd Cashin struct aoetgt *t; 6133a0c40d2SEd Cashin struct frame *f; 6143a0c40d2SEd Cashin struct list_head *pos, *nx, *head; 6153a0c40d2SEd Cashin 6163a0c40d2SEd Cashin head = &d->rexmitq; 6173a0c40d2SEd Cashin list_for_each_safe(pos, nx, head) { 6183a0c40d2SEd Cashin f = list_entry(pos, struct frame, head); 6193a0c40d2SEd Cashin t = f->t; 6203a0c40d2SEd Cashin if (t->nout >= t->maxout) 6213a0c40d2SEd Cashin continue; 6223a0c40d2SEd Cashin list_del(pos); 6233a0c40d2SEd Cashin t->nout++; 6243a0c40d2SEd Cashin resend(d, f); 6253a0c40d2SEd Cashin } 6263a0c40d2SEd Cashin } 6273a0c40d2SEd Cashin 6283a0c40d2SEd Cashin static void 6291da177e4SLinus Torvalds rexmit_timer(ulong vp) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds struct aoedev *d; 6323a0c40d2SEd Cashin struct aoetgt *t; 63368e0d42fSEd L. Cashin struct aoeif *ifp; 634896831f5SEd Cashin struct frame *f; 635896831f5SEd Cashin struct list_head *head, *pos, *nx; 636896831f5SEd Cashin LIST_HEAD(flist); 6371da177e4SLinus Torvalds register long timeout; 6381da177e4SLinus Torvalds ulong flags, n; 639896831f5SEd Cashin int i; 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds d = (struct aoedev *) vp; 6421da177e4SLinus Torvalds 6430d555ecfSEd Cashin spin_lock_irqsave(&d->lock, flags); 6440d555ecfSEd Cashin 6453a0c40d2SEd Cashin /* timeout based on observed timings and variations */ 6463a0c40d2SEd Cashin timeout = 2 * d->rttavg >> RTTSCALE; 6473a0c40d2SEd Cashin timeout += 8 * d->rttdev >> RTTDSCALE; 6483a0c40d2SEd Cashin if (timeout == 0) 6493a0c40d2SEd Cashin timeout = 1; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 6521c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 6531da177e4SLinus Torvalds return; 6541da177e4SLinus Torvalds } 655896831f5SEd Cashin 656896831f5SEd Cashin /* collect all frames to rexmit into flist */ 657896831f5SEd Cashin for (i = 0; i < NFACTIVE; i++) { 65864a80f5aSEd Cashin head = &d->factive[i]; 659896831f5SEd Cashin list_for_each_safe(pos, nx, head) { 660896831f5SEd Cashin f = list_entry(pos, struct frame, head); 661*5f0c9c48SEd Cashin if (tsince_hr(f) < timeout) 66264a80f5aSEd Cashin break; /* end of expired frames */ 663896831f5SEd Cashin /* move to flist for later processing */ 664896831f5SEd Cashin list_move_tail(pos, &flist); 665896831f5SEd Cashin } 666896831f5SEd Cashin } 66769cf2d85SEd Cashin 668896831f5SEd Cashin /* process expired frames */ 669896831f5SEd Cashin while (!list_empty(&flist)) { 670896831f5SEd Cashin pos = flist.next; 671896831f5SEd Cashin f = list_entry(pos, struct frame, head); 672*5f0c9c48SEd Cashin n = f->waited += tsince_hr(f); 673*5f0c9c48SEd Cashin n /= USEC_PER_SEC; 67468e0d42fSEd L. Cashin if (n > aoe_deadsecs) { 675896831f5SEd Cashin /* Waited too long. Device failure. 676896831f5SEd Cashin * Hang all frames on first hash bucket for downdev 677896831f5SEd Cashin * to clean up. 678896831f5SEd Cashin */ 67964a80f5aSEd Cashin list_splice(&flist, &d->factive[0]); 6801da177e4SLinus Torvalds aoedev_downdev(d); 6813a0c40d2SEd Cashin goto out; 6821da177e4SLinus Torvalds } 68368e0d42fSEd L. Cashin 684896831f5SEd Cashin t = f->t; 685d54d35acSEd Cashin if (n > aoe_deadsecs/2) 686d54d35acSEd Cashin d->htgt = t; /* see if another target can help */ 687d54d35acSEd Cashin 6883a0c40d2SEd Cashin if (t->maxout != 1) { 6893a0c40d2SEd Cashin t->ssthresh = t->maxout / 2; 6903a0c40d2SEd Cashin t->maxout = 1; 69168e0d42fSEd L. Cashin } 69268e0d42fSEd L. Cashin 69368e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 69468e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 69568e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 69668e0d42fSEd L. Cashin ejectif(t, ifp); 69768e0d42fSEd L. Cashin ifp = NULL; 69868e0d42fSEd L. Cashin } 6993a0c40d2SEd Cashin list_move_tail(pos, &d->rexmitq); 7003a0c40d2SEd Cashin t->nout--; 7011da177e4SLinus Torvalds } 7023a0c40d2SEd Cashin rexmit_deferred(d); 70368e0d42fSEd L. Cashin 7043a0c40d2SEd Cashin out: 70569cf2d85SEd Cashin if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) { 7064f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 70769cf2d85SEd Cashin d->blkq->request_fn(d->blkq); 7084f51dc5eSEd L. Cashin } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 7111da177e4SLinus Torvalds add_timer(&d->timer); 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 71469cf2d85SEd Cashin } 7151da177e4SLinus Torvalds 71669cf2d85SEd Cashin static unsigned long 71769cf2d85SEd Cashin rqbiocnt(struct request *r) 71869cf2d85SEd Cashin { 71969cf2d85SEd Cashin struct bio *bio; 72069cf2d85SEd Cashin unsigned long n = 0; 72169cf2d85SEd Cashin 72269cf2d85SEd Cashin __rq_for_each_bio(bio, r) 72369cf2d85SEd Cashin n++; 72469cf2d85SEd Cashin return n; 72569cf2d85SEd Cashin } 72669cf2d85SEd Cashin 72769cf2d85SEd Cashin /* This can be removed if we are certain that no users of the block 72869cf2d85SEd Cashin * layer will ever use zero-count pages in bios. Otherwise we have to 72969cf2d85SEd Cashin * protect against the put_page sometimes done by the network layer. 73069cf2d85SEd Cashin * 73169cf2d85SEd Cashin * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for 73269cf2d85SEd Cashin * discussion. 73369cf2d85SEd Cashin * 73469cf2d85SEd Cashin * We cannot use get_page in the workaround, because it insists on a 73569cf2d85SEd Cashin * positive page count as a precondition. So we use _count directly. 73669cf2d85SEd Cashin */ 73769cf2d85SEd Cashin static void 73869cf2d85SEd Cashin bio_pageinc(struct bio *bio) 73969cf2d85SEd Cashin { 74069cf2d85SEd Cashin struct bio_vec *bv; 74169cf2d85SEd Cashin struct page *page; 74269cf2d85SEd Cashin int i; 74369cf2d85SEd Cashin 74469cf2d85SEd Cashin bio_for_each_segment(bv, bio, i) { 74569cf2d85SEd Cashin page = bv->bv_page; 74669cf2d85SEd Cashin /* Non-zero page count for non-head members of 74769cf2d85SEd Cashin * compound pages is no longer allowed by the kernel, 74869cf2d85SEd Cashin * but this has never been seen here. 74969cf2d85SEd Cashin */ 75069cf2d85SEd Cashin if (unlikely(PageCompound(page))) 75169cf2d85SEd Cashin if (compound_trans_head(page) != page) { 75269cf2d85SEd Cashin pr_crit("page tail used for block I/O\n"); 75369cf2d85SEd Cashin BUG(); 75469cf2d85SEd Cashin } 75569cf2d85SEd Cashin atomic_inc(&page->_count); 75669cf2d85SEd Cashin } 75769cf2d85SEd Cashin } 75869cf2d85SEd Cashin 75969cf2d85SEd Cashin static void 76069cf2d85SEd Cashin bio_pagedec(struct bio *bio) 76169cf2d85SEd Cashin { 76269cf2d85SEd Cashin struct bio_vec *bv; 76369cf2d85SEd Cashin int i; 76469cf2d85SEd Cashin 76569cf2d85SEd Cashin bio_for_each_segment(bv, bio, i) 76669cf2d85SEd Cashin atomic_dec(&bv->bv_page->_count); 76769cf2d85SEd Cashin } 76869cf2d85SEd Cashin 76969cf2d85SEd Cashin static void 77069cf2d85SEd Cashin bufinit(struct buf *buf, struct request *rq, struct bio *bio) 77169cf2d85SEd Cashin { 77269cf2d85SEd Cashin struct bio_vec *bv; 77369cf2d85SEd Cashin 77469cf2d85SEd Cashin memset(buf, 0, sizeof(*buf)); 77569cf2d85SEd Cashin buf->rq = rq; 77669cf2d85SEd Cashin buf->bio = bio; 77769cf2d85SEd Cashin buf->resid = bio->bi_size; 77869cf2d85SEd Cashin buf->sector = bio->bi_sector; 77969cf2d85SEd Cashin bio_pageinc(bio); 78069cf2d85SEd Cashin buf->bv = bv = &bio->bi_io_vec[bio->bi_idx]; 78169cf2d85SEd Cashin buf->bv_resid = bv->bv_len; 78269cf2d85SEd Cashin WARN_ON(buf->bv_resid == 0); 78369cf2d85SEd Cashin } 78469cf2d85SEd Cashin 78569cf2d85SEd Cashin static struct buf * 78669cf2d85SEd Cashin nextbuf(struct aoedev *d) 78769cf2d85SEd Cashin { 78869cf2d85SEd Cashin struct request *rq; 78969cf2d85SEd Cashin struct request_queue *q; 79069cf2d85SEd Cashin struct buf *buf; 79169cf2d85SEd Cashin struct bio *bio; 79269cf2d85SEd Cashin 79369cf2d85SEd Cashin q = d->blkq; 79469cf2d85SEd Cashin if (q == NULL) 79569cf2d85SEd Cashin return NULL; /* initializing */ 79669cf2d85SEd Cashin if (d->ip.buf) 79769cf2d85SEd Cashin return d->ip.buf; 79869cf2d85SEd Cashin rq = d->ip.rq; 79969cf2d85SEd Cashin if (rq == NULL) { 80069cf2d85SEd Cashin rq = blk_peek_request(q); 80169cf2d85SEd Cashin if (rq == NULL) 80269cf2d85SEd Cashin return NULL; 80369cf2d85SEd Cashin blk_start_request(rq); 80469cf2d85SEd Cashin d->ip.rq = rq; 80569cf2d85SEd Cashin d->ip.nxbio = rq->bio; 80669cf2d85SEd Cashin rq->special = (void *) rqbiocnt(rq); 80769cf2d85SEd Cashin } 80869cf2d85SEd Cashin buf = mempool_alloc(d->bufpool, GFP_ATOMIC); 80969cf2d85SEd Cashin if (buf == NULL) { 81069cf2d85SEd Cashin pr_err("aoe: nextbuf: unable to mempool_alloc!\n"); 81169cf2d85SEd Cashin return NULL; 81269cf2d85SEd Cashin } 81369cf2d85SEd Cashin bio = d->ip.nxbio; 81469cf2d85SEd Cashin bufinit(buf, rq, bio); 81569cf2d85SEd Cashin bio = bio->bi_next; 81669cf2d85SEd Cashin d->ip.nxbio = bio; 81769cf2d85SEd Cashin if (bio == NULL) 81869cf2d85SEd Cashin d->ip.rq = NULL; 81969cf2d85SEd Cashin return d->ip.buf = buf; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 82268e0d42fSEd L. Cashin /* enters with d->lock held */ 82368e0d42fSEd L. Cashin void 82468e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 82568e0d42fSEd L. Cashin { 82668e0d42fSEd L. Cashin if (d->htgt && !sthtith(d)) 82768e0d42fSEd L. Cashin return; 8283a0c40d2SEd Cashin rexmit_deferred(d); 82969cf2d85SEd Cashin while (aoecmd_ata_rw(d)) 83069cf2d85SEd Cashin ; 83168e0d42fSEd L. Cashin } 83268e0d42fSEd L. Cashin 8333ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 8343ae1c24eSEd L. Cashin */ 8353ae1c24eSEd L. Cashin void 836c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 8373ae1c24eSEd L. Cashin { 838c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 839b21faa25SEd Cashin struct block_device *bd; 840b21faa25SEd Cashin u64 ssize; 8413ae1c24eSEd L. Cashin 8423ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 8433ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 8443ae1c24eSEd L. Cashin 8453ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 84680795aefSTejun Heo ssize = get_capacity(d->gd); 8473ae1c24eSEd L. Cashin bd = bdget_disk(d->gd, 0); 8483ae1c24eSEd L. Cashin if (bd) { 8493ae1c24eSEd L. Cashin mutex_lock(&bd->bd_inode->i_mutex); 8503ae1c24eSEd L. Cashin i_size_write(bd->bd_inode, (loff_t)ssize<<9); 8513ae1c24eSEd L. Cashin mutex_unlock(&bd->bd_inode->i_mutex); 8523ae1c24eSEd L. Cashin bdput(bd); 8533ae1c24eSEd L. Cashin } 854b21faa25SEd Cashin spin_lock_irq(&d->lock); 8553ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 8563ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 857b21faa25SEd Cashin spin_unlock_irq(&d->lock); 8583ae1c24eSEd L. Cashin } 8593ae1c24eSEd L. Cashin } 8603ae1c24eSEd L. Cashin 8611da177e4SLinus Torvalds static void 862667be1e7SEd Cashin ata_ident_fixstring(u16 *id, int ns) 863667be1e7SEd Cashin { 864667be1e7SEd Cashin u16 s; 865667be1e7SEd Cashin 866667be1e7SEd Cashin while (ns-- > 0) { 867667be1e7SEd Cashin s = *id; 868667be1e7SEd Cashin *id++ = s >> 8 | s << 8; 869667be1e7SEd Cashin } 870667be1e7SEd Cashin } 871667be1e7SEd Cashin 872667be1e7SEd Cashin static void 87368e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds u64 ssize; 8761da177e4SLinus Torvalds u16 n; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* word 83: command set supported */ 879f885f8d1SHarvey Harrison n = get_unaligned_le16(&id[83 << 1]); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 882f885f8d1SHarvey Harrison n |= get_unaligned_le16(&id[86 << 1]); 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 8851da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 888f885f8d1SHarvey Harrison ssize = get_unaligned_le64(&id[100 << 1]); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 8911da177e4SLinus Torvalds d->geo.cylinders = ssize; 8921da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 8931da177e4SLinus Torvalds d->geo.heads = 255; 8941da177e4SLinus Torvalds d->geo.sectors = 63; 8951da177e4SLinus Torvalds } else { 8961da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* number lba28 sectors */ 899f885f8d1SHarvey Harrison ssize = get_unaligned_le32(&id[60 << 1]); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 902f885f8d1SHarvey Harrison d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 903f885f8d1SHarvey Harrison d->geo.heads = get_unaligned_le16(&id[55 << 1]); 904f885f8d1SHarvey Harrison d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 9051da177e4SLinus Torvalds } 9063ae1c24eSEd L. Cashin 907667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */ 908667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */ 909667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */ 910667be1e7SEd Cashin memcpy(d->ident, id, sizeof(d->ident)); 911667be1e7SEd Cashin 9123ae1c24eSEd L. Cashin if (d->ssize != ssize) 9131d75981aSEd L. Cashin printk(KERN_INFO 914411c41eeSHarvey Harrison "aoe: %pm e%ld.%d v%04x has %llu sectors\n", 915411c41eeSHarvey Harrison t->addr, 9163ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 9173ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 9181da177e4SLinus Torvalds d->ssize = ssize; 9191da177e4SLinus Torvalds d->geo.start = 0; 9206b9699bbSEd L. Cashin if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 9216b9699bbSEd L. Cashin return; 9221da177e4SLinus Torvalds if (d->gd != NULL) { 92380795aefSTejun Heo set_capacity(d->gd, ssize); 9243ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 92568e0d42fSEd L. Cashin } else 9263ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 9271da177e4SLinus Torvalds schedule_work(&d->work); 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds static void 9313a0c40d2SEd Cashin calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt) 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds register long n; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds n = rtt; 9361da177e4SLinus Torvalds 9373a0c40d2SEd Cashin /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */ 9383a0c40d2SEd Cashin n -= d->rttavg >> RTTSCALE; 9393a0c40d2SEd Cashin d->rttavg += n; 9403a0c40d2SEd Cashin if (n < 0) 9413a0c40d2SEd Cashin n = -n; 9423a0c40d2SEd Cashin n -= d->rttdev >> RTTDSCALE; 9433a0c40d2SEd Cashin d->rttdev += n; 9443a0c40d2SEd Cashin 9453a0c40d2SEd Cashin if (!t || t->maxout >= t->nframes) 9463a0c40d2SEd Cashin return; 9473a0c40d2SEd Cashin if (t->maxout < t->ssthresh) 9483a0c40d2SEd Cashin t->maxout += 1; 9493a0c40d2SEd Cashin else if (t->nout == t->maxout && t->next_cwnd-- == 0) { 9503a0c40d2SEd Cashin t->maxout += 1; 9513a0c40d2SEd Cashin t->next_cwnd = t->maxout; 9523a0c40d2SEd Cashin } 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 95568e0d42fSEd L. Cashin static struct aoetgt * 95668e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 95768e0d42fSEd L. Cashin { 95868e0d42fSEd L. Cashin struct aoetgt **t, **e; 95968e0d42fSEd L. Cashin 96068e0d42fSEd L. Cashin t = d->targets; 96168e0d42fSEd L. Cashin e = t + NTARGETS; 96268e0d42fSEd L. Cashin for (; t < e && *t; t++) 96368e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 96468e0d42fSEd L. Cashin return *t; 96568e0d42fSEd L. Cashin return NULL; 96668e0d42fSEd L. Cashin } 96768e0d42fSEd L. Cashin 9683d5b0605SEd Cashin static void 969896831f5SEd Cashin bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt) 9703d5b0605SEd Cashin { 9713d5b0605SEd Cashin ulong fcnt; 9723d5b0605SEd Cashin char *p; 9733d5b0605SEd Cashin int soff = 0; 9743d5b0605SEd Cashin loop: 9753d5b0605SEd Cashin fcnt = bv->bv_len - (off - bv->bv_offset); 9763d5b0605SEd Cashin if (fcnt > cnt) 9773d5b0605SEd Cashin fcnt = cnt; 9783d5b0605SEd Cashin p = page_address(bv->bv_page) + off; 9793d5b0605SEd Cashin skb_copy_bits(skb, soff, p, fcnt); 9803d5b0605SEd Cashin soff += fcnt; 9813d5b0605SEd Cashin cnt -= fcnt; 9823d5b0605SEd Cashin if (cnt <= 0) 9833d5b0605SEd Cashin return; 9843d5b0605SEd Cashin bv++; 9853d5b0605SEd Cashin off = bv->bv_offset; 9863d5b0605SEd Cashin goto loop; 9873d5b0605SEd Cashin } 9883d5b0605SEd Cashin 98969cf2d85SEd Cashin void 99069cf2d85SEd Cashin aoe_end_request(struct aoedev *d, struct request *rq, int fastfail) 99169cf2d85SEd Cashin { 99269cf2d85SEd Cashin struct bio *bio; 99369cf2d85SEd Cashin int bok; 99469cf2d85SEd Cashin struct request_queue *q; 99569cf2d85SEd Cashin 99669cf2d85SEd Cashin q = d->blkq; 99769cf2d85SEd Cashin if (rq == d->ip.rq) 99869cf2d85SEd Cashin d->ip.rq = NULL; 99969cf2d85SEd Cashin do { 100069cf2d85SEd Cashin bio = rq->bio; 100169cf2d85SEd Cashin bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags); 100269cf2d85SEd Cashin } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size)); 100369cf2d85SEd Cashin 100469cf2d85SEd Cashin /* cf. http://lkml.org/lkml/2006/10/31/28 */ 100569cf2d85SEd Cashin if (!fastfail) 100611cfb6ffSEd Cashin __blk_run_queue(q); 100769cf2d85SEd Cashin } 100869cf2d85SEd Cashin 100969cf2d85SEd Cashin static void 101069cf2d85SEd Cashin aoe_end_buf(struct aoedev *d, struct buf *buf) 101169cf2d85SEd Cashin { 101269cf2d85SEd Cashin struct request *rq; 101369cf2d85SEd Cashin unsigned long n; 101469cf2d85SEd Cashin 101569cf2d85SEd Cashin if (buf == d->ip.buf) 101669cf2d85SEd Cashin d->ip.buf = NULL; 101769cf2d85SEd Cashin rq = buf->rq; 101869cf2d85SEd Cashin bio_pagedec(buf->bio); 101969cf2d85SEd Cashin mempool_free(buf, d->bufpool); 102069cf2d85SEd Cashin n = (unsigned long) rq->special; 102169cf2d85SEd Cashin rq->special = (void *) --n; 102269cf2d85SEd Cashin if (n == 0) 102369cf2d85SEd Cashin aoe_end_request(d, rq, 0); 102469cf2d85SEd Cashin } 102569cf2d85SEd Cashin 10263d5b0605SEd Cashin static void 1027896831f5SEd Cashin ktiocomplete(struct frame *f) 10283d5b0605SEd Cashin { 1029ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 10301da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 10311da177e4SLinus Torvalds struct buf *buf; 1032896831f5SEd Cashin struct sk_buff *skb; 103368e0d42fSEd L. Cashin struct aoetgt *t; 103468e0d42fSEd L. Cashin struct aoeif *ifp; 1035896831f5SEd Cashin struct aoedev *d; 1036896831f5SEd Cashin long n; 1037896831f5SEd Cashin 1038896831f5SEd Cashin if (f == NULL) 1039896831f5SEd Cashin return; 1040896831f5SEd Cashin 1041896831f5SEd Cashin t = f->t; 1042896831f5SEd Cashin d = t->d; 1043896831f5SEd Cashin 1044896831f5SEd Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 1045896831f5SEd Cashin ahout = (struct aoe_atahdr *) (hout+1); 1046896831f5SEd Cashin buf = f->buf; 1047896831f5SEd Cashin skb = f->r_skb; 1048896831f5SEd Cashin if (skb == NULL) 1049896831f5SEd Cashin goto noskb; /* just fail the buf. */ 1050896831f5SEd Cashin 1051896831f5SEd Cashin hin = (struct aoe_hdr *) skb->data; 1052896831f5SEd Cashin skb_pull(skb, sizeof(*hin)); 1053896831f5SEd Cashin ahin = (struct aoe_atahdr *) skb->data; 1054896831f5SEd Cashin skb_pull(skb, sizeof(*ahin)); 1055896831f5SEd Cashin if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 1056896831f5SEd Cashin pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 1057896831f5SEd Cashin ahout->cmdstat, ahin->cmdstat, 1058896831f5SEd Cashin d->aoemajor, d->aoeminor); 1059896831f5SEd Cashin noskb: if (buf) 106069cf2d85SEd Cashin clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 1061896831f5SEd Cashin goto badrsp; 1062896831f5SEd Cashin } 1063896831f5SEd Cashin 1064896831f5SEd Cashin n = ahout->scnt << 9; 1065896831f5SEd Cashin switch (ahout->cmdstat) { 1066896831f5SEd Cashin case ATA_CMD_PIO_READ: 1067896831f5SEd Cashin case ATA_CMD_PIO_READ_EXT: 1068896831f5SEd Cashin if (skb->len < n) { 1069896831f5SEd Cashin pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n", 1070896831f5SEd Cashin skb->len, n); 107169cf2d85SEd Cashin clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 1072896831f5SEd Cashin break; 1073896831f5SEd Cashin } 1074896831f5SEd Cashin bvcpy(f->bv, f->bv_off, skb, n); 1075896831f5SEd Cashin case ATA_CMD_PIO_WRITE: 1076896831f5SEd Cashin case ATA_CMD_PIO_WRITE_EXT: 1077896831f5SEd Cashin spin_lock_irq(&d->lock); 1078896831f5SEd Cashin ifp = getif(t, skb->dev); 10793f0f0133SEd Cashin if (ifp) 1080896831f5SEd Cashin ifp->lost = 0; 1081896831f5SEd Cashin if (d->htgt == t) /* I'll help myself, thank you. */ 1082896831f5SEd Cashin d->htgt = NULL; 1083896831f5SEd Cashin spin_unlock_irq(&d->lock); 1084896831f5SEd Cashin break; 1085896831f5SEd Cashin case ATA_CMD_ID_ATA: 1086896831f5SEd Cashin if (skb->len < 512) { 1087896831f5SEd Cashin pr_info("aoe: runt data size in ataid. skb->len=%d\n", 1088896831f5SEd Cashin skb->len); 1089896831f5SEd Cashin break; 1090896831f5SEd Cashin } 1091896831f5SEd Cashin if (skb_linearize(skb)) 1092896831f5SEd Cashin break; 1093896831f5SEd Cashin spin_lock_irq(&d->lock); 1094896831f5SEd Cashin ataid_complete(d, t, skb->data); 1095896831f5SEd Cashin spin_unlock_irq(&d->lock); 1096896831f5SEd Cashin break; 1097896831f5SEd Cashin default: 1098896831f5SEd Cashin pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n", 1099896831f5SEd Cashin ahout->cmdstat, 1100896831f5SEd Cashin be16_to_cpu(get_unaligned(&hin->major)), 1101896831f5SEd Cashin hin->minor); 1102896831f5SEd Cashin } 1103896831f5SEd Cashin badrsp: 1104896831f5SEd Cashin spin_lock_irq(&d->lock); 1105896831f5SEd Cashin 1106896831f5SEd Cashin aoe_freetframe(f); 1107896831f5SEd Cashin 110869cf2d85SEd Cashin if (buf && --buf->nframesout == 0 && buf->resid == 0) 110969cf2d85SEd Cashin aoe_end_buf(d, buf); 1110896831f5SEd Cashin 111169cf2d85SEd Cashin aoecmd_work(d); 111269cf2d85SEd Cashin 1113896831f5SEd Cashin spin_unlock_irq(&d->lock); 111469cf2d85SEd Cashin aoedev_put(d); 1115896831f5SEd Cashin dev_kfree_skb(skb); 1116896831f5SEd Cashin } 1117896831f5SEd Cashin 1118896831f5SEd Cashin /* Enters with iocq.lock held. 1119896831f5SEd Cashin * Returns true iff responses needing processing remain. 1120896831f5SEd Cashin */ 1121896831f5SEd Cashin static int 1122896831f5SEd Cashin ktio(void) 1123896831f5SEd Cashin { 1124896831f5SEd Cashin struct frame *f; 1125896831f5SEd Cashin struct list_head *pos; 1126896831f5SEd Cashin int i; 1127896831f5SEd Cashin 1128896831f5SEd Cashin for (i = 0; ; ++i) { 1129896831f5SEd Cashin if (i == MAXIOC) 1130896831f5SEd Cashin return 1; 1131896831f5SEd Cashin if (list_empty(&iocq.head)) 1132896831f5SEd Cashin return 0; 1133896831f5SEd Cashin pos = iocq.head.next; 1134896831f5SEd Cashin list_del(pos); 1135896831f5SEd Cashin spin_unlock_irq(&iocq.lock); 1136896831f5SEd Cashin f = list_entry(pos, struct frame, head); 1137896831f5SEd Cashin ktiocomplete(f); 1138896831f5SEd Cashin spin_lock_irq(&iocq.lock); 1139896831f5SEd Cashin } 1140896831f5SEd Cashin } 1141896831f5SEd Cashin 1142896831f5SEd Cashin static int 1143896831f5SEd Cashin kthread(void *vp) 1144896831f5SEd Cashin { 1145896831f5SEd Cashin struct ktstate *k; 1146896831f5SEd Cashin DECLARE_WAITQUEUE(wait, current); 1147896831f5SEd Cashin int more; 1148896831f5SEd Cashin 1149896831f5SEd Cashin k = vp; 1150896831f5SEd Cashin current->flags |= PF_NOFREEZE; 1151896831f5SEd Cashin set_user_nice(current, -10); 1152896831f5SEd Cashin complete(&k->rendez); /* tell spawner we're running */ 1153896831f5SEd Cashin do { 1154896831f5SEd Cashin spin_lock_irq(k->lock); 1155896831f5SEd Cashin more = k->fn(); 1156896831f5SEd Cashin if (!more) { 1157896831f5SEd Cashin add_wait_queue(k->waitq, &wait); 1158896831f5SEd Cashin __set_current_state(TASK_INTERRUPTIBLE); 1159896831f5SEd Cashin } 1160896831f5SEd Cashin spin_unlock_irq(k->lock); 1161896831f5SEd Cashin if (!more) { 1162896831f5SEd Cashin schedule(); 1163896831f5SEd Cashin remove_wait_queue(k->waitq, &wait); 1164896831f5SEd Cashin } else 1165896831f5SEd Cashin cond_resched(); 1166896831f5SEd Cashin } while (!kthread_should_stop()); 1167896831f5SEd Cashin complete(&k->rendez); /* tell spawner we're stopping */ 1168896831f5SEd Cashin return 0; 1169896831f5SEd Cashin } 1170896831f5SEd Cashin 1171eb086ec5SEd Cashin void 1172896831f5SEd Cashin aoe_ktstop(struct ktstate *k) 1173896831f5SEd Cashin { 1174896831f5SEd Cashin kthread_stop(k->task); 1175896831f5SEd Cashin wait_for_completion(&k->rendez); 1176896831f5SEd Cashin } 1177896831f5SEd Cashin 1178eb086ec5SEd Cashin int 1179896831f5SEd Cashin aoe_ktstart(struct ktstate *k) 1180896831f5SEd Cashin { 1181896831f5SEd Cashin struct task_struct *task; 1182896831f5SEd Cashin 1183896831f5SEd Cashin init_completion(&k->rendez); 1184896831f5SEd Cashin task = kthread_run(kthread, k, k->name); 1185896831f5SEd Cashin if (task == NULL || IS_ERR(task)) 1186896831f5SEd Cashin return -ENOMEM; 1187896831f5SEd Cashin k->task = task; 1188896831f5SEd Cashin wait_for_completion(&k->rendez); /* allow kthread to start */ 1189896831f5SEd Cashin init_completion(&k->rendez); /* for waiting for exit later */ 1190896831f5SEd Cashin return 0; 1191896831f5SEd Cashin } 1192896831f5SEd Cashin 1193896831f5SEd Cashin /* pass it off to kthreads for processing */ 1194896831f5SEd Cashin static void 1195896831f5SEd Cashin ktcomplete(struct frame *f, struct sk_buff *skb) 1196896831f5SEd Cashin { 1197896831f5SEd Cashin ulong flags; 1198896831f5SEd Cashin 1199896831f5SEd Cashin f->r_skb = skb; 1200896831f5SEd Cashin spin_lock_irqsave(&iocq.lock, flags); 1201896831f5SEd Cashin list_add_tail(&f->head, &iocq.head); 1202896831f5SEd Cashin spin_unlock_irqrestore(&iocq.lock, flags); 1203896831f5SEd Cashin wake_up(&ktiowq); 1204896831f5SEd Cashin } 1205896831f5SEd Cashin 1206896831f5SEd Cashin struct sk_buff * 1207896831f5SEd Cashin aoecmd_ata_rsp(struct sk_buff *skb) 1208896831f5SEd Cashin { 1209896831f5SEd Cashin struct aoedev *d; 1210896831f5SEd Cashin struct aoe_hdr *h; 1211896831f5SEd Cashin struct frame *f; 1212896831f5SEd Cashin u32 n; 12131da177e4SLinus Torvalds ulong flags; 12141da177e4SLinus Torvalds char ebuf[128]; 121532465c65Secashin@coraid.com u16 aoemajor; 12161da177e4SLinus Torvalds 1217896831f5SEd Cashin h = (struct aoe_hdr *) skb->data; 1218896831f5SEd Cashin aoemajor = be16_to_cpu(get_unaligned(&h->major)); 12190c966214SEd Cashin d = aoedev_by_aoeaddr(aoemajor, h->minor, 0); 12201da177e4SLinus Torvalds if (d == NULL) { 12211da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 12221da177e4SLinus Torvalds "for unknown device %d.%d\n", 1223896831f5SEd Cashin aoemajor, h->minor); 12241da177e4SLinus Torvalds aoechr_error(ebuf); 1225896831f5SEd Cashin return skb; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 12291da177e4SLinus Torvalds 1230896831f5SEd Cashin n = be32_to_cpu(get_unaligned(&h->tag)); 123164a80f5aSEd Cashin f = getframe(d, n); 12323a0c40d2SEd Cashin if (f) { 1233*5f0c9c48SEd Cashin calc_rttavg(d, f->t, tsince_hr(f)); 12343a0c40d2SEd Cashin f->t->nout--; 12353a0c40d2SEd Cashin } else { 12363a0c40d2SEd Cashin f = getframe_deferred(d, n); 12373a0c40d2SEd Cashin if (f) { 1238*5f0c9c48SEd Cashin calc_rttavg(d, NULL, tsince_hr(f)); 12393a0c40d2SEd Cashin } else { 12403a0c40d2SEd Cashin calc_rttavg(d, NULL, tsince(n)); 12411da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 124269cf2d85SEd Cashin aoedev_put(d); 12433a0c40d2SEd Cashin snprintf(ebuf, sizeof(ebuf), 12442292a7e1SEd Cashin "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n", 12451da177e4SLinus Torvalds "unexpected rsp", 1246896831f5SEd Cashin get_unaligned_be16(&h->major), 1247896831f5SEd Cashin h->minor, 1248896831f5SEd Cashin get_unaligned_be32(&h->tag), 12492292a7e1SEd Cashin jiffies, 12502292a7e1SEd Cashin h->src, 12512292a7e1SEd Cashin h->dst); 12521da177e4SLinus Torvalds aoechr_error(ebuf); 1253896831f5SEd Cashin return skb; 12541da177e4SLinus Torvalds } 12553a0c40d2SEd Cashin } 12561da177e4SLinus Torvalds aoecmd_work(d); 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 1259896831f5SEd Cashin 1260896831f5SEd Cashin ktcomplete(f, skb); 1261896831f5SEd Cashin 1262896831f5SEd Cashin /* 1263896831f5SEd Cashin * Note here that we do not perform an aoedev_put, as we are 1264896831f5SEd Cashin * leaving this reference for the ktio to release. 1265896831f5SEd Cashin */ 1266896831f5SEd Cashin return NULL; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds void 12701da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 12711da177e4SLinus Torvalds { 1272e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 12731da177e4SLinus Torvalds 1274e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1275e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 1276e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 127968e0d42fSEd L. Cashin struct sk_buff * 12801da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds struct aoe_hdr *h; 12831da177e4SLinus Torvalds struct aoe_atahdr *ah; 12841da177e4SLinus Torvalds struct frame *f; 12851da177e4SLinus Torvalds struct sk_buff *skb; 128668e0d42fSEd L. Cashin struct aoetgt *t; 12871da177e4SLinus Torvalds 1288896831f5SEd Cashin f = newframe(d); 128968e0d42fSEd L. Cashin if (f == NULL) 12901da177e4SLinus Torvalds return NULL; 129168e0d42fSEd L. Cashin 129268e0d42fSEd L. Cashin t = *d->tgt; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds /* initialize the headers & frame */ 1295e407a7f6SEd L. Cashin skb = f->skb; 1296abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 12971da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 129819900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 129919900cdeSEd L. Cashin memset(h, 0, skb->len); 130068e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 1301896831f5SEd Cashin fhash(f); 130268e0d42fSEd L. Cashin t->nout++; 13031da177e4SLinus Torvalds f->waited = 0; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* set up ata header */ 13061da177e4SLinus Torvalds ah->scnt = 1; 130704b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_ID_ATA; 13081da177e4SLinus Torvalds ah->lba3 = 0xa0; 13091da177e4SLinus Torvalds 131068e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 13111da177e4SLinus Torvalds 13123a0c40d2SEd Cashin d->rttavg = RTTAVG_INIT; 13133a0c40d2SEd Cashin d->rttdev = RTTDEV_INIT; 13141da177e4SLinus Torvalds d->timer.function = rexmit_timer; 13151da177e4SLinus Torvalds 1316*5f0c9c48SEd Cashin skb = skb_clone(skb, GFP_ATOMIC); 1317*5f0c9c48SEd Cashin if (skb) { 1318*5f0c9c48SEd Cashin do_gettimeofday(&f->sent); 1319*5f0c9c48SEd Cashin f->sent_jiffs = (u32) jiffies; 1320*5f0c9c48SEd Cashin } 1321*5f0c9c48SEd Cashin 1322*5f0c9c48SEd Cashin return skb; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 132568e0d42fSEd L. Cashin static struct aoetgt * 132668e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes) 132768e0d42fSEd L. Cashin { 132868e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 132968e0d42fSEd L. Cashin 133068e0d42fSEd L. Cashin tt = d->targets; 133168e0d42fSEd L. Cashin te = tt + NTARGETS; 133268e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) 133368e0d42fSEd L. Cashin ; 133468e0d42fSEd L. Cashin 1335578c4aa0SEd L. Cashin if (tt == te) { 1336578c4aa0SEd L. Cashin printk(KERN_INFO 1337578c4aa0SEd L. Cashin "aoe: device addtgt failure; too many targets\n"); 133868e0d42fSEd L. Cashin return NULL; 1339578c4aa0SEd L. Cashin } 1340896831f5SEd Cashin t = kzalloc(sizeof(*t), GFP_ATOMIC); 1341896831f5SEd Cashin if (!t) { 1342578c4aa0SEd L. Cashin printk(KERN_INFO "aoe: cannot allocate memory to add target\n"); 13439bb237b6SEd L. Cashin return NULL; 13449bb237b6SEd L. Cashin } 13459bb237b6SEd L. Cashin 1346896831f5SEd Cashin d->ntargets++; 134768e0d42fSEd L. Cashin t->nframes = nframes; 1348896831f5SEd Cashin t->d = d; 134968e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 135068e0d42fSEd L. Cashin t->ifp = t->ifs; 13513a0c40d2SEd Cashin aoecmd_wreset(t); 1352896831f5SEd Cashin INIT_LIST_HEAD(&t->ffree); 135368e0d42fSEd L. Cashin return *tt = t; 135468e0d42fSEd L. Cashin } 135568e0d42fSEd L. Cashin 13563f0f0133SEd Cashin static void 13573f0f0133SEd Cashin setdbcnt(struct aoedev *d) 13583f0f0133SEd Cashin { 13593f0f0133SEd Cashin struct aoetgt **t, **e; 13603f0f0133SEd Cashin int bcnt = 0; 13613f0f0133SEd Cashin 13623f0f0133SEd Cashin t = d->targets; 13633f0f0133SEd Cashin e = t + NTARGETS; 13643f0f0133SEd Cashin for (; t < e && *t; t++) 13653f0f0133SEd Cashin if (bcnt == 0 || bcnt > (*t)->minbcnt) 13663f0f0133SEd Cashin bcnt = (*t)->minbcnt; 13673f0f0133SEd Cashin if (bcnt != d->maxbcnt) { 13683f0f0133SEd Cashin d->maxbcnt = bcnt; 13693f0f0133SEd Cashin pr_info("aoe: e%ld.%d: setting %d byte data frames\n", 13703f0f0133SEd Cashin d->aoemajor, d->aoeminor, bcnt); 13713f0f0133SEd Cashin } 13723f0f0133SEd Cashin } 13733f0f0133SEd Cashin 13743f0f0133SEd Cashin static void 13753f0f0133SEd Cashin setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt) 13763f0f0133SEd Cashin { 13773f0f0133SEd Cashin struct aoedev *d; 13783f0f0133SEd Cashin struct aoeif *p, *e; 13793f0f0133SEd Cashin int minbcnt; 13803f0f0133SEd Cashin 13813f0f0133SEd Cashin d = t->d; 13823f0f0133SEd Cashin minbcnt = bcnt; 13833f0f0133SEd Cashin p = t->ifs; 13843f0f0133SEd Cashin e = p + NAOEIFS; 13853f0f0133SEd Cashin for (; p < e; p++) { 13863f0f0133SEd Cashin if (p->nd == NULL) 13873f0f0133SEd Cashin break; /* end of the valid interfaces */ 13883f0f0133SEd Cashin if (p->nd == nd) { 13893f0f0133SEd Cashin p->bcnt = bcnt; /* we're updating */ 13903f0f0133SEd Cashin nd = NULL; 13913f0f0133SEd Cashin } else if (minbcnt > p->bcnt) 13923f0f0133SEd Cashin minbcnt = p->bcnt; /* find the min interface */ 13933f0f0133SEd Cashin } 13943f0f0133SEd Cashin if (nd) { 13953f0f0133SEd Cashin if (p == e) { 13963f0f0133SEd Cashin pr_err("aoe: device setifbcnt failure; too many interfaces.\n"); 13973f0f0133SEd Cashin return; 13983f0f0133SEd Cashin } 13991b86fda9SEd Cashin dev_hold(nd); 14003f0f0133SEd Cashin p->nd = nd; 14013f0f0133SEd Cashin p->bcnt = bcnt; 14023f0f0133SEd Cashin } 14033f0f0133SEd Cashin t->minbcnt = minbcnt; 14043f0f0133SEd Cashin setdbcnt(d); 14053f0f0133SEd Cashin } 14063f0f0133SEd Cashin 14071da177e4SLinus Torvalds void 14081da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds struct aoedev *d; 14111da177e4SLinus Torvalds struct aoe_hdr *h; 14121da177e4SLinus Torvalds struct aoe_cfghdr *ch; 141368e0d42fSEd L. Cashin struct aoetgt *t; 14140c966214SEd Cashin ulong flags, aoemajor; 14151da177e4SLinus Torvalds struct sk_buff *sl; 141669cf2d85SEd Cashin struct sk_buff_head queue; 141719bf2635SEd L. Cashin u16 n; 14181da177e4SLinus Torvalds 141969cf2d85SEd Cashin sl = NULL; 1420abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 14211da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds /* 14241da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 14251da177e4SLinus Torvalds * warrant a loud message for this special case. 14261da177e4SLinus Torvalds */ 1427823ed72eSHarvey Harrison aoemajor = get_unaligned_be16(&h->major); 14281da177e4SLinus Torvalds if (aoemajor == 0xfff) { 1429a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 14306bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 14311da177e4SLinus Torvalds return; 14321da177e4SLinus Torvalds } 14337159e969SEd Cashin if (aoemajor == 0xffff) { 14347159e969SEd Cashin pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n", 14350c966214SEd Cashin aoemajor, (int) h->minor); 14366583303cSEd Cashin return; 14376583303cSEd Cashin } 14387159e969SEd Cashin if (h->minor == 0xff) { 14397159e969SEd Cashin pr_info("aoe: e%ld.%d: broadcast slot number invalid\n", 14407159e969SEd Cashin aoemajor, (int) h->minor); 14411da177e4SLinus Torvalds return; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 144419bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 14457df620d8SEd L. Cashin if (n > aoe_maxout) /* keep it reasonable */ 14467df620d8SEd L. Cashin n = aoe_maxout; 14471da177e4SLinus Torvalds 14487159e969SEd Cashin d = aoedev_by_aoeaddr(aoemajor, h->minor, 1); 14497159e969SEd Cashin if (d == NULL) { 14507159e969SEd Cashin pr_info("aoe: device allocation failure\n"); 14517159e969SEd Cashin return; 14527159e969SEd Cashin } 14537159e969SEd Cashin 14541da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 14551da177e4SLinus Torvalds 145668e0d42fSEd L. Cashin t = gettgt(d, h->src); 14571b8a1636SEd Cashin if (t) { 14581b8a1636SEd Cashin t->nframes = n; 14591b8a1636SEd Cashin if (n < t->maxout) 14603a0c40d2SEd Cashin aoecmd_wreset(t); 14611b8a1636SEd Cashin } else { 146268e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 146369cf2d85SEd Cashin if (!t) 146469cf2d85SEd Cashin goto bail; 146568e0d42fSEd L. Cashin } 14663f0f0133SEd Cashin n = skb->dev->mtu; 146719bf2635SEd L. Cashin n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr); 146819bf2635SEd L. Cashin n /= 512; 146919bf2635SEd L. Cashin if (n > ch->scnt) 147019bf2635SEd L. Cashin n = ch->scnt; 14714f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 14723f0f0133SEd Cashin setifbcnt(t, skb->dev, n); 14733ae1c24eSEd L. Cashin 14743ae1c24eSEd L. Cashin /* don't change users' perspective */ 147569cf2d85SEd Cashin if (d->nopen == 0) { 147663e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 147768e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 147869cf2d85SEd Cashin } 147969cf2d85SEd Cashin bail: 14801da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 148169cf2d85SEd Cashin aoedev_put(d); 1482e9bb8fb0SDavid S. Miller if (sl) { 1483e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1484e9bb8fb0SDavid S. Miller __skb_queue_tail(&queue, sl); 1485e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 1486e9bb8fb0SDavid S. Miller } 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 148968e0d42fSEd L. Cashin void 14903a0c40d2SEd Cashin aoecmd_wreset(struct aoetgt *t) 14913a0c40d2SEd Cashin { 14923a0c40d2SEd Cashin t->maxout = 1; 14933a0c40d2SEd Cashin t->ssthresh = t->nframes / 2; 14943a0c40d2SEd Cashin t->next_cwnd = t->nframes; 14953a0c40d2SEd Cashin } 14963a0c40d2SEd Cashin 14973a0c40d2SEd Cashin void 149868e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 149968e0d42fSEd L. Cashin { 150068e0d42fSEd L. Cashin struct aoetgt **t, **te; 150168e0d42fSEd L. Cashin 15023a0c40d2SEd Cashin d->rttavg = RTTAVG_INIT; 15033a0c40d2SEd Cashin d->rttdev = RTTDEV_INIT; 15043f0f0133SEd Cashin d->maxbcnt = 0; 150568e0d42fSEd L. Cashin 150668e0d42fSEd L. Cashin t = d->targets; 150768e0d42fSEd L. Cashin te = t + NTARGETS; 15083f0f0133SEd Cashin for (; t < te && *t; t++) 15093a0c40d2SEd Cashin aoecmd_wreset(*t); 151068e0d42fSEd L. Cashin } 1511896831f5SEd Cashin 151269cf2d85SEd Cashin void 151369cf2d85SEd Cashin aoe_failbuf(struct aoedev *d, struct buf *buf) 151469cf2d85SEd Cashin { 151569cf2d85SEd Cashin if (buf == NULL) 151669cf2d85SEd Cashin return; 151769cf2d85SEd Cashin buf->resid = 0; 151869cf2d85SEd Cashin clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 151969cf2d85SEd Cashin if (buf->nframesout == 0) 152069cf2d85SEd Cashin aoe_end_buf(d, buf); 152169cf2d85SEd Cashin } 152269cf2d85SEd Cashin 152369cf2d85SEd Cashin void 152469cf2d85SEd Cashin aoe_flush_iocq(void) 1525896831f5SEd Cashin { 1526896831f5SEd Cashin struct frame *f; 1527896831f5SEd Cashin struct aoedev *d; 1528896831f5SEd Cashin LIST_HEAD(flist); 1529896831f5SEd Cashin struct list_head *pos; 1530896831f5SEd Cashin struct sk_buff *skb; 1531896831f5SEd Cashin ulong flags; 1532896831f5SEd Cashin 1533896831f5SEd Cashin spin_lock_irqsave(&iocq.lock, flags); 1534896831f5SEd Cashin list_splice_init(&iocq.head, &flist); 1535896831f5SEd Cashin spin_unlock_irqrestore(&iocq.lock, flags); 1536896831f5SEd Cashin while (!list_empty(&flist)) { 1537896831f5SEd Cashin pos = flist.next; 1538896831f5SEd Cashin list_del(pos); 1539896831f5SEd Cashin f = list_entry(pos, struct frame, head); 1540896831f5SEd Cashin d = f->t->d; 1541896831f5SEd Cashin skb = f->r_skb; 1542896831f5SEd Cashin spin_lock_irqsave(&d->lock, flags); 1543896831f5SEd Cashin if (f->buf) { 1544896831f5SEd Cashin f->buf->nframesout--; 1545896831f5SEd Cashin aoe_failbuf(d, f->buf); 1546896831f5SEd Cashin } 1547896831f5SEd Cashin aoe_freetframe(f); 1548896831f5SEd Cashin spin_unlock_irqrestore(&d->lock, flags); 1549896831f5SEd Cashin dev_kfree_skb(skb); 155069cf2d85SEd Cashin aoedev_put(d); 1551896831f5SEd Cashin } 1552896831f5SEd Cashin } 1553896831f5SEd Cashin 1554896831f5SEd Cashin int __init 1555896831f5SEd Cashin aoecmd_init(void) 1556896831f5SEd Cashin { 1557896831f5SEd Cashin INIT_LIST_HEAD(&iocq.head); 1558896831f5SEd Cashin spin_lock_init(&iocq.lock); 1559896831f5SEd Cashin init_waitqueue_head(&ktiowq); 1560896831f5SEd Cashin kts.name = "aoe_ktio"; 1561896831f5SEd Cashin kts.fn = ktio; 1562896831f5SEd Cashin kts.waitq = &ktiowq; 1563896831f5SEd Cashin kts.lock = &iocq.lock; 1564896831f5SEd Cashin return aoe_ktstart(&kts); 1565896831f5SEd Cashin } 1566896831f5SEd Cashin 1567896831f5SEd Cashin void 1568896831f5SEd Cashin aoecmd_exit(void) 1569896831f5SEd Cashin { 1570896831f5SEd Cashin aoe_ktstop(&kts); 157169cf2d85SEd Cashin aoe_flush_iocq(); 1572896831f5SEd Cashin } 1573