blob: 63c456082d8114f63d01b9f71241aafc6486ef69 [file] [log] [blame]
Ed L. Cashin26114642006-09-20 14:36:48 -04001/* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Ed L. Cashine407a7f2006-09-20 14:36:49 -040020struct sk_buff *
21new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 skb->protocol = __constant_htons(ETH_P_AOE);
29 skb->priority = 0;
30 skb_put(skb, len);
Ed L. Cashin50bba752006-01-19 12:37:24 -050031 memset(skb->head, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 skb->next = skb->prev = NULL;
33
34 /* tell the network layer not to perform IP checksums
35 * or to get the NIC to do it
36 */
37 skb->ip_summed = CHECKSUM_NONE;
38 }
39 return skb;
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static struct frame *
43getframe(struct aoedev *d, int tag)
44{
45 struct frame *f, *e;
46
47 f = d->frames;
48 e = f + d->nframes;
49 for (; f<e; f++)
50 if (f->tag == tag)
51 return f;
52 return NULL;
53}
54
55/*
56 * Leave the top bit clear so we have tagspace for userland.
57 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
58 * This driver reserves tag -1 to mean "unused frame."
59 */
60static int
61newtag(struct aoedev *d)
62{
63 register ulong n;
64
65 n = jiffies & 0xffff;
66 return n |= (++d->lasttag & 0x7fff) << 16;
67}
68
69static int
70aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h)
71{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
75 memcpy(h->dst, d->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070076 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070078 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 h->minor = d->aoeminor;
80 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070081 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 return host_tag;
84}
85
Ed L. Cashin19bf2632006-09-20 14:36:49 -040086static inline void
87put_lba(struct aoe_atahdr *ah, sector_t lba)
88{
89 ah->lba0 = lba;
90 ah->lba1 = lba >>= 8;
91 ah->lba2 = lba >>= 8;
92 ah->lba3 = lba >>= 8;
93 ah->lba4 = lba >>= 8;
94 ah->lba5 = lba >>= 8;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void
98aoecmd_ata_rw(struct aoedev *d, struct frame *f)
99{
100 struct aoe_hdr *h;
101 struct aoe_atahdr *ah;
102 struct buf *buf;
103 struct sk_buff *skb;
104 ulong bcnt;
105 register sector_t sector;
106 char writebit, extbit;
107
108 writebit = 0x10;
109 extbit = 0x4;
110
111 buf = d->inprocess;
112
113 sector = buf->sector;
114 bcnt = buf->bv_resid;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400115 if (bcnt > d->maxbcnt)
116 bcnt = d->maxbcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400119 skb = f->skb;
120 h = (struct aoe_hdr *) skb->mac.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400122 skb->len = sizeof *h + sizeof *ah;
123 memset(h, 0, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 f->tag = aoehdr_atainit(d, h);
125 f->waited = 0;
126 f->buf = buf;
127 f->bufaddr = buf->bufaddr;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400128 f->bcnt = bcnt;
129 f->lba = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* set up ata header */
132 ah->scnt = bcnt >> 9;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400133 put_lba(ah, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (d->flags & DEVFL_EXT) {
135 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 } else {
137 extbit = 0;
138 ah->lba3 &= 0x0f;
139 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
140 }
141
142 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400143 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
144 offset_in_page(f->bufaddr), bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ah->aflags |= AOEAFL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } else {
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400147 skb_shinfo(skb)->nr_frags = 0;
148 skb->len = ETH_ZLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 ah->cmdstat = WIN_READ | writebit | extbit;
153
154 /* mark all tracking fields and load out */
155 buf->nframesout += 1;
156 buf->bufaddr += bcnt;
157 buf->bv_resid -= bcnt;
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400158/* dprintk("bv_resid=%ld\n", buf->bv_resid); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 buf->resid -= bcnt;
160 buf->sector += bcnt >> 9;
161 if (buf->resid == 0) {
162 d->inprocess = NULL;
163 } else if (buf->bv_resid == 0) {
164 buf->bv++;
165 buf->bv_resid = buf->bv->bv_len;
166 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
167 }
168
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400169 skb->dev = d->ifp;
170 skb_get(skb);
171 skb->next = NULL;
172 if (d->sendq_hd)
173 d->sendq_tl->next = skb;
174 else
175 d->sendq_hd = skb;
176 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500179/* some callers cannot sleep, and they can call this function,
180 * transmitting the packets later, when interrupts are on
181 */
182static struct sk_buff *
183aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
184{
185 struct aoe_hdr *h;
186 struct aoe_cfghdr *ch;
187 struct sk_buff *skb, *sl, *sl_tail;
188 struct net_device *ifp;
189
190 sl = sl_tail = NULL;
191
192 read_lock(&dev_base_lock);
193 for (ifp = dev_base; ifp; dev_put(ifp), ifp = ifp->next) {
194 dev_hold(ifp);
195 if (!is_aoe_netif(ifp))
196 continue;
197
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400198 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500199 if (skb == NULL) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400200 iprintk("skb alloc failure\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500201 continue;
202 }
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400203 skb->dev = ifp;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500204 if (sl_tail == NULL)
205 sl_tail = skb;
206 h = (struct aoe_hdr *) skb->mac.raw;
207 memset(h, 0, sizeof *h + sizeof *ch);
208
209 memset(h->dst, 0xff, sizeof h->dst);
210 memcpy(h->src, ifp->dev_addr, sizeof h->src);
211 h->type = __constant_cpu_to_be16(ETH_P_AOE);
212 h->verfl = AOE_HVER;
213 h->major = cpu_to_be16(aoemajor);
214 h->minor = aoeminor;
215 h->cmd = AOECMD_CFG;
216
217 skb->next = sl;
218 sl = skb;
219 }
220 read_unlock(&dev_base_lock);
221
222 if (tail != NULL)
223 *tail = sl_tail;
224 return sl;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* enters with d->lock held */
228void
229aoecmd_work(struct aoedev *d)
230{
231 struct frame *f;
232 struct buf *buf;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500233
234 if (d->flags & DEVFL_PAUSE) {
235 if (!aoedev_isbusy(d))
236 d->sendq_hd = aoecmd_cfg_pkts(d->aoemajor,
237 d->aoeminor, &d->sendq_tl);
238 return;
239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241loop:
242 f = getframe(d, FREETAG);
243 if (f == NULL)
244 return;
245 if (d->inprocess == NULL) {
246 if (list_empty(&d->bufq))
247 return;
248 buf = container_of(d->bufq.next, struct buf, bufs);
249 list_del(d->bufq.next);
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400250/*dprintk("bi_size=%ld\n", buf->bio->bi_size); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 d->inprocess = buf;
252 }
253 aoecmd_ata_rw(d, f);
254 goto loop;
255}
256
257static void
258rexmit(struct aoedev *d, struct frame *f)
259{
260 struct sk_buff *skb;
261 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400262 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 char buf[128];
264 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 n = newtag(d);
267
268 snprintf(buf, sizeof buf,
269 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
270 "retransmit",
271 d->aoemajor, d->aoeminor, f->tag, jiffies, n);
272 aoechr_error(buf);
273
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400274 skb = f->skb;
275 h = (struct aoe_hdr *) skb->mac.raw;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400276 ah = (struct aoe_atahdr *) (h+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700278 h->tag = cpu_to_be32(n);
Ed L. Cashin2dd5e422006-01-19 13:46:25 -0500279 memcpy(h->dst, d->addr, sizeof h->dst);
280 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400282 n = DEFAULTBCNT / 512;
283 if (ah->scnt > n) {
284 ah->scnt = n;
285 if (ah->aflags & AOEAFL_WRITE)
286 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
287 offset_in_page(f->bufaddr), DEFAULTBCNT);
288 if (++d->lostjumbo > (d->nframes << 1))
289 if (d->maxbcnt != DEFAULTBCNT) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400290 iprintk("too many lost jumbo - using 1KB frames.\n");
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400291 d->maxbcnt = DEFAULTBCNT;
292 d->flags |= DEVFL_MAXBCNT;
293 }
294 }
295
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400296 skb->dev = d->ifp;
297 skb_get(skb);
298 skb->next = NULL;
299 if (d->sendq_hd)
300 d->sendq_tl->next = skb;
301 else
302 d->sendq_hd = skb;
303 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306static int
307tsince(int tag)
308{
309 int n;
310
311 n = jiffies & 0xffff;
312 n -= tag & 0xffff;
313 if (n < 0)
314 n += 1<<16;
315 return n;
316}
317
318static void
319rexmit_timer(ulong vp)
320{
321 struct aoedev *d;
322 struct frame *f, *e;
323 struct sk_buff *sl;
324 register long timeout;
325 ulong flags, n;
326
327 d = (struct aoedev *) vp;
328 sl = NULL;
329
330 /* timeout is always ~150% of the moving average */
331 timeout = d->rttavg;
332 timeout += timeout >> 1;
333
334 spin_lock_irqsave(&d->lock, flags);
335
336 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500337 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return;
339 }
340 f = d->frames;
341 e = f + d->nframes;
342 for (; f<e; f++) {
343 if (f->tag != FREETAG && tsince(f->tag) >= timeout) {
344 n = f->waited += timeout;
345 n /= HZ;
346 if (n > MAXWAIT) { /* waited too long. device failure. */
347 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 rexmit(d, f);
351 }
352 }
353
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700354 sl = d->sendq_hd;
355 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (sl) {
357 n = d->rttavg <<= 1;
358 if (n > MAXTIMER)
359 d->rttavg = MAXTIMER;
360 }
361
362 d->timer.expires = jiffies + TIMERTICK;
363 add_timer(&d->timer);
364
365 spin_unlock_irqrestore(&d->lock, flags);
366
367 aoenet_xmit(sl);
368}
369
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500370/* this function performs work that has been deferred until sleeping is OK
371 */
372void
373aoecmd_sleepwork(void *vp)
374{
375 struct aoedev *d = (struct aoedev *) vp;
376
377 if (d->flags & DEVFL_GDALLOC)
378 aoeblk_gdalloc(d);
379
380 if (d->flags & DEVFL_NEWSIZE) {
381 struct block_device *bd;
382 unsigned long flags;
383 u64 ssize;
384
385 ssize = d->gd->capacity;
386 bd = bdget_disk(d->gd, 0);
387
388 if (bd) {
389 mutex_lock(&bd->bd_inode->i_mutex);
390 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
391 mutex_unlock(&bd->bd_inode->i_mutex);
392 bdput(bd);
393 }
394 spin_lock_irqsave(&d->lock, flags);
395 d->flags |= DEVFL_UP;
396 d->flags &= ~DEVFL_NEWSIZE;
397 spin_unlock_irqrestore(&d->lock, flags);
398 }
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static void
402ataid_complete(struct aoedev *d, unsigned char *id)
403{
404 u64 ssize;
405 u16 n;
406
407 /* word 83: command set supported */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400408 n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* word 86: command set/feature enabled */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400411 n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (n & (1<<10)) { /* bit 10: LBA 48 */
414 d->flags |= DEVFL_EXT;
415
416 /* word 100: number lba48 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400417 ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* set as in ide-disk.c:init_idedisk_capacity */
420 d->geo.cylinders = ssize;
421 d->geo.cylinders /= (255 * 63);
422 d->geo.heads = 255;
423 d->geo.sectors = 63;
424 } else {
425 d->flags &= ~DEVFL_EXT;
426
427 /* number lba28 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400428 ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* NOTE: obsolete in ATA 6 */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400431 d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1]));
432 d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1]));
433 d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500435
436 if (d->ssize != ssize)
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400437 iprintk("%012llx e%lu.%lu v%04x has %llu sectors\n",
438 (unsigned long long)mac_addr(d->addr),
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500439 d->aoemajor, d->aoeminor,
440 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 d->ssize = ssize;
442 d->geo.start = 0;
443 if (d->gd != NULL) {
444 d->gd->capacity = ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500445 d->flags |= DEVFL_NEWSIZE;
446 } else {
447 if (d->flags & DEVFL_GDALLOC) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400448 eprintk("can't schedule work for e%lu.%lu, %s\n",
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500449 d->aoemajor, d->aoeminor,
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400450 "it's already on! This shouldn't happen.\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500451 return;
452 }
453 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static void
459calc_rttavg(struct aoedev *d, int rtt)
460{
461 register long n;
462
463 n = rtt;
464 if (n < MINTIMER)
465 n = MINTIMER;
466 else if (n > MAXTIMER)
467 n = MAXTIMER;
468
469 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
470 n -= d->rttavg;
471 d->rttavg += n >> 2;
472}
473
474void
475aoecmd_ata_rsp(struct sk_buff *skb)
476{
477 struct aoedev *d;
478 struct aoe_hdr *hin;
479 struct aoe_atahdr *ahin, *ahout;
480 struct frame *f;
481 struct buf *buf;
482 struct sk_buff *sl;
483 register long n;
484 ulong flags;
485 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700486 u16 aoemajor;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 hin = (struct aoe_hdr *) skb->mac.raw;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700489 aoemajor = be16_to_cpu(hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700490 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (d == NULL) {
492 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
493 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700494 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 aoechr_error(ebuf);
496 return;
497 }
498
499 spin_lock_irqsave(&d->lock, flags);
500
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700501 f = getframe(d, be32_to_cpu(hin->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (f == NULL) {
503 spin_unlock_irqrestore(&d->lock, flags);
504 snprintf(ebuf, sizeof ebuf,
505 "%15s e%d.%d tag=%08x@%08lx\n",
506 "unexpected rsp",
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700507 be16_to_cpu(hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 hin->minor,
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700509 be32_to_cpu(hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 jiffies);
511 aoechr_error(ebuf);
512 return;
513 }
514
515 calc_rttavg(d, tsince(f->tag));
516
517 ahin = (struct aoe_atahdr *) (hin+1);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400518 ahout = (struct aoe_atahdr *) (f->skb->mac.raw + sizeof(struct aoe_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 buf = f->buf;
520
Ed L. Cashin9d419652006-02-07 11:37:24 -0500521 if (ahout->cmdstat == WIN_IDENTIFY)
522 d->flags &= ~DEVFL_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400524 eprintk("ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ahout->cmdstat, ahin->cmdstat,
526 d->aoemajor, d->aoeminor);
527 if (buf)
528 buf->flags |= BUFFL_FAIL;
529 } else {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400530 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 switch (ahout->cmdstat) {
532 case WIN_READ:
533 case WIN_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400535 eprintk("runt data size in read. skb->len=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 skb->len);
537 /* fail frame f? just returning will rexmit. */
538 spin_unlock_irqrestore(&d->lock, flags);
539 return;
540 }
541 memcpy(f->bufaddr, ahin+1, n);
542 case WIN_WRITE:
543 case WIN_WRITE_EXT:
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400544 if (f->bcnt -= n) {
545 f->bufaddr += n;
546 put_lba(ahout, f->lba += ahout->scnt);
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400547 n = f->bcnt;
548 if (n > DEFAULTBCNT)
549 n = DEFAULTBCNT;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400550 ahout->scnt = n >> 9;
551 if (ahout->aflags & AOEAFL_WRITE)
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400552 skb_fill_page_desc(f->skb, 0,
553 virt_to_page(f->bufaddr),
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400554 offset_in_page(f->bufaddr), n);
555 skb_get(f->skb);
556 f->skb->next = NULL;
557 spin_unlock_irqrestore(&d->lock, flags);
558 aoenet_xmit(f->skb);
559 return;
560 }
561 if (n > DEFAULTBCNT)
562 d->lostjumbo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 case WIN_IDENTIFY:
565 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400566 iprintk("runt data size in ataid. skb->len=%d\n",
567 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 spin_unlock_irqrestore(&d->lock, flags);
569 return;
570 }
571 ataid_complete(d, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 default:
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400574 iprintk("unrecognized ata command %2.2Xh for %d.%d\n",
575 ahout->cmdstat,
576 be16_to_cpu(hin->major),
577 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 }
580
581 if (buf) {
582 buf->nframesout -= 1;
583 if (buf->nframesout == 0 && buf->resid == 0) {
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700584 unsigned long duration = jiffies - buf->start_time;
585 unsigned long n_sect = buf->bio->bi_size >> 9;
586 struct gendisk *disk = d->gd;
Jens Axboe496456c2005-11-01 09:54:23 +0100587 const int rw = bio_data_dir(buf->bio);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700588
Jens Axboe496456c2005-11-01 09:54:23 +0100589 disk_stat_inc(disk, ios[rw]);
590 disk_stat_add(disk, ticks[rw], duration);
591 disk_stat_add(disk, sectors[rw], n_sect);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700592 disk_stat_add(disk, io_ticks, duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
594 bio_endio(buf->bio, buf->bio->bi_size, n);
595 mempool_free(buf, d->bufpool);
596 }
597 }
598
599 f->buf = NULL;
600 f->tag = FREETAG;
601
602 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700603 sl = d->sendq_hd;
604 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 aoenet_xmit(sl);
608}
609
610void
611aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
612{
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500613 struct sk_buff *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500615 sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 aoenet_xmit(sl);
618}
619
620/*
621 * Since we only call this in one place (and it only prepares one frame)
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700622 * we just return the skb. Usually we'd chain it up to the aoedev sendq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
624static struct sk_buff *
625aoecmd_ata_id(struct aoedev *d)
626{
627 struct aoe_hdr *h;
628 struct aoe_atahdr *ah;
629 struct frame *f;
630 struct sk_buff *skb;
631
632 f = getframe(d, FREETAG);
633 if (f == NULL) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400634 eprintk("can't get a frame. This shouldn't happen.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return NULL;
636 }
637
638 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400639 skb = f->skb;
640 h = (struct aoe_hdr *) skb->mac.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400642 skb->len = sizeof *h + sizeof *ah;
643 memset(h, 0, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 f->tag = aoehdr_atainit(d, h);
645 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* set up ata header */
648 ah->scnt = 1;
649 ah->cmdstat = WIN_IDENTIFY;
650 ah->lba3 = 0xa0;
651
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400652 skb->dev = d->ifp;
653 skb_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500655 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 return skb;
659}
660
661void
662aoecmd_cfg_rsp(struct sk_buff *skb)
663{
664 struct aoedev *d;
665 struct aoe_hdr *h;
666 struct aoe_cfghdr *ch;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700667 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sk_buff *sl;
Ed L. Cashineaf0a3c2006-01-19 13:46:20 -0500669 enum { MAXFRAMES = 16 };
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400670 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 h = (struct aoe_hdr *) skb->mac.raw;
673 ch = (struct aoe_cfghdr *) (h+1);
674
675 /*
676 * Enough people have their dip switches set backwards to
677 * warrant a loud message for this special case.
678 */
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700679 aoemajor = be16_to_cpu(h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (aoemajor == 0xfff) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400681 eprintk("Warning: shelf address is all ones. "
682 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
684 }
685
686 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700687 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400688 iprintk("e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700689 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691 }
692
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400693 n = be16_to_cpu(ch->bufcnt);
694 if (n > MAXFRAMES) /* keep it reasonable */
695 n = MAXFRAMES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400697 d = aoedev_by_sysminor_m(sysminor, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (d == NULL) {
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400699 iprintk("device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
701 }
702
703 spin_lock_irqsave(&d->lock, flags);
704
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500705 /* permit device to migrate mac and network interface */
706 d->ifp = skb->dev;
707 memcpy(d->addr, h->src, sizeof d->addr);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400708 if (!(d->flags & DEVFL_MAXBCNT)) {
709 n = d->ifp->mtu;
710 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
711 n /= 512;
712 if (n > ch->scnt)
713 n = ch->scnt;
714 d->maxbcnt = n ? n * 512 : DEFAULTBCNT;
715 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500716
717 /* don't change users' perspective */
718 if (d->nopen && !(d->flags & DEVFL_PAUSE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_unlock_irqrestore(&d->lock, flags);
720 return;
721 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500722 d->flags |= DEVFL_PAUSE; /* force pause */
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700723 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500725 /* check for already outstanding ataid */
726 sl = aoedev_isbusy(d) == 0 ? aoecmd_ata_id(d) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 spin_unlock_irqrestore(&d->lock, flags);
729
730 aoenet_xmit(sl);
731}
732