blob: ada4a06645e1aaff65dc4a5b641a3eef98c50536 [file] [log] [blame]
Ed L. Cashin26114642006-09-20 14:36:48 -04001/* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Eric W. Biedermane730c152007-09-17 11:53:39 -070011#include <net/net_namespace.h>
David S. Miller43ecf522007-03-01 18:30:08 -080012#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "aoe.h"
14
15#define NECODES 5
16
17static 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
27enum {
28 IFLISTSZ = 1024,
29};
30
31static char aoe_iflist[IFLISTSZ];
Ed L Cashin03c41c42005-04-29 10:24:03 -040032module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
33MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"\n");
34
35#ifndef MODULE
36static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46int
47is_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
Ed L Cashin03c41c42005-04-29 10:24:03 -040055 p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
56 for (; *p; p = q + strspn(q, WHITESPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 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
72int
73set_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)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -040079 printk(KERN_INFO "aoe: copy from user failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EFAULT;
81 }
82 aoe_iflist[size] = 0x00;
83 return 0;
84}
85
Ed L. Cashin1eb0da42008-02-08 04:20:01 -080086unsigned long long
Linus Torvalds1da177e2005-04-16 15:20:36 -070087mac_addr(char addr[6])
88{
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -070089 __be64 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 char *p = (char *) &n;
91
92 memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */
93
Ed L. Cashin1eb0da42008-02-08 04:20:01 -080094 return (unsigned long long) __be64_to_cpu(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097void
98aoenet_xmit(struct sk_buff *sl)
99{
100 struct sk_buff *skb;
101
102 while ((skb = sl)) {
103 sl = sl->next;
104 skb->next = skb->prev = NULL;
105 dev_queue_xmit(skb);
106 }
107}
108
109/*
110 * (1) len doesn't include the header by default. I want this.
111 */
112static int
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700113aoenet_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 -0700114{
115 struct aoe_hdr *h;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700116 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Eric W. Biedermane730c152007-09-17 11:53:39 -0700118 if (ifp->nd_net != &init_net)
119 goto exit;
120
Ed L. Cashin5dc401e2006-02-07 11:26:39 -0500121 skb = skb_share_check(skb, GFP_ATOMIC);
122 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
Herbert Xu364c6ba2006-06-09 16:10:40 -0700124 if (skb_linearize(skb))
Ed L. Cashin5dc401e2006-02-07 11:26:39 -0500125 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!is_aoe_netif(ifp))
127 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 skb_push(skb, ETH_HLEN); /* (1) */
129
Ed L. Cashinabdbf942007-10-16 23:27:03 -0700130 h = (struct aoe_hdr *) skb_mac_header(skb);
David S. Miller43ecf522007-03-01 18:30:08 -0800131 n = be32_to_cpu(get_unaligned(&h->tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
133 goto exit;
134
135 if (h->verfl & AOEFL_ERR) {
136 n = h->err;
137 if (n > NECODES)
138 n = 0;
139 if (net_ratelimit())
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800140 printk(KERN_ERR
141 "%s%d.%d@%s; ecode=%d '%s'\n",
142 "aoe: error packet from ",
143 be16_to_cpu(get_unaligned(&h->major)),
144 h->minor, skb->dev->name,
145 h->err, aoe_errlist[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto exit;
147 }
148
149 switch (h->cmd) {
150 case AOECMD_ATA:
151 aoecmd_ata_rsp(skb);
152 break;
153 case AOECMD_CFG:
154 aoecmd_cfg_rsp(skb);
155 break;
156 default:
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400157 printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159exit:
160 dev_kfree_skb(skb);
161 return 0;
162}
163
164static struct packet_type aoe_pt = {
165 .type = __constant_htons(ETH_P_AOE),
166 .func = aoenet_rcv,
167};
168
169int __init
170aoenet_init(void)
171{
172 dev_add_pack(&aoe_pt);
173 return 0;
174}
175
176void
177aoenet_exit(void)
178{
179 dev_remove_pack(&aoe_pt);
180}
181