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