Ed Cashin | ca47bbd | 2013-07-03 15:09:06 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * aoecmd.c |
| 4 | * Filesystem request handling methods |
| 5 | */ |
| 6 | |
Bartlomiej Zolnierkiewicz | 04b3ab5 | 2009-04-01 21:42:24 +0200 | [diff] [blame] | 7 | #include <linux/ata.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/hdreg.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/netdevice.h> |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 13 | #include <linux/genhd.h> |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/kthread.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 17 | #include <net/net_namespace.h> |
Ed L. Cashin | 475172f | 2005-09-29 12:47:40 -0400 | [diff] [blame] | 18 | #include <asm/unaligned.h> |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 19 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "aoe.h" |
| 21 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 22 | #define MAXIOC (8192) /* default meant to avoid most soft lockups */ |
| 23 | |
| 24 | static void ktcomplete(struct frame *, struct sk_buff *); |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 25 | static int count_targets(struct aoedev *d, int *untainted); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 26 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 27 | static struct buf *nextbuf(struct aoedev *); |
| 28 | |
Ed L. Cashin | b751e8b | 2006-09-20 14:36:50 -0400 | [diff] [blame] | 29 | static int aoe_deadsecs = 60 * 3; |
| 30 | module_param(aoe_deadsecs, int, 0644); |
| 31 | MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Ed Cashin | 7b6ccc5 | 2012-12-17 16:04:00 -0800 | [diff] [blame] | 33 | static int aoe_maxout = 64; |
Ed L. Cashin | 7df620d | 2008-02-08 04:20:07 -0800 | [diff] [blame] | 34 | module_param(aoe_maxout, int, 0644); |
| 35 | MODULE_PARM_DESC(aoe_maxout, |
| 36 | "Only aoe_maxout outstanding packets for every MAC on eX.Y."); |
| 37 | |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 38 | /* 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 | */ |
| 45 | static int ncpus; |
| 46 | |
| 47 | /* mutex lock used for synchronization while thread spawning */ |
| 48 | static DEFINE_MUTEX(ktio_spawn_lock); |
| 49 | |
| 50 | static wait_queue_head_t *ktiowq; |
| 51 | static struct ktstate *kts; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 52 | |
| 53 | /* io completion queue */ |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 54 | struct iocq_ktio { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 55 | struct list_head head; |
| 56 | spinlock_t lock; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 57 | }; |
| 58 | static struct iocq_ktio *iocq; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 59 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 60 | static struct page *empty_page; |
| 61 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 62 | static struct sk_buff * |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 63 | new_skb(ulong len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | struct sk_buff *skb; |
| 66 | |
Eric Dumazet | 91c5746 | 2013-03-27 18:28:41 +0000 | [diff] [blame] | 67 | skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (skb) { |
Eric Dumazet | 91c5746 | 2013-03-27 18:28:41 +0000 | [diff] [blame] | 69 | skb_reserve(skb, MAX_HEADER); |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 70 | skb_reset_mac_header(skb); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 71 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | skb->protocol = __constant_htons(ETH_P_AOE); |
Ed Cashin | 8babe8c | 2012-09-19 15:46:39 +0000 | [diff] [blame] | 73 | skb_checksum_none_assert(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | return skb; |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static struct frame * |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 79 | getframe_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 | |
| 95 | static struct frame * |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 96 | getframe(struct aoedev *d, u32 tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 98 | struct frame *f; |
| 99 | struct list_head *head, *pos, *nx; |
| 100 | u32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 102 | n = tag % NFACTIVE; |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 103 | head = &d->factive[n]; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 104 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return f; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 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 | */ |
| 119 | static int |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 120 | newtag(struct aoedev *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | register ulong n; |
| 123 | |
| 124 | n = jiffies & 0xffff; |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 125 | return n |= (++d->lasttag & 0x7fff) << 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 128 | static u32 |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 129 | aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 131 | u32 host_tag = newtag(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 133 | memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); |
| 134 | memcpy(h->dst, t->addr, sizeof h->dst); |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 135 | h->type = __constant_cpu_to_be16(ETH_P_AOE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | h->verfl = AOE_HVER; |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 137 | h->major = cpu_to_be16(d->aoemajor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | h->minor = d->aoeminor; |
| 139 | h->cmd = AOECMD_ATA; |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 140 | h->tag = cpu_to_be32(host_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | return host_tag; |
| 143 | } |
| 144 | |
Ed L. Cashin | 19bf263 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 145 | static inline void |
| 146 | put_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 Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 156 | static struct aoeif * |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 157 | ifrotate(struct aoetgt *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 159 | 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. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 170 | static void |
| 171 | skb_pool_put(struct aoedev *d, struct sk_buff *skb) |
| 172 | { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 173 | __skb_queue_tail(&d->skbpool, skb); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static struct sk_buff * |
| 177 | skb_pool_get(struct aoedev *d) |
| 178 | { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 179 | struct sk_buff *skb = skb_peek(&d->skbpool); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 180 | |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 181 | if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 182 | __skb_unlink(skb, &d->skbpool); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 183 | return skb; |
| 184 | } |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 185 | if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && |
| 186 | (skb = new_skb(ETH_ZLEN))) |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 187 | return skb; |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 188 | |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 189 | return NULL; |
| 190 | } |
| 191 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 192 | void |
| 193 | aoe_freetframe(struct frame *f) |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 194 | { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 195 | struct aoetgt *t; |
| 196 | |
| 197 | t = f->t; |
| 198 | f->buf = NULL; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 199 | f->lba = 0; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 200 | f->bv = NULL; |
| 201 | f->r_skb = NULL; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 202 | f->flags = 0; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 203 | list_add(&f->head, &t->ffree); |
| 204 | } |
| 205 | |
| 206 | static struct frame * |
| 207 | newtframe(struct aoedev *d, struct aoetgt *t) |
| 208 | { |
| 209 | struct frame *f; |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 210 | struct sk_buff *skb; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 211 | struct list_head *pos; |
| 212 | |
| 213 | if (list_empty(&t->ffree)) { |
| 214 | if (t->falloc >= NSKBPOOLMAX*2) |
| 215 | return NULL; |
| 216 | f = kcalloc(1, sizeof(*f), GFP_ATOMIC); |
| 217 | if (f == NULL) |
| 218 | return NULL; |
| 219 | t->falloc++; |
| 220 | f->t = t; |
| 221 | } else { |
| 222 | pos = t->ffree.next; |
| 223 | list_del(pos); |
| 224 | f = list_entry(pos, struct frame, head); |
| 225 | } |
| 226 | |
| 227 | skb = f->skb; |
| 228 | if (skb == NULL) { |
| 229 | f->skb = skb = new_skb(ETH_ZLEN); |
| 230 | if (!skb) { |
| 231 | bail: aoe_freetframe(f); |
| 232 | return NULL; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (atomic_read(&skb_shinfo(skb)->dataref) != 1) { |
| 237 | skb = skb_pool_get(d); |
| 238 | if (skb == NULL) |
| 239 | goto bail; |
| 240 | skb_pool_put(d, f->skb); |
| 241 | f->skb = skb; |
| 242 | } |
| 243 | |
| 244 | skb->truesize -= skb->data_len; |
| 245 | skb_shinfo(skb)->nr_frags = skb->data_len = 0; |
| 246 | skb_trim(skb, 0); |
| 247 | return f; |
| 248 | } |
| 249 | |
| 250 | static struct frame * |
| 251 | newframe(struct aoedev *d) |
| 252 | { |
| 253 | struct frame *f; |
| 254 | struct aoetgt *t, **tt; |
| 255 | int totout = 0; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 256 | int use_tainted; |
| 257 | int has_untainted; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 258 | |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 259 | if (!d->targets || !d->targets[0]) { |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 260 | printk(KERN_ERR "aoe: NULL TARGETS!\n"); |
| 261 | return NULL; |
| 262 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 263 | tt = d->tgt; /* last used target */ |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 264 | for (use_tainted = 0, has_untainted = 0;;) { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 265 | tt++; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 266 | if (tt >= &d->targets[d->ntargets] || !*tt) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 267 | tt = d->targets; |
| 268 | t = *tt; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 269 | if (!t->taint) { |
| 270 | has_untainted = 1; |
| 271 | totout += t->nout; |
| 272 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 273 | if (t->nout < t->maxout |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 274 | && (use_tainted || !t->taint) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 275 | && t->ifp->nd) { |
| 276 | f = newtframe(d, t); |
| 277 | if (f) { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 278 | ifrotate(t); |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 279 | d->tgt = tt; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 280 | return f; |
| 281 | } |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 282 | } |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 283 | if (tt == d->tgt) { /* we've looped and found nada */ |
| 284 | if (!use_tainted && !has_untainted) |
| 285 | use_tainted = 1; |
| 286 | else |
| 287 | break; |
| 288 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 289 | } |
| 290 | if (totout == 0) { |
| 291 | d->kicked++; |
| 292 | d->flags |= DEVFL_KICKME; |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 293 | } |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
| 296 | |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 297 | static void |
| 298 | skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt) |
| 299 | { |
| 300 | int frag = 0; |
| 301 | ulong fcnt; |
| 302 | loop: |
| 303 | fcnt = bv->bv_len - (off - bv->bv_offset); |
| 304 | if (fcnt > cnt) |
| 305 | fcnt = cnt; |
| 306 | skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt); |
| 307 | cnt -= fcnt; |
| 308 | if (cnt <= 0) |
| 309 | return; |
| 310 | bv++; |
| 311 | off = bv->bv_offset; |
| 312 | goto loop; |
| 313 | } |
| 314 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 315 | static void |
| 316 | fhash(struct frame *f) |
| 317 | { |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 318 | struct aoedev *d = f->t->d; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 319 | u32 n; |
| 320 | |
| 321 | n = f->tag % NFACTIVE; |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 322 | list_add_tail(&f->head, &d->factive[n]); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 325 | static void |
| 326 | ata_rw_frameinit(struct frame *f) |
| 327 | { |
| 328 | struct aoetgt *t; |
| 329 | struct aoe_hdr *h; |
| 330 | struct aoe_atahdr *ah; |
| 331 | struct sk_buff *skb; |
| 332 | char writebit, extbit; |
| 333 | |
| 334 | skb = f->skb; |
| 335 | h = (struct aoe_hdr *) skb_mac_header(skb); |
| 336 | ah = (struct aoe_atahdr *) (h + 1); |
| 337 | skb_put(skb, sizeof(*h) + sizeof(*ah)); |
| 338 | memset(h, 0, skb->len); |
| 339 | |
| 340 | writebit = 0x10; |
| 341 | extbit = 0x4; |
| 342 | |
| 343 | t = f->t; |
| 344 | f->tag = aoehdr_atainit(t->d, t, h); |
| 345 | fhash(f); |
| 346 | t->nout++; |
| 347 | f->waited = 0; |
| 348 | f->waited_total = 0; |
| 349 | if (f->buf) |
| 350 | f->lba = f->buf->sector; |
| 351 | |
| 352 | /* set up ata header */ |
| 353 | ah->scnt = f->bcnt >> 9; |
| 354 | put_lba(ah, f->lba); |
| 355 | if (t->d->flags & DEVFL_EXT) { |
| 356 | ah->aflags |= AOEAFL_EXT; |
| 357 | } else { |
| 358 | extbit = 0; |
| 359 | ah->lba3 &= 0x0f; |
| 360 | ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */ |
| 361 | } |
| 362 | if (f->buf && bio_data_dir(f->buf->bio) == WRITE) { |
| 363 | skb_fillup(skb, f->bv, f->bv_off, f->bcnt); |
| 364 | ah->aflags |= AOEAFL_WRITE; |
| 365 | skb->len += f->bcnt; |
| 366 | skb->data_len = f->bcnt; |
| 367 | skb->truesize += f->bcnt; |
| 368 | t->wpkts++; |
| 369 | } else { |
| 370 | t->rpkts++; |
| 371 | writebit = 0; |
| 372 | } |
| 373 | |
| 374 | ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit; |
| 375 | skb->dev = t->ifp->nd; |
| 376 | } |
| 377 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 378 | static int |
| 379 | aoecmd_ata_rw(struct aoedev *d) |
| 380 | { |
| 381 | struct frame *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | struct buf *buf; |
| 383 | struct sk_buff *skb; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 384 | struct sk_buff_head queue; |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 385 | ulong bcnt, fbcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 387 | buf = nextbuf(d); |
| 388 | if (buf == NULL) |
| 389 | return 0; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 390 | f = newframe(d); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 391 | if (f == NULL) |
| 392 | return 0; |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 393 | bcnt = d->maxbcnt; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 394 | if (bcnt == 0) |
| 395 | bcnt = DEFAULTBCNT; |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 396 | if (bcnt > buf->resid) |
| 397 | bcnt = buf->resid; |
| 398 | fbcnt = bcnt; |
| 399 | f->bv = buf->bv; |
| 400 | f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid); |
| 401 | do { |
| 402 | if (fbcnt < buf->bv_resid) { |
| 403 | buf->bv_resid -= fbcnt; |
| 404 | buf->resid -= fbcnt; |
| 405 | break; |
| 406 | } |
| 407 | fbcnt -= buf->bv_resid; |
| 408 | buf->resid -= buf->bv_resid; |
| 409 | if (buf->resid == 0) { |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 410 | d->ip.buf = NULL; |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | buf->bv++; |
| 414 | buf->bv_resid = buf->bv->bv_len; |
| 415 | WARN_ON(buf->bv_resid == 0); |
| 416 | } while (fbcnt); |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* initialize the headers & frame */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | f->buf = buf; |
Ed L. Cashin | 19bf263 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 420 | f->bcnt = bcnt; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 421 | ata_rw_frameinit(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | /* mark all tracking fields and load out */ |
| 424 | buf->nframesout += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | buf->sector += bcnt >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 427 | skb = skb_clone(f->skb, GFP_ATOMIC); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 428 | if (skb) { |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 429 | do_gettimeofday(&f->sent); |
| 430 | f->sent_jiffs = (u32) jiffies; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 431 | __skb_queue_head_init(&queue); |
| 432 | __skb_queue_tail(&queue, skb); |
| 433 | aoenet_xmit(&queue); |
| 434 | } |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 435 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 438 | /* some callers cannot sleep, and they can call this function, |
| 439 | * transmitting the packets later, when interrupts are on |
| 440 | */ |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 441 | static void |
| 442 | aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 443 | { |
| 444 | struct aoe_hdr *h; |
| 445 | struct aoe_cfghdr *ch; |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 446 | struct sk_buff *skb; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 447 | struct net_device *ifp; |
| 448 | |
Eric Dumazet | 840a185 | 2010-10-29 01:15:29 +0000 | [diff] [blame] | 449 | rcu_read_lock(); |
| 450 | for_each_netdev_rcu(&init_net, ifp) { |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 451 | dev_hold(ifp); |
| 452 | if (!is_aoe_netif(ifp)) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 453 | goto cont; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 454 | |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 455 | skb = new_skb(sizeof *h + sizeof *ch); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 456 | if (skb == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 457 | printk(KERN_INFO "aoe: skb alloc failure\n"); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 458 | goto cont; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 459 | } |
Ed L. Cashin | 19900cd | 2006-12-22 01:09:21 -0800 | [diff] [blame] | 460 | skb_put(skb, sizeof *h + sizeof *ch); |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 461 | skb->dev = ifp; |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 462 | __skb_queue_tail(queue, skb); |
Ed L. Cashin | abdbf94 | 2007-10-16 23:27:03 -0700 | [diff] [blame] | 463 | h = (struct aoe_hdr *) skb_mac_header(skb); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 464 | memset(h, 0, sizeof *h + sizeof *ch); |
| 465 | |
| 466 | memset(h->dst, 0xff, sizeof h->dst); |
| 467 | memcpy(h->src, ifp->dev_addr, sizeof h->src); |
| 468 | h->type = __constant_cpu_to_be16(ETH_P_AOE); |
| 469 | h->verfl = AOE_HVER; |
| 470 | h->major = cpu_to_be16(aoemajor); |
| 471 | h->minor = aoeminor; |
| 472 | h->cmd = AOECMD_CFG; |
| 473 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 474 | cont: |
| 475 | dev_put(ifp); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 476 | } |
Eric Dumazet | 840a185 | 2010-10-29 01:15:29 +0000 | [diff] [blame] | 477 | rcu_read_unlock(); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 478 | } |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | static void |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 481 | resend(struct aoedev *d, struct frame *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | struct sk_buff *skb; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 484 | struct sk_buff_head queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | struct aoe_hdr *h; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 486 | struct aoetgt *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | char buf[128]; |
| 488 | u32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 490 | t = f->t; |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 491 | n = newtag(d); |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 492 | skb = f->skb; |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 493 | if (ifrotate(t) == NULL) { |
| 494 | /* probably can't happen, but set it up to fail anyway */ |
| 495 | pr_info("aoe: resend: no interfaces to rotate to.\n"); |
| 496 | ktcomplete(f, NULL); |
| 497 | return; |
| 498 | } |
Ed L. Cashin | abdbf94 | 2007-10-16 23:27:03 -0700 | [diff] [blame] | 499 | h = (struct aoe_hdr *) skb_mac_header(skb); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 500 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 501 | if (!(f->flags & FFL_PROBE)) { |
| 502 | snprintf(buf, sizeof(buf), |
| 503 | "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n", |
| 504 | "retransmit", d->aoemajor, d->aoeminor, |
| 505 | f->tag, jiffies, n, |
| 506 | h->src, h->dst, t->nout); |
| 507 | aoechr_error(buf); |
| 508 | } |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | f->tag = n; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 511 | fhash(f); |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 512 | h->tag = cpu_to_be32(n); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 513 | memcpy(h->dst, t->addr, sizeof h->dst); |
| 514 | memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 516 | skb->dev = t->ifp->nd; |
Ed L. Cashin | 4f51dc5 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 517 | skb = skb_clone(skb, GFP_ATOMIC); |
| 518 | if (skb == NULL) |
| 519 | return; |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 520 | do_gettimeofday(&f->sent); |
| 521 | f->sent_jiffs = (u32) jiffies; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 522 | __skb_queue_head_init(&queue); |
| 523 | __skb_queue_tail(&queue, skb); |
| 524 | aoenet_xmit(&queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static int |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 528 | tsince_hr(struct frame *f) |
| 529 | { |
| 530 | struct timeval now; |
| 531 | int n; |
| 532 | |
| 533 | do_gettimeofday(&now); |
| 534 | n = now.tv_usec - f->sent.tv_usec; |
| 535 | n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC; |
| 536 | |
| 537 | if (n < 0) |
| 538 | n = -n; |
| 539 | |
| 540 | /* For relatively long periods, use jiffies to avoid |
| 541 | * discrepancies caused by updates to the system time. |
| 542 | * |
| 543 | * On system with HZ of 1000, 32-bits is over 49 days |
| 544 | * worth of jiffies, or over 71 minutes worth of usecs. |
| 545 | * |
| 546 | * Jiffies overflow is handled by subtraction of unsigned ints: |
| 547 | * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe |
| 548 | * $3 = 4 |
| 549 | * (gdb) |
| 550 | */ |
| 551 | if (n > USEC_PER_SEC / 4) { |
| 552 | n = ((u32) jiffies) - f->sent_jiffs; |
| 553 | n *= USEC_PER_SEC / HZ; |
| 554 | } |
| 555 | |
| 556 | return n; |
| 557 | } |
| 558 | |
| 559 | static int |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 560 | tsince(u32 tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | int n; |
| 563 | |
| 564 | n = jiffies & 0xffff; |
| 565 | n -= tag & 0xffff; |
| 566 | if (n < 0) |
| 567 | n += 1<<16; |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 568 | return jiffies_to_usecs(n + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 571 | static struct aoeif * |
| 572 | getif(struct aoetgt *t, struct net_device *nd) |
| 573 | { |
| 574 | struct aoeif *p, *e; |
| 575 | |
| 576 | p = t->ifs; |
| 577 | e = p + NAOEIFS; |
| 578 | for (; p < e; p++) |
| 579 | if (p->nd == nd) |
| 580 | return p; |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 584 | static void |
| 585 | ejectif(struct aoetgt *t, struct aoeif *ifp) |
| 586 | { |
| 587 | struct aoeif *e; |
Ed Cashin | 1b86fda | 2012-10-04 17:16:34 -0700 | [diff] [blame] | 588 | struct net_device *nd; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 589 | ulong n; |
| 590 | |
Ed Cashin | 1b86fda | 2012-10-04 17:16:34 -0700 | [diff] [blame] | 591 | nd = ifp->nd; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 592 | e = t->ifs + NAOEIFS - 1; |
| 593 | n = (e - ifp) * sizeof *ifp; |
| 594 | memmove(ifp, ifp+1, n); |
| 595 | e->nd = NULL; |
Ed Cashin | 1b86fda | 2012-10-04 17:16:34 -0700 | [diff] [blame] | 596 | dev_put(nd); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 599 | static struct frame * |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 600 | reassign_frame(struct frame *f) |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 601 | { |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 602 | struct frame *nf; |
| 603 | struct sk_buff *skb; |
| 604 | |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 605 | nf = newframe(f->t->d); |
| 606 | if (!nf) |
| 607 | return NULL; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 608 | if (nf->t == f->t) { |
| 609 | aoe_freetframe(nf); |
| 610 | return NULL; |
| 611 | } |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 612 | |
| 613 | skb = nf->skb; |
| 614 | nf->skb = f->skb; |
| 615 | nf->buf = f->buf; |
| 616 | nf->bcnt = f->bcnt; |
| 617 | nf->lba = f->lba; |
| 618 | nf->bv = f->bv; |
| 619 | nf->bv_off = f->bv_off; |
| 620 | nf->waited = 0; |
| 621 | nf->waited_total = f->waited_total; |
| 622 | nf->sent = f->sent; |
Ed Cashin | fe7252b | 2012-12-17 16:03:55 -0800 | [diff] [blame] | 623 | nf->sent_jiffs = f->sent_jiffs; |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 624 | f->skb = skb; |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 625 | |
| 626 | return nf; |
| 627 | } |
| 628 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 629 | static void |
| 630 | probe(struct aoetgt *t) |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 631 | { |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 632 | struct aoedev *d; |
| 633 | struct frame *f; |
| 634 | struct sk_buff *skb; |
| 635 | struct sk_buff_head queue; |
| 636 | size_t n, m; |
| 637 | int frag; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 638 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 639 | d = t->d; |
| 640 | f = newtframe(d, t); |
| 641 | if (!f) { |
| 642 | pr_err("%s %pm for e%ld.%d: %s\n", |
| 643 | "aoe: cannot probe remote address", |
| 644 | t->addr, |
| 645 | (long) d->aoemajor, d->aoeminor, |
| 646 | "no frame available"); |
| 647 | return; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 648 | } |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 649 | f->flags |= FFL_PROBE; |
| 650 | ifrotate(t); |
| 651 | f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT; |
| 652 | ata_rw_frameinit(f); |
| 653 | skb = f->skb; |
| 654 | for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) { |
| 655 | if (n < PAGE_SIZE) |
| 656 | m = n; |
| 657 | else |
| 658 | m = PAGE_SIZE; |
| 659 | skb_fill_page_desc(skb, frag, empty_page, 0, m); |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 660 | } |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 661 | skb->len += f->bcnt; |
| 662 | skb->data_len = f->bcnt; |
| 663 | skb->truesize += f->bcnt; |
| 664 | |
| 665 | skb = skb_clone(f->skb, GFP_ATOMIC); |
| 666 | if (skb) { |
| 667 | do_gettimeofday(&f->sent); |
| 668 | f->sent_jiffs = (u32) jiffies; |
| 669 | __skb_queue_head_init(&queue); |
| 670 | __skb_queue_tail(&queue, skb); |
| 671 | aoenet_xmit(&queue); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | static long |
| 676 | rto(struct aoedev *d) |
| 677 | { |
| 678 | long t; |
| 679 | |
| 680 | t = 2 * d->rttavg >> RTTSCALE; |
| 681 | t += 8 * d->rttdev >> RTTDSCALE; |
| 682 | if (t == 0) |
| 683 | t = 1; |
| 684 | |
| 685 | return t; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | static void |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 689 | rexmit_deferred(struct aoedev *d) |
| 690 | { |
| 691 | struct aoetgt *t; |
| 692 | struct frame *f; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 693 | struct frame *nf; |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 694 | struct list_head *pos, *nx, *head; |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 695 | int since; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 696 | int untainted; |
| 697 | |
| 698 | count_targets(d, &untainted); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 699 | |
| 700 | head = &d->rexmitq; |
| 701 | list_for_each_safe(pos, nx, head) { |
| 702 | f = list_entry(pos, struct frame, head); |
| 703 | t = f->t; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 704 | if (t->taint) { |
| 705 | if (!(f->flags & FFL_PROBE)) { |
| 706 | nf = reassign_frame(f); |
| 707 | if (nf) { |
| 708 | if (t->nout_probes == 0 |
| 709 | && untainted > 0) { |
| 710 | probe(t); |
| 711 | t->nout_probes++; |
| 712 | } |
| 713 | list_replace(&f->head, &nf->head); |
| 714 | pos = &nf->head; |
| 715 | aoe_freetframe(f); |
| 716 | f = nf; |
| 717 | t = f->t; |
| 718 | } |
| 719 | } else if (untainted < 1) { |
| 720 | /* don't probe w/o other untainted aoetgts */ |
| 721 | goto stop_probe; |
| 722 | } else if (tsince_hr(f) < t->taint * rto(d)) { |
| 723 | /* reprobe slowly when taint is high */ |
| 724 | continue; |
| 725 | } |
| 726 | } else if (f->flags & FFL_PROBE) { |
| 727 | stop_probe: /* don't probe untainted aoetgts */ |
| 728 | list_del(pos); |
| 729 | aoe_freetframe(f); |
| 730 | /* leaving d->kicked, because this is routine */ |
| 731 | f->t->d->flags |= DEVFL_KICKME; |
| 732 | continue; |
| 733 | } |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 734 | if (t->nout >= t->maxout) |
| 735 | continue; |
| 736 | list_del(pos); |
| 737 | t->nout++; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 738 | if (f->flags & FFL_PROBE) |
| 739 | t->nout_probes++; |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 740 | since = tsince_hr(f); |
| 741 | f->waited += since; |
| 742 | f->waited_total += since; |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 743 | resend(d, f); |
| 744 | } |
| 745 | } |
| 746 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 747 | /* An aoetgt accumulates demerits quickly, and successful |
| 748 | * probing redeems the aoetgt slowly. |
| 749 | */ |
| 750 | static void |
| 751 | scorn(struct aoetgt *t) |
| 752 | { |
| 753 | int n; |
| 754 | |
| 755 | n = t->taint++; |
| 756 | t->taint += t->taint * 2; |
| 757 | if (n > t->taint) |
| 758 | t->taint = n; |
| 759 | if (t->taint > MAX_TAINT) |
| 760 | t->taint = MAX_TAINT; |
| 761 | } |
| 762 | |
| 763 | static int |
| 764 | count_targets(struct aoedev *d, int *untainted) |
| 765 | { |
| 766 | int i, good; |
| 767 | |
| 768 | for (i = good = 0; i < d->ntargets && d->targets[i]; ++i) |
| 769 | if (d->targets[i]->taint == 0) |
| 770 | good++; |
| 771 | |
| 772 | if (untainted) |
| 773 | *untainted = good; |
| 774 | return i; |
| 775 | } |
| 776 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 777 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | rexmit_timer(ulong vp) |
| 779 | { |
| 780 | struct aoedev *d; |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 781 | struct aoetgt *t; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 782 | struct aoeif *ifp; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 783 | struct frame *f; |
| 784 | struct list_head *head, *pos, *nx; |
| 785 | LIST_HEAD(flist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | register long timeout; |
| 787 | ulong flags, n; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 788 | int i; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 789 | int utgts; /* number of aoetgt descriptors (not slots) */ |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 790 | int since; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | d = (struct aoedev *) vp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Ed Cashin | 0d555ec | 2012-12-17 16:03:47 -0800 | [diff] [blame] | 794 | spin_lock_irqsave(&d->lock, flags); |
| 795 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 796 | /* timeout based on observed timings and variations */ |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 797 | timeout = rto(d); |
| 798 | |
| 799 | utgts = count_targets(d, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | if (d->flags & DEVFL_TKILL) { |
Ed L. Cashin | 1c6f3fc | 2006-01-25 13:54:44 -0500 | [diff] [blame] | 802 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | return; |
| 804 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 805 | |
| 806 | /* collect all frames to rexmit into flist */ |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 807 | for (i = 0; i < NFACTIVE; i++) { |
| 808 | head = &d->factive[i]; |
| 809 | list_for_each_safe(pos, nx, head) { |
| 810 | f = list_entry(pos, struct frame, head); |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 811 | if (tsince_hr(f) < timeout) |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 812 | break; /* end of expired frames */ |
| 813 | /* move to flist for later processing */ |
| 814 | list_move_tail(pos, &flist); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 815 | } |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 816 | } |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 817 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 818 | /* process expired frames */ |
| 819 | while (!list_empty(&flist)) { |
| 820 | pos = flist.next; |
| 821 | f = list_entry(pos, struct frame, head); |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 822 | since = tsince_hr(f); |
| 823 | n = f->waited_total + since; |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 824 | n /= USEC_PER_SEC; |
Ed Cashin | c450ba0 | 2012-12-17 16:04:14 -0800 | [diff] [blame] | 825 | if (aoe_deadsecs |
| 826 | && n > aoe_deadsecs |
| 827 | && !(f->flags & FFL_PROBE)) { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 828 | /* Waited too long. Device failure. |
| 829 | * Hang all frames on first hash bucket for downdev |
| 830 | * to clean up. |
| 831 | */ |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 832 | list_splice(&flist, &d->factive[0]); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 833 | aoedev_downdev(d); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 834 | goto out; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 835 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 836 | |
| 837 | t = f->t; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 838 | n = f->waited + since; |
| 839 | n /= USEC_PER_SEC; |
| 840 | if (aoe_deadsecs && utgts > 0 |
| 841 | && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS)) |
| 842 | scorn(t); /* avoid this target */ |
Ed Cashin | d54d35a | 2012-10-04 17:16:29 -0700 | [diff] [blame] | 843 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 844 | if (t->maxout != 1) { |
| 845 | t->ssthresh = t->maxout / 2; |
| 846 | t->maxout = 1; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 849 | if (f->flags & FFL_PROBE) { |
| 850 | t->nout_probes--; |
| 851 | } else { |
| 852 | ifp = getif(t, f->skb->dev); |
| 853 | if (ifp && ++ifp->lost > (t->nframes << 1) |
| 854 | && (ifp != t->ifs || t->ifs[1].nd)) { |
| 855 | ejectif(t, ifp); |
| 856 | ifp = NULL; |
| 857 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 858 | } |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 859 | list_move_tail(pos, &d->rexmitq); |
| 860 | t->nout--; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 861 | } |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 862 | rexmit_deferred(d); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 863 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 864 | out: |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 865 | if ((d->flags & DEVFL_KICKME) && d->blkq) { |
Ed L. Cashin | 4f51dc5 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 866 | d->flags &= ~DEVFL_KICKME; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 867 | d->blkq->request_fn(d->blkq); |
Ed L. Cashin | 4f51dc5 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | d->timer.expires = jiffies + TIMERTICK; |
| 871 | add_timer(&d->timer); |
| 872 | |
| 873 | spin_unlock_irqrestore(&d->lock, flags); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 876 | static unsigned long |
| 877 | rqbiocnt(struct request *r) |
| 878 | { |
| 879 | struct bio *bio; |
| 880 | unsigned long n = 0; |
| 881 | |
| 882 | __rq_for_each_bio(bio, r) |
| 883 | n++; |
| 884 | return n; |
| 885 | } |
| 886 | |
| 887 | /* This can be removed if we are certain that no users of the block |
| 888 | * layer will ever use zero-count pages in bios. Otherwise we have to |
| 889 | * protect against the put_page sometimes done by the network layer. |
| 890 | * |
| 891 | * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for |
| 892 | * discussion. |
| 893 | * |
| 894 | * We cannot use get_page in the workaround, because it insists on a |
| 895 | * positive page count as a precondition. So we use _count directly. |
| 896 | */ |
| 897 | static void |
| 898 | bio_pageinc(struct bio *bio) |
| 899 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 900 | struct bio_vec bv; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 901 | struct page *page; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 902 | struct bvec_iter iter; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 903 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 904 | bio_for_each_segment(bv, bio, iter) { |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 905 | /* Non-zero page count for non-head members of |
Ed Cashin | fb32975d | 2013-08-13 16:00:53 -0700 | [diff] [blame] | 906 | * compound pages is no longer allowed by the kernel. |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 907 | */ |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 908 | page = compound_trans_head(bv.bv_page); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 909 | atomic_inc(&page->_count); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | static void |
| 914 | bio_pagedec(struct bio *bio) |
| 915 | { |
Ed Cashin | fb32975d | 2013-08-13 16:00:53 -0700 | [diff] [blame] | 916 | struct page *page; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 917 | struct bio_vec bv; |
| 918 | struct bvec_iter iter; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 919 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame^] | 920 | bio_for_each_segment(bv, bio, iter) { |
| 921 | page = compound_trans_head(bv.bv_page); |
Ed Cashin | fb32975d | 2013-08-13 16:00:53 -0700 | [diff] [blame] | 922 | atomic_dec(&page->_count); |
| 923 | } |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static void |
| 927 | bufinit(struct buf *buf, struct request *rq, struct bio *bio) |
| 928 | { |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 929 | memset(buf, 0, sizeof(*buf)); |
| 930 | buf->rq = rq; |
| 931 | buf->bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 932 | buf->resid = bio->bi_iter.bi_size; |
| 933 | buf->sector = bio->bi_iter.bi_sector; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 934 | bio_pageinc(bio); |
Kent Overstreet | a4ad39b1 | 2013-08-07 14:24:32 -0700 | [diff] [blame] | 935 | buf->bv = __bio_iovec(bio); |
Jens Axboe | 2124469 | 2013-03-25 15:27:26 -0600 | [diff] [blame] | 936 | buf->bv_resid = buf->bv->bv_len; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 937 | WARN_ON(buf->bv_resid == 0); |
| 938 | } |
| 939 | |
| 940 | static struct buf * |
| 941 | nextbuf(struct aoedev *d) |
| 942 | { |
| 943 | struct request *rq; |
| 944 | struct request_queue *q; |
| 945 | struct buf *buf; |
| 946 | struct bio *bio; |
| 947 | |
| 948 | q = d->blkq; |
| 949 | if (q == NULL) |
| 950 | return NULL; /* initializing */ |
| 951 | if (d->ip.buf) |
| 952 | return d->ip.buf; |
| 953 | rq = d->ip.rq; |
| 954 | if (rq == NULL) { |
| 955 | rq = blk_peek_request(q); |
| 956 | if (rq == NULL) |
| 957 | return NULL; |
| 958 | blk_start_request(rq); |
| 959 | d->ip.rq = rq; |
| 960 | d->ip.nxbio = rq->bio; |
| 961 | rq->special = (void *) rqbiocnt(rq); |
| 962 | } |
| 963 | buf = mempool_alloc(d->bufpool, GFP_ATOMIC); |
| 964 | if (buf == NULL) { |
| 965 | pr_err("aoe: nextbuf: unable to mempool_alloc!\n"); |
| 966 | return NULL; |
| 967 | } |
| 968 | bio = d->ip.nxbio; |
| 969 | bufinit(buf, rq, bio); |
| 970 | bio = bio->bi_next; |
| 971 | d->ip.nxbio = bio; |
| 972 | if (bio == NULL) |
| 973 | d->ip.rq = NULL; |
| 974 | return d->ip.buf = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 977 | /* enters with d->lock held */ |
| 978 | void |
| 979 | aoecmd_work(struct aoedev *d) |
| 980 | { |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 981 | rexmit_deferred(d); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 982 | while (aoecmd_ata_rw(d)) |
| 983 | ; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 986 | /* this function performs work that has been deferred until sleeping is OK |
| 987 | */ |
| 988 | void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 989 | aoecmd_sleepwork(struct work_struct *work) |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 990 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 991 | struct aoedev *d = container_of(work, struct aoedev, work); |
Ed Cashin | b21faa2 | 2012-10-04 17:16:35 -0700 | [diff] [blame] | 992 | struct block_device *bd; |
| 993 | u64 ssize; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 994 | |
| 995 | if (d->flags & DEVFL_GDALLOC) |
| 996 | aoeblk_gdalloc(d); |
| 997 | |
| 998 | if (d->flags & DEVFL_NEWSIZE) { |
Tejun Heo | 80795ae | 2008-08-25 19:56:07 +0900 | [diff] [blame] | 999 | ssize = get_capacity(d->gd); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1000 | bd = bdget_disk(d->gd, 0); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1001 | if (bd) { |
| 1002 | mutex_lock(&bd->bd_inode->i_mutex); |
| 1003 | i_size_write(bd->bd_inode, (loff_t)ssize<<9); |
| 1004 | mutex_unlock(&bd->bd_inode->i_mutex); |
| 1005 | bdput(bd); |
| 1006 | } |
Ed Cashin | b21faa2 | 2012-10-04 17:16:35 -0700 | [diff] [blame] | 1007 | spin_lock_irq(&d->lock); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1008 | d->flags |= DEVFL_UP; |
| 1009 | d->flags &= ~DEVFL_NEWSIZE; |
Ed Cashin | b21faa2 | 2012-10-04 17:16:35 -0700 | [diff] [blame] | 1010 | spin_unlock_irq(&d->lock); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | static void |
Ed Cashin | 667be1e | 2012-12-17 16:03:42 -0800 | [diff] [blame] | 1015 | ata_ident_fixstring(u16 *id, int ns) |
| 1016 | { |
| 1017 | u16 s; |
| 1018 | |
| 1019 | while (ns-- > 0) { |
| 1020 | s = *id; |
| 1021 | *id++ = s >> 8 | s << 8; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static void |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1026 | ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | { |
| 1028 | u64 ssize; |
| 1029 | u16 n; |
| 1030 | |
| 1031 | /* word 83: command set supported */ |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 1032 | n = get_unaligned_le16(&id[83 << 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | /* word 86: command set/feature enabled */ |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 1035 | n |= get_unaligned_le16(&id[86 << 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (n & (1<<10)) { /* bit 10: LBA 48 */ |
| 1038 | d->flags |= DEVFL_EXT; |
| 1039 | |
| 1040 | /* word 100: number lba48 sectors */ |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 1041 | ssize = get_unaligned_le64(&id[100 << 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | /* set as in ide-disk.c:init_idedisk_capacity */ |
| 1044 | d->geo.cylinders = ssize; |
| 1045 | d->geo.cylinders /= (255 * 63); |
| 1046 | d->geo.heads = 255; |
| 1047 | d->geo.sectors = 63; |
| 1048 | } else { |
| 1049 | d->flags &= ~DEVFL_EXT; |
| 1050 | |
| 1051 | /* number lba28 sectors */ |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 1052 | ssize = get_unaligned_le32(&id[60 << 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* NOTE: obsolete in ATA 6 */ |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 1055 | d->geo.cylinders = get_unaligned_le16(&id[54 << 1]); |
| 1056 | d->geo.heads = get_unaligned_le16(&id[55 << 1]); |
| 1057 | d->geo.sectors = get_unaligned_le16(&id[56 << 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1059 | |
Ed Cashin | 667be1e | 2012-12-17 16:03:42 -0800 | [diff] [blame] | 1060 | ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */ |
| 1061 | ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */ |
| 1062 | ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */ |
| 1063 | memcpy(d->ident, id, sizeof(d->ident)); |
| 1064 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1065 | if (d->ssize != ssize) |
Ed L. Cashin | 1d75981 | 2008-02-08 04:20:08 -0800 | [diff] [blame] | 1066 | printk(KERN_INFO |
Harvey Harrison | 411c41e | 2008-11-25 00:40:37 -0800 | [diff] [blame] | 1067 | "aoe: %pm e%ld.%d v%04x has %llu sectors\n", |
| 1068 | t->addr, |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1069 | d->aoemajor, d->aoeminor, |
| 1070 | d->fw_ver, (long long)ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | d->ssize = ssize; |
| 1072 | d->geo.start = 0; |
Ed L. Cashin | 6b9699b | 2008-02-08 04:20:06 -0800 | [diff] [blame] | 1073 | if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) |
| 1074 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (d->gd != NULL) { |
Tejun Heo | 80795ae | 2008-08-25 19:56:07 +0900 | [diff] [blame] | 1076 | set_capacity(d->gd, ssize); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1077 | d->flags |= DEVFL_NEWSIZE; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1078 | } else |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1079 | d->flags |= DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | schedule_work(&d->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1084 | calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
| 1086 | register long n; |
| 1087 | |
| 1088 | n = rtt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1090 | /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */ |
| 1091 | n -= d->rttavg >> RTTSCALE; |
| 1092 | d->rttavg += n; |
| 1093 | if (n < 0) |
| 1094 | n = -n; |
| 1095 | n -= d->rttdev >> RTTDSCALE; |
| 1096 | d->rttdev += n; |
| 1097 | |
| 1098 | if (!t || t->maxout >= t->nframes) |
| 1099 | return; |
| 1100 | if (t->maxout < t->ssthresh) |
| 1101 | t->maxout += 1; |
| 1102 | else if (t->nout == t->maxout && t->next_cwnd-- == 0) { |
| 1103 | t->maxout += 1; |
| 1104 | t->next_cwnd = t->maxout; |
| 1105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1108 | static struct aoetgt * |
| 1109 | gettgt(struct aoedev *d, char *addr) |
| 1110 | { |
| 1111 | struct aoetgt **t, **e; |
| 1112 | |
| 1113 | t = d->targets; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1114 | e = t + d->ntargets; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1115 | for (; t < e && *t; t++) |
| 1116 | if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) |
| 1117 | return *t; |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 1121 | static void |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1122 | bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt) |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 1123 | { |
| 1124 | ulong fcnt; |
| 1125 | char *p; |
| 1126 | int soff = 0; |
| 1127 | loop: |
| 1128 | fcnt = bv->bv_len - (off - bv->bv_offset); |
| 1129 | if (fcnt > cnt) |
| 1130 | fcnt = cnt; |
| 1131 | p = page_address(bv->bv_page) + off; |
| 1132 | skb_copy_bits(skb, soff, p, fcnt); |
| 1133 | soff += fcnt; |
| 1134 | cnt -= fcnt; |
| 1135 | if (cnt <= 0) |
| 1136 | return; |
| 1137 | bv++; |
| 1138 | off = bv->bv_offset; |
| 1139 | goto loop; |
| 1140 | } |
| 1141 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1142 | void |
| 1143 | aoe_end_request(struct aoedev *d, struct request *rq, int fastfail) |
| 1144 | { |
| 1145 | struct bio *bio; |
| 1146 | int bok; |
| 1147 | struct request_queue *q; |
| 1148 | |
| 1149 | q = d->blkq; |
| 1150 | if (rq == d->ip.rq) |
| 1151 | d->ip.rq = NULL; |
| 1152 | do { |
| 1153 | bio = rq->bio; |
| 1154 | bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1155 | } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_iter.bi_size)); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1156 | |
| 1157 | /* cf. http://lkml.org/lkml/2006/10/31/28 */ |
| 1158 | if (!fastfail) |
Ed Cashin | 11cfb6f | 2012-11-08 19:17:15 -0500 | [diff] [blame] | 1159 | __blk_run_queue(q); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | static void |
| 1163 | aoe_end_buf(struct aoedev *d, struct buf *buf) |
| 1164 | { |
| 1165 | struct request *rq; |
| 1166 | unsigned long n; |
| 1167 | |
| 1168 | if (buf == d->ip.buf) |
| 1169 | d->ip.buf = NULL; |
| 1170 | rq = buf->rq; |
| 1171 | bio_pagedec(buf->bio); |
| 1172 | mempool_free(buf, d->bufpool); |
| 1173 | n = (unsigned long) rq->special; |
| 1174 | rq->special = (void *) --n; |
| 1175 | if (n == 0) |
| 1176 | aoe_end_request(d, rq, 0); |
| 1177 | } |
| 1178 | |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 1179 | static void |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1180 | ktiocomplete(struct frame *f) |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 1181 | { |
Ed L. Cashin | ddec63e | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 1182 | struct aoe_hdr *hin, *hout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | struct aoe_atahdr *ahin, *ahout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | struct buf *buf; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1185 | struct sk_buff *skb; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1186 | struct aoetgt *t; |
| 1187 | struct aoeif *ifp; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1188 | struct aoedev *d; |
| 1189 | long n; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1190 | int untainted; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (f == NULL) |
| 1193 | return; |
| 1194 | |
| 1195 | t = f->t; |
| 1196 | d = t->d; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1197 | skb = f->r_skb; |
| 1198 | buf = f->buf; |
| 1199 | if (f->flags & FFL_PROBE) |
| 1200 | goto out; |
| 1201 | if (!skb) /* just fail the buf. */ |
| 1202 | goto noskb; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1203 | |
| 1204 | hout = (struct aoe_hdr *) skb_mac_header(f->skb); |
| 1205 | ahout = (struct aoe_atahdr *) (hout+1); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1206 | |
| 1207 | hin = (struct aoe_hdr *) skb->data; |
| 1208 | skb_pull(skb, sizeof(*hin)); |
| 1209 | ahin = (struct aoe_atahdr *) skb->data; |
| 1210 | skb_pull(skb, sizeof(*ahin)); |
| 1211 | if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */ |
| 1212 | pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n", |
| 1213 | ahout->cmdstat, ahin->cmdstat, |
| 1214 | d->aoemajor, d->aoeminor); |
Ed Cashin | a04b41c | 2012-12-17 16:03:39 -0800 | [diff] [blame] | 1215 | noskb: if (buf) |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1216 | clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1217 | goto out; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | n = ahout->scnt << 9; |
| 1221 | switch (ahout->cmdstat) { |
| 1222 | case ATA_CMD_PIO_READ: |
| 1223 | case ATA_CMD_PIO_READ_EXT: |
| 1224 | if (skb->len < n) { |
Ed Cashin | bf29754 | 2012-12-17 16:04:17 -0800 | [diff] [blame] | 1225 | pr_err("%s e%ld.%d. skb->len=%d need=%ld\n", |
| 1226 | "aoe: runt data size in read from", |
| 1227 | (long) d->aoemajor, d->aoeminor, |
| 1228 | skb->len, n); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1229 | clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | bvcpy(f->bv, f->bv_off, skb, n); |
| 1233 | case ATA_CMD_PIO_WRITE: |
| 1234 | case ATA_CMD_PIO_WRITE_EXT: |
| 1235 | spin_lock_irq(&d->lock); |
| 1236 | ifp = getif(t, skb->dev); |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1237 | if (ifp) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1238 | ifp->lost = 0; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1239 | spin_unlock_irq(&d->lock); |
| 1240 | break; |
| 1241 | case ATA_CMD_ID_ATA: |
| 1242 | if (skb->len < 512) { |
Ed Cashin | bf29754 | 2012-12-17 16:04:17 -0800 | [diff] [blame] | 1243 | pr_info("%s e%ld.%d. skb->len=%d need=512\n", |
| 1244 | "aoe: runt data size in ataid from", |
| 1245 | (long) d->aoemajor, d->aoeminor, |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1246 | skb->len); |
| 1247 | break; |
| 1248 | } |
| 1249 | if (skb_linearize(skb)) |
| 1250 | break; |
| 1251 | spin_lock_irq(&d->lock); |
| 1252 | ataid_complete(d, t, skb->data); |
| 1253 | spin_unlock_irq(&d->lock); |
| 1254 | break; |
| 1255 | default: |
| 1256 | pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n", |
| 1257 | ahout->cmdstat, |
| 1258 | be16_to_cpu(get_unaligned(&hin->major)), |
| 1259 | hin->minor); |
| 1260 | } |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1261 | out: |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1262 | spin_lock_irq(&d->lock); |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1263 | if (t->taint > 0 |
| 1264 | && --t->taint > 0 |
| 1265 | && t->nout_probes == 0) { |
| 1266 | count_targets(d, &untainted); |
| 1267 | if (untainted > 0) { |
| 1268 | probe(t); |
| 1269 | t->nout_probes++; |
| 1270 | } |
| 1271 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1272 | |
| 1273 | aoe_freetframe(f); |
| 1274 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1275 | if (buf && --buf->nframesout == 0 && buf->resid == 0) |
| 1276 | aoe_end_buf(d, buf); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1277 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1278 | spin_unlock_irq(&d->lock); |
| 1279 | aoedev_put(d); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1280 | dev_kfree_skb(skb); |
| 1281 | } |
| 1282 | |
| 1283 | /* Enters with iocq.lock held. |
| 1284 | * Returns true iff responses needing processing remain. |
| 1285 | */ |
| 1286 | static int |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1287 | ktio(int id) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1288 | { |
| 1289 | struct frame *f; |
| 1290 | struct list_head *pos; |
| 1291 | int i; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1292 | int actual_id; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1293 | |
| 1294 | for (i = 0; ; ++i) { |
| 1295 | if (i == MAXIOC) |
| 1296 | return 1; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1297 | if (list_empty(&iocq[id].head)) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1298 | return 0; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1299 | pos = iocq[id].head.next; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1300 | list_del(pos); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1301 | f = list_entry(pos, struct frame, head); |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1302 | spin_unlock_irq(&iocq[id].lock); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1303 | ktiocomplete(f); |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1304 | |
| 1305 | /* Figure out if extra threads are required. */ |
| 1306 | actual_id = f->t->d->aoeminor % ncpus; |
| 1307 | |
| 1308 | if (!kts[actual_id].active) { |
| 1309 | BUG_ON(id != 0); |
| 1310 | mutex_lock(&ktio_spawn_lock); |
| 1311 | if (!kts[actual_id].active |
| 1312 | && aoe_ktstart(&kts[actual_id]) == 0) |
| 1313 | kts[actual_id].active = 1; |
| 1314 | mutex_unlock(&ktio_spawn_lock); |
| 1315 | } |
| 1316 | spin_lock_irq(&iocq[id].lock); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | static int |
| 1321 | kthread(void *vp) |
| 1322 | { |
| 1323 | struct ktstate *k; |
| 1324 | DECLARE_WAITQUEUE(wait, current); |
| 1325 | int more; |
| 1326 | |
| 1327 | k = vp; |
| 1328 | current->flags |= PF_NOFREEZE; |
| 1329 | set_user_nice(current, -10); |
| 1330 | complete(&k->rendez); /* tell spawner we're running */ |
| 1331 | do { |
| 1332 | spin_lock_irq(k->lock); |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1333 | more = k->fn(k->id); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1334 | if (!more) { |
| 1335 | add_wait_queue(k->waitq, &wait); |
| 1336 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1337 | } |
| 1338 | spin_unlock_irq(k->lock); |
| 1339 | if (!more) { |
| 1340 | schedule(); |
| 1341 | remove_wait_queue(k->waitq, &wait); |
| 1342 | } else |
| 1343 | cond_resched(); |
| 1344 | } while (!kthread_should_stop()); |
| 1345 | complete(&k->rendez); /* tell spawner we're stopping */ |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 1349 | void |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1350 | aoe_ktstop(struct ktstate *k) |
| 1351 | { |
| 1352 | kthread_stop(k->task); |
| 1353 | wait_for_completion(&k->rendez); |
| 1354 | } |
| 1355 | |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 1356 | int |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1357 | aoe_ktstart(struct ktstate *k) |
| 1358 | { |
| 1359 | struct task_struct *task; |
| 1360 | |
| 1361 | init_completion(&k->rendez); |
Kees Cook | f170168 | 2013-07-03 15:04:58 -0700 | [diff] [blame] | 1362 | task = kthread_run(kthread, k, "%s", k->name); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1363 | if (task == NULL || IS_ERR(task)) |
| 1364 | return -ENOMEM; |
| 1365 | k->task = task; |
| 1366 | wait_for_completion(&k->rendez); /* allow kthread to start */ |
| 1367 | init_completion(&k->rendez); /* for waiting for exit later */ |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | /* pass it off to kthreads for processing */ |
| 1372 | static void |
| 1373 | ktcomplete(struct frame *f, struct sk_buff *skb) |
| 1374 | { |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1375 | int id; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1376 | ulong flags; |
| 1377 | |
| 1378 | f->r_skb = skb; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1379 | id = f->t->d->aoeminor % ncpus; |
| 1380 | spin_lock_irqsave(&iocq[id].lock, flags); |
| 1381 | if (!kts[id].active) { |
| 1382 | spin_unlock_irqrestore(&iocq[id].lock, flags); |
| 1383 | /* The thread with id has not been spawned yet, |
| 1384 | * so delegate the work to the main thread and |
| 1385 | * try spawning a new thread. |
| 1386 | */ |
| 1387 | id = 0; |
| 1388 | spin_lock_irqsave(&iocq[id].lock, flags); |
| 1389 | } |
| 1390 | list_add_tail(&f->head, &iocq[id].head); |
| 1391 | spin_unlock_irqrestore(&iocq[id].lock, flags); |
| 1392 | wake_up(&ktiowq[id]); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | struct sk_buff * |
| 1396 | aoecmd_ata_rsp(struct sk_buff *skb) |
| 1397 | { |
| 1398 | struct aoedev *d; |
| 1399 | struct aoe_hdr *h; |
| 1400 | struct frame *f; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1401 | u32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | ulong flags; |
| 1403 | char ebuf[128]; |
ecashin@coraid.com | 32465c6 | 2005-04-18 22:00:18 -0700 | [diff] [blame] | 1404 | u16 aoemajor; |
| 1405 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1406 | h = (struct aoe_hdr *) skb->data; |
| 1407 | aoemajor = be16_to_cpu(get_unaligned(&h->major)); |
Ed Cashin | 0c96621 | 2012-10-04 17:16:40 -0700 | [diff] [blame] | 1408 | d = aoedev_by_aoeaddr(aoemajor, h->minor, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | if (d == NULL) { |
| 1410 | snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " |
| 1411 | "for unknown device %d.%d\n", |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1412 | aoemajor, h->minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | aoechr_error(ebuf); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1414 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | spin_lock_irqsave(&d->lock, flags); |
| 1418 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1419 | n = be32_to_cpu(get_unaligned(&h->tag)); |
Ed Cashin | 64a80f5 | 2012-10-04 17:16:33 -0700 | [diff] [blame] | 1420 | f = getframe(d, n); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1421 | if (f) { |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 1422 | calc_rttavg(d, f->t, tsince_hr(f)); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1423 | f->t->nout--; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1424 | if (f->flags & FFL_PROBE) |
| 1425 | f->t->nout_probes--; |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1426 | } else { |
| 1427 | f = getframe_deferred(d, n); |
| 1428 | if (f) { |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 1429 | calc_rttavg(d, NULL, tsince_hr(f)); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1430 | } else { |
| 1431 | calc_rttavg(d, NULL, tsince(n)); |
| 1432 | spin_unlock_irqrestore(&d->lock, flags); |
| 1433 | aoedev_put(d); |
| 1434 | snprintf(ebuf, sizeof(ebuf), |
Ed Cashin | 2292a7e | 2012-12-17 16:03:45 -0800 | [diff] [blame] | 1435 | "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n", |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1436 | "unexpected rsp", |
| 1437 | get_unaligned_be16(&h->major), |
| 1438 | h->minor, |
| 1439 | get_unaligned_be32(&h->tag), |
Ed Cashin | 2292a7e | 2012-12-17 16:03:45 -0800 | [diff] [blame] | 1440 | jiffies, |
| 1441 | h->src, |
| 1442 | h->dst); |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1443 | aoechr_error(ebuf); |
| 1444 | return skb; |
| 1445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | aoecmd_work(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
| 1449 | spin_unlock_irqrestore(&d->lock, flags); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1450 | |
| 1451 | ktcomplete(f, skb); |
| 1452 | |
| 1453 | /* |
| 1454 | * Note here that we do not perform an aoedev_put, as we are |
| 1455 | * leaving this reference for the ktio to release. |
| 1456 | */ |
| 1457 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | void |
| 1461 | aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) |
| 1462 | { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 1463 | struct sk_buff_head queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 1465 | __skb_queue_head_init(&queue); |
| 1466 | aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); |
| 1467 | aoenet_xmit(&queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
Ed Cashin | a04b41c | 2012-12-17 16:03:39 -0800 | [diff] [blame] | 1469 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1470 | struct sk_buff * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | aoecmd_ata_id(struct aoedev *d) |
| 1472 | { |
| 1473 | struct aoe_hdr *h; |
| 1474 | struct aoe_atahdr *ah; |
| 1475 | struct frame *f; |
| 1476 | struct sk_buff *skb; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1477 | struct aoetgt *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1479 | f = newframe(d); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1480 | if (f == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | return NULL; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1482 | |
| 1483 | t = *d->tgt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
| 1485 | /* initialize the headers & frame */ |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 1486 | skb = f->skb; |
Ed L. Cashin | abdbf94 | 2007-10-16 23:27:03 -0700 | [diff] [blame] | 1487 | h = (struct aoe_hdr *) skb_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | ah = (struct aoe_atahdr *) (h+1); |
Ed L. Cashin | 19900cd | 2006-12-22 01:09:21 -0800 | [diff] [blame] | 1489 | skb_put(skb, sizeof *h + sizeof *ah); |
| 1490 | memset(h, 0, skb->len); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1491 | f->tag = aoehdr_atainit(d, t, h); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1492 | fhash(f); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1493 | t->nout++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | f->waited = 0; |
Ed Cashin | 3fc9b03 | 2012-12-17 16:03:51 -0800 | [diff] [blame] | 1495 | f->waited_total = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | /* set up ata header */ |
| 1498 | ah->scnt = 1; |
Bartlomiej Zolnierkiewicz | 04b3ab5 | 2009-04-01 21:42:24 +0200 | [diff] [blame] | 1499 | ah->cmdstat = ATA_CMD_ID_ATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | ah->lba3 = 0xa0; |
| 1501 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1502 | skb->dev = t->ifp->nd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1504 | d->rttavg = RTTAVG_INIT; |
| 1505 | d->rttdev = RTTDEV_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | d->timer.function = rexmit_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Ed Cashin | 5f0c9c4 | 2012-12-17 16:03:49 -0800 | [diff] [blame] | 1508 | skb = skb_clone(skb, GFP_ATOMIC); |
| 1509 | if (skb) { |
| 1510 | do_gettimeofday(&f->sent); |
| 1511 | f->sent_jiffs = (u32) jiffies; |
| 1512 | } |
| 1513 | |
| 1514 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
Ed Cashin | a04b41c | 2012-12-17 16:03:39 -0800 | [diff] [blame] | 1516 | |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1517 | static struct aoetgt ** |
| 1518 | grow_targets(struct aoedev *d) |
| 1519 | { |
| 1520 | ulong oldn, newn; |
| 1521 | struct aoetgt **tt; |
| 1522 | |
| 1523 | oldn = d->ntargets; |
| 1524 | newn = oldn * 2; |
| 1525 | tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC); |
| 1526 | if (!tt) |
| 1527 | return NULL; |
| 1528 | memmove(tt, d->targets, sizeof(*d->targets) * oldn); |
| 1529 | d->tgt = tt + (d->tgt - d->targets); |
| 1530 | kfree(d->targets); |
| 1531 | d->targets = tt; |
| 1532 | d->ntargets = newn; |
| 1533 | |
| 1534 | return &d->targets[oldn]; |
| 1535 | } |
| 1536 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1537 | static struct aoetgt * |
| 1538 | addtgt(struct aoedev *d, char *addr, ulong nframes) |
| 1539 | { |
| 1540 | struct aoetgt *t, **tt, **te; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1541 | |
| 1542 | tt = d->targets; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1543 | te = tt + d->ntargets; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1544 | for (; tt < te && *tt; tt++) |
| 1545 | ; |
| 1546 | |
Ed L. Cashin | 578c4aa | 2008-02-08 04:20:09 -0800 | [diff] [blame] | 1547 | if (tt == te) { |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1548 | tt = grow_targets(d); |
| 1549 | if (!tt) |
| 1550 | goto nomem; |
Ed L. Cashin | 578c4aa | 2008-02-08 04:20:09 -0800 | [diff] [blame] | 1551 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1552 | t = kzalloc(sizeof(*t), GFP_ATOMIC); |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1553 | if (!t) |
| 1554 | goto nomem; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1555 | t->nframes = nframes; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1556 | t->d = d; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1557 | memcpy(t->addr, addr, sizeof t->addr); |
| 1558 | t->ifp = t->ifs; |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1559 | aoecmd_wreset(t); |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1560 | t->maxout = t->nframes / 2; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1561 | INIT_LIST_HEAD(&t->ffree); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1562 | return *tt = t; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1563 | |
| 1564 | nomem: |
| 1565 | pr_info("aoe: cannot allocate memory to add target\n"); |
| 1566 | return NULL; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1569 | static void |
| 1570 | setdbcnt(struct aoedev *d) |
| 1571 | { |
| 1572 | struct aoetgt **t, **e; |
| 1573 | int bcnt = 0; |
| 1574 | |
| 1575 | t = d->targets; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1576 | e = t + d->ntargets; |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1577 | for (; t < e && *t; t++) |
| 1578 | if (bcnt == 0 || bcnt > (*t)->minbcnt) |
| 1579 | bcnt = (*t)->minbcnt; |
| 1580 | if (bcnt != d->maxbcnt) { |
| 1581 | d->maxbcnt = bcnt; |
| 1582 | pr_info("aoe: e%ld.%d: setting %d byte data frames\n", |
| 1583 | d->aoemajor, d->aoeminor, bcnt); |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | static void |
| 1588 | setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt) |
| 1589 | { |
| 1590 | struct aoedev *d; |
| 1591 | struct aoeif *p, *e; |
| 1592 | int minbcnt; |
| 1593 | |
| 1594 | d = t->d; |
| 1595 | minbcnt = bcnt; |
| 1596 | p = t->ifs; |
| 1597 | e = p + NAOEIFS; |
| 1598 | for (; p < e; p++) { |
| 1599 | if (p->nd == NULL) |
| 1600 | break; /* end of the valid interfaces */ |
| 1601 | if (p->nd == nd) { |
| 1602 | p->bcnt = bcnt; /* we're updating */ |
| 1603 | nd = NULL; |
| 1604 | } else if (minbcnt > p->bcnt) |
| 1605 | minbcnt = p->bcnt; /* find the min interface */ |
| 1606 | } |
| 1607 | if (nd) { |
| 1608 | if (p == e) { |
| 1609 | pr_err("aoe: device setifbcnt failure; too many interfaces.\n"); |
| 1610 | return; |
| 1611 | } |
Ed Cashin | 1b86fda | 2012-10-04 17:16:34 -0700 | [diff] [blame] | 1612 | dev_hold(nd); |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1613 | p->nd = nd; |
| 1614 | p->bcnt = bcnt; |
| 1615 | } |
| 1616 | t->minbcnt = minbcnt; |
| 1617 | setdbcnt(d); |
| 1618 | } |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | void |
| 1621 | aoecmd_cfg_rsp(struct sk_buff *skb) |
| 1622 | { |
| 1623 | struct aoedev *d; |
| 1624 | struct aoe_hdr *h; |
| 1625 | struct aoe_cfghdr *ch; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1626 | struct aoetgt *t; |
Ed Cashin | 0c96621 | 2012-10-04 17:16:40 -0700 | [diff] [blame] | 1627 | ulong flags, aoemajor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | struct sk_buff *sl; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1629 | struct sk_buff_head queue; |
Ed L. Cashin | 19bf263 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 1630 | u16 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1632 | sl = NULL; |
Ed L. Cashin | abdbf94 | 2007-10-16 23:27:03 -0700 | [diff] [blame] | 1633 | h = (struct aoe_hdr *) skb_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | ch = (struct aoe_cfghdr *) (h+1); |
| 1635 | |
| 1636 | /* |
| 1637 | * Enough people have their dip switches set backwards to |
| 1638 | * warrant a loud message for this special case. |
| 1639 | */ |
Harvey Harrison | 823ed72 | 2008-07-04 09:28:32 +0200 | [diff] [blame] | 1640 | aoemajor = get_unaligned_be16(&h->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | if (aoemajor == 0xfff) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 1642 | printk(KERN_ERR "aoe: Warning: shelf address is all ones. " |
Ed L. Cashin | 6bb6285f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 1643 | "Check shelf dip switches.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return; |
| 1645 | } |
Ed Cashin | 7159e96 | 2012-10-04 17:16:44 -0700 | [diff] [blame] | 1646 | if (aoemajor == 0xffff) { |
| 1647 | pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n", |
Ed Cashin | 0c96621 | 2012-10-04 17:16:40 -0700 | [diff] [blame] | 1648 | aoemajor, (int) h->minor); |
Ed Cashin | 6583303 | 2012-10-04 17:16:32 -0700 | [diff] [blame] | 1649 | return; |
| 1650 | } |
Ed Cashin | 7159e96 | 2012-10-04 17:16:44 -0700 | [diff] [blame] | 1651 | if (h->minor == 0xff) { |
| 1652 | pr_info("aoe: e%ld.%d: broadcast slot number invalid\n", |
| 1653 | aoemajor, (int) h->minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | return; |
| 1655 | } |
| 1656 | |
Ed L. Cashin | 19bf263 | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 1657 | n = be16_to_cpu(ch->bufcnt); |
Ed L. Cashin | 7df620d | 2008-02-08 04:20:07 -0800 | [diff] [blame] | 1658 | if (n > aoe_maxout) /* keep it reasonable */ |
| 1659 | n = aoe_maxout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
Ed Cashin | 7159e96 | 2012-10-04 17:16:44 -0700 | [diff] [blame] | 1661 | d = aoedev_by_aoeaddr(aoemajor, h->minor, 1); |
| 1662 | if (d == NULL) { |
| 1663 | pr_info("aoe: device allocation failure\n"); |
| 1664 | return; |
| 1665 | } |
| 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | spin_lock_irqsave(&d->lock, flags); |
| 1668 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1669 | t = gettgt(d, h->src); |
Ed Cashin | 1b8a163 | 2012-12-17 16:03:29 -0800 | [diff] [blame] | 1670 | if (t) { |
| 1671 | t->nframes = n; |
| 1672 | if (n < t->maxout) |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1673 | aoecmd_wreset(t); |
Ed Cashin | 1b8a163 | 2012-12-17 16:03:29 -0800 | [diff] [blame] | 1674 | } else { |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1675 | t = addtgt(d, h->src, n); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1676 | if (!t) |
| 1677 | goto bail; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1678 | } |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1679 | n = skb->dev->mtu; |
| 1680 | n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr); |
| 1681 | n /= 512; |
| 1682 | if (n > ch->scnt) |
| 1683 | n = ch->scnt; |
| 1684 | n = n ? n * 512 : DEFAULTBCNT; |
| 1685 | setifbcnt(t, skb->dev, n); |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 1686 | |
| 1687 | /* don't change users' perspective */ |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1688 | if (d->nopen == 0) { |
| 1689 | d->fw_ver = be16_to_cpu(ch->fwver); |
| 1690 | sl = aoecmd_ata_id(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1692 | bail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | spin_unlock_irqrestore(&d->lock, flags); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1694 | aoedev_put(d); |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 1695 | if (sl) { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 1696 | __skb_queue_head_init(&queue); |
| 1697 | __skb_queue_tail(&queue, sl); |
| 1698 | aoenet_xmit(&queue); |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1702 | void |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1703 | aoecmd_wreset(struct aoetgt *t) |
| 1704 | { |
| 1705 | t->maxout = 1; |
| 1706 | t->ssthresh = t->nframes / 2; |
| 1707 | t->next_cwnd = t->nframes; |
| 1708 | } |
| 1709 | |
| 1710 | void |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1711 | aoecmd_cleanslate(struct aoedev *d) |
| 1712 | { |
| 1713 | struct aoetgt **t, **te; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1714 | |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1715 | d->rttavg = RTTAVG_INIT; |
| 1716 | d->rttdev = RTTDEV_INIT; |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1717 | d->maxbcnt = 0; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1718 | |
| 1719 | t = d->targets; |
Ed Cashin | 71114ec | 2012-12-17 16:04:11 -0800 | [diff] [blame] | 1720 | te = t + d->ntargets; |
Ed Cashin | 3f0f013 | 2012-10-04 17:16:27 -0700 | [diff] [blame] | 1721 | for (; t < te && *t; t++) |
Ed Cashin | 3a0c40d | 2012-12-17 16:03:43 -0800 | [diff] [blame] | 1722 | aoecmd_wreset(*t); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 1723 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1724 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1725 | void |
| 1726 | aoe_failbuf(struct aoedev *d, struct buf *buf) |
| 1727 | { |
| 1728 | if (buf == NULL) |
| 1729 | return; |
| 1730 | buf->resid = 0; |
| 1731 | clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); |
| 1732 | if (buf->nframesout == 0) |
| 1733 | aoe_end_buf(d, buf); |
| 1734 | } |
| 1735 | |
| 1736 | void |
| 1737 | aoe_flush_iocq(void) |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1738 | { |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1739 | int i; |
| 1740 | |
| 1741 | for (i = 0; i < ncpus; i++) { |
| 1742 | if (kts[i].active) |
| 1743 | aoe_flush_iocq_by_index(i); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | void |
| 1748 | aoe_flush_iocq_by_index(int id) |
| 1749 | { |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1750 | struct frame *f; |
| 1751 | struct aoedev *d; |
| 1752 | LIST_HEAD(flist); |
| 1753 | struct list_head *pos; |
| 1754 | struct sk_buff *skb; |
| 1755 | ulong flags; |
| 1756 | |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1757 | spin_lock_irqsave(&iocq[id].lock, flags); |
| 1758 | list_splice_init(&iocq[id].head, &flist); |
| 1759 | spin_unlock_irqrestore(&iocq[id].lock, flags); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1760 | while (!list_empty(&flist)) { |
| 1761 | pos = flist.next; |
| 1762 | list_del(pos); |
| 1763 | f = list_entry(pos, struct frame, head); |
| 1764 | d = f->t->d; |
| 1765 | skb = f->r_skb; |
| 1766 | spin_lock_irqsave(&d->lock, flags); |
| 1767 | if (f->buf) { |
| 1768 | f->buf->nframesout--; |
| 1769 | aoe_failbuf(d, f->buf); |
| 1770 | } |
| 1771 | aoe_freetframe(f); |
| 1772 | spin_unlock_irqrestore(&d->lock, flags); |
| 1773 | dev_kfree_skb(skb); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1774 | aoedev_put(d); |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | int __init |
| 1779 | aoecmd_init(void) |
| 1780 | { |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1781 | void *p; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1782 | int i; |
| 1783 | int ret; |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1784 | |
| 1785 | /* get_zeroed_page returns page with ref count 1 */ |
| 1786 | p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT); |
| 1787 | if (!p) |
| 1788 | return -ENOMEM; |
| 1789 | empty_page = virt_to_page(p); |
| 1790 | |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1791 | ncpus = num_online_cpus(); |
| 1792 | |
| 1793 | iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL); |
| 1794 | if (!iocq) |
| 1795 | return -ENOMEM; |
| 1796 | |
| 1797 | kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL); |
| 1798 | if (!kts) { |
| 1799 | ret = -ENOMEM; |
| 1800 | goto kts_fail; |
| 1801 | } |
| 1802 | |
| 1803 | ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL); |
| 1804 | if (!ktiowq) { |
| 1805 | ret = -ENOMEM; |
| 1806 | goto ktiowq_fail; |
| 1807 | } |
| 1808 | |
| 1809 | mutex_init(&ktio_spawn_lock); |
| 1810 | |
| 1811 | for (i = 0; i < ncpus; i++) { |
| 1812 | INIT_LIST_HEAD(&iocq[i].head); |
| 1813 | spin_lock_init(&iocq[i].lock); |
| 1814 | init_waitqueue_head(&ktiowq[i]); |
| 1815 | snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i); |
| 1816 | kts[i].fn = ktio; |
| 1817 | kts[i].waitq = &ktiowq[i]; |
| 1818 | kts[i].lock = &iocq[i].lock; |
| 1819 | kts[i].id = i; |
| 1820 | kts[i].active = 0; |
| 1821 | } |
| 1822 | kts[0].active = 1; |
| 1823 | if (aoe_ktstart(&kts[0])) { |
| 1824 | ret = -ENOMEM; |
| 1825 | goto ktstart_fail; |
| 1826 | } |
| 1827 | return 0; |
| 1828 | |
| 1829 | ktstart_fail: |
| 1830 | kfree(ktiowq); |
| 1831 | ktiowq_fail: |
| 1832 | kfree(kts); |
| 1833 | kts_fail: |
| 1834 | kfree(iocq); |
| 1835 | |
| 1836 | return ret; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | void |
| 1840 | aoecmd_exit(void) |
| 1841 | { |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1842 | int i; |
| 1843 | |
| 1844 | for (i = 0; i < ncpus; i++) |
| 1845 | if (kts[i].active) |
| 1846 | aoe_ktstop(&kts[i]); |
| 1847 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 1848 | aoe_flush_iocq(); |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1849 | |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 1850 | /* Free up the iocq and thread speicific configuration |
| 1851 | * allocated during startup. |
| 1852 | */ |
| 1853 | kfree(iocq); |
| 1854 | kfree(kts); |
| 1855 | kfree(ktiowq); |
| 1856 | |
Ed Cashin | bbb44e3 | 2012-12-17 16:04:08 -0800 | [diff] [blame] | 1857 | free_page((unsigned long) page_address(empty_page)); |
| 1858 | empty_page = NULL; |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 1859 | } |