Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aoecmd.c
Go to the documentation of this file.
1 /* Copyright (c) 2012 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;
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);
56  skb_checksum_none_assert(skb);
57  }
58  return skb;
59 }
60 
61 static struct frame *
62 getframe(struct aoedev *d, u32 tag)
63 {
64  struct frame *f;
65  struct list_head *head, *pos, *nx;
66  u32 n;
67 
68  n = tag % NFACTIVE;
69  head = &d->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 aoedev *d)
87 {
88  register ulong n;
89 
90  n = jiffies & 0xffff;
91  return n |= (++d->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(d);
98 
99  memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
100  memcpy(h->dst, t->addr, sizeof h->dst);
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
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 aoedev *d = f->t->d;
274  u32 n;
275 
276  n = f->tag % NFACTIVE;
277  list_add_tail(&f->head, &d->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 aoetgt *t;
288  struct sk_buff *skb;
289  struct sk_buff_head queue;
290  ulong bcnt, fbcnt;
291  char writebit, extbit;
292 
293  writebit = 0x10;
294  extbit = 0x4;
295 
296  buf = nextbuf(d);
297  if (buf == NULL)
298  return 0;
299  f = newframe(d);
300  if (f == NULL)
301  return 0;
302  t = *d->tgt;
303  bcnt = d->maxbcnt;
304  if (bcnt == 0)
305  bcnt = DEFAULTBCNT;
306  if (bcnt > buf->resid)
307  bcnt = buf->resid;
308  fbcnt = bcnt;
309  f->bv = buf->bv;
310  f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
311  do {
312  if (fbcnt < buf->bv_resid) {
313  buf->bv_resid -= fbcnt;
314  buf->resid -= fbcnt;
315  break;
316  }
317  fbcnt -= buf->bv_resid;
318  buf->resid -= buf->bv_resid;
319  if (buf->resid == 0) {
320  d->ip.buf = NULL;
321  break;
322  }
323  buf->bv++;
324  buf->bv_resid = buf->bv->bv_len;
325  WARN_ON(buf->bv_resid == 0);
326  } while (fbcnt);
327 
328  /* initialize the headers & frame */
329  skb = f->skb;
330  h = (struct aoe_hdr *) skb_mac_header(skb);
331  ah = (struct aoe_atahdr *) (h+1);
332  skb_put(skb, sizeof *h + sizeof *ah);
333  memset(h, 0, skb->len);
334  f->tag = aoehdr_atainit(d, t, h);
335  fhash(f);
336  t->nout++;
337  f->waited = 0;
338  f->buf = buf;
339  f->bcnt = bcnt;
340  f->lba = buf->sector;
341 
342  /* set up ata header */
343  ah->scnt = bcnt >> 9;
344  put_lba(ah, buf->sector);
345  if (d->flags & DEVFL_EXT) {
346  ah->aflags |= AOEAFL_EXT;
347  } else {
348  extbit = 0;
349  ah->lba3 &= 0x0f;
350  ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
351  }
352  if (bio_data_dir(buf->bio) == WRITE) {
353  skb_fillup(skb, f->bv, f->bv_off, bcnt);
354  ah->aflags |= AOEAFL_WRITE;
355  skb->len += bcnt;
356  skb->data_len = bcnt;
357  skb->truesize += bcnt;
358  t->wpkts++;
359  } else {
360  t->rpkts++;
361  writebit = 0;
362  }
363 
364  ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
365 
366  /* mark all tracking fields and load out */
367  buf->nframesout += 1;
368  buf->sector += bcnt >> 9;
369 
370  skb->dev = t->ifp->nd;
371  skb = skb_clone(skb, GFP_ATOMIC);
372  if (skb) {
373  __skb_queue_head_init(&queue);
374  __skb_queue_tail(&queue, skb);
375  aoenet_xmit(&queue);
376  }
377  return 1;
378 }
379 
380 /* some callers cannot sleep, and they can call this function,
381  * transmitting the packets later, when interrupts are on
382  */
383 static void
384 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
385 {
386  struct aoe_hdr *h;
387  struct aoe_cfghdr *ch;
388  struct sk_buff *skb;
389  struct net_device *ifp;
390 
391  rcu_read_lock();
393  dev_hold(ifp);
394  if (!is_aoe_netif(ifp))
395  goto cont;
396 
397  skb = new_skb(sizeof *h + sizeof *ch);
398  if (skb == NULL) {
399  printk(KERN_INFO "aoe: skb alloc failure\n");
400  goto cont;
401  }
402  skb_put(skb, sizeof *h + sizeof *ch);
403  skb->dev = ifp;
404  __skb_queue_tail(queue, skb);
405  h = (struct aoe_hdr *) skb_mac_header(skb);
406  memset(h, 0, sizeof *h + sizeof *ch);
407 
408  memset(h->dst, 0xff, sizeof h->dst);
409  memcpy(h->src, ifp->dev_addr, sizeof h->src);
411  h->verfl = AOE_HVER;
412  h->major = cpu_to_be16(aoemajor);
413  h->minor = aoeminor;
414  h->cmd = AOECMD_CFG;
415 
416 cont:
417  dev_put(ifp);
418  }
419  rcu_read_unlock();
420 }
421 
422 static void
423 resend(struct aoedev *d, struct frame *f)
424 {
425  struct sk_buff *skb;
426  struct sk_buff_head queue;
427  struct aoe_hdr *h;
428  struct aoe_atahdr *ah;
429  struct aoetgt *t;
430  char buf[128];
431  u32 n;
432 
433  t = f->t;
434  n = newtag(d);
435  skb = f->skb;
436  if (ifrotate(t) == NULL) {
437  /* probably can't happen, but set it up to fail anyway */
438  pr_info("aoe: resend: no interfaces to rotate to.\n");
439  ktcomplete(f, NULL);
440  return;
441  }
442  h = (struct aoe_hdr *) skb_mac_header(skb);
443  ah = (struct aoe_atahdr *) (h+1);
444 
445  snprintf(buf, sizeof buf,
446  "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
447  "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
448  h->src, h->dst, t->nout);
449  aoechr_error(buf);
450 
451  f->tag = n;
452  fhash(f);
453  h->tag = cpu_to_be32(n);
454  memcpy(h->dst, t->addr, sizeof h->dst);
455  memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
456 
457  skb->dev = t->ifp->nd;
458  skb = skb_clone(skb, GFP_ATOMIC);
459  if (skb == NULL)
460  return;
461  __skb_queue_head_init(&queue);
462  __skb_queue_tail(&queue, skb);
463  aoenet_xmit(&queue);
464 }
465 
466 static int
467 tsince(u32 tag)
468 {
469  int n;
470 
471  n = jiffies & 0xffff;
472  n -= tag & 0xffff;
473  if (n < 0)
474  n += 1<<16;
475  return n;
476 }
477 
478 static struct aoeif *
479 getif(struct aoetgt *t, struct net_device *nd)
480 {
481  struct aoeif *p, *e;
482 
483  p = t->ifs;
484  e = p + NAOEIFS;
485  for (; p < e; p++)
486  if (p->nd == nd)
487  return p;
488  return NULL;
489 }
490 
491 static void
492 ejectif(struct aoetgt *t, struct aoeif *ifp)
493 {
494  struct aoeif *e;
495  struct net_device *nd;
496  ulong n;
497 
498  nd = ifp->nd;
499  e = t->ifs + NAOEIFS - 1;
500  n = (e - ifp) * sizeof *ifp;
501  memmove(ifp, ifp+1, n);
502  e->nd = NULL;
503  dev_put(nd);
504 }
505 
506 static int
507 sthtith(struct aoedev *d)
508 {
509  struct frame *f, *nf;
510  struct list_head *nx, *pos, *head;
511  struct sk_buff *skb;
512  struct aoetgt *ht = d->htgt;
513  int i;
514 
515  for (i = 0; i < NFACTIVE; i++) {
516  head = &d->factive[i];
517  list_for_each_safe(pos, nx, head) {
518  f = list_entry(pos, struct frame, head);
519  if (f->t != ht)
520  continue;
521 
522  nf = newframe(d);
523  if (!nf)
524  return 0;
525 
526  /* remove frame from active list */
527  list_del(pos);
528 
529  /* reassign all pertinent bits to new outbound frame */
530  skb = nf->skb;
531  nf->skb = f->skb;
532  nf->buf = f->buf;
533  nf->bcnt = f->bcnt;
534  nf->lba = f->lba;
535  nf->bv = f->bv;
536  nf->bv_off = f->bv_off;
537  nf->waited = 0;
538  f->skb = skb;
539  aoe_freetframe(f);
540  ht->nout--;
541  nf->t->nout++;
542  resend(d, nf);
543  }
544  }
545  /* We've cleaned up the outstanding so take away his
546  * interfaces so he won't be used. We should remove him from
547  * the target array here, but cleaning up a target is
548  * involved. PUNT!
549  */
550  memset(ht->ifs, 0, sizeof ht->ifs);
551  d->htgt = NULL;
552  return 1;
553 }
554 
555 static inline unsigned char
556 ata_scnt(unsigned char *packet) {
557  struct aoe_hdr *h;
558  struct aoe_atahdr *ah;
559 
560  h = (struct aoe_hdr *) packet;
561  ah = (struct aoe_atahdr *) (h+1);
562  return ah->scnt;
563 }
564 
565 static void
566 rexmit_timer(ulong vp)
567 {
568  struct aoedev *d;
569  struct aoetgt *t, **tt, **te;
570  struct aoeif *ifp;
571  struct frame *f;
572  struct list_head *head, *pos, *nx;
573  LIST_HEAD(flist);
574  register long timeout;
575  ulong flags, n;
576  int i;
577 
578  d = (struct aoedev *) vp;
579 
580  /* timeout is always ~150% of the moving average */
581  timeout = d->rttavg;
582  timeout += timeout >> 1;
583 
584  spin_lock_irqsave(&d->lock, flags);
585 
586  if (d->flags & DEVFL_TKILL) {
587  spin_unlock_irqrestore(&d->lock, flags);
588  return;
589  }
590 
591  /* collect all frames to rexmit into flist */
592  for (i = 0; i < NFACTIVE; i++) {
593  head = &d->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  break; /* end of expired frames */
598  /* move to flist for later processing */
599  list_move_tail(pos, &flist);
600  }
601  }
602  /* window check */
603  tt = d->targets;
604  te = tt + d->ntargets;
605  for (; tt < te && (t = *tt); tt++) {
606  if (t->nout == t->maxout
607  && t->maxout < t->nframes
608  && (jiffies - t->lastwadj)/HZ > 10) {
609  t->maxout++;
610  t->lastwadj = jiffies;
611  }
612  }
613 
614  if (!list_empty(&flist)) { /* retransmissions necessary */
615  n = d->rttavg <<= 1;
616  if (n > MAXTIMER)
617  d->rttavg = MAXTIMER;
618  }
619 
620  /* process expired frames */
621  while (!list_empty(&flist)) {
622  pos = flist.next;
623  f = list_entry(pos, struct frame, head);
624  n = f->waited += timeout;
625  n /= HZ;
626  if (n > aoe_deadsecs) {
627  /* Waited too long. Device failure.
628  * Hang all frames on first hash bucket for downdev
629  * to clean up.
630  */
631  list_splice(&flist, &d->factive[0]);
632  aoedev_downdev(d);
633  break;
634  }
635  list_del(pos);
636 
637  t = f->t;
638  if (n > aoe_deadsecs/2)
639  d->htgt = t; /* see if another target can help */
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
787 {
788  struct aoedev *d = container_of(work, struct aoedev, work);
789  struct block_device *bd;
790  u64 ssize;
791 
792  if (d->flags & DEVFL_GDALLOC)
793  aoeblk_gdalloc(d);
794 
795  if (d->flags & DEVFL_NEWSIZE) {
796  ssize = get_capacity(d->gd);
797  bd = bdget_disk(d->gd, 0);
798  if (bd) {
799  mutex_lock(&bd->bd_inode->i_mutex);
800  i_size_write(bd->bd_inode, (loff_t)ssize<<9);
801  mutex_unlock(&bd->bd_inode->i_mutex);
802  bdput(bd);
803  }
804  spin_lock_irq(&d->lock);
805  d->flags |= DEVFL_UP;
806  d->flags &= ~DEVFL_NEWSIZE;
807  spin_unlock_irq(&d->lock);
808  }
809 }
810 
811 static void
812 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
813 {
814  u64 ssize;
815  u16 n;
816 
817  /* word 83: command set supported */
818  n = get_unaligned_le16(&id[83 << 1]);
819 
820  /* word 86: command set/feature enabled */
821  n |= get_unaligned_le16(&id[86 << 1]);
822 
823  if (n & (1<<10)) { /* bit 10: LBA 48 */
824  d->flags |= DEVFL_EXT;
825 
826  /* word 100: number lba48 sectors */
827  ssize = get_unaligned_le64(&id[100 << 1]);
828 
829  /* set as in ide-disk.c:init_idedisk_capacity */
830  d->geo.cylinders = ssize;
831  d->geo.cylinders /= (255 * 63);
832  d->geo.heads = 255;
833  d->geo.sectors = 63;
834  } else {
835  d->flags &= ~DEVFL_EXT;
836 
837  /* number lba28 sectors */
838  ssize = get_unaligned_le32(&id[60 << 1]);
839 
840  /* NOTE: obsolete in ATA 6 */
841  d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
842  d->geo.heads = get_unaligned_le16(&id[55 << 1]);
843  d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
844  }
845 
846  if (d->ssize != ssize)
848  "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
849  t->addr,
850  d->aoemajor, d->aoeminor,
851  d->fw_ver, (long long)ssize);
852  d->ssize = ssize;
853  d->geo.start = 0;
854  if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
855  return;
856  if (d->gd != NULL) {
857  set_capacity(d->gd, ssize);
858  d->flags |= DEVFL_NEWSIZE;
859  } else
860  d->flags |= DEVFL_GDALLOC;
861  schedule_work(&d->work);
862 }
863 
864 static void
865 calc_rttavg(struct aoedev *d, int rtt)
866 {
867  register long n;
868 
869  n = rtt;
870  if (n < 0) {
871  n = -rtt;
872  if (n < MINTIMER)
873  n = MINTIMER;
874  else if (n > MAXTIMER)
875  n = MAXTIMER;
876  d->mintimer += (n - d->mintimer) >> 1;
877  } else if (n < d->mintimer)
878  n = d->mintimer;
879  else if (n > MAXTIMER)
880  n = MAXTIMER;
881 
882  /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
883  n -= d->rttavg;
884  d->rttavg += n >> 2;
885 }
886 
887 static struct aoetgt *
888 gettgt(struct aoedev *d, char *addr)
889 {
890  struct aoetgt **t, **e;
891 
892  t = d->targets;
893  e = t + NTARGETS;
894  for (; t < e && *t; t++)
895  if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
896  return *t;
897  return NULL;
898 }
899 
900 static void
901 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
902 {
903  ulong fcnt;
904  char *p;
905  int soff = 0;
906 loop:
907  fcnt = bv->bv_len - (off - bv->bv_offset);
908  if (fcnt > cnt)
909  fcnt = cnt;
910  p = page_address(bv->bv_page) + off;
911  skb_copy_bits(skb, soff, p, fcnt);
912  soff += fcnt;
913  cnt -= fcnt;
914  if (cnt <= 0)
915  return;
916  bv++;
917  off = bv->bv_offset;
918  goto loop;
919 }
920 
921 void
922 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
923 {
924  struct bio *bio;
925  int bok;
926  struct request_queue *q;
927 
928  q = d->blkq;
929  if (rq == d->ip.rq)
930  d->ip.rq = NULL;
931  do {
932  bio = rq->bio;
933  bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
934  } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
935 
936  /* cf. http://lkml.org/lkml/2006/10/31/28 */
937  if (!fastfail)
938  __blk_run_queue(q);
939 }
940 
941 static void
942 aoe_end_buf(struct aoedev *d, struct buf *buf)
943 {
944  struct request *rq;
945  unsigned long n;
946 
947  if (buf == d->ip.buf)
948  d->ip.buf = NULL;
949  rq = buf->rq;
950  bio_pagedec(buf->bio);
951  mempool_free(buf, d->bufpool);
952  n = (unsigned long) rq->special;
953  rq->special = (void *) --n;
954  if (n == 0)
955  aoe_end_request(d, rq, 0);
956 }
957 
958 static void
959 ktiocomplete(struct frame *f)
960 {
961  struct aoe_hdr *hin, *hout;
962  struct aoe_atahdr *ahin, *ahout;
963  struct buf *buf;
964  struct sk_buff *skb;
965  struct aoetgt *t;
966  struct aoeif *ifp;
967  struct aoedev *d;
968  long n;
969 
970  if (f == NULL)
971  return;
972 
973  t = f->t;
974  d = t->d;
975 
976  hout = (struct aoe_hdr *) skb_mac_header(f->skb);
977  ahout = (struct aoe_atahdr *) (hout+1);
978  buf = f->buf;
979  skb = f->r_skb;
980  if (skb == NULL)
981  goto noskb; /* just fail the buf. */
982 
983  hin = (struct aoe_hdr *) skb->data;
984  skb_pull(skb, sizeof(*hin));
985  ahin = (struct aoe_atahdr *) skb->data;
986  skb_pull(skb, sizeof(*ahin));
987  if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
988  pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
989  ahout->cmdstat, ahin->cmdstat,
990  d->aoemajor, d->aoeminor);
991 noskb: if (buf)
992  clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
993  goto badrsp;
994  }
995 
996  n = ahout->scnt << 9;
997  switch (ahout->cmdstat) {
998  case ATA_CMD_PIO_READ:
1000  if (skb->len < n) {
1001  pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1002  skb->len, n);
1003  clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1004  break;
1005  }
1006  bvcpy(f->bv, f->bv_off, skb, n);
1007  case ATA_CMD_PIO_WRITE:
1008  case ATA_CMD_PIO_WRITE_EXT:
1009  spin_lock_irq(&d->lock);
1010  ifp = getif(t, skb->dev);
1011  if (ifp)
1012  ifp->lost = 0;
1013  if (d->htgt == t) /* I'll help myself, thank you. */
1014  d->htgt = NULL;
1015  spin_unlock_irq(&d->lock);
1016  break;
1017  case ATA_CMD_ID_ATA:
1018  if (skb->len < 512) {
1019  pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1020  skb->len);
1021  break;
1022  }
1023  if (skb_linearize(skb))
1024  break;
1025  spin_lock_irq(&d->lock);
1026  ataid_complete(d, t, skb->data);
1027  spin_unlock_irq(&d->lock);
1028  break;
1029  default:
1030  pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1031  ahout->cmdstat,
1033  hin->minor);
1034  }
1035 badrsp:
1036  spin_lock_irq(&d->lock);
1037 
1038  aoe_freetframe(f);
1039 
1040  if (buf && --buf->nframesout == 0 && buf->resid == 0)
1041  aoe_end_buf(d, buf);
1042 
1043  aoecmd_work(d);
1044 
1045  spin_unlock_irq(&d->lock);
1046  aoedev_put(d);
1047  dev_kfree_skb(skb);
1048 }
1049 
1050 /* Enters with iocq.lock held.
1051  * Returns true iff responses needing processing remain.
1052  */
1053 static int
1054 ktio(void)
1055 {
1056  struct frame *f;
1057  struct list_head *pos;
1058  int i;
1059 
1060  for (i = 0; ; ++i) {
1061  if (i == MAXIOC)
1062  return 1;
1063  if (list_empty(&iocq.head))
1064  return 0;
1065  pos = iocq.head.next;
1066  list_del(pos);
1067  spin_unlock_irq(&iocq.lock);
1068  f = list_entry(pos, struct frame, head);
1069  ktiocomplete(f);
1070  spin_lock_irq(&iocq.lock);
1071  }
1072 }
1073 
1074 static int
1075 kthread(void *vp)
1076 {
1077  struct ktstate *k;
1079  int more;
1080 
1081  k = vp;
1082  current->flags |= PF_NOFREEZE;
1083  set_user_nice(current, -10);
1084  complete(&k->rendez); /* tell spawner we're running */
1085  do {
1086  spin_lock_irq(k->lock);
1087  more = k->fn();
1088  if (!more) {
1089  add_wait_queue(k->waitq, &wait);
1091  }
1092  spin_unlock_irq(k->lock);
1093  if (!more) {
1094  schedule();
1096  } else
1097  cond_resched();
1098  } while (!kthread_should_stop());
1099  complete(&k->rendez); /* tell spawner we're stopping */
1100  return 0;
1101 }
1102 
1103 void
1105 {
1106  kthread_stop(k->task);
1108 }
1109 
1110 int
1112 {
1113  struct task_struct *task;
1114 
1115  init_completion(&k->rendez);
1116  task = kthread_run(kthread, k, k->name);
1117  if (task == NULL || IS_ERR(task))
1118  return -ENOMEM;
1119  k->task = task;
1120  wait_for_completion(&k->rendez); /* allow kthread to start */
1121  init_completion(&k->rendez); /* for waiting for exit later */
1122  return 0;
1123 }
1124 
1125 /* pass it off to kthreads for processing */
1126 static void
1127 ktcomplete(struct frame *f, struct sk_buff *skb)
1128 {
1129  ulong flags;
1130 
1131  f->r_skb = skb;
1132  spin_lock_irqsave(&iocq.lock, flags);
1133  list_add_tail(&f->head, &iocq.head);
1134  spin_unlock_irqrestore(&iocq.lock, flags);
1135  wake_up(&ktiowq);
1136 }
1137 
1138 struct sk_buff *
1140 {
1141  struct aoedev *d;
1142  struct aoe_hdr *h;
1143  struct frame *f;
1144  struct aoetgt *t;
1145  u32 n;
1146  ulong flags;
1147  char ebuf[128];
1148  u16 aoemajor;
1149 
1150  h = (struct aoe_hdr *) skb->data;
1151  aoemajor = be16_to_cpu(get_unaligned(&h->major));
1152  d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
1153  if (d == NULL) {
1154  snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1155  "for unknown device %d.%d\n",
1156  aoemajor, h->minor);
1157  aoechr_error(ebuf);
1158  return skb;
1159  }
1160 
1161  spin_lock_irqsave(&d->lock, flags);
1162 
1163  n = be32_to_cpu(get_unaligned(&h->tag));
1164  f = getframe(d, n);
1165  if (f == NULL) {
1166  calc_rttavg(d, -tsince(n));
1167  spin_unlock_irqrestore(&d->lock, flags);
1168  aoedev_put(d);
1169  snprintf(ebuf, sizeof ebuf,
1170  "%15s e%d.%d tag=%08x@%08lx\n",
1171  "unexpected rsp",
1172  get_unaligned_be16(&h->major),
1173  h->minor,
1174  get_unaligned_be32(&h->tag),
1175  jiffies);
1176  aoechr_error(ebuf);
1177  return skb;
1178  }
1179  t = f->t;
1180  calc_rttavg(d, tsince(f->tag));
1181  t->nout--;
1182  aoecmd_work(d);
1183 
1184  spin_unlock_irqrestore(&d->lock, flags);
1185 
1186  ktcomplete(f, skb);
1187 
1188  /*
1189  * Note here that we do not perform an aoedev_put, as we are
1190  * leaving this reference for the ktio to release.
1191  */
1192  return NULL;
1193 }
1194 
1195 void
1196 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1197 {
1198  struct sk_buff_head queue;
1199 
1200  __skb_queue_head_init(&queue);
1201  aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1202  aoenet_xmit(&queue);
1203 }
1204 
1205 struct sk_buff *
1207 {
1208  struct aoe_hdr *h;
1209  struct aoe_atahdr *ah;
1210  struct frame *f;
1211  struct sk_buff *skb;
1212  struct aoetgt *t;
1213 
1214  f = newframe(d);
1215  if (f == NULL)
1216  return NULL;
1217 
1218  t = *d->tgt;
1219 
1220  /* initialize the headers & frame */
1221  skb = f->skb;
1222  h = (struct aoe_hdr *) skb_mac_header(skb);
1223  ah = (struct aoe_atahdr *) (h+1);
1224  skb_put(skb, sizeof *h + sizeof *ah);
1225  memset(h, 0, skb->len);
1226  f->tag = aoehdr_atainit(d, t, h);
1227  fhash(f);
1228  t->nout++;
1229  f->waited = 0;
1230 
1231  /* set up ata header */
1232  ah->scnt = 1;
1233  ah->cmdstat = ATA_CMD_ID_ATA;
1234  ah->lba3 = 0xa0;
1235 
1236  skb->dev = t->ifp->nd;
1237 
1238  d->rttavg = MAXTIMER;
1239  d->timer.function = rexmit_timer;
1240 
1241  return skb_clone(skb, GFP_ATOMIC);
1242 }
1243 
1244 static struct aoetgt *
1245 addtgt(struct aoedev *d, char *addr, ulong nframes)
1246 {
1247  struct aoetgt *t, **tt, **te;
1248 
1249  tt = d->targets;
1250  te = tt + NTARGETS;
1251  for (; tt < te && *tt; tt++)
1252  ;
1253 
1254  if (tt == te) {
1256  "aoe: device addtgt failure; too many targets\n");
1257  return NULL;
1258  }
1259  t = kzalloc(sizeof(*t), GFP_ATOMIC);
1260  if (!t) {
1261  printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
1262  return NULL;
1263  }
1264 
1265  d->ntargets++;
1266  t->nframes = nframes;
1267  t->d = d;
1268  memcpy(t->addr, addr, sizeof t->addr);
1269  t->ifp = t->ifs;
1270  t->maxout = t->nframes;
1271  INIT_LIST_HEAD(&t->ffree);
1272  return *tt = t;
1273 }
1274 
1275 static void
1276 setdbcnt(struct aoedev *d)
1277 {
1278  struct aoetgt **t, **e;
1279  int bcnt = 0;
1280 
1281  t = d->targets;
1282  e = t + NTARGETS;
1283  for (; t < e && *t; t++)
1284  if (bcnt == 0 || bcnt > (*t)->minbcnt)
1285  bcnt = (*t)->minbcnt;
1286  if (bcnt != d->maxbcnt) {
1287  d->maxbcnt = bcnt;
1288  pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1289  d->aoemajor, d->aoeminor, bcnt);
1290  }
1291 }
1292 
1293 static void
1294 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1295 {
1296  struct aoedev *d;
1297  struct aoeif *p, *e;
1298  int minbcnt;
1299 
1300  d = t->d;
1301  minbcnt = bcnt;
1302  p = t->ifs;
1303  e = p + NAOEIFS;
1304  for (; p < e; p++) {
1305  if (p->nd == NULL)
1306  break; /* end of the valid interfaces */
1307  if (p->nd == nd) {
1308  p->bcnt = bcnt; /* we're updating */
1309  nd = NULL;
1310  } else if (minbcnt > p->bcnt)
1311  minbcnt = p->bcnt; /* find the min interface */
1312  }
1313  if (nd) {
1314  if (p == e) {
1315  pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1316  return;
1317  }
1318  dev_hold(nd);
1319  p->nd = nd;
1320  p->bcnt = bcnt;
1321  }
1322  t->minbcnt = minbcnt;
1323  setdbcnt(d);
1324 }
1325 
1326 void
1328 {
1329  struct aoedev *d;
1330  struct aoe_hdr *h;
1331  struct aoe_cfghdr *ch;
1332  struct aoetgt *t;
1333  ulong flags, aoemajor;
1334  struct sk_buff *sl;
1335  struct sk_buff_head queue;
1336  u16 n;
1337 
1338  sl = NULL;
1339  h = (struct aoe_hdr *) skb_mac_header(skb);
1340  ch = (struct aoe_cfghdr *) (h+1);
1341 
1342  /*
1343  * Enough people have their dip switches set backwards to
1344  * warrant a loud message for this special case.
1345  */
1346  aoemajor = get_unaligned_be16(&h->major);
1347  if (aoemajor == 0xfff) {
1348  printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1349  "Check shelf dip switches.\n");
1350  return;
1351  }
1352  if (aoemajor == 0xffff) {
1353  pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1354  aoemajor, (int) h->minor);
1355  return;
1356  }
1357  if (h->minor == 0xff) {
1358  pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1359  aoemajor, (int) h->minor);
1360  return;
1361  }
1362 
1363  n = be16_to_cpu(ch->bufcnt);
1364  if (n > aoe_maxout) /* keep it reasonable */
1365  n = aoe_maxout;
1366 
1367  d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1368  if (d == NULL) {
1369  pr_info("aoe: device allocation failure\n");
1370  return;
1371  }
1372 
1373  spin_lock_irqsave(&d->lock, flags);
1374 
1375  t = gettgt(d, h->src);
1376  if (!t) {
1377  t = addtgt(d, h->src, n);
1378  if (!t)
1379  goto bail;
1380  }
1381  n = skb->dev->mtu;
1382  n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1383  n /= 512;
1384  if (n > ch->scnt)
1385  n = ch->scnt;
1386  n = n ? n * 512 : DEFAULTBCNT;
1387  setifbcnt(t, skb->dev, n);
1388 
1389  /* don't change users' perspective */
1390  if (d->nopen == 0) {
1391  d->fw_ver = be16_to_cpu(ch->fwver);
1392  sl = aoecmd_ata_id(d);
1393  }
1394 bail:
1395  spin_unlock_irqrestore(&d->lock, flags);
1396  aoedev_put(d);
1397  if (sl) {
1398  __skb_queue_head_init(&queue);
1399  __skb_queue_tail(&queue, sl);
1400  aoenet_xmit(&queue);
1401  }
1402 }
1403 
1404 void
1406 {
1407  struct aoetgt **t, **te;
1408 
1409  d->mintimer = MINTIMER;
1410  d->maxbcnt = 0;
1411 
1412  t = d->targets;
1413  te = t + NTARGETS;
1414  for (; t < te && *t; t++)
1415  (*t)->maxout = (*t)->nframes;
1416 }
1417 
1418 void
1419 aoe_failbuf(struct aoedev *d, struct buf *buf)
1420 {
1421  if (buf == NULL)
1422  return;
1423  buf->resid = 0;
1424  clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1425  if (buf->nframesout == 0)
1426  aoe_end_buf(d, buf);
1427 }
1428 
1429 void
1431 {
1432  struct frame *f;
1433  struct aoedev *d;
1434  LIST_HEAD(flist);
1435  struct list_head *pos;
1436  struct sk_buff *skb;
1437  ulong flags;
1438 
1439  spin_lock_irqsave(&iocq.lock, flags);
1440  list_splice_init(&iocq.head, &flist);
1441  spin_unlock_irqrestore(&iocq.lock, flags);
1442  while (!list_empty(&flist)) {
1443  pos = flist.next;
1444  list_del(pos);
1445  f = list_entry(pos, struct frame, head);
1446  d = f->t->d;
1447  skb = f->r_skb;
1448  spin_lock_irqsave(&d->lock, flags);
1449  if (f->buf) {
1450  f->buf->nframesout--;
1451  aoe_failbuf(d, f->buf);
1452  }
1453  aoe_freetframe(f);
1454  spin_unlock_irqrestore(&d->lock, flags);
1455  dev_kfree_skb(skb);
1456  aoedev_put(d);
1457  }
1458 }
1459 
1460 int __init
1462 {
1463  INIT_LIST_HEAD(&iocq.head);
1464  spin_lock_init(&iocq.lock);
1465  init_waitqueue_head(&ktiowq);
1466  kts.name = "aoe_ktio";
1467  kts.fn = ktio;
1468  kts.waitq = &ktiowq;
1469  kts.lock = &iocq.lock;
1470  return aoe_ktstart(&kts);
1471 }
1472 
1473 void
1475 {
1476  aoe_ktstop(&kts);
1477  aoe_flush_iocq();
1478 }