1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */ 3 4 #include <linux/bitfield.h> 5 #include <linux/netdevice.h> 6 #include <linux/skbuff.h> 7 #include <linux/workqueue.h> 8 #include <net/dst_metadata.h> 9 10 #include "main.h" 11 #include "../nfp_net.h" 12 #include "../nfp_net_repr.h" 13 #include "./cmsg.h" 14 15 static struct nfp_flower_cmsg_hdr * 16 nfp_flower_cmsg_get_hdr(struct sk_buff *skb) 17 { 18 return (struct nfp_flower_cmsg_hdr *)skb->data; 19 } 20 21 struct sk_buff * 22 nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size, 23 enum nfp_flower_cmsg_type_port type, gfp_t flag) 24 { 25 struct nfp_flower_cmsg_hdr *ch; 26 struct sk_buff *skb; 27 28 size += NFP_FLOWER_CMSG_HLEN; 29 30 skb = nfp_app_ctrl_msg_alloc(app, size, flag); 31 if (!skb) 32 return NULL; 33 34 ch = nfp_flower_cmsg_get_hdr(skb); 35 ch->pad = 0; 36 ch->version = NFP_FLOWER_CMSG_VER1; 37 ch->type = type; 38 skb_put(skb, size); 39 40 return skb; 41 } 42 43 struct sk_buff * 44 nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports) 45 { 46 struct nfp_flower_cmsg_mac_repr *msg; 47 struct sk_buff *skb; 48 49 skb = nfp_flower_cmsg_alloc(app, struct_size(msg, ports, num_ports), 50 NFP_FLOWER_CMSG_TYPE_MAC_REPR, GFP_KERNEL); 51 if (!skb) 52 return NULL; 53 54 msg = nfp_flower_cmsg_get_data(skb); 55 memset(msg->reserved, 0, sizeof(msg->reserved)); 56 msg->num_ports = num_ports; 57 58 return skb; 59 } 60 61 void 62 nfp_flower_cmsg_mac_repr_add(struct sk_buff *skb, unsigned int idx, 63 unsigned int nbi, unsigned int nbi_port, 64 unsigned int phys_port) 65 { 66 struct nfp_flower_cmsg_mac_repr *msg; 67 68 msg = nfp_flower_cmsg_get_data(skb); 69 msg->ports[idx].idx = idx; 70 msg->ports[idx].info = nbi & NFP_FLOWER_CMSG_MAC_REPR_NBI; 71 msg->ports[idx].nbi_port = nbi_port; 72 msg->ports[idx].phys_port = phys_port; 73 } 74 75 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok, 76 unsigned int mtu, bool mtu_only) 77 { 78 struct nfp_flower_cmsg_portmod *msg; 79 struct sk_buff *skb; 80 81 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg), 82 NFP_FLOWER_CMSG_TYPE_PORT_MOD, GFP_KERNEL); 83 if (!skb) 84 return -ENOMEM; 85 86 msg = nfp_flower_cmsg_get_data(skb); 87 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id); 88 msg->reserved = 0; 89 msg->info = carrier_ok; 90 91 if (mtu_only) 92 msg->info |= NFP_FLOWER_CMSG_PORTMOD_MTU_CHANGE_ONLY; 93 94 msg->mtu = cpu_to_be16(mtu); 95 96 nfp_ctrl_tx(repr->app->ctrl, skb); 97 98 return 0; 99 } 100 101 int nfp_flower_cmsg_portreify(struct nfp_repr *repr, bool exists) 102 { 103 struct nfp_flower_cmsg_portreify *msg; 104 struct sk_buff *skb; 105 106 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg), 107 NFP_FLOWER_CMSG_TYPE_PORT_REIFY, 108 GFP_KERNEL); 109 if (!skb) 110 return -ENOMEM; 111 112 msg = nfp_flower_cmsg_get_data(skb); 113 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id); 114 msg->reserved = 0; 115 msg->info = cpu_to_be16(exists); 116 117 nfp_ctrl_tx(repr->app->ctrl, skb); 118 119 return 0; 120 } 121 122 static bool 123 nfp_flower_process_mtu_ack(struct nfp_app *app, struct sk_buff *skb) 124 { 125 struct nfp_flower_priv *app_priv = app->priv; 126 struct nfp_flower_cmsg_portmod *msg; 127 128 msg = nfp_flower_cmsg_get_data(skb); 129 130 if (!(msg->info & NFP_FLOWER_CMSG_PORTMOD_MTU_CHANGE_ONLY)) 131 return false; 132 133 spin_lock_bh(&app_priv->mtu_conf.lock); 134 if (!app_priv->mtu_conf.requested_val || 135 app_priv->mtu_conf.portnum != be32_to_cpu(msg->portnum) || 136 be16_to_cpu(msg->mtu) != app_priv->mtu_conf.requested_val) { 137 /* Not an ack for requested MTU change. */ 138 spin_unlock_bh(&app_priv->mtu_conf.lock); 139 return false; 140 } 141 142 app_priv->mtu_conf.ack = true; 143 app_priv->mtu_conf.requested_val = 0; 144 wake_up(&app_priv->mtu_conf.wait_q); 145 spin_unlock_bh(&app_priv->mtu_conf.lock); 146 147 return true; 148 } 149 150 static void 151 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb) 152 { 153 struct nfp_flower_cmsg_portmod *msg; 154 struct net_device *netdev; 155 bool link; 156 157 msg = nfp_flower_cmsg_get_data(skb); 158 link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK; 159 160 rtnl_lock(); 161 rcu_read_lock(); 162 netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum)); 163 rcu_read_unlock(); 164 if (!netdev) { 165 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n", 166 be32_to_cpu(msg->portnum)); 167 rtnl_unlock(); 168 return; 169 } 170 171 if (link) { 172 u16 mtu = be16_to_cpu(msg->mtu); 173 174 netif_carrier_on(netdev); 175 176 /* An MTU of 0 from the firmware should be ignored */ 177 if (mtu) 178 dev_set_mtu(netdev, mtu); 179 } else { 180 netif_carrier_off(netdev); 181 } 182 rtnl_unlock(); 183 } 184 185 static void 186 nfp_flower_cmsg_portreify_rx(struct nfp_app *app, struct sk_buff *skb) 187 { 188 struct nfp_flower_priv *priv = app->priv; 189 struct nfp_flower_cmsg_portreify *msg; 190 bool exists; 191 192 msg = nfp_flower_cmsg_get_data(skb); 193 194 rcu_read_lock(); 195 exists = !!nfp_app_repr_get(app, be32_to_cpu(msg->portnum)); 196 rcu_read_unlock(); 197 if (!exists) { 198 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n", 199 be32_to_cpu(msg->portnum)); 200 return; 201 } 202 203 atomic_inc(&priv->reify_replies); 204 wake_up(&priv->reify_wait_queue); 205 } 206 207 static void 208 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb) 209 { 210 struct nfp_flower_priv *app_priv = app->priv; 211 struct nfp_flower_cmsg_hdr *cmsg_hdr; 212 enum nfp_flower_cmsg_type_port type; 213 bool skb_stored = false; 214 215 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb); 216 217 type = cmsg_hdr->type; 218 switch (type) { 219 case NFP_FLOWER_CMSG_TYPE_PORT_REIFY: 220 nfp_flower_cmsg_portreify_rx(app, skb); 221 break; 222 case NFP_FLOWER_CMSG_TYPE_PORT_MOD: 223 nfp_flower_cmsg_portmod_rx(app, skb); 224 break; 225 case NFP_FLOWER_CMSG_TYPE_NO_NEIGH: 226 nfp_tunnel_request_route(app, skb); 227 break; 228 case NFP_FLOWER_CMSG_TYPE_ACTIVE_TUNS: 229 nfp_tunnel_keep_alive(app, skb); 230 break; 231 case NFP_FLOWER_CMSG_TYPE_LAG_CONFIG: 232 if (app_priv->flower_ext_feats & NFP_FL_FEATS_LAG) { 233 skb_stored = nfp_flower_lag_unprocessed_msg(app, skb); 234 break; 235 } 236 /* fall through */ 237 default: 238 nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n", 239 type); 240 goto out; 241 } 242 243 if (!skb_stored) 244 dev_consume_skb_any(skb); 245 return; 246 out: 247 dev_kfree_skb_any(skb); 248 } 249 250 void nfp_flower_cmsg_process_rx(struct work_struct *work) 251 { 252 struct sk_buff_head cmsg_joined; 253 struct nfp_flower_priv *priv; 254 struct sk_buff *skb; 255 256 priv = container_of(work, struct nfp_flower_priv, cmsg_work); 257 skb_queue_head_init(&cmsg_joined); 258 259 spin_lock_bh(&priv->cmsg_skbs_high.lock); 260 skb_queue_splice_tail_init(&priv->cmsg_skbs_high, &cmsg_joined); 261 spin_unlock_bh(&priv->cmsg_skbs_high.lock); 262 263 spin_lock_bh(&priv->cmsg_skbs_low.lock); 264 skb_queue_splice_tail_init(&priv->cmsg_skbs_low, &cmsg_joined); 265 spin_unlock_bh(&priv->cmsg_skbs_low.lock); 266 267 while ((skb = __skb_dequeue(&cmsg_joined))) 268 nfp_flower_cmsg_process_one_rx(priv->app, skb); 269 } 270 271 static void 272 nfp_flower_queue_ctl_msg(struct nfp_app *app, struct sk_buff *skb, int type) 273 { 274 struct nfp_flower_priv *priv = app->priv; 275 struct sk_buff_head *skb_head; 276 277 if (type == NFP_FLOWER_CMSG_TYPE_PORT_REIFY || 278 type == NFP_FLOWER_CMSG_TYPE_PORT_MOD) 279 skb_head = &priv->cmsg_skbs_high; 280 else 281 skb_head = &priv->cmsg_skbs_low; 282 283 if (skb_queue_len(skb_head) >= NFP_FLOWER_WORKQ_MAX_SKBS) { 284 nfp_flower_cmsg_warn(app, "Dropping queued control messages\n"); 285 dev_kfree_skb_any(skb); 286 return; 287 } 288 289 skb_queue_tail(skb_head, skb); 290 schedule_work(&priv->cmsg_work); 291 } 292 293 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb) 294 { 295 struct nfp_flower_cmsg_hdr *cmsg_hdr; 296 297 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb); 298 299 if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) { 300 nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n", 301 cmsg_hdr->version); 302 dev_kfree_skb_any(skb); 303 return; 304 } 305 306 if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_FLOW_STATS) { 307 /* We need to deal with stats updates from HW asap */ 308 nfp_flower_rx_flow_stats(app, skb); 309 dev_consume_skb_any(skb); 310 } else if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_PORT_MOD && 311 nfp_flower_process_mtu_ack(app, skb)) { 312 /* Handle MTU acks outside wq to prevent RTNL conflict. */ 313 dev_consume_skb_any(skb); 314 } else if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH) { 315 /* Acks from the NFP that the route is added - ignore. */ 316 dev_consume_skb_any(skb); 317 } else { 318 nfp_flower_queue_ctl_msg(app, skb, cmsg_hdr->type); 319 } 320 } 321