xref: /openbmc/linux/drivers/s390/net/qeth_l2_main.c (revision 165f2d28)
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/list.h>
21 #include <linux/hash.h>
22 #include <linux/hashtable.h>
23 #include <asm/setup.h>
24 #include "qeth_core.h"
25 #include "qeth_l2.h"
26 
27 static void qeth_bridgeport_query_support(struct qeth_card *card);
28 static void qeth_bridge_state_change(struct qeth_card *card,
29 					struct qeth_ipa_cmd *cmd);
30 static void qeth_bridge_host_event(struct qeth_card *card,
31 					struct qeth_ipa_cmd *cmd);
32 static void qeth_l2_vnicc_set_defaults(struct qeth_card *card);
33 static void qeth_l2_vnicc_init(struct qeth_card *card);
34 static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
35 					  u32 *timeout);
36 
37 static int qeth_l2_setdelmac_makerc(struct qeth_card *card, u16 retcode)
38 {
39 	int rc;
40 
41 	if (retcode)
42 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
43 	switch (retcode) {
44 	case IPA_RC_SUCCESS:
45 		rc = 0;
46 		break;
47 	case IPA_RC_L2_UNSUPPORTED_CMD:
48 		rc = -EOPNOTSUPP;
49 		break;
50 	case IPA_RC_L2_ADDR_TABLE_FULL:
51 		rc = -ENOSPC;
52 		break;
53 	case IPA_RC_L2_DUP_MAC:
54 	case IPA_RC_L2_DUP_LAYER3_MAC:
55 		rc = -EADDRINUSE;
56 		break;
57 	case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
58 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
59 		rc = -EADDRNOTAVAIL;
60 		break;
61 	case IPA_RC_L2_MAC_NOT_FOUND:
62 		rc = -ENOENT;
63 		break;
64 	default:
65 		rc = -EIO;
66 		break;
67 	}
68 	return rc;
69 }
70 
71 static int qeth_l2_send_setdelmac_cb(struct qeth_card *card,
72 				     struct qeth_reply *reply,
73 				     unsigned long data)
74 {
75 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
76 
77 	return qeth_l2_setdelmac_makerc(card, cmd->hdr.return_code);
78 }
79 
80 static int qeth_l2_send_setdelmac(struct qeth_card *card, __u8 *mac,
81 			   enum qeth_ipa_cmds ipacmd)
82 {
83 	struct qeth_ipa_cmd *cmd;
84 	struct qeth_cmd_buffer *iob;
85 
86 	QETH_CARD_TEXT(card, 2, "L2sdmac");
87 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
88 				 IPA_DATA_SIZEOF(setdelmac));
89 	if (!iob)
90 		return -ENOMEM;
91 	cmd = __ipa_cmd(iob);
92 	cmd->data.setdelmac.mac_length = ETH_ALEN;
93 	ether_addr_copy(cmd->data.setdelmac.mac, mac);
94 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelmac_cb, NULL);
95 }
96 
97 static int qeth_l2_send_setmac(struct qeth_card *card, __u8 *mac)
98 {
99 	int rc;
100 
101 	QETH_CARD_TEXT(card, 2, "L2Setmac");
102 	rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC);
103 	if (rc == 0) {
104 		dev_info(&card->gdev->dev,
105 			 "MAC address %pM successfully registered\n", mac);
106 	} else {
107 		switch (rc) {
108 		case -EADDRINUSE:
109 			dev_warn(&card->gdev->dev,
110 				"MAC address %pM already exists\n", mac);
111 			break;
112 		case -EADDRNOTAVAIL:
113 			dev_warn(&card->gdev->dev,
114 				"MAC address %pM is not authorized\n", mac);
115 			break;
116 		}
117 	}
118 	return rc;
119 }
120 
121 static int qeth_l2_write_mac(struct qeth_card *card, u8 *mac)
122 {
123 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
124 					IPA_CMD_SETGMAC : IPA_CMD_SETVMAC;
125 	int rc;
126 
127 	QETH_CARD_TEXT(card, 2, "L2Wmac");
128 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
129 	if (rc == -EADDRINUSE)
130 		QETH_DBF_MESSAGE(2, "MAC already registered on device %x\n",
131 				 CARD_DEVID(card));
132 	else if (rc)
133 		QETH_DBF_MESSAGE(2, "Failed to register MAC on device %x: %d\n",
134 				 CARD_DEVID(card), rc);
135 	return rc;
136 }
137 
138 static int qeth_l2_remove_mac(struct qeth_card *card, u8 *mac)
139 {
140 	enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
141 					IPA_CMD_DELGMAC : IPA_CMD_DELVMAC;
142 	int rc;
143 
144 	QETH_CARD_TEXT(card, 2, "L2Rmac");
145 	rc = qeth_l2_send_setdelmac(card, mac, cmd);
146 	if (rc)
147 		QETH_DBF_MESSAGE(2, "Failed to delete MAC on device %u: %d\n",
148 				 CARD_DEVID(card), rc);
149 	return rc;
150 }
151 
152 static void qeth_l2_drain_rx_mode_cache(struct qeth_card *card)
153 {
154 	struct qeth_mac *mac;
155 	struct hlist_node *tmp;
156 	int i;
157 
158 	hash_for_each_safe(card->mac_htable, i, tmp, mac, hnode) {
159 		hash_del(&mac->hnode);
160 		kfree(mac);
161 	}
162 }
163 
164 static void qeth_l2_fill_header(struct qeth_qdio_out_q *queue,
165 				struct qeth_hdr *hdr, struct sk_buff *skb,
166 				int ipv, unsigned int data_len)
167 {
168 	int cast_type = qeth_get_ether_cast_type(skb);
169 	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
170 
171 	hdr->hdr.l2.pkt_length = data_len;
172 
173 	if (skb_is_gso(skb)) {
174 		hdr->hdr.l2.id = QETH_HEADER_TYPE_L2_TSO;
175 	} else {
176 		hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
177 		if (skb->ip_summed == CHECKSUM_PARTIAL)
178 			qeth_tx_csum(skb, &hdr->hdr.l2.flags[1], ipv);
179 	}
180 
181 	/* set byte byte 3 to casting flags */
182 	if (cast_type == RTN_MULTICAST)
183 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
184 	else if (cast_type == RTN_BROADCAST)
185 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
186 	else
187 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
188 
189 	/* VSWITCH relies on the VLAN
190 	 * information to be present in
191 	 * the QDIO header */
192 	if (veth->h_vlan_proto == __constant_htons(ETH_P_8021Q)) {
193 		hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
194 		hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
195 	}
196 }
197 
198 static int qeth_l2_setdelvlan_makerc(struct qeth_card *card, u16 retcode)
199 {
200 	if (retcode)
201 		QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
202 
203 	switch (retcode) {
204 	case IPA_RC_SUCCESS:
205 		return 0;
206 	case IPA_RC_L2_INVALID_VLAN_ID:
207 		return -EINVAL;
208 	case IPA_RC_L2_DUP_VLAN_ID:
209 		return -EEXIST;
210 	case IPA_RC_L2_VLAN_ID_NOT_FOUND:
211 		return -ENOENT;
212 	case IPA_RC_L2_VLAN_ID_NOT_ALLOWED:
213 		return -EPERM;
214 	default:
215 		return -EIO;
216 	}
217 }
218 
219 static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
220 				      struct qeth_reply *reply,
221 				      unsigned long data)
222 {
223 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
224 
225 	QETH_CARD_TEXT(card, 2, "L2sdvcb");
226 	if (cmd->hdr.return_code) {
227 		QETH_DBF_MESSAGE(2, "Error in processing VLAN %u on device %x: %#x.\n",
228 				 cmd->data.setdelvlan.vlan_id,
229 				 CARD_DEVID(card), cmd->hdr.return_code);
230 		QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
231 	}
232 	return qeth_l2_setdelvlan_makerc(card, cmd->hdr.return_code);
233 }
234 
235 static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
236 				   enum qeth_ipa_cmds ipacmd)
237 {
238 	struct qeth_ipa_cmd *cmd;
239 	struct qeth_cmd_buffer *iob;
240 
241 	QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
242 	iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
243 				 IPA_DATA_SIZEOF(setdelvlan));
244 	if (!iob)
245 		return -ENOMEM;
246 	cmd = __ipa_cmd(iob);
247 	cmd->data.setdelvlan.vlan_id = i;
248 	return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelvlan_cb, NULL);
249 }
250 
251 static int qeth_l2_vlan_rx_add_vid(struct net_device *dev,
252 				   __be16 proto, u16 vid)
253 {
254 	struct qeth_card *card = dev->ml_priv;
255 
256 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
257 	if (!vid)
258 		return 0;
259 
260 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
261 }
262 
263 static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev,
264 				    __be16 proto, u16 vid)
265 {
266 	struct qeth_card *card = dev->ml_priv;
267 
268 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
269 	if (!vid)
270 		return 0;
271 
272 	return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
273 }
274 
275 static void qeth_l2_stop_card(struct qeth_card *card)
276 {
277 	QETH_CARD_TEXT(card, 2, "stopcard");
278 
279 	qeth_set_allowed_threads(card, 0, 1);
280 
281 	cancel_work_sync(&card->rx_mode_work);
282 	qeth_l2_drain_rx_mode_cache(card);
283 
284 	if (card->state == CARD_STATE_SOFTSETUP) {
285 		qeth_clear_ipacmd_list(card);
286 		qeth_drain_output_queues(card);
287 		cancel_delayed_work_sync(&card->buffer_reclaim_work);
288 		card->state = CARD_STATE_DOWN;
289 	}
290 
291 	qeth_qdio_clear_card(card, 0);
292 	qeth_clear_working_pool_list(card);
293 	flush_workqueue(card->event_wq);
294 	card->info.promisc_mode = 0;
295 }
296 
297 static int qeth_l2_request_initial_mac(struct qeth_card *card)
298 {
299 	int rc = 0;
300 
301 	QETH_CARD_TEXT(card, 2, "l2reqmac");
302 
303 	if (MACHINE_IS_VM) {
304 		rc = qeth_vm_request_mac(card);
305 		if (!rc)
306 			goto out;
307 		QETH_DBF_MESSAGE(2, "z/VM MAC Service failed on device %x: %#x\n",
308 				 CARD_DEVID(card), rc);
309 		QETH_CARD_TEXT_(card, 2, "err%04x", rc);
310 		/* fall back to alternative mechanism: */
311 	}
312 
313 	if (!IS_OSN(card)) {
314 		rc = qeth_setadpparms_change_macaddr(card);
315 		if (!rc)
316 			goto out;
317 		QETH_DBF_MESSAGE(2, "READ_MAC Assist failed on device %x: %#x\n",
318 				 CARD_DEVID(card), rc);
319 		QETH_CARD_TEXT_(card, 2, "1err%04x", rc);
320 		/* fall back once more: */
321 	}
322 
323 	/* some devices don't support a custom MAC address: */
324 	if (IS_OSM(card) || IS_OSX(card))
325 		return (rc) ? rc : -EADDRNOTAVAIL;
326 	eth_hw_addr_random(card->dev);
327 
328 out:
329 	QETH_CARD_HEX(card, 2, card->dev->dev_addr, card->dev->addr_len);
330 	return 0;
331 }
332 
333 static void qeth_l2_register_dev_addr(struct qeth_card *card)
334 {
335 	if (!is_valid_ether_addr(card->dev->dev_addr))
336 		qeth_l2_request_initial_mac(card);
337 
338 	if (!IS_OSN(card) && !qeth_l2_send_setmac(card, card->dev->dev_addr))
339 		card->info.dev_addr_is_registered = 1;
340 	else
341 		card->info.dev_addr_is_registered = 0;
342 }
343 
344 static int qeth_l2_validate_addr(struct net_device *dev)
345 {
346 	struct qeth_card *card = dev->ml_priv;
347 
348 	if (card->info.dev_addr_is_registered)
349 		return eth_validate_addr(dev);
350 
351 	QETH_CARD_TEXT(card, 4, "nomacadr");
352 	return -EPERM;
353 }
354 
355 static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
356 {
357 	struct sockaddr *addr = p;
358 	struct qeth_card *card = dev->ml_priv;
359 	u8 old_addr[ETH_ALEN];
360 	int rc = 0;
361 
362 	QETH_CARD_TEXT(card, 3, "setmac");
363 
364 	if (IS_OSM(card) || IS_OSX(card)) {
365 		QETH_CARD_TEXT(card, 3, "setmcTYP");
366 		return -EOPNOTSUPP;
367 	}
368 	QETH_CARD_HEX(card, 3, addr->sa_data, ETH_ALEN);
369 	if (!is_valid_ether_addr(addr->sa_data))
370 		return -EADDRNOTAVAIL;
371 
372 	/* don't register the same address twice */
373 	if (ether_addr_equal_64bits(dev->dev_addr, addr->sa_data) &&
374 	    card->info.dev_addr_is_registered)
375 		return 0;
376 
377 	/* add the new address, switch over, drop the old */
378 	rc = qeth_l2_send_setmac(card, addr->sa_data);
379 	if (rc)
380 		return rc;
381 	ether_addr_copy(old_addr, dev->dev_addr);
382 	ether_addr_copy(dev->dev_addr, addr->sa_data);
383 
384 	if (card->info.dev_addr_is_registered)
385 		qeth_l2_remove_mac(card, old_addr);
386 	card->info.dev_addr_is_registered = 1;
387 	return 0;
388 }
389 
390 static void qeth_l2_promisc_to_bridge(struct qeth_card *card, bool enable)
391 {
392 	int role;
393 	int rc;
394 
395 	QETH_CARD_TEXT(card, 3, "pmisc2br");
396 
397 	if (enable) {
398 		if (card->options.sbp.reflect_promisc_primary)
399 			role = QETH_SBP_ROLE_PRIMARY;
400 		else
401 			role = QETH_SBP_ROLE_SECONDARY;
402 	} else
403 		role = QETH_SBP_ROLE_NONE;
404 
405 	rc = qeth_bridgeport_setrole(card, role);
406 	QETH_CARD_TEXT_(card, 2, "bpm%c%04x", enable ? '+' : '-', rc);
407 	if (!rc) {
408 		card->options.sbp.role = role;
409 		card->info.promisc_mode = enable;
410 	}
411 }
412 
413 static void qeth_l2_set_promisc_mode(struct qeth_card *card)
414 {
415 	bool enable = card->dev->flags & IFF_PROMISC;
416 
417 	if (card->info.promisc_mode == enable)
418 		return;
419 
420 	if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) {
421 		qeth_setadp_promisc_mode(card, enable);
422 	} else {
423 		mutex_lock(&card->sbp_lock);
424 		if (card->options.sbp.reflect_promisc)
425 			qeth_l2_promisc_to_bridge(card, enable);
426 		mutex_unlock(&card->sbp_lock);
427 	}
428 }
429 
430 /* New MAC address is added to the hash table and marked to be written on card
431  * only if there is not in the hash table storage already
432  *
433 */
434 static void qeth_l2_add_mac(struct qeth_card *card, struct netdev_hw_addr *ha)
435 {
436 	u32 mac_hash = get_unaligned((u32 *)(&ha->addr[2]));
437 	struct qeth_mac *mac;
438 
439 	hash_for_each_possible(card->mac_htable, mac, hnode, mac_hash) {
440 		if (ether_addr_equal_64bits(ha->addr, mac->mac_addr)) {
441 			mac->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
442 			return;
443 		}
444 	}
445 
446 	mac = kzalloc(sizeof(struct qeth_mac), GFP_ATOMIC);
447 	if (!mac)
448 		return;
449 
450 	ether_addr_copy(mac->mac_addr, ha->addr);
451 	mac->disp_flag = QETH_DISP_ADDR_ADD;
452 
453 	hash_add(card->mac_htable, &mac->hnode, mac_hash);
454 }
455 
456 static void qeth_l2_rx_mode_work(struct work_struct *work)
457 {
458 	struct qeth_card *card = container_of(work, struct qeth_card,
459 					      rx_mode_work);
460 	struct net_device *dev = card->dev;
461 	struct netdev_hw_addr *ha;
462 	struct qeth_mac *mac;
463 	struct hlist_node *tmp;
464 	int i;
465 	int rc;
466 
467 	QETH_CARD_TEXT(card, 3, "setmulti");
468 
469 	netif_addr_lock_bh(dev);
470 	netdev_for_each_mc_addr(ha, dev)
471 		qeth_l2_add_mac(card, ha);
472 	netdev_for_each_uc_addr(ha, dev)
473 		qeth_l2_add_mac(card, ha);
474 	netif_addr_unlock_bh(dev);
475 
476 	hash_for_each_safe(card->mac_htable, i, tmp, mac, hnode) {
477 		switch (mac->disp_flag) {
478 		case QETH_DISP_ADDR_DELETE:
479 			qeth_l2_remove_mac(card, mac->mac_addr);
480 			hash_del(&mac->hnode);
481 			kfree(mac);
482 			break;
483 		case QETH_DISP_ADDR_ADD:
484 			rc = qeth_l2_write_mac(card, mac->mac_addr);
485 			if (rc) {
486 				hash_del(&mac->hnode);
487 				kfree(mac);
488 				break;
489 			}
490 			/* fall through */
491 		default:
492 			/* for next call to set_rx_mode(): */
493 			mac->disp_flag = QETH_DISP_ADDR_DELETE;
494 		}
495 	}
496 
497 	qeth_l2_set_promisc_mode(card);
498 }
499 
500 static int qeth_l2_xmit_osn(struct qeth_card *card, struct sk_buff *skb,
501 			    struct qeth_qdio_out_q *queue)
502 {
503 	gfp_t gfp = GFP_ATOMIC | (skb_pfmemalloc(skb) ? __GFP_MEMALLOC : 0);
504 	struct qeth_hdr *hdr = (struct qeth_hdr *)skb->data;
505 	addr_t end = (addr_t)(skb->data + sizeof(*hdr));
506 	addr_t start = (addr_t)skb->data;
507 	unsigned int elements = 0;
508 	unsigned int hd_len = 0;
509 	int rc;
510 
511 	if (skb->protocol == htons(ETH_P_IPV6))
512 		return -EPROTONOSUPPORT;
513 
514 	if (qeth_get_elements_for_range(start, end) > 1) {
515 		/* Misaligned HW header, move it to its own buffer element. */
516 		hdr = kmem_cache_alloc(qeth_core_header_cache, gfp);
517 		if (!hdr)
518 			return -ENOMEM;
519 		hd_len = sizeof(*hdr);
520 		skb_copy_from_linear_data(skb, (char *)hdr, hd_len);
521 		elements++;
522 	}
523 
524 	elements += qeth_count_elements(skb, hd_len);
525 	if (elements > queue->max_elements) {
526 		rc = -E2BIG;
527 		goto out;
528 	}
529 
530 	rc = qeth_do_send_packet(card, queue, skb, hdr, hd_len, hd_len,
531 				 elements);
532 out:
533 	if (rc && hd_len)
534 		kmem_cache_free(qeth_core_header_cache, hdr);
535 	return rc;
536 }
537 
538 static netdev_tx_t qeth_l2_hard_start_xmit(struct sk_buff *skb,
539 					   struct net_device *dev)
540 {
541 	struct qeth_card *card = dev->ml_priv;
542 	u16 txq = skb_get_queue_mapping(skb);
543 	struct qeth_qdio_out_q *queue;
544 	int rc;
545 
546 	if (!skb_is_gso(skb))
547 		qdisc_skb_cb(skb)->pkt_len = skb->len;
548 	if (IS_IQD(card))
549 		txq = qeth_iqd_translate_txq(dev, txq);
550 	queue = card->qdio.out_qs[txq];
551 
552 	if (IS_OSN(card))
553 		rc = qeth_l2_xmit_osn(card, skb, queue);
554 	else
555 		rc = qeth_xmit(card, skb, queue, qeth_get_ip_version(skb),
556 			       qeth_l2_fill_header);
557 
558 	if (!rc)
559 		return NETDEV_TX_OK;
560 
561 	QETH_TXQ_STAT_INC(queue, tx_dropped);
562 	kfree_skb(skb);
563 	return NETDEV_TX_OK;
564 }
565 
566 static u16 qeth_l2_select_queue(struct net_device *dev, struct sk_buff *skb,
567 				struct net_device *sb_dev)
568 {
569 	struct qeth_card *card = dev->ml_priv;
570 
571 	if (IS_IQD(card))
572 		return qeth_iqd_select_queue(dev, skb,
573 					     qeth_get_ether_cast_type(skb),
574 					     sb_dev);
575 
576 	return IS_VM_NIC(card) ? netdev_pick_tx(dev, skb, sb_dev) :
577 				 qeth_get_priority_queue(card, skb);
578 }
579 
580 static const struct device_type qeth_l2_devtype = {
581 	.name = "qeth_layer2",
582 	.groups = qeth_l2_attr_groups,
583 };
584 
585 static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
586 {
587 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
588 	int rc;
589 
590 	if (IS_OSN(card))
591 		dev_notice(&gdev->dev, "OSN support will be dropped in 2021\n");
592 
593 	qeth_l2_vnicc_set_defaults(card);
594 	mutex_init(&card->sbp_lock);
595 
596 	if (gdev->dev.type == &qeth_generic_devtype) {
597 		rc = qeth_l2_create_device_attributes(&gdev->dev);
598 		if (rc)
599 			return rc;
600 	}
601 
602 	hash_init(card->mac_htable);
603 	INIT_WORK(&card->rx_mode_work, qeth_l2_rx_mode_work);
604 	return 0;
605 }
606 
607 static void qeth_l2_remove_device(struct ccwgroup_device *cgdev)
608 {
609 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
610 
611 	if (cgdev->dev.type == &qeth_generic_devtype)
612 		qeth_l2_remove_device_attributes(&cgdev->dev);
613 	qeth_set_allowed_threads(card, 0, 1);
614 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
615 
616 	if (cgdev->state == CCWGROUP_ONLINE)
617 		qeth_set_offline(card, false);
618 
619 	cancel_work_sync(&card->close_dev_work);
620 	if (card->dev->reg_state == NETREG_REGISTERED)
621 		unregister_netdev(card->dev);
622 }
623 
624 static void qeth_l2_set_rx_mode(struct net_device *dev)
625 {
626 	struct qeth_card *card = dev->ml_priv;
627 
628 	schedule_work(&card->rx_mode_work);
629 }
630 
631 static const struct net_device_ops qeth_l2_netdev_ops = {
632 	.ndo_open		= qeth_open,
633 	.ndo_stop		= qeth_stop,
634 	.ndo_get_stats64	= qeth_get_stats64,
635 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
636 	.ndo_features_check	= qeth_features_check,
637 	.ndo_select_queue	= qeth_l2_select_queue,
638 	.ndo_validate_addr	= qeth_l2_validate_addr,
639 	.ndo_set_rx_mode	= qeth_l2_set_rx_mode,
640 	.ndo_do_ioctl		= qeth_do_ioctl,
641 	.ndo_set_mac_address    = qeth_l2_set_mac_address,
642 	.ndo_vlan_rx_add_vid	= qeth_l2_vlan_rx_add_vid,
643 	.ndo_vlan_rx_kill_vid   = qeth_l2_vlan_rx_kill_vid,
644 	.ndo_tx_timeout	   	= qeth_tx_timeout,
645 	.ndo_fix_features	= qeth_fix_features,
646 	.ndo_set_features	= qeth_set_features
647 };
648 
649 static const struct net_device_ops qeth_osn_netdev_ops = {
650 	.ndo_open		= qeth_open,
651 	.ndo_stop		= qeth_stop,
652 	.ndo_get_stats64	= qeth_get_stats64,
653 	.ndo_start_xmit		= qeth_l2_hard_start_xmit,
654 	.ndo_validate_addr	= eth_validate_addr,
655 	.ndo_tx_timeout		= qeth_tx_timeout,
656 };
657 
658 static int qeth_l2_setup_netdev(struct qeth_card *card)
659 {
660 	int rc;
661 
662 	if (IS_OSN(card)) {
663 		card->dev->netdev_ops = &qeth_osn_netdev_ops;
664 		card->dev->flags |= IFF_NOARP;
665 		goto add_napi;
666 	}
667 
668 	rc = qeth_setup_netdev(card);
669 	if (rc)
670 		return rc;
671 
672 	card->dev->needed_headroom = sizeof(struct qeth_hdr);
673 	card->dev->netdev_ops = &qeth_l2_netdev_ops;
674 	card->dev->priv_flags |= IFF_UNICAST_FLT;
675 
676 	if (IS_OSM(card)) {
677 		card->dev->features |= NETIF_F_VLAN_CHALLENGED;
678 	} else {
679 		if (!IS_VM_NIC(card))
680 			card->dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
681 		card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
682 	}
683 
684 	if (IS_OSD(card) && !IS_VM_NIC(card)) {
685 		card->dev->features |= NETIF_F_SG;
686 		/* OSA 3S and earlier has no RX/TX support */
687 		if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
688 			card->dev->hw_features |= NETIF_F_IP_CSUM;
689 			card->dev->vlan_features |= NETIF_F_IP_CSUM;
690 		}
691 	}
692 	if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
693 		card->dev->hw_features |= NETIF_F_IPV6_CSUM;
694 		card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
695 	}
696 	if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM) ||
697 	    qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6)) {
698 		card->dev->hw_features |= NETIF_F_RXCSUM;
699 		card->dev->vlan_features |= NETIF_F_RXCSUM;
700 	}
701 	if (qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
702 		card->dev->hw_features |= NETIF_F_TSO;
703 		card->dev->vlan_features |= NETIF_F_TSO;
704 	}
705 	if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
706 		card->dev->hw_features |= NETIF_F_TSO6;
707 		card->dev->vlan_features |= NETIF_F_TSO6;
708 	}
709 
710 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) {
711 		card->dev->needed_headroom = sizeof(struct qeth_hdr_tso);
712 		netif_set_gso_max_size(card->dev,
713 				       PAGE_SIZE * (QDIO_MAX_ELEMENTS_PER_BUFFER - 1));
714 	}
715 
716 add_napi:
717 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
718 	return register_netdev(card->dev);
719 }
720 
721 static void qeth_l2_trace_features(struct qeth_card *card)
722 {
723 	/* Set BridgePort features */
724 	QETH_CARD_TEXT(card, 2, "featuSBP");
725 	QETH_CARD_HEX(card, 2, &card->options.sbp.supported_funcs,
726 		      sizeof(card->options.sbp.supported_funcs));
727 	/* VNIC Characteristics features */
728 	QETH_CARD_TEXT(card, 2, "feaVNICC");
729 	QETH_CARD_HEX(card, 2, &card->options.vnicc.sup_chars,
730 		      sizeof(card->options.vnicc.sup_chars));
731 }
732 
733 static void qeth_l2_setup_bridgeport_attrs(struct qeth_card *card)
734 {
735 	if (!card->options.sbp.reflect_promisc &&
736 	    card->options.sbp.role != QETH_SBP_ROLE_NONE) {
737 		/* Conditional to avoid spurious error messages */
738 		qeth_bridgeport_setrole(card, card->options.sbp.role);
739 		/* Let the callback function refresh the stored role value. */
740 		qeth_bridgeport_query_ports(card, &card->options.sbp.role,
741 					    NULL);
742 	}
743 	if (card->options.sbp.hostnotification) {
744 		if (qeth_bridgeport_an_set(card, 1))
745 			card->options.sbp.hostnotification = 0;
746 	} else {
747 		qeth_bridgeport_an_set(card, 0);
748 	}
749 }
750 
751 static int qeth_l2_set_online(struct qeth_card *card)
752 {
753 	struct ccwgroup_device *gdev = card->gdev;
754 	struct net_device *dev = card->dev;
755 	int rc = 0;
756 	bool carrier_ok;
757 
758 	rc = qeth_core_hardsetup_card(card, &carrier_ok);
759 	if (rc) {
760 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
761 		rc = -ENODEV;
762 		goto out_remove;
763 	}
764 
765 	mutex_lock(&card->sbp_lock);
766 	qeth_bridgeport_query_support(card);
767 	if (card->options.sbp.supported_funcs) {
768 		qeth_l2_setup_bridgeport_attrs(card);
769 		dev_info(&card->gdev->dev,
770 			 "The device represents a Bridge Capable Port\n");
771 	}
772 	mutex_unlock(&card->sbp_lock);
773 
774 	qeth_l2_register_dev_addr(card);
775 
776 	/* for the rx_bcast characteristic, init VNICC after setmac */
777 	qeth_l2_vnicc_init(card);
778 
779 	qeth_trace_features(card);
780 	qeth_l2_trace_features(card);
781 
782 	qeth_print_status_message(card);
783 
784 	/* softsetup */
785 	QETH_CARD_TEXT(card, 2, "softsetp");
786 
787 	card->state = CARD_STATE_SOFTSETUP;
788 
789 	qeth_set_allowed_threads(card, 0xffffffff, 0);
790 
791 	if (dev->reg_state != NETREG_REGISTERED) {
792 		rc = qeth_l2_setup_netdev(card);
793 		if (rc)
794 			goto out_remove;
795 
796 		if (carrier_ok)
797 			netif_carrier_on(dev);
798 	} else {
799 		rtnl_lock();
800 		if (carrier_ok)
801 			netif_carrier_on(dev);
802 		else
803 			netif_carrier_off(dev);
804 
805 		netif_device_attach(dev);
806 		qeth_enable_hw_features(dev);
807 
808 		if (card->info.open_when_online) {
809 			card->info.open_when_online = 0;
810 			dev_open(dev, NULL);
811 		}
812 		rtnl_unlock();
813 	}
814 	/* let user_space know that device is online */
815 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
816 	return 0;
817 
818 out_remove:
819 	qeth_l2_stop_card(card);
820 	qeth_stop_channel(&card->data);
821 	qeth_stop_channel(&card->write);
822 	qeth_stop_channel(&card->read);
823 	qdio_free(CARD_DDEV(card));
824 	return rc;
825 }
826 
827 static void qeth_l2_set_offline(struct qeth_card *card)
828 {
829 	qeth_l2_stop_card(card);
830 }
831 
832 static int __init qeth_l2_init(void)
833 {
834 	pr_info("register layer 2 discipline\n");
835 	return 0;
836 }
837 
838 static void __exit qeth_l2_exit(void)
839 {
840 	pr_info("unregister layer 2 discipline\n");
841 }
842 
843 /* Returns zero if the command is successfully "consumed" */
844 static int qeth_l2_control_event(struct qeth_card *card,
845 					struct qeth_ipa_cmd *cmd)
846 {
847 	switch (cmd->hdr.command) {
848 	case IPA_CMD_SETBRIDGEPORT_OSA:
849 	case IPA_CMD_SETBRIDGEPORT_IQD:
850 		if (cmd->data.sbp.hdr.command_code ==
851 				IPA_SBP_BRIDGE_PORT_STATE_CHANGE) {
852 			qeth_bridge_state_change(card, cmd);
853 			return 0;
854 		} else
855 			return 1;
856 	case IPA_CMD_ADDRESS_CHANGE_NOTIF:
857 		qeth_bridge_host_event(card, cmd);
858 		return 0;
859 	default:
860 		return 1;
861 	}
862 }
863 
864 struct qeth_discipline qeth_l2_discipline = {
865 	.devtype = &qeth_l2_devtype,
866 	.setup = qeth_l2_probe_device,
867 	.remove = qeth_l2_remove_device,
868 	.set_online = qeth_l2_set_online,
869 	.set_offline = qeth_l2_set_offline,
870 	.do_ioctl = NULL,
871 	.control_event_handler = qeth_l2_control_event,
872 };
873 EXPORT_SYMBOL_GPL(qeth_l2_discipline);
874 
875 #ifdef CONFIG_QETH_OSN
876 static void qeth_osn_assist_cb(struct qeth_card *card,
877 			       struct qeth_cmd_buffer *iob,
878 			       unsigned int data_length)
879 {
880 	qeth_notify_cmd(iob, 0);
881 	qeth_put_cmd(iob);
882 }
883 
884 int qeth_osn_assist(struct net_device *dev, void *data, int data_len)
885 {
886 	struct qeth_cmd_buffer *iob;
887 	struct qeth_card *card;
888 
889 	if (data_len < 0)
890 		return -EINVAL;
891 	if (!dev)
892 		return -ENODEV;
893 	card = dev->ml_priv;
894 	if (!card)
895 		return -ENODEV;
896 	QETH_CARD_TEXT(card, 2, "osnsdmc");
897 	if (!qeth_card_hw_is_reachable(card))
898 		return -ENODEV;
899 
900 	iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_len, 1,
901 			     QETH_IPA_TIMEOUT);
902 	if (!iob)
903 		return -ENOMEM;
904 
905 	qeth_prepare_ipa_cmd(card, iob, (u16) data_len, NULL);
906 
907 	memcpy(__ipa_cmd(iob), data, data_len);
908 	iob->callback = qeth_osn_assist_cb;
909 	return qeth_send_ipa_cmd(card, iob, NULL, NULL);
910 }
911 EXPORT_SYMBOL(qeth_osn_assist);
912 
913 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **dev,
914 		  int (*assist_cb)(struct net_device *, void *),
915 		  int (*data_cb)(struct sk_buff *))
916 {
917 	struct qeth_card *card;
918 	char bus_id[16];
919 	u16 devno;
920 
921 	memcpy(&devno, read_dev_no, 2);
922 	sprintf(bus_id, "0.0.%04x", devno);
923 	card = qeth_get_card_by_busid(bus_id);
924 	if (!card || !IS_OSN(card))
925 		return -ENODEV;
926 	*dev = card->dev;
927 
928 	QETH_CARD_TEXT(card, 2, "osnreg");
929 	if ((assist_cb == NULL) || (data_cb == NULL))
930 		return -EINVAL;
931 	card->osn_info.assist_cb = assist_cb;
932 	card->osn_info.data_cb = data_cb;
933 	return 0;
934 }
935 EXPORT_SYMBOL(qeth_osn_register);
936 
937 void qeth_osn_deregister(struct net_device *dev)
938 {
939 	struct qeth_card *card;
940 
941 	if (!dev)
942 		return;
943 	card = dev->ml_priv;
944 	if (!card)
945 		return;
946 	QETH_CARD_TEXT(card, 2, "osndereg");
947 	card->osn_info.assist_cb = NULL;
948 	card->osn_info.data_cb = NULL;
949 	return;
950 }
951 EXPORT_SYMBOL(qeth_osn_deregister);
952 #endif
953 
954 /* SETBRIDGEPORT support, async notifications */
955 
956 enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
957 
958 /**
959  * qeth_bridge_emit_host_event() - bridgeport address change notification
960  * @card:  qeth_card structure pointer, for udev events.
961  * @evtype:  "normal" register/unregister, or abort, or reset. For abort
962  *	      and reset token and addr_lnid are unused and may be NULL.
963  * @code:  event bitmask: high order bit 0x80 value 1 means removal of an
964  *			  object, 0 - addition of an object.
965  *			  0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
966  * @token: "network token" structure identifying physical address of the port.
967  * @addr_lnid: pointer to structure with MAC address and VLAN ID.
968  *
969  * This function is called when registrations and deregistrations are
970  * reported by the hardware, and also when notifications are enabled -
971  * for all currently registered addresses.
972  */
973 static void qeth_bridge_emit_host_event(struct qeth_card *card,
974 	enum qeth_an_event_type evtype,
975 	u8 code, struct net_if_token *token, struct mac_addr_lnid *addr_lnid)
976 {
977 	char str[7][32];
978 	char *env[8];
979 	int i = 0;
980 
981 	switch (evtype) {
982 	case anev_reg_unreg:
983 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
984 				(code & IPA_ADDR_CHANGE_CODE_REMOVAL)
985 				? "deregister" : "register");
986 		env[i] = str[i]; i++;
987 		if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
988 			snprintf(str[i], sizeof(str[i]), "VLAN=%d",
989 				addr_lnid->lnid);
990 			env[i] = str[i]; i++;
991 		}
992 		if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
993 			snprintf(str[i], sizeof(str[i]), "MAC=%pM",
994 				addr_lnid->mac);
995 			env[i] = str[i]; i++;
996 		}
997 		snprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
998 			token->cssid, token->ssid, token->devnum);
999 		env[i] = str[i]; i++;
1000 		snprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1001 		env[i] = str[i]; i++;
1002 		snprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1003 				token->chpid);
1004 		env[i] = str[i]; i++;
1005 		snprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x", token->chid);
1006 		env[i] = str[i]; i++;
1007 		break;
1008 	case anev_abort:
1009 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1010 		env[i] = str[i]; i++;
1011 		break;
1012 	case anev_reset:
1013 		snprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1014 		env[i] = str[i]; i++;
1015 		break;
1016 	}
1017 	env[i] = NULL;
1018 	kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1019 }
1020 
1021 struct qeth_bridge_state_data {
1022 	struct work_struct worker;
1023 	struct qeth_card *card;
1024 	struct qeth_sbp_state_change qports;
1025 };
1026 
1027 static void qeth_bridge_state_change_worker(struct work_struct *work)
1028 {
1029 	struct qeth_bridge_state_data *data =
1030 		container_of(work, struct qeth_bridge_state_data, worker);
1031 	/* We are only interested in the first entry - local port */
1032 	struct qeth_sbp_port_entry *entry = &data->qports.entry[0];
1033 	char env_locrem[32];
1034 	char env_role[32];
1035 	char env_state[32];
1036 	char *env[] = {
1037 		env_locrem,
1038 		env_role,
1039 		env_state,
1040 		NULL
1041 	};
1042 
1043 	/* Role should not change by itself, but if it did, */
1044 	/* information from the hardware is authoritative.  */
1045 	mutex_lock(&data->card->sbp_lock);
1046 	data->card->options.sbp.role = entry->role;
1047 	mutex_unlock(&data->card->sbp_lock);
1048 
1049 	snprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1050 	snprintf(env_role, sizeof(env_role), "ROLE=%s",
1051 		(entry->role == QETH_SBP_ROLE_NONE) ? "none" :
1052 		(entry->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1053 		(entry->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1054 		"<INVALID>");
1055 	snprintf(env_state, sizeof(env_state), "STATE=%s",
1056 		(entry->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1057 		(entry->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1058 		(entry->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1059 		"<INVALID>");
1060 	kobject_uevent_env(&data->card->gdev->dev.kobj,
1061 				KOBJ_CHANGE, env);
1062 	kfree(data);
1063 }
1064 
1065 static void qeth_bridge_state_change(struct qeth_card *card,
1066 					struct qeth_ipa_cmd *cmd)
1067 {
1068 	struct qeth_sbp_state_change *qports =
1069 		 &cmd->data.sbp.data.state_change;
1070 	struct qeth_bridge_state_data *data;
1071 	int extrasize;
1072 
1073 	QETH_CARD_TEXT(card, 2, "brstchng");
1074 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1075 		QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1076 		return;
1077 	}
1078 	extrasize = sizeof(struct qeth_sbp_port_entry) * qports->num_entries;
1079 	data = kzalloc(sizeof(struct qeth_bridge_state_data) + extrasize,
1080 		GFP_ATOMIC);
1081 	if (!data) {
1082 		QETH_CARD_TEXT(card, 2, "BPSalloc");
1083 		return;
1084 	}
1085 	INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1086 	data->card = card;
1087 	memcpy(&data->qports, qports,
1088 			sizeof(struct qeth_sbp_state_change) + extrasize);
1089 	queue_work(card->event_wq, &data->worker);
1090 }
1091 
1092 struct qeth_bridge_host_data {
1093 	struct work_struct worker;
1094 	struct qeth_card *card;
1095 	struct qeth_ipacmd_addr_change hostevs;
1096 };
1097 
1098 static void qeth_bridge_host_event_worker(struct work_struct *work)
1099 {
1100 	struct qeth_bridge_host_data *data =
1101 		container_of(work, struct qeth_bridge_host_data, worker);
1102 	int i;
1103 
1104 	if (data->hostevs.lost_event_mask) {
1105 		dev_info(&data->card->gdev->dev,
1106 "Address notification from the Bridge Port stopped %s (%s)\n",
1107 			data->card->dev->name,
1108 			(data->hostevs.lost_event_mask == 0x01)
1109 			? "Overflow"
1110 			: (data->hostevs.lost_event_mask == 0x02)
1111 			? "Bridge port state change"
1112 			: "Unknown reason");
1113 		mutex_lock(&data->card->sbp_lock);
1114 		data->card->options.sbp.hostnotification = 0;
1115 		mutex_unlock(&data->card->sbp_lock);
1116 		qeth_bridge_emit_host_event(data->card, anev_abort,
1117 			0, NULL, NULL);
1118 	} else
1119 		for (i = 0; i < data->hostevs.num_entries; i++) {
1120 			struct qeth_ipacmd_addr_change_entry *entry =
1121 					&data->hostevs.entry[i];
1122 			qeth_bridge_emit_host_event(data->card,
1123 					anev_reg_unreg,
1124 					entry->change_code,
1125 					&entry->token, &entry->addr_lnid);
1126 		}
1127 	kfree(data);
1128 }
1129 
1130 static void qeth_bridge_host_event(struct qeth_card *card,
1131 					struct qeth_ipa_cmd *cmd)
1132 {
1133 	struct qeth_ipacmd_addr_change *hostevs =
1134 		 &cmd->data.addrchange;
1135 	struct qeth_bridge_host_data *data;
1136 	int extrasize;
1137 
1138 	QETH_CARD_TEXT(card, 2, "brhostev");
1139 	if (cmd->hdr.return_code != 0x0000) {
1140 		if (cmd->hdr.return_code == 0x0010) {
1141 			if (hostevs->lost_event_mask == 0x00)
1142 				hostevs->lost_event_mask = 0xff;
1143 		} else {
1144 			QETH_CARD_TEXT_(card, 2, "BPHe%04x",
1145 				cmd->hdr.return_code);
1146 			return;
1147 		}
1148 	}
1149 	extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1150 						hostevs->num_entries;
1151 	data = kzalloc(sizeof(struct qeth_bridge_host_data) + extrasize,
1152 		GFP_ATOMIC);
1153 	if (!data) {
1154 		QETH_CARD_TEXT(card, 2, "BPHalloc");
1155 		return;
1156 	}
1157 	INIT_WORK(&data->worker, qeth_bridge_host_event_worker);
1158 	data->card = card;
1159 	memcpy(&data->hostevs, hostevs,
1160 			sizeof(struct qeth_ipacmd_addr_change) + extrasize);
1161 	queue_work(card->event_wq, &data->worker);
1162 }
1163 
1164 /* SETBRIDGEPORT support; sending commands */
1165 
1166 struct _qeth_sbp_cbctl {
1167 	union {
1168 		u32 supported;
1169 		struct {
1170 			enum qeth_sbp_roles *role;
1171 			enum qeth_sbp_states *state;
1172 		} qports;
1173 	} data;
1174 };
1175 
1176 static int qeth_bridgeport_makerc(struct qeth_card *card,
1177 				  struct qeth_ipa_cmd *cmd)
1178 {
1179 	struct qeth_ipacmd_setbridgeport *sbp = &cmd->data.sbp;
1180 	enum qeth_ipa_sbp_cmd setcmd = sbp->hdr.command_code;
1181 	u16 ipa_rc = cmd->hdr.return_code;
1182 	u16 sbp_rc = sbp->hdr.return_code;
1183 	int rc;
1184 
1185 	if (ipa_rc == IPA_RC_SUCCESS && sbp_rc == IPA_RC_SUCCESS)
1186 		return 0;
1187 
1188 	if ((IS_IQD(card) && ipa_rc == IPA_RC_SUCCESS) ||
1189 	    (!IS_IQD(card) && ipa_rc == sbp_rc)) {
1190 		switch (sbp_rc) {
1191 		case IPA_RC_SUCCESS:
1192 			rc = 0;
1193 			break;
1194 		case IPA_RC_L2_UNSUPPORTED_CMD:
1195 		case IPA_RC_UNSUPPORTED_COMMAND:
1196 			rc = -EOPNOTSUPP;
1197 			break;
1198 		case IPA_RC_SBP_OSA_NOT_CONFIGURED:
1199 		case IPA_RC_SBP_IQD_NOT_CONFIGURED:
1200 			rc = -ENODEV; /* maybe not the best code here? */
1201 			dev_err(&card->gdev->dev,
1202 	"The device is not configured as a Bridge Port\n");
1203 			break;
1204 		case IPA_RC_SBP_OSA_OS_MISMATCH:
1205 		case IPA_RC_SBP_IQD_OS_MISMATCH:
1206 			rc = -EPERM;
1207 			dev_err(&card->gdev->dev,
1208 	"A Bridge Port is already configured by a different operating system\n");
1209 			break;
1210 		case IPA_RC_SBP_OSA_ANO_DEV_PRIMARY:
1211 		case IPA_RC_SBP_IQD_ANO_DEV_PRIMARY:
1212 			switch (setcmd) {
1213 			case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1214 				rc = -EEXIST;
1215 				dev_err(&card->gdev->dev,
1216 	"The LAN already has a primary Bridge Port\n");
1217 				break;
1218 			case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1219 				rc = -EBUSY;
1220 				dev_err(&card->gdev->dev,
1221 	"The device is already a primary Bridge Port\n");
1222 				break;
1223 			default:
1224 				rc = -EIO;
1225 			}
1226 			break;
1227 		case IPA_RC_SBP_OSA_CURRENT_SECOND:
1228 		case IPA_RC_SBP_IQD_CURRENT_SECOND:
1229 			rc = -EBUSY;
1230 			dev_err(&card->gdev->dev,
1231 	"The device is already a secondary Bridge Port\n");
1232 			break;
1233 		case IPA_RC_SBP_OSA_LIMIT_SECOND:
1234 		case IPA_RC_SBP_IQD_LIMIT_SECOND:
1235 			rc = -EEXIST;
1236 			dev_err(&card->gdev->dev,
1237 	"The LAN cannot have more secondary Bridge Ports\n");
1238 			break;
1239 		case IPA_RC_SBP_OSA_CURRENT_PRIMARY:
1240 		case IPA_RC_SBP_IQD_CURRENT_PRIMARY:
1241 			rc = -EBUSY;
1242 			dev_err(&card->gdev->dev,
1243 	"The device is already a primary Bridge Port\n");
1244 			break;
1245 		case IPA_RC_SBP_OSA_NOT_AUTHD_BY_ZMAN:
1246 		case IPA_RC_SBP_IQD_NOT_AUTHD_BY_ZMAN:
1247 			rc = -EACCES;
1248 			dev_err(&card->gdev->dev,
1249 	"The device is not authorized to be a Bridge Port\n");
1250 			break;
1251 		default:
1252 			rc = -EIO;
1253 		}
1254 	} else {
1255 		switch (ipa_rc) {
1256 		case IPA_RC_NOTSUPP:
1257 			rc = -EOPNOTSUPP;
1258 			break;
1259 		case IPA_RC_UNSUPPORTED_COMMAND:
1260 			rc = -EOPNOTSUPP;
1261 			break;
1262 		default:
1263 			rc = -EIO;
1264 		}
1265 	}
1266 
1267 	if (rc) {
1268 		QETH_CARD_TEXT_(card, 2, "SBPi%04x", ipa_rc);
1269 		QETH_CARD_TEXT_(card, 2, "SBPc%04x", sbp_rc);
1270 	}
1271 	return rc;
1272 }
1273 
1274 static struct qeth_cmd_buffer *qeth_sbp_build_cmd(struct qeth_card *card,
1275 						  enum qeth_ipa_sbp_cmd sbp_cmd,
1276 						  unsigned int data_length)
1277 {
1278 	enum qeth_ipa_cmds ipa_cmd = IS_IQD(card) ? IPA_CMD_SETBRIDGEPORT_IQD :
1279 						    IPA_CMD_SETBRIDGEPORT_OSA;
1280 	struct qeth_ipacmd_sbp_hdr *hdr;
1281 	struct qeth_cmd_buffer *iob;
1282 
1283 	iob = qeth_ipa_alloc_cmd(card, ipa_cmd, QETH_PROT_NONE,
1284 				 data_length +
1285 				 offsetof(struct qeth_ipacmd_setbridgeport,
1286 					  data));
1287 	if (!iob)
1288 		return iob;
1289 
1290 	hdr = &__ipa_cmd(iob)->data.sbp.hdr;
1291 	hdr->cmdlength = sizeof(*hdr) + data_length;
1292 	hdr->command_code = sbp_cmd;
1293 	hdr->used_total = 1;
1294 	hdr->seq_no = 1;
1295 	return iob;
1296 }
1297 
1298 static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1299 	struct qeth_reply *reply, unsigned long data)
1300 {
1301 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1302 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1303 	int rc;
1304 
1305 	QETH_CARD_TEXT(card, 2, "brqsupcb");
1306 	rc = qeth_bridgeport_makerc(card, cmd);
1307 	if (rc)
1308 		return rc;
1309 
1310 	cbctl->data.supported =
1311 		cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1312 	return 0;
1313 }
1314 
1315 /**
1316  * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1317  * @card:			     qeth_card structure pointer.
1318  *
1319  * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1320  * strucutre: card->options.sbp.supported_funcs.
1321  */
1322 static void qeth_bridgeport_query_support(struct qeth_card *card)
1323 {
1324 	struct qeth_cmd_buffer *iob;
1325 	struct _qeth_sbp_cbctl cbctl;
1326 
1327 	QETH_CARD_TEXT(card, 2, "brqsuppo");
1328 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_COMMANDS_SUPPORTED,
1329 				 SBP_DATA_SIZEOF(query_cmds_supp));
1330 	if (!iob)
1331 		return;
1332 
1333 	if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1334 			      &cbctl)) {
1335 		card->options.sbp.role = QETH_SBP_ROLE_NONE;
1336 		card->options.sbp.supported_funcs = 0;
1337 		return;
1338 	}
1339 	card->options.sbp.supported_funcs = cbctl.data.supported;
1340 }
1341 
1342 static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1343 	struct qeth_reply *reply, unsigned long data)
1344 {
1345 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1346 	struct qeth_sbp_query_ports *qports = &cmd->data.sbp.data.query_ports;
1347 	struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1348 	int rc;
1349 
1350 	QETH_CARD_TEXT(card, 2, "brqprtcb");
1351 	rc = qeth_bridgeport_makerc(card, cmd);
1352 	if (rc)
1353 		return rc;
1354 
1355 	if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1356 		QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1357 		return -EINVAL;
1358 	}
1359 	/* first entry contains the state of the local port */
1360 	if (qports->num_entries > 0) {
1361 		if (cbctl->data.qports.role)
1362 			*cbctl->data.qports.role = qports->entry[0].role;
1363 		if (cbctl->data.qports.state)
1364 			*cbctl->data.qports.state = qports->entry[0].state;
1365 	}
1366 	return 0;
1367 }
1368 
1369 /**
1370  * qeth_bridgeport_query_ports() - query local bridgeport status.
1371  * @card:			   qeth_card structure pointer.
1372  * @role:   Role of the port: 0-none, 1-primary, 2-secondary.
1373  * @state:  State of the port: 0-inactive, 1-standby, 2-active.
1374  *
1375  * Returns negative errno-compatible error indication or 0 on success.
1376  *
1377  * 'role' and 'state' are not updated in case of hardware operation failure.
1378  */
1379 int qeth_bridgeport_query_ports(struct qeth_card *card,
1380 	enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1381 {
1382 	struct qeth_cmd_buffer *iob;
1383 	struct _qeth_sbp_cbctl cbctl = {
1384 		.data = {
1385 			.qports = {
1386 				.role = role,
1387 				.state = state,
1388 			},
1389 		},
1390 	};
1391 
1392 	QETH_CARD_TEXT(card, 2, "brqports");
1393 	if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1394 		return -EOPNOTSUPP;
1395 	iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_BRIDGE_PORTS, 0);
1396 	if (!iob)
1397 		return -ENOMEM;
1398 
1399 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1400 				 &cbctl);
1401 }
1402 
1403 static int qeth_bridgeport_set_cb(struct qeth_card *card,
1404 	struct qeth_reply *reply, unsigned long data)
1405 {
1406 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1407 
1408 	QETH_CARD_TEXT(card, 2, "brsetrcb");
1409 	return qeth_bridgeport_makerc(card, cmd);
1410 }
1411 
1412 /**
1413  * qeth_bridgeport_setrole() - Assign primary role to the port.
1414  * @card:		       qeth_card structure pointer.
1415  * @role:		       Role to assign.
1416  *
1417  * Returns negative errno-compatible error indication or 0 on success.
1418  */
1419 int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
1420 {
1421 	struct qeth_cmd_buffer *iob;
1422 	enum qeth_ipa_sbp_cmd setcmd;
1423 	unsigned int cmdlength = 0;
1424 
1425 	QETH_CARD_TEXT(card, 2, "brsetrol");
1426 	switch (role) {
1427 	case QETH_SBP_ROLE_NONE:
1428 		setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
1429 		break;
1430 	case QETH_SBP_ROLE_PRIMARY:
1431 		setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
1432 		cmdlength = SBP_DATA_SIZEOF(set_primary);
1433 		break;
1434 	case QETH_SBP_ROLE_SECONDARY:
1435 		setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
1436 		break;
1437 	default:
1438 		return -EINVAL;
1439 	}
1440 	if (!(card->options.sbp.supported_funcs & setcmd))
1441 		return -EOPNOTSUPP;
1442 	iob = qeth_sbp_build_cmd(card, setcmd, cmdlength);
1443 	if (!iob)
1444 		return -ENOMEM;
1445 
1446 	return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb, NULL);
1447 }
1448 
1449 /**
1450  * qeth_anset_makerc() - derive "traditional" error from hardware codes.
1451  * @card:		      qeth_card structure pointer, for debug messages.
1452  *
1453  * Returns negative errno-compatible error indication or 0 on success.
1454  */
1455 static int qeth_anset_makerc(struct qeth_card *card, int pnso_rc, u16 response)
1456 {
1457 	int rc;
1458 
1459 	if (pnso_rc == 0)
1460 		switch (response) {
1461 		case 0x0001:
1462 			rc = 0;
1463 			break;
1464 		case 0x0004:
1465 		case 0x0100:
1466 		case 0x0106:
1467 			rc = -EOPNOTSUPP;
1468 			dev_err(&card->gdev->dev,
1469 				"Setting address notification failed\n");
1470 			break;
1471 		case 0x0107:
1472 			rc = -EAGAIN;
1473 			break;
1474 		default:
1475 			rc = -EIO;
1476 		}
1477 	else
1478 		rc = -EIO;
1479 
1480 	if (rc) {
1481 		QETH_CARD_TEXT_(card, 2, "SBPp%04x", pnso_rc);
1482 		QETH_CARD_TEXT_(card, 2, "SBPr%04x", response);
1483 	}
1484 	return rc;
1485 }
1486 
1487 static void qeth_bridgeport_an_set_cb(void *priv,
1488 		enum qdio_brinfo_entry_type type, void *entry)
1489 {
1490 	struct qeth_card *card = (struct qeth_card *)priv;
1491 	struct qdio_brinfo_entry_l2 *l2entry;
1492 	u8 code;
1493 
1494 	if (type != l2_addr_lnid) {
1495 		WARN_ON_ONCE(1);
1496 		return;
1497 	}
1498 
1499 	l2entry = (struct qdio_brinfo_entry_l2 *)entry;
1500 	code = IPA_ADDR_CHANGE_CODE_MACADDR;
1501 	if (l2entry->addr_lnid.lnid < VLAN_N_VID)
1502 		code |= IPA_ADDR_CHANGE_CODE_VLANID;
1503 	qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
1504 		(struct net_if_token *)&l2entry->nit,
1505 		(struct mac_addr_lnid *)&l2entry->addr_lnid);
1506 }
1507 
1508 /**
1509  * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
1510  * @card:		      qeth_card structure pointer.
1511  * @enable:		      0 - disable, non-zero - enable notifications
1512  *
1513  * Returns negative errno-compatible error indication or 0 on success.
1514  *
1515  * On enable, emits a series of address notifications udev events for all
1516  * currently registered hosts.
1517  */
1518 int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
1519 {
1520 	int rc;
1521 	u16 response;
1522 	struct ccw_device *ddev;
1523 	struct subchannel_id schid;
1524 
1525 	if (!card->options.sbp.supported_funcs)
1526 		return -EOPNOTSUPP;
1527 	ddev = CARD_DDEV(card);
1528 	ccw_device_get_schid(ddev, &schid);
1529 
1530 	if (enable) {
1531 		qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
1532 		rc = qdio_pnso_brinfo(schid, 1, &response,
1533 			qeth_bridgeport_an_set_cb, card);
1534 	} else
1535 		rc = qdio_pnso_brinfo(schid, 0, &response, NULL, NULL);
1536 	return qeth_anset_makerc(card, rc, response);
1537 }
1538 
1539 static bool qeth_bridgeport_is_in_use(struct qeth_card *card)
1540 {
1541 	return (card->options.sbp.role || card->options.sbp.reflect_promisc ||
1542 		card->options.sbp.hostnotification);
1543 }
1544 
1545 /* VNIC Characteristics support */
1546 
1547 /* handle VNICC IPA command return codes; convert to error codes */
1548 static int qeth_l2_vnicc_makerc(struct qeth_card *card, u16 ipa_rc)
1549 {
1550 	int rc;
1551 
1552 	switch (ipa_rc) {
1553 	case IPA_RC_SUCCESS:
1554 		return ipa_rc;
1555 	case IPA_RC_L2_UNSUPPORTED_CMD:
1556 	case IPA_RC_NOTSUPP:
1557 		rc = -EOPNOTSUPP;
1558 		break;
1559 	case IPA_RC_VNICC_OOSEQ:
1560 		rc = -EALREADY;
1561 		break;
1562 	case IPA_RC_VNICC_VNICBP:
1563 		rc = -EBUSY;
1564 		break;
1565 	case IPA_RC_L2_ADDR_TABLE_FULL:
1566 		rc = -ENOSPC;
1567 		break;
1568 	case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
1569 		rc = -EACCES;
1570 		break;
1571 	default:
1572 		rc = -EIO;
1573 	}
1574 
1575 	QETH_CARD_TEXT_(card, 2, "err%04x", ipa_rc);
1576 	return rc;
1577 }
1578 
1579 /* generic VNICC request call back */
1580 static int qeth_l2_vnicc_request_cb(struct qeth_card *card,
1581 				    struct qeth_reply *reply,
1582 				    unsigned long data)
1583 {
1584 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1585 	struct qeth_ipacmd_vnicc *rep = &cmd->data.vnicc;
1586 	u32 sub_cmd = cmd->data.vnicc.hdr.sub_command;
1587 
1588 	QETH_CARD_TEXT(card, 2, "vniccrcb");
1589 	if (cmd->hdr.return_code)
1590 		return qeth_l2_vnicc_makerc(card, cmd->hdr.return_code);
1591 	/* return results to caller */
1592 	card->options.vnicc.sup_chars = rep->vnicc_cmds.supported;
1593 	card->options.vnicc.cur_chars = rep->vnicc_cmds.enabled;
1594 
1595 	if (sub_cmd == IPA_VNICC_QUERY_CMDS)
1596 		*(u32 *)reply->param = rep->data.query_cmds.sup_cmds;
1597 	else if (sub_cmd == IPA_VNICC_GET_TIMEOUT)
1598 		*(u32 *)reply->param = rep->data.getset_timeout.timeout;
1599 
1600 	return 0;
1601 }
1602 
1603 static struct qeth_cmd_buffer *qeth_l2_vnicc_build_cmd(struct qeth_card *card,
1604 						       u32 vnicc_cmd,
1605 						       unsigned int data_length)
1606 {
1607 	struct qeth_ipacmd_vnicc_hdr *hdr;
1608 	struct qeth_cmd_buffer *iob;
1609 
1610 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_VNICC, QETH_PROT_NONE,
1611 				 data_length +
1612 				 offsetof(struct qeth_ipacmd_vnicc, data));
1613 	if (!iob)
1614 		return NULL;
1615 
1616 	hdr = &__ipa_cmd(iob)->data.vnicc.hdr;
1617 	hdr->data_length = sizeof(*hdr) + data_length;
1618 	hdr->sub_command = vnicc_cmd;
1619 	return iob;
1620 }
1621 
1622 /* VNICC query VNIC characteristics request */
1623 static int qeth_l2_vnicc_query_chars(struct qeth_card *card)
1624 {
1625 	struct qeth_cmd_buffer *iob;
1626 
1627 	QETH_CARD_TEXT(card, 2, "vniccqch");
1628 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CHARS, 0);
1629 	if (!iob)
1630 		return -ENOMEM;
1631 
1632 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1633 }
1634 
1635 /* VNICC query sub commands request */
1636 static int qeth_l2_vnicc_query_cmds(struct qeth_card *card, u32 vnic_char,
1637 				    u32 *sup_cmds)
1638 {
1639 	struct qeth_cmd_buffer *iob;
1640 
1641 	QETH_CARD_TEXT(card, 2, "vniccqcm");
1642 	iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CMDS,
1643 				      VNICC_DATA_SIZEOF(query_cmds));
1644 	if (!iob)
1645 		return -ENOMEM;
1646 
1647 	__ipa_cmd(iob)->data.vnicc.data.query_cmds.vnic_char = vnic_char;
1648 
1649 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, sup_cmds);
1650 }
1651 
1652 /* VNICC enable/disable characteristic request */
1653 static int qeth_l2_vnicc_set_char(struct qeth_card *card, u32 vnic_char,
1654 				      u32 cmd)
1655 {
1656 	struct qeth_cmd_buffer *iob;
1657 
1658 	QETH_CARD_TEXT(card, 2, "vniccedc");
1659 	iob = qeth_l2_vnicc_build_cmd(card, cmd, VNICC_DATA_SIZEOF(set_char));
1660 	if (!iob)
1661 		return -ENOMEM;
1662 
1663 	__ipa_cmd(iob)->data.vnicc.data.set_char.vnic_char = vnic_char;
1664 
1665 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1666 }
1667 
1668 /* VNICC get/set timeout for characteristic request */
1669 static int qeth_l2_vnicc_getset_timeout(struct qeth_card *card, u32 vnicc,
1670 					u32 cmd, u32 *timeout)
1671 {
1672 	struct qeth_vnicc_getset_timeout *getset_timeout;
1673 	struct qeth_cmd_buffer *iob;
1674 
1675 	QETH_CARD_TEXT(card, 2, "vniccgst");
1676 	iob = qeth_l2_vnicc_build_cmd(card, cmd,
1677 				      VNICC_DATA_SIZEOF(getset_timeout));
1678 	if (!iob)
1679 		return -ENOMEM;
1680 
1681 	getset_timeout = &__ipa_cmd(iob)->data.vnicc.data.getset_timeout;
1682 	getset_timeout->vnic_char = vnicc;
1683 
1684 	if (cmd == IPA_VNICC_SET_TIMEOUT)
1685 		getset_timeout->timeout = *timeout;
1686 
1687 	return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, timeout);
1688 }
1689 
1690 /* set current VNICC flag state; called from sysfs store function */
1691 int qeth_l2_vnicc_set_state(struct qeth_card *card, u32 vnicc, bool state)
1692 {
1693 	int rc = 0;
1694 	u32 cmd;
1695 
1696 	QETH_CARD_TEXT(card, 2, "vniccsch");
1697 
1698 	/* check if characteristic and enable/disable are supported */
1699 	if (!(card->options.vnicc.sup_chars & vnicc) ||
1700 	    !(card->options.vnicc.set_char_sup & vnicc))
1701 		return -EOPNOTSUPP;
1702 
1703 	if (qeth_bridgeport_is_in_use(card))
1704 		return -EBUSY;
1705 
1706 	/* set enable/disable command and store wanted characteristic */
1707 	if (state) {
1708 		cmd = IPA_VNICC_ENABLE;
1709 		card->options.vnicc.wanted_chars |= vnicc;
1710 	} else {
1711 		cmd = IPA_VNICC_DISABLE;
1712 		card->options.vnicc.wanted_chars &= ~vnicc;
1713 	}
1714 
1715 	/* do we need to do anything? */
1716 	if (card->options.vnicc.cur_chars == card->options.vnicc.wanted_chars)
1717 		return rc;
1718 
1719 	/* if card is not ready, simply stop here */
1720 	if (!qeth_card_hw_is_reachable(card)) {
1721 		if (state)
1722 			card->options.vnicc.cur_chars |= vnicc;
1723 		else
1724 			card->options.vnicc.cur_chars &= ~vnicc;
1725 		return rc;
1726 	}
1727 
1728 	rc = qeth_l2_vnicc_set_char(card, vnicc, cmd);
1729 	if (rc)
1730 		card->options.vnicc.wanted_chars =
1731 			card->options.vnicc.cur_chars;
1732 	else {
1733 		/* successful online VNICC change; handle special cases */
1734 		if (state && vnicc == QETH_VNICC_RX_BCAST)
1735 			card->options.vnicc.rx_bcast_enabled = true;
1736 		if (!state && vnicc == QETH_VNICC_LEARNING)
1737 			qeth_l2_vnicc_recover_timeout(card, vnicc,
1738 					&card->options.vnicc.learning_timeout);
1739 	}
1740 
1741 	return rc;
1742 }
1743 
1744 /* get current VNICC flag state; called from sysfs show function */
1745 int qeth_l2_vnicc_get_state(struct qeth_card *card, u32 vnicc, bool *state)
1746 {
1747 	int rc = 0;
1748 
1749 	QETH_CARD_TEXT(card, 2, "vniccgch");
1750 
1751 	/* check if characteristic is supported */
1752 	if (!(card->options.vnicc.sup_chars & vnicc))
1753 		return -EOPNOTSUPP;
1754 
1755 	if (qeth_bridgeport_is_in_use(card))
1756 		return -EBUSY;
1757 
1758 	/* if card is ready, query current VNICC state */
1759 	if (qeth_card_hw_is_reachable(card))
1760 		rc = qeth_l2_vnicc_query_chars(card);
1761 
1762 	*state = (card->options.vnicc.cur_chars & vnicc) ? true : false;
1763 	return rc;
1764 }
1765 
1766 /* set VNICC timeout; called from sysfs store function. Currently, only learning
1767  * supports timeout
1768  */
1769 int qeth_l2_vnicc_set_timeout(struct qeth_card *card, u32 timeout)
1770 {
1771 	int rc = 0;
1772 
1773 	QETH_CARD_TEXT(card, 2, "vniccsto");
1774 
1775 	/* check if characteristic and set_timeout are supported */
1776 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
1777 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
1778 		return -EOPNOTSUPP;
1779 
1780 	if (qeth_bridgeport_is_in_use(card))
1781 		return -EBUSY;
1782 
1783 	/* do we need to do anything? */
1784 	if (card->options.vnicc.learning_timeout == timeout)
1785 		return rc;
1786 
1787 	/* if card is not ready, simply store the value internally and return */
1788 	if (!qeth_card_hw_is_reachable(card)) {
1789 		card->options.vnicc.learning_timeout = timeout;
1790 		return rc;
1791 	}
1792 
1793 	/* send timeout value to card; if successful, store value internally */
1794 	rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
1795 					  IPA_VNICC_SET_TIMEOUT, &timeout);
1796 	if (!rc)
1797 		card->options.vnicc.learning_timeout = timeout;
1798 
1799 	return rc;
1800 }
1801 
1802 /* get current VNICC timeout; called from sysfs show function. Currently, only
1803  * learning supports timeout
1804  */
1805 int qeth_l2_vnicc_get_timeout(struct qeth_card *card, u32 *timeout)
1806 {
1807 	int rc = 0;
1808 
1809 	QETH_CARD_TEXT(card, 2, "vniccgto");
1810 
1811 	/* check if characteristic and get_timeout are supported */
1812 	if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
1813 	    !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
1814 		return -EOPNOTSUPP;
1815 
1816 	if (qeth_bridgeport_is_in_use(card))
1817 		return -EBUSY;
1818 
1819 	/* if card is ready, get timeout. Otherwise, just return stored value */
1820 	*timeout = card->options.vnicc.learning_timeout;
1821 	if (qeth_card_hw_is_reachable(card))
1822 		rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
1823 						  IPA_VNICC_GET_TIMEOUT,
1824 						  timeout);
1825 
1826 	return rc;
1827 }
1828 
1829 /* check if VNICC is currently enabled */
1830 bool qeth_l2_vnicc_is_in_use(struct qeth_card *card)
1831 {
1832 	if (!card->options.vnicc.sup_chars)
1833 		return false;
1834 	/* default values are only OK if rx_bcast was not enabled by user
1835 	 * or the card is offline.
1836 	 */
1837 	if (card->options.vnicc.cur_chars == QETH_VNICC_DEFAULT) {
1838 		if (!card->options.vnicc.rx_bcast_enabled ||
1839 		    !qeth_card_hw_is_reachable(card))
1840 			return false;
1841 	}
1842 	return true;
1843 }
1844 
1845 /* recover user timeout setting */
1846 static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
1847 					  u32 *timeout)
1848 {
1849 	if (card->options.vnicc.sup_chars & vnicc &&
1850 	    card->options.vnicc.getset_timeout_sup & vnicc &&
1851 	    !qeth_l2_vnicc_getset_timeout(card, vnicc, IPA_VNICC_SET_TIMEOUT,
1852 					  timeout))
1853 		return false;
1854 	*timeout = QETH_VNICC_DEFAULT_TIMEOUT;
1855 	return true;
1856 }
1857 
1858 /* recover user characteristic setting */
1859 static bool qeth_l2_vnicc_recover_char(struct qeth_card *card, u32 vnicc,
1860 				       bool enable)
1861 {
1862 	u32 cmd = enable ? IPA_VNICC_ENABLE : IPA_VNICC_DISABLE;
1863 
1864 	if (card->options.vnicc.sup_chars & vnicc &&
1865 	    card->options.vnicc.set_char_sup & vnicc &&
1866 	    !qeth_l2_vnicc_set_char(card, vnicc, cmd))
1867 		return false;
1868 	card->options.vnicc.wanted_chars &= ~vnicc;
1869 	card->options.vnicc.wanted_chars |= QETH_VNICC_DEFAULT & vnicc;
1870 	return true;
1871 }
1872 
1873 /* (re-)initialize VNICC */
1874 static void qeth_l2_vnicc_init(struct qeth_card *card)
1875 {
1876 	u32 *timeout = &card->options.vnicc.learning_timeout;
1877 	bool enable, error = false;
1878 	unsigned int chars_len, i;
1879 	unsigned long chars_tmp;
1880 	u32 sup_cmds, vnicc;
1881 
1882 	QETH_CARD_TEXT(card, 2, "vniccini");
1883 	/* reset rx_bcast */
1884 	card->options.vnicc.rx_bcast_enabled = 0;
1885 	/* initial query and storage of VNIC characteristics */
1886 	if (qeth_l2_vnicc_query_chars(card)) {
1887 		if (card->options.vnicc.wanted_chars != QETH_VNICC_DEFAULT ||
1888 		    *timeout != QETH_VNICC_DEFAULT_TIMEOUT)
1889 			dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
1890 		/* fail quietly if user didn't change the default config */
1891 		card->options.vnicc.sup_chars = 0;
1892 		card->options.vnicc.cur_chars = 0;
1893 		card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
1894 		return;
1895 	}
1896 	/* get supported commands for each supported characteristic */
1897 	chars_tmp = card->options.vnicc.sup_chars;
1898 	chars_len = sizeof(card->options.vnicc.sup_chars) * BITS_PER_BYTE;
1899 	for_each_set_bit(i, &chars_tmp, chars_len) {
1900 		vnicc = BIT(i);
1901 		if (qeth_l2_vnicc_query_cmds(card, vnicc, &sup_cmds)) {
1902 			sup_cmds = 0;
1903 			error = true;
1904 		}
1905 		if ((sup_cmds & IPA_VNICC_SET_TIMEOUT) &&
1906 		    (sup_cmds & IPA_VNICC_GET_TIMEOUT))
1907 			card->options.vnicc.getset_timeout_sup |= vnicc;
1908 		else
1909 			card->options.vnicc.getset_timeout_sup &= ~vnicc;
1910 		if ((sup_cmds & IPA_VNICC_ENABLE) &&
1911 		    (sup_cmds & IPA_VNICC_DISABLE))
1912 			card->options.vnicc.set_char_sup |= vnicc;
1913 		else
1914 			card->options.vnicc.set_char_sup &= ~vnicc;
1915 	}
1916 	/* enforce assumed default values and recover settings, if changed  */
1917 	error |= qeth_l2_vnicc_recover_timeout(card, QETH_VNICC_LEARNING,
1918 					       timeout);
1919 	/* Change chars, if necessary  */
1920 	chars_tmp = card->options.vnicc.wanted_chars ^
1921 		    card->options.vnicc.cur_chars;
1922 	chars_len = sizeof(card->options.vnicc.wanted_chars) * BITS_PER_BYTE;
1923 	for_each_set_bit(i, &chars_tmp, chars_len) {
1924 		vnicc = BIT(i);
1925 		enable = card->options.vnicc.wanted_chars & vnicc;
1926 		error |= qeth_l2_vnicc_recover_char(card, vnicc, enable);
1927 	}
1928 	if (error)
1929 		dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
1930 }
1931 
1932 /* configure default values of VNIC characteristics */
1933 static void qeth_l2_vnicc_set_defaults(struct qeth_card *card)
1934 {
1935 	/* characteristics values */
1936 	card->options.vnicc.sup_chars = QETH_VNICC_ALL;
1937 	card->options.vnicc.cur_chars = QETH_VNICC_DEFAULT;
1938 	card->options.vnicc.learning_timeout = QETH_VNICC_DEFAULT_TIMEOUT;
1939 	/* supported commands */
1940 	card->options.vnicc.set_char_sup = QETH_VNICC_ALL;
1941 	card->options.vnicc.getset_timeout_sup = QETH_VNICC_LEARNING;
1942 	/* settings wanted by users */
1943 	card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
1944 }
1945 
1946 module_init(qeth_l2_init);
1947 module_exit(qeth_l2_exit);
1948 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
1949 MODULE_DESCRIPTION("qeth layer 2 discipline");
1950 MODULE_LICENSE("GPL");
1951