xref: /linux/drivers/block/aoe/aoenet.c (revision 896831f5909e2733c13c9cb13a1a215f10c3eaa8)
152e112b3SEd L. Cashin /* Copyright (c) 2007 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>
1343ecf529SDavid S. Miller #include <asm/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);
3461a2d07dSNiels de Vos MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"");
3503c41c43SEd L Cashin 
3603c41c43SEd L Cashin #ifndef MODULE
3703c41c43SEd L Cashin static int __init aoe_iflist_setup(char *str)
3803c41c43SEd L Cashin {
3903c41c43SEd L Cashin 	strncpy(aoe_iflist, str, IFLISTSZ);
4003c41c43SEd L Cashin 	aoe_iflist[IFLISTSZ - 1] = '\0';
4103c41c43SEd L Cashin 	return 1;
4203c41c43SEd L Cashin }
4303c41c43SEd L Cashin 
4403c41c43SEd L Cashin __setup("aoe_iflist=", aoe_iflist_setup);
4503c41c43SEd L Cashin #endif
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds int
481da177e4SLinus Torvalds is_aoe_netif(struct net_device *ifp)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	register char *p, *q;
511da177e4SLinus Torvalds 	register int len;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	if (aoe_iflist[0] == '\0')
541da177e4SLinus Torvalds 		return 1;
551da177e4SLinus Torvalds 
5603c41c43SEd L Cashin 	p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
5703c41c43SEd L Cashin 	for (; *p; p = q + strspn(q, WHITESPACE)) {
581da177e4SLinus Torvalds 		q = p + strcspn(p, WHITESPACE);
591da177e4SLinus Torvalds 		if (q != p)
601da177e4SLinus Torvalds 			len = q - p;
611da177e4SLinus Torvalds 		else
621da177e4SLinus Torvalds 			len = strlen(p); /* last token in aoe_iflist */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 		if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
651da177e4SLinus Torvalds 			return 1;
661da177e4SLinus Torvalds 		if (q == p)
671da177e4SLinus Torvalds 			break;
681da177e4SLinus Torvalds 	}
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	return 0;
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds int
741da177e4SLinus Torvalds set_aoe_iflist(const char __user *user_str, size_t size)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	if (size >= IFLISTSZ)
771da177e4SLinus Torvalds 		return -EINVAL;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	if (copy_from_user(aoe_iflist, user_str, size)) {
80a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: copy from user failed\n");
811da177e4SLinus Torvalds 		return -EFAULT;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	aoe_iflist[size] = 0x00;
841da177e4SLinus Torvalds 	return 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds void
88e9bb8fb0SDavid S. Miller aoenet_xmit(struct sk_buff_head *queue)
891da177e4SLinus Torvalds {
90e9bb8fb0SDavid S. Miller 	struct sk_buff *skb, *tmp;
911da177e4SLinus Torvalds 
92d8779845SDavid S. Miller 	skb_queue_walk_safe(queue, skb, tmp) {
93d8779845SDavid S. Miller 		__skb_unlink(skb, queue);
941da177e4SLinus Torvalds 		dev_queue_xmit(skb);
951da177e4SLinus Torvalds 	}
96d8779845SDavid S. Miller }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * (1) len doesn't include the header by default.  I want this.
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds static int
102f2ccd8faSDavid S. Miller aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	struct aoe_hdr *h;
1053d5b0605SEd Cashin 	struct aoe_atahdr *ah;
10663e9cc5dSecashin@coraid.com 	u32 n;
1073d5b0605SEd Cashin 	int sn;
1081da177e4SLinus Torvalds 
109c346dca1SYOSHIFUJI Hideaki 	if (dev_net(ifp) != &init_net)
110e730c155SEric W. Biederman 		goto exit;
111e730c155SEric W. Biederman 
1125dc401eeSEd L. Cashin 	skb = skb_share_check(skb, GFP_ATOMIC);
1135dc401eeSEd L. Cashin 	if (skb == NULL)
1141da177e4SLinus Torvalds 		return 0;
1151da177e4SLinus Torvalds 	if (!is_aoe_netif(ifp))
1161da177e4SLinus Torvalds 		goto exit;
1171da177e4SLinus Torvalds 	skb_push(skb, ETH_HLEN);	/* (1) */
1183d5b0605SEd Cashin 	sn = sizeof(*h) + sizeof(*ah);
1193d5b0605SEd Cashin 	if (skb->len >= sn) {
1203d5b0605SEd Cashin 		sn -= skb_headlen(skb);
1213d5b0605SEd Cashin 		if (sn > 0 && !__pskb_pull_tail(skb, sn))
1223d5b0605SEd Cashin 			goto exit;
1233d5b0605SEd Cashin 	}
1243d5b0605SEd Cashin 	h = (struct aoe_hdr *) skb->data;
125f885f8d1SHarvey Harrison 	n = get_unaligned_be32(&h->tag);
1261da177e4SLinus Torvalds 	if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
1271da177e4SLinus Torvalds 		goto exit;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (h->verfl & AOEFL_ERR) {
1301da177e4SLinus Torvalds 		n = h->err;
1311da177e4SLinus Torvalds 		if (n > NECODES)
1321da177e4SLinus Torvalds 			n = 0;
1331da177e4SLinus Torvalds 		if (net_ratelimit())
13468e0d42fSEd L. Cashin 			printk(KERN_ERR
13568e0d42fSEd L. Cashin 				"%s%d.%d@%s; ecode=%d '%s'\n",
13668e0d42fSEd L. Cashin 				"aoe: error packet from ",
137f885f8d1SHarvey Harrison 				get_unaligned_be16(&h->major),
13868e0d42fSEd L. Cashin 				h->minor, skb->dev->name,
1391da177e4SLinus Torvalds 				h->err, aoe_errlist[n]);
1401da177e4SLinus Torvalds 		goto exit;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	switch (h->cmd) {
1441da177e4SLinus Torvalds 	case AOECMD_ATA:
145*896831f5SEd Cashin 		/* ata_rsp may keep skb for later processing or give it back */
146*896831f5SEd Cashin 		skb = aoecmd_ata_rsp(skb);
1471da177e4SLinus Torvalds 		break;
1481da177e4SLinus Torvalds 	case AOECMD_CFG:
1491da177e4SLinus Torvalds 		aoecmd_cfg_rsp(skb);
1501da177e4SLinus Torvalds 		break;
1511da177e4SLinus Torvalds 	default:
152b6d6c517SEd Cashin 		if (h->cmd >= AOECMD_VEND_MIN)
153b6d6c517SEd Cashin 			break;	/* don't complain about vendor commands */
154a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd);
1551da177e4SLinus Torvalds 	}
156*896831f5SEd Cashin 
157*896831f5SEd Cashin 	if (!skb)
158*896831f5SEd Cashin 		return 0;
1591da177e4SLinus Torvalds exit:
1601da177e4SLinus Torvalds 	dev_kfree_skb(skb);
1611da177e4SLinus Torvalds 	return 0;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1647546dd97SStephen Hemminger static struct packet_type aoe_pt __read_mostly = {
1651da177e4SLinus Torvalds 	.type = __constant_htons(ETH_P_AOE),
1661da177e4SLinus Torvalds 	.func = aoenet_rcv,
1671da177e4SLinus Torvalds };
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds int __init
1701da177e4SLinus Torvalds aoenet_init(void)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	dev_add_pack(&aoe_pt);
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds void
1771da177e4SLinus Torvalds aoenet_exit(void)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	dev_remove_pack(&aoe_pt);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182