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