blob: 4be976940f6971f28bc03b18bab09312c0be9957 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Copyright (c) 2004 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>
Ed L Cashin03c41c42005-04-29 10:24:03 -040010#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "aoe.h"
12
13#define NECODES 5
14
15static char *aoe_errlist[] =
16{
17 "no such error",
18 "unrecognized command code",
19 "bad argument parameter",
20 "device unavailable",
21 "config string present",
22 "unsupported version"
23};
24
25enum {
26 IFLISTSZ = 1024,
27};
28
29static char aoe_iflist[IFLISTSZ];
Ed L Cashin03c41c42005-04-29 10:24:03 -040030module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
31MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"\n");
32
33#ifndef MODULE
34static int __init aoe_iflist_setup(char *str)
35{
36 strncpy(aoe_iflist, str, IFLISTSZ);
37 aoe_iflist[IFLISTSZ - 1] = '\0';
38 return 1;
39}
40
41__setup("aoe_iflist=", aoe_iflist_setup);
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44int
45is_aoe_netif(struct net_device *ifp)
46{
47 register char *p, *q;
48 register int len;
49
50 if (aoe_iflist[0] == '\0')
51 return 1;
52
Ed L Cashin03c41c42005-04-29 10:24:03 -040053 p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
54 for (; *p; p = q + strspn(q, WHITESPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 q = p + strcspn(p, WHITESPACE);
56 if (q != p)
57 len = q - p;
58 else
59 len = strlen(p); /* last token in aoe_iflist */
60
61 if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
62 return 1;
63 if (q == p)
64 break;
65 }
66
67 return 0;
68}
69
70int
71set_aoe_iflist(const char __user *user_str, size_t size)
72{
73 if (size >= IFLISTSZ)
74 return -EINVAL;
75
76 if (copy_from_user(aoe_iflist, user_str, size)) {
77 printk(KERN_INFO "aoe: %s: copy from user failed\n", __FUNCTION__);
78 return -EFAULT;
79 }
80 aoe_iflist[size] = 0x00;
81 return 0;
82}
83
84u64
85mac_addr(char addr[6])
86{
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070087 __be64 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char *p = (char *) &n;
89
90 memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */
91
92 return __be64_to_cpu(n);
93}
94
95static struct sk_buff *
96skb_check(struct sk_buff *skb)
97{
98 if (skb_is_nonlinear(skb))
99 if ((skb = skb_share_check(skb, GFP_ATOMIC)))
100 if (skb_linearize(skb, GFP_ATOMIC) < 0) {
101 dev_kfree_skb(skb);
102 return NULL;
103 }
104 return skb;
105}
106
107void
108aoenet_xmit(struct sk_buff *sl)
109{
110 struct sk_buff *skb;
111
112 while ((skb = sl)) {
113 sl = sl->next;
114 skb->next = skb->prev = NULL;
115 dev_queue_xmit(skb);
116 }
117}
118
119/*
120 * (1) len doesn't include the header by default. I want this.
121 */
122static int
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700123aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct aoe_hdr *h;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700126 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 skb = skb_check(skb);
129 if (!skb)
130 return 0;
131
132 if (!is_aoe_netif(ifp))
133 goto exit;
134
135 //skb->len += ETH_HLEN; /* (1) */
136 skb_push(skb, ETH_HLEN); /* (1) */
137
138 h = (struct aoe_hdr *) skb->mac.raw;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700139 n = be32_to_cpu(h->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
141 goto exit;
142
143 if (h->verfl & AOEFL_ERR) {
144 n = h->err;
145 if (n > NECODES)
146 n = 0;
147 if (net_ratelimit())
148 printk(KERN_ERR "aoe: aoenet_rcv: error packet from %d.%d; "
149 "ecode=%d '%s'\n",
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700150 be16_to_cpu(h->major), h->minor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 h->err, aoe_errlist[n]);
152 goto exit;
153 }
154
155 switch (h->cmd) {
156 case AOECMD_ATA:
157 aoecmd_ata_rsp(skb);
158 break;
159 case AOECMD_CFG:
160 aoecmd_cfg_rsp(skb);
161 break;
162 default:
163 printk(KERN_INFO "aoe: aoenet_rcv: unknown cmd %d\n", h->cmd);
164 }
165exit:
166 dev_kfree_skb(skb);
167 return 0;
168}
169
170static struct packet_type aoe_pt = {
171 .type = __constant_htons(ETH_P_AOE),
172 .func = aoenet_rcv,
173};
174
175int __init
176aoenet_init(void)
177{
178 dev_add_pack(&aoe_pt);
179 return 0;
180}
181
182void
183aoenet_exit(void)
184{
185 dev_remove_pack(&aoe_pt);
186}
187