blob: 9fe4f1865558abce9ccd7ebf1ee10a56284caf61 [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 Cashin64a80f52012-10-04 17:16:33 -070062getframe(struct aoedev *d, u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Ed Cashin896831f2012-10-04 17:16:21 -070064 struct frame *f;
65 struct list_head *head, *pos, *nx;
66 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ed Cashin896831f2012-10-04 17:16:21 -070068 n = tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -070069 head = &d->factive[n];
Ed Cashin896831f2012-10-04 17:16:21 -070070 list_for_each_safe(pos, nx, head) {
71 f = list_entry(pos, struct frame, head);
72 if (f->tag == tag) {
73 list_del(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return f;
Ed Cashin896831f2012-10-04 17:16:21 -070075 }
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return NULL;
78}
79
80/*
81 * Leave the top bit clear so we have tagspace for userland.
82 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
83 * This driver reserves tag -1 to mean "unused frame."
84 */
85static int
Ed Cashin64a80f52012-10-04 17:16:33 -070086newtag(struct aoedev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 register ulong n;
89
90 n = jiffies & 0xffff;
Ed Cashin64a80f52012-10-04 17:16:33 -070091 return n |= (++d->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Ed Cashin896831f2012-10-04 17:16:21 -070094static u32
Ed L. Cashin68e0d422008-02-08 04:20:00 -080095aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Ed Cashin64a80f52012-10-04 17:16:33 -070097 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Ed L. Cashin68e0d422008-02-08 04:20:00 -080099 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
100 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700101 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700103 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 h->minor = d->aoeminor;
105 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700106 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return host_tag;
109}
110
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400111static inline void
112put_lba(struct aoe_atahdr *ah, sector_t lba)
113{
114 ah->lba0 = lba;
115 ah->lba1 = lba >>= 8;
116 ah->lba2 = lba >>= 8;
117 ah->lba3 = lba >>= 8;
118 ah->lba4 = lba >>= 8;
119 ah->lba5 = lba >>= 8;
120}
121
Ed Cashin3f0f0132012-10-04 17:16:27 -0700122static struct aoeif *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800123ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Ed Cashin3f0f0132012-10-04 17:16:27 -0700125 struct aoeif *ifp;
126
127 ifp = t->ifp;
128 ifp++;
129 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
130 ifp = t->ifs;
131 if (ifp->nd == NULL)
132 return NULL;
133 return t->ifp = ifp;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800134}
135
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800136static void
137skb_pool_put(struct aoedev *d, struct sk_buff *skb)
138{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700139 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800140}
141
142static struct sk_buff *
143skb_pool_get(struct aoedev *d)
144{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700145 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800146
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800147 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb02008-09-21 22:36:49 -0700148 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800149 return skb;
150 }
David S. Millere9bb8fb02008-09-21 22:36:49 -0700151 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
152 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800153 return skb;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700154
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800155 return NULL;
156}
157
Ed Cashin896831f2012-10-04 17:16:21 -0700158void
159aoe_freetframe(struct frame *f)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800160{
Ed Cashin896831f2012-10-04 17:16:21 -0700161 struct aoetgt *t;
162
163 t = f->t;
164 f->buf = NULL;
165 f->bv = NULL;
166 f->r_skb = NULL;
167 list_add(&f->head, &t->ffree);
168}
169
170static struct frame *
171newtframe(struct aoedev *d, struct aoetgt *t)
172{
173 struct frame *f;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800174 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700175 struct list_head *pos;
176
177 if (list_empty(&t->ffree)) {
178 if (t->falloc >= NSKBPOOLMAX*2)
179 return NULL;
180 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
181 if (f == NULL)
182 return NULL;
183 t->falloc++;
184 f->t = t;
185 } else {
186 pos = t->ffree.next;
187 list_del(pos);
188 f = list_entry(pos, struct frame, head);
189 }
190
191 skb = f->skb;
192 if (skb == NULL) {
193 f->skb = skb = new_skb(ETH_ZLEN);
194 if (!skb) {
195bail: aoe_freetframe(f);
196 return NULL;
197 }
198 }
199
200 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
201 skb = skb_pool_get(d);
202 if (skb == NULL)
203 goto bail;
204 skb_pool_put(d, f->skb);
205 f->skb = skb;
206 }
207
208 skb->truesize -= skb->data_len;
209 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
210 skb_trim(skb, 0);
211 return f;
212}
213
214static struct frame *
215newframe(struct aoedev *d)
216{
217 struct frame *f;
218 struct aoetgt *t, **tt;
219 int totout = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800220
221 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
222 printk(KERN_ERR "aoe: NULL TARGETS!\n");
223 return NULL;
224 }
Ed Cashin896831f2012-10-04 17:16:21 -0700225 tt = d->tgt; /* last used target */
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800226 for (;;) {
Ed Cashin896831f2012-10-04 17:16:21 -0700227 tt++;
228 if (tt >= &d->targets[NTARGETS] || !*tt)
229 tt = d->targets;
230 t = *tt;
231 totout += t->nout;
232 if (t->nout < t->maxout
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800233 && t != d->htgt
Ed Cashin896831f2012-10-04 17:16:21 -0700234 && t->ifp->nd) {
235 f = newtframe(d, t);
236 if (f) {
Ed Cashin896831f2012-10-04 17:16:21 -0700237 ifrotate(t);
Ed Cashin3f0f0132012-10-04 17:16:27 -0700238 d->tgt = tt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800239 return f;
240 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800241 }
Ed Cashin896831f2012-10-04 17:16:21 -0700242 if (tt == d->tgt) /* we've looped and found nada */
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800243 break;
Ed Cashin896831f2012-10-04 17:16:21 -0700244 }
245 if (totout == 0) {
246 d->kicked++;
247 d->flags |= DEVFL_KICKME;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800248 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800249 return NULL;
250}
251
Ed Cashin3d5b0602012-10-04 17:16:20 -0700252static void
253skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
254{
255 int frag = 0;
256 ulong fcnt;
257loop:
258 fcnt = bv->bv_len - (off - bv->bv_offset);
259 if (fcnt > cnt)
260 fcnt = cnt;
261 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
262 cnt -= fcnt;
263 if (cnt <= 0)
264 return;
265 bv++;
266 off = bv->bv_offset;
267 goto loop;
268}
269
Ed Cashin896831f2012-10-04 17:16:21 -0700270static void
271fhash(struct frame *f)
272{
Ed Cashin64a80f52012-10-04 17:16:33 -0700273 struct aoedev *d = f->t->d;
Ed Cashin896831f2012-10-04 17:16:21 -0700274 u32 n;
275
276 n = f->tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -0700277 list_add_tail(&f->head, &d->factive[n]);
Ed Cashin896831f2012-10-04 17:16:21 -0700278}
279
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800280static int
281aoecmd_ata_rw(struct aoedev *d)
282{
283 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct aoe_hdr *h;
285 struct aoe_atahdr *ah;
286 struct buf *buf;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800287 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700289 struct sk_buff_head queue;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700290 ulong bcnt, fbcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 char writebit, extbit;
292
293 writebit = 0x10;
294 extbit = 0x4;
295
Ed Cashin69cf2d852012-10-04 17:16:23 -0700296 buf = nextbuf(d);
297 if (buf == NULL)
298 return 0;
Ed Cashin896831f2012-10-04 17:16:21 -0700299 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800300 if (f == NULL)
301 return 0;
302 t = *d->tgt;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700303 bcnt = d->maxbcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800304 if (bcnt == 0)
305 bcnt = DEFAULTBCNT;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700306 if (bcnt > buf->resid)
307 bcnt = buf->resid;
308 fbcnt = bcnt;
309 f->bv = buf->bv;
310 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
311 do {
312 if (fbcnt < buf->bv_resid) {
313 buf->bv_resid -= fbcnt;
314 buf->resid -= fbcnt;
315 break;
316 }
317 fbcnt -= buf->bv_resid;
318 buf->resid -= buf->bv_resid;
319 if (buf->resid == 0) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700320 d->ip.buf = NULL;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700321 break;
322 }
323 buf->bv++;
324 buf->bv_resid = buf->bv->bv_len;
325 WARN_ON(buf->bv_resid == 0);
326 } while (fbcnt);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400329 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700330 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800332 skb_put(skb, sizeof *h + sizeof *ah);
333 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800334 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -0700335 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800336 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 f->waited = 0;
338 f->buf = buf;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400339 f->bcnt = bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800340 f->lba = buf->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* set up ata header */
343 ah->scnt = bcnt >> 9;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800344 put_lba(ah, buf->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (d->flags & DEVFL_EXT) {
346 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
348 extbit = 0;
349 ah->lba3 &= 0x0f;
350 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (bio_data_dir(buf->bio) == WRITE) {
Ed Cashin3d5b0602012-10-04 17:16:20 -0700353 skb_fillup(skb, f->bv, f->bv_off, bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400355 skb->len += bcnt;
356 skb->data_len = bcnt;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700357 skb->truesize += bcnt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800358 t->wpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800360 t->rpkts++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +0200364 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* mark all tracking fields and load out */
367 buf->nframesout += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 buf->sector += bcnt >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800370 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400371 skb = skb_clone(skb, GFP_ATOMIC);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700372 if (skb) {
373 __skb_queue_head_init(&queue);
374 __skb_queue_tail(&queue, skb);
375 aoenet_xmit(&queue);
376 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800377 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500380/* some callers cannot sleep, and they can call this function,
381 * transmitting the packets later, when interrupts are on
382 */
David S. Millere9bb8fb02008-09-21 22:36:49 -0700383static void
384aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500385{
386 struct aoe_hdr *h;
387 struct aoe_cfghdr *ch;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700388 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500389 struct net_device *ifp;
390
Eric Dumazet840a1852010-10-29 01:15:29 +0000391 rcu_read_lock();
392 for_each_netdev_rcu(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500393 dev_hold(ifp);
394 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700395 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500396
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400397 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500398 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400399 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700400 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500401 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800402 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400403 skb->dev = ifp;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700404 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700405 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500406 memset(h, 0, sizeof *h + sizeof *ch);
407
408 memset(h->dst, 0xff, sizeof h->dst);
409 memcpy(h->src, ifp->dev_addr, sizeof h->src);
410 h->type = __constant_cpu_to_be16(ETH_P_AOE);
411 h->verfl = AOE_HVER;
412 h->major = cpu_to_be16(aoemajor);
413 h->minor = aoeminor;
414 h->cmd = AOECMD_CFG;
415
Pavel Emelianov7562f872007-05-03 15:13:45 -0700416cont:
417 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500418 }
Eric Dumazet840a1852010-10-29 01:15:29 +0000419 rcu_read_unlock();
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void
Ed Cashin896831f2012-10-04 17:16:21 -0700423resend(struct aoedev *d, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700426 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400428 struct aoe_atahdr *ah;
Ed Cashin896831f2012-10-04 17:16:21 -0700429 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 char buf[128];
431 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Ed Cashin896831f2012-10-04 17:16:21 -0700433 t = f->t;
Ed Cashin64a80f52012-10-04 17:16:33 -0700434 n = newtag(d);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400435 skb = f->skb;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700436 if (ifrotate(t) == NULL) {
437 /* probably can't happen, but set it up to fail anyway */
438 pr_info("aoe: resend: no interfaces to rotate to.\n");
439 ktcomplete(f, NULL);
440 return;
441 }
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700442 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400443 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800444
445 snprintf(buf, sizeof buf,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800446 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800447 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
Harvey Harrison411c41e2008-11-25 00:40:37 -0800448 h->src, h->dst, t->nout);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800449 aoechr_error(buf);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 f->tag = n;
Ed Cashin896831f2012-10-04 17:16:21 -0700452 fhash(f);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700453 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800454 memcpy(h->dst, t->addr, sizeof h->dst);
455 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800457 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400458 skb = skb_clone(skb, GFP_ATOMIC);
459 if (skb == NULL)
460 return;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700461 __skb_queue_head_init(&queue);
462 __skb_queue_tail(&queue, skb);
463 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static int
Ed Cashin896831f2012-10-04 17:16:21 -0700467tsince(u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int n;
470
471 n = jiffies & 0xffff;
472 n -= tag & 0xffff;
473 if (n < 0)
474 n += 1<<16;
475 return n;
476}
477
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800478static struct aoeif *
479getif(struct aoetgt *t, struct net_device *nd)
480{
481 struct aoeif *p, *e;
482
483 p = t->ifs;
484 e = p + NAOEIFS;
485 for (; p < e; p++)
486 if (p->nd == nd)
487 return p;
488 return NULL;
489}
490
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800491static void
492ejectif(struct aoetgt *t, struct aoeif *ifp)
493{
494 struct aoeif *e;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700495 struct net_device *nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800496 ulong n;
497
Ed Cashin1b86fda2012-10-04 17:16:34 -0700498 nd = ifp->nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800499 e = t->ifs + NAOEIFS - 1;
500 n = (e - ifp) * sizeof *ifp;
501 memmove(ifp, ifp+1, n);
502 e->nd = NULL;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700503 dev_put(nd);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800504}
505
506static int
507sthtith(struct aoedev *d)
508{
Ed Cashin896831f2012-10-04 17:16:21 -0700509 struct frame *f, *nf;
510 struct list_head *nx, *pos, *head;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800511 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700512 struct aoetgt *ht = d->htgt;
513 int i;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800514
Ed Cashin896831f2012-10-04 17:16:21 -0700515 for (i = 0; i < NFACTIVE; i++) {
Ed Cashin64a80f52012-10-04 17:16:33 -0700516 head = &d->factive[i];
Ed Cashin896831f2012-10-04 17:16:21 -0700517 list_for_each_safe(pos, nx, head) {
518 f = list_entry(pos, struct frame, head);
Ed Cashin64a80f52012-10-04 17:16:33 -0700519 if (f->t != ht)
520 continue;
521
Ed Cashin896831f2012-10-04 17:16:21 -0700522 nf = newframe(d);
523 if (!nf)
524 return 0;
525
526 /* remove frame from active list */
527 list_del(pos);
528
529 /* reassign all pertinent bits to new outbound frame */
530 skb = nf->skb;
531 nf->skb = f->skb;
532 nf->buf = f->buf;
533 nf->bcnt = f->bcnt;
534 nf->lba = f->lba;
535 nf->bv = f->bv;
536 nf->bv_off = f->bv_off;
537 nf->waited = 0;
538 f->skb = skb;
539 aoe_freetframe(f);
540 ht->nout--;
541 nf->t->nout++;
542 resend(d, nf);
543 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800544 }
Ed Cashin3f0f0132012-10-04 17:16:27 -0700545 /* We've cleaned up the outstanding so take away his
546 * interfaces so he won't be used. We should remove him from
547 * the target array here, but cleaning up a target is
548 * involved. PUNT!
549 */
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800550 memset(ht->ifs, 0, sizeof ht->ifs);
551 d->htgt = NULL;
552 return 1;
553}
554
555static inline unsigned char
556ata_scnt(unsigned char *packet) {
557 struct aoe_hdr *h;
558 struct aoe_atahdr *ah;
559
560 h = (struct aoe_hdr *) packet;
561 ah = (struct aoe_atahdr *) (h+1);
562 return ah->scnt;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static void
566rexmit_timer(ulong vp)
567{
568 struct aoedev *d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800569 struct aoetgt *t, **tt, **te;
570 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700571 struct frame *f;
572 struct list_head *head, *pos, *nx;
573 LIST_HEAD(flist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 register long timeout;
575 ulong flags, n;
Ed Cashin896831f2012-10-04 17:16:21 -0700576 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* timeout is always ~150% of the moving average */
581 timeout = d->rttavg;
582 timeout += timeout >> 1;
583
584 spin_lock_irqsave(&d->lock, flags);
585
586 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500587 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
589 }
Ed Cashin896831f2012-10-04 17:16:21 -0700590
591 /* collect all frames to rexmit into flist */
Ed Cashin64a80f52012-10-04 17:16:33 -0700592 for (i = 0; i < NFACTIVE; i++) {
593 head = &d->factive[i];
594 list_for_each_safe(pos, nx, head) {
595 f = list_entry(pos, struct frame, head);
596 if (tsince(f->tag) < timeout)
597 break; /* end of expired frames */
598 /* move to flist for later processing */
599 list_move_tail(pos, &flist);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800600 }
Ed Cashin64a80f52012-10-04 17:16:33 -0700601 }
602 /* window check */
603 tt = d->targets;
604 te = tt + d->ntargets;
605 for (; tt < te && (t = *tt); tt++) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800606 if (t->nout == t->maxout
607 && t->maxout < t->nframes
608 && (jiffies - t->lastwadj)/HZ > 10) {
609 t->maxout++;
610 t->lastwadj = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800613
Ed Cashin69cf2d852012-10-04 17:16:23 -0700614 if (!list_empty(&flist)) { /* retransmissions necessary */
615 n = d->rttavg <<= 1;
616 if (n > MAXTIMER)
617 d->rttavg = MAXTIMER;
618 }
619
Ed Cashin896831f2012-10-04 17:16:21 -0700620 /* process expired frames */
621 while (!list_empty(&flist)) {
622 pos = flist.next;
623 f = list_entry(pos, struct frame, head);
624 n = f->waited += timeout;
625 n /= HZ;
626 if (n > aoe_deadsecs) {
627 /* Waited too long. Device failure.
628 * Hang all frames on first hash bucket for downdev
629 * to clean up.
630 */
Ed Cashin64a80f52012-10-04 17:16:33 -0700631 list_splice(&flist, &d->factive[0]);
Ed Cashin896831f2012-10-04 17:16:21 -0700632 aoedev_downdev(d);
633 break;
634 }
635 list_del(pos);
636
637 t = f->t;
Ed Cashind54d35a2012-10-04 17:16:29 -0700638 if (n > aoe_deadsecs/2)
639 d->htgt = t; /* see if another target can help */
640
Ed Cashin896831f2012-10-04 17:16:21 -0700641 if (t->nout == t->maxout) {
642 if (t->maxout > 1)
643 t->maxout--;
644 t->lastwadj = jiffies;
645 }
646
647 ifp = getif(t, f->skb->dev);
648 if (ifp && ++ifp->lost > (t->nframes << 1)
649 && (ifp != t->ifs || t->ifs[1].nd)) {
650 ejectif(t, ifp);
651 ifp = NULL;
652 }
653 resend(d, f);
654 }
655
Ed Cashin69cf2d852012-10-04 17:16:23 -0700656 if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400657 d->flags &= ~DEVFL_KICKME;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700658 d->blkq->request_fn(d->blkq);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 d->timer.expires = jiffies + TIMERTICK;
662 add_timer(&d->timer);
663
664 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Ed Cashin69cf2d852012-10-04 17:16:23 -0700667static unsigned long
668rqbiocnt(struct request *r)
669{
670 struct bio *bio;
671 unsigned long n = 0;
672
673 __rq_for_each_bio(bio, r)
674 n++;
675 return n;
676}
677
678/* This can be removed if we are certain that no users of the block
679 * layer will ever use zero-count pages in bios. Otherwise we have to
680 * protect against the put_page sometimes done by the network layer.
681 *
682 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
683 * discussion.
684 *
685 * We cannot use get_page in the workaround, because it insists on a
686 * positive page count as a precondition. So we use _count directly.
687 */
688static void
689bio_pageinc(struct bio *bio)
690{
691 struct bio_vec *bv;
692 struct page *page;
693 int i;
694
695 bio_for_each_segment(bv, bio, i) {
696 page = bv->bv_page;
697 /* Non-zero page count for non-head members of
698 * compound pages is no longer allowed by the kernel,
699 * but this has never been seen here.
700 */
701 if (unlikely(PageCompound(page)))
702 if (compound_trans_head(page) != page) {
703 pr_crit("page tail used for block I/O\n");
704 BUG();
705 }
706 atomic_inc(&page->_count);
707 }
708}
709
710static void
711bio_pagedec(struct bio *bio)
712{
713 struct bio_vec *bv;
714 int i;
715
716 bio_for_each_segment(bv, bio, i)
717 atomic_dec(&bv->bv_page->_count);
718}
719
720static void
721bufinit(struct buf *buf, struct request *rq, struct bio *bio)
722{
723 struct bio_vec *bv;
724
725 memset(buf, 0, sizeof(*buf));
726 buf->rq = rq;
727 buf->bio = bio;
728 buf->resid = bio->bi_size;
729 buf->sector = bio->bi_sector;
730 bio_pageinc(bio);
731 buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
732 buf->bv_resid = bv->bv_len;
733 WARN_ON(buf->bv_resid == 0);
734}
735
736static struct buf *
737nextbuf(struct aoedev *d)
738{
739 struct request *rq;
740 struct request_queue *q;
741 struct buf *buf;
742 struct bio *bio;
743
744 q = d->blkq;
745 if (q == NULL)
746 return NULL; /* initializing */
747 if (d->ip.buf)
748 return d->ip.buf;
749 rq = d->ip.rq;
750 if (rq == NULL) {
751 rq = blk_peek_request(q);
752 if (rq == NULL)
753 return NULL;
754 blk_start_request(rq);
755 d->ip.rq = rq;
756 d->ip.nxbio = rq->bio;
757 rq->special = (void *) rqbiocnt(rq);
758 }
759 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
760 if (buf == NULL) {
761 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
762 return NULL;
763 }
764 bio = d->ip.nxbio;
765 bufinit(buf, rq, bio);
766 bio = bio->bi_next;
767 d->ip.nxbio = bio;
768 if (bio == NULL)
769 d->ip.rq = NULL;
770 return d->ip.buf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800773/* enters with d->lock held */
774void
775aoecmd_work(struct aoedev *d)
776{
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800777 if (d->htgt && !sthtith(d))
778 return;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700779 while (aoecmd_ata_rw(d))
780 ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800781}
782
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500783/* this function performs work that has been deferred until sleeping is OK
784 */
785void
David Howellsc4028952006-11-22 14:57:56 +0000786aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500787{
David Howellsc4028952006-11-22 14:57:56 +0000788 struct aoedev *d = container_of(work, struct aoedev, work);
Ed Cashinb21faa22012-10-04 17:16:35 -0700789 struct block_device *bd;
790 u64 ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500791
792 if (d->flags & DEVFL_GDALLOC)
793 aoeblk_gdalloc(d);
794
795 if (d->flags & DEVFL_NEWSIZE) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900796 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500797 bd = bdget_disk(d->gd, 0);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500798 if (bd) {
799 mutex_lock(&bd->bd_inode->i_mutex);
800 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
801 mutex_unlock(&bd->bd_inode->i_mutex);
802 bdput(bd);
803 }
Ed Cashinb21faa22012-10-04 17:16:35 -0700804 spin_lock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500805 d->flags |= DEVFL_UP;
806 d->flags &= ~DEVFL_NEWSIZE;
Ed Cashinb21faa22012-10-04 17:16:35 -0700807 spin_unlock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500808 }
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800812ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 u64 ssize;
815 u16 n;
816
817 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700818 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700821 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (n & (1<<10)) { /* bit 10: LBA 48 */
824 d->flags |= DEVFL_EXT;
825
826 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700827 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* set as in ide-disk.c:init_idedisk_capacity */
830 d->geo.cylinders = ssize;
831 d->geo.cylinders /= (255 * 63);
832 d->geo.heads = 255;
833 d->geo.sectors = 63;
834 } else {
835 d->flags &= ~DEVFL_EXT;
836
837 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700838 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700841 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
842 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
843 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500845
846 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800847 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800848 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
849 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500850 d->aoemajor, d->aoeminor,
851 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 d->ssize = ssize;
853 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800854 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
855 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900857 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500858 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800859 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500860 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864static void
865calc_rttavg(struct aoedev *d, int rtt)
866{
867 register long n;
868
869 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400870 if (n < 0) {
871 n = -rtt;
872 if (n < MINTIMER)
873 n = MINTIMER;
874 else if (n > MAXTIMER)
875 n = MAXTIMER;
876 d->mintimer += (n - d->mintimer) >> 1;
877 } else if (n < d->mintimer)
878 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 else if (n > MAXTIMER)
880 n = MAXTIMER;
881
882 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
883 n -= d->rttavg;
884 d->rttavg += n >> 2;
885}
886
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800887static struct aoetgt *
888gettgt(struct aoedev *d, char *addr)
889{
890 struct aoetgt **t, **e;
891
892 t = d->targets;
893 e = t + NTARGETS;
894 for (; t < e && *t; t++)
895 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
896 return *t;
897 return NULL;
898}
899
Ed Cashin3d5b0602012-10-04 17:16:20 -0700900static void
Ed Cashin896831f2012-10-04 17:16:21 -0700901bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700902{
903 ulong fcnt;
904 char *p;
905 int soff = 0;
906loop:
907 fcnt = bv->bv_len - (off - bv->bv_offset);
908 if (fcnt > cnt)
909 fcnt = cnt;
910 p = page_address(bv->bv_page) + off;
911 skb_copy_bits(skb, soff, p, fcnt);
912 soff += fcnt;
913 cnt -= fcnt;
914 if (cnt <= 0)
915 return;
916 bv++;
917 off = bv->bv_offset;
918 goto loop;
919}
920
Ed Cashin69cf2d852012-10-04 17:16:23 -0700921void
922aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
923{
924 struct bio *bio;
925 int bok;
926 struct request_queue *q;
927
928 q = d->blkq;
929 if (rq == d->ip.rq)
930 d->ip.rq = NULL;
931 do {
932 bio = rq->bio;
933 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
934 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
935
936 /* cf. http://lkml.org/lkml/2006/10/31/28 */
937 if (!fastfail)
Ed Cashin11cfb6f2012-11-08 19:17:15 -0500938 __blk_run_queue(q);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700939}
940
941static void
942aoe_end_buf(struct aoedev *d, struct buf *buf)
943{
944 struct request *rq;
945 unsigned long n;
946
947 if (buf == d->ip.buf)
948 d->ip.buf = NULL;
949 rq = buf->rq;
950 bio_pagedec(buf->bio);
951 mempool_free(buf, d->bufpool);
952 n = (unsigned long) rq->special;
953 rq->special = (void *) --n;
954 if (n == 0)
955 aoe_end_request(d, rq, 0);
956}
957
Ed Cashin3d5b0602012-10-04 17:16:20 -0700958static void
Ed Cashin896831f2012-10-04 17:16:21 -0700959ktiocomplete(struct frame *f)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700960{
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400961 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct aoe_atahdr *ahin, *ahout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct buf *buf;
Ed Cashin896831f2012-10-04 17:16:21 -0700964 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800965 struct aoetgt *t;
966 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700967 struct aoedev *d;
968 long n;
969
970 if (f == NULL)
971 return;
972
973 t = f->t;
974 d = t->d;
975
976 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
977 ahout = (struct aoe_atahdr *) (hout+1);
978 buf = f->buf;
979 skb = f->r_skb;
980 if (skb == NULL)
981 goto noskb; /* just fail the buf. */
982
983 hin = (struct aoe_hdr *) skb->data;
984 skb_pull(skb, sizeof(*hin));
985 ahin = (struct aoe_atahdr *) skb->data;
986 skb_pull(skb, sizeof(*ahin));
987 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
988 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
989 ahout->cmdstat, ahin->cmdstat,
990 d->aoemajor, d->aoeminor);
991noskb: if (buf)
Ed Cashin69cf2d852012-10-04 17:16:23 -0700992 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -0700993 goto badrsp;
994 }
995
996 n = ahout->scnt << 9;
997 switch (ahout->cmdstat) {
998 case ATA_CMD_PIO_READ:
999 case ATA_CMD_PIO_READ_EXT:
1000 if (skb->len < n) {
1001 pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1002 skb->len, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001003 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001004 break;
1005 }
1006 bvcpy(f->bv, f->bv_off, skb, n);
1007 case ATA_CMD_PIO_WRITE:
1008 case ATA_CMD_PIO_WRITE_EXT:
1009 spin_lock_irq(&d->lock);
1010 ifp = getif(t, skb->dev);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001011 if (ifp)
Ed Cashin896831f2012-10-04 17:16:21 -07001012 ifp->lost = 0;
Ed Cashin896831f2012-10-04 17:16:21 -07001013 if (d->htgt == t) /* I'll help myself, thank you. */
1014 d->htgt = NULL;
1015 spin_unlock_irq(&d->lock);
1016 break;
1017 case ATA_CMD_ID_ATA:
1018 if (skb->len < 512) {
1019 pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1020 skb->len);
1021 break;
1022 }
1023 if (skb_linearize(skb))
1024 break;
1025 spin_lock_irq(&d->lock);
1026 ataid_complete(d, t, skb->data);
1027 spin_unlock_irq(&d->lock);
1028 break;
1029 default:
1030 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1031 ahout->cmdstat,
1032 be16_to_cpu(get_unaligned(&hin->major)),
1033 hin->minor);
1034 }
1035badrsp:
1036 spin_lock_irq(&d->lock);
1037
1038 aoe_freetframe(f);
1039
Ed Cashin69cf2d852012-10-04 17:16:23 -07001040 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1041 aoe_end_buf(d, buf);
Ed Cashin896831f2012-10-04 17:16:21 -07001042
Ed Cashin69cf2d852012-10-04 17:16:23 -07001043 aoecmd_work(d);
1044
1045 spin_unlock_irq(&d->lock);
1046 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001047 dev_kfree_skb(skb);
1048}
1049
1050/* Enters with iocq.lock held.
1051 * Returns true iff responses needing processing remain.
1052 */
1053static int
1054ktio(void)
1055{
1056 struct frame *f;
1057 struct list_head *pos;
1058 int i;
1059
1060 for (i = 0; ; ++i) {
1061 if (i == MAXIOC)
1062 return 1;
1063 if (list_empty(&iocq.head))
1064 return 0;
1065 pos = iocq.head.next;
1066 list_del(pos);
1067 spin_unlock_irq(&iocq.lock);
1068 f = list_entry(pos, struct frame, head);
1069 ktiocomplete(f);
1070 spin_lock_irq(&iocq.lock);
1071 }
1072}
1073
1074static int
1075kthread(void *vp)
1076{
1077 struct ktstate *k;
1078 DECLARE_WAITQUEUE(wait, current);
1079 int more;
1080
1081 k = vp;
1082 current->flags |= PF_NOFREEZE;
1083 set_user_nice(current, -10);
1084 complete(&k->rendez); /* tell spawner we're running */
1085 do {
1086 spin_lock_irq(k->lock);
1087 more = k->fn();
1088 if (!more) {
1089 add_wait_queue(k->waitq, &wait);
1090 __set_current_state(TASK_INTERRUPTIBLE);
1091 }
1092 spin_unlock_irq(k->lock);
1093 if (!more) {
1094 schedule();
1095 remove_wait_queue(k->waitq, &wait);
1096 } else
1097 cond_resched();
1098 } while (!kthread_should_stop());
1099 complete(&k->rendez); /* tell spawner we're stopping */
1100 return 0;
1101}
1102
Ed Cashineb086ec2012-10-04 17:16:25 -07001103void
Ed Cashin896831f2012-10-04 17:16:21 -07001104aoe_ktstop(struct ktstate *k)
1105{
1106 kthread_stop(k->task);
1107 wait_for_completion(&k->rendez);
1108}
1109
Ed Cashineb086ec2012-10-04 17:16:25 -07001110int
Ed Cashin896831f2012-10-04 17:16:21 -07001111aoe_ktstart(struct ktstate *k)
1112{
1113 struct task_struct *task;
1114
1115 init_completion(&k->rendez);
1116 task = kthread_run(kthread, k, k->name);
1117 if (task == NULL || IS_ERR(task))
1118 return -ENOMEM;
1119 k->task = task;
1120 wait_for_completion(&k->rendez); /* allow kthread to start */
1121 init_completion(&k->rendez); /* for waiting for exit later */
1122 return 0;
1123}
1124
1125/* pass it off to kthreads for processing */
1126static void
1127ktcomplete(struct frame *f, struct sk_buff *skb)
1128{
1129 ulong flags;
1130
1131 f->r_skb = skb;
1132 spin_lock_irqsave(&iocq.lock, flags);
1133 list_add_tail(&f->head, &iocq.head);
1134 spin_unlock_irqrestore(&iocq.lock, flags);
1135 wake_up(&ktiowq);
1136}
1137
1138struct sk_buff *
1139aoecmd_ata_rsp(struct sk_buff *skb)
1140{
1141 struct aoedev *d;
1142 struct aoe_hdr *h;
1143 struct frame *f;
1144 struct aoetgt *t;
1145 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ulong flags;
1147 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -07001148 u16 aoemajor;
1149
Ed Cashin896831f2012-10-04 17:16:21 -07001150 h = (struct aoe_hdr *) skb->data;
1151 aoemajor = be16_to_cpu(get_unaligned(&h->major));
Ed Cashin0c966212012-10-04 17:16:40 -07001152 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (d == NULL) {
1154 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1155 "for unknown device %d.%d\n",
Ed Cashin896831f2012-10-04 17:16:21 -07001156 aoemajor, h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 aoechr_error(ebuf);
Ed Cashin896831f2012-10-04 17:16:21 -07001158 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
1161 spin_lock_irqsave(&d->lock, flags);
1162
Ed Cashin896831f2012-10-04 17:16:21 -07001163 n = be32_to_cpu(get_unaligned(&h->tag));
Ed Cashin64a80f52012-10-04 17:16:33 -07001164 f = getframe(d, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -04001166 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001168 aoedev_put(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 snprintf(ebuf, sizeof ebuf,
1170 "%15s e%d.%d tag=%08x@%08lx\n",
1171 "unexpected rsp",
Ed Cashin896831f2012-10-04 17:16:21 -07001172 get_unaligned_be16(&h->major),
1173 h->minor,
1174 get_unaligned_be32(&h->tag),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 jiffies);
1176 aoechr_error(ebuf);
Ed Cashin896831f2012-10-04 17:16:21 -07001177 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Ed Cashin64a80f52012-10-04 17:16:33 -07001179 t = f->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 calc_rttavg(d, tsince(f->tag));
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001181 t->nout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001185
1186 ktcomplete(f, skb);
1187
1188 /*
1189 * Note here that we do not perform an aoedev_put, as we are
1190 * leaving this reference for the ktio to release.
1191 */
1192 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195void
1196aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1197{
David S. Millere9bb8fb02008-09-21 22:36:49 -07001198 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
David S. Millere9bb8fb02008-09-21 22:36:49 -07001200 __skb_queue_head_init(&queue);
1201 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1202 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001205struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206aoecmd_ata_id(struct aoedev *d)
1207{
1208 struct aoe_hdr *h;
1209 struct aoe_atahdr *ah;
1210 struct frame *f;
1211 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001212 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Ed Cashin896831f2012-10-04 17:16:21 -07001214 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001215 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001217
1218 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -04001221 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001222 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -08001224 skb_put(skb, sizeof *h + sizeof *ah);
1225 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001226 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -07001227 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001228 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* set up ata header */
1232 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +02001233 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ah->lba3 = 0xa0;
1235
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001236 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001238 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Ed L. Cashin4f51dc52006-09-20 14:36:49 -04001241 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001244static struct aoetgt *
1245addtgt(struct aoedev *d, char *addr, ulong nframes)
1246{
1247 struct aoetgt *t, **tt, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001248
1249 tt = d->targets;
1250 te = tt + NTARGETS;
1251 for (; tt < te && *tt; tt++)
1252 ;
1253
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001254 if (tt == te) {
1255 printk(KERN_INFO
1256 "aoe: device addtgt failure; too many targets\n");
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001257 return NULL;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001258 }
Ed Cashin896831f2012-10-04 17:16:21 -07001259 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1260 if (!t) {
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001261 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
Ed L. Cashin9bb237b2008-02-08 04:20:05 -08001262 return NULL;
1263 }
1264
Ed Cashin896831f2012-10-04 17:16:21 -07001265 d->ntargets++;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001266 t->nframes = nframes;
Ed Cashin896831f2012-10-04 17:16:21 -07001267 t->d = d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001268 memcpy(t->addr, addr, sizeof t->addr);
1269 t->ifp = t->ifs;
1270 t->maxout = t->nframes;
Ed Cashin896831f2012-10-04 17:16:21 -07001271 INIT_LIST_HEAD(&t->ffree);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001272 return *tt = t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001273}
1274
Ed Cashin3f0f0132012-10-04 17:16:27 -07001275static void
1276setdbcnt(struct aoedev *d)
1277{
1278 struct aoetgt **t, **e;
1279 int bcnt = 0;
1280
1281 t = d->targets;
1282 e = t + NTARGETS;
1283 for (; t < e && *t; t++)
1284 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1285 bcnt = (*t)->minbcnt;
1286 if (bcnt != d->maxbcnt) {
1287 d->maxbcnt = bcnt;
1288 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1289 d->aoemajor, d->aoeminor, bcnt);
1290 }
1291}
1292
1293static void
1294setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1295{
1296 struct aoedev *d;
1297 struct aoeif *p, *e;
1298 int minbcnt;
1299
1300 d = t->d;
1301 minbcnt = bcnt;
1302 p = t->ifs;
1303 e = p + NAOEIFS;
1304 for (; p < e; p++) {
1305 if (p->nd == NULL)
1306 break; /* end of the valid interfaces */
1307 if (p->nd == nd) {
1308 p->bcnt = bcnt; /* we're updating */
1309 nd = NULL;
1310 } else if (minbcnt > p->bcnt)
1311 minbcnt = p->bcnt; /* find the min interface */
1312 }
1313 if (nd) {
1314 if (p == e) {
1315 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1316 return;
1317 }
Ed Cashin1b86fda2012-10-04 17:16:34 -07001318 dev_hold(nd);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001319 p->nd = nd;
1320 p->bcnt = bcnt;
1321 }
1322 t->minbcnt = minbcnt;
1323 setdbcnt(d);
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326void
1327aoecmd_cfg_rsp(struct sk_buff *skb)
1328{
1329 struct aoedev *d;
1330 struct aoe_hdr *h;
1331 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001332 struct aoetgt *t;
Ed Cashin0c966212012-10-04 17:16:40 -07001333 ulong flags, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct sk_buff *sl;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001335 struct sk_buff_head queue;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001336 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Ed Cashin69cf2d852012-10-04 17:16:23 -07001338 sl = NULL;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001339 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 ch = (struct aoe_cfghdr *) (h+1);
1341
1342 /*
1343 * Enough people have their dip switches set backwards to
1344 * warrant a loud message for this special case.
1345 */
Harvey Harrison823ed722008-07-04 09:28:32 +02001346 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001348 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -04001349 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return;
1351 }
Ed Cashin7159e962012-10-04 17:16:44 -07001352 if (aoemajor == 0xffff) {
1353 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
Ed Cashin0c966212012-10-04 17:16:40 -07001354 aoemajor, (int) h->minor);
Ed Cashin65833032012-10-04 17:16:32 -07001355 return;
1356 }
Ed Cashin7159e962012-10-04 17:16:44 -07001357 if (h->minor == 0xff) {
1358 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1359 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return;
1361 }
1362
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001363 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001364 if (n > aoe_maxout) /* keep it reasonable */
1365 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Ed Cashin7159e962012-10-04 17:16:44 -07001367 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1368 if (d == NULL) {
1369 pr_info("aoe: device allocation failure\n");
1370 return;
1371 }
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_lock_irqsave(&d->lock, flags);
1374
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001375 t = gettgt(d, h->src);
1376 if (!t) {
1377 t = addtgt(d, h->src, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001378 if (!t)
1379 goto bail;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001380 }
Ed Cashin3f0f0132012-10-04 17:16:27 -07001381 n = skb->dev->mtu;
1382 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1383 n /= 512;
1384 if (n > ch->scnt)
1385 n = ch->scnt;
1386 n = n ? n * 512 : DEFAULTBCNT;
1387 setifbcnt(t, skb->dev, n);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001388
1389 /* don't change users' perspective */
Ed Cashin69cf2d852012-10-04 17:16:23 -07001390 if (d->nopen == 0) {
1391 d->fw_ver = be16_to_cpu(ch->fwver);
1392 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Ed Cashin69cf2d852012-10-04 17:16:23 -07001394bail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001396 aoedev_put(d);
David S. Millere9bb8fb02008-09-21 22:36:49 -07001397 if (sl) {
David S. Millere9bb8fb02008-09-21 22:36:49 -07001398 __skb_queue_head_init(&queue);
1399 __skb_queue_tail(&queue, sl);
1400 aoenet_xmit(&queue);
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001404void
1405aoecmd_cleanslate(struct aoedev *d)
1406{
1407 struct aoetgt **t, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001408
1409 d->mintimer = MINTIMER;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001410 d->maxbcnt = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001411
1412 t = d->targets;
1413 te = t + NTARGETS;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001414 for (; t < te && *t; t++)
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001415 (*t)->maxout = (*t)->nframes;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001416}
Ed Cashin896831f2012-10-04 17:16:21 -07001417
Ed Cashin69cf2d852012-10-04 17:16:23 -07001418void
1419aoe_failbuf(struct aoedev *d, struct buf *buf)
1420{
1421 if (buf == NULL)
1422 return;
1423 buf->resid = 0;
1424 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1425 if (buf->nframesout == 0)
1426 aoe_end_buf(d, buf);
1427}
1428
1429void
1430aoe_flush_iocq(void)
Ed Cashin896831f2012-10-04 17:16:21 -07001431{
1432 struct frame *f;
1433 struct aoedev *d;
1434 LIST_HEAD(flist);
1435 struct list_head *pos;
1436 struct sk_buff *skb;
1437 ulong flags;
1438
1439 spin_lock_irqsave(&iocq.lock, flags);
1440 list_splice_init(&iocq.head, &flist);
1441 spin_unlock_irqrestore(&iocq.lock, flags);
1442 while (!list_empty(&flist)) {
1443 pos = flist.next;
1444 list_del(pos);
1445 f = list_entry(pos, struct frame, head);
1446 d = f->t->d;
1447 skb = f->r_skb;
1448 spin_lock_irqsave(&d->lock, flags);
1449 if (f->buf) {
1450 f->buf->nframesout--;
1451 aoe_failbuf(d, f->buf);
1452 }
1453 aoe_freetframe(f);
1454 spin_unlock_irqrestore(&d->lock, flags);
1455 dev_kfree_skb(skb);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001456 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001457 }
1458}
1459
1460int __init
1461aoecmd_init(void)
1462{
1463 INIT_LIST_HEAD(&iocq.head);
1464 spin_lock_init(&iocq.lock);
1465 init_waitqueue_head(&ktiowq);
1466 kts.name = "aoe_ktio";
1467 kts.fn = ktio;
1468 kts.waitq = &ktiowq;
1469 kts.lock = &iocq.lock;
1470 return aoe_ktstart(&kts);
1471}
1472
1473void
1474aoecmd_exit(void)
1475{
1476 aoe_ktstop(&kts);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001477 aoe_flush_iocq();
Ed Cashin896831f2012-10-04 17:16:21 -07001478}