xref: /linux/net/devlink/port.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5  */
6 
7 #include "devl_internal.h"
8 
9 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
10 	(_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
11 
12 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
13 	[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
14 	[DEVLINK_PORT_FN_ATTR_STATE] =
15 		NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
16 				 DEVLINK_PORT_FN_STATE_ACTIVE),
17 	[DEVLINK_PORT_FN_ATTR_CAPS] =
18 		NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
19 };
20 
21 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port)				\
22 	WARN_ON_ONCE(!(devlink_port)->registered)
23 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port)			\
24 	WARN_ON_ONCE((devlink_port)->registered)
25 
26 struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
27 					       unsigned int port_index)
28 {
29 	return xa_load(&devlink->ports, port_index);
30 }
31 
32 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
33 						 struct nlattr **attrs)
34 {
35 	if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
36 		u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
37 		struct devlink_port *devlink_port;
38 
39 		devlink_port = devlink_port_get_by_index(devlink, port_index);
40 		if (!devlink_port)
41 			return ERR_PTR(-ENODEV);
42 		return devlink_port;
43 	}
44 	return ERR_PTR(-EINVAL);
45 }
46 
47 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
48 						struct genl_info *info)
49 {
50 	return devlink_port_get_from_attrs(devlink, info->attrs);
51 }
52 
53 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
54 				     u32 cap, bool is_enable)
55 {
56 	caps->selector |= cap;
57 	if (is_enable)
58 		caps->value |= cap;
59 }
60 
61 static int devlink_port_fn_roce_fill(struct devlink_port *devlink_port,
62 				     struct nla_bitfield32 *caps,
63 				     struct netlink_ext_ack *extack)
64 {
65 	bool is_enable;
66 	int err;
67 
68 	if (!devlink_port->ops->port_fn_roce_get)
69 		return 0;
70 
71 	err = devlink_port->ops->port_fn_roce_get(devlink_port, &is_enable,
72 						  extack);
73 	if (err) {
74 		if (err == -EOPNOTSUPP)
75 			return 0;
76 		return err;
77 	}
78 
79 	devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
80 	return 0;
81 }
82 
83 static int devlink_port_fn_migratable_fill(struct devlink_port *devlink_port,
84 					   struct nla_bitfield32 *caps,
85 					   struct netlink_ext_ack *extack)
86 {
87 	bool is_enable;
88 	int err;
89 
90 	if (!devlink_port->ops->port_fn_migratable_get ||
91 	    devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
92 		return 0;
93 
94 	err = devlink_port->ops->port_fn_migratable_get(devlink_port,
95 							&is_enable, extack);
96 	if (err) {
97 		if (err == -EOPNOTSUPP)
98 			return 0;
99 		return err;
100 	}
101 
102 	devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
103 	return 0;
104 }
105 
106 static int devlink_port_fn_ipsec_crypto_fill(struct devlink_port *devlink_port,
107 					     struct nla_bitfield32 *caps,
108 					     struct netlink_ext_ack *extack)
109 {
110 	bool is_enable;
111 	int err;
112 
113 	if (!devlink_port->ops->port_fn_ipsec_crypto_get ||
114 	    devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
115 		return 0;
116 
117 	err = devlink_port->ops->port_fn_ipsec_crypto_get(devlink_port, &is_enable, extack);
118 	if (err) {
119 		if (err == -EOPNOTSUPP)
120 			return 0;
121 		return err;
122 	}
123 
124 	devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO, is_enable);
125 	return 0;
126 }
127 
128 static int devlink_port_fn_ipsec_packet_fill(struct devlink_port *devlink_port,
129 					     struct nla_bitfield32 *caps,
130 					     struct netlink_ext_ack *extack)
131 {
132 	bool is_enable;
133 	int err;
134 
135 	if (!devlink_port->ops->port_fn_ipsec_packet_get ||
136 	    devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
137 		return 0;
138 
139 	err = devlink_port->ops->port_fn_ipsec_packet_get(devlink_port, &is_enable, extack);
140 	if (err) {
141 		if (err == -EOPNOTSUPP)
142 			return 0;
143 		return err;
144 	}
145 
146 	devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_PACKET, is_enable);
147 	return 0;
148 }
149 
150 static int devlink_port_fn_caps_fill(struct devlink_port *devlink_port,
151 				     struct sk_buff *msg,
152 				     struct netlink_ext_ack *extack,
153 				     bool *msg_updated)
154 {
155 	struct nla_bitfield32 caps = {};
156 	int err;
157 
158 	err = devlink_port_fn_roce_fill(devlink_port, &caps, extack);
159 	if (err)
160 		return err;
161 
162 	err = devlink_port_fn_migratable_fill(devlink_port, &caps, extack);
163 	if (err)
164 		return err;
165 
166 	err = devlink_port_fn_ipsec_crypto_fill(devlink_port, &caps, extack);
167 	if (err)
168 		return err;
169 
170 	err = devlink_port_fn_ipsec_packet_fill(devlink_port, &caps, extack);
171 	if (err)
172 		return err;
173 
174 	if (!caps.selector)
175 		return 0;
176 	err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
177 				 caps.selector);
178 	if (err)
179 		return err;
180 
181 	*msg_updated = true;
182 	return 0;
183 }
184 
185 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
186 {
187 	if (devlink_nl_put_handle(msg, devlink_port->devlink))
188 		return -EMSGSIZE;
189 	if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
190 		return -EMSGSIZE;
191 	return 0;
192 }
193 
194 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
195 {
196 	struct devlink *devlink = devlink_port->devlink;
197 
198 	return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
199 	     + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
200 	     + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
201 }
202 
203 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
204 				     struct devlink_port *devlink_port)
205 {
206 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
207 
208 	if (!devlink_port->attrs_set)
209 		return 0;
210 	if (attrs->lanes) {
211 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
212 			return -EMSGSIZE;
213 	}
214 	if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
215 		return -EMSGSIZE;
216 	if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
217 		return -EMSGSIZE;
218 	switch (devlink_port->attrs.flavour) {
219 	case DEVLINK_PORT_FLAVOUR_PCI_PF:
220 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
221 				attrs->pci_pf.controller) ||
222 		    nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
223 			return -EMSGSIZE;
224 		if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
225 			return -EMSGSIZE;
226 		break;
227 	case DEVLINK_PORT_FLAVOUR_PCI_VF:
228 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
229 				attrs->pci_vf.controller) ||
230 		    nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
231 		    nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
232 			return -EMSGSIZE;
233 		if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
234 			return -EMSGSIZE;
235 		break;
236 	case DEVLINK_PORT_FLAVOUR_PCI_SF:
237 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
238 				attrs->pci_sf.controller) ||
239 		    nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
240 				attrs->pci_sf.pf) ||
241 		    nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
242 				attrs->pci_sf.sf))
243 			return -EMSGSIZE;
244 		break;
245 	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
246 	case DEVLINK_PORT_FLAVOUR_CPU:
247 	case DEVLINK_PORT_FLAVOUR_DSA:
248 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
249 				attrs->phys.port_number))
250 			return -EMSGSIZE;
251 		if (!attrs->split)
252 			return 0;
253 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
254 				attrs->phys.port_number))
255 			return -EMSGSIZE;
256 		if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
257 				attrs->phys.split_subport_number))
258 			return -EMSGSIZE;
259 		break;
260 	default:
261 		break;
262 	}
263 	return 0;
264 }
265 
266 static int devlink_port_fn_hw_addr_fill(struct devlink_port *port,
267 					struct sk_buff *msg,
268 					struct netlink_ext_ack *extack,
269 					bool *msg_updated)
270 {
271 	u8 hw_addr[MAX_ADDR_LEN];
272 	int hw_addr_len;
273 	int err;
274 
275 	if (!port->ops->port_fn_hw_addr_get)
276 		return 0;
277 
278 	err = port->ops->port_fn_hw_addr_get(port, hw_addr, &hw_addr_len,
279 					     extack);
280 	if (err) {
281 		if (err == -EOPNOTSUPP)
282 			return 0;
283 		return err;
284 	}
285 	err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
286 	if (err)
287 		return err;
288 	*msg_updated = true;
289 	return 0;
290 }
291 
292 static bool
293 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
294 {
295 	return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
296 	       state == DEVLINK_PORT_FN_STATE_ACTIVE;
297 }
298 
299 static bool
300 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
301 {
302 	return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
303 	       opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
304 }
305 
306 static int devlink_port_fn_state_fill(struct devlink_port *port,
307 				      struct sk_buff *msg,
308 				      struct netlink_ext_ack *extack,
309 				      bool *msg_updated)
310 {
311 	enum devlink_port_fn_opstate opstate;
312 	enum devlink_port_fn_state state;
313 	int err;
314 
315 	if (!port->ops->port_fn_state_get)
316 		return 0;
317 
318 	err = port->ops->port_fn_state_get(port, &state, &opstate, extack);
319 	if (err) {
320 		if (err == -EOPNOTSUPP)
321 			return 0;
322 		return err;
323 	}
324 	if (!devlink_port_fn_state_valid(state)) {
325 		WARN_ON_ONCE(1);
326 		NL_SET_ERR_MSG(extack, "Invalid state read from driver");
327 		return -EINVAL;
328 	}
329 	if (!devlink_port_fn_opstate_valid(opstate)) {
330 		WARN_ON_ONCE(1);
331 		NL_SET_ERR_MSG(extack, "Invalid operational state read from driver");
332 		return -EINVAL;
333 	}
334 	if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
335 	    nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
336 		return -EMSGSIZE;
337 	*msg_updated = true;
338 	return 0;
339 }
340 
341 static int
342 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
343 			struct netlink_ext_ack *extack)
344 {
345 	return devlink_port->ops->port_fn_migratable_set(devlink_port, enable,
346 							 extack);
347 }
348 
349 static int
350 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
351 			 struct netlink_ext_ack *extack)
352 {
353 	return devlink_port->ops->port_fn_roce_set(devlink_port, enable,
354 						   extack);
355 }
356 
357 static int
358 devlink_port_fn_ipsec_crypto_set(struct devlink_port *devlink_port, bool enable,
359 				 struct netlink_ext_ack *extack)
360 {
361 	return devlink_port->ops->port_fn_ipsec_crypto_set(devlink_port, enable, extack);
362 }
363 
364 static int
365 devlink_port_fn_ipsec_packet_set(struct devlink_port *devlink_port, bool enable,
366 				 struct netlink_ext_ack *extack)
367 {
368 	return devlink_port->ops->port_fn_ipsec_packet_set(devlink_port, enable, extack);
369 }
370 
371 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
372 				    const struct nlattr *attr,
373 				    struct netlink_ext_ack *extack)
374 {
375 	struct nla_bitfield32 caps;
376 	u32 caps_value;
377 	int err;
378 
379 	caps = nla_get_bitfield32(attr);
380 	caps_value = caps.value & caps.selector;
381 	if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) {
382 		err = devlink_port_fn_roce_set(devlink_port,
383 					       caps_value & DEVLINK_PORT_FN_CAP_ROCE,
384 					       extack);
385 		if (err)
386 			return err;
387 	}
388 	if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
389 		err = devlink_port_fn_mig_set(devlink_port, caps_value &
390 					      DEVLINK_PORT_FN_CAP_MIGRATABLE,
391 					      extack);
392 		if (err)
393 			return err;
394 	}
395 	if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO) {
396 		err = devlink_port_fn_ipsec_crypto_set(devlink_port, caps_value &
397 						       DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO,
398 						       extack);
399 		if (err)
400 			return err;
401 	}
402 	if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_PACKET) {
403 		err = devlink_port_fn_ipsec_packet_set(devlink_port, caps_value &
404 						       DEVLINK_PORT_FN_CAP_IPSEC_PACKET,
405 						       extack);
406 		if (err)
407 			return err;
408 	}
409 	return 0;
410 }
411 
412 static int
413 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
414 				   struct netlink_ext_ack *extack)
415 {
416 	struct nlattr *function_attr;
417 	bool msg_updated = false;
418 	int err;
419 
420 	function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
421 	if (!function_attr)
422 		return -EMSGSIZE;
423 
424 	err = devlink_port_fn_hw_addr_fill(port, msg, extack, &msg_updated);
425 	if (err)
426 		goto out;
427 	err = devlink_port_fn_caps_fill(port, msg, extack, &msg_updated);
428 	if (err)
429 		goto out;
430 	err = devlink_port_fn_state_fill(port, msg, extack, &msg_updated);
431 	if (err)
432 		goto out;
433 	err = devlink_rel_devlink_handle_put(msg, port->devlink,
434 					     port->rel_index,
435 					     DEVLINK_PORT_FN_ATTR_DEVLINK,
436 					     &msg_updated);
437 
438 out:
439 	if (err || !msg_updated)
440 		nla_nest_cancel(msg, function_attr);
441 	else
442 		nla_nest_end(msg, function_attr);
443 	return err;
444 }
445 
446 static int devlink_nl_port_fill(struct sk_buff *msg,
447 				struct devlink_port *devlink_port,
448 				enum devlink_command cmd, u32 portid, u32 seq,
449 				int flags, struct netlink_ext_ack *extack)
450 {
451 	struct devlink *devlink = devlink_port->devlink;
452 	void *hdr;
453 
454 	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
455 	if (!hdr)
456 		return -EMSGSIZE;
457 
458 	if (devlink_nl_put_handle(msg, devlink))
459 		goto nla_put_failure;
460 	if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
461 		goto nla_put_failure;
462 
463 	spin_lock_bh(&devlink_port->type_lock);
464 	if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
465 		goto nla_put_failure_type_locked;
466 	if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
467 	    nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
468 			devlink_port->desired_type))
469 		goto nla_put_failure_type_locked;
470 	if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
471 		if (devlink_port->type_eth.netdev &&
472 		    (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
473 				 devlink_port->type_eth.ifindex) ||
474 		     nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
475 				    devlink_port->type_eth.ifname)))
476 			goto nla_put_failure_type_locked;
477 	}
478 	if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
479 		struct ib_device *ibdev = devlink_port->type_ib.ibdev;
480 
481 		if (ibdev &&
482 		    nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
483 				   ibdev->name))
484 			goto nla_put_failure_type_locked;
485 	}
486 	spin_unlock_bh(&devlink_port->type_lock);
487 	if (devlink_nl_port_attrs_put(msg, devlink_port))
488 		goto nla_put_failure;
489 	if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
490 		goto nla_put_failure;
491 	if (devlink_port->linecard &&
492 	    nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
493 			devlink_linecard_index(devlink_port->linecard)))
494 		goto nla_put_failure;
495 
496 	genlmsg_end(msg, hdr);
497 	return 0;
498 
499 nla_put_failure_type_locked:
500 	spin_unlock_bh(&devlink_port->type_lock);
501 nla_put_failure:
502 	genlmsg_cancel(msg, hdr);
503 	return -EMSGSIZE;
504 }
505 
506 static void devlink_port_notify(struct devlink_port *devlink_port,
507 				enum devlink_command cmd)
508 {
509 	struct devlink *devlink = devlink_port->devlink;
510 	struct sk_buff *msg;
511 	int err;
512 
513 	WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
514 
515 	if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
516 		return;
517 
518 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
519 	if (!msg)
520 		return;
521 
522 	err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
523 	if (err) {
524 		nlmsg_free(msg);
525 		return;
526 	}
527 
528 	genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
529 				0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
530 }
531 
532 static void devlink_ports_notify(struct devlink *devlink,
533 				 enum devlink_command cmd)
534 {
535 	struct devlink_port *devlink_port;
536 	unsigned long port_index;
537 
538 	xa_for_each(&devlink->ports, port_index, devlink_port)
539 		devlink_port_notify(devlink_port, cmd);
540 }
541 
542 void devlink_ports_notify_register(struct devlink *devlink)
543 {
544 	devlink_ports_notify(devlink, DEVLINK_CMD_PORT_NEW);
545 }
546 
547 void devlink_ports_notify_unregister(struct devlink *devlink)
548 {
549 	devlink_ports_notify(devlink, DEVLINK_CMD_PORT_DEL);
550 }
551 
552 int devlink_nl_port_get_doit(struct sk_buff *skb, struct genl_info *info)
553 {
554 	struct devlink_port *devlink_port = info->user_ptr[1];
555 	struct sk_buff *msg;
556 	int err;
557 
558 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
559 	if (!msg)
560 		return -ENOMEM;
561 
562 	err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
563 				   info->snd_portid, info->snd_seq, 0,
564 				   info->extack);
565 	if (err) {
566 		nlmsg_free(msg);
567 		return err;
568 	}
569 
570 	return genlmsg_reply(msg, info);
571 }
572 
573 static int
574 devlink_nl_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
575 			     struct netlink_callback *cb, int flags)
576 {
577 	struct devlink_nl_dump_state *state = devlink_dump_state(cb);
578 	struct devlink_port *devlink_port;
579 	unsigned long port_index;
580 	int err = 0;
581 
582 	xa_for_each_start(&devlink->ports, port_index, devlink_port, state->idx) {
583 		err = devlink_nl_port_fill(msg, devlink_port,
584 					   DEVLINK_CMD_NEW,
585 					   NETLINK_CB(cb->skb).portid,
586 					   cb->nlh->nlmsg_seq, flags,
587 					   cb->extack);
588 		if (err) {
589 			state->idx = port_index;
590 			break;
591 		}
592 	}
593 
594 	return err;
595 }
596 
597 int devlink_nl_port_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
598 {
599 	return devlink_nl_dumpit(skb, cb, devlink_nl_port_get_dump_one);
600 }
601 
602 static int devlink_port_type_set(struct devlink_port *devlink_port,
603 				 enum devlink_port_type port_type)
604 
605 {
606 	int err;
607 
608 	if (!devlink_port->ops->port_type_set)
609 		return -EOPNOTSUPP;
610 
611 	if (port_type == devlink_port->type)
612 		return 0;
613 
614 	err = devlink_port->ops->port_type_set(devlink_port, port_type);
615 	if (err)
616 		return err;
617 
618 	devlink_port->desired_type = port_type;
619 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
620 	return 0;
621 }
622 
623 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
624 					     const struct nlattr *attr,
625 					     struct netlink_ext_ack *extack)
626 {
627 	const u8 *hw_addr;
628 	int hw_addr_len;
629 
630 	hw_addr = nla_data(attr);
631 	hw_addr_len = nla_len(attr);
632 	if (hw_addr_len > MAX_ADDR_LEN) {
633 		NL_SET_ERR_MSG(extack, "Port function hardware address too long");
634 		return -EINVAL;
635 	}
636 	if (port->type == DEVLINK_PORT_TYPE_ETH) {
637 		if (hw_addr_len != ETH_ALEN) {
638 			NL_SET_ERR_MSG(extack, "Address must be 6 bytes for Ethernet device");
639 			return -EINVAL;
640 		}
641 		if (!is_unicast_ether_addr(hw_addr)) {
642 			NL_SET_ERR_MSG(extack, "Non-unicast hardware address unsupported");
643 			return -EINVAL;
644 		}
645 	}
646 
647 	return port->ops->port_fn_hw_addr_set(port, hw_addr, hw_addr_len,
648 					      extack);
649 }
650 
651 static int devlink_port_fn_state_set(struct devlink_port *port,
652 				     const struct nlattr *attr,
653 				     struct netlink_ext_ack *extack)
654 {
655 	enum devlink_port_fn_state state;
656 
657 	state = nla_get_u8(attr);
658 	return port->ops->port_fn_state_set(port, state, extack);
659 }
660 
661 static int devlink_port_function_validate(struct devlink_port *devlink_port,
662 					  struct nlattr **tb,
663 					  struct netlink_ext_ack *extack)
664 {
665 	const struct devlink_port_ops *ops = devlink_port->ops;
666 	struct nlattr *attr;
667 
668 	if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
669 	    !ops->port_fn_hw_addr_set) {
670 		NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
671 				    "Port doesn't support function attributes");
672 		return -EOPNOTSUPP;
673 	}
674 	if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
675 		NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
676 				    "Function does not support state setting");
677 		return -EOPNOTSUPP;
678 	}
679 	attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
680 	if (attr) {
681 		struct nla_bitfield32 caps;
682 
683 		caps = nla_get_bitfield32(attr);
684 		if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
685 		    !ops->port_fn_roce_set) {
686 			NL_SET_ERR_MSG_ATTR(extack, attr,
687 					    "Port doesn't support RoCE function attribute");
688 			return -EOPNOTSUPP;
689 		}
690 		if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
691 			if (!ops->port_fn_migratable_set) {
692 				NL_SET_ERR_MSG_ATTR(extack, attr,
693 						    "Port doesn't support migratable function attribute");
694 				return -EOPNOTSUPP;
695 			}
696 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
697 				NL_SET_ERR_MSG_ATTR(extack, attr,
698 						    "migratable function attribute supported for VFs only");
699 				return -EOPNOTSUPP;
700 			}
701 		}
702 		if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO) {
703 			if (!ops->port_fn_ipsec_crypto_set) {
704 				NL_SET_ERR_MSG_ATTR(extack, attr,
705 						    "Port doesn't support ipsec_crypto function attribute");
706 				return -EOPNOTSUPP;
707 			}
708 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
709 				NL_SET_ERR_MSG_ATTR(extack, attr,
710 						    "ipsec_crypto function attribute supported for VFs only");
711 				return -EOPNOTSUPP;
712 			}
713 		}
714 		if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_PACKET) {
715 			if (!ops->port_fn_ipsec_packet_set) {
716 				NL_SET_ERR_MSG_ATTR(extack, attr,
717 						    "Port doesn't support ipsec_packet function attribute");
718 				return -EOPNOTSUPP;
719 			}
720 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
721 				NL_SET_ERR_MSG_ATTR(extack, attr,
722 						    "ipsec_packet function attribute supported for VFs only");
723 				return -EOPNOTSUPP;
724 			}
725 		}
726 	}
727 	return 0;
728 }
729 
730 static int devlink_port_function_set(struct devlink_port *port,
731 				     const struct nlattr *attr,
732 				     struct netlink_ext_ack *extack)
733 {
734 	struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
735 	int err;
736 
737 	err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
738 			       devlink_function_nl_policy, extack);
739 	if (err < 0) {
740 		NL_SET_ERR_MSG(extack, "Fail to parse port function attributes");
741 		return err;
742 	}
743 
744 	err = devlink_port_function_validate(port, tb, extack);
745 	if (err)
746 		return err;
747 
748 	attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
749 	if (attr) {
750 		err = devlink_port_function_hw_addr_set(port, attr, extack);
751 		if (err)
752 			return err;
753 	}
754 
755 	attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
756 	if (attr) {
757 		err = devlink_port_fn_caps_set(port, attr, extack);
758 		if (err)
759 			return err;
760 	}
761 
762 	/* Keep this as the last function attribute set, so that when
763 	 * multiple port function attributes are set along with state,
764 	 * Those can be applied first before activating the state.
765 	 */
766 	attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
767 	if (attr)
768 		err = devlink_port_fn_state_set(port, attr, extack);
769 
770 	if (!err)
771 		devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
772 	return err;
773 }
774 
775 int devlink_nl_port_set_doit(struct sk_buff *skb, struct genl_info *info)
776 {
777 	struct devlink_port *devlink_port = info->user_ptr[1];
778 	int err;
779 
780 	if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
781 		enum devlink_port_type port_type;
782 
783 		port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
784 		err = devlink_port_type_set(devlink_port, port_type);
785 		if (err)
786 			return err;
787 	}
788 
789 	if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
790 		struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
791 		struct netlink_ext_ack *extack = info->extack;
792 
793 		err = devlink_port_function_set(devlink_port, attr, extack);
794 		if (err)
795 			return err;
796 	}
797 
798 	return 0;
799 }
800 
801 int devlink_nl_port_split_doit(struct sk_buff *skb, struct genl_info *info)
802 {
803 	struct devlink_port *devlink_port = info->user_ptr[1];
804 	struct devlink *devlink = info->user_ptr[0];
805 	u32 count;
806 
807 	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
808 		return -EINVAL;
809 	if (!devlink_port->ops->port_split)
810 		return -EOPNOTSUPP;
811 
812 	count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
813 
814 	if (!devlink_port->attrs.splittable) {
815 		/* Split ports cannot be split. */
816 		if (devlink_port->attrs.split)
817 			NL_SET_ERR_MSG(info->extack, "Port cannot be split further");
818 		else
819 			NL_SET_ERR_MSG(info->extack, "Port cannot be split");
820 		return -EINVAL;
821 	}
822 
823 	if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
824 		NL_SET_ERR_MSG(info->extack, "Invalid split count");
825 		return -EINVAL;
826 	}
827 
828 	return devlink_port->ops->port_split(devlink, devlink_port, count,
829 					     info->extack);
830 }
831 
832 int devlink_nl_port_unsplit_doit(struct sk_buff *skb, struct genl_info *info)
833 {
834 	struct devlink_port *devlink_port = info->user_ptr[1];
835 	struct devlink *devlink = info->user_ptr[0];
836 
837 	if (!devlink_port->ops->port_unsplit)
838 		return -EOPNOTSUPP;
839 	return devlink_port->ops->port_unsplit(devlink, devlink_port, info->extack);
840 }
841 
842 int devlink_nl_port_new_doit(struct sk_buff *skb, struct genl_info *info)
843 {
844 	struct netlink_ext_ack *extack = info->extack;
845 	struct devlink_port_new_attrs new_attrs = {};
846 	struct devlink *devlink = info->user_ptr[0];
847 	struct devlink_port *devlink_port;
848 	struct sk_buff *msg;
849 	int err;
850 
851 	if (!devlink->ops->port_new)
852 		return -EOPNOTSUPP;
853 
854 	if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
855 	    !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
856 		NL_SET_ERR_MSG(extack, "Port flavour or PCI PF are not specified");
857 		return -EINVAL;
858 	}
859 	new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
860 	new_attrs.pfnum =
861 		nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
862 
863 	if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
864 		/* Port index of the new port being created by driver. */
865 		new_attrs.port_index =
866 			nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
867 		new_attrs.port_index_valid = true;
868 	}
869 	if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
870 		new_attrs.controller =
871 			nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
872 		new_attrs.controller_valid = true;
873 	}
874 	if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
875 	    info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
876 		new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
877 		new_attrs.sfnum_valid = true;
878 	}
879 
880 	err = devlink->ops->port_new(devlink, &new_attrs,
881 				     extack, &devlink_port);
882 	if (err)
883 		return err;
884 
885 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
886 	if (!msg) {
887 		err = -ENOMEM;
888 		goto err_out_port_del;
889 	}
890 	err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
891 				   info->snd_portid, info->snd_seq, 0, NULL);
892 	if (WARN_ON_ONCE(err))
893 		goto err_out_msg_free;
894 	err = genlmsg_reply(msg, info);
895 	if (err)
896 		goto err_out_port_del;
897 	return 0;
898 
899 err_out_msg_free:
900 	nlmsg_free(msg);
901 err_out_port_del:
902 	devlink_port->ops->port_del(devlink, devlink_port, NULL);
903 	return err;
904 }
905 
906 int devlink_nl_port_del_doit(struct sk_buff *skb, struct genl_info *info)
907 {
908 	struct devlink_port *devlink_port = info->user_ptr[1];
909 	struct netlink_ext_ack *extack = info->extack;
910 	struct devlink *devlink = info->user_ptr[0];
911 
912 	if (!devlink_port->ops->port_del)
913 		return -EOPNOTSUPP;
914 
915 	return devlink_port->ops->port_del(devlink, devlink_port, extack);
916 }
917 
918 static void devlink_port_type_warn(struct work_struct *work)
919 {
920 	struct devlink_port *port = container_of(to_delayed_work(work),
921 						 struct devlink_port,
922 						 type_warn_dw);
923 	dev_warn(port->devlink->dev, "Type was not set for devlink port.");
924 }
925 
926 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
927 {
928 	/* Ignore CPU and DSA flavours. */
929 	return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
930 	       devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
931 	       devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
932 }
933 
934 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
935 
936 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
937 {
938 	if (!devlink_port_type_should_warn(devlink_port))
939 		return;
940 	/* Schedule a work to WARN in case driver does not set port
941 	 * type within timeout.
942 	 */
943 	schedule_delayed_work(&devlink_port->type_warn_dw,
944 			      DEVLINK_PORT_TYPE_WARN_TIMEOUT);
945 }
946 
947 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
948 {
949 	if (!devlink_port_type_should_warn(devlink_port))
950 		return;
951 	cancel_delayed_work_sync(&devlink_port->type_warn_dw);
952 }
953 
954 /**
955  * devlink_port_init() - Init devlink port
956  *
957  * @devlink: devlink
958  * @devlink_port: devlink port
959  *
960  * Initialize essential stuff that is needed for functions
961  * that may be called before devlink port registration.
962  * Call to this function is optional and not needed
963  * in case the driver does not use such functions.
964  */
965 void devlink_port_init(struct devlink *devlink,
966 		       struct devlink_port *devlink_port)
967 {
968 	if (devlink_port->initialized)
969 		return;
970 	devlink_port->devlink = devlink;
971 	INIT_LIST_HEAD(&devlink_port->region_list);
972 	devlink_port->initialized = true;
973 }
974 EXPORT_SYMBOL_GPL(devlink_port_init);
975 
976 /**
977  * devlink_port_fini() - Deinitialize devlink port
978  *
979  * @devlink_port: devlink port
980  *
981  * Deinitialize essential stuff that is in use for functions
982  * that may be called after devlink port unregistration.
983  * Call to this function is optional and not needed
984  * in case the driver does not use such functions.
985  */
986 void devlink_port_fini(struct devlink_port *devlink_port)
987 {
988 	WARN_ON(!list_empty(&devlink_port->region_list));
989 }
990 EXPORT_SYMBOL_GPL(devlink_port_fini);
991 
992 static const struct devlink_port_ops devlink_port_dummy_ops = {};
993 
994 /**
995  * devl_port_register_with_ops() - Register devlink port
996  *
997  * @devlink: devlink
998  * @devlink_port: devlink port
999  * @port_index: driver-specific numerical identifier of the port
1000  * @ops: port ops
1001  *
1002  * Register devlink port with provided port index. User can use
1003  * any indexing, even hw-related one. devlink_port structure
1004  * is convenient to be embedded inside user driver private structure.
1005  * Note that the caller should take care of zeroing the devlink_port
1006  * structure.
1007  */
1008 int devl_port_register_with_ops(struct devlink *devlink,
1009 				struct devlink_port *devlink_port,
1010 				unsigned int port_index,
1011 				const struct devlink_port_ops *ops)
1012 {
1013 	int err;
1014 
1015 	devl_assert_locked(devlink);
1016 
1017 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1018 
1019 	devlink_port_init(devlink, devlink_port);
1020 	devlink_port->registered = true;
1021 	devlink_port->index = port_index;
1022 	devlink_port->ops = ops ? ops : &devlink_port_dummy_ops;
1023 	spin_lock_init(&devlink_port->type_lock);
1024 	INIT_LIST_HEAD(&devlink_port->reporter_list);
1025 	err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
1026 	if (err) {
1027 		devlink_port->registered = false;
1028 		return err;
1029 	}
1030 
1031 	INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
1032 	devlink_port_type_warn_schedule(devlink_port);
1033 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1034 	return 0;
1035 }
1036 EXPORT_SYMBOL_GPL(devl_port_register_with_ops);
1037 
1038 /**
1039  *	devlink_port_register_with_ops - Register devlink port
1040  *
1041  *	@devlink: devlink
1042  *	@devlink_port: devlink port
1043  *	@port_index: driver-specific numerical identifier of the port
1044  *	@ops: port ops
1045  *
1046  *	Register devlink port with provided port index. User can use
1047  *	any indexing, even hw-related one. devlink_port structure
1048  *	is convenient to be embedded inside user driver private structure.
1049  *	Note that the caller should take care of zeroing the devlink_port
1050  *	structure.
1051  *
1052  *	Context: Takes and release devlink->lock <mutex>.
1053  */
1054 int devlink_port_register_with_ops(struct devlink *devlink,
1055 				   struct devlink_port *devlink_port,
1056 				   unsigned int port_index,
1057 				   const struct devlink_port_ops *ops)
1058 {
1059 	int err;
1060 
1061 	devl_lock(devlink);
1062 	err = devl_port_register_with_ops(devlink, devlink_port,
1063 					  port_index, ops);
1064 	devl_unlock(devlink);
1065 	return err;
1066 }
1067 EXPORT_SYMBOL_GPL(devlink_port_register_with_ops);
1068 
1069 /**
1070  * devl_port_unregister() - Unregister devlink port
1071  *
1072  * @devlink_port: devlink port
1073  */
1074 void devl_port_unregister(struct devlink_port *devlink_port)
1075 {
1076 	lockdep_assert_held(&devlink_port->devlink->lock);
1077 	WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
1078 
1079 	devlink_port_type_warn_cancel(devlink_port);
1080 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
1081 	xa_erase(&devlink_port->devlink->ports, devlink_port->index);
1082 	WARN_ON(!list_empty(&devlink_port->reporter_list));
1083 	devlink_port->registered = false;
1084 }
1085 EXPORT_SYMBOL_GPL(devl_port_unregister);
1086 
1087 /**
1088  *	devlink_port_unregister - Unregister devlink port
1089  *
1090  *	@devlink_port: devlink port
1091  *
1092  *	Context: Takes and release devlink->lock <mutex>.
1093  */
1094 void devlink_port_unregister(struct devlink_port *devlink_port)
1095 {
1096 	struct devlink *devlink = devlink_port->devlink;
1097 
1098 	devl_lock(devlink);
1099 	devl_port_unregister(devlink_port);
1100 	devl_unlock(devlink);
1101 }
1102 EXPORT_SYMBOL_GPL(devlink_port_unregister);
1103 
1104 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
1105 					    struct net_device *netdev)
1106 {
1107 	const struct net_device_ops *ops = netdev->netdev_ops;
1108 
1109 	/* If driver registers devlink port, it should set devlink port
1110 	 * attributes accordingly so the compat functions are called
1111 	 * and the original ops are not used.
1112 	 */
1113 	if (ops->ndo_get_phys_port_name) {
1114 		/* Some drivers use the same set of ndos for netdevs
1115 		 * that have devlink_port registered and also for
1116 		 * those who don't. Make sure that ndo_get_phys_port_name
1117 		 * returns -EOPNOTSUPP here in case it is defined.
1118 		 * Warn if not.
1119 		 */
1120 		char name[IFNAMSIZ];
1121 		int err;
1122 
1123 		err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
1124 		WARN_ON(err != -EOPNOTSUPP);
1125 	}
1126 	if (ops->ndo_get_port_parent_id) {
1127 		/* Some drivers use the same set of ndos for netdevs
1128 		 * that have devlink_port registered and also for
1129 		 * those who don't. Make sure that ndo_get_port_parent_id
1130 		 * returns -EOPNOTSUPP here in case it is defined.
1131 		 * Warn if not.
1132 		 */
1133 		struct netdev_phys_item_id ppid;
1134 		int err;
1135 
1136 		err = ops->ndo_get_port_parent_id(netdev, &ppid);
1137 		WARN_ON(err != -EOPNOTSUPP);
1138 	}
1139 }
1140 
1141 static void __devlink_port_type_set(struct devlink_port *devlink_port,
1142 				    enum devlink_port_type type,
1143 				    void *type_dev)
1144 {
1145 	struct net_device *netdev = type_dev;
1146 
1147 	ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
1148 
1149 	if (type == DEVLINK_PORT_TYPE_NOTSET) {
1150 		devlink_port_type_warn_schedule(devlink_port);
1151 	} else {
1152 		devlink_port_type_warn_cancel(devlink_port);
1153 		if (type == DEVLINK_PORT_TYPE_ETH && netdev)
1154 			devlink_port_type_netdev_checks(devlink_port, netdev);
1155 	}
1156 
1157 	spin_lock_bh(&devlink_port->type_lock);
1158 	devlink_port->type = type;
1159 	switch (type) {
1160 	case DEVLINK_PORT_TYPE_ETH:
1161 		devlink_port->type_eth.netdev = netdev;
1162 		if (netdev) {
1163 			ASSERT_RTNL();
1164 			devlink_port->type_eth.ifindex = netdev->ifindex;
1165 			BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
1166 				     sizeof(netdev->name));
1167 			strcpy(devlink_port->type_eth.ifname, netdev->name);
1168 		}
1169 		break;
1170 	case DEVLINK_PORT_TYPE_IB:
1171 		devlink_port->type_ib.ibdev = type_dev;
1172 		break;
1173 	default:
1174 		break;
1175 	}
1176 	spin_unlock_bh(&devlink_port->type_lock);
1177 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1178 }
1179 
1180 /**
1181  *	devlink_port_type_eth_set - Set port type to Ethernet
1182  *
1183  *	@devlink_port: devlink port
1184  *
1185  *	If driver is calling this, most likely it is doing something wrong.
1186  */
1187 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
1188 {
1189 	dev_warn(devlink_port->devlink->dev,
1190 		 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
1191 		 devlink_port->index);
1192 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
1193 }
1194 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
1195 
1196 /**
1197  *	devlink_port_type_ib_set - Set port type to InfiniBand
1198  *
1199  *	@devlink_port: devlink port
1200  *	@ibdev: related IB device
1201  */
1202 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1203 			      struct ib_device *ibdev)
1204 {
1205 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
1206 }
1207 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
1208 
1209 /**
1210  *	devlink_port_type_clear - Clear port type
1211  *
1212  *	@devlink_port: devlink port
1213  *
1214  *	If driver is calling this for clearing Ethernet type, most likely
1215  *	it is doing something wrong.
1216  */
1217 void devlink_port_type_clear(struct devlink_port *devlink_port)
1218 {
1219 	if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
1220 		dev_warn(devlink_port->devlink->dev,
1221 			 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
1222 			 devlink_port->index);
1223 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
1224 }
1225 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
1226 
1227 int devlink_port_netdevice_event(struct notifier_block *nb,
1228 				 unsigned long event, void *ptr)
1229 {
1230 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1231 	struct devlink_port *devlink_port = netdev->devlink_port;
1232 	struct devlink *devlink;
1233 
1234 	if (!devlink_port)
1235 		return NOTIFY_OK;
1236 	devlink = devlink_port->devlink;
1237 
1238 	switch (event) {
1239 	case NETDEV_POST_INIT:
1240 		/* Set the type but not netdev pointer. It is going to be set
1241 		 * later on by NETDEV_REGISTER event. Happens once during
1242 		 * netdevice register
1243 		 */
1244 		__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
1245 					NULL);
1246 		break;
1247 	case NETDEV_REGISTER:
1248 	case NETDEV_CHANGENAME:
1249 		if (devlink_net(devlink) != dev_net(netdev))
1250 			return NOTIFY_OK;
1251 		/* Set the netdev on top of previously set type. Note this
1252 		 * event happens also during net namespace change so here
1253 		 * we take into account netdev pointer appearing in this
1254 		 * namespace.
1255 		 */
1256 		__devlink_port_type_set(devlink_port, devlink_port->type,
1257 					netdev);
1258 		break;
1259 	case NETDEV_UNREGISTER:
1260 		if (devlink_net(devlink) != dev_net(netdev))
1261 			return NOTIFY_OK;
1262 		/* Clear netdev pointer, but not the type. This event happens
1263 		 * also during net namespace change so we need to clear
1264 		 * pointer to netdev that is going to another net namespace.
1265 		 */
1266 		__devlink_port_type_set(devlink_port, devlink_port->type,
1267 					NULL);
1268 		break;
1269 	case NETDEV_PRE_UNINIT:
1270 		/* Clear the type and the netdev pointer. Happens one during
1271 		 * netdevice unregister.
1272 		 */
1273 		__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
1274 					NULL);
1275 		break;
1276 	}
1277 
1278 	return NOTIFY_OK;
1279 }
1280 
1281 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
1282 				    enum devlink_port_flavour flavour)
1283 {
1284 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1285 
1286 	devlink_port->attrs_set = true;
1287 	attrs->flavour = flavour;
1288 	if (attrs->switch_id.id_len) {
1289 		devlink_port->switch_port = true;
1290 		if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
1291 			attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
1292 	} else {
1293 		devlink_port->switch_port = false;
1294 	}
1295 	return 0;
1296 }
1297 
1298 /**
1299  *	devlink_port_attrs_set - Set port attributes
1300  *
1301  *	@devlink_port: devlink port
1302  *	@attrs: devlink port attrs
1303  */
1304 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1305 			    struct devlink_port_attrs *attrs)
1306 {
1307 	int ret;
1308 
1309 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1310 
1311 	devlink_port->attrs = *attrs;
1312 	ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
1313 	if (ret)
1314 		return;
1315 	WARN_ON(attrs->splittable && attrs->split);
1316 }
1317 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
1318 
1319 /**
1320  *	devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
1321  *
1322  *	@devlink_port: devlink port
1323  *	@controller: associated controller number for the devlink port instance
1324  *	@pf: associated PF for the devlink port instance
1325  *	@external: indicates if the port is for an external controller
1326  */
1327 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
1328 				   u16 pf, bool external)
1329 {
1330 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1331 	int ret;
1332 
1333 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1334 
1335 	ret = __devlink_port_attrs_set(devlink_port,
1336 				       DEVLINK_PORT_FLAVOUR_PCI_PF);
1337 	if (ret)
1338 		return;
1339 	attrs->pci_pf.controller = controller;
1340 	attrs->pci_pf.pf = pf;
1341 	attrs->pci_pf.external = external;
1342 }
1343 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
1344 
1345 /**
1346  *	devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
1347  *
1348  *	@devlink_port: devlink port
1349  *	@controller: associated controller number for the devlink port instance
1350  *	@pf: associated PF for the devlink port instance
1351  *	@vf: associated VF of a PF for the devlink port instance
1352  *	@external: indicates if the port is for an external controller
1353  */
1354 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
1355 				   u16 pf, u16 vf, bool external)
1356 {
1357 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1358 	int ret;
1359 
1360 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1361 
1362 	ret = __devlink_port_attrs_set(devlink_port,
1363 				       DEVLINK_PORT_FLAVOUR_PCI_VF);
1364 	if (ret)
1365 		return;
1366 	attrs->pci_vf.controller = controller;
1367 	attrs->pci_vf.pf = pf;
1368 	attrs->pci_vf.vf = vf;
1369 	attrs->pci_vf.external = external;
1370 }
1371 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
1372 
1373 /**
1374  *	devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
1375  *
1376  *	@devlink_port: devlink port
1377  *	@controller: associated controller number for the devlink port instance
1378  *	@pf: associated PF for the devlink port instance
1379  *	@sf: associated SF of a PF for the devlink port instance
1380  *	@external: indicates if the port is for an external controller
1381  */
1382 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
1383 				   u16 pf, u32 sf, bool external)
1384 {
1385 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1386 	int ret;
1387 
1388 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1389 
1390 	ret = __devlink_port_attrs_set(devlink_port,
1391 				       DEVLINK_PORT_FLAVOUR_PCI_SF);
1392 	if (ret)
1393 		return;
1394 	attrs->pci_sf.controller = controller;
1395 	attrs->pci_sf.pf = pf;
1396 	attrs->pci_sf.sf = sf;
1397 	attrs->pci_sf.external = external;
1398 }
1399 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
1400 
1401 static void devlink_port_rel_notify_cb(struct devlink *devlink, u32 port_index)
1402 {
1403 	struct devlink_port *devlink_port;
1404 
1405 	devlink_port = devlink_port_get_by_index(devlink, port_index);
1406 	if (!devlink_port)
1407 		return;
1408 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1409 }
1410 
1411 static void devlink_port_rel_cleanup_cb(struct devlink *devlink, u32 port_index,
1412 					u32 rel_index)
1413 {
1414 	struct devlink_port *devlink_port;
1415 
1416 	devlink_port = devlink_port_get_by_index(devlink, port_index);
1417 	if (devlink_port && devlink_port->rel_index == rel_index)
1418 		devlink_port->rel_index = 0;
1419 }
1420 
1421 /**
1422  * devl_port_fn_devlink_set - Attach peer devlink
1423  *			      instance to port function.
1424  * @devlink_port: devlink port
1425  * @fn_devlink: devlink instance to attach
1426  */
1427 int devl_port_fn_devlink_set(struct devlink_port *devlink_port,
1428 			     struct devlink *fn_devlink)
1429 {
1430 	ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
1431 
1432 	if (WARN_ON(devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_SF ||
1433 		    devlink_port->attrs.pci_sf.external))
1434 		return -EINVAL;
1435 
1436 	return devlink_rel_nested_in_add(&devlink_port->rel_index,
1437 					 devlink_port->devlink->index,
1438 					 devlink_port->index,
1439 					 devlink_port_rel_notify_cb,
1440 					 devlink_port_rel_cleanup_cb,
1441 					 fn_devlink);
1442 }
1443 EXPORT_SYMBOL_GPL(devl_port_fn_devlink_set);
1444 
1445 /**
1446  *	devlink_port_linecard_set - Link port with a linecard
1447  *
1448  *	@devlink_port: devlink port
1449  *	@linecard: devlink linecard
1450  */
1451 void devlink_port_linecard_set(struct devlink_port *devlink_port,
1452 			       struct devlink_linecard *linecard)
1453 {
1454 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1455 
1456 	devlink_port->linecard = linecard;
1457 }
1458 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
1459 
1460 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
1461 					     char *name, size_t len)
1462 {
1463 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1464 	int n = 0;
1465 
1466 	if (!devlink_port->attrs_set)
1467 		return -EOPNOTSUPP;
1468 
1469 	switch (attrs->flavour) {
1470 	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
1471 		if (devlink_port->linecard)
1472 			n = snprintf(name, len, "l%u",
1473 				     devlink_linecard_index(devlink_port->linecard));
1474 		if (n < len)
1475 			n += snprintf(name + n, len - n, "p%u",
1476 				      attrs->phys.port_number);
1477 		if (n < len && attrs->split)
1478 			n += snprintf(name + n, len - n, "s%u",
1479 				      attrs->phys.split_subport_number);
1480 		break;
1481 	case DEVLINK_PORT_FLAVOUR_CPU:
1482 	case DEVLINK_PORT_FLAVOUR_DSA:
1483 	case DEVLINK_PORT_FLAVOUR_UNUSED:
1484 		/* As CPU and DSA ports do not have a netdevice associated
1485 		 * case should not ever happen.
1486 		 */
1487 		WARN_ON(1);
1488 		return -EINVAL;
1489 	case DEVLINK_PORT_FLAVOUR_PCI_PF:
1490 		if (attrs->pci_pf.external) {
1491 			n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
1492 			if (n >= len)
1493 				return -EINVAL;
1494 			len -= n;
1495 			name += n;
1496 		}
1497 		n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
1498 		break;
1499 	case DEVLINK_PORT_FLAVOUR_PCI_VF:
1500 		if (attrs->pci_vf.external) {
1501 			n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
1502 			if (n >= len)
1503 				return -EINVAL;
1504 			len -= n;
1505 			name += n;
1506 		}
1507 		n = snprintf(name, len, "pf%uvf%u",
1508 			     attrs->pci_vf.pf, attrs->pci_vf.vf);
1509 		break;
1510 	case DEVLINK_PORT_FLAVOUR_PCI_SF:
1511 		if (attrs->pci_sf.external) {
1512 			n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
1513 			if (n >= len)
1514 				return -EINVAL;
1515 			len -= n;
1516 			name += n;
1517 		}
1518 		n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
1519 			     attrs->pci_sf.sf);
1520 		break;
1521 	case DEVLINK_PORT_FLAVOUR_VIRTUAL:
1522 		return -EOPNOTSUPP;
1523 	}
1524 
1525 	if (n >= len)
1526 		return -EINVAL;
1527 
1528 	return 0;
1529 }
1530 
1531 int devlink_compat_phys_port_name_get(struct net_device *dev,
1532 				      char *name, size_t len)
1533 {
1534 	struct devlink_port *devlink_port;
1535 
1536 	/* RTNL mutex is held here which ensures that devlink_port
1537 	 * instance cannot disappear in the middle. No need to take
1538 	 * any devlink lock as only permanent values are accessed.
1539 	 */
1540 	ASSERT_RTNL();
1541 
1542 	devlink_port = dev->devlink_port;
1543 	if (!devlink_port)
1544 		return -EOPNOTSUPP;
1545 
1546 	return __devlink_port_phys_port_name_get(devlink_port, name, len);
1547 }
1548 
1549 int devlink_compat_switch_id_get(struct net_device *dev,
1550 				 struct netdev_phys_item_id *ppid)
1551 {
1552 	struct devlink_port *devlink_port;
1553 
1554 	/* Caller must hold RTNL mutex or reference to dev, which ensures that
1555 	 * devlink_port instance cannot disappear in the middle. No need to take
1556 	 * any devlink lock as only permanent values are accessed.
1557 	 */
1558 	devlink_port = dev->devlink_port;
1559 	if (!devlink_port || !devlink_port->switch_port)
1560 		return -EOPNOTSUPP;
1561 
1562 	memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
1563 
1564 	return 0;
1565 }
1566