1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright Gavin Shan, IBM Corporation 2016. 4 */ 5 6 #include <linux/module.h> 7 #include <linux/kernel.h> 8 #include <linux/init.h> 9 #include <linux/netdevice.h> 10 #include <linux/skbuff.h> 11 12 #include <net/ncsi.h> 13 #include <net/net_namespace.h> 14 #include <net/sock.h> 15 16 #include "internal.h" 17 #include "ncsi-pkt.h" 18 19 static int ncsi_validate_aen_pkt(struct ncsi_aen_pkt_hdr *h, 20 const unsigned short payload) 21 { 22 u32 checksum; 23 __be32 *pchecksum; 24 25 if (h->common.revision != NCSI_PKT_REVISION) 26 return -EINVAL; 27 if (ntohs(h->common.length) != payload) 28 return -EINVAL; 29 30 /* Validate checksum, which might be zeroes if the 31 * sender doesn't support checksum according to NCSI 32 * specification. 33 */ 34 pchecksum = (__be32 *)((void *)(h + 1) + payload - 4); 35 if (ntohl(*pchecksum) == 0) 36 return 0; 37 38 checksum = ncsi_calculate_checksum((unsigned char *)h, 39 sizeof(*h) + payload - 4); 40 if (*pchecksum != htonl(checksum)) 41 return -EINVAL; 42 43 return 0; 44 } 45 46 static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp, 47 struct ncsi_aen_pkt_hdr *h) 48 { 49 struct ncsi_channel *nc, *tmp; 50 struct ncsi_channel_mode *ncm; 51 unsigned long old_data, data; 52 struct ncsi_aen_lsc_pkt *lsc; 53 struct ncsi_package *np; 54 bool had_link, has_link; 55 unsigned long flags; 56 bool chained; 57 int state; 58 59 /* Find the NCSI channel */ 60 ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc); 61 if (!nc) 62 return -ENODEV; 63 64 /* Update the link status */ 65 lsc = (struct ncsi_aen_lsc_pkt *)h; 66 67 spin_lock_irqsave(&nc->lock, flags); 68 ncm = &nc->modes[NCSI_MODE_LINK]; 69 old_data = ncm->data[2]; 70 data = ntohl(lsc->status); 71 ncm->data[2] = data; 72 ncm->data[4] = ntohl(lsc->oem_status); 73 74 had_link = !!(old_data & 0x1); 75 has_link = !!(data & 0x1); 76 77 netdev_dbg(ndp->ndev.dev, "NCSI: LSC AEN - channel %u state %s\n", 78 nc->id, data & 0x1 ? "up" : "down"); 79 80 chained = !list_empty(&nc->link); 81 state = nc->state; 82 spin_unlock_irqrestore(&nc->lock, flags); 83 84 if (state == NCSI_CHANNEL_INACTIVE) 85 netdev_warn(ndp->ndev.dev, 86 "NCSI: Inactive channel %u received AEN!\n", 87 nc->id); 88 89 if ((had_link == has_link) || chained) 90 return 0; 91 92 if (had_link) 93 netif_carrier_off(ndp->ndev.dev); 94 else 95 netif_carrier_on(ndp->ndev.dev); 96 97 if (!ndp->multi_package && !nc->package->multi_channel) { 98 if (had_link) { 99 ndp->flags |= NCSI_DEV_RESHUFFLE; 100 ncsi_stop_channel_monitor(nc); 101 spin_lock_irqsave(&ndp->lock, flags); 102 list_add_tail_rcu(&nc->link, &ndp->channel_queue); 103 spin_unlock_irqrestore(&ndp->lock, flags); 104 return ncsi_process_next_channel(ndp); 105 } 106 /* Configured channel came up */ 107 return 0; 108 } 109 110 if (had_link) { 111 ncm = &nc->modes[NCSI_MODE_TX_ENABLE]; 112 if (ncsi_channel_is_last(ndp, nc)) { 113 /* No channels left, reconfigure */ 114 return ncsi_reset_dev(&ndp->ndev); 115 } else if (ncm->enable) { 116 /* Need to failover Tx channel */ 117 ncsi_update_tx_channel(ndp, nc->package, nc, NULL); 118 } 119 } else if (has_link && nc->package->preferred_channel == nc) { 120 /* Return Tx to preferred channel */ 121 ncsi_update_tx_channel(ndp, nc->package, NULL, nc); 122 } else if (has_link) { 123 NCSI_FOR_EACH_PACKAGE(ndp, np) { 124 NCSI_FOR_EACH_CHANNEL(np, tmp) { 125 /* Enable Tx on this channel if the current Tx 126 * channel is down. 127 */ 128 ncm = &tmp->modes[NCSI_MODE_TX_ENABLE]; 129 if (ncm->enable && 130 !ncsi_channel_has_link(tmp)) { 131 ncsi_update_tx_channel(ndp, nc->package, 132 tmp, nc); 133 break; 134 } 135 } 136 } 137 } 138 139 /* Leave configured channels active in a multi-channel scenario so 140 * AEN events are still received. 141 */ 142 return 0; 143 } 144 145 static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp, 146 struct ncsi_aen_pkt_hdr *h) 147 { 148 struct ncsi_channel *nc; 149 unsigned long flags; 150 151 /* Find the NCSI channel */ 152 ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc); 153 if (!nc) 154 return -ENODEV; 155 156 spin_lock_irqsave(&nc->lock, flags); 157 if (!list_empty(&nc->link) || 158 nc->state != NCSI_CHANNEL_ACTIVE) { 159 spin_unlock_irqrestore(&nc->lock, flags); 160 return 0; 161 } 162 spin_unlock_irqrestore(&nc->lock, flags); 163 164 ncsi_stop_channel_monitor(nc); 165 spin_lock_irqsave(&nc->lock, flags); 166 nc->state = NCSI_CHANNEL_INVISIBLE; 167 spin_unlock_irqrestore(&nc->lock, flags); 168 169 spin_lock_irqsave(&ndp->lock, flags); 170 nc->state = NCSI_CHANNEL_INACTIVE; 171 list_add_tail_rcu(&nc->link, &ndp->channel_queue); 172 spin_unlock_irqrestore(&ndp->lock, flags); 173 nc->modes[NCSI_MODE_TX_ENABLE].enable = 0; 174 175 return ncsi_process_next_channel(ndp); 176 } 177 178 static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp, 179 struct ncsi_aen_pkt_hdr *h) 180 { 181 struct ncsi_channel *nc; 182 struct ncsi_channel_mode *ncm; 183 struct ncsi_aen_hncdsc_pkt *hncdsc; 184 unsigned long flags; 185 186 /* Find the NCSI channel */ 187 ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc); 188 if (!nc) 189 return -ENODEV; 190 191 spin_lock_irqsave(&nc->lock, flags); 192 ncm = &nc->modes[NCSI_MODE_LINK]; 193 hncdsc = (struct ncsi_aen_hncdsc_pkt *)h; 194 ncm->data[3] = ntohl(hncdsc->status); 195 spin_unlock_irqrestore(&nc->lock, flags); 196 netdev_dbg(ndp->ndev.dev, 197 "NCSI: host driver %srunning on channel %u\n", 198 ncm->data[3] & 0x1 ? "" : "not ", nc->id); 199 200 return 0; 201 } 202 203 static struct ncsi_aen_handler { 204 unsigned char type; 205 int payload; 206 int (*handler)(struct ncsi_dev_priv *ndp, 207 struct ncsi_aen_pkt_hdr *h); 208 } ncsi_aen_handlers[] = { 209 { NCSI_PKT_AEN_LSC, 12, ncsi_aen_handler_lsc }, 210 { NCSI_PKT_AEN_CR, 4, ncsi_aen_handler_cr }, 211 { NCSI_PKT_AEN_HNCDSC, 8, ncsi_aen_handler_hncdsc } 212 }; 213 214 int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb) 215 { 216 struct ncsi_aen_pkt_hdr *h; 217 struct ncsi_aen_handler *nah = NULL; 218 int i, ret; 219 220 /* Find the handler */ 221 h = (struct ncsi_aen_pkt_hdr *)skb_network_header(skb); 222 for (i = 0; i < ARRAY_SIZE(ncsi_aen_handlers); i++) { 223 if (ncsi_aen_handlers[i].type == h->type) { 224 nah = &ncsi_aen_handlers[i]; 225 break; 226 } 227 } 228 229 if (!nah) { 230 netdev_warn(ndp->ndev.dev, "Invalid AEN (0x%x) received\n", 231 h->type); 232 return -ENOENT; 233 } 234 235 ret = ncsi_validate_aen_pkt(h, nah->payload); 236 if (ret) { 237 netdev_warn(ndp->ndev.dev, 238 "NCSI: 'bad' packet ignored for AEN type 0x%x\n", 239 h->type); 240 goto out; 241 } 242 243 ret = nah->handler(ndp, h); 244 if (ret) 245 netdev_err(ndp->ndev.dev, 246 "NCSI: Handler for AEN type 0x%x returned %d\n", 247 h->type, ret); 248 out: 249 consume_skb(skb); 250 return ret; 251 } 252