Ed L. Cashin | 2611464 | 2006-09-20 14:36:48 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * aoenet.c |
| 4 | * Ethernet portion of AoE driver |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hdreg.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/netdevice.h> |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 10 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "aoe.h" |
| 12 | |
| 13 | #define NECODES 5 |
| 14 | |
| 15 | static char *aoe_errlist[] = |
| 16 | { |
| 17 | "no such error", |
| 18 | "unrecognized command code", |
| 19 | "bad argument parameter", |
| 20 | "device unavailable", |
| 21 | "config string present", |
| 22 | "unsupported version" |
| 23 | }; |
| 24 | |
| 25 | enum { |
| 26 | IFLISTSZ = 1024, |
| 27 | }; |
| 28 | |
| 29 | static char aoe_iflist[IFLISTSZ]; |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 30 | module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600); |
| 31 | MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"\n"); |
| 32 | |
| 33 | #ifndef MODULE |
| 34 | static int __init aoe_iflist_setup(char *str) |
| 35 | { |
| 36 | strncpy(aoe_iflist, str, IFLISTSZ); |
| 37 | aoe_iflist[IFLISTSZ - 1] = '\0'; |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | __setup("aoe_iflist=", aoe_iflist_setup); |
| 42 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | int |
| 45 | is_aoe_netif(struct net_device *ifp) |
| 46 | { |
| 47 | register char *p, *q; |
| 48 | register int len; |
| 49 | |
| 50 | if (aoe_iflist[0] == '\0') |
| 51 | return 1; |
| 52 | |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 53 | p = aoe_iflist + strspn(aoe_iflist, WHITESPACE); |
| 54 | for (; *p; p = q + strspn(q, WHITESPACE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | q = p + strcspn(p, WHITESPACE); |
| 56 | if (q != p) |
| 57 | len = q - p; |
| 58 | else |
| 59 | len = strlen(p); /* last token in aoe_iflist */ |
| 60 | |
| 61 | if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len)) |
| 62 | return 1; |
| 63 | if (q == p) |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | set_aoe_iflist(const char __user *user_str, size_t size) |
| 72 | { |
| 73 | if (size >= IFLISTSZ) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | if (copy_from_user(aoe_iflist, user_str, size)) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 77 | printk(KERN_INFO "aoe: copy from user failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return -EFAULT; |
| 79 | } |
| 80 | aoe_iflist[size] = 0x00; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | u64 |
| 85 | mac_addr(char addr[6]) |
| 86 | { |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 87 | __be64 n = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | char *p = (char *) &n; |
| 89 | |
| 90 | memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */ |
| 91 | |
| 92 | return __be64_to_cpu(n); |
| 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | void |
| 96 | aoenet_xmit(struct sk_buff *sl) |
| 97 | { |
| 98 | struct sk_buff *skb; |
| 99 | |
| 100 | while ((skb = sl)) { |
| 101 | sl = sl->next; |
| 102 | skb->next = skb->prev = NULL; |
| 103 | dev_queue_xmit(skb); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * (1) len doesn't include the header by default. I want this. |
| 109 | */ |
| 110 | static int |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 111 | aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | struct aoe_hdr *h; |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 114 | u32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Ed L. Cashin | 5dc401e | 2006-02-07 11:26:39 -0500 | [diff] [blame] | 116 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 117 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
Herbert Xu | 364c6ba | 2006-06-09 16:10:40 -0700 | [diff] [blame] | 119 | if (skb_linearize(skb)) |
Ed L. Cashin | 5dc401e | 2006-02-07 11:26:39 -0500 | [diff] [blame] | 120 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (!is_aoe_netif(ifp)) |
| 122 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | skb_push(skb, ETH_HLEN); /* (1) */ |
| 124 | |
| 125 | h = (struct aoe_hdr *) skb->mac.raw; |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 126 | n = be32_to_cpu(h->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) |
| 128 | goto exit; |
| 129 | |
| 130 | if (h->verfl & AOEFL_ERR) { |
| 131 | n = h->err; |
| 132 | if (n > NECODES) |
| 133 | n = 0; |
| 134 | if (net_ratelimit()) |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 135 | printk(KERN_ERR "aoe: error packet from %d.%d; ecode=%d '%s'\n", |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 136 | be16_to_cpu(h->major), h->minor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | h->err, aoe_errlist[n]); |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | switch (h->cmd) { |
| 142 | case AOECMD_ATA: |
| 143 | aoecmd_ata_rsp(skb); |
| 144 | break; |
| 145 | case AOECMD_CFG: |
| 146 | aoecmd_cfg_rsp(skb); |
| 147 | break; |
| 148 | default: |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 149 | printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | exit: |
| 152 | dev_kfree_skb(skb); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static struct packet_type aoe_pt = { |
| 157 | .type = __constant_htons(ETH_P_AOE), |
| 158 | .func = aoenet_rcv, |
| 159 | }; |
| 160 | |
| 161 | int __init |
| 162 | aoenet_init(void) |
| 163 | { |
| 164 | dev_add_pack(&aoe_pt); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | aoenet_exit(void) |
| 170 | { |
| 171 | dev_remove_pack(&aoe_pt); |
| 172 | } |
| 173 | |