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