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