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