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 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> 11e730c155SEric 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); 3361a2d07dSNiels de Vos MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\""); 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 void 87e9bb8fb0SDavid S. Miller aoenet_xmit(struct sk_buff_head *queue) 881da177e4SLinus Torvalds { 89e9bb8fb0SDavid S. Miller struct sk_buff *skb, *tmp; 901da177e4SLinus Torvalds 91d8779845SDavid S. Miller skb_queue_walk_safe(queue, skb, tmp) { 92d8779845SDavid S. Miller __skb_unlink(skb, queue); 931da177e4SLinus Torvalds dev_queue_xmit(skb); 941da177e4SLinus Torvalds } 95d8779845SDavid S. Miller } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * (1) len doesn't include the header by default. I want this. 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds static int 101f2ccd8faSDavid S. Miller aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds struct aoe_hdr *h; 10463e9cc5dSecashin@coraid.com u32 n; 1051da177e4SLinus Torvalds 106c346dca1SYOSHIFUJI Hideaki if (dev_net(ifp) != &init_net) 107e730c155SEric W. Biederman goto exit; 108e730c155SEric W. Biederman 1095dc401eeSEd L. Cashin skb = skb_share_check(skb, GFP_ATOMIC); 1105dc401eeSEd L. Cashin if (skb == NULL) 1111da177e4SLinus Torvalds return 0; 112364c6badSHerbert Xu if (skb_linearize(skb)) 1135dc401eeSEd L. Cashin goto exit; 1141da177e4SLinus Torvalds if (!is_aoe_netif(ifp)) 1151da177e4SLinus Torvalds goto exit; 1161da177e4SLinus Torvalds skb_push(skb, ETH_HLEN); /* (1) */ 1171da177e4SLinus Torvalds 118abdbf94dSEd L. Cashin h = (struct aoe_hdr *) skb_mac_header(skb); 119f885f8d1SHarvey Harrison n = get_unaligned_be32(&h->tag); 1201da177e4SLinus Torvalds if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) 1211da177e4SLinus Torvalds goto exit; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (h->verfl & AOEFL_ERR) { 1241da177e4SLinus Torvalds n = h->err; 1251da177e4SLinus Torvalds if (n > NECODES) 1261da177e4SLinus Torvalds n = 0; 1271da177e4SLinus Torvalds if (net_ratelimit()) 12868e0d42fSEd L. Cashin printk(KERN_ERR 12968e0d42fSEd L. Cashin "%s%d.%d@%s; ecode=%d '%s'\n", 13068e0d42fSEd L. Cashin "aoe: error packet from ", 131f885f8d1SHarvey Harrison get_unaligned_be16(&h->major), 13268e0d42fSEd L. Cashin h->minor, skb->dev->name, 1331da177e4SLinus Torvalds h->err, aoe_errlist[n]); 1341da177e4SLinus Torvalds goto exit; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds switch (h->cmd) { 1381da177e4SLinus Torvalds case AOECMD_ATA: 1391da177e4SLinus Torvalds aoecmd_ata_rsp(skb); 1401da177e4SLinus Torvalds break; 1411da177e4SLinus Torvalds case AOECMD_CFG: 1421da177e4SLinus Torvalds aoecmd_cfg_rsp(skb); 1431da177e4SLinus Torvalds break; 1441da177e4SLinus Torvalds default: 145b6d6c517SEd Cashin if (h->cmd >= AOECMD_VEND_MIN) 146b6d6c517SEd Cashin break; /* don't complain about vendor commands */ 147a12c93f0SEd L. Cashin printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds exit: 1501da177e4SLinus Torvalds dev_kfree_skb(skb); 1511da177e4SLinus Torvalds return 0; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 154*7546dd97SStephen Hemminger static struct packet_type aoe_pt __read_mostly = { 1551da177e4SLinus Torvalds .type = __constant_htons(ETH_P_AOE), 1561da177e4SLinus Torvalds .func = aoenet_rcv, 1571da177e4SLinus Torvalds }; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds int __init 1601da177e4SLinus Torvalds aoenet_init(void) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds dev_add_pack(&aoe_pt); 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds void 1671da177e4SLinus Torvalds aoenet_exit(void) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds dev_remove_pack(&aoe_pt); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 172