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