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