Commit | Line | Data |
---|---|---|
ca47bbd9 | 1 | /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */ |
1da177e4 LT |
2 | /* |
3 | * aoecmd.c | |
4 | * Filesystem request handling methods | |
5 | */ | |
6 | ||
04b3ab52 | 7 | #include <linux/ata.h> |
5a0e3ad6 | 8 | #include <linux/slab.h> |
1da177e4 LT |
9 | #include <linux/hdreg.h> |
10 | #include <linux/blkdev.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netdevice.h> | |
3ae1c24e | 13 | #include <linux/genhd.h> |
68e0d42f | 14 | #include <linux/moduleparam.h> |
896831f5 EC |
15 | #include <linux/workqueue.h> |
16 | #include <linux/kthread.h> | |
881d966b | 17 | #include <net/net_namespace.h> |
475172fb | 18 | #include <asm/unaligned.h> |
896831f5 | 19 | #include <linux/uio.h> |
1da177e4 LT |
20 | #include "aoe.h" |
21 | ||
896831f5 EC |
22 | #define MAXIOC (8192) /* default meant to avoid most soft lockups */ |
23 | ||
24 | static void ktcomplete(struct frame *, struct sk_buff *); | |
bbb44e30 | 25 | static int count_targets(struct aoedev *d, int *untainted); |
896831f5 | 26 | |
69cf2d85 EC |
27 | static struct buf *nextbuf(struct aoedev *); |
28 | ||
b751e8b6 EC |
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."); | |
1da177e4 | 32 | |
7b6ccc5f | 33 | static int aoe_maxout = 64; |
7df620d8 EC |
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 | ||
8030d343 EC |
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; | |
896831f5 EC |
52 | |
53 | /* io completion queue */ | |
8030d343 | 54 | struct iocq_ktio { |
896831f5 EC |
55 | struct list_head head; |
56 | spinlock_t lock; | |
8030d343 EC |
57 | }; |
58 | static struct iocq_ktio *iocq; | |
896831f5 | 59 | |
bbb44e30 EC |
60 | static struct page *empty_page; |
61 | ||
68e0d42f | 62 | static struct sk_buff * |
e407a7f6 | 63 | new_skb(ulong len) |
1da177e4 LT |
64 | { |
65 | struct sk_buff *skb; | |
66 | ||
91c57464 | 67 | skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC); |
1da177e4 | 68 | if (skb) { |
91c57464 | 69 | skb_reserve(skb, MAX_HEADER); |
459a98ed | 70 | skb_reset_mac_header(skb); |
c1d2bbe1 | 71 | skb_reset_network_header(skb); |
1da177e4 | 72 | skb->protocol = __constant_htons(ETH_P_AOE); |
8babe8cc | 73 | skb_checksum_none_assert(skb); |
1da177e4 LT |
74 | } |
75 | return skb; | |
76 | } | |
77 | ||
3a0c40d2 EC |
78 | static struct frame * |
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 | ||
1da177e4 | 95 | static struct frame * |
64a80f5a | 96 | getframe(struct aoedev *d, u32 tag) |
1da177e4 | 97 | { |
896831f5 EC |
98 | struct frame *f; |
99 | struct list_head *head, *pos, *nx; | |
100 | u32 n; | |
1da177e4 | 101 | |
896831f5 | 102 | n = tag % NFACTIVE; |
64a80f5a | 103 | head = &d->factive[n]; |
896831f5 EC |
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); | |
1da177e4 | 108 | return f; |
896831f5 EC |
109 | } |
110 | } | |
1da177e4 LT |
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 | |
64a80f5a | 120 | newtag(struct aoedev *d) |
1da177e4 LT |
121 | { |
122 | register ulong n; | |
123 | ||
124 | n = jiffies & 0xffff; | |
64a80f5a | 125 | return n |= (++d->lasttag & 0x7fff) << 16; |
1da177e4 LT |
126 | } |
127 | ||
896831f5 | 128 | static u32 |
68e0d42f | 129 | aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h) |
1da177e4 | 130 | { |
64a80f5a | 131 | u32 host_tag = newtag(d); |
1da177e4 | 132 | |
68e0d42f EC |
133 | memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); |
134 | memcpy(h->dst, t->addr, sizeof h->dst); | |
63e9cc5d | 135 | h->type = __constant_cpu_to_be16(ETH_P_AOE); |
1da177e4 | 136 | h->verfl = AOE_HVER; |
63e9cc5d | 137 | h->major = cpu_to_be16(d->aoemajor); |
1da177e4 LT |
138 | h->minor = d->aoeminor; |
139 | h->cmd = AOECMD_ATA; | |
63e9cc5d | 140 | h->tag = cpu_to_be32(host_tag); |
1da177e4 LT |
141 | |
142 | return host_tag; | |
143 | } | |
144 | ||
19bf2635 EC |
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 | ||
3f0f0133 | 156 | static struct aoeif * |
68e0d42f EC |
157 | ifrotate(struct aoetgt *t) |
158 | { | |
3f0f0133 EC |
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; | |
68e0d42f EC |
168 | } |
169 | ||
9bb237b6 EC |
170 | static void |
171 | skb_pool_put(struct aoedev *d, struct sk_buff *skb) | |
172 | { | |
e9bb8fb0 | 173 | __skb_queue_tail(&d->skbpool, skb); |
9bb237b6 EC |
174 | } |
175 | ||
176 | static struct sk_buff * | |
177 | skb_pool_get(struct aoedev *d) | |
178 | { | |
e9bb8fb0 | 179 | struct sk_buff *skb = skb_peek(&d->skbpool); |
9bb237b6 | 180 | |
9bb237b6 | 181 | if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) { |
e9bb8fb0 | 182 | __skb_unlink(skb, &d->skbpool); |
9bb237b6 EC |
183 | return skb; |
184 | } | |
e9bb8fb0 DM |
185 | if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX && |
186 | (skb = new_skb(ETH_ZLEN))) | |
9bb237b6 | 187 | return skb; |
e9bb8fb0 | 188 | |
9bb237b6 EC |
189 | return NULL; |
190 | } | |
191 | ||
896831f5 EC |
192 | void |
193 | aoe_freetframe(struct frame *f) | |
194 | { | |
195 | struct aoetgt *t; | |
196 | ||
197 | t = f->t; | |
198 | f->buf = NULL; | |
bbb44e30 | 199 | f->lba = 0; |
896831f5 EC |
200 | f->bv = NULL; |
201 | f->r_skb = NULL; | |
bbb44e30 | 202 | f->flags = 0; |
896831f5 EC |
203 | list_add(&f->head, &t->ffree); |
204 | } | |
205 | ||
68e0d42f | 206 | static struct frame * |
896831f5 | 207 | newtframe(struct aoedev *d, struct aoetgt *t) |
1da177e4 | 208 | { |
896831f5 | 209 | struct frame *f; |
9bb237b6 | 210 | struct sk_buff *skb; |
896831f5 EC |
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; | |
bbb44e30 EC |
256 | int use_tainted; |
257 | int has_untainted; | |
68e0d42f | 258 | |
71114ec4 | 259 | if (!d->targets || !d->targets[0]) { |
68e0d42f EC |
260 | printk(KERN_ERR "aoe: NULL TARGETS!\n"); |
261 | return NULL; | |
262 | } | |
896831f5 | 263 | tt = d->tgt; /* last used target */ |
bbb44e30 | 264 | for (use_tainted = 0, has_untainted = 0;;) { |
896831f5 | 265 | tt++; |
71114ec4 | 266 | if (tt >= &d->targets[d->ntargets] || !*tt) |
896831f5 EC |
267 | tt = d->targets; |
268 | t = *tt; | |
bbb44e30 EC |
269 | if (!t->taint) { |
270 | has_untainted = 1; | |
271 | totout += t->nout; | |
272 | } | |
896831f5 | 273 | if (t->nout < t->maxout |
bbb44e30 | 274 | && (use_tainted || !t->taint) |
896831f5 EC |
275 | && t->ifp->nd) { |
276 | f = newtframe(d, t); | |
277 | if (f) { | |
896831f5 | 278 | ifrotate(t); |
3f0f0133 | 279 | d->tgt = tt; |
68e0d42f EC |
280 | return f; |
281 | } | |
68e0d42f | 282 | } |
bbb44e30 EC |
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 | } | |
896831f5 EC |
289 | } |
290 | if (totout == 0) { | |
291 | d->kicked++; | |
292 | d->flags |= DEVFL_KICKME; | |
9bb237b6 | 293 | } |
68e0d42f EC |
294 | return NULL; |
295 | } | |
296 | ||
3d5b0605 EC |
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 | ||
896831f5 EC |
315 | static void |
316 | fhash(struct frame *f) | |
317 | { | |
64a80f5a | 318 | struct aoedev *d = f->t->d; |
896831f5 EC |
319 | u32 n; |
320 | ||
321 | n = f->tag % NFACTIVE; | |
64a80f5a | 322 | list_add_tail(&f->head, &d->factive[n]); |
896831f5 EC |
323 | } |
324 | ||
bbb44e30 EC |
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 | ||
68e0d42f EC |
378 | static int |
379 | aoecmd_ata_rw(struct aoedev *d) | |
380 | { | |
381 | struct frame *f; | |
1da177e4 LT |
382 | struct buf *buf; |
383 | struct sk_buff *skb; | |
69cf2d85 | 384 | struct sk_buff_head queue; |
3d5b0605 | 385 | ulong bcnt, fbcnt; |
1da177e4 | 386 | |
69cf2d85 EC |
387 | buf = nextbuf(d); |
388 | if (buf == NULL) | |
389 | return 0; | |
896831f5 | 390 | f = newframe(d); |
68e0d42f EC |
391 | if (f == NULL) |
392 | return 0; | |
3f0f0133 | 393 | bcnt = d->maxbcnt; |
68e0d42f EC |
394 | if (bcnt == 0) |
395 | bcnt = DEFAULTBCNT; | |
3d5b0605 EC |
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) { | |
69cf2d85 | 410 | d->ip.buf = NULL; |
3d5b0605 EC |
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 | ||
1da177e4 | 418 | /* initialize the headers & frame */ |
1da177e4 | 419 | f->buf = buf; |
19bf2635 | 420 | f->bcnt = bcnt; |
bbb44e30 | 421 | ata_rw_frameinit(f); |
1da177e4 LT |
422 | |
423 | /* mark all tracking fields and load out */ | |
424 | buf->nframesout += 1; | |
1da177e4 | 425 | buf->sector += bcnt >> 9; |
1da177e4 | 426 | |
bbb44e30 | 427 | skb = skb_clone(f->skb, GFP_ATOMIC); |
69cf2d85 | 428 | if (skb) { |
5f0c9c48 EC |
429 | do_gettimeofday(&f->sent); |
430 | f->sent_jiffs = (u32) jiffies; | |
69cf2d85 EC |
431 | __skb_queue_head_init(&queue); |
432 | __skb_queue_tail(&queue, skb); | |
433 | aoenet_xmit(&queue); | |
434 | } | |
68e0d42f | 435 | return 1; |
1da177e4 LT |
436 | } |
437 | ||
3ae1c24e EC |
438 | /* some callers cannot sleep, and they can call this function, |
439 | * transmitting the packets later, when interrupts are on | |
440 | */ | |
e9bb8fb0 DM |
441 | static void |
442 | aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue) | |
3ae1c24e EC |
443 | { |
444 | struct aoe_hdr *h; | |
445 | struct aoe_cfghdr *ch; | |
e9bb8fb0 | 446 | struct sk_buff *skb; |
3ae1c24e EC |
447 | struct net_device *ifp; |
448 | ||
840a185d ED |
449 | rcu_read_lock(); |
450 | for_each_netdev_rcu(&init_net, ifp) { | |
3ae1c24e EC |
451 | dev_hold(ifp); |
452 | if (!is_aoe_netif(ifp)) | |
7562f876 | 453 | goto cont; |
3ae1c24e | 454 | |
e407a7f6 | 455 | skb = new_skb(sizeof *h + sizeof *ch); |
3ae1c24e | 456 | if (skb == NULL) { |
a12c93f0 | 457 | printk(KERN_INFO "aoe: skb alloc failure\n"); |
7562f876 | 458 | goto cont; |
3ae1c24e | 459 | } |
19900cde | 460 | skb_put(skb, sizeof *h + sizeof *ch); |
e407a7f6 | 461 | skb->dev = ifp; |
e9bb8fb0 | 462 | __skb_queue_tail(queue, skb); |
abdbf94d | 463 | h = (struct aoe_hdr *) skb_mac_header(skb); |
3ae1c24e EC |
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 | ||
7562f876 PE |
474 | cont: |
475 | dev_put(ifp); | |
3ae1c24e | 476 | } |
840a185d | 477 | rcu_read_unlock(); |
3ae1c24e EC |
478 | } |
479 | ||
1da177e4 | 480 | static void |
896831f5 | 481 | resend(struct aoedev *d, struct frame *f) |
1da177e4 LT |
482 | { |
483 | struct sk_buff *skb; | |
69cf2d85 | 484 | struct sk_buff_head queue; |
1da177e4 | 485 | struct aoe_hdr *h; |
896831f5 | 486 | struct aoetgt *t; |
1da177e4 LT |
487 | char buf[128]; |
488 | u32 n; | |
1da177e4 | 489 | |
896831f5 | 490 | t = f->t; |
64a80f5a | 491 | n = newtag(d); |
68e0d42f | 492 | skb = f->skb; |
3f0f0133 EC |
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 | } | |
68e0d42f | 499 | h = (struct aoe_hdr *) skb_mac_header(skb); |
1da177e4 | 500 | |
bbb44e30 EC |
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 | } | |
1da177e4 | 509 | |
1da177e4 | 510 | f->tag = n; |
896831f5 | 511 | fhash(f); |
63e9cc5d | 512 | h->tag = cpu_to_be32(n); |
68e0d42f EC |
513 | memcpy(h->dst, t->addr, sizeof h->dst); |
514 | memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src); | |
515 | ||
68e0d42f | 516 | skb->dev = t->ifp->nd; |
4f51dc5e EC |
517 | skb = skb_clone(skb, GFP_ATOMIC); |
518 | if (skb == NULL) | |
519 | return; | |
5f0c9c48 EC |
520 | do_gettimeofday(&f->sent); |
521 | f->sent_jiffs = (u32) jiffies; | |
69cf2d85 EC |
522 | __skb_queue_head_init(&queue); |
523 | __skb_queue_tail(&queue, skb); | |
524 | aoenet_xmit(&queue); | |
1da177e4 LT |
525 | } |
526 | ||
5f0c9c48 EC |
527 | static int |
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 | ||
1da177e4 | 559 | static int |
896831f5 | 560 | tsince(u32 tag) |
1da177e4 LT |
561 | { |
562 | int n; | |
563 | ||
564 | n = jiffies & 0xffff; | |
565 | n -= tag & 0xffff; | |
566 | if (n < 0) | |
567 | n += 1<<16; | |
5f0c9c48 | 568 | return jiffies_to_usecs(n + 1); |
1da177e4 LT |
569 | } |
570 | ||
68e0d42f EC |
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 | ||
68e0d42f EC |
584 | static void |
585 | ejectif(struct aoetgt *t, struct aoeif *ifp) | |
586 | { | |
587 | struct aoeif *e; | |
1b86fda9 | 588 | struct net_device *nd; |
68e0d42f EC |
589 | ulong n; |
590 | ||
1b86fda9 | 591 | nd = ifp->nd; |
68e0d42f EC |
592 | e = t->ifs + NAOEIFS - 1; |
593 | n = (e - ifp) * sizeof *ifp; | |
594 | memmove(ifp, ifp+1, n); | |
595 | e->nd = NULL; | |
1b86fda9 | 596 | dev_put(nd); |
68e0d42f EC |
597 | } |
598 | ||
3fc9b032 | 599 | static struct frame * |
bbb44e30 | 600 | reassign_frame(struct frame *f) |
3fc9b032 | 601 | { |
3fc9b032 EC |
602 | struct frame *nf; |
603 | struct sk_buff *skb; | |
604 | ||
3fc9b032 EC |
605 | nf = newframe(f->t->d); |
606 | if (!nf) | |
607 | return NULL; | |
bbb44e30 EC |
608 | if (nf->t == f->t) { |
609 | aoe_freetframe(nf); | |
610 | return NULL; | |
611 | } | |
3fc9b032 EC |
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; | |
fe7252bf | 623 | nf->sent_jiffs = f->sent_jiffs; |
3fc9b032 | 624 | f->skb = skb; |
3fc9b032 EC |
625 | |
626 | return nf; | |
627 | } | |
628 | ||
bbb44e30 EC |
629 | static void |
630 | probe(struct aoetgt *t) | |
68e0d42f | 631 | { |
bbb44e30 EC |
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; | |
896831f5 | 638 | |
bbb44e30 EC |
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; | |
68e0d42f | 648 | } |
bbb44e30 EC |
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); | |
3fc9b032 | 660 | } |
bbb44e30 EC |
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; | |
68e0d42f EC |
686 | } |
687 | ||
3a0c40d2 EC |
688 | static void |
689 | rexmit_deferred(struct aoedev *d) | |
690 | { | |
691 | struct aoetgt *t; | |
692 | struct frame *f; | |
bbb44e30 | 693 | struct frame *nf; |
3a0c40d2 | 694 | struct list_head *pos, *nx, *head; |
3fc9b032 | 695 | int since; |
bbb44e30 EC |
696 | int untainted; |
697 | ||
698 | count_targets(d, &untainted); | |
3a0c40d2 EC |
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; | |
bbb44e30 EC |
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 | } | |
3a0c40d2 EC |
734 | if (t->nout >= t->maxout) |
735 | continue; | |
736 | list_del(pos); | |
737 | t->nout++; | |
bbb44e30 EC |
738 | if (f->flags & FFL_PROBE) |
739 | t->nout_probes++; | |
3fc9b032 EC |
740 | since = tsince_hr(f); |
741 | f->waited += since; | |
742 | f->waited_total += since; | |
3a0c40d2 EC |
743 | resend(d, f); |
744 | } | |
745 | } | |
746 | ||
bbb44e30 EC |
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 | ||
1da177e4 LT |
777 | static void |
778 | rexmit_timer(ulong vp) | |
779 | { | |
780 | struct aoedev *d; | |
3a0c40d2 | 781 | struct aoetgt *t; |
68e0d42f | 782 | struct aoeif *ifp; |
896831f5 EC |
783 | struct frame *f; |
784 | struct list_head *head, *pos, *nx; | |
785 | LIST_HEAD(flist); | |
1da177e4 LT |
786 | register long timeout; |
787 | ulong flags, n; | |
896831f5 | 788 | int i; |
bbb44e30 | 789 | int utgts; /* number of aoetgt descriptors (not slots) */ |
3fc9b032 | 790 | int since; |
1da177e4 LT |
791 | |
792 | d = (struct aoedev *) vp; | |
1da177e4 | 793 | |
0d555ecf EC |
794 | spin_lock_irqsave(&d->lock, flags); |
795 | ||
3a0c40d2 | 796 | /* timeout based on observed timings and variations */ |
bbb44e30 EC |
797 | timeout = rto(d); |
798 | ||
799 | utgts = count_targets(d, NULL); | |
1da177e4 | 800 | |
1da177e4 | 801 | if (d->flags & DEVFL_TKILL) { |
1c6f3fca | 802 | spin_unlock_irqrestore(&d->lock, flags); |
1da177e4 LT |
803 | return; |
804 | } | |
896831f5 EC |
805 | |
806 | /* collect all frames to rexmit into flist */ | |
64a80f5a EC |
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); | |
5f0c9c48 | 811 | if (tsince_hr(f) < timeout) |
64a80f5a EC |
812 | break; /* end of expired frames */ |
813 | /* move to flist for later processing */ | |
814 | list_move_tail(pos, &flist); | |
68e0d42f | 815 | } |
64a80f5a | 816 | } |
69cf2d85 | 817 | |
896831f5 EC |
818 | /* process expired frames */ |
819 | while (!list_empty(&flist)) { | |
820 | pos = flist.next; | |
821 | f = list_entry(pos, struct frame, head); | |
3fc9b032 EC |
822 | since = tsince_hr(f); |
823 | n = f->waited_total + since; | |
5f0c9c48 | 824 | n /= USEC_PER_SEC; |
c450ba0f EC |
825 | if (aoe_deadsecs |
826 | && n > aoe_deadsecs | |
827 | && !(f->flags & FFL_PROBE)) { | |
896831f5 EC |
828 | /* Waited too long. Device failure. |
829 | * Hang all frames on first hash bucket for downdev | |
830 | * to clean up. | |
831 | */ | |
64a80f5a | 832 | list_splice(&flist, &d->factive[0]); |
896831f5 | 833 | aoedev_downdev(d); |
3a0c40d2 | 834 | goto out; |
896831f5 | 835 | } |
896831f5 EC |
836 | |
837 | t = f->t; | |
bbb44e30 EC |
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 */ | |
d54d35ac | 843 | |
3a0c40d2 EC |
844 | if (t->maxout != 1) { |
845 | t->ssthresh = t->maxout / 2; | |
846 | t->maxout = 1; | |
896831f5 EC |
847 | } |
848 | ||
bbb44e30 EC |
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 | } | |
896831f5 | 858 | } |
3a0c40d2 EC |
859 | list_move_tail(pos, &d->rexmitq); |
860 | t->nout--; | |
896831f5 | 861 | } |
3a0c40d2 | 862 | rexmit_deferred(d); |
896831f5 | 863 | |
3a0c40d2 | 864 | out: |
bbb44e30 | 865 | if ((d->flags & DEVFL_KICKME) && d->blkq) { |
4f51dc5e | 866 | d->flags &= ~DEVFL_KICKME; |
69cf2d85 | 867 | d->blkq->request_fn(d->blkq); |
4f51dc5e | 868 | } |
1da177e4 | 869 | |
1da177e4 LT |
870 | d->timer.expires = jiffies + TIMERTICK; |
871 | add_timer(&d->timer); | |
872 | ||
873 | spin_unlock_irqrestore(&d->lock, flags); | |
69cf2d85 | 874 | } |
1da177e4 | 875 | |
69cf2d85 EC |
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 | { | |
900 | struct bio_vec *bv; | |
901 | struct page *page; | |
902 | int i; | |
903 | ||
904 | bio_for_each_segment(bv, bio, i) { | |
69cf2d85 | 905 | /* Non-zero page count for non-head members of |
fb32975d | 906 | * compound pages is no longer allowed by the kernel. |
69cf2d85 | 907 | */ |
fb32975d | 908 | page = compound_trans_head(bv->bv_page); |
69cf2d85 EC |
909 | atomic_inc(&page->_count); |
910 | } | |
911 | } | |
912 | ||
913 | static void | |
914 | bio_pagedec(struct bio *bio) | |
915 | { | |
916 | struct bio_vec *bv; | |
fb32975d | 917 | struct page *page; |
69cf2d85 EC |
918 | int i; |
919 | ||
fb32975d EC |
920 | bio_for_each_segment(bv, bio, i) { |
921 | page = compound_trans_head(bv->bv_page); | |
922 | atomic_dec(&page->_count); | |
923 | } | |
69cf2d85 EC |
924 | } |
925 | ||
926 | static void | |
927 | bufinit(struct buf *buf, struct request *rq, struct bio *bio) | |
928 | { | |
69cf2d85 EC |
929 | memset(buf, 0, sizeof(*buf)); |
930 | buf->rq = rq; | |
931 | buf->bio = bio; | |
4f024f37 KO |
932 | buf->resid = bio->bi_iter.bi_size; |
933 | buf->sector = bio->bi_iter.bi_sector; | |
69cf2d85 | 934 | bio_pageinc(bio); |
ebb37277 | 935 | buf->bv = bio_iovec(bio); |
2124469e | 936 | buf->bv_resid = buf->bv->bv_len; |
69cf2d85 EC |
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; | |
1da177e4 LT |
975 | } |
976 | ||
68e0d42f EC |
977 | /* enters with d->lock held */ |
978 | void | |
979 | aoecmd_work(struct aoedev *d) | |
980 | { | |
3a0c40d2 | 981 | rexmit_deferred(d); |
69cf2d85 EC |
982 | while (aoecmd_ata_rw(d)) |
983 | ; | |
68e0d42f EC |
984 | } |
985 | ||
3ae1c24e EC |
986 | /* this function performs work that has been deferred until sleeping is OK |
987 | */ | |
988 | void | |
c4028958 | 989 | aoecmd_sleepwork(struct work_struct *work) |
3ae1c24e | 990 | { |
c4028958 | 991 | struct aoedev *d = container_of(work, struct aoedev, work); |
b21faa25 EC |
992 | struct block_device *bd; |
993 | u64 ssize; | |
3ae1c24e EC |
994 | |
995 | if (d->flags & DEVFL_GDALLOC) | |
996 | aoeblk_gdalloc(d); | |
997 | ||
998 | if (d->flags & DEVFL_NEWSIZE) { | |
80795aef | 999 | ssize = get_capacity(d->gd); |
3ae1c24e | 1000 | bd = bdget_disk(d->gd, 0); |
3ae1c24e EC |
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 | } | |
b21faa25 | 1007 | spin_lock_irq(&d->lock); |
3ae1c24e EC |
1008 | d->flags |= DEVFL_UP; |
1009 | d->flags &= ~DEVFL_NEWSIZE; | |
b21faa25 | 1010 | spin_unlock_irq(&d->lock); |
3ae1c24e EC |
1011 | } |
1012 | } | |
1013 | ||
667be1e7 EC |
1014 | static void |
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 | ||
1da177e4 | 1025 | static void |
68e0d42f | 1026 | ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id) |
1da177e4 LT |
1027 | { |
1028 | u64 ssize; | |
1029 | u16 n; | |
1030 | ||
1031 | /* word 83: command set supported */ | |
f885f8d1 | 1032 | n = get_unaligned_le16(&id[83 << 1]); |
1da177e4 LT |
1033 | |
1034 | /* word 86: command set/feature enabled */ | |
f885f8d1 | 1035 | n |= get_unaligned_le16(&id[86 << 1]); |
1da177e4 LT |
1036 | |
1037 | if (n & (1<<10)) { /* bit 10: LBA 48 */ | |
1038 | d->flags |= DEVFL_EXT; | |
1039 | ||
1040 | /* word 100: number lba48 sectors */ | |
f885f8d1 | 1041 | ssize = get_unaligned_le64(&id[100 << 1]); |
1da177e4 LT |
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 */ | |
f885f8d1 | 1052 | ssize = get_unaligned_le32(&id[60 << 1]); |
1da177e4 LT |
1053 | |
1054 | /* NOTE: obsolete in ATA 6 */ | |
f885f8d1 HH |
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]); | |
1da177e4 | 1058 | } |
3ae1c24e | 1059 | |
667be1e7 EC |
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 | ||
3ae1c24e | 1065 | if (d->ssize != ssize) |
1d75981a | 1066 | printk(KERN_INFO |
411c41ee HH |
1067 | "aoe: %pm e%ld.%d v%04x has %llu sectors\n", |
1068 | t->addr, | |
3ae1c24e EC |
1069 | d->aoemajor, d->aoeminor, |
1070 | d->fw_ver, (long long)ssize); | |
1da177e4 LT |
1071 | d->ssize = ssize; |
1072 | d->geo.start = 0; | |
6b9699bb EC |
1073 | if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE)) |
1074 | return; | |
1da177e4 | 1075 | if (d->gd != NULL) { |
80795aef | 1076 | set_capacity(d->gd, ssize); |
3ae1c24e | 1077 | d->flags |= DEVFL_NEWSIZE; |
68e0d42f | 1078 | } else |
3ae1c24e | 1079 | d->flags |= DEVFL_GDALLOC; |
1da177e4 | 1080 | schedule_work(&d->work); |
1da177e4 LT |
1081 | } |
1082 | ||
1083 | static void | |
3a0c40d2 | 1084 | calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt) |
1da177e4 LT |
1085 | { |
1086 | register long n; | |
1087 | ||
1088 | n = rtt; | |
3a0c40d2 EC |
1089 | |
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 | } | |
1da177e4 LT |
1106 | } |
1107 | ||
68e0d42f EC |
1108 | static struct aoetgt * |
1109 | gettgt(struct aoedev *d, char *addr) | |
1110 | { | |
1111 | struct aoetgt **t, **e; | |
1112 | ||
1113 | t = d->targets; | |
71114ec4 | 1114 | e = t + d->ntargets; |
68e0d42f EC |
1115 | for (; t < e && *t; t++) |
1116 | if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0) | |
1117 | return *t; | |
1118 | return NULL; | |
1119 | } | |
1120 | ||
3d5b0605 | 1121 | static void |
896831f5 | 1122 | bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt) |
3d5b0605 EC |
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 | ||
69cf2d85 EC |
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); | |
4f024f37 | 1155 | } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_iter.bi_size)); |
69cf2d85 EC |
1156 | |
1157 | /* cf. http://lkml.org/lkml/2006/10/31/28 */ | |
1158 | if (!fastfail) | |
11cfb6ff | 1159 | __blk_run_queue(q); |
69cf2d85 EC |
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 | ||
3d5b0605 | 1179 | static void |
896831f5 | 1180 | ktiocomplete(struct frame *f) |
3d5b0605 | 1181 | { |
896831f5 EC |
1182 | struct aoe_hdr *hin, *hout; |
1183 | struct aoe_atahdr *ahin, *ahout; | |
1184 | struct buf *buf; | |
1185 | struct sk_buff *skb; | |
1186 | struct aoetgt *t; | |
1187 | struct aoeif *ifp; | |
1188 | struct aoedev *d; | |
1189 | long n; | |
bbb44e30 | 1190 | int untainted; |
3d5b0605 | 1191 | |
896831f5 | 1192 | if (f == NULL) |
3d5b0605 | 1193 | return; |
896831f5 EC |
1194 | |
1195 | t = f->t; | |
1196 | d = t->d; | |
bbb44e30 EC |
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; | |
896831f5 EC |
1203 | |
1204 | hout = (struct aoe_hdr *) skb_mac_header(f->skb); | |
1205 | ahout = (struct aoe_atahdr *) (hout+1); | |
896831f5 EC |
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); | |
a04b41cd | 1215 | noskb: if (buf) |
69cf2d85 | 1216 | clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); |
bbb44e30 | 1217 | goto out; |
3d5b0605 | 1218 | } |
896831f5 EC |
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) { | |
bf29754a EC |
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); | |
69cf2d85 | 1229 | clear_bit(BIO_UPTODATE, &buf->bio->bi_flags); |
896831f5 EC |
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); | |
3f0f0133 | 1237 | if (ifp) |
896831f5 | 1238 | ifp->lost = 0; |
896831f5 EC |
1239 | spin_unlock_irq(&d->lock); |
1240 | break; | |
1241 | case ATA_CMD_ID_ATA: | |
1242 | if (skb->len < 512) { | |
bf29754a EC |
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, | |
896831f5 EC |
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 | } | |
bbb44e30 | 1261 | out: |
896831f5 | 1262 | spin_lock_irq(&d->lock); |
bbb44e30 EC |
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 | } | |
896831f5 EC |
1272 | |
1273 | aoe_freetframe(f); | |
1274 | ||
69cf2d85 EC |
1275 | if (buf && --buf->nframesout == 0 && buf->resid == 0) |
1276 | aoe_end_buf(d, buf); | |
896831f5 | 1277 | |
69cf2d85 EC |
1278 | spin_unlock_irq(&d->lock); |
1279 | aoedev_put(d); | |
896831f5 | 1280 | dev_kfree_skb(skb); |
3d5b0605 EC |
1281 | } |
1282 | ||
896831f5 EC |
1283 | /* Enters with iocq.lock held. |
1284 | * Returns true iff responses needing processing remain. | |
1285 | */ | |
1286 | static int | |
8030d343 | 1287 | ktio(int id) |
896831f5 EC |
1288 | { |
1289 | struct frame *f; | |
1290 | struct list_head *pos; | |
1291 | int i; | |
8030d343 | 1292 | int actual_id; |
896831f5 EC |
1293 | |
1294 | for (i = 0; ; ++i) { | |
1295 | if (i == MAXIOC) | |
1296 | return 1; | |
8030d343 | 1297 | if (list_empty(&iocq[id].head)) |
896831f5 | 1298 | return 0; |
8030d343 | 1299 | pos = iocq[id].head.next; |
896831f5 | 1300 | list_del(pos); |
896831f5 | 1301 | f = list_entry(pos, struct frame, head); |
8030d343 | 1302 | spin_unlock_irq(&iocq[id].lock); |
896831f5 | 1303 | ktiocomplete(f); |
8030d343 EC |
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); | |
896831f5 EC |
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); | |
8030d343 | 1333 | more = k->fn(k->id); |
896831f5 EC |
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 | ||
eb086ec5 | 1349 | void |
896831f5 EC |
1350 | aoe_ktstop(struct ktstate *k) |
1351 | { | |
1352 | kthread_stop(k->task); | |
1353 | wait_for_completion(&k->rendez); | |
1354 | } | |
1355 | ||
eb086ec5 | 1356 | int |
896831f5 EC |
1357 | aoe_ktstart(struct ktstate *k) |
1358 | { | |
1359 | struct task_struct *task; | |
1360 | ||
1361 | init_completion(&k->rendez); | |
f170168b | 1362 | task = kthread_run(kthread, k, "%s", k->name); |
896831f5 EC |
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 | { | |
8030d343 | 1375 | int id; |
896831f5 EC |
1376 | ulong flags; |
1377 | ||
1378 | f->r_skb = skb; | |
8030d343 EC |
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]); | |
896831f5 EC |
1393 | } |
1394 | ||
1395 | struct sk_buff * | |
1da177e4 LT |
1396 | aoecmd_ata_rsp(struct sk_buff *skb) |
1397 | { | |
1398 | struct aoedev *d; | |
896831f5 | 1399 | struct aoe_hdr *h; |
1da177e4 | 1400 | struct frame *f; |
896831f5 | 1401 | u32 n; |
1da177e4 LT |
1402 | ulong flags; |
1403 | char ebuf[128]; | |
32465c65 | 1404 | u16 aoemajor; |
1405 | ||
896831f5 EC |
1406 | h = (struct aoe_hdr *) skb->data; |
1407 | aoemajor = be16_to_cpu(get_unaligned(&h->major)); | |
0c966214 | 1408 | d = aoedev_by_aoeaddr(aoemajor, h->minor, 0); |
1da177e4 LT |
1409 | if (d == NULL) { |
1410 | snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response " | |
1411 | "for unknown device %d.%d\n", | |
896831f5 | 1412 | aoemajor, h->minor); |
1da177e4 | 1413 | aoechr_error(ebuf); |
896831f5 | 1414 | return skb; |
1da177e4 LT |
1415 | } |
1416 | ||
1417 | spin_lock_irqsave(&d->lock, flags); | |
1418 | ||
896831f5 | 1419 | n = be32_to_cpu(get_unaligned(&h->tag)); |
64a80f5a | 1420 | f = getframe(d, n); |
3a0c40d2 | 1421 | if (f) { |
5f0c9c48 | 1422 | calc_rttavg(d, f->t, tsince_hr(f)); |
3a0c40d2 | 1423 | f->t->nout--; |
bbb44e30 EC |
1424 | if (f->flags & FFL_PROBE) |
1425 | f->t->nout_probes--; | |
3a0c40d2 EC |
1426 | } else { |
1427 | f = getframe_deferred(d, n); | |
1428 | if (f) { | |
5f0c9c48 | 1429 | calc_rttavg(d, NULL, tsince_hr(f)); |
3a0c40d2 EC |
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), | |
2292a7e1 | 1435 | "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n", |
3a0c40d2 EC |
1436 | "unexpected rsp", |
1437 | get_unaligned_be16(&h->major), | |
1438 | h->minor, | |
1439 | get_unaligned_be32(&h->tag), | |
2292a7e1 EC |
1440 | jiffies, |
1441 | h->src, | |
1442 | h->dst); | |
3a0c40d2 EC |
1443 | aoechr_error(ebuf); |
1444 | return skb; | |
1445 | } | |
1da177e4 | 1446 | } |
1da177e4 | 1447 | aoecmd_work(d); |
1da177e4 LT |
1448 | |
1449 | spin_unlock_irqrestore(&d->lock, flags); | |
896831f5 EC |
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; | |
1da177e4 LT |
1458 | } |
1459 | ||
1460 | void | |
1461 | aoecmd_cfg(ushort aoemajor, unsigned char aoeminor) | |
1462 | { | |
e9bb8fb0 | 1463 | struct sk_buff_head queue; |
1da177e4 | 1464 | |
e9bb8fb0 DM |
1465 | __skb_queue_head_init(&queue); |
1466 | aoecmd_cfg_pkts(aoemajor, aoeminor, &queue); | |
1467 | aoenet_xmit(&queue); | |
1da177e4 | 1468 | } |
a04b41cd | 1469 | |
68e0d42f | 1470 | struct sk_buff * |
1da177e4 LT |
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; | |
68e0d42f | 1477 | struct aoetgt *t; |
1da177e4 | 1478 | |
896831f5 | 1479 | f = newframe(d); |
68e0d42f | 1480 | if (f == NULL) |
1da177e4 | 1481 | return NULL; |
68e0d42f EC |
1482 | |
1483 | t = *d->tgt; | |
1da177e4 LT |
1484 | |
1485 | /* initialize the headers & frame */ | |
e407a7f6 | 1486 | skb = f->skb; |
abdbf94d | 1487 | h = (struct aoe_hdr *) skb_mac_header(skb); |
1da177e4 | 1488 | ah = (struct aoe_atahdr *) (h+1); |
19900cde EC |
1489 | skb_put(skb, sizeof *h + sizeof *ah); |
1490 | memset(h, 0, skb->len); | |
68e0d42f | 1491 | f->tag = aoehdr_atainit(d, t, h); |
896831f5 | 1492 | fhash(f); |
68e0d42f | 1493 | t->nout++; |
1da177e4 | 1494 | f->waited = 0; |
3fc9b032 | 1495 | f->waited_total = 0; |
1da177e4 | 1496 | |
1da177e4 LT |
1497 | /* set up ata header */ |
1498 | ah->scnt = 1; | |
04b3ab52 | 1499 | ah->cmdstat = ATA_CMD_ID_ATA; |
1da177e4 LT |
1500 | ah->lba3 = 0xa0; |
1501 | ||
68e0d42f | 1502 | skb->dev = t->ifp->nd; |
1da177e4 | 1503 | |
3a0c40d2 EC |
1504 | d->rttavg = RTTAVG_INIT; |
1505 | d->rttdev = RTTDEV_INIT; | |
1da177e4 | 1506 | d->timer.function = rexmit_timer; |
1da177e4 | 1507 | |
5f0c9c48 EC |
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; | |
1da177e4 | 1515 | } |
a04b41cd | 1516 | |
71114ec4 EC |
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 | ||
68e0d42f EC |
1537 | static struct aoetgt * |
1538 | addtgt(struct aoedev *d, char *addr, ulong nframes) | |
1539 | { | |
1540 | struct aoetgt *t, **tt, **te; | |
68e0d42f EC |
1541 | |
1542 | tt = d->targets; | |
71114ec4 | 1543 | te = tt + d->ntargets; |
68e0d42f EC |
1544 | for (; tt < te && *tt; tt++) |
1545 | ; | |
1546 | ||
578c4aa0 | 1547 | if (tt == te) { |
71114ec4 EC |
1548 | tt = grow_targets(d); |
1549 | if (!tt) | |
1550 | goto nomem; | |
578c4aa0 | 1551 | } |
896831f5 | 1552 | t = kzalloc(sizeof(*t), GFP_ATOMIC); |
71114ec4 EC |
1553 | if (!t) |
1554 | goto nomem; | |
68e0d42f | 1555 | t->nframes = nframes; |
896831f5 | 1556 | t->d = d; |
68e0d42f EC |
1557 | memcpy(t->addr, addr, sizeof t->addr); |
1558 | t->ifp = t->ifs; | |
3a0c40d2 | 1559 | aoecmd_wreset(t); |
bbb44e30 | 1560 | t->maxout = t->nframes / 2; |
896831f5 | 1561 | INIT_LIST_HEAD(&t->ffree); |
68e0d42f | 1562 | return *tt = t; |
71114ec4 EC |
1563 | |
1564 | nomem: | |
1565 | pr_info("aoe: cannot allocate memory to add target\n"); | |
1566 | return NULL; | |
68e0d42f EC |
1567 | } |
1568 | ||
3f0f0133 EC |
1569 | static void |
1570 | setdbcnt(struct aoedev *d) | |
1571 | { | |
1572 | struct aoetgt **t, **e; | |
1573 | int bcnt = 0; | |
1574 | ||
1575 | t = d->targets; | |
71114ec4 | 1576 | e = t + d->ntargets; |
3f0f0133 EC |
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 | } | |
1b86fda9 | 1612 | dev_hold(nd); |
3f0f0133 EC |
1613 | p->nd = nd; |
1614 | p->bcnt = bcnt; | |
1615 | } | |
1616 | t->minbcnt = minbcnt; | |
1617 | setdbcnt(d); | |
1618 | } | |
1619 | ||
1da177e4 LT |
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; | |
68e0d42f | 1626 | struct aoetgt *t; |
0c966214 | 1627 | ulong flags, aoemajor; |
1da177e4 | 1628 | struct sk_buff *sl; |
69cf2d85 | 1629 | struct sk_buff_head queue; |
19bf2635 | 1630 | u16 n; |
1da177e4 | 1631 | |
69cf2d85 | 1632 | sl = NULL; |
abdbf94d | 1633 | h = (struct aoe_hdr *) skb_mac_header(skb); |
1da177e4 LT |
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 | */ | |
823ed72e | 1640 | aoemajor = get_unaligned_be16(&h->major); |
1da177e4 | 1641 | if (aoemajor == 0xfff) { |
a12c93f0 | 1642 | printk(KERN_ERR "aoe: Warning: shelf address is all ones. " |
6bb6285f | 1643 | "Check shelf dip switches.\n"); |
1da177e4 LT |
1644 | return; |
1645 | } | |
7159e969 EC |
1646 | if (aoemajor == 0xffff) { |
1647 | pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n", | |
0c966214 | 1648 | aoemajor, (int) h->minor); |
6583303c EC |
1649 | return; |
1650 | } | |
7159e969 EC |
1651 | if (h->minor == 0xff) { |
1652 | pr_info("aoe: e%ld.%d: broadcast slot number invalid\n", | |
1653 | aoemajor, (int) h->minor); | |
1da177e4 LT |
1654 | return; |
1655 | } | |
1656 | ||
19bf2635 | 1657 | n = be16_to_cpu(ch->bufcnt); |
7df620d8 EC |
1658 | if (n > aoe_maxout) /* keep it reasonable */ |
1659 | n = aoe_maxout; | |
1da177e4 | 1660 | |
7159e969 EC |
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 | ||
1da177e4 LT |
1667 | spin_lock_irqsave(&d->lock, flags); |
1668 | ||
68e0d42f | 1669 | t = gettgt(d, h->src); |
1b8a1636 EC |
1670 | if (t) { |
1671 | t->nframes = n; | |
1672 | if (n < t->maxout) | |
3a0c40d2 | 1673 | aoecmd_wreset(t); |
1b8a1636 | 1674 | } else { |
68e0d42f | 1675 | t = addtgt(d, h->src, n); |
69cf2d85 EC |
1676 | if (!t) |
1677 | goto bail; | |
68e0d42f | 1678 | } |
3f0f0133 EC |
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); | |
3ae1c24e EC |
1686 | |
1687 | /* don't change users' perspective */ | |
69cf2d85 EC |
1688 | if (d->nopen == 0) { |
1689 | d->fw_ver = be16_to_cpu(ch->fwver); | |
1690 | sl = aoecmd_ata_id(d); | |
1da177e4 | 1691 | } |
69cf2d85 | 1692 | bail: |
1da177e4 | 1693 | spin_unlock_irqrestore(&d->lock, flags); |
69cf2d85 | 1694 | aoedev_put(d); |
e9bb8fb0 | 1695 | if (sl) { |
e9bb8fb0 DM |
1696 | __skb_queue_head_init(&queue); |
1697 | __skb_queue_tail(&queue, sl); | |
1698 | aoenet_xmit(&queue); | |
1699 | } | |
1da177e4 LT |
1700 | } |
1701 | ||
3a0c40d2 EC |
1702 | void |
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 | ||
68e0d42f EC |
1710 | void |
1711 | aoecmd_cleanslate(struct aoedev *d) | |
1712 | { | |
1713 | struct aoetgt **t, **te; | |
68e0d42f | 1714 | |
3a0c40d2 EC |
1715 | d->rttavg = RTTAVG_INIT; |
1716 | d->rttdev = RTTDEV_INIT; | |
3f0f0133 | 1717 | d->maxbcnt = 0; |
68e0d42f EC |
1718 | |
1719 | t = d->targets; | |
71114ec4 | 1720 | te = t + d->ntargets; |
3f0f0133 | 1721 | for (; t < te && *t; t++) |
3a0c40d2 | 1722 | aoecmd_wreset(*t); |
68e0d42f | 1723 | } |
896831f5 | 1724 | |
69cf2d85 EC |
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) | |
8030d343 EC |
1738 | { |
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) | |
896831f5 EC |
1749 | { |
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 | ||
8030d343 EC |
1757 | spin_lock_irqsave(&iocq[id].lock, flags); |
1758 | list_splice_init(&iocq[id].head, &flist); | |
1759 | spin_unlock_irqrestore(&iocq[id].lock, flags); | |
896831f5 EC |
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); | |
69cf2d85 | 1774 | aoedev_put(d); |
896831f5 EC |
1775 | } |
1776 | } | |
1777 | ||
1778 | int __init | |
1779 | aoecmd_init(void) | |
1780 | { | |
bbb44e30 | 1781 | void *p; |
8030d343 EC |
1782 | int i; |
1783 | int ret; | |
bbb44e30 EC |
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 | ||
8030d343 EC |
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; | |
896831f5 EC |
1837 | } |
1838 | ||
1839 | void | |
1840 | aoecmd_exit(void) | |
1841 | { | |
8030d343 EC |
1842 | int i; |
1843 | ||
1844 | for (i = 0; i < ncpus; i++) | |
1845 | if (kts[i].active) | |
1846 | aoe_ktstop(&kts[i]); | |
1847 | ||
69cf2d85 | 1848 | aoe_flush_iocq(); |
bbb44e30 | 1849 | |
8030d343 EC |
1850 | /* Free up the iocq and thread speicific configuration |
1851 | * allocated during startup. | |
1852 | */ | |
1853 | kfree(iocq); | |
1854 | kfree(kts); | |
1855 | kfree(ktiowq); | |
1856 | ||
bbb44e30 EC |
1857 | free_page((unsigned long) page_address(empty_page)); |
1858 | empty_page = NULL; | |
896831f5 | 1859 | } |