blob: 3c606c09fd5acbd2897c680c3249929f30b6a9a8 [file] [log] [blame]
Ed Cashinca47bbd2013-07-03 15:09:06 -07001/* Copyright (c) 2013 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 *);
Ed Cashinbbb44e32012-12-17 16:04:08 -080025static int count_targets(struct aoedev *d, int *untainted);
Ed Cashin896831f2012-10-04 17:16:21 -070026
Ed Cashin69cf2d852012-10-04 17:16:23 -070027static struct buf *nextbuf(struct aoedev *);
28
Ed L. Cashinb751e8b2006-09-20 14:36:50 -040029static int aoe_deadsecs = 60 * 3;
30module_param(aoe_deadsecs, int, 0644);
31MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Ed Cashin7b6ccc52012-12-17 16:04:00 -080033static int aoe_maxout = 64;
Ed L. Cashin7df620d2008-02-08 04:20:07 -080034module_param(aoe_maxout, int, 0644);
35MODULE_PARM_DESC(aoe_maxout,
36 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
37
Ed Cashin8030d342013-07-03 15:09:05 -070038/* The number of online cpus during module initialization gives us a
39 * convenient heuristic cap on the parallelism used for ktio threads
40 * doing I/O completion. It is not important that the cap equal the
41 * actual number of running CPUs at any given time, but because of CPU
42 * hotplug, we take care to use ncpus instead of using
43 * num_online_cpus() after module initialization.
44 */
45static int ncpus;
46
47/* mutex lock used for synchronization while thread spawning */
48static DEFINE_MUTEX(ktio_spawn_lock);
49
50static wait_queue_head_t *ktiowq;
51static struct ktstate *kts;
Ed Cashin896831f2012-10-04 17:16:21 -070052
53/* io completion queue */
Ed Cashin8030d342013-07-03 15:09:05 -070054struct iocq_ktio {
Ed Cashin896831f2012-10-04 17:16:21 -070055 struct list_head head;
56 spinlock_t lock;
Ed Cashin8030d342013-07-03 15:09:05 -070057};
58static struct iocq_ktio *iocq;
Ed Cashin896831f2012-10-04 17:16:21 -070059
Ed Cashinbbb44e32012-12-17 16:04:08 -080060static struct page *empty_page;
61
Ed L. Cashin68e0d422008-02-08 04:20:00 -080062static struct sk_buff *
Ed L. Cashine407a7f2006-09-20 14:36:49 -040063new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct sk_buff *skb;
66
Eric Dumazet91c57462013-03-27 18:28:41 +000067 skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (skb) {
Eric Dumazet91c57462013-03-27 18:28:41 +000069 skb_reserve(skb, MAX_HEADER);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070070 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070071 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 skb->protocol = __constant_htons(ETH_P_AOE);
Ed Cashin8babe8c2012-09-19 15:46:39 +000073 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 return skb;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct frame *
Ed Cashin3a0c40d2012-12-17 16:03:43 -080079getframe_deferred(struct aoedev *d, u32 tag)
80{
81 struct list_head *head, *pos, *nx;
82 struct frame *f;
83
84 head = &d->rexmitq;
85 list_for_each_safe(pos, nx, head) {
86 f = list_entry(pos, struct frame, head);
87 if (f->tag == tag) {
88 list_del(pos);
89 return f;
90 }
91 }
92 return NULL;
93}
94
95static struct frame *
Ed Cashin64a80f52012-10-04 17:16:33 -070096getframe(struct aoedev *d, u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Ed Cashin896831f2012-10-04 17:16:21 -070098 struct frame *f;
99 struct list_head *head, *pos, *nx;
100 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Ed Cashin896831f2012-10-04 17:16:21 -0700102 n = tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -0700103 head = &d->factive[n];
Ed Cashin896831f2012-10-04 17:16:21 -0700104 list_for_each_safe(pos, nx, head) {
105 f = list_entry(pos, struct frame, head);
106 if (f->tag == tag) {
107 list_del(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return f;
Ed Cashin896831f2012-10-04 17:16:21 -0700109 }
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
112}
113
114/*
115 * Leave the top bit clear so we have tagspace for userland.
116 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
117 * This driver reserves tag -1 to mean "unused frame."
118 */
119static int
Ed Cashin64a80f52012-10-04 17:16:33 -0700120newtag(struct aoedev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 register ulong n;
123
124 n = jiffies & 0xffff;
Ed Cashin64a80f52012-10-04 17:16:33 -0700125 return n |= (++d->lasttag & 0x7fff) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Ed Cashin896831f2012-10-04 17:16:21 -0700128static u32
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800129aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Ed Cashin64a80f52012-10-04 17:16:33 -0700131 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800133 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
134 memcpy(h->dst, t->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700135 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700137 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 h->minor = d->aoeminor;
139 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700140 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return host_tag;
143}
144
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400145static inline void
146put_lba(struct aoe_atahdr *ah, sector_t lba)
147{
148 ah->lba0 = lba;
149 ah->lba1 = lba >>= 8;
150 ah->lba2 = lba >>= 8;
151 ah->lba3 = lba >>= 8;
152 ah->lba4 = lba >>= 8;
153 ah->lba5 = lba >>= 8;
154}
155
Ed Cashin3f0f0132012-10-04 17:16:27 -0700156static struct aoeif *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800157ifrotate(struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Ed Cashin3f0f0132012-10-04 17:16:27 -0700159 struct aoeif *ifp;
160
161 ifp = t->ifp;
162 ifp++;
163 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
164 ifp = t->ifs;
165 if (ifp->nd == NULL)
166 return NULL;
167 return t->ifp = ifp;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800168}
169
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800170static void
171skb_pool_put(struct aoedev *d, struct sk_buff *skb)
172{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700173 __skb_queue_tail(&d->skbpool, skb);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800174}
175
176static struct sk_buff *
177skb_pool_get(struct aoedev *d)
178{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700179 struct sk_buff *skb = skb_peek(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800180
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800181 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
David S. Millere9bb8fb02008-09-21 22:36:49 -0700182 __skb_unlink(skb, &d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800183 return skb;
184 }
David S. Millere9bb8fb02008-09-21 22:36:49 -0700185 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
186 (skb = new_skb(ETH_ZLEN)))
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800187 return skb;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700188
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800189 return NULL;
190}
191
Ed Cashin896831f2012-10-04 17:16:21 -0700192void
193aoe_freetframe(struct frame *f)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800194{
Ed Cashin896831f2012-10-04 17:16:21 -0700195 struct aoetgt *t;
196
197 t = f->t;
198 f->buf = NULL;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700199 memset(&f->iter, 0, sizeof(f->iter));
Ed Cashin896831f2012-10-04 17:16:21 -0700200 f->r_skb = NULL;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800201 f->flags = 0;
Ed Cashin896831f2012-10-04 17:16:21 -0700202 list_add(&f->head, &t->ffree);
203}
204
205static struct frame *
206newtframe(struct aoedev *d, struct aoetgt *t)
207{
208 struct frame *f;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800209 struct sk_buff *skb;
Ed Cashin896831f2012-10-04 17:16:21 -0700210 struct list_head *pos;
211
212 if (list_empty(&t->ffree)) {
213 if (t->falloc >= NSKBPOOLMAX*2)
214 return NULL;
215 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
216 if (f == NULL)
217 return NULL;
218 t->falloc++;
219 f->t = t;
220 } else {
221 pos = t->ffree.next;
222 list_del(pos);
223 f = list_entry(pos, struct frame, head);
224 }
225
226 skb = f->skb;
227 if (skb == NULL) {
228 f->skb = skb = new_skb(ETH_ZLEN);
229 if (!skb) {
230bail: aoe_freetframe(f);
231 return NULL;
232 }
233 }
234
235 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
236 skb = skb_pool_get(d);
237 if (skb == NULL)
238 goto bail;
239 skb_pool_put(d, f->skb);
240 f->skb = skb;
241 }
242
243 skb->truesize -= skb->data_len;
244 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
245 skb_trim(skb, 0);
246 return f;
247}
248
249static struct frame *
250newframe(struct aoedev *d)
251{
252 struct frame *f;
253 struct aoetgt *t, **tt;
254 int totout = 0;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800255 int use_tainted;
256 int has_untainted;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800257
Ed Cashin71114ec2012-12-17 16:04:11 -0800258 if (!d->targets || !d->targets[0]) {
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800259 printk(KERN_ERR "aoe: NULL TARGETS!\n");
260 return NULL;
261 }
Ed Cashin896831f2012-10-04 17:16:21 -0700262 tt = d->tgt; /* last used target */
Ed Cashinbbb44e32012-12-17 16:04:08 -0800263 for (use_tainted = 0, has_untainted = 0;;) {
Ed Cashin896831f2012-10-04 17:16:21 -0700264 tt++;
Ed Cashin71114ec2012-12-17 16:04:11 -0800265 if (tt >= &d->targets[d->ntargets] || !*tt)
Ed Cashin896831f2012-10-04 17:16:21 -0700266 tt = d->targets;
267 t = *tt;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800268 if (!t->taint) {
269 has_untainted = 1;
270 totout += t->nout;
271 }
Ed Cashin896831f2012-10-04 17:16:21 -0700272 if (t->nout < t->maxout
Ed Cashinbbb44e32012-12-17 16:04:08 -0800273 && (use_tainted || !t->taint)
Ed Cashin896831f2012-10-04 17:16:21 -0700274 && t->ifp->nd) {
275 f = newtframe(d, t);
276 if (f) {
Ed Cashin896831f2012-10-04 17:16:21 -0700277 ifrotate(t);
Ed Cashin3f0f0132012-10-04 17:16:27 -0700278 d->tgt = tt;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800279 return f;
280 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800281 }
Ed Cashinbbb44e32012-12-17 16:04:08 -0800282 if (tt == d->tgt) { /* we've looped and found nada */
283 if (!use_tainted && !has_untainted)
284 use_tainted = 1;
285 else
286 break;
287 }
Ed Cashin896831f2012-10-04 17:16:21 -0700288 }
289 if (totout == 0) {
290 d->kicked++;
291 d->flags |= DEVFL_KICKME;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800292 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800293 return NULL;
294}
295
Ed Cashin3d5b0602012-10-04 17:16:20 -0700296static void
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700297skb_fillup(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter)
Ed Cashin3d5b0602012-10-04 17:16:20 -0700298{
299 int frag = 0;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700300 struct bio_vec bv;
301
302 __bio_for_each_segment(bv, bio, iter, iter)
303 skb_fill_page_desc(skb, frag++, bv.bv_page,
304 bv.bv_offset, bv.bv_len);
Ed Cashin3d5b0602012-10-04 17:16:20 -0700305}
306
Ed Cashin896831f2012-10-04 17:16:21 -0700307static void
308fhash(struct frame *f)
309{
Ed Cashin64a80f52012-10-04 17:16:33 -0700310 struct aoedev *d = f->t->d;
Ed Cashin896831f2012-10-04 17:16:21 -0700311 u32 n;
312
313 n = f->tag % NFACTIVE;
Ed Cashin64a80f52012-10-04 17:16:33 -0700314 list_add_tail(&f->head, &d->factive[n]);
Ed Cashin896831f2012-10-04 17:16:21 -0700315}
316
Ed Cashinbbb44e32012-12-17 16:04:08 -0800317static void
318ata_rw_frameinit(struct frame *f)
319{
320 struct aoetgt *t;
321 struct aoe_hdr *h;
322 struct aoe_atahdr *ah;
323 struct sk_buff *skb;
324 char writebit, extbit;
325
326 skb = f->skb;
327 h = (struct aoe_hdr *) skb_mac_header(skb);
328 ah = (struct aoe_atahdr *) (h + 1);
329 skb_put(skb, sizeof(*h) + sizeof(*ah));
330 memset(h, 0, skb->len);
331
332 writebit = 0x10;
333 extbit = 0x4;
334
335 t = f->t;
336 f->tag = aoehdr_atainit(t->d, t, h);
337 fhash(f);
338 t->nout++;
339 f->waited = 0;
340 f->waited_total = 0;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800341
342 /* set up ata header */
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700343 ah->scnt = f->iter.bi_size >> 9;
344 put_lba(ah, f->iter.bi_sector);
Ed Cashinbbb44e32012-12-17 16:04:08 -0800345 if (t->d->flags & DEVFL_EXT) {
346 ah->aflags |= AOEAFL_EXT;
347 } else {
348 extbit = 0;
349 ah->lba3 &= 0x0f;
350 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
351 }
352 if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700353 skb_fillup(skb, f->buf->bio, f->iter);
Ed Cashinbbb44e32012-12-17 16:04:08 -0800354 ah->aflags |= AOEAFL_WRITE;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700355 skb->len += f->iter.bi_size;
356 skb->data_len = f->iter.bi_size;
357 skb->truesize += f->iter.bi_size;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800358 t->wpkts++;
359 } else {
360 t->rpkts++;
361 writebit = 0;
362 }
363
364 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
365 skb->dev = t->ifp->nd;
366}
367
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800368static int
369aoecmd_ata_rw(struct aoedev *d)
370{
371 struct frame *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct buf *buf;
373 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700374 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ed Cashin69cf2d852012-10-04 17:16:23 -0700376 buf = nextbuf(d);
377 if (buf == NULL)
378 return 0;
Ed Cashin896831f2012-10-04 17:16:21 -0700379 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800380 if (f == NULL)
381 return 0;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* initialize the headers & frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 f->buf = buf;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700385 f->iter = buf->iter;
386 f->iter.bi_size = min_t(unsigned long,
387 d->maxbcnt ?: DEFAULTBCNT,
388 f->iter.bi_size);
389 bio_advance_iter(buf->bio, &buf->iter, f->iter.bi_size);
390
391 if (!buf->iter.bi_size)
392 d->ip.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* mark all tracking fields and load out */
395 buf->nframesout += 1;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700396
397 ata_rw_frameinit(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ed Cashinbbb44e32012-12-17 16:04:08 -0800399 skb = skb_clone(f->skb, GFP_ATOMIC);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700400 if (skb) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800401 do_gettimeofday(&f->sent);
402 f->sent_jiffs = (u32) jiffies;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700403 __skb_queue_head_init(&queue);
404 __skb_queue_tail(&queue, skb);
405 aoenet_xmit(&queue);
406 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800407 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500410/* some callers cannot sleep, and they can call this function,
411 * transmitting the packets later, when interrupts are on
412 */
David S. Millere9bb8fb02008-09-21 22:36:49 -0700413static void
414aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500415{
416 struct aoe_hdr *h;
417 struct aoe_cfghdr *ch;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700418 struct sk_buff *skb;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500419 struct net_device *ifp;
420
Eric Dumazet840a1852010-10-29 01:15:29 +0000421 rcu_read_lock();
422 for_each_netdev_rcu(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500423 dev_hold(ifp);
424 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700425 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500426
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400427 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500428 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400429 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700430 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500431 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800432 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400433 skb->dev = ifp;
David S. Millere9bb8fb02008-09-21 22:36:49 -0700434 __skb_queue_tail(queue, skb);
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700435 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500436 memset(h, 0, sizeof *h + sizeof *ch);
437
438 memset(h->dst, 0xff, sizeof h->dst);
439 memcpy(h->src, ifp->dev_addr, sizeof h->src);
440 h->type = __constant_cpu_to_be16(ETH_P_AOE);
441 h->verfl = AOE_HVER;
442 h->major = cpu_to_be16(aoemajor);
443 h->minor = aoeminor;
444 h->cmd = AOECMD_CFG;
445
Pavel Emelianov7562f872007-05-03 15:13:45 -0700446cont:
447 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500448 }
Eric Dumazet840a1852010-10-29 01:15:29 +0000449 rcu_read_unlock();
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static void
Ed Cashin896831f2012-10-04 17:16:21 -0700453resend(struct aoedev *d, struct frame *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct sk_buff *skb;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700456 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct aoe_hdr *h;
Ed Cashin896831f2012-10-04 17:16:21 -0700458 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 char buf[128];
460 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Ed Cashin896831f2012-10-04 17:16:21 -0700462 t = f->t;
Ed Cashin64a80f52012-10-04 17:16:33 -0700463 n = newtag(d);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400464 skb = f->skb;
Ed Cashin3f0f0132012-10-04 17:16:27 -0700465 if (ifrotate(t) == NULL) {
466 /* probably can't happen, but set it up to fail anyway */
467 pr_info("aoe: resend: no interfaces to rotate to.\n");
468 ktcomplete(f, NULL);
469 return;
470 }
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700471 h = (struct aoe_hdr *) skb_mac_header(skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800472
Ed Cashinbbb44e32012-12-17 16:04:08 -0800473 if (!(f->flags & FFL_PROBE)) {
474 snprintf(buf, sizeof(buf),
475 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
476 "retransmit", d->aoemajor, d->aoeminor,
477 f->tag, jiffies, n,
478 h->src, h->dst, t->nout);
479 aoechr_error(buf);
480 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 f->tag = n;
Ed Cashin896831f2012-10-04 17:16:21 -0700483 fhash(f);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700484 h->tag = cpu_to_be32(n);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800485 memcpy(h->dst, t->addr, sizeof h->dst);
486 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800488 skb->dev = t->ifp->nd;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400489 skb = skb_clone(skb, GFP_ATOMIC);
490 if (skb == NULL)
491 return;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800492 do_gettimeofday(&f->sent);
493 f->sent_jiffs = (u32) jiffies;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700494 __skb_queue_head_init(&queue);
495 __skb_queue_tail(&queue, skb);
496 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static int
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800500tsince_hr(struct frame *f)
501{
502 struct timeval now;
503 int n;
504
505 do_gettimeofday(&now);
506 n = now.tv_usec - f->sent.tv_usec;
507 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
508
509 if (n < 0)
510 n = -n;
511
512 /* For relatively long periods, use jiffies to avoid
513 * discrepancies caused by updates to the system time.
514 *
515 * On system with HZ of 1000, 32-bits is over 49 days
516 * worth of jiffies, or over 71 minutes worth of usecs.
517 *
518 * Jiffies overflow is handled by subtraction of unsigned ints:
519 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
520 * $3 = 4
521 * (gdb)
522 */
523 if (n > USEC_PER_SEC / 4) {
524 n = ((u32) jiffies) - f->sent_jiffs;
525 n *= USEC_PER_SEC / HZ;
526 }
527
528 return n;
529}
530
531static int
Ed Cashin896831f2012-10-04 17:16:21 -0700532tsince(u32 tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int n;
535
536 n = jiffies & 0xffff;
537 n -= tag & 0xffff;
538 if (n < 0)
539 n += 1<<16;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800540 return jiffies_to_usecs(n + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800543static struct aoeif *
544getif(struct aoetgt *t, struct net_device *nd)
545{
546 struct aoeif *p, *e;
547
548 p = t->ifs;
549 e = p + NAOEIFS;
550 for (; p < e; p++)
551 if (p->nd == nd)
552 return p;
553 return NULL;
554}
555
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800556static void
557ejectif(struct aoetgt *t, struct aoeif *ifp)
558{
559 struct aoeif *e;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700560 struct net_device *nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800561 ulong n;
562
Ed Cashin1b86fda2012-10-04 17:16:34 -0700563 nd = ifp->nd;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800564 e = t->ifs + NAOEIFS - 1;
565 n = (e - ifp) * sizeof *ifp;
566 memmove(ifp, ifp+1, n);
567 e->nd = NULL;
Ed Cashin1b86fda2012-10-04 17:16:34 -0700568 dev_put(nd);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800569}
570
Ed Cashin3fc9b032012-12-17 16:03:51 -0800571static struct frame *
Ed Cashinbbb44e32012-12-17 16:04:08 -0800572reassign_frame(struct frame *f)
Ed Cashin3fc9b032012-12-17 16:03:51 -0800573{
Ed Cashin3fc9b032012-12-17 16:03:51 -0800574 struct frame *nf;
575 struct sk_buff *skb;
576
Ed Cashin3fc9b032012-12-17 16:03:51 -0800577 nf = newframe(f->t->d);
578 if (!nf)
579 return NULL;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800580 if (nf->t == f->t) {
581 aoe_freetframe(nf);
582 return NULL;
583 }
Ed Cashin3fc9b032012-12-17 16:03:51 -0800584
585 skb = nf->skb;
586 nf->skb = f->skb;
587 nf->buf = f->buf;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700588 nf->iter = f->iter;
Ed Cashin3fc9b032012-12-17 16:03:51 -0800589 nf->waited = 0;
590 nf->waited_total = f->waited_total;
591 nf->sent = f->sent;
Ed Cashinfe7252b2012-12-17 16:03:55 -0800592 nf->sent_jiffs = f->sent_jiffs;
Ed Cashin3fc9b032012-12-17 16:03:51 -0800593 f->skb = skb;
Ed Cashin3fc9b032012-12-17 16:03:51 -0800594
595 return nf;
596}
597
Ed Cashinbbb44e32012-12-17 16:04:08 -0800598static void
599probe(struct aoetgt *t)
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800600{
Ed Cashinbbb44e32012-12-17 16:04:08 -0800601 struct aoedev *d;
602 struct frame *f;
603 struct sk_buff *skb;
604 struct sk_buff_head queue;
605 size_t n, m;
606 int frag;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800607
Ed Cashinbbb44e32012-12-17 16:04:08 -0800608 d = t->d;
609 f = newtframe(d, t);
610 if (!f) {
611 pr_err("%s %pm for e%ld.%d: %s\n",
612 "aoe: cannot probe remote address",
613 t->addr,
614 (long) d->aoemajor, d->aoeminor,
615 "no frame available");
616 return;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800617 }
Ed Cashinbbb44e32012-12-17 16:04:08 -0800618 f->flags |= FFL_PROBE;
619 ifrotate(t);
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700620 f->iter.bi_size = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800621 ata_rw_frameinit(f);
622 skb = f->skb;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700623 for (frag = 0, n = f->iter.bi_size; n > 0; ++frag, n -= m) {
Ed Cashinbbb44e32012-12-17 16:04:08 -0800624 if (n < PAGE_SIZE)
625 m = n;
626 else
627 m = PAGE_SIZE;
628 skb_fill_page_desc(skb, frag, empty_page, 0, m);
Ed Cashin3fc9b032012-12-17 16:03:51 -0800629 }
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700630 skb->len += f->iter.bi_size;
631 skb->data_len = f->iter.bi_size;
632 skb->truesize += f->iter.bi_size;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800633
634 skb = skb_clone(f->skb, GFP_ATOMIC);
635 if (skb) {
636 do_gettimeofday(&f->sent);
637 f->sent_jiffs = (u32) jiffies;
638 __skb_queue_head_init(&queue);
639 __skb_queue_tail(&queue, skb);
640 aoenet_xmit(&queue);
641 }
642}
643
644static long
645rto(struct aoedev *d)
646{
647 long t;
648
649 t = 2 * d->rttavg >> RTTSCALE;
650 t += 8 * d->rttdev >> RTTDSCALE;
651 if (t == 0)
652 t = 1;
653
654 return t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800658rexmit_deferred(struct aoedev *d)
659{
660 struct aoetgt *t;
661 struct frame *f;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800662 struct frame *nf;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800663 struct list_head *pos, *nx, *head;
Ed Cashin3fc9b032012-12-17 16:03:51 -0800664 int since;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800665 int untainted;
666
667 count_targets(d, &untainted);
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800668
669 head = &d->rexmitq;
670 list_for_each_safe(pos, nx, head) {
671 f = list_entry(pos, struct frame, head);
672 t = f->t;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800673 if (t->taint) {
674 if (!(f->flags & FFL_PROBE)) {
675 nf = reassign_frame(f);
676 if (nf) {
677 if (t->nout_probes == 0
678 && untainted > 0) {
679 probe(t);
680 t->nout_probes++;
681 }
682 list_replace(&f->head, &nf->head);
683 pos = &nf->head;
684 aoe_freetframe(f);
685 f = nf;
686 t = f->t;
687 }
688 } else if (untainted < 1) {
689 /* don't probe w/o other untainted aoetgts */
690 goto stop_probe;
691 } else if (tsince_hr(f) < t->taint * rto(d)) {
692 /* reprobe slowly when taint is high */
693 continue;
694 }
695 } else if (f->flags & FFL_PROBE) {
696stop_probe: /* don't probe untainted aoetgts */
697 list_del(pos);
698 aoe_freetframe(f);
699 /* leaving d->kicked, because this is routine */
700 f->t->d->flags |= DEVFL_KICKME;
701 continue;
702 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800703 if (t->nout >= t->maxout)
704 continue;
705 list_del(pos);
706 t->nout++;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800707 if (f->flags & FFL_PROBE)
708 t->nout_probes++;
Ed Cashin3fc9b032012-12-17 16:03:51 -0800709 since = tsince_hr(f);
710 f->waited += since;
711 f->waited_total += since;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800712 resend(d, f);
713 }
714}
715
Ed Cashinbbb44e32012-12-17 16:04:08 -0800716/* An aoetgt accumulates demerits quickly, and successful
717 * probing redeems the aoetgt slowly.
718 */
719static void
720scorn(struct aoetgt *t)
721{
722 int n;
723
724 n = t->taint++;
725 t->taint += t->taint * 2;
726 if (n > t->taint)
727 t->taint = n;
728 if (t->taint > MAX_TAINT)
729 t->taint = MAX_TAINT;
730}
731
732static int
733count_targets(struct aoedev *d, int *untainted)
734{
735 int i, good;
736
737 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
738 if (d->targets[i]->taint == 0)
739 good++;
740
741 if (untainted)
742 *untainted = good;
743 return i;
744}
745
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800746static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747rexmit_timer(ulong vp)
748{
749 struct aoedev *d;
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800750 struct aoetgt *t;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800751 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -0700752 struct frame *f;
753 struct list_head *head, *pos, *nx;
754 LIST_HEAD(flist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 register long timeout;
756 ulong flags, n;
Ed Cashin896831f2012-10-04 17:16:21 -0700757 int i;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800758 int utgts; /* number of aoetgt descriptors (not slots) */
Ed Cashin3fc9b032012-12-17 16:03:51 -0800759 int since;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 d = (struct aoedev *) vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Ed Cashin0d555ec2012-12-17 16:03:47 -0800763 spin_lock_irqsave(&d->lock, flags);
764
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800765 /* timeout based on observed timings and variations */
Ed Cashinbbb44e32012-12-17 16:04:08 -0800766 timeout = rto(d);
767
768 utgts = count_targets(d, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500771 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return;
773 }
Ed Cashin896831f2012-10-04 17:16:21 -0700774
775 /* collect all frames to rexmit into flist */
Ed Cashin64a80f52012-10-04 17:16:33 -0700776 for (i = 0; i < NFACTIVE; i++) {
777 head = &d->factive[i];
778 list_for_each_safe(pos, nx, head) {
779 f = list_entry(pos, struct frame, head);
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800780 if (tsince_hr(f) < timeout)
Ed Cashin64a80f52012-10-04 17:16:33 -0700781 break; /* end of expired frames */
782 /* move to flist for later processing */
783 list_move_tail(pos, &flist);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800784 }
Ed Cashin64a80f52012-10-04 17:16:33 -0700785 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700786
Ed Cashin896831f2012-10-04 17:16:21 -0700787 /* process expired frames */
788 while (!list_empty(&flist)) {
789 pos = flist.next;
790 f = list_entry(pos, struct frame, head);
Ed Cashin3fc9b032012-12-17 16:03:51 -0800791 since = tsince_hr(f);
792 n = f->waited_total + since;
Ed Cashin5f0c9c42012-12-17 16:03:49 -0800793 n /= USEC_PER_SEC;
Ed Cashinc450ba02012-12-17 16:04:14 -0800794 if (aoe_deadsecs
795 && n > aoe_deadsecs
796 && !(f->flags & FFL_PROBE)) {
Ed Cashin896831f2012-10-04 17:16:21 -0700797 /* Waited too long. Device failure.
798 * Hang all frames on first hash bucket for downdev
799 * to clean up.
800 */
Ed Cashin64a80f52012-10-04 17:16:33 -0700801 list_splice(&flist, &d->factive[0]);
Ed Cashin896831f2012-10-04 17:16:21 -0700802 aoedev_downdev(d);
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800803 goto out;
Ed Cashin896831f2012-10-04 17:16:21 -0700804 }
Ed Cashin896831f2012-10-04 17:16:21 -0700805
806 t = f->t;
Ed Cashinbbb44e32012-12-17 16:04:08 -0800807 n = f->waited + since;
808 n /= USEC_PER_SEC;
809 if (aoe_deadsecs && utgts > 0
810 && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
811 scorn(t); /* avoid this target */
Ed Cashind54d35a2012-10-04 17:16:29 -0700812
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800813 if (t->maxout != 1) {
814 t->ssthresh = t->maxout / 2;
815 t->maxout = 1;
Ed Cashin896831f2012-10-04 17:16:21 -0700816 }
817
Ed Cashinbbb44e32012-12-17 16:04:08 -0800818 if (f->flags & FFL_PROBE) {
819 t->nout_probes--;
820 } else {
821 ifp = getif(t, f->skb->dev);
822 if (ifp && ++ifp->lost > (t->nframes << 1)
823 && (ifp != t->ifs || t->ifs[1].nd)) {
824 ejectif(t, ifp);
825 ifp = NULL;
826 }
Ed Cashin896831f2012-10-04 17:16:21 -0700827 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800828 list_move_tail(pos, &d->rexmitq);
829 t->nout--;
Ed Cashin896831f2012-10-04 17:16:21 -0700830 }
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800831 rexmit_deferred(d);
Ed Cashin896831f2012-10-04 17:16:21 -0700832
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800833out:
Ed Cashinbbb44e32012-12-17 16:04:08 -0800834 if ((d->flags & DEVFL_KICKME) && d->blkq) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400835 d->flags &= ~DEVFL_KICKME;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700836 d->blkq->request_fn(d->blkq);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 d->timer.expires = jiffies + TIMERTICK;
840 add_timer(&d->timer);
841
842 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700843}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Ed Cashin69cf2d852012-10-04 17:16:23 -0700845static unsigned long
846rqbiocnt(struct request *r)
847{
848 struct bio *bio;
849 unsigned long n = 0;
850
851 __rq_for_each_bio(bio, r)
852 n++;
853 return n;
854}
855
Ed Cashin69cf2d852012-10-04 17:16:23 -0700856static void
857bufinit(struct buf *buf, struct request *rq, struct bio *bio)
858{
Ed Cashin69cf2d852012-10-04 17:16:23 -0700859 memset(buf, 0, sizeof(*buf));
860 buf->rq = rq;
861 buf->bio = bio;
Kent Overstreetfeb261e2013-08-13 11:41:43 -0700862 buf->iter = bio->bi_iter;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700863}
864
865static struct buf *
866nextbuf(struct aoedev *d)
867{
868 struct request *rq;
869 struct request_queue *q;
870 struct buf *buf;
871 struct bio *bio;
872
873 q = d->blkq;
874 if (q == NULL)
875 return NULL; /* initializing */
876 if (d->ip.buf)
877 return d->ip.buf;
878 rq = d->ip.rq;
879 if (rq == NULL) {
880 rq = blk_peek_request(q);
881 if (rq == NULL)
882 return NULL;
883 blk_start_request(rq);
884 d->ip.rq = rq;
885 d->ip.nxbio = rq->bio;
886 rq->special = (void *) rqbiocnt(rq);
887 }
888 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
889 if (buf == NULL) {
890 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
891 return NULL;
892 }
893 bio = d->ip.nxbio;
894 bufinit(buf, rq, bio);
895 bio = bio->bi_next;
896 d->ip.nxbio = bio;
897 if (bio == NULL)
898 d->ip.rq = NULL;
899 return d->ip.buf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800902/* enters with d->lock held */
903void
904aoecmd_work(struct aoedev *d)
905{
Ed Cashin3a0c40d2012-12-17 16:03:43 -0800906 rexmit_deferred(d);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700907 while (aoecmd_ata_rw(d))
908 ;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800909}
910
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500911/* this function performs work that has been deferred until sleeping is OK
912 */
913void
David Howellsc4028952006-11-22 14:57:56 +0000914aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500915{
David Howellsc4028952006-11-22 14:57:56 +0000916 struct aoedev *d = container_of(work, struct aoedev, work);
Ed Cashinb21faa22012-10-04 17:16:35 -0700917 struct block_device *bd;
918 u64 ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500919
920 if (d->flags & DEVFL_GDALLOC)
921 aoeblk_gdalloc(d);
922
923 if (d->flags & DEVFL_NEWSIZE) {
Tejun Heo80795ae2008-08-25 19:56:07 +0900924 ssize = get_capacity(d->gd);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500925 bd = bdget_disk(d->gd, 0);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500926 if (bd) {
Al Viro59551022016-01-22 15:40:57 -0500927 inode_lock(bd->bd_inode);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500928 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
Al Viro59551022016-01-22 15:40:57 -0500929 inode_unlock(bd->bd_inode);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500930 bdput(bd);
931 }
Ed Cashinb21faa22012-10-04 17:16:35 -0700932 spin_lock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500933 d->flags |= DEVFL_UP;
934 d->flags &= ~DEVFL_NEWSIZE;
Ed Cashinb21faa22012-10-04 17:16:35 -0700935 spin_unlock_irq(&d->lock);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500936 }
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static void
Ed Cashin667be1e2012-12-17 16:03:42 -0800940ata_ident_fixstring(u16 *id, int ns)
941{
942 u16 s;
943
944 while (ns-- > 0) {
945 s = *id;
946 *id++ = s >> 8 | s << 8;
947 }
948}
949
950static void
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800951ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 u64 ssize;
954 u16 n;
955
956 /* word 83: command set supported */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700957 n = get_unaligned_le16(&id[83 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* word 86: command set/feature enabled */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700960 n |= get_unaligned_le16(&id[86 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (n & (1<<10)) { /* bit 10: LBA 48 */
963 d->flags |= DEVFL_EXT;
964
965 /* word 100: number lba48 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700966 ssize = get_unaligned_le64(&id[100 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* set as in ide-disk.c:init_idedisk_capacity */
969 d->geo.cylinders = ssize;
970 d->geo.cylinders /= (255 * 63);
971 d->geo.heads = 255;
972 d->geo.sectors = 63;
973 } else {
974 d->flags &= ~DEVFL_EXT;
975
976 /* number lba28 sectors */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700977 ssize = get_unaligned_le32(&id[60 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* NOTE: obsolete in ATA 6 */
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700980 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
981 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
982 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500984
Ed Cashin667be1e2012-12-17 16:03:42 -0800985 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
986 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
987 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
988 memcpy(d->ident, id, sizeof(d->ident));
989
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500990 if (d->ssize != ssize)
Ed L. Cashin1d759812008-02-08 04:20:08 -0800991 printk(KERN_INFO
Harvey Harrison411c41e2008-11-25 00:40:37 -0800992 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
993 t->addr,
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500994 d->aoemajor, d->aoeminor,
995 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 d->ssize = ssize;
997 d->geo.start = 0;
Ed L. Cashin6b9699b2008-02-08 04:20:06 -0800998 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (d->gd != NULL) {
Tejun Heo80795ae2008-08-25 19:56:07 +09001001 set_capacity(d->gd, ssize);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001002 d->flags |= DEVFL_NEWSIZE;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001003 } else
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001004 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static void
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001009calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 register long n;
1012
1013 n = rtt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001015 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
1016 n -= d->rttavg >> RTTSCALE;
1017 d->rttavg += n;
1018 if (n < 0)
1019 n = -n;
1020 n -= d->rttdev >> RTTDSCALE;
1021 d->rttdev += n;
1022
1023 if (!t || t->maxout >= t->nframes)
1024 return;
1025 if (t->maxout < t->ssthresh)
1026 t->maxout += 1;
1027 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
1028 t->maxout += 1;
1029 t->next_cwnd = t->maxout;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001033static struct aoetgt *
1034gettgt(struct aoedev *d, char *addr)
1035{
1036 struct aoetgt **t, **e;
1037
1038 t = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -08001039 e = t + d->ntargets;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001040 for (; t < e && *t; t++)
1041 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
1042 return *t;
1043 return NULL;
1044}
1045
Ed Cashin3d5b0602012-10-04 17:16:20 -07001046static void
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001047bvcpy(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter, long cnt)
Ed Cashin3d5b0602012-10-04 17:16:20 -07001048{
Ed Cashin3d5b0602012-10-04 17:16:20 -07001049 int soff = 0;
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001050 struct bio_vec bv;
1051
1052 iter.bi_size = cnt;
1053
1054 __bio_for_each_segment(bv, bio, iter, iter) {
1055 char *p = page_address(bv.bv_page) + bv.bv_offset;
1056 skb_copy_bits(skb, soff, p, bv.bv_len);
1057 soff += bv.bv_len;
1058 }
Ed Cashin3d5b0602012-10-04 17:16:20 -07001059}
1060
Ed Cashin69cf2d852012-10-04 17:16:23 -07001061void
1062aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1063{
1064 struct bio *bio;
1065 int bok;
1066 struct request_queue *q;
1067
1068 q = d->blkq;
1069 if (rq == d->ip.rq)
1070 d->ip.rq = NULL;
1071 do {
1072 bio = rq->bio;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001073 bok = !fastfail && !bio->bi_error;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001074 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_iter.bi_size));
Ed Cashin69cf2d852012-10-04 17:16:23 -07001075
1076 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1077 if (!fastfail)
Ed Cashin11cfb6f2012-11-08 19:17:15 -05001078 __blk_run_queue(q);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001079}
1080
1081static void
1082aoe_end_buf(struct aoedev *d, struct buf *buf)
1083{
1084 struct request *rq;
1085 unsigned long n;
1086
1087 if (buf == d->ip.buf)
1088 d->ip.buf = NULL;
1089 rq = buf->rq;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001090 mempool_free(buf, d->bufpool);
1091 n = (unsigned long) rq->special;
1092 rq->special = (void *) --n;
1093 if (n == 0)
1094 aoe_end_request(d, rq, 0);
1095}
1096
Ed Cashin3d5b0602012-10-04 17:16:20 -07001097static void
Ed Cashin896831f2012-10-04 17:16:21 -07001098ktiocomplete(struct frame *f)
Ed Cashin3d5b0602012-10-04 17:16:20 -07001099{
Ed L. Cashinddec63e2006-09-20 14:36:49 -04001100 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct aoe_atahdr *ahin, *ahout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct buf *buf;
Ed Cashin896831f2012-10-04 17:16:21 -07001103 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001104 struct aoetgt *t;
1105 struct aoeif *ifp;
Ed Cashin896831f2012-10-04 17:16:21 -07001106 struct aoedev *d;
1107 long n;
Ed Cashinbbb44e32012-12-17 16:04:08 -08001108 int untainted;
Ed Cashin896831f2012-10-04 17:16:21 -07001109
1110 if (f == NULL)
1111 return;
1112
1113 t = f->t;
1114 d = t->d;
Ed Cashinbbb44e32012-12-17 16:04:08 -08001115 skb = f->r_skb;
1116 buf = f->buf;
1117 if (f->flags & FFL_PROBE)
1118 goto out;
1119 if (!skb) /* just fail the buf. */
1120 goto noskb;
Ed Cashin896831f2012-10-04 17:16:21 -07001121
1122 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1123 ahout = (struct aoe_atahdr *) (hout+1);
Ed Cashin896831f2012-10-04 17:16:21 -07001124
1125 hin = (struct aoe_hdr *) skb->data;
1126 skb_pull(skb, sizeof(*hin));
1127 ahin = (struct aoe_atahdr *) skb->data;
1128 skb_pull(skb, sizeof(*ahin));
1129 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1130 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1131 ahout->cmdstat, ahin->cmdstat,
1132 d->aoemajor, d->aoeminor);
Ed Cashina04b41c2012-12-17 16:03:39 -08001133noskb: if (buf)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001134 buf->bio->bi_error = -EIO;
Ed Cashinbbb44e32012-12-17 16:04:08 -08001135 goto out;
Ed Cashin896831f2012-10-04 17:16:21 -07001136 }
1137
1138 n = ahout->scnt << 9;
1139 switch (ahout->cmdstat) {
1140 case ATA_CMD_PIO_READ:
1141 case ATA_CMD_PIO_READ_EXT:
1142 if (skb->len < n) {
Ed Cashinbf297542012-12-17 16:04:17 -08001143 pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
1144 "aoe: runt data size in read from",
1145 (long) d->aoemajor, d->aoeminor,
1146 skb->len, n);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001147 buf->bio->bi_error = -EIO;
Ed Cashin896831f2012-10-04 17:16:21 -07001148 break;
1149 }
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001150 if (n > f->iter.bi_size) {
1151 pr_err_ratelimited("%s e%ld.%d. bytes=%ld need=%u\n",
1152 "aoe: too-large data size in read from",
1153 (long) d->aoemajor, d->aoeminor,
1154 n, f->iter.bi_size);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001155 buf->bio->bi_error = -EIO;
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001156 break;
1157 }
1158 bvcpy(skb, f->buf->bio, f->iter, n);
Ed Cashin896831f2012-10-04 17:16:21 -07001159 case ATA_CMD_PIO_WRITE:
1160 case ATA_CMD_PIO_WRITE_EXT:
1161 spin_lock_irq(&d->lock);
1162 ifp = getif(t, skb->dev);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001163 if (ifp)
Ed Cashin896831f2012-10-04 17:16:21 -07001164 ifp->lost = 0;
Ed Cashin896831f2012-10-04 17:16:21 -07001165 spin_unlock_irq(&d->lock);
1166 break;
1167 case ATA_CMD_ID_ATA:
1168 if (skb->len < 512) {
Ed Cashinbf297542012-12-17 16:04:17 -08001169 pr_info("%s e%ld.%d. skb->len=%d need=512\n",
1170 "aoe: runt data size in ataid from",
1171 (long) d->aoemajor, d->aoeminor,
Ed Cashin896831f2012-10-04 17:16:21 -07001172 skb->len);
1173 break;
1174 }
1175 if (skb_linearize(skb))
1176 break;
1177 spin_lock_irq(&d->lock);
1178 ataid_complete(d, t, skb->data);
1179 spin_unlock_irq(&d->lock);
1180 break;
1181 default:
1182 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1183 ahout->cmdstat,
1184 be16_to_cpu(get_unaligned(&hin->major)),
1185 hin->minor);
1186 }
Ed Cashinbbb44e32012-12-17 16:04:08 -08001187out:
Ed Cashin896831f2012-10-04 17:16:21 -07001188 spin_lock_irq(&d->lock);
Ed Cashinbbb44e32012-12-17 16:04:08 -08001189 if (t->taint > 0
1190 && --t->taint > 0
1191 && t->nout_probes == 0) {
1192 count_targets(d, &untainted);
1193 if (untainted > 0) {
1194 probe(t);
1195 t->nout_probes++;
1196 }
1197 }
Ed Cashin896831f2012-10-04 17:16:21 -07001198
1199 aoe_freetframe(f);
1200
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001201 if (buf && --buf->nframesout == 0 && buf->iter.bi_size == 0)
Ed Cashin69cf2d852012-10-04 17:16:23 -07001202 aoe_end_buf(d, buf);
Ed Cashin896831f2012-10-04 17:16:21 -07001203
Ed Cashin69cf2d852012-10-04 17:16:23 -07001204 spin_unlock_irq(&d->lock);
1205 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001206 dev_kfree_skb(skb);
1207}
1208
1209/* Enters with iocq.lock held.
1210 * Returns true iff responses needing processing remain.
1211 */
1212static int
Ed Cashin8030d342013-07-03 15:09:05 -07001213ktio(int id)
Ed Cashin896831f2012-10-04 17:16:21 -07001214{
1215 struct frame *f;
1216 struct list_head *pos;
1217 int i;
Ed Cashin8030d342013-07-03 15:09:05 -07001218 int actual_id;
Ed Cashin896831f2012-10-04 17:16:21 -07001219
1220 for (i = 0; ; ++i) {
1221 if (i == MAXIOC)
1222 return 1;
Ed Cashin8030d342013-07-03 15:09:05 -07001223 if (list_empty(&iocq[id].head))
Ed Cashin896831f2012-10-04 17:16:21 -07001224 return 0;
Ed Cashin8030d342013-07-03 15:09:05 -07001225 pos = iocq[id].head.next;
Ed Cashin896831f2012-10-04 17:16:21 -07001226 list_del(pos);
Ed Cashin896831f2012-10-04 17:16:21 -07001227 f = list_entry(pos, struct frame, head);
Ed Cashin8030d342013-07-03 15:09:05 -07001228 spin_unlock_irq(&iocq[id].lock);
Ed Cashin896831f2012-10-04 17:16:21 -07001229 ktiocomplete(f);
Ed Cashin8030d342013-07-03 15:09:05 -07001230
1231 /* Figure out if extra threads are required. */
1232 actual_id = f->t->d->aoeminor % ncpus;
1233
1234 if (!kts[actual_id].active) {
1235 BUG_ON(id != 0);
1236 mutex_lock(&ktio_spawn_lock);
1237 if (!kts[actual_id].active
1238 && aoe_ktstart(&kts[actual_id]) == 0)
1239 kts[actual_id].active = 1;
1240 mutex_unlock(&ktio_spawn_lock);
1241 }
1242 spin_lock_irq(&iocq[id].lock);
Ed Cashin896831f2012-10-04 17:16:21 -07001243 }
1244}
1245
1246static int
1247kthread(void *vp)
1248{
1249 struct ktstate *k;
1250 DECLARE_WAITQUEUE(wait, current);
1251 int more;
1252
1253 k = vp;
1254 current->flags |= PF_NOFREEZE;
1255 set_user_nice(current, -10);
1256 complete(&k->rendez); /* tell spawner we're running */
1257 do {
1258 spin_lock_irq(k->lock);
Ed Cashin8030d342013-07-03 15:09:05 -07001259 more = k->fn(k->id);
Ed Cashin896831f2012-10-04 17:16:21 -07001260 if (!more) {
1261 add_wait_queue(k->waitq, &wait);
1262 __set_current_state(TASK_INTERRUPTIBLE);
1263 }
1264 spin_unlock_irq(k->lock);
1265 if (!more) {
1266 schedule();
1267 remove_wait_queue(k->waitq, &wait);
1268 } else
1269 cond_resched();
1270 } while (!kthread_should_stop());
1271 complete(&k->rendez); /* tell spawner we're stopping */
1272 return 0;
1273}
1274
Ed Cashineb086ec2012-10-04 17:16:25 -07001275void
Ed Cashin896831f2012-10-04 17:16:21 -07001276aoe_ktstop(struct ktstate *k)
1277{
1278 kthread_stop(k->task);
1279 wait_for_completion(&k->rendez);
1280}
1281
Ed Cashineb086ec2012-10-04 17:16:25 -07001282int
Ed Cashin896831f2012-10-04 17:16:21 -07001283aoe_ktstart(struct ktstate *k)
1284{
1285 struct task_struct *task;
1286
1287 init_completion(&k->rendez);
Kees Cookf1701682013-07-03 15:04:58 -07001288 task = kthread_run(kthread, k, "%s", k->name);
Ed Cashin896831f2012-10-04 17:16:21 -07001289 if (task == NULL || IS_ERR(task))
1290 return -ENOMEM;
1291 k->task = task;
1292 wait_for_completion(&k->rendez); /* allow kthread to start */
1293 init_completion(&k->rendez); /* for waiting for exit later */
1294 return 0;
1295}
1296
1297/* pass it off to kthreads for processing */
1298static void
1299ktcomplete(struct frame *f, struct sk_buff *skb)
1300{
Ed Cashin8030d342013-07-03 15:09:05 -07001301 int id;
Ed Cashin896831f2012-10-04 17:16:21 -07001302 ulong flags;
1303
1304 f->r_skb = skb;
Ed Cashin8030d342013-07-03 15:09:05 -07001305 id = f->t->d->aoeminor % ncpus;
1306 spin_lock_irqsave(&iocq[id].lock, flags);
1307 if (!kts[id].active) {
1308 spin_unlock_irqrestore(&iocq[id].lock, flags);
1309 /* The thread with id has not been spawned yet,
1310 * so delegate the work to the main thread and
1311 * try spawning a new thread.
1312 */
1313 id = 0;
1314 spin_lock_irqsave(&iocq[id].lock, flags);
1315 }
1316 list_add_tail(&f->head, &iocq[id].head);
1317 spin_unlock_irqrestore(&iocq[id].lock, flags);
1318 wake_up(&ktiowq[id]);
Ed Cashin896831f2012-10-04 17:16:21 -07001319}
1320
1321struct sk_buff *
1322aoecmd_ata_rsp(struct sk_buff *skb)
1323{
1324 struct aoedev *d;
1325 struct aoe_hdr *h;
1326 struct frame *f;
Ed Cashin896831f2012-10-04 17:16:21 -07001327 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 ulong flags;
1329 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -07001330 u16 aoemajor;
1331
Ed Cashin896831f2012-10-04 17:16:21 -07001332 h = (struct aoe_hdr *) skb->data;
1333 aoemajor = be16_to_cpu(get_unaligned(&h->major));
Ed Cashin0c966212012-10-04 17:16:40 -07001334 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (d == NULL) {
1336 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1337 "for unknown device %d.%d\n",
Ed Cashin896831f2012-10-04 17:16:21 -07001338 aoemajor, h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 aoechr_error(ebuf);
Ed Cashin896831f2012-10-04 17:16:21 -07001340 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 spin_lock_irqsave(&d->lock, flags);
1344
Ed Cashin896831f2012-10-04 17:16:21 -07001345 n = be32_to_cpu(get_unaligned(&h->tag));
Ed Cashin64a80f52012-10-04 17:16:33 -07001346 f = getframe(d, n);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001347 if (f) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001348 calc_rttavg(d, f->t, tsince_hr(f));
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001349 f->t->nout--;
Ed Cashinbbb44e32012-12-17 16:04:08 -08001350 if (f->flags & FFL_PROBE)
1351 f->t->nout_probes--;
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001352 } else {
1353 f = getframe_deferred(d, n);
1354 if (f) {
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001355 calc_rttavg(d, NULL, tsince_hr(f));
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001356 } else {
1357 calc_rttavg(d, NULL, tsince(n));
1358 spin_unlock_irqrestore(&d->lock, flags);
1359 aoedev_put(d);
1360 snprintf(ebuf, sizeof(ebuf),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001361 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001362 "unexpected rsp",
1363 get_unaligned_be16(&h->major),
1364 h->minor,
1365 get_unaligned_be32(&h->tag),
Ed Cashin2292a7e2012-12-17 16:03:45 -08001366 jiffies,
1367 h->src,
1368 h->dst);
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001369 aoechr_error(ebuf);
1370 return skb;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001376
1377 ktcomplete(f, skb);
1378
1379 /*
1380 * Note here that we do not perform an aoedev_put, as we are
1381 * leaving this reference for the ktio to release.
1382 */
1383 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386void
1387aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1388{
David S. Millere9bb8fb02008-09-21 22:36:49 -07001389 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David S. Millere9bb8fb02008-09-21 22:36:49 -07001391 __skb_queue_head_init(&queue);
1392 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1393 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Ed Cashina04b41c2012-12-17 16:03:39 -08001395
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001396struct sk_buff *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397aoecmd_ata_id(struct aoedev *d)
1398{
1399 struct aoe_hdr *h;
1400 struct aoe_atahdr *ah;
1401 struct frame *f;
1402 struct sk_buff *skb;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001403 struct aoetgt *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Ed Cashin896831f2012-10-04 17:16:21 -07001405 f = newframe(d);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001406 if (f == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001408
1409 t = *d->tgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -04001412 skb = f->skb;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001413 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -08001415 skb_put(skb, sizeof *h + sizeof *ah);
1416 memset(h, 0, skb->len);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001417 f->tag = aoehdr_atainit(d, t, h);
Ed Cashin896831f2012-10-04 17:16:21 -07001418 fhash(f);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001419 t->nout++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 f->waited = 0;
Ed Cashin3fc9b032012-12-17 16:03:51 -08001421 f->waited_total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* set up ata header */
1424 ah->scnt = 1;
Bartlomiej Zolnierkiewicz04b3ab52009-04-01 21:42:24 +02001425 ah->cmdstat = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ah->lba3 = 0xa0;
1427
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001428 skb->dev = t->ifp->nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001430 d->rttavg = RTTAVG_INIT;
1431 d->rttdev = RTTDEV_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Ed Cashin5f0c9c42012-12-17 16:03:49 -08001434 skb = skb_clone(skb, GFP_ATOMIC);
1435 if (skb) {
1436 do_gettimeofday(&f->sent);
1437 f->sent_jiffs = (u32) jiffies;
1438 }
1439
1440 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
Ed Cashina04b41c2012-12-17 16:03:39 -08001442
Ed Cashin71114ec2012-12-17 16:04:11 -08001443static struct aoetgt **
1444grow_targets(struct aoedev *d)
1445{
1446 ulong oldn, newn;
1447 struct aoetgt **tt;
1448
1449 oldn = d->ntargets;
1450 newn = oldn * 2;
1451 tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
1452 if (!tt)
1453 return NULL;
1454 memmove(tt, d->targets, sizeof(*d->targets) * oldn);
1455 d->tgt = tt + (d->tgt - d->targets);
1456 kfree(d->targets);
1457 d->targets = tt;
1458 d->ntargets = newn;
1459
1460 return &d->targets[oldn];
1461}
1462
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001463static struct aoetgt *
1464addtgt(struct aoedev *d, char *addr, ulong nframes)
1465{
1466 struct aoetgt *t, **tt, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001467
1468 tt = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -08001469 te = tt + d->ntargets;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001470 for (; tt < te && *tt; tt++)
1471 ;
1472
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001473 if (tt == te) {
Ed Cashin71114ec2012-12-17 16:04:11 -08001474 tt = grow_targets(d);
1475 if (!tt)
1476 goto nomem;
Ed L. Cashin578c4aa2008-02-08 04:20:09 -08001477 }
Ed Cashin896831f2012-10-04 17:16:21 -07001478 t = kzalloc(sizeof(*t), GFP_ATOMIC);
Ed Cashin71114ec2012-12-17 16:04:11 -08001479 if (!t)
1480 goto nomem;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001481 t->nframes = nframes;
Ed Cashin896831f2012-10-04 17:16:21 -07001482 t->d = d;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001483 memcpy(t->addr, addr, sizeof t->addr);
1484 t->ifp = t->ifs;
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001485 aoecmd_wreset(t);
Ed Cashinbbb44e32012-12-17 16:04:08 -08001486 t->maxout = t->nframes / 2;
Ed Cashin896831f2012-10-04 17:16:21 -07001487 INIT_LIST_HEAD(&t->ffree);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001488 return *tt = t;
Ed Cashin71114ec2012-12-17 16:04:11 -08001489
1490 nomem:
1491 pr_info("aoe: cannot allocate memory to add target\n");
1492 return NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001493}
1494
Ed Cashin3f0f0132012-10-04 17:16:27 -07001495static void
1496setdbcnt(struct aoedev *d)
1497{
1498 struct aoetgt **t, **e;
1499 int bcnt = 0;
1500
1501 t = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -08001502 e = t + d->ntargets;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001503 for (; t < e && *t; t++)
1504 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1505 bcnt = (*t)->minbcnt;
1506 if (bcnt != d->maxbcnt) {
1507 d->maxbcnt = bcnt;
1508 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1509 d->aoemajor, d->aoeminor, bcnt);
1510 }
1511}
1512
1513static void
1514setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1515{
1516 struct aoedev *d;
1517 struct aoeif *p, *e;
1518 int minbcnt;
1519
1520 d = t->d;
1521 minbcnt = bcnt;
1522 p = t->ifs;
1523 e = p + NAOEIFS;
1524 for (; p < e; p++) {
1525 if (p->nd == NULL)
1526 break; /* end of the valid interfaces */
1527 if (p->nd == nd) {
1528 p->bcnt = bcnt; /* we're updating */
1529 nd = NULL;
1530 } else if (minbcnt > p->bcnt)
1531 minbcnt = p->bcnt; /* find the min interface */
1532 }
1533 if (nd) {
1534 if (p == e) {
1535 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1536 return;
1537 }
Ed Cashin1b86fda2012-10-04 17:16:34 -07001538 dev_hold(nd);
Ed Cashin3f0f0132012-10-04 17:16:27 -07001539 p->nd = nd;
1540 p->bcnt = bcnt;
1541 }
1542 t->minbcnt = minbcnt;
1543 setdbcnt(d);
1544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546void
1547aoecmd_cfg_rsp(struct sk_buff *skb)
1548{
1549 struct aoedev *d;
1550 struct aoe_hdr *h;
1551 struct aoe_cfghdr *ch;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001552 struct aoetgt *t;
Ed Cashin0c966212012-10-04 17:16:40 -07001553 ulong flags, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct sk_buff *sl;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001555 struct sk_buff_head queue;
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001556 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Ed Cashin69cf2d852012-10-04 17:16:23 -07001558 sl = NULL;
Ed L. Cashinabdbf942007-10-16 23:27:03 -07001559 h = (struct aoe_hdr *) skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 ch = (struct aoe_cfghdr *) (h+1);
1561
1562 /*
1563 * Enough people have their dip switches set backwards to
1564 * warrant a loud message for this special case.
1565 */
Harvey Harrison823ed722008-07-04 09:28:32 +02001566 aoemajor = get_unaligned_be16(&h->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -04001568 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -04001569 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return;
1571 }
Ed Cashin7159e962012-10-04 17:16:44 -07001572 if (aoemajor == 0xffff) {
1573 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
Ed Cashin0c966212012-10-04 17:16:40 -07001574 aoemajor, (int) h->minor);
Ed Cashin65833032012-10-04 17:16:32 -07001575 return;
1576 }
Ed Cashin7159e962012-10-04 17:16:44 -07001577 if (h->minor == 0xff) {
1578 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1579 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return;
1581 }
1582
Ed L. Cashin19bf2632006-09-20 14:36:49 -04001583 n = be16_to_cpu(ch->bufcnt);
Ed L. Cashin7df620d2008-02-08 04:20:07 -08001584 if (n > aoe_maxout) /* keep it reasonable */
1585 n = aoe_maxout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Ed Cashin7159e962012-10-04 17:16:44 -07001587 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1588 if (d == NULL) {
1589 pr_info("aoe: device allocation failure\n");
1590 return;
1591 }
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 spin_lock_irqsave(&d->lock, flags);
1594
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001595 t = gettgt(d, h->src);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001596 if (t) {
1597 t->nframes = n;
1598 if (n < t->maxout)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001599 aoecmd_wreset(t);
Ed Cashin1b8a1632012-12-17 16:03:29 -08001600 } else {
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001601 t = addtgt(d, h->src, n);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001602 if (!t)
1603 goto bail;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001604 }
Ed Cashin3f0f0132012-10-04 17:16:27 -07001605 n = skb->dev->mtu;
1606 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1607 n /= 512;
1608 if (n > ch->scnt)
1609 n = ch->scnt;
1610 n = n ? n * 512 : DEFAULTBCNT;
1611 setifbcnt(t, skb->dev, n);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -05001612
1613 /* don't change users' perspective */
Ed Cashin69cf2d852012-10-04 17:16:23 -07001614 if (d->nopen == 0) {
1615 d->fw_ver = be16_to_cpu(ch->fwver);
1616 sl = aoecmd_ata_id(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Ed Cashin69cf2d852012-10-04 17:16:23 -07001618bail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 spin_unlock_irqrestore(&d->lock, flags);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001620 aoedev_put(d);
David S. Millere9bb8fb02008-09-21 22:36:49 -07001621 if (sl) {
David S. Millere9bb8fb02008-09-21 22:36:49 -07001622 __skb_queue_head_init(&queue);
1623 __skb_queue_tail(&queue, sl);
1624 aoenet_xmit(&queue);
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001628void
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001629aoecmd_wreset(struct aoetgt *t)
1630{
1631 t->maxout = 1;
1632 t->ssthresh = t->nframes / 2;
1633 t->next_cwnd = t->nframes;
1634}
1635
1636void
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001637aoecmd_cleanslate(struct aoedev *d)
1638{
1639 struct aoetgt **t, **te;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001640
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001641 d->rttavg = RTTAVG_INIT;
1642 d->rttdev = RTTDEV_INIT;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001643 d->maxbcnt = 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001644
1645 t = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -08001646 te = t + d->ntargets;
Ed Cashin3f0f0132012-10-04 17:16:27 -07001647 for (; t < te && *t; t++)
Ed Cashin3a0c40d2012-12-17 16:03:43 -08001648 aoecmd_wreset(*t);
Ed L. Cashin68e0d422008-02-08 04:20:00 -08001649}
Ed Cashin896831f2012-10-04 17:16:21 -07001650
Ed Cashin69cf2d852012-10-04 17:16:23 -07001651void
1652aoe_failbuf(struct aoedev *d, struct buf *buf)
1653{
1654 if (buf == NULL)
1655 return;
Kent Overstreetfeb261e2013-08-13 11:41:43 -07001656 buf->iter.bi_size = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001657 buf->bio->bi_error = -EIO;
Ed Cashin69cf2d852012-10-04 17:16:23 -07001658 if (buf->nframesout == 0)
1659 aoe_end_buf(d, buf);
1660}
1661
1662void
1663aoe_flush_iocq(void)
Ed Cashin896831f2012-10-04 17:16:21 -07001664{
Ed Cashin8030d342013-07-03 15:09:05 -07001665 int i;
1666
1667 for (i = 0; i < ncpus; i++) {
1668 if (kts[i].active)
1669 aoe_flush_iocq_by_index(i);
1670 }
1671}
1672
1673void
1674aoe_flush_iocq_by_index(int id)
1675{
Ed Cashin896831f2012-10-04 17:16:21 -07001676 struct frame *f;
1677 struct aoedev *d;
1678 LIST_HEAD(flist);
1679 struct list_head *pos;
1680 struct sk_buff *skb;
1681 ulong flags;
1682
Ed Cashin8030d342013-07-03 15:09:05 -07001683 spin_lock_irqsave(&iocq[id].lock, flags);
1684 list_splice_init(&iocq[id].head, &flist);
1685 spin_unlock_irqrestore(&iocq[id].lock, flags);
Ed Cashin896831f2012-10-04 17:16:21 -07001686 while (!list_empty(&flist)) {
1687 pos = flist.next;
1688 list_del(pos);
1689 f = list_entry(pos, struct frame, head);
1690 d = f->t->d;
1691 skb = f->r_skb;
1692 spin_lock_irqsave(&d->lock, flags);
1693 if (f->buf) {
1694 f->buf->nframesout--;
1695 aoe_failbuf(d, f->buf);
1696 }
1697 aoe_freetframe(f);
1698 spin_unlock_irqrestore(&d->lock, flags);
1699 dev_kfree_skb(skb);
Ed Cashin69cf2d852012-10-04 17:16:23 -07001700 aoedev_put(d);
Ed Cashin896831f2012-10-04 17:16:21 -07001701 }
1702}
1703
1704int __init
1705aoecmd_init(void)
1706{
Ed Cashinbbb44e32012-12-17 16:04:08 -08001707 void *p;
Ed Cashin8030d342013-07-03 15:09:05 -07001708 int i;
1709 int ret;
Ed Cashinbbb44e32012-12-17 16:04:08 -08001710
1711 /* get_zeroed_page returns page with ref count 1 */
Michal Hocko32d6bd92016-06-24 14:48:47 -07001712 p = (void *) get_zeroed_page(GFP_KERNEL);
Ed Cashinbbb44e32012-12-17 16:04:08 -08001713 if (!p)
1714 return -ENOMEM;
1715 empty_page = virt_to_page(p);
1716
Ed Cashin8030d342013-07-03 15:09:05 -07001717 ncpus = num_online_cpus();
1718
1719 iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1720 if (!iocq)
1721 return -ENOMEM;
1722
1723 kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1724 if (!kts) {
1725 ret = -ENOMEM;
1726 goto kts_fail;
1727 }
1728
1729 ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1730 if (!ktiowq) {
1731 ret = -ENOMEM;
1732 goto ktiowq_fail;
1733 }
1734
1735 mutex_init(&ktio_spawn_lock);
1736
1737 for (i = 0; i < ncpus; i++) {
1738 INIT_LIST_HEAD(&iocq[i].head);
1739 spin_lock_init(&iocq[i].lock);
1740 init_waitqueue_head(&ktiowq[i]);
1741 snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
1742 kts[i].fn = ktio;
1743 kts[i].waitq = &ktiowq[i];
1744 kts[i].lock = &iocq[i].lock;
1745 kts[i].id = i;
1746 kts[i].active = 0;
1747 }
1748 kts[0].active = 1;
1749 if (aoe_ktstart(&kts[0])) {
1750 ret = -ENOMEM;
1751 goto ktstart_fail;
1752 }
1753 return 0;
1754
1755ktstart_fail:
1756 kfree(ktiowq);
1757ktiowq_fail:
1758 kfree(kts);
1759kts_fail:
1760 kfree(iocq);
1761
1762 return ret;
Ed Cashin896831f2012-10-04 17:16:21 -07001763}
1764
1765void
1766aoecmd_exit(void)
1767{
Ed Cashin8030d342013-07-03 15:09:05 -07001768 int i;
1769
1770 for (i = 0; i < ncpus; i++)
1771 if (kts[i].active)
1772 aoe_ktstop(&kts[i]);
1773
Ed Cashin69cf2d852012-10-04 17:16:23 -07001774 aoe_flush_iocq();
Ed Cashinbbb44e32012-12-17 16:04:08 -08001775
Ed Cashin8030d342013-07-03 15:09:05 -07001776 /* Free up the iocq and thread speicific configuration
1777 * allocated during startup.
1778 */
1779 kfree(iocq);
1780 kfree(kts);
1781 kfree(ktiowq);
1782
Ed Cashinbbb44e32012-12-17 16:04:08 -08001783 free_page((unsigned long) page_address(empty_page));
1784 empty_page = NULL;
Ed Cashin896831f2012-10-04 17:16:21 -07001785}