60beb8db4612a452fddeaf5d876e62ce0742c1c4
[pandora-kernel.git] / drivers / block / aoe / aoecmd.c
1 /* Copyright (c) 2004 Coraid, Inc.  See COPYING for GPL terms. */
2 /*
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>
11 #include "aoe.h"
12
13 #define TIMERTICK (HZ / 10)
14 #define MINTIMER (2 * TIMERTICK)
15 #define MAXTIMER (HZ << 1)
16 #define MAXWAIT (60 * 3)        /* After MAXWAIT seconds, give up and fail dev */
17
18 static struct sk_buff *
19 new_skb(struct net_device *if_dev, ulong len)
20 {
21         struct sk_buff *skb;
22
23         skb = alloc_skb(len, GFP_ATOMIC);
24         if (skb) {
25                 skb->nh.raw = skb->mac.raw = skb->data;
26                 skb->dev = if_dev;
27                 skb->protocol = __constant_htons(ETH_P_AOE);
28                 skb->priority = 0;
29                 skb_put(skb, len);
30                 skb->next = skb->prev = NULL;
31
32                 /* tell the network layer not to perform IP checksums
33                  * or to get the NIC to do it
34                  */
35                 skb->ip_summed = CHECKSUM_NONE;
36         }
37         return skb;
38 }
39
40 static struct sk_buff *
41 skb_prepare(struct aoedev *d, struct frame *f)
42 {
43         struct sk_buff *skb;
44         char *p;
45
46         skb = new_skb(d->ifp, f->ndata + f->writedatalen);
47         if (!skb) {
48                 printk(KERN_INFO "aoe: skb_prepare: failure to allocate skb\n");
49                 return NULL;
50         }
51
52         p = skb->mac.raw;
53         memcpy(p, f->data, f->ndata);
54
55         if (f->writedatalen) {
56                 p += sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
57                 memcpy(p, f->bufaddr, f->writedatalen);
58         }
59
60         return skb;
61 }
62
63 static struct frame *
64 getframe(struct aoedev *d, int tag)
65 {
66         struct frame *f, *e;
67
68         f = d->frames;
69         e = f + d->nframes;
70         for (; f<e; f++)
71                 if (f->tag == tag)
72                         return f;
73         return NULL;
74 }
75
76 /*
77  * Leave the top bit clear so we have tagspace for userland.
78  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
79  * This driver reserves tag -1 to mean "unused frame."
80  */
81 static int
82 newtag(struct aoedev *d)
83 {
84         register ulong n;
85
86         n = jiffies & 0xffff;
87         return n |= (++d->lasttag & 0x7fff) << 16;
88 }
89
90 static int
91 aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h)
92 {
93         u32 host_tag = newtag(d);
94
95         memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
96         memcpy(h->dst, d->addr, sizeof h->dst);
97         h->type = __constant_cpu_to_be16(ETH_P_AOE);
98         h->verfl = AOE_HVER;
99         h->major = cpu_to_be16(d->aoemajor);
100         h->minor = d->aoeminor;
101         h->cmd = AOECMD_ATA;
102         h->tag = cpu_to_be32(host_tag);
103
104         return host_tag;
105 }
106
107 static void
108 aoecmd_ata_rw(struct aoedev *d, struct frame *f)
109 {
110         struct aoe_hdr *h;
111         struct aoe_atahdr *ah;
112         struct buf *buf;
113         struct sk_buff *skb;
114         ulong bcnt;
115         register sector_t sector;
116         char writebit, extbit;
117
118         writebit = 0x10;
119         extbit = 0x4;
120
121         buf = d->inprocess;
122
123         sector = buf->sector;
124         bcnt = buf->bv_resid;
125         if (bcnt > MAXATADATA)
126                 bcnt = MAXATADATA;
127
128         /* initialize the headers & frame */
129         h = (struct aoe_hdr *) f->data;
130         ah = (struct aoe_atahdr *) (h+1);
131         f->ndata = sizeof *h + sizeof *ah;
132         memset(h, 0, f->ndata);
133         f->tag = aoehdr_atainit(d, h);
134         f->waited = 0;
135         f->buf = buf;
136         f->bufaddr = buf->bufaddr;
137
138         /* set up ata header */
139         ah->scnt = bcnt >> 9;
140         ah->lba0 = sector;
141         ah->lba1 = sector >>= 8;
142         ah->lba2 = sector >>= 8;
143         ah->lba3 = sector >>= 8;
144         if (d->flags & DEVFL_EXT) {
145                 ah->aflags |= AOEAFL_EXT;
146                 ah->lba4 = sector >>= 8;
147                 ah->lba5 = sector >>= 8;
148         } else {
149                 extbit = 0;
150                 ah->lba3 &= 0x0f;
151                 ah->lba3 |= 0xe0;       /* LBA bit + obsolete 0xa0 */
152         }
153
154         if (bio_data_dir(buf->bio) == WRITE) {
155                 ah->aflags |= AOEAFL_WRITE;
156                 f->writedatalen = bcnt;
157         } else {
158                 writebit = 0;
159                 f->writedatalen = 0;
160         }
161
162         ah->cmdstat = WIN_READ | writebit | extbit;
163
164         /* mark all tracking fields and load out */
165         buf->nframesout += 1;
166         buf->bufaddr += bcnt;
167         buf->bv_resid -= bcnt;
168 /* printk(KERN_INFO "aoe: bv_resid=%ld\n", buf->bv_resid); */
169         buf->resid -= bcnt;
170         buf->sector += bcnt >> 9;
171         if (buf->resid == 0) {
172                 d->inprocess = NULL;
173         } else if (buf->bv_resid == 0) {
174                 buf->bv++;
175                 buf->bv_resid = buf->bv->bv_len;
176                 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
177         }
178
179         skb = skb_prepare(d, f);
180         if (skb) {
181                 skb->next = d->skblist;
182                 d->skblist = skb;
183         }
184 }
185
186 /* enters with d->lock held */
187 void
188 aoecmd_work(struct aoedev *d)
189 {
190         struct frame *f;
191         struct buf *buf;
192 loop:
193         f = getframe(d, FREETAG);
194         if (f == NULL)
195                 return;
196         if (d->inprocess == NULL) {
197                 if (list_empty(&d->bufq))
198                         return;
199                 buf = container_of(d->bufq.next, struct buf, bufs);
200                 list_del(d->bufq.next);
201 /*printk(KERN_INFO "aoecmd_work: bi_size=%ld\n", buf->bio->bi_size); */
202                 d->inprocess = buf;
203         }
204         aoecmd_ata_rw(d, f);
205         goto loop;
206 }
207
208 static void
209 rexmit(struct aoedev *d, struct frame *f)
210 {
211         struct sk_buff *skb;
212         struct aoe_hdr *h;
213         char buf[128];
214         u32 n;
215
216         n = newtag(d);
217
218         snprintf(buf, sizeof buf,
219                 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
220                 "retransmit",
221                 d->aoemajor, d->aoeminor, f->tag, jiffies, n);
222         aoechr_error(buf);
223
224         h = (struct aoe_hdr *) f->data;
225         f->tag = n;
226         h->tag = cpu_to_be32(n);
227
228         skb = skb_prepare(d, f);
229         if (skb) {
230                 skb->next = d->skblist;
231                 d->skblist = skb;
232         }
233 }
234
235 static int
236 tsince(int tag)
237 {
238         int n;
239
240         n = jiffies & 0xffff;
241         n -= tag & 0xffff;
242         if (n < 0)
243                 n += 1<<16;
244         return n;
245 }
246
247 static void
248 rexmit_timer(ulong vp)
249 {
250         struct aoedev *d;
251         struct frame *f, *e;
252         struct sk_buff *sl;
253         register long timeout;
254         ulong flags, n;
255
256         d = (struct aoedev *) vp;
257         sl = NULL;
258
259         /* timeout is always ~150% of the moving average */
260         timeout = d->rttavg;
261         timeout += timeout >> 1;
262
263         spin_lock_irqsave(&d->lock, flags);
264
265         if (d->flags & DEVFL_TKILL) {
266 tdie:           spin_unlock_irqrestore(&d->lock, flags);
267                 return;
268         }
269         f = d->frames;
270         e = f + d->nframes;
271         for (; f<e; f++) {
272                 if (f->tag != FREETAG && tsince(f->tag) >= timeout) {
273                         n = f->waited += timeout;
274                         n /= HZ;
275                         if (n > MAXWAIT) { /* waited too long.  device failure. */
276                                 aoedev_downdev(d);
277                                 goto tdie;
278                         }
279                         rexmit(d, f);
280                 }
281         }
282
283         sl = d->skblist;
284         d->skblist = NULL;
285         if (sl) {
286                 n = d->rttavg <<= 1;
287                 if (n > MAXTIMER)
288                         d->rttavg = MAXTIMER;
289         }
290
291         d->timer.expires = jiffies + TIMERTICK;
292         add_timer(&d->timer);
293
294         spin_unlock_irqrestore(&d->lock, flags);
295
296         aoenet_xmit(sl);
297 }
298
299 static void
300 ataid_complete(struct aoedev *d, unsigned char *id)
301 {
302         u64 ssize;
303         u16 n;
304
305         /* word 83: command set supported */
306         n = le16_to_cpup((__le16 *) &id[83<<1]);
307
308         /* word 86: command set/feature enabled */
309         n |= le16_to_cpup((__le16 *) &id[86<<1]);
310
311         if (n & (1<<10)) {      /* bit 10: LBA 48 */
312                 d->flags |= DEVFL_EXT;
313
314                 /* word 100: number lba48 sectors */
315                 ssize = le64_to_cpup((__le64 *) &id[100<<1]);
316
317                 /* set as in ide-disk.c:init_idedisk_capacity */
318                 d->geo.cylinders = ssize;
319                 d->geo.cylinders /= (255 * 63);
320                 d->geo.heads = 255;
321                 d->geo.sectors = 63;
322         } else {
323                 d->flags &= ~DEVFL_EXT;
324
325                 /* number lba28 sectors */
326                 ssize = le32_to_cpup((__le32 *) &id[60<<1]);
327
328                 /* NOTE: obsolete in ATA 6 */
329                 d->geo.cylinders = le16_to_cpup((__le16 *) &id[54<<1]);
330                 d->geo.heads = le16_to_cpup((__le16 *) &id[55<<1]);
331                 d->geo.sectors = le16_to_cpup((__le16 *) &id[56<<1]);
332         }
333         d->ssize = ssize;
334         d->geo.start = 0;
335         if (d->gd != NULL) {
336                 d->gd->capacity = ssize;
337                 d->flags |= DEVFL_UP;
338                 return;
339         }
340         if (d->flags & DEVFL_WORKON) {
341                 printk(KERN_INFO "aoe: ataid_complete: can't schedule work, it's already on!  "
342                         "(This really shouldn't happen).\n");
343                 return;
344         }
345         INIT_WORK(&d->work, aoeblk_gdalloc, d);
346         schedule_work(&d->work);
347         d->flags |= DEVFL_WORKON;
348 }
349
350 static void
351 calc_rttavg(struct aoedev *d, int rtt)
352 {
353         register long n;
354
355         n = rtt;
356         if (n < MINTIMER)
357                 n = MINTIMER;
358         else if (n > MAXTIMER)
359                 n = MAXTIMER;
360
361         /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
362         n -= d->rttavg;
363         d->rttavg += n >> 2;
364 }
365
366 void
367 aoecmd_ata_rsp(struct sk_buff *skb)
368 {
369         struct aoedev *d;
370         struct aoe_hdr *hin;
371         struct aoe_atahdr *ahin, *ahout;
372         struct frame *f;
373         struct buf *buf;
374         struct sk_buff *sl;
375         register long n;
376         ulong flags;
377         char ebuf[128];
378         u16 aoemajor;
379
380         hin = (struct aoe_hdr *) skb->mac.raw;
381         aoemajor = be16_to_cpu(hin->major);
382         d = aoedev_by_aoeaddr(aoemajor, hin->minor);
383         if (d == NULL) {
384                 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
385                         "for unknown device %d.%d\n",
386                          aoemajor, hin->minor);
387                 aoechr_error(ebuf);
388                 return;
389         }
390
391         spin_lock_irqsave(&d->lock, flags);
392
393         f = getframe(d, be32_to_cpu(hin->tag));
394         if (f == NULL) {
395                 spin_unlock_irqrestore(&d->lock, flags);
396                 snprintf(ebuf, sizeof ebuf,
397                         "%15s e%d.%d    tag=%08x@%08lx\n",
398                         "unexpected rsp",
399                         be16_to_cpu(hin->major),
400                         hin->minor,
401                         be32_to_cpu(hin->tag),
402                         jiffies);
403                 aoechr_error(ebuf);
404                 return;
405         }
406
407         calc_rttavg(d, tsince(f->tag));
408
409         ahin = (struct aoe_atahdr *) (hin+1);
410         ahout = (struct aoe_atahdr *) (f->data + sizeof(struct aoe_hdr));
411         buf = f->buf;
412
413         if (ahin->cmdstat & 0xa9) {     /* these bits cleared on success */
414                 printk(KERN_CRIT "aoe: aoecmd_ata_rsp: ata error cmd=%2.2Xh "
415                         "stat=%2.2Xh from e%ld.%ld\n", 
416                         ahout->cmdstat, ahin->cmdstat,
417                         d->aoemajor, d->aoeminor);
418                 if (buf)
419                         buf->flags |= BUFFL_FAIL;
420         } else {
421                 switch (ahout->cmdstat) {
422                 case WIN_READ:
423                 case WIN_READ_EXT:
424                         n = ahout->scnt << 9;
425                         if (skb->len - sizeof *hin - sizeof *ahin < n) {
426                                 printk(KERN_CRIT "aoe: aoecmd_ata_rsp: runt "
427                                         "ata data size in read.  skb->len=%d\n",
428                                         skb->len);
429                                 /* fail frame f?  just returning will rexmit. */
430                                 spin_unlock_irqrestore(&d->lock, flags);
431                                 return;
432                         }
433                         memcpy(f->bufaddr, ahin+1, n);
434                 case WIN_WRITE:
435                 case WIN_WRITE_EXT:
436                         break;
437                 case WIN_IDENTIFY:
438                         if (skb->len - sizeof *hin - sizeof *ahin < 512) {
439                                 printk(KERN_INFO "aoe: aoecmd_ata_rsp: runt data size "
440                                         "in ataid.  skb->len=%d\n", skb->len);
441                                 spin_unlock_irqrestore(&d->lock, flags);
442                                 return;
443                         }
444                         ataid_complete(d, (char *) (ahin+1));
445                         /* d->flags |= DEVFL_WC_UPDATE; */
446                         break;
447                 default:
448                         printk(KERN_INFO "aoe: aoecmd_ata_rsp: unrecognized "
449                                "outbound ata command %2.2Xh for %d.%d\n", 
450                                ahout->cmdstat,
451                                be16_to_cpu(hin->major),
452                                hin->minor);
453                 }
454         }
455
456         if (buf) {
457                 buf->nframesout -= 1;
458                 if (buf->nframesout == 0 && buf->resid == 0) {
459                         unsigned long duration = jiffies - buf->start_time;
460                         unsigned long n_sect = buf->bio->bi_size >> 9;
461                         struct gendisk *disk = d->gd;
462
463                         if (bio_data_dir(buf->bio) == WRITE) {
464                                 disk_stat_inc(disk, writes);
465                                 disk_stat_add(disk, write_ticks, duration);
466                                 disk_stat_add(disk, write_sectors, n_sect);
467                         } else {
468                                 disk_stat_inc(disk, reads);
469                                 disk_stat_add(disk, read_ticks, duration);
470                                 disk_stat_add(disk, read_sectors, n_sect);
471                         }
472                         disk_stat_add(disk, io_ticks, duration);
473                         n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
474                         bio_endio(buf->bio, buf->bio->bi_size, n);
475                         mempool_free(buf, d->bufpool);
476                 }
477         }
478
479         f->buf = NULL;
480         f->tag = FREETAG;
481
482         aoecmd_work(d);
483
484         sl = d->skblist;
485         d->skblist = NULL;
486
487         spin_unlock_irqrestore(&d->lock, flags);
488
489         aoenet_xmit(sl);
490 }
491
492 void
493 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
494 {
495         struct aoe_hdr *h;
496         struct aoe_cfghdr *ch;
497         struct sk_buff *skb, *sl;
498         struct net_device *ifp;
499
500         sl = NULL;
501
502         read_lock(&dev_base_lock);
503         for (ifp = dev_base; ifp; dev_put(ifp), ifp = ifp->next) {
504                 dev_hold(ifp);
505                 if (!is_aoe_netif(ifp))
506                         continue;
507
508                 skb = new_skb(ifp, sizeof *h + sizeof *ch);
509                 if (skb == NULL) {
510                         printk(KERN_INFO "aoe: aoecmd_cfg: skb alloc failure\n");
511                         continue;
512                 }
513                 h = (struct aoe_hdr *) skb->mac.raw;
514                 memset(h, 0, sizeof *h + sizeof *ch);
515
516                 memset(h->dst, 0xff, sizeof h->dst);
517                 memcpy(h->src, ifp->dev_addr, sizeof h->src);
518                 h->type = __constant_cpu_to_be16(ETH_P_AOE);
519                 h->verfl = AOE_HVER;
520                 h->major = cpu_to_be16(aoemajor);
521                 h->minor = aoeminor;
522                 h->cmd = AOECMD_CFG;
523
524                 skb->next = sl;
525                 sl = skb;
526         }
527         read_unlock(&dev_base_lock);
528
529         aoenet_xmit(sl);
530 }
531  
532 /*
533  * Since we only call this in one place (and it only prepares one frame)
534  * we just return the skb.  Usually we'd chain it up to the d->skblist.
535  */
536 static struct sk_buff *
537 aoecmd_ata_id(struct aoedev *d)
538 {
539         struct aoe_hdr *h;
540         struct aoe_atahdr *ah;
541         struct frame *f;
542         struct sk_buff *skb;
543
544         f = getframe(d, FREETAG);
545         if (f == NULL) {
546                 printk(KERN_CRIT "aoe: aoecmd_ata_id: can't get a frame.  "
547                         "This shouldn't happen.\n");
548                 return NULL;
549         }
550
551         /* initialize the headers & frame */
552         h = (struct aoe_hdr *) f->data;
553         ah = (struct aoe_atahdr *) (h+1);
554         f->ndata = sizeof *h + sizeof *ah;
555         memset(h, 0, f->ndata);
556         f->tag = aoehdr_atainit(d, h);
557         f->waited = 0;
558         f->writedatalen = 0;
559
560         /* this message initializes the device, so we reset the rttavg */
561         d->rttavg = MAXTIMER;
562
563         /* set up ata header */
564         ah->scnt = 1;
565         ah->cmdstat = WIN_IDENTIFY;
566         ah->lba3 = 0xa0;
567
568         skb = skb_prepare(d, f);
569
570         /* we now want to start the rexmit tracking */
571         d->flags &= ~DEVFL_TKILL;
572         d->timer.data = (ulong) d;
573         d->timer.function = rexmit_timer;
574         d->timer.expires = jiffies + TIMERTICK;
575         add_timer(&d->timer);
576
577         return skb;
578 }
579  
580 void
581 aoecmd_cfg_rsp(struct sk_buff *skb)
582 {
583         struct aoedev *d;
584         struct aoe_hdr *h;
585         struct aoe_cfghdr *ch;
586         ulong flags, sysminor, aoemajor;
587         u16 bufcnt;
588         struct sk_buff *sl;
589         enum { MAXFRAMES = 8 };
590
591         h = (struct aoe_hdr *) skb->mac.raw;
592         ch = (struct aoe_cfghdr *) (h+1);
593
594         /*
595          * Enough people have their dip switches set backwards to
596          * warrant a loud message for this special case.
597          */
598         aoemajor = be16_to_cpu(h->major);
599         if (aoemajor == 0xfff) {
600                 printk(KERN_CRIT "aoe: aoecmd_cfg_rsp: Warning: shelf "
601                         "address is all ones.  Check shelf dip switches\n");
602                 return;
603         }
604
605         sysminor = SYSMINOR(aoemajor, h->minor);
606         if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
607                 printk(KERN_INFO
608                         "aoe: e%ld.%d: minor number too large\n", 
609                         aoemajor, (int) h->minor);
610                 return;
611         }
612
613         bufcnt = be16_to_cpu(ch->bufcnt);
614         if (bufcnt > MAXFRAMES) /* keep it reasonable */
615                 bufcnt = MAXFRAMES;
616
617         d = aoedev_set(sysminor, h->src, skb->dev, bufcnt);
618         if (d == NULL) {
619                 printk(KERN_INFO "aoe: aoecmd_cfg_rsp: device set failure\n");
620                 return;
621         }
622
623         spin_lock_irqsave(&d->lock, flags);
624
625         if (d->flags & (DEVFL_UP | DEVFL_CLOSEWAIT)) {
626                 spin_unlock_irqrestore(&d->lock, flags);
627                 return;
628         }
629
630         d->fw_ver = be16_to_cpu(ch->fwver);
631
632         /* we get here only if the device is new */
633         sl = aoecmd_ata_id(d);
634
635         spin_unlock_irqrestore(&d->lock, flags);
636
637         aoenet_xmit(sl);
638 }
639