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