]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/block/aoe/aoenet.c
8fb26030eba8540b3ad7c3c9fd7a995e96b4da1e
[linux-2.6-omap-h63xx.git] / drivers / block / aoe / aoenet.c
1 /* Copyright (c) 2007 Coraid, Inc.  See COPYING for GPL terms. */
2 /*
3  * aoenet.c
4  * Ethernet portion of AoE driver
5  */
6
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/netdevice.h>
10 #include <linux/moduleparam.h>
11 #include <net/net_namespace.h>
12 #include <asm/unaligned.h>
13 #include "aoe.h"
14
15 #define NECODES 5
16
17 static char *aoe_errlist[] =
18 {
19         "no such error",
20         "unrecognized command code",
21         "bad argument parameter",
22         "device unavailable",
23         "config string present",
24         "unsupported version"
25 };
26
27 enum {
28         IFLISTSZ = 1024,
29 };
30
31 static char aoe_iflist[IFLISTSZ];
32 module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
33 MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"");
34
35 #ifndef MODULE
36 static int __init aoe_iflist_setup(char *str)
37 {
38         strncpy(aoe_iflist, str, IFLISTSZ);
39         aoe_iflist[IFLISTSZ - 1] = '\0';
40         return 1;
41 }
42
43 __setup("aoe_iflist=", aoe_iflist_setup);
44 #endif
45
46 int
47 is_aoe_netif(struct net_device *ifp)
48 {
49         register char *p, *q;
50         register int len;
51
52         if (aoe_iflist[0] == '\0')
53                 return 1;
54
55         p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
56         for (; *p; p = q + strspn(q, WHITESPACE)) {
57                 q = p + strcspn(p, WHITESPACE);
58                 if (q != p)
59                         len = q - p;
60                 else
61                         len = strlen(p); /* last token in aoe_iflist */
62
63                 if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
64                         return 1;
65                 if (q == p)
66                         break;
67         }
68
69         return 0;
70 }
71
72 int
73 set_aoe_iflist(const char __user *user_str, size_t size)
74 {
75         if (size >= IFLISTSZ)
76                 return -EINVAL;
77
78         if (copy_from_user(aoe_iflist, user_str, size)) {
79                 printk(KERN_INFO "aoe: copy from user failed\n");
80                 return -EFAULT;
81         }
82         aoe_iflist[size] = 0x00;
83         return 0;
84 }
85
86 unsigned long long
87 mac_addr(char addr[6])
88 {
89         __be64 n = 0;
90         char *p = (char *) &n;
91
92         memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */
93
94         return (unsigned long long) __be64_to_cpu(n);
95 }
96
97 void
98 aoenet_xmit(struct sk_buff_head *queue)
99 {
100         struct sk_buff *skb, *tmp;
101
102         skb_queue_walk_safe(queue, skb, tmp)
103                 dev_queue_xmit(skb);
104 }
105
106 /* 
107  * (1) len doesn't include the header by default.  I want this. 
108  */
109 static int
110 aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
111 {
112         struct aoe_hdr *h;
113         u32 n;
114
115         if (dev_net(ifp) != &init_net)
116                 goto exit;
117
118         skb = skb_share_check(skb, GFP_ATOMIC);
119         if (skb == NULL)
120                 return 0;
121         if (skb_linearize(skb))
122                 goto exit;
123         if (!is_aoe_netif(ifp))
124                 goto exit;
125         skb_push(skb, ETH_HLEN);        /* (1) */
126
127         h = (struct aoe_hdr *) skb_mac_header(skb);
128         n = get_unaligned_be32(&h->tag);
129         if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
130                 goto exit;
131
132         if (h->verfl & AOEFL_ERR) {
133                 n = h->err;
134                 if (n > NECODES)
135                         n = 0;
136                 if (net_ratelimit())
137                         printk(KERN_ERR
138                                 "%s%d.%d@%s; ecode=%d '%s'\n",
139                                 "aoe: error packet from ",
140                                 get_unaligned_be16(&h->major),
141                                 h->minor, skb->dev->name,
142                                 h->err, aoe_errlist[n]);
143                 goto exit;
144         }
145
146         switch (h->cmd) {
147         case AOECMD_ATA:
148                 aoecmd_ata_rsp(skb);
149                 break;
150         case AOECMD_CFG:
151                 aoecmd_cfg_rsp(skb);
152                 break;
153         default:
154                 printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd);
155         }
156 exit:
157         dev_kfree_skb(skb);
158         return 0;
159 }
160
161 static struct packet_type aoe_pt = {
162         .type = __constant_htons(ETH_P_AOE),
163         .func = aoenet_rcv,
164 };
165
166 int __init
167 aoenet_init(void)
168 {
169         dev_add_pack(&aoe_pt);
170         return 0;
171 }
172
173 void
174 aoenet_exit(void)
175 {
176         dev_remove_pack(&aoe_pt);
177 }
178