1ca47bbd9SEd Cashin /* Copyright (c) 2013 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> 103582dd29SJens Axboe #include <linux/blk-mq.h> 111da177e4SLinus Torvalds #include <linux/skbuff.h> 121da177e4SLinus Torvalds #include <linux/netdevice.h> 1368e0d42fSEd L. Cashin #include <linux/moduleparam.h> 14896831f5SEd Cashin #include <linux/workqueue.h> 15896831f5SEd Cashin #include <linux/kthread.h> 16881d966bSEric W. Biederman #include <net/net_namespace.h> 17475172fbSEd L. Cashin #include <asm/unaligned.h> 18896831f5SEd Cashin #include <linux/uio.h> 191da177e4SLinus Torvalds #include "aoe.h" 201da177e4SLinus Torvalds 21896831f5SEd Cashin #define MAXIOC (8192) /* default meant to avoid most soft lockups */ 22896831f5SEd Cashin 23896831f5SEd Cashin static void ktcomplete(struct frame *, struct sk_buff *); 24bbb44e30SEd Cashin static int count_targets(struct aoedev *d, int *untainted); 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 327b6ccc5fSEd Cashin static int aoe_maxout = 64; 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 378030d343SEd Cashin /* The number of online cpus during module initialization gives us a 388030d343SEd Cashin * convenient heuristic cap on the parallelism used for ktio threads 398030d343SEd Cashin * doing I/O completion. It is not important that the cap equal the 408030d343SEd Cashin * actual number of running CPUs at any given time, but because of CPU 418030d343SEd Cashin * hotplug, we take care to use ncpus instead of using 428030d343SEd Cashin * num_online_cpus() after module initialization. 438030d343SEd Cashin */ 448030d343SEd Cashin static int ncpus; 458030d343SEd Cashin 468030d343SEd Cashin /* mutex lock used for synchronization while thread spawning */ 478030d343SEd Cashin static DEFINE_MUTEX(ktio_spawn_lock); 488030d343SEd Cashin 498030d343SEd Cashin static wait_queue_head_t *ktiowq; 508030d343SEd Cashin static struct ktstate *kts; 51896831f5SEd Cashin 52896831f5SEd Cashin /* io completion queue */ 538030d343SEd Cashin struct iocq_ktio { 54896831f5SEd Cashin struct list_head head; 55896831f5SEd Cashin spinlock_t lock; 568030d343SEd Cashin }; 578030d343SEd Cashin static struct iocq_ktio *iocq; 58896831f5SEd Cashin 59bbb44e30SEd Cashin static struct page *empty_page; 60bbb44e30SEd Cashin 6168e0d42fSEd L. Cashin static struct sk_buff * 62e407a7f6SEd L. Cashin new_skb(ulong len) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds struct sk_buff *skb; 651da177e4SLinus Torvalds 6691c57464SEric Dumazet skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC); 671da177e4SLinus Torvalds if (skb) { 6891c57464SEric Dumazet skb_reserve(skb, MAX_HEADER); 69459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 70c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 711da177e4SLinus Torvalds skb->protocol = __constant_htons(ETH_P_AOE); 728babe8ccSEd Cashin skb_checksum_none_assert(skb); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds return skb; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static struct frame * 783a0c40d2SEd Cashin getframe_deferred(struct aoedev *d, u32 tag) 793a0c40d2SEd Cashin { 803a0c40d2SEd Cashin struct list_head *head, *pos, *nx; 813a0c40d2SEd Cashin struct frame *f; 823a0c40d2SEd Cashin 833a0c40d2SEd Cashin head = &d->rexmitq; 843a0c40d2SEd Cashin list_for_each_safe(pos, nx, head) { 853a0c40d2SEd Cashin f = list_entry(pos, struct frame, head); 863a0c40d2SEd Cashin if (f->tag == tag) { 873a0c40d2SEd Cashin list_del(pos); 883a0c40d2SEd Cashin return f; 893a0c40d2SEd Cashin } 903a0c40d2SEd Cashin } 913a0c40d2SEd Cashin return NULL; 923a0c40d2SEd Cashin } 933a0c40d2SEd Cashin 943a0c40d2SEd Cashin static struct frame * 9564a80f5aSEd Cashin getframe(struct aoedev *d, u32 tag) 961da177e4SLinus Torvalds { 97896831f5SEd Cashin struct frame *f; 98896831f5SEd Cashin struct list_head *head, *pos, *nx; 99896831f5SEd Cashin u32 n; 1001da177e4SLinus Torvalds 101896831f5SEd Cashin n = tag % NFACTIVE; 10264a80f5aSEd Cashin head = &d->factive[n]; 103896831f5SEd Cashin list_for_each_safe(pos, nx, head) { 104896831f5SEd Cashin f = list_entry(pos, struct frame, head); 105896831f5SEd Cashin if (f->tag == tag) { 106896831f5SEd Cashin list_del(pos); 1071da177e4SLinus Torvalds return f; 108896831f5SEd Cashin } 109896831f5SEd Cashin } 1101da177e4SLinus Torvalds return NULL; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* 1141da177e4SLinus Torvalds * Leave the top bit clear so we have tagspace for userland. 1151da177e4SLinus Torvalds * The bottom 16 bits are the xmit tick for rexmit/rttavg processing. 1161da177e4SLinus Torvalds * This driver reserves tag -1 to mean "unused frame." 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds static int 11964a80f5aSEd Cashin newtag(struct aoedev *d) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds register ulong n; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds n = jiffies & 0xffff; 124a6431e35SColin Ian King return n | (++d->lasttag & 0x7fff) << 16; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127896831f5SEd Cashin static u32 12868e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) 1291da177e4SLinus Torvalds { 13064a80f5aSEd Cashin u32 host_tag = newtag(d); 1311da177e4SLinus Torvalds 13268e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 13368e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 13463e9cc5dSecashin@coraid.com h->type = __constant_cpu_to_be16(ETH_P_AOE); 1351da177e4SLinus Torvalds h->verfl = AOE_HVER; 13663e9cc5dSecashin@coraid.com h->major = cpu_to_be16(d->aoemajor); 1371da177e4SLinus Torvalds h->minor = d->aoeminor; 1381da177e4SLinus Torvalds h->cmd = AOECMD_ATA; 13963e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(host_tag); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds return host_tag; 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 14419bf2635SEd L. Cashin static inline void 14519bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba) 14619bf2635SEd L. Cashin { 14719bf2635SEd L. Cashin ah->lba0 = lba; 14819bf2635SEd L. Cashin ah->lba1 = lba >>= 8; 14919bf2635SEd L. Cashin ah->lba2 = lba >>= 8; 15019bf2635SEd L. Cashin ah->lba3 = lba >>= 8; 15119bf2635SEd L. Cashin ah->lba4 = lba >>= 8; 15219bf2635SEd L. Cashin ah->lba5 = lba >>= 8; 15319bf2635SEd L. Cashin } 15419bf2635SEd L. Cashin 1553f0f0133SEd Cashin static struct aoeif * 15668e0d42fSEd L. Cashin ifrotate(struct aoetgt *t) 1571da177e4SLinus Torvalds { 1583f0f0133SEd Cashin struct aoeif *ifp; 1593f0f0133SEd Cashin 1603f0f0133SEd Cashin ifp = t->ifp; 1613f0f0133SEd Cashin ifp++; 1623f0f0133SEd Cashin if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL) 1633f0f0133SEd Cashin ifp = t->ifs; 1643f0f0133SEd Cashin if (ifp->nd == NULL) 1653f0f0133SEd Cashin return NULL; 1663f0f0133SEd Cashin return t->ifp = ifp; 16768e0d42fSEd L. Cashin } 16868e0d42fSEd L. Cashin 1699bb237b6SEd L. Cashin static void 1709bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb) 1719bb237b6SEd L. Cashin { 172e9bb8fb0SDavid S. Miller __skb_queue_tail(&d->skbpool, skb); 1739bb237b6SEd L. Cashin } 1749bb237b6SEd L. Cashin 1759bb237b6SEd L. Cashin static struct sk_buff * 1769bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d) 1779bb237b6SEd L. Cashin { 178e9bb8fb0SDavid S. Miller struct sk_buff *skb = skb_peek(&d->skbpool); 1799bb237b6SEd L. Cashin 1809bb237b6SEd L. Cashin if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 181e9bb8fb0SDavid S. Miller __skb_unlink(skb, &d->skbpool); 1829bb237b6SEd L. Cashin return skb; 1839bb237b6SEd L. Cashin } 184e9bb8fb0SDavid S. Miller if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 185e9bb8fb0SDavid S. Miller (skb = new_skb(ETH_ZLEN))) 1869bb237b6SEd L. Cashin return skb; 187e9bb8fb0SDavid S. Miller 1889bb237b6SEd L. Cashin return NULL; 1899bb237b6SEd L. Cashin } 1909bb237b6SEd L. Cashin 191896831f5SEd Cashin void 192896831f5SEd Cashin aoe_freetframe(struct frame *f) 19368e0d42fSEd L. Cashin { 194896831f5SEd Cashin struct aoetgt *t; 195896831f5SEd Cashin 196896831f5SEd Cashin t = f->t; 197896831f5SEd Cashin f->buf = NULL; 198feb261e2SKent Overstreet memset(&f->iter, 0, sizeof(f->iter)); 199896831f5SEd Cashin f->r_skb = NULL; 200bbb44e30SEd Cashin f->flags = 0; 201896831f5SEd Cashin list_add(&f->head, &t->ffree); 202896831f5SEd Cashin } 203896831f5SEd Cashin 204896831f5SEd Cashin static struct frame * 205896831f5SEd Cashin newtframe(struct aoedev *d, struct aoetgt *t) 206896831f5SEd Cashin { 207896831f5SEd Cashin struct frame *f; 2089bb237b6SEd L. Cashin struct sk_buff *skb; 209896831f5SEd Cashin struct list_head *pos; 210896831f5SEd Cashin 211896831f5SEd Cashin if (list_empty(&t->ffree)) { 212896831f5SEd Cashin if (t->falloc >= NSKBPOOLMAX*2) 213896831f5SEd Cashin return NULL; 214896831f5SEd Cashin f = kcalloc(1, sizeof(*f), GFP_ATOMIC); 215896831f5SEd Cashin if (f == NULL) 216896831f5SEd Cashin return NULL; 217896831f5SEd Cashin t->falloc++; 218896831f5SEd Cashin f->t = t; 219896831f5SEd Cashin } else { 220896831f5SEd Cashin pos = t->ffree.next; 221896831f5SEd Cashin list_del(pos); 222896831f5SEd Cashin f = list_entry(pos, struct frame, head); 223896831f5SEd Cashin } 224896831f5SEd Cashin 225896831f5SEd Cashin skb = f->skb; 226896831f5SEd Cashin if (skb == NULL) { 227896831f5SEd Cashin f->skb = skb = new_skb(ETH_ZLEN); 228896831f5SEd Cashin if (!skb) { 229896831f5SEd Cashin bail: aoe_freetframe(f); 230896831f5SEd Cashin return NULL; 231896831f5SEd Cashin } 232896831f5SEd Cashin } 233896831f5SEd Cashin 234896831f5SEd Cashin if (atomic_read(&skb_shinfo(skb)->dataref) != 1) { 235896831f5SEd Cashin skb = skb_pool_get(d); 236896831f5SEd Cashin if (skb == NULL) 237896831f5SEd Cashin goto bail; 238896831f5SEd Cashin skb_pool_put(d, f->skb); 239896831f5SEd Cashin f->skb = skb; 240896831f5SEd Cashin } 241896831f5SEd Cashin 242896831f5SEd Cashin skb->truesize -= skb->data_len; 243896831f5SEd Cashin skb_shinfo(skb)->nr_frags = skb->data_len = 0; 244896831f5SEd Cashin skb_trim(skb, 0); 245896831f5SEd Cashin return f; 246896831f5SEd Cashin } 247896831f5SEd Cashin 248896831f5SEd Cashin static struct frame * 249896831f5SEd Cashin newframe(struct aoedev *d) 250896831f5SEd Cashin { 251896831f5SEd Cashin struct frame *f; 252896831f5SEd Cashin struct aoetgt *t, **tt; 253896831f5SEd Cashin int totout = 0; 254bbb44e30SEd Cashin int use_tainted; 255bbb44e30SEd Cashin int has_untainted; 25668e0d42fSEd L. Cashin 25771114ec4SEd Cashin if (!d->targets || !d->targets[0]) { 25868e0d42fSEd L. Cashin printk(KERN_ERR "aoe: NULL TARGETS!\n"); 25968e0d42fSEd L. Cashin return NULL; 26068e0d42fSEd L. Cashin } 261896831f5SEd Cashin tt = d->tgt; /* last used target */ 262bbb44e30SEd Cashin for (use_tainted = 0, has_untainted = 0;;) { 263896831f5SEd Cashin tt++; 26471114ec4SEd Cashin if (tt >= &d->targets[d->ntargets] || !*tt) 265896831f5SEd Cashin tt = d->targets; 266896831f5SEd Cashin t = *tt; 267bbb44e30SEd Cashin if (!t->taint) { 268bbb44e30SEd Cashin has_untainted = 1; 269896831f5SEd Cashin totout += t->nout; 270bbb44e30SEd Cashin } 271896831f5SEd Cashin if (t->nout < t->maxout 272bbb44e30SEd Cashin && (use_tainted || !t->taint) 273896831f5SEd Cashin && t->ifp->nd) { 274896831f5SEd Cashin f = newtframe(d, t); 275896831f5SEd Cashin if (f) { 276896831f5SEd Cashin ifrotate(t); 2773f0f0133SEd Cashin d->tgt = tt; 27868e0d42fSEd L. Cashin return f; 27968e0d42fSEd L. Cashin } 2809bb237b6SEd L. Cashin } 281bbb44e30SEd Cashin if (tt == d->tgt) { /* we've looped and found nada */ 282bbb44e30SEd Cashin if (!use_tainted && !has_untainted) 283bbb44e30SEd Cashin use_tainted = 1; 284bbb44e30SEd Cashin else 2859bb237b6SEd L. Cashin break; 286896831f5SEd Cashin } 287bbb44e30SEd Cashin } 288896831f5SEd Cashin if (totout == 0) { 289896831f5SEd Cashin d->kicked++; 290896831f5SEd Cashin d->flags |= DEVFL_KICKME; 2919bb237b6SEd L. Cashin } 29268e0d42fSEd L. Cashin return NULL; 29368e0d42fSEd L. Cashin } 29468e0d42fSEd L. Cashin 2953d5b0605SEd Cashin static void 296feb261e2SKent Overstreet skb_fillup(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter) 2973d5b0605SEd Cashin { 2983d5b0605SEd Cashin int frag = 0; 299feb261e2SKent Overstreet struct bio_vec bv; 300feb261e2SKent Overstreet 301feb261e2SKent Overstreet __bio_for_each_segment(bv, bio, iter, iter) 302feb261e2SKent Overstreet skb_fill_page_desc(skb, frag++, bv.bv_page, 303feb261e2SKent Overstreet bv.bv_offset, bv.bv_len); 3043d5b0605SEd Cashin } 3053d5b0605SEd Cashin 306896831f5SEd Cashin static void 307896831f5SEd Cashin fhash(struct frame *f) 308896831f5SEd Cashin { 30964a80f5aSEd Cashin struct aoedev *d = f->t->d; 310896831f5SEd Cashin u32 n; 311896831f5SEd Cashin 312896831f5SEd Cashin n = f->tag % NFACTIVE; 31364a80f5aSEd Cashin list_add_tail(&f->head, &d->factive[n]); 314896831f5SEd Cashin } 315896831f5SEd Cashin 316bbb44e30SEd Cashin static void 317bbb44e30SEd Cashin ata_rw_frameinit(struct frame *f) 318bbb44e30SEd Cashin { 319bbb44e30SEd Cashin struct aoetgt *t; 320bbb44e30SEd Cashin struct aoe_hdr *h; 321bbb44e30SEd Cashin struct aoe_atahdr *ah; 322bbb44e30SEd Cashin struct sk_buff *skb; 323bbb44e30SEd Cashin char writebit, extbit; 324bbb44e30SEd Cashin 325bbb44e30SEd Cashin skb = f->skb; 326bbb44e30SEd Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 327bbb44e30SEd Cashin ah = (struct aoe_atahdr *) (h + 1); 328bbb44e30SEd Cashin skb_put(skb, sizeof(*h) + sizeof(*ah)); 329bbb44e30SEd Cashin memset(h, 0, skb->len); 330bbb44e30SEd Cashin 331bbb44e30SEd Cashin writebit = 0x10; 332bbb44e30SEd Cashin extbit = 0x4; 333bbb44e30SEd Cashin 334bbb44e30SEd Cashin t = f->t; 335bbb44e30SEd Cashin f->tag = aoehdr_atainit(t->d, t, h); 336bbb44e30SEd Cashin fhash(f); 337bbb44e30SEd Cashin t->nout++; 338bbb44e30SEd Cashin f->waited = 0; 339bbb44e30SEd Cashin f->waited_total = 0; 340bbb44e30SEd Cashin 341bbb44e30SEd Cashin /* set up ata header */ 342feb261e2SKent Overstreet ah->scnt = f->iter.bi_size >> 9; 343feb261e2SKent Overstreet put_lba(ah, f->iter.bi_sector); 344bbb44e30SEd Cashin if (t->d->flags & DEVFL_EXT) { 345bbb44e30SEd Cashin ah->aflags |= AOEAFL_EXT; 346bbb44e30SEd Cashin } else { 347bbb44e30SEd Cashin extbit = 0; 348bbb44e30SEd Cashin ah->lba3 &= 0x0f; 349bbb44e30SEd Cashin ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 350bbb44e30SEd Cashin } 351bbb44e30SEd Cashin if (f->buf && bio_data_dir(f->buf->bio) == WRITE) { 352feb261e2SKent Overstreet skb_fillup(skb, f->buf->bio, f->iter); 353bbb44e30SEd Cashin ah->aflags |= AOEAFL_WRITE; 354feb261e2SKent Overstreet skb->len += f->iter.bi_size; 355feb261e2SKent Overstreet skb->data_len = f->iter.bi_size; 356feb261e2SKent Overstreet skb->truesize += f->iter.bi_size; 357bbb44e30SEd Cashin t->wpkts++; 358bbb44e30SEd Cashin } else { 359bbb44e30SEd Cashin t->rpkts++; 360bbb44e30SEd Cashin writebit = 0; 361bbb44e30SEd Cashin } 362bbb44e30SEd Cashin 363bbb44e30SEd Cashin ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; 364*acc5103aSChun-Yi Lee dev_hold(t->ifp->nd); 365bbb44e30SEd Cashin skb->dev = t->ifp->nd; 366bbb44e30SEd Cashin } 367bbb44e30SEd Cashin 36868e0d42fSEd L. Cashin static int 36968e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d) 37068e0d42fSEd L. Cashin { 37168e0d42fSEd L. Cashin struct frame *f; 3721da177e4SLinus Torvalds struct buf *buf; 3731da177e4SLinus Torvalds struct sk_buff *skb; 37469cf2d85SEd Cashin struct sk_buff_head queue; 3751da177e4SLinus Torvalds 37669cf2d85SEd Cashin buf = nextbuf(d); 37769cf2d85SEd Cashin if (buf == NULL) 37869cf2d85SEd Cashin return 0; 379896831f5SEd Cashin f = newframe(d); 38068e0d42fSEd L. Cashin if (f == NULL) 38168e0d42fSEd L. Cashin return 0; 3823d5b0605SEd Cashin 3831da177e4SLinus Torvalds /* initialize the headers & frame */ 3841da177e4SLinus Torvalds f->buf = buf; 385feb261e2SKent Overstreet f->iter = buf->iter; 386feb261e2SKent Overstreet f->iter.bi_size = min_t(unsigned long, 387feb261e2SKent Overstreet d->maxbcnt ?: DEFAULTBCNT, 388feb261e2SKent Overstreet f->iter.bi_size); 389feb261e2SKent Overstreet bio_advance_iter(buf->bio, &buf->iter, f->iter.bi_size); 390feb261e2SKent Overstreet 391feb261e2SKent Overstreet if (!buf->iter.bi_size) 392feb261e2SKent Overstreet d->ip.buf = NULL; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* mark all tracking fields and load out */ 3951da177e4SLinus Torvalds buf->nframesout += 1; 396feb261e2SKent Overstreet 397feb261e2SKent Overstreet ata_rw_frameinit(f); 3981da177e4SLinus Torvalds 399bbb44e30SEd Cashin skb = skb_clone(f->skb, GFP_ATOMIC); 40069cf2d85SEd Cashin if (skb) { 40185cf955dSTina Ruchandani f->sent = ktime_get(); 40269cf2d85SEd Cashin __skb_queue_head_init(&queue); 40369cf2d85SEd Cashin __skb_queue_tail(&queue, skb); 40469cf2d85SEd Cashin aoenet_xmit(&queue); 405*acc5103aSChun-Yi Lee } else { 406*acc5103aSChun-Yi Lee dev_put(f->t->ifp->nd); 40769cf2d85SEd Cashin } 40868e0d42fSEd L. Cashin return 1; 40968e0d42fSEd L. Cashin } 4101da177e4SLinus Torvalds 4113ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function, 4123ae1c24eSEd L. Cashin * transmitting the packets later, when interrupts are on 4133ae1c24eSEd L. Cashin */ 414e9bb8fb0SDavid S. Miller static void 415e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 4163ae1c24eSEd L. Cashin { 4173ae1c24eSEd L. Cashin struct aoe_hdr *h; 4183ae1c24eSEd L. Cashin struct aoe_cfghdr *ch; 419e9bb8fb0SDavid S. Miller struct sk_buff *skb; 4203ae1c24eSEd L. Cashin struct net_device *ifp; 4213ae1c24eSEd L. Cashin 422840a185dSEric Dumazet rcu_read_lock(); 423840a185dSEric Dumazet for_each_netdev_rcu(&init_net, ifp) { 4243ae1c24eSEd L. Cashin dev_hold(ifp); 425eb48680bSChun-Yi Lee if (!is_aoe_netif(ifp)) { 426eb48680bSChun-Yi Lee dev_put(ifp); 427eb48680bSChun-Yi Lee continue; 428eb48680bSChun-Yi Lee } 4293ae1c24eSEd L. Cashin 430e407a7f6SEd L. Cashin skb = new_skb(sizeof *h + sizeof *ch); 4313ae1c24eSEd L. Cashin if (skb == NULL) { 432a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: skb alloc failure\n"); 433eb48680bSChun-Yi Lee dev_put(ifp); 434eb48680bSChun-Yi Lee continue; 4353ae1c24eSEd L. Cashin } 43619900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ch); 437e407a7f6SEd L. Cashin skb->dev = ifp; 438e9bb8fb0SDavid S. Miller __skb_queue_tail(queue, skb); 439abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 4403ae1c24eSEd L. Cashin memset(h, 0, sizeof *h + sizeof *ch); 4413ae1c24eSEd L. Cashin 4423ae1c24eSEd L. Cashin memset(h->dst, 0xff, sizeof h->dst); 4433ae1c24eSEd L. Cashin memcpy(h->src, ifp->dev_addr, sizeof h->src); 4443ae1c24eSEd L. Cashin h->type = __constant_cpu_to_be16(ETH_P_AOE); 4453ae1c24eSEd L. Cashin h->verfl = AOE_HVER; 4463ae1c24eSEd L. Cashin h->major = cpu_to_be16(aoemajor); 4473ae1c24eSEd L. Cashin h->minor = aoeminor; 4483ae1c24eSEd L. Cashin h->cmd = AOECMD_CFG; 4493ae1c24eSEd L. Cashin } 450840a185dSEric Dumazet rcu_read_unlock(); 4513ae1c24eSEd L. Cashin } 4523ae1c24eSEd L. Cashin 4531da177e4SLinus Torvalds static void 454896831f5SEd Cashin resend(struct aoedev *d, struct frame *f) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds struct sk_buff *skb; 45769cf2d85SEd Cashin struct sk_buff_head queue; 4581da177e4SLinus Torvalds struct aoe_hdr *h; 459896831f5SEd Cashin struct aoetgt *t; 4601da177e4SLinus Torvalds char buf[128]; 4611da177e4SLinus Torvalds u32 n; 4621da177e4SLinus Torvalds 463896831f5SEd Cashin t = f->t; 46464a80f5aSEd Cashin n = newtag(d); 465e407a7f6SEd L. Cashin skb = f->skb; 4663f0f0133SEd Cashin if (ifrotate(t) == NULL) { 4673f0f0133SEd Cashin /* probably can't happen, but set it up to fail anyway */ 4683f0f0133SEd Cashin pr_info("aoe: resend: no interfaces to rotate to.\n"); 4693f0f0133SEd Cashin ktcomplete(f, NULL); 4703f0f0133SEd Cashin return; 4713f0f0133SEd Cashin } 472abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 47368e0d42fSEd L. Cashin 474bbb44e30SEd Cashin if (!(f->flags & FFL_PROBE)) { 475bbb44e30SEd Cashin snprintf(buf, sizeof(buf), 476411c41eeSHarvey Harrison "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", 477bbb44e30SEd Cashin "retransmit", d->aoemajor, d->aoeminor, 478bbb44e30SEd Cashin f->tag, jiffies, n, 479411c41eeSHarvey Harrison h->src, h->dst, t->nout); 48068e0d42fSEd L. Cashin aoechr_error(buf); 481bbb44e30SEd Cashin } 48268e0d42fSEd L. Cashin 4831da177e4SLinus Torvalds f->tag = n; 484896831f5SEd Cashin fhash(f); 48563e9cc5dSecashin@coraid.com h->tag = cpu_to_be32(n); 48668e0d42fSEd L. Cashin memcpy(h->dst, t->addr, sizeof h->dst); 48768e0d42fSEd L. Cashin memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 4881da177e4SLinus Torvalds 489*acc5103aSChun-Yi Lee dev_hold(t->ifp->nd); 49068e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 4914f51dc5eSEd L. Cashin skb = skb_clone(skb, GFP_ATOMIC); 492*acc5103aSChun-Yi Lee if (skb == NULL) { 493*acc5103aSChun-Yi Lee dev_put(t->ifp->nd); 4944f51dc5eSEd L. Cashin return; 495*acc5103aSChun-Yi Lee } 49685cf955dSTina Ruchandani f->sent = ktime_get(); 49769cf2d85SEd Cashin __skb_queue_head_init(&queue); 49869cf2d85SEd Cashin __skb_queue_tail(&queue, skb); 49969cf2d85SEd Cashin aoenet_xmit(&queue); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds static int 5035f0c9c48SEd Cashin tsince_hr(struct frame *f) 5045f0c9c48SEd Cashin { 50585cf955dSTina Ruchandani u64 delta = ktime_to_ns(ktime_sub(ktime_get(), f->sent)); 5065f0c9c48SEd Cashin 50785cf955dSTina Ruchandani /* delta is normally under 4.2 seconds, avoid 64-bit division */ 50885cf955dSTina Ruchandani if (likely(delta <= UINT_MAX)) 50985cf955dSTina Ruchandani return (u32)delta / NSEC_PER_USEC; 5105f0c9c48SEd Cashin 51185cf955dSTina Ruchandani /* avoid overflow after 71 minutes */ 51285cf955dSTina Ruchandani if (delta > ((u64)INT_MAX * NSEC_PER_USEC)) 51385cf955dSTina Ruchandani return INT_MAX; 5145f0c9c48SEd Cashin 51585cf955dSTina Ruchandani return div_u64(delta, NSEC_PER_USEC); 5165f0c9c48SEd Cashin } 5175f0c9c48SEd Cashin 5185f0c9c48SEd Cashin static int 519896831f5SEd Cashin tsince(u32 tag) 5201da177e4SLinus Torvalds { 5211da177e4SLinus Torvalds int n; 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds n = jiffies & 0xffff; 5241da177e4SLinus Torvalds n -= tag & 0xffff; 5251da177e4SLinus Torvalds if (n < 0) 5261da177e4SLinus Torvalds n += 1<<16; 5275f0c9c48SEd Cashin return jiffies_to_usecs(n + 1); 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 53068e0d42fSEd L. Cashin static struct aoeif * 53168e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd) 53268e0d42fSEd L. Cashin { 53368e0d42fSEd L. Cashin struct aoeif *p, *e; 53468e0d42fSEd L. Cashin 53568e0d42fSEd L. Cashin p = t->ifs; 53668e0d42fSEd L. Cashin e = p + NAOEIFS; 53768e0d42fSEd L. Cashin for (; p < e; p++) 53868e0d42fSEd L. Cashin if (p->nd == nd) 53968e0d42fSEd L. Cashin return p; 54068e0d42fSEd L. Cashin return NULL; 54168e0d42fSEd L. Cashin } 54268e0d42fSEd L. Cashin 54368e0d42fSEd L. Cashin static void 54468e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp) 54568e0d42fSEd L. Cashin { 54668e0d42fSEd L. Cashin struct aoeif *e; 5471b86fda9SEd Cashin struct net_device *nd; 54868e0d42fSEd L. Cashin ulong n; 54968e0d42fSEd L. Cashin 5501b86fda9SEd Cashin nd = ifp->nd; 55168e0d42fSEd L. Cashin e = t->ifs + NAOEIFS - 1; 55268e0d42fSEd L. Cashin n = (e - ifp) * sizeof *ifp; 55368e0d42fSEd L. Cashin memmove(ifp, ifp+1, n); 55468e0d42fSEd L. Cashin e->nd = NULL; 5551b86fda9SEd Cashin dev_put(nd); 55668e0d42fSEd L. Cashin } 55768e0d42fSEd L. Cashin 5583fc9b032SEd Cashin static struct frame * 559bbb44e30SEd Cashin reassign_frame(struct frame *f) 56068e0d42fSEd L. Cashin { 5613fc9b032SEd Cashin struct frame *nf; 56268e0d42fSEd L. Cashin struct sk_buff *skb; 56368e0d42fSEd L. Cashin 5643fc9b032SEd Cashin nf = newframe(f->t->d); 56568e0d42fSEd L. Cashin if (!nf) 5663fc9b032SEd Cashin return NULL; 567bbb44e30SEd Cashin if (nf->t == f->t) { 568bbb44e30SEd Cashin aoe_freetframe(nf); 569bbb44e30SEd Cashin return NULL; 570bbb44e30SEd Cashin } 571896831f5SEd Cashin 57268e0d42fSEd L. Cashin skb = nf->skb; 573896831f5SEd Cashin nf->skb = f->skb; 574896831f5SEd Cashin nf->buf = f->buf; 575feb261e2SKent Overstreet nf->iter = f->iter; 57668e0d42fSEd L. Cashin nf->waited = 0; 5773fc9b032SEd Cashin nf->waited_total = f->waited_total; 5783fc9b032SEd Cashin nf->sent = f->sent; 579896831f5SEd Cashin f->skb = skb; 5803fc9b032SEd Cashin 5813fc9b032SEd Cashin return nf; 5823fc9b032SEd Cashin } 5833fc9b032SEd Cashin 584bbb44e30SEd Cashin static void 585bbb44e30SEd Cashin probe(struct aoetgt *t) 5863fc9b032SEd Cashin { 587bbb44e30SEd Cashin struct aoedev *d; 588bbb44e30SEd Cashin struct frame *f; 589bbb44e30SEd Cashin struct sk_buff *skb; 590bbb44e30SEd Cashin struct sk_buff_head queue; 591bbb44e30SEd Cashin size_t n, m; 592bbb44e30SEd Cashin int frag; 5933fc9b032SEd Cashin 594bbb44e30SEd Cashin d = t->d; 595bbb44e30SEd Cashin f = newtframe(d, t); 596bbb44e30SEd Cashin if (!f) { 597bbb44e30SEd Cashin pr_err("%s %pm for e%ld.%d: %s\n", 598bbb44e30SEd Cashin "aoe: cannot probe remote address", 599bbb44e30SEd Cashin t->addr, 600bbb44e30SEd Cashin (long) d->aoemajor, d->aoeminor, 601bbb44e30SEd Cashin "no frame available"); 602bbb44e30SEd Cashin return; 603bbb44e30SEd Cashin } 604bbb44e30SEd Cashin f->flags |= FFL_PROBE; 605bbb44e30SEd Cashin ifrotate(t); 606feb261e2SKent Overstreet f->iter.bi_size = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT; 607bbb44e30SEd Cashin ata_rw_frameinit(f); 608bbb44e30SEd Cashin skb = f->skb; 609feb261e2SKent Overstreet for (frag = 0, n = f->iter.bi_size; n > 0; ++frag, n -= m) { 610bbb44e30SEd Cashin if (n < PAGE_SIZE) 611bbb44e30SEd Cashin m = n; 612bbb44e30SEd Cashin else 613bbb44e30SEd Cashin m = PAGE_SIZE; 614bbb44e30SEd Cashin skb_fill_page_desc(skb, frag, empty_page, 0, m); 615bbb44e30SEd Cashin } 616feb261e2SKent Overstreet skb->len += f->iter.bi_size; 617feb261e2SKent Overstreet skb->data_len = f->iter.bi_size; 618feb261e2SKent Overstreet skb->truesize += f->iter.bi_size; 619bbb44e30SEd Cashin 620bbb44e30SEd Cashin skb = skb_clone(f->skb, GFP_ATOMIC); 621bbb44e30SEd Cashin if (skb) { 62285cf955dSTina Ruchandani f->sent = ktime_get(); 623bbb44e30SEd Cashin __skb_queue_head_init(&queue); 624bbb44e30SEd Cashin __skb_queue_tail(&queue, skb); 625bbb44e30SEd Cashin aoenet_xmit(&queue); 626*acc5103aSChun-Yi Lee } else { 627*acc5103aSChun-Yi Lee dev_put(f->t->ifp->nd); 628896831f5SEd Cashin } 62968e0d42fSEd L. Cashin } 630bbb44e30SEd Cashin 631bbb44e30SEd Cashin static long 632bbb44e30SEd Cashin rto(struct aoedev *d) 633bbb44e30SEd Cashin { 634bbb44e30SEd Cashin long t; 635bbb44e30SEd Cashin 636bbb44e30SEd Cashin t = 2 * d->rttavg >> RTTSCALE; 637bbb44e30SEd Cashin t += 8 * d->rttdev >> RTTDSCALE; 638bbb44e30SEd Cashin if (t == 0) 639bbb44e30SEd Cashin t = 1; 640bbb44e30SEd Cashin 641bbb44e30SEd Cashin return t; 64268e0d42fSEd L. Cashin } 64368e0d42fSEd L. Cashin 6441da177e4SLinus Torvalds static void 6453a0c40d2SEd Cashin rexmit_deferred(struct aoedev *d) 6463a0c40d2SEd Cashin { 6473a0c40d2SEd Cashin struct aoetgt *t; 6483a0c40d2SEd Cashin struct frame *f; 649bbb44e30SEd Cashin struct frame *nf; 6503a0c40d2SEd Cashin struct list_head *pos, *nx, *head; 6513fc9b032SEd Cashin int since; 652bbb44e30SEd Cashin int untainted; 653bbb44e30SEd Cashin 654bbb44e30SEd Cashin count_targets(d, &untainted); 6553a0c40d2SEd Cashin 6563a0c40d2SEd Cashin head = &d->rexmitq; 6573a0c40d2SEd Cashin list_for_each_safe(pos, nx, head) { 6583a0c40d2SEd Cashin f = list_entry(pos, struct frame, head); 6593a0c40d2SEd Cashin t = f->t; 660bbb44e30SEd Cashin if (t->taint) { 661bbb44e30SEd Cashin if (!(f->flags & FFL_PROBE)) { 662bbb44e30SEd Cashin nf = reassign_frame(f); 663bbb44e30SEd Cashin if (nf) { 664bbb44e30SEd Cashin if (t->nout_probes == 0 665bbb44e30SEd Cashin && untainted > 0) { 666bbb44e30SEd Cashin probe(t); 667bbb44e30SEd Cashin t->nout_probes++; 668bbb44e30SEd Cashin } 669bbb44e30SEd Cashin list_replace(&f->head, &nf->head); 670bbb44e30SEd Cashin pos = &nf->head; 671bbb44e30SEd Cashin aoe_freetframe(f); 672bbb44e30SEd Cashin f = nf; 673bbb44e30SEd Cashin t = f->t; 674bbb44e30SEd Cashin } 675bbb44e30SEd Cashin } else if (untainted < 1) { 676bbb44e30SEd Cashin /* don't probe w/o other untainted aoetgts */ 677bbb44e30SEd Cashin goto stop_probe; 678bbb44e30SEd Cashin } else if (tsince_hr(f) < t->taint * rto(d)) { 679bbb44e30SEd Cashin /* reprobe slowly when taint is high */ 680bbb44e30SEd Cashin continue; 681bbb44e30SEd Cashin } 682bbb44e30SEd Cashin } else if (f->flags & FFL_PROBE) { 683bbb44e30SEd Cashin stop_probe: /* don't probe untainted aoetgts */ 684bbb44e30SEd Cashin list_del(pos); 685bbb44e30SEd Cashin aoe_freetframe(f); 686bbb44e30SEd Cashin /* leaving d->kicked, because this is routine */ 687bbb44e30SEd Cashin f->t->d->flags |= DEVFL_KICKME; 688bbb44e30SEd Cashin continue; 689bbb44e30SEd Cashin } 6903a0c40d2SEd Cashin if (t->nout >= t->maxout) 6913a0c40d2SEd Cashin continue; 6923a0c40d2SEd Cashin list_del(pos); 6933a0c40d2SEd Cashin t->nout++; 694bbb44e30SEd Cashin if (f->flags & FFL_PROBE) 695bbb44e30SEd Cashin t->nout_probes++; 6963fc9b032SEd Cashin since = tsince_hr(f); 6973fc9b032SEd Cashin f->waited += since; 6983fc9b032SEd Cashin f->waited_total += since; 6993a0c40d2SEd Cashin resend(d, f); 7003a0c40d2SEd Cashin } 7013a0c40d2SEd Cashin } 7023a0c40d2SEd Cashin 703bbb44e30SEd Cashin /* An aoetgt accumulates demerits quickly, and successful 704bbb44e30SEd Cashin * probing redeems the aoetgt slowly. 705bbb44e30SEd Cashin */ 706bbb44e30SEd Cashin static void 707bbb44e30SEd Cashin scorn(struct aoetgt *t) 708bbb44e30SEd Cashin { 709bbb44e30SEd Cashin int n; 710bbb44e30SEd Cashin 711bbb44e30SEd Cashin n = t->taint++; 712bbb44e30SEd Cashin t->taint += t->taint * 2; 713bbb44e30SEd Cashin if (n > t->taint) 714bbb44e30SEd Cashin t->taint = n; 715bbb44e30SEd Cashin if (t->taint > MAX_TAINT) 716bbb44e30SEd Cashin t->taint = MAX_TAINT; 717bbb44e30SEd Cashin } 718bbb44e30SEd Cashin 719bbb44e30SEd Cashin static int 720bbb44e30SEd Cashin count_targets(struct aoedev *d, int *untainted) 721bbb44e30SEd Cashin { 722bbb44e30SEd Cashin int i, good; 723bbb44e30SEd Cashin 724bbb44e30SEd Cashin for (i = good = 0; i < d->ntargets && d->targets[i]; ++i) 725bbb44e30SEd Cashin if (d->targets[i]->taint == 0) 726bbb44e30SEd Cashin good++; 727bbb44e30SEd Cashin 728bbb44e30SEd Cashin if (untainted) 729bbb44e30SEd Cashin *untainted = good; 730bbb44e30SEd Cashin return i; 731bbb44e30SEd Cashin } 732bbb44e30SEd Cashin 7333a0c40d2SEd Cashin static void 7340e0cc9dfSKees Cook rexmit_timer(struct timer_list *timer) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds struct aoedev *d; 7373a0c40d2SEd Cashin struct aoetgt *t; 73868e0d42fSEd L. Cashin struct aoeif *ifp; 739896831f5SEd Cashin struct frame *f; 740896831f5SEd Cashin struct list_head *head, *pos, *nx; 741896831f5SEd Cashin LIST_HEAD(flist); 7421da177e4SLinus Torvalds register long timeout; 7431da177e4SLinus Torvalds ulong flags, n; 744896831f5SEd Cashin int i; 745bbb44e30SEd Cashin int utgts; /* number of aoetgt descriptors (not slots) */ 7463fc9b032SEd Cashin int since; 7471da177e4SLinus Torvalds 7480e0cc9dfSKees Cook d = from_timer(d, timer, timer); 7491da177e4SLinus Torvalds 7500d555ecfSEd Cashin spin_lock_irqsave(&d->lock, flags); 7510d555ecfSEd Cashin 7523a0c40d2SEd Cashin /* timeout based on observed timings and variations */ 753bbb44e30SEd Cashin timeout = rto(d); 754bbb44e30SEd Cashin 755bbb44e30SEd Cashin utgts = count_targets(d, NULL); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (d->flags & DEVFL_TKILL) { 7581c6f3fcaSEd L. Cashin spin_unlock_irqrestore(&d->lock, flags); 7591da177e4SLinus Torvalds return; 7601da177e4SLinus Torvalds } 761896831f5SEd Cashin 762896831f5SEd Cashin /* collect all frames to rexmit into flist */ 763896831f5SEd Cashin for (i = 0; i < NFACTIVE; i++) { 76464a80f5aSEd Cashin head = &d->factive[i]; 765896831f5SEd Cashin list_for_each_safe(pos, nx, head) { 766896831f5SEd Cashin f = list_entry(pos, struct frame, head); 7675f0c9c48SEd Cashin if (tsince_hr(f) < timeout) 76864a80f5aSEd Cashin break; /* end of expired frames */ 769896831f5SEd Cashin /* move to flist for later processing */ 770896831f5SEd Cashin list_move_tail(pos, &flist); 771896831f5SEd Cashin } 772896831f5SEd Cashin } 77369cf2d85SEd Cashin 774896831f5SEd Cashin /* process expired frames */ 775896831f5SEd Cashin while (!list_empty(&flist)) { 776896831f5SEd Cashin pos = flist.next; 777896831f5SEd Cashin f = list_entry(pos, struct frame, head); 7783fc9b032SEd Cashin since = tsince_hr(f); 7793fc9b032SEd Cashin n = f->waited_total + since; 7805f0c9c48SEd Cashin n /= USEC_PER_SEC; 781c450ba0fSEd Cashin if (aoe_deadsecs 782c450ba0fSEd Cashin && n > aoe_deadsecs 783c450ba0fSEd Cashin && !(f->flags & FFL_PROBE)) { 784896831f5SEd Cashin /* Waited too long. Device failure. 785896831f5SEd Cashin * Hang all frames on first hash bucket for downdev 786896831f5SEd Cashin * to clean up. 787896831f5SEd Cashin */ 78864a80f5aSEd Cashin list_splice(&flist, &d->factive[0]); 7891da177e4SLinus Torvalds aoedev_downdev(d); 7903a0c40d2SEd Cashin goto out; 7911da177e4SLinus Torvalds } 79268e0d42fSEd L. Cashin 793896831f5SEd Cashin t = f->t; 794bbb44e30SEd Cashin n = f->waited + since; 795bbb44e30SEd Cashin n /= USEC_PER_SEC; 796bbb44e30SEd Cashin if (aoe_deadsecs && utgts > 0 797bbb44e30SEd Cashin && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS)) 798bbb44e30SEd Cashin scorn(t); /* avoid this target */ 799d54d35acSEd Cashin 8003a0c40d2SEd Cashin if (t->maxout != 1) { 8013a0c40d2SEd Cashin t->ssthresh = t->maxout / 2; 8023a0c40d2SEd Cashin t->maxout = 1; 80368e0d42fSEd L. Cashin } 80468e0d42fSEd L. Cashin 805bbb44e30SEd Cashin if (f->flags & FFL_PROBE) { 806bbb44e30SEd Cashin t->nout_probes--; 807bbb44e30SEd Cashin } else { 80868e0d42fSEd L. Cashin ifp = getif(t, f->skb->dev); 80968e0d42fSEd L. Cashin if (ifp && ++ifp->lost > (t->nframes << 1) 81068e0d42fSEd L. Cashin && (ifp != t->ifs || t->ifs[1].nd)) { 81168e0d42fSEd L. Cashin ejectif(t, ifp); 81268e0d42fSEd L. Cashin ifp = NULL; 81368e0d42fSEd L. Cashin } 814bbb44e30SEd Cashin } 8153a0c40d2SEd Cashin list_move_tail(pos, &d->rexmitq); 8163a0c40d2SEd Cashin t->nout--; 8171da177e4SLinus Torvalds } 8183a0c40d2SEd Cashin rexmit_deferred(d); 81968e0d42fSEd L. Cashin 8203a0c40d2SEd Cashin out: 821bbb44e30SEd Cashin if ((d->flags & DEVFL_KICKME) && d->blkq) { 8224f51dc5eSEd L. Cashin d->flags &= ~DEVFL_KICKME; 8233582dd29SJens Axboe blk_mq_run_hw_queues(d->blkq, true); 8244f51dc5eSEd L. Cashin } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds d->timer.expires = jiffies + TIMERTICK; 8271da177e4SLinus Torvalds add_timer(&d->timer); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 83069cf2d85SEd Cashin } 8311da177e4SLinus Torvalds 83269cf2d85SEd Cashin static void 83369cf2d85SEd Cashin bufinit(struct buf *buf, struct request *rq, struct bio *bio) 83469cf2d85SEd Cashin { 83569cf2d85SEd Cashin memset(buf, 0, sizeof(*buf)); 83669cf2d85SEd Cashin buf->rq = rq; 83769cf2d85SEd Cashin buf->bio = bio; 838feb261e2SKent Overstreet buf->iter = bio->bi_iter; 83969cf2d85SEd Cashin } 84069cf2d85SEd Cashin 84169cf2d85SEd Cashin static struct buf * 84269cf2d85SEd Cashin nextbuf(struct aoedev *d) 84369cf2d85SEd Cashin { 84469cf2d85SEd Cashin struct request *rq; 84569cf2d85SEd Cashin struct request_queue *q; 84661e7712eSChristoph Hellwig struct aoe_req *req; 84769cf2d85SEd Cashin struct buf *buf; 84869cf2d85SEd Cashin struct bio *bio; 84969cf2d85SEd Cashin 85069cf2d85SEd Cashin q = d->blkq; 85169cf2d85SEd Cashin if (q == NULL) 85269cf2d85SEd Cashin return NULL; /* initializing */ 85369cf2d85SEd Cashin if (d->ip.buf) 85469cf2d85SEd Cashin return d->ip.buf; 85569cf2d85SEd Cashin rq = d->ip.rq; 85669cf2d85SEd Cashin if (rq == NULL) { 8573582dd29SJens Axboe rq = list_first_entry_or_null(&d->rq_list, struct request, 8583582dd29SJens Axboe queuelist); 85969cf2d85SEd Cashin if (rq == NULL) 86069cf2d85SEd Cashin return NULL; 8613582dd29SJens Axboe list_del_init(&rq->queuelist); 8623582dd29SJens Axboe blk_mq_start_request(rq); 86369cf2d85SEd Cashin d->ip.rq = rq; 86469cf2d85SEd Cashin d->ip.nxbio = rq->bio; 86561e7712eSChristoph Hellwig 86661e7712eSChristoph Hellwig req = blk_mq_rq_to_pdu(rq); 86761e7712eSChristoph Hellwig req->nr_bios = 0; 86861e7712eSChristoph Hellwig __rq_for_each_bio(bio, rq) 86961e7712eSChristoph Hellwig req->nr_bios++; 87069cf2d85SEd Cashin } 87169cf2d85SEd Cashin buf = mempool_alloc(d->bufpool, GFP_ATOMIC); 87269cf2d85SEd Cashin if (buf == NULL) { 87369cf2d85SEd Cashin pr_err("aoe: nextbuf: unable to mempool_alloc!\n"); 87469cf2d85SEd Cashin return NULL; 87569cf2d85SEd Cashin } 87669cf2d85SEd Cashin bio = d->ip.nxbio; 87769cf2d85SEd Cashin bufinit(buf, rq, bio); 87869cf2d85SEd Cashin bio = bio->bi_next; 87969cf2d85SEd Cashin d->ip.nxbio = bio; 88069cf2d85SEd Cashin if (bio == NULL) 88169cf2d85SEd Cashin d->ip.rq = NULL; 88269cf2d85SEd Cashin return d->ip.buf = buf; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 88568e0d42fSEd L. Cashin /* enters with d->lock held */ 88668e0d42fSEd L. Cashin void 88768e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d) 88868e0d42fSEd L. Cashin { 8893a0c40d2SEd Cashin rexmit_deferred(d); 89069cf2d85SEd Cashin while (aoecmd_ata_rw(d)) 89169cf2d85SEd Cashin ; 89268e0d42fSEd L. Cashin } 89368e0d42fSEd L. Cashin 8943ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK 8953ae1c24eSEd L. Cashin */ 8963ae1c24eSEd L. Cashin void 897c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work) 8983ae1c24eSEd L. Cashin { 899c4028958SDavid Howells struct aoedev *d = container_of(work, struct aoedev, work); 9003ae1c24eSEd L. Cashin 9013ae1c24eSEd L. Cashin if (d->flags & DEVFL_GDALLOC) 9023ae1c24eSEd L. Cashin aoeblk_gdalloc(d); 9033ae1c24eSEd L. Cashin 9043ae1c24eSEd L. Cashin if (d->flags & DEVFL_NEWSIZE) { 9058a6f7bbfSChristoph Hellwig set_capacity_and_notify(d->gd, d->ssize); 9068a6f7bbfSChristoph Hellwig 907b21faa25SEd Cashin spin_lock_irq(&d->lock); 9083ae1c24eSEd L. Cashin d->flags |= DEVFL_UP; 9093ae1c24eSEd L. Cashin d->flags &= ~DEVFL_NEWSIZE; 910b21faa25SEd Cashin spin_unlock_irq(&d->lock); 9113ae1c24eSEd L. Cashin } 9123ae1c24eSEd L. Cashin } 9133ae1c24eSEd L. Cashin 9141da177e4SLinus Torvalds static void 915667be1e7SEd Cashin ata_ident_fixstring(u16 *id, int ns) 916667be1e7SEd Cashin { 917667be1e7SEd Cashin u16 s; 918667be1e7SEd Cashin 919667be1e7SEd Cashin while (ns-- > 0) { 920667be1e7SEd Cashin s = *id; 921667be1e7SEd Cashin *id++ = s >> 8 | s << 8; 922667be1e7SEd Cashin } 923667be1e7SEd Cashin } 924667be1e7SEd Cashin 925667be1e7SEd Cashin static void 92668e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 9271da177e4SLinus Torvalds { 9281da177e4SLinus Torvalds u64 ssize; 9291da177e4SLinus Torvalds u16 n; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds /* word 83: command set supported */ 932f885f8d1SHarvey Harrison n = get_unaligned_le16(&id[83 << 1]); 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* word 86: command set/feature enabled */ 935f885f8d1SHarvey Harrison n |= get_unaligned_le16(&id[86 << 1]); 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (n & (1<<10)) { /* bit 10: LBA 48 */ 9381da177e4SLinus Torvalds d->flags |= DEVFL_EXT; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds /* word 100: number lba48 sectors */ 941f885f8d1SHarvey Harrison ssize = get_unaligned_le64(&id[100 << 1]); 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* set as in ide-disk.c:init_idedisk_capacity */ 9441da177e4SLinus Torvalds d->geo.cylinders = ssize; 9451da177e4SLinus Torvalds d->geo.cylinders /= (255 * 63); 9461da177e4SLinus Torvalds d->geo.heads = 255; 9471da177e4SLinus Torvalds d->geo.sectors = 63; 9481da177e4SLinus Torvalds } else { 9491da177e4SLinus Torvalds d->flags &= ~DEVFL_EXT; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /* number lba28 sectors */ 952f885f8d1SHarvey Harrison ssize = get_unaligned_le32(&id[60 << 1]); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* NOTE: obsolete in ATA 6 */ 955f885f8d1SHarvey Harrison d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 956f885f8d1SHarvey Harrison d->geo.heads = get_unaligned_le16(&id[55 << 1]); 957f885f8d1SHarvey Harrison d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 9581da177e4SLinus Torvalds } 9593ae1c24eSEd L. Cashin 960667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */ 961667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */ 962667be1e7SEd Cashin ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */ 963667be1e7SEd Cashin memcpy(d->ident, id, sizeof(d->ident)); 964667be1e7SEd Cashin 9653ae1c24eSEd L. Cashin if (d->ssize != ssize) 9661d75981aSEd L. Cashin printk(KERN_INFO 967411c41eeSHarvey Harrison "aoe: %pm e%ld.%d v%04x has %llu sectors\n", 968411c41eeSHarvey Harrison t->addr, 9693ae1c24eSEd L. Cashin d->aoemajor, d->aoeminor, 9703ae1c24eSEd L. Cashin d->fw_ver, (long long)ssize); 9711da177e4SLinus Torvalds d->ssize = ssize; 9721da177e4SLinus Torvalds d->geo.start = 0; 9736b9699bbSEd L. Cashin if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 9746b9699bbSEd L. Cashin return; 9758a6f7bbfSChristoph Hellwig if (d->gd != NULL) 9763ae1c24eSEd L. Cashin d->flags |= DEVFL_NEWSIZE; 9778a6f7bbfSChristoph Hellwig else 9783ae1c24eSEd L. Cashin d->flags |= DEVFL_GDALLOC; 9790b8d7622STetsuo Handa queue_work(aoe_wq, &d->work); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds static void 9833a0c40d2SEd Cashin calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds register long n; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds n = rtt; 9881da177e4SLinus Torvalds 9893a0c40d2SEd Cashin /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */ 9903a0c40d2SEd Cashin n -= d->rttavg >> RTTSCALE; 9913a0c40d2SEd Cashin d->rttavg += n; 9923a0c40d2SEd Cashin if (n < 0) 9933a0c40d2SEd Cashin n = -n; 9943a0c40d2SEd Cashin n -= d->rttdev >> RTTDSCALE; 9953a0c40d2SEd Cashin d->rttdev += n; 9963a0c40d2SEd Cashin 9973a0c40d2SEd Cashin if (!t || t->maxout >= t->nframes) 9983a0c40d2SEd Cashin return; 9993a0c40d2SEd Cashin if (t->maxout < t->ssthresh) 10003a0c40d2SEd Cashin t->maxout += 1; 10013a0c40d2SEd Cashin else if (t->nout == t->maxout && t->next_cwnd-- == 0) { 10023a0c40d2SEd Cashin t->maxout += 1; 10033a0c40d2SEd Cashin t->next_cwnd = t->maxout; 10043a0c40d2SEd Cashin } 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 100768e0d42fSEd L. Cashin static struct aoetgt * 100868e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr) 100968e0d42fSEd L. Cashin { 101068e0d42fSEd L. Cashin struct aoetgt **t, **e; 101168e0d42fSEd L. Cashin 101268e0d42fSEd L. Cashin t = d->targets; 101371114ec4SEd Cashin e = t + d->ntargets; 101468e0d42fSEd L. Cashin for (; t < e && *t; t++) 101568e0d42fSEd L. Cashin if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 101668e0d42fSEd L. Cashin return *t; 101768e0d42fSEd L. Cashin return NULL; 101868e0d42fSEd L. Cashin } 101968e0d42fSEd L. Cashin 10203d5b0605SEd Cashin static void 1021feb261e2SKent Overstreet bvcpy(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter, long cnt) 10223d5b0605SEd Cashin { 10233d5b0605SEd Cashin int soff = 0; 1024feb261e2SKent Overstreet struct bio_vec bv; 1025feb261e2SKent Overstreet 1026feb261e2SKent Overstreet iter.bi_size = cnt; 1027feb261e2SKent Overstreet 1028feb261e2SKent Overstreet __bio_for_each_segment(bv, bio, iter, iter) { 1029b7ab4611SChristoph Hellwig char *p = bvec_kmap_local(&bv); 1030feb261e2SKent Overstreet skb_copy_bits(skb, soff, p, bv.bv_len); 1031b7ab4611SChristoph Hellwig kunmap_local(p); 1032feb261e2SKent Overstreet soff += bv.bv_len; 1033feb261e2SKent Overstreet } 10343d5b0605SEd Cashin } 10353d5b0605SEd Cashin 103669cf2d85SEd Cashin void 103769cf2d85SEd Cashin aoe_end_request(struct aoedev *d, struct request *rq, int fastfail) 103869cf2d85SEd Cashin { 103969cf2d85SEd Cashin struct bio *bio; 104069cf2d85SEd Cashin int bok; 104169cf2d85SEd Cashin struct request_queue *q; 10423582dd29SJens Axboe blk_status_t err = BLK_STS_OK; 104369cf2d85SEd Cashin 104469cf2d85SEd Cashin q = d->blkq; 104569cf2d85SEd Cashin if (rq == d->ip.rq) 104669cf2d85SEd Cashin d->ip.rq = NULL; 104769cf2d85SEd Cashin do { 104869cf2d85SEd Cashin bio = rq->bio; 10494e4cbee9SChristoph Hellwig bok = !fastfail && !bio->bi_status; 10503582dd29SJens Axboe if (!bok) 10513582dd29SJens Axboe err = BLK_STS_IOERR; 10523582dd29SJens Axboe } while (blk_update_request(rq, bok ? BLK_STS_OK : BLK_STS_IOERR, bio->bi_iter.bi_size)); 10533582dd29SJens Axboe 10543582dd29SJens Axboe __blk_mq_end_request(rq, err); 105569cf2d85SEd Cashin 105659788683SKees Cook /* cf. https://lore.kernel.org/lkml/20061031071040.GS14055@kernel.dk/ */ 105769cf2d85SEd Cashin if (!fastfail) 10583582dd29SJens Axboe blk_mq_run_hw_queues(q, true); 105969cf2d85SEd Cashin } 106069cf2d85SEd Cashin 106169cf2d85SEd Cashin static void 106269cf2d85SEd Cashin aoe_end_buf(struct aoedev *d, struct buf *buf) 106369cf2d85SEd Cashin { 106461e7712eSChristoph Hellwig struct request *rq = buf->rq; 106561e7712eSChristoph Hellwig struct aoe_req *req = blk_mq_rq_to_pdu(rq); 106669cf2d85SEd Cashin 106769cf2d85SEd Cashin if (buf == d->ip.buf) 106869cf2d85SEd Cashin d->ip.buf = NULL; 106969cf2d85SEd Cashin mempool_free(buf, d->bufpool); 107061e7712eSChristoph Hellwig if (--req->nr_bios == 0) 107169cf2d85SEd Cashin aoe_end_request(d, rq, 0); 107269cf2d85SEd Cashin } 107369cf2d85SEd Cashin 10743d5b0605SEd Cashin static void 1075896831f5SEd Cashin ktiocomplete(struct frame *f) 10763d5b0605SEd Cashin { 1077ddec63e8SEd L. Cashin struct aoe_hdr *hin, *hout; 10781da177e4SLinus Torvalds struct aoe_atahdr *ahin, *ahout; 10791da177e4SLinus Torvalds struct buf *buf; 1080896831f5SEd Cashin struct sk_buff *skb; 108168e0d42fSEd L. Cashin struct aoetgt *t; 108268e0d42fSEd L. Cashin struct aoeif *ifp; 1083896831f5SEd Cashin struct aoedev *d; 1084896831f5SEd Cashin long n; 1085bbb44e30SEd Cashin int untainted; 1086896831f5SEd Cashin 1087896831f5SEd Cashin if (f == NULL) 1088896831f5SEd Cashin return; 1089896831f5SEd Cashin 1090896831f5SEd Cashin t = f->t; 1091896831f5SEd Cashin d = t->d; 1092bbb44e30SEd Cashin skb = f->r_skb; 1093bbb44e30SEd Cashin buf = f->buf; 1094bbb44e30SEd Cashin if (f->flags & FFL_PROBE) 1095bbb44e30SEd Cashin goto out; 1096bbb44e30SEd Cashin if (!skb) /* just fail the buf. */ 1097bbb44e30SEd Cashin goto noskb; 1098896831f5SEd Cashin 1099896831f5SEd Cashin hout = (struct aoe_hdr *) skb_mac_header(f->skb); 1100896831f5SEd Cashin ahout = (struct aoe_atahdr *) (hout+1); 1101896831f5SEd Cashin 1102896831f5SEd Cashin hin = (struct aoe_hdr *) skb->data; 1103896831f5SEd Cashin skb_pull(skb, sizeof(*hin)); 1104896831f5SEd Cashin ahin = (struct aoe_atahdr *) skb->data; 1105896831f5SEd Cashin skb_pull(skb, sizeof(*ahin)); 1106896831f5SEd Cashin if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 1107896831f5SEd Cashin pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 1108896831f5SEd Cashin ahout->cmdstat, ahin->cmdstat, 1109896831f5SEd Cashin d->aoemajor, d->aoeminor); 1110896831f5SEd Cashin noskb: if (buf) 11114e4cbee9SChristoph Hellwig buf->bio->bi_status = BLK_STS_IOERR; 1112bbb44e30SEd Cashin goto out; 1113896831f5SEd Cashin } 1114896831f5SEd Cashin 1115896831f5SEd Cashin n = ahout->scnt << 9; 1116896831f5SEd Cashin switch (ahout->cmdstat) { 1117896831f5SEd Cashin case ATA_CMD_PIO_READ: 1118896831f5SEd Cashin case ATA_CMD_PIO_READ_EXT: 1119896831f5SEd Cashin if (skb->len < n) { 1120bf29754aSEd Cashin pr_err("%s e%ld.%d. skb->len=%d need=%ld\n", 1121bf29754aSEd Cashin "aoe: runt data size in read from", 1122bf29754aSEd Cashin (long) d->aoemajor, d->aoeminor, 1123896831f5SEd Cashin skb->len, n); 11244e4cbee9SChristoph Hellwig buf->bio->bi_status = BLK_STS_IOERR; 1125896831f5SEd Cashin break; 1126896831f5SEd Cashin } 1127feb261e2SKent Overstreet if (n > f->iter.bi_size) { 1128feb261e2SKent Overstreet pr_err_ratelimited("%s e%ld.%d. bytes=%ld need=%u\n", 1129feb261e2SKent Overstreet "aoe: too-large data size in read from", 1130feb261e2SKent Overstreet (long) d->aoemajor, d->aoeminor, 1131feb261e2SKent Overstreet n, f->iter.bi_size); 11324e4cbee9SChristoph Hellwig buf->bio->bi_status = BLK_STS_IOERR; 1133feb261e2SKent Overstreet break; 1134feb261e2SKent Overstreet } 1135feb261e2SKent Overstreet bvcpy(skb, f->buf->bio, f->iter, n); 1136df561f66SGustavo A. R. Silva fallthrough; 1137896831f5SEd Cashin case ATA_CMD_PIO_WRITE: 1138896831f5SEd Cashin case ATA_CMD_PIO_WRITE_EXT: 1139896831f5SEd Cashin spin_lock_irq(&d->lock); 1140896831f5SEd Cashin ifp = getif(t, skb->dev); 11413f0f0133SEd Cashin if (ifp) 1142896831f5SEd Cashin ifp->lost = 0; 1143896831f5SEd Cashin spin_unlock_irq(&d->lock); 1144896831f5SEd Cashin break; 1145896831f5SEd Cashin case ATA_CMD_ID_ATA: 1146896831f5SEd Cashin if (skb->len < 512) { 1147bf29754aSEd Cashin pr_info("%s e%ld.%d. skb->len=%d need=512\n", 1148bf29754aSEd Cashin "aoe: runt data size in ataid from", 1149bf29754aSEd Cashin (long) d->aoemajor, d->aoeminor, 1150896831f5SEd Cashin skb->len); 1151896831f5SEd Cashin break; 1152896831f5SEd Cashin } 1153896831f5SEd Cashin if (skb_linearize(skb)) 1154896831f5SEd Cashin break; 1155896831f5SEd Cashin spin_lock_irq(&d->lock); 1156896831f5SEd Cashin ataid_complete(d, t, skb->data); 1157896831f5SEd Cashin spin_unlock_irq(&d->lock); 1158896831f5SEd Cashin break; 1159896831f5SEd Cashin default: 1160896831f5SEd Cashin pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n", 1161896831f5SEd Cashin ahout->cmdstat, 1162896831f5SEd Cashin be16_to_cpu(get_unaligned(&hin->major)), 1163896831f5SEd Cashin hin->minor); 1164896831f5SEd Cashin } 1165bbb44e30SEd Cashin out: 1166896831f5SEd Cashin spin_lock_irq(&d->lock); 1167bbb44e30SEd Cashin if (t->taint > 0 1168bbb44e30SEd Cashin && --t->taint > 0 1169bbb44e30SEd Cashin && t->nout_probes == 0) { 1170bbb44e30SEd Cashin count_targets(d, &untainted); 1171bbb44e30SEd Cashin if (untainted > 0) { 1172bbb44e30SEd Cashin probe(t); 1173bbb44e30SEd Cashin t->nout_probes++; 1174bbb44e30SEd Cashin } 1175bbb44e30SEd Cashin } 1176896831f5SEd Cashin 1177896831f5SEd Cashin aoe_freetframe(f); 1178896831f5SEd Cashin 1179feb261e2SKent Overstreet if (buf && --buf->nframesout == 0 && buf->iter.bi_size == 0) 118069cf2d85SEd Cashin aoe_end_buf(d, buf); 1181896831f5SEd Cashin 1182896831f5SEd Cashin spin_unlock_irq(&d->lock); 118369cf2d85SEd Cashin aoedev_put(d); 1184896831f5SEd Cashin dev_kfree_skb(skb); 1185896831f5SEd Cashin } 1186896831f5SEd Cashin 1187896831f5SEd Cashin /* Enters with iocq.lock held. 1188896831f5SEd Cashin * Returns true iff responses needing processing remain. 1189896831f5SEd Cashin */ 1190896831f5SEd Cashin static int 11918030d343SEd Cashin ktio(int id) 1192896831f5SEd Cashin { 1193896831f5SEd Cashin struct frame *f; 1194896831f5SEd Cashin struct list_head *pos; 1195896831f5SEd Cashin int i; 11968030d343SEd Cashin int actual_id; 1197896831f5SEd Cashin 1198896831f5SEd Cashin for (i = 0; ; ++i) { 1199896831f5SEd Cashin if (i == MAXIOC) 1200896831f5SEd Cashin return 1; 12018030d343SEd Cashin if (list_empty(&iocq[id].head)) 1202896831f5SEd Cashin return 0; 12038030d343SEd Cashin pos = iocq[id].head.next; 1204896831f5SEd Cashin list_del(pos); 1205896831f5SEd Cashin f = list_entry(pos, struct frame, head); 12068030d343SEd Cashin spin_unlock_irq(&iocq[id].lock); 1207896831f5SEd Cashin ktiocomplete(f); 12088030d343SEd Cashin 12098030d343SEd Cashin /* Figure out if extra threads are required. */ 12108030d343SEd Cashin actual_id = f->t->d->aoeminor % ncpus; 12118030d343SEd Cashin 12128030d343SEd Cashin if (!kts[actual_id].active) { 12138030d343SEd Cashin BUG_ON(id != 0); 12148030d343SEd Cashin mutex_lock(&ktio_spawn_lock); 12158030d343SEd Cashin if (!kts[actual_id].active 12168030d343SEd Cashin && aoe_ktstart(&kts[actual_id]) == 0) 12178030d343SEd Cashin kts[actual_id].active = 1; 12188030d343SEd Cashin mutex_unlock(&ktio_spawn_lock); 12198030d343SEd Cashin } 12208030d343SEd Cashin spin_lock_irq(&iocq[id].lock); 1221896831f5SEd Cashin } 1222896831f5SEd Cashin } 1223896831f5SEd Cashin 1224896831f5SEd Cashin static int 1225896831f5SEd Cashin kthread(void *vp) 1226896831f5SEd Cashin { 1227896831f5SEd Cashin struct ktstate *k; 1228896831f5SEd Cashin DECLARE_WAITQUEUE(wait, current); 1229896831f5SEd Cashin int more; 1230896831f5SEd Cashin 1231896831f5SEd Cashin k = vp; 1232896831f5SEd Cashin current->flags |= PF_NOFREEZE; 1233896831f5SEd Cashin set_user_nice(current, -10); 1234896831f5SEd Cashin complete(&k->rendez); /* tell spawner we're running */ 1235896831f5SEd Cashin do { 1236896831f5SEd Cashin spin_lock_irq(k->lock); 12378030d343SEd Cashin more = k->fn(k->id); 1238896831f5SEd Cashin if (!more) { 1239896831f5SEd Cashin add_wait_queue(k->waitq, &wait); 1240896831f5SEd Cashin __set_current_state(TASK_INTERRUPTIBLE); 1241896831f5SEd Cashin } 1242896831f5SEd Cashin spin_unlock_irq(k->lock); 1243896831f5SEd Cashin if (!more) { 1244896831f5SEd Cashin schedule(); 1245896831f5SEd Cashin remove_wait_queue(k->waitq, &wait); 1246896831f5SEd Cashin } else 1247896831f5SEd Cashin cond_resched(); 1248896831f5SEd Cashin } while (!kthread_should_stop()); 1249896831f5SEd Cashin complete(&k->rendez); /* tell spawner we're stopping */ 1250896831f5SEd Cashin return 0; 1251896831f5SEd Cashin } 1252896831f5SEd Cashin 1253eb086ec5SEd Cashin void 1254896831f5SEd Cashin aoe_ktstop(struct ktstate *k) 1255896831f5SEd Cashin { 1256896831f5SEd Cashin kthread_stop(k->task); 1257896831f5SEd Cashin wait_for_completion(&k->rendez); 1258896831f5SEd Cashin } 1259896831f5SEd Cashin 1260eb086ec5SEd Cashin int 1261896831f5SEd Cashin aoe_ktstart(struct ktstate *k) 1262896831f5SEd Cashin { 1263896831f5SEd Cashin struct task_struct *task; 1264896831f5SEd Cashin 1265896831f5SEd Cashin init_completion(&k->rendez); 1266f170168bSKees Cook task = kthread_run(kthread, k, "%s", k->name); 1267896831f5SEd Cashin if (task == NULL || IS_ERR(task)) 1268896831f5SEd Cashin return -ENOMEM; 1269896831f5SEd Cashin k->task = task; 1270896831f5SEd Cashin wait_for_completion(&k->rendez); /* allow kthread to start */ 1271896831f5SEd Cashin init_completion(&k->rendez); /* for waiting for exit later */ 1272896831f5SEd Cashin return 0; 1273896831f5SEd Cashin } 1274896831f5SEd Cashin 1275896831f5SEd Cashin /* pass it off to kthreads for processing */ 1276896831f5SEd Cashin static void 1277896831f5SEd Cashin ktcomplete(struct frame *f, struct sk_buff *skb) 1278896831f5SEd Cashin { 12798030d343SEd Cashin int id; 1280896831f5SEd Cashin ulong flags; 1281896831f5SEd Cashin 1282896831f5SEd Cashin f->r_skb = skb; 12838030d343SEd Cashin id = f->t->d->aoeminor % ncpus; 12848030d343SEd Cashin spin_lock_irqsave(&iocq[id].lock, flags); 12858030d343SEd Cashin if (!kts[id].active) { 12868030d343SEd Cashin spin_unlock_irqrestore(&iocq[id].lock, flags); 12878030d343SEd Cashin /* The thread with id has not been spawned yet, 12888030d343SEd Cashin * so delegate the work to the main thread and 12898030d343SEd Cashin * try spawning a new thread. 12908030d343SEd Cashin */ 12918030d343SEd Cashin id = 0; 12928030d343SEd Cashin spin_lock_irqsave(&iocq[id].lock, flags); 12938030d343SEd Cashin } 12948030d343SEd Cashin list_add_tail(&f->head, &iocq[id].head); 12958030d343SEd Cashin spin_unlock_irqrestore(&iocq[id].lock, flags); 12968030d343SEd Cashin wake_up(&ktiowq[id]); 1297896831f5SEd Cashin } 1298896831f5SEd Cashin 1299896831f5SEd Cashin struct sk_buff * 1300896831f5SEd Cashin aoecmd_ata_rsp(struct sk_buff *skb) 1301896831f5SEd Cashin { 1302896831f5SEd Cashin struct aoedev *d; 1303896831f5SEd Cashin struct aoe_hdr *h; 1304896831f5SEd Cashin struct frame *f; 1305896831f5SEd Cashin u32 n; 13061da177e4SLinus Torvalds ulong flags; 13071da177e4SLinus Torvalds char ebuf[128]; 130832465c65Secashin@coraid.com u16 aoemajor; 13091da177e4SLinus Torvalds 1310896831f5SEd Cashin h = (struct aoe_hdr *) skb->data; 1311896831f5SEd Cashin aoemajor = be16_to_cpu(get_unaligned(&h->major)); 13120c966214SEd Cashin d = aoedev_by_aoeaddr(aoemajor, h->minor, 0); 13131da177e4SLinus Torvalds if (d == NULL) { 13141da177e4SLinus Torvalds snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 13151da177e4SLinus Torvalds "for unknown device %d.%d\n", 1316896831f5SEd Cashin aoemajor, h->minor); 13171da177e4SLinus Torvalds aoechr_error(ebuf); 1318896831f5SEd Cashin return skb; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 13221da177e4SLinus Torvalds 1323896831f5SEd Cashin n = be32_to_cpu(get_unaligned(&h->tag)); 132464a80f5aSEd Cashin f = getframe(d, n); 13253a0c40d2SEd Cashin if (f) { 13265f0c9c48SEd Cashin calc_rttavg(d, f->t, tsince_hr(f)); 13273a0c40d2SEd Cashin f->t->nout--; 1328bbb44e30SEd Cashin if (f->flags & FFL_PROBE) 1329bbb44e30SEd Cashin f->t->nout_probes--; 13303a0c40d2SEd Cashin } else { 13313a0c40d2SEd Cashin f = getframe_deferred(d, n); 13323a0c40d2SEd Cashin if (f) { 13335f0c9c48SEd Cashin calc_rttavg(d, NULL, tsince_hr(f)); 13343a0c40d2SEd Cashin } else { 13353a0c40d2SEd Cashin calc_rttavg(d, NULL, tsince(n)); 13361da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 133769cf2d85SEd Cashin aoedev_put(d); 13383a0c40d2SEd Cashin snprintf(ebuf, sizeof(ebuf), 13392292a7e1SEd Cashin "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n", 13401da177e4SLinus Torvalds "unexpected rsp", 1341896831f5SEd Cashin get_unaligned_be16(&h->major), 1342896831f5SEd Cashin h->minor, 1343896831f5SEd Cashin get_unaligned_be32(&h->tag), 13442292a7e1SEd Cashin jiffies, 13452292a7e1SEd Cashin h->src, 13462292a7e1SEd Cashin h->dst); 13471da177e4SLinus Torvalds aoechr_error(ebuf); 1348896831f5SEd Cashin return skb; 13491da177e4SLinus Torvalds } 13503a0c40d2SEd Cashin } 13511da177e4SLinus Torvalds aoecmd_work(d); 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 1354896831f5SEd Cashin 1355896831f5SEd Cashin ktcomplete(f, skb); 1356896831f5SEd Cashin 1357896831f5SEd Cashin /* 1358896831f5SEd Cashin * Note here that we do not perform an aoedev_put, as we are 1359896831f5SEd Cashin * leaving this reference for the ktio to release. 1360896831f5SEd Cashin */ 1361896831f5SEd Cashin return NULL; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds void 13651da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 13661da177e4SLinus Torvalds { 1367e9bb8fb0SDavid S. Miller struct sk_buff_head queue; 13681da177e4SLinus Torvalds 1369e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1370e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 1371e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 137468e0d42fSEd L. Cashin struct sk_buff * 13751da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d) 13761da177e4SLinus Torvalds { 13771da177e4SLinus Torvalds struct aoe_hdr *h; 13781da177e4SLinus Torvalds struct aoe_atahdr *ah; 13791da177e4SLinus Torvalds struct frame *f; 13801da177e4SLinus Torvalds struct sk_buff *skb; 138168e0d42fSEd L. Cashin struct aoetgt *t; 13821da177e4SLinus Torvalds 1383896831f5SEd Cashin f = newframe(d); 138468e0d42fSEd L. Cashin if (f == NULL) 13851da177e4SLinus Torvalds return NULL; 138668e0d42fSEd L. Cashin 138768e0d42fSEd L. Cashin t = *d->tgt; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* initialize the headers & frame */ 1390e407a7f6SEd L. Cashin skb = f->skb; 1391abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 13921da177e4SLinus Torvalds ah = (struct aoe_atahdr *) (h+1); 139319900cdeSEd L. Cashin skb_put(skb, sizeof *h + sizeof *ah); 139419900cdeSEd L. Cashin memset(h, 0, skb->len); 139568e0d42fSEd L. Cashin f->tag = aoehdr_atainit(d, t, h); 1396896831f5SEd Cashin fhash(f); 139768e0d42fSEd L. Cashin t->nout++; 13981da177e4SLinus Torvalds f->waited = 0; 13993fc9b032SEd Cashin f->waited_total = 0; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /* set up ata header */ 14021da177e4SLinus Torvalds ah->scnt = 1; 140304b3ab52SBartlomiej Zolnierkiewicz ah->cmdstat = ATA_CMD_ID_ATA; 14041da177e4SLinus Torvalds ah->lba3 = 0xa0; 14051da177e4SLinus Torvalds 1406*acc5103aSChun-Yi Lee dev_hold(t->ifp->nd); 140768e0d42fSEd L. Cashin skb->dev = t->ifp->nd; 14081da177e4SLinus Torvalds 14093a0c40d2SEd Cashin d->rttavg = RTTAVG_INIT; 14103a0c40d2SEd Cashin d->rttdev = RTTDEV_INIT; 1411841b86f3SKees Cook d->timer.function = rexmit_timer; 14121da177e4SLinus Torvalds 14135f0c9c48SEd Cashin skb = skb_clone(skb, GFP_ATOMIC); 141485cf955dSTina Ruchandani if (skb) 141585cf955dSTina Ruchandani f->sent = ktime_get(); 1416*acc5103aSChun-Yi Lee else 1417*acc5103aSChun-Yi Lee dev_put(t->ifp->nd); 14185f0c9c48SEd Cashin 14195f0c9c48SEd Cashin return skb; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 142271114ec4SEd Cashin static struct aoetgt ** 142371114ec4SEd Cashin grow_targets(struct aoedev *d) 142471114ec4SEd Cashin { 142571114ec4SEd Cashin ulong oldn, newn; 142671114ec4SEd Cashin struct aoetgt **tt; 142771114ec4SEd Cashin 142871114ec4SEd Cashin oldn = d->ntargets; 142971114ec4SEd Cashin newn = oldn * 2; 143071114ec4SEd Cashin tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC); 143171114ec4SEd Cashin if (!tt) 143271114ec4SEd Cashin return NULL; 143371114ec4SEd Cashin memmove(tt, d->targets, sizeof(*d->targets) * oldn); 143471114ec4SEd Cashin d->tgt = tt + (d->tgt - d->targets); 143571114ec4SEd Cashin kfree(d->targets); 143671114ec4SEd Cashin d->targets = tt; 143771114ec4SEd Cashin d->ntargets = newn; 143871114ec4SEd Cashin 143971114ec4SEd Cashin return &d->targets[oldn]; 144071114ec4SEd Cashin } 144171114ec4SEd Cashin 144268e0d42fSEd L. Cashin static struct aoetgt * 144368e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes) 144468e0d42fSEd L. Cashin { 144568e0d42fSEd L. Cashin struct aoetgt *t, **tt, **te; 144668e0d42fSEd L. Cashin 144768e0d42fSEd L. Cashin tt = d->targets; 144871114ec4SEd Cashin te = tt + d->ntargets; 144968e0d42fSEd L. Cashin for (; tt < te && *tt; tt++) 145068e0d42fSEd L. Cashin ; 145168e0d42fSEd L. Cashin 1452578c4aa0SEd L. Cashin if (tt == te) { 145371114ec4SEd Cashin tt = grow_targets(d); 145471114ec4SEd Cashin if (!tt) 145571114ec4SEd Cashin goto nomem; 1456578c4aa0SEd L. Cashin } 1457896831f5SEd Cashin t = kzalloc(sizeof(*t), GFP_ATOMIC); 145871114ec4SEd Cashin if (!t) 145971114ec4SEd Cashin goto nomem; 146068e0d42fSEd L. Cashin t->nframes = nframes; 1461896831f5SEd Cashin t->d = d; 146268e0d42fSEd L. Cashin memcpy(t->addr, addr, sizeof t->addr); 146368e0d42fSEd L. Cashin t->ifp = t->ifs; 14643a0c40d2SEd Cashin aoecmd_wreset(t); 1465bbb44e30SEd Cashin t->maxout = t->nframes / 2; 1466896831f5SEd Cashin INIT_LIST_HEAD(&t->ffree); 146768e0d42fSEd L. Cashin return *tt = t; 146871114ec4SEd Cashin 146971114ec4SEd Cashin nomem: 147071114ec4SEd Cashin pr_info("aoe: cannot allocate memory to add target\n"); 147171114ec4SEd Cashin return NULL; 147268e0d42fSEd L. Cashin } 147368e0d42fSEd L. Cashin 14743f0f0133SEd Cashin static void 14753f0f0133SEd Cashin setdbcnt(struct aoedev *d) 14763f0f0133SEd Cashin { 14773f0f0133SEd Cashin struct aoetgt **t, **e; 14783f0f0133SEd Cashin int bcnt = 0; 14793f0f0133SEd Cashin 14803f0f0133SEd Cashin t = d->targets; 148171114ec4SEd Cashin e = t + d->ntargets; 14823f0f0133SEd Cashin for (; t < e && *t; t++) 14833f0f0133SEd Cashin if (bcnt == 0 || bcnt > (*t)->minbcnt) 14843f0f0133SEd Cashin bcnt = (*t)->minbcnt; 14853f0f0133SEd Cashin if (bcnt != d->maxbcnt) { 14863f0f0133SEd Cashin d->maxbcnt = bcnt; 14873f0f0133SEd Cashin pr_info("aoe: e%ld.%d: setting %d byte data frames\n", 14883f0f0133SEd Cashin d->aoemajor, d->aoeminor, bcnt); 14893f0f0133SEd Cashin } 14903f0f0133SEd Cashin } 14913f0f0133SEd Cashin 14923f0f0133SEd Cashin static void 14933f0f0133SEd Cashin setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt) 14943f0f0133SEd Cashin { 14953f0f0133SEd Cashin struct aoedev *d; 14963f0f0133SEd Cashin struct aoeif *p, *e; 14973f0f0133SEd Cashin int minbcnt; 14983f0f0133SEd Cashin 14993f0f0133SEd Cashin d = t->d; 15003f0f0133SEd Cashin minbcnt = bcnt; 15013f0f0133SEd Cashin p = t->ifs; 15023f0f0133SEd Cashin e = p + NAOEIFS; 15033f0f0133SEd Cashin for (; p < e; p++) { 15043f0f0133SEd Cashin if (p->nd == NULL) 15053f0f0133SEd Cashin break; /* end of the valid interfaces */ 15063f0f0133SEd Cashin if (p->nd == nd) { 15073f0f0133SEd Cashin p->bcnt = bcnt; /* we're updating */ 15083f0f0133SEd Cashin nd = NULL; 15093f0f0133SEd Cashin } else if (minbcnt > p->bcnt) 15103f0f0133SEd Cashin minbcnt = p->bcnt; /* find the min interface */ 15113f0f0133SEd Cashin } 15123f0f0133SEd Cashin if (nd) { 15133f0f0133SEd Cashin if (p == e) { 15143f0f0133SEd Cashin pr_err("aoe: device setifbcnt failure; too many interfaces.\n"); 15153f0f0133SEd Cashin return; 15163f0f0133SEd Cashin } 15171b86fda9SEd Cashin dev_hold(nd); 15183f0f0133SEd Cashin p->nd = nd; 15193f0f0133SEd Cashin p->bcnt = bcnt; 15203f0f0133SEd Cashin } 15213f0f0133SEd Cashin t->minbcnt = minbcnt; 15223f0f0133SEd Cashin setdbcnt(d); 15233f0f0133SEd Cashin } 15243f0f0133SEd Cashin 15251da177e4SLinus Torvalds void 15261da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds struct aoedev *d; 15291da177e4SLinus Torvalds struct aoe_hdr *h; 15301da177e4SLinus Torvalds struct aoe_cfghdr *ch; 153168e0d42fSEd L. Cashin struct aoetgt *t; 15320c966214SEd Cashin ulong flags, aoemajor; 15331da177e4SLinus Torvalds struct sk_buff *sl; 153469cf2d85SEd Cashin struct sk_buff_head queue; 153519bf2635SEd L. Cashin u16 n; 15361da177e4SLinus Torvalds 153769cf2d85SEd Cashin sl = NULL; 1538abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 15391da177e4SLinus Torvalds ch = (struct aoe_cfghdr *) (h+1); 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds /* 15421da177e4SLinus Torvalds * Enough people have their dip switches set backwards to 15431da177e4SLinus Torvalds * warrant a loud message for this special case. 15441da177e4SLinus Torvalds */ 1545823ed72eSHarvey Harrison aoemajor = get_unaligned_be16(&h->major); 15461da177e4SLinus Torvalds if (aoemajor == 0xfff) { 1547a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 15486bb6285fSEd L. Cashin "Check shelf dip switches.\n"); 15491da177e4SLinus Torvalds return; 15501da177e4SLinus Torvalds } 15517159e969SEd Cashin if (aoemajor == 0xffff) { 15527159e969SEd Cashin pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n", 15530c966214SEd Cashin aoemajor, (int) h->minor); 15546583303cSEd Cashin return; 15556583303cSEd Cashin } 15567159e969SEd Cashin if (h->minor == 0xff) { 15577159e969SEd Cashin pr_info("aoe: e%ld.%d: broadcast slot number invalid\n", 15587159e969SEd Cashin aoemajor, (int) h->minor); 15591da177e4SLinus Torvalds return; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 156219bf2635SEd L. Cashin n = be16_to_cpu(ch->bufcnt); 15637df620d8SEd L. Cashin if (n > aoe_maxout) /* keep it reasonable */ 15647df620d8SEd L. Cashin n = aoe_maxout; 15651da177e4SLinus Torvalds 15667159e969SEd Cashin d = aoedev_by_aoeaddr(aoemajor, h->minor, 1); 15677159e969SEd Cashin if (d == NULL) { 15687159e969SEd Cashin pr_info("aoe: device allocation failure\n"); 15697159e969SEd Cashin return; 15707159e969SEd Cashin } 15717159e969SEd Cashin 15721da177e4SLinus Torvalds spin_lock_irqsave(&d->lock, flags); 15731da177e4SLinus Torvalds 157468e0d42fSEd L. Cashin t = gettgt(d, h->src); 15751b8a1636SEd Cashin if (t) { 15761b8a1636SEd Cashin t->nframes = n; 15771b8a1636SEd Cashin if (n < t->maxout) 15783a0c40d2SEd Cashin aoecmd_wreset(t); 15791b8a1636SEd Cashin } else { 158068e0d42fSEd L. Cashin t = addtgt(d, h->src, n); 158169cf2d85SEd Cashin if (!t) 158269cf2d85SEd Cashin goto bail; 158368e0d42fSEd L. Cashin } 15843f0f0133SEd Cashin n = skb->dev->mtu; 158519bf2635SEd L. Cashin n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr); 158619bf2635SEd L. Cashin n /= 512; 158719bf2635SEd L. Cashin if (n > ch->scnt) 158819bf2635SEd L. Cashin n = ch->scnt; 15894f51dc5eSEd L. Cashin n = n ? n * 512 : DEFAULTBCNT; 15903f0f0133SEd Cashin setifbcnt(t, skb->dev, n); 15913ae1c24eSEd L. Cashin 15923ae1c24eSEd L. Cashin /* don't change users' perspective */ 159369cf2d85SEd Cashin if (d->nopen == 0) { 159463e9cc5dSecashin@coraid.com d->fw_ver = be16_to_cpu(ch->fwver); 159568e0d42fSEd L. Cashin sl = aoecmd_ata_id(d); 159669cf2d85SEd Cashin } 159769cf2d85SEd Cashin bail: 15981da177e4SLinus Torvalds spin_unlock_irqrestore(&d->lock, flags); 159969cf2d85SEd Cashin aoedev_put(d); 1600e9bb8fb0SDavid S. Miller if (sl) { 1601e9bb8fb0SDavid S. Miller __skb_queue_head_init(&queue); 1602e9bb8fb0SDavid S. Miller __skb_queue_tail(&queue, sl); 1603e9bb8fb0SDavid S. Miller aoenet_xmit(&queue); 1604e9bb8fb0SDavid S. Miller } 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 160768e0d42fSEd L. Cashin void 16083a0c40d2SEd Cashin aoecmd_wreset(struct aoetgt *t) 16093a0c40d2SEd Cashin { 16103a0c40d2SEd Cashin t->maxout = 1; 16113a0c40d2SEd Cashin t->ssthresh = t->nframes / 2; 16123a0c40d2SEd Cashin t->next_cwnd = t->nframes; 16133a0c40d2SEd Cashin } 16143a0c40d2SEd Cashin 16153a0c40d2SEd Cashin void 161668e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d) 161768e0d42fSEd L. Cashin { 161868e0d42fSEd L. Cashin struct aoetgt **t, **te; 161968e0d42fSEd L. Cashin 16203a0c40d2SEd Cashin d->rttavg = RTTAVG_INIT; 16213a0c40d2SEd Cashin d->rttdev = RTTDEV_INIT; 16223f0f0133SEd Cashin d->maxbcnt = 0; 162368e0d42fSEd L. Cashin 162468e0d42fSEd L. Cashin t = d->targets; 162571114ec4SEd Cashin te = t + d->ntargets; 16263f0f0133SEd Cashin for (; t < te && *t; t++) 16273a0c40d2SEd Cashin aoecmd_wreset(*t); 162868e0d42fSEd L. Cashin } 1629896831f5SEd Cashin 163069cf2d85SEd Cashin void 163169cf2d85SEd Cashin aoe_failbuf(struct aoedev *d, struct buf *buf) 163269cf2d85SEd Cashin { 163369cf2d85SEd Cashin if (buf == NULL) 163469cf2d85SEd Cashin return; 1635feb261e2SKent Overstreet buf->iter.bi_size = 0; 16364e4cbee9SChristoph Hellwig buf->bio->bi_status = BLK_STS_IOERR; 163769cf2d85SEd Cashin if (buf->nframesout == 0) 163869cf2d85SEd Cashin aoe_end_buf(d, buf); 163969cf2d85SEd Cashin } 164069cf2d85SEd Cashin 164169cf2d85SEd Cashin void 164269cf2d85SEd Cashin aoe_flush_iocq(void) 1643896831f5SEd Cashin { 16448030d343SEd Cashin int i; 16458030d343SEd Cashin 16468030d343SEd Cashin for (i = 0; i < ncpus; i++) { 16478030d343SEd Cashin if (kts[i].active) 16488030d343SEd Cashin aoe_flush_iocq_by_index(i); 16498030d343SEd Cashin } 16508030d343SEd Cashin } 16518030d343SEd Cashin 16528030d343SEd Cashin void 16538030d343SEd Cashin aoe_flush_iocq_by_index(int id) 16548030d343SEd Cashin { 1655896831f5SEd Cashin struct frame *f; 1656896831f5SEd Cashin struct aoedev *d; 1657896831f5SEd Cashin LIST_HEAD(flist); 1658896831f5SEd Cashin struct list_head *pos; 1659896831f5SEd Cashin struct sk_buff *skb; 1660896831f5SEd Cashin ulong flags; 1661896831f5SEd Cashin 16628030d343SEd Cashin spin_lock_irqsave(&iocq[id].lock, flags); 16638030d343SEd Cashin list_splice_init(&iocq[id].head, &flist); 16648030d343SEd Cashin spin_unlock_irqrestore(&iocq[id].lock, flags); 1665896831f5SEd Cashin while (!list_empty(&flist)) { 1666896831f5SEd Cashin pos = flist.next; 1667896831f5SEd Cashin list_del(pos); 1668896831f5SEd Cashin f = list_entry(pos, struct frame, head); 1669896831f5SEd Cashin d = f->t->d; 1670896831f5SEd Cashin skb = f->r_skb; 1671896831f5SEd Cashin spin_lock_irqsave(&d->lock, flags); 1672896831f5SEd Cashin if (f->buf) { 1673896831f5SEd Cashin f->buf->nframesout--; 1674896831f5SEd Cashin aoe_failbuf(d, f->buf); 1675896831f5SEd Cashin } 1676896831f5SEd Cashin aoe_freetframe(f); 1677896831f5SEd Cashin spin_unlock_irqrestore(&d->lock, flags); 1678896831f5SEd Cashin dev_kfree_skb(skb); 167969cf2d85SEd Cashin aoedev_put(d); 1680896831f5SEd Cashin } 1681896831f5SEd Cashin } 1682896831f5SEd Cashin 1683896831f5SEd Cashin int __init 1684896831f5SEd Cashin aoecmd_init(void) 1685896831f5SEd Cashin { 1686bbb44e30SEd Cashin void *p; 16878030d343SEd Cashin int i; 16888030d343SEd Cashin int ret; 1689bbb44e30SEd Cashin 1690bbb44e30SEd Cashin /* get_zeroed_page returns page with ref count 1 */ 169132d6bd90SMichal Hocko p = (void *) get_zeroed_page(GFP_KERNEL); 1692bbb44e30SEd Cashin if (!p) 1693bbb44e30SEd Cashin return -ENOMEM; 1694bbb44e30SEd Cashin empty_page = virt_to_page(p); 1695bbb44e30SEd Cashin 16968030d343SEd Cashin ncpus = num_online_cpus(); 16978030d343SEd Cashin 16988030d343SEd Cashin iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL); 16998030d343SEd Cashin if (!iocq) 17008030d343SEd Cashin return -ENOMEM; 17018030d343SEd Cashin 17028030d343SEd Cashin kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL); 17038030d343SEd Cashin if (!kts) { 17048030d343SEd Cashin ret = -ENOMEM; 17058030d343SEd Cashin goto kts_fail; 17068030d343SEd Cashin } 17078030d343SEd Cashin 17088030d343SEd Cashin ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL); 17098030d343SEd Cashin if (!ktiowq) { 17108030d343SEd Cashin ret = -ENOMEM; 17118030d343SEd Cashin goto ktiowq_fail; 17128030d343SEd Cashin } 17138030d343SEd Cashin 17148030d343SEd Cashin for (i = 0; i < ncpus; i++) { 17158030d343SEd Cashin INIT_LIST_HEAD(&iocq[i].head); 17168030d343SEd Cashin spin_lock_init(&iocq[i].lock); 17178030d343SEd Cashin init_waitqueue_head(&ktiowq[i]); 17188030d343SEd Cashin snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i); 17198030d343SEd Cashin kts[i].fn = ktio; 17208030d343SEd Cashin kts[i].waitq = &ktiowq[i]; 17218030d343SEd Cashin kts[i].lock = &iocq[i].lock; 17228030d343SEd Cashin kts[i].id = i; 17238030d343SEd Cashin kts[i].active = 0; 17248030d343SEd Cashin } 17258030d343SEd Cashin kts[0].active = 1; 17268030d343SEd Cashin if (aoe_ktstart(&kts[0])) { 17278030d343SEd Cashin ret = -ENOMEM; 17288030d343SEd Cashin goto ktstart_fail; 17298030d343SEd Cashin } 17308030d343SEd Cashin return 0; 17318030d343SEd Cashin 17328030d343SEd Cashin ktstart_fail: 17338030d343SEd Cashin kfree(ktiowq); 17348030d343SEd Cashin ktiowq_fail: 17358030d343SEd Cashin kfree(kts); 17368030d343SEd Cashin kts_fail: 17378030d343SEd Cashin kfree(iocq); 17388030d343SEd Cashin 17398030d343SEd Cashin return ret; 1740896831f5SEd Cashin } 1741896831f5SEd Cashin 1742896831f5SEd Cashin void 1743896831f5SEd Cashin aoecmd_exit(void) 1744896831f5SEd Cashin { 17458030d343SEd Cashin int i; 17468030d343SEd Cashin 17478030d343SEd Cashin for (i = 0; i < ncpus; i++) 17488030d343SEd Cashin if (kts[i].active) 17498030d343SEd Cashin aoe_ktstop(&kts[i]); 17508030d343SEd Cashin 175169cf2d85SEd Cashin aoe_flush_iocq(); 1752bbb44e30SEd Cashin 17538030d343SEd Cashin /* Free up the iocq and thread speicific configuration 17548030d343SEd Cashin * allocated during startup. 17558030d343SEd Cashin */ 17568030d343SEd Cashin kfree(iocq); 17578030d343SEd Cashin kfree(kts); 17588030d343SEd Cashin kfree(ktiowq); 17598030d343SEd Cashin 1760bbb44e30SEd Cashin free_page((unsigned long) page_address(empty_page)); 1761bbb44e30SEd Cashin empty_page = NULL; 1762896831f5SEd Cashin } 1763