aoe: for performance support larger packet payloads
[deliverable/linux.git] / drivers / block / aoe / aoecmd.c
1 /* Copyright (c) 2007 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 <net/net_namespace.h>
16 #include <asm/unaligned.h>
17 #include "aoe.h"
18
19 static int aoe_deadsecs = 60 * 3;
20 module_param(aoe_deadsecs, int, 0644);
21 MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
22
23 static int aoe_maxout = 16;
24 module_param(aoe_maxout, int, 0644);
25 MODULE_PARM_DESC(aoe_maxout,
26 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
27
28 static struct sk_buff *
29 new_skb(ulong len)
30 {
31 struct sk_buff *skb;
32
33 skb = alloc_skb(len, GFP_ATOMIC);
34 if (skb) {
35 skb_reset_mac_header(skb);
36 skb_reset_network_header(skb);
37 skb->protocol = __constant_htons(ETH_P_AOE);
38 skb_checksum_none_assert(skb);
39 }
40 return skb;
41 }
42
43 static struct frame *
44 getframe(struct aoetgt *t, int tag)
45 {
46 struct frame *f, *e;
47
48 f = t->frames;
49 e = f + t->nframes;
50 for (; f<e; f++)
51 if (f->tag == tag)
52 return f;
53 return NULL;
54 }
55
56 /*
57 * Leave the top bit clear so we have tagspace for userland.
58 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
59 * This driver reserves tag -1 to mean "unused frame."
60 */
61 static int
62 newtag(struct aoetgt *t)
63 {
64 register ulong n;
65
66 n = jiffies & 0xffff;
67 return n |= (++t->lasttag & 0x7fff) << 16;
68 }
69
70 static int
71 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
72 {
73 u32 host_tag = newtag(t);
74
75 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
76 memcpy(h->dst, t->addr, sizeof h->dst);
77 h->type = __constant_cpu_to_be16(ETH_P_AOE);
78 h->verfl = AOE_HVER;
79 h->major = cpu_to_be16(d->aoemajor);
80 h->minor = d->aoeminor;
81 h->cmd = AOECMD_ATA;
82 h->tag = cpu_to_be32(host_tag);
83
84 return host_tag;
85 }
86
87 static inline void
88 put_lba(struct aoe_atahdr *ah, sector_t lba)
89 {
90 ah->lba0 = lba;
91 ah->lba1 = lba >>= 8;
92 ah->lba2 = lba >>= 8;
93 ah->lba3 = lba >>= 8;
94 ah->lba4 = lba >>= 8;
95 ah->lba5 = lba >>= 8;
96 }
97
98 static void
99 ifrotate(struct aoetgt *t)
100 {
101 t->ifp++;
102 if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
103 t->ifp = t->ifs;
104 if (t->ifp->nd == NULL) {
105 printk(KERN_INFO "aoe: no interface to rotate to\n");
106 BUG();
107 }
108 }
109
110 static void
111 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
112 {
113 __skb_queue_tail(&d->skbpool, skb);
114 }
115
116 static struct sk_buff *
117 skb_pool_get(struct aoedev *d)
118 {
119 struct sk_buff *skb = skb_peek(&d->skbpool);
120
121 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
122 __skb_unlink(skb, &d->skbpool);
123 return skb;
124 }
125 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
126 (skb = new_skb(ETH_ZLEN)))
127 return skb;
128
129 return NULL;
130 }
131
132 /* freeframe is where we do our load balancing so it's a little hairy. */
133 static struct frame *
134 freeframe(struct aoedev *d)
135 {
136 struct frame *f, *e, *rf;
137 struct aoetgt **t;
138 struct sk_buff *skb;
139
140 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
141 printk(KERN_ERR "aoe: NULL TARGETS!\n");
142 return NULL;
143 }
144 t = d->tgt;
145 t++;
146 if (t >= &d->targets[NTARGETS] || !*t)
147 t = d->targets;
148 for (;;) {
149 if ((*t)->nout < (*t)->maxout
150 && t != d->htgt
151 && (*t)->ifp->nd) {
152 rf = NULL;
153 f = (*t)->frames;
154 e = f + (*t)->nframes;
155 for (; f < e; f++) {
156 if (f->tag != FREETAG)
157 continue;
158 skb = f->skb;
159 if (!skb
160 && !(f->skb = skb = new_skb(ETH_ZLEN)))
161 continue;
162 if (atomic_read(&skb_shinfo(skb)->dataref)
163 != 1) {
164 if (!rf)
165 rf = f;
166 continue;
167 }
168 gotone: skb->truesize -= skb->data_len;
169 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
170 skb_trim(skb, 0);
171 d->tgt = t;
172 ifrotate(*t);
173 return f;
174 }
175 /* Work can be done, but the network layer is
176 holding our precious packets. Try to grab
177 one from the pool. */
178 f = rf;
179 if (f == NULL) { /* more paranoia */
180 printk(KERN_ERR
181 "aoe: freeframe: %s.\n",
182 "unexpected null rf");
183 d->flags |= DEVFL_KICKME;
184 return NULL;
185 }
186 skb = skb_pool_get(d);
187 if (skb) {
188 skb_pool_put(d, f->skb);
189 f->skb = skb;
190 goto gotone;
191 }
192 (*t)->dataref++;
193 if ((*t)->nout == 0)
194 d->flags |= DEVFL_KICKME;
195 }
196 if (t == d->tgt) /* we've looped and found nada */
197 break;
198 t++;
199 if (t >= &d->targets[NTARGETS] || !*t)
200 t = d->targets;
201 }
202 return NULL;
203 }
204
205 static void
206 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
207 {
208 int frag = 0;
209 ulong fcnt;
210 loop:
211 fcnt = bv->bv_len - (off - bv->bv_offset);
212 if (fcnt > cnt)
213 fcnt = cnt;
214 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
215 cnt -= fcnt;
216 if (cnt <= 0)
217 return;
218 bv++;
219 off = bv->bv_offset;
220 goto loop;
221 }
222
223 static int
224 aoecmd_ata_rw(struct aoedev *d)
225 {
226 struct frame *f;
227 struct aoe_hdr *h;
228 struct aoe_atahdr *ah;
229 struct buf *buf;
230 struct bio_vec *bv;
231 struct aoetgt *t;
232 struct sk_buff *skb;
233 ulong bcnt, fbcnt;
234 char writebit, extbit;
235
236 writebit = 0x10;
237 extbit = 0x4;
238
239 f = freeframe(d);
240 if (f == NULL)
241 return 0;
242 t = *d->tgt;
243 buf = d->inprocess;
244 bv = buf->bv;
245 bcnt = t->ifp->maxbcnt;
246 if (bcnt == 0)
247 bcnt = DEFAULTBCNT;
248 if (bcnt > buf->resid)
249 bcnt = buf->resid;
250 fbcnt = bcnt;
251 f->bv = buf->bv;
252 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
253 do {
254 if (fbcnt < buf->bv_resid) {
255 buf->bv_resid -= fbcnt;
256 buf->resid -= fbcnt;
257 break;
258 }
259 fbcnt -= buf->bv_resid;
260 buf->resid -= buf->bv_resid;
261 if (buf->resid == 0) {
262 d->inprocess = NULL;
263 break;
264 }
265 buf->bv++;
266 buf->bv_resid = buf->bv->bv_len;
267 WARN_ON(buf->bv_resid == 0);
268 } while (fbcnt);
269
270 /* initialize the headers & frame */
271 skb = f->skb;
272 h = (struct aoe_hdr *) skb_mac_header(skb);
273 ah = (struct aoe_atahdr *) (h+1);
274 skb_put(skb, sizeof *h + sizeof *ah);
275 memset(h, 0, skb->len);
276 f->tag = aoehdr_atainit(d, t, h);
277 t->nout++;
278 f->waited = 0;
279 f->buf = buf;
280 f->bcnt = bcnt;
281 f->lba = buf->sector;
282
283 /* set up ata header */
284 ah->scnt = bcnt >> 9;
285 put_lba(ah, buf->sector);
286 if (d->flags & DEVFL_EXT) {
287 ah->aflags |= AOEAFL_EXT;
288 } else {
289 extbit = 0;
290 ah->lba3 &= 0x0f;
291 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
292 }
293 if (bio_data_dir(buf->bio) == WRITE) {
294 skb_fillup(skb, f->bv, f->bv_off, bcnt);
295 ah->aflags |= AOEAFL_WRITE;
296 skb->len += bcnt;
297 skb->data_len = bcnt;
298 skb->truesize += bcnt;
299 t->wpkts++;
300 } else {
301 t->rpkts++;
302 writebit = 0;
303 }
304
305 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
306
307 /* mark all tracking fields and load out */
308 buf->nframesout += 1;
309 buf->sector += bcnt >> 9;
310
311 skb->dev = t->ifp->nd;
312 skb = skb_clone(skb, GFP_ATOMIC);
313 if (skb)
314 __skb_queue_tail(&d->sendq, skb);
315 return 1;
316 }
317
318 /* some callers cannot sleep, and they can call this function,
319 * transmitting the packets later, when interrupts are on
320 */
321 static void
322 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
323 {
324 struct aoe_hdr *h;
325 struct aoe_cfghdr *ch;
326 struct sk_buff *skb;
327 struct net_device *ifp;
328
329 rcu_read_lock();
330 for_each_netdev_rcu(&init_net, ifp) {
331 dev_hold(ifp);
332 if (!is_aoe_netif(ifp))
333 goto cont;
334
335 skb = new_skb(sizeof *h + sizeof *ch);
336 if (skb == NULL) {
337 printk(KERN_INFO "aoe: skb alloc failure\n");
338 goto cont;
339 }
340 skb_put(skb, sizeof *h + sizeof *ch);
341 skb->dev = ifp;
342 __skb_queue_tail(queue, skb);
343 h = (struct aoe_hdr *) skb_mac_header(skb);
344 memset(h, 0, sizeof *h + sizeof *ch);
345
346 memset(h->dst, 0xff, sizeof h->dst);
347 memcpy(h->src, ifp->dev_addr, sizeof h->src);
348 h->type = __constant_cpu_to_be16(ETH_P_AOE);
349 h->verfl = AOE_HVER;
350 h->major = cpu_to_be16(aoemajor);
351 h->minor = aoeminor;
352 h->cmd = AOECMD_CFG;
353
354 cont:
355 dev_put(ifp);
356 }
357 rcu_read_unlock();
358 }
359
360 static void
361 resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
362 {
363 struct sk_buff *skb;
364 struct aoe_hdr *h;
365 struct aoe_atahdr *ah;
366 char buf[128];
367 u32 n;
368
369 ifrotate(t);
370 n = newtag(t);
371 skb = f->skb;
372 h = (struct aoe_hdr *) skb_mac_header(skb);
373 ah = (struct aoe_atahdr *) (h+1);
374
375 snprintf(buf, sizeof buf,
376 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
377 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
378 h->src, h->dst, t->nout);
379 aoechr_error(buf);
380
381 f->tag = n;
382 h->tag = cpu_to_be32(n);
383 memcpy(h->dst, t->addr, sizeof h->dst);
384 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
385
386 switch (ah->cmdstat) {
387 default:
388 break;
389 case ATA_CMD_PIO_READ:
390 case ATA_CMD_PIO_READ_EXT:
391 case ATA_CMD_PIO_WRITE:
392 case ATA_CMD_PIO_WRITE_EXT:
393 put_lba(ah, f->lba);
394
395 n = f->bcnt;
396 ah->scnt = n >> 9;
397 if (ah->aflags & AOEAFL_WRITE) {
398 skb_fillup(skb, f->bv, f->bv_off, n);
399 skb->len = sizeof *h + sizeof *ah + n;
400 skb->data_len = n;
401 skb->truesize += n;
402 }
403 }
404 skb->dev = t->ifp->nd;
405 skb = skb_clone(skb, GFP_ATOMIC);
406 if (skb == NULL)
407 return;
408 __skb_queue_tail(&d->sendq, skb);
409 }
410
411 static int
412 tsince(int tag)
413 {
414 int n;
415
416 n = jiffies & 0xffff;
417 n -= tag & 0xffff;
418 if (n < 0)
419 n += 1<<16;
420 return n;
421 }
422
423 static struct aoeif *
424 getif(struct aoetgt *t, struct net_device *nd)
425 {
426 struct aoeif *p, *e;
427
428 p = t->ifs;
429 e = p + NAOEIFS;
430 for (; p < e; p++)
431 if (p->nd == nd)
432 return p;
433 return NULL;
434 }
435
436 static struct aoeif *
437 addif(struct aoetgt *t, struct net_device *nd)
438 {
439 struct aoeif *p;
440
441 p = getif(t, NULL);
442 if (!p)
443 return NULL;
444 p->nd = nd;
445 p->maxbcnt = DEFAULTBCNT;
446 p->lost = 0;
447 p->lostjumbo = 0;
448 return p;
449 }
450
451 static void
452 ejectif(struct aoetgt *t, struct aoeif *ifp)
453 {
454 struct aoeif *e;
455 ulong n;
456
457 e = t->ifs + NAOEIFS - 1;
458 n = (e - ifp) * sizeof *ifp;
459 memmove(ifp, ifp+1, n);
460 e->nd = NULL;
461 }
462
463 static int
464 sthtith(struct aoedev *d)
465 {
466 struct frame *f, *e, *nf;
467 struct sk_buff *skb;
468 struct aoetgt *ht = *d->htgt;
469
470 f = ht->frames;
471 e = f + ht->nframes;
472 for (; f < e; f++) {
473 if (f->tag == FREETAG)
474 continue;
475 nf = freeframe(d);
476 if (!nf)
477 return 0;
478 skb = nf->skb;
479 *nf = *f;
480 f->skb = skb;
481 f->tag = FREETAG;
482 nf->waited = 0;
483 ht->nout--;
484 (*d->tgt)->nout++;
485 resend(d, *d->tgt, nf);
486 }
487 /* he's clean, he's useless. take away his interfaces */
488 memset(ht->ifs, 0, sizeof ht->ifs);
489 d->htgt = NULL;
490 return 1;
491 }
492
493 static inline unsigned char
494 ata_scnt(unsigned char *packet) {
495 struct aoe_hdr *h;
496 struct aoe_atahdr *ah;
497
498 h = (struct aoe_hdr *) packet;
499 ah = (struct aoe_atahdr *) (h+1);
500 return ah->scnt;
501 }
502
503 static void
504 rexmit_timer(ulong vp)
505 {
506 struct sk_buff_head queue;
507 struct aoedev *d;
508 struct aoetgt *t, **tt, **te;
509 struct aoeif *ifp;
510 struct frame *f, *e;
511 register long timeout;
512 ulong flags, n;
513
514 d = (struct aoedev *) vp;
515
516 /* timeout is always ~150% of the moving average */
517 timeout = d->rttavg;
518 timeout += timeout >> 1;
519
520 spin_lock_irqsave(&d->lock, flags);
521
522 if (d->flags & DEVFL_TKILL) {
523 spin_unlock_irqrestore(&d->lock, flags);
524 return;
525 }
526 tt = d->targets;
527 te = tt + NTARGETS;
528 for (; tt < te && *tt; tt++) {
529 t = *tt;
530 f = t->frames;
531 e = f + t->nframes;
532 for (; f < e; f++) {
533 if (f->tag == FREETAG
534 || tsince(f->tag) < timeout)
535 continue;
536 n = f->waited += timeout;
537 n /= HZ;
538 if (n > aoe_deadsecs) {
539 /* waited too long. device failure. */
540 aoedev_downdev(d);
541 break;
542 }
543
544 if (n > HELPWAIT /* see if another target can help */
545 && (tt != d->targets || d->targets[1]))
546 d->htgt = tt;
547
548 if (t->nout == t->maxout) {
549 if (t->maxout > 1)
550 t->maxout--;
551 t->lastwadj = jiffies;
552 }
553
554 ifp = getif(t, f->skb->dev);
555 if (ifp && ++ifp->lost > (t->nframes << 1)
556 && (ifp != t->ifs || t->ifs[1].nd)) {
557 ejectif(t, ifp);
558 ifp = NULL;
559 }
560 resend(d, t, f);
561 }
562
563 /* window check */
564 if (t->nout == t->maxout
565 && t->maxout < t->nframes
566 && (jiffies - t->lastwadj)/HZ > 10) {
567 t->maxout++;
568 t->lastwadj = jiffies;
569 }
570 }
571
572 if (!skb_queue_empty(&d->sendq)) {
573 n = d->rttavg <<= 1;
574 if (n > MAXTIMER)
575 d->rttavg = MAXTIMER;
576 }
577
578 if (d->flags & DEVFL_KICKME || d->htgt) {
579 d->flags &= ~DEVFL_KICKME;
580 aoecmd_work(d);
581 }
582
583 __skb_queue_head_init(&queue);
584 skb_queue_splice_init(&d->sendq, &queue);
585
586 d->timer.expires = jiffies + TIMERTICK;
587 add_timer(&d->timer);
588
589 spin_unlock_irqrestore(&d->lock, flags);
590
591 aoenet_xmit(&queue);
592 }
593
594 /* enters with d->lock held */
595 void
596 aoecmd_work(struct aoedev *d)
597 {
598 struct buf *buf;
599 loop:
600 if (d->htgt && !sthtith(d))
601 return;
602 if (d->inprocess == NULL) {
603 if (list_empty(&d->bufq))
604 return;
605 buf = container_of(d->bufq.next, struct buf, bufs);
606 list_del(d->bufq.next);
607 d->inprocess = buf;
608 }
609 if (aoecmd_ata_rw(d))
610 goto loop;
611 }
612
613 /* this function performs work that has been deferred until sleeping is OK
614 */
615 void
616 aoecmd_sleepwork(struct work_struct *work)
617 {
618 struct aoedev *d = container_of(work, struct aoedev, work);
619
620 if (d->flags & DEVFL_GDALLOC)
621 aoeblk_gdalloc(d);
622
623 if (d->flags & DEVFL_NEWSIZE) {
624 struct block_device *bd;
625 unsigned long flags;
626 u64 ssize;
627
628 ssize = get_capacity(d->gd);
629 bd = bdget_disk(d->gd, 0);
630
631 if (bd) {
632 mutex_lock(&bd->bd_inode->i_mutex);
633 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
634 mutex_unlock(&bd->bd_inode->i_mutex);
635 bdput(bd);
636 }
637 spin_lock_irqsave(&d->lock, flags);
638 d->flags |= DEVFL_UP;
639 d->flags &= ~DEVFL_NEWSIZE;
640 spin_unlock_irqrestore(&d->lock, flags);
641 }
642 }
643
644 static void
645 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
646 {
647 u64 ssize;
648 u16 n;
649
650 /* word 83: command set supported */
651 n = get_unaligned_le16(&id[83 << 1]);
652
653 /* word 86: command set/feature enabled */
654 n |= get_unaligned_le16(&id[86 << 1]);
655
656 if (n & (1<<10)) { /* bit 10: LBA 48 */
657 d->flags |= DEVFL_EXT;
658
659 /* word 100: number lba48 sectors */
660 ssize = get_unaligned_le64(&id[100 << 1]);
661
662 /* set as in ide-disk.c:init_idedisk_capacity */
663 d->geo.cylinders = ssize;
664 d->geo.cylinders /= (255 * 63);
665 d->geo.heads = 255;
666 d->geo.sectors = 63;
667 } else {
668 d->flags &= ~DEVFL_EXT;
669
670 /* number lba28 sectors */
671 ssize = get_unaligned_le32(&id[60 << 1]);
672
673 /* NOTE: obsolete in ATA 6 */
674 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
675 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
676 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
677 }
678
679 if (d->ssize != ssize)
680 printk(KERN_INFO
681 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
682 t->addr,
683 d->aoemajor, d->aoeminor,
684 d->fw_ver, (long long)ssize);
685 d->ssize = ssize;
686 d->geo.start = 0;
687 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
688 return;
689 if (d->gd != NULL) {
690 set_capacity(d->gd, ssize);
691 d->flags |= DEVFL_NEWSIZE;
692 } else
693 d->flags |= DEVFL_GDALLOC;
694 schedule_work(&d->work);
695 }
696
697 static void
698 calc_rttavg(struct aoedev *d, int rtt)
699 {
700 register long n;
701
702 n = rtt;
703 if (n < 0) {
704 n = -rtt;
705 if (n < MINTIMER)
706 n = MINTIMER;
707 else if (n > MAXTIMER)
708 n = MAXTIMER;
709 d->mintimer += (n - d->mintimer) >> 1;
710 } else if (n < d->mintimer)
711 n = d->mintimer;
712 else if (n > MAXTIMER)
713 n = MAXTIMER;
714
715 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
716 n -= d->rttavg;
717 d->rttavg += n >> 2;
718 }
719
720 static struct aoetgt *
721 gettgt(struct aoedev *d, char *addr)
722 {
723 struct aoetgt **t, **e;
724
725 t = d->targets;
726 e = t + NTARGETS;
727 for (; t < e && *t; t++)
728 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
729 return *t;
730 return NULL;
731 }
732
733 static inline void
734 diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
735 {
736 unsigned long n_sect = bio->bi_size >> 9;
737 const int rw = bio_data_dir(bio);
738 struct hd_struct *part;
739 int cpu;
740
741 cpu = part_stat_lock();
742 part = disk_map_sector_rcu(disk, sector);
743
744 part_stat_inc(cpu, part, ios[rw]);
745 part_stat_add(cpu, part, ticks[rw], duration);
746 part_stat_add(cpu, part, sectors[rw], n_sect);
747 part_stat_add(cpu, part, io_ticks, duration);
748
749 part_stat_unlock();
750 }
751
752 static void
753 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, ulong cnt)
754 {
755 ulong fcnt;
756 char *p;
757 int soff = 0;
758 loop:
759 fcnt = bv->bv_len - (off - bv->bv_offset);
760 if (fcnt > cnt)
761 fcnt = cnt;
762 p = page_address(bv->bv_page) + off;
763 skb_copy_bits(skb, soff, p, fcnt);
764 soff += fcnt;
765 cnt -= fcnt;
766 if (cnt <= 0)
767 return;
768 bv++;
769 off = bv->bv_offset;
770 goto loop;
771 }
772
773 static void
774 fadvance(struct frame *f, ulong cnt)
775 {
776 ulong fcnt;
777
778 f->lba += cnt >> 9;
779 loop:
780 fcnt = f->bv->bv_len - (f->bv_off - f->bv->bv_offset);
781 if (fcnt > cnt) {
782 f->bv_off += cnt;
783 return;
784 }
785 cnt -= fcnt;
786 f->bv++;
787 f->bv_off = f->bv->bv_offset;
788 goto loop;
789 }
790
791 void
792 aoecmd_ata_rsp(struct sk_buff *skb)
793 {
794 struct sk_buff_head queue;
795 struct aoedev *d;
796 struct aoe_hdr *hin, *hout;
797 struct aoe_atahdr *ahin, *ahout;
798 struct frame *f;
799 struct buf *buf;
800 struct aoetgt *t;
801 struct aoeif *ifp;
802 register long n;
803 ulong flags;
804 char ebuf[128];
805 u16 aoemajor;
806
807 hin = (struct aoe_hdr *) skb_mac_header(skb);
808 skb_pull(skb, sizeof(*hin));
809 aoemajor = get_unaligned_be16(&hin->major);
810 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
811 if (d == NULL) {
812 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
813 "for unknown device %d.%d\n",
814 aoemajor, hin->minor);
815 aoechr_error(ebuf);
816 return;
817 }
818
819 spin_lock_irqsave(&d->lock, flags);
820
821 n = get_unaligned_be32(&hin->tag);
822 t = gettgt(d, hin->src);
823 if (t == NULL) {
824 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
825 d->aoemajor, d->aoeminor, hin->src);
826 spin_unlock_irqrestore(&d->lock, flags);
827 return;
828 }
829 f = getframe(t, n);
830 if (f == NULL) {
831 calc_rttavg(d, -tsince(n));
832 spin_unlock_irqrestore(&d->lock, flags);
833 snprintf(ebuf, sizeof ebuf,
834 "%15s e%d.%d tag=%08x@%08lx\n",
835 "unexpected rsp",
836 get_unaligned_be16(&hin->major),
837 hin->minor,
838 get_unaligned_be32(&hin->tag),
839 jiffies);
840 aoechr_error(ebuf);
841 return;
842 }
843
844 calc_rttavg(d, tsince(f->tag));
845
846 ahin = (struct aoe_atahdr *) skb->data;
847 skb_pull(skb, sizeof(*ahin));
848 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
849 ahout = (struct aoe_atahdr *) (hout+1);
850 buf = f->buf;
851
852 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
853 printk(KERN_ERR
854 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
855 ahout->cmdstat, ahin->cmdstat,
856 d->aoemajor, d->aoeminor);
857 if (buf)
858 buf->flags |= BUFFL_FAIL;
859 } else {
860 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
861 d->htgt = NULL;
862 n = ahout->scnt << 9;
863 switch (ahout->cmdstat) {
864 case ATA_CMD_PIO_READ:
865 case ATA_CMD_PIO_READ_EXT:
866 if (skb->len < n) {
867 printk(KERN_ERR
868 "aoe: %s. skb->len=%d need=%ld\n",
869 "runt data size in read", skb->len, n);
870 /* fail frame f? just returning will rexmit. */
871 spin_unlock_irqrestore(&d->lock, flags);
872 return;
873 }
874 bvcpy(f->bv, f->bv_off, skb, n);
875 case ATA_CMD_PIO_WRITE:
876 case ATA_CMD_PIO_WRITE_EXT:
877 ifp = getif(t, skb->dev);
878 if (ifp) {
879 ifp->lost = 0;
880 if (n > DEFAULTBCNT)
881 ifp->lostjumbo = 0;
882 }
883 if (f->bcnt -= n) {
884 fadvance(f, n);
885 resend(d, t, f);
886 goto xmit;
887 }
888 break;
889 case ATA_CMD_ID_ATA:
890 if (skb->len < 512) {
891 printk(KERN_INFO
892 "aoe: runt data size in ataid. skb->len=%d\n",
893 skb->len);
894 spin_unlock_irqrestore(&d->lock, flags);
895 return;
896 }
897 if (skb_linearize(skb))
898 break;
899 ataid_complete(d, t, skb->data);
900 break;
901 default:
902 printk(KERN_INFO
903 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
904 ahout->cmdstat,
905 get_unaligned_be16(&hin->major),
906 hin->minor);
907 }
908 }
909
910 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
911 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
912 if (buf->flags & BUFFL_FAIL)
913 bio_endio(buf->bio, -EIO);
914 else {
915 bio_flush_dcache_pages(buf->bio);
916 bio_endio(buf->bio, 0);
917 }
918 mempool_free(buf, d->bufpool);
919 }
920
921 f->buf = NULL;
922 f->tag = FREETAG;
923 t->nout--;
924
925 aoecmd_work(d);
926 xmit:
927 __skb_queue_head_init(&queue);
928 skb_queue_splice_init(&d->sendq, &queue);
929
930 spin_unlock_irqrestore(&d->lock, flags);
931 aoenet_xmit(&queue);
932 }
933
934 void
935 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
936 {
937 struct sk_buff_head queue;
938
939 __skb_queue_head_init(&queue);
940 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
941 aoenet_xmit(&queue);
942 }
943
944 struct sk_buff *
945 aoecmd_ata_id(struct aoedev *d)
946 {
947 struct aoe_hdr *h;
948 struct aoe_atahdr *ah;
949 struct frame *f;
950 struct sk_buff *skb;
951 struct aoetgt *t;
952
953 f = freeframe(d);
954 if (f == NULL)
955 return NULL;
956
957 t = *d->tgt;
958
959 /* initialize the headers & frame */
960 skb = f->skb;
961 h = (struct aoe_hdr *) skb_mac_header(skb);
962 ah = (struct aoe_atahdr *) (h+1);
963 skb_put(skb, sizeof *h + sizeof *ah);
964 memset(h, 0, skb->len);
965 f->tag = aoehdr_atainit(d, t, h);
966 t->nout++;
967 f->waited = 0;
968
969 /* set up ata header */
970 ah->scnt = 1;
971 ah->cmdstat = ATA_CMD_ID_ATA;
972 ah->lba3 = 0xa0;
973
974 skb->dev = t->ifp->nd;
975
976 d->rttavg = MAXTIMER;
977 d->timer.function = rexmit_timer;
978
979 return skb_clone(skb, GFP_ATOMIC);
980 }
981
982 static struct aoetgt *
983 addtgt(struct aoedev *d, char *addr, ulong nframes)
984 {
985 struct aoetgt *t, **tt, **te;
986 struct frame *f, *e;
987
988 tt = d->targets;
989 te = tt + NTARGETS;
990 for (; tt < te && *tt; tt++)
991 ;
992
993 if (tt == te) {
994 printk(KERN_INFO
995 "aoe: device addtgt failure; too many targets\n");
996 return NULL;
997 }
998 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
999 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
1000 if (!t || !f) {
1001 kfree(f);
1002 kfree(t);
1003 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
1004 return NULL;
1005 }
1006
1007 t->nframes = nframes;
1008 t->frames = f;
1009 e = f + nframes;
1010 for (; f < e; f++)
1011 f->tag = FREETAG;
1012 memcpy(t->addr, addr, sizeof t->addr);
1013 t->ifp = t->ifs;
1014 t->maxout = t->nframes;
1015 return *tt = t;
1016 }
1017
1018 void
1019 aoecmd_cfg_rsp(struct sk_buff *skb)
1020 {
1021 struct aoedev *d;
1022 struct aoe_hdr *h;
1023 struct aoe_cfghdr *ch;
1024 struct aoetgt *t;
1025 struct aoeif *ifp;
1026 ulong flags, sysminor, aoemajor;
1027 struct sk_buff *sl;
1028 u16 n;
1029
1030 h = (struct aoe_hdr *) skb_mac_header(skb);
1031 ch = (struct aoe_cfghdr *) (h+1);
1032
1033 /*
1034 * Enough people have their dip switches set backwards to
1035 * warrant a loud message for this special case.
1036 */
1037 aoemajor = get_unaligned_be16(&h->major);
1038 if (aoemajor == 0xfff) {
1039 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1040 "Check shelf dip switches.\n");
1041 return;
1042 }
1043
1044 sysminor = SYSMINOR(aoemajor, h->minor);
1045 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
1046 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
1047 aoemajor, (int) h->minor);
1048 return;
1049 }
1050
1051 n = be16_to_cpu(ch->bufcnt);
1052 if (n > aoe_maxout) /* keep it reasonable */
1053 n = aoe_maxout;
1054
1055 d = aoedev_by_sysminor_m(sysminor);
1056 if (d == NULL) {
1057 printk(KERN_INFO "aoe: device sysminor_m failure\n");
1058 return;
1059 }
1060
1061 spin_lock_irqsave(&d->lock, flags);
1062
1063 t = gettgt(d, h->src);
1064 if (!t) {
1065 t = addtgt(d, h->src, n);
1066 if (!t) {
1067 spin_unlock_irqrestore(&d->lock, flags);
1068 return;
1069 }
1070 }
1071 ifp = getif(t, skb->dev);
1072 if (!ifp) {
1073 ifp = addif(t, skb->dev);
1074 if (!ifp) {
1075 printk(KERN_INFO
1076 "aoe: device addif failure; "
1077 "too many interfaces?\n");
1078 spin_unlock_irqrestore(&d->lock, flags);
1079 return;
1080 }
1081 }
1082 if (ifp->maxbcnt) {
1083 n = ifp->nd->mtu;
1084 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1085 n /= 512;
1086 if (n > ch->scnt)
1087 n = ch->scnt;
1088 n = n ? n * 512 : DEFAULTBCNT;
1089 if (n != ifp->maxbcnt) {
1090 printk(KERN_INFO
1091 "aoe: e%ld.%d: setting %d%s%s:%pm\n",
1092 d->aoemajor, d->aoeminor, n,
1093 " byte data frames on ", ifp->nd->name,
1094 t->addr);
1095 ifp->maxbcnt = n;
1096 }
1097 }
1098
1099 /* don't change users' perspective */
1100 if (d->nopen) {
1101 spin_unlock_irqrestore(&d->lock, flags);
1102 return;
1103 }
1104 d->fw_ver = be16_to_cpu(ch->fwver);
1105
1106 sl = aoecmd_ata_id(d);
1107
1108 spin_unlock_irqrestore(&d->lock, flags);
1109
1110 if (sl) {
1111 struct sk_buff_head queue;
1112 __skb_queue_head_init(&queue);
1113 __skb_queue_tail(&queue, sl);
1114 aoenet_xmit(&queue);
1115 }
1116 }
1117
1118 void
1119 aoecmd_cleanslate(struct aoedev *d)
1120 {
1121 struct aoetgt **t, **te;
1122 struct aoeif *p, *e;
1123
1124 d->mintimer = MINTIMER;
1125
1126 t = d->targets;
1127 te = t + NTARGETS;
1128 for (; t < te && *t; t++) {
1129 (*t)->maxout = (*t)->nframes;
1130 p = (*t)->ifs;
1131 e = p + NAOEIFS;
1132 for (; p < e; p++) {
1133 p->lostjumbo = 0;
1134 p->lost = 0;
1135 p->maxbcnt = DEFAULTBCNT;
1136 }
1137 }
1138 }
This page took 0.072553 seconds and 5 git commands to generate.