xref: /linux/drivers/block/aoe/aoenet.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
1ca47bbd9SEd Cashin /* Copyright (c) 2013 Coraid, Inc.  See COPYING for GPL terms. */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * aoenet.c
41da177e4SLinus Torvalds  * Ethernet portion of AoE driver
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
75a0e3ad6STejun Heo #include <linux/gfp.h>
81da177e4SLinus Torvalds #include <linux/hdreg.h>
91da177e4SLinus Torvalds #include <linux/blkdev.h>
101da177e4SLinus Torvalds #include <linux/netdevice.h>
1103c41c43SEd L Cashin #include <linux/moduleparam.h>
12e730c155SEric W. Biederman #include <net/net_namespace.h>
13*5f60d5f6SAl Viro #include <linux/unaligned.h>
141da177e4SLinus Torvalds #include "aoe.h"
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #define NECODES 5
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds static char *aoe_errlist[] =
191da177e4SLinus Torvalds {
201da177e4SLinus Torvalds 	"no such error",
211da177e4SLinus Torvalds 	"unrecognized command code",
221da177e4SLinus Torvalds 	"bad argument parameter",
231da177e4SLinus Torvalds 	"device unavailable",
241da177e4SLinus Torvalds 	"config string present",
251da177e4SLinus Torvalds 	"unsupported version"
261da177e4SLinus Torvalds };
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds enum {
291da177e4SLinus Torvalds 	IFLISTSZ = 1024,
301da177e4SLinus Torvalds };
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds static char aoe_iflist[IFLISTSZ];
3303c41c43SEd L Cashin module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
344a6c9ee9SEd Cashin MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=dev1[,dev2...]");
3503c41c43SEd L Cashin 
36eb086ec5SEd Cashin static wait_queue_head_t txwq;
37eb086ec5SEd Cashin static struct ktstate kts;
38eb086ec5SEd Cashin 
3903c41c43SEd L Cashin #ifndef MODULE
aoe_iflist_setup(char * str)4003c41c43SEd L Cashin static int __init aoe_iflist_setup(char *str)
4103c41c43SEd L Cashin {
425e9b7cfcSJustin Stitt 	strscpy(aoe_iflist, str, IFLISTSZ);
4303c41c43SEd L Cashin 	return 1;
4403c41c43SEd L Cashin }
4503c41c43SEd L Cashin 
4603c41c43SEd L Cashin __setup("aoe_iflist=", aoe_iflist_setup);
4703c41c43SEd L Cashin #endif
481da177e4SLinus Torvalds 
49eb086ec5SEd Cashin static spinlock_t txlock;
50eb086ec5SEd Cashin static struct sk_buff_head skbtxq;
51eb086ec5SEd Cashin 
52eb086ec5SEd Cashin /* enters with txlock held */
53eb086ec5SEd Cashin static int
tx(int id)548030d343SEd Cashin tx(int id) __must_hold(&txlock)
55eb086ec5SEd Cashin {
56eb086ec5SEd Cashin 	struct sk_buff *skb;
574e78dd14SEd Cashin 	struct net_device *ifp;
58eb086ec5SEd Cashin 
59eb086ec5SEd Cashin 	while ((skb = skb_dequeue(&skbtxq))) {
60eb086ec5SEd Cashin 		spin_unlock_irq(&txlock);
614e78dd14SEd Cashin 		ifp = skb->dev;
624e78dd14SEd Cashin 		if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit())
634e78dd14SEd Cashin 			pr_warn("aoe: packet could not be sent on %s.  %s\n",
644e78dd14SEd Cashin 				ifp ? ifp->name : "netif",
654e78dd14SEd Cashin 				"consider increasing tx_queue_len");
66f98364e9SChun-Yi Lee 		dev_put(ifp);
67eb086ec5SEd Cashin 		spin_lock_irq(&txlock);
68eb086ec5SEd Cashin 	}
69eb086ec5SEd Cashin 	return 0;
70eb086ec5SEd Cashin }
71eb086ec5SEd Cashin 
721da177e4SLinus Torvalds int
is_aoe_netif(struct net_device * ifp)731da177e4SLinus Torvalds is_aoe_netif(struct net_device *ifp)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	register char *p, *q;
761da177e4SLinus Torvalds 	register int len;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	if (aoe_iflist[0] == '\0')
791da177e4SLinus Torvalds 		return 1;
801da177e4SLinus Torvalds 
8103c41c43SEd L Cashin 	p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
8203c41c43SEd L Cashin 	for (; *p; p = q + strspn(q, WHITESPACE)) {
831da177e4SLinus Torvalds 		q = p + strcspn(p, WHITESPACE);
841da177e4SLinus Torvalds 		if (q != p)
851da177e4SLinus Torvalds 			len = q - p;
861da177e4SLinus Torvalds 		else
871da177e4SLinus Torvalds 			len = strlen(p); /* last token in aoe_iflist */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 		if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
901da177e4SLinus Torvalds 			return 1;
911da177e4SLinus Torvalds 		if (q == p)
921da177e4SLinus Torvalds 			break;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds int
set_aoe_iflist(const char __user * user_str,size_t size)991da177e4SLinus Torvalds set_aoe_iflist(const char __user *user_str, size_t size)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	if (size >= IFLISTSZ)
1021da177e4SLinus Torvalds 		return -EINVAL;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	if (copy_from_user(aoe_iflist, user_str, size)) {
105a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: copy from user failed\n");
1061da177e4SLinus Torvalds 		return -EFAULT;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds 	aoe_iflist[size] = 0x00;
1091da177e4SLinus Torvalds 	return 0;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds void
aoenet_xmit(struct sk_buff_head * queue)113e9bb8fb0SDavid S. Miller aoenet_xmit(struct sk_buff_head *queue)
1141da177e4SLinus Torvalds {
115e9bb8fb0SDavid S. Miller 	struct sk_buff *skb, *tmp;
116eb086ec5SEd Cashin 	ulong flags;
1171da177e4SLinus Torvalds 
118d8779845SDavid S. Miller 	skb_queue_walk_safe(queue, skb, tmp) {
119d8779845SDavid S. Miller 		__skb_unlink(skb, queue);
120eb086ec5SEd Cashin 		spin_lock_irqsave(&txlock, flags);
121eb086ec5SEd Cashin 		skb_queue_tail(&skbtxq, skb);
122eb086ec5SEd Cashin 		spin_unlock_irqrestore(&txlock, flags);
123eb086ec5SEd Cashin 		wake_up(&txwq);
1241da177e4SLinus Torvalds 	}
125d8779845SDavid S. Miller }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /*
1281da177e4SLinus Torvalds  * (1) len doesn't include the header by default.  I want this.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds static int
aoenet_rcv(struct sk_buff * skb,struct net_device * ifp,struct packet_type * pt,struct net_device * orig_dev)131f2ccd8faSDavid S. Miller aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct aoe_hdr *h;
1343d5b0605SEd Cashin 	struct aoe_atahdr *ah;
13563e9cc5dSecashin@coraid.com 	u32 n;
1363d5b0605SEd Cashin 	int sn;
1371da177e4SLinus Torvalds 
138c346dca1SYOSHIFUJI Hideaki 	if (dev_net(ifp) != &init_net)
139e730c155SEric W. Biederman 		goto exit;
140e730c155SEric W. Biederman 
1415dc401eeSEd L. Cashin 	skb = skb_share_check(skb, GFP_ATOMIC);
1425dc401eeSEd L. Cashin 	if (skb == NULL)
1431da177e4SLinus Torvalds 		return 0;
1441da177e4SLinus Torvalds 	if (!is_aoe_netif(ifp))
1451da177e4SLinus Torvalds 		goto exit;
1461da177e4SLinus Torvalds 	skb_push(skb, ETH_HLEN);	/* (1) */
1473d5b0605SEd Cashin 	sn = sizeof(*h) + sizeof(*ah);
1483d5b0605SEd Cashin 	if (skb->len >= sn) {
1493d5b0605SEd Cashin 		sn -= skb_headlen(skb);
1503d5b0605SEd Cashin 		if (sn > 0 && !__pskb_pull_tail(skb, sn))
1513d5b0605SEd Cashin 			goto exit;
1523d5b0605SEd Cashin 	}
1533d5b0605SEd Cashin 	h = (struct aoe_hdr *) skb->data;
154f885f8d1SHarvey Harrison 	n = get_unaligned_be32(&h->tag);
1551da177e4SLinus Torvalds 	if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
1561da177e4SLinus Torvalds 		goto exit;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (h->verfl & AOEFL_ERR) {
1591da177e4SLinus Torvalds 		n = h->err;
1601da177e4SLinus Torvalds 		if (n > NECODES)
1611da177e4SLinus Torvalds 			n = 0;
1621da177e4SLinus Torvalds 		if (net_ratelimit())
16368e0d42fSEd L. Cashin 			printk(KERN_ERR
16468e0d42fSEd L. Cashin 				"%s%d.%d@%s; ecode=%d '%s'\n",
16568e0d42fSEd L. Cashin 				"aoe: error packet from ",
166f885f8d1SHarvey Harrison 				get_unaligned_be16(&h->major),
16768e0d42fSEd L. Cashin 				h->minor, skb->dev->name,
1681da177e4SLinus Torvalds 				h->err, aoe_errlist[n]);
1691da177e4SLinus Torvalds 		goto exit;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	switch (h->cmd) {
1731da177e4SLinus Torvalds 	case AOECMD_ATA:
174896831f5SEd Cashin 		/* ata_rsp may keep skb for later processing or give it back */
175896831f5SEd Cashin 		skb = aoecmd_ata_rsp(skb);
1761da177e4SLinus Torvalds 		break;
1771da177e4SLinus Torvalds 	case AOECMD_CFG:
1781da177e4SLinus Torvalds 		aoecmd_cfg_rsp(skb);
1791da177e4SLinus Torvalds 		break;
1801da177e4SLinus Torvalds 	default:
181b6d6c517SEd Cashin 		if (h->cmd >= AOECMD_VEND_MIN)
182b6d6c517SEd Cashin 			break;	/* don't complain about vendor commands */
183b21faa25SEd Cashin 		pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd);
184b21faa25SEd Cashin 		break;
1851da177e4SLinus Torvalds 	}
186896831f5SEd Cashin 
187896831f5SEd Cashin 	if (!skb)
188896831f5SEd Cashin 		return 0;
1891da177e4SLinus Torvalds exit:
1901da177e4SLinus Torvalds 	dev_kfree_skb(skb);
1911da177e4SLinus Torvalds 	return 0;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1947546dd97SStephen Hemminger static struct packet_type aoe_pt __read_mostly = {
1951da177e4SLinus Torvalds 	.type = __constant_htons(ETH_P_AOE),
1961da177e4SLinus Torvalds 	.func = aoenet_rcv,
1971da177e4SLinus Torvalds };
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds int __init
aoenet_init(void)2001da177e4SLinus Torvalds aoenet_init(void)
2011da177e4SLinus Torvalds {
202eb086ec5SEd Cashin 	skb_queue_head_init(&skbtxq);
203eb086ec5SEd Cashin 	init_waitqueue_head(&txwq);
204eb086ec5SEd Cashin 	spin_lock_init(&txlock);
205eb086ec5SEd Cashin 	kts.lock = &txlock;
206eb086ec5SEd Cashin 	kts.fn = tx;
207eb086ec5SEd Cashin 	kts.waitq = &txwq;
2088030d343SEd Cashin 	kts.id = 0;
2098030d343SEd Cashin 	snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id);
210eb086ec5SEd Cashin 	if (aoe_ktstart(&kts))
211eb086ec5SEd Cashin 		return -EAGAIN;
2121da177e4SLinus Torvalds 	dev_add_pack(&aoe_pt);
2131da177e4SLinus Torvalds 	return 0;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds void
aoenet_exit(void)2171da177e4SLinus Torvalds aoenet_exit(void)
2181da177e4SLinus Torvalds {
219eb086ec5SEd Cashin 	aoe_ktstop(&kts);
220eb086ec5SEd Cashin 	skb_queue_purge(&skbtxq);
2211da177e4SLinus Torvalds 	dev_remove_pack(&aoe_pt);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224