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