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