xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision 835fd614)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *		 Frank Pavlic <fpavlic@de.ibm.com>,
6  *		 Thomas Spatzier <tspat@de.ibm.com>,
7  *		 Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9 
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_bridge.h>
21 #include <linux/list.h>
22 #include <linux/hash.h>
23 #include <linux/hashtable.h>
24 #include <net/switchdev.h>
25 #include <asm/chsc.h>
26 #include <asm/css_chars.h>
27 #include <asm/setup.h>
28 #include "qeth_core.h"
29 #include "qeth_l2.h"
30 
31 static int qeth_l2_setdelmac_makerc(struct qeth_card *card, u16 retcode)
32 {
33 	int rc;
34 
35 	if (retcode)
36 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
37 	switch (retcode) {
38 	case IPA_RC_SUCCESS:
39 		rc = 0;
40 		break;
41 	case IPA_RC_L2_UNSUPPORTED_CMD:
42 		rc = -EOPNOTSUPP;
43 		break;
44 	case IPA_RC_L2_ADDR_TABLE_FULL:
45 		rc = -ENOSPC;
46 		break;
47 	case IPA_RC_L2_DUP_MAC:
48 	case IPA_RC_L2_DUP_LAYER3_MAC:
49 		rc = -EADDRINUSE;
50 		break;
51 	case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
52 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
53 		rc = -EADDRNOTAVAIL;
54 		break;
55 	case IPA_RC_L2_MAC_NOT_FOUND:
56 		rc = -ENOENT;
57 		break;
58 	default:
59 		rc = -EIO;
60 		break;
61 	}
62 	return rc;
63 }
64 
65 static int qeth_l2_send_setdelmac_cb(struct qeth_card *card,
66 				     struct qeth_reply *reply,
67 				     unsigned long data)
68 {
69 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
70 
71 	return qeth_l2_setdelmac_makerc(card, cmd->hdr.return_code);
72 }
73 
74 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
75 			   enum qeth_ipa_cmds ipacmd)
76 {
77 	struct qeth_ipa_cmd *cmd;
78 	struct qeth_cmd_buffer *iob;
79 
80 	QETH_CARD_TEXT(card, 2, "L2sdmac");
81 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
82 				 IPA_DATA_SIZEOF(setdelmac));
83 	if (!iob)
84 		return -ENOMEM;
85 	cmd = __ipa_cmd(iob);
86 	cmd->data.setdelmac.mac_length = ETH_ALEN;
87 	ether_addr_copy(cmd->data.setdelmac.mac, mac);
88 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelmac_cb, NULL);
89 }
90 
91 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
92 {
93 	int rc;
94 
95 	QETH_CARD_TEXT(card, 2, "L2Setmac");
96 	rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC);
97 	if (rc == 0) {
98 		dev_info(&card->gdev->dev,
99 			 "MAC address %pM successfully registered\n", mac);
100 	} else {
101 		switch (rc) {
102 		case -EADDRINUSE:
103 			dev_warn(&card->gdev->dev,
104 				"MAC address %pM already exists\n", mac);
105 			break;
106 		case -EADDRNOTAVAIL:
107 			dev_warn(&card->gdev->dev,
108 				"MAC address %pM is not authorized\n", mac);
109 			break;
110 		}
111 	}
112 	return rc;
113 }
114 
115 static int qeth_l2_write_mac(struct qeth_card *card, u8 *mac)
116 {
117 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
118 					IPA_CMD_SETGMAC : IPA_CMD_SETVMAC;
119 	int rc;
120 
121 	QETH_CARD_TEXT(card, 2, "L2Wmac");
122 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
123 	if (rc == -EADDRINUSE)
124 		QETH_DBF_MESSAGE(2, "MAC already registered on device %x\n",
125 				 CARD_DEVID(card));
126 	else if (rc)
127 		QETH_DBF_MESSAGE(2, "Failed to register MAC on device %x: %d\n",
128 				 CARD_DEVID(card), rc);
129 	return rc;
130 }
131 
132 static int qeth_l2_remove_mac(struct qeth_card *card, u8 *mac)
133 {
134 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
135 					IPA_CMD_DELGMAC : IPA_CMD_DELVMAC;
136 	int rc;
137 
138 	QETH_CARD_TEXT(card, 2, "L2Rmac");
139 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
140 	if (rc)
141 		QETH_DBF_MESSAGE(2, "Failed to delete MAC on device %u: %d\n",
142 				 CARD_DEVID(card), rc);
143 	return rc;
144 }
145 
146 static void qeth_l2_drain_rx_mode_cache(struct qeth_card *card)
147 {
148 	struct qeth_mac *mac;
149 	struct hlist_node *tmp;
150 	int i;
151 
152 	hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
153 		hash_del(&mac->hnode);
154 		kfree(mac);
155 	}
156 }
157 
158 static void qeth_l2_fill_header(struct qeth_qdio_out_q *queue,
159 				struct qeth_hdr *hdr, struct sk_buff *skb,
160 				int ipv, unsigned int data_len)
161 {
162 	int cast_type = qeth_get_ether_cast_type(skb);
163 	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
164 
165 	hdr->hdr.l2.pkt_length = data_len;
166 
167 	if (skb_is_gso(skb)) {
168 		hdr->hdr.l2.id = QETH_HEADER_TYPE_L2_TSO;
169 	} else {
170 		hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
171 		if (skb->ip_summed == CHECKSUM_PARTIAL)
172 			qeth_tx_csum(skb, &hdr->hdr.l2.flags[1], ipv);
173 	}
174 
175 	/* set byte byte 3 to casting flags */
176 	if (cast_type == RTN_MULTICAST)
177 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
178 	else if (cast_type == RTN_BROADCAST)
179 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
180 	else
181 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
182 
183 	/* VSWITCH relies on the VLAN
184 	 * information to be present in
185 	 * the QDIO header */
186 	if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
187 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
188 		hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
189 	}
190 }
191 
192 static int qeth_l2_setdelvlan_makerc(struct qeth_card *card, u16 retcode)
193 {
194 	if (retcode)
195 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
196 
197 	switch (retcode) {
198 	case IPA_RC_SUCCESS:
199 		return 0;
200 	case IPA_RC_L2_INVALID_VLAN_ID:
201 		return -EINVAL;
202 	case IPA_RC_L2_DUP_VLAN_ID:
203 		return -EEXIST;
204 	case IPA_RC_L2_VLAN_ID_NOT_FOUND:
205 		return -ENOENT;
206 	case IPA_RC_L2_VLAN_ID_NOT_ALLOWED:
207 		return -EPERM;
208 	default:
209 		return -EIO;
210 	}
211 }
212 
213 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
214 				      struct qeth_reply *reply,
215 				      unsigned long data)
216 {
217 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
218 
219 	QETH_CARD_TEXT(card, 2, "L2sdvcb");
220 	if (cmd->hdr.return_code) {
221 		QETH_DBF_MESSAGE(2, "Error in processing VLAN %u on device %x: %#x.\n",
222 				 cmd->data.setdelvlan.vlan_id,
223 				 CARD_DEVID(card), cmd->hdr.return_code);
224 		QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
225 	}
226 	return qeth_l2_setdelvlan_makerc(card, cmd->hdr.return_code);
227 }
228 
229 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
230 				   enum qeth_ipa_cmds ipacmd)
231 {
232 	struct qeth_ipa_cmd *cmd;
233 	struct qeth_cmd_buffer *iob;
234 
235 	QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
236 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
237 				 IPA_DATA_SIZEOF(setdelvlan));
238 	if (!iob)
239 		return -ENOMEM;
240 	cmd = __ipa_cmd(iob);
241 	cmd->data.setdelvlan.vlan_id = i;
242 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelvlan_cb, NULL);
243 }
244 
245 static int qeth_l2_vlan_rx_add_vid(struct net_device *dev,
246 				   __be16 proto, u16 vid)
247 {
248 	struct qeth_card *card = dev->ml_priv;
249 
250 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
251 	if (!vid)
252 		return 0;
253 
254 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
255 }
256 
257 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev,
258 				    __be16 proto, u16 vid)
259 {
260 	struct qeth_card *card = dev->ml_priv;
261 
262 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
263 	if (!vid)
264 		return 0;
265 
266 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
267 }
268 
269 static void qeth_l2_set_pnso_mode(struct qeth_card *card,
270 				  enum qeth_pnso_mode mode)
271 {
272 	spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
273 	WRITE_ONCE(card->info.pnso_mode, mode);
274 	spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
275 
276 	if (mode == QETH_PNSO_NONE)
277 		drain_workqueue(card->event_wq);
278 }
279 
280 static void qeth_l2_dev2br_fdb_flush(struct qeth_card *card)
281 {
282 	struct switchdev_notifier_fdb_info info;
283 
284 	QETH_CARD_TEXT(card, 2, "fdbflush");
285 
286 	info.addr = NULL;
287 	/* flush all VLANs: */
288 	info.vid = 0;
289 	info.added_by_user = false;
290 	info.offloaded = true;
291 
292 	call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
293 				 card->dev, &info.info, NULL);
294 }
295 
296 static int qeth_l2_request_initial_mac(struct qeth_card *card)
297 {
298 	int rc = 0;
299 
300 	QETH_CARD_TEXT(card, 2, "l2reqmac");
301 
302 	if (MACHINE_IS_VM) {
303 		rc = qeth_vm_request_mac(card);
304 		if (!rc)
305 			goto out;
306 		QETH_DBF_MESSAGE(2, "z/VM MAC Service failed on device %x: %#x\n",
307 				 CARD_DEVID(card), rc);
308 		QETH_CARD_TEXT_(card, 2, "err%04x", rc);
309 		/* fall back to alternative mechanism: */
310 	}
311 
312 	if (!IS_OSN(card)) {
313 		rc = qeth_setadpparms_change_macaddr(card);
314 		if (!rc)
315 			goto out;
316 		QETH_DBF_MESSAGE(2, "READ_MAC Assist failed on device %x: %#x\n",
317 				 CARD_DEVID(card), rc);
318 		QETH_CARD_TEXT_(card, 2, "1err%04x", rc);
319 		/* fall back once more: */
320 	}
321 
322 	/* some devices don't support a custom MAC address: */
323 	if (IS_OSM(card) || IS_OSX(card))
324 		return (rc) ? rc : -EADDRNOTAVAIL;
325 	eth_hw_addr_random(card->dev);
326 
327 out:
328 	QETH_CARD_HEX(card, 2, card->dev->dev_addr, card->dev->addr_len);
329 	return 0;
330 }
331 
332 static void qeth_l2_register_dev_addr(struct qeth_card *card)
333 {
334 	if (!is_valid_ether_addr(card->dev->dev_addr))
335 		qeth_l2_request_initial_mac(card);
336 
337 	if (!IS_OSN(card) && !qeth_l2_send_setmac(card, card->dev->dev_addr))
338 		card->info.dev_addr_is_registered = 1;
339 	else
340 		card->info.dev_addr_is_registered = 0;
341 }
342 
343 static int qeth_l2_validate_addr(struct net_device *dev)
344 {
345 	struct qeth_card *card = dev->ml_priv;
346 
347 	if (card->info.dev_addr_is_registered)
348 		return eth_validate_addr(dev);
349 
350 	QETH_CARD_TEXT(card, 4, "nomacadr");
351 	return -EPERM;
352 }
353 
354 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
355 {
356 	struct sockaddr *addr = p;
357 	struct qeth_card *card = dev->ml_priv;
358 	u8 old_addr[ETH_ALEN];
359 	int rc = 0;
360 
361 	QETH_CARD_TEXT(card, 3, "setmac");
362 
363 	if (IS_OSM(card) || IS_OSX(card)) {
364 		QETH_CARD_TEXT(card, 3, "setmcTYP");
365 		return -EOPNOTSUPP;
366 	}
367 	QETH_CARD_HEX(card, 3, addr->sa_data, ETH_ALEN);
368 	if (!is_valid_ether_addr(addr->sa_data))
369 		return -EADDRNOTAVAIL;
370 
371 	/* don't register the same address twice */
372 	if (ether_addr_equal_64bits(dev->dev_addr, addr->sa_data) &&
373 	    card->info.dev_addr_is_registered)
374 		return 0;
375 
376 	/* add the new address, switch over, drop the old */
377 	rc = qeth_l2_send_setmac(card, addr->sa_data);
378 	if (rc)
379 		return rc;
380 	ether_addr_copy(old_addr, dev->dev_addr);
381 	ether_addr_copy(dev->dev_addr, addr->sa_data);
382 
383 	if (card->info.dev_addr_is_registered)
384 		qeth_l2_remove_mac(card, old_addr);
385 	card->info.dev_addr_is_registered = 1;
386 	return 0;
387 }
388 
389 static void qeth_l2_promisc_to_bridge(struct qeth_card *card, bool enable)
390 {
391 	int role;
392 	int rc;
393 
394 	QETH_CARD_TEXT(card, 3, "pmisc2br");
395 
396 	if (enable) {
397 		if (card->options.sbp.reflect_promisc_primary)
398 			role = QETH_SBP_ROLE_PRIMARY;
399 		else
400 			role = QETH_SBP_ROLE_SECONDARY;
401 	} else
402 		role = QETH_SBP_ROLE_NONE;
403 
404 	rc = qeth_bridgeport_setrole(card, role);
405 	QETH_CARD_TEXT_(card, 2, "bpm%c%04x", enable ? '+' : '-', rc);
406 	if (!rc) {
407 		card->options.sbp.role = role;
408 		card->info.promisc_mode = enable;
409 	}
410 }
411 
412 static void qeth_l2_set_promisc_mode(struct qeth_card *card)
413 {
414 	bool enable = card->dev->flags & IFF_PROMISC;
415 
416 	if (card->info.promisc_mode == enable)
417 		return;
418 
419 	if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) {
420 		qeth_setadp_promisc_mode(card, enable);
421 	} else {
422 		mutex_lock(&card->sbp_lock);
423 		if (card->options.sbp.reflect_promisc)
424 			qeth_l2_promisc_to_bridge(card, enable);
425 		mutex_unlock(&card->sbp_lock);
426 	}
427 }
428 
429 /* New MAC address is added to the hash table and marked to be written on card
430  * only if there is not in the hash table storage already
431  *
432 */
433 static void qeth_l2_add_mac(struct qeth_card *card, struct netdev_hw_addr *ha)
434 {
435 	u32 mac_hash = get_unaligned((u32 *)(&ha->addr[2]));
436 	struct qeth_mac *mac;
437 
438 	hash_for_each_possible(card->rx_mode_addrs, mac, hnode, mac_hash) {
439 		if (ether_addr_equal_64bits(ha->addr, mac->mac_addr)) {
440 			mac->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
441 			return;
442 		}
443 	}
444 
445 	mac = kzalloc(sizeof(struct qeth_mac), GFP_ATOMIC);
446 	if (!mac)
447 		return;
448 
449 	ether_addr_copy(mac->mac_addr, ha->addr);
450 	mac->disp_flag = QETH_DISP_ADDR_ADD;
451 
452 	hash_add(card->rx_mode_addrs, &mac->hnode, mac_hash);
453 }
454 
455 static void qeth_l2_rx_mode_work(struct work_struct *work)
456 {
457 	struct qeth_card *card = container_of(work, struct qeth_card,
458 					      rx_mode_work);
459 	struct net_device *dev = card->dev;
460 	struct netdev_hw_addr *ha;
461 	struct qeth_mac *mac;
462 	struct hlist_node *tmp;
463 	int i;
464 	int rc;
465 
466 	QETH_CARD_TEXT(card, 3, "setmulti");
467 
468 	netif_addr_lock_bh(dev);
469 	netdev_for_each_mc_addr(ha, dev)
470 		qeth_l2_add_mac(card, ha);
471 	netdev_for_each_uc_addr(ha, dev)
472 		qeth_l2_add_mac(card, ha);
473 	netif_addr_unlock_bh(dev);
474 
475 	hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
476 		switch (mac->disp_flag) {
477 		case QETH_DISP_ADDR_DELETE:
478 			qeth_l2_remove_mac(card, mac->mac_addr);
479 			hash_del(&mac->hnode);
480 			kfree(mac);
481 			break;
482 		case QETH_DISP_ADDR_ADD:
483 			rc = qeth_l2_write_mac(card, mac->mac_addr);
484 			if (rc) {
485 				hash_del(&mac->hnode);
486 				kfree(mac);
487 				break;
488 			}
489 			fallthrough;
490 		default:
491 			/* for next call to set_rx_mode(): */
492 			mac->disp_flag = QETH_DISP_ADDR_DELETE;
493 		}
494 	}
495 
496 	qeth_l2_set_promisc_mode(card);
497 }
498 
499 static int qeth_l2_xmit_osn(struct qeth_card *card, struct sk_buff *skb,
500 			    struct qeth_qdio_out_q *queue)
501 {
502 	gfp_t gfp = GFP_ATOMIC | (skb_pfmemalloc(skb) ? __GFP_MEMALLOC : 0);
503 	struct qeth_hdr *hdr = (struct qeth_hdr *)skb->data;
504 	addr_t end = (addr_t)(skb->data + sizeof(*hdr));
505 	addr_t start = (addr_t)skb->data;
506 	unsigned int elements = 0;
507 	unsigned int hd_len = 0;
508 	int rc;
509 
510 	if (skb->protocol == htons(ETH_P_IPV6))
511 		return -EPROTONOSUPPORT;
512 
513 	if (qeth_get_elements_for_range(start, end) > 1) {
514 		/* Misaligned HW header, move it to its own buffer element. */
515 		hdr = kmem_cache_alloc(qeth_core_header_cache, gfp);
516 		if (!hdr)
517 			return -ENOMEM;
518 		hd_len = sizeof(*hdr);
519 		skb_copy_from_linear_data(skb, (char *)hdr, hd_len);
520 		elements++;
521 	}
522 
523 	elements += qeth_count_elements(skb, hd_len);
524 	if (elements > queue->max_elements) {
525 		rc = -E2BIG;
526 		goto out;
527 	}
528 
529 	rc = qeth_do_send_packet(card, queue, skb, hdr, hd_len, hd_len,
530 				 elements);
531 out:
532 	if (rc && hd_len)
533 		kmem_cache_free(qeth_core_header_cache, hdr);
534 	return rc;
535 }
536 
537 static netdev_tx_t qeth_l2_hard_start_xmit(struct sk_buff *skb,
538 					   struct net_device *dev)
539 {
540 	struct qeth_card *card = dev->ml_priv;
541 	u16 txq = skb_get_queue_mapping(skb);
542 	struct qeth_qdio_out_q *queue;
543 	int rc;
544 
545 	if (!skb_is_gso(skb))
546 		qdisc_skb_cb(skb)->pkt_len = skb->len;
547 	if (IS_IQD(card))
548 		txq = qeth_iqd_translate_txq(dev, txq);
549 	queue = card->qdio.out_qs[txq];
550 
551 	if (IS_OSN(card))
552 		rc = qeth_l2_xmit_osn(card, skb, queue);
553 	else
554 		rc = qeth_xmit(card, skb, queue, qeth_get_ip_version(skb),
555 			       qeth_l2_fill_header);
556 
557 	if (!rc)
558 		return NETDEV_TX_OK;
559 
560 	QETH_TXQ_STAT_INC(queue, tx_dropped);
561 	kfree_skb(skb);
562 	return NETDEV_TX_OK;
563 }
564 
565 static u16 qeth_l2_select_queue(struct net_device *dev, struct sk_buff *skb,
566 				struct net_device *sb_dev)
567 {
568 	struct qeth_card *card = dev->ml_priv;
569 
570 	if (IS_IQD(card))
571 		return qeth_iqd_select_queue(dev, skb,
572 					     qeth_get_ether_cast_type(skb),
573 					     sb_dev);
574 	if (qeth_uses_tx_prio_queueing(card))
575 		return qeth_get_priority_queue(card, skb);
576 
577 	return netdev_pick_tx(dev, skb, sb_dev);
578 }
579 
580 static void qeth_l2_set_rx_mode(struct net_device *dev)
581 {
582 	struct qeth_card *card = dev->ml_priv;
583 
584 	schedule_work(&card->rx_mode_work);
585 }
586 
587 /**
588  *	qeth_l2_pnso() - perform network subchannel operation
589  *	@card: qeth_card structure pointer
590  *	@oc: Operation Code
591  *	@cnc: Boolean Change-Notification Control
592  *	@cb: Callback function will be executed for each element
593  *		of the address list
594  *	@priv: Pointer to pass to the callback function.
595  *
596  *	Collects network information in a network address list and calls the
597  *	callback function for every entry in the list. If "change-notification-
598  *	control" is set, further changes in the address list will be reported
599  *	via the IPA command.
600  */
601 static int qeth_l2_pnso(struct qeth_card *card, u8 oc, int cnc,
602 			void (*cb)(void *priv, struct chsc_pnso_naid_l2 *entry),
603 			void *priv)
604 {
605 	struct ccw_device *ddev = CARD_DDEV(card);
606 	struct chsc_pnso_area *rr;
607 	u32 prev_instance = 0;
608 	int isfirstblock = 1;
609 	int i, size, elems;
610 	int rc;
611 
612 	rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
613 	if (rr == NULL)
614 		return -ENOMEM;
615 	do {
616 		QETH_CARD_TEXT(card, 2, "PNSO");
617 		/* on the first iteration, naihdr.resume_token will be zero */
618 		rc = ccw_device_pnso(ddev, rr, oc, rr->naihdr.resume_token,
619 				     cnc);
620 		if (rc)
621 			continue;
622 		if (cb == NULL)
623 			continue;
624 
625 		size = rr->naihdr.naids;
626 		if (size != sizeof(struct chsc_pnso_naid_l2)) {
627 			WARN_ON_ONCE(1);
628 			continue;
629 		}
630 
631 		elems = (rr->response.length - sizeof(struct chsc_header) -
632 			 sizeof(struct chsc_pnso_naihdr)) / size;
633 
634 		if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
635 			/* Inform the caller that they need to scrap */
636 			/* the data that was already reported via cb */
637 			rc = -EAGAIN;
638 			break;
639 		}
640 		isfirstblock = 0;
641 		prev_instance = rr->naihdr.instance;
642 		for (i = 0; i < elems; i++)
643 			(*cb)(priv, &rr->entries[i]);
644 	} while ((rc == -EBUSY) || (!rc && /* list stored */
645 		   /* resume token is non-zero => list incomplete */
646 		   (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
647 
648 	if (rc)
649 		QETH_CARD_TEXT_(card, 2, "PNrp%04x", rr->response.code);
650 
651 	free_page((unsigned long)rr);
652 	return rc;
653 }
654 
655 static bool qeth_is_my_net_if_token(struct qeth_card *card,
656 				    struct net_if_token *token)
657 {
658 	return ((card->info.ddev_devno == token->devnum) &&
659 		(card->info.cssid == token->cssid) &&
660 		(card->info.iid == token->iid) &&
661 		(card->info.ssid == token->ssid) &&
662 		(card->info.chpid == token->chpid) &&
663 		(card->info.chid == token->chid));
664 }
665 
666 /**
667  *	qeth_l2_dev2br_fdb_notify() - update fdb of master bridge
668  *	@card:	qeth_card structure pointer
669  *	@code:	event bitmask: high order bit 0x80 set to
670  *				1 - removal of an object
671  *				0 - addition of an object
672  *			       Object type(s):
673  *				0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC
674  *	@token: "network token" structure identifying 'physical' location
675  *		of the target
676  *	@addr_lnid: structure with MAC address and VLAN ID of the target
677  */
678 static void qeth_l2_dev2br_fdb_notify(struct qeth_card *card, u8 code,
679 				      struct net_if_token *token,
680 				      struct mac_addr_lnid *addr_lnid)
681 {
682 	struct switchdev_notifier_fdb_info info;
683 	u8 ntfy_mac[ETH_ALEN];
684 
685 	ether_addr_copy(ntfy_mac, addr_lnid->mac);
686 	/* Ignore VLAN only changes */
687 	if (!(code & IPA_ADDR_CHANGE_CODE_MACADDR))
688 		return;
689 	/* Ignore mcast entries */
690 	if (is_multicast_ether_addr(ntfy_mac))
691 		return;
692 	/* Ignore my own addresses */
693 	if (qeth_is_my_net_if_token(card, token))
694 		return;
695 
696 	info.addr = ntfy_mac;
697 	/* don't report VLAN IDs */
698 	info.vid = 0;
699 	info.added_by_user = false;
700 	info.offloaded = true;
701 
702 	if (code & IPA_ADDR_CHANGE_CODE_REMOVAL) {
703 		call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
704 					 card->dev, &info.info, NULL);
705 		QETH_CARD_TEXT(card, 4, "andelmac");
706 		QETH_CARD_TEXT_(card, 4,
707 				"mc%012lx", ether_addr_to_u64(ntfy_mac));
708 	} else {
709 		call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
710 					 card->dev, &info.info, NULL);
711 		QETH_CARD_TEXT(card, 4, "anaddmac");
712 		QETH_CARD_TEXT_(card, 4,
713 				"mc%012lx", ether_addr_to_u64(ntfy_mac));
714 	}
715 }
716 
717 static void qeth_l2_dev2br_an_set_cb(void *priv,
718 				     struct chsc_pnso_naid_l2 *entry)
719 {
720 	u8 code = IPA_ADDR_CHANGE_CODE_MACADDR;
721 	struct qeth_card *card = priv;
722 
723 	if (entry->addr_lnid.lnid < VLAN_N_VID)
724 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
725 	qeth_l2_dev2br_fdb_notify(card, code,
726 				  (struct net_if_token *)&entry->nit,
727 				  (struct mac_addr_lnid *)&entry->addr_lnid);
728 }
729 
730 /**
731  *	qeth_l2_dev2br_an_set() -
732  *	Enable or disable 'dev to bridge network address notification'
733  *	@card: qeth_card structure pointer
734  *	@enable: Enable or disable 'dev to bridge network address notification'
735  *
736  *	Returns negative errno-compatible error indication or 0 on success.
737  *
738  *	On enable, emits a series of address notifications for all
739  *	currently registered hosts.
740  *
741  *	Must be called under rtnl_lock
742  */
743 static int qeth_l2_dev2br_an_set(struct qeth_card *card, bool enable)
744 {
745 	int rc;
746 
747 	if (enable) {
748 		QETH_CARD_TEXT(card, 2, "anseton");
749 		rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 1,
750 				  qeth_l2_dev2br_an_set_cb, card);
751 		if (rc == -EAGAIN)
752 			/* address notification enabled, but inconsistent
753 			 * addresses reported -> disable address notification
754 			 */
755 			qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0,
756 				     NULL, NULL);
757 	} else {
758 		QETH_CARD_TEXT(card, 2, "ansetoff");
759 		rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0, NULL, NULL);
760 	}
761 
762 	return rc;
763 }
764 
765 static int qeth_l2_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
766 				  struct net_device *dev, u32 filter_mask,
767 				  int nlflags)
768 {
769 	struct qeth_priv *priv = netdev_priv(dev);
770 	struct qeth_card *card = dev->ml_priv;
771 	u16 mode = BRIDGE_MODE_UNDEF;
772 
773 	/* Do not even show qeth devs that cannot do bridge_setlink */
774 	if (!priv->brport_hw_features || !netif_device_present(dev) ||
775 	    qeth_bridgeport_is_in_use(card))
776 		return -EOPNOTSUPP;
777 
778 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
779 				       mode, priv->brport_features,
780 				       priv->brport_hw_features,
781 				       nlflags, filter_mask, NULL);
782 }
783 
784 static const struct nla_policy qeth_brport_policy[IFLA_BRPORT_MAX + 1] = {
785 	[IFLA_BRPORT_LEARNING_SYNC]	= { .type = NLA_U8 },
786 };
787 
788 /**
789  *	qeth_l2_bridge_setlink() - set bridgeport attributes
790  *	@dev: netdevice
791  *	@nlh: netlink message header
792  *	@flags: bridge flags (here: BRIDGE_FLAGS_SELF)
793  *	@extack: extended ACK report struct
794  *
795  *	Called under rtnl_lock
796  */
797 static int qeth_l2_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
798 				  u16 flags, struct netlink_ext_ack *extack)
799 {
800 	struct qeth_priv *priv = netdev_priv(dev);
801 	struct nlattr *bp_tb[IFLA_BRPORT_MAX + 1];
802 	struct qeth_card *card = dev->ml_priv;
803 	struct nlattr *attr, *nested_attr;
804 	bool enable, has_protinfo = false;
805 	int rem1, rem2;
806 	int rc;
807 
808 	if (!netif_device_present(dev))
809 		return -ENODEV;
810 	if (!(priv->brport_hw_features))
811 		return -EOPNOTSUPP;
812 
813 	nlmsg_for_each_attr(attr, nlh, sizeof(struct ifinfomsg), rem1) {
814 		if (nla_type(attr) == IFLA_PROTINFO) {
815 			rc = nla_parse_nested(bp_tb, IFLA_BRPORT_MAX, attr,
816 					      qeth_brport_policy, extack);
817 			if (rc)
818 				return rc;
819 			has_protinfo = true;
820 		} else if (nla_type(attr) == IFLA_AF_SPEC) {
821 			nla_for_each_nested(nested_attr, attr, rem2) {
822 				if (nla_type(nested_attr) == IFLA_BRIDGE_FLAGS)
823 					continue;
824 				NL_SET_ERR_MSG_ATTR(extack, nested_attr,
825 						    "Unsupported attribute");
826 				return -EINVAL;
827 			}
828 		} else {
829 			NL_SET_ERR_MSG_ATTR(extack, attr, "Unsupported attribute");
830 			return -EINVAL;
831 		}
832 	}
833 	if (!has_protinfo)
834 		return 0;
835 	if (!bp_tb[IFLA_BRPORT_LEARNING_SYNC])
836 		return -EINVAL;
837 	enable = !!nla_get_u8(bp_tb[IFLA_BRPORT_LEARNING_SYNC]);
838 
839 	if (enable == !!(priv->brport_features & BR_LEARNING_SYNC))
840 		return 0;
841 
842 	mutex_lock(&card->sbp_lock);
843 	/* do not change anything if BridgePort is enabled */
844 	if (qeth_bridgeport_is_in_use(card)) {
845 		NL_SET_ERR_MSG(extack, "n/a (BridgePort)");
846 		rc = -EBUSY;
847 	} else if (enable) {
848 		qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
849 		rc = qeth_l2_dev2br_an_set(card, true);
850 		if (rc)
851 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
852 		else
853 			priv->brport_features |= BR_LEARNING_SYNC;
854 	} else {
855 		rc = qeth_l2_dev2br_an_set(card, false);
856 		if (!rc) {
857 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
858 			priv->brport_features ^= BR_LEARNING_SYNC;
859 			qeth_l2_dev2br_fdb_flush(card);
860 		}
861 	}
862 	mutex_unlock(&card->sbp_lock);
863 
864 	return rc;
865 }
866 
867 static const struct net_device_ops qeth_l2_netdev_ops = {
868 	.ndo_open		= qeth_open,
869 	.ndo_stop		= qeth_stop,
870 	.ndo_get_stats64	= qeth_get_stats64,
871 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
872 	.ndo_features_check	= qeth_features_check,
873 	.ndo_select_queue	= qeth_l2_select_queue,
874 	.ndo_validate_addr	= qeth_l2_validate_addr,
875 	.ndo_set_rx_mode	= qeth_l2_set_rx_mode,
876 	.ndo_do_ioctl		= qeth_do_ioctl,
877 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
878 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
879 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
880 	.ndo_tx_timeout		= qeth_tx_timeout,
881 	.ndo_fix_features	= qeth_fix_features,
882 	.ndo_set_features	= qeth_set_features,
883 	.ndo_bridge_getlink	= qeth_l2_bridge_getlink,
884 	.ndo_bridge_setlink	= qeth_l2_bridge_setlink,
885 };
886 
887 static const struct net_device_ops qeth_osn_netdev_ops = {
888 	.ndo_open		= qeth_open,
889 	.ndo_stop		= qeth_stop,
890 	.ndo_get_stats64	= qeth_get_stats64,
891 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
892 	.ndo_validate_addr	= eth_validate_addr,
893 	.ndo_tx_timeout		= qeth_tx_timeout,
894 };
895 
896 static int qeth_l2_setup_netdev(struct qeth_card *card)
897 {
898 	if (IS_OSN(card)) {
899 		card->dev->netdev_ops = &qeth_osn_netdev_ops;
900 		card->dev->flags |= IFF_NOARP;
901 		goto add_napi;
902 	}
903 
904 	card->dev->needed_headroom = sizeof(struct qeth_hdr);
905 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
906 	card->dev->priv_flags |= IFF_UNICAST_FLT;
907 
908 	if (IS_OSM(card)) {
909 		card->dev->features |= NETIF_F_VLAN_CHALLENGED;
910 	} else {
911 		if (!IS_VM_NIC(card))
912 			card->dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
913 		card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
914 	}
915 
916 	if (IS_OSD(card) && !IS_VM_NIC(card)) {
917 		card->dev->features |= NETIF_F_SG;
918 		/* OSA 3S and earlier has no RX/TX support */
919 		if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
920 			card->dev->hw_features |= NETIF_F_IP_CSUM;
921 			card->dev->vlan_features |= NETIF_F_IP_CSUM;
922 		}
923 	}
924 	if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
925 		card->dev->hw_features |= NETIF_F_IPV6_CSUM;
926 		card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
927 	}
928 	if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM) ||
929 	    qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6)) {
930 		card->dev->hw_features |= NETIF_F_RXCSUM;
931 		card->dev->vlan_features |= NETIF_F_RXCSUM;
932 	}
933 	if (qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
934 		card->dev->hw_features |= NETIF_F_TSO;
935 		card->dev->vlan_features |= NETIF_F_TSO;
936 	}
937 	if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
938 		card->dev->hw_features |= NETIF_F_TSO6;
939 		card->dev->vlan_features |= NETIF_F_TSO6;
940 	}
941 
942 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) {
943 		card->dev->needed_headroom = sizeof(struct qeth_hdr_tso);
944 		netif_keep_dst(card->dev);
945 		netif_set_gso_max_size(card->dev,
946 				       PAGE_SIZE * (QDIO_MAX_ELEMENTS_PER_BUFFER - 1));
947 	}
948 
949 add_napi:
950 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
951 	return register_netdev(card->dev);
952 }
953 
954 static void qeth_l2_trace_features(struct qeth_card *card)
955 {
956 	/* Set BridgePort features */
957 	QETH_CARD_TEXT(card, 2, "featuSBP");
958 	QETH_CARD_HEX(card, 2, &card->options.sbp.supported_funcs,
959 		      sizeof(card->options.sbp.supported_funcs));
960 	/* VNIC Characteristics features */
961 	QETH_CARD_TEXT(card, 2, "feaVNICC");
962 	QETH_CARD_HEX(card, 2, &card->options.vnicc.sup_chars,
963 		      sizeof(card->options.vnicc.sup_chars));
964 }
965 
966 static void qeth_l2_setup_bridgeport_attrs(struct qeth_card *card)
967 {
968 	if (!card->options.sbp.reflect_promisc &&
969 	    card->options.sbp.role != QETH_SBP_ROLE_NONE) {
970 		/* Conditional to avoid spurious error messages */
971 		qeth_bridgeport_setrole(card, card->options.sbp.role);
972 		/* Let the callback function refresh the stored role value. */
973 		qeth_bridgeport_query_ports(card, &card->options.sbp.role,
974 					    NULL);
975 	}
976 	if (card->options.sbp.hostnotification) {
977 		if (qeth_bridgeport_an_set(card, 1))
978 			card->options.sbp.hostnotification = 0;
979 	}
980 }
981 
982 /**
983  *	qeth_l2_detect_dev2br_support() -
984  *	Detect whether this card supports 'dev to bridge fdb network address
985  *	change notification' and thus can support the learning_sync bridgeport
986  *	attribute
987  *	@card: qeth_card structure pointer
988  *
989  *	This is a destructive test and must be called before dev2br or
990  *	bridgeport address notification is enabled!
991  */
992 static void qeth_l2_detect_dev2br_support(struct qeth_card *card)
993 {
994 	struct qeth_priv *priv = netdev_priv(card->dev);
995 	bool dev2br_supported;
996 	int rc;
997 
998 	QETH_CARD_TEXT(card, 2, "d2brsup");
999 	if (!IS_IQD(card))
1000 		return;
1001 
1002 	/* dev2br requires valid cssid,iid,chid */
1003 	if (!card->info.ids_valid) {
1004 		dev2br_supported = false;
1005 	} else if (css_general_characteristics.enarf) {
1006 		dev2br_supported = true;
1007 	} else {
1008 		/* Old machines don't have the feature bit:
1009 		 * Probe by testing whether a disable succeeds
1010 		 */
1011 		rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0, NULL, NULL);
1012 		dev2br_supported = !rc;
1013 	}
1014 	QETH_CARD_TEXT_(card, 2, "D2Bsup%02x", dev2br_supported);
1015 
1016 	if (dev2br_supported)
1017 		priv->brport_hw_features |= BR_LEARNING_SYNC;
1018 	else
1019 		priv->brport_hw_features &= ~BR_LEARNING_SYNC;
1020 }
1021 
1022 static void qeth_l2_enable_brport_features(struct qeth_card *card)
1023 {
1024 	struct qeth_priv *priv = netdev_priv(card->dev);
1025 	int rc;
1026 
1027 	if (priv->brport_features & BR_LEARNING_SYNC) {
1028 		if (priv->brport_hw_features & BR_LEARNING_SYNC) {
1029 			qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1030 			rc = qeth_l2_dev2br_an_set(card, true);
1031 			if (rc == -EAGAIN) {
1032 				/* Recoverable error, retry once */
1033 				qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1034 				qeth_l2_dev2br_fdb_flush(card);
1035 				qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1036 				rc = qeth_l2_dev2br_an_set(card, true);
1037 			}
1038 			if (rc) {
1039 				netdev_err(card->dev,
1040 					   "failed to enable bridge learning_sync: %d\n",
1041 					   rc);
1042 				qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1043 				qeth_l2_dev2br_fdb_flush(card);
1044 				priv->brport_features ^= BR_LEARNING_SYNC;
1045 			}
1046 		} else {
1047 			dev_warn(&card->gdev->dev,
1048 				"bridge learning_sync not supported\n");
1049 			priv->brport_features ^= BR_LEARNING_SYNC;
1050 		}
1051 	}
1052 }
1053 
1054 #ifdef CONFIG_QETH_OSN
1055 static void qeth_osn_assist_cb(struct qeth_card *card,
1056 			       struct qeth_cmd_buffer *iob,
1057 			       unsigned int data_length)
1058 {
1059 	qeth_notify_cmd(iob, 0);
1060 	qeth_put_cmd(iob);
1061 }
1062 
1063 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
1064 {
1065 	struct qeth_cmd_buffer *iob;
1066 	struct qeth_card *card;
1067 
1068 	if (data_len < 0)
1069 		return -EINVAL;
1070 	if (!dev)
1071 		return -ENODEV;
1072 	card = dev->ml_priv;
1073 	if (!card)
1074 		return -ENODEV;
1075 	QETH_CARD_TEXT(card, 2, "osnsdmc");
1076 	if (!qeth_card_hw_is_reachable(card))
1077 		return -ENODEV;
1078 
1079 	iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_len, 1,
1080 			     QETH_IPA_TIMEOUT);
1081 	if (!iob)
1082 		return -ENOMEM;
1083 
1084 	qeth_prepare_ipa_cmd(card, iob, (u16) data_len, NULL);
1085 
1086 	memcpy(__ipa_cmd(iob), data, data_len);
1087 	iob->callback = qeth_osn_assist_cb;
1088 	return qeth_send_ipa_cmd(card, iob, NULL, NULL);
1089 }
1090 EXPORT_SYMBOL(qeth_osn_assist);
1091 
1092 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
1093 		  int (*assist_cb)(struct net_device *, void *),
1094 		  int (*data_cb)(struct sk_buff *))
1095 {
1096 	struct qeth_card *card;
1097 	char bus_id[16];
1098 	u16 devno;
1099 
1100 	memcpy(&devno, read_dev_no, 2);
1101 	sprintf(bus_id, "0.0.%04x", devno);
1102 	card = qeth_get_card_by_busid(bus_id);
1103 	if (!card || !IS_OSN(card))
1104 		return -ENODEV;
1105 	*dev = card->dev;
1106 
1107 	QETH_CARD_TEXT(card, 2, "osnreg");
1108 	if ((assist_cb == NULL) || (data_cb == NULL))
1109 		return -EINVAL;
1110 	card->osn_info.assist_cb = assist_cb;
1111 	card->osn_info.data_cb = data_cb;
1112 	return 0;
1113 }
1114 EXPORT_SYMBOL(qeth_osn_register);
1115 
1116 void qeth_osn_deregister(struct net_device *dev)
1117 {
1118 	struct qeth_card *card;
1119 
1120 	if (!dev)
1121 		return;
1122 	card = dev->ml_priv;
1123 	if (!card)
1124 		return;
1125 	QETH_CARD_TEXT(card, 2, "osndereg");
1126 	card->osn_info.assist_cb = NULL;
1127 	card->osn_info.data_cb = NULL;
1128 }
1129 EXPORT_SYMBOL(qeth_osn_deregister);
1130 #endif
1131 
1132 /* SETBRIDGEPORT support, async notifications */
1133 
1134 enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
1135 
1136 /**
1137  * qeth_bridge_emit_host_event() - bridgeport address change notification
1138  * @card:  qeth_card structure pointer, for udev events.
1139  * @evtype:  "normal" register/unregister, or abort, or reset. For abort
1140  *	      and reset token and addr_lnid are unused and may be NULL.
1141  * @code:  event bitmask: high order bit 0x80 value 1 means removal of an
1142  *			  object, 0 - addition of an object.
1143  *			  0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
1144  * @token: "network token" structure identifying physical address of the port.
1145  * @addr_lnid: pointer to structure with MAC address and VLAN ID.
1146  *
1147  * This function is called when registrations and deregistrations are
1148  * reported by the hardware, and also when notifications are enabled -
1149  * for all currently registered addresses.
1150  */
1151 static void qeth_bridge_emit_host_event(struct qeth_card *card,
1152 					enum qeth_an_event_type evtype,
1153 					u8 code,
1154 					struct net_if_token *token,
1155 					struct mac_addr_lnid *addr_lnid)
1156 {
1157 	char str[7][32];
1158 	char *env[8];
1159 	int i = 0;
1160 
1161 	switch (evtype) {
1162 	case anev_reg_unreg:
1163 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
1164 				(code & IPA_ADDR_CHANGE_CODE_REMOVAL)
1165 				? "deregister" : "register");
1166 		env[i] = str[i]; i++;
1167 		if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
1168 			snprintf(str[i], sizeof(str[i]), "VLAN=%d",
1169 				addr_lnid->lnid);
1170 			env[i] = str[i]; i++;
1171 		}
1172 		if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
1173 			snprintf(str[i], sizeof(str[i]), "MAC=%pM",
1174 				addr_lnid->mac);
1175 			env[i] = str[i]; i++;
1176 		}
1177 		snprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
1178 			token->cssid, token->ssid, token->devnum);
1179 		env[i] = str[i]; i++;
1180 		snprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1181 		env[i] = str[i]; i++;
1182 		snprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1183 				token->chpid);
1184 		env[i] = str[i]; i++;
1185 		snprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x", token->chid);
1186 		env[i] = str[i]; i++;
1187 		break;
1188 	case anev_abort:
1189 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1190 		env[i] = str[i]; i++;
1191 		break;
1192 	case anev_reset:
1193 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1194 		env[i] = str[i]; i++;
1195 		break;
1196 	}
1197 	env[i] = NULL;
1198 	kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1199 }
1200 
1201 struct qeth_bridge_state_data {
1202 	struct work_struct worker;
1203 	struct qeth_card *card;
1204 	u8 role;
1205 	u8 state;
1206 };
1207 
1208 static void qeth_bridge_state_change_worker(struct work_struct *work)
1209 {
1210 	struct qeth_bridge_state_data *data =
1211 		container_of(work, struct qeth_bridge_state_data, worker);
1212 	char env_locrem[32];
1213 	char env_role[32];
1214 	char env_state[32];
1215 	char *env[] = {
1216 		env_locrem,
1217 		env_role,
1218 		env_state,
1219 		NULL
1220 	};
1221 
1222 	snprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1223 	snprintf(env_role, sizeof(env_role), "ROLE=%s",
1224 		(data->role == QETH_SBP_ROLE_NONE) ? "none" :
1225 		(data->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1226 		(data->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1227 		"<INVALID>");
1228 	snprintf(env_state, sizeof(env_state), "STATE=%s",
1229 		(data->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1230 		(data->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1231 		(data->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1232 		"<INVALID>");
1233 	kobject_uevent_env(&data->card->gdev->dev.kobj,
1234 				KOBJ_CHANGE, env);
1235 	kfree(data);
1236 }
1237 
1238 static void qeth_bridge_state_change(struct qeth_card *card,
1239 					struct qeth_ipa_cmd *cmd)
1240 {
1241 	struct qeth_sbp_port_data *qports = &cmd->data.sbp.data.port_data;
1242 	struct qeth_bridge_state_data *data;
1243 
1244 	QETH_CARD_TEXT(card, 2, "brstchng");
1245 	if (qports->num_entries == 0) {
1246 		QETH_CARD_TEXT(card, 2, "BPempty");
1247 		return;
1248 	}
1249 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1250 		QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1251 		return;
1252 	}
1253 
1254 	data = kzalloc(sizeof(*data), GFP_ATOMIC);
1255 	if (!data) {
1256 		QETH_CARD_TEXT(card, 2, "BPSalloc");
1257 		return;
1258 	}
1259 	INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1260 	data->card = card;
1261 	/* Information for the local port: */
1262 	data->role = qports->entry[0].role;
1263 	data->state = qports->entry[0].state;
1264 
1265 	queue_work(card->event_wq, &data->worker);
1266 }
1267 
1268 struct qeth_addr_change_data {
1269 	struct delayed_work dwork;
1270 	struct qeth_card *card;
1271 	struct qeth_ipacmd_addr_change ac_event;
1272 };
1273 
1274 static void qeth_l2_dev2br_worker(struct work_struct *work)
1275 {
1276 	struct delayed_work *dwork = to_delayed_work(work);
1277 	struct qeth_addr_change_data *data;
1278 	struct qeth_card *card;
1279 	struct qeth_priv *priv;
1280 	unsigned int i;
1281 	int rc;
1282 
1283 	data = container_of(dwork, struct qeth_addr_change_data, dwork);
1284 	card = data->card;
1285 	priv = netdev_priv(card->dev);
1286 
1287 	QETH_CARD_TEXT(card, 4, "dev2brew");
1288 
1289 	if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1290 		goto free;
1291 
1292 	/* Potential re-config in progress, try again later: */
1293 	if (!rtnl_trylock()) {
1294 		queue_delayed_work(card->event_wq, dwork,
1295 				   msecs_to_jiffies(100));
1296 		return;
1297 	}
1298 	if (!netif_device_present(card->dev))
1299 		goto out_unlock;
1300 
1301 	if (data->ac_event.lost_event_mask) {
1302 		QETH_DBF_MESSAGE(3,
1303 				 "Address change notification overflow on device %x\n",
1304 				 CARD_DEVID(card));
1305 		/* Card fdb and bridge fdb are out of sync, card has stopped
1306 		 * notifications (no need to drain_workqueue). Purge all
1307 		 * 'extern_learn' entries from the parent bridge and restart
1308 		 * the notifications.
1309 		 */
1310 		qeth_l2_dev2br_fdb_flush(card);
1311 		rc = qeth_l2_dev2br_an_set(card, true);
1312 		if (rc) {
1313 			/* TODO: if we want to retry after -EAGAIN, be
1314 			 * aware there could be stale entries in the
1315 			 * workqueue now, that need to be drained.
1316 			 * For now we give up:
1317 			 */
1318 			netdev_err(card->dev,
1319 				   "bridge learning_sync failed to recover: %d\n",
1320 				   rc);
1321 			WRITE_ONCE(card->info.pnso_mode,
1322 				   QETH_PNSO_NONE);
1323 			/* To remove fdb entries reported by an_set: */
1324 			qeth_l2_dev2br_fdb_flush(card);
1325 			priv->brport_features ^= BR_LEARNING_SYNC;
1326 		} else {
1327 			QETH_DBF_MESSAGE(3,
1328 					 "Address Notification resynced on device %x\n",
1329 					 CARD_DEVID(card));
1330 		}
1331 	} else {
1332 		for (i = 0; i < data->ac_event.num_entries; i++) {
1333 			struct qeth_ipacmd_addr_change_entry *entry =
1334 					&data->ac_event.entry[i];
1335 			qeth_l2_dev2br_fdb_notify(card,
1336 						  entry->change_code,
1337 						  &entry->token,
1338 						  &entry->addr_lnid);
1339 		}
1340 	}
1341 
1342 out_unlock:
1343 	rtnl_unlock();
1344 
1345 free:
1346 	kfree(data);
1347 }
1348 
1349 static void qeth_addr_change_event_worker(struct work_struct *work)
1350 {
1351 	struct delayed_work *dwork = to_delayed_work(work);
1352 	struct qeth_addr_change_data *data;
1353 	struct qeth_card *card;
1354 	int i;
1355 
1356 	data = container_of(dwork, struct qeth_addr_change_data, dwork);
1357 	card = data->card;
1358 
1359 	QETH_CARD_TEXT(data->card, 4, "adrchgew");
1360 
1361 	if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1362 		goto free;
1363 
1364 	if (data->ac_event.lost_event_mask) {
1365 		/* Potential re-config in progress, try again later: */
1366 		if (!mutex_trylock(&card->sbp_lock)) {
1367 			queue_delayed_work(card->event_wq, dwork,
1368 					   msecs_to_jiffies(100));
1369 			return;
1370 		}
1371 
1372 		dev_info(&data->card->gdev->dev,
1373 			 "Address change notification stopped on %s (%s)\n",
1374 			 netdev_name(card->dev),
1375 			(data->ac_event.lost_event_mask == 0x01)
1376 			? "Overflow"
1377 			: (data->ac_event.lost_event_mask == 0x02)
1378 			? "Bridge port state change"
1379 			: "Unknown reason");
1380 
1381 		data->card->options.sbp.hostnotification = 0;
1382 		card->info.pnso_mode = QETH_PNSO_NONE;
1383 		mutex_unlock(&data->card->sbp_lock);
1384 		qeth_bridge_emit_host_event(data->card, anev_abort,
1385 					    0, NULL, NULL);
1386 	} else
1387 		for (i = 0; i < data->ac_event.num_entries; i++) {
1388 			struct qeth_ipacmd_addr_change_entry *entry =
1389 					&data->ac_event.entry[i];
1390 			qeth_bridge_emit_host_event(data->card,
1391 						    anev_reg_unreg,
1392 						    entry->change_code,
1393 						    &entry->token,
1394 						    &entry->addr_lnid);
1395 		}
1396 
1397 free:
1398 	kfree(data);
1399 }
1400 
1401 static void qeth_addr_change_event(struct qeth_card *card,
1402 				   struct qeth_ipa_cmd *cmd)
1403 {
1404 	struct qeth_ipacmd_addr_change *hostevs =
1405 		 &cmd->data.addrchange;
1406 	struct qeth_addr_change_data *data;
1407 	int extrasize;
1408 
1409 	if (card->info.pnso_mode == QETH_PNSO_NONE)
1410 		return;
1411 
1412 	QETH_CARD_TEXT(card, 4, "adrchgev");
1413 	if (cmd->hdr.return_code != 0x0000) {
1414 		if (cmd->hdr.return_code == 0x0010) {
1415 			if (hostevs->lost_event_mask == 0x00)
1416 				hostevs->lost_event_mask = 0xff;
1417 		} else {
1418 			QETH_CARD_TEXT_(card, 2, "ACHN%04x",
1419 				cmd->hdr.return_code);
1420 			return;
1421 		}
1422 	}
1423 	extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1424 						hostevs->num_entries;
1425 	data = kzalloc(sizeof(struct qeth_addr_change_data) + extrasize,
1426 		       GFP_ATOMIC);
1427 	if (!data) {
1428 		QETH_CARD_TEXT(card, 2, "ACNalloc");
1429 		return;
1430 	}
1431 	if (card->info.pnso_mode == QETH_PNSO_BRIDGEPORT)
1432 		INIT_DELAYED_WORK(&data->dwork, qeth_addr_change_event_worker);
1433 	else
1434 		INIT_DELAYED_WORK(&data->dwork, qeth_l2_dev2br_worker);
1435 	data->card = card;
1436 	memcpy(&data->ac_event, hostevs,
1437 			sizeof(struct qeth_ipacmd_addr_change) + extrasize);
1438 	queue_delayed_work(card->event_wq, &data->dwork, 0);
1439 }
1440 
1441 /* SETBRIDGEPORT support; sending commands */
1442 
1443 struct _qeth_sbp_cbctl {
1444 	union {
1445 		u32 supported;
1446 		struct {
1447 			enum qeth_sbp_roles *role;
1448 			enum qeth_sbp_states *state;
1449 		} qports;
1450 	} data;
1451 };
1452 
1453 static int qeth_bridgeport_makerc(struct qeth_card *card,
1454 				  struct qeth_ipa_cmd *cmd)
1455 {
1456 	struct qeth_ipacmd_setbridgeport *sbp = &cmd->data.sbp;
1457 	enum qeth_ipa_sbp_cmd setcmd = sbp->hdr.command_code;
1458 	u16 ipa_rc = cmd->hdr.return_code;
1459 	u16 sbp_rc = sbp->hdr.return_code;
1460 	int rc;
1461 
1462 	if (ipa_rc == IPA_RC_SUCCESS && sbp_rc == IPA_RC_SUCCESS)
1463 		return 0;
1464 
1465 	if ((IS_IQD(card) && ipa_rc == IPA_RC_SUCCESS) ||
1466 	    (!IS_IQD(card) && ipa_rc == sbp_rc)) {
1467 		switch (sbp_rc) {
1468 		case IPA_RC_SUCCESS:
1469 			rc = 0;
1470 			break;
1471 		case IPA_RC_L2_UNSUPPORTED_CMD:
1472 		case IPA_RC_UNSUPPORTED_COMMAND:
1473 			rc = -EOPNOTSUPP;
1474 			break;
1475 		case IPA_RC_SBP_OSA_NOT_CONFIGURED:
1476 		case IPA_RC_SBP_IQD_NOT_CONFIGURED:
1477 			rc = -ENODEV; /* maybe not the best code here? */
1478 			dev_err(&card->gdev->dev,
1479 	"The device is not configured as a Bridge Port\n");
1480 			break;
1481 		case IPA_RC_SBP_OSA_OS_MISMATCH:
1482 		case IPA_RC_SBP_IQD_OS_MISMATCH:
1483 			rc = -EPERM;
1484 			dev_err(&card->gdev->dev,
1485 	"A Bridge Port is already configured by a different operating system\n");
1486 			break;
1487 		case IPA_RC_SBP_OSA_ANO_DEV_PRIMARY:
1488 		case IPA_RC_SBP_IQD_ANO_DEV_PRIMARY:
1489 			switch (setcmd) {
1490 			case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1491 				rc = -EEXIST;
1492 				dev_err(&card->gdev->dev,
1493 	"The LAN already has a primary Bridge Port\n");
1494 				break;
1495 			case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1496 				rc = -EBUSY;
1497 				dev_err(&card->gdev->dev,
1498 	"The device is already a primary Bridge Port\n");
1499 				break;
1500 			default:
1501 				rc = -EIO;
1502 			}
1503 			break;
1504 		case IPA_RC_SBP_OSA_CURRENT_SECOND:
1505 		case IPA_RC_SBP_IQD_CURRENT_SECOND:
1506 			rc = -EBUSY;
1507 			dev_err(&card->gdev->dev,
1508 	"The device is already a secondary Bridge Port\n");
1509 			break;
1510 		case IPA_RC_SBP_OSA_LIMIT_SECOND:
1511 		case IPA_RC_SBP_IQD_LIMIT_SECOND:
1512 			rc = -EEXIST;
1513 			dev_err(&card->gdev->dev,
1514 	"The LAN cannot have more secondary Bridge Ports\n");
1515 			break;
1516 		case IPA_RC_SBP_OSA_CURRENT_PRIMARY:
1517 		case IPA_RC_SBP_IQD_CURRENT_PRIMARY:
1518 			rc = -EBUSY;
1519 			dev_err(&card->gdev->dev,
1520 	"The device is already a primary Bridge Port\n");
1521 			break;
1522 		case IPA_RC_SBP_OSA_NOT_AUTHD_BY_ZMAN:
1523 		case IPA_RC_SBP_IQD_NOT_AUTHD_BY_ZMAN:
1524 			rc = -EACCES;
1525 			dev_err(&card->gdev->dev,
1526 	"The device is not authorized to be a Bridge Port\n");
1527 			break;
1528 		default:
1529 			rc = -EIO;
1530 		}
1531 	} else {
1532 		switch (ipa_rc) {
1533 		case IPA_RC_NOTSUPP:
1534 			rc = -EOPNOTSUPP;
1535 			break;
1536 		case IPA_RC_UNSUPPORTED_COMMAND:
1537 			rc = -EOPNOTSUPP;
1538 			break;
1539 		default:
1540 			rc = -EIO;
1541 		}
1542 	}
1543 
1544 	if (rc) {
1545 		QETH_CARD_TEXT_(card, 2, "SBPi%04x", ipa_rc);
1546 		QETH_CARD_TEXT_(card, 2, "SBPc%04x", sbp_rc);
1547 	}
1548 	return rc;
1549 }
1550 
1551 static struct qeth_cmd_buffer *qeth_sbp_build_cmd(struct qeth_card *card,
1552 						  enum qeth_ipa_sbp_cmd sbp_cmd,
1553 						  unsigned int data_length)
1554 {
1555 	enum qeth_ipa_cmds ipa_cmd = IS_IQD(card) ? IPA_CMD_SETBRIDGEPORT_IQD :
1556 						    IPA_CMD_SETBRIDGEPORT_OSA;
1557 	struct qeth_ipacmd_sbp_hdr *hdr;
1558 	struct qeth_cmd_buffer *iob;
1559 
1560 	iob = qeth_ipa_alloc_cmd(card, ipa_cmd, QETH_PROT_NONE,
1561 				 data_length +
1562 				 offsetof(struct qeth_ipacmd_setbridgeport,
1563 					  data));
1564 	if (!iob)
1565 		return iob;
1566 
1567 	hdr = &__ipa_cmd(iob)->data.sbp.hdr;
1568 	hdr->cmdlength = sizeof(*hdr) + data_length;
1569 	hdr->command_code = sbp_cmd;
1570 	hdr->used_total = 1;
1571 	hdr->seq_no = 1;
1572 	return iob;
1573 }
1574 
1575 static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1576 	struct qeth_reply *reply, unsigned long data)
1577 {
1578 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1579 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1580 	int rc;
1581 
1582 	QETH_CARD_TEXT(card, 2, "brqsupcb");
1583 	rc = qeth_bridgeport_makerc(card, cmd);
1584 	if (rc)
1585 		return rc;
1586 
1587 	cbctl->data.supported =
1588 		cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1589 	return 0;
1590 }
1591 
1592 /**
1593  * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1594  * @card:			     qeth_card structure pointer.
1595  *
1596  * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1597  * strucutre: card->options.sbp.supported_funcs.
1598  */
1599 static void qeth_bridgeport_query_support(struct qeth_card *card)
1600 {
1601 	struct qeth_cmd_buffer *iob;
1602 	struct _qeth_sbp_cbctl cbctl;
1603 
1604 	QETH_CARD_TEXT(card, 2, "brqsuppo");
1605 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_COMMANDS_SUPPORTED,
1606 				 SBP_DATA_SIZEOF(query_cmds_supp));
1607 	if (!iob)
1608 		return;
1609 
1610 	if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1611 			      &cbctl)) {
1612 		card->options.sbp.role = QETH_SBP_ROLE_NONE;
1613 		card->options.sbp.supported_funcs = 0;
1614 		return;
1615 	}
1616 	card->options.sbp.supported_funcs = cbctl.data.supported;
1617 }
1618 
1619 static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1620 	struct qeth_reply *reply, unsigned long data)
1621 {
1622 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1623 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1624 	struct qeth_sbp_port_data *qports;
1625 	int rc;
1626 
1627 	QETH_CARD_TEXT(card, 2, "brqprtcb");
1628 	rc = qeth_bridgeport_makerc(card, cmd);
1629 	if (rc)
1630 		return rc;
1631 
1632 	qports = &cmd->data.sbp.data.port_data;
1633 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1634 		QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1635 		return -EINVAL;
1636 	}
1637 	/* first entry contains the state of the local port */
1638 	if (qports->num_entries > 0) {
1639 		if (cbctl->data.qports.role)
1640 			*cbctl->data.qports.role = qports->entry[0].role;
1641 		if (cbctl->data.qports.state)
1642 			*cbctl->data.qports.state = qports->entry[0].state;
1643 	}
1644 	return 0;
1645 }
1646 
1647 /**
1648  * qeth_bridgeport_query_ports() - query local bridgeport status.
1649  * @card:			   qeth_card structure pointer.
1650  * @role:   Role of the port: 0-none, 1-primary, 2-secondary.
1651  * @state:  State of the port: 0-inactive, 1-standby, 2-active.
1652  *
1653  * Returns negative errno-compatible error indication or 0 on success.
1654  *
1655  * 'role' and 'state' are not updated in case of hardware operation failure.
1656  */
1657 int qeth_bridgeport_query_ports(struct qeth_card *card,
1658 	enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1659 {
1660 	struct qeth_cmd_buffer *iob;
1661 	struct _qeth_sbp_cbctl cbctl = {
1662 		.data = {
1663 			.qports = {
1664 				.role = role,
1665 				.state = state,
1666 			},
1667 		},
1668 	};
1669 
1670 	QETH_CARD_TEXT(card, 2, "brqports");
1671 	if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1672 		return -EOPNOTSUPP;
1673 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_BRIDGE_PORTS, 0);
1674 	if (!iob)
1675 		return -ENOMEM;
1676 
1677 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1678 				 &cbctl);
1679 }
1680 
1681 static int qeth_bridgeport_set_cb(struct qeth_card *card,
1682 	struct qeth_reply *reply, unsigned long data)
1683 {
1684 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1685 
1686 	QETH_CARD_TEXT(card, 2, "brsetrcb");
1687 	return qeth_bridgeport_makerc(card, cmd);
1688 }
1689 
1690 /**
1691  * qeth_bridgeport_setrole() - Assign primary role to the port.
1692  * @card:		       qeth_card structure pointer.
1693  * @role:		       Role to assign.
1694  *
1695  * Returns negative errno-compatible error indication or 0 on success.
1696  */
1697 int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
1698 {
1699 	struct qeth_cmd_buffer *iob;
1700 	enum qeth_ipa_sbp_cmd setcmd;
1701 	unsigned int cmdlength = 0;
1702 
1703 	QETH_CARD_TEXT(card, 2, "brsetrol");
1704 	switch (role) {
1705 	case QETH_SBP_ROLE_NONE:
1706 		setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
1707 		break;
1708 	case QETH_SBP_ROLE_PRIMARY:
1709 		setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
1710 		cmdlength = SBP_DATA_SIZEOF(set_primary);
1711 		break;
1712 	case QETH_SBP_ROLE_SECONDARY:
1713 		setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
1714 		break;
1715 	default:
1716 		return -EINVAL;
1717 	}
1718 	if (!(card->options.sbp.supported_funcs & setcmd))
1719 		return -EOPNOTSUPP;
1720 	iob = qeth_sbp_build_cmd(card, setcmd, cmdlength);
1721 	if (!iob)
1722 		return -ENOMEM;
1723 
1724 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb, NULL);
1725 }
1726 
1727 static void qeth_bridgeport_an_set_cb(void *priv,
1728 				      struct chsc_pnso_naid_l2 *entry)
1729 {
1730 	struct qeth_card *card = (struct qeth_card *)priv;
1731 	u8 code;
1732 
1733 	code = IPA_ADDR_CHANGE_CODE_MACADDR;
1734 	if (entry->addr_lnid.lnid < VLAN_N_VID)
1735 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
1736 	qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
1737 				    (struct net_if_token *)&entry->nit,
1738 				    (struct mac_addr_lnid *)&entry->addr_lnid);
1739 }
1740 
1741 /**
1742  * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
1743  * @card:		      qeth_card structure pointer.
1744  * @enable:		      0 - disable, non-zero - enable notifications
1745  *
1746  * Returns negative errno-compatible error indication or 0 on success.
1747  *
1748  * On enable, emits a series of address notifications udev events for all
1749  * currently registered hosts.
1750  */
1751 int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
1752 {
1753 	int rc;
1754 
1755 	if (!card->options.sbp.supported_funcs)
1756 		return -EOPNOTSUPP;
1757 
1758 	if (enable) {
1759 		qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
1760 		qeth_l2_set_pnso_mode(card, QETH_PNSO_BRIDGEPORT);
1761 		rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 1,
1762 				  qeth_bridgeport_an_set_cb, card);
1763 		if (rc)
1764 			qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1765 	} else {
1766 		rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 0, NULL, NULL);
1767 		qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1768 	}
1769 	return rc;
1770 }
1771 
1772 /* VNIC Characteristics support */
1773 
1774 /* handle VNICC IPA command return codes; convert to error codes */
1775 static int qeth_l2_vnicc_makerc(struct qeth_card *card, u16 ipa_rc)
1776 {
1777 	int rc;
1778 
1779 	switch (ipa_rc) {
1780 	case IPA_RC_SUCCESS:
1781 		return ipa_rc;
1782 	case IPA_RC_L2_UNSUPPORTED_CMD:
1783 	case IPA_RC_NOTSUPP:
1784 		rc = -EOPNOTSUPP;
1785 		break;
1786 	case IPA_RC_VNICC_OOSEQ:
1787 		rc = -EALREADY;
1788 		break;
1789 	case IPA_RC_VNICC_VNICBP:
1790 		rc = -EBUSY;
1791 		break;
1792 	case IPA_RC_L2_ADDR_TABLE_FULL:
1793 		rc = -ENOSPC;
1794 		break;
1795 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
1796 		rc = -EACCES;
1797 		break;
1798 	default:
1799 		rc = -EIO;
1800 	}
1801 
1802 	QETH_CARD_TEXT_(card, 2, "err%04x", ipa_rc);
1803 	return rc;
1804 }
1805 
1806 /* generic VNICC request call back */
1807 static int qeth_l2_vnicc_request_cb(struct qeth_card *card,
1808 				    struct qeth_reply *reply,
1809 				    unsigned long data)
1810 {
1811 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1812 	struct qeth_ipacmd_vnicc *rep = &cmd->data.vnicc;
1813 	u32 sub_cmd = cmd->data.vnicc.hdr.sub_command;
1814 
1815 	QETH_CARD_TEXT(card, 2, "vniccrcb");
1816 	if (cmd->hdr.return_code)
1817 		return qeth_l2_vnicc_makerc(card, cmd->hdr.return_code);
1818 	/* return results to caller */
1819 	card->options.vnicc.sup_chars = rep->vnicc_cmds.supported;
1820 	card->options.vnicc.cur_chars = rep->vnicc_cmds.enabled;
1821 
1822 	if (sub_cmd == IPA_VNICC_QUERY_CMDS)
1823 		*(u32 *)reply->param = rep->data.query_cmds.sup_cmds;
1824 	else if (sub_cmd == IPA_VNICC_GET_TIMEOUT)
1825 		*(u32 *)reply->param = rep->data.getset_timeout.timeout;
1826 
1827 	return 0;
1828 }
1829 
1830 static struct qeth_cmd_buffer *qeth_l2_vnicc_build_cmd(struct qeth_card *card,
1831 						       u32 vnicc_cmd,
1832 						       unsigned int data_length)
1833 {
1834 	struct qeth_ipacmd_vnicc_hdr *hdr;
1835 	struct qeth_cmd_buffer *iob;
1836 
1837 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_VNICC, QETH_PROT_NONE,
1838 				 data_length +
1839 				 offsetof(struct qeth_ipacmd_vnicc, data));
1840 	if (!iob)
1841 		return NULL;
1842 
1843 	hdr = &__ipa_cmd(iob)->data.vnicc.hdr;
1844 	hdr->data_length = sizeof(*hdr) + data_length;
1845 	hdr->sub_command = vnicc_cmd;
1846 	return iob;
1847 }
1848 
1849 /* VNICC query VNIC characteristics request */
1850 static int qeth_l2_vnicc_query_chars(struct qeth_card *card)
1851 {
1852 	struct qeth_cmd_buffer *iob;
1853 
1854 	QETH_CARD_TEXT(card, 2, "vniccqch");
1855 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CHARS, 0);
1856 	if (!iob)
1857 		return -ENOMEM;
1858 
1859 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1860 }
1861 
1862 /* VNICC query sub commands request */
1863 static int qeth_l2_vnicc_query_cmds(struct qeth_card *card, u32 vnic_char,
1864 				    u32 *sup_cmds)
1865 {
1866 	struct qeth_cmd_buffer *iob;
1867 
1868 	QETH_CARD_TEXT(card, 2, "vniccqcm");
1869 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CMDS,
1870 				      VNICC_DATA_SIZEOF(query_cmds));
1871 	if (!iob)
1872 		return -ENOMEM;
1873 
1874 	__ipa_cmd(iob)->data.vnicc.data.query_cmds.vnic_char = vnic_char;
1875 
1876 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, sup_cmds);
1877 }
1878 
1879 /* VNICC enable/disable characteristic request */
1880 static int qeth_l2_vnicc_set_char(struct qeth_card *card, u32 vnic_char,
1881 				      u32 cmd)
1882 {
1883 	struct qeth_cmd_buffer *iob;
1884 
1885 	QETH_CARD_TEXT(card, 2, "vniccedc");
1886 	iob = qeth_l2_vnicc_build_cmd(card, cmd, VNICC_DATA_SIZEOF(set_char));
1887 	if (!iob)
1888 		return -ENOMEM;
1889 
1890 	__ipa_cmd(iob)->data.vnicc.data.set_char.vnic_char = vnic_char;
1891 
1892 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1893 }
1894 
1895 /* VNICC get/set timeout for characteristic request */
1896 static int qeth_l2_vnicc_getset_timeout(struct qeth_card *card, u32 vnicc,
1897 					u32 cmd, u32 *timeout)
1898 {
1899 	struct qeth_vnicc_getset_timeout *getset_timeout;
1900 	struct qeth_cmd_buffer *iob;
1901 
1902 	QETH_CARD_TEXT(card, 2, "vniccgst");
1903 	iob = qeth_l2_vnicc_build_cmd(card, cmd,
1904 				      VNICC_DATA_SIZEOF(getset_timeout));
1905 	if (!iob)
1906 		return -ENOMEM;
1907 
1908 	getset_timeout = &__ipa_cmd(iob)->data.vnicc.data.getset_timeout;
1909 	getset_timeout->vnic_char = vnicc;
1910 
1911 	if (cmd == IPA_VNICC_SET_TIMEOUT)
1912 		getset_timeout->timeout = *timeout;
1913 
1914 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, timeout);
1915 }
1916 
1917 /* recover user timeout setting */
1918 static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
1919 					  u32 *timeout)
1920 {
1921 	if (card->options.vnicc.sup_chars & vnicc &&
1922 	    card->options.vnicc.getset_timeout_sup & vnicc &&
1923 	    !qeth_l2_vnicc_getset_timeout(card, vnicc, IPA_VNICC_SET_TIMEOUT,
1924 					  timeout))
1925 		return false;
1926 	*timeout = QETH_VNICC_DEFAULT_TIMEOUT;
1927 	return true;
1928 }
1929 
1930 /* set current VNICC flag state; called from sysfs store function */
1931 int qeth_l2_vnicc_set_state(struct qeth_card *card, u32 vnicc, bool state)
1932 {
1933 	int rc = 0;
1934 	u32 cmd;
1935 
1936 	QETH_CARD_TEXT(card, 2, "vniccsch");
1937 
1938 	/* check if characteristic and enable/disable are supported */
1939 	if (!(card->options.vnicc.sup_chars & vnicc) ||
1940 	    !(card->options.vnicc.set_char_sup & vnicc))
1941 		return -EOPNOTSUPP;
1942 
1943 	if (qeth_bridgeport_is_in_use(card))
1944 		return -EBUSY;
1945 
1946 	/* set enable/disable command and store wanted characteristic */
1947 	if (state) {
1948 		cmd = IPA_VNICC_ENABLE;
1949 		card->options.vnicc.wanted_chars |= vnicc;
1950 	} else {
1951 		cmd = IPA_VNICC_DISABLE;
1952 		card->options.vnicc.wanted_chars &= ~vnicc;
1953 	}
1954 
1955 	/* do we need to do anything? */
1956 	if (card->options.vnicc.cur_chars == card->options.vnicc.wanted_chars)
1957 		return rc;
1958 
1959 	/* if card is not ready, simply stop here */
1960 	if (!qeth_card_hw_is_reachable(card)) {
1961 		if (state)
1962 			card->options.vnicc.cur_chars |= vnicc;
1963 		else
1964 			card->options.vnicc.cur_chars &= ~vnicc;
1965 		return rc;
1966 	}
1967 
1968 	rc = qeth_l2_vnicc_set_char(card, vnicc, cmd);
1969 	if (rc)
1970 		card->options.vnicc.wanted_chars =
1971 			card->options.vnicc.cur_chars;
1972 	else {
1973 		/* successful online VNICC change; handle special cases */
1974 		if (state && vnicc == QETH_VNICC_RX_BCAST)
1975 			card->options.vnicc.rx_bcast_enabled = true;
1976 		if (!state && vnicc == QETH_VNICC_LEARNING)
1977 			qeth_l2_vnicc_recover_timeout(card, vnicc,
1978 					&card->options.vnicc.learning_timeout);
1979 	}
1980 
1981 	return rc;
1982 }
1983 
1984 /* get current VNICC flag state; called from sysfs show function */
1985 int qeth_l2_vnicc_get_state(struct qeth_card *card, u32 vnicc, bool *state)
1986 {
1987 	int rc = 0;
1988 
1989 	QETH_CARD_TEXT(card, 2, "vniccgch");
1990 
1991 	/* check if characteristic is supported */
1992 	if (!(card->options.vnicc.sup_chars & vnicc))
1993 		return -EOPNOTSUPP;
1994 
1995 	if (qeth_bridgeport_is_in_use(card))
1996 		return -EBUSY;
1997 
1998 	/* if card is ready, query current VNICC state */
1999 	if (qeth_card_hw_is_reachable(card))
2000 		rc = qeth_l2_vnicc_query_chars(card);
2001 
2002 	*state = (card->options.vnicc.cur_chars & vnicc) ? true : false;
2003 	return rc;
2004 }
2005 
2006 /* set VNICC timeout; called from sysfs store function. Currently, only learning
2007  * supports timeout
2008  */
2009 int qeth_l2_vnicc_set_timeout(struct qeth_card *card, u32 timeout)
2010 {
2011 	int rc = 0;
2012 
2013 	QETH_CARD_TEXT(card, 2, "vniccsto");
2014 
2015 	/* check if characteristic and set_timeout are supported */
2016 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2017 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2018 		return -EOPNOTSUPP;
2019 
2020 	if (qeth_bridgeport_is_in_use(card))
2021 		return -EBUSY;
2022 
2023 	/* do we need to do anything? */
2024 	if (card->options.vnicc.learning_timeout == timeout)
2025 		return rc;
2026 
2027 	/* if card is not ready, simply store the value internally and return */
2028 	if (!qeth_card_hw_is_reachable(card)) {
2029 		card->options.vnicc.learning_timeout = timeout;
2030 		return rc;
2031 	}
2032 
2033 	/* send timeout value to card; if successful, store value internally */
2034 	rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2035 					  IPA_VNICC_SET_TIMEOUT, &timeout);
2036 	if (!rc)
2037 		card->options.vnicc.learning_timeout = timeout;
2038 
2039 	return rc;
2040 }
2041 
2042 /* get current VNICC timeout; called from sysfs show function. Currently, only
2043  * learning supports timeout
2044  */
2045 int qeth_l2_vnicc_get_timeout(struct qeth_card *card, u32 *timeout)
2046 {
2047 	int rc = 0;
2048 
2049 	QETH_CARD_TEXT(card, 2, "vniccgto");
2050 
2051 	/* check if characteristic and get_timeout are supported */
2052 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2053 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2054 		return -EOPNOTSUPP;
2055 
2056 	if (qeth_bridgeport_is_in_use(card))
2057 		return -EBUSY;
2058 
2059 	/* if card is ready, get timeout. Otherwise, just return stored value */
2060 	*timeout = card->options.vnicc.learning_timeout;
2061 	if (qeth_card_hw_is_reachable(card))
2062 		rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2063 						  IPA_VNICC_GET_TIMEOUT,
2064 						  timeout);
2065 
2066 	return rc;
2067 }
2068 
2069 /* check if VNICC is currently enabled */
2070 static bool _qeth_l2_vnicc_is_in_use(struct qeth_card *card)
2071 {
2072 	if (!card->options.vnicc.sup_chars)
2073 		return false;
2074 	/* default values are only OK if rx_bcast was not enabled by user
2075 	 * or the card is offline.
2076 	 */
2077 	if (card->options.vnicc.cur_chars == QETH_VNICC_DEFAULT) {
2078 		if (!card->options.vnicc.rx_bcast_enabled ||
2079 		    !qeth_card_hw_is_reachable(card))
2080 			return false;
2081 	}
2082 	return true;
2083 }
2084 
2085 /**
2086  *	qeth_bridgeport_allowed - are any qeth_bridgeport functions allowed?
2087  *	@card: qeth_card structure pointer
2088  *
2089  *	qeth_bridgeport functionality is mutually exclusive with usage of the
2090  *	VNIC Characteristics and dev2br address notifications
2091  */
2092 bool qeth_bridgeport_allowed(struct qeth_card *card)
2093 {
2094 	struct qeth_priv *priv = netdev_priv(card->dev);
2095 
2096 	return (!_qeth_l2_vnicc_is_in_use(card) &&
2097 		!(priv->brport_features & BR_LEARNING_SYNC));
2098 }
2099 
2100 /* recover user characteristic setting */
2101 static bool qeth_l2_vnicc_recover_char(struct qeth_card *card, u32 vnicc,
2102 				       bool enable)
2103 {
2104 	u32 cmd = enable ? IPA_VNICC_ENABLE : IPA_VNICC_DISABLE;
2105 
2106 	if (card->options.vnicc.sup_chars & vnicc &&
2107 	    card->options.vnicc.set_char_sup & vnicc &&
2108 	    !qeth_l2_vnicc_set_char(card, vnicc, cmd))
2109 		return false;
2110 	card->options.vnicc.wanted_chars &= ~vnicc;
2111 	card->options.vnicc.wanted_chars |= QETH_VNICC_DEFAULT & vnicc;
2112 	return true;
2113 }
2114 
2115 /* (re-)initialize VNICC */
2116 static void qeth_l2_vnicc_init(struct qeth_card *card)
2117 {
2118 	u32 *timeout = &card->options.vnicc.learning_timeout;
2119 	bool enable, error = false;
2120 	unsigned int chars_len, i;
2121 	unsigned long chars_tmp;
2122 	u32 sup_cmds, vnicc;
2123 
2124 	QETH_CARD_TEXT(card, 2, "vniccini");
2125 	/* reset rx_bcast */
2126 	card->options.vnicc.rx_bcast_enabled = 0;
2127 	/* initial query and storage of VNIC characteristics */
2128 	if (qeth_l2_vnicc_query_chars(card)) {
2129 		if (card->options.vnicc.wanted_chars != QETH_VNICC_DEFAULT ||
2130 		    *timeout != QETH_VNICC_DEFAULT_TIMEOUT)
2131 			dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2132 		/* fail quietly if user didn't change the default config */
2133 		card->options.vnicc.sup_chars = 0;
2134 		card->options.vnicc.cur_chars = 0;
2135 		card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2136 		return;
2137 	}
2138 	/* get supported commands for each supported characteristic */
2139 	chars_tmp = card->options.vnicc.sup_chars;
2140 	chars_len = sizeof(card->options.vnicc.sup_chars) * BITS_PER_BYTE;
2141 	for_each_set_bit(i, &chars_tmp, chars_len) {
2142 		vnicc = BIT(i);
2143 		if (qeth_l2_vnicc_query_cmds(card, vnicc, &sup_cmds)) {
2144 			sup_cmds = 0;
2145 			error = true;
2146 		}
2147 		if ((sup_cmds & IPA_VNICC_SET_TIMEOUT) &&
2148 		    (sup_cmds & IPA_VNICC_GET_TIMEOUT))
2149 			card->options.vnicc.getset_timeout_sup |= vnicc;
2150 		else
2151 			card->options.vnicc.getset_timeout_sup &= ~vnicc;
2152 		if ((sup_cmds & IPA_VNICC_ENABLE) &&
2153 		    (sup_cmds & IPA_VNICC_DISABLE))
2154 			card->options.vnicc.set_char_sup |= vnicc;
2155 		else
2156 			card->options.vnicc.set_char_sup &= ~vnicc;
2157 	}
2158 	/* enforce assumed default values and recover settings, if changed  */
2159 	error |= qeth_l2_vnicc_recover_timeout(card, QETH_VNICC_LEARNING,
2160 					       timeout);
2161 	/* Change chars, if necessary  */
2162 	chars_tmp = card->options.vnicc.wanted_chars ^
2163 		    card->options.vnicc.cur_chars;
2164 	chars_len = sizeof(card->options.vnicc.wanted_chars) * BITS_PER_BYTE;
2165 	for_each_set_bit(i, &chars_tmp, chars_len) {
2166 		vnicc = BIT(i);
2167 		enable = card->options.vnicc.wanted_chars & vnicc;
2168 		error |= qeth_l2_vnicc_recover_char(card, vnicc, enable);
2169 	}
2170 	if (error)
2171 		dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2172 }
2173 
2174 /* configure default values of VNIC characteristics */
2175 static void qeth_l2_vnicc_set_defaults(struct qeth_card *card)
2176 {
2177 	/* characteristics values */
2178 	card->options.vnicc.sup_chars = QETH_VNICC_ALL;
2179 	card->options.vnicc.cur_chars = QETH_VNICC_DEFAULT;
2180 	card->options.vnicc.learning_timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2181 	/* supported commands */
2182 	card->options.vnicc.set_char_sup = QETH_VNICC_ALL;
2183 	card->options.vnicc.getset_timeout_sup = QETH_VNICC_LEARNING;
2184 	/* settings wanted by users */
2185 	card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2186 }
2187 
2188 static const struct device_type qeth_l2_devtype = {
2189 	.name = "qeth_layer2",
2190 	.groups = qeth_l2_attr_groups,
2191 };
2192 
2193 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
2194 {
2195 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2196 	int rc;
2197 
2198 	if (IS_OSN(card))
2199 		dev_notice(&gdev->dev, "OSN support will be dropped in 2021\n");
2200 
2201 	qeth_l2_vnicc_set_defaults(card);
2202 	mutex_init(&card->sbp_lock);
2203 
2204 	if (gdev->dev.type == &qeth_generic_devtype) {
2205 		rc = qeth_l2_create_device_attributes(&gdev->dev);
2206 		if (rc)
2207 			return rc;
2208 	}
2209 
2210 	INIT_WORK(&card->rx_mode_work, qeth_l2_rx_mode_work);
2211 	return 0;
2212 }
2213 
2214 static void qeth_l2_remove_device(struct ccwgroup_device *gdev)
2215 {
2216 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2217 
2218 	if (gdev->dev.type == &qeth_generic_devtype)
2219 		qeth_l2_remove_device_attributes(&gdev->dev);
2220 	qeth_set_allowed_threads(card, 0, 1);
2221 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2222 
2223 	if (gdev->state == CCWGROUP_ONLINE)
2224 		qeth_set_offline(card, false);
2225 
2226 	cancel_work_sync(&card->close_dev_work);
2227 	if (card->dev->reg_state == NETREG_REGISTERED)
2228 		unregister_netdev(card->dev);
2229 }
2230 
2231 static int qeth_l2_set_online(struct qeth_card *card, bool carrier_ok)
2232 {
2233 	struct net_device *dev = card->dev;
2234 	int rc = 0;
2235 
2236 	/* query before bridgeport_notification may be enabled */
2237 	qeth_l2_detect_dev2br_support(card);
2238 
2239 	mutex_lock(&card->sbp_lock);
2240 	qeth_bridgeport_query_support(card);
2241 	if (card->options.sbp.supported_funcs) {
2242 		qeth_l2_setup_bridgeport_attrs(card);
2243 		dev_info(&card->gdev->dev,
2244 			 "The device represents a Bridge Capable Port\n");
2245 	}
2246 	mutex_unlock(&card->sbp_lock);
2247 
2248 	qeth_l2_register_dev_addr(card);
2249 
2250 	/* for the rx_bcast characteristic, init VNICC after setmac */
2251 	qeth_l2_vnicc_init(card);
2252 
2253 	qeth_l2_trace_features(card);
2254 
2255 	/* softsetup */
2256 	QETH_CARD_TEXT(card, 2, "softsetp");
2257 
2258 	card->state = CARD_STATE_SOFTSETUP;
2259 
2260 	qeth_set_allowed_threads(card, 0xffffffff, 0);
2261 
2262 	if (dev->reg_state != NETREG_REGISTERED) {
2263 		rc = qeth_l2_setup_netdev(card);
2264 		if (rc)
2265 			goto err_setup;
2266 
2267 		if (carrier_ok)
2268 			netif_carrier_on(dev);
2269 	} else {
2270 		rtnl_lock();
2271 		rc = qeth_set_real_num_tx_queues(card,
2272 						 qeth_tx_actual_queues(card));
2273 		if (rc) {
2274 			rtnl_unlock();
2275 			goto err_set_queues;
2276 		}
2277 
2278 		if (carrier_ok)
2279 			netif_carrier_on(dev);
2280 		else
2281 			netif_carrier_off(dev);
2282 
2283 		netif_device_attach(dev);
2284 		qeth_enable_hw_features(dev);
2285 		qeth_l2_enable_brport_features(card);
2286 
2287 		if (card->info.open_when_online) {
2288 			card->info.open_when_online = 0;
2289 			dev_open(dev, NULL);
2290 		}
2291 		rtnl_unlock();
2292 	}
2293 	return 0;
2294 
2295 err_set_queues:
2296 err_setup:
2297 	qeth_set_allowed_threads(card, 0, 1);
2298 	card->state = CARD_STATE_DOWN;
2299 	return rc;
2300 }
2301 
2302 static void qeth_l2_set_offline(struct qeth_card *card)
2303 {
2304 	struct qeth_priv *priv = netdev_priv(card->dev);
2305 
2306 	qeth_set_allowed_threads(card, 0, 1);
2307 	qeth_l2_drain_rx_mode_cache(card);
2308 
2309 	if (card->state == CARD_STATE_SOFTSETUP)
2310 		card->state = CARD_STATE_DOWN;
2311 
2312 	qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
2313 	if (priv->brport_features & BR_LEARNING_SYNC) {
2314 		rtnl_lock();
2315 		qeth_l2_dev2br_fdb_flush(card);
2316 		rtnl_unlock();
2317 	}
2318 }
2319 
2320 /* Returns zero if the command is successfully "consumed" */
2321 static int qeth_l2_control_event(struct qeth_card *card,
2322 				 struct qeth_ipa_cmd *cmd)
2323 {
2324 	switch (cmd->hdr.command) {
2325 	case IPA_CMD_SETBRIDGEPORT_OSA:
2326 	case IPA_CMD_SETBRIDGEPORT_IQD:
2327 		if (cmd->data.sbp.hdr.command_code ==
2328 		    IPA_SBP_BRIDGE_PORT_STATE_CHANGE) {
2329 			qeth_bridge_state_change(card, cmd);
2330 			return 0;
2331 		}
2332 
2333 		return 1;
2334 	case IPA_CMD_ADDRESS_CHANGE_NOTIF:
2335 		qeth_addr_change_event(card, cmd);
2336 		return 0;
2337 	default:
2338 		return 1;
2339 	}
2340 }
2341 
2342 const struct qeth_discipline qeth_l2_discipline = {
2343 	.devtype = &qeth_l2_devtype,
2344 	.setup = qeth_l2_probe_device,
2345 	.remove = qeth_l2_remove_device,
2346 	.set_online = qeth_l2_set_online,
2347 	.set_offline = qeth_l2_set_offline,
2348 	.do_ioctl = NULL,
2349 	.control_event_handler = qeth_l2_control_event,
2350 };
2351 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
2352 
2353 static int __init qeth_l2_init(void)
2354 {
2355 	pr_info("register layer 2 discipline\n");
2356 	return 0;
2357 }
2358 
2359 static void __exit qeth_l2_exit(void)
2360 {
2361 	pr_info("unregister layer 2 discipline\n");
2362 }
2363 
2364 module_init(qeth_l2_init);
2365 module_exit(qeth_l2_exit);
2366 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2367 MODULE_DESCRIPTION("qeth layer 2 discipline");
2368 MODULE_LICENSE("GPL");
2369