blob: a99220ad62628b117ed9f089a37034afdb479143 [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. Millere9bb8fb2008-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. Millere9bb8fb2008-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. Millere9bb8fb2008-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. Millere9bb8fb2008-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. Millere9bb8fb2008-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) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800390 do_gettimeofday(&f->sent);
391 f->sent_jiffs = (u32) jiffies;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700392 __skb_queue_head_init(&queue);
393 __skb_queue_tail(&queue, skb);
394 aoenet_xmit(&queue);
395 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800396 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500399/* some callers cannot sleep, and they can call this function,
400 * transmitting the packets later, when interrupts are on
401 */
David S. Millere9bb8fb2008-09-21 22:36:49 -0700402static void
403aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500404{
405 struct aoe_hdr *h;
406 struct aoe_cfghdr *ch;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700407 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500408 struct net_device *ifp;
409
Eric Dumazet840a1852010-10-29 01:15:29 +0000410 rcu_read_lock();
411 for_each_netdev_rcu(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500412 dev_hold(ifp);
413 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700414 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500415
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400416 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500417 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400418 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700419 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500420 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800421 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400422 skb->dev = ifp;
David S. Millere9bb8fb2008-09-21 22:36:49 -0700423 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700424 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500425 memset(h, 0, sizeof *h + sizeof *ch);
426
427 memset(h->dst, 0xff, sizeof h->dst);
428 memcpy(h->src, ifp->dev_addr, sizeof h->src);
429 h->type = __constant_cpu_to_be16(ETH_P_AOE);
430 h->verfl = AOE_HVER;
431 h->major = cpu_to_be16(aoemajor);
432 h->minor = aoeminor;
433 h->cmd = AOECMD_CFG;
434
Pavel Emelianov7562f872007-05-03 15:13:45 -0700435cont:
436 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500437 }
Eric Dumazet840a1852010-10-29 01:15:29 +0000438 rcu_read_unlock();
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void
Ed Cashin896831f2012-10-04 17:16:21 -0700442resend(struct aoedev *d, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700445 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400447 struct aoe_atahdr *ah;
Ed Cashin896831f2012-10-04 17:16:21 -0700448 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 char buf[128];
450 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Ed Cashin896831f2012-10-04 17:16:21 -0700452 t = f->t;
Ed Cashin64a80f52012-10-04 17:16:33 -0700453 n = newtag(d);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400454 skb = f->skb;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700455 if (ifrotate(t) == NULL) {
456 /* probably can't happen, but set it up to fail anyway */
457 pr_info("aoe: resend: no interfaces to rotate to.\n");
458 ktcomplete(f, NULL);
459 return;
460 }
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700461 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400462 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800463
464 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800465 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800466 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800467 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800468 aoechr_error(buf);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 f->tag = n;
Ed Cashin896831f2012-10-04 17:16:21 -0700471 fhash(f);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700472 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800473 memcpy(h->dst, t->addr, sizeof h->dst);
474 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800476 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400477 skb = skb_clone(skb, GFP_ATOMIC);
478 if (skb == NULL)
479 return;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800480 do_gettimeofday(&f->sent);
481 f->sent_jiffs = (u32) jiffies;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700482 __skb_queue_head_init(&queue);
483 __skb_queue_tail(&queue, skb);
484 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static int
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800488tsince_hr(struct frame *f)
489{
490 struct timeval now;
491 int n;
492
493 do_gettimeofday(&now);
494 n = now.tv_usec - f->sent.tv_usec;
495 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
496
497 if (n < 0)
498 n = -n;
499
500 /* For relatively long periods, use jiffies to avoid
501 * discrepancies caused by updates to the system time.
502 *
503 * On system with HZ of 1000, 32-bits is over 49 days
504 * worth of jiffies, or over 71 minutes worth of usecs.
505 *
506 * Jiffies overflow is handled by subtraction of unsigned ints:
507 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
508 * $3 = 4
509 * (gdb)
510 */
511 if (n > USEC_PER_SEC / 4) {
512 n = ((u32) jiffies) - f->sent_jiffs;
513 n *= USEC_PER_SEC / HZ;
514 }
515
516 return n;
517}
518
519static int
Ed Cashin896831f2012-10-04 17:16:21 -0700520tsince(u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int n;
523
524 n = jiffies & 0xffff;
525 n -= tag & 0xffff;
526 if (n < 0)
527 n += 1<<16;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800528 return jiffies_to_usecs(n + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800531static struct aoeif *
532getif(struct aoetgt *t, struct net_device *nd)
533{
534 struct aoeif *p, *e;
535
536 p = t->ifs;
537 e = p + NAOEIFS;
538 for (; p < e; p++)
539 if (p->nd == nd)
540 return p;
541 return NULL;
542}
543
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800544static void
545ejectif(struct aoetgt *t, struct aoeif *ifp)
546{
547 struct aoeif *e;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700548 struct net_device *nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800549 ulong n;
550
Ed Cashin1b86fda2012-10-04 17:16:34 -0700551 nd = ifp->nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800552 e = t->ifs + NAOEIFS - 1;
553 n = (e - ifp) * sizeof *ifp;
554 memmove(ifp, ifp+1, n);
555 e->nd = NULL;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700556 dev_put(nd);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800557}
558
559static int
560sthtith(struct aoedev *d)
561{
Ed Cashin896831f2012-10-04 17:16:21 -0700562 struct frame *f, *nf;
563 struct list_head *nx, *pos, *head;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800564 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700565 struct aoetgt *ht = d->htgt;
566 int i;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800567
Ed Cashin896831f2012-10-04 17:16:21 -0700568 for (i = 0; i < NFACTIVE; i++) {
Ed Cashin64a80f52012-10-04 17:16:33 -0700569 head = &d->factive[i];
Ed Cashin896831f2012-10-04 17:16:21 -0700570 list_for_each_safe(pos, nx, head) {
571 f = list_entry(pos, struct frame, head);
Ed Cashin64a80f52012-10-04 17:16:33 -0700572 if (f->t != ht)
573 continue;
574
Ed Cashin896831f2012-10-04 17:16:21 -0700575 nf = newframe(d);
576 if (!nf)
577 return 0;
578
579 /* remove frame from active list */
580 list_del(pos);
581
582 /* reassign all pertinent bits to new outbound frame */
583 skb = nf->skb;
584 nf->skb = f->skb;
585 nf->buf = f->buf;
586 nf->bcnt = f->bcnt;
587 nf->lba = f->lba;
588 nf->bv = f->bv;
589 nf->bv_off = f->bv_off;
590 nf->waited = 0;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800591 nf->sent_jiffs = f->sent_jiffs;
Ed Cashin896831f2012-10-04 17:16:21 -0700592 f->skb = skb;
593 aoe_freetframe(f);
594 ht->nout--;
595 nf->t->nout++;
596 resend(d, nf);
597 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800598 }
Ed Cashin3f0f0132012-10-04 17:16:27 -0700599 /* We've cleaned up the outstanding so take away his
600 * interfaces so he won't be used. We should remove him from
601 * the target array here, but cleaning up a target is
602 * involved. PUNT!
603 */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800604 memset(ht->ifs, 0, sizeof ht->ifs);
605 d->htgt = NULL;
606 return 1;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800610rexmit_deferred(struct aoedev *d)
611{
612 struct aoetgt *t;
613 struct frame *f;
614 struct list_head *pos, *nx, *head;
615
616 head = &d->rexmitq;
617 list_for_each_safe(pos, nx, head) {
618 f = list_entry(pos, struct frame, head);
619 t = f->t;
620 if (t->nout >= t->maxout)
621 continue;
622 list_del(pos);
623 t->nout++;
624 resend(d, f);
625 }
626}
627
628static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629rexmit_timer(ulong vp)
630{
631 struct aoedev *d;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800632 struct aoetgt *t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800633 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700634 struct frame *f;
635 struct list_head *head, *pos, *nx;
636 LIST_HEAD(flist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 register long timeout;
638 ulong flags, n;
Ed Cashin896831f2012-10-04 17:16:21 -0700639 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ed Cashin0d555ec2012-12-17 16:03:47 -0800643 spin_lock_irqsave(&d->lock, flags);
644
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800645 /* timeout based on observed timings and variations */
646 timeout = 2 * d->rttavg >> RTTSCALE;
647 timeout += 8 * d->rttdev >> RTTDSCALE;
648 if (timeout == 0)
649 timeout = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500652 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return;
654 }
Ed Cashin896831f2012-10-04 17:16:21 -0700655
656 /* collect all frames to rexmit into flist */
Ed Cashin64a80f52012-10-04 17:16:33 -0700657 for (i = 0; i < NFACTIVE; i++) {
658 head = &d->factive[i];
659 list_for_each_safe(pos, nx, head) {
660 f = list_entry(pos, struct frame, head);
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800661 if (tsince_hr(f) < timeout)
Ed Cashin64a80f52012-10-04 17:16:33 -0700662 break; /* end of expired frames */
663 /* move to flist for later processing */
664 list_move_tail(pos, &flist);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800665 }
Ed Cashin64a80f52012-10-04 17:16:33 -0700666 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700667
Ed Cashin896831f2012-10-04 17:16:21 -0700668 /* process expired frames */
669 while (!list_empty(&flist)) {
670 pos = flist.next;
671 f = list_entry(pos, struct frame, head);
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800672 n = f->waited += tsince_hr(f);
673 n /= USEC_PER_SEC;
Ed Cashin896831f2012-10-04 17:16:21 -0700674 if (n > aoe_deadsecs) {
675 /* Waited too long. Device failure.
676 * Hang all frames on first hash bucket for downdev
677 * to clean up.
678 */
Ed Cashin64a80f52012-10-04 17:16:33 -0700679 list_splice(&flist, &d->factive[0]);
Ed Cashin896831f2012-10-04 17:16:21 -0700680 aoedev_downdev(d);
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800681 goto out;
Ed Cashin896831f2012-10-04 17:16:21 -0700682 }
Ed Cashin896831f2012-10-04 17:16:21 -0700683
684 t = f->t;
Ed Cashind54d35a2012-10-04 17:16:29 -0700685 if (n > aoe_deadsecs/2)
686 d->htgt = t; /* see if another target can help */
687
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800688 if (t->maxout != 1) {
689 t->ssthresh = t->maxout / 2;
690 t->maxout = 1;
Ed Cashin896831f2012-10-04 17:16:21 -0700691 }
692
693 ifp = getif(t, f->skb->dev);
694 if (ifp && ++ifp->lost > (t->nframes << 1)
695 && (ifp != t->ifs || t->ifs[1].nd)) {
696 ejectif(t, ifp);
697 ifp = NULL;
698 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800699 list_move_tail(pos, &d->rexmitq);
700 t->nout--;
Ed Cashin896831f2012-10-04 17:16:21 -0700701 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800702 rexmit_deferred(d);
Ed Cashin896831f2012-10-04 17:16:21 -0700703
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800704out:
Ed Cashin69cf2d852012-10-04 17:16:23 -0700705 if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400706 d->flags &= ~DEVFL_KICKME;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700707 d->blkq->request_fn(d->blkq);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 d->timer.expires = jiffies + TIMERTICK;
711 add_timer(&d->timer);
712
713 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Ed Cashin69cf2d852012-10-04 17:16:23 -0700716static unsigned long
717rqbiocnt(struct request *r)
718{
719 struct bio *bio;
720 unsigned long n = 0;
721
722 __rq_for_each_bio(bio, r)
723 n++;
724 return n;
725}
726
727/* This can be removed if we are certain that no users of the block
728 * layer will ever use zero-count pages in bios. Otherwise we have to
729 * protect against the put_page sometimes done by the network layer.
730 *
731 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
732 * discussion.
733 *
734 * We cannot use get_page in the workaround, because it insists on a
735 * positive page count as a precondition. So we use _count directly.
736 */
737static void
738bio_pageinc(struct bio *bio)
739{
740 struct bio_vec *bv;
741 struct page *page;
742 int i;
743
744 bio_for_each_segment(bv, bio, i) {
745 page = bv->bv_page;
746 /* Non-zero page count for non-head members of
747 * compound pages is no longer allowed by the kernel,
748 * but this has never been seen here.
749 */
750 if (unlikely(PageCompound(page)))
751 if (compound_trans_head(page) != page) {
752 pr_crit("page tail used for block I/O\n");
753 BUG();
754 }
755 atomic_inc(&page->_count);
756 }
757}
758
759static void
760bio_pagedec(struct bio *bio)
761{
762 struct bio_vec *bv;
763 int i;
764
765 bio_for_each_segment(bv, bio, i)
766 atomic_dec(&bv->bv_page->_count);
767}
768
769static void
770bufinit(struct buf *buf, struct request *rq, struct bio *bio)
771{
772 struct bio_vec *bv;
773
774 memset(buf, 0, sizeof(*buf));
775 buf->rq = rq;
776 buf->bio = bio;
777 buf->resid = bio->bi_size;
778 buf->sector = bio->bi_sector;
779 bio_pageinc(bio);
780 buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
781 buf->bv_resid = bv->bv_len;
782 WARN_ON(buf->bv_resid == 0);
783}
784
785static struct buf *
786nextbuf(struct aoedev *d)
787{
788 struct request *rq;
789 struct request_queue *q;
790 struct buf *buf;
791 struct bio *bio;
792
793 q = d->blkq;
794 if (q == NULL)
795 return NULL; /* initializing */
796 if (d->ip.buf)
797 return d->ip.buf;
798 rq = d->ip.rq;
799 if (rq == NULL) {
800 rq = blk_peek_request(q);
801 if (rq == NULL)
802 return NULL;
803 blk_start_request(rq);
804 d->ip.rq = rq;
805 d->ip.nxbio = rq->bio;
806 rq->special = (void *) rqbiocnt(rq);
807 }
808 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
809 if (buf == NULL) {
810 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
811 return NULL;
812 }
813 bio = d->ip.nxbio;
814 bufinit(buf, rq, bio);
815 bio = bio->bi_next;
816 d->ip.nxbio = bio;
817 if (bio == NULL)
818 d->ip.rq = NULL;
819 return d->ip.buf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800822/* enters with d->lock held */
823void
824aoecmd_work(struct aoedev *d)
825{
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800826 if (d->htgt && !sthtith(d))
827 return;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800828 rexmit_deferred(d);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700829 while (aoecmd_ata_rw(d))
830 ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800831}
832
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500833/* this function performs work that has been deferred until sleeping is OK
834 */
835void
David Howellsc4028952006-11-22 14:57:56 +0000836aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500837{
David Howellsc4028952006-11-22 14:57:56 +0000838 struct aoedev *d = container_of(work, struct aoedev, work);
Ed Cashinb21faa22012-10-04 17:16:35 -0700839 struct block_device *bd;
840 u64 ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500841
842 if (d->flags & DEVFL_GDALLOC)
843 aoeblk_gdalloc(d);
844
845 if (d->flags & DEVFL_NEWSIZE) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900846 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500847 bd = bdget_disk(d->gd, 0);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500848 if (bd) {
849 mutex_lock(&bd->bd_inode->i_mutex);
850 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
851 mutex_unlock(&bd->bd_inode->i_mutex);
852 bdput(bd);
853 }
Ed Cashinb21faa22012-10-04 17:16:35 -0700854 spin_lock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500855 d->flags |= DEVFL_UP;
856 d->flags &= ~DEVFL_NEWSIZE;
Ed Cashinb21faa22012-10-04 17:16:35 -0700857 spin_unlock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500858 }
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861static void
Ed Cashin667be1e2012-12-17 16:03:42 -0800862ata_ident_fixstring(u16 *id, int ns)
863{
864 u16 s;
865
866 while (ns-- > 0) {
867 s = *id;
868 *id++ = s >> 8 | s << 8;
869 }
870}
871
872static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800873ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 u64 ssize;
876 u16 n;
877
878 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700879 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700882 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (n & (1<<10)) { /* bit 10: LBA 48 */
885 d->flags |= DEVFL_EXT;
886
887 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700888 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* set as in ide-disk.c:init_idedisk_capacity */
891 d->geo.cylinders = ssize;
892 d->geo.cylinders /= (255 * 63);
893 d->geo.heads = 255;
894 d->geo.sectors = 63;
895 } else {
896 d->flags &= ~DEVFL_EXT;
897
898 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700899 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700902 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
903 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
904 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500906
Ed Cashin667be1e2012-12-17 16:03:42 -0800907 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
908 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
909 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
910 memcpy(d->ident, id, sizeof(d->ident));
911
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500912 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800913 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800914 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
915 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500916 d->aoemajor, d->aoeminor,
917 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 d->ssize = ssize;
919 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800920 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900923 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500924 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800925 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500926 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800931calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 register long n;
934
935 n = rtt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800937 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
938 n -= d->rttavg >> RTTSCALE;
939 d->rttavg += n;
940 if (n < 0)
941 n = -n;
942 n -= d->rttdev >> RTTDSCALE;
943 d->rttdev += n;
944
945 if (!t || t->maxout >= t->nframes)
946 return;
947 if (t->maxout < t->ssthresh)
948 t->maxout += 1;
949 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
950 t->maxout += 1;
951 t->next_cwnd = t->maxout;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800955static struct aoetgt *
956gettgt(struct aoedev *d, char *addr)
957{
958 struct aoetgt **t, **e;
959
960 t = d->targets;
961 e = t + NTARGETS;
962 for (; t < e && *t; t++)
963 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
964 return *t;
965 return NULL;
966}
967
Ed Cashin3d5b0602012-10-04 17:16:20 -0700968static void
Ed Cashin896831f2012-10-04 17:16:21 -0700969bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700970{
971 ulong fcnt;
972 char *p;
973 int soff = 0;
974loop:
975 fcnt = bv->bv_len - (off - bv->bv_offset);
976 if (fcnt > cnt)
977 fcnt = cnt;
978 p = page_address(bv->bv_page) + off;
979 skb_copy_bits(skb, soff, p, fcnt);
980 soff += fcnt;
981 cnt -= fcnt;
982 if (cnt <= 0)
983 return;
984 bv++;
985 off = bv->bv_offset;
986 goto loop;
987}
988
Ed Cashin69cf2d852012-10-04 17:16:23 -0700989void
990aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
991{
992 struct bio *bio;
993 int bok;
994 struct request_queue *q;
995
996 q = d->blkq;
997 if (rq == d->ip.rq)
998 d->ip.rq = NULL;
999 do {
1000 bio = rq->bio;
1001 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
1002 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
1003
1004 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1005 if (!fastfail)
Ed Cashin11cfb6f2012-11-08 19:17:15 -05001006 __blk_run_queue(q);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001007}
1008
1009static void
1010aoe_end_buf(struct aoedev *d, struct buf *buf)
1011{
1012 struct request *rq;
1013 unsigned long n;
1014
1015 if (buf == d->ip.buf)
1016 d->ip.buf = NULL;
1017 rq = buf->rq;
1018 bio_pagedec(buf->bio);
1019 mempool_free(buf, d->bufpool);
1020 n = (unsigned long) rq->special;
1021 rq->special = (void *) --n;
1022 if (n == 0)
1023 aoe_end_request(d, rq, 0);
1024}
1025
Ed Cashin3d5b0602012-10-04 17:16:20 -07001026static void
Ed Cashin896831f2012-10-04 17:16:21 -07001027ktiocomplete(struct frame *f)
Ed Cashin3d5b0602012-10-04 17:16:20 -07001028{
Ed L. Cashinddec63e2006-09-20 14:36:49 -04001029 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct aoe_atahdr *ahin, *ahout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct buf *buf;
Ed Cashin896831f2012-10-04 17:16:21 -07001032 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001033 struct aoetgt *t;
1034 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -07001035 struct aoedev *d;
1036 long n;
1037
1038 if (f == NULL)
1039 return;
1040
1041 t = f->t;
1042 d = t->d;
1043
1044 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1045 ahout = (struct aoe_atahdr *) (hout+1);
1046 buf = f->buf;
1047 skb = f->r_skb;
1048 if (skb == NULL)
1049 goto noskb; /* just fail the buf. */
1050
1051 hin = (struct aoe_hdr *) skb->data;
1052 skb_pull(skb, sizeof(*hin));
1053 ahin = (struct aoe_atahdr *) skb->data;
1054 skb_pull(skb, sizeof(*ahin));
1055 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1056 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1057 ahout->cmdstat, ahin->cmdstat,
1058 d->aoemajor, d->aoeminor);
Ed Cashina04b41c2012-12-17 16:03:39 -08001059noskb: if (buf)
Ed Cashin69cf2d852012-10-04 17:16:23 -07001060 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001061 goto badrsp;
1062 }
1063
1064 n = ahout->scnt << 9;
1065 switch (ahout->cmdstat) {
1066 case ATA_CMD_PIO_READ:
1067 case ATA_CMD_PIO_READ_EXT:
1068 if (skb->len < n) {
1069 pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1070 skb->len, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001071 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001072 break;
1073 }
1074 bvcpy(f->bv, f->bv_off, skb, n);
1075 case ATA_CMD_PIO_WRITE:
1076 case ATA_CMD_PIO_WRITE_EXT:
1077 spin_lock_irq(&d->lock);
1078 ifp = getif(t, skb->dev);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001079 if (ifp)
Ed Cashin896831f2012-10-04 17:16:21 -07001080 ifp->lost = 0;
Ed Cashin896831f2012-10-04 17:16:21 -07001081 if (d->htgt == t) /* I'll help myself, thank you. */
1082 d->htgt = NULL;
1083 spin_unlock_irq(&d->lock);
1084 break;
1085 case ATA_CMD_ID_ATA:
1086 if (skb->len < 512) {
1087 pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1088 skb->len);
1089 break;
1090 }
1091 if (skb_linearize(skb))
1092 break;
1093 spin_lock_irq(&d->lock);
1094 ataid_complete(d, t, skb->data);
1095 spin_unlock_irq(&d->lock);
1096 break;
1097 default:
1098 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1099 ahout->cmdstat,
1100 be16_to_cpu(get_unaligned(&hin->major)),
1101 hin->minor);
1102 }
1103badrsp:
1104 spin_lock_irq(&d->lock);
1105
1106 aoe_freetframe(f);
1107
Ed Cashin69cf2d852012-10-04 17:16:23 -07001108 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1109 aoe_end_buf(d, buf);
Ed Cashin896831f2012-10-04 17:16:21 -07001110
Ed Cashin69cf2d852012-10-04 17:16:23 -07001111 aoecmd_work(d);
1112
1113 spin_unlock_irq(&d->lock);
1114 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001115 dev_kfree_skb(skb);
1116}
1117
1118/* Enters with iocq.lock held.
1119 * Returns true iff responses needing processing remain.
1120 */
1121static int
1122ktio(void)
1123{
1124 struct frame *f;
1125 struct list_head *pos;
1126 int i;
1127
1128 for (i = 0; ; ++i) {
1129 if (i == MAXIOC)
1130 return 1;
1131 if (list_empty(&iocq.head))
1132 return 0;
1133 pos = iocq.head.next;
1134 list_del(pos);
1135 spin_unlock_irq(&iocq.lock);
1136 f = list_entry(pos, struct frame, head);
1137 ktiocomplete(f);
1138 spin_lock_irq(&iocq.lock);
1139 }
1140}
1141
1142static int
1143kthread(void *vp)
1144{
1145 struct ktstate *k;
1146 DECLARE_WAITQUEUE(wait, current);
1147 int more;
1148
1149 k = vp;
1150 current->flags |= PF_NOFREEZE;
1151 set_user_nice(current, -10);
1152 complete(&k->rendez); /* tell spawner we're running */
1153 do {
1154 spin_lock_irq(k->lock);
1155 more = k->fn();
1156 if (!more) {
1157 add_wait_queue(k->waitq, &wait);
1158 __set_current_state(TASK_INTERRUPTIBLE);
1159 }
1160 spin_unlock_irq(k->lock);
1161 if (!more) {
1162 schedule();
1163 remove_wait_queue(k->waitq, &wait);
1164 } else
1165 cond_resched();
1166 } while (!kthread_should_stop());
1167 complete(&k->rendez); /* tell spawner we're stopping */
1168 return 0;
1169}
1170
Ed Cashineb086ec2012-10-04 17:16:25 -07001171void
Ed Cashin896831f2012-10-04 17:16:21 -07001172aoe_ktstop(struct ktstate *k)
1173{
1174 kthread_stop(k->task);
1175 wait_for_completion(&k->rendez);
1176}
1177
Ed Cashineb086ec2012-10-04 17:16:25 -07001178int
Ed Cashin896831f2012-10-04 17:16:21 -07001179aoe_ktstart(struct ktstate *k)
1180{
1181 struct task_struct *task;
1182
1183 init_completion(&k->rendez);
1184 task = kthread_run(kthread, k, k->name);
1185 if (task == NULL || IS_ERR(task))
1186 return -ENOMEM;
1187 k->task = task;
1188 wait_for_completion(&k->rendez); /* allow kthread to start */
1189 init_completion(&k->rendez); /* for waiting for exit later */
1190 return 0;
1191}
1192
1193/* pass it off to kthreads for processing */
1194static void
1195ktcomplete(struct frame *f, struct sk_buff *skb)
1196{
1197 ulong flags;
1198
1199 f->r_skb = skb;
1200 spin_lock_irqsave(&iocq.lock, flags);
1201 list_add_tail(&f->head, &iocq.head);
1202 spin_unlock_irqrestore(&iocq.lock, flags);
1203 wake_up(&ktiowq);
1204}
1205
1206struct sk_buff *
1207aoecmd_ata_rsp(struct sk_buff *skb)
1208{
1209 struct aoedev *d;
1210 struct aoe_hdr *h;
1211 struct frame *f;
Ed Cashin896831f2012-10-04 17:16:21 -07001212 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 ulong flags;
1214 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -07001215 u16 aoemajor;
1216
Ed Cashin896831f2012-10-04 17:16:21 -07001217 h = (struct aoe_hdr *) skb->data;
1218 aoemajor = be16_to_cpu(get_unaligned(&h->major));
Ed Cashin0c966212012-10-04 17:16:40 -07001219 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (d == NULL) {
1221 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1222 "for unknown device %d.%d\n",
Ed Cashin896831f2012-10-04 17:16:21 -07001223 aoemajor, h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 aoechr_error(ebuf);
Ed Cashin896831f2012-10-04 17:16:21 -07001225 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 spin_lock_irqsave(&d->lock, flags);
1229
Ed Cashin896831f2012-10-04 17:16:21 -07001230 n = be32_to_cpu(get_unaligned(&h->tag));
Ed Cashin64a80f52012-10-04 17:16:33 -07001231 f = getframe(d, n);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001232 if (f) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001233 calc_rttavg(d, f->t, tsince_hr(f));
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001234 f->t->nout--;
1235 } else {
1236 f = getframe_deferred(d, n);
1237 if (f) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001238 calc_rttavg(d, NULL, tsince_hr(f));
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001239 } else {
1240 calc_rttavg(d, NULL, tsince(n));
1241 spin_unlock_irqrestore(&d->lock, flags);
1242 aoedev_put(d);
1243 snprintf(ebuf, sizeof(ebuf),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001244 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001245 "unexpected rsp",
1246 get_unaligned_be16(&h->major),
1247 h->minor,
1248 get_unaligned_be32(&h->tag),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001249 jiffies,
1250 h->src,
1251 h->dst);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001252 aoechr_error(ebuf);
1253 return skb;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001259
1260 ktcomplete(f, skb);
1261
1262 /*
1263 * Note here that we do not perform an aoedev_put, as we are
1264 * leaving this reference for the ktio to release.
1265 */
1266 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269void
1270aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1271{
David S. Millere9bb8fb2008-09-21 22:36:49 -07001272 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
David S. Millere9bb8fb2008-09-21 22:36:49 -07001274 __skb_queue_head_init(&queue);
1275 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1276 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
Ed Cashina04b41c2012-12-17 16:03:39 -08001278
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001279struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280aoecmd_ata_id(struct aoedev *d)
1281{
1282 struct aoe_hdr *h;
1283 struct aoe_atahdr *ah;
1284 struct frame *f;
1285 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001286 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Ed Cashin896831f2012-10-04 17:16:21 -07001288 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001289 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001291
1292 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -04001295 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001296 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -08001298 skb_put(skb, sizeof *h + sizeof *ah);
1299 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001300 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -07001301 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001302 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* set up ata header */
1306 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +02001307 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ah->lba3 = 0xa0;
1309
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001310 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001312 d->rttavg = RTTAVG_INIT;
1313 d->rttdev = RTTDEV_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001316 skb = skb_clone(skb, GFP_ATOMIC);
1317 if (skb) {
1318 do_gettimeofday(&f->sent);
1319 f->sent_jiffs = (u32) jiffies;
1320 }
1321
1322 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Ed Cashina04b41c2012-12-17 16:03:39 -08001324
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001325static struct aoetgt *
1326addtgt(struct aoedev *d, char *addr, ulong nframes)
1327{
1328 struct aoetgt *t, **tt, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001329
1330 tt = d->targets;
1331 te = tt + NTARGETS;
1332 for (; tt < te && *tt; tt++)
1333 ;
1334
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001335 if (tt == te) {
1336 printk(KERN_INFO
1337 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001338 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001339 }
Ed Cashin896831f2012-10-04 17:16:21 -07001340 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1341 if (!t) {
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001342 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -08001343 return NULL;
1344 }
1345
Ed Cashin896831f2012-10-04 17:16:21 -07001346 d->ntargets++;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001347 t->nframes = nframes;
Ed Cashin896831f2012-10-04 17:16:21 -07001348 t->d = d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001349 memcpy(t->addr, addr, sizeof t->addr);
1350 t->ifp = t->ifs;
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001351 aoecmd_wreset(t);
Ed Cashin896831f2012-10-04 17:16:21 -07001352 INIT_LIST_HEAD(&t->ffree);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001353 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001354}
1355
Ed Cashin3f0f0132012-10-04 17:16:27 -07001356static void
1357setdbcnt(struct aoedev *d)
1358{
1359 struct aoetgt **t, **e;
1360 int bcnt = 0;
1361
1362 t = d->targets;
1363 e = t + NTARGETS;
1364 for (; t < e && *t; t++)
1365 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1366 bcnt = (*t)->minbcnt;
1367 if (bcnt != d->maxbcnt) {
1368 d->maxbcnt = bcnt;
1369 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1370 d->aoemajor, d->aoeminor, bcnt);
1371 }
1372}
1373
1374static void
1375setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1376{
1377 struct aoedev *d;
1378 struct aoeif *p, *e;
1379 int minbcnt;
1380
1381 d = t->d;
1382 minbcnt = bcnt;
1383 p = t->ifs;
1384 e = p + NAOEIFS;
1385 for (; p < e; p++) {
1386 if (p->nd == NULL)
1387 break; /* end of the valid interfaces */
1388 if (p->nd == nd) {
1389 p->bcnt = bcnt; /* we're updating */
1390 nd = NULL;
1391 } else if (minbcnt > p->bcnt)
1392 minbcnt = p->bcnt; /* find the min interface */
1393 }
1394 if (nd) {
1395 if (p == e) {
1396 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1397 return;
1398 }
Ed Cashin1b86fda2012-10-04 17:16:34 -07001399 dev_hold(nd);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001400 p->nd = nd;
1401 p->bcnt = bcnt;
1402 }
1403 t->minbcnt = minbcnt;
1404 setdbcnt(d);
1405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407void
1408aoecmd_cfg_rsp(struct sk_buff *skb)
1409{
1410 struct aoedev *d;
1411 struct aoe_hdr *h;
1412 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001413 struct aoetgt *t;
Ed Cashin0c966212012-10-04 17:16:40 -07001414 ulong flags, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct sk_buff *sl;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001416 struct sk_buff_head queue;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001417 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Ed Cashin69cf2d852012-10-04 17:16:23 -07001419 sl = NULL;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001420 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ch = (struct aoe_cfghdr *) (h+1);
1422
1423 /*
1424 * Enough people have their dip switches set backwards to
1425 * warrant a loud message for this special case.
1426 */
Harvey Harrison823ed722008-07-04 09:28:32 +02001427 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001429 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -04001430 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return;
1432 }
Ed Cashin7159e962012-10-04 17:16:44 -07001433 if (aoemajor == 0xffff) {
1434 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
Ed Cashin0c966212012-10-04 17:16:40 -07001435 aoemajor, (int) h->minor);
Ed Cashin65833032012-10-04 17:16:32 -07001436 return;
1437 }
Ed Cashin7159e962012-10-04 17:16:44 -07001438 if (h->minor == 0xff) {
1439 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1440 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return;
1442 }
1443
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001444 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001445 if (n > aoe_maxout) /* keep it reasonable */
1446 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Ed Cashin7159e962012-10-04 17:16:44 -07001448 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1449 if (d == NULL) {
1450 pr_info("aoe: device allocation failure\n");
1451 return;
1452 }
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 spin_lock_irqsave(&d->lock, flags);
1455
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001456 t = gettgt(d, h->src);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001457 if (t) {
1458 t->nframes = n;
1459 if (n < t->maxout)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001460 aoecmd_wreset(t);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001461 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001462 t = addtgt(d, h->src, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001463 if (!t)
1464 goto bail;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001465 }
Ed Cashin3f0f0132012-10-04 17:16:27 -07001466 n = skb->dev->mtu;
1467 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1468 n /= 512;
1469 if (n > ch->scnt)
1470 n = ch->scnt;
1471 n = n ? n * 512 : DEFAULTBCNT;
1472 setifbcnt(t, skb->dev, n);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001473
1474 /* don't change users' perspective */
Ed Cashin69cf2d852012-10-04 17:16:23 -07001475 if (d->nopen == 0) {
1476 d->fw_ver = be16_to_cpu(ch->fwver);
1477 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Ed Cashin69cf2d852012-10-04 17:16:23 -07001479bail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001481 aoedev_put(d);
David S. Millere9bb8fb2008-09-21 22:36:49 -07001482 if (sl) {
David S. Millere9bb8fb2008-09-21 22:36:49 -07001483 __skb_queue_head_init(&queue);
1484 __skb_queue_tail(&queue, sl);
1485 aoenet_xmit(&queue);
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001489void
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001490aoecmd_wreset(struct aoetgt *t)
1491{
1492 t->maxout = 1;
1493 t->ssthresh = t->nframes / 2;
1494 t->next_cwnd = t->nframes;
1495}
1496
1497void
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001498aoecmd_cleanslate(struct aoedev *d)
1499{
1500 struct aoetgt **t, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001501
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001502 d->rttavg = RTTAVG_INIT;
1503 d->rttdev = RTTDEV_INIT;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001504 d->maxbcnt = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001505
1506 t = d->targets;
1507 te = t + NTARGETS;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001508 for (; t < te && *t; t++)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001509 aoecmd_wreset(*t);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001510}
Ed Cashin896831f2012-10-04 17:16:21 -07001511
Ed Cashin69cf2d852012-10-04 17:16:23 -07001512void
1513aoe_failbuf(struct aoedev *d, struct buf *buf)
1514{
1515 if (buf == NULL)
1516 return;
1517 buf->resid = 0;
1518 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1519 if (buf->nframesout == 0)
1520 aoe_end_buf(d, buf);
1521}
1522
1523void
1524aoe_flush_iocq(void)
Ed Cashin896831f2012-10-04 17:16:21 -07001525{
1526 struct frame *f;
1527 struct aoedev *d;
1528 LIST_HEAD(flist);
1529 struct list_head *pos;
1530 struct sk_buff *skb;
1531 ulong flags;
1532
1533 spin_lock_irqsave(&iocq.lock, flags);
1534 list_splice_init(&iocq.head, &flist);
1535 spin_unlock_irqrestore(&iocq.lock, flags);
1536 while (!list_empty(&flist)) {
1537 pos = flist.next;
1538 list_del(pos);
1539 f = list_entry(pos, struct frame, head);
1540 d = f->t->d;
1541 skb = f->r_skb;
1542 spin_lock_irqsave(&d->lock, flags);
1543 if (f->buf) {
1544 f->buf->nframesout--;
1545 aoe_failbuf(d, f->buf);
1546 }
1547 aoe_freetframe(f);
1548 spin_unlock_irqrestore(&d->lock, flags);
1549 dev_kfree_skb(skb);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001550 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001551 }
1552}
1553
1554int __init
1555aoecmd_init(void)
1556{
1557 INIT_LIST_HEAD(&iocq.head);
1558 spin_lock_init(&iocq.lock);
1559 init_waitqueue_head(&ktiowq);
1560 kts.name = "aoe_ktio";
1561 kts.fn = ktio;
1562 kts.waitq = &ktiowq;
1563 kts.lock = &iocq.lock;
1564 return aoe_ktstart(&kts);
1565}
1566
1567void
1568aoecmd_exit(void)
1569{
1570 aoe_ktstop(&kts);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001571 aoe_flush_iocq();
Ed Cashin896831f2012-10-04 17:16:21 -07001572}