blob: 9aefbe3957ca6d8e9ed899c3918aa6db044de234 [file] [log] [blame]
Ed Cashinfea05a22012-10-04 17:16:38 -07001/* Copyright (c) 2012 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/hdreg.h>
10#include <linux/blkdev.h>
11#include <linux/skbuff.h>
12#include <linux/netdevice.h>
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050013#include <linux/genhd.h>
Ed L. Cashin68e0d422008-02-08 04:20:00 -080014#include <linux/moduleparam.h>
Ed Cashin896831f2012-10-04 17:16:21 -070015#include <linux/workqueue.h>
16#include <linux/kthread.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070017#include <net/net_namespace.h>
Ed L. Cashin475172f2005-09-29 12:47:40 -040018#include <asm/unaligned.h>
Ed Cashin896831f2012-10-04 17:16:21 -070019#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "aoe.h"
21
Ed Cashin896831f2012-10-04 17:16:21 -070022#define MAXIOC (8192) /* default meant to avoid most soft lockups */
23
24static void ktcomplete(struct frame *, struct sk_buff *);
25
Ed Cashin69cf2d852012-10-04 17:16:23 -070026static struct buf *nextbuf(struct aoedev *);
27
Ed L. Cashinb751e8b2006-09-20 14:36:50 -040028static int aoe_deadsecs = 60 * 3;
29module_param(aoe_deadsecs, int, 0644);
30MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Ed L. Cashin7df620d2008-02-08 04:20:07 -080032static int aoe_maxout = 16;
33module_param(aoe_maxout, int, 0644);
34MODULE_PARM_DESC(aoe_maxout,
35 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
36
Ed Cashin896831f2012-10-04 17:16:21 -070037static wait_queue_head_t ktiowq;
38static struct ktstate kts;
39
40/* io completion queue */
41static struct {
42 struct list_head head;
43 spinlock_t lock;
44} iocq;
45
Ed L. Cashin68e0d422008-02-08 04:20:00 -080046static struct sk_buff *
Ed L. Cashine407a7f2006-09-20 14:36:49 -040047new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct sk_buff *skb;
50
51 skb = alloc_skb(len, GFP_ATOMIC);
52 if (skb) {
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070053 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070054 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 skb->protocol = __constant_htons(ETH_P_AOE);
Ed Cashin8babe8c2012-09-19 15:46:39 +000056 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58 return skb;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static struct frame *
Ed Cashin3a0c40d2012-12-17 16:03:43 -080062getframe_deferred(struct aoedev *d, u32 tag)
63{
64 struct list_head *head, *pos, *nx;
65 struct frame *f;
66
67 head = &d->rexmitq;
68 list_for_each_safe(pos, nx, head) {
69 f = list_entry(pos, struct frame, head);
70 if (f->tag == tag) {
71 list_del(pos);
72 return f;
73 }
74 }
75 return NULL;
76}
77
78static struct frame *
Ed Cashin64a80f52012-10-04 17:16:33 -070079getframe(struct aoedev *d, u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Ed Cashin896831f2012-10-04 17:16:21 -070081 struct frame *f;
82 struct list_head *head, *pos, *nx;
83 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Ed Cashin896831f2012-10-04 17:16:21 -070085 n = tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -070086 head = &d->factive[n];
Ed Cashin896831f2012-10-04 17:16:21 -070087 list_for_each_safe(pos, nx, head) {
88 f = list_entry(pos, struct frame, head);
89 if (f->tag == tag) {
90 list_del(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return f;
Ed Cashin896831f2012-10-04 17:16:21 -070092 }
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return NULL;
95}
96
97/*
98 * Leave the top bit clear so we have tagspace for userland.
99 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
100 * This driver reserves tag -1 to mean "unused frame."
101 */
102static int
Ed Cashin64a80f52012-10-04 17:16:33 -0700103newtag(struct aoedev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 register ulong n;
106
107 n = jiffies & 0xffff;
Ed Cashin64a80f52012-10-04 17:16:33 -0700108 return n |= (++d->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Ed Cashin896831f2012-10-04 17:16:21 -0700111static u32
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800112aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Ed Cashin64a80f52012-10-04 17:16:33 -0700114 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800116 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
117 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700118 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700120 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 h->minor = d->aoeminor;
122 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700123 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return host_tag;
126}
127
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400128static inline void
129put_lba(struct aoe_atahdr *ah, sector_t lba)
130{
131 ah->lba0 = lba;
132 ah->lba1 = lba >>= 8;
133 ah->lba2 = lba >>= 8;
134 ah->lba3 = lba >>= 8;
135 ah->lba4 = lba >>= 8;
136 ah->lba5 = lba >>= 8;
137}
138
Ed Cashin3f0f0132012-10-04 17:16:27 -0700139static struct aoeif *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800140ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Ed Cashin3f0f0132012-10-04 17:16:27 -0700142 struct aoeif *ifp;
143
144 ifp = t->ifp;
145 ifp++;
146 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
147 ifp = t->ifs;
148 if (ifp->nd == NULL)
149 return NULL;
150 return t->ifp = ifp;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800151}
152
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800153static void
154skb_pool_put(struct aoedev *d, struct sk_buff *skb)
155{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700156 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800157}
158
159static struct sk_buff *
160skb_pool_get(struct aoedev *d)
161{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700162 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800163
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800164 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb02008-09-21 22:36:49 -0700165 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800166 return skb;
167 }
David S. Millere9bb8fb02008-09-21 22:36:49 -0700168 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
169 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800170 return skb;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700171
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800172 return NULL;
173}
174
Ed Cashin896831f2012-10-04 17:16:21 -0700175void
176aoe_freetframe(struct frame *f)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800177{
Ed Cashin896831f2012-10-04 17:16:21 -0700178 struct aoetgt *t;
179
180 t = f->t;
181 f->buf = NULL;
182 f->bv = NULL;
183 f->r_skb = NULL;
184 list_add(&f->head, &t->ffree);
185}
186
187static struct frame *
188newtframe(struct aoedev *d, struct aoetgt *t)
189{
190 struct frame *f;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800191 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700192 struct list_head *pos;
193
194 if (list_empty(&t->ffree)) {
195 if (t->falloc >= NSKBPOOLMAX*2)
196 return NULL;
197 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
198 if (f == NULL)
199 return NULL;
200 t->falloc++;
201 f->t = t;
202 } else {
203 pos = t->ffree.next;
204 list_del(pos);
205 f = list_entry(pos, struct frame, head);
206 }
207
208 skb = f->skb;
209 if (skb == NULL) {
210 f->skb = skb = new_skb(ETH_ZLEN);
211 if (!skb) {
212bail: aoe_freetframe(f);
213 return NULL;
214 }
215 }
216
217 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
218 skb = skb_pool_get(d);
219 if (skb == NULL)
220 goto bail;
221 skb_pool_put(d, f->skb);
222 f->skb = skb;
223 }
224
225 skb->truesize -= skb->data_len;
226 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
227 skb_trim(skb, 0);
228 return f;
229}
230
231static struct frame *
232newframe(struct aoedev *d)
233{
234 struct frame *f;
235 struct aoetgt *t, **tt;
236 int totout = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800237
238 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
239 printk(KERN_ERR "aoe: NULL TARGETS!\n");
240 return NULL;
241 }
Ed Cashin896831f2012-10-04 17:16:21 -0700242 tt = d->tgt; /* last used target */
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800243 for (;;) {
Ed Cashin896831f2012-10-04 17:16:21 -0700244 tt++;
245 if (tt >= &d->targets[NTARGETS] || !*tt)
246 tt = d->targets;
247 t = *tt;
248 totout += t->nout;
249 if (t->nout < t->maxout
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800250 && t != d->htgt
Ed Cashin896831f2012-10-04 17:16:21 -0700251 && t->ifp->nd) {
252 f = newtframe(d, t);
253 if (f) {
Ed Cashin896831f2012-10-04 17:16:21 -0700254 ifrotate(t);
Ed Cashin3f0f0132012-10-04 17:16:27 -0700255 d->tgt = tt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800256 return f;
257 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800258 }
Ed Cashin896831f2012-10-04 17:16:21 -0700259 if (tt == d->tgt) /* we've looped and found nada */
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800260 break;
Ed Cashin896831f2012-10-04 17:16:21 -0700261 }
262 if (totout == 0) {
263 d->kicked++;
264 d->flags |= DEVFL_KICKME;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800265 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800266 return NULL;
267}
268
Ed Cashin3d5b0602012-10-04 17:16:20 -0700269static void
270skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
271{
272 int frag = 0;
273 ulong fcnt;
274loop:
275 fcnt = bv->bv_len - (off - bv->bv_offset);
276 if (fcnt > cnt)
277 fcnt = cnt;
278 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
279 cnt -= fcnt;
280 if (cnt <= 0)
281 return;
282 bv++;
283 off = bv->bv_offset;
284 goto loop;
285}
286
Ed Cashin896831f2012-10-04 17:16:21 -0700287static void
288fhash(struct frame *f)
289{
Ed Cashin64a80f52012-10-04 17:16:33 -0700290 struct aoedev *d = f->t->d;
Ed Cashin896831f2012-10-04 17:16:21 -0700291 u32 n;
292
293 n = f->tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -0700294 list_add_tail(&f->head, &d->factive[n]);
Ed Cashin896831f2012-10-04 17:16:21 -0700295}
296
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800297static int
298aoecmd_ata_rw(struct aoedev *d)
299{
300 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct aoe_hdr *h;
302 struct aoe_atahdr *ah;
303 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800304 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700306 struct sk_buff_head queue;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700307 ulong bcnt, fbcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 char writebit, extbit;
309
310 writebit = 0x10;
311 extbit = 0x4;
312
Ed Cashin69cf2d852012-10-04 17:16:23 -0700313 buf = nextbuf(d);
314 if (buf == NULL)
315 return 0;
Ed Cashin896831f2012-10-04 17:16:21 -0700316 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800317 if (f == NULL)
318 return 0;
319 t = *d->tgt;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700320 bcnt = d->maxbcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800321 if (bcnt == 0)
322 bcnt = DEFAULTBCNT;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700323 if (bcnt > buf->resid)
324 bcnt = buf->resid;
325 fbcnt = bcnt;
326 f->bv = buf->bv;
327 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
328 do {
329 if (fbcnt < buf->bv_resid) {
330 buf->bv_resid -= fbcnt;
331 buf->resid -= fbcnt;
332 break;
333 }
334 fbcnt -= buf->bv_resid;
335 buf->resid -= buf->bv_resid;
336 if (buf->resid == 0) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700337 d->ip.buf = NULL;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700338 break;
339 }
340 buf->bv++;
341 buf->bv_resid = buf->bv->bv_len;
342 WARN_ON(buf->bv_resid == 0);
343 } while (fbcnt);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400346 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700347 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800349 skb_put(skb, sizeof *h + sizeof *ah);
350 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800351 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -0700352 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800353 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 f->waited = 0;
355 f->buf = buf;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400356 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800357 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* set up ata header */
360 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800361 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (d->flags & DEVFL_EXT) {
363 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else {
365 extbit = 0;
366 ah->lba3 &= 0x0f;
367 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (bio_data_dir(buf->bio) == WRITE) {
Ed Cashin3d5b0602012-10-04 17:16:20 -0700370 skb_fillup(skb, f->bv, f->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400372 skb->len += bcnt;
373 skb->data_len = bcnt;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700374 skb->truesize += bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800375 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800377 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200381 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* mark all tracking fields and load out */
384 buf->nframesout += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 buf->sector += bcnt >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800387 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400388 skb = skb_clone(skb, GFP_ATOMIC);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700389 if (skb) {
390 __skb_queue_head_init(&queue);
391 __skb_queue_tail(&queue, skb);
392 aoenet_xmit(&queue);
393 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800394 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500397/* some callers cannot sleep, and they can call this function,
398 * transmitting the packets later, when interrupts are on
399 */
David S. Millere9bb8fb02008-09-21 22:36:49 -0700400static void
401aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500402{
403 struct aoe_hdr *h;
404 struct aoe_cfghdr *ch;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700405 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500406 struct net_device *ifp;
407
Eric Dumazet840a1852010-10-29 01:15:29 +0000408 rcu_read_lock();
409 for_each_netdev_rcu(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500410 dev_hold(ifp);
411 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700412 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500413
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400414 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500415 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400416 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700417 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500418 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800419 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400420 skb->dev = ifp;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700421 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700422 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500423 memset(h, 0, sizeof *h + sizeof *ch);
424
425 memset(h->dst, 0xff, sizeof h->dst);
426 memcpy(h->src, ifp->dev_addr, sizeof h->src);
427 h->type = __constant_cpu_to_be16(ETH_P_AOE);
428 h->verfl = AOE_HVER;
429 h->major = cpu_to_be16(aoemajor);
430 h->minor = aoeminor;
431 h->cmd = AOECMD_CFG;
432
Pavel Emelianov7562f872007-05-03 15:13:45 -0700433cont:
434 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500435 }
Eric Dumazet840a1852010-10-29 01:15:29 +0000436 rcu_read_unlock();
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void
Ed Cashin896831f2012-10-04 17:16:21 -0700440resend(struct aoedev *d, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700443 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400445 struct aoe_atahdr *ah;
Ed Cashin896831f2012-10-04 17:16:21 -0700446 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 char buf[128];
448 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Ed Cashin896831f2012-10-04 17:16:21 -0700450 t = f->t;
Ed Cashin64a80f52012-10-04 17:16:33 -0700451 n = newtag(d);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400452 skb = f->skb;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700453 if (ifrotate(t) == NULL) {
454 /* probably can't happen, but set it up to fail anyway */
455 pr_info("aoe: resend: no interfaces to rotate to.\n");
456 ktcomplete(f, NULL);
457 return;
458 }
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700459 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400460 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800461
462 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800463 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800464 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800465 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800466 aoechr_error(buf);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 f->tag = n;
Ed Cashin896831f2012-10-04 17:16:21 -0700469 fhash(f);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700470 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800471 memcpy(h->dst, t->addr, sizeof h->dst);
472 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800474 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400475 skb = skb_clone(skb, GFP_ATOMIC);
476 if (skb == NULL)
477 return;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700478 __skb_queue_head_init(&queue);
479 __skb_queue_tail(&queue, skb);
480 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483static int
Ed Cashin896831f2012-10-04 17:16:21 -0700484tsince(u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 int n;
487
488 n = jiffies & 0xffff;
489 n -= tag & 0xffff;
490 if (n < 0)
491 n += 1<<16;
492 return n;
493}
494
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800495static struct aoeif *
496getif(struct aoetgt *t, struct net_device *nd)
497{
498 struct aoeif *p, *e;
499
500 p = t->ifs;
501 e = p + NAOEIFS;
502 for (; p < e; p++)
503 if (p->nd == nd)
504 return p;
505 return NULL;
506}
507
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800508static void
509ejectif(struct aoetgt *t, struct aoeif *ifp)
510{
511 struct aoeif *e;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700512 struct net_device *nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800513 ulong n;
514
Ed Cashin1b86fda2012-10-04 17:16:34 -0700515 nd = ifp->nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800516 e = t->ifs + NAOEIFS - 1;
517 n = (e - ifp) * sizeof *ifp;
518 memmove(ifp, ifp+1, n);
519 e->nd = NULL;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700520 dev_put(nd);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800521}
522
523static int
524sthtith(struct aoedev *d)
525{
Ed Cashin896831f2012-10-04 17:16:21 -0700526 struct frame *f, *nf;
527 struct list_head *nx, *pos, *head;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800528 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700529 struct aoetgt *ht = d->htgt;
530 int i;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800531
Ed Cashin896831f2012-10-04 17:16:21 -0700532 for (i = 0; i < NFACTIVE; i++) {
Ed Cashin64a80f52012-10-04 17:16:33 -0700533 head = &d->factive[i];
Ed Cashin896831f2012-10-04 17:16:21 -0700534 list_for_each_safe(pos, nx, head) {
535 f = list_entry(pos, struct frame, head);
Ed Cashin64a80f52012-10-04 17:16:33 -0700536 if (f->t != ht)
537 continue;
538
Ed Cashin896831f2012-10-04 17:16:21 -0700539 nf = newframe(d);
540 if (!nf)
541 return 0;
542
543 /* remove frame from active list */
544 list_del(pos);
545
546 /* reassign all pertinent bits to new outbound frame */
547 skb = nf->skb;
548 nf->skb = f->skb;
549 nf->buf = f->buf;
550 nf->bcnt = f->bcnt;
551 nf->lba = f->lba;
552 nf->bv = f->bv;
553 nf->bv_off = f->bv_off;
554 nf->waited = 0;
555 f->skb = skb;
556 aoe_freetframe(f);
557 ht->nout--;
558 nf->t->nout++;
559 resend(d, nf);
560 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800561 }
Ed Cashin3f0f0132012-10-04 17:16:27 -0700562 /* We've cleaned up the outstanding so take away his
563 * interfaces so he won't be used. We should remove him from
564 * the target array here, but cleaning up a target is
565 * involved. PUNT!
566 */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800567 memset(ht->ifs, 0, sizeof ht->ifs);
568 d->htgt = NULL;
569 return 1;
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800573rexmit_deferred(struct aoedev *d)
574{
575 struct aoetgt *t;
576 struct frame *f;
577 struct list_head *pos, *nx, *head;
578
579 head = &d->rexmitq;
580 list_for_each_safe(pos, nx, head) {
581 f = list_entry(pos, struct frame, head);
582 t = f->t;
583 if (t->nout >= t->maxout)
584 continue;
585 list_del(pos);
586 t->nout++;
587 resend(d, f);
588 }
589}
590
591static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592rexmit_timer(ulong vp)
593{
594 struct aoedev *d;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800595 struct aoetgt *t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800596 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700597 struct frame *f;
598 struct list_head *head, *pos, *nx;
599 LIST_HEAD(flist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 register long timeout;
601 ulong flags, n;
Ed Cashin896831f2012-10-04 17:16:21 -0700602 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Ed Cashin0d555ec2012-12-17 16:03:47 -0800606 spin_lock_irqsave(&d->lock, flags);
607
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800608 /* timeout based on observed timings and variations */
609 timeout = 2 * d->rttavg >> RTTSCALE;
610 timeout += 8 * d->rttdev >> RTTDSCALE;
611 if (timeout == 0)
612 timeout = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500615 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
617 }
Ed Cashin896831f2012-10-04 17:16:21 -0700618
619 /* collect all frames to rexmit into flist */
Ed Cashin64a80f52012-10-04 17:16:33 -0700620 for (i = 0; i < NFACTIVE; i++) {
621 head = &d->factive[i];
622 list_for_each_safe(pos, nx, head) {
623 f = list_entry(pos, struct frame, head);
624 if (tsince(f->tag) < timeout)
625 break; /* end of expired frames */
626 /* move to flist for later processing */
627 list_move_tail(pos, &flist);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800628 }
Ed Cashin64a80f52012-10-04 17:16:33 -0700629 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700630
Ed Cashin896831f2012-10-04 17:16:21 -0700631 /* process expired frames */
632 while (!list_empty(&flist)) {
633 pos = flist.next;
634 f = list_entry(pos, struct frame, head);
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800635 n = f->waited += tsince(f->tag);
Ed Cashin896831f2012-10-04 17:16:21 -0700636 n /= HZ;
637 if (n > aoe_deadsecs) {
638 /* Waited too long. Device failure.
639 * Hang all frames on first hash bucket for downdev
640 * to clean up.
641 */
Ed Cashin64a80f52012-10-04 17:16:33 -0700642 list_splice(&flist, &d->factive[0]);
Ed Cashin896831f2012-10-04 17:16:21 -0700643 aoedev_downdev(d);
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800644 goto out;
Ed Cashin896831f2012-10-04 17:16:21 -0700645 }
Ed Cashin896831f2012-10-04 17:16:21 -0700646
647 t = f->t;
Ed Cashind54d35a2012-10-04 17:16:29 -0700648 if (n > aoe_deadsecs/2)
649 d->htgt = t; /* see if another target can help */
650
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800651 if (t->maxout != 1) {
652 t->ssthresh = t->maxout / 2;
653 t->maxout = 1;
Ed Cashin896831f2012-10-04 17:16:21 -0700654 }
655
656 ifp = getif(t, f->skb->dev);
657 if (ifp && ++ifp->lost > (t->nframes << 1)
658 && (ifp != t->ifs || t->ifs[1].nd)) {
659 ejectif(t, ifp);
660 ifp = NULL;
661 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800662 list_move_tail(pos, &d->rexmitq);
663 t->nout--;
Ed Cashin896831f2012-10-04 17:16:21 -0700664 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800665 rexmit_deferred(d);
Ed Cashin896831f2012-10-04 17:16:21 -0700666
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800667out:
Ed Cashin69cf2d852012-10-04 17:16:23 -0700668 if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400669 d->flags &= ~DEVFL_KICKME;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700670 d->blkq->request_fn(d->blkq);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 d->timer.expires = jiffies + TIMERTICK;
674 add_timer(&d->timer);
675
676 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Ed Cashin69cf2d852012-10-04 17:16:23 -0700679static unsigned long
680rqbiocnt(struct request *r)
681{
682 struct bio *bio;
683 unsigned long n = 0;
684
685 __rq_for_each_bio(bio, r)
686 n++;
687 return n;
688}
689
690/* This can be removed if we are certain that no users of the block
691 * layer will ever use zero-count pages in bios. Otherwise we have to
692 * protect against the put_page sometimes done by the network layer.
693 *
694 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
695 * discussion.
696 *
697 * We cannot use get_page in the workaround, because it insists on a
698 * positive page count as a precondition. So we use _count directly.
699 */
700static void
701bio_pageinc(struct bio *bio)
702{
703 struct bio_vec *bv;
704 struct page *page;
705 int i;
706
707 bio_for_each_segment(bv, bio, i) {
708 page = bv->bv_page;
709 /* Non-zero page count for non-head members of
710 * compound pages is no longer allowed by the kernel,
711 * but this has never been seen here.
712 */
713 if (unlikely(PageCompound(page)))
714 if (compound_trans_head(page) != page) {
715 pr_crit("page tail used for block I/O\n");
716 BUG();
717 }
718 atomic_inc(&page->_count);
719 }
720}
721
722static void
723bio_pagedec(struct bio *bio)
724{
725 struct bio_vec *bv;
726 int i;
727
728 bio_for_each_segment(bv, bio, i)
729 atomic_dec(&bv->bv_page->_count);
730}
731
732static void
733bufinit(struct buf *buf, struct request *rq, struct bio *bio)
734{
735 struct bio_vec *bv;
736
737 memset(buf, 0, sizeof(*buf));
738 buf->rq = rq;
739 buf->bio = bio;
740 buf->resid = bio->bi_size;
741 buf->sector = bio->bi_sector;
742 bio_pageinc(bio);
743 buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
744 buf->bv_resid = bv->bv_len;
745 WARN_ON(buf->bv_resid == 0);
746}
747
748static struct buf *
749nextbuf(struct aoedev *d)
750{
751 struct request *rq;
752 struct request_queue *q;
753 struct buf *buf;
754 struct bio *bio;
755
756 q = d->blkq;
757 if (q == NULL)
758 return NULL; /* initializing */
759 if (d->ip.buf)
760 return d->ip.buf;
761 rq = d->ip.rq;
762 if (rq == NULL) {
763 rq = blk_peek_request(q);
764 if (rq == NULL)
765 return NULL;
766 blk_start_request(rq);
767 d->ip.rq = rq;
768 d->ip.nxbio = rq->bio;
769 rq->special = (void *) rqbiocnt(rq);
770 }
771 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
772 if (buf == NULL) {
773 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
774 return NULL;
775 }
776 bio = d->ip.nxbio;
777 bufinit(buf, rq, bio);
778 bio = bio->bi_next;
779 d->ip.nxbio = bio;
780 if (bio == NULL)
781 d->ip.rq = NULL;
782 return d->ip.buf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800785/* enters with d->lock held */
786void
787aoecmd_work(struct aoedev *d)
788{
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800789 if (d->htgt && !sthtith(d))
790 return;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800791 rexmit_deferred(d);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700792 while (aoecmd_ata_rw(d))
793 ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800794}
795
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500796/* this function performs work that has been deferred until sleeping is OK
797 */
798void
David Howellsc4028952006-11-22 14:57:56 +0000799aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500800{
David Howellsc4028952006-11-22 14:57:56 +0000801 struct aoedev *d = container_of(work, struct aoedev, work);
Ed Cashinb21faa22012-10-04 17:16:35 -0700802 struct block_device *bd;
803 u64 ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500804
805 if (d->flags & DEVFL_GDALLOC)
806 aoeblk_gdalloc(d);
807
808 if (d->flags & DEVFL_NEWSIZE) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900809 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500810 bd = bdget_disk(d->gd, 0);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500811 if (bd) {
812 mutex_lock(&bd->bd_inode->i_mutex);
813 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
814 mutex_unlock(&bd->bd_inode->i_mutex);
815 bdput(bd);
816 }
Ed Cashinb21faa22012-10-04 17:16:35 -0700817 spin_lock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500818 d->flags |= DEVFL_UP;
819 d->flags &= ~DEVFL_NEWSIZE;
Ed Cashinb21faa22012-10-04 17:16:35 -0700820 spin_unlock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500821 }
822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824static void
Ed Cashin667be1e2012-12-17 16:03:42 -0800825ata_ident_fixstring(u16 *id, int ns)
826{
827 u16 s;
828
829 while (ns-- > 0) {
830 s = *id;
831 *id++ = s >> 8 | s << 8;
832 }
833}
834
835static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800836ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 u64 ssize;
839 u16 n;
840
841 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700842 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700845 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (n & (1<<10)) { /* bit 10: LBA 48 */
848 d->flags |= DEVFL_EXT;
849
850 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700851 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* set as in ide-disk.c:init_idedisk_capacity */
854 d->geo.cylinders = ssize;
855 d->geo.cylinders /= (255 * 63);
856 d->geo.heads = 255;
857 d->geo.sectors = 63;
858 } else {
859 d->flags &= ~DEVFL_EXT;
860
861 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700862 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700865 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
866 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
867 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500869
Ed Cashin667be1e2012-12-17 16:03:42 -0800870 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
871 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
872 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
873 memcpy(d->ident, id, sizeof(d->ident));
874
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500875 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800876 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800877 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
878 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500879 d->aoemajor, d->aoeminor,
880 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 d->ssize = ssize;
882 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800883 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900886 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500887 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800888 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500889 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800894calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 register long n;
897
898 n = rtt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800900 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
901 n -= d->rttavg >> RTTSCALE;
902 d->rttavg += n;
903 if (n < 0)
904 n = -n;
905 n -= d->rttdev >> RTTDSCALE;
906 d->rttdev += n;
907
908 if (!t || t->maxout >= t->nframes)
909 return;
910 if (t->maxout < t->ssthresh)
911 t->maxout += 1;
912 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
913 t->maxout += 1;
914 t->next_cwnd = t->maxout;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800918static struct aoetgt *
919gettgt(struct aoedev *d, char *addr)
920{
921 struct aoetgt **t, **e;
922
923 t = d->targets;
924 e = t + NTARGETS;
925 for (; t < e && *t; t++)
926 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
927 return *t;
928 return NULL;
929}
930
Ed Cashin3d5b0602012-10-04 17:16:20 -0700931static void
Ed Cashin896831f2012-10-04 17:16:21 -0700932bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700933{
934 ulong fcnt;
935 char *p;
936 int soff = 0;
937loop:
938 fcnt = bv->bv_len - (off - bv->bv_offset);
939 if (fcnt > cnt)
940 fcnt = cnt;
941 p = page_address(bv->bv_page) + off;
942 skb_copy_bits(skb, soff, p, fcnt);
943 soff += fcnt;
944 cnt -= fcnt;
945 if (cnt <= 0)
946 return;
947 bv++;
948 off = bv->bv_offset;
949 goto loop;
950}
951
Ed Cashin69cf2d852012-10-04 17:16:23 -0700952void
953aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
954{
955 struct bio *bio;
956 int bok;
957 struct request_queue *q;
958
959 q = d->blkq;
960 if (rq == d->ip.rq)
961 d->ip.rq = NULL;
962 do {
963 bio = rq->bio;
964 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
965 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
966
967 /* cf. http://lkml.org/lkml/2006/10/31/28 */
968 if (!fastfail)
Ed Cashin11cfb6f2012-11-08 19:17:15 -0500969 __blk_run_queue(q);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700970}
971
972static void
973aoe_end_buf(struct aoedev *d, struct buf *buf)
974{
975 struct request *rq;
976 unsigned long n;
977
978 if (buf == d->ip.buf)
979 d->ip.buf = NULL;
980 rq = buf->rq;
981 bio_pagedec(buf->bio);
982 mempool_free(buf, d->bufpool);
983 n = (unsigned long) rq->special;
984 rq->special = (void *) --n;
985 if (n == 0)
986 aoe_end_request(d, rq, 0);
987}
988
Ed Cashin3d5b0602012-10-04 17:16:20 -0700989static void
Ed Cashin896831f2012-10-04 17:16:21 -0700990ktiocomplete(struct frame *f)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700991{
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400992 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct aoe_atahdr *ahin, *ahout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct buf *buf;
Ed Cashin896831f2012-10-04 17:16:21 -0700995 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800996 struct aoetgt *t;
997 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700998 struct aoedev *d;
999 long n;
1000
1001 if (f == NULL)
1002 return;
1003
1004 t = f->t;
1005 d = t->d;
1006
1007 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1008 ahout = (struct aoe_atahdr *) (hout+1);
1009 buf = f->buf;
1010 skb = f->r_skb;
1011 if (skb == NULL)
1012 goto noskb; /* just fail the buf. */
1013
1014 hin = (struct aoe_hdr *) skb->data;
1015 skb_pull(skb, sizeof(*hin));
1016 ahin = (struct aoe_atahdr *) skb->data;
1017 skb_pull(skb, sizeof(*ahin));
1018 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1019 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1020 ahout->cmdstat, ahin->cmdstat,
1021 d->aoemajor, d->aoeminor);
Ed Cashina04b41c2012-12-17 16:03:39 -08001022noskb: if (buf)
Ed Cashin69cf2d852012-10-04 17:16:23 -07001023 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001024 goto badrsp;
1025 }
1026
1027 n = ahout->scnt << 9;
1028 switch (ahout->cmdstat) {
1029 case ATA_CMD_PIO_READ:
1030 case ATA_CMD_PIO_READ_EXT:
1031 if (skb->len < n) {
1032 pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1033 skb->len, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001034 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001035 break;
1036 }
1037 bvcpy(f->bv, f->bv_off, skb, n);
1038 case ATA_CMD_PIO_WRITE:
1039 case ATA_CMD_PIO_WRITE_EXT:
1040 spin_lock_irq(&d->lock);
1041 ifp = getif(t, skb->dev);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001042 if (ifp)
Ed Cashin896831f2012-10-04 17:16:21 -07001043 ifp->lost = 0;
Ed Cashin896831f2012-10-04 17:16:21 -07001044 if (d->htgt == t) /* I'll help myself, thank you. */
1045 d->htgt = NULL;
1046 spin_unlock_irq(&d->lock);
1047 break;
1048 case ATA_CMD_ID_ATA:
1049 if (skb->len < 512) {
1050 pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1051 skb->len);
1052 break;
1053 }
1054 if (skb_linearize(skb))
1055 break;
1056 spin_lock_irq(&d->lock);
1057 ataid_complete(d, t, skb->data);
1058 spin_unlock_irq(&d->lock);
1059 break;
1060 default:
1061 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1062 ahout->cmdstat,
1063 be16_to_cpu(get_unaligned(&hin->major)),
1064 hin->minor);
1065 }
1066badrsp:
1067 spin_lock_irq(&d->lock);
1068
1069 aoe_freetframe(f);
1070
Ed Cashin69cf2d852012-10-04 17:16:23 -07001071 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1072 aoe_end_buf(d, buf);
Ed Cashin896831f2012-10-04 17:16:21 -07001073
Ed Cashin69cf2d852012-10-04 17:16:23 -07001074 aoecmd_work(d);
1075
1076 spin_unlock_irq(&d->lock);
1077 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001078 dev_kfree_skb(skb);
1079}
1080
1081/* Enters with iocq.lock held.
1082 * Returns true iff responses needing processing remain.
1083 */
1084static int
1085ktio(void)
1086{
1087 struct frame *f;
1088 struct list_head *pos;
1089 int i;
1090
1091 for (i = 0; ; ++i) {
1092 if (i == MAXIOC)
1093 return 1;
1094 if (list_empty(&iocq.head))
1095 return 0;
1096 pos = iocq.head.next;
1097 list_del(pos);
1098 spin_unlock_irq(&iocq.lock);
1099 f = list_entry(pos, struct frame, head);
1100 ktiocomplete(f);
1101 spin_lock_irq(&iocq.lock);
1102 }
1103}
1104
1105static int
1106kthread(void *vp)
1107{
1108 struct ktstate *k;
1109 DECLARE_WAITQUEUE(wait, current);
1110 int more;
1111
1112 k = vp;
1113 current->flags |= PF_NOFREEZE;
1114 set_user_nice(current, -10);
1115 complete(&k->rendez); /* tell spawner we're running */
1116 do {
1117 spin_lock_irq(k->lock);
1118 more = k->fn();
1119 if (!more) {
1120 add_wait_queue(k->waitq, &wait);
1121 __set_current_state(TASK_INTERRUPTIBLE);
1122 }
1123 spin_unlock_irq(k->lock);
1124 if (!more) {
1125 schedule();
1126 remove_wait_queue(k->waitq, &wait);
1127 } else
1128 cond_resched();
1129 } while (!kthread_should_stop());
1130 complete(&k->rendez); /* tell spawner we're stopping */
1131 return 0;
1132}
1133
Ed Cashineb086ec2012-10-04 17:16:25 -07001134void
Ed Cashin896831f2012-10-04 17:16:21 -07001135aoe_ktstop(struct ktstate *k)
1136{
1137 kthread_stop(k->task);
1138 wait_for_completion(&k->rendez);
1139}
1140
Ed Cashineb086ec2012-10-04 17:16:25 -07001141int
Ed Cashin896831f2012-10-04 17:16:21 -07001142aoe_ktstart(struct ktstate *k)
1143{
1144 struct task_struct *task;
1145
1146 init_completion(&k->rendez);
1147 task = kthread_run(kthread, k, k->name);
1148 if (task == NULL || IS_ERR(task))
1149 return -ENOMEM;
1150 k->task = task;
1151 wait_for_completion(&k->rendez); /* allow kthread to start */
1152 init_completion(&k->rendez); /* for waiting for exit later */
1153 return 0;
1154}
1155
1156/* pass it off to kthreads for processing */
1157static void
1158ktcomplete(struct frame *f, struct sk_buff *skb)
1159{
1160 ulong flags;
1161
1162 f->r_skb = skb;
1163 spin_lock_irqsave(&iocq.lock, flags);
1164 list_add_tail(&f->head, &iocq.head);
1165 spin_unlock_irqrestore(&iocq.lock, flags);
1166 wake_up(&ktiowq);
1167}
1168
1169struct sk_buff *
1170aoecmd_ata_rsp(struct sk_buff *skb)
1171{
1172 struct aoedev *d;
1173 struct aoe_hdr *h;
1174 struct frame *f;
Ed Cashin896831f2012-10-04 17:16:21 -07001175 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ulong flags;
1177 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -07001178 u16 aoemajor;
1179
Ed Cashin896831f2012-10-04 17:16:21 -07001180 h = (struct aoe_hdr *) skb->data;
1181 aoemajor = be16_to_cpu(get_unaligned(&h->major));
Ed Cashin0c966212012-10-04 17:16:40 -07001182 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (d == NULL) {
1184 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1185 "for unknown device %d.%d\n",
Ed Cashin896831f2012-10-04 17:16:21 -07001186 aoemajor, h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 aoechr_error(ebuf);
Ed Cashin896831f2012-10-04 17:16:21 -07001188 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
1191 spin_lock_irqsave(&d->lock, flags);
1192
Ed Cashin896831f2012-10-04 17:16:21 -07001193 n = be32_to_cpu(get_unaligned(&h->tag));
Ed Cashin64a80f52012-10-04 17:16:33 -07001194 f = getframe(d, n);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001195 if (f) {
1196 calc_rttavg(d, f->t, tsince(n));
1197 f->t->nout--;
1198 } else {
1199 f = getframe_deferred(d, n);
1200 if (f) {
1201 calc_rttavg(d, NULL, tsince(n));
1202 } else {
1203 calc_rttavg(d, NULL, tsince(n));
1204 spin_unlock_irqrestore(&d->lock, flags);
1205 aoedev_put(d);
1206 snprintf(ebuf, sizeof(ebuf),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001207 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001208 "unexpected rsp",
1209 get_unaligned_be16(&h->major),
1210 h->minor,
1211 get_unaligned_be32(&h->tag),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001212 jiffies,
1213 h->src,
1214 h->dst);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001215 aoechr_error(ebuf);
1216 return skb;
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001222
1223 ktcomplete(f, skb);
1224
1225 /*
1226 * Note here that we do not perform an aoedev_put, as we are
1227 * leaving this reference for the ktio to release.
1228 */
1229 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232void
1233aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1234{
David S. Millere9bb8fb02008-09-21 22:36:49 -07001235 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
David S. Millere9bb8fb02008-09-21 22:36:49 -07001237 __skb_queue_head_init(&queue);
1238 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1239 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
Ed Cashina04b41c2012-12-17 16:03:39 -08001241
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001242struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243aoecmd_ata_id(struct aoedev *d)
1244{
1245 struct aoe_hdr *h;
1246 struct aoe_atahdr *ah;
1247 struct frame *f;
1248 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001249 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Ed Cashin896831f2012-10-04 17:16:21 -07001251 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001252 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001254
1255 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -04001258 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001259 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -08001261 skb_put(skb, sizeof *h + sizeof *ah);
1262 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001263 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -07001264 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001265 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* set up ata header */
1269 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +02001270 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ah->lba3 = 0xa0;
1272
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001273 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001275 d->rttavg = RTTAVG_INIT;
1276 d->rttdev = RTTDEV_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001279 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
Ed Cashina04b41c2012-12-17 16:03:39 -08001281
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001282static struct aoetgt *
1283addtgt(struct aoedev *d, char *addr, ulong nframes)
1284{
1285 struct aoetgt *t, **tt, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001286
1287 tt = d->targets;
1288 te = tt + NTARGETS;
1289 for (; tt < te && *tt; tt++)
1290 ;
1291
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001292 if (tt == te) {
1293 printk(KERN_INFO
1294 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001295 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001296 }
Ed Cashin896831f2012-10-04 17:16:21 -07001297 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1298 if (!t) {
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001299 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -08001300 return NULL;
1301 }
1302
Ed Cashin896831f2012-10-04 17:16:21 -07001303 d->ntargets++;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001304 t->nframes = nframes;
Ed Cashin896831f2012-10-04 17:16:21 -07001305 t->d = d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001306 memcpy(t->addr, addr, sizeof t->addr);
1307 t->ifp = t->ifs;
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001308 aoecmd_wreset(t);
Ed Cashin896831f2012-10-04 17:16:21 -07001309 INIT_LIST_HEAD(&t->ffree);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001310 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001311}
1312
Ed Cashin3f0f0132012-10-04 17:16:27 -07001313static void
1314setdbcnt(struct aoedev *d)
1315{
1316 struct aoetgt **t, **e;
1317 int bcnt = 0;
1318
1319 t = d->targets;
1320 e = t + NTARGETS;
1321 for (; t < e && *t; t++)
1322 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1323 bcnt = (*t)->minbcnt;
1324 if (bcnt != d->maxbcnt) {
1325 d->maxbcnt = bcnt;
1326 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1327 d->aoemajor, d->aoeminor, bcnt);
1328 }
1329}
1330
1331static void
1332setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1333{
1334 struct aoedev *d;
1335 struct aoeif *p, *e;
1336 int minbcnt;
1337
1338 d = t->d;
1339 minbcnt = bcnt;
1340 p = t->ifs;
1341 e = p + NAOEIFS;
1342 for (; p < e; p++) {
1343 if (p->nd == NULL)
1344 break; /* end of the valid interfaces */
1345 if (p->nd == nd) {
1346 p->bcnt = bcnt; /* we're updating */
1347 nd = NULL;
1348 } else if (minbcnt > p->bcnt)
1349 minbcnt = p->bcnt; /* find the min interface */
1350 }
1351 if (nd) {
1352 if (p == e) {
1353 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1354 return;
1355 }
Ed Cashin1b86fda2012-10-04 17:16:34 -07001356 dev_hold(nd);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001357 p->nd = nd;
1358 p->bcnt = bcnt;
1359 }
1360 t->minbcnt = minbcnt;
1361 setdbcnt(d);
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364void
1365aoecmd_cfg_rsp(struct sk_buff *skb)
1366{
1367 struct aoedev *d;
1368 struct aoe_hdr *h;
1369 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001370 struct aoetgt *t;
Ed Cashin0c966212012-10-04 17:16:40 -07001371 ulong flags, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 struct sk_buff *sl;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001373 struct sk_buff_head queue;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001374 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Ed Cashin69cf2d852012-10-04 17:16:23 -07001376 sl = NULL;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001377 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ch = (struct aoe_cfghdr *) (h+1);
1379
1380 /*
1381 * Enough people have their dip switches set backwards to
1382 * warrant a loud message for this special case.
1383 */
Harvey Harrison823ed722008-07-04 09:28:32 +02001384 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001386 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -04001387 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return;
1389 }
Ed Cashin7159e962012-10-04 17:16:44 -07001390 if (aoemajor == 0xffff) {
1391 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
Ed Cashin0c966212012-10-04 17:16:40 -07001392 aoemajor, (int) h->minor);
Ed Cashin65833032012-10-04 17:16:32 -07001393 return;
1394 }
Ed Cashin7159e962012-10-04 17:16:44 -07001395 if (h->minor == 0xff) {
1396 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1397 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return;
1399 }
1400
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001401 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001402 if (n > aoe_maxout) /* keep it reasonable */
1403 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Ed Cashin7159e962012-10-04 17:16:44 -07001405 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1406 if (d == NULL) {
1407 pr_info("aoe: device allocation failure\n");
1408 return;
1409 }
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 spin_lock_irqsave(&d->lock, flags);
1412
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001413 t = gettgt(d, h->src);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001414 if (t) {
1415 t->nframes = n;
1416 if (n < t->maxout)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001417 aoecmd_wreset(t);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001418 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001419 t = addtgt(d, h->src, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001420 if (!t)
1421 goto bail;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001422 }
Ed Cashin3f0f0132012-10-04 17:16:27 -07001423 n = skb->dev->mtu;
1424 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1425 n /= 512;
1426 if (n > ch->scnt)
1427 n = ch->scnt;
1428 n = n ? n * 512 : DEFAULTBCNT;
1429 setifbcnt(t, skb->dev, n);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001430
1431 /* don't change users' perspective */
Ed Cashin69cf2d852012-10-04 17:16:23 -07001432 if (d->nopen == 0) {
1433 d->fw_ver = be16_to_cpu(ch->fwver);
1434 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Ed Cashin69cf2d852012-10-04 17:16:23 -07001436bail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001438 aoedev_put(d);
David S. Millere9bb8fb02008-09-21 22:36:49 -07001439 if (sl) {
David S. Millere9bb8fb02008-09-21 22:36:49 -07001440 __skb_queue_head_init(&queue);
1441 __skb_queue_tail(&queue, sl);
1442 aoenet_xmit(&queue);
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001446void
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001447aoecmd_wreset(struct aoetgt *t)
1448{
1449 t->maxout = 1;
1450 t->ssthresh = t->nframes / 2;
1451 t->next_cwnd = t->nframes;
1452}
1453
1454void
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001455aoecmd_cleanslate(struct aoedev *d)
1456{
1457 struct aoetgt **t, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001458
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001459 d->rttavg = RTTAVG_INIT;
1460 d->rttdev = RTTDEV_INIT;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001461 d->maxbcnt = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001462
1463 t = d->targets;
1464 te = t + NTARGETS;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001465 for (; t < te && *t; t++)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001466 aoecmd_wreset(*t);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001467}
Ed Cashin896831f2012-10-04 17:16:21 -07001468
Ed Cashin69cf2d852012-10-04 17:16:23 -07001469void
1470aoe_failbuf(struct aoedev *d, struct buf *buf)
1471{
1472 if (buf == NULL)
1473 return;
1474 buf->resid = 0;
1475 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1476 if (buf->nframesout == 0)
1477 aoe_end_buf(d, buf);
1478}
1479
1480void
1481aoe_flush_iocq(void)
Ed Cashin896831f2012-10-04 17:16:21 -07001482{
1483 struct frame *f;
1484 struct aoedev *d;
1485 LIST_HEAD(flist);
1486 struct list_head *pos;
1487 struct sk_buff *skb;
1488 ulong flags;
1489
1490 spin_lock_irqsave(&iocq.lock, flags);
1491 list_splice_init(&iocq.head, &flist);
1492 spin_unlock_irqrestore(&iocq.lock, flags);
1493 while (!list_empty(&flist)) {
1494 pos = flist.next;
1495 list_del(pos);
1496 f = list_entry(pos, struct frame, head);
1497 d = f->t->d;
1498 skb = f->r_skb;
1499 spin_lock_irqsave(&d->lock, flags);
1500 if (f->buf) {
1501 f->buf->nframesout--;
1502 aoe_failbuf(d, f->buf);
1503 }
1504 aoe_freetframe(f);
1505 spin_unlock_irqrestore(&d->lock, flags);
1506 dev_kfree_skb(skb);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001507 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001508 }
1509}
1510
1511int __init
1512aoecmd_init(void)
1513{
1514 INIT_LIST_HEAD(&iocq.head);
1515 spin_lock_init(&iocq.lock);
1516 init_waitqueue_head(&ktiowq);
1517 kts.name = "aoe_ktio";
1518 kts.fn = ktio;
1519 kts.waitq = &ktiowq;
1520 kts.lock = &iocq.lock;
1521 return aoe_ktstart(&kts);
1522}
1523
1524void
1525aoecmd_exit(void)
1526{
1527 aoe_ktstop(&kts);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001528 aoe_flush_iocq();
Ed Cashin896831f2012-10-04 17:16:21 -07001529}