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