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> 11*e730c155SEric W. Biederman #include <net/net_namespace.h> 1243ecf529SDavid S. Miller #include <asm/unaligned.h> 131da177e4SLinus Torvalds #include "aoe.h" 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #define NECODES 5 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds static char *aoe_errlist[] = 181da177e4SLinus Torvalds { 191da177e4SLinus Torvalds "no such error", 201da177e4SLinus Torvalds "unrecognized command code", 211da177e4SLinus Torvalds "bad argument parameter", 221da177e4SLinus Torvalds "device unavailable", 231da177e4SLinus Torvalds "config string present", 241da177e4SLinus Torvalds "unsupported version" 251da177e4SLinus Torvalds }; 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds enum { 281da177e4SLinus Torvalds IFLISTSZ = 1024, 291da177e4SLinus Torvalds }; 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds static char aoe_iflist[IFLISTSZ]; 3203c41c43SEd L Cashin module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600); 3303c41c43SEd L Cashin MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"\n"); 3403c41c43SEd L Cashin 3503c41c43SEd L Cashin #ifndef MODULE 3603c41c43SEd L Cashin static int __init aoe_iflist_setup(char *str) 3703c41c43SEd L Cashin { 3803c41c43SEd L Cashin strncpy(aoe_iflist, str, IFLISTSZ); 3903c41c43SEd L Cashin aoe_iflist[IFLISTSZ - 1] = '\0'; 4003c41c43SEd L Cashin return 1; 4103c41c43SEd L Cashin } 4203c41c43SEd L Cashin 4303c41c43SEd L Cashin __setup("aoe_iflist=", aoe_iflist_setup); 4403c41c43SEd L Cashin #endif 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds int 471da177e4SLinus Torvalds is_aoe_netif(struct net_device *ifp) 481da177e4SLinus Torvalds { 491da177e4SLinus Torvalds register char *p, *q; 501da177e4SLinus Torvalds register int len; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds if (aoe_iflist[0] == '\0') 531da177e4SLinus Torvalds return 1; 541da177e4SLinus Torvalds 5503c41c43SEd L Cashin p = aoe_iflist + strspn(aoe_iflist, WHITESPACE); 5603c41c43SEd L Cashin for (; *p; p = q + strspn(q, WHITESPACE)) { 571da177e4SLinus Torvalds q = p + strcspn(p, WHITESPACE); 581da177e4SLinus Torvalds if (q != p) 591da177e4SLinus Torvalds len = q - p; 601da177e4SLinus Torvalds else 611da177e4SLinus Torvalds len = strlen(p); /* last token in aoe_iflist */ 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len)) 641da177e4SLinus Torvalds return 1; 651da177e4SLinus Torvalds if (q == p) 661da177e4SLinus Torvalds break; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds return 0; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds int 731da177e4SLinus Torvalds set_aoe_iflist(const char __user *user_str, size_t size) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds if (size >= IFLISTSZ) 761da177e4SLinus Torvalds return -EINVAL; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds if (copy_from_user(aoe_iflist, user_str, size)) { 79a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: copy from user failed\n"); 801da177e4SLinus Torvalds return -EFAULT; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds aoe_iflist[size] = 0x00; 831da177e4SLinus Torvalds return 0; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds u64 871da177e4SLinus Torvalds mac_addr(char addr[6]) 881da177e4SLinus Torvalds { 8963e9cc5dSecashin@coraid.com __be64 n = 0; 901da177e4SLinus Torvalds char *p = (char *) &n; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */ 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds return __be64_to_cpu(n); 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds void 981da177e4SLinus Torvalds aoenet_xmit(struct sk_buff *sl) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds struct sk_buff *skb; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds while ((skb = sl)) { 1031da177e4SLinus Torvalds sl = sl->next; 1041da177e4SLinus Torvalds skb->next = skb->prev = NULL; 1051da177e4SLinus Torvalds dev_queue_xmit(skb); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* 1101da177e4SLinus Torvalds * (1) len doesn't include the header by default. I want this. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds static int 113f2ccd8faSDavid S. Miller aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds struct aoe_hdr *h; 11663e9cc5dSecashin@coraid.com u32 n; 1171da177e4SLinus Torvalds 118*e730c155SEric W. Biederman if (ifp->nd_net != &init_net) 119*e730c155SEric W. Biederman goto exit; 120*e730c155SEric W. Biederman 1215dc401eeSEd L. Cashin skb = skb_share_check(skb, GFP_ATOMIC); 1225dc401eeSEd L. Cashin if (skb == NULL) 1231da177e4SLinus Torvalds return 0; 124364c6badSHerbert Xu if (skb_linearize(skb)) 1255dc401eeSEd L. Cashin goto exit; 1261da177e4SLinus Torvalds if (!is_aoe_netif(ifp)) 1271da177e4SLinus Torvalds goto exit; 1281da177e4SLinus Torvalds skb_push(skb, ETH_HLEN); /* (1) */ 1291da177e4SLinus Torvalds 130029720f1SArnaldo Carvalho de Melo h = aoe_hdr(skb); 13143ecf529SDavid S. Miller n = be32_to_cpu(get_unaligned(&h->tag)); 1321da177e4SLinus Torvalds if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) 1331da177e4SLinus Torvalds goto exit; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds if (h->verfl & AOEFL_ERR) { 1361da177e4SLinus Torvalds n = h->err; 1371da177e4SLinus Torvalds if (n > NECODES) 1381da177e4SLinus Torvalds n = 0; 1391da177e4SLinus Torvalds if (net_ratelimit()) 140a12c93f0SEd L. Cashin printk(KERN_ERR "aoe: error packet from %d.%d; ecode=%d '%s'\n", 14143ecf529SDavid S. Miller be16_to_cpu(get_unaligned(&h->major)), h->minor, 1421da177e4SLinus Torvalds h->err, aoe_errlist[n]); 1431da177e4SLinus Torvalds goto exit; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds switch (h->cmd) { 1471da177e4SLinus Torvalds case AOECMD_ATA: 1481da177e4SLinus Torvalds aoecmd_ata_rsp(skb); 1491da177e4SLinus Torvalds break; 1501da177e4SLinus Torvalds case AOECMD_CFG: 1511da177e4SLinus Torvalds aoecmd_cfg_rsp(skb); 1521da177e4SLinus Torvalds break; 1531da177e4SLinus Torvalds default: 154a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds exit: 1571da177e4SLinus Torvalds dev_kfree_skb(skb); 1581da177e4SLinus Torvalds return 0; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static struct packet_type aoe_pt = { 1621da177e4SLinus Torvalds .type = __constant_htons(ETH_P_AOE), 1631da177e4SLinus Torvalds .func = aoenet_rcv, 1641da177e4SLinus Torvalds }; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds int __init 1671da177e4SLinus Torvalds aoenet_init(void) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds dev_add_pack(&aoe_pt); 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds void 1741da177e4SLinus Torvalds aoenet_exit(void) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds dev_remove_pack(&aoe_pt); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179