Ed Cashin | ca47bbd | 2013-07-03 15:09:06 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2013 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/hdreg.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/netdevice.h> |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 11 | #include <linux/moduleparam.h> |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 12 | #include <net/net_namespace.h> |
David S. Miller | 43ecf52 | 2007-03-01 18:30:08 -0800 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "aoe.h" |
| 15 | |
| 16 | #define NECODES 5 |
| 17 | |
| 18 | static char *aoe_errlist[] = |
| 19 | { |
| 20 | "no such error", |
| 21 | "unrecognized command code", |
| 22 | "bad argument parameter", |
| 23 | "device unavailable", |
| 24 | "config string present", |
| 25 | "unsupported version" |
| 26 | }; |
| 27 | |
| 28 | enum { |
| 29 | IFLISTSZ = 1024, |
| 30 | }; |
| 31 | |
| 32 | static char aoe_iflist[IFLISTSZ]; |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 33 | module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600); |
Ed Cashin | 4a6c9ee | 2012-12-17 16:04:15 -0800 | [diff] [blame] | 34 | MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=dev1[,dev2...]"); |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 35 | |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 36 | static wait_queue_head_t txwq; |
| 37 | static struct ktstate kts; |
| 38 | |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 39 | #ifndef MODULE |
| 40 | static int __init aoe_iflist_setup(char *str) |
| 41 | { |
| 42 | strncpy(aoe_iflist, str, IFLISTSZ); |
| 43 | aoe_iflist[IFLISTSZ - 1] = '\0'; |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | __setup("aoe_iflist=", aoe_iflist_setup); |
| 48 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 50 | static spinlock_t txlock; |
| 51 | static struct sk_buff_head skbtxq; |
| 52 | |
| 53 | /* enters with txlock held */ |
| 54 | static int |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 55 | tx(int id) __must_hold(&txlock) |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 56 | { |
| 57 | struct sk_buff *skb; |
Ed Cashin | 4e78dd1 | 2012-12-17 16:03:28 -0800 | [diff] [blame] | 58 | struct net_device *ifp; |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 59 | |
| 60 | while ((skb = skb_dequeue(&skbtxq))) { |
| 61 | spin_unlock_irq(&txlock); |
Ed Cashin | 4e78dd1 | 2012-12-17 16:03:28 -0800 | [diff] [blame] | 62 | ifp = skb->dev; |
| 63 | if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit()) |
| 64 | pr_warn("aoe: packet could not be sent on %s. %s\n", |
| 65 | ifp ? ifp->name : "netif", |
| 66 | "consider increasing tx_queue_len"); |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 67 | spin_lock_irq(&txlock); |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | int |
| 73 | is_aoe_netif(struct net_device *ifp) |
| 74 | { |
| 75 | register char *p, *q; |
| 76 | register int len; |
| 77 | |
| 78 | if (aoe_iflist[0] == '\0') |
| 79 | return 1; |
| 80 | |
Ed L Cashin | 03c41c4 | 2005-04-29 10:24:03 -0400 | [diff] [blame] | 81 | p = aoe_iflist + strspn(aoe_iflist, WHITESPACE); |
| 82 | for (; *p; p = q + strspn(q, WHITESPACE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | q = p + strcspn(p, WHITESPACE); |
| 84 | if (q != p) |
| 85 | len = q - p; |
| 86 | else |
| 87 | len = strlen(p); /* last token in aoe_iflist */ |
| 88 | |
| 89 | if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len)) |
| 90 | return 1; |
| 91 | if (q == p) |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | set_aoe_iflist(const char __user *user_str, size_t size) |
| 100 | { |
| 101 | if (size >= IFLISTSZ) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | if (copy_from_user(aoe_iflist, user_str, size)) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 105 | printk(KERN_INFO "aoe: copy from user failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EFAULT; |
| 107 | } |
| 108 | aoe_iflist[size] = 0x00; |
| 109 | return 0; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | void |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 113 | aoenet_xmit(struct sk_buff_head *queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 115 | struct sk_buff *skb, *tmp; |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 116 | ulong flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
David S. Miller | d877984 | 2008-09-23 20:47:22 -0700 | [diff] [blame] | 118 | skb_queue_walk_safe(queue, skb, tmp) { |
| 119 | __skb_unlink(skb, queue); |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 120 | spin_lock_irqsave(&txlock, flags); |
| 121 | skb_queue_tail(&skbtxq, skb); |
| 122 | spin_unlock_irqrestore(&txlock, flags); |
| 123 | wake_up(&txwq); |
David S. Miller | d877984 | 2008-09-23 20:47:22 -0700 | [diff] [blame] | 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Ed Cashin | a04b41c | 2012-12-17 16:03:39 -0800 | [diff] [blame] | 127 | /* |
| 128 | * (1) len doesn't include the header by default. I want this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | */ |
| 130 | static int |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | struct aoe_hdr *h; |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 134 | struct aoe_atahdr *ah; |
ecashin@coraid.com | 63e9cc5 | 2005-04-18 22:00:20 -0700 | [diff] [blame] | 135 | u32 n; |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 136 | int sn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 138 | if (dev_net(ifp) != &init_net) |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 139 | goto exit; |
| 140 | |
Ed L. Cashin | 5dc401e | 2006-02-07 11:26:39 -0500 | [diff] [blame] | 141 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 142 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (!is_aoe_netif(ifp)) |
| 145 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | skb_push(skb, ETH_HLEN); /* (1) */ |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 147 | sn = sizeof(*h) + sizeof(*ah); |
| 148 | if (skb->len >= sn) { |
| 149 | sn -= skb_headlen(skb); |
| 150 | if (sn > 0 && !__pskb_pull_tail(skb, sn)) |
| 151 | goto exit; |
| 152 | } |
| 153 | h = (struct aoe_hdr *) skb->data; |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 154 | n = get_unaligned_be32(&h->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) |
| 156 | goto exit; |
| 157 | |
| 158 | if (h->verfl & AOEFL_ERR) { |
| 159 | n = h->err; |
| 160 | if (n > NECODES) |
| 161 | n = 0; |
| 162 | if (net_ratelimit()) |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 163 | printk(KERN_ERR |
| 164 | "%s%d.%d@%s; ecode=%d '%s'\n", |
| 165 | "aoe: error packet from ", |
Harvey Harrison | f885f8d | 2008-04-29 01:03:30 -0700 | [diff] [blame] | 166 | get_unaligned_be16(&h->major), |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 167 | h->minor, skb->dev->name, |
| 168 | h->err, aoe_errlist[n]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | goto exit; |
| 170 | } |
| 171 | |
| 172 | switch (h->cmd) { |
| 173 | case AOECMD_ATA: |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 174 | /* ata_rsp may keep skb for later processing or give it back */ |
| 175 | skb = aoecmd_ata_rsp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | break; |
| 177 | case AOECMD_CFG: |
| 178 | aoecmd_cfg_rsp(skb); |
| 179 | break; |
| 180 | default: |
Ed Cashin | b6d6c51 | 2009-02-18 14:48:13 -0800 | [diff] [blame] | 181 | if (h->cmd >= AOECMD_VEND_MIN) |
| 182 | break; /* don't complain about vendor commands */ |
Ed Cashin | b21faa2 | 2012-10-04 17:16:35 -0700 | [diff] [blame] | 183 | pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd); |
| 184 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Ed Cashin | 896831f | 2012-10-04 17:16:21 -0700 | [diff] [blame] | 186 | |
| 187 | if (!skb) |
| 188 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | exit: |
| 190 | dev_kfree_skb(skb); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 194 | static struct packet_type aoe_pt __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | .type = __constant_htons(ETH_P_AOE), |
| 196 | .func = aoenet_rcv, |
| 197 | }; |
| 198 | |
| 199 | int __init |
| 200 | aoenet_init(void) |
| 201 | { |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 202 | skb_queue_head_init(&skbtxq); |
| 203 | init_waitqueue_head(&txwq); |
| 204 | spin_lock_init(&txlock); |
| 205 | kts.lock = &txlock; |
| 206 | kts.fn = tx; |
| 207 | kts.waitq = &txwq; |
Ed Cashin | 8030d34 | 2013-07-03 15:09:05 -0700 | [diff] [blame] | 208 | kts.id = 0; |
| 209 | snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id); |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 210 | if (aoe_ktstart(&kts)) |
| 211 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | dev_add_pack(&aoe_pt); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | aoenet_exit(void) |
| 218 | { |
Ed Cashin | eb086ec | 2012-10-04 17:16:25 -0700 | [diff] [blame] | 219 | aoe_ktstop(&kts); |
| 220 | skb_queue_purge(&skbtxq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | dev_remove_pack(&aoe_pt); |
| 222 | } |
| 223 | |