11da177e4SLinus Torvalds /* Copyright (c) 2004 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> 101da177e4SLinus Torvalds #include "aoe.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #define NECODES 5 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds static char *aoe_errlist[] = 151da177e4SLinus Torvalds { 161da177e4SLinus Torvalds "no such error", 171da177e4SLinus Torvalds "unrecognized command code", 181da177e4SLinus Torvalds "bad argument parameter", 191da177e4SLinus Torvalds "device unavailable", 201da177e4SLinus Torvalds "config string present", 211da177e4SLinus Torvalds "unsupported version" 221da177e4SLinus Torvalds }; 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds enum { 251da177e4SLinus Torvalds IFLISTSZ = 1024, 261da177e4SLinus Torvalds }; 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds static char aoe_iflist[IFLISTSZ]; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds int 311da177e4SLinus Torvalds is_aoe_netif(struct net_device *ifp) 321da177e4SLinus Torvalds { 331da177e4SLinus Torvalds register char *p, *q; 341da177e4SLinus Torvalds register int len; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds if (aoe_iflist[0] == '\0') 371da177e4SLinus Torvalds return 1; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds for (p = aoe_iflist; *p; p = q + strspn(q, WHITESPACE)) { 401da177e4SLinus Torvalds q = p + strcspn(p, WHITESPACE); 411da177e4SLinus Torvalds if (q != p) 421da177e4SLinus Torvalds len = q - p; 431da177e4SLinus Torvalds else 441da177e4SLinus Torvalds len = strlen(p); /* last token in aoe_iflist */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len)) 471da177e4SLinus Torvalds return 1; 481da177e4SLinus Torvalds if (q == p) 491da177e4SLinus Torvalds break; 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds return 0; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds int 561da177e4SLinus Torvalds set_aoe_iflist(const char __user *user_str, size_t size) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds if (size >= IFLISTSZ) 591da177e4SLinus Torvalds return -EINVAL; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds if (copy_from_user(aoe_iflist, user_str, size)) { 621da177e4SLinus Torvalds printk(KERN_INFO "aoe: %s: copy from user failed\n", __FUNCTION__); 631da177e4SLinus Torvalds return -EFAULT; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds aoe_iflist[size] = 0x00; 661da177e4SLinus Torvalds return 0; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds u64 701da177e4SLinus Torvalds mac_addr(char addr[6]) 711da177e4SLinus Torvalds { 72*63e9cc5dSecashin@coraid.com __be64 n = 0; 731da177e4SLinus Torvalds char *p = (char *) &n; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */ 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds return __be64_to_cpu(n); 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static struct sk_buff * 811da177e4SLinus Torvalds skb_check(struct sk_buff *skb) 821da177e4SLinus Torvalds { 831da177e4SLinus Torvalds if (skb_is_nonlinear(skb)) 841da177e4SLinus Torvalds if ((skb = skb_share_check(skb, GFP_ATOMIC))) 851da177e4SLinus Torvalds if (skb_linearize(skb, GFP_ATOMIC) < 0) { 861da177e4SLinus Torvalds dev_kfree_skb(skb); 871da177e4SLinus Torvalds return NULL; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds return skb; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds void 931da177e4SLinus Torvalds aoenet_xmit(struct sk_buff *sl) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds struct sk_buff *skb; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds while ((skb = sl)) { 981da177e4SLinus Torvalds sl = sl->next; 991da177e4SLinus Torvalds skb->next = skb->prev = NULL; 1001da177e4SLinus Torvalds dev_queue_xmit(skb); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * (1) len doesn't include the header by default. I want this. 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds static int 1081da177e4SLinus Torvalds aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds struct aoe_hdr *h; 111*63e9cc5dSecashin@coraid.com u32 n; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds skb = skb_check(skb); 1141da177e4SLinus Torvalds if (!skb) 1151da177e4SLinus Torvalds return 0; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds if (!is_aoe_netif(ifp)) 1181da177e4SLinus Torvalds goto exit; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds //skb->len += ETH_HLEN; /* (1) */ 1211da177e4SLinus Torvalds skb_push(skb, ETH_HLEN); /* (1) */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds h = (struct aoe_hdr *) skb->mac.raw; 124*63e9cc5dSecashin@coraid.com n = be32_to_cpu(h->tag); 1251da177e4SLinus Torvalds if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) 1261da177e4SLinus Torvalds goto exit; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds if (h->verfl & AOEFL_ERR) { 1291da177e4SLinus Torvalds n = h->err; 1301da177e4SLinus Torvalds if (n > NECODES) 1311da177e4SLinus Torvalds n = 0; 1321da177e4SLinus Torvalds if (net_ratelimit()) 1331da177e4SLinus Torvalds printk(KERN_ERR "aoe: aoenet_rcv: error packet from %d.%d; " 1341da177e4SLinus Torvalds "ecode=%d '%s'\n", 135*63e9cc5dSecashin@coraid.com be16_to_cpu(h->major), h->minor, 1361da177e4SLinus Torvalds h->err, aoe_errlist[n]); 1371da177e4SLinus Torvalds goto exit; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds switch (h->cmd) { 1411da177e4SLinus Torvalds case AOECMD_ATA: 1421da177e4SLinus Torvalds aoecmd_ata_rsp(skb); 1431da177e4SLinus Torvalds break; 1441da177e4SLinus Torvalds case AOECMD_CFG: 1451da177e4SLinus Torvalds aoecmd_cfg_rsp(skb); 1461da177e4SLinus Torvalds break; 1471da177e4SLinus Torvalds default: 1481da177e4SLinus Torvalds printk(KERN_INFO "aoe: aoenet_rcv: unknown cmd %d\n", h->cmd); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds exit: 1511da177e4SLinus Torvalds dev_kfree_skb(skb); 1521da177e4SLinus Torvalds return 0; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static struct packet_type aoe_pt = { 1561da177e4SLinus Torvalds .type = __constant_htons(ETH_P_AOE), 1571da177e4SLinus Torvalds .func = aoenet_rcv, 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds int __init 1611da177e4SLinus Torvalds aoenet_init(void) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds dev_add_pack(&aoe_pt); 1641da177e4SLinus Torvalds return 0; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds void 1681da177e4SLinus Torvalds aoenet_exit(void) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds dev_remove_pack(&aoe_pt); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 173