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