Merge tag 'stratix10_dts_fix_for_v4.20' of git://git.kernel.org/pub/scm/linux/kernel...
[muen/linux.git] / drivers / s390 / net / qeth_l3_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *               Frank Pavlic <fpavlic@de.ibm.com>,
6  *               Thomas Spatzier <tspat@de.ibm.com>,
7  *               Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/bitops.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ip.h>
21 #include <linux/in.h>
22 #include <linux/ipv6.h>
23 #include <linux/inetdevice.h>
24 #include <linux/igmp.h>
25 #include <linux/slab.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/skbuff.h>
29
30 #include <net/ip.h>
31 #include <net/arp.h>
32 #include <net/route.h>
33 #include <net/ipv6.h>
34 #include <net/ip6_route.h>
35 #include <net/ip6_fib.h>
36 #include <net/iucv/af_iucv.h>
37 #include <linux/hashtable.h>
38
39 #include "qeth_l3.h"
40
41
42 static int qeth_l3_set_offline(struct ccwgroup_device *);
43 static int qeth_l3_stop(struct net_device *);
44 static void qeth_l3_set_rx_mode(struct net_device *dev);
45 static int qeth_l3_register_addr_entry(struct qeth_card *,
46                 struct qeth_ipaddr *);
47 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
48                 struct qeth_ipaddr *);
49
50 static void qeth_l3_ipaddr4_to_string(const __u8 *addr, char *buf)
51 {
52         sprintf(buf, "%pI4", addr);
53 }
54
55 static void qeth_l3_ipaddr6_to_string(const __u8 *addr, char *buf)
56 {
57         sprintf(buf, "%pI6", addr);
58 }
59
60 void qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const __u8 *addr,
61                                 char *buf)
62 {
63         if (proto == QETH_PROT_IPV4)
64                 qeth_l3_ipaddr4_to_string(addr, buf);
65         else if (proto == QETH_PROT_IPV6)
66                 qeth_l3_ipaddr6_to_string(addr, buf);
67 }
68
69 static struct qeth_ipaddr *qeth_l3_get_addr_buffer(enum qeth_prot_versions prot)
70 {
71         struct qeth_ipaddr *addr = kmalloc(sizeof(*addr), GFP_ATOMIC);
72
73         if (addr)
74                 qeth_l3_init_ipaddr(addr, QETH_IP_TYPE_NORMAL, prot);
75         return addr;
76 }
77
78 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
79                                                    struct qeth_ipaddr *query)
80 {
81         u64 key = qeth_l3_ipaddr_hash(query);
82         struct qeth_ipaddr *addr;
83
84         if (query->is_multicast) {
85                 hash_for_each_possible(card->ip_mc_htable, addr, hnode, key)
86                         if (qeth_l3_addr_match_ip(addr, query))
87                                 return addr;
88         } else {
89                 hash_for_each_possible(card->ip_htable,  addr, hnode, key)
90                         if (qeth_l3_addr_match_ip(addr, query))
91                                 return addr;
92         }
93         return NULL;
94 }
95
96 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
97 {
98         int i, j;
99         u8 octet;
100
101         for (i = 0; i < len; ++i) {
102                 octet = addr[i];
103                 for (j = 7; j >= 0; --j) {
104                         bits[i*8 + j] = octet & 1;
105                         octet >>= 1;
106                 }
107         }
108 }
109
110 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
111                                              struct qeth_ipaddr *addr)
112 {
113         struct qeth_ipato_entry *ipatoe;
114         u8 addr_bits[128] = {0, };
115         u8 ipatoe_bits[128] = {0, };
116         int rc = 0;
117
118         if (!card->ipato.enabled)
119                 return false;
120         if (addr->type != QETH_IP_TYPE_NORMAL)
121                 return false;
122
123         qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
124                                   (addr->proto == QETH_PROT_IPV4)? 4:16);
125         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
126                 if (addr->proto != ipatoe->proto)
127                         continue;
128                 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
129                                           (ipatoe->proto == QETH_PROT_IPV4) ?
130                                           4 : 16);
131                 if (addr->proto == QETH_PROT_IPV4)
132                         rc = !memcmp(addr_bits, ipatoe_bits,
133                                      min(32, ipatoe->mask_bits));
134                 else
135                         rc = !memcmp(addr_bits, ipatoe_bits,
136                                      min(128, ipatoe->mask_bits));
137                 if (rc)
138                         break;
139         }
140         /* invert? */
141         if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
142                 rc = !rc;
143         else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
144                 rc = !rc;
145
146         return rc;
147 }
148
149 static int qeth_l3_delete_ip(struct qeth_card *card,
150                              struct qeth_ipaddr *tmp_addr)
151 {
152         int rc = 0;
153         struct qeth_ipaddr *addr;
154
155         if (tmp_addr->type == QETH_IP_TYPE_RXIP)
156                 QETH_CARD_TEXT(card, 2, "delrxip");
157         else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
158                 QETH_CARD_TEXT(card, 2, "delvipa");
159         else
160                 QETH_CARD_TEXT(card, 2, "delip");
161
162         if (tmp_addr->proto == QETH_PROT_IPV4)
163                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
164         else {
165                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
166                 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
167         }
168
169         addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
170         if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
171                 return -ENOENT;
172
173         addr->ref_counter--;
174         if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
175                 return rc;
176         if (addr->in_progress)
177                 return -EINPROGRESS;
178
179         if (qeth_card_hw_is_reachable(card))
180                 rc = qeth_l3_deregister_addr_entry(card, addr);
181
182         hash_del(&addr->hnode);
183         kfree(addr);
184
185         return rc;
186 }
187
188 static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
189 {
190         int rc = 0;
191         struct qeth_ipaddr *addr;
192         char buf[40];
193
194         if (tmp_addr->type == QETH_IP_TYPE_RXIP)
195                 QETH_CARD_TEXT(card, 2, "addrxip");
196         else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
197                 QETH_CARD_TEXT(card, 2, "addvipa");
198         else
199                 QETH_CARD_TEXT(card, 2, "addip");
200
201         if (tmp_addr->proto == QETH_PROT_IPV4)
202                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
203         else {
204                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
205                 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
206         }
207
208         addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
209         if (addr) {
210                 if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
211                         return -EADDRINUSE;
212                 if (qeth_l3_addr_match_all(addr, tmp_addr)) {
213                         addr->ref_counter++;
214                         return 0;
215                 }
216                 qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
217                                          buf);
218                 dev_warn(&card->gdev->dev,
219                          "Registering IP address %s failed\n", buf);
220                 return -EADDRINUSE;
221         } else {
222                 addr = qeth_l3_get_addr_buffer(tmp_addr->proto);
223                 if (!addr)
224                         return -ENOMEM;
225
226                 memcpy(addr, tmp_addr, sizeof(struct qeth_ipaddr));
227                 addr->ref_counter = 1;
228
229                 if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
230                         QETH_CARD_TEXT(card, 2, "tkovaddr");
231                         addr->ipato = 1;
232                 }
233                 hash_add(card->ip_htable, &addr->hnode,
234                                 qeth_l3_ipaddr_hash(addr));
235
236                 if (!qeth_card_hw_is_reachable(card)) {
237                         addr->disp_flag = QETH_DISP_ADDR_ADD;
238                         return 0;
239                 }
240
241                 /* qeth_l3_register_addr_entry can go to sleep
242                  * if we add a IPV4 addr. It is caused by the reason
243                  * that SETIP ipa cmd starts ARP staff for IPV4 addr.
244                  * Thus we should unlock spinlock, and make a protection
245                  * using in_progress variable to indicate that there is
246                  * an hardware operation with this IPV4 address
247                  */
248                 if (addr->proto == QETH_PROT_IPV4) {
249                         addr->in_progress = 1;
250                         spin_unlock_bh(&card->ip_lock);
251                         rc = qeth_l3_register_addr_entry(card, addr);
252                         spin_lock_bh(&card->ip_lock);
253                         addr->in_progress = 0;
254                 } else
255                         rc = qeth_l3_register_addr_entry(card, addr);
256
257                 if (!rc || (rc == IPA_RC_DUPLICATE_IP_ADDRESS) ||
258                                 (rc == IPA_RC_LAN_OFFLINE)) {
259                         addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
260                         if (addr->ref_counter < 1) {
261                                 qeth_l3_deregister_addr_entry(card, addr);
262                                 hash_del(&addr->hnode);
263                                 kfree(addr);
264                         }
265                 } else {
266                         hash_del(&addr->hnode);
267                         kfree(addr);
268                 }
269         }
270         return rc;
271 }
272
273 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
274 {
275         struct qeth_ipaddr *addr;
276         struct hlist_node *tmp;
277         int i;
278
279         QETH_CARD_TEXT(card, 4, "clearip");
280
281         if (recover && card->options.sniffer)
282                 return;
283
284         spin_lock_bh(&card->ip_lock);
285
286         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
287                 if (!recover) {
288                         hash_del(&addr->hnode);
289                         kfree(addr);
290                         continue;
291                 }
292                 addr->disp_flag = QETH_DISP_ADDR_ADD;
293         }
294
295         spin_unlock_bh(&card->ip_lock);
296
297         spin_lock_bh(&card->mclock);
298
299         hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
300                 hash_del(&addr->hnode);
301                 kfree(addr);
302         }
303
304         spin_unlock_bh(&card->mclock);
305
306
307 }
308 static void qeth_l3_recover_ip(struct qeth_card *card)
309 {
310         struct qeth_ipaddr *addr;
311         struct hlist_node *tmp;
312         int i;
313         int rc;
314
315         QETH_CARD_TEXT(card, 4, "recovrip");
316
317         spin_lock_bh(&card->ip_lock);
318
319         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
320                 if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
321                         if (addr->proto == QETH_PROT_IPV4) {
322                                 addr->in_progress = 1;
323                                 spin_unlock_bh(&card->ip_lock);
324                                 rc = qeth_l3_register_addr_entry(card, addr);
325                                 spin_lock_bh(&card->ip_lock);
326                                 addr->in_progress = 0;
327                         } else
328                                 rc = qeth_l3_register_addr_entry(card, addr);
329
330                         if (!rc) {
331                                 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
332                                 if (addr->ref_counter < 1)
333                                         qeth_l3_delete_ip(card, addr);
334                         } else {
335                                 hash_del(&addr->hnode);
336                                 kfree(addr);
337                         }
338                 }
339         }
340
341         spin_unlock_bh(&card->ip_lock);
342
343 }
344
345 static int qeth_l3_send_setdelmc(struct qeth_card *card,
346                         struct qeth_ipaddr *addr, int ipacmd)
347 {
348         int rc;
349         struct qeth_cmd_buffer *iob;
350         struct qeth_ipa_cmd *cmd;
351
352         QETH_CARD_TEXT(card, 4, "setdelmc");
353
354         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
355         if (!iob)
356                 return -ENOMEM;
357         cmd = __ipa_cmd(iob);
358         ether_addr_copy(cmd->data.setdelipm.mac, addr->mac);
359         if (addr->proto == QETH_PROT_IPV6)
360                 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
361                        sizeof(struct in6_addr));
362         else
363                 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
364
365         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
366
367         return rc;
368 }
369
370 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
371 {
372         int i, j;
373         for (i = 0; i < 16; i++) {
374                 j = (len) - (i * 8);
375                 if (j >= 8)
376                         netmask[i] = 0xff;
377                 else if (j > 0)
378                         netmask[i] = (u8)(0xFF00 >> j);
379                 else
380                         netmask[i] = 0;
381         }
382 }
383
384 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
385 {
386         switch (addr->type) {
387         case QETH_IP_TYPE_RXIP:
388                 return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
389         case QETH_IP_TYPE_VIPA:
390                 return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
391                                QETH_IPA_DELIP_VIPA_FLAG;
392         default:
393                 return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
394         }
395 }
396
397 static int qeth_l3_send_setdelip(struct qeth_card *card,
398                                  struct qeth_ipaddr *addr,
399                                  enum qeth_ipa_cmds ipacmd)
400 {
401         struct qeth_cmd_buffer *iob;
402         struct qeth_ipa_cmd *cmd;
403         __u8 netmask[16];
404         u32 flags;
405
406         QETH_CARD_TEXT(card, 4, "setdelip");
407
408         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
409         if (!iob)
410                 return -ENOMEM;
411         cmd = __ipa_cmd(iob);
412
413         flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
414         QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
415
416         if (addr->proto == QETH_PROT_IPV6) {
417                 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
418                        sizeof(struct in6_addr));
419                 qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
420                 memcpy(cmd->data.setdelip6.mask, netmask,
421                        sizeof(struct in6_addr));
422                 cmd->data.setdelip6.flags = flags;
423         } else {
424                 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
425                 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
426                 cmd->data.setdelip4.flags = flags;
427         }
428
429         return qeth_send_ipa_cmd(card, iob, NULL, NULL);
430 }
431
432 static int qeth_l3_send_setrouting(struct qeth_card *card,
433         enum qeth_routing_types type, enum qeth_prot_versions prot)
434 {
435         int rc;
436         struct qeth_ipa_cmd *cmd;
437         struct qeth_cmd_buffer *iob;
438
439         QETH_CARD_TEXT(card, 4, "setroutg");
440         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
441         if (!iob)
442                 return -ENOMEM;
443         cmd = __ipa_cmd(iob);
444         cmd->data.setrtg.type = (type);
445         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
446
447         return rc;
448 }
449
450 static int qeth_l3_correct_routing_type(struct qeth_card *card,
451                 enum qeth_routing_types *type, enum qeth_prot_versions prot)
452 {
453         if (card->info.type == QETH_CARD_TYPE_IQD) {
454                 switch (*type) {
455                 case NO_ROUTER:
456                 case PRIMARY_CONNECTOR:
457                 case SECONDARY_CONNECTOR:
458                 case MULTICAST_ROUTER:
459                         return 0;
460                 default:
461                         goto out_inval;
462                 }
463         } else {
464                 switch (*type) {
465                 case NO_ROUTER:
466                 case PRIMARY_ROUTER:
467                 case SECONDARY_ROUTER:
468                         return 0;
469                 case MULTICAST_ROUTER:
470                         if (qeth_is_ipafunc_supported(card, prot,
471                                                       IPA_OSA_MC_ROUTER))
472                                 return 0;
473                 default:
474                         goto out_inval;
475                 }
476         }
477 out_inval:
478         *type = NO_ROUTER;
479         return -EINVAL;
480 }
481
482 int qeth_l3_setrouting_v4(struct qeth_card *card)
483 {
484         int rc;
485
486         QETH_CARD_TEXT(card, 3, "setrtg4");
487
488         rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
489                                   QETH_PROT_IPV4);
490         if (rc)
491                 return rc;
492
493         rc = qeth_l3_send_setrouting(card, card->options.route4.type,
494                                   QETH_PROT_IPV4);
495         if (rc) {
496                 card->options.route4.type = NO_ROUTER;
497                 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
498                         " on %s. Type set to 'no router'.\n", rc,
499                         QETH_CARD_IFNAME(card));
500         }
501         return rc;
502 }
503
504 int qeth_l3_setrouting_v6(struct qeth_card *card)
505 {
506         int rc = 0;
507
508         QETH_CARD_TEXT(card, 3, "setrtg6");
509
510         if (!qeth_is_supported(card, IPA_IPV6))
511                 return 0;
512         rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
513                                   QETH_PROT_IPV6);
514         if (rc)
515                 return rc;
516
517         rc = qeth_l3_send_setrouting(card, card->options.route6.type,
518                                   QETH_PROT_IPV6);
519         if (rc) {
520                 card->options.route6.type = NO_ROUTER;
521                 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
522                         " on %s. Type set to 'no router'.\n", rc,
523                         QETH_CARD_IFNAME(card));
524         }
525         return rc;
526 }
527
528 /*
529  * IP address takeover related functions
530  */
531
532 /**
533  * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
534  *
535  * Caller must hold ip_lock.
536  */
537 void qeth_l3_update_ipato(struct qeth_card *card)
538 {
539         struct qeth_ipaddr *addr;
540         unsigned int i;
541
542         hash_for_each(card->ip_htable, i, addr, hnode) {
543                 if (addr->type != QETH_IP_TYPE_NORMAL)
544                         continue;
545                 addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
546         }
547 }
548
549 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
550 {
551         struct qeth_ipato_entry *ipatoe, *tmp;
552
553         spin_lock_bh(&card->ip_lock);
554
555         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
556                 list_del(&ipatoe->entry);
557                 kfree(ipatoe);
558         }
559
560         qeth_l3_update_ipato(card);
561         spin_unlock_bh(&card->ip_lock);
562 }
563
564 int qeth_l3_add_ipato_entry(struct qeth_card *card,
565                                 struct qeth_ipato_entry *new)
566 {
567         struct qeth_ipato_entry *ipatoe;
568         int rc = 0;
569
570         QETH_CARD_TEXT(card, 2, "addipato");
571
572         spin_lock_bh(&card->ip_lock);
573
574         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
575                 if (ipatoe->proto != new->proto)
576                         continue;
577                 if (!memcmp(ipatoe->addr, new->addr,
578                             (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
579                     (ipatoe->mask_bits == new->mask_bits)) {
580                         rc = -EEXIST;
581                         break;
582                 }
583         }
584
585         if (!rc) {
586                 list_add_tail(&new->entry, &card->ipato.entries);
587                 qeth_l3_update_ipato(card);
588         }
589
590         spin_unlock_bh(&card->ip_lock);
591
592         return rc;
593 }
594
595 int qeth_l3_del_ipato_entry(struct qeth_card *card,
596                             enum qeth_prot_versions proto, u8 *addr,
597                             int mask_bits)
598 {
599         struct qeth_ipato_entry *ipatoe, *tmp;
600         int rc = -ENOENT;
601
602         QETH_CARD_TEXT(card, 2, "delipato");
603
604         spin_lock_bh(&card->ip_lock);
605
606         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
607                 if (ipatoe->proto != proto)
608                         continue;
609                 if (!memcmp(ipatoe->addr, addr,
610                             (proto == QETH_PROT_IPV4)? 4:16) &&
611                     (ipatoe->mask_bits == mask_bits)) {
612                         list_del(&ipatoe->entry);
613                         qeth_l3_update_ipato(card);
614                         kfree(ipatoe);
615                         rc = 0;
616                 }
617         }
618
619         spin_unlock_bh(&card->ip_lock);
620         return rc;
621 }
622
623 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
624                              enum qeth_ip_types type,
625                              enum qeth_prot_versions proto)
626 {
627         struct qeth_ipaddr addr;
628         int rc;
629
630         qeth_l3_init_ipaddr(&addr, type, proto);
631         if (proto == QETH_PROT_IPV4)
632                 memcpy(&addr.u.a4.addr, ip, 4);
633         else
634                 memcpy(&addr.u.a6.addr, ip, 16);
635
636         spin_lock_bh(&card->ip_lock);
637         rc = add ? qeth_l3_add_ip(card, &addr) : qeth_l3_delete_ip(card, &addr);
638         spin_unlock_bh(&card->ip_lock);
639         return rc;
640 }
641
642 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
643 {
644         struct qeth_ipaddr addr;
645         int rc, i;
646
647         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
648         addr.u.a6.addr.s6_addr[0] = 0xfe;
649         addr.u.a6.addr.s6_addr[1] = 0x80;
650         for (i = 0; i < 8; i++)
651                 addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
652
653         spin_lock_bh(&card->ip_lock);
654         rc = add ? qeth_l3_add_ip(card, &addr) : qeth_l3_delete_ip(card, &addr);
655         spin_unlock_bh(&card->ip_lock);
656         return rc;
657 }
658
659 static int qeth_l3_register_addr_entry(struct qeth_card *card,
660                                 struct qeth_ipaddr *addr)
661 {
662         char buf[50];
663         int rc = 0;
664         int cnt = 3;
665
666
667         if (addr->proto == QETH_PROT_IPV4) {
668                 QETH_CARD_TEXT(card, 2, "setaddr4");
669                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
670         } else if (addr->proto == QETH_PROT_IPV6) {
671                 QETH_CARD_TEXT(card, 2, "setaddr6");
672                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
673                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
674         } else {
675                 QETH_CARD_TEXT(card, 2, "setaddr?");
676                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
677         }
678         do {
679                 if (addr->is_multicast)
680                         rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
681                 else
682                         rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
683                 if (rc)
684                         QETH_CARD_TEXT(card, 2, "failed");
685         } while ((--cnt > 0) && rc);
686         if (rc) {
687                 QETH_CARD_TEXT(card, 2, "FAILED");
688                 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
689                 dev_warn(&card->gdev->dev,
690                         "Registering IP address %s failed\n", buf);
691         }
692         return rc;
693 }
694
695 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
696                                                 struct qeth_ipaddr *addr)
697 {
698         int rc = 0;
699
700         if (addr->proto == QETH_PROT_IPV4) {
701                 QETH_CARD_TEXT(card, 2, "deladdr4");
702                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
703         } else if (addr->proto == QETH_PROT_IPV6) {
704                 QETH_CARD_TEXT(card, 2, "deladdr6");
705                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
706                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
707         } else {
708                 QETH_CARD_TEXT(card, 2, "deladdr?");
709                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
710         }
711         if (addr->is_multicast)
712                 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
713         else
714                 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP);
715         if (rc)
716                 QETH_CARD_TEXT(card, 2, "failed");
717
718         return rc;
719 }
720
721 static int qeth_l3_setadapter_parms(struct qeth_card *card)
722 {
723         int rc = 0;
724
725         QETH_DBF_TEXT(SETUP, 2, "setadprm");
726
727         if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
728                 rc = qeth_setadpparms_change_macaddr(card);
729                 if (rc)
730                         dev_warn(&card->gdev->dev, "Reading the adapter MAC"
731                                 " address failed\n");
732         }
733
734         return rc;
735 }
736
737 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
738 {
739         int rc;
740
741         QETH_CARD_TEXT(card, 3, "ipaarp");
742
743         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
744                 dev_info(&card->gdev->dev,
745                         "ARP processing not supported on %s!\n",
746                         QETH_CARD_IFNAME(card));
747                 return 0;
748         }
749         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
750                                           IPA_CMD_ASS_START, 0);
751         if (rc) {
752                 dev_warn(&card->gdev->dev,
753                         "Starting ARP processing support for %s failed\n",
754                         QETH_CARD_IFNAME(card));
755         }
756         return rc;
757 }
758
759 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
760 {
761         int rc;
762
763         QETH_CARD_TEXT(card, 3, "stsrcmac");
764
765         if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
766                 dev_info(&card->gdev->dev,
767                         "Inbound source MAC-address not supported on %s\n",
768                         QETH_CARD_IFNAME(card));
769                 return -EOPNOTSUPP;
770         }
771
772         rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
773                                           IPA_CMD_ASS_START, 0);
774         if (rc)
775                 dev_warn(&card->gdev->dev,
776                         "Starting source MAC-address support for %s failed\n",
777                         QETH_CARD_IFNAME(card));
778         return rc;
779 }
780
781 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
782 {
783         int rc = 0;
784
785         QETH_CARD_TEXT(card, 3, "strtvlan");
786
787         if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
788                 dev_info(&card->gdev->dev,
789                         "VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
790                 return -EOPNOTSUPP;
791         }
792
793         rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
794                                           IPA_CMD_ASS_START, 0);
795         if (rc) {
796                 dev_warn(&card->gdev->dev,
797                         "Starting VLAN support for %s failed\n",
798                         QETH_CARD_IFNAME(card));
799         } else {
800                 dev_info(&card->gdev->dev, "VLAN enabled\n");
801         }
802         return rc;
803 }
804
805 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
806 {
807         int rc;
808
809         QETH_CARD_TEXT(card, 3, "stmcast");
810
811         if (!qeth_is_supported(card, IPA_MULTICASTING)) {
812                 dev_info(&card->gdev->dev,
813                         "Multicast not supported on %s\n",
814                         QETH_CARD_IFNAME(card));
815                 return -EOPNOTSUPP;
816         }
817
818         rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
819                                           IPA_CMD_ASS_START, 0);
820         if (rc) {
821                 dev_warn(&card->gdev->dev,
822                         "Starting multicast support for %s failed\n",
823                         QETH_CARD_IFNAME(card));
824         } else {
825                 dev_info(&card->gdev->dev, "Multicast enabled\n");
826                 card->dev->flags |= IFF_MULTICAST;
827         }
828         return rc;
829 }
830
831 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
832 {
833         int rc;
834
835         QETH_CARD_TEXT(card, 3, "softipv6");
836
837         if (card->info.type == QETH_CARD_TYPE_IQD)
838                 goto out;
839
840         rc = qeth_send_simple_setassparms(card, IPA_IPV6,
841                                           IPA_CMD_ASS_START, 3);
842         if (rc) {
843                 dev_err(&card->gdev->dev,
844                         "Activating IPv6 support for %s failed\n",
845                         QETH_CARD_IFNAME(card));
846                 return rc;
847         }
848         rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6,
849                                              IPA_CMD_ASS_START, 0);
850         if (rc) {
851                 dev_err(&card->gdev->dev,
852                         "Activating IPv6 support for %s failed\n",
853                          QETH_CARD_IFNAME(card));
854                 return rc;
855         }
856         rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU,
857                                              IPA_CMD_ASS_START, 0);
858         if (rc) {
859                 dev_warn(&card->gdev->dev,
860                         "Enabling the passthrough mode for %s failed\n",
861                         QETH_CARD_IFNAME(card));
862                 return rc;
863         }
864 out:
865         dev_info(&card->gdev->dev, "IPV6 enabled\n");
866         return 0;
867 }
868
869 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
870 {
871         QETH_CARD_TEXT(card, 3, "strtipv6");
872
873         if (!qeth_is_supported(card, IPA_IPV6)) {
874                 dev_info(&card->gdev->dev,
875                         "IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
876                 return 0;
877         }
878         return qeth_l3_softsetup_ipv6(card);
879 }
880
881 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
882 {
883         int rc;
884
885         QETH_CARD_TEXT(card, 3, "stbrdcst");
886         card->info.broadcast_capable = 0;
887         if (!qeth_is_supported(card, IPA_FILTERING)) {
888                 dev_info(&card->gdev->dev,
889                         "Broadcast not supported on %s\n",
890                         QETH_CARD_IFNAME(card));
891                 rc = -EOPNOTSUPP;
892                 goto out;
893         }
894         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
895                                           IPA_CMD_ASS_START, 0);
896         if (rc) {
897                 dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
898                         "%s failed\n", QETH_CARD_IFNAME(card));
899                 goto out;
900         }
901
902         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
903                                           IPA_CMD_ASS_CONFIGURE, 1);
904         if (rc) {
905                 dev_warn(&card->gdev->dev,
906                         "Setting up broadcast filtering for %s failed\n",
907                         QETH_CARD_IFNAME(card));
908                 goto out;
909         }
910         card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
911         dev_info(&card->gdev->dev, "Broadcast enabled\n");
912         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
913                                           IPA_CMD_ASS_ENABLE, 1);
914         if (rc) {
915                 dev_warn(&card->gdev->dev, "Setting up broadcast echo "
916                         "filtering for %s failed\n", QETH_CARD_IFNAME(card));
917                 goto out;
918         }
919         card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
920 out:
921         if (card->info.broadcast_capable)
922                 card->dev->flags |= IFF_BROADCAST;
923         else
924                 card->dev->flags &= ~IFF_BROADCAST;
925         return rc;
926 }
927
928 static int qeth_l3_start_ipassists(struct qeth_card *card)
929 {
930         QETH_CARD_TEXT(card, 3, "strtipas");
931
932         if (qeth_set_access_ctrl_online(card, 0))
933                 return -EIO;
934         qeth_l3_start_ipa_arp_processing(card); /* go on*/
935         qeth_l3_start_ipa_source_mac(card);     /* go on*/
936         qeth_l3_start_ipa_vlan(card);           /* go on*/
937         qeth_l3_start_ipa_multicast(card);              /* go on*/
938         qeth_l3_start_ipa_ipv6(card);           /* go on*/
939         qeth_l3_start_ipa_broadcast(card);              /* go on*/
940         return 0;
941 }
942
943 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
944                 struct qeth_reply *reply, unsigned long data)
945 {
946         struct qeth_ipa_cmd *cmd;
947
948         cmd = (struct qeth_ipa_cmd *) data;
949         if (cmd->hdr.return_code == 0)
950                 ether_addr_copy(card->dev->dev_addr,
951                                 cmd->data.create_destroy_addr.unique_id);
952         else
953                 eth_random_addr(card->dev->dev_addr);
954
955         return 0;
956 }
957
958 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
959 {
960         int rc = 0;
961         struct qeth_cmd_buffer *iob;
962         struct qeth_ipa_cmd *cmd;
963
964         QETH_DBF_TEXT(SETUP, 2, "hsrmac");
965
966         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
967                                      QETH_PROT_IPV6);
968         if (!iob)
969                 return -ENOMEM;
970         cmd = __ipa_cmd(iob);
971         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
972                         card->info.unique_id;
973
974         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
975                                 NULL);
976         return rc;
977 }
978
979 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
980                 struct qeth_reply *reply, unsigned long data)
981 {
982         struct qeth_ipa_cmd *cmd;
983
984         cmd = (struct qeth_ipa_cmd *) data;
985         if (cmd->hdr.return_code == 0)
986                 card->info.unique_id = *((__u16 *)
987                                 &cmd->data.create_destroy_addr.unique_id[6]);
988         else {
989                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
990                                         UNIQUE_ID_NOT_BY_CARD;
991                 dev_warn(&card->gdev->dev, "The network adapter failed to "
992                         "generate a unique ID\n");
993         }
994         return 0;
995 }
996
997 static int qeth_l3_get_unique_id(struct qeth_card *card)
998 {
999         int rc = 0;
1000         struct qeth_cmd_buffer *iob;
1001         struct qeth_ipa_cmd *cmd;
1002
1003         QETH_DBF_TEXT(SETUP, 2, "guniqeid");
1004
1005         if (!qeth_is_supported(card, IPA_IPV6)) {
1006                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1007                                         UNIQUE_ID_NOT_BY_CARD;
1008                 return 0;
1009         }
1010
1011         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1012                                      QETH_PROT_IPV6);
1013         if (!iob)
1014                 return -ENOMEM;
1015         cmd = __ipa_cmd(iob);
1016         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1017                         card->info.unique_id;
1018
1019         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
1020         return rc;
1021 }
1022
1023 static int
1024 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
1025                             unsigned long data)
1026 {
1027         struct qeth_ipa_cmd        *cmd;
1028         __u16 rc;
1029
1030         QETH_DBF_TEXT(SETUP, 2, "diastrcb");
1031
1032         cmd = (struct qeth_ipa_cmd *)data;
1033         rc = cmd->hdr.return_code;
1034         if (rc)
1035                 QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1036         switch (cmd->data.diagass.action) {
1037         case QETH_DIAGS_CMD_TRACE_QUERY:
1038                 break;
1039         case QETH_DIAGS_CMD_TRACE_DISABLE:
1040                 switch (rc) {
1041                 case 0:
1042                 case IPA_RC_INVALID_SUBCMD:
1043                         card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1044                         dev_info(&card->gdev->dev, "The HiperSockets network "
1045                                 "traffic analyzer is deactivated\n");
1046                         break;
1047                 default:
1048                         break;
1049                 }
1050                 break;
1051         case QETH_DIAGS_CMD_TRACE_ENABLE:
1052                 switch (rc) {
1053                 case 0:
1054                         card->info.promisc_mode = SET_PROMISC_MODE_ON;
1055                         dev_info(&card->gdev->dev, "The HiperSockets network "
1056                                 "traffic analyzer is activated\n");
1057                         break;
1058                 case IPA_RC_HARDWARE_AUTH_ERROR:
1059                         dev_warn(&card->gdev->dev, "The device is not "
1060                                 "authorized to run as a HiperSockets network "
1061                                 "traffic analyzer\n");
1062                         break;
1063                 case IPA_RC_TRACE_ALREADY_ACTIVE:
1064                         dev_warn(&card->gdev->dev, "A HiperSockets "
1065                                 "network traffic analyzer is already "
1066                                 "active in the HiperSockets LAN\n");
1067                         break;
1068                 default:
1069                         break;
1070                 }
1071                 break;
1072         default:
1073                 QETH_DBF_MESSAGE(2, "Unknown sniffer action (0x%04x) on %s\n",
1074                         cmd->data.diagass.action, QETH_CARD_IFNAME(card));
1075         }
1076
1077         return 0;
1078 }
1079
1080 static int
1081 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1082 {
1083         struct qeth_cmd_buffer *iob;
1084         struct qeth_ipa_cmd    *cmd;
1085
1086         QETH_DBF_TEXT(SETUP, 2, "diagtrac");
1087
1088         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
1089         if (!iob)
1090                 return -ENOMEM;
1091         cmd = __ipa_cmd(iob);
1092         cmd->data.diagass.subcmd_len = 16;
1093         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRACE;
1094         cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1095         cmd->data.diagass.action = diags_cmd;
1096         return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1097 }
1098
1099 static void
1100 qeth_l3_add_mc_to_hash(struct qeth_card *card, struct in_device *in4_dev)
1101 {
1102         struct ip_mc_list *im4;
1103         struct qeth_ipaddr *tmp, *ipm;
1104
1105         QETH_CARD_TEXT(card, 4, "addmc");
1106
1107         tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1108         if (!tmp)
1109                 return;
1110
1111         for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
1112              im4 = rcu_dereference(im4->next_rcu)) {
1113                 ip_eth_mc_map(im4->multiaddr, tmp->mac);
1114                 tmp->u.a4.addr = be32_to_cpu(im4->multiaddr);
1115                 tmp->is_multicast = 1;
1116
1117                 ipm = qeth_l3_find_addr_by_ip(card, tmp);
1118                 if (ipm) {
1119                         /* for mcast, by-IP match means full match */
1120                         ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1121                 } else {
1122                         ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1123                         if (!ipm)
1124                                 continue;
1125                         ether_addr_copy(ipm->mac, tmp->mac);
1126                         ipm->u.a4.addr = be32_to_cpu(im4->multiaddr);
1127                         ipm->is_multicast = 1;
1128                         ipm->disp_flag = QETH_DISP_ADDR_ADD;
1129                         hash_add(card->ip_mc_htable,
1130                                         &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1131                 }
1132         }
1133
1134         kfree(tmp);
1135 }
1136
1137 /* called with rcu_read_lock */
1138 static void qeth_l3_add_vlan_mc(struct qeth_card *card)
1139 {
1140         struct in_device *in_dev;
1141         u16 vid;
1142
1143         QETH_CARD_TEXT(card, 4, "addmcvl");
1144
1145         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1146                 return;
1147
1148         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1149                 struct net_device *netdev;
1150
1151                 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1152                                               vid);
1153                 if (netdev == NULL ||
1154                     !(netdev->flags & IFF_UP))
1155                         continue;
1156                 in_dev = __in_dev_get_rcu(netdev);
1157                 if (!in_dev)
1158                         continue;
1159                 qeth_l3_add_mc_to_hash(card, in_dev);
1160         }
1161 }
1162
1163 static void qeth_l3_add_multicast_ipv4(struct qeth_card *card)
1164 {
1165         struct in_device *in4_dev;
1166
1167         QETH_CARD_TEXT(card, 4, "chkmcv4");
1168
1169         rcu_read_lock();
1170         in4_dev = __in_dev_get_rcu(card->dev);
1171         if (in4_dev == NULL)
1172                 goto unlock;
1173         qeth_l3_add_mc_to_hash(card, in4_dev);
1174         qeth_l3_add_vlan_mc(card);
1175 unlock:
1176         rcu_read_unlock();
1177 }
1178
1179 static void qeth_l3_add_mc6_to_hash(struct qeth_card *card,
1180                                     struct inet6_dev *in6_dev)
1181 {
1182         struct qeth_ipaddr *ipm;
1183         struct ifmcaddr6 *im6;
1184         struct qeth_ipaddr *tmp;
1185
1186         QETH_CARD_TEXT(card, 4, "addmc6");
1187
1188         tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1189         if (!tmp)
1190                 return;
1191
1192         for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1193                 ipv6_eth_mc_map(&im6->mca_addr, tmp->mac);
1194                 memcpy(&tmp->u.a6.addr, &im6->mca_addr.s6_addr,
1195                        sizeof(struct in6_addr));
1196                 tmp->is_multicast = 1;
1197
1198                 ipm = qeth_l3_find_addr_by_ip(card, tmp);
1199                 if (ipm) {
1200                         /* for mcast, by-IP match means full match */
1201                         ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1202                         continue;
1203                 }
1204
1205                 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1206                 if (!ipm)
1207                         continue;
1208
1209                 ether_addr_copy(ipm->mac, tmp->mac);
1210                 memcpy(&ipm->u.a6.addr, &im6->mca_addr.s6_addr,
1211                        sizeof(struct in6_addr));
1212                 ipm->is_multicast = 1;
1213                 ipm->disp_flag = QETH_DISP_ADDR_ADD;
1214                 hash_add(card->ip_mc_htable,
1215                                 &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1216
1217         }
1218         kfree(tmp);
1219 }
1220
1221 /* called with rcu_read_lock */
1222 static void qeth_l3_add_vlan_mc6(struct qeth_card *card)
1223 {
1224         struct inet6_dev *in_dev;
1225         u16 vid;
1226
1227         QETH_CARD_TEXT(card, 4, "admc6vl");
1228
1229         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1230                 return;
1231
1232         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1233                 struct net_device *netdev;
1234
1235                 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1236                                               vid);
1237                 if (netdev == NULL ||
1238                     !(netdev->flags & IFF_UP))
1239                         continue;
1240                 in_dev = in6_dev_get(netdev);
1241                 if (!in_dev)
1242                         continue;
1243                 read_lock_bh(&in_dev->lock);
1244                 qeth_l3_add_mc6_to_hash(card, in_dev);
1245                 read_unlock_bh(&in_dev->lock);
1246                 in6_dev_put(in_dev);
1247         }
1248 }
1249
1250 static void qeth_l3_add_multicast_ipv6(struct qeth_card *card)
1251 {
1252         struct inet6_dev *in6_dev;
1253
1254         QETH_CARD_TEXT(card, 4, "chkmcv6");
1255
1256         if (!qeth_is_supported(card, IPA_IPV6))
1257                 return ;
1258         in6_dev = in6_dev_get(card->dev);
1259         if (!in6_dev)
1260                 return;
1261
1262         rcu_read_lock();
1263         read_lock_bh(&in6_dev->lock);
1264         qeth_l3_add_mc6_to_hash(card, in6_dev);
1265         qeth_l3_add_vlan_mc6(card);
1266         read_unlock_bh(&in6_dev->lock);
1267         rcu_read_unlock();
1268         in6_dev_put(in6_dev);
1269 }
1270
1271 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1272                                    __be16 proto, u16 vid)
1273 {
1274         struct qeth_card *card = dev->ml_priv;
1275
1276         set_bit(vid, card->active_vlans);
1277         return 0;
1278 }
1279
1280 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1281                                     __be16 proto, u16 vid)
1282 {
1283         struct qeth_card *card = dev->ml_priv;
1284
1285         QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1286
1287         if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
1288                 QETH_CARD_TEXT(card, 3, "kidREC");
1289                 return 0;
1290         }
1291         clear_bit(vid, card->active_vlans);
1292         qeth_l3_set_rx_mode(dev);
1293         return 0;
1294 }
1295
1296 static void qeth_l3_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
1297                                 struct qeth_hdr *hdr)
1298 {
1299         if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1300                 u16 prot = (hdr->hdr.l3.flags & QETH_HDR_IPV6) ? ETH_P_IPV6 :
1301                                                                  ETH_P_IP;
1302                 unsigned char tg_addr[ETH_ALEN];
1303
1304                 skb_reset_network_header(skb);
1305                 switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
1306                 case QETH_CAST_MULTICAST:
1307                         if (prot == ETH_P_IP)
1308                                 ip_eth_mc_map(ip_hdr(skb)->daddr, tg_addr);
1309                         else
1310                                 ipv6_eth_mc_map(&ipv6_hdr(skb)->daddr, tg_addr);
1311
1312                         card->stats.multicast++;
1313                         break;
1314                 case QETH_CAST_BROADCAST:
1315                         ether_addr_copy(tg_addr, card->dev->broadcast);
1316                         card->stats.multicast++;
1317                         break;
1318                 default:
1319                         if (card->options.sniffer)
1320                                 skb->pkt_type = PACKET_OTHERHOST;
1321                         ether_addr_copy(tg_addr, card->dev->dev_addr);
1322                 }
1323
1324                 if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
1325                         card->dev->header_ops->create(skb, card->dev, prot,
1326                                 tg_addr, &hdr->hdr.l3.next_hop.rx.src_mac,
1327                                 skb->len);
1328                 else
1329                         card->dev->header_ops->create(skb, card->dev, prot,
1330                                 tg_addr, "FAKELL", skb->len);
1331         }
1332
1333         skb->protocol = eth_type_trans(skb, card->dev);
1334
1335         /* copy VLAN tag from hdr into skb */
1336         if (!card->options.sniffer &&
1337             (hdr->hdr.l3.ext_flags & (QETH_HDR_EXT_VLAN_FRAME |
1338                                       QETH_HDR_EXT_INCLUDE_VLAN_TAG))) {
1339                 u16 tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
1340                                 hdr->hdr.l3.vlan_id :
1341                                 hdr->hdr.l3.next_hop.rx.vlan_id;
1342                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
1343         }
1344
1345         qeth_rx_csum(card, skb, hdr->hdr.l3.ext_flags);
1346 }
1347
1348 static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
1349                                 int budget, int *done)
1350 {
1351         struct net_device *dev = card->dev;
1352         int work_done = 0;
1353         struct sk_buff *skb;
1354         struct qeth_hdr *hdr;
1355         unsigned int len;
1356         __u16 magic;
1357
1358         *done = 0;
1359         WARN_ON_ONCE(!budget);
1360         while (budget) {
1361                 skb = qeth_core_get_next_skb(card,
1362                         &card->qdio.in_q->bufs[card->rx.b_index],
1363                         &card->rx.b_element, &card->rx.e_offset, &hdr);
1364                 if (!skb) {
1365                         *done = 1;
1366                         break;
1367                 }
1368                 switch (hdr->hdr.l3.id) {
1369                 case QETH_HEADER_TYPE_LAYER3:
1370                         magic = *(__u16 *)skb->data;
1371                         if ((card->info.type == QETH_CARD_TYPE_IQD) &&
1372                             (magic == ETH_P_AF_IUCV)) {
1373                                 len = skb->len;
1374                                 dev_hard_header(skb, dev, ETH_P_AF_IUCV,
1375                                                 dev->dev_addr, "FAKELL", len);
1376                                 skb->protocol = eth_type_trans(skb, dev);
1377                                 netif_receive_skb(skb);
1378                         } else {
1379                                 qeth_l3_rebuild_skb(card, skb, hdr);
1380                                 len = skb->len;
1381                                 napi_gro_receive(&card->napi, skb);
1382                         }
1383                         break;
1384                 case QETH_HEADER_TYPE_LAYER2: /* for HiperSockets sniffer */
1385                         skb->protocol = eth_type_trans(skb, skb->dev);
1386                         len = skb->len;
1387                         netif_receive_skb(skb);
1388                         break;
1389                 default:
1390                         dev_kfree_skb_any(skb);
1391                         QETH_CARD_TEXT(card, 3, "inbunkno");
1392                         QETH_DBF_HEX(CTRL, 3, hdr, sizeof(*hdr));
1393                         continue;
1394                 }
1395                 work_done++;
1396                 budget--;
1397                 card->stats.rx_packets++;
1398                 card->stats.rx_bytes += len;
1399         }
1400         return work_done;
1401 }
1402
1403 static void qeth_l3_stop_card(struct qeth_card *card, int recovery_mode)
1404 {
1405         QETH_DBF_TEXT(SETUP, 2, "stopcard");
1406         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1407
1408         qeth_set_allowed_threads(card, 0, 1);
1409         if (card->options.sniffer &&
1410             (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1411                 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1412         if (card->read.state == CH_STATE_UP &&
1413             card->write.state == CH_STATE_UP &&
1414             (card->state == CARD_STATE_UP)) {
1415                 if (recovery_mode)
1416                         qeth_l3_stop(card->dev);
1417                 else {
1418                         rtnl_lock();
1419                         dev_close(card->dev);
1420                         rtnl_unlock();
1421                 }
1422                 card->state = CARD_STATE_SOFTSETUP;
1423         }
1424         if (card->state == CARD_STATE_SOFTSETUP) {
1425                 qeth_l3_clear_ip_htable(card, 1);
1426                 qeth_clear_ipacmd_list(card);
1427                 card->state = CARD_STATE_HARDSETUP;
1428         }
1429         if (card->state == CARD_STATE_HARDSETUP) {
1430                 qeth_qdio_clear_card(card, 0);
1431                 qeth_clear_qdio_buffers(card);
1432                 qeth_clear_working_pool_list(card);
1433                 card->state = CARD_STATE_DOWN;
1434         }
1435         if (card->state == CARD_STATE_DOWN) {
1436                 qeth_clear_cmd_buffers(&card->read);
1437                 qeth_clear_cmd_buffers(&card->write);
1438         }
1439 }
1440
1441 /*
1442  * test for and Switch promiscuous mode (on or off)
1443  *  either for guestlan or HiperSocket Sniffer
1444  */
1445 static void
1446 qeth_l3_handle_promisc_mode(struct qeth_card *card)
1447 {
1448         struct net_device *dev = card->dev;
1449
1450         if (((dev->flags & IFF_PROMISC) &&
1451              (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
1452             (!(dev->flags & IFF_PROMISC) &&
1453              (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
1454                 return;
1455
1456         if (card->info.guestlan) {              /* Guestlan trace */
1457                 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1458                         qeth_setadp_promisc_mode(card);
1459         } else if (card->options.sniffer &&     /* HiperSockets trace */
1460                    qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1461                 if (dev->flags & IFF_PROMISC) {
1462                         QETH_CARD_TEXT(card, 3, "+promisc");
1463                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1464                 } else {
1465                         QETH_CARD_TEXT(card, 3, "-promisc");
1466                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1467                 }
1468         }
1469 }
1470
1471 static void qeth_l3_set_rx_mode(struct net_device *dev)
1472 {
1473         struct qeth_card *card = dev->ml_priv;
1474         struct qeth_ipaddr *addr;
1475         struct hlist_node *tmp;
1476         int i, rc;
1477
1478         QETH_CARD_TEXT(card, 3, "setmulti");
1479         if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
1480             (card->state != CARD_STATE_UP))
1481                 return;
1482         if (!card->options.sniffer) {
1483                 spin_lock_bh(&card->mclock);
1484
1485                 qeth_l3_add_multicast_ipv4(card);
1486                 qeth_l3_add_multicast_ipv6(card);
1487
1488                 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1489                         switch (addr->disp_flag) {
1490                         case QETH_DISP_ADDR_DELETE:
1491                                 rc = qeth_l3_deregister_addr_entry(card, addr);
1492                                 if (!rc || rc == IPA_RC_MC_ADDR_NOT_FOUND) {
1493                                         hash_del(&addr->hnode);
1494                                         kfree(addr);
1495                                 }
1496                                 break;
1497                         case QETH_DISP_ADDR_ADD:
1498                                 rc = qeth_l3_register_addr_entry(card, addr);
1499                                 if (rc && rc != IPA_RC_LAN_OFFLINE) {
1500                                         hash_del(&addr->hnode);
1501                                         kfree(addr);
1502                                         break;
1503                                 }
1504                                 addr->ref_counter = 1;
1505                                 /* fall through */
1506                         default:
1507                                 /* for next call to set_rx_mode(): */
1508                                 addr->disp_flag = QETH_DISP_ADDR_DELETE;
1509                         }
1510                 }
1511
1512                 spin_unlock_bh(&card->mclock);
1513
1514                 if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1515                         return;
1516         }
1517         qeth_l3_handle_promisc_mode(card);
1518 }
1519
1520 static const char *qeth_l3_arp_get_error_cause(int *rc)
1521 {
1522         switch (*rc) {
1523         case QETH_IPA_ARP_RC_FAILED:
1524                 *rc = -EIO;
1525                 return "operation failed";
1526         case QETH_IPA_ARP_RC_NOTSUPP:
1527                 *rc = -EOPNOTSUPP;
1528                 return "operation not supported";
1529         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1530                 *rc = -EINVAL;
1531                 return "argument out of range";
1532         case QETH_IPA_ARP_RC_Q_NOTSUPP:
1533                 *rc = -EOPNOTSUPP;
1534                 return "query operation not supported";
1535         case QETH_IPA_ARP_RC_Q_NO_DATA:
1536                 *rc = -ENOENT;
1537                 return "no query data available";
1538         default:
1539                 return "unknown error";
1540         }
1541 }
1542
1543 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1544 {
1545         int tmp;
1546         int rc;
1547
1548         QETH_CARD_TEXT(card, 3, "arpstnoe");
1549
1550         /*
1551          * currently GuestLAN only supports the ARP assist function
1552          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1553          * thus we say EOPNOTSUPP for this ARP function
1554          */
1555         if (card->info.guestlan)
1556                 return -EOPNOTSUPP;
1557         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1558                 return -EOPNOTSUPP;
1559         }
1560         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1561                                           IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1562                                           no_entries);
1563         if (rc) {
1564                 tmp = rc;
1565                 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on "
1566                         "%s: %s (0x%x/%d)\n", QETH_CARD_IFNAME(card),
1567                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1568         }
1569         return rc;
1570 }
1571
1572 static __u32 get_arp_entry_size(struct qeth_card *card,
1573                         struct qeth_arp_query_data *qdata,
1574                         struct qeth_arp_entrytype *type, __u8 strip_entries)
1575 {
1576         __u32 rc;
1577         __u8 is_hsi;
1578
1579         is_hsi = qdata->reply_bits == 5;
1580         if (type->ip == QETHARP_IP_ADDR_V4) {
1581                 QETH_CARD_TEXT(card, 4, "arpev4");
1582                 if (strip_entries) {
1583                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1584                                 sizeof(struct qeth_arp_qi_entry7_short);
1585                 } else {
1586                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1587                                 sizeof(struct qeth_arp_qi_entry7);
1588                 }
1589         } else if (type->ip == QETHARP_IP_ADDR_V6) {
1590                 QETH_CARD_TEXT(card, 4, "arpev6");
1591                 if (strip_entries) {
1592                         rc = is_hsi ?
1593                                 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1594                                 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1595                 } else {
1596                         rc = is_hsi ?
1597                                 sizeof(struct qeth_arp_qi_entry5_ipv6) :
1598                                 sizeof(struct qeth_arp_qi_entry7_ipv6);
1599                 }
1600         } else {
1601                 QETH_CARD_TEXT(card, 4, "arpinv");
1602                 rc = 0;
1603         }
1604
1605         return rc;
1606 }
1607
1608 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1609 {
1610         return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1611                 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1612 }
1613
1614 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1615                 struct qeth_reply *reply, unsigned long data)
1616 {
1617         struct qeth_ipa_cmd *cmd;
1618         struct qeth_arp_query_data *qdata;
1619         struct qeth_arp_query_info *qinfo;
1620         int i;
1621         int e;
1622         int entrybytes_done;
1623         int stripped_bytes;
1624         __u8 do_strip_entries;
1625
1626         QETH_CARD_TEXT(card, 3, "arpquecb");
1627
1628         qinfo = (struct qeth_arp_query_info *) reply->param;
1629         cmd = (struct qeth_ipa_cmd *) data;
1630         QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1631         if (cmd->hdr.return_code) {
1632                 QETH_CARD_TEXT(card, 4, "arpcberr");
1633                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1634                 return 0;
1635         }
1636         if (cmd->data.setassparms.hdr.return_code) {
1637                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1638                 QETH_CARD_TEXT(card, 4, "setaperr");
1639                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1640                 return 0;
1641         }
1642         qdata = &cmd->data.setassparms.data.query_arp;
1643         QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1644
1645         do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1646         stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1647         entrybytes_done = 0;
1648         for (e = 0; e < qdata->no_entries; ++e) {
1649                 char *cur_entry;
1650                 __u32 esize;
1651                 struct qeth_arp_entrytype *etype;
1652
1653                 cur_entry = &qdata->data + entrybytes_done;
1654                 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1655                 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1656                         QETH_CARD_TEXT(card, 4, "pmis");
1657                         QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1658                         break;
1659                 }
1660                 esize = get_arp_entry_size(card, qdata, etype,
1661                         do_strip_entries);
1662                 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1663                 if (!esize)
1664                         break;
1665
1666                 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1667                         QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOMEM);
1668                         cmd->hdr.return_code = IPA_RC_ENOMEM;
1669                         goto out_error;
1670                 }
1671
1672                 memcpy(qinfo->udata + qinfo->udata_offset,
1673                         &qdata->data + entrybytes_done + stripped_bytes,
1674                         esize);
1675                 entrybytes_done += esize + stripped_bytes;
1676                 qinfo->udata_offset += esize;
1677                 ++qinfo->no_entries;
1678         }
1679         /* check if all replies received ... */
1680         if (cmd->data.setassparms.hdr.seq_no <
1681             cmd->data.setassparms.hdr.number_of_replies)
1682                 return 1;
1683         QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1684         memcpy(qinfo->udata, &qinfo->no_entries, 4);
1685         /* keep STRIP_ENTRIES flag so the user program can distinguish
1686          * stripped entries from normal ones */
1687         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1688                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1689         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1690         QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1691         return 0;
1692 out_error:
1693         i = 0;
1694         memcpy(qinfo->udata, &i, 4);
1695         return 0;
1696 }
1697
1698 static int qeth_l3_send_ipa_arp_cmd(struct qeth_card *card,
1699                 struct qeth_cmd_buffer *iob, int len,
1700                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1701                         unsigned long),
1702                 void *reply_param)
1703 {
1704         QETH_CARD_TEXT(card, 4, "sendarp");
1705
1706         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1707         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1708                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1709         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
1710                                       reply_cb, reply_param);
1711 }
1712
1713 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1714         enum qeth_prot_versions prot,
1715         struct qeth_arp_query_info *qinfo)
1716 {
1717         struct qeth_cmd_buffer *iob;
1718         struct qeth_ipa_cmd *cmd;
1719         int tmp;
1720         int rc;
1721
1722         QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1723
1724         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1725                                        IPA_CMD_ASS_ARP_QUERY_INFO,
1726                                        sizeof(struct qeth_arp_query_data)
1727                                                 - sizeof(char),
1728                                        prot);
1729         if (!iob)
1730                 return -ENOMEM;
1731         cmd = __ipa_cmd(iob);
1732         cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1733         cmd->data.setassparms.data.query_arp.reply_bits = 0;
1734         cmd->data.setassparms.data.query_arp.no_entries = 0;
1735         rc = qeth_l3_send_ipa_arp_cmd(card, iob,
1736                            QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
1737                            qeth_l3_arp_query_cb, (void *)qinfo);
1738         if (rc) {
1739                 tmp = rc;
1740                 QETH_DBF_MESSAGE(2,
1741                         "Error while querying ARP cache on %s: %s "
1742                         "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
1743                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1744         }
1745
1746         return rc;
1747 }
1748
1749 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
1750 {
1751         struct qeth_arp_query_info qinfo = {0, };
1752         int rc;
1753
1754         QETH_CARD_TEXT(card, 3, "arpquery");
1755
1756         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
1757                                IPA_ARP_PROCESSING)) {
1758                 QETH_CARD_TEXT(card, 3, "arpqnsup");
1759                 rc = -EOPNOTSUPP;
1760                 goto out;
1761         }
1762         /* get size of userspace buffer and mask_bits -> 6 bytes */
1763         if (copy_from_user(&qinfo, udata, 6)) {
1764                 rc = -EFAULT;
1765                 goto out;
1766         }
1767         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1768         if (!qinfo.udata) {
1769                 rc = -ENOMEM;
1770                 goto out;
1771         }
1772         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1773         rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
1774         if (rc) {
1775                 if (copy_to_user(udata, qinfo.udata, 4))
1776                         rc = -EFAULT;
1777                 goto free_and_out;
1778         }
1779         if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
1780                 /* fails in case of GuestLAN QDIO mode */
1781                 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
1782         }
1783         if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
1784                 QETH_CARD_TEXT(card, 4, "qactf");
1785                 rc = -EFAULT;
1786                 goto free_and_out;
1787         }
1788         QETH_CARD_TEXT(card, 4, "qacts");
1789
1790 free_and_out:
1791         kfree(qinfo.udata);
1792 out:
1793         return rc;
1794 }
1795
1796 static int qeth_l3_arp_add_entry(struct qeth_card *card,
1797                                 struct qeth_arp_cache_entry *entry)
1798 {
1799         struct qeth_cmd_buffer *iob;
1800         char buf[16];
1801         int tmp;
1802         int rc;
1803
1804         QETH_CARD_TEXT(card, 3, "arpadent");
1805
1806         /*
1807          * currently GuestLAN only supports the ARP assist function
1808          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
1809          * thus we say EOPNOTSUPP for this ARP function
1810          */
1811         if (card->info.guestlan)
1812                 return -EOPNOTSUPP;
1813         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1814                 return -EOPNOTSUPP;
1815         }
1816
1817         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1818                                        IPA_CMD_ASS_ARP_ADD_ENTRY,
1819                                        sizeof(struct qeth_arp_cache_entry),
1820                                        QETH_PROT_IPV4);
1821         if (!iob)
1822                 return -ENOMEM;
1823         rc = qeth_send_setassparms(card, iob,
1824                                    sizeof(struct qeth_arp_cache_entry),
1825                                    (unsigned long) entry,
1826                                    qeth_setassparms_cb, NULL);
1827         if (rc) {
1828                 tmp = rc;
1829                 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
1830                 QETH_DBF_MESSAGE(2, "Could not add ARP entry for address %s "
1831                         "on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
1832                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1833         }
1834         return rc;
1835 }
1836
1837 static int qeth_l3_arp_remove_entry(struct qeth_card *card,
1838                                 struct qeth_arp_cache_entry *entry)
1839 {
1840         struct qeth_cmd_buffer *iob;
1841         char buf[16] = {0, };
1842         int tmp;
1843         int rc;
1844
1845         QETH_CARD_TEXT(card, 3, "arprment");
1846
1847         /*
1848          * currently GuestLAN only supports the ARP assist function
1849          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
1850          * thus we say EOPNOTSUPP for this ARP function
1851          */
1852         if (card->info.guestlan)
1853                 return -EOPNOTSUPP;
1854         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1855                 return -EOPNOTSUPP;
1856         }
1857         memcpy(buf, entry, 12);
1858         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1859                                        IPA_CMD_ASS_ARP_REMOVE_ENTRY,
1860                                        12,
1861                                        QETH_PROT_IPV4);
1862         if (!iob)
1863                 return -ENOMEM;
1864         rc = qeth_send_setassparms(card, iob,
1865                                    12, (unsigned long)buf,
1866                                    qeth_setassparms_cb, NULL);
1867         if (rc) {
1868                 tmp = rc;
1869                 memset(buf, 0, 16);
1870                 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
1871                 QETH_DBF_MESSAGE(2, "Could not delete ARP entry for address %s"
1872                         " on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
1873                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1874         }
1875         return rc;
1876 }
1877
1878 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
1879 {
1880         int rc;
1881         int tmp;
1882
1883         QETH_CARD_TEXT(card, 3, "arpflush");
1884
1885         /*
1886          * currently GuestLAN only supports the ARP assist function
1887          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
1888          * thus we say EOPNOTSUPP for this ARP function
1889         */
1890         if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
1891                 return -EOPNOTSUPP;
1892         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1893                 return -EOPNOTSUPP;
1894         }
1895         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1896                                           IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
1897         if (rc) {
1898                 tmp = rc;
1899                 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on %s: %s "
1900                         "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
1901                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1902         }
1903         return rc;
1904 }
1905
1906 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1907 {
1908         struct qeth_card *card = dev->ml_priv;
1909         struct qeth_arp_cache_entry arp_entry;
1910         int rc = 0;
1911
1912         switch (cmd) {
1913         case SIOC_QETH_ARP_SET_NO_ENTRIES:
1914                 if (!capable(CAP_NET_ADMIN)) {
1915                         rc = -EPERM;
1916                         break;
1917                 }
1918                 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
1919                 break;
1920         case SIOC_QETH_ARP_QUERY_INFO:
1921                 if (!capable(CAP_NET_ADMIN)) {
1922                         rc = -EPERM;
1923                         break;
1924                 }
1925                 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
1926                 break;
1927         case SIOC_QETH_ARP_ADD_ENTRY:
1928                 if (!capable(CAP_NET_ADMIN)) {
1929                         rc = -EPERM;
1930                         break;
1931                 }
1932                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
1933                                    sizeof(struct qeth_arp_cache_entry)))
1934                         rc = -EFAULT;
1935                 else
1936                         rc = qeth_l3_arp_add_entry(card, &arp_entry);
1937                 break;
1938         case SIOC_QETH_ARP_REMOVE_ENTRY:
1939                 if (!capable(CAP_NET_ADMIN)) {
1940                         rc = -EPERM;
1941                         break;
1942                 }
1943                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
1944                                    sizeof(struct qeth_arp_cache_entry)))
1945                         rc = -EFAULT;
1946                 else
1947                         rc = qeth_l3_arp_remove_entry(card, &arp_entry);
1948                 break;
1949         case SIOC_QETH_ARP_FLUSH_CACHE:
1950                 if (!capable(CAP_NET_ADMIN)) {
1951                         rc = -EPERM;
1952                         break;
1953                 }
1954                 rc = qeth_l3_arp_flush_cache(card);
1955                 break;
1956         default:
1957                 rc = -EOPNOTSUPP;
1958         }
1959         return rc;
1960 }
1961
1962 static int qeth_l3_get_cast_type(struct sk_buff *skb)
1963 {
1964         struct neighbour *n = NULL;
1965         struct dst_entry *dst;
1966
1967         rcu_read_lock();
1968         dst = skb_dst(skb);
1969         if (dst)
1970                 n = dst_neigh_lookup_skb(dst, skb);
1971         if (n) {
1972                 int cast_type = n->type;
1973
1974                 rcu_read_unlock();
1975                 neigh_release(n);
1976                 if ((cast_type == RTN_BROADCAST) ||
1977                     (cast_type == RTN_MULTICAST) ||
1978                     (cast_type == RTN_ANYCAST))
1979                         return cast_type;
1980                 return RTN_UNICAST;
1981         }
1982         rcu_read_unlock();
1983
1984         /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1985         switch (qeth_get_ip_version(skb)) {
1986         case 4:
1987                 return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1988                                 RTN_MULTICAST : RTN_UNICAST;
1989         case 6:
1990                 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
1991                                 RTN_MULTICAST : RTN_UNICAST;
1992         default:
1993                 /* ... and MAC address */
1994                 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
1995                                             skb->dev->broadcast))
1996                         return RTN_BROADCAST;
1997                 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1998                         return RTN_MULTICAST;
1999                 /* default to unicast */
2000                 return RTN_UNICAST;
2001         }
2002 }
2003
2004 static void qeth_l3_fill_af_iucv_hdr(struct qeth_hdr *hdr, struct sk_buff *skb,
2005                                      unsigned int data_len)
2006 {
2007         char daddr[16];
2008
2009         hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2010         hdr->hdr.l3.length = data_len;
2011         hdr->hdr.l3.flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
2012
2013         memset(daddr, 0, sizeof(daddr));
2014         daddr[0] = 0xfe;
2015         daddr[1] = 0x80;
2016         memcpy(&daddr[8], iucv_trans_hdr(skb)->destUserID, 8);
2017         memcpy(hdr->hdr.l3.next_hop.ipv6_addr, daddr, 16);
2018 }
2019
2020 static u8 qeth_l3_cast_type_to_flag(int cast_type)
2021 {
2022         if (cast_type == RTN_MULTICAST)
2023                 return QETH_CAST_MULTICAST;
2024         if (cast_type == RTN_ANYCAST)
2025                 return QETH_CAST_ANYCAST;
2026         if (cast_type == RTN_BROADCAST)
2027                 return QETH_CAST_BROADCAST;
2028         return QETH_CAST_UNICAST;
2029 }
2030
2031 static void qeth_l3_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
2032                                 struct sk_buff *skb, int ipv, int cast_type,
2033                                 unsigned int data_len)
2034 {
2035         struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
2036
2037         hdr->hdr.l3.length = data_len;
2038
2039         if (skb_is_gso(skb)) {
2040                 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
2041         } else {
2042                 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2043                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2044                         qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
2045                         /* some HW requires combined L3+L4 csum offload: */
2046                         if (ipv == 4)
2047                                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
2048                         if (card->options.performance_stats)
2049                                 card->perf_stats.tx_csum++;
2050                 }
2051         }
2052
2053         if (ipv == 4 || IS_IQD(card)) {
2054                 /* NETIF_F_HW_VLAN_CTAG_TX */
2055                 if (skb_vlan_tag_present(skb)) {
2056                         hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
2057                         hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
2058                 }
2059         } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
2060                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
2061                 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
2062         }
2063
2064         /* OSA only: */
2065         if (!ipv) {
2066                 hdr->hdr.l3.flags = QETH_HDR_PASSTHRU;
2067                 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
2068                                             skb->dev->broadcast))
2069                         hdr->hdr.l3.flags |= QETH_CAST_BROADCAST;
2070                 else
2071                         hdr->hdr.l3.flags |= (cast_type == RTN_MULTICAST) ?
2072                                 QETH_CAST_MULTICAST : QETH_CAST_UNICAST;
2073                 return;
2074         }
2075
2076         hdr->hdr.l3.flags = qeth_l3_cast_type_to_flag(cast_type);
2077         rcu_read_lock();
2078         if (ipv == 4) {
2079                 struct rtable *rt = skb_rtable(skb);
2080
2081                 *((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
2082                                 rt_nexthop(rt, ip_hdr(skb)->daddr) :
2083                                 ip_hdr(skb)->daddr;
2084         } else {
2085                 /* IPv6 */
2086                 const struct rt6_info *rt = skb_rt6_info(skb);
2087                 const struct in6_addr *next_hop;
2088
2089                 if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
2090                         next_hop = &rt->rt6i_gateway;
2091                 else
2092                         next_hop = &ipv6_hdr(skb)->daddr;
2093                 memcpy(hdr->hdr.l3.next_hop.ipv6_addr, next_hop, 16);
2094
2095                 hdr->hdr.l3.flags |= QETH_HDR_IPV6;
2096                 if (card->info.type != QETH_CARD_TYPE_IQD)
2097                         hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
2098         }
2099         rcu_read_unlock();
2100 }
2101
2102 static void qeth_l3_fixup_headers(struct sk_buff *skb)
2103 {
2104         struct iphdr *iph = ip_hdr(skb);
2105
2106         /* this is safe, IPv6 traffic takes a different path */
2107         if (skb->ip_summed == CHECKSUM_PARTIAL)
2108                 iph->check = 0;
2109         if (skb_is_gso(skb)) {
2110                 iph->tot_len = 0;
2111                 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
2112                                                     iph->daddr, 0);
2113         }
2114 }
2115
2116 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
2117                         struct qeth_qdio_out_q *queue, int ipv, int cast_type)
2118 {
2119         unsigned int hw_hdr_len, proto_len, frame_len, elements;
2120         unsigned char eth_hdr[ETH_HLEN];
2121         bool is_tso = skb_is_gso(skb);
2122         unsigned int data_offset = 0;
2123         struct qeth_hdr *hdr = NULL;
2124         unsigned int hd_len = 0;
2125         int push_len, rc;
2126         bool is_sg;
2127
2128         if (is_tso) {
2129                 hw_hdr_len = sizeof(struct qeth_hdr_tso);
2130                 proto_len = skb_transport_offset(skb) + tcp_hdrlen(skb) -
2131                             ETH_HLEN;
2132         } else {
2133                 hw_hdr_len = sizeof(struct qeth_hdr);
2134                 proto_len = 0;
2135         }
2136
2137         /* re-use the L2 header area for the HW header: */
2138         rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
2139         if (rc)
2140                 return rc;
2141         skb_copy_from_linear_data(skb, eth_hdr, ETH_HLEN);
2142         skb_pull(skb, ETH_HLEN);
2143         frame_len = skb->len;
2144
2145         qeth_l3_fixup_headers(skb);
2146         push_len = qeth_add_hw_header(card, skb, &hdr, hw_hdr_len, proto_len,
2147                                       &elements);
2148         if (push_len < 0)
2149                 return push_len;
2150         if (is_tso || !push_len) {
2151                 /* HW header needs its own buffer element. */
2152                 hd_len = hw_hdr_len + proto_len;
2153                 data_offset = push_len + proto_len;
2154         }
2155         memset(hdr, 0, hw_hdr_len);
2156
2157         if (skb->protocol == htons(ETH_P_AF_IUCV)) {
2158                 qeth_l3_fill_af_iucv_hdr(hdr, skb, frame_len);
2159         } else {
2160                 qeth_l3_fill_header(card, hdr, skb, ipv, cast_type, frame_len);
2161                 if (is_tso)
2162                         qeth_fill_tso_ext((struct qeth_hdr_tso *) hdr,
2163                                           frame_len - proto_len, skb,
2164                                           proto_len);
2165         }
2166
2167         is_sg = skb_is_nonlinear(skb);
2168         if (IS_IQD(card)) {
2169                 rc = qeth_do_send_packet_fast(queue, skb, hdr, data_offset,
2170                                               hd_len);
2171         } else {
2172                 /* TODO: drop skb_orphan() once TX completion is fast enough */
2173                 skb_orphan(skb);
2174                 rc = qeth_do_send_packet(card, queue, skb, hdr, data_offset,
2175                                          hd_len, elements);
2176         }
2177
2178         if (!rc) {
2179                 if (card->options.performance_stats) {
2180                         card->perf_stats.buf_elements_sent += elements;
2181                         if (is_sg)
2182                                 card->perf_stats.sg_skbs_sent++;
2183                         if (is_tso) {
2184                                 card->perf_stats.large_send_bytes += frame_len;
2185                                 card->perf_stats.large_send_cnt++;
2186                         }
2187                 }
2188         } else {
2189                 if (!push_len)
2190                         kmem_cache_free(qeth_core_header_cache, hdr);
2191                 if (rc == -EBUSY) {
2192                         /* roll back to ETH header */
2193                         skb_pull(skb, push_len);
2194                         skb_push(skb, ETH_HLEN);
2195                         skb_copy_to_linear_data(skb, eth_hdr, ETH_HLEN);
2196                 }
2197         }
2198         return rc;
2199 }
2200
2201 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
2202                                            struct net_device *dev)
2203 {
2204         int cast_type = qeth_l3_get_cast_type(skb);
2205         struct qeth_card *card = dev->ml_priv;
2206         int ipv = qeth_get_ip_version(skb);
2207         struct qeth_qdio_out_q *queue;
2208         int tx_bytes = skb->len;
2209         int rc;
2210
2211         if (IS_IQD(card)) {
2212                 if (card->options.sniffer)
2213                         goto tx_drop;
2214                 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
2215                     (card->options.cq == QETH_CQ_ENABLED &&
2216                      skb->protocol != htons(ETH_P_AF_IUCV)))
2217                         goto tx_drop;
2218         }
2219
2220         if (card->state != CARD_STATE_UP) {
2221                 card->stats.tx_carrier_errors++;
2222                 goto tx_drop;
2223         }
2224
2225         if (cast_type == RTN_BROADCAST && !card->info.broadcast_capable)
2226                 goto tx_drop;
2227
2228         queue = qeth_get_tx_queue(card, skb, ipv, cast_type);
2229
2230         if (card->options.performance_stats) {
2231                 card->perf_stats.outbound_cnt++;
2232                 card->perf_stats.outbound_start_time = qeth_get_micros();
2233         }
2234         netif_stop_queue(dev);
2235
2236         if (ipv == 4 || IS_IQD(card))
2237                 rc = qeth_l3_xmit(card, skb, queue, ipv, cast_type);
2238         else
2239                 rc = qeth_xmit(card, skb, queue, ipv, cast_type,
2240                                qeth_l3_fill_header);
2241
2242         if (!rc) {
2243                 card->stats.tx_packets++;
2244                 card->stats.tx_bytes += tx_bytes;
2245                 if (card->options.performance_stats)
2246                         card->perf_stats.outbound_time += qeth_get_micros() -
2247                                 card->perf_stats.outbound_start_time;
2248                 netif_wake_queue(dev);
2249                 return NETDEV_TX_OK;
2250         } else if (rc == -EBUSY) {
2251                 return NETDEV_TX_BUSY;
2252         } /* else fall through */
2253
2254 tx_drop:
2255         card->stats.tx_dropped++;
2256         card->stats.tx_errors++;
2257         dev_kfree_skb_any(skb);
2258         netif_wake_queue(dev);
2259         return NETDEV_TX_OK;
2260 }
2261
2262 static int __qeth_l3_open(struct net_device *dev)
2263 {
2264         struct qeth_card *card = dev->ml_priv;
2265         int rc = 0;
2266
2267         QETH_CARD_TEXT(card, 4, "qethopen");
2268         if (card->state == CARD_STATE_UP)
2269                 return rc;
2270         if (card->state != CARD_STATE_SOFTSETUP)
2271                 return -ENODEV;
2272         card->data.state = CH_STATE_UP;
2273         card->state = CARD_STATE_UP;
2274         netif_start_queue(dev);
2275
2276         if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
2277                 napi_enable(&card->napi);
2278                 local_bh_disable();
2279                 napi_schedule(&card->napi);
2280                 /* kick-start the NAPI softirq: */
2281                 local_bh_enable();
2282         } else
2283                 rc = -EIO;
2284         return rc;
2285 }
2286
2287 static int qeth_l3_open(struct net_device *dev)
2288 {
2289         struct qeth_card *card = dev->ml_priv;
2290
2291         QETH_CARD_TEXT(card, 5, "qethope_");
2292         if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
2293                 QETH_CARD_TEXT(card, 3, "openREC");
2294                 return -ERESTARTSYS;
2295         }
2296         return __qeth_l3_open(dev);
2297 }
2298
2299 static int qeth_l3_stop(struct net_device *dev)
2300 {
2301         struct qeth_card *card = dev->ml_priv;
2302
2303         QETH_CARD_TEXT(card, 4, "qethstop");
2304         netif_tx_disable(dev);
2305         if (card->state == CARD_STATE_UP) {
2306                 card->state = CARD_STATE_SOFTSETUP;
2307                 napi_disable(&card->napi);
2308         }
2309         return 0;
2310 }
2311
2312 static const struct ethtool_ops qeth_l3_ethtool_ops = {
2313         .get_link = ethtool_op_get_link,
2314         .get_strings = qeth_core_get_strings,
2315         .get_ethtool_stats = qeth_core_get_ethtool_stats,
2316         .get_sset_count = qeth_core_get_sset_count,
2317         .get_drvinfo = qeth_core_get_drvinfo,
2318         .get_link_ksettings = qeth_core_ethtool_get_link_ksettings,
2319 };
2320
2321 /*
2322  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
2323  * NOARP on the netdevice is no option because it also turns off neighbor
2324  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
2325  * arp resolution but we want the hard header (packet socket will work
2326  * e.g. tcpdump)
2327  */
2328 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
2329 {
2330         n->nud_state = NUD_NOARP;
2331         memcpy(n->ha, "FAKELL", 6);
2332         n->output = n->ops->connected_output;
2333         return 0;
2334 }
2335
2336 static int
2337 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
2338 {
2339         if (np->tbl->family == AF_INET)
2340                 np->neigh_setup = qeth_l3_neigh_setup_noarp;
2341
2342         return 0;
2343 }
2344
2345 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
2346                                                     struct net_device *dev,
2347                                                     netdev_features_t features)
2348 {
2349         if (qeth_get_ip_version(skb) != 4)
2350                 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2351         return qeth_features_check(skb, dev, features);
2352 }
2353
2354 static const struct net_device_ops qeth_l3_netdev_ops = {
2355         .ndo_open               = qeth_l3_open,
2356         .ndo_stop               = qeth_l3_stop,
2357         .ndo_get_stats          = qeth_get_stats,
2358         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2359         .ndo_validate_addr      = eth_validate_addr,
2360         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2361         .ndo_do_ioctl           = qeth_do_ioctl,
2362         .ndo_fix_features       = qeth_fix_features,
2363         .ndo_set_features       = qeth_set_features,
2364         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2365         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2366         .ndo_tx_timeout         = qeth_tx_timeout,
2367 };
2368
2369 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2370         .ndo_open               = qeth_l3_open,
2371         .ndo_stop               = qeth_l3_stop,
2372         .ndo_get_stats          = qeth_get_stats,
2373         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2374         .ndo_features_check     = qeth_l3_osa_features_check,
2375         .ndo_validate_addr      = eth_validate_addr,
2376         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2377         .ndo_do_ioctl           = qeth_do_ioctl,
2378         .ndo_fix_features       = qeth_fix_features,
2379         .ndo_set_features       = qeth_set_features,
2380         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2381         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2382         .ndo_tx_timeout         = qeth_tx_timeout,
2383         .ndo_neigh_setup        = qeth_l3_neigh_setup,
2384 };
2385
2386 static int qeth_l3_setup_netdev(struct qeth_card *card)
2387 {
2388         unsigned int headroom;
2389         int rc;
2390
2391         if (card->dev->netdev_ops)
2392                 return 0;
2393
2394         if (card->info.type == QETH_CARD_TYPE_OSD ||
2395             card->info.type == QETH_CARD_TYPE_OSX) {
2396                 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
2397                     (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2398                         pr_info("qeth_l3: ignoring TR device\n");
2399                         return -ENODEV;
2400                 }
2401
2402                 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
2403
2404                 /*IPv6 address autoconfiguration stuff*/
2405                 qeth_l3_get_unique_id(card);
2406                 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
2407                         card->dev->dev_id = card->info.unique_id & 0xffff;
2408
2409                 if (!card->info.guestlan) {
2410                         card->dev->features |= NETIF_F_SG;
2411                         card->dev->hw_features |= NETIF_F_TSO |
2412                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2413                         card->dev->vlan_features |= NETIF_F_TSO |
2414                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2415                 }
2416
2417                 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
2418                         card->dev->hw_features |= NETIF_F_IPV6_CSUM;
2419                         card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
2420                 }
2421                 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
2422                         card->dev->hw_features |= NETIF_F_TSO6;
2423                         card->dev->vlan_features |= NETIF_F_TSO6;
2424                 }
2425
2426                 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
2427                 if (card->dev->hw_features & NETIF_F_TSO6)
2428                         headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
2429                 else if (card->dev->hw_features & NETIF_F_TSO)
2430                         headroom = sizeof(struct qeth_hdr_tso);
2431                 else
2432                         headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
2433         } else if (card->info.type == QETH_CARD_TYPE_IQD) {
2434                 card->dev->flags |= IFF_NOARP;
2435                 card->dev->netdev_ops = &qeth_l3_netdev_ops;
2436                 headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
2437
2438                 rc = qeth_l3_iqd_read_initial_mac(card);
2439                 if (rc)
2440                         goto out;
2441
2442                 if (card->options.hsuid[0])
2443                         memcpy(card->dev->perm_addr, card->options.hsuid, 9);
2444         } else
2445                 return -ENODEV;
2446
2447         card->dev->needed_headroom = headroom;
2448         card->dev->ethtool_ops = &qeth_l3_ethtool_ops;
2449         card->dev->features |=  NETIF_F_HW_VLAN_CTAG_TX |
2450                                 NETIF_F_HW_VLAN_CTAG_RX |
2451                                 NETIF_F_HW_VLAN_CTAG_FILTER;
2452
2453         netif_keep_dst(card->dev);
2454         if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
2455                 netif_set_gso_max_size(card->dev,
2456                                        PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
2457
2458         netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2459         rc = register_netdev(card->dev);
2460 out:
2461         if (rc)
2462                 card->dev->netdev_ops = NULL;
2463         return rc;
2464 }
2465
2466 static const struct device_type qeth_l3_devtype = {
2467         .name = "qeth_layer3",
2468         .groups = qeth_l3_attr_groups,
2469 };
2470
2471 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
2472 {
2473         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2474         int rc;
2475
2476         if (gdev->dev.type == &qeth_generic_devtype) {
2477                 rc = qeth_l3_create_device_attributes(&gdev->dev);
2478                 if (rc)
2479                         return rc;
2480         }
2481         hash_init(card->ip_htable);
2482         hash_init(card->ip_mc_htable);
2483         card->info.hwtrap = 0;
2484         return 0;
2485 }
2486
2487 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2488 {
2489         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2490
2491         if (cgdev->dev.type == &qeth_generic_devtype)
2492                 qeth_l3_remove_device_attributes(&cgdev->dev);
2493
2494         qeth_set_allowed_threads(card, 0, 1);
2495         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2496
2497         if (cgdev->state == CCWGROUP_ONLINE)
2498                 qeth_l3_set_offline(cgdev);
2499
2500         unregister_netdev(card->dev);
2501         qeth_l3_clear_ip_htable(card, 0);
2502         qeth_l3_clear_ipato_list(card);
2503 }
2504
2505 static int __qeth_l3_set_online(struct ccwgroup_device *gdev, int recovery_mode)
2506 {
2507         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2508         int rc = 0;
2509         enum qeth_card_states recover_flag;
2510
2511         mutex_lock(&card->discipline_mutex);
2512         mutex_lock(&card->conf_mutex);
2513         QETH_DBF_TEXT(SETUP, 2, "setonlin");
2514         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
2515
2516         recover_flag = card->state;
2517         rc = qeth_core_hardsetup_card(card);
2518         if (rc) {
2519                 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2520                 rc = -ENODEV;
2521                 goto out_remove;
2522         }
2523
2524         rc = qeth_l3_setup_netdev(card);
2525         if (rc)
2526                 goto out_remove;
2527
2528         if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
2529                 if (card->info.hwtrap &&
2530                     qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
2531                         card->info.hwtrap = 0;
2532         } else
2533                 card->info.hwtrap = 0;
2534
2535         card->state = CARD_STATE_HARDSETUP;
2536         qeth_print_status_message(card);
2537
2538         /* softsetup */
2539         QETH_DBF_TEXT(SETUP, 2, "softsetp");
2540
2541         rc = qeth_l3_setadapter_parms(card);
2542         if (rc)
2543                 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2544         if (!card->options.sniffer) {
2545                 rc = qeth_l3_start_ipassists(card);
2546                 if (rc) {
2547                         QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2548                         goto out_remove;
2549                 }
2550                 rc = qeth_l3_setrouting_v4(card);
2551                 if (rc)
2552                         QETH_DBF_TEXT_(SETUP, 2, "4err%04x", rc);
2553                 rc = qeth_l3_setrouting_v6(card);
2554                 if (rc)
2555                         QETH_DBF_TEXT_(SETUP, 2, "5err%04x", rc);
2556         }
2557         netif_tx_disable(card->dev);
2558
2559         rc = qeth_init_qdio_queues(card);
2560         if (rc) {
2561                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2562                 rc = -ENODEV;
2563                 goto out_remove;
2564         }
2565         card->state = CARD_STATE_SOFTSETUP;
2566
2567         qeth_set_allowed_threads(card, 0xffffffff, 0);
2568         qeth_l3_recover_ip(card);
2569
2570         qeth_enable_hw_features(card->dev);
2571         if (recover_flag == CARD_STATE_RECOVER) {
2572                 rtnl_lock();
2573                 if (recovery_mode) {
2574                         __qeth_l3_open(card->dev);
2575                         qeth_l3_set_rx_mode(card->dev);
2576                 } else {
2577                         dev_open(card->dev);
2578                 }
2579                 rtnl_unlock();
2580         }
2581         qeth_trace_features(card);
2582         /* let user_space know that device is online */
2583         kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2584         mutex_unlock(&card->conf_mutex);
2585         mutex_unlock(&card->discipline_mutex);
2586         return 0;
2587 out_remove:
2588         qeth_l3_stop_card(card, 0);
2589         ccw_device_set_offline(CARD_DDEV(card));
2590         ccw_device_set_offline(CARD_WDEV(card));
2591         ccw_device_set_offline(CARD_RDEV(card));
2592         qdio_free(CARD_DDEV(card));
2593         if (recover_flag == CARD_STATE_RECOVER)
2594                 card->state = CARD_STATE_RECOVER;
2595         else
2596                 card->state = CARD_STATE_DOWN;
2597         mutex_unlock(&card->conf_mutex);
2598         mutex_unlock(&card->discipline_mutex);
2599         return rc;
2600 }
2601
2602 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
2603 {
2604         return __qeth_l3_set_online(gdev, 0);
2605 }
2606
2607 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
2608                         int recovery_mode)
2609 {
2610         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2611         int rc = 0, rc2 = 0, rc3 = 0;
2612         enum qeth_card_states recover_flag;
2613
2614         mutex_lock(&card->discipline_mutex);
2615         mutex_lock(&card->conf_mutex);
2616         QETH_DBF_TEXT(SETUP, 3, "setoffl");
2617         QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
2618
2619         netif_carrier_off(card->dev);
2620         recover_flag = card->state;
2621         if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
2622                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
2623                 card->info.hwtrap = 1;
2624         }
2625         qeth_l3_stop_card(card, recovery_mode);
2626         if ((card->options.cq == QETH_CQ_ENABLED) && card->dev) {
2627                 rtnl_lock();
2628                 call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
2629                 rtnl_unlock();
2630         }
2631         rc  = ccw_device_set_offline(CARD_DDEV(card));
2632         rc2 = ccw_device_set_offline(CARD_WDEV(card));
2633         rc3 = ccw_device_set_offline(CARD_RDEV(card));
2634         if (!rc)
2635                 rc = (rc2) ? rc2 : rc3;
2636         if (rc)
2637                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2638         qdio_free(CARD_DDEV(card));
2639         if (recover_flag == CARD_STATE_UP)
2640                 card->state = CARD_STATE_RECOVER;
2641         /* let user_space know that device is offline */
2642         kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
2643         mutex_unlock(&card->conf_mutex);
2644         mutex_unlock(&card->discipline_mutex);
2645         return 0;
2646 }
2647
2648 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
2649 {
2650         return __qeth_l3_set_offline(cgdev, 0);
2651 }
2652
2653 static int qeth_l3_recover(void *ptr)
2654 {
2655         struct qeth_card *card;
2656         int rc = 0;
2657
2658         card = (struct qeth_card *) ptr;
2659         QETH_CARD_TEXT(card, 2, "recover1");
2660         QETH_CARD_HEX(card, 2, &card, sizeof(void *));
2661         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
2662                 return 0;
2663         QETH_CARD_TEXT(card, 2, "recover2");
2664         dev_warn(&card->gdev->dev,
2665                 "A recovery process has been started for the device\n");
2666         qeth_set_recovery_task(card);
2667         __qeth_l3_set_offline(card->gdev, 1);
2668         rc = __qeth_l3_set_online(card->gdev, 1);
2669         if (!rc)
2670                 dev_info(&card->gdev->dev,
2671                         "Device successfully recovered!\n");
2672         else {
2673                 qeth_close_dev(card);
2674                 dev_warn(&card->gdev->dev, "The qeth device driver "
2675                                 "failed to recover an error on the device\n");
2676         }
2677         qeth_clear_recovery_task(card);
2678         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
2679         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
2680         return 0;
2681 }
2682
2683 static int qeth_l3_pm_suspend(struct ccwgroup_device *gdev)
2684 {
2685         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2686
2687         netif_device_detach(card->dev);
2688         qeth_set_allowed_threads(card, 0, 1);
2689         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2690         if (gdev->state == CCWGROUP_OFFLINE)
2691                 return 0;
2692         if (card->state == CARD_STATE_UP) {
2693                 if (card->info.hwtrap)
2694                         qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
2695                 __qeth_l3_set_offline(card->gdev, 1);
2696         } else
2697                 __qeth_l3_set_offline(card->gdev, 0);
2698         return 0;
2699 }
2700
2701 static int qeth_l3_pm_resume(struct ccwgroup_device *gdev)
2702 {
2703         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2704         int rc = 0;
2705
2706         if (card->state == CARD_STATE_RECOVER) {
2707                 rc = __qeth_l3_set_online(card->gdev, 1);
2708                 if (rc) {
2709                         rtnl_lock();
2710                         dev_close(card->dev);
2711                         rtnl_unlock();
2712                 }
2713         } else
2714                 rc = __qeth_l3_set_online(card->gdev, 0);
2715
2716         qeth_set_allowed_threads(card, 0xffffffff, 0);
2717         netif_device_attach(card->dev);
2718         if (rc)
2719                 dev_warn(&card->gdev->dev, "The qeth device driver "
2720                         "failed to recover an error on the device\n");
2721         return rc;
2722 }
2723
2724 /* Returns zero if the command is successfully "consumed" */
2725 static int qeth_l3_control_event(struct qeth_card *card,
2726                                         struct qeth_ipa_cmd *cmd)
2727 {
2728         return 1;
2729 }
2730
2731 struct qeth_discipline qeth_l3_discipline = {
2732         .devtype = &qeth_l3_devtype,
2733         .process_rx_buffer = qeth_l3_process_inbound_buffer,
2734         .recover = qeth_l3_recover,
2735         .setup = qeth_l3_probe_device,
2736         .remove = qeth_l3_remove_device,
2737         .set_online = qeth_l3_set_online,
2738         .set_offline = qeth_l3_set_offline,
2739         .freeze = qeth_l3_pm_suspend,
2740         .thaw = qeth_l3_pm_resume,
2741         .restore = qeth_l3_pm_resume,
2742         .do_ioctl = qeth_l3_do_ioctl,
2743         .control_event_handler = qeth_l3_control_event,
2744 };
2745 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2746
2747 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2748                                    struct qeth_ipaddr *addr,
2749                                    unsigned long event)
2750 {
2751         switch (event) {
2752         case NETDEV_UP:
2753                 spin_lock_bh(&card->ip_lock);
2754                 qeth_l3_add_ip(card, addr);
2755                 spin_unlock_bh(&card->ip_lock);
2756                 return NOTIFY_OK;
2757         case NETDEV_DOWN:
2758                 spin_lock_bh(&card->ip_lock);
2759                 qeth_l3_delete_ip(card, addr);
2760                 spin_unlock_bh(&card->ip_lock);
2761                 return NOTIFY_OK;
2762         default:
2763                 return NOTIFY_DONE;
2764         }
2765 }
2766
2767 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2768 {
2769         if (is_vlan_dev(dev))
2770                 dev = vlan_dev_real_dev(dev);
2771         if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2772             dev->netdev_ops == &qeth_l3_netdev_ops)
2773                 return (struct qeth_card *) dev->ml_priv;
2774         return NULL;
2775 }
2776
2777 static int qeth_l3_ip_event(struct notifier_block *this,
2778                             unsigned long event, void *ptr)
2779 {
2780
2781         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2782         struct net_device *dev = ifa->ifa_dev->dev;
2783         struct qeth_ipaddr addr;
2784         struct qeth_card *card;
2785
2786         if (dev_net(dev) != &init_net)
2787                 return NOTIFY_DONE;
2788
2789         card = qeth_l3_get_card_from_dev(dev);
2790         if (!card)
2791                 return NOTIFY_DONE;
2792         QETH_CARD_TEXT(card, 3, "ipevent");
2793
2794         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2795         addr.u.a4.addr = be32_to_cpu(ifa->ifa_address);
2796         addr.u.a4.mask = be32_to_cpu(ifa->ifa_mask);
2797
2798         return qeth_l3_handle_ip_event(card, &addr, event);
2799 }
2800
2801 static struct notifier_block qeth_l3_ip_notifier = {
2802         qeth_l3_ip_event,
2803         NULL,
2804 };
2805
2806 static int qeth_l3_ip6_event(struct notifier_block *this,
2807                              unsigned long event, void *ptr)
2808 {
2809         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2810         struct net_device *dev = ifa->idev->dev;
2811         struct qeth_ipaddr addr;
2812         struct qeth_card *card;
2813
2814         card = qeth_l3_get_card_from_dev(dev);
2815         if (!card)
2816                 return NOTIFY_DONE;
2817         QETH_CARD_TEXT(card, 3, "ip6event");
2818         if (!qeth_is_supported(card, IPA_IPV6))
2819                 return NOTIFY_DONE;
2820
2821         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
2822         addr.u.a6.addr = ifa->addr;
2823         addr.u.a6.pfxlen = ifa->prefix_len;
2824
2825         return qeth_l3_handle_ip_event(card, &addr, event);
2826 }
2827
2828 static struct notifier_block qeth_l3_ip6_notifier = {
2829         qeth_l3_ip6_event,
2830         NULL,
2831 };
2832
2833 static int qeth_l3_register_notifiers(void)
2834 {
2835         int rc;
2836
2837         QETH_DBF_TEXT(SETUP, 5, "regnotif");
2838         rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2839         if (rc)
2840                 return rc;
2841         rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2842         if (rc) {
2843                 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2844                 return rc;
2845         }
2846         return 0;
2847 }
2848
2849 static void qeth_l3_unregister_notifiers(void)
2850 {
2851         QETH_DBF_TEXT(SETUP, 5, "unregnot");
2852         WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2853         WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2854 }
2855
2856 static int __init qeth_l3_init(void)
2857 {
2858         pr_info("register layer 3 discipline\n");
2859         return qeth_l3_register_notifiers();
2860 }
2861
2862 static void __exit qeth_l3_exit(void)
2863 {
2864         qeth_l3_unregister_notifiers();
2865         pr_info("unregister layer 3 discipline\n");
2866 }
2867
2868 module_init(qeth_l3_init);
2869 module_exit(qeth_l3_exit);
2870 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2871 MODULE_DESCRIPTION("qeth layer 3 discipline");
2872 MODULE_LICENSE("GPL");