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