1 /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */ 2 /* 3 * aoenet.c 4 * Ethernet portion of AoE driver 5 */ 6 7 #include <linux/gfp.h> 8 #include <linux/hdreg.h> 9 #include <linux/blkdev.h> 10 #include <linux/netdevice.h> 11 #include <linux/moduleparam.h> 12 #include <net/net_namespace.h> 13 #include <asm/unaligned.h> 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]; 33 module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600); 34 MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=dev1[,dev2...]"); 35 36 static wait_queue_head_t txwq; 37 static struct ktstate kts; 38 39 #ifndef MODULE 40 static int __init aoe_iflist_setup(char *str) 41 { 42 strscpy(aoe_iflist, str, IFLISTSZ); 43 return 1; 44 } 45 46 __setup("aoe_iflist=", aoe_iflist_setup); 47 #endif 48 49 static spinlock_t txlock; 50 static struct sk_buff_head skbtxq; 51 52 /* enters with txlock held */ 53 static int 54 tx(int id) __must_hold(&txlock) 55 { 56 struct sk_buff *skb; 57 struct net_device *ifp; 58 59 while ((skb = skb_dequeue(&skbtxq))) { 60 spin_unlock_irq(&txlock); 61 ifp = skb->dev; 62 if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit()) 63 pr_warn("aoe: packet could not be sent on %s. %s\n", 64 ifp ? ifp->name : "netif", 65 "consider increasing tx_queue_len"); 66 spin_lock_irq(&txlock); 67 } 68 return 0; 69 } 70 71 int 72 is_aoe_netif(struct net_device *ifp) 73 { 74 register char *p, *q; 75 register int len; 76 77 if (aoe_iflist[0] == '\0') 78 return 1; 79 80 p = aoe_iflist + strspn(aoe_iflist, WHITESPACE); 81 for (; *p; p = q + strspn(q, WHITESPACE)) { 82 q = p + strcspn(p, WHITESPACE); 83 if (q != p) 84 len = q - p; 85 else 86 len = strlen(p); /* last token in aoe_iflist */ 87 88 if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len)) 89 return 1; 90 if (q == p) 91 break; 92 } 93 94 return 0; 95 } 96 97 int 98 set_aoe_iflist(const char __user *user_str, size_t size) 99 { 100 if (size >= IFLISTSZ) 101 return -EINVAL; 102 103 if (copy_from_user(aoe_iflist, user_str, size)) { 104 printk(KERN_INFO "aoe: copy from user failed\n"); 105 return -EFAULT; 106 } 107 aoe_iflist[size] = 0x00; 108 return 0; 109 } 110 111 void 112 aoenet_xmit(struct sk_buff_head *queue) 113 { 114 struct sk_buff *skb, *tmp; 115 ulong flags; 116 117 skb_queue_walk_safe(queue, skb, tmp) { 118 __skb_unlink(skb, queue); 119 spin_lock_irqsave(&txlock, flags); 120 skb_queue_tail(&skbtxq, skb); 121 spin_unlock_irqrestore(&txlock, flags); 122 wake_up(&txwq); 123 } 124 } 125 126 /* 127 * (1) len doesn't include the header by default. I want this. 128 */ 129 static int 130 aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev) 131 { 132 struct aoe_hdr *h; 133 struct aoe_atahdr *ah; 134 u32 n; 135 int sn; 136 137 if (dev_net(ifp) != &init_net) 138 goto exit; 139 140 skb = skb_share_check(skb, GFP_ATOMIC); 141 if (skb == NULL) 142 return 0; 143 if (!is_aoe_netif(ifp)) 144 goto exit; 145 skb_push(skb, ETH_HLEN); /* (1) */ 146 sn = sizeof(*h) + sizeof(*ah); 147 if (skb->len >= sn) { 148 sn -= skb_headlen(skb); 149 if (sn > 0 && !__pskb_pull_tail(skb, sn)) 150 goto exit; 151 } 152 h = (struct aoe_hdr *) skb->data; 153 n = get_unaligned_be32(&h->tag); 154 if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31)) 155 goto exit; 156 157 if (h->verfl & AOEFL_ERR) { 158 n = h->err; 159 if (n > NECODES) 160 n = 0; 161 if (net_ratelimit()) 162 printk(KERN_ERR 163 "%s%d.%d@%s; ecode=%d '%s'\n", 164 "aoe: error packet from ", 165 get_unaligned_be16(&h->major), 166 h->minor, skb->dev->name, 167 h->err, aoe_errlist[n]); 168 goto exit; 169 } 170 171 switch (h->cmd) { 172 case AOECMD_ATA: 173 /* ata_rsp may keep skb for later processing or give it back */ 174 skb = aoecmd_ata_rsp(skb); 175 break; 176 case AOECMD_CFG: 177 aoecmd_cfg_rsp(skb); 178 break; 179 default: 180 if (h->cmd >= AOECMD_VEND_MIN) 181 break; /* don't complain about vendor commands */ 182 pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd); 183 break; 184 } 185 186 if (!skb) 187 return 0; 188 exit: 189 dev_kfree_skb(skb); 190 return 0; 191 } 192 193 static struct packet_type aoe_pt __read_mostly = { 194 .type = __constant_htons(ETH_P_AOE), 195 .func = aoenet_rcv, 196 }; 197 198 int __init 199 aoenet_init(void) 200 { 201 skb_queue_head_init(&skbtxq); 202 init_waitqueue_head(&txwq); 203 spin_lock_init(&txlock); 204 kts.lock = &txlock; 205 kts.fn = tx; 206 kts.waitq = &txwq; 207 kts.id = 0; 208 snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id); 209 if (aoe_ktstart(&kts)) 210 return -EAGAIN; 211 dev_add_pack(&aoe_pt); 212 return 0; 213 } 214 215 void 216 aoenet_exit(void) 217 { 218 aoe_ktstop(&kts); 219 skb_queue_purge(&skbtxq); 220 dev_remove_pack(&aoe_pt); 221 } 222 223