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