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