blob: 45c5a33daf498d623e9749bd75e8f4bf94a9e49e [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{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700117 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800118}
119
120static struct sk_buff *
121skb_pool_get(struct aoedev *d)
122{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700123 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800124
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800125 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb2008-09-21 22:36:49 -0700126 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800127 return skb;
128 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700129 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
130 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800131 return skb;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700132
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800133 return NULL;
134}
135
136/* freeframe is where we do our load balancing so it's a little hairy. */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800137static struct frame *
138freeframe(struct aoedev *d)
139{
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800140 struct frame *f, *e, *rf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800141 struct aoetgt **t;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800142 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800143
144 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
145 printk(KERN_ERR "aoe: NULL TARGETS!\n");
146 return NULL;
147 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800148 t = d->tgt;
149 t++;
150 if (t >= &d->targets[NTARGETS] || !*t)
151 t = d->targets;
152 for (;;) {
153 if ((*t)->nout < (*t)->maxout
154 && t != d->htgt
155 && (*t)->ifp->nd) {
156 rf = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800157 f = (*t)->frames;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800158 e = f + (*t)->nframes;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800159 for (; f < e; f++) {
160 if (f->tag != FREETAG)
161 continue;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800162 skb = f->skb;
163 if (!skb
164 && !(f->skb = skb = new_skb(ETH_ZLEN)))
165 continue;
166 if (atomic_read(&skb_shinfo(skb)->dataref)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800167 != 1) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800168 if (!rf)
169 rf = f;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800170 continue;
171 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800172gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
173 skb_trim(skb, 0);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800174 d->tgt = t;
175 ifrotate(*t);
176 return f;
177 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800178 /* Work can be done, but the network layer is
179 holding our precious packets. Try to grab
180 one from the pool. */
181 f = rf;
182 if (f == NULL) { /* more paranoia */
183 printk(KERN_ERR
184 "aoe: freeframe: %s.\n",
185 "unexpected null rf");
186 d->flags |= DEVFL_KICKME;
187 return NULL;
188 }
189 skb = skb_pool_get(d);
190 if (skb) {
191 skb_pool_put(d, f->skb);
192 f->skb = skb;
193 goto gotone;
194 }
195 (*t)->dataref++;
196 if ((*t)->nout == 0)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800197 d->flags |= DEVFL_KICKME;
198 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800199 if (t == d->tgt) /* we've looped and found nada */
200 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800201 t++;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800202 if (t >= &d->targets[NTARGETS] || !*t)
203 t = d->targets;
204 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800205 return NULL;
206}
207
208static int
209aoecmd_ata_rw(struct aoedev *d)
210{
211 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct aoe_hdr *h;
213 struct aoe_atahdr *ah;
214 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800215 struct bio_vec *bv;
216 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct sk_buff *skb;
218 ulong bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 char writebit, extbit;
220
221 writebit = 0x10;
222 extbit = 0x4;
223
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800224 f = freeframe(d);
225 if (f == NULL)
226 return 0;
227 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 buf = d->inprocess;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800229 bv = buf->bv;
230 bcnt = t->ifp->maxbcnt;
231 if (bcnt == 0)
232 bcnt = DEFAULTBCNT;
233 if (bcnt > buf->bv_resid)
234 bcnt = buf->bv_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400236 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700237 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800239 skb_put(skb, sizeof *h + sizeof *ah);
240 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800241 f->tag = aoehdr_atainit(d, t, h);
242 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 f->waited = 0;
244 f->buf = buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800245 f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400246 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800247 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* set up ata header */
250 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800251 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (d->flags & DEVFL_EXT) {
253 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else {
255 extbit = 0;
256 ah->lba3 &= 0x0f;
257 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800260 skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400262 skb->len += bcnt;
263 skb->data_len = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800264 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800266 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 ah->cmdstat = WIN_READ | writebit | extbit;
271
272 /* mark all tracking fields and load out */
273 buf->nframesout += 1;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800274 buf->bv_off += bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 buf->bv_resid -= bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 buf->resid -= bcnt;
277 buf->sector += bcnt >> 9;
278 if (buf->resid == 0) {
279 d->inprocess = NULL;
280 } else if (buf->bv_resid == 0) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800281 buf->bv = ++bv;
282 buf->bv_resid = bv->bv_len;
283 WARN_ON(buf->bv_resid == 0);
284 buf->bv_off = bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800287 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400288 skb = skb_clone(skb, GFP_ATOMIC);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700289 if (skb)
290 __skb_queue_tail(&d->sendq, skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800291 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500294/* some callers cannot sleep, and they can call this function,
295 * transmitting the packets later, when interrupts are on
296 */
David S. Millere9bb8fb2008-09-21 22:36:49 -0700297static void
298aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500299{
300 struct aoe_hdr *h;
301 struct aoe_cfghdr *ch;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700302 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500303 struct net_device *ifp;
304
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500305 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700306 for_each_netdev(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500307 dev_hold(ifp);
308 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700309 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500310
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400311 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500312 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400313 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700314 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500315 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800316 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400317 skb->dev = ifp;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700318 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700319 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500320 memset(h, 0, sizeof *h + sizeof *ch);
321
322 memset(h->dst, 0xff, sizeof h->dst);
323 memcpy(h->src, ifp->dev_addr, sizeof h->src);
324 h->type = __constant_cpu_to_be16(ETH_P_AOE);
325 h->verfl = AOE_HVER;
326 h->major = cpu_to_be16(aoemajor);
327 h->minor = aoeminor;
328 h->cmd = AOECMD_CFG;
329
Pavel Emelianov7562f872007-05-03 15:13:45 -0700330cont:
331 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500332 }
333 read_unlock(&dev_base_lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800337resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct sk_buff *skb;
340 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400341 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 char buf[128];
343 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800345 ifrotate(t);
346 n = newtag(t);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400347 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700348 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400349 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800350
351 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800352 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800353 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800354 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800355 aoechr_error(buf);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700358 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800359 memcpy(h->dst, t->addr, sizeof h->dst);
360 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800362 switch (ah->cmdstat) {
363 default:
364 break;
365 case WIN_READ:
366 case WIN_READ_EXT:
367 case WIN_WRITE:
368 case WIN_WRITE_EXT:
369 put_lba(ah, f->lba);
370
371 n = f->bcnt;
372 if (n > DEFAULTBCNT)
373 n = DEFAULTBCNT;
374 ah->scnt = n >> 9;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400375 if (ah->aflags & AOEAFL_WRITE) {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400376 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800377 offset_in_page(f->bufaddr), n);
378 skb->len = sizeof *h + sizeof *ah + n;
379 skb->data_len = n;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400380 }
381 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800382 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400383 skb = skb_clone(skb, GFP_ATOMIC);
384 if (skb == NULL)
385 return;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700386 __skb_queue_tail(&d->sendq, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static int
390tsince(int tag)
391{
392 int n;
393
394 n = jiffies & 0xffff;
395 n -= tag & 0xffff;
396 if (n < 0)
397 n += 1<<16;
398 return n;
399}
400
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800401static struct aoeif *
402getif(struct aoetgt *t, struct net_device *nd)
403{
404 struct aoeif *p, *e;
405
406 p = t->ifs;
407 e = p + NAOEIFS;
408 for (; p < e; p++)
409 if (p->nd == nd)
410 return p;
411 return NULL;
412}
413
414static struct aoeif *
415addif(struct aoetgt *t, struct net_device *nd)
416{
417 struct aoeif *p;
418
419 p = getif(t, NULL);
420 if (!p)
421 return NULL;
422 p->nd = nd;
423 p->maxbcnt = DEFAULTBCNT;
424 p->lost = 0;
425 p->lostjumbo = 0;
426 return p;
427}
428
429static void
430ejectif(struct aoetgt *t, struct aoeif *ifp)
431{
432 struct aoeif *e;
433 ulong n;
434
435 e = t->ifs + NAOEIFS - 1;
436 n = (e - ifp) * sizeof *ifp;
437 memmove(ifp, ifp+1, n);
438 e->nd = NULL;
439}
440
441static int
442sthtith(struct aoedev *d)
443{
444 struct frame *f, *e, *nf;
445 struct sk_buff *skb;
446 struct aoetgt *ht = *d->htgt;
447
448 f = ht->frames;
449 e = f + ht->nframes;
450 for (; f < e; f++) {
451 if (f->tag == FREETAG)
452 continue;
453 nf = freeframe(d);
454 if (!nf)
455 return 0;
456 skb = nf->skb;
457 *nf = *f;
458 f->skb = skb;
459 f->tag = FREETAG;
460 nf->waited = 0;
461 ht->nout--;
462 (*d->tgt)->nout++;
463 resend(d, *d->tgt, nf);
464 }
465 /* he's clean, he's useless. take away his interfaces */
466 memset(ht->ifs, 0, sizeof ht->ifs);
467 d->htgt = NULL;
468 return 1;
469}
470
471static inline unsigned char
472ata_scnt(unsigned char *packet) {
473 struct aoe_hdr *h;
474 struct aoe_atahdr *ah;
475
476 h = (struct aoe_hdr *) packet;
477 ah = (struct aoe_atahdr *) (h+1);
478 return ah->scnt;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static void
482rexmit_timer(ulong vp)
483{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700484 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800486 struct aoetgt *t, **tt, **te;
487 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct frame *f, *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 register long timeout;
490 ulong flags, n;
491
492 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* timeout is always ~150% of the moving average */
495 timeout = d->rttavg;
496 timeout += timeout >> 1;
497
498 spin_lock_irqsave(&d->lock, flags);
499
500 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500501 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return;
503 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800504 tt = d->targets;
505 te = tt + NTARGETS;
506 for (; tt < te && *tt; tt++) {
507 t = *tt;
508 f = t->frames;
509 e = f + t->nframes;
510 for (; f < e; f++) {
511 if (f->tag == FREETAG
512 || tsince(f->tag) < timeout)
513 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 n = f->waited += timeout;
515 n /= HZ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800516 if (n > aoe_deadsecs) {
517 /* waited too long. device failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800521
522 if (n > HELPWAIT /* see if another target can help */
523 && (tt != d->targets || d->targets[1]))
524 d->htgt = tt;
525
526 if (t->nout == t->maxout) {
527 if (t->maxout > 1)
528 t->maxout--;
529 t->lastwadj = jiffies;
530 }
531
532 ifp = getif(t, f->skb->dev);
533 if (ifp && ++ifp->lost > (t->nframes << 1)
534 && (ifp != t->ifs || t->ifs[1].nd)) {
535 ejectif(t, ifp);
536 ifp = NULL;
537 }
538
539 if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
540 && ifp && ++ifp->lostjumbo > (t->nframes << 1)
541 && ifp->maxbcnt != DEFAULTBCNT) {
542 printk(KERN_INFO
543 "aoe: e%ld.%d: "
544 "too many lost jumbo on "
Harvey Harrison411c41e2008-11-25 00:40:37 -0800545 "%s:%pm - "
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800546 "falling back to %d frames.\n",
547 d->aoemajor, d->aoeminor,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800548 ifp->nd->name, t->addr,
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800549 DEFAULTBCNT);
550 ifp->maxbcnt = 0;
551 }
552 resend(d, t, f);
553 }
554
555 /* window check */
556 if (t->nout == t->maxout
557 && t->maxout < t->nframes
558 && (jiffies - t->lastwadj)/HZ > 10) {
559 t->maxout++;
560 t->lastwadj = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800563
David S. Millere9bb8fb2008-09-21 22:36:49 -0700564 if (!skb_queue_empty(&d->sendq)) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800565 n = d->rttavg <<= 1;
566 if (n > MAXTIMER)
567 d->rttavg = MAXTIMER;
568 }
569
570 if (d->flags & DEVFL_KICKME || d->htgt) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400571 d->flags &= ~DEVFL_KICKME;
572 aoecmd_work(d);
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David S. Millere9bb8fb2008-09-21 22:36:49 -0700575 __skb_queue_head_init(&queue);
576 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 d->timer.expires = jiffies + TIMERTICK;
579 add_timer(&d->timer);
580
581 spin_unlock_irqrestore(&d->lock, flags);
582
David S. Millere9bb8fb2008-09-21 22:36:49 -0700583 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800586/* enters with d->lock held */
587void
588aoecmd_work(struct aoedev *d)
589{
590 struct buf *buf;
591loop:
592 if (d->htgt && !sthtith(d))
593 return;
594 if (d->inprocess == NULL) {
595 if (list_empty(&d->bufq))
596 return;
597 buf = container_of(d->bufq.next, struct buf, bufs);
598 list_del(d->bufq.next);
599 d->inprocess = buf;
600 }
601 if (aoecmd_ata_rw(d))
602 goto loop;
603}
604
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500605/* this function performs work that has been deferred until sleeping is OK
606 */
607void
David Howellsc4028952006-11-22 14:57:56 +0000608aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500609{
David Howellsc4028952006-11-22 14:57:56 +0000610 struct aoedev *d = container_of(work, struct aoedev, work);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500611
612 if (d->flags & DEVFL_GDALLOC)
613 aoeblk_gdalloc(d);
614
615 if (d->flags & DEVFL_NEWSIZE) {
616 struct block_device *bd;
617 unsigned long flags;
618 u64 ssize;
619
Tejun Heo80795ae2008-08-25 19:56:07 +0900620 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500621 bd = bdget_disk(d->gd, 0);
622
623 if (bd) {
624 mutex_lock(&bd->bd_inode->i_mutex);
625 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
626 mutex_unlock(&bd->bd_inode->i_mutex);
627 bdput(bd);
628 }
629 spin_lock_irqsave(&d->lock, flags);
630 d->flags |= DEVFL_UP;
631 d->flags &= ~DEVFL_NEWSIZE;
632 spin_unlock_irqrestore(&d->lock, flags);
633 }
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800637ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 u64 ssize;
640 u16 n;
641
642 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700643 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700646 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (n & (1<<10)) { /* bit 10: LBA 48 */
649 d->flags |= DEVFL_EXT;
650
651 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700652 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* set as in ide-disk.c:init_idedisk_capacity */
655 d->geo.cylinders = ssize;
656 d->geo.cylinders /= (255 * 63);
657 d->geo.heads = 255;
658 d->geo.sectors = 63;
659 } else {
660 d->flags &= ~DEVFL_EXT;
661
662 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700663 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700666 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
667 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
668 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500670
671 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800672 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800673 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
674 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500675 d->aoemajor, d->aoeminor,
676 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 d->ssize = ssize;
678 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800679 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
680 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900682 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500683 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800684 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500685 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void
690calc_rttavg(struct aoedev *d, int rtt)
691{
692 register long n;
693
694 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400695 if (n < 0) {
696 n = -rtt;
697 if (n < MINTIMER)
698 n = MINTIMER;
699 else if (n > MAXTIMER)
700 n = MAXTIMER;
701 d->mintimer += (n - d->mintimer) >> 1;
702 } else if (n < d->mintimer)
703 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else if (n > MAXTIMER)
705 n = MAXTIMER;
706
707 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
708 n -= d->rttavg;
709 d->rttavg += n >> 2;
710}
711
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800712static struct aoetgt *
713gettgt(struct aoedev *d, char *addr)
714{
715 struct aoetgt **t, **e;
716
717 t = d->targets;
718 e = t + NTARGETS;
719 for (; t < e && *t; t++)
720 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
721 return *t;
722 return NULL;
723}
724
725static inline void
Linus Torvalds03054de2008-02-08 09:42:46 -0800726diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800727{
728 unsigned long n_sect = bio->bi_size >> 9;
729 const int rw = bio_data_dir(bio);
Jens Axboe28f13702008-05-07 10:15:46 +0200730 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +0900731 int cpu;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800732
Tejun Heo074a7ac2008-08-25 19:56:14 +0900733 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200734 part = disk_map_sector_rcu(disk, sector);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200735
Tejun Heo074a7ac2008-08-25 19:56:14 +0900736 part_stat_inc(cpu, part, ios[rw]);
737 part_stat_add(cpu, part, ticks[rw], duration);
738 part_stat_add(cpu, part, sectors[rw], n_sect);
739 part_stat_add(cpu, part, io_ticks, duration);
Tejun Heoc9959052008-08-25 19:47:21 +0900740
Tejun Heo074a7ac2008-08-25 19:56:14 +0900741 part_stat_unlock();
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744void
745aoecmd_ata_rsp(struct sk_buff *skb)
746{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700747 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct aoedev *d;
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400749 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct aoe_atahdr *ahin, *ahout;
751 struct frame *f;
752 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800753 struct aoetgt *t;
754 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 register long n;
756 ulong flags;
757 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700758 u16 aoemajor;
759
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700760 hin = (struct aoe_hdr *) skb_mac_header(skb);
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700761 aoemajor = get_unaligned_be16(&hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700762 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (d == NULL) {
764 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
765 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700766 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 aoechr_error(ebuf);
768 return;
769 }
770
771 spin_lock_irqsave(&d->lock, flags);
772
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700773 n = get_unaligned_be32(&hin->tag);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800774 t = gettgt(d, hin->src);
775 if (t == NULL) {
Harvey Harrison411c41e2008-11-25 00:40:37 -0800776 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
777 d->aoemajor, d->aoeminor, hin->src);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800778 spin_unlock_irqrestore(&d->lock, flags);
779 return;
780 }
781 f = getframe(t, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -0400783 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 spin_unlock_irqrestore(&d->lock, flags);
785 snprintf(ebuf, sizeof ebuf,
786 "%15s e%d.%d tag=%08x@%08lx\n",
787 "unexpected rsp",
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700788 get_unaligned_be16(&hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 hin->minor,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700790 get_unaligned_be32(&hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 jiffies);
792 aoechr_error(ebuf);
793 return;
794 }
795
796 calc_rttavg(d, tsince(f->tag));
797
798 ahin = (struct aoe_atahdr *) (hin+1);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700799 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400800 ahout = (struct aoe_atahdr *) (hout+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 buf = f->buf;
802
803 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400804 printk(KERN_ERR
Ed L. Cashin1d759812008-02-08 04:20:08 -0800805 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ahout->cmdstat, ahin->cmdstat,
807 d->aoemajor, d->aoeminor);
808 if (buf)
809 buf->flags |= BUFFL_FAIL;
810 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800811 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
812 d->htgt = NULL;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400813 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 switch (ahout->cmdstat) {
815 case WIN_READ:
816 case WIN_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400818 printk(KERN_ERR
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800819 "aoe: %s. skb->len=%d need=%ld\n",
820 "runt data size in read", skb->len, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* fail frame f? just returning will rexmit. */
822 spin_unlock_irqrestore(&d->lock, flags);
823 return;
824 }
825 memcpy(f->bufaddr, ahin+1, n);
826 case WIN_WRITE:
827 case WIN_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800828 ifp = getif(t, skb->dev);
829 if (ifp) {
830 ifp->lost = 0;
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400831 if (n > DEFAULTBCNT)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800832 ifp->lostjumbo = 0;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400833 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800834 if (f->bcnt -= n) {
835 f->lba += n >> 9;
836 f->bufaddr += n;
837 resend(d, t, f);
838 goto xmit;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
841 case WIN_IDENTIFY:
842 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400843 printk(KERN_INFO
844 "aoe: runt data size in ataid. skb->len=%d\n",
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400845 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 spin_unlock_irqrestore(&d->lock, flags);
847 return;
848 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800849 ataid_complete(d, t, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400852 printk(KERN_INFO
853 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400854 ahout->cmdstat,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700855 get_unaligned_be16(&hin->major),
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400856 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 }
859
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800860 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
Linus Torvalds03054de2008-02-08 09:42:46 -0800861 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800862 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
863 bio_endio(buf->bio, n);
864 mempool_free(buf, d->bufpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 f->buf = NULL;
868 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800869 t->nout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 aoecmd_work(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800872xmit:
David S. Millere9bb8fb2008-09-21 22:36:49 -0700873 __skb_queue_head_init(&queue);
874 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700877 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880void
881aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
882{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700883 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
David S. Millere9bb8fb2008-09-21 22:36:49 -0700885 __skb_queue_head_init(&queue);
886 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
887 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800890struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891aoecmd_ata_id(struct aoedev *d)
892{
893 struct aoe_hdr *h;
894 struct aoe_atahdr *ah;
895 struct frame *f;
896 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800897 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400899 f = freeframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800900 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800902
903 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400906 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700907 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800909 skb_put(skb, sizeof *h + sizeof *ah);
910 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800911 f->tag = aoehdr_atainit(d, t, h);
912 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* set up ata header */
916 ah->scnt = 1;
917 ah->cmdstat = WIN_IDENTIFY;
918 ah->lba3 = 0xa0;
919
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800920 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500922 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400925 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800928static struct aoetgt *
929addtgt(struct aoedev *d, char *addr, ulong nframes)
930{
931 struct aoetgt *t, **tt, **te;
932 struct frame *f, *e;
933
934 tt = d->targets;
935 te = tt + NTARGETS;
936 for (; tt < te && *tt; tt++)
937 ;
938
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800939 if (tt == te) {
940 printk(KERN_INFO
941 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800942 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800943 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800944 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
945 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800946 if (!t || !f) {
947 kfree(f);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800948 kfree(t);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800949 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800950 return NULL;
951 }
952
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800953 t->nframes = nframes;
954 t->frames = f;
955 e = f + nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800956 for (; f < e; f++)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800957 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800958 memcpy(t->addr, addr, sizeof t->addr);
959 t->ifp = t->ifs;
960 t->maxout = t->nframes;
961 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964void
965aoecmd_cfg_rsp(struct sk_buff *skb)
966{
967 struct aoedev *d;
968 struct aoe_hdr *h;
969 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800970 struct aoetgt *t;
971 struct aoeif *ifp;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700972 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct sk_buff *sl;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400974 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700976 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 ch = (struct aoe_cfghdr *) (h+1);
978
979 /*
980 * Enough people have their dip switches set backwards to
981 * warrant a loud message for this special case.
982 */
Harvey Harrison823ed722008-07-04 09:28:32 +0200983 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400985 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400986 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return;
988 }
989
990 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700991 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400992 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700993 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return;
995 }
996
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400997 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -0800998 if (n > aoe_maxout) /* keep it reasonable */
999 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001001 d = aoedev_by_sysminor_m(sysminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (d == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001003 printk(KERN_INFO "aoe: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006
1007 spin_lock_irqsave(&d->lock, flags);
1008
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001009 t = gettgt(d, h->src);
1010 if (!t) {
1011 t = addtgt(d, h->src, n);
1012 if (!t) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001013 spin_unlock_irqrestore(&d->lock, flags);
1014 return;
1015 }
1016 }
1017 ifp = getif(t, skb->dev);
1018 if (!ifp) {
1019 ifp = addif(t, skb->dev);
1020 if (!ifp) {
1021 printk(KERN_INFO
1022 "aoe: device addif failure; "
1023 "too many interfaces?\n");
1024 spin_unlock_irqrestore(&d->lock, flags);
1025 return;
1026 }
1027 }
1028 if (ifp->maxbcnt) {
1029 n = ifp->nd->mtu;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001030 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1031 n /= 512;
1032 if (n > ch->scnt)
1033 n = ch->scnt;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001034 n = n ? n * 512 : DEFAULTBCNT;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001035 if (n != ifp->maxbcnt) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001036 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -08001037 "aoe: e%ld.%d: setting %d%s%s:%pm\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001038 d->aoemajor, d->aoeminor, n,
1039 " byte data frames on ", ifp->nd->name,
Harvey Harrison411c41e2008-11-25 00:40:37 -08001040 t->addr);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001041 ifp->maxbcnt = n;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001042 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001043 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001044
1045 /* don't change users' perspective */
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001046 if (d->nopen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 spin_unlock_irqrestore(&d->lock, flags);
1048 return;
1049 }
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -07001050 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001052 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 spin_unlock_irqrestore(&d->lock, flags);
1055
David S. Millere9bb8fb2008-09-21 22:36:49 -07001056 if (sl) {
1057 struct sk_buff_head queue;
1058 __skb_queue_head_init(&queue);
1059 __skb_queue_tail(&queue, sl);
1060 aoenet_xmit(&queue);
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001064void
1065aoecmd_cleanslate(struct aoedev *d)
1066{
1067 struct aoetgt **t, **te;
1068 struct aoeif *p, *e;
1069
1070 d->mintimer = MINTIMER;
1071
1072 t = d->targets;
1073 te = t + NTARGETS;
1074 for (; t < te && *t; t++) {
1075 (*t)->maxout = (*t)->nframes;
1076 p = (*t)->ifs;
1077 e = p + NAOEIFS;
1078 for (; p < e; p++) {
1079 p->lostjumbo = 0;
1080 p->lost = 0;
1081 p->maxbcnt = DEFAULTBCNT;
1082 }
1083 }
1084}