blob: 30394f78cac2bd7928cc1f8b26d662c668f354d1 [file] [log] [blame]
Ed L. Cashin26114642006-09-20 14:36:48 -04001/* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoecmd.c
4 * Filesystem request handling methods
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
9#include <linux/skbuff.h>
10#include <linux/netdevice.h>
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050011#include <linux/genhd.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070012#include <net/net_namespace.h>
Ed L. Cashin475172f2005-09-29 12:47:40 -040013#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "aoe.h"
15
16#define TIMERTICK (HZ / 10)
17#define MINTIMER (2 * TIMERTICK)
18#define MAXTIMER (HZ << 1)
Ed L. Cashinb751e8b2006-09-20 14:36:50 -040019
20static int aoe_deadsecs = 60 * 3;
21module_param(aoe_deadsecs, int, 0644);
22MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ed L. Cashine407a7f2006-09-20 14:36:49 -040024struct sk_buff *
25new_skb(ulong len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 struct sk_buff *skb;
28
29 skb = alloc_skb(len, GFP_ATOMIC);
30 if (skb) {
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070031 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070032 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 skb->protocol = __constant_htons(ETH_P_AOE);
34 skb->priority = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 skb->next = skb->prev = NULL;
36
37 /* tell the network layer not to perform IP checksums
38 * or to get the NIC to do it
39 */
40 skb->ip_summed = CHECKSUM_NONE;
41 }
42 return skb;
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static struct frame *
46getframe(struct aoedev *d, int tag)
47{
48 struct frame *f, *e;
49
50 f = d->frames;
51 e = f + d->nframes;
52 for (; f<e; f++)
53 if (f->tag == tag)
54 return f;
55 return NULL;
56}
57
58/*
59 * Leave the top bit clear so we have tagspace for userland.
60 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
61 * This driver reserves tag -1 to mean "unused frame."
62 */
63static int
64newtag(struct aoedev *d)
65{
66 register ulong n;
67
68 n = jiffies & 0xffff;
69 return n |= (++d->lasttag & 0x7fff) << 16;
70}
71
72static int
73aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h)
74{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 host_tag = newtag(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
78 memcpy(h->dst, d->addr, sizeof h->dst);
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070079 h->type = __constant_cpu_to_be16(ETH_P_AOE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 h->verfl = AOE_HVER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070081 h->major = cpu_to_be16(d->aoemajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 h->minor = d->aoeminor;
83 h->cmd = AOECMD_ATA;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070084 h->tag = cpu_to_be32(host_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 return host_tag;
87}
88
Ed L. Cashin19bf2632006-09-20 14:36:49 -040089static inline void
90put_lba(struct aoe_atahdr *ah, sector_t lba)
91{
92 ah->lba0 = lba;
93 ah->lba1 = lba >>= 8;
94 ah->lba2 = lba >>= 8;
95 ah->lba3 = lba >>= 8;
96 ah->lba4 = lba >>= 8;
97 ah->lba5 = lba >>= 8;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void
101aoecmd_ata_rw(struct aoedev *d, struct frame *f)
102{
103 struct aoe_hdr *h;
104 struct aoe_atahdr *ah;
105 struct buf *buf;
106 struct sk_buff *skb;
107 ulong bcnt;
108 register sector_t sector;
109 char writebit, extbit;
110
111 writebit = 0x10;
112 extbit = 0x4;
113
114 buf = d->inprocess;
115
116 sector = buf->sector;
117 bcnt = buf->bv_resid;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400118 if (bcnt > d->maxbcnt)
119 bcnt = d->maxbcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400122 skb = f->skb;
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300123 h = aoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800125 skb_put(skb, sizeof *h + sizeof *ah);
126 memset(h, 0, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 f->tag = aoehdr_atainit(d, h);
128 f->waited = 0;
129 f->buf = buf;
130 f->bufaddr = buf->bufaddr;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400131 f->bcnt = bcnt;
132 f->lba = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* set up ata header */
135 ah->scnt = bcnt >> 9;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400136 put_lba(ah, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (d->flags & DEVFL_EXT) {
138 ah->aflags |= AOEAFL_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } else {
140 extbit = 0;
141 ah->lba3 &= 0x0f;
142 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
143 }
144
145 if (bio_data_dir(buf->bio) == WRITE) {
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400146 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
147 offset_in_page(f->bufaddr), bcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ah->aflags |= AOEAFL_WRITE;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400149 skb->len += bcnt;
150 skb->data_len = bcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
152 writebit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 ah->cmdstat = WIN_READ | writebit | extbit;
156
157 /* mark all tracking fields and load out */
158 buf->nframesout += 1;
159 buf->bufaddr += bcnt;
160 buf->bv_resid -= bcnt;
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400161/* printk(KERN_DEBUG "aoe: bv_resid=%ld\n", buf->bv_resid); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 buf->resid -= bcnt;
163 buf->sector += bcnt >> 9;
164 if (buf->resid == 0) {
165 d->inprocess = NULL;
166 } else if (buf->bv_resid == 0) {
167 buf->bv++;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400168 WARN_ON(buf->bv->bv_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 buf->bv_resid = buf->bv->bv_len;
170 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
171 }
172
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400173 skb->dev = d->ifp;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400174 skb = skb_clone(skb, GFP_ATOMIC);
175 if (skb == NULL)
176 return;
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400177 if (d->sendq_hd)
178 d->sendq_tl->next = skb;
179 else
180 d->sendq_hd = skb;
181 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500184/* some callers cannot sleep, and they can call this function,
185 * transmitting the packets later, when interrupts are on
186 */
187static struct sk_buff *
188aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
189{
190 struct aoe_hdr *h;
191 struct aoe_cfghdr *ch;
192 struct sk_buff *skb, *sl, *sl_tail;
193 struct net_device *ifp;
194
195 sl = sl_tail = NULL;
196
197 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700198 for_each_netdev(&init_net, ifp) {
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500199 dev_hold(ifp);
200 if (!is_aoe_netif(ifp))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700201 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500202
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400203 skb = new_skb(sizeof *h + sizeof *ch);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500204 if (skb == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400205 printk(KERN_INFO "aoe: skb alloc failure\n");
Pavel Emelianov7562f872007-05-03 15:13:45 -0700206 goto cont;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500207 }
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800208 skb_put(skb, sizeof *h + sizeof *ch);
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400209 skb->dev = ifp;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500210 if (sl_tail == NULL)
211 sl_tail = skb;
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300212 h = aoe_hdr(skb);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500213 memset(h, 0, sizeof *h + sizeof *ch);
214
215 memset(h->dst, 0xff, sizeof h->dst);
216 memcpy(h->src, ifp->dev_addr, sizeof h->src);
217 h->type = __constant_cpu_to_be16(ETH_P_AOE);
218 h->verfl = AOE_HVER;
219 h->major = cpu_to_be16(aoemajor);
220 h->minor = aoeminor;
221 h->cmd = AOECMD_CFG;
222
223 skb->next = sl;
224 sl = skb;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700225cont:
226 dev_put(ifp);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500227 }
228 read_unlock(&dev_base_lock);
229
230 if (tail != NULL)
231 *tail = sl_tail;
232 return sl;
233}
234
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400235static struct frame *
236freeframe(struct aoedev *d)
237{
238 struct frame *f, *e;
239 int n = 0;
240
241 f = d->frames;
242 e = f + d->nframes;
243 for (; f<e; f++) {
244 if (f->tag != FREETAG)
245 continue;
246 if (atomic_read(&skb_shinfo(f->skb)->dataref) == 1) {
247 skb_shinfo(f->skb)->nr_frags = f->skb->data_len = 0;
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800248 skb_trim(f->skb, 0);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400249 return f;
250 }
251 n++;
252 }
253 if (n == d->nframes) /* wait for network layer */
254 d->flags |= DEVFL_KICKME;
255
256 return NULL;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* enters with d->lock held */
260void
261aoecmd_work(struct aoedev *d)
262{
263 struct frame *f;
264 struct buf *buf;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500265
266 if (d->flags & DEVFL_PAUSE) {
267 if (!aoedev_isbusy(d))
268 d->sendq_hd = aoecmd_cfg_pkts(d->aoemajor,
269 d->aoeminor, &d->sendq_tl);
270 return;
271 }
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273loop:
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400274 f = freeframe(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (f == NULL)
276 return;
277 if (d->inprocess == NULL) {
278 if (list_empty(&d->bufq))
279 return;
280 buf = container_of(d->bufq.next, struct buf, bufs);
281 list_del(d->bufq.next);
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400282/*printk(KERN_DEBUG "aoe: bi_size=%ld\n", buf->bio->bi_size); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 d->inprocess = buf;
284 }
285 aoecmd_ata_rw(d, f);
286 goto loop;
287}
288
289static void
290rexmit(struct aoedev *d, struct frame *f)
291{
292 struct sk_buff *skb;
293 struct aoe_hdr *h;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400294 struct aoe_atahdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 char buf[128];
296 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 n = newtag(d);
299
300 snprintf(buf, sizeof buf,
301 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
302 "retransmit",
303 d->aoemajor, d->aoeminor, f->tag, jiffies, n);
304 aoechr_error(buf);
305
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400306 skb = f->skb;
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300307 h = aoe_hdr(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400308 ah = (struct aoe_atahdr *) (h+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 f->tag = n;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700310 h->tag = cpu_to_be32(n);
Ed L. Cashin2dd5e422006-01-19 13:46:25 -0500311 memcpy(h->dst, d->addr, sizeof h->dst);
312 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400314 n = DEFAULTBCNT / 512;
315 if (ah->scnt > n) {
316 ah->scnt = n;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400317 if (ah->aflags & AOEAFL_WRITE) {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400318 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
319 offset_in_page(f->bufaddr), DEFAULTBCNT);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400320 skb->len = sizeof *h + sizeof *ah + DEFAULTBCNT;
321 skb->data_len = DEFAULTBCNT;
322 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400323 if (++d->lostjumbo > (d->nframes << 1))
324 if (d->maxbcnt != DEFAULTBCNT) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400325 printk(KERN_INFO "aoe: e%ld.%ld: too many lost jumbo on %s - using 1KB frames.\n",
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400326 d->aoemajor, d->aoeminor, d->ifp->name);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400327 d->maxbcnt = DEFAULTBCNT;
328 d->flags |= DEVFL_MAXBCNT;
329 }
330 }
331
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400332 skb->dev = d->ifp;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400333 skb = skb_clone(skb, GFP_ATOMIC);
334 if (skb == NULL)
335 return;
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400336 if (d->sendq_hd)
337 d->sendq_tl->next = skb;
338 else
339 d->sendq_hd = skb;
340 d->sendq_tl = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static int
344tsince(int tag)
345{
346 int n;
347
348 n = jiffies & 0xffff;
349 n -= tag & 0xffff;
350 if (n < 0)
351 n += 1<<16;
352 return n;
353}
354
355static void
356rexmit_timer(ulong vp)
357{
358 struct aoedev *d;
359 struct frame *f, *e;
360 struct sk_buff *sl;
361 register long timeout;
362 ulong flags, n;
363
364 d = (struct aoedev *) vp;
365 sl = NULL;
366
367 /* timeout is always ~150% of the moving average */
368 timeout = d->rttavg;
369 timeout += timeout >> 1;
370
371 spin_lock_irqsave(&d->lock, flags);
372
373 if (d->flags & DEVFL_TKILL) {
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500374 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return;
376 }
377 f = d->frames;
378 e = f + d->nframes;
379 for (; f<e; f++) {
380 if (f->tag != FREETAG && tsince(f->tag) >= timeout) {
381 n = f->waited += timeout;
382 n /= HZ;
Ed L. Cashinb751e8b2006-09-20 14:36:50 -0400383 if (n > aoe_deadsecs) { /* waited too long for response */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 aoedev_downdev(d);
Ed L. Cashin1c6f3fc2006-01-25 13:54:44 -0500385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 rexmit(d, f);
388 }
389 }
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400390 if (d->flags & DEVFL_KICKME) {
391 d->flags &= ~DEVFL_KICKME;
392 aoecmd_work(d);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700395 sl = d->sendq_hd;
396 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (sl) {
398 n = d->rttavg <<= 1;
399 if (n > MAXTIMER)
400 d->rttavg = MAXTIMER;
401 }
402
403 d->timer.expires = jiffies + TIMERTICK;
404 add_timer(&d->timer);
405
406 spin_unlock_irqrestore(&d->lock, flags);
407
408 aoenet_xmit(sl);
409}
410
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500411/* this function performs work that has been deferred until sleeping is OK
412 */
413void
David Howellsc4028952006-11-22 14:57:56 +0000414aoecmd_sleepwork(struct work_struct *work)
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500415{
David Howellsc4028952006-11-22 14:57:56 +0000416 struct aoedev *d = container_of(work, struct aoedev, work);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500417
418 if (d->flags & DEVFL_GDALLOC)
419 aoeblk_gdalloc(d);
420
421 if (d->flags & DEVFL_NEWSIZE) {
422 struct block_device *bd;
423 unsigned long flags;
424 u64 ssize;
425
426 ssize = d->gd->capacity;
427 bd = bdget_disk(d->gd, 0);
428
429 if (bd) {
430 mutex_lock(&bd->bd_inode->i_mutex);
431 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
432 mutex_unlock(&bd->bd_inode->i_mutex);
433 bdput(bd);
434 }
435 spin_lock_irqsave(&d->lock, flags);
436 d->flags |= DEVFL_UP;
437 d->flags &= ~DEVFL_NEWSIZE;
438 spin_unlock_irqrestore(&d->lock, flags);
439 }
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static void
443ataid_complete(struct aoedev *d, unsigned char *id)
444{
445 u64 ssize;
446 u16 n;
447
448 /* word 83: command set supported */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400449 n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* word 86: command set/feature enabled */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400452 n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (n & (1<<10)) { /* bit 10: LBA 48 */
455 d->flags |= DEVFL_EXT;
456
457 /* word 100: number lba48 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400458 ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* set as in ide-disk.c:init_idedisk_capacity */
461 d->geo.cylinders = ssize;
462 d->geo.cylinders /= (255 * 63);
463 d->geo.heads = 255;
464 d->geo.sectors = 63;
465 } else {
466 d->flags &= ~DEVFL_EXT;
467
468 /* number lba28 sectors */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400469 ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* NOTE: obsolete in ATA 6 */
Ed L. Cashin475172f2005-09-29 12:47:40 -0400472 d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1]));
473 d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1]));
474 d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500476
477 if (d->ssize != ssize)
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400478 printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu sectors\n",
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400479 (unsigned long long)mac_addr(d->addr),
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500480 d->aoemajor, d->aoeminor,
481 d->fw_ver, (long long)ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 d->ssize = ssize;
483 d->geo.start = 0;
484 if (d->gd != NULL) {
485 d->gd->capacity = ssize;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500486 d->flags |= DEVFL_NEWSIZE;
487 } else {
488 if (d->flags & DEVFL_GDALLOC) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400489 printk(KERN_ERR "aoe: can't schedule work for e%lu.%lu, %s\n",
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500490 d->aoemajor, d->aoeminor,
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400491 "it's already on! This shouldn't happen.\n");
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500492 return;
493 }
494 d->flags |= DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 schedule_work(&d->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static void
500calc_rttavg(struct aoedev *d, int rtt)
501{
502 register long n;
503
504 n = rtt;
Ed L. Cashindced3a02006-09-20 14:36:49 -0400505 if (n < 0) {
506 n = -rtt;
507 if (n < MINTIMER)
508 n = MINTIMER;
509 else if (n > MAXTIMER)
510 n = MAXTIMER;
511 d->mintimer += (n - d->mintimer) >> 1;
512 } else if (n < d->mintimer)
513 n = d->mintimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 else if (n > MAXTIMER)
515 n = MAXTIMER;
516
517 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
518 n -= d->rttavg;
519 d->rttavg += n >> 2;
520}
521
522void
523aoecmd_ata_rsp(struct sk_buff *skb)
524{
525 struct aoedev *d;
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400526 struct aoe_hdr *hin, *hout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct aoe_atahdr *ahin, *ahout;
528 struct frame *f;
529 struct buf *buf;
530 struct sk_buff *sl;
531 register long n;
532 ulong flags;
533 char ebuf[128];
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700534 u16 aoemajor;
535
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300536 hin = aoe_hdr(skb);
David S. Miller43ecf522007-03-01 18:30:08 -0800537 aoemajor = be16_to_cpu(get_unaligned(&hin->major));
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700538 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (d == NULL) {
540 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
541 "for unknown device %d.%d\n",
ecashin@coraid.com32465c62005-04-18 22:00:18 -0700542 aoemajor, hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 aoechr_error(ebuf);
544 return;
545 }
546
547 spin_lock_irqsave(&d->lock, flags);
548
David S. Miller43ecf522007-03-01 18:30:08 -0800549 n = be32_to_cpu(get_unaligned(&hin->tag));
Ed L. Cashindced3a02006-09-20 14:36:49 -0400550 f = getframe(d, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (f == NULL) {
Ed L. Cashindced3a02006-09-20 14:36:49 -0400552 calc_rttavg(d, -tsince(n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_unlock_irqrestore(&d->lock, flags);
554 snprintf(ebuf, sizeof ebuf,
555 "%15s e%d.%d tag=%08x@%08lx\n",
556 "unexpected rsp",
David S. Miller43ecf522007-03-01 18:30:08 -0800557 be16_to_cpu(get_unaligned(&hin->major)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 hin->minor,
David S. Miller43ecf522007-03-01 18:30:08 -0800559 be32_to_cpu(get_unaligned(&hin->tag)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 jiffies);
561 aoechr_error(ebuf);
562 return;
563 }
564
565 calc_rttavg(d, tsince(f->tag));
566
567 ahin = (struct aoe_atahdr *) (hin+1);
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300568 hout = aoe_hdr(f->skb);
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400569 ahout = (struct aoe_atahdr *) (hout+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 buf = f->buf;
571
Ed L. Cashin9d419652006-02-07 11:37:24 -0500572 if (ahout->cmdstat == WIN_IDENTIFY)
573 d->flags &= ~DEVFL_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400575 printk(KERN_ERR
576 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ahout->cmdstat, ahin->cmdstat,
578 d->aoemajor, d->aoeminor);
579 if (buf)
580 buf->flags |= BUFFL_FAIL;
581 } else {
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400582 n = ahout->scnt << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 switch (ahout->cmdstat) {
584 case WIN_READ:
585 case WIN_READ_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (skb->len - sizeof *hin - sizeof *ahin < n) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400587 printk(KERN_ERR
588 "aoe: runt data size in read. skb->len=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 skb->len);
590 /* fail frame f? just returning will rexmit. */
591 spin_unlock_irqrestore(&d->lock, flags);
592 return;
593 }
594 memcpy(f->bufaddr, ahin+1, n);
595 case WIN_WRITE:
596 case WIN_WRITE_EXT:
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400597 if (f->bcnt -= n) {
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400598 skb = f->skb;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400599 f->bufaddr += n;
600 put_lba(ahout, f->lba += ahout->scnt);
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400601 n = f->bcnt;
602 if (n > DEFAULTBCNT)
603 n = DEFAULTBCNT;
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400604 ahout->scnt = n >> 9;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400605 if (ahout->aflags & AOEAFL_WRITE) {
606 skb_fill_page_desc(skb, 0,
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400607 virt_to_page(f->bufaddr),
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400608 offset_in_page(f->bufaddr), n);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400609 skb->len = sizeof *hout + sizeof *ahout + n;
610 skb->data_len = n;
611 }
Ed L. Cashinddec63e2006-09-20 14:36:49 -0400612 f->tag = newtag(d);
613 hout->tag = cpu_to_be32(f->tag);
614 skb->dev = d->ifp;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400615 skb = skb_clone(skb, GFP_ATOMIC);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400616 spin_unlock_irqrestore(&d->lock, flags);
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400617 if (skb)
618 aoenet_xmit(skb);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400619 return;
620 }
621 if (n > DEFAULTBCNT)
622 d->lostjumbo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 case WIN_IDENTIFY:
625 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400626 printk(KERN_INFO
627 "aoe: runt data size in ataid. skb->len=%d\n",
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400628 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 spin_unlock_irqrestore(&d->lock, flags);
630 return;
631 }
632 ataid_complete(d, (char *) (ahin+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400635 printk(KERN_INFO
636 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400637 ahout->cmdstat,
David S. Miller43ecf522007-03-01 18:30:08 -0800638 be16_to_cpu(get_unaligned(&hin->major)),
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400639 hin->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
642
643 if (buf) {
644 buf->nframesout -= 1;
645 if (buf->nframesout == 0 && buf->resid == 0) {
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700646 unsigned long duration = jiffies - buf->start_time;
647 unsigned long n_sect = buf->bio->bi_size >> 9;
648 struct gendisk *disk = d->gd;
Jens Axboe496456c2005-11-01 09:54:23 +0100649 const int rw = bio_data_dir(buf->bio);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700650
Jens Axboe496456c2005-11-01 09:54:23 +0100651 disk_stat_inc(disk, ios[rw]);
652 disk_stat_add(disk, ticks[rw], duration);
653 disk_stat_add(disk, sectors[rw], n_sect);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700654 disk_stat_add(disk, io_ticks, duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
656 bio_endio(buf->bio, buf->bio->bi_size, n);
657 mempool_free(buf, d->bufpool);
658 }
659 }
660
661 f->buf = NULL;
662 f->tag = FREETAG;
663
664 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700665 sl = d->sendq_hd;
666 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 aoenet_xmit(sl);
670}
671
672void
673aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
674{
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500675 struct sk_buff *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500677 sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 aoenet_xmit(sl);
680}
681
682/*
683 * Since we only call this in one place (and it only prepares one frame)
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700684 * we just return the skb. Usually we'd chain it up to the aoedev sendq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
686static struct sk_buff *
687aoecmd_ata_id(struct aoedev *d)
688{
689 struct aoe_hdr *h;
690 struct aoe_atahdr *ah;
691 struct frame *f;
692 struct sk_buff *skb;
693
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400694 f = freeframe(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (f == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400696 printk(KERN_ERR "aoe: can't get a frame. This shouldn't happen.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return NULL;
698 }
699
700 /* initialize the headers & frame */
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400701 skb = f->skb;
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300702 h = aoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ah = (struct aoe_atahdr *) (h+1);
Ed L. Cashin19900cd2006-12-22 01:09:21 -0800704 skb_put(skb, sizeof *h + sizeof *ah);
705 memset(h, 0, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 f->tag = aoehdr_atainit(d, h);
707 f->waited = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* set up ata header */
710 ah->scnt = 1;
711 ah->cmdstat = WIN_IDENTIFY;
712 ah->lba3 = 0xa0;
713
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400714 skb->dev = d->ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500716 d->rttavg = MAXTIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 d->timer.function = rexmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400719 return skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722void
723aoecmd_cfg_rsp(struct sk_buff *skb)
724{
725 struct aoedev *d;
726 struct aoe_hdr *h;
727 struct aoe_cfghdr *ch;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700728 ulong flags, sysminor, aoemajor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct sk_buff *sl;
Ed L. Cashineaf0a3c2006-01-19 13:46:20 -0500730 enum { MAXFRAMES = 16 };
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400731 u16 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Arnaldo Carvalho de Melo029720f2007-03-10 11:20:07 -0300733 h = aoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ch = (struct aoe_cfghdr *) (h+1);
735
736 /*
737 * Enough people have their dip switches set backwards to
738 * warrant a loud message for this special case.
739 */
David S. Miller43ecf522007-03-01 18:30:08 -0800740 aoemajor = be16_to_cpu(get_unaligned(&h->major));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (aoemajor == 0xfff) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400742 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
Ed L. Cashin6bb62852006-09-20 14:36:49 -0400743 "Check shelf dip switches.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return;
745 }
746
747 sysminor = SYSMINOR(aoemajor, h->minor);
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700748 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400749 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
ecashin@coraid.comfc458dc2005-04-18 22:00:17 -0700750 aoemajor, (int) h->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752 }
753
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400754 n = be16_to_cpu(ch->bufcnt);
755 if (n > MAXFRAMES) /* keep it reasonable */
756 n = MAXFRAMES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400758 d = aoedev_by_sysminor_m(sysminor, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (d == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400760 printk(KERN_INFO "aoe: device sysminor_m failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return;
762 }
763
764 spin_lock_irqsave(&d->lock, flags);
765
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500766 /* permit device to migrate mac and network interface */
767 d->ifp = skb->dev;
768 memcpy(d->addr, h->src, sizeof d->addr);
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400769 if (!(d->flags & DEVFL_MAXBCNT)) {
770 n = d->ifp->mtu;
771 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
772 n /= 512;
773 if (n > ch->scnt)
774 n = ch->scnt;
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400775 n = n ? n * 512 : DEFAULTBCNT;
776 if (n != d->maxbcnt) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400777 printk(KERN_INFO
778 "aoe: e%ld.%ld: setting %d byte data frames on %s\n",
Ed L. Cashin4f51dc52006-09-20 14:36:49 -0400779 d->aoemajor, d->aoeminor, n, d->ifp->name);
780 d->maxbcnt = n;
781 }
Ed L. Cashin19bf2632006-09-20 14:36:49 -0400782 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500783
784 /* don't change users' perspective */
785 if (d->nopen && !(d->flags & DEVFL_PAUSE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 spin_unlock_irqrestore(&d->lock, flags);
787 return;
788 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500789 d->flags |= DEVFL_PAUSE; /* force pause */
Ed L. Cashindced3a02006-09-20 14:36:49 -0400790 d->mintimer = MINTIMER;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700791 d->fw_ver = be16_to_cpu(ch->fwver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500793 /* check for already outstanding ataid */
794 sl = aoedev_isbusy(d) == 0 ? aoecmd_ata_id(d) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 spin_unlock_irqrestore(&d->lock, flags);
797
798 aoenet_xmit(sl);
799}
800