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