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