blob: 63773a90581dd36818051a114e38b5b60af0a874 [file] [log] [blame]
Ed Cashinca47bbd2013-07-03 15:09:06 -07001/* Copyright (c) 2013 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/hdreg.h>
9#include <linux/blkdev.h>
10#include <linux/netdevice.h>
Ed L Cashin03c41c42005-04-29 10:24:03 -040011#include <linux/moduleparam.h>
Eric W. Biedermane730c152007-09-17 11:53:39 -070012#include <net/net_namespace.h>
David S. Miller43ecf522007-03-01 18:30:08 -080013#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "aoe.h"
15
16#define NECODES 5
17
18static char *aoe_errlist[] =
19{
20 "no such error",
21 "unrecognized command code",
22 "bad argument parameter",
23 "device unavailable",
24 "config string present",
25 "unsupported version"
26};
27
28enum {
29 IFLISTSZ = 1024,
30};
31
32static char aoe_iflist[IFLISTSZ];
Ed L Cashin03c41c42005-04-29 10:24:03 -040033module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
Ed Cashin4a6c9ee2012-12-17 16:04:15 -080034MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=dev1[,dev2...]");
Ed L Cashin03c41c42005-04-29 10:24:03 -040035
Ed Cashineb086ec2012-10-04 17:16:25 -070036static wait_queue_head_t txwq;
37static struct ktstate kts;
38
Ed L Cashin03c41c42005-04-29 10:24:03 -040039#ifndef MODULE
40static int __init aoe_iflist_setup(char *str)
41{
42 strncpy(aoe_iflist, str, IFLISTSZ);
43 aoe_iflist[IFLISTSZ - 1] = '\0';
44 return 1;
45}
46
47__setup("aoe_iflist=", aoe_iflist_setup);
48#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ed Cashineb086ec2012-10-04 17:16:25 -070050static spinlock_t txlock;
51static struct sk_buff_head skbtxq;
52
53/* enters with txlock held */
54static int
Ed Cashin8030d342013-07-03 15:09:05 -070055tx(int id) __must_hold(&txlock)
Ed Cashineb086ec2012-10-04 17:16:25 -070056{
57 struct sk_buff *skb;
Ed Cashin4e78dd12012-12-17 16:03:28 -080058 struct net_device *ifp;
Ed Cashineb086ec2012-10-04 17:16:25 -070059
60 while ((skb = skb_dequeue(&skbtxq))) {
61 spin_unlock_irq(&txlock);
Ed Cashin4e78dd12012-12-17 16:03:28 -080062 ifp = skb->dev;
63 if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit())
64 pr_warn("aoe: packet could not be sent on %s. %s\n",
65 ifp ? ifp->name : "netif",
66 "consider increasing tx_queue_len");
Ed Cashineb086ec2012-10-04 17:16:25 -070067 spin_lock_irq(&txlock);
68 }
69 return 0;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072int
73is_aoe_netif(struct net_device *ifp)
74{
75 register char *p, *q;
76 register int len;
77
78 if (aoe_iflist[0] == '\0')
79 return 1;
80
Ed L Cashin03c41c42005-04-29 10:24:03 -040081 p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
82 for (; *p; p = q + strspn(q, WHITESPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 q = p + strcspn(p, WHITESPACE);
84 if (q != p)
85 len = q - p;
86 else
87 len = strlen(p); /* last token in aoe_iflist */
88
89 if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
90 return 1;
91 if (q == p)
92 break;
93 }
94
95 return 0;
96}
97
98int
99set_aoe_iflist(const char __user *user_str, size_t size)
100{
101 if (size >= IFLISTSZ)
102 return -EINVAL;
103
104 if (copy_from_user(aoe_iflist, user_str, size)) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400105 printk(KERN_INFO "aoe: copy from user failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EFAULT;
107 }
108 aoe_iflist[size] = 0x00;
109 return 0;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void
David S. Millere9bb8fb02008-09-21 22:36:49 -0700113aoenet_xmit(struct sk_buff_head *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700115 struct sk_buff *skb, *tmp;
Ed Cashineb086ec2012-10-04 17:16:25 -0700116 ulong flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David S. Millerd8779842008-09-23 20:47:22 -0700118 skb_queue_walk_safe(queue, skb, tmp) {
119 __skb_unlink(skb, queue);
Ed Cashineb086ec2012-10-04 17:16:25 -0700120 spin_lock_irqsave(&txlock, flags);
121 skb_queue_tail(&skbtxq, skb);
122 spin_unlock_irqrestore(&txlock, flags);
123 wake_up(&txwq);
David S. Millerd8779842008-09-23 20:47:22 -0700124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Ed Cashina04b41c2012-12-17 16:03:39 -0800127/*
128 * (1) len doesn't include the header by default. I want this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
130static int
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700131aoenet_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 -0700132{
133 struct aoe_hdr *h;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700134 struct aoe_atahdr *ah;
ecashin@coraid.com63e9cc52005-04-18 22:00:20 -0700135 u32 n;
Ed Cashin3d5b0602012-10-04 17:16:20 -0700136 int sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900138 if (dev_net(ifp) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -0700139 goto exit;
140
Ed L. Cashin5dc401e2006-02-07 11:26:39 -0500141 skb = skb_share_check(skb, GFP_ATOMIC);
142 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!is_aoe_netif(ifp))
145 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 skb_push(skb, ETH_HLEN); /* (1) */
Ed Cashin3d5b0602012-10-04 17:16:20 -0700147 sn = sizeof(*h) + sizeof(*ah);
148 if (skb->len >= sn) {
149 sn -= skb_headlen(skb);
150 if (sn > 0 && !__pskb_pull_tail(skb, sn))
151 goto exit;
152 }
153 h = (struct aoe_hdr *) skb->data;
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700154 n = get_unaligned_be32(&h->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
156 goto exit;
157
158 if (h->verfl & AOEFL_ERR) {
159 n = h->err;
160 if (n > NECODES)
161 n = 0;
162 if (net_ratelimit())
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800163 printk(KERN_ERR
164 "%s%d.%d@%s; ecode=%d '%s'\n",
165 "aoe: error packet from ",
Harvey Harrisonf885f8d2008-04-29 01:03:30 -0700166 get_unaligned_be16(&h->major),
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800167 h->minor, skb->dev->name,
168 h->err, aoe_errlist[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto exit;
170 }
171
172 switch (h->cmd) {
173 case AOECMD_ATA:
Ed Cashin896831f2012-10-04 17:16:21 -0700174 /* ata_rsp may keep skb for later processing or give it back */
175 skb = aoecmd_ata_rsp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 case AOECMD_CFG:
178 aoecmd_cfg_rsp(skb);
179 break;
180 default:
Ed Cashinb6d6c512009-02-18 14:48:13 -0800181 if (h->cmd >= AOECMD_VEND_MIN)
182 break; /* don't complain about vendor commands */
Ed Cashinb21faa22012-10-04 17:16:35 -0700183 pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd);
184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Ed Cashin896831f2012-10-04 17:16:21 -0700186
187 if (!skb)
188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189exit:
190 dev_kfree_skb(skb);
191 return 0;
192}
193
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000194static struct packet_type aoe_pt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .type = __constant_htons(ETH_P_AOE),
196 .func = aoenet_rcv,
197};
198
199int __init
200aoenet_init(void)
201{
Ed Cashineb086ec2012-10-04 17:16:25 -0700202 skb_queue_head_init(&skbtxq);
203 init_waitqueue_head(&txwq);
204 spin_lock_init(&txlock);
205 kts.lock = &txlock;
206 kts.fn = tx;
207 kts.waitq = &txwq;
Ed Cashin8030d342013-07-03 15:09:05 -0700208 kts.id = 0;
209 snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id);
Ed Cashineb086ec2012-10-04 17:16:25 -0700210 if (aoe_ktstart(&kts))
211 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 dev_add_pack(&aoe_pt);
213 return 0;
214}
215
216void
217aoenet_exit(void)
218{
Ed Cashineb086ec2012-10-04 17:16:25 -0700219 aoe_ktstop(&kts);
220 skb_queue_purge(&skbtxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev_remove_pack(&aoe_pt);
222}
223