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