blob: 84c03d65dcc5e5fb930200e091656fe0fb08df41 [file] [log] [blame]
Ed L. Cashin52e112b2008-02-08 04:20:09 -08001/* Copyright (c) 2007 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. Cashin68e0d422008-02-08 04:20:00 -080012#include <linux/moduleparam.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070013#include <net/net_namespace.h>
Ed L. Cashin475172f2005-09-29 12:47:40 -040014#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "aoe.h"
16
Ed L. Cashinb751e8b2006-09-20 14:36:50 -040017static int aoe_deadsecs = 60 * 3;
18module_param(aoe_deadsecs, int, 0644);
19MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ed L. Cashin7df620d2008-02-08 04:20:07 -080021static int aoe_maxout = 16;
22module_param(aoe_maxout, int, 0644);
23MODULE_PARM_DESC(aoe_maxout,
24 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
25
Ed L. Cashin68e0d422008-02-08 04:20:00 -080026static struct sk_buff *
Ed L. Cashine407a7f2006-09-20 14:36:49 -040027new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct sk_buff *skb;
30
31 skb = alloc_skb(len, GFP_ATOMIC);
32 if (skb) {
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070033 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070034 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 skb->protocol = __constant_htons(ETH_P_AOE);
36 skb->priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 skb->next = skb->prev = NULL;
38
39 /* tell the network layer not to perform IP checksums
40 * or to get the NIC to do it
41 */
42 skb->ip_summed = CHECKSUM_NONE;
43 }
44 return skb;
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static struct frame *
Ed L. Cashin68e0d422008-02-08 04:20:00 -080048getframe(struct aoetgt *t, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct frame *f, *e;
51
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 f = t->frames;
53 e = f + t->nframes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 for (; f<e; f++)
55 if (f->tag == tag)
56 return f;
57 return NULL;
58}
59
60/*
61 * Leave the top bit clear so we have tagspace for userland.
62 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
63 * This driver reserves tag -1 to mean "unused frame."
64 */
65static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080066newtag(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 register ulong n;
69
70 n = jiffies & 0xffff;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080071 return n |= (++t->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080075aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080077 u32 host_tag = newtag(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Ed L. Cashin68e0d422008-02-08 04:20:00 -080079 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
80 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070081 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070083 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 h->minor = d->aoeminor;
85 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070086 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 return host_tag;
89}
90
Ed L. Cashin19bf2632006-09-20 14:36:49 -040091static inline void
92put_lba(struct aoe_atahdr *ah, sector_t lba)
93{
94 ah->lba0 = lba;
95 ah->lba1 = lba >>= 8;
96 ah->lba2 = lba >>= 8;
97 ah->lba3 = lba >>= 8;
98 ah->lba4 = lba >>= 8;
99 ah->lba5 = lba >>= 8;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800103ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800105 t->ifp++;
106 if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
107 t->ifp = t->ifs;
108 if (t->ifp->nd == NULL) {
109 printk(KERN_INFO "aoe: no interface to rotate to\n");
110 BUG();
111 }
112}
113
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800114static void
115skb_pool_put(struct aoedev *d, struct sk_buff *skb)
116{
117 if (!d->skbpool_hd)
118 d->skbpool_hd = skb;
119 else
120 d->skbpool_tl->next = skb;
121 d->skbpool_tl = skb;
122}
123
124static struct sk_buff *
125skb_pool_get(struct aoedev *d)
126{
127 struct sk_buff *skb;
128
129 skb = d->skbpool_hd;
130 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
131 d->skbpool_hd = skb->next;
132 skb->next = NULL;
133 return skb;
134 }
135 if (d->nskbpool < NSKBPOOLMAX
136 && (skb = new_skb(ETH_ZLEN))) {
137 d->nskbpool++;
138 return skb;
139 }
140 return NULL;
141}
142
143/* freeframe is where we do our load balancing so it's a little hairy. */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800144static struct frame *
145freeframe(struct aoedev *d)
146{
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800147 struct frame *f, *e, *rf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800148 struct aoetgt **t;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800149 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800150
151 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
152 printk(KERN_ERR "aoe: NULL TARGETS!\n");
153 return NULL;
154 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800155 t = d->tgt;
156 t++;
157 if (t >= &d->targets[NTARGETS] || !*t)
158 t = d->targets;
159 for (;;) {
160 if ((*t)->nout < (*t)->maxout
161 && t != d->htgt
162 && (*t)->ifp->nd) {
163 rf = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800164 f = (*t)->frames;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800165 e = f + (*t)->nframes;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800166 for (; f < e; f++) {
167 if (f->tag != FREETAG)
168 continue;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800169 skb = f->skb;
170 if (!skb
171 && !(f->skb = skb = new_skb(ETH_ZLEN)))
172 continue;
173 if (atomic_read(&skb_shinfo(skb)->dataref)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800174 != 1) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800175 if (!rf)
176 rf = f;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800177 continue;
178 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800179gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
180 skb_trim(skb, 0);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800181 d->tgt = t;
182 ifrotate(*t);
183 return f;
184 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800185 /* Work can be done, but the network layer is
186 holding our precious packets. Try to grab
187 one from the pool. */
188 f = rf;
189 if (f == NULL) { /* more paranoia */
190 printk(KERN_ERR
191 "aoe: freeframe: %s.\n",
192 "unexpected null rf");
193 d->flags |= DEVFL_KICKME;
194 return NULL;
195 }
196 skb = skb_pool_get(d);
197 if (skb) {
198 skb_pool_put(d, f->skb);
199 f->skb = skb;
200 goto gotone;
201 }
202 (*t)->dataref++;
203 if ((*t)->nout == 0)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800204 d->flags |= DEVFL_KICKME;
205 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800206 if (t == d->tgt) /* we've looped and found nada */
207 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800208 t++;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800209 if (t >= &d->targets[NTARGETS] || !*t)
210 t = d->targets;
211 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800212 return NULL;
213}
214
215static int
216aoecmd_ata_rw(struct aoedev *d)
217{
218 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct aoe_hdr *h;
220 struct aoe_atahdr *ah;
221 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800222 struct bio_vec *bv;
223 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct sk_buff *skb;
225 ulong bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 char writebit, extbit;
227
228 writebit = 0x10;
229 extbit = 0x4;
230
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800231 f = freeframe(d);
232 if (f == NULL)
233 return 0;
234 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 buf = d->inprocess;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800236 bv = buf->bv;
237 bcnt = t->ifp->maxbcnt;
238 if (bcnt == 0)
239 bcnt = DEFAULTBCNT;
240 if (bcnt > buf->bv_resid)
241 bcnt = buf->bv_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400243 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700244 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800246 skb_put(skb, sizeof *h + sizeof *ah);
247 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800248 f->tag = aoehdr_atainit(d, t, h);
249 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 f->waited = 0;
251 f->buf = buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800252 f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400253 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800254 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* set up ata header */
257 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800258 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (d->flags & DEVFL_EXT) {
260 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
262 extbit = 0;
263 ah->lba3 &= 0x0f;
264 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800267 skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400269 skb->len += bcnt;
270 skb->data_len = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800271 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800273 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 ah->cmdstat = WIN_READ | writebit | extbit;
278
279 /* mark all tracking fields and load out */
280 buf->nframesout += 1;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800281 buf->bv_off += bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 buf->bv_resid -= bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 buf->resid -= bcnt;
284 buf->sector += bcnt >> 9;
285 if (buf->resid == 0) {
286 d->inprocess = NULL;
287 } else if (buf->bv_resid == 0) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800288 buf->bv = ++bv;
289 buf->bv_resid = bv->bv_len;
290 WARN_ON(buf->bv_resid == 0);
291 buf->bv_off = bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800294 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400295 skb = skb_clone(skb, GFP_ATOMIC);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800296 if (skb) {
297 if (d->sendq_hd)
298 d->sendq_tl->next = skb;
299 else
300 d->sendq_hd = skb;
301 d->sendq_tl = skb;
302 }
303 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500306/* some callers cannot sleep, and they can call this function,
307 * transmitting the packets later, when interrupts are on
308 */
309static struct sk_buff *
310aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
311{
312 struct aoe_hdr *h;
313 struct aoe_cfghdr *ch;
314 struct sk_buff *skb, *sl, *sl_tail;
315 struct net_device *ifp;
316
317 sl = sl_tail = NULL;
318
319 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700320 for_each_netdev(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500321 dev_hold(ifp);
322 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700323 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500324
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400325 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500326 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400327 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700328 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500329 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800330 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400331 skb->dev = ifp;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500332 if (sl_tail == NULL)
333 sl_tail = skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700334 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500335 memset(h, 0, sizeof *h + sizeof *ch);
336
337 memset(h->dst, 0xff, sizeof h->dst);
338 memcpy(h->src, ifp->dev_addr, sizeof h->src);
339 h->type = __constant_cpu_to_be16(ETH_P_AOE);
340 h->verfl = AOE_HVER;
341 h->major = cpu_to_be16(aoemajor);
342 h->minor = aoeminor;
343 h->cmd = AOECMD_CFG;
344
345 skb->next = sl;
346 sl = skb;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700347cont:
348 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500349 }
350 read_unlock(&dev_base_lock);
351
352 if (tail != NULL)
353 *tail = sl_tail;
354 return sl;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800358resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct sk_buff *skb;
361 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400362 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 char buf[128];
364 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800366 ifrotate(t);
367 n = newtag(t);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400368 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700369 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400370 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800371
372 snprintf(buf, sizeof buf,
373 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x "
374 "s=%012llx d=%012llx nout=%d\n",
375 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Ed L. Cashin1eb0da42008-02-08 04:20:01 -0800376 mac_addr(h->src),
377 mac_addr(h->dst), t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800378 aoechr_error(buf);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700381 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800382 memcpy(h->dst, t->addr, sizeof h->dst);
383 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800385 switch (ah->cmdstat) {
386 default:
387 break;
388 case WIN_READ:
389 case WIN_READ_EXT:
390 case WIN_WRITE:
391 case WIN_WRITE_EXT:
392 put_lba(ah, f->lba);
393
394 n = f->bcnt;
395 if (n > DEFAULTBCNT)
396 n = DEFAULTBCNT;
397 ah->scnt = n >> 9;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400398 if (ah->aflags & AOEAFL_WRITE) {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400399 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800400 offset_in_page(f->bufaddr), n);
401 skb->len = sizeof *h + sizeof *ah + n;
402 skb->data_len = n;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400403 }
404 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800405 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400406 skb = skb_clone(skb, GFP_ATOMIC);
407 if (skb == NULL)
408 return;
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400409 if (d->sendq_hd)
410 d->sendq_tl->next = skb;
411 else
412 d->sendq_hd = skb;
413 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416static int
417tsince(int tag)
418{
419 int n;
420
421 n = jiffies & 0xffff;
422 n -= tag & 0xffff;
423 if (n < 0)
424 n += 1<<16;
425 return n;
426}
427
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800428static struct aoeif *
429getif(struct aoetgt *t, struct net_device *nd)
430{
431 struct aoeif *p, *e;
432
433 p = t->ifs;
434 e = p + NAOEIFS;
435 for (; p < e; p++)
436 if (p->nd == nd)
437 return p;
438 return NULL;
439}
440
441static struct aoeif *
442addif(struct aoetgt *t, struct net_device *nd)
443{
444 struct aoeif *p;
445
446 p = getif(t, NULL);
447 if (!p)
448 return NULL;
449 p->nd = nd;
450 p->maxbcnt = DEFAULTBCNT;
451 p->lost = 0;
452 p->lostjumbo = 0;
453 return p;
454}
455
456static void
457ejectif(struct aoetgt *t, struct aoeif *ifp)
458{
459 struct aoeif *e;
460 ulong n;
461
462 e = t->ifs + NAOEIFS - 1;
463 n = (e - ifp) * sizeof *ifp;
464 memmove(ifp, ifp+1, n);
465 e->nd = NULL;
466}
467
468static int
469sthtith(struct aoedev *d)
470{
471 struct frame *f, *e, *nf;
472 struct sk_buff *skb;
473 struct aoetgt *ht = *d->htgt;
474
475 f = ht->frames;
476 e = f + ht->nframes;
477 for (; f < e; f++) {
478 if (f->tag == FREETAG)
479 continue;
480 nf = freeframe(d);
481 if (!nf)
482 return 0;
483 skb = nf->skb;
484 *nf = *f;
485 f->skb = skb;
486 f->tag = FREETAG;
487 nf->waited = 0;
488 ht->nout--;
489 (*d->tgt)->nout++;
490 resend(d, *d->tgt, nf);
491 }
492 /* he's clean, he's useless. take away his interfaces */
493 memset(ht->ifs, 0, sizeof ht->ifs);
494 d->htgt = NULL;
495 return 1;
496}
497
498static inline unsigned char
499ata_scnt(unsigned char *packet) {
500 struct aoe_hdr *h;
501 struct aoe_atahdr *ah;
502
503 h = (struct aoe_hdr *) packet;
504 ah = (struct aoe_atahdr *) (h+1);
505 return ah->scnt;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508static void
509rexmit_timer(ulong vp)
510{
511 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800512 struct aoetgt *t, **tt, **te;
513 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct frame *f, *e;
515 struct sk_buff *sl;
516 register long timeout;
517 ulong flags, n;
518
519 d = (struct aoedev *) vp;
520 sl = NULL;
521
522 /* timeout is always ~150% of the moving average */
523 timeout = d->rttavg;
524 timeout += timeout >> 1;
525
526 spin_lock_irqsave(&d->lock, flags);
527
528 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500529 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
531 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800532 tt = d->targets;
533 te = tt + NTARGETS;
534 for (; tt < te && *tt; tt++) {
535 t = *tt;
536 f = t->frames;
537 e = f + t->nframes;
538 for (; f < e; f++) {
539 if (f->tag == FREETAG
540 || tsince(f->tag) < timeout)
541 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 n = f->waited += timeout;
543 n /= HZ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800544 if (n > aoe_deadsecs) {
545 /* waited too long. device failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800549
550 if (n > HELPWAIT /* see if another target can help */
551 && (tt != d->targets || d->targets[1]))
552 d->htgt = tt;
553
554 if (t->nout == t->maxout) {
555 if (t->maxout > 1)
556 t->maxout--;
557 t->lastwadj = jiffies;
558 }
559
560 ifp = getif(t, f->skb->dev);
561 if (ifp && ++ifp->lost > (t->nframes << 1)
562 && (ifp != t->ifs || t->ifs[1].nd)) {
563 ejectif(t, ifp);
564 ifp = NULL;
565 }
566
567 if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
568 && ifp && ++ifp->lostjumbo > (t->nframes << 1)
569 && ifp->maxbcnt != DEFAULTBCNT) {
570 printk(KERN_INFO
571 "aoe: e%ld.%d: "
572 "too many lost jumbo on "
573 "%s:%012llx - "
574 "falling back to %d frames.\n",
575 d->aoemajor, d->aoeminor,
576 ifp->nd->name, mac_addr(t->addr),
577 DEFAULTBCNT);
578 ifp->maxbcnt = 0;
579 }
580 resend(d, t, f);
581 }
582
583 /* window check */
584 if (t->nout == t->maxout
585 && t->maxout < t->nframes
586 && (jiffies - t->lastwadj)/HZ > 10) {
587 t->maxout++;
588 t->lastwadj = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800591
592 if (d->sendq_hd) {
593 n = d->rttavg <<= 1;
594 if (n > MAXTIMER)
595 d->rttavg = MAXTIMER;
596 }
597
598 if (d->flags & DEVFL_KICKME || d->htgt) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400599 d->flags &= ~DEVFL_KICKME;
600 aoecmd_work(d);
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
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 d->timer.expires = jiffies + TIMERTICK;
607 add_timer(&d->timer);
608
609 spin_unlock_irqrestore(&d->lock, flags);
610
611 aoenet_xmit(sl);
612}
613
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800614/* enters with d->lock held */
615void
616aoecmd_work(struct aoedev *d)
617{
618 struct buf *buf;
619loop:
620 if (d->htgt && !sthtith(d))
621 return;
622 if (d->inprocess == NULL) {
623 if (list_empty(&d->bufq))
624 return;
625 buf = container_of(d->bufq.next, struct buf, bufs);
626 list_del(d->bufq.next);
627 d->inprocess = buf;
628 }
629 if (aoecmd_ata_rw(d))
630 goto loop;
631}
632
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500633/* this function performs work that has been deferred until sleeping is OK
634 */
635void
David Howellsc4028952006-11-22 14:57:56 +0000636aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500637{
David Howellsc4028952006-11-22 14:57:56 +0000638 struct aoedev *d = container_of(work, struct aoedev, work);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500639
640 if (d->flags & DEVFL_GDALLOC)
641 aoeblk_gdalloc(d);
642
643 if (d->flags & DEVFL_NEWSIZE) {
644 struct block_device *bd;
645 unsigned long flags;
646 u64 ssize;
647
648 ssize = d->gd->capacity;
649 bd = bdget_disk(d->gd, 0);
650
651 if (bd) {
652 mutex_lock(&bd->bd_inode->i_mutex);
653 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
654 mutex_unlock(&bd->bd_inode->i_mutex);
655 bdput(bd);
656 }
657 spin_lock_irqsave(&d->lock, flags);
658 d->flags |= DEVFL_UP;
659 d->flags &= ~DEVFL_NEWSIZE;
660 spin_unlock_irqrestore(&d->lock, flags);
661 }
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800665ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 u64 ssize;
668 u16 n;
669
670 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700671 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700674 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (n & (1<<10)) { /* bit 10: LBA 48 */
677 d->flags |= DEVFL_EXT;
678
679 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700680 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* set as in ide-disk.c:init_idedisk_capacity */
683 d->geo.cylinders = ssize;
684 d->geo.cylinders /= (255 * 63);
685 d->geo.heads = 255;
686 d->geo.sectors = 63;
687 } else {
688 d->flags &= ~DEVFL_EXT;
689
690 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700691 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700694 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
695 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
696 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500698
699 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800700 printk(KERN_INFO
701 "aoe: %012llx e%ld.%d v%04x has %llu sectors\n",
Ed L. Cashin1eb0da42008-02-08 04:20:01 -0800702 mac_addr(t->addr),
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500703 d->aoemajor, d->aoeminor,
704 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 d->ssize = ssize;
706 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800707 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (d->gd != NULL) {
710 d->gd->capacity = ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500711 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800712 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500713 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static void
718calc_rttavg(struct aoedev *d, int rtt)
719{
720 register long n;
721
722 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400723 if (n < 0) {
724 n = -rtt;
725 if (n < MINTIMER)
726 n = MINTIMER;
727 else if (n > MAXTIMER)
728 n = MAXTIMER;
729 d->mintimer += (n - d->mintimer) >> 1;
730 } else if (n < d->mintimer)
731 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 else if (n > MAXTIMER)
733 n = MAXTIMER;
734
735 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
736 n -= d->rttavg;
737 d->rttavg += n >> 2;
738}
739
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800740static struct aoetgt *
741gettgt(struct aoedev *d, char *addr)
742{
743 struct aoetgt **t, **e;
744
745 t = d->targets;
746 e = t + NTARGETS;
747 for (; t < e && *t; t++)
748 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
749 return *t;
750 return NULL;
751}
752
753static inline void
Linus Torvalds03054de2008-02-08 09:42:46 -0800754diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800755{
756 unsigned long n_sect = bio->bi_size >> 9;
757 const int rw = bio_data_dir(bio);
Jens Axboe28f13702008-05-07 10:15:46 +0200758 struct hd_struct *part;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800759
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200760 rcu_read_lock();
761
762 part = disk_map_sector_rcu(disk, sector);
Jens Axboe28f13702008-05-07 10:15:46 +0200763 all_stat_inc(disk, part, ios[rw], sector);
764 all_stat_add(disk, part, ticks[rw], duration, sector);
765 all_stat_add(disk, part, sectors[rw], n_sect, sector);
766 all_stat_add(disk, part, io_ticks, duration, sector);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200767
768 rcu_read_unlock();
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771void
772aoecmd_ata_rsp(struct sk_buff *skb)
773{
774 struct aoedev *d;
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400775 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct aoe_atahdr *ahin, *ahout;
777 struct frame *f;
778 struct buf *buf;
779 struct sk_buff *sl;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800780 struct aoetgt *t;
781 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 register long n;
783 ulong flags;
784 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700785 u16 aoemajor;
786
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700787 hin = (struct aoe_hdr *) skb_mac_header(skb);
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700788 aoemajor = get_unaligned_be16(&hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700789 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (d == NULL) {
791 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
792 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700793 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 aoechr_error(ebuf);
795 return;
796 }
797
798 spin_lock_irqsave(&d->lock, flags);
799
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700800 n = get_unaligned_be32(&hin->tag);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800801 t = gettgt(d, hin->src);
802 if (t == NULL) {
803 printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n",
Ed L. Cashin1eb0da42008-02-08 04:20:01 -0800804 d->aoemajor, d->aoeminor, mac_addr(hin->src));
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800805 spin_unlock_irqrestore(&d->lock, flags);
806 return;
807 }
808 f = getframe(t, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -0400810 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 spin_unlock_irqrestore(&d->lock, flags);
812 snprintf(ebuf, sizeof ebuf,
813 "%15s e%d.%d tag=%08x@%08lx\n",
814 "unexpected rsp",
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700815 get_unaligned_be16(&hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 hin->minor,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700817 get_unaligned_be32(&hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 jiffies);
819 aoechr_error(ebuf);
820 return;
821 }
822
823 calc_rttavg(d, tsince(f->tag));
824
825 ahin = (struct aoe_atahdr *) (hin+1);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700826 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400827 ahout = (struct aoe_atahdr *) (hout+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 buf = f->buf;
829
830 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400831 printk(KERN_ERR
Ed L. Cashin1d759812008-02-08 04:20:08 -0800832 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ahout->cmdstat, ahin->cmdstat,
834 d->aoemajor, d->aoeminor);
835 if (buf)
836 buf->flags |= BUFFL_FAIL;
837 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800838 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
839 d->htgt = NULL;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400840 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 switch (ahout->cmdstat) {
842 case WIN_READ:
843 case WIN_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400845 printk(KERN_ERR
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800846 "aoe: %s. skb->len=%d need=%ld\n",
847 "runt data size in read", skb->len, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* fail frame f? just returning will rexmit. */
849 spin_unlock_irqrestore(&d->lock, flags);
850 return;
851 }
852 memcpy(f->bufaddr, ahin+1, n);
853 case WIN_WRITE:
854 case WIN_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800855 ifp = getif(t, skb->dev);
856 if (ifp) {
857 ifp->lost = 0;
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400858 if (n > DEFAULTBCNT)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800859 ifp->lostjumbo = 0;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400860 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800861 if (f->bcnt -= n) {
862 f->lba += n >> 9;
863 f->bufaddr += n;
864 resend(d, t, f);
865 goto xmit;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
868 case WIN_IDENTIFY:
869 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400870 printk(KERN_INFO
871 "aoe: runt data size in ataid. skb->len=%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400872 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 spin_unlock_irqrestore(&d->lock, flags);
874 return;
875 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800876 ataid_complete(d, t, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400879 printk(KERN_INFO
880 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400881 ahout->cmdstat,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700882 get_unaligned_be16(&hin->major),
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400883 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800887 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
Linus Torvalds03054de2008-02-08 09:42:46 -0800888 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800889 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
890 bio_endio(buf->bio, n);
891 mempool_free(buf, d->bufpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 f->buf = NULL;
895 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800896 t->nout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 aoecmd_work(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800899xmit:
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700900 sl = d->sendq_hd;
901 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 aoenet_xmit(sl);
905}
906
907void
908aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
909{
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500910 struct sk_buff *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500912 sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 aoenet_xmit(sl);
915}
916
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800917struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918aoecmd_ata_id(struct aoedev *d)
919{
920 struct aoe_hdr *h;
921 struct aoe_atahdr *ah;
922 struct frame *f;
923 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800924 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400926 f = freeframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800927 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800929
930 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400933 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700934 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800936 skb_put(skb, sizeof *h + sizeof *ah);
937 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800938 f->tag = aoehdr_atainit(d, t, h);
939 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* set up ata header */
943 ah->scnt = 1;
944 ah->cmdstat = WIN_IDENTIFY;
945 ah->lba3 = 0xa0;
946
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800947 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500949 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400952 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800955static struct aoetgt *
956addtgt(struct aoedev *d, char *addr, ulong nframes)
957{
958 struct aoetgt *t, **tt, **te;
959 struct frame *f, *e;
960
961 tt = d->targets;
962 te = tt + NTARGETS;
963 for (; tt < te && *tt; tt++)
964 ;
965
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800966 if (tt == te) {
967 printk(KERN_INFO
968 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800969 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800970 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800971 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
972 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800973 if (!t || !f) {
974 kfree(f);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800975 kfree(t);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800976 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800977 return NULL;
978 }
979
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800980 t->nframes = nframes;
981 t->frames = f;
982 e = f + nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800983 for (; f < e; f++)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800984 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800985 memcpy(t->addr, addr, sizeof t->addr);
986 t->ifp = t->ifs;
987 t->maxout = t->nframes;
988 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991void
992aoecmd_cfg_rsp(struct sk_buff *skb)
993{
994 struct aoedev *d;
995 struct aoe_hdr *h;
996 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800997 struct aoetgt *t;
998 struct aoeif *ifp;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700999 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct sk_buff *sl;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001001 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001003 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ch = (struct aoe_cfghdr *) (h+1);
1005
1006 /*
1007 * Enough people have their dip switches set backwards to
1008 * warrant a loud message for this special case.
1009 */
Harvey Harrison823ed722008-07-04 09:28:32 +02001010 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001012 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -04001013 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return;
1015 }
1016
1017 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -07001018 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001019 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -07001020 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return;
1022 }
1023
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001024 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001025 if (n > aoe_maxout) /* keep it reasonable */
1026 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001028 d = aoedev_by_sysminor_m(sysminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (d == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001030 printk(KERN_INFO "aoe: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032 }
1033
1034 spin_lock_irqsave(&d->lock, flags);
1035
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001036 t = gettgt(d, h->src);
1037 if (!t) {
1038 t = addtgt(d, h->src, n);
1039 if (!t) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001040 spin_unlock_irqrestore(&d->lock, flags);
1041 return;
1042 }
1043 }
1044 ifp = getif(t, skb->dev);
1045 if (!ifp) {
1046 ifp = addif(t, skb->dev);
1047 if (!ifp) {
1048 printk(KERN_INFO
1049 "aoe: device addif failure; "
1050 "too many interfaces?\n");
1051 spin_unlock_irqrestore(&d->lock, flags);
1052 return;
1053 }
1054 }
1055 if (ifp->maxbcnt) {
1056 n = ifp->nd->mtu;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001057 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1058 n /= 512;
1059 if (n > ch->scnt)
1060 n = ch->scnt;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001061 n = n ? n * 512 : DEFAULTBCNT;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001062 if (n != ifp->maxbcnt) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001063 printk(KERN_INFO
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001064 "aoe: e%ld.%d: setting %d%s%s:%012llx\n",
1065 d->aoemajor, d->aoeminor, n,
1066 " byte data frames on ", ifp->nd->name,
Ed L. Cashin1eb0da42008-02-08 04:20:01 -08001067 mac_addr(t->addr));
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001068 ifp->maxbcnt = n;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001069 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001070 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001071
1072 /* don't change users' perspective */
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001073 if (d->nopen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_unlock_irqrestore(&d->lock, flags);
1075 return;
1076 }
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -07001077 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001079 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 spin_unlock_irqrestore(&d->lock, flags);
1082
1083 aoenet_xmit(sl);
1084}
1085
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001086void
1087aoecmd_cleanslate(struct aoedev *d)
1088{
1089 struct aoetgt **t, **te;
1090 struct aoeif *p, *e;
1091
1092 d->mintimer = MINTIMER;
1093
1094 t = d->targets;
1095 te = t + NTARGETS;
1096 for (; t < te && *t; t++) {
1097 (*t)->maxout = (*t)->nframes;
1098 p = (*t)->ifs;
1099 e = p + NAOEIFS;
1100 for (; p < e; p++) {
1101 p->lostjumbo = 0;
1102 p->lost = 0;
1103 p->maxbcnt = DEFAULTBCNT;
1104 }
1105 }
1106}