xref: /linux/drivers/block/aoe/aoenet.c (revision a12c93f08b8fc83b7fcdabaf92b1adcea7489f5e)
12611464dSEd L. Cashin /* Copyright (c) 2006 Coraid, Inc.  See COPYING for GPL terms. */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * aoenet.c
41da177e4SLinus Torvalds  * Ethernet portion of AoE driver
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/hdreg.h>
81da177e4SLinus Torvalds #include <linux/blkdev.h>
91da177e4SLinus Torvalds #include <linux/netdevice.h>
1003c41c43SEd L Cashin #include <linux/moduleparam.h>
111da177e4SLinus Torvalds #include "aoe.h"
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #define NECODES 5
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds static char *aoe_errlist[] =
161da177e4SLinus Torvalds {
171da177e4SLinus Torvalds 	"no such error",
181da177e4SLinus Torvalds 	"unrecognized command code",
191da177e4SLinus Torvalds 	"bad argument parameter",
201da177e4SLinus Torvalds 	"device unavailable",
211da177e4SLinus Torvalds 	"config string present",
221da177e4SLinus Torvalds 	"unsupported version"
231da177e4SLinus Torvalds };
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds enum {
261da177e4SLinus Torvalds 	IFLISTSZ = 1024,
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static char aoe_iflist[IFLISTSZ];
3003c41c43SEd L Cashin module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
3103c41c43SEd L Cashin MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"\n");
3203c41c43SEd L Cashin 
3303c41c43SEd L Cashin #ifndef MODULE
3403c41c43SEd L Cashin static int __init aoe_iflist_setup(char *str)
3503c41c43SEd L Cashin {
3603c41c43SEd L Cashin 	strncpy(aoe_iflist, str, IFLISTSZ);
3703c41c43SEd L Cashin 	aoe_iflist[IFLISTSZ - 1] = '\0';
3803c41c43SEd L Cashin 	return 1;
3903c41c43SEd L Cashin }
4003c41c43SEd L Cashin 
4103c41c43SEd L Cashin __setup("aoe_iflist=", aoe_iflist_setup);
4203c41c43SEd L Cashin #endif
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds int
451da177e4SLinus Torvalds is_aoe_netif(struct net_device *ifp)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	register char *p, *q;
481da177e4SLinus Torvalds 	register int len;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	if (aoe_iflist[0] == '\0')
511da177e4SLinus Torvalds 		return 1;
521da177e4SLinus Torvalds 
5303c41c43SEd L Cashin 	p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
5403c41c43SEd L Cashin 	for (; *p; p = q + strspn(q, WHITESPACE)) {
551da177e4SLinus Torvalds 		q = p + strcspn(p, WHITESPACE);
561da177e4SLinus Torvalds 		if (q != p)
571da177e4SLinus Torvalds 			len = q - p;
581da177e4SLinus Torvalds 		else
591da177e4SLinus Torvalds 			len = strlen(p); /* last token in aoe_iflist */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 		if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
621da177e4SLinus Torvalds 			return 1;
631da177e4SLinus Torvalds 		if (q == p)
641da177e4SLinus Torvalds 			break;
651da177e4SLinus Torvalds 	}
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	return 0;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds int
711da177e4SLinus Torvalds set_aoe_iflist(const char __user *user_str, size_t size)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	if (size >= IFLISTSZ)
741da177e4SLinus Torvalds 		return -EINVAL;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (copy_from_user(aoe_iflist, user_str, size)) {
77*a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: copy from user failed\n");
781da177e4SLinus Torvalds 		return -EFAULT;
791da177e4SLinus Torvalds 	}
801da177e4SLinus Torvalds 	aoe_iflist[size] = 0x00;
811da177e4SLinus Torvalds 	return 0;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds u64
851da177e4SLinus Torvalds mac_addr(char addr[6])
861da177e4SLinus Torvalds {
8763e9cc5dSecashin@coraid.com 	__be64 n = 0;
881da177e4SLinus Torvalds 	char *p = (char *) &n;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	memcpy(p + 2, addr, 6);	/* (sizeof addr != 6) */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	return __be64_to_cpu(n);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds void
961da177e4SLinus Torvalds aoenet_xmit(struct sk_buff *sl)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct sk_buff *skb;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	while ((skb = sl)) {
1011da177e4SLinus Torvalds 		sl = sl->next;
1021da177e4SLinus Torvalds 		skb->next = skb->prev = NULL;
1031da177e4SLinus Torvalds 		dev_queue_xmit(skb);
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * (1) len doesn't include the header by default.  I want this.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds static int
111f2ccd8faSDavid S. Miller aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct aoe_hdr *h;
11463e9cc5dSecashin@coraid.com 	u32 n;
1151da177e4SLinus Torvalds 
1165dc401eeSEd L. Cashin 	skb = skb_share_check(skb, GFP_ATOMIC);
1175dc401eeSEd L. Cashin 	if (skb == NULL)
1181da177e4SLinus Torvalds 		return 0;
119364c6badSHerbert Xu 	if (skb_linearize(skb))
1205dc401eeSEd L. Cashin 		goto exit;
1211da177e4SLinus Torvalds 	if (!is_aoe_netif(ifp))
1221da177e4SLinus Torvalds 		goto exit;
1231da177e4SLinus Torvalds 	skb_push(skb, ETH_HLEN);	/* (1) */
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	h = (struct aoe_hdr *) skb->mac.raw;
12663e9cc5dSecashin@coraid.com 	n = be32_to_cpu(h->tag);
1271da177e4SLinus Torvalds 	if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
1281da177e4SLinus Torvalds 		goto exit;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	if (h->verfl & AOEFL_ERR) {
1311da177e4SLinus Torvalds 		n = h->err;
1321da177e4SLinus Torvalds 		if (n > NECODES)
1331da177e4SLinus Torvalds 			n = 0;
1341da177e4SLinus Torvalds 		if (net_ratelimit())
135*a12c93f0SEd L. Cashin 			printk(KERN_ERR "aoe: error packet from %d.%d; ecode=%d '%s'\n",
13663e9cc5dSecashin@coraid.com 			       be16_to_cpu(h->major), h->minor,
1371da177e4SLinus Torvalds 			       h->err, aoe_errlist[n]);
1381da177e4SLinus Torvalds 		goto exit;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	switch (h->cmd) {
1421da177e4SLinus Torvalds 	case AOECMD_ATA:
1431da177e4SLinus Torvalds 		aoecmd_ata_rsp(skb);
1441da177e4SLinus Torvalds 		break;
1451da177e4SLinus Torvalds 	case AOECMD_CFG:
1461da177e4SLinus Torvalds 		aoecmd_cfg_rsp(skb);
1471da177e4SLinus Torvalds 		break;
1481da177e4SLinus Torvalds 	default:
149*a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds exit:
1521da177e4SLinus Torvalds 	dev_kfree_skb(skb);
1531da177e4SLinus Torvalds 	return 0;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static struct packet_type aoe_pt = {
1571da177e4SLinus Torvalds 	.type = __constant_htons(ETH_P_AOE),
1581da177e4SLinus Torvalds 	.func = aoenet_rcv,
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds int __init
1621da177e4SLinus Torvalds aoenet_init(void)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	dev_add_pack(&aoe_pt);
1651da177e4SLinus Torvalds 	return 0;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds void
1691da177e4SLinus Torvalds aoenet_exit(void)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	dev_remove_pack(&aoe_pt);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
174