1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */ 2 /* 3 * aoecmd.c 4 * Filesystem request handling methods 5 */ 6 7 #include <linux/ata.h> 8 #include <linux/slab.h> 9 #include <linux/hdreg.h> 10 #include <linux/blkdev.h> 11 #include <linux/skbuff.h> 12 #include <linux/netdevice.h> 13 #include <linux/genhd.h> 14 #include <linux/moduleparam.h> 15 #include <linux/workqueue.h> 16 #include <linux/kthread.h> 17 #include <net/net_namespace.h> 18 #include <asm/unaligned.h> 19 #include <linux/uio.h> 20 #include "aoe.h" 21 22 #define MAXIOC (8192) /* default meant to avoid most soft lockups */ 23 24 static void ktcomplete(struct frame *, struct sk_buff *); 25 static int count_targets(struct aoedev *d, int *untainted); 26 27 static struct buf *nextbuf(struct aoedev *); 28 29 static int aoe_deadsecs = 60 * 3; 30 module_param(aoe_deadsecs, int, 0644); 31 MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev."); 32 33 static int aoe_maxout = 64; 34 module_param(aoe_maxout, int, 0644); 35 MODULE_PARM_DESC(aoe_maxout, 36 "Only aoe_maxout outstanding packets for every MAC on eX.Y."); 37 38 static wait_queue_head_t ktiowq; 39 static struct ktstate kts; 40 41 /* io completion queue */ 42 static struct { 43 struct list_head head; 44 spinlock_t lock; 45 } iocq; 46 47 static struct page *empty_page; 48 49 static struct sk_buff * 50 new_skb(ulong len) 51 { 52 struct sk_buff *skb; 53 54 skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC); 55 if (skb) { 56 skb_reserve(skb, MAX_HEADER); 57 skb_reset_mac_header(skb); 58 skb_reset_network_header(skb); 59 skb->protocol = __constant_htons(ETH_P_AOE); 60 skb_checksum_none_assert(skb); 61 } 62 return skb; 63 } 64 65 static struct frame * 66 getframe_deferred(struct aoedev *d, u32 tag) 67 { 68 struct list_head *head, *pos, *nx; 69 struct frame *f; 70 71 head = &d->rexmitq; 72 list_for_each_safe(pos, nx, head) { 73 f = list_entry(pos, struct frame, head); 74 if (f->tag == tag) { 75 list_del(pos); 76 return f; 77 } 78 } 79 return NULL; 80 } 81 82 static struct frame * 83 getframe(struct aoedev *d, u32 tag) 84 { 85 struct frame *f; 86 struct list_head *head, *pos, *nx; 87 u32 n; 88 89 n = tag % NFACTIVE; 90 head = &d->factive[n]; 91 list_for_each_safe(pos, nx, head) { 92 f = list_entry(pos, struct frame, head); 93 if (f->tag == tag) { 94 list_del(pos); 95 return f; 96 } 97 } 98 return NULL; 99 } 100 101 /* 102 * Leave the top bit clear so we have tagspace for userland. 103 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing. 104 * This driver reserves tag -1 to mean "unused frame." 105 */ 106 static int 107 newtag(struct aoedev *d) 108 { 109 register ulong n; 110 111 n = jiffies & 0xffff; 112 return n |= (++d->lasttag & 0x7fff) << 16; 113 } 114 115 static u32 116 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) 117 { 118 u32 host_tag = newtag(d); 119 120 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 121 memcpy(h->dst, t->addr, sizeof h->dst); 122 h->type = __constant_cpu_to_be16(ETH_P_AOE); 123 h->verfl = AOE_HVER; 124 h->major = cpu_to_be16(d->aoemajor); 125 h->minor = d->aoeminor; 126 h->cmd = AOECMD_ATA; 127 h->tag = cpu_to_be32(host_tag); 128 129 return host_tag; 130 } 131 132 static inline void 133 put_lba(struct aoe_atahdr *ah, sector_t lba) 134 { 135 ah->lba0 = lba; 136 ah->lba1 = lba >>= 8; 137 ah->lba2 = lba >>= 8; 138 ah->lba3 = lba >>= 8; 139 ah->lba4 = lba >>= 8; 140 ah->lba5 = lba >>= 8; 141 } 142 143 static struct aoeif * 144 ifrotate(struct aoetgt *t) 145 { 146 struct aoeif *ifp; 147 148 ifp = t->ifp; 149 ifp++; 150 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL) 151 ifp = t->ifs; 152 if (ifp->nd == NULL) 153 return NULL; 154 return t->ifp = ifp; 155 } 156 157 static void 158 skb_pool_put(struct aoedev *d, struct sk_buff *skb) 159 { 160 __skb_queue_tail(&d->skbpool, skb); 161 } 162 163 static struct sk_buff * 164 skb_pool_get(struct aoedev *d) 165 { 166 struct sk_buff *skb = skb_peek(&d->skbpool); 167 168 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { 169 __skb_unlink(skb, &d->skbpool); 170 return skb; 171 } 172 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && 173 (skb = new_skb(ETH_ZLEN))) 174 return skb; 175 176 return NULL; 177 } 178 179 void 180 aoe_freetframe(struct frame *f) 181 { 182 struct aoetgt *t; 183 184 t = f->t; 185 f->buf = NULL; 186 f->lba = 0; 187 f->bv = NULL; 188 f->r_skb = NULL; 189 f->flags = 0; 190 list_add(&f->head, &t->ffree); 191 } 192 193 static struct frame * 194 newtframe(struct aoedev *d, struct aoetgt *t) 195 { 196 struct frame *f; 197 struct sk_buff *skb; 198 struct list_head *pos; 199 200 if (list_empty(&t->ffree)) { 201 if (t->falloc >= NSKBPOOLMAX*2) 202 return NULL; 203 f = kcalloc(1, sizeof(*f), GFP_ATOMIC); 204 if (f == NULL) 205 return NULL; 206 t->falloc++; 207 f->t = t; 208 } else { 209 pos = t->ffree.next; 210 list_del(pos); 211 f = list_entry(pos, struct frame, head); 212 } 213 214 skb = f->skb; 215 if (skb == NULL) { 216 f->skb = skb = new_skb(ETH_ZLEN); 217 if (!skb) { 218 bail: aoe_freetframe(f); 219 return NULL; 220 } 221 } 222 223 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) { 224 skb = skb_pool_get(d); 225 if (skb == NULL) 226 goto bail; 227 skb_pool_put(d, f->skb); 228 f->skb = skb; 229 } 230 231 skb->truesize -= skb->data_len; 232 skb_shinfo(skb)->nr_frags = skb->data_len = 0; 233 skb_trim(skb, 0); 234 return f; 235 } 236 237 static struct frame * 238 newframe(struct aoedev *d) 239 { 240 struct frame *f; 241 struct aoetgt *t, **tt; 242 int totout = 0; 243 int use_tainted; 244 int has_untainted; 245 246 if (!d->targets || !d->targets[0]) { 247 printk(KERN_ERR "aoe: NULL TARGETS!\n"); 248 return NULL; 249 } 250 tt = d->tgt; /* last used target */ 251 for (use_tainted = 0, has_untainted = 0;;) { 252 tt++; 253 if (tt >= &d->targets[d->ntargets] || !*tt) 254 tt = d->targets; 255 t = *tt; 256 if (!t->taint) { 257 has_untainted = 1; 258 totout += t->nout; 259 } 260 if (t->nout < t->maxout 261 && (use_tainted || !t->taint) 262 && t->ifp->nd) { 263 f = newtframe(d, t); 264 if (f) { 265 ifrotate(t); 266 d->tgt = tt; 267 return f; 268 } 269 } 270 if (tt == d->tgt) { /* we've looped and found nada */ 271 if (!use_tainted && !has_untainted) 272 use_tainted = 1; 273 else 274 break; 275 } 276 } 277 if (totout == 0) { 278 d->kicked++; 279 d->flags |= DEVFL_KICKME; 280 } 281 return NULL; 282 } 283 284 static void 285 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt) 286 { 287 int frag = 0; 288 ulong fcnt; 289 loop: 290 fcnt = bv->bv_len - (off - bv->bv_offset); 291 if (fcnt > cnt) 292 fcnt = cnt; 293 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt); 294 cnt -= fcnt; 295 if (cnt <= 0) 296 return; 297 bv++; 298 off = bv->bv_offset; 299 goto loop; 300 } 301 302 static void 303 fhash(struct frame *f) 304 { 305 struct aoedev *d = f->t->d; 306 u32 n; 307 308 n = f->tag % NFACTIVE; 309 list_add_tail(&f->head, &d->factive[n]); 310 } 311 312 static void 313 ata_rw_frameinit(struct frame *f) 314 { 315 struct aoetgt *t; 316 struct aoe_hdr *h; 317 struct aoe_atahdr *ah; 318 struct sk_buff *skb; 319 char writebit, extbit; 320 321 skb = f->skb; 322 h = (struct aoe_hdr *) skb_mac_header(skb); 323 ah = (struct aoe_atahdr *) (h + 1); 324 skb_put(skb, sizeof(*h) + sizeof(*ah)); 325 memset(h, 0, skb->len); 326 327 writebit = 0x10; 328 extbit = 0x4; 329 330 t = f->t; 331 f->tag = aoehdr_atainit(t->d, t, h); 332 fhash(f); 333 t->nout++; 334 f->waited = 0; 335 f->waited_total = 0; 336 if (f->buf) 337 f->lba = f->buf->sector; 338 339 /* set up ata header */ 340 ah->scnt = f->bcnt >> 9; 341 put_lba(ah, f->lba); 342 if (t->d->flags & DEVFL_EXT) { 343 ah->aflags |= AOEAFL_EXT; 344 } else { 345 extbit = 0; 346 ah->lba3 &= 0x0f; 347 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ 348 } 349 if (f->buf && bio_data_dir(f->buf->bio) == WRITE) { 350 skb_fillup(skb, f->bv, f->bv_off, f->bcnt); 351 ah->aflags |= AOEAFL_WRITE; 352 skb->len += f->bcnt; 353 skb->data_len = f->bcnt; 354 skb->truesize += f->bcnt; 355 t->wpkts++; 356 } else { 357 t->rpkts++; 358 writebit = 0; 359 } 360 361 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; 362 skb->dev = t->ifp->nd; 363 } 364 365 static int 366 aoecmd_ata_rw(struct aoedev *d) 367 { 368 struct frame *f; 369 struct buf *buf; 370 struct aoetgt *t; 371 struct sk_buff *skb; 372 struct sk_buff_head queue; 373 ulong bcnt, fbcnt; 374 375 buf = nextbuf(d); 376 if (buf == NULL) 377 return 0; 378 f = newframe(d); 379 if (f == NULL) 380 return 0; 381 t = *d->tgt; 382 bcnt = d->maxbcnt; 383 if (bcnt == 0) 384 bcnt = DEFAULTBCNT; 385 if (bcnt > buf->resid) 386 bcnt = buf->resid; 387 fbcnt = bcnt; 388 f->bv = buf->bv; 389 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid); 390 do { 391 if (fbcnt < buf->bv_resid) { 392 buf->bv_resid -= fbcnt; 393 buf->resid -= fbcnt; 394 break; 395 } 396 fbcnt -= buf->bv_resid; 397 buf->resid -= buf->bv_resid; 398 if (buf->resid == 0) { 399 d->ip.buf = NULL; 400 break; 401 } 402 buf->bv++; 403 buf->bv_resid = buf->bv->bv_len; 404 WARN_ON(buf->bv_resid == 0); 405 } while (fbcnt); 406 407 /* initialize the headers & frame */ 408 f->buf = buf; 409 f->bcnt = bcnt; 410 ata_rw_frameinit(f); 411 412 /* mark all tracking fields and load out */ 413 buf->nframesout += 1; 414 buf->sector += bcnt >> 9; 415 416 skb = skb_clone(f->skb, GFP_ATOMIC); 417 if (skb) { 418 do_gettimeofday(&f->sent); 419 f->sent_jiffs = (u32) jiffies; 420 __skb_queue_head_init(&queue); 421 __skb_queue_tail(&queue, skb); 422 aoenet_xmit(&queue); 423 } 424 return 1; 425 } 426 427 /* some callers cannot sleep, and they can call this function, 428 * transmitting the packets later, when interrupts are on 429 */ 430 static void 431 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) 432 { 433 struct aoe_hdr *h; 434 struct aoe_cfghdr *ch; 435 struct sk_buff *skb; 436 struct net_device *ifp; 437 438 rcu_read_lock(); 439 for_each_netdev_rcu(&init_net, ifp) { 440 dev_hold(ifp); 441 if (!is_aoe_netif(ifp)) 442 goto cont; 443 444 skb = new_skb(sizeof *h + sizeof *ch); 445 if (skb == NULL) { 446 printk(KERN_INFO "aoe: skb alloc failure\n"); 447 goto cont; 448 } 449 skb_put(skb, sizeof *h + sizeof *ch); 450 skb->dev = ifp; 451 __skb_queue_tail(queue, skb); 452 h = (struct aoe_hdr *) skb_mac_header(skb); 453 memset(h, 0, sizeof *h + sizeof *ch); 454 455 memset(h->dst, 0xff, sizeof h->dst); 456 memcpy(h->src, ifp->dev_addr, sizeof h->src); 457 h->type = __constant_cpu_to_be16(ETH_P_AOE); 458 h->verfl = AOE_HVER; 459 h->major = cpu_to_be16(aoemajor); 460 h->minor = aoeminor; 461 h->cmd = AOECMD_CFG; 462 463 cont: 464 dev_put(ifp); 465 } 466 rcu_read_unlock(); 467 } 468 469 static void 470 resend(struct aoedev *d, struct frame *f) 471 { 472 struct sk_buff *skb; 473 struct sk_buff_head queue; 474 struct aoe_hdr *h; 475 struct aoe_atahdr *ah; 476 struct aoetgt *t; 477 char buf[128]; 478 u32 n; 479 480 t = f->t; 481 n = newtag(d); 482 skb = f->skb; 483 if (ifrotate(t) == NULL) { 484 /* probably can't happen, but set it up to fail anyway */ 485 pr_info("aoe: resend: no interfaces to rotate to.\n"); 486 ktcomplete(f, NULL); 487 return; 488 } 489 h = (struct aoe_hdr *) skb_mac_header(skb); 490 ah = (struct aoe_atahdr *) (h+1); 491 492 if (!(f->flags & FFL_PROBE)) { 493 snprintf(buf, sizeof(buf), 494 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", 495 "retransmit", d->aoemajor, d->aoeminor, 496 f->tag, jiffies, n, 497 h->src, h->dst, t->nout); 498 aoechr_error(buf); 499 } 500 501 f->tag = n; 502 fhash(f); 503 h->tag = cpu_to_be32(n); 504 memcpy(h->dst, t->addr, sizeof h->dst); 505 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); 506 507 skb->dev = t->ifp->nd; 508 skb = skb_clone(skb, GFP_ATOMIC); 509 if (skb == NULL) 510 return; 511 do_gettimeofday(&f->sent); 512 f->sent_jiffs = (u32) jiffies; 513 __skb_queue_head_init(&queue); 514 __skb_queue_tail(&queue, skb); 515 aoenet_xmit(&queue); 516 } 517 518 static int 519 tsince_hr(struct frame *f) 520 { 521 struct timeval now; 522 int n; 523 524 do_gettimeofday(&now); 525 n = now.tv_usec - f->sent.tv_usec; 526 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC; 527 528 if (n < 0) 529 n = -n; 530 531 /* For relatively long periods, use jiffies to avoid 532 * discrepancies caused by updates to the system time. 533 * 534 * On system with HZ of 1000, 32-bits is over 49 days 535 * worth of jiffies, or over 71 minutes worth of usecs. 536 * 537 * Jiffies overflow is handled by subtraction of unsigned ints: 538 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe 539 * $3 = 4 540 * (gdb) 541 */ 542 if (n > USEC_PER_SEC / 4) { 543 n = ((u32) jiffies) - f->sent_jiffs; 544 n *= USEC_PER_SEC / HZ; 545 } 546 547 return n; 548 } 549 550 static int 551 tsince(u32 tag) 552 { 553 int n; 554 555 n = jiffies & 0xffff; 556 n -= tag & 0xffff; 557 if (n < 0) 558 n += 1<<16; 559 return jiffies_to_usecs(n + 1); 560 } 561 562 static struct aoeif * 563 getif(struct aoetgt *t, struct net_device *nd) 564 { 565 struct aoeif *p, *e; 566 567 p = t->ifs; 568 e = p + NAOEIFS; 569 for (; p < e; p++) 570 if (p->nd == nd) 571 return p; 572 return NULL; 573 } 574 575 static void 576 ejectif(struct aoetgt *t, struct aoeif *ifp) 577 { 578 struct aoeif *e; 579 struct net_device *nd; 580 ulong n; 581 582 nd = ifp->nd; 583 e = t->ifs + NAOEIFS - 1; 584 n = (e - ifp) * sizeof *ifp; 585 memmove(ifp, ifp+1, n); 586 e->nd = NULL; 587 dev_put(nd); 588 } 589 590 static struct frame * 591 reassign_frame(struct frame *f) 592 { 593 struct frame *nf; 594 struct sk_buff *skb; 595 596 nf = newframe(f->t->d); 597 if (!nf) 598 return NULL; 599 if (nf->t == f->t) { 600 aoe_freetframe(nf); 601 return NULL; 602 } 603 604 skb = nf->skb; 605 nf->skb = f->skb; 606 nf->buf = f->buf; 607 nf->bcnt = f->bcnt; 608 nf->lba = f->lba; 609 nf->bv = f->bv; 610 nf->bv_off = f->bv_off; 611 nf->waited = 0; 612 nf->waited_total = f->waited_total; 613 nf->sent = f->sent; 614 nf->sent_jiffs = f->sent_jiffs; 615 f->skb = skb; 616 617 return nf; 618 } 619 620 static void 621 probe(struct aoetgt *t) 622 { 623 struct aoedev *d; 624 struct frame *f; 625 struct sk_buff *skb; 626 struct sk_buff_head queue; 627 size_t n, m; 628 int frag; 629 630 d = t->d; 631 f = newtframe(d, t); 632 if (!f) { 633 pr_err("%s %pm for e%ld.%d: %s\n", 634 "aoe: cannot probe remote address", 635 t->addr, 636 (long) d->aoemajor, d->aoeminor, 637 "no frame available"); 638 return; 639 } 640 f->flags |= FFL_PROBE; 641 ifrotate(t); 642 f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT; 643 ata_rw_frameinit(f); 644 skb = f->skb; 645 for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) { 646 if (n < PAGE_SIZE) 647 m = n; 648 else 649 m = PAGE_SIZE; 650 skb_fill_page_desc(skb, frag, empty_page, 0, m); 651 } 652 skb->len += f->bcnt; 653 skb->data_len = f->bcnt; 654 skb->truesize += f->bcnt; 655 656 skb = skb_clone(f->skb, GFP_ATOMIC); 657 if (skb) { 658 do_gettimeofday(&f->sent); 659 f->sent_jiffs = (u32) jiffies; 660 __skb_queue_head_init(&queue); 661 __skb_queue_tail(&queue, skb); 662 aoenet_xmit(&queue); 663 } 664 } 665 666 static long 667 rto(struct aoedev *d) 668 { 669 long t; 670 671 t = 2 * d->rttavg >> RTTSCALE; 672 t += 8 * d->rttdev >> RTTDSCALE; 673 if (t == 0) 674 t = 1; 675 676 return t; 677 } 678 679 static void 680 rexmit_deferred(struct aoedev *d) 681 { 682 struct aoetgt *t; 683 struct frame *f; 684 struct frame *nf; 685 struct list_head *pos, *nx, *head; 686 int since; 687 int untainted; 688 689 count_targets(d, &untainted); 690 691 head = &d->rexmitq; 692 list_for_each_safe(pos, nx, head) { 693 f = list_entry(pos, struct frame, head); 694 t = f->t; 695 if (t->taint) { 696 if (!(f->flags & FFL_PROBE)) { 697 nf = reassign_frame(f); 698 if (nf) { 699 if (t->nout_probes == 0 700 && untainted > 0) { 701 probe(t); 702 t->nout_probes++; 703 } 704 list_replace(&f->head, &nf->head); 705 pos = &nf->head; 706 aoe_freetframe(f); 707 f = nf; 708 t = f->t; 709 } 710 } else if (untainted < 1) { 711 /* don't probe w/o other untainted aoetgts */ 712 goto stop_probe; 713 } else if (tsince_hr(f) < t->taint * rto(d)) { 714 /* reprobe slowly when taint is high */ 715 continue; 716 } 717 } else if (f->flags & FFL_PROBE) { 718 stop_probe: /* don't probe untainted aoetgts */ 719 list_del(pos); 720 aoe_freetframe(f); 721 /* leaving d->kicked, because this is routine */ 722 f->t->d->flags |= DEVFL_KICKME; 723 continue; 724 } 725 if (t->nout >= t->maxout) 726 continue; 727 list_del(pos); 728 t->nout++; 729 if (f->flags & FFL_PROBE) 730 t->nout_probes++; 731 since = tsince_hr(f); 732 f->waited += since; 733 f->waited_total += since; 734 resend(d, f); 735 } 736 } 737 738 /* An aoetgt accumulates demerits quickly, and successful 739 * probing redeems the aoetgt slowly. 740 */ 741 static void 742 scorn(struct aoetgt *t) 743 { 744 int n; 745 746 n = t->taint++; 747 t->taint += t->taint * 2; 748 if (n > t->taint) 749 t->taint = n; 750 if (t->taint > MAX_TAINT) 751 t->taint = MAX_TAINT; 752 } 753 754 static int 755 count_targets(struct aoedev *d, int *untainted) 756 { 757 int i, good; 758 759 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i) 760 if (d->targets[i]->taint == 0) 761 good++; 762 763 if (untainted) 764 *untainted = good; 765 return i; 766 } 767 768 static void 769 rexmit_timer(ulong vp) 770 { 771 struct aoedev *d; 772 struct aoetgt *t; 773 struct aoeif *ifp; 774 struct frame *f; 775 struct list_head *head, *pos, *nx; 776 LIST_HEAD(flist); 777 register long timeout; 778 ulong flags, n; 779 int i; 780 int utgts; /* number of aoetgt descriptors (not slots) */ 781 int since; 782 783 d = (struct aoedev *) vp; 784 785 spin_lock_irqsave(&d->lock, flags); 786 787 /* timeout based on observed timings and variations */ 788 timeout = rto(d); 789 790 utgts = count_targets(d, NULL); 791 792 if (d->flags & DEVFL_TKILL) { 793 spin_unlock_irqrestore(&d->lock, flags); 794 return; 795 } 796 797 /* collect all frames to rexmit into flist */ 798 for (i = 0; i < NFACTIVE; i++) { 799 head = &d->factive[i]; 800 list_for_each_safe(pos, nx, head) { 801 f = list_entry(pos, struct frame, head); 802 if (tsince_hr(f) < timeout) 803 break; /* end of expired frames */ 804 /* move to flist for later processing */ 805 list_move_tail(pos, &flist); 806 } 807 } 808 809 /* process expired frames */ 810 while (!list_empty(&flist)) { 811 pos = flist.next; 812 f = list_entry(pos, struct frame, head); 813 since = tsince_hr(f); 814 n = f->waited_total + since; 815 n /= USEC_PER_SEC; 816 if (aoe_deadsecs 817 && n > aoe_deadsecs 818 && !(f->flags & FFL_PROBE)) { 819 /* Waited too long. Device failure. 820 * Hang all frames on first hash bucket for downdev 821 * to clean up. 822 */ 823 list_splice(&flist, &d->factive[0]); 824 aoedev_downdev(d); 825 goto out; 826 } 827 828 t = f->t; 829 n = f->waited + since; 830 n /= USEC_PER_SEC; 831 if (aoe_deadsecs && utgts > 0 832 && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS)) 833 scorn(t); /* avoid this target */ 834 835 if (t->maxout != 1) { 836 t->ssthresh = t->maxout / 2; 837 t->maxout = 1; 838 } 839 840 if (f->flags & FFL_PROBE) { 841 t->nout_probes--; 842 } else { 843 ifp = getif(t, f->skb->dev); 844 if (ifp && ++ifp->lost > (t->nframes << 1) 845 && (ifp != t->ifs || t->ifs[1].nd)) { 846 ejectif(t, ifp); 847 ifp = NULL; 848 } 849 } 850 list_move_tail(pos, &d->rexmitq); 851 t->nout--; 852 } 853 rexmit_deferred(d); 854 855 out: 856 if ((d->flags & DEVFL_KICKME) && d->blkq) { 857 d->flags &= ~DEVFL_KICKME; 858 d->blkq->request_fn(d->blkq); 859 } 860 861 d->timer.expires = jiffies + TIMERTICK; 862 add_timer(&d->timer); 863 864 spin_unlock_irqrestore(&d->lock, flags); 865 } 866 867 static unsigned long 868 rqbiocnt(struct request *r) 869 { 870 struct bio *bio; 871 unsigned long n = 0; 872 873 __rq_for_each_bio(bio, r) 874 n++; 875 return n; 876 } 877 878 /* This can be removed if we are certain that no users of the block 879 * layer will ever use zero-count pages in bios. Otherwise we have to 880 * protect against the put_page sometimes done by the network layer. 881 * 882 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for 883 * discussion. 884 * 885 * We cannot use get_page in the workaround, because it insists on a 886 * positive page count as a precondition. So we use _count directly. 887 */ 888 static void 889 bio_pageinc(struct bio *bio) 890 { 891 struct bio_vec *bv; 892 struct page *page; 893 int i; 894 895 bio_for_each_segment(bv, bio, i) { 896 page = bv->bv_page; 897 /* Non-zero page count for non-head members of 898 * compound pages is no longer allowed by the kernel, 899 * but this has never been seen here. 900 */ 901 if (unlikely(PageCompound(page))) 902 if (compound_trans_head(page) != page) { 903 pr_crit("page tail used for block I/O\n"); 904 BUG(); 905 } 906 atomic_inc(&page->_count); 907 } 908 } 909 910 static void 911 bio_pagedec(struct bio *bio) 912 { 913 struct bio_vec *bv; 914 int i; 915 916 bio_for_each_segment(bv, bio, i) 917 atomic_dec(&bv->bv_page->_count); 918 } 919 920 static void 921 bufinit(struct buf *buf, struct request *rq, struct bio *bio) 922 { 923 memset(buf, 0, sizeof(*buf)); 924 buf->rq = rq; 925 buf->bio = bio; 926 buf->resid = bio->bi_size; 927 buf->sector = bio->bi_sector; 928 bio_pageinc(bio); 929 buf->bv = bio_iovec(bio); 930 buf->bv_resid = buf->bv->bv_len; 931 WARN_ON(buf->bv_resid == 0); 932 } 933 934 static struct buf * 935 nextbuf(struct aoedev *d) 936 { 937 struct request *rq; 938 struct request_queue *q; 939 struct buf *buf; 940 struct bio *bio; 941 942 q = d->blkq; 943 if (q == NULL) 944 return NULL; /* initializing */ 945 if (d->ip.buf) 946 return d->ip.buf; 947 rq = d->ip.rq; 948 if (rq == NULL) { 949 rq = blk_peek_request(q); 950 if (rq == NULL) 951 return NULL; 952 blk_start_request(rq); 953 d->ip.rq = rq; 954 d->ip.nxbio = rq->bio; 955 rq->special = (void *) rqbiocnt(rq); 956 } 957 buf = mempool_alloc(d->bufpool, GFP_ATOMIC); 958 if (buf == NULL) { 959 pr_err("aoe: nextbuf: unable to mempool_alloc!\n"); 960 return NULL; 961 } 962 bio = d->ip.nxbio; 963 bufinit(buf, rq, bio); 964 bio = bio->bi_next; 965 d->ip.nxbio = bio; 966 if (bio == NULL) 967 d->ip.rq = NULL; 968 return d->ip.buf = buf; 969 } 970 971 /* enters with d->lock held */ 972 void 973 aoecmd_work(struct aoedev *d) 974 { 975 rexmit_deferred(d); 976 while (aoecmd_ata_rw(d)) 977 ; 978 } 979 980 /* this function performs work that has been deferred until sleeping is OK 981 */ 982 void 983 aoecmd_sleepwork(struct work_struct *work) 984 { 985 struct aoedev *d = container_of(work, struct aoedev, work); 986 struct block_device *bd; 987 u64 ssize; 988 989 if (d->flags & DEVFL_GDALLOC) 990 aoeblk_gdalloc(d); 991 992 if (d->flags & DEVFL_NEWSIZE) { 993 ssize = get_capacity(d->gd); 994 bd = bdget_disk(d->gd, 0); 995 if (bd) { 996 mutex_lock(&bd->bd_inode->i_mutex); 997 i_size_write(bd->bd_inode, (loff_t)ssize<<9); 998 mutex_unlock(&bd->bd_inode->i_mutex); 999 bdput(bd); 1000 } 1001 spin_lock_irq(&d->lock); 1002 d->flags |= DEVFL_UP; 1003 d->flags &= ~DEVFL_NEWSIZE; 1004 spin_unlock_irq(&d->lock); 1005 } 1006 } 1007 1008 static void 1009 ata_ident_fixstring(u16 *id, int ns) 1010 { 1011 u16 s; 1012 1013 while (ns-- > 0) { 1014 s = *id; 1015 *id++ = s >> 8 | s << 8; 1016 } 1017 } 1018 1019 static void 1020 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) 1021 { 1022 u64 ssize; 1023 u16 n; 1024 1025 /* word 83: command set supported */ 1026 n = get_unaligned_le16(&id[83 << 1]); 1027 1028 /* word 86: command set/feature enabled */ 1029 n |= get_unaligned_le16(&id[86 << 1]); 1030 1031 if (n & (1<<10)) { /* bit 10: LBA 48 */ 1032 d->flags |= DEVFL_EXT; 1033 1034 /* word 100: number lba48 sectors */ 1035 ssize = get_unaligned_le64(&id[100 << 1]); 1036 1037 /* set as in ide-disk.c:init_idedisk_capacity */ 1038 d->geo.cylinders = ssize; 1039 d->geo.cylinders /= (255 * 63); 1040 d->geo.heads = 255; 1041 d->geo.sectors = 63; 1042 } else { 1043 d->flags &= ~DEVFL_EXT; 1044 1045 /* number lba28 sectors */ 1046 ssize = get_unaligned_le32(&id[60 << 1]); 1047 1048 /* NOTE: obsolete in ATA 6 */ 1049 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); 1050 d->geo.heads = get_unaligned_le16(&id[55 << 1]); 1051 d->geo.sectors = get_unaligned_le16(&id[56 << 1]); 1052 } 1053 1054 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */ 1055 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */ 1056 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */ 1057 memcpy(d->ident, id, sizeof(d->ident)); 1058 1059 if (d->ssize != ssize) 1060 printk(KERN_INFO 1061 "aoe: %pm e%ld.%d v%04x has %llu sectors\n", 1062 t->addr, 1063 d->aoemajor, d->aoeminor, 1064 d->fw_ver, (long long)ssize); 1065 d->ssize = ssize; 1066 d->geo.start = 0; 1067 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) 1068 return; 1069 if (d->gd != NULL) { 1070 set_capacity(d->gd, ssize); 1071 d->flags |= DEVFL_NEWSIZE; 1072 } else 1073 d->flags |= DEVFL_GDALLOC; 1074 schedule_work(&d->work); 1075 } 1076 1077 static void 1078 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt) 1079 { 1080 register long n; 1081 1082 n = rtt; 1083 1084 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */ 1085 n -= d->rttavg >> RTTSCALE; 1086 d->rttavg += n; 1087 if (n < 0) 1088 n = -n; 1089 n -= d->rttdev >> RTTDSCALE; 1090 d->rttdev += n; 1091 1092 if (!t || t->maxout >= t->nframes) 1093 return; 1094 if (t->maxout < t->ssthresh) 1095 t->maxout += 1; 1096 else if (t->nout == t->maxout && t->next_cwnd-- == 0) { 1097 t->maxout += 1; 1098 t->next_cwnd = t->maxout; 1099 } 1100 } 1101 1102 static struct aoetgt * 1103 gettgt(struct aoedev *d, char *addr) 1104 { 1105 struct aoetgt **t, **e; 1106 1107 t = d->targets; 1108 e = t + d->ntargets; 1109 for (; t < e && *t; t++) 1110 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) 1111 return *t; 1112 return NULL; 1113 } 1114 1115 static void 1116 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt) 1117 { 1118 ulong fcnt; 1119 char *p; 1120 int soff = 0; 1121 loop: 1122 fcnt = bv->bv_len - (off - bv->bv_offset); 1123 if (fcnt > cnt) 1124 fcnt = cnt; 1125 p = page_address(bv->bv_page) + off; 1126 skb_copy_bits(skb, soff, p, fcnt); 1127 soff += fcnt; 1128 cnt -= fcnt; 1129 if (cnt <= 0) 1130 return; 1131 bv++; 1132 off = bv->bv_offset; 1133 goto loop; 1134 } 1135 1136 void 1137 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail) 1138 { 1139 struct bio *bio; 1140 int bok; 1141 struct request_queue *q; 1142 1143 q = d->blkq; 1144 if (rq == d->ip.rq) 1145 d->ip.rq = NULL; 1146 do { 1147 bio = rq->bio; 1148 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags); 1149 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size)); 1150 1151 /* cf. http://lkml.org/lkml/2006/10/31/28 */ 1152 if (!fastfail) 1153 __blk_run_queue(q); 1154 } 1155 1156 static void 1157 aoe_end_buf(struct aoedev *d, struct buf *buf) 1158 { 1159 struct request *rq; 1160 unsigned long n; 1161 1162 if (buf == d->ip.buf) 1163 d->ip.buf = NULL; 1164 rq = buf->rq; 1165 bio_pagedec(buf->bio); 1166 mempool_free(buf, d->bufpool); 1167 n = (unsigned long) rq->special; 1168 rq->special = (void *) --n; 1169 if (n == 0) 1170 aoe_end_request(d, rq, 0); 1171 } 1172 1173 static void 1174 ktiocomplete(struct frame *f) 1175 { 1176 struct aoe_hdr *hin, *hout; 1177 struct aoe_atahdr *ahin, *ahout; 1178 struct buf *buf; 1179 struct sk_buff *skb; 1180 struct aoetgt *t; 1181 struct aoeif *ifp; 1182 struct aoedev *d; 1183 long n; 1184 int untainted; 1185 1186 if (f == NULL) 1187 return; 1188 1189 t = f->t; 1190 d = t->d; 1191 skb = f->r_skb; 1192 buf = f->buf; 1193 if (f->flags & FFL_PROBE) 1194 goto out; 1195 if (!skb) /* just fail the buf. */ 1196 goto noskb; 1197 1198 hout = (struct aoe_hdr *) skb_mac_header(f->skb); 1199 ahout = (struct aoe_atahdr *) (hout+1); 1200 1201 hin = (struct aoe_hdr *) skb->data; 1202 skb_pull(skb, sizeof(*hin)); 1203 ahin = (struct aoe_atahdr *) skb->data; 1204 skb_pull(skb, sizeof(*ahin)); 1205 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ 1206 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", 1207 ahout->cmdstat, ahin->cmdstat, 1208 d->aoemajor, d->aoeminor); 1209 noskb: if (buf) 1210 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 1211 goto out; 1212 } 1213 1214 n = ahout->scnt << 9; 1215 switch (ahout->cmdstat) { 1216 case ATA_CMD_PIO_READ: 1217 case ATA_CMD_PIO_READ_EXT: 1218 if (skb->len < n) { 1219 pr_err("%s e%ld.%d. skb->len=%d need=%ld\n", 1220 "aoe: runt data size in read from", 1221 (long) d->aoemajor, d->aoeminor, 1222 skb->len, n); 1223 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 1224 break; 1225 } 1226 bvcpy(f->bv, f->bv_off, skb, n); 1227 case ATA_CMD_PIO_WRITE: 1228 case ATA_CMD_PIO_WRITE_EXT: 1229 spin_lock_irq(&d->lock); 1230 ifp = getif(t, skb->dev); 1231 if (ifp) 1232 ifp->lost = 0; 1233 spin_unlock_irq(&d->lock); 1234 break; 1235 case ATA_CMD_ID_ATA: 1236 if (skb->len < 512) { 1237 pr_info("%s e%ld.%d. skb->len=%d need=512\n", 1238 "aoe: runt data size in ataid from", 1239 (long) d->aoemajor, d->aoeminor, 1240 skb->len); 1241 break; 1242 } 1243 if (skb_linearize(skb)) 1244 break; 1245 spin_lock_irq(&d->lock); 1246 ataid_complete(d, t, skb->data); 1247 spin_unlock_irq(&d->lock); 1248 break; 1249 default: 1250 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n", 1251 ahout->cmdstat, 1252 be16_to_cpu(get_unaligned(&hin->major)), 1253 hin->minor); 1254 } 1255 out: 1256 spin_lock_irq(&d->lock); 1257 if (t->taint > 0 1258 && --t->taint > 0 1259 && t->nout_probes == 0) { 1260 count_targets(d, &untainted); 1261 if (untainted > 0) { 1262 probe(t); 1263 t->nout_probes++; 1264 } 1265 } 1266 1267 aoe_freetframe(f); 1268 1269 if (buf && --buf->nframesout == 0 && buf->resid == 0) 1270 aoe_end_buf(d, buf); 1271 1272 spin_unlock_irq(&d->lock); 1273 aoedev_put(d); 1274 dev_kfree_skb(skb); 1275 } 1276 1277 /* Enters with iocq.lock held. 1278 * Returns true iff responses needing processing remain. 1279 */ 1280 static int 1281 ktio(void) 1282 { 1283 struct frame *f; 1284 struct list_head *pos; 1285 int i; 1286 1287 for (i = 0; ; ++i) { 1288 if (i == MAXIOC) 1289 return 1; 1290 if (list_empty(&iocq.head)) 1291 return 0; 1292 pos = iocq.head.next; 1293 list_del(pos); 1294 spin_unlock_irq(&iocq.lock); 1295 f = list_entry(pos, struct frame, head); 1296 ktiocomplete(f); 1297 spin_lock_irq(&iocq.lock); 1298 } 1299 } 1300 1301 static int 1302 kthread(void *vp) 1303 { 1304 struct ktstate *k; 1305 DECLARE_WAITQUEUE(wait, current); 1306 int more; 1307 1308 k = vp; 1309 current->flags |= PF_NOFREEZE; 1310 set_user_nice(current, -10); 1311 complete(&k->rendez); /* tell spawner we're running */ 1312 do { 1313 spin_lock_irq(k->lock); 1314 more = k->fn(); 1315 if (!more) { 1316 add_wait_queue(k->waitq, &wait); 1317 __set_current_state(TASK_INTERRUPTIBLE); 1318 } 1319 spin_unlock_irq(k->lock); 1320 if (!more) { 1321 schedule(); 1322 remove_wait_queue(k->waitq, &wait); 1323 } else 1324 cond_resched(); 1325 } while (!kthread_should_stop()); 1326 complete(&k->rendez); /* tell spawner we're stopping */ 1327 return 0; 1328 } 1329 1330 void 1331 aoe_ktstop(struct ktstate *k) 1332 { 1333 kthread_stop(k->task); 1334 wait_for_completion(&k->rendez); 1335 } 1336 1337 int 1338 aoe_ktstart(struct ktstate *k) 1339 { 1340 struct task_struct *task; 1341 1342 init_completion(&k->rendez); 1343 task = kthread_run(kthread, k, k->name); 1344 if (task == NULL || IS_ERR(task)) 1345 return -ENOMEM; 1346 k->task = task; 1347 wait_for_completion(&k->rendez); /* allow kthread to start */ 1348 init_completion(&k->rendez); /* for waiting for exit later */ 1349 return 0; 1350 } 1351 1352 /* pass it off to kthreads for processing */ 1353 static void 1354 ktcomplete(struct frame *f, struct sk_buff *skb) 1355 { 1356 ulong flags; 1357 1358 f->r_skb = skb; 1359 spin_lock_irqsave(&iocq.lock, flags); 1360 list_add_tail(&f->head, &iocq.head); 1361 spin_unlock_irqrestore(&iocq.lock, flags); 1362 wake_up(&ktiowq); 1363 } 1364 1365 struct sk_buff * 1366 aoecmd_ata_rsp(struct sk_buff *skb) 1367 { 1368 struct aoedev *d; 1369 struct aoe_hdr *h; 1370 struct frame *f; 1371 u32 n; 1372 ulong flags; 1373 char ebuf[128]; 1374 u16 aoemajor; 1375 1376 h = (struct aoe_hdr *) skb->data; 1377 aoemajor = be16_to_cpu(get_unaligned(&h->major)); 1378 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0); 1379 if (d == NULL) { 1380 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " 1381 "for unknown device %d.%d\n", 1382 aoemajor, h->minor); 1383 aoechr_error(ebuf); 1384 return skb; 1385 } 1386 1387 spin_lock_irqsave(&d->lock, flags); 1388 1389 n = be32_to_cpu(get_unaligned(&h->tag)); 1390 f = getframe(d, n); 1391 if (f) { 1392 calc_rttavg(d, f->t, tsince_hr(f)); 1393 f->t->nout--; 1394 if (f->flags & FFL_PROBE) 1395 f->t->nout_probes--; 1396 } else { 1397 f = getframe_deferred(d, n); 1398 if (f) { 1399 calc_rttavg(d, NULL, tsince_hr(f)); 1400 } else { 1401 calc_rttavg(d, NULL, tsince(n)); 1402 spin_unlock_irqrestore(&d->lock, flags); 1403 aoedev_put(d); 1404 snprintf(ebuf, sizeof(ebuf), 1405 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n", 1406 "unexpected rsp", 1407 get_unaligned_be16(&h->major), 1408 h->minor, 1409 get_unaligned_be32(&h->tag), 1410 jiffies, 1411 h->src, 1412 h->dst); 1413 aoechr_error(ebuf); 1414 return skb; 1415 } 1416 } 1417 aoecmd_work(d); 1418 1419 spin_unlock_irqrestore(&d->lock, flags); 1420 1421 ktcomplete(f, skb); 1422 1423 /* 1424 * Note here that we do not perform an aoedev_put, as we are 1425 * leaving this reference for the ktio to release. 1426 */ 1427 return NULL; 1428 } 1429 1430 void 1431 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) 1432 { 1433 struct sk_buff_head queue; 1434 1435 __skb_queue_head_init(&queue); 1436 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); 1437 aoenet_xmit(&queue); 1438 } 1439 1440 struct sk_buff * 1441 aoecmd_ata_id(struct aoedev *d) 1442 { 1443 struct aoe_hdr *h; 1444 struct aoe_atahdr *ah; 1445 struct frame *f; 1446 struct sk_buff *skb; 1447 struct aoetgt *t; 1448 1449 f = newframe(d); 1450 if (f == NULL) 1451 return NULL; 1452 1453 t = *d->tgt; 1454 1455 /* initialize the headers & frame */ 1456 skb = f->skb; 1457 h = (struct aoe_hdr *) skb_mac_header(skb); 1458 ah = (struct aoe_atahdr *) (h+1); 1459 skb_put(skb, sizeof *h + sizeof *ah); 1460 memset(h, 0, skb->len); 1461 f->tag = aoehdr_atainit(d, t, h); 1462 fhash(f); 1463 t->nout++; 1464 f->waited = 0; 1465 f->waited_total = 0; 1466 1467 /* set up ata header */ 1468 ah->scnt = 1; 1469 ah->cmdstat = ATA_CMD_ID_ATA; 1470 ah->lba3 = 0xa0; 1471 1472 skb->dev = t->ifp->nd; 1473 1474 d->rttavg = RTTAVG_INIT; 1475 d->rttdev = RTTDEV_INIT; 1476 d->timer.function = rexmit_timer; 1477 1478 skb = skb_clone(skb, GFP_ATOMIC); 1479 if (skb) { 1480 do_gettimeofday(&f->sent); 1481 f->sent_jiffs = (u32) jiffies; 1482 } 1483 1484 return skb; 1485 } 1486 1487 static struct aoetgt ** 1488 grow_targets(struct aoedev *d) 1489 { 1490 ulong oldn, newn; 1491 struct aoetgt **tt; 1492 1493 oldn = d->ntargets; 1494 newn = oldn * 2; 1495 tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC); 1496 if (!tt) 1497 return NULL; 1498 memmove(tt, d->targets, sizeof(*d->targets) * oldn); 1499 d->tgt = tt + (d->tgt - d->targets); 1500 kfree(d->targets); 1501 d->targets = tt; 1502 d->ntargets = newn; 1503 1504 return &d->targets[oldn]; 1505 } 1506 1507 static struct aoetgt * 1508 addtgt(struct aoedev *d, char *addr, ulong nframes) 1509 { 1510 struct aoetgt *t, **tt, **te; 1511 1512 tt = d->targets; 1513 te = tt + d->ntargets; 1514 for (; tt < te && *tt; tt++) 1515 ; 1516 1517 if (tt == te) { 1518 tt = grow_targets(d); 1519 if (!tt) 1520 goto nomem; 1521 } 1522 t = kzalloc(sizeof(*t), GFP_ATOMIC); 1523 if (!t) 1524 goto nomem; 1525 t->nframes = nframes; 1526 t->d = d; 1527 memcpy(t->addr, addr, sizeof t->addr); 1528 t->ifp = t->ifs; 1529 aoecmd_wreset(t); 1530 t->maxout = t->nframes / 2; 1531 INIT_LIST_HEAD(&t->ffree); 1532 return *tt = t; 1533 1534 nomem: 1535 pr_info("aoe: cannot allocate memory to add target\n"); 1536 return NULL; 1537 } 1538 1539 static void 1540 setdbcnt(struct aoedev *d) 1541 { 1542 struct aoetgt **t, **e; 1543 int bcnt = 0; 1544 1545 t = d->targets; 1546 e = t + d->ntargets; 1547 for (; t < e && *t; t++) 1548 if (bcnt == 0 || bcnt > (*t)->minbcnt) 1549 bcnt = (*t)->minbcnt; 1550 if (bcnt != d->maxbcnt) { 1551 d->maxbcnt = bcnt; 1552 pr_info("aoe: e%ld.%d: setting %d byte data frames\n", 1553 d->aoemajor, d->aoeminor, bcnt); 1554 } 1555 } 1556 1557 static void 1558 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt) 1559 { 1560 struct aoedev *d; 1561 struct aoeif *p, *e; 1562 int minbcnt; 1563 1564 d = t->d; 1565 minbcnt = bcnt; 1566 p = t->ifs; 1567 e = p + NAOEIFS; 1568 for (; p < e; p++) { 1569 if (p->nd == NULL) 1570 break; /* end of the valid interfaces */ 1571 if (p->nd == nd) { 1572 p->bcnt = bcnt; /* we're updating */ 1573 nd = NULL; 1574 } else if (minbcnt > p->bcnt) 1575 minbcnt = p->bcnt; /* find the min interface */ 1576 } 1577 if (nd) { 1578 if (p == e) { 1579 pr_err("aoe: device setifbcnt failure; too many interfaces.\n"); 1580 return; 1581 } 1582 dev_hold(nd); 1583 p->nd = nd; 1584 p->bcnt = bcnt; 1585 } 1586 t->minbcnt = minbcnt; 1587 setdbcnt(d); 1588 } 1589 1590 void 1591 aoecmd_cfg_rsp(struct sk_buff *skb) 1592 { 1593 struct aoedev *d; 1594 struct aoe_hdr *h; 1595 struct aoe_cfghdr *ch; 1596 struct aoetgt *t; 1597 ulong flags, aoemajor; 1598 struct sk_buff *sl; 1599 struct sk_buff_head queue; 1600 u16 n; 1601 1602 sl = NULL; 1603 h = (struct aoe_hdr *) skb_mac_header(skb); 1604 ch = (struct aoe_cfghdr *) (h+1); 1605 1606 /* 1607 * Enough people have their dip switches set backwards to 1608 * warrant a loud message for this special case. 1609 */ 1610 aoemajor = get_unaligned_be16(&h->major); 1611 if (aoemajor == 0xfff) { 1612 printk(KERN_ERR "aoe: Warning: shelf address is all ones. " 1613 "Check shelf dip switches.\n"); 1614 return; 1615 } 1616 if (aoemajor == 0xffff) { 1617 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n", 1618 aoemajor, (int) h->minor); 1619 return; 1620 } 1621 if (h->minor == 0xff) { 1622 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n", 1623 aoemajor, (int) h->minor); 1624 return; 1625 } 1626 1627 n = be16_to_cpu(ch->bufcnt); 1628 if (n > aoe_maxout) /* keep it reasonable */ 1629 n = aoe_maxout; 1630 1631 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1); 1632 if (d == NULL) { 1633 pr_info("aoe: device allocation failure\n"); 1634 return; 1635 } 1636 1637 spin_lock_irqsave(&d->lock, flags); 1638 1639 t = gettgt(d, h->src); 1640 if (t) { 1641 t->nframes = n; 1642 if (n < t->maxout) 1643 aoecmd_wreset(t); 1644 } else { 1645 t = addtgt(d, h->src, n); 1646 if (!t) 1647 goto bail; 1648 } 1649 n = skb->dev->mtu; 1650 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr); 1651 n /= 512; 1652 if (n > ch->scnt) 1653 n = ch->scnt; 1654 n = n ? n * 512 : DEFAULTBCNT; 1655 setifbcnt(t, skb->dev, n); 1656 1657 /* don't change users' perspective */ 1658 if (d->nopen == 0) { 1659 d->fw_ver = be16_to_cpu(ch->fwver); 1660 sl = aoecmd_ata_id(d); 1661 } 1662 bail: 1663 spin_unlock_irqrestore(&d->lock, flags); 1664 aoedev_put(d); 1665 if (sl) { 1666 __skb_queue_head_init(&queue); 1667 __skb_queue_tail(&queue, sl); 1668 aoenet_xmit(&queue); 1669 } 1670 } 1671 1672 void 1673 aoecmd_wreset(struct aoetgt *t) 1674 { 1675 t->maxout = 1; 1676 t->ssthresh = t->nframes / 2; 1677 t->next_cwnd = t->nframes; 1678 } 1679 1680 void 1681 aoecmd_cleanslate(struct aoedev *d) 1682 { 1683 struct aoetgt **t, **te; 1684 1685 d->rttavg = RTTAVG_INIT; 1686 d->rttdev = RTTDEV_INIT; 1687 d->maxbcnt = 0; 1688 1689 t = d->targets; 1690 te = t + d->ntargets; 1691 for (; t < te && *t; t++) 1692 aoecmd_wreset(*t); 1693 } 1694 1695 void 1696 aoe_failbuf(struct aoedev *d, struct buf *buf) 1697 { 1698 if (buf == NULL) 1699 return; 1700 buf->resid = 0; 1701 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); 1702 if (buf->nframesout == 0) 1703 aoe_end_buf(d, buf); 1704 } 1705 1706 void 1707 aoe_flush_iocq(void) 1708 { 1709 struct frame *f; 1710 struct aoedev *d; 1711 LIST_HEAD(flist); 1712 struct list_head *pos; 1713 struct sk_buff *skb; 1714 ulong flags; 1715 1716 spin_lock_irqsave(&iocq.lock, flags); 1717 list_splice_init(&iocq.head, &flist); 1718 spin_unlock_irqrestore(&iocq.lock, flags); 1719 while (!list_empty(&flist)) { 1720 pos = flist.next; 1721 list_del(pos); 1722 f = list_entry(pos, struct frame, head); 1723 d = f->t->d; 1724 skb = f->r_skb; 1725 spin_lock_irqsave(&d->lock, flags); 1726 if (f->buf) { 1727 f->buf->nframesout--; 1728 aoe_failbuf(d, f->buf); 1729 } 1730 aoe_freetframe(f); 1731 spin_unlock_irqrestore(&d->lock, flags); 1732 dev_kfree_skb(skb); 1733 aoedev_put(d); 1734 } 1735 } 1736 1737 int __init 1738 aoecmd_init(void) 1739 { 1740 void *p; 1741 1742 /* get_zeroed_page returns page with ref count 1 */ 1743 p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT); 1744 if (!p) 1745 return -ENOMEM; 1746 empty_page = virt_to_page(p); 1747 1748 INIT_LIST_HEAD(&iocq.head); 1749 spin_lock_init(&iocq.lock); 1750 init_waitqueue_head(&ktiowq); 1751 kts.name = "aoe_ktio"; 1752 kts.fn = ktio; 1753 kts.waitq = &ktiowq; 1754 kts.lock = &iocq.lock; 1755 return aoe_ktstart(&kts); 1756 } 1757 1758 void 1759 aoecmd_exit(void) 1760 { 1761 aoe_ktstop(&kts); 1762 aoe_flush_iocq(); 1763 1764 free_page((unsigned long) page_address(empty_page)); 1765 empty_page = NULL; 1766 } 1767