blob: 13bb69d2abb3307d8cb95c9f21e04e0c7b2e9ae8 [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
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +02007#include <linux/ata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/hdreg.h>
9#include <linux/blkdev.h>
10#include <linux/skbuff.h>
11#include <linux/netdevice.h>
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050012#include <linux/genhd.h>
Ed L. Cashin68e0d422008-02-08 04:20:00 -080013#include <linux/moduleparam.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070014#include <net/net_namespace.h>
Ed L. Cashin475172f2005-09-29 12:47:40 -040015#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "aoe.h"
17
Ed L. Cashinb751e8b2006-09-20 14:36:50 -040018static int aoe_deadsecs = 60 * 3;
19module_param(aoe_deadsecs, int, 0644);
20MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Ed L. Cashin7df620d2008-02-08 04:20:07 -080022static int aoe_maxout = 16;
23module_param(aoe_maxout, int, 0644);
24MODULE_PARM_DESC(aoe_maxout,
25 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
26
Ed L. Cashin68e0d422008-02-08 04:20:00 -080027static struct sk_buff *
Ed L. Cashine407a7f2006-09-20 14:36:49 -040028new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 struct sk_buff *skb;
31
32 skb = alloc_skb(len, GFP_ATOMIC);
33 if (skb) {
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070034 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070035 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 skb->protocol = __constant_htons(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38 return skb;
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static struct frame *
Ed L. Cashin68e0d422008-02-08 04:20:00 -080042getframe(struct aoetgt *t, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct frame *f, *e;
45
Ed L. Cashin68e0d422008-02-08 04:20:00 -080046 f = t->frames;
47 e = f + t->nframes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 for (; f<e; f++)
49 if (f->tag == tag)
50 return f;
51 return NULL;
52}
53
54/*
55 * Leave the top bit clear so we have tagspace for userland.
56 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
57 * This driver reserves tag -1 to mean "unused frame."
58 */
59static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080060newtag(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 register ulong n;
63
64 n = jiffies & 0xffff;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080065 return n |= (++t->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080069aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080071 u32 host_tag = newtag(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Ed L. Cashin68e0d422008-02-08 04:20:00 -080073 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
74 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070075 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070077 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 h->minor = d->aoeminor;
79 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070080 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 return host_tag;
83}
84
Ed L. Cashin19bf2632006-09-20 14:36:49 -040085static inline void
86put_lba(struct aoe_atahdr *ah, sector_t lba)
87{
88 ah->lba0 = lba;
89 ah->lba1 = lba >>= 8;
90 ah->lba2 = lba >>= 8;
91 ah->lba3 = lba >>= 8;
92 ah->lba4 = lba >>= 8;
93 ah->lba5 = lba >>= 8;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -080097ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080099 t->ifp++;
100 if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
101 t->ifp = t->ifs;
102 if (t->ifp->nd == NULL) {
103 printk(KERN_INFO "aoe: no interface to rotate to\n");
104 BUG();
105 }
106}
107
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800108static void
109skb_pool_put(struct aoedev *d, struct sk_buff *skb)
110{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700111 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800112}
113
114static struct sk_buff *
115skb_pool_get(struct aoedev *d)
116{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700117 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800118
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800119 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb2008-09-21 22:36:49 -0700120 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800121 return skb;
122 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700123 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
124 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800125 return skb;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700126
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800127 return NULL;
128}
129
130/* freeframe is where we do our load balancing so it's a little hairy. */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800131static struct frame *
132freeframe(struct aoedev *d)
133{
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800134 struct frame *f, *e, *rf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800135 struct aoetgt **t;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800136 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800137
138 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
139 printk(KERN_ERR "aoe: NULL TARGETS!\n");
140 return NULL;
141 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800142 t = d->tgt;
143 t++;
144 if (t >= &d->targets[NTARGETS] || !*t)
145 t = d->targets;
146 for (;;) {
147 if ((*t)->nout < (*t)->maxout
148 && t != d->htgt
149 && (*t)->ifp->nd) {
150 rf = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800151 f = (*t)->frames;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800152 e = f + (*t)->nframes;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800153 for (; f < e; f++) {
154 if (f->tag != FREETAG)
155 continue;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800156 skb = f->skb;
157 if (!skb
158 && !(f->skb = skb = new_skb(ETH_ZLEN)))
159 continue;
160 if (atomic_read(&skb_shinfo(skb)->dataref)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800161 != 1) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800162 if (!rf)
163 rf = f;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800164 continue;
165 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800166gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
167 skb_trim(skb, 0);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800168 d->tgt = t;
169 ifrotate(*t);
170 return f;
171 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800172 /* Work can be done, but the network layer is
173 holding our precious packets. Try to grab
174 one from the pool. */
175 f = rf;
176 if (f == NULL) { /* more paranoia */
177 printk(KERN_ERR
178 "aoe: freeframe: %s.\n",
179 "unexpected null rf");
180 d->flags |= DEVFL_KICKME;
181 return NULL;
182 }
183 skb = skb_pool_get(d);
184 if (skb) {
185 skb_pool_put(d, f->skb);
186 f->skb = skb;
187 goto gotone;
188 }
189 (*t)->dataref++;
190 if ((*t)->nout == 0)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800191 d->flags |= DEVFL_KICKME;
192 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800193 if (t == d->tgt) /* we've looped and found nada */
194 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800195 t++;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800196 if (t >= &d->targets[NTARGETS] || !*t)
197 t = d->targets;
198 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800199 return NULL;
200}
201
202static int
203aoecmd_ata_rw(struct aoedev *d)
204{
205 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct aoe_hdr *h;
207 struct aoe_atahdr *ah;
208 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800209 struct bio_vec *bv;
210 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct sk_buff *skb;
212 ulong bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 char writebit, extbit;
214
215 writebit = 0x10;
216 extbit = 0x4;
217
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800218 f = freeframe(d);
219 if (f == NULL)
220 return 0;
221 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 buf = d->inprocess;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800223 bv = buf->bv;
224 bcnt = t->ifp->maxbcnt;
225 if (bcnt == 0)
226 bcnt = DEFAULTBCNT;
227 if (bcnt > buf->bv_resid)
228 bcnt = buf->bv_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400230 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700231 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800233 skb_put(skb, sizeof *h + sizeof *ah);
234 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800235 f->tag = aoehdr_atainit(d, t, h);
236 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 f->waited = 0;
238 f->buf = buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800239 f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400240 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800241 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* set up ata header */
244 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800245 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (d->flags & DEVFL_EXT) {
247 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 } else {
249 extbit = 0;
250 ah->lba3 &= 0x0f;
251 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800254 skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400256 skb->len += bcnt;
257 skb->data_len = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800258 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800260 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200264 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* mark all tracking fields and load out */
267 buf->nframesout += 1;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800268 buf->bv_off += bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 buf->bv_resid -= bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 buf->resid -= bcnt;
271 buf->sector += bcnt >> 9;
272 if (buf->resid == 0) {
273 d->inprocess = NULL;
274 } else if (buf->bv_resid == 0) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800275 buf->bv = ++bv;
276 buf->bv_resid = bv->bv_len;
277 WARN_ON(buf->bv_resid == 0);
278 buf->bv_off = bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800281 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400282 skb = skb_clone(skb, GFP_ATOMIC);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700283 if (skb)
284 __skb_queue_tail(&d->sendq, skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800285 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500288/* some callers cannot sleep, and they can call this function,
289 * transmitting the packets later, when interrupts are on
290 */
David S. Millere9bb8fb2008-09-21 22:36:49 -0700291static void
292aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500293{
294 struct aoe_hdr *h;
295 struct aoe_cfghdr *ch;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700296 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500297 struct net_device *ifp;
298
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500299 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700300 for_each_netdev(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500301 dev_hold(ifp);
302 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700303 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500304
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400305 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500306 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400307 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700308 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500309 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800310 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400311 skb->dev = ifp;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700312 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700313 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500314 memset(h, 0, sizeof *h + sizeof *ch);
315
316 memset(h->dst, 0xff, sizeof h->dst);
317 memcpy(h->src, ifp->dev_addr, sizeof h->src);
318 h->type = __constant_cpu_to_be16(ETH_P_AOE);
319 h->verfl = AOE_HVER;
320 h->major = cpu_to_be16(aoemajor);
321 h->minor = aoeminor;
322 h->cmd = AOECMD_CFG;
323
Pavel Emelianov7562f872007-05-03 15:13:45 -0700324cont:
325 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500326 }
327 read_unlock(&dev_base_lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800331resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct sk_buff *skb;
334 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400335 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 char buf[128];
337 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800339 ifrotate(t);
340 n = newtag(t);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400341 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700342 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400343 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800344
345 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800346 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800347 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800348 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800349 aoechr_error(buf);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700352 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800353 memcpy(h->dst, t->addr, sizeof h->dst);
354 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800356 switch (ah->cmdstat) {
357 default:
358 break;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200359 case ATA_CMD_PIO_READ:
360 case ATA_CMD_PIO_READ_EXT:
361 case ATA_CMD_PIO_WRITE:
362 case ATA_CMD_PIO_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800363 put_lba(ah, f->lba);
364
365 n = f->bcnt;
366 if (n > DEFAULTBCNT)
367 n = DEFAULTBCNT;
368 ah->scnt = n >> 9;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400369 if (ah->aflags & AOEAFL_WRITE) {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400370 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800371 offset_in_page(f->bufaddr), n);
372 skb->len = sizeof *h + sizeof *ah + n;
373 skb->data_len = n;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400374 }
375 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800376 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400377 skb = skb_clone(skb, GFP_ATOMIC);
378 if (skb == NULL)
379 return;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700380 __skb_queue_tail(&d->sendq, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static int
384tsince(int tag)
385{
386 int n;
387
388 n = jiffies & 0xffff;
389 n -= tag & 0xffff;
390 if (n < 0)
391 n += 1<<16;
392 return n;
393}
394
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800395static struct aoeif *
396getif(struct aoetgt *t, struct net_device *nd)
397{
398 struct aoeif *p, *e;
399
400 p = t->ifs;
401 e = p + NAOEIFS;
402 for (; p < e; p++)
403 if (p->nd == nd)
404 return p;
405 return NULL;
406}
407
408static struct aoeif *
409addif(struct aoetgt *t, struct net_device *nd)
410{
411 struct aoeif *p;
412
413 p = getif(t, NULL);
414 if (!p)
415 return NULL;
416 p->nd = nd;
417 p->maxbcnt = DEFAULTBCNT;
418 p->lost = 0;
419 p->lostjumbo = 0;
420 return p;
421}
422
423static void
424ejectif(struct aoetgt *t, struct aoeif *ifp)
425{
426 struct aoeif *e;
427 ulong n;
428
429 e = t->ifs + NAOEIFS - 1;
430 n = (e - ifp) * sizeof *ifp;
431 memmove(ifp, ifp+1, n);
432 e->nd = NULL;
433}
434
435static int
436sthtith(struct aoedev *d)
437{
438 struct frame *f, *e, *nf;
439 struct sk_buff *skb;
440 struct aoetgt *ht = *d->htgt;
441
442 f = ht->frames;
443 e = f + ht->nframes;
444 for (; f < e; f++) {
445 if (f->tag == FREETAG)
446 continue;
447 nf = freeframe(d);
448 if (!nf)
449 return 0;
450 skb = nf->skb;
451 *nf = *f;
452 f->skb = skb;
453 f->tag = FREETAG;
454 nf->waited = 0;
455 ht->nout--;
456 (*d->tgt)->nout++;
457 resend(d, *d->tgt, nf);
458 }
459 /* he's clean, he's useless. take away his interfaces */
460 memset(ht->ifs, 0, sizeof ht->ifs);
461 d->htgt = NULL;
462 return 1;
463}
464
465static inline unsigned char
466ata_scnt(unsigned char *packet) {
467 struct aoe_hdr *h;
468 struct aoe_atahdr *ah;
469
470 h = (struct aoe_hdr *) packet;
471 ah = (struct aoe_atahdr *) (h+1);
472 return ah->scnt;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void
476rexmit_timer(ulong vp)
477{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700478 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800480 struct aoetgt *t, **tt, **te;
481 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct frame *f, *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 register long timeout;
484 ulong flags, n;
485
486 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* timeout is always ~150% of the moving average */
489 timeout = d->rttavg;
490 timeout += timeout >> 1;
491
492 spin_lock_irqsave(&d->lock, flags);
493
494 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500495 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
497 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800498 tt = d->targets;
499 te = tt + NTARGETS;
500 for (; tt < te && *tt; tt++) {
501 t = *tt;
502 f = t->frames;
503 e = f + t->nframes;
504 for (; f < e; f++) {
505 if (f->tag == FREETAG
506 || tsince(f->tag) < timeout)
507 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 n = f->waited += timeout;
509 n /= HZ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800510 if (n > aoe_deadsecs) {
511 /* waited too long. device failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800515
516 if (n > HELPWAIT /* see if another target can help */
517 && (tt != d->targets || d->targets[1]))
518 d->htgt = tt;
519
520 if (t->nout == t->maxout) {
521 if (t->maxout > 1)
522 t->maxout--;
523 t->lastwadj = jiffies;
524 }
525
526 ifp = getif(t, f->skb->dev);
527 if (ifp && ++ifp->lost > (t->nframes << 1)
528 && (ifp != t->ifs || t->ifs[1].nd)) {
529 ejectif(t, ifp);
530 ifp = NULL;
531 }
532
533 if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
534 && ifp && ++ifp->lostjumbo > (t->nframes << 1)
535 && ifp->maxbcnt != DEFAULTBCNT) {
536 printk(KERN_INFO
537 "aoe: e%ld.%d: "
538 "too many lost jumbo on "
Harvey Harrison411c41e2008-11-25 00:40:37 -0800539 "%s:%pm - "
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800540 "falling back to %d frames.\n",
541 d->aoemajor, d->aoeminor,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800542 ifp->nd->name, t->addr,
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800543 DEFAULTBCNT);
544 ifp->maxbcnt = 0;
545 }
546 resend(d, t, f);
547 }
548
549 /* window check */
550 if (t->nout == t->maxout
551 && t->maxout < t->nframes
552 && (jiffies - t->lastwadj)/HZ > 10) {
553 t->maxout++;
554 t->lastwadj = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800557
David S. Millere9bb8fb2008-09-21 22:36:49 -0700558 if (!skb_queue_empty(&d->sendq)) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800559 n = d->rttavg <<= 1;
560 if (n > MAXTIMER)
561 d->rttavg = MAXTIMER;
562 }
563
564 if (d->flags & DEVFL_KICKME || d->htgt) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400565 d->flags &= ~DEVFL_KICKME;
566 aoecmd_work(d);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
David S. Millere9bb8fb2008-09-21 22:36:49 -0700569 __skb_queue_head_init(&queue);
570 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 d->timer.expires = jiffies + TIMERTICK;
573 add_timer(&d->timer);
574
575 spin_unlock_irqrestore(&d->lock, flags);
576
David S. Millere9bb8fb2008-09-21 22:36:49 -0700577 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800580/* enters with d->lock held */
581void
582aoecmd_work(struct aoedev *d)
583{
584 struct buf *buf;
585loop:
586 if (d->htgt && !sthtith(d))
587 return;
588 if (d->inprocess == NULL) {
589 if (list_empty(&d->bufq))
590 return;
591 buf = container_of(d->bufq.next, struct buf, bufs);
592 list_del(d->bufq.next);
593 d->inprocess = buf;
594 }
595 if (aoecmd_ata_rw(d))
596 goto loop;
597}
598
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500599/* this function performs work that has been deferred until sleeping is OK
600 */
601void
David Howellsc4028952006-11-22 14:57:56 +0000602aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500603{
David Howellsc4028952006-11-22 14:57:56 +0000604 struct aoedev *d = container_of(work, struct aoedev, work);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500605
606 if (d->flags & DEVFL_GDALLOC)
607 aoeblk_gdalloc(d);
608
609 if (d->flags & DEVFL_NEWSIZE) {
610 struct block_device *bd;
611 unsigned long flags;
612 u64 ssize;
613
Tejun Heo80795ae2008-08-25 19:56:07 +0900614 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500615 bd = bdget_disk(d->gd, 0);
616
617 if (bd) {
618 mutex_lock(&bd->bd_inode->i_mutex);
619 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
620 mutex_unlock(&bd->bd_inode->i_mutex);
621 bdput(bd);
622 }
623 spin_lock_irqsave(&d->lock, flags);
624 d->flags |= DEVFL_UP;
625 d->flags &= ~DEVFL_NEWSIZE;
626 spin_unlock_irqrestore(&d->lock, flags);
627 }
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800631ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 u64 ssize;
634 u16 n;
635
636 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700637 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700640 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (n & (1<<10)) { /* bit 10: LBA 48 */
643 d->flags |= DEVFL_EXT;
644
645 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700646 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* set as in ide-disk.c:init_idedisk_capacity */
649 d->geo.cylinders = ssize;
650 d->geo.cylinders /= (255 * 63);
651 d->geo.heads = 255;
652 d->geo.sectors = 63;
653 } else {
654 d->flags &= ~DEVFL_EXT;
655
656 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700657 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700660 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
661 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
662 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500664
665 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800666 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800667 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
668 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500669 d->aoemajor, d->aoeminor,
670 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 d->ssize = ssize;
672 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800673 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900676 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500677 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800678 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500679 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683static void
684calc_rttavg(struct aoedev *d, int rtt)
685{
686 register long n;
687
688 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400689 if (n < 0) {
690 n = -rtt;
691 if (n < MINTIMER)
692 n = MINTIMER;
693 else if (n > MAXTIMER)
694 n = MAXTIMER;
695 d->mintimer += (n - d->mintimer) >> 1;
696 } else if (n < d->mintimer)
697 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 else if (n > MAXTIMER)
699 n = MAXTIMER;
700
701 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
702 n -= d->rttavg;
703 d->rttavg += n >> 2;
704}
705
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800706static struct aoetgt *
707gettgt(struct aoedev *d, char *addr)
708{
709 struct aoetgt **t, **e;
710
711 t = d->targets;
712 e = t + NTARGETS;
713 for (; t < e && *t; t++)
714 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
715 return *t;
716 return NULL;
717}
718
719static inline void
Linus Torvalds03054de2008-02-08 09:42:46 -0800720diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800721{
722 unsigned long n_sect = bio->bi_size >> 9;
723 const int rw = bio_data_dir(bio);
Jens Axboe28f13702008-05-07 10:15:46 +0200724 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +0900725 int cpu;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800726
Tejun Heo074a7ac2008-08-25 19:56:14 +0900727 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200728 part = disk_map_sector_rcu(disk, sector);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200729
Tejun Heo074a7ac2008-08-25 19:56:14 +0900730 part_stat_inc(cpu, part, ios[rw]);
731 part_stat_add(cpu, part, ticks[rw], duration);
732 part_stat_add(cpu, part, sectors[rw], n_sect);
733 part_stat_add(cpu, part, io_ticks, duration);
Tejun Heoc9959052008-08-25 19:47:21 +0900734
Tejun Heo074a7ac2008-08-25 19:56:14 +0900735 part_stat_unlock();
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800736}
737
Peter Horton0a1f1272009-12-01 13:17:46 -0800738/*
739 * Ensure we don't create aliases in VI caches
740 */
741static inline void
742killalias(struct bio *bio)
743{
744 struct bio_vec *bv;
745 int i;
746
747 if (bio_data_dir(bio) == READ)
748 __bio_for_each_segment(bv, bio, i, 0) {
749 flush_dcache_page(bv->bv_page);
750 }
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753void
754aoecmd_ata_rsp(struct sk_buff *skb)
755{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700756 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct aoedev *d;
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400758 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct aoe_atahdr *ahin, *ahout;
760 struct frame *f;
761 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800762 struct aoetgt *t;
763 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 register long n;
765 ulong flags;
766 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700767 u16 aoemajor;
768
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700769 hin = (struct aoe_hdr *) skb_mac_header(skb);
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700770 aoemajor = get_unaligned_be16(&hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700771 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (d == NULL) {
773 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
774 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700775 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 aoechr_error(ebuf);
777 return;
778 }
779
780 spin_lock_irqsave(&d->lock, flags);
781
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700782 n = get_unaligned_be32(&hin->tag);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800783 t = gettgt(d, hin->src);
784 if (t == NULL) {
Harvey Harrison411c41e2008-11-25 00:40:37 -0800785 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
786 d->aoemajor, d->aoeminor, hin->src);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800787 spin_unlock_irqrestore(&d->lock, flags);
788 return;
789 }
790 f = getframe(t, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -0400792 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 spin_unlock_irqrestore(&d->lock, flags);
794 snprintf(ebuf, sizeof ebuf,
795 "%15s e%d.%d tag=%08x@%08lx\n",
796 "unexpected rsp",
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700797 get_unaligned_be16(&hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 hin->minor,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700799 get_unaligned_be32(&hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 jiffies);
801 aoechr_error(ebuf);
802 return;
803 }
804
805 calc_rttavg(d, tsince(f->tag));
806
807 ahin = (struct aoe_atahdr *) (hin+1);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700808 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400809 ahout = (struct aoe_atahdr *) (hout+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 buf = f->buf;
811
812 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400813 printk(KERN_ERR
Ed L. Cashin1d759812008-02-08 04:20:08 -0800814 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ahout->cmdstat, ahin->cmdstat,
816 d->aoemajor, d->aoeminor);
817 if (buf)
818 buf->flags |= BUFFL_FAIL;
819 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800820 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
821 d->htgt = NULL;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400822 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 switch (ahout->cmdstat) {
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200824 case ATA_CMD_PIO_READ:
825 case ATA_CMD_PIO_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400827 printk(KERN_ERR
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800828 "aoe: %s. skb->len=%d need=%ld\n",
829 "runt data size in read", skb->len, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* fail frame f? just returning will rexmit. */
831 spin_unlock_irqrestore(&d->lock, flags);
832 return;
833 }
834 memcpy(f->bufaddr, ahin+1, n);
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200835 case ATA_CMD_PIO_WRITE:
836 case ATA_CMD_PIO_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800837 ifp = getif(t, skb->dev);
838 if (ifp) {
839 ifp->lost = 0;
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400840 if (n > DEFAULTBCNT)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800841 ifp->lostjumbo = 0;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400842 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800843 if (f->bcnt -= n) {
844 f->lba += n >> 9;
845 f->bufaddr += n;
846 resend(d, t, f);
847 goto xmit;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 break;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200850 case ATA_CMD_ID_ATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400852 printk(KERN_INFO
853 "aoe: runt data size in ataid. skb->len=%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400854 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock_irqrestore(&d->lock, flags);
856 return;
857 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800858 ataid_complete(d, t, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400861 printk(KERN_INFO
862 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400863 ahout->cmdstat,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700864 get_unaligned_be16(&hin->major),
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400865 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 }
868
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800869 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
Linus Torvalds03054de2008-02-08 09:42:46 -0800870 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
Peter Horton0a1f1272009-12-01 13:17:46 -0800871 if (buf->flags & BUFFL_FAIL)
872 bio_endio(buf->bio, -EIO);
873 else {
874 killalias(buf->bio);
875 bio_endio(buf->bio, 0);
876 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800877 mempool_free(buf, d->bufpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 f->buf = NULL;
881 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800882 t->nout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 aoecmd_work(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800885xmit:
David S. Millere9bb8fb2008-09-21 22:36:49 -0700886 __skb_queue_head_init(&queue);
887 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700890 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893void
894aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
895{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700896 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
David S. Millere9bb8fb2008-09-21 22:36:49 -0700898 __skb_queue_head_init(&queue);
899 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
900 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800903struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904aoecmd_ata_id(struct aoedev *d)
905{
906 struct aoe_hdr *h;
907 struct aoe_atahdr *ah;
908 struct frame *f;
909 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800910 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400912 f = freeframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800913 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800915
916 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400919 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700920 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800922 skb_put(skb, sizeof *h + sizeof *ah);
923 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800924 f->tag = aoehdr_atainit(d, t, h);
925 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* set up ata header */
929 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200930 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ah->lba3 = 0xa0;
932
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800933 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500935 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400938 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800941static struct aoetgt *
942addtgt(struct aoedev *d, char *addr, ulong nframes)
943{
944 struct aoetgt *t, **tt, **te;
945 struct frame *f, *e;
946
947 tt = d->targets;
948 te = tt + NTARGETS;
949 for (; tt < te && *tt; tt++)
950 ;
951
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800952 if (tt == te) {
953 printk(KERN_INFO
954 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800955 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800956 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800957 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
958 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800959 if (!t || !f) {
960 kfree(f);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800961 kfree(t);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800962 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800963 return NULL;
964 }
965
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800966 t->nframes = nframes;
967 t->frames = f;
968 e = f + nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800969 for (; f < e; f++)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800970 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800971 memcpy(t->addr, addr, sizeof t->addr);
972 t->ifp = t->ifs;
973 t->maxout = t->nframes;
974 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977void
978aoecmd_cfg_rsp(struct sk_buff *skb)
979{
980 struct aoedev *d;
981 struct aoe_hdr *h;
982 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800983 struct aoetgt *t;
984 struct aoeif *ifp;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700985 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct sk_buff *sl;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400987 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700989 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ch = (struct aoe_cfghdr *) (h+1);
991
992 /*
993 * Enough people have their dip switches set backwards to
994 * warrant a loud message for this special case.
995 */
Harvey Harrison823ed722008-07-04 09:28:32 +0200996 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400998 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400999 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return;
1001 }
1002
1003 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -07001004 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001005 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -07001006 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
1008 }
1009
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001010 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001011 if (n > aoe_maxout) /* keep it reasonable */
1012 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001014 d = aoedev_by_sysminor_m(sysminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (d == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001016 printk(KERN_INFO "aoe: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return;
1018 }
1019
1020 spin_lock_irqsave(&d->lock, flags);
1021
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001022 t = gettgt(d, h->src);
1023 if (!t) {
1024 t = addtgt(d, h->src, n);
1025 if (!t) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001026 spin_unlock_irqrestore(&d->lock, flags);
1027 return;
1028 }
1029 }
1030 ifp = getif(t, skb->dev);
1031 if (!ifp) {
1032 ifp = addif(t, skb->dev);
1033 if (!ifp) {
1034 printk(KERN_INFO
1035 "aoe: device addif failure; "
1036 "too many interfaces?\n");
1037 spin_unlock_irqrestore(&d->lock, flags);
1038 return;
1039 }
1040 }
1041 if (ifp->maxbcnt) {
1042 n = ifp->nd->mtu;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001043 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1044 n /= 512;
1045 if (n > ch->scnt)
1046 n = ch->scnt;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001047 n = n ? n * 512 : DEFAULTBCNT;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001048 if (n != ifp->maxbcnt) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001049 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -08001050 "aoe: e%ld.%d: setting %d%s%s:%pm\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001051 d->aoemajor, d->aoeminor, n,
1052 " byte data frames on ", ifp->nd->name,
Harvey Harrison411c41e2008-11-25 00:40:37 -08001053 t->addr);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001054 ifp->maxbcnt = n;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001055 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001056 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001057
1058 /* don't change users' perspective */
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001059 if (d->nopen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 spin_unlock_irqrestore(&d->lock, flags);
1061 return;
1062 }
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -07001063 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001065 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 spin_unlock_irqrestore(&d->lock, flags);
1068
David S. Millere9bb8fb2008-09-21 22:36:49 -07001069 if (sl) {
1070 struct sk_buff_head queue;
1071 __skb_queue_head_init(&queue);
1072 __skb_queue_tail(&queue, sl);
1073 aoenet_xmit(&queue);
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001077void
1078aoecmd_cleanslate(struct aoedev *d)
1079{
1080 struct aoetgt **t, **te;
1081 struct aoeif *p, *e;
1082
1083 d->mintimer = MINTIMER;
1084
1085 t = d->targets;
1086 te = t + NTARGETS;
1087 for (; t < te && *t; t++) {
1088 (*t)->maxout = (*t)->nframes;
1089 p = (*t)->ifs;
1090 e = p + NAOEIFS;
1091 for (; p < e; p++) {
1092 p->lostjumbo = 0;
1093 p->lost = 0;
1094 p->maxbcnt = DEFAULTBCNT;
1095 }
1096 }
1097}