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