Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[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         spin_lock_bh(&card->ip_lock);
282
283         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
284                 if (!recover) {
285                         hash_del(&addr->hnode);
286                         kfree(addr);
287                         continue;
288                 }
289                 addr->disp_flag = QETH_DISP_ADDR_ADD;
290         }
291
292         spin_unlock_bh(&card->ip_lock);
293
294         spin_lock_bh(&card->mclock);
295
296         hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
297                 hash_del(&addr->hnode);
298                 kfree(addr);
299         }
300
301         spin_unlock_bh(&card->mclock);
302
303
304 }
305 static void qeth_l3_recover_ip(struct qeth_card *card)
306 {
307         struct qeth_ipaddr *addr;
308         struct hlist_node *tmp;
309         int i;
310         int rc;
311
312         QETH_CARD_TEXT(card, 4, "recovrip");
313
314         spin_lock_bh(&card->ip_lock);
315
316         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
317                 if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
318                         if (addr->proto == QETH_PROT_IPV4) {
319                                 addr->in_progress = 1;
320                                 spin_unlock_bh(&card->ip_lock);
321                                 rc = qeth_l3_register_addr_entry(card, addr);
322                                 spin_lock_bh(&card->ip_lock);
323                                 addr->in_progress = 0;
324                         } else
325                                 rc = qeth_l3_register_addr_entry(card, addr);
326
327                         if (!rc) {
328                                 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
329                                 if (addr->ref_counter < 1)
330                                         qeth_l3_delete_ip(card, addr);
331                         } else {
332                                 hash_del(&addr->hnode);
333                                 kfree(addr);
334                         }
335                 }
336         }
337
338         spin_unlock_bh(&card->ip_lock);
339
340 }
341
342 static int qeth_l3_send_setdelmc(struct qeth_card *card,
343                         struct qeth_ipaddr *addr, int ipacmd)
344 {
345         int rc;
346         struct qeth_cmd_buffer *iob;
347         struct qeth_ipa_cmd *cmd;
348
349         QETH_CARD_TEXT(card, 4, "setdelmc");
350
351         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
352         if (!iob)
353                 return -ENOMEM;
354         cmd = __ipa_cmd(iob);
355         ether_addr_copy(cmd->data.setdelipm.mac, addr->mac);
356         if (addr->proto == QETH_PROT_IPV6)
357                 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
358                        sizeof(struct in6_addr));
359         else
360                 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
361
362         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
363
364         return rc;
365 }
366
367 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
368 {
369         int i, j;
370         for (i = 0; i < 16; i++) {
371                 j = (len) - (i * 8);
372                 if (j >= 8)
373                         netmask[i] = 0xff;
374                 else if (j > 0)
375                         netmask[i] = (u8)(0xFF00 >> j);
376                 else
377                         netmask[i] = 0;
378         }
379 }
380
381 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
382 {
383         switch (addr->type) {
384         case QETH_IP_TYPE_RXIP:
385                 return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
386         case QETH_IP_TYPE_VIPA:
387                 return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
388                                QETH_IPA_DELIP_VIPA_FLAG;
389         default:
390                 return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
391         }
392 }
393
394 static int qeth_l3_send_setdelip(struct qeth_card *card,
395                                  struct qeth_ipaddr *addr,
396                                  enum qeth_ipa_cmds ipacmd)
397 {
398         struct qeth_cmd_buffer *iob;
399         struct qeth_ipa_cmd *cmd;
400         __u8 netmask[16];
401         u32 flags;
402
403         QETH_CARD_TEXT(card, 4, "setdelip");
404
405         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
406         if (!iob)
407                 return -ENOMEM;
408         cmd = __ipa_cmd(iob);
409
410         flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
411         QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
412
413         if (addr->proto == QETH_PROT_IPV6) {
414                 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
415                        sizeof(struct in6_addr));
416                 qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
417                 memcpy(cmd->data.setdelip6.mask, netmask,
418                        sizeof(struct in6_addr));
419                 cmd->data.setdelip6.flags = flags;
420         } else {
421                 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
422                 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
423                 cmd->data.setdelip4.flags = flags;
424         }
425
426         return qeth_send_ipa_cmd(card, iob, NULL, NULL);
427 }
428
429 static int qeth_l3_send_setrouting(struct qeth_card *card,
430         enum qeth_routing_types type, enum qeth_prot_versions prot)
431 {
432         int rc;
433         struct qeth_ipa_cmd *cmd;
434         struct qeth_cmd_buffer *iob;
435
436         QETH_CARD_TEXT(card, 4, "setroutg");
437         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
438         if (!iob)
439                 return -ENOMEM;
440         cmd = __ipa_cmd(iob);
441         cmd->data.setrtg.type = (type);
442         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
443
444         return rc;
445 }
446
447 static int qeth_l3_correct_routing_type(struct qeth_card *card,
448                 enum qeth_routing_types *type, enum qeth_prot_versions prot)
449 {
450         if (card->info.type == QETH_CARD_TYPE_IQD) {
451                 switch (*type) {
452                 case NO_ROUTER:
453                 case PRIMARY_CONNECTOR:
454                 case SECONDARY_CONNECTOR:
455                 case MULTICAST_ROUTER:
456                         return 0;
457                 default:
458                         goto out_inval;
459                 }
460         } else {
461                 switch (*type) {
462                 case NO_ROUTER:
463                 case PRIMARY_ROUTER:
464                 case SECONDARY_ROUTER:
465                         return 0;
466                 case MULTICAST_ROUTER:
467                         if (qeth_is_ipafunc_supported(card, prot,
468                                                       IPA_OSA_MC_ROUTER))
469                                 return 0;
470                 default:
471                         goto out_inval;
472                 }
473         }
474 out_inval:
475         *type = NO_ROUTER;
476         return -EINVAL;
477 }
478
479 int qeth_l3_setrouting_v4(struct qeth_card *card)
480 {
481         int rc;
482
483         QETH_CARD_TEXT(card, 3, "setrtg4");
484
485         rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
486                                   QETH_PROT_IPV4);
487         if (rc)
488                 return rc;
489
490         rc = qeth_l3_send_setrouting(card, card->options.route4.type,
491                                   QETH_PROT_IPV4);
492         if (rc) {
493                 card->options.route4.type = NO_ROUTER;
494                 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
495                                  rc, CARD_DEVID(card));
496         }
497         return rc;
498 }
499
500 int qeth_l3_setrouting_v6(struct qeth_card *card)
501 {
502         int rc = 0;
503
504         QETH_CARD_TEXT(card, 3, "setrtg6");
505
506         if (!qeth_is_supported(card, IPA_IPV6))
507                 return 0;
508         rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
509                                   QETH_PROT_IPV6);
510         if (rc)
511                 return rc;
512
513         rc = qeth_l3_send_setrouting(card, card->options.route6.type,
514                                   QETH_PROT_IPV6);
515         if (rc) {
516                 card->options.route6.type = NO_ROUTER;
517                 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
518                                  rc, CARD_DEVID(card));
519         }
520         return rc;
521 }
522
523 /*
524  * IP address takeover related functions
525  */
526
527 /**
528  * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
529  *
530  * Caller must hold ip_lock.
531  */
532 void qeth_l3_update_ipato(struct qeth_card *card)
533 {
534         struct qeth_ipaddr *addr;
535         unsigned int i;
536
537         hash_for_each(card->ip_htable, i, addr, hnode) {
538                 if (addr->type != QETH_IP_TYPE_NORMAL)
539                         continue;
540                 addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
541         }
542 }
543
544 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
545 {
546         struct qeth_ipato_entry *ipatoe, *tmp;
547
548         spin_lock_bh(&card->ip_lock);
549
550         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
551                 list_del(&ipatoe->entry);
552                 kfree(ipatoe);
553         }
554
555         qeth_l3_update_ipato(card);
556         spin_unlock_bh(&card->ip_lock);
557 }
558
559 int qeth_l3_add_ipato_entry(struct qeth_card *card,
560                                 struct qeth_ipato_entry *new)
561 {
562         struct qeth_ipato_entry *ipatoe;
563         int rc = 0;
564
565         QETH_CARD_TEXT(card, 2, "addipato");
566
567         spin_lock_bh(&card->ip_lock);
568
569         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
570                 if (ipatoe->proto != new->proto)
571                         continue;
572                 if (!memcmp(ipatoe->addr, new->addr,
573                             (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
574                     (ipatoe->mask_bits == new->mask_bits)) {
575                         rc = -EEXIST;
576                         break;
577                 }
578         }
579
580         if (!rc) {
581                 list_add_tail(&new->entry, &card->ipato.entries);
582                 qeth_l3_update_ipato(card);
583         }
584
585         spin_unlock_bh(&card->ip_lock);
586
587         return rc;
588 }
589
590 int qeth_l3_del_ipato_entry(struct qeth_card *card,
591                             enum qeth_prot_versions proto, u8 *addr,
592                             int mask_bits)
593 {
594         struct qeth_ipato_entry *ipatoe, *tmp;
595         int rc = -ENOENT;
596
597         QETH_CARD_TEXT(card, 2, "delipato");
598
599         spin_lock_bh(&card->ip_lock);
600
601         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
602                 if (ipatoe->proto != proto)
603                         continue;
604                 if (!memcmp(ipatoe->addr, addr,
605                             (proto == QETH_PROT_IPV4)? 4:16) &&
606                     (ipatoe->mask_bits == mask_bits)) {
607                         list_del(&ipatoe->entry);
608                         qeth_l3_update_ipato(card);
609                         kfree(ipatoe);
610                         rc = 0;
611                 }
612         }
613
614         spin_unlock_bh(&card->ip_lock);
615         return rc;
616 }
617
618 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
619                              enum qeth_ip_types type,
620                              enum qeth_prot_versions proto)
621 {
622         struct qeth_ipaddr addr;
623         int rc;
624
625         qeth_l3_init_ipaddr(&addr, type, proto);
626         if (proto == QETH_PROT_IPV4)
627                 memcpy(&addr.u.a4.addr, ip, 4);
628         else
629                 memcpy(&addr.u.a6.addr, ip, 16);
630
631         spin_lock_bh(&card->ip_lock);
632         rc = add ? qeth_l3_add_ip(card, &addr) : qeth_l3_delete_ip(card, &addr);
633         spin_unlock_bh(&card->ip_lock);
634         return rc;
635 }
636
637 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
638 {
639         struct qeth_ipaddr addr;
640         int rc, i;
641
642         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
643         addr.u.a6.addr.s6_addr[0] = 0xfe;
644         addr.u.a6.addr.s6_addr[1] = 0x80;
645         for (i = 0; i < 8; i++)
646                 addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
647
648         spin_lock_bh(&card->ip_lock);
649         rc = add ? qeth_l3_add_ip(card, &addr) : qeth_l3_delete_ip(card, &addr);
650         spin_unlock_bh(&card->ip_lock);
651         return rc;
652 }
653
654 static int qeth_l3_register_addr_entry(struct qeth_card *card,
655                                 struct qeth_ipaddr *addr)
656 {
657         char buf[50];
658         int rc = 0;
659         int cnt = 3;
660
661         if (card->options.sniffer)
662                 return 0;
663
664         if (addr->proto == QETH_PROT_IPV4) {
665                 QETH_CARD_TEXT(card, 2, "setaddr4");
666                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
667         } else if (addr->proto == QETH_PROT_IPV6) {
668                 QETH_CARD_TEXT(card, 2, "setaddr6");
669                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
670                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
671         } else {
672                 QETH_CARD_TEXT(card, 2, "setaddr?");
673                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
674         }
675         do {
676                 if (addr->is_multicast)
677                         rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
678                 else
679                         rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
680                 if (rc)
681                         QETH_CARD_TEXT(card, 2, "failed");
682         } while ((--cnt > 0) && rc);
683         if (rc) {
684                 QETH_CARD_TEXT(card, 2, "FAILED");
685                 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
686                 dev_warn(&card->gdev->dev,
687                         "Registering IP address %s failed\n", buf);
688         }
689         return rc;
690 }
691
692 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
693                                                 struct qeth_ipaddr *addr)
694 {
695         int rc = 0;
696
697         if (card->options.sniffer)
698                 return 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 (%#06x) on device %x\n",
1074                                  cmd->data.diagass.action, CARD_DEVID(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 int qeth_l3_arp_makerc(int rc)
1521 {
1522         switch (rc) {
1523         case IPA_RC_SUCCESS:
1524                 return 0;
1525         case QETH_IPA_ARP_RC_NOTSUPP:
1526         case QETH_IPA_ARP_RC_Q_NOTSUPP:
1527                 return -EOPNOTSUPP;
1528         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1529                 return -EINVAL;
1530         case QETH_IPA_ARP_RC_Q_NO_DATA:
1531                 return -ENOENT;
1532         default:
1533                 return -EIO;
1534         }
1535 }
1536
1537 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1538 {
1539         int rc;
1540
1541         QETH_CARD_TEXT(card, 3, "arpstnoe");
1542
1543         /*
1544          * currently GuestLAN only supports the ARP assist function
1545          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1546          * thus we say EOPNOTSUPP for this ARP function
1547          */
1548         if (card->info.guestlan)
1549                 return -EOPNOTSUPP;
1550         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1551                 return -EOPNOTSUPP;
1552         }
1553         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1554                                           IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1555                                           no_entries);
1556         if (rc)
1557                 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n",
1558                                  CARD_DEVID(card), rc);
1559         return qeth_l3_arp_makerc(rc);
1560 }
1561
1562 static __u32 get_arp_entry_size(struct qeth_card *card,
1563                         struct qeth_arp_query_data *qdata,
1564                         struct qeth_arp_entrytype *type, __u8 strip_entries)
1565 {
1566         __u32 rc;
1567         __u8 is_hsi;
1568
1569         is_hsi = qdata->reply_bits == 5;
1570         if (type->ip == QETHARP_IP_ADDR_V4) {
1571                 QETH_CARD_TEXT(card, 4, "arpev4");
1572                 if (strip_entries) {
1573                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1574                                 sizeof(struct qeth_arp_qi_entry7_short);
1575                 } else {
1576                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1577                                 sizeof(struct qeth_arp_qi_entry7);
1578                 }
1579         } else if (type->ip == QETHARP_IP_ADDR_V6) {
1580                 QETH_CARD_TEXT(card, 4, "arpev6");
1581                 if (strip_entries) {
1582                         rc = is_hsi ?
1583                                 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1584                                 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1585                 } else {
1586                         rc = is_hsi ?
1587                                 sizeof(struct qeth_arp_qi_entry5_ipv6) :
1588                                 sizeof(struct qeth_arp_qi_entry7_ipv6);
1589                 }
1590         } else {
1591                 QETH_CARD_TEXT(card, 4, "arpinv");
1592                 rc = 0;
1593         }
1594
1595         return rc;
1596 }
1597
1598 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1599 {
1600         return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1601                 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1602 }
1603
1604 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1605                 struct qeth_reply *reply, unsigned long data)
1606 {
1607         struct qeth_ipa_cmd *cmd;
1608         struct qeth_arp_query_data *qdata;
1609         struct qeth_arp_query_info *qinfo;
1610         int i;
1611         int e;
1612         int entrybytes_done;
1613         int stripped_bytes;
1614         __u8 do_strip_entries;
1615
1616         QETH_CARD_TEXT(card, 3, "arpquecb");
1617
1618         qinfo = (struct qeth_arp_query_info *) reply->param;
1619         cmd = (struct qeth_ipa_cmd *) data;
1620         QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1621         if (cmd->hdr.return_code) {
1622                 QETH_CARD_TEXT(card, 4, "arpcberr");
1623                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1624                 return 0;
1625         }
1626         if (cmd->data.setassparms.hdr.return_code) {
1627                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1628                 QETH_CARD_TEXT(card, 4, "setaperr");
1629                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1630                 return 0;
1631         }
1632         qdata = &cmd->data.setassparms.data.query_arp;
1633         QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1634
1635         do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1636         stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1637         entrybytes_done = 0;
1638         for (e = 0; e < qdata->no_entries; ++e) {
1639                 char *cur_entry;
1640                 __u32 esize;
1641                 struct qeth_arp_entrytype *etype;
1642
1643                 cur_entry = &qdata->data + entrybytes_done;
1644                 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1645                 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1646                         QETH_CARD_TEXT(card, 4, "pmis");
1647                         QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1648                         break;
1649                 }
1650                 esize = get_arp_entry_size(card, qdata, etype,
1651                         do_strip_entries);
1652                 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1653                 if (!esize)
1654                         break;
1655
1656                 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1657                         QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOMEM);
1658                         cmd->hdr.return_code = IPA_RC_ENOMEM;
1659                         goto out_error;
1660                 }
1661
1662                 memcpy(qinfo->udata + qinfo->udata_offset,
1663                         &qdata->data + entrybytes_done + stripped_bytes,
1664                         esize);
1665                 entrybytes_done += esize + stripped_bytes;
1666                 qinfo->udata_offset += esize;
1667                 ++qinfo->no_entries;
1668         }
1669         /* check if all replies received ... */
1670         if (cmd->data.setassparms.hdr.seq_no <
1671             cmd->data.setassparms.hdr.number_of_replies)
1672                 return 1;
1673         QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1674         memcpy(qinfo->udata, &qinfo->no_entries, 4);
1675         /* keep STRIP_ENTRIES flag so the user program can distinguish
1676          * stripped entries from normal ones */
1677         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1678                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1679         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1680         QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1681         return 0;
1682 out_error:
1683         i = 0;
1684         memcpy(qinfo->udata, &i, 4);
1685         return 0;
1686 }
1687
1688 static int qeth_l3_send_ipa_arp_cmd(struct qeth_card *card,
1689                 struct qeth_cmd_buffer *iob, int len,
1690                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1691                         unsigned long),
1692                 void *reply_param)
1693 {
1694         QETH_CARD_TEXT(card, 4, "sendarp");
1695
1696         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1697         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1698                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1699         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
1700                                       reply_cb, reply_param);
1701 }
1702
1703 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1704         enum qeth_prot_versions prot,
1705         struct qeth_arp_query_info *qinfo)
1706 {
1707         struct qeth_cmd_buffer *iob;
1708         struct qeth_ipa_cmd *cmd;
1709         int rc;
1710
1711         QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1712
1713         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1714                                        IPA_CMD_ASS_ARP_QUERY_INFO,
1715                                        sizeof(struct qeth_arp_query_data)
1716                                                 - sizeof(char),
1717                                        prot);
1718         if (!iob)
1719                 return -ENOMEM;
1720         cmd = __ipa_cmd(iob);
1721         cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1722         cmd->data.setassparms.data.query_arp.reply_bits = 0;
1723         cmd->data.setassparms.data.query_arp.no_entries = 0;
1724         rc = qeth_l3_send_ipa_arp_cmd(card, iob,
1725                            QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
1726                            qeth_l3_arp_query_cb, (void *)qinfo);
1727         if (rc)
1728                 QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n",
1729                                  CARD_DEVID(card), rc);
1730         return qeth_l3_arp_makerc(rc);
1731 }
1732
1733 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
1734 {
1735         struct qeth_arp_query_info qinfo = {0, };
1736         int rc;
1737
1738         QETH_CARD_TEXT(card, 3, "arpquery");
1739
1740         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
1741                                IPA_ARP_PROCESSING)) {
1742                 QETH_CARD_TEXT(card, 3, "arpqnsup");
1743                 rc = -EOPNOTSUPP;
1744                 goto out;
1745         }
1746         /* get size of userspace buffer and mask_bits -> 6 bytes */
1747         if (copy_from_user(&qinfo, udata, 6)) {
1748                 rc = -EFAULT;
1749                 goto out;
1750         }
1751         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1752         if (!qinfo.udata) {
1753                 rc = -ENOMEM;
1754                 goto out;
1755         }
1756         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1757         rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
1758         if (rc) {
1759                 if (copy_to_user(udata, qinfo.udata, 4))
1760                         rc = -EFAULT;
1761                 goto free_and_out;
1762         }
1763         if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
1764                 /* fails in case of GuestLAN QDIO mode */
1765                 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
1766         }
1767         if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
1768                 QETH_CARD_TEXT(card, 4, "qactf");
1769                 rc = -EFAULT;
1770                 goto free_and_out;
1771         }
1772         QETH_CARD_TEXT(card, 4, "qacts");
1773
1774 free_and_out:
1775         kfree(qinfo.udata);
1776 out:
1777         return rc;
1778 }
1779
1780 static int qeth_l3_arp_modify_entry(struct qeth_card *card,
1781                                     struct qeth_arp_cache_entry *entry,
1782                                     enum qeth_arp_process_subcmds arp_cmd)
1783 {
1784         struct qeth_arp_cache_entry *cmd_entry;
1785         struct qeth_cmd_buffer *iob;
1786         int rc;
1787
1788         if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY)
1789                 QETH_CARD_TEXT(card, 3, "arpadd");
1790         else
1791                 QETH_CARD_TEXT(card, 3, "arpdel");
1792
1793         /*
1794          * currently GuestLAN only supports the ARP assist function
1795          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
1796          * thus we say EOPNOTSUPP for this ARP function
1797          */
1798         if (card->info.guestlan)
1799                 return -EOPNOTSUPP;
1800         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1801                 return -EOPNOTSUPP;
1802         }
1803
1804         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd,
1805                                        sizeof(*cmd_entry), QETH_PROT_IPV4);
1806         if (!iob)
1807                 return -ENOMEM;
1808
1809         cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry;
1810         ether_addr_copy(cmd_entry->macaddr, entry->macaddr);
1811         memcpy(cmd_entry->ipaddr, entry->ipaddr, 4);
1812         rc = qeth_send_ipa_cmd(card, iob, qeth_setassparms_cb, NULL);
1813         if (rc)
1814                 QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n",
1815                                  arp_cmd, CARD_DEVID(card), rc);
1816
1817         return qeth_l3_arp_makerc(rc);
1818 }
1819
1820 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
1821 {
1822         int rc;
1823
1824         QETH_CARD_TEXT(card, 3, "arpflush");
1825
1826         /*
1827          * currently GuestLAN only supports the ARP assist function
1828          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
1829          * thus we say EOPNOTSUPP for this ARP function
1830         */
1831         if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
1832                 return -EOPNOTSUPP;
1833         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1834                 return -EOPNOTSUPP;
1835         }
1836         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1837                                           IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
1838         if (rc)
1839                 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n",
1840                                  CARD_DEVID(card), rc);
1841         return qeth_l3_arp_makerc(rc);
1842 }
1843
1844 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1845 {
1846         struct qeth_card *card = dev->ml_priv;
1847         struct qeth_arp_cache_entry arp_entry;
1848         enum qeth_arp_process_subcmds arp_cmd;
1849         int rc = 0;
1850
1851         switch (cmd) {
1852         case SIOC_QETH_ARP_SET_NO_ENTRIES:
1853                 if (!capable(CAP_NET_ADMIN)) {
1854                         rc = -EPERM;
1855                         break;
1856                 }
1857                 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
1858                 break;
1859         case SIOC_QETH_ARP_QUERY_INFO:
1860                 if (!capable(CAP_NET_ADMIN)) {
1861                         rc = -EPERM;
1862                         break;
1863                 }
1864                 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
1865                 break;
1866         case SIOC_QETH_ARP_ADD_ENTRY:
1867         case SIOC_QETH_ARP_REMOVE_ENTRY:
1868                 if (!capable(CAP_NET_ADMIN))
1869                         return -EPERM;
1870                 if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry)))
1871                         return -EFAULT;
1872
1873                 arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ?
1874                                 IPA_CMD_ASS_ARP_ADD_ENTRY :
1875                                 IPA_CMD_ASS_ARP_REMOVE_ENTRY;
1876                 return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd);
1877         case SIOC_QETH_ARP_FLUSH_CACHE:
1878                 if (!capable(CAP_NET_ADMIN)) {
1879                         rc = -EPERM;
1880                         break;
1881                 }
1882                 rc = qeth_l3_arp_flush_cache(card);
1883                 break;
1884         default:
1885                 rc = -EOPNOTSUPP;
1886         }
1887         return rc;
1888 }
1889
1890 static int qeth_l3_get_cast_type(struct sk_buff *skb)
1891 {
1892         struct neighbour *n = NULL;
1893         struct dst_entry *dst;
1894
1895         rcu_read_lock();
1896         dst = skb_dst(skb);
1897         if (dst)
1898                 n = dst_neigh_lookup_skb(dst, skb);
1899         if (n) {
1900                 int cast_type = n->type;
1901
1902                 rcu_read_unlock();
1903                 neigh_release(n);
1904                 if ((cast_type == RTN_BROADCAST) ||
1905                     (cast_type == RTN_MULTICAST) ||
1906                     (cast_type == RTN_ANYCAST))
1907                         return cast_type;
1908                 return RTN_UNICAST;
1909         }
1910         rcu_read_unlock();
1911
1912         /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1913         switch (qeth_get_ip_version(skb)) {
1914         case 4:
1915                 return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1916                                 RTN_MULTICAST : RTN_UNICAST;
1917         case 6:
1918                 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
1919                                 RTN_MULTICAST : RTN_UNICAST;
1920         default:
1921                 /* ... and MAC address */
1922                 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
1923                                             skb->dev->broadcast))
1924                         return RTN_BROADCAST;
1925                 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1926                         return RTN_MULTICAST;
1927                 /* default to unicast */
1928                 return RTN_UNICAST;
1929         }
1930 }
1931
1932 static void qeth_l3_fill_af_iucv_hdr(struct qeth_hdr *hdr, struct sk_buff *skb,
1933                                      unsigned int data_len)
1934 {
1935         char daddr[16];
1936
1937         hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1938         hdr->hdr.l3.length = data_len;
1939         hdr->hdr.l3.flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
1940
1941         memset(daddr, 0, sizeof(daddr));
1942         daddr[0] = 0xfe;
1943         daddr[1] = 0x80;
1944         memcpy(&daddr[8], iucv_trans_hdr(skb)->destUserID, 8);
1945         memcpy(hdr->hdr.l3.next_hop.ipv6_addr, daddr, 16);
1946 }
1947
1948 static u8 qeth_l3_cast_type_to_flag(int cast_type)
1949 {
1950         if (cast_type == RTN_MULTICAST)
1951                 return QETH_CAST_MULTICAST;
1952         if (cast_type == RTN_ANYCAST)
1953                 return QETH_CAST_ANYCAST;
1954         if (cast_type == RTN_BROADCAST)
1955                 return QETH_CAST_BROADCAST;
1956         return QETH_CAST_UNICAST;
1957 }
1958
1959 static void qeth_l3_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
1960                                 struct sk_buff *skb, int ipv, int cast_type,
1961                                 unsigned int data_len)
1962 {
1963         struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
1964
1965         hdr->hdr.l3.length = data_len;
1966
1967         if (skb_is_gso(skb)) {
1968                 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
1969         } else {
1970                 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1971                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1972                         qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
1973                         /* some HW requires combined L3+L4 csum offload: */
1974                         if (ipv == 4)
1975                                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
1976                         if (card->options.performance_stats)
1977                                 card->perf_stats.tx_csum++;
1978                 }
1979         }
1980
1981         if (ipv == 4 || IS_IQD(card)) {
1982                 /* NETIF_F_HW_VLAN_CTAG_TX */
1983                 if (skb_vlan_tag_present(skb)) {
1984                         hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
1985                         hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
1986                 }
1987         } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
1988                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
1989                 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
1990         }
1991
1992         /* OSA only: */
1993         if (!ipv) {
1994                 hdr->hdr.l3.flags = QETH_HDR_PASSTHRU;
1995                 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
1996                                             skb->dev->broadcast))
1997                         hdr->hdr.l3.flags |= QETH_CAST_BROADCAST;
1998                 else
1999                         hdr->hdr.l3.flags |= (cast_type == RTN_MULTICAST) ?
2000                                 QETH_CAST_MULTICAST : QETH_CAST_UNICAST;
2001                 return;
2002         }
2003
2004         hdr->hdr.l3.flags = qeth_l3_cast_type_to_flag(cast_type);
2005         rcu_read_lock();
2006         if (ipv == 4) {
2007                 struct rtable *rt = skb_rtable(skb);
2008
2009                 *((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
2010                                 rt_nexthop(rt, ip_hdr(skb)->daddr) :
2011                                 ip_hdr(skb)->daddr;
2012         } else {
2013                 /* IPv6 */
2014                 const struct rt6_info *rt = skb_rt6_info(skb);
2015                 const struct in6_addr *next_hop;
2016
2017                 if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
2018                         next_hop = &rt->rt6i_gateway;
2019                 else
2020                         next_hop = &ipv6_hdr(skb)->daddr;
2021                 memcpy(hdr->hdr.l3.next_hop.ipv6_addr, next_hop, 16);
2022
2023                 hdr->hdr.l3.flags |= QETH_HDR_IPV6;
2024                 if (card->info.type != QETH_CARD_TYPE_IQD)
2025                         hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
2026         }
2027         rcu_read_unlock();
2028 }
2029
2030 static void qeth_l3_fixup_headers(struct sk_buff *skb)
2031 {
2032         struct iphdr *iph = ip_hdr(skb);
2033
2034         /* this is safe, IPv6 traffic takes a different path */
2035         if (skb->ip_summed == CHECKSUM_PARTIAL)
2036                 iph->check = 0;
2037         if (skb_is_gso(skb)) {
2038                 iph->tot_len = 0;
2039                 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
2040                                                     iph->daddr, 0);
2041         }
2042 }
2043
2044 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
2045                         struct qeth_qdio_out_q *queue, int ipv, int cast_type)
2046 {
2047         unsigned int hw_hdr_len, proto_len, frame_len, elements;
2048         unsigned char eth_hdr[ETH_HLEN];
2049         bool is_tso = skb_is_gso(skb);
2050         unsigned int data_offset = 0;
2051         struct qeth_hdr *hdr = NULL;
2052         unsigned int hd_len = 0;
2053         int push_len, rc;
2054         bool is_sg;
2055
2056         if (is_tso) {
2057                 hw_hdr_len = sizeof(struct qeth_hdr_tso);
2058                 proto_len = skb_transport_offset(skb) + tcp_hdrlen(skb) -
2059                             ETH_HLEN;
2060         } else {
2061                 hw_hdr_len = sizeof(struct qeth_hdr);
2062                 proto_len = 0;
2063         }
2064
2065         /* re-use the L2 header area for the HW header: */
2066         rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
2067         if (rc)
2068                 return rc;
2069         skb_copy_from_linear_data(skb, eth_hdr, ETH_HLEN);
2070         skb_pull(skb, ETH_HLEN);
2071         frame_len = skb->len;
2072
2073         qeth_l3_fixup_headers(skb);
2074         push_len = qeth_add_hw_header(card, skb, &hdr, hw_hdr_len, proto_len,
2075                                       &elements);
2076         if (push_len < 0)
2077                 return push_len;
2078         if (is_tso || !push_len) {
2079                 /* HW header needs its own buffer element. */
2080                 hd_len = hw_hdr_len + proto_len;
2081                 data_offset = push_len + proto_len;
2082         }
2083         memset(hdr, 0, hw_hdr_len);
2084
2085         if (skb->protocol == htons(ETH_P_AF_IUCV)) {
2086                 qeth_l3_fill_af_iucv_hdr(hdr, skb, frame_len);
2087         } else {
2088                 qeth_l3_fill_header(card, hdr, skb, ipv, cast_type, frame_len);
2089                 if (is_tso)
2090                         qeth_fill_tso_ext((struct qeth_hdr_tso *) hdr,
2091                                           frame_len - proto_len, skb,
2092                                           proto_len);
2093         }
2094
2095         is_sg = skb_is_nonlinear(skb);
2096         if (IS_IQD(card)) {
2097                 rc = qeth_do_send_packet_fast(queue, skb, hdr, data_offset,
2098                                               hd_len);
2099         } else {
2100                 /* TODO: drop skb_orphan() once TX completion is fast enough */
2101                 skb_orphan(skb);
2102                 rc = qeth_do_send_packet(card, queue, skb, hdr, data_offset,
2103                                          hd_len, elements);
2104         }
2105
2106         if (!rc) {
2107                 if (card->options.performance_stats) {
2108                         card->perf_stats.buf_elements_sent += elements;
2109                         if (is_sg)
2110                                 card->perf_stats.sg_skbs_sent++;
2111                         if (is_tso) {
2112                                 card->perf_stats.large_send_bytes += frame_len;
2113                                 card->perf_stats.large_send_cnt++;
2114                         }
2115                 }
2116         } else {
2117                 if (!push_len)
2118                         kmem_cache_free(qeth_core_header_cache, hdr);
2119                 if (rc == -EBUSY) {
2120                         /* roll back to ETH header */
2121                         skb_pull(skb, push_len);
2122                         skb_push(skb, ETH_HLEN);
2123                         skb_copy_to_linear_data(skb, eth_hdr, ETH_HLEN);
2124                 }
2125         }
2126         return rc;
2127 }
2128
2129 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
2130                                            struct net_device *dev)
2131 {
2132         int cast_type = qeth_l3_get_cast_type(skb);
2133         struct qeth_card *card = dev->ml_priv;
2134         int ipv = qeth_get_ip_version(skb);
2135         struct qeth_qdio_out_q *queue;
2136         int tx_bytes = skb->len;
2137         int rc;
2138
2139         if (IS_IQD(card)) {
2140                 if (card->options.sniffer)
2141                         goto tx_drop;
2142                 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
2143                     (card->options.cq == QETH_CQ_ENABLED &&
2144                      skb->protocol != htons(ETH_P_AF_IUCV)))
2145                         goto tx_drop;
2146         }
2147
2148         if (card->state != CARD_STATE_UP) {
2149                 card->stats.tx_carrier_errors++;
2150                 goto tx_drop;
2151         }
2152
2153         if (cast_type == RTN_BROADCAST && !card->info.broadcast_capable)
2154                 goto tx_drop;
2155
2156         queue = qeth_get_tx_queue(card, skb, ipv, cast_type);
2157
2158         if (card->options.performance_stats) {
2159                 card->perf_stats.outbound_cnt++;
2160                 card->perf_stats.outbound_start_time = qeth_get_micros();
2161         }
2162         netif_stop_queue(dev);
2163
2164         if (ipv == 4 || IS_IQD(card))
2165                 rc = qeth_l3_xmit(card, skb, queue, ipv, cast_type);
2166         else
2167                 rc = qeth_xmit(card, skb, queue, ipv, cast_type,
2168                                qeth_l3_fill_header);
2169
2170         if (!rc) {
2171                 card->stats.tx_packets++;
2172                 card->stats.tx_bytes += tx_bytes;
2173                 if (card->options.performance_stats)
2174                         card->perf_stats.outbound_time += qeth_get_micros() -
2175                                 card->perf_stats.outbound_start_time;
2176                 netif_wake_queue(dev);
2177                 return NETDEV_TX_OK;
2178         } else if (rc == -EBUSY) {
2179                 return NETDEV_TX_BUSY;
2180         } /* else fall through */
2181
2182 tx_drop:
2183         card->stats.tx_dropped++;
2184         card->stats.tx_errors++;
2185         dev_kfree_skb_any(skb);
2186         netif_wake_queue(dev);
2187         return NETDEV_TX_OK;
2188 }
2189
2190 static int __qeth_l3_open(struct net_device *dev)
2191 {
2192         struct qeth_card *card = dev->ml_priv;
2193         int rc = 0;
2194
2195         QETH_CARD_TEXT(card, 4, "qethopen");
2196         if (card->state == CARD_STATE_UP)
2197                 return rc;
2198         if (card->state != CARD_STATE_SOFTSETUP)
2199                 return -ENODEV;
2200         card->data.state = CH_STATE_UP;
2201         card->state = CARD_STATE_UP;
2202         netif_start_queue(dev);
2203
2204         if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
2205                 napi_enable(&card->napi);
2206                 local_bh_disable();
2207                 napi_schedule(&card->napi);
2208                 /* kick-start the NAPI softirq: */
2209                 local_bh_enable();
2210         } else
2211                 rc = -EIO;
2212         return rc;
2213 }
2214
2215 static int qeth_l3_open(struct net_device *dev)
2216 {
2217         struct qeth_card *card = dev->ml_priv;
2218
2219         QETH_CARD_TEXT(card, 5, "qethope_");
2220         if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
2221                 QETH_CARD_TEXT(card, 3, "openREC");
2222                 return -ERESTARTSYS;
2223         }
2224         return __qeth_l3_open(dev);
2225 }
2226
2227 static int qeth_l3_stop(struct net_device *dev)
2228 {
2229         struct qeth_card *card = dev->ml_priv;
2230
2231         QETH_CARD_TEXT(card, 4, "qethstop");
2232         netif_tx_disable(dev);
2233         if (card->state == CARD_STATE_UP) {
2234                 card->state = CARD_STATE_SOFTSETUP;
2235                 napi_disable(&card->napi);
2236         }
2237         return 0;
2238 }
2239
2240 static const struct ethtool_ops qeth_l3_ethtool_ops = {
2241         .get_link = ethtool_op_get_link,
2242         .get_strings = qeth_core_get_strings,
2243         .get_ethtool_stats = qeth_core_get_ethtool_stats,
2244         .get_sset_count = qeth_core_get_sset_count,
2245         .get_drvinfo = qeth_core_get_drvinfo,
2246         .get_link_ksettings = qeth_core_ethtool_get_link_ksettings,
2247 };
2248
2249 /*
2250  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
2251  * NOARP on the netdevice is no option because it also turns off neighbor
2252  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
2253  * arp resolution but we want the hard header (packet socket will work
2254  * e.g. tcpdump)
2255  */
2256 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
2257 {
2258         n->nud_state = NUD_NOARP;
2259         memcpy(n->ha, "FAKELL", 6);
2260         n->output = n->ops->connected_output;
2261         return 0;
2262 }
2263
2264 static int
2265 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
2266 {
2267         if (np->tbl->family == AF_INET)
2268                 np->neigh_setup = qeth_l3_neigh_setup_noarp;
2269
2270         return 0;
2271 }
2272
2273 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
2274                                                     struct net_device *dev,
2275                                                     netdev_features_t features)
2276 {
2277         if (qeth_get_ip_version(skb) != 4)
2278                 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2279         return qeth_features_check(skb, dev, features);
2280 }
2281
2282 static const struct net_device_ops qeth_l3_netdev_ops = {
2283         .ndo_open               = qeth_l3_open,
2284         .ndo_stop               = qeth_l3_stop,
2285         .ndo_get_stats          = qeth_get_stats,
2286         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2287         .ndo_validate_addr      = eth_validate_addr,
2288         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2289         .ndo_do_ioctl           = qeth_do_ioctl,
2290         .ndo_fix_features       = qeth_fix_features,
2291         .ndo_set_features       = qeth_set_features,
2292         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2293         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2294         .ndo_tx_timeout         = qeth_tx_timeout,
2295 };
2296
2297 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2298         .ndo_open               = qeth_l3_open,
2299         .ndo_stop               = qeth_l3_stop,
2300         .ndo_get_stats          = qeth_get_stats,
2301         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2302         .ndo_features_check     = qeth_l3_osa_features_check,
2303         .ndo_validate_addr      = eth_validate_addr,
2304         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2305         .ndo_do_ioctl           = qeth_do_ioctl,
2306         .ndo_fix_features       = qeth_fix_features,
2307         .ndo_set_features       = qeth_set_features,
2308         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2309         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2310         .ndo_tx_timeout         = qeth_tx_timeout,
2311         .ndo_neigh_setup        = qeth_l3_neigh_setup,
2312 };
2313
2314 static int qeth_l3_setup_netdev(struct qeth_card *card, bool carrier_ok)
2315 {
2316         unsigned int headroom;
2317         int rc;
2318
2319         if (qeth_netdev_is_registered(card->dev))
2320                 return 0;
2321
2322         if (card->info.type == QETH_CARD_TYPE_OSD ||
2323             card->info.type == QETH_CARD_TYPE_OSX) {
2324                 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
2325                     (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2326                         pr_info("qeth_l3: ignoring TR device\n");
2327                         return -ENODEV;
2328                 }
2329
2330                 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
2331
2332                 /*IPv6 address autoconfiguration stuff*/
2333                 qeth_l3_get_unique_id(card);
2334                 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
2335                         card->dev->dev_id = card->info.unique_id & 0xffff;
2336
2337                 if (!card->info.guestlan) {
2338                         card->dev->features |= NETIF_F_SG;
2339                         card->dev->hw_features |= NETIF_F_TSO |
2340                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2341                         card->dev->vlan_features |= NETIF_F_TSO |
2342                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2343                 }
2344
2345                 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
2346                         card->dev->hw_features |= NETIF_F_IPV6_CSUM;
2347                         card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
2348                 }
2349                 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
2350                         card->dev->hw_features |= NETIF_F_TSO6;
2351                         card->dev->vlan_features |= NETIF_F_TSO6;
2352                 }
2353
2354                 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
2355                 if (card->dev->hw_features & NETIF_F_TSO6)
2356                         headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
2357                 else if (card->dev->hw_features & NETIF_F_TSO)
2358                         headroom = sizeof(struct qeth_hdr_tso);
2359                 else
2360                         headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
2361         } else if (card->info.type == QETH_CARD_TYPE_IQD) {
2362                 card->dev->flags |= IFF_NOARP;
2363                 card->dev->netdev_ops = &qeth_l3_netdev_ops;
2364                 headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
2365
2366                 rc = qeth_l3_iqd_read_initial_mac(card);
2367                 if (rc)
2368                         goto out;
2369
2370                 if (card->options.hsuid[0])
2371                         memcpy(card->dev->perm_addr, card->options.hsuid, 9);
2372         } else
2373                 return -ENODEV;
2374
2375         card->dev->needed_headroom = headroom;
2376         card->dev->ethtool_ops = &qeth_l3_ethtool_ops;
2377         card->dev->features |=  NETIF_F_HW_VLAN_CTAG_TX |
2378                                 NETIF_F_HW_VLAN_CTAG_RX |
2379                                 NETIF_F_HW_VLAN_CTAG_FILTER;
2380
2381         netif_keep_dst(card->dev);
2382         if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
2383                 netif_set_gso_max_size(card->dev,
2384                                        PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
2385
2386         netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2387         rc = register_netdev(card->dev);
2388         if (!rc && carrier_ok)
2389                 netif_carrier_on(card->dev);
2390
2391 out:
2392         if (rc)
2393                 card->dev->netdev_ops = NULL;
2394         return rc;
2395 }
2396
2397 static const struct device_type qeth_l3_devtype = {
2398         .name = "qeth_layer3",
2399         .groups = qeth_l3_attr_groups,
2400 };
2401
2402 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
2403 {
2404         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2405         int rc;
2406
2407         if (gdev->dev.type == &qeth_generic_devtype) {
2408                 rc = qeth_l3_create_device_attributes(&gdev->dev);
2409                 if (rc)
2410                         return rc;
2411         }
2412         hash_init(card->ip_htable);
2413         hash_init(card->ip_mc_htable);
2414         card->info.hwtrap = 0;
2415         return 0;
2416 }
2417
2418 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2419 {
2420         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2421
2422         if (cgdev->dev.type == &qeth_generic_devtype)
2423                 qeth_l3_remove_device_attributes(&cgdev->dev);
2424
2425         qeth_set_allowed_threads(card, 0, 1);
2426         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2427
2428         if (cgdev->state == CCWGROUP_ONLINE)
2429                 qeth_l3_set_offline(cgdev);
2430
2431         if (qeth_netdev_is_registered(card->dev))
2432                 unregister_netdev(card->dev);
2433         qeth_l3_clear_ip_htable(card, 0);
2434         qeth_l3_clear_ipato_list(card);
2435 }
2436
2437 static int __qeth_l3_set_online(struct ccwgroup_device *gdev, int recovery_mode)
2438 {
2439         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2440         int rc = 0;
2441         enum qeth_card_states recover_flag;
2442         bool carrier_ok;
2443
2444         mutex_lock(&card->discipline_mutex);
2445         mutex_lock(&card->conf_mutex);
2446         QETH_DBF_TEXT(SETUP, 2, "setonlin");
2447         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
2448
2449         recover_flag = card->state;
2450         rc = qeth_core_hardsetup_card(card, &carrier_ok);
2451         if (rc) {
2452                 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2453                 rc = -ENODEV;
2454                 goto out_remove;
2455         }
2456
2457         rc = qeth_l3_setup_netdev(card, carrier_ok);
2458         if (rc)
2459                 goto out_remove;
2460
2461         if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
2462                 if (card->info.hwtrap &&
2463                     qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
2464                         card->info.hwtrap = 0;
2465         } else
2466                 card->info.hwtrap = 0;
2467
2468         card->state = CARD_STATE_HARDSETUP;
2469         qeth_print_status_message(card);
2470
2471         /* softsetup */
2472         QETH_DBF_TEXT(SETUP, 2, "softsetp");
2473
2474         rc = qeth_l3_setadapter_parms(card);
2475         if (rc)
2476                 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2477         if (!card->options.sniffer) {
2478                 rc = qeth_l3_start_ipassists(card);
2479                 if (rc) {
2480                         QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2481                         goto out_remove;
2482                 }
2483                 rc = qeth_l3_setrouting_v4(card);
2484                 if (rc)
2485                         QETH_DBF_TEXT_(SETUP, 2, "4err%04x", rc);
2486                 rc = qeth_l3_setrouting_v6(card);
2487                 if (rc)
2488                         QETH_DBF_TEXT_(SETUP, 2, "5err%04x", rc);
2489         }
2490         netif_tx_disable(card->dev);
2491
2492         rc = qeth_init_qdio_queues(card);
2493         if (rc) {
2494                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2495                 rc = -ENODEV;
2496                 goto out_remove;
2497         }
2498         card->state = CARD_STATE_SOFTSETUP;
2499
2500         qeth_set_allowed_threads(card, 0xffffffff, 0);
2501         qeth_l3_recover_ip(card);
2502
2503         qeth_enable_hw_features(card->dev);
2504         if (recover_flag == CARD_STATE_RECOVER) {
2505                 rtnl_lock();
2506                 if (recovery_mode) {
2507                         __qeth_l3_open(card->dev);
2508                         qeth_l3_set_rx_mode(card->dev);
2509                 } else {
2510                         dev_open(card->dev);
2511                 }
2512                 rtnl_unlock();
2513         }
2514         qeth_trace_features(card);
2515         /* let user_space know that device is online */
2516         kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2517         mutex_unlock(&card->conf_mutex);
2518         mutex_unlock(&card->discipline_mutex);
2519         return 0;
2520 out_remove:
2521         qeth_l3_stop_card(card, 0);
2522         ccw_device_set_offline(CARD_DDEV(card));
2523         ccw_device_set_offline(CARD_WDEV(card));
2524         ccw_device_set_offline(CARD_RDEV(card));
2525         qdio_free(CARD_DDEV(card));
2526         if (recover_flag == CARD_STATE_RECOVER)
2527                 card->state = CARD_STATE_RECOVER;
2528         else
2529                 card->state = CARD_STATE_DOWN;
2530         mutex_unlock(&card->conf_mutex);
2531         mutex_unlock(&card->discipline_mutex);
2532         return rc;
2533 }
2534
2535 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
2536 {
2537         return __qeth_l3_set_online(gdev, 0);
2538 }
2539
2540 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
2541                         int recovery_mode)
2542 {
2543         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2544         int rc = 0, rc2 = 0, rc3 = 0;
2545         enum qeth_card_states recover_flag;
2546
2547         mutex_lock(&card->discipline_mutex);
2548         mutex_lock(&card->conf_mutex);
2549         QETH_DBF_TEXT(SETUP, 3, "setoffl");
2550         QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
2551
2552         netif_carrier_off(card->dev);
2553         recover_flag = card->state;
2554         if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
2555                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
2556                 card->info.hwtrap = 1;
2557         }
2558         qeth_l3_stop_card(card, recovery_mode);
2559         if ((card->options.cq == QETH_CQ_ENABLED) && card->dev) {
2560                 rtnl_lock();
2561                 call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
2562                 rtnl_unlock();
2563         }
2564         rc  = ccw_device_set_offline(CARD_DDEV(card));
2565         rc2 = ccw_device_set_offline(CARD_WDEV(card));
2566         rc3 = ccw_device_set_offline(CARD_RDEV(card));
2567         if (!rc)
2568                 rc = (rc2) ? rc2 : rc3;
2569         if (rc)
2570                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2571         qdio_free(CARD_DDEV(card));
2572         if (recover_flag == CARD_STATE_UP)
2573                 card->state = CARD_STATE_RECOVER;
2574         /* let user_space know that device is offline */
2575         kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
2576         mutex_unlock(&card->conf_mutex);
2577         mutex_unlock(&card->discipline_mutex);
2578         return 0;
2579 }
2580
2581 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
2582 {
2583         return __qeth_l3_set_offline(cgdev, 0);
2584 }
2585
2586 static int qeth_l3_recover(void *ptr)
2587 {
2588         struct qeth_card *card;
2589         int rc = 0;
2590
2591         card = (struct qeth_card *) ptr;
2592         QETH_CARD_TEXT(card, 2, "recover1");
2593         QETH_CARD_HEX(card, 2, &card, sizeof(void *));
2594         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
2595                 return 0;
2596         QETH_CARD_TEXT(card, 2, "recover2");
2597         dev_warn(&card->gdev->dev,
2598                 "A recovery process has been started for the device\n");
2599         qeth_set_recovery_task(card);
2600         __qeth_l3_set_offline(card->gdev, 1);
2601         rc = __qeth_l3_set_online(card->gdev, 1);
2602         if (!rc)
2603                 dev_info(&card->gdev->dev,
2604                         "Device successfully recovered!\n");
2605         else {
2606                 qeth_close_dev(card);
2607                 dev_warn(&card->gdev->dev, "The qeth device driver "
2608                                 "failed to recover an error on the device\n");
2609         }
2610         qeth_clear_recovery_task(card);
2611         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
2612         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
2613         return 0;
2614 }
2615
2616 static int qeth_l3_pm_suspend(struct ccwgroup_device *gdev)
2617 {
2618         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2619
2620         netif_device_detach(card->dev);
2621         qeth_set_allowed_threads(card, 0, 1);
2622         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2623         if (gdev->state == CCWGROUP_OFFLINE)
2624                 return 0;
2625         if (card->state == CARD_STATE_UP) {
2626                 if (card->info.hwtrap)
2627                         qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
2628                 __qeth_l3_set_offline(card->gdev, 1);
2629         } else
2630                 __qeth_l3_set_offline(card->gdev, 0);
2631         return 0;
2632 }
2633
2634 static int qeth_l3_pm_resume(struct ccwgroup_device *gdev)
2635 {
2636         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2637         int rc = 0;
2638
2639         if (card->state == CARD_STATE_RECOVER) {
2640                 rc = __qeth_l3_set_online(card->gdev, 1);
2641                 if (rc) {
2642                         rtnl_lock();
2643                         dev_close(card->dev);
2644                         rtnl_unlock();
2645                 }
2646         } else
2647                 rc = __qeth_l3_set_online(card->gdev, 0);
2648
2649         qeth_set_allowed_threads(card, 0xffffffff, 0);
2650         netif_device_attach(card->dev);
2651         if (rc)
2652                 dev_warn(&card->gdev->dev, "The qeth device driver "
2653                         "failed to recover an error on the device\n");
2654         return rc;
2655 }
2656
2657 /* Returns zero if the command is successfully "consumed" */
2658 static int qeth_l3_control_event(struct qeth_card *card,
2659                                         struct qeth_ipa_cmd *cmd)
2660 {
2661         return 1;
2662 }
2663
2664 struct qeth_discipline qeth_l3_discipline = {
2665         .devtype = &qeth_l3_devtype,
2666         .process_rx_buffer = qeth_l3_process_inbound_buffer,
2667         .recover = qeth_l3_recover,
2668         .setup = qeth_l3_probe_device,
2669         .remove = qeth_l3_remove_device,
2670         .set_online = qeth_l3_set_online,
2671         .set_offline = qeth_l3_set_offline,
2672         .freeze = qeth_l3_pm_suspend,
2673         .thaw = qeth_l3_pm_resume,
2674         .restore = qeth_l3_pm_resume,
2675         .do_ioctl = qeth_l3_do_ioctl,
2676         .control_event_handler = qeth_l3_control_event,
2677 };
2678 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2679
2680 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2681                                    struct qeth_ipaddr *addr,
2682                                    unsigned long event)
2683 {
2684         switch (event) {
2685         case NETDEV_UP:
2686                 spin_lock_bh(&card->ip_lock);
2687                 qeth_l3_add_ip(card, addr);
2688                 spin_unlock_bh(&card->ip_lock);
2689                 return NOTIFY_OK;
2690         case NETDEV_DOWN:
2691                 spin_lock_bh(&card->ip_lock);
2692                 qeth_l3_delete_ip(card, addr);
2693                 spin_unlock_bh(&card->ip_lock);
2694                 return NOTIFY_OK;
2695         default:
2696                 return NOTIFY_DONE;
2697         }
2698 }
2699
2700 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2701 {
2702         if (is_vlan_dev(dev))
2703                 dev = vlan_dev_real_dev(dev);
2704         if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2705             dev->netdev_ops == &qeth_l3_netdev_ops)
2706                 return (struct qeth_card *) dev->ml_priv;
2707         return NULL;
2708 }
2709
2710 static int qeth_l3_ip_event(struct notifier_block *this,
2711                             unsigned long event, void *ptr)
2712 {
2713
2714         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2715         struct net_device *dev = ifa->ifa_dev->dev;
2716         struct qeth_ipaddr addr;
2717         struct qeth_card *card;
2718
2719         if (dev_net(dev) != &init_net)
2720                 return NOTIFY_DONE;
2721
2722         card = qeth_l3_get_card_from_dev(dev);
2723         if (!card)
2724                 return NOTIFY_DONE;
2725         QETH_CARD_TEXT(card, 3, "ipevent");
2726
2727         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2728         addr.u.a4.addr = be32_to_cpu(ifa->ifa_address);
2729         addr.u.a4.mask = be32_to_cpu(ifa->ifa_mask);
2730
2731         return qeth_l3_handle_ip_event(card, &addr, event);
2732 }
2733
2734 static struct notifier_block qeth_l3_ip_notifier = {
2735         qeth_l3_ip_event,
2736         NULL,
2737 };
2738
2739 static int qeth_l3_ip6_event(struct notifier_block *this,
2740                              unsigned long event, void *ptr)
2741 {
2742         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2743         struct net_device *dev = ifa->idev->dev;
2744         struct qeth_ipaddr addr;
2745         struct qeth_card *card;
2746
2747         card = qeth_l3_get_card_from_dev(dev);
2748         if (!card)
2749                 return NOTIFY_DONE;
2750         QETH_CARD_TEXT(card, 3, "ip6event");
2751         if (!qeth_is_supported(card, IPA_IPV6))
2752                 return NOTIFY_DONE;
2753
2754         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
2755         addr.u.a6.addr = ifa->addr;
2756         addr.u.a6.pfxlen = ifa->prefix_len;
2757
2758         return qeth_l3_handle_ip_event(card, &addr, event);
2759 }
2760
2761 static struct notifier_block qeth_l3_ip6_notifier = {
2762         qeth_l3_ip6_event,
2763         NULL,
2764 };
2765
2766 static int qeth_l3_register_notifiers(void)
2767 {
2768         int rc;
2769
2770         QETH_DBF_TEXT(SETUP, 5, "regnotif");
2771         rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2772         if (rc)
2773                 return rc;
2774         rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2775         if (rc) {
2776                 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2777                 return rc;
2778         }
2779         return 0;
2780 }
2781
2782 static void qeth_l3_unregister_notifiers(void)
2783 {
2784         QETH_DBF_TEXT(SETUP, 5, "unregnot");
2785         WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2786         WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2787 }
2788
2789 static int __init qeth_l3_init(void)
2790 {
2791         pr_info("register layer 3 discipline\n");
2792         return qeth_l3_register_notifiers();
2793 }
2794
2795 static void __exit qeth_l3_exit(void)
2796 {
2797         qeth_l3_unregister_notifiers();
2798         pr_info("unregister layer 3 discipline\n");
2799 }
2800
2801 module_init(qeth_l3_init);
2802 module_exit(qeth_l3_exit);
2803 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2804 MODULE_DESCRIPTION("qeth layer 3 discipline");
2805 MODULE_LICENSE("GPL");