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