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