Loading...
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/if_bridge.h>
21#include <linux/list.h>
22#include <linux/hash.h>
23#include <linux/hashtable.h>
24#include <net/switchdev.h>
25#include <asm/chsc.h>
26#include <asm/css_chars.h>
27#include <asm/setup.h>
28#include "qeth_core.h"
29#include "qeth_l2.h"
30
31static int qeth_l2_setdelmac_makerc(struct qeth_card *card, u16 retcode)
32{
33 int rc;
34
35 if (retcode)
36 QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
37 switch (retcode) {
38 case IPA_RC_SUCCESS:
39 rc = 0;
40 break;
41 case IPA_RC_L2_UNSUPPORTED_CMD:
42 rc = -EOPNOTSUPP;
43 break;
44 case IPA_RC_L2_ADDR_TABLE_FULL:
45 rc = -ENOSPC;
46 break;
47 case IPA_RC_L2_DUP_MAC:
48 case IPA_RC_L2_DUP_LAYER3_MAC:
49 rc = -EADDRINUSE;
50 break;
51 case IPA_RC_L2_MAC_NOT_AUTH_BY_HYP:
52 case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
53 rc = -EADDRNOTAVAIL;
54 break;
55 case IPA_RC_L2_MAC_NOT_FOUND:
56 rc = -ENOENT;
57 break;
58 default:
59 rc = -EIO;
60 break;
61 }
62 return rc;
63}
64
65static int qeth_l2_send_setdelmac_cb(struct qeth_card *card,
66 struct qeth_reply *reply,
67 unsigned long data)
68{
69 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
70
71 return qeth_l2_setdelmac_makerc(card, cmd->hdr.return_code);
72}
73
74static int qeth_l2_send_setdelmac(struct qeth_card *card, const __u8 *mac,
75 enum qeth_ipa_cmds ipacmd)
76{
77 struct qeth_ipa_cmd *cmd;
78 struct qeth_cmd_buffer *iob;
79
80 QETH_CARD_TEXT(card, 2, "L2sdmac");
81 iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
82 IPA_DATA_SIZEOF(setdelmac));
83 if (!iob)
84 return -ENOMEM;
85 cmd = __ipa_cmd(iob);
86 cmd->data.setdelmac.mac_length = ETH_ALEN;
87 ether_addr_copy(cmd->data.setdelmac.mac, mac);
88 return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelmac_cb, NULL);
89}
90
91static int qeth_l2_send_setmac(struct qeth_card *card, const __u8 *mac)
92{
93 int rc;
94
95 QETH_CARD_TEXT(card, 2, "L2Setmac");
96 rc = qeth_l2_send_setdelmac(card, mac, IPA_CMD_SETVMAC);
97 if (rc == 0) {
98 dev_info(&card->gdev->dev,
99 "MAC address %pM successfully registered\n", mac);
100 } else {
101 switch (rc) {
102 case -EADDRINUSE:
103 dev_warn(&card->gdev->dev,
104 "MAC address %pM already exists\n", mac);
105 break;
106 case -EADDRNOTAVAIL:
107 dev_warn(&card->gdev->dev,
108 "MAC address %pM is not authorized\n", mac);
109 break;
110 }
111 }
112 return rc;
113}
114
115static int qeth_l2_write_mac(struct qeth_card *card, u8 *mac)
116{
117 enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
118 IPA_CMD_SETGMAC : IPA_CMD_SETVMAC;
119 int rc;
120
121 QETH_CARD_TEXT(card, 2, "L2Wmac");
122 rc = qeth_l2_send_setdelmac(card, mac, cmd);
123 if (rc == -EADDRINUSE)
124 QETH_DBF_MESSAGE(2, "MAC address %012llx is already registered on device %x\n",
125 ether_addr_to_u64(mac), CARD_DEVID(card));
126 else if (rc)
127 QETH_DBF_MESSAGE(2, "Failed to register MAC address %012llx on device %x: %d\n",
128 ether_addr_to_u64(mac), CARD_DEVID(card), rc);
129 return rc;
130}
131
132static int qeth_l2_remove_mac(struct qeth_card *card, u8 *mac)
133{
134 enum qeth_ipa_cmds cmd = is_multicast_ether_addr(mac) ?
135 IPA_CMD_DELGMAC : IPA_CMD_DELVMAC;
136 int rc;
137
138 QETH_CARD_TEXT(card, 2, "L2Rmac");
139 rc = qeth_l2_send_setdelmac(card, mac, cmd);
140 if (rc)
141 QETH_DBF_MESSAGE(2, "Failed to delete MAC address %012llx on device %x: %d\n",
142 ether_addr_to_u64(mac), CARD_DEVID(card), rc);
143 return rc;
144}
145
146static void qeth_l2_drain_rx_mode_cache(struct qeth_card *card)
147{
148 struct qeth_mac *mac;
149 struct hlist_node *tmp;
150 int i;
151
152 hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
153 hash_del(&mac->hnode);
154 kfree(mac);
155 }
156}
157
158static void qeth_l2_fill_header(struct qeth_qdio_out_q *queue,
159 struct qeth_hdr *hdr, struct sk_buff *skb,
160 __be16 proto, unsigned int data_len)
161{
162 int cast_type = qeth_get_ether_cast_type(skb);
163 struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
164
165 hdr->hdr.l2.pkt_length = data_len;
166
167 if (skb_is_gso(skb)) {
168 hdr->hdr.l2.id = QETH_HEADER_TYPE_L2_TSO;
169 } else {
170 hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
171 if (skb->ip_summed == CHECKSUM_PARTIAL)
172 qeth_tx_csum(skb, &hdr->hdr.l2.flags[1], proto);
173 }
174
175 /* set byte byte 3 to casting flags */
176 if (cast_type == RTN_MULTICAST)
177 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_MULTICAST;
178 else if (cast_type == RTN_BROADCAST)
179 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_BROADCAST;
180 else
181 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_UNICAST;
182
183 /* VSWITCH relies on the VLAN
184 * information to be present in
185 * the QDIO header */
186 if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
187 hdr->hdr.l2.flags[2] |= QETH_LAYER2_FLAG_VLAN;
188 hdr->hdr.l2.vlan_id = ntohs(veth->h_vlan_TCI);
189 }
190}
191
192static int qeth_l2_setdelvlan_makerc(struct qeth_card *card, u16 retcode)
193{
194 if (retcode)
195 QETH_CARD_TEXT_(card, 2, "err%04x", retcode);
196
197 switch (retcode) {
198 case IPA_RC_SUCCESS:
199 return 0;
200 case IPA_RC_L2_INVALID_VLAN_ID:
201 return -EINVAL;
202 case IPA_RC_L2_DUP_VLAN_ID:
203 return -EEXIST;
204 case IPA_RC_L2_VLAN_ID_NOT_FOUND:
205 return -ENOENT;
206 case IPA_RC_L2_VLAN_ID_NOT_ALLOWED:
207 return -EPERM;
208 default:
209 return -EIO;
210 }
211}
212
213static int qeth_l2_send_setdelvlan_cb(struct qeth_card *card,
214 struct qeth_reply *reply,
215 unsigned long data)
216{
217 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
218
219 QETH_CARD_TEXT(card, 2, "L2sdvcb");
220 if (cmd->hdr.return_code) {
221 QETH_DBF_MESSAGE(2, "Error in processing VLAN %u on device %x: %#x.\n",
222 cmd->data.setdelvlan.vlan_id,
223 CARD_DEVID(card), cmd->hdr.return_code);
224 QETH_CARD_TEXT_(card, 2, "L2VL%4x", cmd->hdr.command);
225 }
226 return qeth_l2_setdelvlan_makerc(card, cmd->hdr.return_code);
227}
228
229static int qeth_l2_send_setdelvlan(struct qeth_card *card, __u16 i,
230 enum qeth_ipa_cmds ipacmd)
231{
232 struct qeth_ipa_cmd *cmd;
233 struct qeth_cmd_buffer *iob;
234
235 QETH_CARD_TEXT_(card, 4, "L2sdv%x", ipacmd);
236 iob = qeth_ipa_alloc_cmd(card, ipacmd, QETH_PROT_IPV4,
237 IPA_DATA_SIZEOF(setdelvlan));
238 if (!iob)
239 return -ENOMEM;
240 cmd = __ipa_cmd(iob);
241 cmd->data.setdelvlan.vlan_id = i;
242 return qeth_send_ipa_cmd(card, iob, qeth_l2_send_setdelvlan_cb, NULL);
243}
244
245static int qeth_l2_vlan_rx_add_vid(struct net_device *dev,
246 __be16 proto, u16 vid)
247{
248 struct qeth_card *card = dev->ml_priv;
249
250 QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
251 if (!vid)
252 return 0;
253
254 return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
255}
256
257static int qeth_l2_vlan_rx_kill_vid(struct net_device *dev,
258 __be16 proto, u16 vid)
259{
260 struct qeth_card *card = dev->ml_priv;
261
262 QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
263 if (!vid)
264 return 0;
265
266 return qeth_l2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
267}
268
269static void qeth_l2_set_pnso_mode(struct qeth_card *card,
270 enum qeth_pnso_mode mode)
271{
272 spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
273 WRITE_ONCE(card->info.pnso_mode, mode);
274 spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
275
276 if (mode == QETH_PNSO_NONE)
277 drain_workqueue(card->event_wq);
278}
279
280static void qeth_l2_dev2br_fdb_flush(struct qeth_card *card)
281{
282 struct switchdev_notifier_fdb_info info = {};
283
284 QETH_CARD_TEXT(card, 2, "fdbflush");
285
286 info.addr = NULL;
287 /* flush all VLANs: */
288 info.vid = 0;
289 info.added_by_user = false;
290 info.offloaded = true;
291
292 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
293 card->dev, &info.info, NULL);
294}
295
296static int qeth_l2_request_initial_mac(struct qeth_card *card)
297{
298 int rc = 0;
299
300 QETH_CARD_TEXT(card, 2, "l2reqmac");
301
302 if (MACHINE_IS_VM) {
303 rc = qeth_vm_request_mac(card);
304 if (!rc)
305 goto out;
306 QETH_DBF_MESSAGE(2, "z/VM MAC Service failed on device %x: %#x\n",
307 CARD_DEVID(card), rc);
308 QETH_CARD_TEXT_(card, 2, "err%04x", rc);
309 /* fall back to alternative mechanism: */
310 }
311
312 rc = qeth_setadpparms_change_macaddr(card);
313 if (!rc)
314 goto out;
315 QETH_DBF_MESSAGE(2, "READ_MAC Assist failed on device %x: %#x\n",
316 CARD_DEVID(card), rc);
317 QETH_CARD_TEXT_(card, 2, "1err%04x", rc);
318
319 /* Fall back once more, but some devices don't support a custom MAC
320 * address:
321 */
322 if (IS_OSM(card) || IS_OSX(card))
323 return (rc) ? rc : -EADDRNOTAVAIL;
324 eth_hw_addr_random(card->dev);
325
326out:
327 QETH_CARD_HEX(card, 2, card->dev->dev_addr, card->dev->addr_len);
328 return 0;
329}
330
331static void qeth_l2_register_dev_addr(struct qeth_card *card)
332{
333 if (!is_valid_ether_addr(card->dev->dev_addr))
334 qeth_l2_request_initial_mac(card);
335
336 if (!qeth_l2_send_setmac(card, card->dev->dev_addr))
337 card->info.dev_addr_is_registered = 1;
338 else
339 card->info.dev_addr_is_registered = 0;
340}
341
342static int qeth_l2_validate_addr(struct net_device *dev)
343{
344 struct qeth_card *card = dev->ml_priv;
345
346 if (card->info.dev_addr_is_registered)
347 return eth_validate_addr(dev);
348
349 QETH_CARD_TEXT(card, 4, "nomacadr");
350 return -EPERM;
351}
352
353static int qeth_l2_set_mac_address(struct net_device *dev, void *p)
354{
355 struct sockaddr *addr = p;
356 struct qeth_card *card = dev->ml_priv;
357 u8 old_addr[ETH_ALEN];
358 int rc = 0;
359
360 QETH_CARD_TEXT(card, 3, "setmac");
361
362 if (IS_OSM(card) || IS_OSX(card)) {
363 QETH_CARD_TEXT(card, 3, "setmcTYP");
364 return -EOPNOTSUPP;
365 }
366 QETH_CARD_HEX(card, 3, addr->sa_data, ETH_ALEN);
367 if (!is_valid_ether_addr(addr->sa_data))
368 return -EADDRNOTAVAIL;
369
370 /* don't register the same address twice */
371 if (ether_addr_equal_64bits(dev->dev_addr, addr->sa_data) &&
372 card->info.dev_addr_is_registered)
373 return 0;
374
375 /* add the new address, switch over, drop the old */
376 rc = qeth_l2_send_setmac(card, addr->sa_data);
377 if (rc)
378 return rc;
379 ether_addr_copy(old_addr, dev->dev_addr);
380 eth_hw_addr_set(dev, addr->sa_data);
381
382 if (card->info.dev_addr_is_registered)
383 qeth_l2_remove_mac(card, old_addr);
384 card->info.dev_addr_is_registered = 1;
385 return 0;
386}
387
388static void qeth_l2_promisc_to_bridge(struct qeth_card *card, bool enable)
389{
390 int role;
391 int rc;
392
393 QETH_CARD_TEXT(card, 3, "pmisc2br");
394
395 if (enable) {
396 if (card->options.sbp.reflect_promisc_primary)
397 role = QETH_SBP_ROLE_PRIMARY;
398 else
399 role = QETH_SBP_ROLE_SECONDARY;
400 } else
401 role = QETH_SBP_ROLE_NONE;
402
403 rc = qeth_bridgeport_setrole(card, role);
404 QETH_CARD_TEXT_(card, 2, "bpm%c%04x", enable ? '+' : '-', rc);
405 if (!rc) {
406 card->options.sbp.role = role;
407 card->info.promisc_mode = enable;
408 }
409}
410
411static void qeth_l2_set_promisc_mode(struct qeth_card *card)
412{
413 bool enable = card->dev->flags & IFF_PROMISC;
414
415 if (card->info.promisc_mode == enable)
416 return;
417
418 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) {
419 qeth_setadp_promisc_mode(card, enable);
420 } else {
421 mutex_lock(&card->sbp_lock);
422 if (card->options.sbp.reflect_promisc)
423 qeth_l2_promisc_to_bridge(card, enable);
424 mutex_unlock(&card->sbp_lock);
425 }
426}
427
428/* New MAC address is added to the hash table and marked to be written on card
429 * only if there is not in the hash table storage already
430 *
431*/
432static void qeth_l2_add_mac(struct qeth_card *card, struct netdev_hw_addr *ha)
433{
434 u32 mac_hash = get_unaligned((u32 *)(&ha->addr[2]));
435 struct qeth_mac *mac;
436
437 hash_for_each_possible(card->rx_mode_addrs, mac, hnode, mac_hash) {
438 if (ether_addr_equal_64bits(ha->addr, mac->mac_addr)) {
439 mac->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
440 return;
441 }
442 }
443
444 mac = kzalloc(sizeof(struct qeth_mac), GFP_ATOMIC);
445 if (!mac)
446 return;
447
448 ether_addr_copy(mac->mac_addr, ha->addr);
449 mac->disp_flag = QETH_DISP_ADDR_ADD;
450
451 hash_add(card->rx_mode_addrs, &mac->hnode, mac_hash);
452}
453
454static void qeth_l2_rx_mode_work(struct work_struct *work)
455{
456 struct qeth_card *card = container_of(work, struct qeth_card,
457 rx_mode_work);
458 struct net_device *dev = card->dev;
459 struct netdev_hw_addr *ha;
460 struct qeth_mac *mac;
461 struct hlist_node *tmp;
462 int i;
463 int rc;
464
465 QETH_CARD_TEXT(card, 3, "setmulti");
466
467 netif_addr_lock_bh(dev);
468 netdev_for_each_mc_addr(ha, dev)
469 qeth_l2_add_mac(card, ha);
470 netdev_for_each_uc_addr(ha, dev)
471 qeth_l2_add_mac(card, ha);
472 netif_addr_unlock_bh(dev);
473
474 hash_for_each_safe(card->rx_mode_addrs, i, tmp, mac, hnode) {
475 switch (mac->disp_flag) {
476 case QETH_DISP_ADDR_DELETE:
477 qeth_l2_remove_mac(card, mac->mac_addr);
478 hash_del(&mac->hnode);
479 kfree(mac);
480 break;
481 case QETH_DISP_ADDR_ADD:
482 rc = qeth_l2_write_mac(card, mac->mac_addr);
483 if (rc) {
484 hash_del(&mac->hnode);
485 kfree(mac);
486 break;
487 }
488 fallthrough;
489 default:
490 /* for next call to set_rx_mode(): */
491 mac->disp_flag = QETH_DISP_ADDR_DELETE;
492 }
493 }
494
495 qeth_l2_set_promisc_mode(card);
496}
497
498static netdev_tx_t qeth_l2_hard_start_xmit(struct sk_buff *skb,
499 struct net_device *dev)
500{
501 struct qeth_card *card = dev->ml_priv;
502 u16 txq = skb_get_queue_mapping(skb);
503 struct qeth_qdio_out_q *queue;
504 int rc;
505
506 if (!skb_is_gso(skb))
507 qdisc_skb_cb(skb)->pkt_len = skb->len;
508 if (IS_IQD(card))
509 txq = qeth_iqd_translate_txq(dev, txq);
510 queue = card->qdio.out_qs[txq];
511
512 rc = qeth_xmit(card, skb, queue, vlan_get_protocol(skb),
513 qeth_l2_fill_header);
514 if (!rc)
515 return NETDEV_TX_OK;
516
517 QETH_TXQ_STAT_INC(queue, tx_dropped);
518 kfree_skb(skb);
519 return NETDEV_TX_OK;
520}
521
522static u16 qeth_l2_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
523 struct net_device *sb_dev)
524{
525 return qeth_iqd_select_queue(dev, skb, qeth_get_ether_cast_type(skb),
526 sb_dev);
527}
528
529static void qeth_l2_set_rx_mode(struct net_device *dev)
530{
531 struct qeth_card *card = dev->ml_priv;
532
533 schedule_work(&card->rx_mode_work);
534}
535
536/**
537 * qeth_l2_pnso() - perform network subchannel operation
538 * @card: qeth_card structure pointer
539 * @oc: Operation Code
540 * @cnc: Boolean Change-Notification Control
541 * @cb: Callback function will be executed for each element
542 * of the address list
543 * @priv: Pointer to pass to the callback function.
544 *
545 * Collects network information in a network address list and calls the
546 * callback function for every entry in the list. If "change-notification-
547 * control" is set, further changes in the address list will be reported
548 * via the IPA command.
549 */
550static int qeth_l2_pnso(struct qeth_card *card, u8 oc, int cnc,
551 void (*cb)(void *priv, struct chsc_pnso_naid_l2 *entry),
552 void *priv)
553{
554 struct ccw_device *ddev = CARD_DDEV(card);
555 struct chsc_pnso_area *rr;
556 u32 prev_instance = 0;
557 int isfirstblock = 1;
558 int i, size, elems;
559 int rc;
560
561 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
562 if (rr == NULL)
563 return -ENOMEM;
564 do {
565 QETH_CARD_TEXT(card, 2, "PNSO");
566 /* on the first iteration, naihdr.resume_token will be zero */
567 rc = ccw_device_pnso(ddev, rr, oc, rr->naihdr.resume_token,
568 cnc);
569 if (rc)
570 continue;
571 if (cb == NULL)
572 continue;
573
574 size = rr->naihdr.naids;
575 if (size != sizeof(struct chsc_pnso_naid_l2)) {
576 WARN_ON_ONCE(1);
577 continue;
578 }
579
580 elems = (rr->response.length - sizeof(struct chsc_header) -
581 sizeof(struct chsc_pnso_naihdr)) / size;
582
583 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
584 /* Inform the caller that they need to scrap */
585 /* the data that was already reported via cb */
586 rc = -EAGAIN;
587 break;
588 }
589 isfirstblock = 0;
590 prev_instance = rr->naihdr.instance;
591 for (i = 0; i < elems; i++)
592 (*cb)(priv, &rr->entries[i]);
593 } while ((rc == -EBUSY) || (!rc && /* list stored */
594 /* resume token is non-zero => list incomplete */
595 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
596
597 if (rc)
598 QETH_CARD_TEXT_(card, 2, "PNrp%04x", rr->response.code);
599
600 free_page((unsigned long)rr);
601 return rc;
602}
603
604static bool qeth_is_my_net_if_token(struct qeth_card *card,
605 struct net_if_token *token)
606{
607 return ((card->info.ddev_devno == token->devnum) &&
608 (card->info.cssid == token->cssid) &&
609 (card->info.iid == token->iid) &&
610 (card->info.ssid == token->ssid) &&
611 (card->info.chpid == token->chpid) &&
612 (card->info.chid == token->chid));
613}
614
615/**
616 * qeth_l2_dev2br_fdb_notify() - update fdb of master bridge
617 * @card: qeth_card structure pointer
618 * @code: event bitmask: high order bit 0x80 set to
619 * 1 - removal of an object
620 * 0 - addition of an object
621 * Object type(s):
622 * 0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC
623 * @token: "network token" structure identifying 'physical' location
624 * of the target
625 * @addr_lnid: structure with MAC address and VLAN ID of the target
626 */
627static void qeth_l2_dev2br_fdb_notify(struct qeth_card *card, u8 code,
628 struct net_if_token *token,
629 struct mac_addr_lnid *addr_lnid)
630{
631 struct switchdev_notifier_fdb_info info = {};
632 u8 ntfy_mac[ETH_ALEN];
633
634 ether_addr_copy(ntfy_mac, addr_lnid->mac);
635 /* Ignore VLAN only changes */
636 if (!(code & IPA_ADDR_CHANGE_CODE_MACADDR))
637 return;
638 /* Ignore mcast entries */
639 if (is_multicast_ether_addr(ntfy_mac))
640 return;
641 /* Ignore my own addresses */
642 if (qeth_is_my_net_if_token(card, token))
643 return;
644
645 info.addr = ntfy_mac;
646 /* don't report VLAN IDs */
647 info.vid = 0;
648 info.added_by_user = false;
649 info.offloaded = true;
650
651 if (code & IPA_ADDR_CHANGE_CODE_REMOVAL) {
652 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
653 card->dev, &info.info, NULL);
654 QETH_CARD_TEXT(card, 4, "andelmac");
655 QETH_CARD_TEXT_(card, 4,
656 "mc%012llx", ether_addr_to_u64(ntfy_mac));
657 } else {
658 call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
659 card->dev, &info.info, NULL);
660 QETH_CARD_TEXT(card, 4, "anaddmac");
661 QETH_CARD_TEXT_(card, 4,
662 "mc%012llx", ether_addr_to_u64(ntfy_mac));
663 }
664}
665
666static void qeth_l2_dev2br_an_set_cb(void *priv,
667 struct chsc_pnso_naid_l2 *entry)
668{
669 u8 code = IPA_ADDR_CHANGE_CODE_MACADDR;
670 struct qeth_card *card = priv;
671
672 if (entry->addr_lnid.lnid < VLAN_N_VID)
673 code |= IPA_ADDR_CHANGE_CODE_VLANID;
674 qeth_l2_dev2br_fdb_notify(card, code,
675 (struct net_if_token *)&entry->nit,
676 (struct mac_addr_lnid *)&entry->addr_lnid);
677}
678
679/**
680 * qeth_l2_dev2br_an_set() -
681 * Enable or disable 'dev to bridge network address notification'
682 * @card: qeth_card structure pointer
683 * @enable: Enable or disable 'dev to bridge network address notification'
684 *
685 * Returns negative errno-compatible error indication or 0 on success.
686 *
687 * On enable, emits a series of address notifications for all
688 * currently registered hosts.
689 */
690static int qeth_l2_dev2br_an_set(struct qeth_card *card, bool enable)
691{
692 int rc;
693
694 if (enable) {
695 QETH_CARD_TEXT(card, 2, "anseton");
696 rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 1,
697 qeth_l2_dev2br_an_set_cb, card);
698 if (rc == -EAGAIN)
699 /* address notification enabled, but inconsistent
700 * addresses reported -> disable address notification
701 */
702 qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0,
703 NULL, NULL);
704 } else {
705 QETH_CARD_TEXT(card, 2, "ansetoff");
706 rc = qeth_l2_pnso(card, PNSO_OC_NET_ADDR_INFO, 0, NULL, NULL);
707 }
708
709 return rc;
710}
711
712struct qeth_l2_br2dev_event_work {
713 struct work_struct work;
714 struct net_device *br_dev;
715 struct net_device *lsync_dev;
716 struct net_device *dst_dev;
717 unsigned long event;
718 unsigned char addr[ETH_ALEN];
719};
720
721static const struct net_device_ops qeth_l2_iqd_netdev_ops;
722static const struct net_device_ops qeth_l2_osa_netdev_ops;
723
724static bool qeth_l2_must_learn(struct net_device *netdev,
725 struct net_device *dstdev)
726{
727 struct qeth_priv *priv;
728
729 priv = netdev_priv(netdev);
730 return (netdev != dstdev &&
731 (priv->brport_features & BR_LEARNING_SYNC) &&
732 !(br_port_flag_is_set(netdev, BR_ISOLATED) &&
733 br_port_flag_is_set(dstdev, BR_ISOLATED)) &&
734 (netdev->netdev_ops == &qeth_l2_iqd_netdev_ops ||
735 netdev->netdev_ops == &qeth_l2_osa_netdev_ops));
736}
737
738/**
739 * qeth_l2_br2dev_worker() - update local MACs
740 * @work: bridge to device FDB update
741 *
742 * Update local MACs of a learning_sync bridgeport so it can receive
743 * messages for a destination port.
744 * In case of an isolated learning_sync port, also update its isolated
745 * siblings.
746 */
747static void qeth_l2_br2dev_worker(struct work_struct *work)
748{
749 struct qeth_l2_br2dev_event_work *br2dev_event_work =
750 container_of(work, struct qeth_l2_br2dev_event_work, work);
751 struct net_device *lsyncdev = br2dev_event_work->lsync_dev;
752 struct net_device *dstdev = br2dev_event_work->dst_dev;
753 struct net_device *brdev = br2dev_event_work->br_dev;
754 unsigned long event = br2dev_event_work->event;
755 unsigned char *addr = br2dev_event_work->addr;
756 struct qeth_card *card = lsyncdev->ml_priv;
757 struct net_device *lowerdev;
758 struct list_head *iter;
759 int err = 0;
760
761 QETH_CARD_TEXT_(card, 4, "b2dw%04lx", event);
762 QETH_CARD_TEXT_(card, 4, "ma%012llx", ether_addr_to_u64(addr));
763
764 rcu_read_lock();
765 /* Verify preconditions are still valid: */
766 if (!netif_is_bridge_port(lsyncdev) ||
767 brdev != netdev_master_upper_dev_get_rcu(lsyncdev))
768 goto unlock;
769 if (!qeth_l2_must_learn(lsyncdev, dstdev))
770 goto unlock;
771
772 if (br_port_flag_is_set(lsyncdev, BR_ISOLATED)) {
773 /* Update lsyncdev and its isolated sibling(s): */
774 iter = &brdev->adj_list.lower;
775 lowerdev = netdev_next_lower_dev_rcu(brdev, &iter);
776 while (lowerdev) {
777 if (br_port_flag_is_set(lowerdev, BR_ISOLATED)) {
778 switch (event) {
779 case SWITCHDEV_FDB_ADD_TO_DEVICE:
780 err = dev_uc_add(lowerdev, addr);
781 break;
782 case SWITCHDEV_FDB_DEL_TO_DEVICE:
783 err = dev_uc_del(lowerdev, addr);
784 break;
785 default:
786 break;
787 }
788 if (err) {
789 QETH_CARD_TEXT(card, 2, "b2derris");
790 QETH_CARD_TEXT_(card, 2,
791 "err%02lx%03d", event,
792 lowerdev->ifindex);
793 }
794 }
795 lowerdev = netdev_next_lower_dev_rcu(brdev, &iter);
796 }
797 } else {
798 switch (event) {
799 case SWITCHDEV_FDB_ADD_TO_DEVICE:
800 err = dev_uc_add(lsyncdev, addr);
801 break;
802 case SWITCHDEV_FDB_DEL_TO_DEVICE:
803 err = dev_uc_del(lsyncdev, addr);
804 break;
805 default:
806 break;
807 }
808 if (err)
809 QETH_CARD_TEXT_(card, 2, "b2derr%02lx", event);
810 }
811
812unlock:
813 rcu_read_unlock();
814 dev_put(brdev);
815 dev_put(lsyncdev);
816 dev_put(dstdev);
817 kfree(br2dev_event_work);
818}
819
820static int qeth_l2_br2dev_queue_work(struct net_device *brdev,
821 struct net_device *lsyncdev,
822 struct net_device *dstdev,
823 unsigned long event,
824 const unsigned char *addr)
825{
826 struct qeth_l2_br2dev_event_work *worker_data;
827 struct qeth_card *card;
828
829 worker_data = kzalloc(sizeof(*worker_data), GFP_ATOMIC);
830 if (!worker_data)
831 return -ENOMEM;
832 INIT_WORK(&worker_data->work, qeth_l2_br2dev_worker);
833 worker_data->br_dev = brdev;
834 worker_data->lsync_dev = lsyncdev;
835 worker_data->dst_dev = dstdev;
836 worker_data->event = event;
837 ether_addr_copy(worker_data->addr, addr);
838
839 card = lsyncdev->ml_priv;
840 /* Take a reference on the sw port devices and the bridge */
841 dev_hold(brdev);
842 dev_hold(lsyncdev);
843 dev_hold(dstdev);
844 queue_work(card->event_wq, &worker_data->work);
845 return 0;
846}
847
848/* Called under rtnl_lock */
849static int qeth_l2_switchdev_event(struct notifier_block *unused,
850 unsigned long event, void *ptr)
851{
852 struct net_device *dstdev, *brdev, *lowerdev;
853 struct switchdev_notifier_fdb_info *fdb_info;
854 struct switchdev_notifier_info *info = ptr;
855 struct list_head *iter;
856 struct qeth_card *card;
857 int rc;
858
859 if (!(event == SWITCHDEV_FDB_ADD_TO_DEVICE ||
860 event == SWITCHDEV_FDB_DEL_TO_DEVICE))
861 return NOTIFY_DONE;
862
863 dstdev = switchdev_notifier_info_to_dev(info);
864 brdev = netdev_master_upper_dev_get_rcu(dstdev);
865 if (!brdev || !netif_is_bridge_master(brdev))
866 return NOTIFY_DONE;
867 fdb_info = container_of(info,
868 struct switchdev_notifier_fdb_info,
869 info);
870 iter = &brdev->adj_list.lower;
871 lowerdev = netdev_next_lower_dev_rcu(brdev, &iter);
872 while (lowerdev) {
873 if (qeth_l2_must_learn(lowerdev, dstdev)) {
874 card = lowerdev->ml_priv;
875 QETH_CARD_TEXT_(card, 4, "b2dqw%03lx", event);
876 rc = qeth_l2_br2dev_queue_work(brdev, lowerdev,
877 dstdev, event,
878 fdb_info->addr);
879 if (rc) {
880 QETH_CARD_TEXT(card, 2, "b2dqwerr");
881 return NOTIFY_BAD;
882 }
883 }
884 lowerdev = netdev_next_lower_dev_rcu(brdev, &iter);
885 }
886 return NOTIFY_DONE;
887}
888
889static struct notifier_block qeth_l2_sw_notifier = {
890 .notifier_call = qeth_l2_switchdev_event,
891};
892
893static refcount_t qeth_l2_switchdev_notify_refcnt;
894
895/* Called under rtnl_lock */
896static void qeth_l2_br2dev_get(void)
897{
898 int rc;
899
900 if (!refcount_inc_not_zero(&qeth_l2_switchdev_notify_refcnt)) {
901 rc = register_switchdev_notifier(&qeth_l2_sw_notifier);
902 if (rc) {
903 QETH_DBF_MESSAGE(2,
904 "failed to register qeth_l2_sw_notifier: %d\n",
905 rc);
906 } else {
907 refcount_set(&qeth_l2_switchdev_notify_refcnt, 1);
908 QETH_DBF_MESSAGE(2, "qeth_l2_sw_notifier registered\n");
909 }
910 }
911 QETH_DBF_TEXT_(SETUP, 2, "b2d+%04d",
912 qeth_l2_switchdev_notify_refcnt.refs.counter);
913}
914
915/* Called under rtnl_lock */
916static void qeth_l2_br2dev_put(void)
917{
918 int rc;
919
920 if (refcount_dec_and_test(&qeth_l2_switchdev_notify_refcnt)) {
921 rc = unregister_switchdev_notifier(&qeth_l2_sw_notifier);
922 if (rc) {
923 QETH_DBF_MESSAGE(2,
924 "failed to unregister qeth_l2_sw_notifier: %d\n",
925 rc);
926 } else {
927 QETH_DBF_MESSAGE(2,
928 "qeth_l2_sw_notifier unregistered\n");
929 }
930 }
931 QETH_DBF_TEXT_(SETUP, 2, "b2d-%04d",
932 qeth_l2_switchdev_notify_refcnt.refs.counter);
933}
934
935static int qeth_l2_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
936 struct net_device *dev, u32 filter_mask,
937 int nlflags)
938{
939 struct qeth_priv *priv = netdev_priv(dev);
940 struct qeth_card *card = dev->ml_priv;
941 u16 mode = BRIDGE_MODE_UNDEF;
942
943 /* Do not even show qeth devs that cannot do bridge_setlink */
944 if (!priv->brport_hw_features || !netif_device_present(dev) ||
945 qeth_bridgeport_is_in_use(card))
946 return -EOPNOTSUPP;
947
948 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
949 mode, priv->brport_features,
950 priv->brport_hw_features,
951 nlflags, filter_mask, NULL);
952}
953
954static const struct nla_policy qeth_brport_policy[IFLA_BRPORT_MAX + 1] = {
955 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
956};
957
958/**
959 * qeth_l2_bridge_setlink() - set bridgeport attributes
960 * @dev: netdevice
961 * @nlh: netlink message header
962 * @flags: bridge flags (here: BRIDGE_FLAGS_SELF)
963 * @extack: extended ACK report struct
964 *
965 * Called under rtnl_lock
966 */
967static int qeth_l2_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
968 u16 flags, struct netlink_ext_ack *extack)
969{
970 struct qeth_priv *priv = netdev_priv(dev);
971 struct nlattr *bp_tb[IFLA_BRPORT_MAX + 1];
972 struct qeth_card *card = dev->ml_priv;
973 struct nlattr *attr, *nested_attr;
974 bool enable, has_protinfo = false;
975 int rem1, rem2;
976 int rc;
977
978 if (!netif_device_present(dev))
979 return -ENODEV;
980
981 nlmsg_for_each_attr(attr, nlh, sizeof(struct ifinfomsg), rem1) {
982 if (nla_type(attr) == IFLA_PROTINFO) {
983 rc = nla_parse_nested(bp_tb, IFLA_BRPORT_MAX, attr,
984 qeth_brport_policy, extack);
985 if (rc)
986 return rc;
987 has_protinfo = true;
988 } else if (nla_type(attr) == IFLA_AF_SPEC) {
989 nla_for_each_nested(nested_attr, attr, rem2) {
990 if (nla_type(nested_attr) == IFLA_BRIDGE_FLAGS)
991 continue;
992 NL_SET_ERR_MSG_ATTR(extack, nested_attr,
993 "Unsupported attribute");
994 return -EINVAL;
995 }
996 } else {
997 NL_SET_ERR_MSG_ATTR(extack, attr, "Unsupported attribute");
998 return -EINVAL;
999 }
1000 }
1001 if (!has_protinfo)
1002 return 0;
1003 if (!bp_tb[IFLA_BRPORT_LEARNING_SYNC])
1004 return -EINVAL;
1005 if (!(priv->brport_hw_features & BR_LEARNING_SYNC)) {
1006 NL_SET_ERR_MSG_ATTR(extack, bp_tb[IFLA_BRPORT_LEARNING_SYNC],
1007 "Operation not supported by HW");
1008 return -EOPNOTSUPP;
1009 }
1010 if (!IS_ENABLED(CONFIG_NET_SWITCHDEV)) {
1011 NL_SET_ERR_MSG_ATTR(extack, bp_tb[IFLA_BRPORT_LEARNING_SYNC],
1012 "Requires NET_SWITCHDEV");
1013 return -EOPNOTSUPP;
1014 }
1015 enable = !!nla_get_u8(bp_tb[IFLA_BRPORT_LEARNING_SYNC]);
1016
1017 if (enable == !!(priv->brport_features & BR_LEARNING_SYNC))
1018 return 0;
1019
1020 mutex_lock(&card->sbp_lock);
1021 /* do not change anything if BridgePort is enabled */
1022 if (qeth_bridgeport_is_in_use(card)) {
1023 NL_SET_ERR_MSG(extack, "n/a (BridgePort)");
1024 rc = -EBUSY;
1025 } else if (enable) {
1026 qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1027 rc = qeth_l2_dev2br_an_set(card, true);
1028 if (rc) {
1029 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1030 } else {
1031 priv->brport_features |= BR_LEARNING_SYNC;
1032 qeth_l2_br2dev_get();
1033 }
1034 } else {
1035 rc = qeth_l2_dev2br_an_set(card, false);
1036 if (!rc) {
1037 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1038 priv->brport_features ^= BR_LEARNING_SYNC;
1039 qeth_l2_dev2br_fdb_flush(card);
1040 qeth_l2_br2dev_put();
1041 }
1042 }
1043 mutex_unlock(&card->sbp_lock);
1044
1045 return rc;
1046}
1047
1048static const struct net_device_ops qeth_l2_iqd_netdev_ops = {
1049 .ndo_open = qeth_open,
1050 .ndo_stop = qeth_stop,
1051 .ndo_get_stats64 = qeth_get_stats64,
1052 .ndo_start_xmit = qeth_l2_hard_start_xmit,
1053 .ndo_features_check = qeth_features_check,
1054 .ndo_select_queue = qeth_l2_iqd_select_queue,
1055 .ndo_validate_addr = qeth_l2_validate_addr,
1056 .ndo_set_rx_mode = qeth_l2_set_rx_mode,
1057 .ndo_eth_ioctl = qeth_do_ioctl,
1058 .ndo_siocdevprivate = qeth_siocdevprivate,
1059 .ndo_set_mac_address = qeth_l2_set_mac_address,
1060 .ndo_vlan_rx_add_vid = qeth_l2_vlan_rx_add_vid,
1061 .ndo_vlan_rx_kill_vid = qeth_l2_vlan_rx_kill_vid,
1062 .ndo_tx_timeout = qeth_tx_timeout,
1063 .ndo_fix_features = qeth_fix_features,
1064 .ndo_set_features = qeth_set_features,
1065 .ndo_bridge_getlink = qeth_l2_bridge_getlink,
1066 .ndo_bridge_setlink = qeth_l2_bridge_setlink,
1067};
1068
1069static const struct net_device_ops qeth_l2_osa_netdev_ops = {
1070 .ndo_open = qeth_open,
1071 .ndo_stop = qeth_stop,
1072 .ndo_get_stats64 = qeth_get_stats64,
1073 .ndo_start_xmit = qeth_l2_hard_start_xmit,
1074 .ndo_features_check = qeth_features_check,
1075 .ndo_select_queue = qeth_osa_select_queue,
1076 .ndo_validate_addr = qeth_l2_validate_addr,
1077 .ndo_set_rx_mode = qeth_l2_set_rx_mode,
1078 .ndo_eth_ioctl = qeth_do_ioctl,
1079 .ndo_siocdevprivate = qeth_siocdevprivate,
1080 .ndo_set_mac_address = qeth_l2_set_mac_address,
1081 .ndo_vlan_rx_add_vid = qeth_l2_vlan_rx_add_vid,
1082 .ndo_vlan_rx_kill_vid = qeth_l2_vlan_rx_kill_vid,
1083 .ndo_tx_timeout = qeth_tx_timeout,
1084 .ndo_fix_features = qeth_fix_features,
1085 .ndo_set_features = qeth_set_features,
1086};
1087
1088static int qeth_l2_setup_netdev(struct qeth_card *card)
1089{
1090 card->dev->netdev_ops = IS_IQD(card) ? &qeth_l2_iqd_netdev_ops :
1091 &qeth_l2_osa_netdev_ops;
1092 card->dev->needed_headroom = sizeof(struct qeth_hdr);
1093 card->dev->priv_flags |= IFF_UNICAST_FLT;
1094
1095 if (IS_OSM(card)) {
1096 card->dev->features |= NETIF_F_VLAN_CHALLENGED;
1097 } else {
1098 if (!IS_VM_NIC(card))
1099 card->dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1100 card->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1101 }
1102
1103 if (IS_OSD(card) && !IS_VM_NIC(card)) {
1104 card->dev->features |= NETIF_F_SG;
1105 /* OSA 3S and earlier has no RX/TX support */
1106 if (qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM)) {
1107 card->dev->hw_features |= NETIF_F_IP_CSUM;
1108 card->dev->vlan_features |= NETIF_F_IP_CSUM;
1109 }
1110 }
1111 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
1112 card->dev->hw_features |= NETIF_F_IPV6_CSUM;
1113 card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
1114 }
1115 if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM) ||
1116 qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6)) {
1117 card->dev->hw_features |= NETIF_F_RXCSUM;
1118 card->dev->vlan_features |= NETIF_F_RXCSUM;
1119 }
1120 if (qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
1121 card->dev->hw_features |= NETIF_F_TSO;
1122 card->dev->vlan_features |= NETIF_F_TSO;
1123 }
1124 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
1125 card->dev->hw_features |= NETIF_F_TSO6;
1126 card->dev->vlan_features |= NETIF_F_TSO6;
1127 }
1128
1129 if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) {
1130 card->dev->needed_headroom = sizeof(struct qeth_hdr_tso);
1131 netif_keep_dst(card->dev);
1132 netif_set_tso_max_size(card->dev,
1133 PAGE_SIZE * (QDIO_MAX_ELEMENTS_PER_BUFFER - 1));
1134 }
1135
1136 netif_napi_add(card->dev, &card->napi, qeth_poll);
1137 return register_netdev(card->dev);
1138}
1139
1140static void qeth_l2_trace_features(struct qeth_card *card)
1141{
1142 /* Set BridgePort features */
1143 QETH_CARD_TEXT(card, 2, "featuSBP");
1144 QETH_CARD_HEX(card, 2, &card->options.sbp.supported_funcs,
1145 sizeof(card->options.sbp.supported_funcs));
1146 /* VNIC Characteristics features */
1147 QETH_CARD_TEXT(card, 2, "feaVNICC");
1148 QETH_CARD_HEX(card, 2, &card->options.vnicc.sup_chars,
1149 sizeof(card->options.vnicc.sup_chars));
1150}
1151
1152static void qeth_l2_setup_bridgeport_attrs(struct qeth_card *card)
1153{
1154 if (!card->options.sbp.reflect_promisc &&
1155 card->options.sbp.role != QETH_SBP_ROLE_NONE) {
1156 /* Conditional to avoid spurious error messages */
1157 qeth_bridgeport_setrole(card, card->options.sbp.role);
1158 /* Let the callback function refresh the stored role value. */
1159 qeth_bridgeport_query_ports(card, &card->options.sbp.role,
1160 NULL);
1161 }
1162 if (card->options.sbp.hostnotification) {
1163 if (qeth_bridgeport_an_set(card, 1))
1164 card->options.sbp.hostnotification = 0;
1165 }
1166}
1167
1168/**
1169 * qeth_l2_detect_dev2br_support() -
1170 * Detect whether this card supports 'dev to bridge fdb network address
1171 * change notification' and thus can support the learning_sync bridgeport
1172 * attribute
1173 * @card: qeth_card structure pointer
1174 */
1175static void qeth_l2_detect_dev2br_support(struct qeth_card *card)
1176{
1177 struct qeth_priv *priv = netdev_priv(card->dev);
1178 bool dev2br_supported;
1179
1180 QETH_CARD_TEXT(card, 2, "d2brsup");
1181 if (!IS_IQD(card))
1182 return;
1183
1184 /* dev2br requires valid cssid,iid,chid */
1185 dev2br_supported = card->info.ids_valid &&
1186 css_general_characteristics.enarf;
1187 QETH_CARD_TEXT_(card, 2, "D2Bsup%02x", dev2br_supported);
1188
1189 if (dev2br_supported)
1190 priv->brport_hw_features |= BR_LEARNING_SYNC;
1191 else
1192 priv->brport_hw_features &= ~BR_LEARNING_SYNC;
1193}
1194
1195static void qeth_l2_enable_brport_features(struct qeth_card *card)
1196{
1197 struct qeth_priv *priv = netdev_priv(card->dev);
1198 int rc;
1199
1200 if (priv->brport_features & BR_LEARNING_SYNC) {
1201 if (priv->brport_hw_features & BR_LEARNING_SYNC) {
1202 qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1203 rc = qeth_l2_dev2br_an_set(card, true);
1204 if (rc == -EAGAIN) {
1205 /* Recoverable error, retry once */
1206 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1207 qeth_l2_dev2br_fdb_flush(card);
1208 qeth_l2_set_pnso_mode(card, QETH_PNSO_ADDR_INFO);
1209 rc = qeth_l2_dev2br_an_set(card, true);
1210 }
1211 if (rc) {
1212 netdev_err(card->dev,
1213 "failed to enable bridge learning_sync: %d\n",
1214 rc);
1215 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1216 qeth_l2_dev2br_fdb_flush(card);
1217 priv->brport_features ^= BR_LEARNING_SYNC;
1218 }
1219 } else {
1220 dev_warn(&card->gdev->dev,
1221 "bridge learning_sync not supported\n");
1222 priv->brport_features ^= BR_LEARNING_SYNC;
1223 }
1224 }
1225}
1226
1227/* SETBRIDGEPORT support, async notifications */
1228
1229enum qeth_an_event_type {anev_reg_unreg, anev_abort, anev_reset};
1230
1231/**
1232 * qeth_bridge_emit_host_event() - bridgeport address change notification
1233 * @card: qeth_card structure pointer, for udev events.
1234 * @evtype: "normal" register/unregister, or abort, or reset. For abort
1235 * and reset token and addr_lnid are unused and may be NULL.
1236 * @code: event bitmask: high order bit 0x80 value 1 means removal of an
1237 * object, 0 - addition of an object.
1238 * 0x01 - VLAN, 0x02 - MAC, 0x03 - VLAN and MAC.
1239 * @token: "network token" structure identifying physical address of the port.
1240 * @addr_lnid: pointer to structure with MAC address and VLAN ID.
1241 *
1242 * This function is called when registrations and deregistrations are
1243 * reported by the hardware, and also when notifications are enabled -
1244 * for all currently registered addresses.
1245 */
1246static void qeth_bridge_emit_host_event(struct qeth_card *card,
1247 enum qeth_an_event_type evtype,
1248 u8 code,
1249 struct net_if_token *token,
1250 struct mac_addr_lnid *addr_lnid)
1251{
1252 char str[7][32];
1253 char *env[8];
1254 int i = 0;
1255
1256 switch (evtype) {
1257 case anev_reg_unreg:
1258 scnprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=%s",
1259 (code & IPA_ADDR_CHANGE_CODE_REMOVAL)
1260 ? "deregister" : "register");
1261 env[i] = str[i]; i++;
1262 if (code & IPA_ADDR_CHANGE_CODE_VLANID) {
1263 scnprintf(str[i], sizeof(str[i]), "VLAN=%d",
1264 addr_lnid->lnid);
1265 env[i] = str[i]; i++;
1266 }
1267 if (code & IPA_ADDR_CHANGE_CODE_MACADDR) {
1268 scnprintf(str[i], sizeof(str[i]), "MAC=%pM",
1269 addr_lnid->mac);
1270 env[i] = str[i]; i++;
1271 }
1272 scnprintf(str[i], sizeof(str[i]), "NTOK_BUSID=%x.%x.%04x",
1273 token->cssid, token->ssid, token->devnum);
1274 env[i] = str[i]; i++;
1275 scnprintf(str[i], sizeof(str[i]), "NTOK_IID=%02x", token->iid);
1276 env[i] = str[i]; i++;
1277 scnprintf(str[i], sizeof(str[i]), "NTOK_CHPID=%02x",
1278 token->chpid);
1279 env[i] = str[i]; i++;
1280 scnprintf(str[i], sizeof(str[i]), "NTOK_CHID=%04x",
1281 token->chid);
1282 env[i] = str[i]; i++;
1283 break;
1284 case anev_abort:
1285 scnprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=abort");
1286 env[i] = str[i]; i++;
1287 break;
1288 case anev_reset:
1289 scnprintf(str[i], sizeof(str[i]), "BRIDGEDHOST=reset");
1290 env[i] = str[i]; i++;
1291 break;
1292 }
1293 env[i] = NULL;
1294 kobject_uevent_env(&card->gdev->dev.kobj, KOBJ_CHANGE, env);
1295}
1296
1297struct qeth_bridge_state_data {
1298 struct work_struct worker;
1299 struct qeth_card *card;
1300 u8 role;
1301 u8 state;
1302};
1303
1304static void qeth_bridge_state_change_worker(struct work_struct *work)
1305{
1306 struct qeth_bridge_state_data *data =
1307 container_of(work, struct qeth_bridge_state_data, worker);
1308 char env_locrem[32];
1309 char env_role[32];
1310 char env_state[32];
1311 char *env[] = {
1312 env_locrem,
1313 env_role,
1314 env_state,
1315 NULL
1316 };
1317
1318 scnprintf(env_locrem, sizeof(env_locrem), "BRIDGEPORT=statechange");
1319 scnprintf(env_role, sizeof(env_role), "ROLE=%s",
1320 (data->role == QETH_SBP_ROLE_NONE) ? "none" :
1321 (data->role == QETH_SBP_ROLE_PRIMARY) ? "primary" :
1322 (data->role == QETH_SBP_ROLE_SECONDARY) ? "secondary" :
1323 "<INVALID>");
1324 scnprintf(env_state, sizeof(env_state), "STATE=%s",
1325 (data->state == QETH_SBP_STATE_INACTIVE) ? "inactive" :
1326 (data->state == QETH_SBP_STATE_STANDBY) ? "standby" :
1327 (data->state == QETH_SBP_STATE_ACTIVE) ? "active" :
1328 "<INVALID>");
1329 kobject_uevent_env(&data->card->gdev->dev.kobj,
1330 KOBJ_CHANGE, env);
1331 kfree(data);
1332}
1333
1334static void qeth_bridge_state_change(struct qeth_card *card,
1335 struct qeth_ipa_cmd *cmd)
1336{
1337 struct qeth_sbp_port_data *qports = &cmd->data.sbp.data.port_data;
1338 struct qeth_bridge_state_data *data;
1339
1340 QETH_CARD_TEXT(card, 2, "brstchng");
1341 if (qports->num_entries == 0) {
1342 QETH_CARD_TEXT(card, 2, "BPempty");
1343 return;
1344 }
1345 if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1346 QETH_CARD_TEXT_(card, 2, "BPsz%04x", qports->entry_length);
1347 return;
1348 }
1349
1350 data = kzalloc(sizeof(*data), GFP_ATOMIC);
1351 if (!data) {
1352 QETH_CARD_TEXT(card, 2, "BPSalloc");
1353 return;
1354 }
1355 INIT_WORK(&data->worker, qeth_bridge_state_change_worker);
1356 data->card = card;
1357 /* Information for the local port: */
1358 data->role = qports->entry[0].role;
1359 data->state = qports->entry[0].state;
1360
1361 queue_work(card->event_wq, &data->worker);
1362}
1363
1364struct qeth_addr_change_data {
1365 struct delayed_work dwork;
1366 struct qeth_card *card;
1367 struct qeth_ipacmd_addr_change ac_event;
1368};
1369
1370static void qeth_l2_dev2br_worker(struct work_struct *work)
1371{
1372 struct delayed_work *dwork = to_delayed_work(work);
1373 struct qeth_addr_change_data *data;
1374 struct qeth_card *card;
1375 struct qeth_priv *priv;
1376 unsigned int i;
1377 int rc;
1378
1379 data = container_of(dwork, struct qeth_addr_change_data, dwork);
1380 card = data->card;
1381 priv = netdev_priv(card->dev);
1382
1383 QETH_CARD_TEXT(card, 4, "dev2brew");
1384
1385 if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1386 goto free;
1387
1388 if (data->ac_event.lost_event_mask) {
1389 /* Potential re-config in progress, try again later: */
1390 if (!rtnl_trylock()) {
1391 queue_delayed_work(card->event_wq, dwork,
1392 msecs_to_jiffies(100));
1393 return;
1394 }
1395
1396 if (!netif_device_present(card->dev)) {
1397 rtnl_unlock();
1398 goto free;
1399 }
1400
1401 QETH_DBF_MESSAGE(3,
1402 "Address change notification overflow on device %x\n",
1403 CARD_DEVID(card));
1404 /* Card fdb and bridge fdb are out of sync, card has stopped
1405 * notifications (no need to drain_workqueue). Purge all
1406 * 'extern_learn' entries from the parent bridge and restart
1407 * the notifications.
1408 */
1409 qeth_l2_dev2br_fdb_flush(card);
1410 rc = qeth_l2_dev2br_an_set(card, true);
1411 if (rc) {
1412 /* TODO: if we want to retry after -EAGAIN, be
1413 * aware there could be stale entries in the
1414 * workqueue now, that need to be drained.
1415 * For now we give up:
1416 */
1417 netdev_err(card->dev,
1418 "bridge learning_sync failed to recover: %d\n",
1419 rc);
1420 WRITE_ONCE(card->info.pnso_mode,
1421 QETH_PNSO_NONE);
1422 /* To remove fdb entries reported by an_set: */
1423 qeth_l2_dev2br_fdb_flush(card);
1424 priv->brport_features ^= BR_LEARNING_SYNC;
1425 } else {
1426 QETH_DBF_MESSAGE(3,
1427 "Address Notification resynced on device %x\n",
1428 CARD_DEVID(card));
1429 }
1430
1431 rtnl_unlock();
1432 } else {
1433 for (i = 0; i < data->ac_event.num_entries; i++) {
1434 struct qeth_ipacmd_addr_change_entry *entry =
1435 &data->ac_event.entry[i];
1436 qeth_l2_dev2br_fdb_notify(card,
1437 entry->change_code,
1438 &entry->token,
1439 &entry->addr_lnid);
1440 }
1441 }
1442
1443free:
1444 kfree(data);
1445}
1446
1447static void qeth_addr_change_event_worker(struct work_struct *work)
1448{
1449 struct delayed_work *dwork = to_delayed_work(work);
1450 struct qeth_addr_change_data *data;
1451 struct qeth_card *card;
1452 int i;
1453
1454 data = container_of(dwork, struct qeth_addr_change_data, dwork);
1455 card = data->card;
1456
1457 QETH_CARD_TEXT(data->card, 4, "adrchgew");
1458
1459 if (READ_ONCE(card->info.pnso_mode) == QETH_PNSO_NONE)
1460 goto free;
1461
1462 if (data->ac_event.lost_event_mask) {
1463 /* Potential re-config in progress, try again later: */
1464 if (!mutex_trylock(&card->sbp_lock)) {
1465 queue_delayed_work(card->event_wq, dwork,
1466 msecs_to_jiffies(100));
1467 return;
1468 }
1469
1470 dev_info(&data->card->gdev->dev,
1471 "Address change notification stopped on %s (%s)\n",
1472 netdev_name(card->dev),
1473 (data->ac_event.lost_event_mask == 0x01)
1474 ? "Overflow"
1475 : (data->ac_event.lost_event_mask == 0x02)
1476 ? "Bridge port state change"
1477 : "Unknown reason");
1478
1479 data->card->options.sbp.hostnotification = 0;
1480 card->info.pnso_mode = QETH_PNSO_NONE;
1481 mutex_unlock(&data->card->sbp_lock);
1482 qeth_bridge_emit_host_event(data->card, anev_abort,
1483 0, NULL, NULL);
1484 } else
1485 for (i = 0; i < data->ac_event.num_entries; i++) {
1486 struct qeth_ipacmd_addr_change_entry *entry =
1487 &data->ac_event.entry[i];
1488 qeth_bridge_emit_host_event(data->card,
1489 anev_reg_unreg,
1490 entry->change_code,
1491 &entry->token,
1492 &entry->addr_lnid);
1493 }
1494
1495free:
1496 kfree(data);
1497}
1498
1499static void qeth_addr_change_event(struct qeth_card *card,
1500 struct qeth_ipa_cmd *cmd)
1501{
1502 struct qeth_ipacmd_addr_change *hostevs =
1503 &cmd->data.addrchange;
1504 struct qeth_addr_change_data *data;
1505 int extrasize;
1506
1507 if (card->info.pnso_mode == QETH_PNSO_NONE)
1508 return;
1509
1510 QETH_CARD_TEXT(card, 4, "adrchgev");
1511 if (cmd->hdr.return_code != 0x0000) {
1512 if (cmd->hdr.return_code == 0x0010) {
1513 if (hostevs->lost_event_mask == 0x00)
1514 hostevs->lost_event_mask = 0xff;
1515 } else {
1516 QETH_CARD_TEXT_(card, 2, "ACHN%04x",
1517 cmd->hdr.return_code);
1518 return;
1519 }
1520 }
1521 extrasize = sizeof(struct qeth_ipacmd_addr_change_entry) *
1522 hostevs->num_entries;
1523 data = kzalloc(sizeof(struct qeth_addr_change_data) + extrasize,
1524 GFP_ATOMIC);
1525 if (!data) {
1526 QETH_CARD_TEXT(card, 2, "ACNalloc");
1527 return;
1528 }
1529 if (card->info.pnso_mode == QETH_PNSO_BRIDGEPORT)
1530 INIT_DELAYED_WORK(&data->dwork, qeth_addr_change_event_worker);
1531 else
1532 INIT_DELAYED_WORK(&data->dwork, qeth_l2_dev2br_worker);
1533 data->card = card;
1534 data->ac_event = *hostevs;
1535 memcpy(data->ac_event.entry, hostevs->entry, extrasize);
1536 queue_delayed_work(card->event_wq, &data->dwork, 0);
1537}
1538
1539/* SETBRIDGEPORT support; sending commands */
1540
1541struct _qeth_sbp_cbctl {
1542 union {
1543 u32 supported;
1544 struct {
1545 enum qeth_sbp_roles *role;
1546 enum qeth_sbp_states *state;
1547 } qports;
1548 } data;
1549};
1550
1551static int qeth_bridgeport_makerc(struct qeth_card *card,
1552 struct qeth_ipa_cmd *cmd)
1553{
1554 struct qeth_ipacmd_setbridgeport *sbp = &cmd->data.sbp;
1555 enum qeth_ipa_sbp_cmd setcmd = sbp->hdr.command_code;
1556 u16 ipa_rc = cmd->hdr.return_code;
1557 u16 sbp_rc = sbp->hdr.return_code;
1558 int rc;
1559
1560 if (ipa_rc == IPA_RC_SUCCESS && sbp_rc == IPA_RC_SUCCESS)
1561 return 0;
1562
1563 if ((IS_IQD(card) && ipa_rc == IPA_RC_SUCCESS) ||
1564 (!IS_IQD(card) && ipa_rc == sbp_rc)) {
1565 switch (sbp_rc) {
1566 case IPA_RC_SUCCESS:
1567 rc = 0;
1568 break;
1569 case IPA_RC_L2_UNSUPPORTED_CMD:
1570 case IPA_RC_UNSUPPORTED_COMMAND:
1571 rc = -EOPNOTSUPP;
1572 break;
1573 case IPA_RC_SBP_OSA_NOT_CONFIGURED:
1574 case IPA_RC_SBP_IQD_NOT_CONFIGURED:
1575 rc = -ENODEV; /* maybe not the best code here? */
1576 dev_err(&card->gdev->dev,
1577 "The device is not configured as a Bridge Port\n");
1578 break;
1579 case IPA_RC_SBP_OSA_OS_MISMATCH:
1580 case IPA_RC_SBP_IQD_OS_MISMATCH:
1581 rc = -EPERM;
1582 dev_err(&card->gdev->dev,
1583 "A Bridge Port is already configured by a different operating system\n");
1584 break;
1585 case IPA_RC_SBP_OSA_ANO_DEV_PRIMARY:
1586 case IPA_RC_SBP_IQD_ANO_DEV_PRIMARY:
1587 switch (setcmd) {
1588 case IPA_SBP_SET_PRIMARY_BRIDGE_PORT:
1589 rc = -EEXIST;
1590 dev_err(&card->gdev->dev,
1591 "The LAN already has a primary Bridge Port\n");
1592 break;
1593 case IPA_SBP_SET_SECONDARY_BRIDGE_PORT:
1594 rc = -EBUSY;
1595 dev_err(&card->gdev->dev,
1596 "The device is already a primary Bridge Port\n");
1597 break;
1598 default:
1599 rc = -EIO;
1600 }
1601 break;
1602 case IPA_RC_SBP_OSA_CURRENT_SECOND:
1603 case IPA_RC_SBP_IQD_CURRENT_SECOND:
1604 rc = -EBUSY;
1605 dev_err(&card->gdev->dev,
1606 "The device is already a secondary Bridge Port\n");
1607 break;
1608 case IPA_RC_SBP_OSA_LIMIT_SECOND:
1609 case IPA_RC_SBP_IQD_LIMIT_SECOND:
1610 rc = -EEXIST;
1611 dev_err(&card->gdev->dev,
1612 "The LAN cannot have more secondary Bridge Ports\n");
1613 break;
1614 case IPA_RC_SBP_OSA_CURRENT_PRIMARY:
1615 case IPA_RC_SBP_IQD_CURRENT_PRIMARY:
1616 rc = -EBUSY;
1617 dev_err(&card->gdev->dev,
1618 "The device is already a primary Bridge Port\n");
1619 break;
1620 case IPA_RC_SBP_OSA_NOT_AUTHD_BY_ZMAN:
1621 case IPA_RC_SBP_IQD_NOT_AUTHD_BY_ZMAN:
1622 rc = -EACCES;
1623 dev_err(&card->gdev->dev,
1624 "The device is not authorized to be a Bridge Port\n");
1625 break;
1626 default:
1627 rc = -EIO;
1628 }
1629 } else {
1630 switch (ipa_rc) {
1631 case IPA_RC_NOTSUPP:
1632 rc = -EOPNOTSUPP;
1633 break;
1634 case IPA_RC_UNSUPPORTED_COMMAND:
1635 rc = -EOPNOTSUPP;
1636 break;
1637 default:
1638 rc = -EIO;
1639 }
1640 }
1641
1642 if (rc) {
1643 QETH_CARD_TEXT_(card, 2, "SBPi%04x", ipa_rc);
1644 QETH_CARD_TEXT_(card, 2, "SBPc%04x", sbp_rc);
1645 }
1646 return rc;
1647}
1648
1649static struct qeth_cmd_buffer *qeth_sbp_build_cmd(struct qeth_card *card,
1650 enum qeth_ipa_sbp_cmd sbp_cmd,
1651 unsigned int data_length)
1652{
1653 enum qeth_ipa_cmds ipa_cmd = IS_IQD(card) ? IPA_CMD_SETBRIDGEPORT_IQD :
1654 IPA_CMD_SETBRIDGEPORT_OSA;
1655 struct qeth_ipacmd_sbp_hdr *hdr;
1656 struct qeth_cmd_buffer *iob;
1657
1658 iob = qeth_ipa_alloc_cmd(card, ipa_cmd, QETH_PROT_NONE,
1659 data_length +
1660 offsetof(struct qeth_ipacmd_setbridgeport,
1661 data));
1662 if (!iob)
1663 return iob;
1664
1665 hdr = &__ipa_cmd(iob)->data.sbp.hdr;
1666 hdr->cmdlength = sizeof(*hdr) + data_length;
1667 hdr->command_code = sbp_cmd;
1668 hdr->used_total = 1;
1669 hdr->seq_no = 1;
1670 return iob;
1671}
1672
1673static int qeth_bridgeport_query_support_cb(struct qeth_card *card,
1674 struct qeth_reply *reply, unsigned long data)
1675{
1676 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1677 struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1678 int rc;
1679
1680 QETH_CARD_TEXT(card, 2, "brqsupcb");
1681 rc = qeth_bridgeport_makerc(card, cmd);
1682 if (rc)
1683 return rc;
1684
1685 cbctl->data.supported =
1686 cmd->data.sbp.data.query_cmds_supp.supported_cmds;
1687 return 0;
1688}
1689
1690/**
1691 * qeth_bridgeport_query_support() - store bitmask of supported subfunctions.
1692 * @card: qeth_card structure pointer.
1693 *
1694 * Sets bitmask of supported setbridgeport subfunctions in the qeth_card
1695 * strucutre: card->options.sbp.supported_funcs.
1696 */
1697static void qeth_bridgeport_query_support(struct qeth_card *card)
1698{
1699 struct qeth_cmd_buffer *iob;
1700 struct _qeth_sbp_cbctl cbctl;
1701
1702 QETH_CARD_TEXT(card, 2, "brqsuppo");
1703 iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_COMMANDS_SUPPORTED,
1704 SBP_DATA_SIZEOF(query_cmds_supp));
1705 if (!iob)
1706 return;
1707
1708 if (qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_support_cb,
1709 &cbctl)) {
1710 card->options.sbp.role = QETH_SBP_ROLE_NONE;
1711 card->options.sbp.supported_funcs = 0;
1712 return;
1713 }
1714 card->options.sbp.supported_funcs = cbctl.data.supported;
1715}
1716
1717static int qeth_bridgeport_query_ports_cb(struct qeth_card *card,
1718 struct qeth_reply *reply, unsigned long data)
1719{
1720 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1721 struct _qeth_sbp_cbctl *cbctl = (struct _qeth_sbp_cbctl *)reply->param;
1722 struct qeth_sbp_port_data *qports;
1723 int rc;
1724
1725 QETH_CARD_TEXT(card, 2, "brqprtcb");
1726 rc = qeth_bridgeport_makerc(card, cmd);
1727 if (rc)
1728 return rc;
1729
1730 qports = &cmd->data.sbp.data.port_data;
1731 if (qports->entry_length != sizeof(struct qeth_sbp_port_entry)) {
1732 QETH_CARD_TEXT_(card, 2, "SBPs%04x", qports->entry_length);
1733 return -EINVAL;
1734 }
1735 /* first entry contains the state of the local port */
1736 if (qports->num_entries > 0) {
1737 if (cbctl->data.qports.role)
1738 *cbctl->data.qports.role = qports->entry[0].role;
1739 if (cbctl->data.qports.state)
1740 *cbctl->data.qports.state = qports->entry[0].state;
1741 }
1742 return 0;
1743}
1744
1745/**
1746 * qeth_bridgeport_query_ports() - query local bridgeport status.
1747 * @card: qeth_card structure pointer.
1748 * @role: Role of the port: 0-none, 1-primary, 2-secondary.
1749 * @state: State of the port: 0-inactive, 1-standby, 2-active.
1750 *
1751 * Returns negative errno-compatible error indication or 0 on success.
1752 *
1753 * 'role' and 'state' are not updated in case of hardware operation failure.
1754 */
1755int qeth_bridgeport_query_ports(struct qeth_card *card,
1756 enum qeth_sbp_roles *role, enum qeth_sbp_states *state)
1757{
1758 struct qeth_cmd_buffer *iob;
1759 struct _qeth_sbp_cbctl cbctl = {
1760 .data = {
1761 .qports = {
1762 .role = role,
1763 .state = state,
1764 },
1765 },
1766 };
1767
1768 QETH_CARD_TEXT(card, 2, "brqports");
1769 if (!(card->options.sbp.supported_funcs & IPA_SBP_QUERY_BRIDGE_PORTS))
1770 return -EOPNOTSUPP;
1771 iob = qeth_sbp_build_cmd(card, IPA_SBP_QUERY_BRIDGE_PORTS, 0);
1772 if (!iob)
1773 return -ENOMEM;
1774
1775 return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_query_ports_cb,
1776 &cbctl);
1777}
1778
1779static int qeth_bridgeport_set_cb(struct qeth_card *card,
1780 struct qeth_reply *reply, unsigned long data)
1781{
1782 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
1783
1784 QETH_CARD_TEXT(card, 2, "brsetrcb");
1785 return qeth_bridgeport_makerc(card, cmd);
1786}
1787
1788/**
1789 * qeth_bridgeport_setrole() - Assign primary role to the port.
1790 * @card: qeth_card structure pointer.
1791 * @role: Role to assign.
1792 *
1793 * Returns negative errno-compatible error indication or 0 on success.
1794 */
1795int qeth_bridgeport_setrole(struct qeth_card *card, enum qeth_sbp_roles role)
1796{
1797 struct qeth_cmd_buffer *iob;
1798 enum qeth_ipa_sbp_cmd setcmd;
1799 unsigned int cmdlength = 0;
1800
1801 QETH_CARD_TEXT(card, 2, "brsetrol");
1802 switch (role) {
1803 case QETH_SBP_ROLE_NONE:
1804 setcmd = IPA_SBP_RESET_BRIDGE_PORT_ROLE;
1805 break;
1806 case QETH_SBP_ROLE_PRIMARY:
1807 setcmd = IPA_SBP_SET_PRIMARY_BRIDGE_PORT;
1808 cmdlength = SBP_DATA_SIZEOF(set_primary);
1809 break;
1810 case QETH_SBP_ROLE_SECONDARY:
1811 setcmd = IPA_SBP_SET_SECONDARY_BRIDGE_PORT;
1812 break;
1813 default:
1814 return -EINVAL;
1815 }
1816 if (!(card->options.sbp.supported_funcs & setcmd))
1817 return -EOPNOTSUPP;
1818 iob = qeth_sbp_build_cmd(card, setcmd, cmdlength);
1819 if (!iob)
1820 return -ENOMEM;
1821
1822 return qeth_send_ipa_cmd(card, iob, qeth_bridgeport_set_cb, NULL);
1823}
1824
1825static void qeth_bridgeport_an_set_cb(void *priv,
1826 struct chsc_pnso_naid_l2 *entry)
1827{
1828 struct qeth_card *card = (struct qeth_card *)priv;
1829 u8 code;
1830
1831 code = IPA_ADDR_CHANGE_CODE_MACADDR;
1832 if (entry->addr_lnid.lnid < VLAN_N_VID)
1833 code |= IPA_ADDR_CHANGE_CODE_VLANID;
1834 qeth_bridge_emit_host_event(card, anev_reg_unreg, code,
1835 (struct net_if_token *)&entry->nit,
1836 (struct mac_addr_lnid *)&entry->addr_lnid);
1837}
1838
1839/**
1840 * qeth_bridgeport_an_set() - Enable or disable bridgeport address notification
1841 * @card: qeth_card structure pointer.
1842 * @enable: 0 - disable, non-zero - enable notifications
1843 *
1844 * Returns negative errno-compatible error indication or 0 on success.
1845 *
1846 * On enable, emits a series of address notifications udev events for all
1847 * currently registered hosts.
1848 */
1849int qeth_bridgeport_an_set(struct qeth_card *card, int enable)
1850{
1851 int rc;
1852
1853 if (!card->options.sbp.supported_funcs)
1854 return -EOPNOTSUPP;
1855
1856 if (enable) {
1857 qeth_bridge_emit_host_event(card, anev_reset, 0, NULL, NULL);
1858 qeth_l2_set_pnso_mode(card, QETH_PNSO_BRIDGEPORT);
1859 rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 1,
1860 qeth_bridgeport_an_set_cb, card);
1861 if (rc)
1862 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1863 } else {
1864 rc = qeth_l2_pnso(card, PNSO_OC_NET_BRIDGE_INFO, 0, NULL, NULL);
1865 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
1866 }
1867 return rc;
1868}
1869
1870/* VNIC Characteristics support */
1871
1872/* handle VNICC IPA command return codes; convert to error codes */
1873static int qeth_l2_vnicc_makerc(struct qeth_card *card, u16 ipa_rc)
1874{
1875 int rc;
1876
1877 switch (ipa_rc) {
1878 case IPA_RC_SUCCESS:
1879 return ipa_rc;
1880 case IPA_RC_L2_UNSUPPORTED_CMD:
1881 case IPA_RC_NOTSUPP:
1882 rc = -EOPNOTSUPP;
1883 break;
1884 case IPA_RC_VNICC_OOSEQ:
1885 rc = -EALREADY;
1886 break;
1887 case IPA_RC_VNICC_VNICBP:
1888 rc = -EBUSY;
1889 break;
1890 case IPA_RC_L2_ADDR_TABLE_FULL:
1891 rc = -ENOSPC;
1892 break;
1893 case IPA_RC_L2_MAC_NOT_AUTH_BY_ADP:
1894 rc = -EACCES;
1895 break;
1896 default:
1897 rc = -EIO;
1898 }
1899
1900 QETH_CARD_TEXT_(card, 2, "err%04x", ipa_rc);
1901 return rc;
1902}
1903
1904/* generic VNICC request call back */
1905static int qeth_l2_vnicc_request_cb(struct qeth_card *card,
1906 struct qeth_reply *reply,
1907 unsigned long data)
1908{
1909 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1910 struct qeth_ipacmd_vnicc *rep = &cmd->data.vnicc;
1911 u32 sub_cmd = cmd->data.vnicc.hdr.sub_command;
1912
1913 QETH_CARD_TEXT(card, 2, "vniccrcb");
1914 if (cmd->hdr.return_code)
1915 return qeth_l2_vnicc_makerc(card, cmd->hdr.return_code);
1916 /* return results to caller */
1917 card->options.vnicc.sup_chars = rep->vnicc_cmds.supported;
1918 card->options.vnicc.cur_chars = rep->vnicc_cmds.enabled;
1919
1920 if (sub_cmd == IPA_VNICC_QUERY_CMDS)
1921 *(u32 *)reply->param = rep->data.query_cmds.sup_cmds;
1922 else if (sub_cmd == IPA_VNICC_GET_TIMEOUT)
1923 *(u32 *)reply->param = rep->data.getset_timeout.timeout;
1924
1925 return 0;
1926}
1927
1928static struct qeth_cmd_buffer *qeth_l2_vnicc_build_cmd(struct qeth_card *card,
1929 u32 vnicc_cmd,
1930 unsigned int data_length)
1931{
1932 struct qeth_ipacmd_vnicc_hdr *hdr;
1933 struct qeth_cmd_buffer *iob;
1934
1935 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_VNICC, QETH_PROT_NONE,
1936 data_length +
1937 offsetof(struct qeth_ipacmd_vnicc, data));
1938 if (!iob)
1939 return NULL;
1940
1941 hdr = &__ipa_cmd(iob)->data.vnicc.hdr;
1942 hdr->data_length = sizeof(*hdr) + data_length;
1943 hdr->sub_command = vnicc_cmd;
1944 return iob;
1945}
1946
1947/* VNICC query VNIC characteristics request */
1948static int qeth_l2_vnicc_query_chars(struct qeth_card *card)
1949{
1950 struct qeth_cmd_buffer *iob;
1951
1952 QETH_CARD_TEXT(card, 2, "vniccqch");
1953 iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CHARS, 0);
1954 if (!iob)
1955 return -ENOMEM;
1956
1957 return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1958}
1959
1960/* VNICC query sub commands request */
1961static int qeth_l2_vnicc_query_cmds(struct qeth_card *card, u32 vnic_char,
1962 u32 *sup_cmds)
1963{
1964 struct qeth_cmd_buffer *iob;
1965
1966 QETH_CARD_TEXT(card, 2, "vniccqcm");
1967 iob = qeth_l2_vnicc_build_cmd(card, IPA_VNICC_QUERY_CMDS,
1968 VNICC_DATA_SIZEOF(query_cmds));
1969 if (!iob)
1970 return -ENOMEM;
1971
1972 __ipa_cmd(iob)->data.vnicc.data.query_cmds.vnic_char = vnic_char;
1973
1974 return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, sup_cmds);
1975}
1976
1977/* VNICC enable/disable characteristic request */
1978static int qeth_l2_vnicc_set_char(struct qeth_card *card, u32 vnic_char,
1979 u32 cmd)
1980{
1981 struct qeth_cmd_buffer *iob;
1982
1983 QETH_CARD_TEXT(card, 2, "vniccedc");
1984 iob = qeth_l2_vnicc_build_cmd(card, cmd, VNICC_DATA_SIZEOF(set_char));
1985 if (!iob)
1986 return -ENOMEM;
1987
1988 __ipa_cmd(iob)->data.vnicc.data.set_char.vnic_char = vnic_char;
1989
1990 return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, NULL);
1991}
1992
1993/* VNICC get/set timeout for characteristic request */
1994static int qeth_l2_vnicc_getset_timeout(struct qeth_card *card, u32 vnicc,
1995 u32 cmd, u32 *timeout)
1996{
1997 struct qeth_vnicc_getset_timeout *getset_timeout;
1998 struct qeth_cmd_buffer *iob;
1999
2000 QETH_CARD_TEXT(card, 2, "vniccgst");
2001 iob = qeth_l2_vnicc_build_cmd(card, cmd,
2002 VNICC_DATA_SIZEOF(getset_timeout));
2003 if (!iob)
2004 return -ENOMEM;
2005
2006 getset_timeout = &__ipa_cmd(iob)->data.vnicc.data.getset_timeout;
2007 getset_timeout->vnic_char = vnicc;
2008
2009 if (cmd == IPA_VNICC_SET_TIMEOUT)
2010 getset_timeout->timeout = *timeout;
2011
2012 return qeth_send_ipa_cmd(card, iob, qeth_l2_vnicc_request_cb, timeout);
2013}
2014
2015/* recover user timeout setting */
2016static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
2017 u32 *timeout)
2018{
2019 if (card->options.vnicc.sup_chars & vnicc &&
2020 card->options.vnicc.getset_timeout_sup & vnicc &&
2021 !qeth_l2_vnicc_getset_timeout(card, vnicc, IPA_VNICC_SET_TIMEOUT,
2022 timeout))
2023 return false;
2024 *timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2025 return true;
2026}
2027
2028/* set current VNICC flag state; called from sysfs store function */
2029int qeth_l2_vnicc_set_state(struct qeth_card *card, u32 vnicc, bool state)
2030{
2031 int rc = 0;
2032 u32 cmd;
2033
2034 QETH_CARD_TEXT(card, 2, "vniccsch");
2035
2036 /* check if characteristic and enable/disable are supported */
2037 if (!(card->options.vnicc.sup_chars & vnicc) ||
2038 !(card->options.vnicc.set_char_sup & vnicc))
2039 return -EOPNOTSUPP;
2040
2041 if (qeth_bridgeport_is_in_use(card))
2042 return -EBUSY;
2043
2044 /* set enable/disable command and store wanted characteristic */
2045 if (state) {
2046 cmd = IPA_VNICC_ENABLE;
2047 card->options.vnicc.wanted_chars |= vnicc;
2048 } else {
2049 cmd = IPA_VNICC_DISABLE;
2050 card->options.vnicc.wanted_chars &= ~vnicc;
2051 }
2052
2053 /* do we need to do anything? */
2054 if (card->options.vnicc.cur_chars == card->options.vnicc.wanted_chars)
2055 return rc;
2056
2057 /* if card is not ready, simply stop here */
2058 if (!qeth_card_hw_is_reachable(card)) {
2059 if (state)
2060 card->options.vnicc.cur_chars |= vnicc;
2061 else
2062 card->options.vnicc.cur_chars &= ~vnicc;
2063 return rc;
2064 }
2065
2066 rc = qeth_l2_vnicc_set_char(card, vnicc, cmd);
2067 if (rc)
2068 card->options.vnicc.wanted_chars =
2069 card->options.vnicc.cur_chars;
2070 else {
2071 /* successful online VNICC change; handle special cases */
2072 if (state && vnicc == QETH_VNICC_RX_BCAST)
2073 card->options.vnicc.rx_bcast_enabled = true;
2074 if (!state && vnicc == QETH_VNICC_LEARNING)
2075 qeth_l2_vnicc_recover_timeout(card, vnicc,
2076 &card->options.vnicc.learning_timeout);
2077 }
2078
2079 return rc;
2080}
2081
2082/* get current VNICC flag state; called from sysfs show function */
2083int qeth_l2_vnicc_get_state(struct qeth_card *card, u32 vnicc, bool *state)
2084{
2085 int rc = 0;
2086
2087 QETH_CARD_TEXT(card, 2, "vniccgch");
2088
2089 /* check if characteristic is supported */
2090 if (!(card->options.vnicc.sup_chars & vnicc))
2091 return -EOPNOTSUPP;
2092
2093 if (qeth_bridgeport_is_in_use(card))
2094 return -EBUSY;
2095
2096 /* if card is ready, query current VNICC state */
2097 if (qeth_card_hw_is_reachable(card))
2098 rc = qeth_l2_vnicc_query_chars(card);
2099
2100 *state = (card->options.vnicc.cur_chars & vnicc) ? true : false;
2101 return rc;
2102}
2103
2104/* set VNICC timeout; called from sysfs store function. Currently, only learning
2105 * supports timeout
2106 */
2107int qeth_l2_vnicc_set_timeout(struct qeth_card *card, u32 timeout)
2108{
2109 int rc = 0;
2110
2111 QETH_CARD_TEXT(card, 2, "vniccsto");
2112
2113 /* check if characteristic and set_timeout are supported */
2114 if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2115 !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2116 return -EOPNOTSUPP;
2117
2118 if (qeth_bridgeport_is_in_use(card))
2119 return -EBUSY;
2120
2121 /* do we need to do anything? */
2122 if (card->options.vnicc.learning_timeout == timeout)
2123 return rc;
2124
2125 /* if card is not ready, simply store the value internally and return */
2126 if (!qeth_card_hw_is_reachable(card)) {
2127 card->options.vnicc.learning_timeout = timeout;
2128 return rc;
2129 }
2130
2131 /* send timeout value to card; if successful, store value internally */
2132 rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2133 IPA_VNICC_SET_TIMEOUT, &timeout);
2134 if (!rc)
2135 card->options.vnicc.learning_timeout = timeout;
2136
2137 return rc;
2138}
2139
2140/* get current VNICC timeout; called from sysfs show function. Currently, only
2141 * learning supports timeout
2142 */
2143int qeth_l2_vnicc_get_timeout(struct qeth_card *card, u32 *timeout)
2144{
2145 int rc = 0;
2146
2147 QETH_CARD_TEXT(card, 2, "vniccgto");
2148
2149 /* check if characteristic and get_timeout are supported */
2150 if (!(card->options.vnicc.sup_chars & QETH_VNICC_LEARNING) ||
2151 !(card->options.vnicc.getset_timeout_sup & QETH_VNICC_LEARNING))
2152 return -EOPNOTSUPP;
2153
2154 if (qeth_bridgeport_is_in_use(card))
2155 return -EBUSY;
2156
2157 /* if card is ready, get timeout. Otherwise, just return stored value */
2158 *timeout = card->options.vnicc.learning_timeout;
2159 if (qeth_card_hw_is_reachable(card))
2160 rc = qeth_l2_vnicc_getset_timeout(card, QETH_VNICC_LEARNING,
2161 IPA_VNICC_GET_TIMEOUT,
2162 timeout);
2163
2164 return rc;
2165}
2166
2167/* check if VNICC is currently enabled */
2168static bool _qeth_l2_vnicc_is_in_use(struct qeth_card *card)
2169{
2170 if (!card->options.vnicc.sup_chars)
2171 return false;
2172 /* default values are only OK if rx_bcast was not enabled by user
2173 * or the card is offline.
2174 */
2175 if (card->options.vnicc.cur_chars == QETH_VNICC_DEFAULT) {
2176 if (!card->options.vnicc.rx_bcast_enabled ||
2177 !qeth_card_hw_is_reachable(card))
2178 return false;
2179 }
2180 return true;
2181}
2182
2183/**
2184 * qeth_bridgeport_allowed - are any qeth_bridgeport functions allowed?
2185 * @card: qeth_card structure pointer
2186 *
2187 * qeth_bridgeport functionality is mutually exclusive with usage of the
2188 * VNIC Characteristics and dev2br address notifications
2189 */
2190bool qeth_bridgeport_allowed(struct qeth_card *card)
2191{
2192 struct qeth_priv *priv = netdev_priv(card->dev);
2193
2194 return (!_qeth_l2_vnicc_is_in_use(card) &&
2195 !(priv->brport_features & BR_LEARNING_SYNC));
2196}
2197
2198/* recover user characteristic setting */
2199static bool qeth_l2_vnicc_recover_char(struct qeth_card *card, u32 vnicc,
2200 bool enable)
2201{
2202 u32 cmd = enable ? IPA_VNICC_ENABLE : IPA_VNICC_DISABLE;
2203
2204 if (card->options.vnicc.sup_chars & vnicc &&
2205 card->options.vnicc.set_char_sup & vnicc &&
2206 !qeth_l2_vnicc_set_char(card, vnicc, cmd))
2207 return false;
2208 card->options.vnicc.wanted_chars &= ~vnicc;
2209 card->options.vnicc.wanted_chars |= QETH_VNICC_DEFAULT & vnicc;
2210 return true;
2211}
2212
2213/* (re-)initialize VNICC */
2214static void qeth_l2_vnicc_init(struct qeth_card *card)
2215{
2216 u32 *timeout = &card->options.vnicc.learning_timeout;
2217 bool enable, error = false;
2218 unsigned int chars_len, i;
2219 unsigned long chars_tmp;
2220 u32 sup_cmds, vnicc;
2221
2222 QETH_CARD_TEXT(card, 2, "vniccini");
2223 /* reset rx_bcast */
2224 card->options.vnicc.rx_bcast_enabled = 0;
2225 /* initial query and storage of VNIC characteristics */
2226 if (qeth_l2_vnicc_query_chars(card)) {
2227 if (card->options.vnicc.wanted_chars != QETH_VNICC_DEFAULT ||
2228 *timeout != QETH_VNICC_DEFAULT_TIMEOUT)
2229 dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2230 /* fail quietly if user didn't change the default config */
2231 card->options.vnicc.sup_chars = 0;
2232 card->options.vnicc.cur_chars = 0;
2233 card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2234 return;
2235 }
2236 /* get supported commands for each supported characteristic */
2237 chars_tmp = card->options.vnicc.sup_chars;
2238 chars_len = sizeof(card->options.vnicc.sup_chars) * BITS_PER_BYTE;
2239 for_each_set_bit(i, &chars_tmp, chars_len) {
2240 vnicc = BIT(i);
2241 if (qeth_l2_vnicc_query_cmds(card, vnicc, &sup_cmds)) {
2242 sup_cmds = 0;
2243 error = true;
2244 }
2245 if ((sup_cmds & IPA_VNICC_SET_TIMEOUT) &&
2246 (sup_cmds & IPA_VNICC_GET_TIMEOUT))
2247 card->options.vnicc.getset_timeout_sup |= vnicc;
2248 else
2249 card->options.vnicc.getset_timeout_sup &= ~vnicc;
2250 if ((sup_cmds & IPA_VNICC_ENABLE) &&
2251 (sup_cmds & IPA_VNICC_DISABLE))
2252 card->options.vnicc.set_char_sup |= vnicc;
2253 else
2254 card->options.vnicc.set_char_sup &= ~vnicc;
2255 }
2256 /* enforce assumed default values and recover settings, if changed */
2257 error |= qeth_l2_vnicc_recover_timeout(card, QETH_VNICC_LEARNING,
2258 timeout);
2259 /* Change chars, if necessary */
2260 chars_tmp = card->options.vnicc.wanted_chars ^
2261 card->options.vnicc.cur_chars;
2262 chars_len = sizeof(card->options.vnicc.wanted_chars) * BITS_PER_BYTE;
2263 for_each_set_bit(i, &chars_tmp, chars_len) {
2264 vnicc = BIT(i);
2265 enable = card->options.vnicc.wanted_chars & vnicc;
2266 error |= qeth_l2_vnicc_recover_char(card, vnicc, enable);
2267 }
2268 if (error)
2269 dev_err(&card->gdev->dev, "Configuring the VNIC characteristics failed\n");
2270}
2271
2272/* configure default values of VNIC characteristics */
2273static void qeth_l2_vnicc_set_defaults(struct qeth_card *card)
2274{
2275 /* characteristics values */
2276 card->options.vnicc.sup_chars = QETH_VNICC_ALL;
2277 card->options.vnicc.cur_chars = QETH_VNICC_DEFAULT;
2278 card->options.vnicc.learning_timeout = QETH_VNICC_DEFAULT_TIMEOUT;
2279 /* supported commands */
2280 card->options.vnicc.set_char_sup = QETH_VNICC_ALL;
2281 card->options.vnicc.getset_timeout_sup = QETH_VNICC_LEARNING;
2282 /* settings wanted by users */
2283 card->options.vnicc.wanted_chars = QETH_VNICC_DEFAULT;
2284}
2285
2286static const struct device_type qeth_l2_devtype = {
2287 .name = "qeth_layer2",
2288 .groups = qeth_l2_attr_groups,
2289};
2290
2291static int qeth_l2_probe_device(struct ccwgroup_device *gdev)
2292{
2293 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2294 int rc;
2295
2296 qeth_l2_vnicc_set_defaults(card);
2297 mutex_init(&card->sbp_lock);
2298
2299 if (gdev->dev.type) {
2300 rc = device_add_groups(&gdev->dev, qeth_l2_attr_groups);
2301 if (rc)
2302 return rc;
2303 } else {
2304 gdev->dev.type = &qeth_l2_devtype;
2305 }
2306
2307 INIT_WORK(&card->rx_mode_work, qeth_l2_rx_mode_work);
2308 return 0;
2309}
2310
2311static void qeth_l2_remove_device(struct ccwgroup_device *gdev)
2312{
2313 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2314 struct qeth_priv *priv;
2315
2316 if (gdev->dev.type != &qeth_l2_devtype)
2317 device_remove_groups(&gdev->dev, qeth_l2_attr_groups);
2318
2319 qeth_set_allowed_threads(card, 0, 1);
2320 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2321
2322 if (gdev->state == CCWGROUP_ONLINE)
2323 qeth_set_offline(card, card->discipline, false);
2324
2325 if (card->dev->reg_state == NETREG_REGISTERED) {
2326 priv = netdev_priv(card->dev);
2327 if (priv->brport_features & BR_LEARNING_SYNC) {
2328 rtnl_lock();
2329 qeth_l2_br2dev_put();
2330 rtnl_unlock();
2331 }
2332 unregister_netdev(card->dev);
2333 }
2334}
2335
2336static int qeth_l2_set_online(struct qeth_card *card, bool carrier_ok)
2337{
2338 struct net_device *dev = card->dev;
2339 int rc = 0;
2340
2341 qeth_l2_detect_dev2br_support(card);
2342
2343 mutex_lock(&card->sbp_lock);
2344 qeth_bridgeport_query_support(card);
2345 if (card->options.sbp.supported_funcs) {
2346 qeth_l2_setup_bridgeport_attrs(card);
2347 dev_info(&card->gdev->dev,
2348 "The device represents a Bridge Capable Port\n");
2349 }
2350 mutex_unlock(&card->sbp_lock);
2351
2352 qeth_l2_register_dev_addr(card);
2353
2354 /* for the rx_bcast characteristic, init VNICC after setmac */
2355 qeth_l2_vnicc_init(card);
2356
2357 qeth_l2_trace_features(card);
2358
2359 /* softsetup */
2360 QETH_CARD_TEXT(card, 2, "softsetp");
2361
2362 card->state = CARD_STATE_SOFTSETUP;
2363
2364 qeth_set_allowed_threads(card, 0xffffffff, 0);
2365
2366 if (dev->reg_state != NETREG_REGISTERED) {
2367 rc = qeth_l2_setup_netdev(card);
2368 if (rc)
2369 goto err_setup;
2370
2371 if (carrier_ok)
2372 netif_carrier_on(dev);
2373 } else {
2374 rtnl_lock();
2375 rc = qeth_set_real_num_tx_queues(card,
2376 qeth_tx_actual_queues(card));
2377 if (rc) {
2378 rtnl_unlock();
2379 goto err_set_queues;
2380 }
2381
2382 if (carrier_ok)
2383 netif_carrier_on(dev);
2384 else
2385 netif_carrier_off(dev);
2386
2387 netif_device_attach(dev);
2388 qeth_enable_hw_features(dev);
2389 qeth_l2_enable_brport_features(card);
2390
2391 if (netif_running(dev)) {
2392 local_bh_disable();
2393 napi_schedule(&card->napi);
2394 /* kick-start the NAPI softirq: */
2395 local_bh_enable();
2396 qeth_l2_set_rx_mode(dev);
2397 }
2398 rtnl_unlock();
2399 }
2400 return 0;
2401
2402err_set_queues:
2403err_setup:
2404 qeth_set_allowed_threads(card, 0, 1);
2405 card->state = CARD_STATE_DOWN;
2406 return rc;
2407}
2408
2409static void qeth_l2_set_offline(struct qeth_card *card)
2410{
2411 struct qeth_priv *priv = netdev_priv(card->dev);
2412
2413 qeth_set_allowed_threads(card, 0, 1);
2414 qeth_l2_drain_rx_mode_cache(card);
2415
2416 if (card->state == CARD_STATE_SOFTSETUP)
2417 card->state = CARD_STATE_DOWN;
2418
2419 qeth_l2_set_pnso_mode(card, QETH_PNSO_NONE);
2420 if (priv->brport_features & BR_LEARNING_SYNC)
2421 qeth_l2_dev2br_fdb_flush(card);
2422}
2423
2424/* Returns zero if the command is successfully "consumed" */
2425static int qeth_l2_control_event(struct qeth_card *card,
2426 struct qeth_ipa_cmd *cmd)
2427{
2428 switch (cmd->hdr.command) {
2429 case IPA_CMD_SETBRIDGEPORT_OSA:
2430 case IPA_CMD_SETBRIDGEPORT_IQD:
2431 if (cmd->data.sbp.hdr.command_code ==
2432 IPA_SBP_BRIDGE_PORT_STATE_CHANGE) {
2433 qeth_bridge_state_change(card, cmd);
2434 return 0;
2435 }
2436
2437 return 1;
2438 case IPA_CMD_ADDRESS_CHANGE_NOTIF:
2439 qeth_addr_change_event(card, cmd);
2440 return 0;
2441 default:
2442 return 1;
2443 }
2444}
2445
2446const struct qeth_discipline qeth_l2_discipline = {
2447 .setup = qeth_l2_probe_device,
2448 .remove = qeth_l2_remove_device,
2449 .set_online = qeth_l2_set_online,
2450 .set_offline = qeth_l2_set_offline,
2451 .control_event_handler = qeth_l2_control_event,
2452};
2453EXPORT_SYMBOL_GPL(qeth_l2_discipline);
2454
2455static int __init qeth_l2_init(void)
2456{
2457 pr_info("register layer 2 discipline\n");
2458 refcount_set(&qeth_l2_switchdev_notify_refcnt, 0);
2459 return 0;
2460}
2461
2462static void __exit qeth_l2_exit(void)
2463{
2464 pr_info("unregister layer 2 discipline\n");
2465}
2466
2467module_init(qeth_l2_init);
2468module_exit(qeth_l2_exit);
2469MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2470MODULE_DESCRIPTION("qeth layer 2 discipline");
2471MODULE_LICENSE("GPL");
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
27static int qeth_l2_set_offline(struct ccwgroup_device *);
28static void qeth_bridgeport_query_support(struct qeth_card *card);
29static void qeth_bridge_state_change(struct qeth_card *card,
30 struct qeth_ipa_cmd *cmd);
31static void qeth_bridge_host_event(struct qeth_card *card,
32 struct qeth_ipa_cmd *cmd);
33static void qeth_l2_vnicc_set_defaults(struct qeth_card *card);
34static void qeth_l2_vnicc_init(struct qeth_card *card);
35static bool qeth_l2_vnicc_recover_timeout(struct qeth_card *card, u32 vnicc,
36 u32 *timeout);
37
38static 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
72static 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
81static 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
98static 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
122static 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
139static 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
153static 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
165static 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
199static 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
220static 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
236static 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
252static 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
264static 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
276static 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
300static 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
349static 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
380out:
381 QETH_CARD_HEX(card, 2, card->dev->dev_addr, card->dev->addr_len);
382 return 0;
383}
384
385static 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
394static 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
405static 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
440static 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
463static 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*/
484static 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
506static 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
550static 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);
581out:
582 if (rc && hd_len)
583 kmem_cache_free(qeth_core_header_cache, hdr);
584 return rc;
585}
586
587static 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
615static 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
627static const struct device_type qeth_l2_devtype = {
628 .name = "qeth_layer2",
629 .groups = qeth_l2_attr_groups,
630};
631
632static 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
651static 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
668static 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
675static 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
693static 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
702static 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
756add_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
767static 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
775static 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
787static 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
877out_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
889static 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
928static int qeth_l2_set_offline(struct ccwgroup_device *cgdev)
929{
930 return __qeth_l2_set_offline(cgdev, 0);
931}
932
933static 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
960static int __init qeth_l2_init(void)
961{
962 pr_info("register layer 2 discipline\n");
963 return 0;
964}
965
966static 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" */
972static 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
992struct 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};
1003EXPORT_SYMBOL_GPL(qeth_l2_discipline);
1004
1005static 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
1013int 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}
1039EXPORT_SYMBOL(qeth_osn_assist);
1040
1041int 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}
1063EXPORT_SYMBOL(qeth_osn_register);
1064
1065void 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}
1079EXPORT_SYMBOL(qeth_osn_deregister);
1080
1081/* SETBRIDGEPORT support, async notifications */
1082
1083enum 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 */
1100static 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
1148struct qeth_bridge_state_data {
1149 struct work_struct worker;
1150 struct qeth_card *card;
1151 struct qeth_sbp_state_change qports;
1152};
1153
1154static 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
1192static 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
1219struct qeth_bridge_host_data {
1220 struct work_struct worker;
1221 struct qeth_card *card;
1222 struct qeth_ipacmd_addr_change hostevs;
1223};
1224
1225static 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
1257static 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
1293struct _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
1303static 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
1401static 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
1425static 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 */
1449static 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
1469static 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 */
1506int 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
1530static 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 */
1546int 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 */
1582static 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
1614static 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 */
1645int 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
1668static 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 */
1677static 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 */
1709struct _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 */
1719static 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
1744static 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 */
1764static 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 */
1777static 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 */
1798static 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 */
1814static 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 */
1841int 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 */
1896int 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 */
1921int 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 */
1958int 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 */
1983bool 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 */
2000static 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 */
2013static 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 */
2028static 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 */
2086static 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
2099module_init(qeth_l2_init);
2100module_exit(qeth_l2_exit);
2101MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2102MODULE_DESCRIPTION("qeth layer 2 discipline");
2103MODULE_LICENSE("GPL");