xref: /linux/net/devlink/port.c (revision f2ec98566775dd4341ec1dcf93aa5859c60de826)
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 devlink_obj_desc desc;
511 	struct sk_buff *msg;
512 	int err;
513 
514 	WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
515 
516 	if (!__devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
517 		return;
518 
519 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
520 	if (!msg)
521 		return;
522 
523 	err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
524 	if (err) {
525 		nlmsg_free(msg);
526 		return;
527 	}
528 
529 	devlink_nl_obj_desc_init(&desc, devlink);
530 	devlink_nl_obj_desc_port_set(&desc, devlink_port);
531 	devlink_nl_notify_send_desc(devlink, msg, &desc);
532 }
533 
534 static void devlink_ports_notify(struct devlink *devlink,
535 				 enum devlink_command cmd)
536 {
537 	struct devlink_port *devlink_port;
538 	unsigned long port_index;
539 
540 	xa_for_each(&devlink->ports, port_index, devlink_port)
541 		devlink_port_notify(devlink_port, cmd);
542 }
543 
544 void devlink_ports_notify_register(struct devlink *devlink)
545 {
546 	devlink_ports_notify(devlink, DEVLINK_CMD_PORT_NEW);
547 }
548 
549 void devlink_ports_notify_unregister(struct devlink *devlink)
550 {
551 	devlink_ports_notify(devlink, DEVLINK_CMD_PORT_DEL);
552 }
553 
554 int devlink_nl_port_get_doit(struct sk_buff *skb, struct genl_info *info)
555 {
556 	struct devlink_port *devlink_port = info->user_ptr[1];
557 	struct sk_buff *msg;
558 	int err;
559 
560 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
561 	if (!msg)
562 		return -ENOMEM;
563 
564 	err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
565 				   info->snd_portid, info->snd_seq, 0,
566 				   info->extack);
567 	if (err) {
568 		nlmsg_free(msg);
569 		return err;
570 	}
571 
572 	return genlmsg_reply(msg, info);
573 }
574 
575 static int
576 devlink_nl_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
577 			     struct netlink_callback *cb, int flags)
578 {
579 	struct devlink_nl_dump_state *state = devlink_dump_state(cb);
580 	struct devlink_port *devlink_port;
581 	unsigned long port_index;
582 	int err = 0;
583 
584 	xa_for_each_start(&devlink->ports, port_index, devlink_port, state->idx) {
585 		err = devlink_nl_port_fill(msg, devlink_port,
586 					   DEVLINK_CMD_NEW,
587 					   NETLINK_CB(cb->skb).portid,
588 					   cb->nlh->nlmsg_seq, flags,
589 					   cb->extack);
590 		if (err) {
591 			state->idx = port_index;
592 			break;
593 		}
594 	}
595 
596 	return err;
597 }
598 
599 int devlink_nl_port_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
600 {
601 	return devlink_nl_dumpit(skb, cb, devlink_nl_port_get_dump_one);
602 }
603 
604 static int devlink_port_type_set(struct devlink_port *devlink_port,
605 				 enum devlink_port_type port_type)
606 
607 {
608 	int err;
609 
610 	if (!devlink_port->ops->port_type_set)
611 		return -EOPNOTSUPP;
612 
613 	if (port_type == devlink_port->type)
614 		return 0;
615 
616 	err = devlink_port->ops->port_type_set(devlink_port, port_type);
617 	if (err)
618 		return err;
619 
620 	devlink_port->desired_type = port_type;
621 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
622 	return 0;
623 }
624 
625 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
626 					     const struct nlattr *attr,
627 					     struct netlink_ext_ack *extack)
628 {
629 	const u8 *hw_addr;
630 	int hw_addr_len;
631 
632 	hw_addr = nla_data(attr);
633 	hw_addr_len = nla_len(attr);
634 	if (hw_addr_len > MAX_ADDR_LEN) {
635 		NL_SET_ERR_MSG(extack, "Port function hardware address too long");
636 		return -EINVAL;
637 	}
638 	if (port->type == DEVLINK_PORT_TYPE_ETH) {
639 		if (hw_addr_len != ETH_ALEN) {
640 			NL_SET_ERR_MSG(extack, "Address must be 6 bytes for Ethernet device");
641 			return -EINVAL;
642 		}
643 		if (!is_unicast_ether_addr(hw_addr)) {
644 			NL_SET_ERR_MSG(extack, "Non-unicast hardware address unsupported");
645 			return -EINVAL;
646 		}
647 	}
648 
649 	return port->ops->port_fn_hw_addr_set(port, hw_addr, hw_addr_len,
650 					      extack);
651 }
652 
653 static int devlink_port_fn_state_set(struct devlink_port *port,
654 				     const struct nlattr *attr,
655 				     struct netlink_ext_ack *extack)
656 {
657 	enum devlink_port_fn_state state;
658 
659 	state = nla_get_u8(attr);
660 	return port->ops->port_fn_state_set(port, state, extack);
661 }
662 
663 static int devlink_port_function_validate(struct devlink_port *devlink_port,
664 					  struct nlattr **tb,
665 					  struct netlink_ext_ack *extack)
666 {
667 	const struct devlink_port_ops *ops = devlink_port->ops;
668 	struct nlattr *attr;
669 
670 	if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
671 	    !ops->port_fn_hw_addr_set) {
672 		NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
673 				    "Port doesn't support function attributes");
674 		return -EOPNOTSUPP;
675 	}
676 	if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
677 		NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FN_ATTR_STATE],
678 				    "Function does not support state setting");
679 		return -EOPNOTSUPP;
680 	}
681 	attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
682 	if (attr) {
683 		struct nla_bitfield32 caps;
684 
685 		caps = nla_get_bitfield32(attr);
686 		if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
687 		    !ops->port_fn_roce_set) {
688 			NL_SET_ERR_MSG_ATTR(extack, attr,
689 					    "Port doesn't support RoCE function attribute");
690 			return -EOPNOTSUPP;
691 		}
692 		if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
693 			if (!ops->port_fn_migratable_set) {
694 				NL_SET_ERR_MSG_ATTR(extack, attr,
695 						    "Port doesn't support migratable function attribute");
696 				return -EOPNOTSUPP;
697 			}
698 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
699 				NL_SET_ERR_MSG_ATTR(extack, attr,
700 						    "migratable function attribute supported for VFs only");
701 				return -EOPNOTSUPP;
702 			}
703 		}
704 		if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO) {
705 			if (!ops->port_fn_ipsec_crypto_set) {
706 				NL_SET_ERR_MSG_ATTR(extack, attr,
707 						    "Port doesn't support ipsec_crypto function attribute");
708 				return -EOPNOTSUPP;
709 			}
710 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
711 				NL_SET_ERR_MSG_ATTR(extack, attr,
712 						    "ipsec_crypto function attribute supported for VFs only");
713 				return -EOPNOTSUPP;
714 			}
715 		}
716 		if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_PACKET) {
717 			if (!ops->port_fn_ipsec_packet_set) {
718 				NL_SET_ERR_MSG_ATTR(extack, attr,
719 						    "Port doesn't support ipsec_packet function attribute");
720 				return -EOPNOTSUPP;
721 			}
722 			if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
723 				NL_SET_ERR_MSG_ATTR(extack, attr,
724 						    "ipsec_packet function attribute supported for VFs only");
725 				return -EOPNOTSUPP;
726 			}
727 		}
728 	}
729 	return 0;
730 }
731 
732 static int devlink_port_function_set(struct devlink_port *port,
733 				     const struct nlattr *attr,
734 				     struct netlink_ext_ack *extack)
735 {
736 	struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
737 	int err;
738 
739 	err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
740 			       devlink_function_nl_policy, extack);
741 	if (err < 0) {
742 		NL_SET_ERR_MSG(extack, "Fail to parse port function attributes");
743 		return err;
744 	}
745 
746 	err = devlink_port_function_validate(port, tb, extack);
747 	if (err)
748 		return err;
749 
750 	attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
751 	if (attr) {
752 		err = devlink_port_function_hw_addr_set(port, attr, extack);
753 		if (err)
754 			return err;
755 	}
756 
757 	attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
758 	if (attr) {
759 		err = devlink_port_fn_caps_set(port, attr, extack);
760 		if (err)
761 			return err;
762 	}
763 
764 	/* Keep this as the last function attribute set, so that when
765 	 * multiple port function attributes are set along with state,
766 	 * Those can be applied first before activating the state.
767 	 */
768 	attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
769 	if (attr)
770 		err = devlink_port_fn_state_set(port, attr, extack);
771 
772 	if (!err)
773 		devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
774 	return err;
775 }
776 
777 int devlink_nl_port_set_doit(struct sk_buff *skb, struct genl_info *info)
778 {
779 	struct devlink_port *devlink_port = info->user_ptr[1];
780 	int err;
781 
782 	if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
783 		enum devlink_port_type port_type;
784 
785 		port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
786 		err = devlink_port_type_set(devlink_port, port_type);
787 		if (err)
788 			return err;
789 	}
790 
791 	if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
792 		struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
793 		struct netlink_ext_ack *extack = info->extack;
794 
795 		err = devlink_port_function_set(devlink_port, attr, extack);
796 		if (err)
797 			return err;
798 	}
799 
800 	return 0;
801 }
802 
803 int devlink_nl_port_split_doit(struct sk_buff *skb, struct genl_info *info)
804 {
805 	struct devlink_port *devlink_port = info->user_ptr[1];
806 	struct devlink *devlink = info->user_ptr[0];
807 	u32 count;
808 
809 	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
810 		return -EINVAL;
811 	if (!devlink_port->ops->port_split)
812 		return -EOPNOTSUPP;
813 
814 	count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
815 
816 	if (!devlink_port->attrs.splittable) {
817 		/* Split ports cannot be split. */
818 		if (devlink_port->attrs.split)
819 			NL_SET_ERR_MSG(info->extack, "Port cannot be split further");
820 		else
821 			NL_SET_ERR_MSG(info->extack, "Port cannot be split");
822 		return -EINVAL;
823 	}
824 
825 	if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
826 		NL_SET_ERR_MSG(info->extack, "Invalid split count");
827 		return -EINVAL;
828 	}
829 
830 	return devlink_port->ops->port_split(devlink, devlink_port, count,
831 					     info->extack);
832 }
833 
834 int devlink_nl_port_unsplit_doit(struct sk_buff *skb, struct genl_info *info)
835 {
836 	struct devlink_port *devlink_port = info->user_ptr[1];
837 	struct devlink *devlink = info->user_ptr[0];
838 
839 	if (!devlink_port->ops->port_unsplit)
840 		return -EOPNOTSUPP;
841 	return devlink_port->ops->port_unsplit(devlink, devlink_port, info->extack);
842 }
843 
844 int devlink_nl_port_new_doit(struct sk_buff *skb, struct genl_info *info)
845 {
846 	struct netlink_ext_ack *extack = info->extack;
847 	struct devlink_port_new_attrs new_attrs = {};
848 	struct devlink *devlink = info->user_ptr[0];
849 	struct devlink_port *devlink_port;
850 	struct sk_buff *msg;
851 	int err;
852 
853 	if (!devlink->ops->port_new)
854 		return -EOPNOTSUPP;
855 
856 	if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
857 	    !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
858 		NL_SET_ERR_MSG(extack, "Port flavour or PCI PF are not specified");
859 		return -EINVAL;
860 	}
861 	new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
862 	new_attrs.pfnum =
863 		nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
864 
865 	if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
866 		/* Port index of the new port being created by driver. */
867 		new_attrs.port_index =
868 			nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
869 		new_attrs.port_index_valid = true;
870 	}
871 	if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
872 		new_attrs.controller =
873 			nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
874 		new_attrs.controller_valid = true;
875 	}
876 	if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
877 	    info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
878 		new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
879 		new_attrs.sfnum_valid = true;
880 	}
881 
882 	err = devlink->ops->port_new(devlink, &new_attrs,
883 				     extack, &devlink_port);
884 	if (err)
885 		return err;
886 
887 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
888 	if (!msg) {
889 		err = -ENOMEM;
890 		goto err_out_port_del;
891 	}
892 	err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
893 				   info->snd_portid, info->snd_seq, 0, NULL);
894 	if (WARN_ON_ONCE(err))
895 		goto err_out_msg_free;
896 	err = genlmsg_reply(msg, info);
897 	if (err)
898 		goto err_out_port_del;
899 	return 0;
900 
901 err_out_msg_free:
902 	nlmsg_free(msg);
903 err_out_port_del:
904 	devlink_port->ops->port_del(devlink, devlink_port, NULL);
905 	return err;
906 }
907 
908 int devlink_nl_port_del_doit(struct sk_buff *skb, struct genl_info *info)
909 {
910 	struct devlink_port *devlink_port = info->user_ptr[1];
911 	struct netlink_ext_ack *extack = info->extack;
912 	struct devlink *devlink = info->user_ptr[0];
913 
914 	if (!devlink_port->ops->port_del)
915 		return -EOPNOTSUPP;
916 
917 	return devlink_port->ops->port_del(devlink, devlink_port, extack);
918 }
919 
920 static void devlink_port_type_warn(struct work_struct *work)
921 {
922 	struct devlink_port *port = container_of(to_delayed_work(work),
923 						 struct devlink_port,
924 						 type_warn_dw);
925 	dev_warn(port->devlink->dev, "Type was not set for devlink port.");
926 }
927 
928 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
929 {
930 	/* Ignore CPU and DSA flavours. */
931 	return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
932 	       devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
933 	       devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
934 }
935 
936 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
937 
938 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
939 {
940 	if (!devlink_port_type_should_warn(devlink_port))
941 		return;
942 	/* Schedule a work to WARN in case driver does not set port
943 	 * type within timeout.
944 	 */
945 	schedule_delayed_work(&devlink_port->type_warn_dw,
946 			      DEVLINK_PORT_TYPE_WARN_TIMEOUT);
947 }
948 
949 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
950 {
951 	if (!devlink_port_type_should_warn(devlink_port))
952 		return;
953 	cancel_delayed_work_sync(&devlink_port->type_warn_dw);
954 }
955 
956 /**
957  * devlink_port_init() - Init devlink port
958  *
959  * @devlink: devlink
960  * @devlink_port: devlink port
961  *
962  * Initialize essential stuff that is needed for functions
963  * that may be called before devlink port registration.
964  * Call to this function is optional and not needed
965  * in case the driver does not use such functions.
966  */
967 void devlink_port_init(struct devlink *devlink,
968 		       struct devlink_port *devlink_port)
969 {
970 	if (devlink_port->initialized)
971 		return;
972 	devlink_port->devlink = devlink;
973 	INIT_LIST_HEAD(&devlink_port->region_list);
974 	devlink_port->initialized = true;
975 }
976 EXPORT_SYMBOL_GPL(devlink_port_init);
977 
978 /**
979  * devlink_port_fini() - Deinitialize devlink port
980  *
981  * @devlink_port: devlink port
982  *
983  * Deinitialize essential stuff that is in use for functions
984  * that may be called after devlink port unregistration.
985  * Call to this function is optional and not needed
986  * in case the driver does not use such functions.
987  */
988 void devlink_port_fini(struct devlink_port *devlink_port)
989 {
990 	WARN_ON(!list_empty(&devlink_port->region_list));
991 }
992 EXPORT_SYMBOL_GPL(devlink_port_fini);
993 
994 static const struct devlink_port_ops devlink_port_dummy_ops = {};
995 
996 /**
997  * devl_port_register_with_ops() - Register devlink port
998  *
999  * @devlink: devlink
1000  * @devlink_port: devlink port
1001  * @port_index: driver-specific numerical identifier of the port
1002  * @ops: port ops
1003  *
1004  * Register devlink port with provided port index. User can use
1005  * any indexing, even hw-related one. devlink_port structure
1006  * is convenient to be embedded inside user driver private structure.
1007  * Note that the caller should take care of zeroing the devlink_port
1008  * structure.
1009  */
1010 int devl_port_register_with_ops(struct devlink *devlink,
1011 				struct devlink_port *devlink_port,
1012 				unsigned int port_index,
1013 				const struct devlink_port_ops *ops)
1014 {
1015 	int err;
1016 
1017 	devl_assert_locked(devlink);
1018 
1019 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1020 
1021 	devlink_port_init(devlink, devlink_port);
1022 	devlink_port->registered = true;
1023 	devlink_port->index = port_index;
1024 	devlink_port->ops = ops ? ops : &devlink_port_dummy_ops;
1025 	spin_lock_init(&devlink_port->type_lock);
1026 	INIT_LIST_HEAD(&devlink_port->reporter_list);
1027 	err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
1028 	if (err) {
1029 		devlink_port->registered = false;
1030 		return err;
1031 	}
1032 
1033 	INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
1034 	devlink_port_type_warn_schedule(devlink_port);
1035 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1036 	return 0;
1037 }
1038 EXPORT_SYMBOL_GPL(devl_port_register_with_ops);
1039 
1040 /**
1041  *	devlink_port_register_with_ops - Register devlink port
1042  *
1043  *	@devlink: devlink
1044  *	@devlink_port: devlink port
1045  *	@port_index: driver-specific numerical identifier of the port
1046  *	@ops: port ops
1047  *
1048  *	Register devlink port with provided port index. User can use
1049  *	any indexing, even hw-related one. devlink_port structure
1050  *	is convenient to be embedded inside user driver private structure.
1051  *	Note that the caller should take care of zeroing the devlink_port
1052  *	structure.
1053  *
1054  *	Context: Takes and release devlink->lock <mutex>.
1055  */
1056 int devlink_port_register_with_ops(struct devlink *devlink,
1057 				   struct devlink_port *devlink_port,
1058 				   unsigned int port_index,
1059 				   const struct devlink_port_ops *ops)
1060 {
1061 	int err;
1062 
1063 	devl_lock(devlink);
1064 	err = devl_port_register_with_ops(devlink, devlink_port,
1065 					  port_index, ops);
1066 	devl_unlock(devlink);
1067 	return err;
1068 }
1069 EXPORT_SYMBOL_GPL(devlink_port_register_with_ops);
1070 
1071 /**
1072  * devl_port_unregister() - Unregister devlink port
1073  *
1074  * @devlink_port: devlink port
1075  */
1076 void devl_port_unregister(struct devlink_port *devlink_port)
1077 {
1078 	lockdep_assert_held(&devlink_port->devlink->lock);
1079 	WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
1080 
1081 	devlink_port_type_warn_cancel(devlink_port);
1082 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
1083 	xa_erase(&devlink_port->devlink->ports, devlink_port->index);
1084 	WARN_ON(!list_empty(&devlink_port->reporter_list));
1085 	devlink_port->registered = false;
1086 }
1087 EXPORT_SYMBOL_GPL(devl_port_unregister);
1088 
1089 /**
1090  *	devlink_port_unregister - Unregister devlink port
1091  *
1092  *	@devlink_port: devlink port
1093  *
1094  *	Context: Takes and release devlink->lock <mutex>.
1095  */
1096 void devlink_port_unregister(struct devlink_port *devlink_port)
1097 {
1098 	struct devlink *devlink = devlink_port->devlink;
1099 
1100 	devl_lock(devlink);
1101 	devl_port_unregister(devlink_port);
1102 	devl_unlock(devlink);
1103 }
1104 EXPORT_SYMBOL_GPL(devlink_port_unregister);
1105 
1106 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
1107 					    struct net_device *netdev)
1108 {
1109 	const struct net_device_ops *ops = netdev->netdev_ops;
1110 
1111 	/* If driver registers devlink port, it should set devlink port
1112 	 * attributes accordingly so the compat functions are called
1113 	 * and the original ops are not used.
1114 	 */
1115 	if (ops->ndo_get_phys_port_name) {
1116 		/* Some drivers use the same set of ndos for netdevs
1117 		 * that have devlink_port registered and also for
1118 		 * those who don't. Make sure that ndo_get_phys_port_name
1119 		 * returns -EOPNOTSUPP here in case it is defined.
1120 		 * Warn if not.
1121 		 */
1122 		char name[IFNAMSIZ];
1123 		int err;
1124 
1125 		err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
1126 		WARN_ON(err != -EOPNOTSUPP);
1127 	}
1128 	if (ops->ndo_get_port_parent_id) {
1129 		/* Some drivers use the same set of ndos for netdevs
1130 		 * that have devlink_port registered and also for
1131 		 * those who don't. Make sure that ndo_get_port_parent_id
1132 		 * returns -EOPNOTSUPP here in case it is defined.
1133 		 * Warn if not.
1134 		 */
1135 		struct netdev_phys_item_id ppid;
1136 		int err;
1137 
1138 		err = ops->ndo_get_port_parent_id(netdev, &ppid);
1139 		WARN_ON(err != -EOPNOTSUPP);
1140 	}
1141 }
1142 
1143 static void __devlink_port_type_set(struct devlink_port *devlink_port,
1144 				    enum devlink_port_type type,
1145 				    void *type_dev)
1146 {
1147 	struct net_device *netdev = type_dev;
1148 
1149 	ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
1150 
1151 	if (type == DEVLINK_PORT_TYPE_NOTSET) {
1152 		devlink_port_type_warn_schedule(devlink_port);
1153 	} else {
1154 		devlink_port_type_warn_cancel(devlink_port);
1155 		if (type == DEVLINK_PORT_TYPE_ETH && netdev)
1156 			devlink_port_type_netdev_checks(devlink_port, netdev);
1157 	}
1158 
1159 	spin_lock_bh(&devlink_port->type_lock);
1160 	devlink_port->type = type;
1161 	switch (type) {
1162 	case DEVLINK_PORT_TYPE_ETH:
1163 		devlink_port->type_eth.netdev = netdev;
1164 		if (netdev) {
1165 			ASSERT_RTNL();
1166 			devlink_port->type_eth.ifindex = netdev->ifindex;
1167 			BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
1168 				     sizeof(netdev->name));
1169 			strcpy(devlink_port->type_eth.ifname, netdev->name);
1170 		}
1171 		break;
1172 	case DEVLINK_PORT_TYPE_IB:
1173 		devlink_port->type_ib.ibdev = type_dev;
1174 		break;
1175 	default:
1176 		break;
1177 	}
1178 	spin_unlock_bh(&devlink_port->type_lock);
1179 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1180 }
1181 
1182 /**
1183  *	devlink_port_type_eth_set - Set port type to Ethernet
1184  *
1185  *	@devlink_port: devlink port
1186  *
1187  *	If driver is calling this, most likely it is doing something wrong.
1188  */
1189 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
1190 {
1191 	dev_warn(devlink_port->devlink->dev,
1192 		 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
1193 		 devlink_port->index);
1194 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
1195 }
1196 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
1197 
1198 /**
1199  *	devlink_port_type_ib_set - Set port type to InfiniBand
1200  *
1201  *	@devlink_port: devlink port
1202  *	@ibdev: related IB device
1203  */
1204 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1205 			      struct ib_device *ibdev)
1206 {
1207 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
1208 }
1209 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
1210 
1211 /**
1212  *	devlink_port_type_clear - Clear port type
1213  *
1214  *	@devlink_port: devlink port
1215  *
1216  *	If driver is calling this for clearing Ethernet type, most likely
1217  *	it is doing something wrong.
1218  */
1219 void devlink_port_type_clear(struct devlink_port *devlink_port)
1220 {
1221 	if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
1222 		dev_warn(devlink_port->devlink->dev,
1223 			 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
1224 			 devlink_port->index);
1225 	__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
1226 }
1227 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
1228 
1229 int devlink_port_netdevice_event(struct notifier_block *nb,
1230 				 unsigned long event, void *ptr)
1231 {
1232 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1233 	struct devlink_port *devlink_port = netdev->devlink_port;
1234 	struct devlink *devlink;
1235 
1236 	if (!devlink_port)
1237 		return NOTIFY_OK;
1238 	devlink = devlink_port->devlink;
1239 
1240 	switch (event) {
1241 	case NETDEV_POST_INIT:
1242 		/* Set the type but not netdev pointer. It is going to be set
1243 		 * later on by NETDEV_REGISTER event. Happens once during
1244 		 * netdevice register
1245 		 */
1246 		__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
1247 					NULL);
1248 		break;
1249 	case NETDEV_REGISTER:
1250 	case NETDEV_CHANGENAME:
1251 		if (devlink_net(devlink) != dev_net(netdev))
1252 			return NOTIFY_OK;
1253 		/* Set the netdev on top of previously set type. Note this
1254 		 * event happens also during net namespace change so here
1255 		 * we take into account netdev pointer appearing in this
1256 		 * namespace.
1257 		 */
1258 		__devlink_port_type_set(devlink_port, devlink_port->type,
1259 					netdev);
1260 		break;
1261 	case NETDEV_UNREGISTER:
1262 		if (devlink_net(devlink) != dev_net(netdev))
1263 			return NOTIFY_OK;
1264 		/* Clear netdev pointer, but not the type. This event happens
1265 		 * also during net namespace change so we need to clear
1266 		 * pointer to netdev that is going to another net namespace.
1267 		 */
1268 		__devlink_port_type_set(devlink_port, devlink_port->type,
1269 					NULL);
1270 		break;
1271 	case NETDEV_PRE_UNINIT:
1272 		/* Clear the type and the netdev pointer. Happens one during
1273 		 * netdevice unregister.
1274 		 */
1275 		__devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
1276 					NULL);
1277 		break;
1278 	}
1279 
1280 	return NOTIFY_OK;
1281 }
1282 
1283 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
1284 				    enum devlink_port_flavour flavour)
1285 {
1286 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1287 
1288 	devlink_port->attrs_set = true;
1289 	attrs->flavour = flavour;
1290 	if (attrs->switch_id.id_len) {
1291 		devlink_port->switch_port = true;
1292 		if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
1293 			attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
1294 	} else {
1295 		devlink_port->switch_port = false;
1296 	}
1297 	return 0;
1298 }
1299 
1300 /**
1301  *	devlink_port_attrs_set - Set port attributes
1302  *
1303  *	@devlink_port: devlink port
1304  *	@attrs: devlink port attrs
1305  */
1306 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1307 			    struct devlink_port_attrs *attrs)
1308 {
1309 	int ret;
1310 
1311 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1312 
1313 	devlink_port->attrs = *attrs;
1314 	ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
1315 	if (ret)
1316 		return;
1317 	WARN_ON(attrs->splittable && attrs->split);
1318 }
1319 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
1320 
1321 /**
1322  *	devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
1323  *
1324  *	@devlink_port: devlink port
1325  *	@controller: associated controller number for the devlink port instance
1326  *	@pf: associated PF for the devlink port instance
1327  *	@external: indicates if the port is for an external controller
1328  */
1329 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
1330 				   u16 pf, bool external)
1331 {
1332 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1333 	int ret;
1334 
1335 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1336 
1337 	ret = __devlink_port_attrs_set(devlink_port,
1338 				       DEVLINK_PORT_FLAVOUR_PCI_PF);
1339 	if (ret)
1340 		return;
1341 	attrs->pci_pf.controller = controller;
1342 	attrs->pci_pf.pf = pf;
1343 	attrs->pci_pf.external = external;
1344 }
1345 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
1346 
1347 /**
1348  *	devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
1349  *
1350  *	@devlink_port: devlink port
1351  *	@controller: associated controller number for the devlink port instance
1352  *	@pf: associated PF for the devlink port instance
1353  *	@vf: associated VF of a PF for the devlink port instance
1354  *	@external: indicates if the port is for an external controller
1355  */
1356 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
1357 				   u16 pf, u16 vf, bool external)
1358 {
1359 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1360 	int ret;
1361 
1362 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1363 
1364 	ret = __devlink_port_attrs_set(devlink_port,
1365 				       DEVLINK_PORT_FLAVOUR_PCI_VF);
1366 	if (ret)
1367 		return;
1368 	attrs->pci_vf.controller = controller;
1369 	attrs->pci_vf.pf = pf;
1370 	attrs->pci_vf.vf = vf;
1371 	attrs->pci_vf.external = external;
1372 }
1373 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
1374 
1375 /**
1376  *	devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
1377  *
1378  *	@devlink_port: devlink port
1379  *	@controller: associated controller number for the devlink port instance
1380  *	@pf: associated PF for the devlink port instance
1381  *	@sf: associated SF of a PF for the devlink port instance
1382  *	@external: indicates if the port is for an external controller
1383  */
1384 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
1385 				   u16 pf, u32 sf, bool external)
1386 {
1387 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1388 	int ret;
1389 
1390 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1391 
1392 	ret = __devlink_port_attrs_set(devlink_port,
1393 				       DEVLINK_PORT_FLAVOUR_PCI_SF);
1394 	if (ret)
1395 		return;
1396 	attrs->pci_sf.controller = controller;
1397 	attrs->pci_sf.pf = pf;
1398 	attrs->pci_sf.sf = sf;
1399 	attrs->pci_sf.external = external;
1400 }
1401 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
1402 
1403 static void devlink_port_rel_notify_cb(struct devlink *devlink, u32 port_index)
1404 {
1405 	struct devlink_port *devlink_port;
1406 
1407 	devlink_port = devlink_port_get_by_index(devlink, port_index);
1408 	if (!devlink_port)
1409 		return;
1410 	devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1411 }
1412 
1413 static void devlink_port_rel_cleanup_cb(struct devlink *devlink, u32 port_index,
1414 					u32 rel_index)
1415 {
1416 	struct devlink_port *devlink_port;
1417 
1418 	devlink_port = devlink_port_get_by_index(devlink, port_index);
1419 	if (devlink_port && devlink_port->rel_index == rel_index)
1420 		devlink_port->rel_index = 0;
1421 }
1422 
1423 /**
1424  * devl_port_fn_devlink_set - Attach peer devlink
1425  *			      instance to port function.
1426  * @devlink_port: devlink port
1427  * @fn_devlink: devlink instance to attach
1428  */
1429 int devl_port_fn_devlink_set(struct devlink_port *devlink_port,
1430 			     struct devlink *fn_devlink)
1431 {
1432 	ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
1433 
1434 	if (WARN_ON(devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_SF ||
1435 		    devlink_port->attrs.pci_sf.external))
1436 		return -EINVAL;
1437 
1438 	return devlink_rel_nested_in_add(&devlink_port->rel_index,
1439 					 devlink_port->devlink->index,
1440 					 devlink_port->index,
1441 					 devlink_port_rel_notify_cb,
1442 					 devlink_port_rel_cleanup_cb,
1443 					 fn_devlink);
1444 }
1445 EXPORT_SYMBOL_GPL(devl_port_fn_devlink_set);
1446 
1447 /**
1448  *	devlink_port_linecard_set - Link port with a linecard
1449  *
1450  *	@devlink_port: devlink port
1451  *	@linecard: devlink linecard
1452  */
1453 void devlink_port_linecard_set(struct devlink_port *devlink_port,
1454 			       struct devlink_linecard *linecard)
1455 {
1456 	ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1457 
1458 	devlink_port->linecard = linecard;
1459 }
1460 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
1461 
1462 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
1463 					     char *name, size_t len)
1464 {
1465 	struct devlink_port_attrs *attrs = &devlink_port->attrs;
1466 	int n = 0;
1467 
1468 	if (!devlink_port->attrs_set)
1469 		return -EOPNOTSUPP;
1470 
1471 	switch (attrs->flavour) {
1472 	case DEVLINK_PORT_FLAVOUR_PHYSICAL:
1473 		if (devlink_port->linecard)
1474 			n = snprintf(name, len, "l%u",
1475 				     devlink_linecard_index(devlink_port->linecard));
1476 		if (n < len)
1477 			n += snprintf(name + n, len - n, "p%u",
1478 				      attrs->phys.port_number);
1479 		if (n < len && attrs->split)
1480 			n += snprintf(name + n, len - n, "s%u",
1481 				      attrs->phys.split_subport_number);
1482 		break;
1483 	case DEVLINK_PORT_FLAVOUR_CPU:
1484 	case DEVLINK_PORT_FLAVOUR_DSA:
1485 	case DEVLINK_PORT_FLAVOUR_UNUSED:
1486 		/* As CPU and DSA ports do not have a netdevice associated
1487 		 * case should not ever happen.
1488 		 */
1489 		WARN_ON(1);
1490 		return -EINVAL;
1491 	case DEVLINK_PORT_FLAVOUR_PCI_PF:
1492 		if (attrs->pci_pf.external) {
1493 			n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
1494 			if (n >= len)
1495 				return -EINVAL;
1496 			len -= n;
1497 			name += n;
1498 		}
1499 		n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
1500 		break;
1501 	case DEVLINK_PORT_FLAVOUR_PCI_VF:
1502 		if (attrs->pci_vf.external) {
1503 			n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
1504 			if (n >= len)
1505 				return -EINVAL;
1506 			len -= n;
1507 			name += n;
1508 		}
1509 		n = snprintf(name, len, "pf%uvf%u",
1510 			     attrs->pci_vf.pf, attrs->pci_vf.vf);
1511 		break;
1512 	case DEVLINK_PORT_FLAVOUR_PCI_SF:
1513 		if (attrs->pci_sf.external) {
1514 			n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
1515 			if (n >= len)
1516 				return -EINVAL;
1517 			len -= n;
1518 			name += n;
1519 		}
1520 		n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
1521 			     attrs->pci_sf.sf);
1522 		break;
1523 	case DEVLINK_PORT_FLAVOUR_VIRTUAL:
1524 		return -EOPNOTSUPP;
1525 	}
1526 
1527 	if (n >= len)
1528 		return -EINVAL;
1529 
1530 	return 0;
1531 }
1532 
1533 int devlink_compat_phys_port_name_get(struct net_device *dev,
1534 				      char *name, size_t len)
1535 {
1536 	struct devlink_port *devlink_port;
1537 
1538 	/* RTNL mutex is held here which ensures that devlink_port
1539 	 * instance cannot disappear in the middle. No need to take
1540 	 * any devlink lock as only permanent values are accessed.
1541 	 */
1542 	ASSERT_RTNL();
1543 
1544 	devlink_port = dev->devlink_port;
1545 	if (!devlink_port)
1546 		return -EOPNOTSUPP;
1547 
1548 	return __devlink_port_phys_port_name_get(devlink_port, name, len);
1549 }
1550 
1551 int devlink_compat_switch_id_get(struct net_device *dev,
1552 				 struct netdev_phys_item_id *ppid)
1553 {
1554 	struct devlink_port *devlink_port;
1555 
1556 	/* Caller must hold RTNL mutex or reference to dev, which ensures that
1557 	 * devlink_port instance cannot disappear in the middle. No need to take
1558 	 * any devlink lock as only permanent values are accessed.
1559 	 */
1560 	devlink_port = dev->devlink_port;
1561 	if (!devlink_port || !devlink_port->switch_port)
1562 		return -EOPNOTSUPP;
1563 
1564 	memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
1565 
1566 	return 0;
1567 }
1568