blob: 31693bc24444f3541c74dfccc9c9c384edf8fd5a [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);
37 skb->priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 skb->next = skb->prev = NULL;
39
40 /* tell the network layer not to perform IP checksums
41 * or to get the NIC to do it
42 */
43 skb->ip_summed = CHECKSUM_NONE;
44 }
45 return skb;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static struct frame *
Ed L. Cashin68e0d422008-02-08 04:20:00 -080049getframe(struct aoetgt *t, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct frame *f, *e;
52
Ed L. Cashin68e0d422008-02-08 04:20:00 -080053 f = t->frames;
54 e = f + t->nframes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 for (; f<e; f++)
56 if (f->tag == tag)
57 return f;
58 return NULL;
59}
60
61/*
62 * Leave the top bit clear so we have tagspace for userland.
63 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
64 * This driver reserves tag -1 to mean "unused frame."
65 */
66static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080067newtag(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 register ulong n;
70
71 n = jiffies & 0xffff;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080072 return n |= (++t->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75static int
Ed L. Cashin68e0d422008-02-08 04:20:00 -080076aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080078 u32 host_tag = newtag(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Ed L. Cashin68e0d422008-02-08 04:20:00 -080080 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
81 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070082 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070084 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 h->minor = d->aoeminor;
86 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070087 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return host_tag;
90}
91
Ed L. Cashin19bf2632006-09-20 14:36:49 -040092static inline void
93put_lba(struct aoe_atahdr *ah, sector_t lba)
94{
95 ah->lba0 = lba;
96 ah->lba1 = lba >>= 8;
97 ah->lba2 = lba >>= 8;
98 ah->lba3 = lba >>= 8;
99 ah->lba4 = lba >>= 8;
100 ah->lba5 = lba >>= 8;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800104ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800106 t->ifp++;
107 if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
108 t->ifp = t->ifs;
109 if (t->ifp->nd == NULL) {
110 printk(KERN_INFO "aoe: no interface to rotate to\n");
111 BUG();
112 }
113}
114
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800115static void
116skb_pool_put(struct aoedev *d, struct sk_buff *skb)
117{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700118 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800119}
120
121static struct sk_buff *
122skb_pool_get(struct aoedev *d)
123{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700124 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800125
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800126 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb2008-09-21 22:36:49 -0700127 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800128 return skb;
129 }
David S. Millere9bb8fb2008-09-21 22:36:49 -0700130 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
131 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800132 return skb;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700133
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800134 return NULL;
135}
136
137/* freeframe is where we do our load balancing so it's a little hairy. */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800138static struct frame *
139freeframe(struct aoedev *d)
140{
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800141 struct frame *f, *e, *rf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800142 struct aoetgt **t;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800143 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800144
145 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
146 printk(KERN_ERR "aoe: NULL TARGETS!\n");
147 return NULL;
148 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800149 t = d->tgt;
150 t++;
151 if (t >= &d->targets[NTARGETS] || !*t)
152 t = d->targets;
153 for (;;) {
154 if ((*t)->nout < (*t)->maxout
155 && t != d->htgt
156 && (*t)->ifp->nd) {
157 rf = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800158 f = (*t)->frames;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800159 e = f + (*t)->nframes;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800160 for (; f < e; f++) {
161 if (f->tag != FREETAG)
162 continue;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800163 skb = f->skb;
164 if (!skb
165 && !(f->skb = skb = new_skb(ETH_ZLEN)))
166 continue;
167 if (atomic_read(&skb_shinfo(skb)->dataref)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800168 != 1) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800169 if (!rf)
170 rf = f;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800171 continue;
172 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800173gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
174 skb_trim(skb, 0);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800175 d->tgt = t;
176 ifrotate(*t);
177 return f;
178 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800179 /* Work can be done, but the network layer is
180 holding our precious packets. Try to grab
181 one from the pool. */
182 f = rf;
183 if (f == NULL) { /* more paranoia */
184 printk(KERN_ERR
185 "aoe: freeframe: %s.\n",
186 "unexpected null rf");
187 d->flags |= DEVFL_KICKME;
188 return NULL;
189 }
190 skb = skb_pool_get(d);
191 if (skb) {
192 skb_pool_put(d, f->skb);
193 f->skb = skb;
194 goto gotone;
195 }
196 (*t)->dataref++;
197 if ((*t)->nout == 0)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800198 d->flags |= DEVFL_KICKME;
199 }
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800200 if (t == d->tgt) /* we've looped and found nada */
201 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800202 t++;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800203 if (t >= &d->targets[NTARGETS] || !*t)
204 t = d->targets;
205 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800206 return NULL;
207}
208
209static int
210aoecmd_ata_rw(struct aoedev *d)
211{
212 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct aoe_hdr *h;
214 struct aoe_atahdr *ah;
215 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800216 struct bio_vec *bv;
217 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct sk_buff *skb;
219 ulong bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 char writebit, extbit;
221
222 writebit = 0x10;
223 extbit = 0x4;
224
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800225 f = freeframe(d);
226 if (f == NULL)
227 return 0;
228 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 buf = d->inprocess;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800230 bv = buf->bv;
231 bcnt = t->ifp->maxbcnt;
232 if (bcnt == 0)
233 bcnt = DEFAULTBCNT;
234 if (bcnt > buf->bv_resid)
235 bcnt = buf->bv_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400237 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700238 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800240 skb_put(skb, sizeof *h + sizeof *ah);
241 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800242 f->tag = aoehdr_atainit(d, t, h);
243 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 f->waited = 0;
245 f->buf = buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800246 f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400247 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800248 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* set up ata header */
251 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800252 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (d->flags & DEVFL_EXT) {
254 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 } else {
256 extbit = 0;
257 ah->lba3 &= 0x0f;
258 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800261 skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400263 skb->len += bcnt;
264 skb->data_len = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800265 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800267 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200271 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* mark all tracking fields and load out */
274 buf->nframesout += 1;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800275 buf->bv_off += bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 buf->bv_resid -= bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 buf->resid -= bcnt;
278 buf->sector += bcnt >> 9;
279 if (buf->resid == 0) {
280 d->inprocess = NULL;
281 } else if (buf->bv_resid == 0) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800282 buf->bv = ++bv;
283 buf->bv_resid = bv->bv_len;
284 WARN_ON(buf->bv_resid == 0);
285 buf->bv_off = bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800288 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400289 skb = skb_clone(skb, GFP_ATOMIC);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700290 if (skb)
291 __skb_queue_tail(&d->sendq, skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800292 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500295/* some callers cannot sleep, and they can call this function,
296 * transmitting the packets later, when interrupts are on
297 */
David S. Millere9bb8fb2008-09-21 22:36:49 -0700298static void
299aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500300{
301 struct aoe_hdr *h;
302 struct aoe_cfghdr *ch;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700303 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500304 struct net_device *ifp;
305
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500306 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700307 for_each_netdev(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500308 dev_hold(ifp);
309 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700310 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500311
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400312 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500313 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400314 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700315 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500316 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800317 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400318 skb->dev = ifp;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700319 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700320 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500321 memset(h, 0, sizeof *h + sizeof *ch);
322
323 memset(h->dst, 0xff, sizeof h->dst);
324 memcpy(h->src, ifp->dev_addr, sizeof h->src);
325 h->type = __constant_cpu_to_be16(ETH_P_AOE);
326 h->verfl = AOE_HVER;
327 h->major = cpu_to_be16(aoemajor);
328 h->minor = aoeminor;
329 h->cmd = AOECMD_CFG;
330
Pavel Emelianov7562f872007-05-03 15:13:45 -0700331cont:
332 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500333 }
334 read_unlock(&dev_base_lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800338resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct sk_buff *skb;
341 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400342 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 char buf[128];
344 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800346 ifrotate(t);
347 n = newtag(t);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400348 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700349 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400350 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800351
352 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800353 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800354 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800355 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800356 aoechr_error(buf);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700359 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800360 memcpy(h->dst, t->addr, sizeof h->dst);
361 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800363 switch (ah->cmdstat) {
364 default:
365 break;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200366 case ATA_CMD_PIO_READ:
367 case ATA_CMD_PIO_READ_EXT:
368 case ATA_CMD_PIO_WRITE:
369 case ATA_CMD_PIO_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800370 put_lba(ah, f->lba);
371
372 n = f->bcnt;
373 if (n > DEFAULTBCNT)
374 n = DEFAULTBCNT;
375 ah->scnt = n >> 9;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400376 if (ah->aflags & AOEAFL_WRITE) {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400377 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800378 offset_in_page(f->bufaddr), n);
379 skb->len = sizeof *h + sizeof *ah + n;
380 skb->data_len = n;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400381 }
382 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800383 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400384 skb = skb_clone(skb, GFP_ATOMIC);
385 if (skb == NULL)
386 return;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700387 __skb_queue_tail(&d->sendq, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static int
391tsince(int tag)
392{
393 int n;
394
395 n = jiffies & 0xffff;
396 n -= tag & 0xffff;
397 if (n < 0)
398 n += 1<<16;
399 return n;
400}
401
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800402static struct aoeif *
403getif(struct aoetgt *t, struct net_device *nd)
404{
405 struct aoeif *p, *e;
406
407 p = t->ifs;
408 e = p + NAOEIFS;
409 for (; p < e; p++)
410 if (p->nd == nd)
411 return p;
412 return NULL;
413}
414
415static struct aoeif *
416addif(struct aoetgt *t, struct net_device *nd)
417{
418 struct aoeif *p;
419
420 p = getif(t, NULL);
421 if (!p)
422 return NULL;
423 p->nd = nd;
424 p->maxbcnt = DEFAULTBCNT;
425 p->lost = 0;
426 p->lostjumbo = 0;
427 return p;
428}
429
430static void
431ejectif(struct aoetgt *t, struct aoeif *ifp)
432{
433 struct aoeif *e;
434 ulong n;
435
436 e = t->ifs + NAOEIFS - 1;
437 n = (e - ifp) * sizeof *ifp;
438 memmove(ifp, ifp+1, n);
439 e->nd = NULL;
440}
441
442static int
443sthtith(struct aoedev *d)
444{
445 struct frame *f, *e, *nf;
446 struct sk_buff *skb;
447 struct aoetgt *ht = *d->htgt;
448
449 f = ht->frames;
450 e = f + ht->nframes;
451 for (; f < e; f++) {
452 if (f->tag == FREETAG)
453 continue;
454 nf = freeframe(d);
455 if (!nf)
456 return 0;
457 skb = nf->skb;
458 *nf = *f;
459 f->skb = skb;
460 f->tag = FREETAG;
461 nf->waited = 0;
462 ht->nout--;
463 (*d->tgt)->nout++;
464 resend(d, *d->tgt, nf);
465 }
466 /* he's clean, he's useless. take away his interfaces */
467 memset(ht->ifs, 0, sizeof ht->ifs);
468 d->htgt = NULL;
469 return 1;
470}
471
472static inline unsigned char
473ata_scnt(unsigned char *packet) {
474 struct aoe_hdr *h;
475 struct aoe_atahdr *ah;
476
477 h = (struct aoe_hdr *) packet;
478 ah = (struct aoe_atahdr *) (h+1);
479 return ah->scnt;
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void
483rexmit_timer(ulong vp)
484{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700485 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800487 struct aoetgt *t, **tt, **te;
488 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct frame *f, *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 register long timeout;
491 ulong flags, n;
492
493 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* timeout is always ~150% of the moving average */
496 timeout = d->rttavg;
497 timeout += timeout >> 1;
498
499 spin_lock_irqsave(&d->lock, flags);
500
501 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500502 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return;
504 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800505 tt = d->targets;
506 te = tt + NTARGETS;
507 for (; tt < te && *tt; tt++) {
508 t = *tt;
509 f = t->frames;
510 e = f + t->nframes;
511 for (; f < e; f++) {
512 if (f->tag == FREETAG
513 || tsince(f->tag) < timeout)
514 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 n = f->waited += timeout;
516 n /= HZ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800517 if (n > aoe_deadsecs) {
518 /* waited too long. device failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800522
523 if (n > HELPWAIT /* see if another target can help */
524 && (tt != d->targets || d->targets[1]))
525 d->htgt = tt;
526
527 if (t->nout == t->maxout) {
528 if (t->maxout > 1)
529 t->maxout--;
530 t->lastwadj = jiffies;
531 }
532
533 ifp = getif(t, f->skb->dev);
534 if (ifp && ++ifp->lost > (t->nframes << 1)
535 && (ifp != t->ifs || t->ifs[1].nd)) {
536 ejectif(t, ifp);
537 ifp = NULL;
538 }
539
540 if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
541 && ifp && ++ifp->lostjumbo > (t->nframes << 1)
542 && ifp->maxbcnt != DEFAULTBCNT) {
543 printk(KERN_INFO
544 "aoe: e%ld.%d: "
545 "too many lost jumbo on "
Harvey Harrison411c41e2008-11-25 00:40:37 -0800546 "%s:%pm - "
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800547 "falling back to %d frames.\n",
548 d->aoemajor, d->aoeminor,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800549 ifp->nd->name, t->addr,
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800550 DEFAULTBCNT);
551 ifp->maxbcnt = 0;
552 }
553 resend(d, t, f);
554 }
555
556 /* window check */
557 if (t->nout == t->maxout
558 && t->maxout < t->nframes
559 && (jiffies - t->lastwadj)/HZ > 10) {
560 t->maxout++;
561 t->lastwadj = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800564
David S. Millere9bb8fb2008-09-21 22:36:49 -0700565 if (!skb_queue_empty(&d->sendq)) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800566 n = d->rttavg <<= 1;
567 if (n > MAXTIMER)
568 d->rttavg = MAXTIMER;
569 }
570
571 if (d->flags & DEVFL_KICKME || d->htgt) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400572 d->flags &= ~DEVFL_KICKME;
573 aoecmd_work(d);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
David S. Millere9bb8fb2008-09-21 22:36:49 -0700576 __skb_queue_head_init(&queue);
577 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 d->timer.expires = jiffies + TIMERTICK;
580 add_timer(&d->timer);
581
582 spin_unlock_irqrestore(&d->lock, flags);
583
David S. Millere9bb8fb2008-09-21 22:36:49 -0700584 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800587/* enters with d->lock held */
588void
589aoecmd_work(struct aoedev *d)
590{
591 struct buf *buf;
592loop:
593 if (d->htgt && !sthtith(d))
594 return;
595 if (d->inprocess == NULL) {
596 if (list_empty(&d->bufq))
597 return;
598 buf = container_of(d->bufq.next, struct buf, bufs);
599 list_del(d->bufq.next);
600 d->inprocess = buf;
601 }
602 if (aoecmd_ata_rw(d))
603 goto loop;
604}
605
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500606/* this function performs work that has been deferred until sleeping is OK
607 */
608void
David Howellsc4028952006-11-22 14:57:56 +0000609aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500610{
David Howellsc4028952006-11-22 14:57:56 +0000611 struct aoedev *d = container_of(work, struct aoedev, work);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500612
613 if (d->flags & DEVFL_GDALLOC)
614 aoeblk_gdalloc(d);
615
616 if (d->flags & DEVFL_NEWSIZE) {
617 struct block_device *bd;
618 unsigned long flags;
619 u64 ssize;
620
Tejun Heo80795ae2008-08-25 19:56:07 +0900621 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500622 bd = bdget_disk(d->gd, 0);
623
624 if (bd) {
625 mutex_lock(&bd->bd_inode->i_mutex);
626 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
627 mutex_unlock(&bd->bd_inode->i_mutex);
628 bdput(bd);
629 }
630 spin_lock_irqsave(&d->lock, flags);
631 d->flags |= DEVFL_UP;
632 d->flags &= ~DEVFL_NEWSIZE;
633 spin_unlock_irqrestore(&d->lock, flags);
634 }
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800638ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 u64 ssize;
641 u16 n;
642
643 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700644 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700647 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (n & (1<<10)) { /* bit 10: LBA 48 */
650 d->flags |= DEVFL_EXT;
651
652 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700653 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* set as in ide-disk.c:init_idedisk_capacity */
656 d->geo.cylinders = ssize;
657 d->geo.cylinders /= (255 * 63);
658 d->geo.heads = 255;
659 d->geo.sectors = 63;
660 } else {
661 d->flags &= ~DEVFL_EXT;
662
663 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700664 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700667 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
668 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
669 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500671
672 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800673 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800674 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
675 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500676 d->aoemajor, d->aoeminor,
677 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 d->ssize = ssize;
679 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800680 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
681 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900683 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500684 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800685 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500686 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static void
691calc_rttavg(struct aoedev *d, int rtt)
692{
693 register long n;
694
695 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400696 if (n < 0) {
697 n = -rtt;
698 if (n < MINTIMER)
699 n = MINTIMER;
700 else if (n > MAXTIMER)
701 n = MAXTIMER;
702 d->mintimer += (n - d->mintimer) >> 1;
703 } else if (n < d->mintimer)
704 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 else if (n > MAXTIMER)
706 n = MAXTIMER;
707
708 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
709 n -= d->rttavg;
710 d->rttavg += n >> 2;
711}
712
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800713static struct aoetgt *
714gettgt(struct aoedev *d, char *addr)
715{
716 struct aoetgt **t, **e;
717
718 t = d->targets;
719 e = t + NTARGETS;
720 for (; t < e && *t; t++)
721 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
722 return *t;
723 return NULL;
724}
725
726static inline void
Linus Torvalds03054de2008-02-08 09:42:46 -0800727diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800728{
729 unsigned long n_sect = bio->bi_size >> 9;
730 const int rw = bio_data_dir(bio);
Jens Axboe28f13702008-05-07 10:15:46 +0200731 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +0900732 int cpu;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800733
Tejun Heo074a7ac2008-08-25 19:56:14 +0900734 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200735 part = disk_map_sector_rcu(disk, sector);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200736
Tejun Heo074a7ac2008-08-25 19:56:14 +0900737 part_stat_inc(cpu, part, ios[rw]);
738 part_stat_add(cpu, part, ticks[rw], duration);
739 part_stat_add(cpu, part, sectors[rw], n_sect);
740 part_stat_add(cpu, part, io_ticks, duration);
Tejun Heoc9959052008-08-25 19:47:21 +0900741
Tejun Heo074a7ac2008-08-25 19:56:14 +0900742 part_stat_unlock();
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745void
746aoecmd_ata_rsp(struct sk_buff *skb)
747{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700748 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct aoedev *d;
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400750 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct aoe_atahdr *ahin, *ahout;
752 struct frame *f;
753 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800754 struct aoetgt *t;
755 struct aoeif *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 register long n;
757 ulong flags;
758 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700759 u16 aoemajor;
760
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700761 hin = (struct aoe_hdr *) skb_mac_header(skb);
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700762 aoemajor = get_unaligned_be16(&hin->major);
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700763 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (d == NULL) {
765 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
766 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700767 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 aoechr_error(ebuf);
769 return;
770 }
771
772 spin_lock_irqsave(&d->lock, flags);
773
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700774 n = get_unaligned_be32(&hin->tag);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800775 t = gettgt(d, hin->src);
776 if (t == NULL) {
Harvey Harrison411c41e2008-11-25 00:40:37 -0800777 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
778 d->aoemajor, d->aoeminor, hin->src);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800779 spin_unlock_irqrestore(&d->lock, flags);
780 return;
781 }
782 f = getframe(t, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -0400784 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 spin_unlock_irqrestore(&d->lock, flags);
786 snprintf(ebuf, sizeof ebuf,
787 "%15s e%d.%d tag=%08x@%08lx\n",
788 "unexpected rsp",
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700789 get_unaligned_be16(&hin->major),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 hin->minor,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700791 get_unaligned_be32(&hin->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 jiffies);
793 aoechr_error(ebuf);
794 return;
795 }
796
797 calc_rttavg(d, tsince(f->tag));
798
799 ahin = (struct aoe_atahdr *) (hin+1);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700800 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400801 ahout = (struct aoe_atahdr *) (hout+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 buf = f->buf;
803
804 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400805 printk(KERN_ERR
Ed L. Cashin1d759812008-02-08 04:20:08 -0800806 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ahout->cmdstat, ahin->cmdstat,
808 d->aoemajor, d->aoeminor);
809 if (buf)
810 buf->flags |= BUFFL_FAIL;
811 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800812 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
813 d->htgt = NULL;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400814 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 switch (ahout->cmdstat) {
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200816 case ATA_CMD_PIO_READ:
817 case ATA_CMD_PIO_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400819 printk(KERN_ERR
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800820 "aoe: %s. skb->len=%d need=%ld\n",
821 "runt data size in read", skb->len, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* fail frame f? just returning will rexmit. */
823 spin_unlock_irqrestore(&d->lock, flags);
824 return;
825 }
826 memcpy(f->bufaddr, ahin+1, n);
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200827 case ATA_CMD_PIO_WRITE:
828 case ATA_CMD_PIO_WRITE_EXT:
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800829 ifp = getif(t, skb->dev);
830 if (ifp) {
831 ifp->lost = 0;
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400832 if (n > DEFAULTBCNT)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800833 ifp->lostjumbo = 0;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400834 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800835 if (f->bcnt -= n) {
836 f->lba += n >> 9;
837 f->bufaddr += n;
838 resend(d, t, f);
839 goto xmit;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200842 case ATA_CMD_ID_ATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400844 printk(KERN_INFO
845 "aoe: runt data size in ataid. skb->len=%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400846 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 spin_unlock_irqrestore(&d->lock, flags);
848 return;
849 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800850 ataid_complete(d, t, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400853 printk(KERN_INFO
854 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400855 ahout->cmdstat,
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700856 get_unaligned_be16(&hin->major),
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400857 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 }
860
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800861 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
Linus Torvalds03054de2008-02-08 09:42:46 -0800862 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800863 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
864 bio_endio(buf->bio, n);
865 mempool_free(buf, d->bufpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868 f->buf = NULL;
869 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800870 t->nout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 aoecmd_work(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800873xmit:
David S. Millere9bb8fb2008-09-21 22:36:49 -0700874 __skb_queue_head_init(&queue);
875 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700878 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881void
882aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
883{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700884 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David S. Millere9bb8fb2008-09-21 22:36:49 -0700886 __skb_queue_head_init(&queue);
887 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
888 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800891struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892aoecmd_ata_id(struct aoedev *d)
893{
894 struct aoe_hdr *h;
895 struct aoe_atahdr *ah;
896 struct frame *f;
897 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800898 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400900 f = freeframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800901 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800903
904 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400907 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700908 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800910 skb_put(skb, sizeof *h + sizeof *ah);
911 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800912 f->tag = aoehdr_atainit(d, t, h);
913 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* set up ata header */
917 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200918 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ah->lba3 = 0xa0;
920
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800921 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500923 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400926 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800929static struct aoetgt *
930addtgt(struct aoedev *d, char *addr, ulong nframes)
931{
932 struct aoetgt *t, **tt, **te;
933 struct frame *f, *e;
934
935 tt = d->targets;
936 te = tt + NTARGETS;
937 for (; tt < te && *tt; tt++)
938 ;
939
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800940 if (tt == te) {
941 printk(KERN_INFO
942 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800943 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800944 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800945 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
946 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800947 if (!t || !f) {
948 kfree(f);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800949 kfree(t);
Ed L. Cashin578c4aa2008-02-08 04:20:09 -0800950 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800951 return NULL;
952 }
953
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800954 t->nframes = nframes;
955 t->frames = f;
956 e = f + nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800957 for (; f < e; f++)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800958 f->tag = FREETAG;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800959 memcpy(t->addr, addr, sizeof t->addr);
960 t->ifp = t->ifs;
961 t->maxout = t->nframes;
962 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965void
966aoecmd_cfg_rsp(struct sk_buff *skb)
967{
968 struct aoedev *d;
969 struct aoe_hdr *h;
970 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800971 struct aoetgt *t;
972 struct aoeif *ifp;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700973 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct sk_buff *sl;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400975 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700977 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ch = (struct aoe_cfghdr *) (h+1);
979
980 /*
981 * Enough people have their dip switches set backwards to
982 * warrant a loud message for this special case.
983 */
Harvey Harrison823ed722008-07-04 09:28:32 +0200984 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400986 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400987 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return;
989 }
990
991 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700992 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400993 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700994 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return;
996 }
997
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400998 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -0800999 if (n > aoe_maxout) /* keep it reasonable */
1000 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001002 d = aoedev_by_sysminor_m(sysminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (d == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001004 printk(KERN_INFO "aoe: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
1006 }
1007
1008 spin_lock_irqsave(&d->lock, flags);
1009
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001010 t = gettgt(d, h->src);
1011 if (!t) {
1012 t = addtgt(d, h->src, n);
1013 if (!t) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001014 spin_unlock_irqrestore(&d->lock, flags);
1015 return;
1016 }
1017 }
1018 ifp = getif(t, skb->dev);
1019 if (!ifp) {
1020 ifp = addif(t, skb->dev);
1021 if (!ifp) {
1022 printk(KERN_INFO
1023 "aoe: device addif failure; "
1024 "too many interfaces?\n");
1025 spin_unlock_irqrestore(&d->lock, flags);
1026 return;
1027 }
1028 }
1029 if (ifp->maxbcnt) {
1030 n = ifp->nd->mtu;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001031 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1032 n /= 512;
1033 if (n > ch->scnt)
1034 n = ch->scnt;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001035 n = n ? n * 512 : DEFAULTBCNT;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001036 if (n != ifp->maxbcnt) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001037 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -08001038 "aoe: e%ld.%d: setting %d%s%s:%pm\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001039 d->aoemajor, d->aoeminor, n,
1040 " byte data frames on ", ifp->nd->name,
Harvey Harrison411c41e2008-11-25 00:40:37 -08001041 t->addr);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001042 ifp->maxbcnt = n;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001043 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001044 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001045
1046 /* don't change users' perspective */
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001047 if (d->nopen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_unlock_irqrestore(&d->lock, flags);
1049 return;
1050 }
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -07001051 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001053 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 spin_unlock_irqrestore(&d->lock, flags);
1056
David S. Millere9bb8fb2008-09-21 22:36:49 -07001057 if (sl) {
1058 struct sk_buff_head queue;
1059 __skb_queue_head_init(&queue);
1060 __skb_queue_tail(&queue, sl);
1061 aoenet_xmit(&queue);
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001065void
1066aoecmd_cleanslate(struct aoedev *d)
1067{
1068 struct aoetgt **t, **te;
1069 struct aoeif *p, *e;
1070
1071 d->mintimer = MINTIMER;
1072
1073 t = d->targets;
1074 te = t + NTARGETS;
1075 for (; t < te && *t; t++) {
1076 (*t)->maxout = (*t)->nframes;
1077 p = (*t)->ifs;
1078 e = p + NAOEIFS;
1079 for (; p < e; p++) {
1080 p->lostjumbo = 0;
1081 p->lost = 0;
1082 p->maxbcnt = DEFAULTBCNT;
1083 }
1084 }
1085}