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