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