blob: 39da28d344fe9465186f6bed89be04ff449a7f77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */
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>
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050011#include <linux/genhd.h>
Ed L. Cashin475172f2005-09-29 12:47:40 -040012#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "aoe.h"
14
15#define TIMERTICK (HZ / 10)
16#define MINTIMER (2 * TIMERTICK)
17#define MAXTIMER (HZ << 1)
18#define MAXWAIT (60 * 3) /* After MAXWAIT seconds, give up and fail dev */
19
20static struct sk_buff *
21new_skb(struct net_device *if_dev, ulong len)
22{
23 struct sk_buff *skb;
24
25 skb = alloc_skb(len, GFP_ATOMIC);
26 if (skb) {
27 skb->nh.raw = skb->mac.raw = skb->data;
28 skb->dev = if_dev;
29 skb->protocol = __constant_htons(ETH_P_AOE);
30 skb->priority = 0;
31 skb_put(skb, len);
Ed L. Cashin50bba752006-01-19 12:37:24 -050032 memset(skb->head, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 skb->next = skb->prev = NULL;
34
35 /* tell the network layer not to perform IP checksums
36 * or to get the NIC to do it
37 */
38 skb->ip_summed = CHECKSUM_NONE;
39 }
40 return skb;
41}
42
43static struct sk_buff *
44skb_prepare(struct aoedev *d, struct frame *f)
45{
46 struct sk_buff *skb;
47 char *p;
48
49 skb = new_skb(d->ifp, f->ndata + f->writedatalen);
50 if (!skb) {
51 printk(KERN_INFO "aoe: skb_prepare: failure to allocate skb\n");
52 return NULL;
53 }
54
55 p = skb->mac.raw;
56 memcpy(p, f->data, f->ndata);
57
58 if (f->writedatalen) {
59 p += sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
60 memcpy(p, f->bufaddr, f->writedatalen);
61 }
62
63 return skb;
64}
65
66static struct frame *
67getframe(struct aoedev *d, int tag)
68{
69 struct frame *f, *e;
70
71 f = d->frames;
72 e = f + d->nframes;
73 for (; f<e; f++)
74 if (f->tag == tag)
75 return f;
76 return NULL;
77}
78
79/*
80 * Leave the top bit clear so we have tagspace for userland.
81 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
82 * This driver reserves tag -1 to mean "unused frame."
83 */
84static int
85newtag(struct aoedev *d)
86{
87 register ulong n;
88
89 n = jiffies & 0xffff;
90 return n |= (++d->lasttag & 0x7fff) << 16;
91}
92
93static int
94aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h)
95{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
99 memcpy(h->dst, d->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700100 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700102 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 h->minor = d->aoeminor;
104 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700105 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return host_tag;
108}
109
110static void
111aoecmd_ata_rw(struct aoedev *d, struct frame *f)
112{
113 struct aoe_hdr *h;
114 struct aoe_atahdr *ah;
115 struct buf *buf;
116 struct sk_buff *skb;
117 ulong bcnt;
118 register sector_t sector;
119 char writebit, extbit;
120
121 writebit = 0x10;
122 extbit = 0x4;
123
124 buf = d->inprocess;
125
126 sector = buf->sector;
127 bcnt = buf->bv_resid;
128 if (bcnt > MAXATADATA)
129 bcnt = MAXATADATA;
130
131 /* initialize the headers & frame */
132 h = (struct aoe_hdr *) f->data;
133 ah = (struct aoe_atahdr *) (h+1);
134 f->ndata = sizeof *h + sizeof *ah;
135 memset(h, 0, f->ndata);
136 f->tag = aoehdr_atainit(d, h);
137 f->waited = 0;
138 f->buf = buf;
139 f->bufaddr = buf->bufaddr;
140
141 /* set up ata header */
142 ah->scnt = bcnt >> 9;
143 ah->lba0 = sector;
144 ah->lba1 = sector >>= 8;
145 ah->lba2 = sector >>= 8;
146 ah->lba3 = sector >>= 8;
147 if (d->flags & DEVFL_EXT) {
148 ah->aflags |= AOEAFL_EXT;
149 ah->lba4 = sector >>= 8;
150 ah->lba5 = sector >>= 8;
151 } else {
152 extbit = 0;
153 ah->lba3 &= 0x0f;
154 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
155 }
156
157 if (bio_data_dir(buf->bio) == WRITE) {
158 ah->aflags |= AOEAFL_WRITE;
159 f->writedatalen = bcnt;
160 } else {
161 writebit = 0;
162 f->writedatalen = 0;
163 }
164
165 ah->cmdstat = WIN_READ | writebit | extbit;
166
167 /* mark all tracking fields and load out */
168 buf->nframesout += 1;
169 buf->bufaddr += bcnt;
170 buf->bv_resid -= bcnt;
171/* printk(KERN_INFO "aoe: bv_resid=%ld\n", buf->bv_resid); */
172 buf->resid -= bcnt;
173 buf->sector += bcnt >> 9;
174 if (buf->resid == 0) {
175 d->inprocess = NULL;
176 } else if (buf->bv_resid == 0) {
177 buf->bv++;
178 buf->bv_resid = buf->bv->bv_len;
179 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
180 }
181
182 skb = skb_prepare(d, f);
183 if (skb) {
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700184 skb->next = NULL;
185 if (d->sendq_hd)
186 d->sendq_tl->next = skb;
187 else
188 d->sendq_hd = skb;
189 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191}
192
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500193/* some callers cannot sleep, and they can call this function,
194 * transmitting the packets later, when interrupts are on
195 */
196static struct sk_buff *
197aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
198{
199 struct aoe_hdr *h;
200 struct aoe_cfghdr *ch;
201 struct sk_buff *skb, *sl, *sl_tail;
202 struct net_device *ifp;
203
204 sl = sl_tail = NULL;
205
206 read_lock(&dev_base_lock);
207 for (ifp = dev_base; ifp; dev_put(ifp), ifp = ifp->next) {
208 dev_hold(ifp);
209 if (!is_aoe_netif(ifp))
210 continue;
211
212 skb = new_skb(ifp, sizeof *h + sizeof *ch);
213 if (skb == NULL) {
214 printk(KERN_INFO "aoe: aoecmd_cfg: skb alloc failure\n");
215 continue;
216 }
217 if (sl_tail == NULL)
218 sl_tail = skb;
219 h = (struct aoe_hdr *) skb->mac.raw;
220 memset(h, 0, sizeof *h + sizeof *ch);
221
222 memset(h->dst, 0xff, sizeof h->dst);
223 memcpy(h->src, ifp->dev_addr, sizeof h->src);
224 h->type = __constant_cpu_to_be16(ETH_P_AOE);
225 h->verfl = AOE_HVER;
226 h->major = cpu_to_be16(aoemajor);
227 h->minor = aoeminor;
228 h->cmd = AOECMD_CFG;
229
230 skb->next = sl;
231 sl = skb;
232 }
233 read_unlock(&dev_base_lock);
234
235 if (tail != NULL)
236 *tail = sl_tail;
237 return sl;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* enters with d->lock held */
241void
242aoecmd_work(struct aoedev *d)
243{
244 struct frame *f;
245 struct buf *buf;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500246
247 if (d->flags & DEVFL_PAUSE) {
248 if (!aoedev_isbusy(d))
249 d->sendq_hd = aoecmd_cfg_pkts(d->aoemajor,
250 d->aoeminor, &d->sendq_tl);
251 return;
252 }
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254loop:
255 f = getframe(d, FREETAG);
256 if (f == NULL)
257 return;
258 if (d->inprocess == NULL) {
259 if (list_empty(&d->bufq))
260 return;
261 buf = container_of(d->bufq.next, struct buf, bufs);
262 list_del(d->bufq.next);
263/*printk(KERN_INFO "aoecmd_work: bi_size=%ld\n", buf->bio->bi_size); */
264 d->inprocess = buf;
265 }
266 aoecmd_ata_rw(d, f);
267 goto loop;
268}
269
270static void
271rexmit(struct aoedev *d, struct frame *f)
272{
273 struct sk_buff *skb;
274 struct aoe_hdr *h;
275 char buf[128];
276 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 n = newtag(d);
279
280 snprintf(buf, sizeof buf,
281 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
282 "retransmit",
283 d->aoemajor, d->aoeminor, f->tag, jiffies, n);
284 aoechr_error(buf);
285
286 h = (struct aoe_hdr *) f->data;
287 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700288 h->tag = cpu_to_be32(n);
Ed L. Cashin2dd5e422006-01-19 13:46:25 -0500289 memcpy(h->dst, d->addr, sizeof h->dst);
290 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 skb = skb_prepare(d, f);
293 if (skb) {
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700294 skb->next = NULL;
295 if (d->sendq_hd)
296 d->sendq_tl->next = skb;
297 else
298 d->sendq_hd = skb;
299 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}
302
303static int
304tsince(int tag)
305{
306 int n;
307
308 n = jiffies & 0xffff;
309 n -= tag & 0xffff;
310 if (n < 0)
311 n += 1<<16;
312 return n;
313}
314
315static void
316rexmit_timer(ulong vp)
317{
318 struct aoedev *d;
319 struct frame *f, *e;
320 struct sk_buff *sl;
321 register long timeout;
322 ulong flags, n;
323
324 d = (struct aoedev *) vp;
325 sl = NULL;
326
327 /* timeout is always ~150% of the moving average */
328 timeout = d->rttavg;
329 timeout += timeout >> 1;
330
331 spin_lock_irqsave(&d->lock, flags);
332
333 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500334 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return;
336 }
337 f = d->frames;
338 e = f + d->nframes;
339 for (; f<e; f++) {
340 if (f->tag != FREETAG && tsince(f->tag) >= timeout) {
341 n = f->waited += timeout;
342 n /= HZ;
343 if (n > MAXWAIT) { /* waited too long. device failure. */
344 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 rexmit(d, f);
348 }
349 }
350
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700351 sl = d->sendq_hd;
352 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (sl) {
354 n = d->rttavg <<= 1;
355 if (n > MAXTIMER)
356 d->rttavg = MAXTIMER;
357 }
358
359 d->timer.expires = jiffies + TIMERTICK;
360 add_timer(&d->timer);
361
362 spin_unlock_irqrestore(&d->lock, flags);
363
364 aoenet_xmit(sl);
365}
366
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500367/* this function performs work that has been deferred until sleeping is OK
368 */
369void
370aoecmd_sleepwork(void *vp)
371{
372 struct aoedev *d = (struct aoedev *) vp;
373
374 if (d->flags & DEVFL_GDALLOC)
375 aoeblk_gdalloc(d);
376
377 if (d->flags & DEVFL_NEWSIZE) {
378 struct block_device *bd;
379 unsigned long flags;
380 u64 ssize;
381
382 ssize = d->gd->capacity;
383 bd = bdget_disk(d->gd, 0);
384
385 if (bd) {
386 mutex_lock(&bd->bd_inode->i_mutex);
387 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
388 mutex_unlock(&bd->bd_inode->i_mutex);
389 bdput(bd);
390 }
391 spin_lock_irqsave(&d->lock, flags);
392 d->flags |= DEVFL_UP;
393 d->flags &= ~DEVFL_NEWSIZE;
394 spin_unlock_irqrestore(&d->lock, flags);
395 }
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static void
399ataid_complete(struct aoedev *d, unsigned char *id)
400{
401 u64 ssize;
402 u16 n;
403
404 /* word 83: command set supported */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400405 n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* word 86: command set/feature enabled */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400408 n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (n & (1<<10)) { /* bit 10: LBA 48 */
411 d->flags |= DEVFL_EXT;
412
413 /* word 100: number lba48 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400414 ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* set as in ide-disk.c:init_idedisk_capacity */
417 d->geo.cylinders = ssize;
418 d->geo.cylinders /= (255 * 63);
419 d->geo.heads = 255;
420 d->geo.sectors = 63;
421 } else {
422 d->flags &= ~DEVFL_EXT;
423
424 /* number lba28 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400425 ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* NOTE: obsolete in ATA 6 */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400428 d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1]));
429 d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1]));
430 d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500432
433 if (d->ssize != ssize)
434 printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu "
435 "sectors\n", (unsigned long long)mac_addr(d->addr),
436 d->aoemajor, d->aoeminor,
437 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 d->ssize = ssize;
439 d->geo.start = 0;
440 if (d->gd != NULL) {
441 d->gd->capacity = ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500442 d->flags |= DEVFL_NEWSIZE;
443 } else {
444 if (d->flags & DEVFL_GDALLOC) {
445 printk(KERN_INFO "aoe: %s: %s e%lu.%lu, %s\n",
446 __FUNCTION__,
447 "can't schedule work for",
448 d->aoemajor, d->aoeminor,
449 "it's already on! (This really shouldn't happen).\n");
450 return;
451 }
452 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static void
458calc_rttavg(struct aoedev *d, int rtt)
459{
460 register long n;
461
462 n = rtt;
463 if (n < MINTIMER)
464 n = MINTIMER;
465 else if (n > MAXTIMER)
466 n = MAXTIMER;
467
468 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
469 n -= d->rttavg;
470 d->rttavg += n >> 2;
471}
472
473void
474aoecmd_ata_rsp(struct sk_buff *skb)
475{
476 struct aoedev *d;
477 struct aoe_hdr *hin;
478 struct aoe_atahdr *ahin, *ahout;
479 struct frame *f;
480 struct buf *buf;
481 struct sk_buff *sl;
482 register long n;
483 ulong flags;
484 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700485 u16 aoemajor;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 hin = (struct aoe_hdr *) skb->mac.raw;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700488 aoemajor = be16_to_cpu(hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700489 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (d == NULL) {
491 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
492 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700493 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 aoechr_error(ebuf);
495 return;
496 }
497
498 spin_lock_irqsave(&d->lock, flags);
499
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700500 f = getframe(d, be32_to_cpu(hin->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (f == NULL) {
502 spin_unlock_irqrestore(&d->lock, flags);
503 snprintf(ebuf, sizeof ebuf,
504 "%15s e%d.%d tag=%08x@%08lx\n",
505 "unexpected rsp",
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700506 be16_to_cpu(hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 hin->minor,
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700508 be32_to_cpu(hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 jiffies);
510 aoechr_error(ebuf);
511 return;
512 }
513
514 calc_rttavg(d, tsince(f->tag));
515
516 ahin = (struct aoe_atahdr *) (hin+1);
517 ahout = (struct aoe_atahdr *) (f->data + sizeof(struct aoe_hdr));
518 buf = f->buf;
519
Ed L. Cashin9d419652006-02-07 11:37:24 -0500520 if (ahout->cmdstat == WIN_IDENTIFY)
521 d->flags &= ~DEVFL_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
523 printk(KERN_CRIT "aoe: aoecmd_ata_rsp: ata error cmd=%2.2Xh "
524 "stat=%2.2Xh from e%ld.%ld\n",
525 ahout->cmdstat, ahin->cmdstat,
526 d->aoemajor, d->aoeminor);
527 if (buf)
528 buf->flags |= BUFFL_FAIL;
529 } else {
530 switch (ahout->cmdstat) {
531 case WIN_READ:
532 case WIN_READ_EXT:
533 n = ahout->scnt << 9;
534 if (skb->len - sizeof *hin - sizeof *ahin < n) {
535 printk(KERN_CRIT "aoe: aoecmd_ata_rsp: runt "
536 "ata data size in read. skb->len=%d\n",
537 skb->len);
538 /* fail frame f? just returning will rexmit. */
539 spin_unlock_irqrestore(&d->lock, flags);
540 return;
541 }
542 memcpy(f->bufaddr, ahin+1, n);
543 case WIN_WRITE:
544 case WIN_WRITE_EXT:
545 break;
546 case WIN_IDENTIFY:
547 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
548 printk(KERN_INFO "aoe: aoecmd_ata_rsp: runt data size "
549 "in ataid. skb->len=%d\n", skb->len);
550 spin_unlock_irqrestore(&d->lock, flags);
551 return;
552 }
553 ataid_complete(d, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 default:
556 printk(KERN_INFO "aoe: aoecmd_ata_rsp: unrecognized "
557 "outbound ata command %2.2Xh for %d.%d\n",
558 ahout->cmdstat,
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700559 be16_to_cpu(hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 hin->minor);
561 }
562 }
563
564 if (buf) {
565 buf->nframesout -= 1;
566 if (buf->nframesout == 0 && buf->resid == 0) {
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700567 unsigned long duration = jiffies - buf->start_time;
568 unsigned long n_sect = buf->bio->bi_size >> 9;
569 struct gendisk *disk = d->gd;
Jens Axboe496456c2005-11-01 09:54:23 +0100570 const int rw = bio_data_dir(buf->bio);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700571
Jens Axboe496456c2005-11-01 09:54:23 +0100572 disk_stat_inc(disk, ios[rw]);
573 disk_stat_add(disk, ticks[rw], duration);
574 disk_stat_add(disk, sectors[rw], n_sect);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700575 disk_stat_add(disk, io_ticks, duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
577 bio_endio(buf->bio, buf->bio->bi_size, n);
578 mempool_free(buf, d->bufpool);
579 }
580 }
581
582 f->buf = NULL;
583 f->tag = FREETAG;
584
585 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700586 sl = d->sendq_hd;
587 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 aoenet_xmit(sl);
591}
592
593void
594aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
595{
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500596 struct sk_buff *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500598 sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 aoenet_xmit(sl);
601}
602
603/*
604 * Since we only call this in one place (and it only prepares one frame)
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700605 * we just return the skb. Usually we'd chain it up to the aoedev sendq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607static struct sk_buff *
608aoecmd_ata_id(struct aoedev *d)
609{
610 struct aoe_hdr *h;
611 struct aoe_atahdr *ah;
612 struct frame *f;
613 struct sk_buff *skb;
614
615 f = getframe(d, FREETAG);
616 if (f == NULL) {
617 printk(KERN_CRIT "aoe: aoecmd_ata_id: can't get a frame. "
618 "This shouldn't happen.\n");
619 return NULL;
620 }
621
622 /* initialize the headers & frame */
623 h = (struct aoe_hdr *) f->data;
624 ah = (struct aoe_atahdr *) (h+1);
625 f->ndata = sizeof *h + sizeof *ah;
626 memset(h, 0, f->ndata);
627 f->tag = aoehdr_atainit(d, h);
628 f->waited = 0;
629 f->writedatalen = 0;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* set up ata header */
632 ah->scnt = 1;
633 ah->cmdstat = WIN_IDENTIFY;
634 ah->lba3 = 0xa0;
635
636 skb = skb_prepare(d, f);
637
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500638 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return skb;
642}
643
644void
645aoecmd_cfg_rsp(struct sk_buff *skb)
646{
647 struct aoedev *d;
648 struct aoe_hdr *h;
649 struct aoe_cfghdr *ch;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700650 ulong flags, sysminor, aoemajor;
651 u16 bufcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct sk_buff *sl;
Ed L. Cashineaf0a3c2006-01-19 13:46:20 -0500653 enum { MAXFRAMES = 16 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 h = (struct aoe_hdr *) skb->mac.raw;
656 ch = (struct aoe_cfghdr *) (h+1);
657
658 /*
659 * Enough people have their dip switches set backwards to
660 * warrant a loud message for this special case.
661 */
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700662 aoemajor = be16_to_cpu(h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (aoemajor == 0xfff) {
664 printk(KERN_CRIT "aoe: aoecmd_cfg_rsp: Warning: shelf "
665 "address is all ones. Check shelf dip switches\n");
666 return;
667 }
668
669 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700670 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
671 printk(KERN_INFO
672 "aoe: e%ld.%d: minor number too large\n",
673 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return;
675 }
676
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700677 bufcnt = be16_to_cpu(ch->bufcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (bufcnt > MAXFRAMES) /* keep it reasonable */
679 bufcnt = MAXFRAMES;
680
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500681 d = aoedev_by_sysminor_m(sysminor, bufcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (d == NULL) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500683 printk(KERN_INFO "aoe: aoecmd_cfg_rsp: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
685 }
686
687 spin_lock_irqsave(&d->lock, flags);
688
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500689 /* permit device to migrate mac and network interface */
690 d->ifp = skb->dev;
691 memcpy(d->addr, h->src, sizeof d->addr);
692
693 /* don't change users' perspective */
694 if (d->nopen && !(d->flags & DEVFL_PAUSE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 spin_unlock_irqrestore(&d->lock, flags);
696 return;
697 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500698 d->flags |= DEVFL_PAUSE; /* force pause */
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700699 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500701 /* check for already outstanding ataid */
702 sl = aoedev_isbusy(d) == 0 ? aoecmd_ata_id(d) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 spin_unlock_irqrestore(&d->lock, flags);
705
706 aoenet_xmit(sl);
707}
708