Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-next
[linux-3.10.git] / drivers / s390 / net / qeth_l3_main.c
1 /*
2  *  drivers/s390/net/qeth_l3_main.c
3  *
4  *    Copyright IBM Corp. 2007, 2009
5  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6  *               Frank Pavlic <fpavlic@de.ibm.com>,
7  *               Thomas Spatzier <tspat@de.ibm.com>,
8  *               Frank Blaschka <frank.blaschka@de.ibm.com>
9  */
10
11 #define KMSG_COMPONENT "qeth"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/bitops.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/etherdevice.h>
21 #include <linux/mii.h>
22 #include <linux/ip.h>
23 #include <linux/ipv6.h>
24 #include <linux/inetdevice.h>
25 #include <linux/igmp.h>
26 #include <linux/slab.h>
27 #include <linux/if_vlan.h>
28
29 #include <net/ip.h>
30 #include <net/arp.h>
31 #include <net/route.h>
32 #include <net/ip6_fib.h>
33 #include <net/ip6_checksum.h>
34 #include <net/iucv/af_iucv.h>
35
36 #include "qeth_l3.h"
37
38
39 static int qeth_l3_set_offline(struct ccwgroup_device *);
40 static int qeth_l3_recover(void *);
41 static int qeth_l3_stop(struct net_device *);
42 static void qeth_l3_set_multicast_list(struct net_device *);
43 static int qeth_l3_neigh_setup(struct net_device *, struct neigh_parms *);
44 static int qeth_l3_register_addr_entry(struct qeth_card *,
45                 struct qeth_ipaddr *);
46 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
47                 struct qeth_ipaddr *);
48 static int __qeth_l3_set_online(struct ccwgroup_device *, int);
49 static int __qeth_l3_set_offline(struct ccwgroup_device *, int);
50
51 static int qeth_l3_isxdigit(char *buf)
52 {
53         while (*buf) {
54                 if (!isxdigit(*buf++))
55                         return 0;
56         }
57         return 1;
58 }
59
60 void qeth_l3_ipaddr4_to_string(const __u8 *addr, char *buf)
61 {
62         sprintf(buf, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
63 }
64
65 int qeth_l3_string_to_ipaddr4(const char *buf, __u8 *addr)
66 {
67         int count = 0, rc = 0;
68         int in[4];
69         char c;
70
71         rc = sscanf(buf, "%u.%u.%u.%u%c",
72                     &in[0], &in[1], &in[2], &in[3], &c);
73         if (rc != 4 && (rc != 5 || c != '\n'))
74                 return -EINVAL;
75         for (count = 0; count < 4; count++) {
76                 if (in[count] > 255)
77                         return -EINVAL;
78                 addr[count] = in[count];
79         }
80         return 0;
81 }
82
83 void qeth_l3_ipaddr6_to_string(const __u8 *addr, char *buf)
84 {
85         sprintf(buf, "%pI6", addr);
86 }
87
88 int qeth_l3_string_to_ipaddr6(const char *buf, __u8 *addr)
89 {
90         const char *end, *end_tmp, *start;
91         __u16 *in;
92         char num[5];
93         int num2, cnt, out, found, save_cnt;
94         unsigned short in_tmp[8] = {0, };
95
96         cnt = out = found = save_cnt = num2 = 0;
97         end = start = buf;
98         in = (__u16 *) addr;
99         memset(in, 0, 16);
100         while (*end) {
101                 end = strchr(start, ':');
102                 if (end == NULL) {
103                         end = buf + strlen(buf);
104                         end_tmp = strchr(start, '\n');
105                         if (end_tmp != NULL)
106                                 end = end_tmp;
107                         out = 1;
108                 }
109                 if ((end - start)) {
110                         memset(num, 0, 5);
111                         if ((end - start) > 4)
112                                 return -EINVAL;
113                         memcpy(num, start, end - start);
114                         if (!qeth_l3_isxdigit(num))
115                                 return -EINVAL;
116                         sscanf(start, "%x", &num2);
117                         if (found)
118                                 in_tmp[save_cnt++] = num2;
119                         else
120                                 in[cnt++] = num2;
121                         if (out)
122                                 break;
123                 } else {
124                         if (found)
125                                 return -EINVAL;
126                         found = 1;
127                 }
128                 start = ++end;
129         }
130         if (cnt + save_cnt > 8)
131                 return -EINVAL;
132         cnt = 7;
133         while (save_cnt)
134                 in[cnt--] = in_tmp[--save_cnt];
135         return 0;
136 }
137
138 void qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const __u8 *addr,
139                                 char *buf)
140 {
141         if (proto == QETH_PROT_IPV4)
142                 qeth_l3_ipaddr4_to_string(addr, buf);
143         else if (proto == QETH_PROT_IPV6)
144                 qeth_l3_ipaddr6_to_string(addr, buf);
145 }
146
147 int qeth_l3_string_to_ipaddr(const char *buf, enum qeth_prot_versions proto,
148                                 __u8 *addr)
149 {
150         if (proto == QETH_PROT_IPV4)
151                 return qeth_l3_string_to_ipaddr4(buf, addr);
152         else if (proto == QETH_PROT_IPV6)
153                 return qeth_l3_string_to_ipaddr6(buf, addr);
154         else
155                 return -EINVAL;
156 }
157
158 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
159 {
160         int i, j;
161         u8 octet;
162
163         for (i = 0; i < len; ++i) {
164                 octet = addr[i];
165                 for (j = 7; j >= 0; --j) {
166                         bits[i*8 + j] = octet & 1;
167                         octet >>= 1;
168                 }
169         }
170 }
171
172 int qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
173                                                 struct qeth_ipaddr *addr)
174 {
175         struct qeth_ipato_entry *ipatoe;
176         u8 addr_bits[128] = {0, };
177         u8 ipatoe_bits[128] = {0, };
178         int rc = 0;
179
180         if (!card->ipato.enabled)
181                 return 0;
182
183         qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
184                                   (addr->proto == QETH_PROT_IPV4)? 4:16);
185         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
186                 if (addr->proto != ipatoe->proto)
187                         continue;
188                 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
189                                           (ipatoe->proto == QETH_PROT_IPV4) ?
190                                           4 : 16);
191                 if (addr->proto == QETH_PROT_IPV4)
192                         rc = !memcmp(addr_bits, ipatoe_bits,
193                                      min(32, ipatoe->mask_bits));
194                 else
195                         rc = !memcmp(addr_bits, ipatoe_bits,
196                                      min(128, ipatoe->mask_bits));
197                 if (rc)
198                         break;
199         }
200         /* invert? */
201         if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
202                 rc = !rc;
203         else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
204                 rc = !rc;
205
206         return rc;
207 }
208
209 /*
210  * Add IP to be added to todo list. If there is already an "add todo"
211  * in this list we just incremenent the reference count.
212  * Returns 0 if we  just incremented reference count.
213  */
214 static int __qeth_l3_insert_ip_todo(struct qeth_card *card,
215                                         struct qeth_ipaddr *addr, int add)
216 {
217         struct qeth_ipaddr *tmp, *t;
218         int found = 0;
219
220         if (card->options.sniffer)
221                 return 0;
222         list_for_each_entry_safe(tmp, t, card->ip_tbd_list, entry) {
223                 if ((addr->type == QETH_IP_TYPE_DEL_ALL_MC) &&
224                     (tmp->type == QETH_IP_TYPE_DEL_ALL_MC))
225                         return 0;
226                 if ((tmp->proto        == QETH_PROT_IPV4)     &&
227                     (addr->proto       == QETH_PROT_IPV4)     &&
228                     (tmp->type         == addr->type)         &&
229                     (tmp->is_multicast == addr->is_multicast) &&
230                     (tmp->u.a4.addr    == addr->u.a4.addr)    &&
231                     (tmp->u.a4.mask    == addr->u.a4.mask)) {
232                         found = 1;
233                         break;
234                 }
235                 if ((tmp->proto        == QETH_PROT_IPV6)      &&
236                     (addr->proto       == QETH_PROT_IPV6)      &&
237                     (tmp->type         == addr->type)          &&
238                     (tmp->is_multicast == addr->is_multicast)  &&
239                     (tmp->u.a6.pfxlen  == addr->u.a6.pfxlen)   &&
240                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
241                             sizeof(struct in6_addr)) == 0)) {
242                         found = 1;
243                         break;
244                 }
245         }
246         if (found) {
247                 if (addr->users != 0)
248                         tmp->users += addr->users;
249                 else
250                         tmp->users += add ? 1 : -1;
251                 if (tmp->users == 0) {
252                         list_del(&tmp->entry);
253                         kfree(tmp);
254                 }
255                 return 0;
256         } else {
257                 if (addr->type == QETH_IP_TYPE_DEL_ALL_MC)
258                         list_add(&addr->entry, card->ip_tbd_list);
259                 else {
260                         if (addr->users == 0)
261                                 addr->users += add ? 1 : -1;
262                         if (add && (addr->type == QETH_IP_TYPE_NORMAL) &&
263                             qeth_l3_is_addr_covered_by_ipato(card, addr)) {
264                                 QETH_CARD_TEXT(card, 2, "tkovaddr");
265                                 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
266                         }
267                         list_add_tail(&addr->entry, card->ip_tbd_list);
268                 }
269                 return 1;
270         }
271 }
272
273 int qeth_l3_delete_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
274 {
275         unsigned long flags;
276         int rc = 0;
277
278         QETH_CARD_TEXT(card, 4, "delip");
279
280         if (addr->proto == QETH_PROT_IPV4)
281                 QETH_CARD_HEX(card, 4, &addr->u.a4.addr, 4);
282         else {
283                 QETH_CARD_HEX(card, 4, &addr->u.a6.addr, 8);
284                 QETH_CARD_HEX(card, 4, ((char *)&addr->u.a6.addr) + 8, 8);
285         }
286         spin_lock_irqsave(&card->ip_lock, flags);
287         rc = __qeth_l3_insert_ip_todo(card, addr, 0);
288         spin_unlock_irqrestore(&card->ip_lock, flags);
289         return rc;
290 }
291
292 int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
293 {
294         unsigned long flags;
295         int rc = 0;
296
297         QETH_CARD_TEXT(card, 4, "addip");
298         if (addr->proto == QETH_PROT_IPV4)
299                 QETH_CARD_HEX(card, 4, &addr->u.a4.addr, 4);
300         else {
301                 QETH_CARD_HEX(card, 4, &addr->u.a6.addr, 8);
302                 QETH_CARD_HEX(card, 4, ((char *)&addr->u.a6.addr) + 8, 8);
303         }
304         spin_lock_irqsave(&card->ip_lock, flags);
305         rc = __qeth_l3_insert_ip_todo(card, addr, 1);
306         spin_unlock_irqrestore(&card->ip_lock, flags);
307         return rc;
308 }
309
310
311 struct qeth_ipaddr *qeth_l3_get_addr_buffer(
312                                 enum qeth_prot_versions prot)
313 {
314         struct qeth_ipaddr *addr;
315
316         addr = kzalloc(sizeof(struct qeth_ipaddr), GFP_ATOMIC);
317         if (addr == NULL) {
318                 return NULL;
319         }
320         addr->type = QETH_IP_TYPE_NORMAL;
321         addr->proto = prot;
322         return addr;
323 }
324
325 static void qeth_l3_delete_mc_addresses(struct qeth_card *card)
326 {
327         struct qeth_ipaddr *iptodo;
328         unsigned long flags;
329
330         QETH_CARD_TEXT(card, 4, "delmc");
331         iptodo = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
332         if (!iptodo) {
333                 QETH_CARD_TEXT(card, 2, "dmcnomem");
334                 return;
335         }
336         iptodo->type = QETH_IP_TYPE_DEL_ALL_MC;
337         spin_lock_irqsave(&card->ip_lock, flags);
338         if (!__qeth_l3_insert_ip_todo(card, iptodo, 0))
339                 kfree(iptodo);
340         spin_unlock_irqrestore(&card->ip_lock, flags);
341 }
342
343 /*
344  * Add/remove address to/from card's ip list, i.e. try to add or remove
345  * reference to/from an IP address that is already registered on the card.
346  * Returns:
347  *      0  address was on card and its reference count has been adjusted,
348  *         but is still > 0, so nothing has to be done
349  *         also returns 0 if card was not on card and the todo was to delete
350  *         the address -> there is also nothing to be done
351  *      1  address was not on card and the todo is to add it to the card's ip
352  *         list
353  *      -1 address was on card and its reference count has been decremented
354  *         to <= 0 by the todo -> address must be removed from card
355  */
356 static int __qeth_l3_ref_ip_on_card(struct qeth_card *card,
357                 struct qeth_ipaddr *todo, struct qeth_ipaddr **__addr)
358 {
359         struct qeth_ipaddr *addr;
360         int found = 0;
361
362         list_for_each_entry(addr, &card->ip_list, entry) {
363                 if ((addr->proto == QETH_PROT_IPV4) &&
364                     (todo->proto == QETH_PROT_IPV4) &&
365                     (addr->type == todo->type) &&
366                     (addr->u.a4.addr == todo->u.a4.addr) &&
367                     (addr->u.a4.mask == todo->u.a4.mask)) {
368                         found = 1;
369                         break;
370                 }
371                 if ((addr->proto == QETH_PROT_IPV6) &&
372                     (todo->proto == QETH_PROT_IPV6) &&
373                     (addr->type == todo->type) &&
374                     (addr->u.a6.pfxlen == todo->u.a6.pfxlen) &&
375                     (memcmp(&addr->u.a6.addr, &todo->u.a6.addr,
376                             sizeof(struct in6_addr)) == 0)) {
377                         found = 1;
378                         break;
379                 }
380         }
381         if (found) {
382                 addr->users += todo->users;
383                 if (addr->users <= 0) {
384                         *__addr = addr;
385                         return -1;
386                 } else {
387                         /* for VIPA and RXIP limit refcount to 1 */
388                         if (addr->type != QETH_IP_TYPE_NORMAL)
389                                 addr->users = 1;
390                         return 0;
391                 }
392         }
393         if (todo->users > 0) {
394                 /* for VIPA and RXIP limit refcount to 1 */
395                 if (todo->type != QETH_IP_TYPE_NORMAL)
396                         todo->users = 1;
397                 return 1;
398         } else
399                 return 0;
400 }
401
402 static void __qeth_l3_delete_all_mc(struct qeth_card *card,
403                                         unsigned long *flags)
404 {
405         struct list_head fail_list;
406         struct qeth_ipaddr *addr, *tmp;
407         int rc;
408
409         INIT_LIST_HEAD(&fail_list);
410 again:
411         list_for_each_entry_safe(addr, tmp, &card->ip_list, entry) {
412                 if (addr->is_multicast) {
413                         list_del(&addr->entry);
414                         spin_unlock_irqrestore(&card->ip_lock, *flags);
415                         rc = qeth_l3_deregister_addr_entry(card, addr);
416                         spin_lock_irqsave(&card->ip_lock, *flags);
417                         if (!rc || (rc == IPA_RC_MC_ADDR_NOT_FOUND))
418                                 kfree(addr);
419                         else
420                                 list_add_tail(&addr->entry, &fail_list);
421                         goto again;
422                 }
423         }
424         list_splice(&fail_list, &card->ip_list);
425 }
426
427 void qeth_l3_set_ip_addr_list(struct qeth_card *card)
428 {
429         struct list_head *tbd_list;
430         struct qeth_ipaddr *todo, *addr;
431         unsigned long flags;
432         int rc;
433
434         QETH_CARD_TEXT(card, 2, "sdiplist");
435         QETH_CARD_HEX(card, 2, &card, sizeof(void *));
436
437         if ((card->state != CARD_STATE_UP &&
438              card->state != CARD_STATE_SOFTSETUP) || card->options.sniffer) {
439                 return;
440         }
441
442         spin_lock_irqsave(&card->ip_lock, flags);
443         tbd_list = card->ip_tbd_list;
444         card->ip_tbd_list = kzalloc(sizeof(struct list_head), GFP_ATOMIC);
445         if (!card->ip_tbd_list) {
446                 QETH_CARD_TEXT(card, 0, "silnomem");
447                 card->ip_tbd_list = tbd_list;
448                 spin_unlock_irqrestore(&card->ip_lock, flags);
449                 return;
450         } else
451                 INIT_LIST_HEAD(card->ip_tbd_list);
452
453         while (!list_empty(tbd_list)) {
454                 todo = list_entry(tbd_list->next, struct qeth_ipaddr, entry);
455                 list_del(&todo->entry);
456                 if (todo->type == QETH_IP_TYPE_DEL_ALL_MC) {
457                         __qeth_l3_delete_all_mc(card, &flags);
458                         kfree(todo);
459                         continue;
460                 }
461                 rc = __qeth_l3_ref_ip_on_card(card, todo, &addr);
462                 if (rc == 0) {
463                         /* nothing to be done; only adjusted refcount */
464                         kfree(todo);
465                 } else if (rc == 1) {
466                         /* new entry to be added to on-card list */
467                         spin_unlock_irqrestore(&card->ip_lock, flags);
468                         rc = qeth_l3_register_addr_entry(card, todo);
469                         spin_lock_irqsave(&card->ip_lock, flags);
470                         if (!rc || (rc == IPA_RC_LAN_OFFLINE))
471                                 list_add_tail(&todo->entry, &card->ip_list);
472                         else
473                                 kfree(todo);
474                 } else if (rc == -1) {
475                         /* on-card entry to be removed */
476                         list_del_init(&addr->entry);
477                         spin_unlock_irqrestore(&card->ip_lock, flags);
478                         rc = qeth_l3_deregister_addr_entry(card, addr);
479                         spin_lock_irqsave(&card->ip_lock, flags);
480                         if (!rc || (rc == IPA_RC_IP_ADDRESS_NOT_DEFINED))
481                                 kfree(addr);
482                         else
483                                 list_add_tail(&addr->entry, &card->ip_list);
484                         kfree(todo);
485                 }
486         }
487         spin_unlock_irqrestore(&card->ip_lock, flags);
488         kfree(tbd_list);
489 }
490
491 static void qeth_l3_clear_ip_list(struct qeth_card *card, int recover)
492 {
493         struct qeth_ipaddr *addr, *tmp;
494         unsigned long flags;
495
496         QETH_CARD_TEXT(card, 4, "clearip");
497         if (recover && card->options.sniffer)
498                 return;
499         spin_lock_irqsave(&card->ip_lock, flags);
500         /* clear todo list */
501         list_for_each_entry_safe(addr, tmp, card->ip_tbd_list, entry) {
502                 list_del(&addr->entry);
503                 kfree(addr);
504         }
505
506         while (!list_empty(&card->ip_list)) {
507                 addr = list_entry(card->ip_list.next,
508                                   struct qeth_ipaddr, entry);
509                 list_del_init(&addr->entry);
510                 if (!recover || addr->is_multicast) {
511                         kfree(addr);
512                         continue;
513                 }
514                 list_add_tail(&addr->entry, card->ip_tbd_list);
515         }
516         spin_unlock_irqrestore(&card->ip_lock, flags);
517 }
518
519 static int qeth_l3_address_exists_in_list(struct list_head *list,
520                 struct qeth_ipaddr *addr, int same_type)
521 {
522         struct qeth_ipaddr *tmp;
523
524         list_for_each_entry(tmp, list, entry) {
525                 if ((tmp->proto == QETH_PROT_IPV4) &&
526                     (addr->proto == QETH_PROT_IPV4) &&
527                     ((same_type && (tmp->type == addr->type)) ||
528                     (!same_type && (tmp->type != addr->type))) &&
529                     (tmp->u.a4.addr == addr->u.a4.addr))
530                         return 1;
531
532                 if ((tmp->proto == QETH_PROT_IPV6) &&
533                     (addr->proto == QETH_PROT_IPV6) &&
534                     ((same_type && (tmp->type == addr->type)) ||
535                     (!same_type && (tmp->type != addr->type))) &&
536                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
537                             sizeof(struct in6_addr)) == 0))
538                         return 1;
539
540         }
541         return 0;
542 }
543
544 static int qeth_l3_send_setdelmc(struct qeth_card *card,
545                         struct qeth_ipaddr *addr, int ipacmd)
546 {
547         int rc;
548         struct qeth_cmd_buffer *iob;
549         struct qeth_ipa_cmd *cmd;
550
551         QETH_CARD_TEXT(card, 4, "setdelmc");
552
553         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
554         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
555         memcpy(&cmd->data.setdelipm.mac, addr->mac, OSA_ADDR_LEN);
556         if (addr->proto == QETH_PROT_IPV6)
557                 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
558                        sizeof(struct in6_addr));
559         else
560                 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
561
562         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
563
564         return rc;
565 }
566
567 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
568 {
569         int i, j;
570         for (i = 0; i < 16; i++) {
571                 j = (len) - (i * 8);
572                 if (j >= 8)
573                         netmask[i] = 0xff;
574                 else if (j > 0)
575                         netmask[i] = (u8)(0xFF00 >> j);
576                 else
577                         netmask[i] = 0;
578         }
579 }
580
581 static int qeth_l3_send_setdelip(struct qeth_card *card,
582                 struct qeth_ipaddr *addr, int ipacmd, unsigned int flags)
583 {
584         int rc;
585         struct qeth_cmd_buffer *iob;
586         struct qeth_ipa_cmd *cmd;
587         __u8 netmask[16];
588
589         QETH_CARD_TEXT(card, 4, "setdelip");
590         QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
591
592         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
593         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
594         if (addr->proto == QETH_PROT_IPV6) {
595                 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
596                        sizeof(struct in6_addr));
597                 qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
598                 memcpy(cmd->data.setdelip6.mask, netmask,
599                        sizeof(struct in6_addr));
600                 cmd->data.setdelip6.flags = flags;
601         } else {
602                 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
603                 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
604                 cmd->data.setdelip4.flags = flags;
605         }
606
607         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
608
609         return rc;
610 }
611
612 static int qeth_l3_send_setrouting(struct qeth_card *card,
613         enum qeth_routing_types type, enum qeth_prot_versions prot)
614 {
615         int rc;
616         struct qeth_ipa_cmd *cmd;
617         struct qeth_cmd_buffer *iob;
618
619         QETH_CARD_TEXT(card, 4, "setroutg");
620         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
621         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
622         cmd->data.setrtg.type = (type);
623         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
624
625         return rc;
626 }
627
628 static void qeth_l3_correct_routing_type(struct qeth_card *card,
629                 enum qeth_routing_types *type, enum qeth_prot_versions prot)
630 {
631         if (card->info.type == QETH_CARD_TYPE_IQD) {
632                 switch (*type) {
633                 case NO_ROUTER:
634                 case PRIMARY_CONNECTOR:
635                 case SECONDARY_CONNECTOR:
636                 case MULTICAST_ROUTER:
637                         return;
638                 default:
639                         goto out_inval;
640                 }
641         } else {
642                 switch (*type) {
643                 case NO_ROUTER:
644                 case PRIMARY_ROUTER:
645                 case SECONDARY_ROUTER:
646                         return;
647                 case MULTICAST_ROUTER:
648                         if (qeth_is_ipafunc_supported(card, prot,
649                                                       IPA_OSA_MC_ROUTER))
650                                 return;
651                 default:
652                         goto out_inval;
653                 }
654         }
655 out_inval:
656         *type = NO_ROUTER;
657 }
658
659 int qeth_l3_setrouting_v4(struct qeth_card *card)
660 {
661         int rc;
662
663         QETH_CARD_TEXT(card, 3, "setrtg4");
664
665         qeth_l3_correct_routing_type(card, &card->options.route4.type,
666                                   QETH_PROT_IPV4);
667
668         rc = qeth_l3_send_setrouting(card, card->options.route4.type,
669                                   QETH_PROT_IPV4);
670         if (rc) {
671                 card->options.route4.type = NO_ROUTER;
672                 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
673                         " on %s. Type set to 'no router'.\n", rc,
674                         QETH_CARD_IFNAME(card));
675         }
676         return rc;
677 }
678
679 int qeth_l3_setrouting_v6(struct qeth_card *card)
680 {
681         int rc = 0;
682
683         QETH_CARD_TEXT(card, 3, "setrtg6");
684 #ifdef CONFIG_QETH_IPV6
685
686         if (!qeth_is_supported(card, IPA_IPV6))
687                 return 0;
688         qeth_l3_correct_routing_type(card, &card->options.route6.type,
689                                   QETH_PROT_IPV6);
690
691         rc = qeth_l3_send_setrouting(card, card->options.route6.type,
692                                   QETH_PROT_IPV6);
693         if (rc) {
694                 card->options.route6.type = NO_ROUTER;
695                 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
696                         " on %s. Type set to 'no router'.\n", rc,
697                         QETH_CARD_IFNAME(card));
698         }
699 #endif
700         return rc;
701 }
702
703 /*
704  * IP address takeover related functions
705  */
706 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
707 {
708
709         struct qeth_ipato_entry *ipatoe, *tmp;
710         unsigned long flags;
711
712         spin_lock_irqsave(&card->ip_lock, flags);
713         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
714                 list_del(&ipatoe->entry);
715                 kfree(ipatoe);
716         }
717         spin_unlock_irqrestore(&card->ip_lock, flags);
718 }
719
720 int qeth_l3_add_ipato_entry(struct qeth_card *card,
721                                 struct qeth_ipato_entry *new)
722 {
723         struct qeth_ipato_entry *ipatoe;
724         unsigned long flags;
725         int rc = 0;
726
727         QETH_CARD_TEXT(card, 2, "addipato");
728         spin_lock_irqsave(&card->ip_lock, flags);
729         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
730                 if (ipatoe->proto != new->proto)
731                         continue;
732                 if (!memcmp(ipatoe->addr, new->addr,
733                             (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
734                     (ipatoe->mask_bits == new->mask_bits)) {
735                         rc = -EEXIST;
736                         break;
737                 }
738         }
739         if (!rc)
740                 list_add_tail(&new->entry, &card->ipato.entries);
741
742         spin_unlock_irqrestore(&card->ip_lock, flags);
743         return rc;
744 }
745
746 void qeth_l3_del_ipato_entry(struct qeth_card *card,
747                 enum qeth_prot_versions proto, u8 *addr, int mask_bits)
748 {
749         struct qeth_ipato_entry *ipatoe, *tmp;
750         unsigned long flags;
751
752         QETH_CARD_TEXT(card, 2, "delipato");
753         spin_lock_irqsave(&card->ip_lock, flags);
754         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
755                 if (ipatoe->proto != proto)
756                         continue;
757                 if (!memcmp(ipatoe->addr, addr,
758                             (proto == QETH_PROT_IPV4)? 4:16) &&
759                     (ipatoe->mask_bits == mask_bits)) {
760                         list_del(&ipatoe->entry);
761                         kfree(ipatoe);
762                 }
763         }
764         spin_unlock_irqrestore(&card->ip_lock, flags);
765 }
766
767 /*
768  * VIPA related functions
769  */
770 int qeth_l3_add_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
771               const u8 *addr)
772 {
773         struct qeth_ipaddr *ipaddr;
774         unsigned long flags;
775         int rc = 0;
776
777         ipaddr = qeth_l3_get_addr_buffer(proto);
778         if (ipaddr) {
779                 if (proto == QETH_PROT_IPV4) {
780                         QETH_CARD_TEXT(card, 2, "addvipa4");
781                         memcpy(&ipaddr->u.a4.addr, addr, 4);
782                         ipaddr->u.a4.mask = 0;
783                 } else if (proto == QETH_PROT_IPV6) {
784                         QETH_CARD_TEXT(card, 2, "addvipa6");
785                         memcpy(&ipaddr->u.a6.addr, addr, 16);
786                         ipaddr->u.a6.pfxlen = 0;
787                 }
788                 ipaddr->type = QETH_IP_TYPE_VIPA;
789                 ipaddr->set_flags = QETH_IPA_SETIP_VIPA_FLAG;
790                 ipaddr->del_flags = QETH_IPA_DELIP_VIPA_FLAG;
791         } else
792                 return -ENOMEM;
793         spin_lock_irqsave(&card->ip_lock, flags);
794         if (qeth_l3_address_exists_in_list(&card->ip_list, ipaddr, 0) ||
795             qeth_l3_address_exists_in_list(card->ip_tbd_list, ipaddr, 0))
796                 rc = -EEXIST;
797         spin_unlock_irqrestore(&card->ip_lock, flags);
798         if (rc) {
799                 return rc;
800         }
801         if (!qeth_l3_add_ip(card, ipaddr))
802                 kfree(ipaddr);
803         qeth_l3_set_ip_addr_list(card);
804         return rc;
805 }
806
807 void qeth_l3_del_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
808               const u8 *addr)
809 {
810         struct qeth_ipaddr *ipaddr;
811
812         ipaddr = qeth_l3_get_addr_buffer(proto);
813         if (ipaddr) {
814                 if (proto == QETH_PROT_IPV4) {
815                         QETH_CARD_TEXT(card, 2, "delvipa4");
816                         memcpy(&ipaddr->u.a4.addr, addr, 4);
817                         ipaddr->u.a4.mask = 0;
818                 } else if (proto == QETH_PROT_IPV6) {
819                         QETH_CARD_TEXT(card, 2, "delvipa6");
820                         memcpy(&ipaddr->u.a6.addr, addr, 16);
821                         ipaddr->u.a6.pfxlen = 0;
822                 }
823                 ipaddr->type = QETH_IP_TYPE_VIPA;
824         } else
825                 return;
826         if (!qeth_l3_delete_ip(card, ipaddr))
827                 kfree(ipaddr);
828         qeth_l3_set_ip_addr_list(card);
829 }
830
831 /*
832  * proxy ARP related functions
833  */
834 int qeth_l3_add_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
835               const u8 *addr)
836 {
837         struct qeth_ipaddr *ipaddr;
838         unsigned long flags;
839         int rc = 0;
840
841         ipaddr = qeth_l3_get_addr_buffer(proto);
842         if (ipaddr) {
843                 if (proto == QETH_PROT_IPV4) {
844                         QETH_CARD_TEXT(card, 2, "addrxip4");
845                         memcpy(&ipaddr->u.a4.addr, addr, 4);
846                         ipaddr->u.a4.mask = 0;
847                 } else if (proto == QETH_PROT_IPV6) {
848                         QETH_CARD_TEXT(card, 2, "addrxip6");
849                         memcpy(&ipaddr->u.a6.addr, addr, 16);
850                         ipaddr->u.a6.pfxlen = 0;
851                 }
852                 ipaddr->type = QETH_IP_TYPE_RXIP;
853                 ipaddr->set_flags = QETH_IPA_SETIP_TAKEOVER_FLAG;
854                 ipaddr->del_flags = 0;
855         } else
856                 return -ENOMEM;
857         spin_lock_irqsave(&card->ip_lock, flags);
858         if (qeth_l3_address_exists_in_list(&card->ip_list, ipaddr, 0) ||
859             qeth_l3_address_exists_in_list(card->ip_tbd_list, ipaddr, 0))
860                 rc = -EEXIST;
861         spin_unlock_irqrestore(&card->ip_lock, flags);
862         if (rc) {
863                 return rc;
864         }
865         if (!qeth_l3_add_ip(card, ipaddr))
866                 kfree(ipaddr);
867         qeth_l3_set_ip_addr_list(card);
868         return 0;
869 }
870
871 void qeth_l3_del_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
872                         const u8 *addr)
873 {
874         struct qeth_ipaddr *ipaddr;
875
876         ipaddr = qeth_l3_get_addr_buffer(proto);
877         if (ipaddr) {
878                 if (proto == QETH_PROT_IPV4) {
879                         QETH_CARD_TEXT(card, 2, "addrxip4");
880                         memcpy(&ipaddr->u.a4.addr, addr, 4);
881                         ipaddr->u.a4.mask = 0;
882                 } else if (proto == QETH_PROT_IPV6) {
883                         QETH_CARD_TEXT(card, 2, "addrxip6");
884                         memcpy(&ipaddr->u.a6.addr, addr, 16);
885                         ipaddr->u.a6.pfxlen = 0;
886                 }
887                 ipaddr->type = QETH_IP_TYPE_RXIP;
888         } else
889                 return;
890         if (!qeth_l3_delete_ip(card, ipaddr))
891                 kfree(ipaddr);
892         qeth_l3_set_ip_addr_list(card);
893 }
894
895 static int qeth_l3_register_addr_entry(struct qeth_card *card,
896                                 struct qeth_ipaddr *addr)
897 {
898         char buf[50];
899         int rc = 0;
900         int cnt = 3;
901
902         if (addr->proto == QETH_PROT_IPV4) {
903                 QETH_CARD_TEXT(card, 2, "setaddr4");
904                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
905         } else if (addr->proto == QETH_PROT_IPV6) {
906                 QETH_CARD_TEXT(card, 2, "setaddr6");
907                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
908                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
909         } else {
910                 QETH_CARD_TEXT(card, 2, "setaddr?");
911                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
912         }
913         do {
914                 if (addr->is_multicast)
915                         rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
916                 else
917                         rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP,
918                                         addr->set_flags);
919                 if (rc)
920                         QETH_CARD_TEXT(card, 2, "failed");
921         } while ((--cnt > 0) && rc);
922         if (rc) {
923                 QETH_CARD_TEXT(card, 2, "FAILED");
924                 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
925                 dev_warn(&card->gdev->dev,
926                         "Registering IP address %s failed\n", buf);
927         }
928         return rc;
929 }
930
931 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
932                                                 struct qeth_ipaddr *addr)
933 {
934         int rc = 0;
935
936         if (addr->proto == QETH_PROT_IPV4) {
937                 QETH_CARD_TEXT(card, 2, "deladdr4");
938                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
939         } else if (addr->proto == QETH_PROT_IPV6) {
940                 QETH_CARD_TEXT(card, 2, "deladdr6");
941                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
942                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
943         } else {
944                 QETH_CARD_TEXT(card, 2, "deladdr?");
945                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
946         }
947         if (addr->is_multicast)
948                 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
949         else
950                 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP,
951                                         addr->del_flags);
952         if (rc)
953                 QETH_CARD_TEXT(card, 2, "failed");
954
955         return rc;
956 }
957
958 static inline u8 qeth_l3_get_qeth_hdr_flags4(int cast_type)
959 {
960         if (cast_type == RTN_MULTICAST)
961                 return QETH_CAST_MULTICAST;
962         if (cast_type == RTN_BROADCAST)
963                 return QETH_CAST_BROADCAST;
964         return QETH_CAST_UNICAST;
965 }
966
967 static inline u8 qeth_l3_get_qeth_hdr_flags6(int cast_type)
968 {
969         u8 ct = QETH_HDR_PASSTHRU | QETH_HDR_IPV6;
970         if (cast_type == RTN_MULTICAST)
971                 return ct | QETH_CAST_MULTICAST;
972         if (cast_type == RTN_ANYCAST)
973                 return ct | QETH_CAST_ANYCAST;
974         if (cast_type == RTN_BROADCAST)
975                 return ct | QETH_CAST_BROADCAST;
976         return ct | QETH_CAST_UNICAST;
977 }
978
979 static int qeth_l3_setadapter_parms(struct qeth_card *card)
980 {
981         int rc;
982
983         QETH_DBF_TEXT(SETUP, 2, "setadprm");
984
985         if (!qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
986                 dev_info(&card->gdev->dev,
987                         "set adapter parameters not supported.\n");
988                 QETH_DBF_TEXT(SETUP, 2, " notsupp");
989                 return 0;
990         }
991         rc = qeth_query_setadapterparms(card);
992         if (rc) {
993                 QETH_DBF_MESSAGE(2, "%s couldn't set adapter parameters: "
994                         "0x%x\n", dev_name(&card->gdev->dev), rc);
995                 return rc;
996         }
997         if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
998                 rc = qeth_setadpparms_change_macaddr(card);
999                 if (rc)
1000                         dev_warn(&card->gdev->dev, "Reading the adapter MAC"
1001                                 " address failed\n");
1002         }
1003
1004         return rc;
1005 }
1006
1007 static int qeth_l3_default_setassparms_cb(struct qeth_card *card,
1008                         struct qeth_reply *reply, unsigned long data)
1009 {
1010         struct qeth_ipa_cmd *cmd;
1011
1012         QETH_CARD_TEXT(card, 4, "defadpcb");
1013
1014         cmd = (struct qeth_ipa_cmd *) data;
1015         if (cmd->hdr.return_code == 0) {
1016                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1017                 if (cmd->hdr.prot_version == QETH_PROT_IPV4)
1018                         card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
1019                 if (cmd->hdr.prot_version == QETH_PROT_IPV6)
1020                         card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
1021         }
1022         if (cmd->data.setassparms.hdr.assist_no == IPA_INBOUND_CHECKSUM &&
1023             cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
1024                 card->info.csum_mask = cmd->data.setassparms.data.flags_32bit;
1025                 QETH_CARD_TEXT_(card, 3, "csum:%d", card->info.csum_mask);
1026         }
1027         if (cmd->data.setassparms.hdr.assist_no == IPA_OUTBOUND_CHECKSUM &&
1028             cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
1029                 card->info.tx_csum_mask =
1030                         cmd->data.setassparms.data.flags_32bit;
1031                 QETH_CARD_TEXT_(card, 3, "tcsu:%d", card->info.tx_csum_mask);
1032         }
1033
1034         return 0;
1035 }
1036
1037 static struct qeth_cmd_buffer *qeth_l3_get_setassparms_cmd(
1038         struct qeth_card *card, enum qeth_ipa_funcs ipa_func, __u16 cmd_code,
1039         __u16 len, enum qeth_prot_versions prot)
1040 {
1041         struct qeth_cmd_buffer *iob;
1042         struct qeth_ipa_cmd *cmd;
1043
1044         QETH_CARD_TEXT(card, 4, "getasscm");
1045         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETASSPARMS, prot);
1046
1047         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1048         cmd->data.setassparms.hdr.assist_no = ipa_func;
1049         cmd->data.setassparms.hdr.length = 8 + len;
1050         cmd->data.setassparms.hdr.command_code = cmd_code;
1051         cmd->data.setassparms.hdr.return_code = 0;
1052         cmd->data.setassparms.hdr.seq_no = 0;
1053
1054         return iob;
1055 }
1056
1057 static int qeth_l3_send_setassparms(struct qeth_card *card,
1058         struct qeth_cmd_buffer *iob, __u16 len, long data,
1059         int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1060                 unsigned long),
1061         void *reply_param)
1062 {
1063         int rc;
1064         struct qeth_ipa_cmd *cmd;
1065
1066         QETH_CARD_TEXT(card, 4, "sendassp");
1067
1068         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1069         if (len <= sizeof(__u32))
1070                 cmd->data.setassparms.data.flags_32bit = (__u32) data;
1071         else   /* (len > sizeof(__u32)) */
1072                 memcpy(&cmd->data.setassparms.data, (void *) data, len);
1073
1074         rc = qeth_send_ipa_cmd(card, iob, reply_cb, reply_param);
1075         return rc;
1076 }
1077
1078 #ifdef CONFIG_QETH_IPV6
1079 static int qeth_l3_send_simple_setassparms_ipv6(struct qeth_card *card,
1080                 enum qeth_ipa_funcs ipa_func, __u16 cmd_code)
1081 {
1082         int rc;
1083         struct qeth_cmd_buffer *iob;
1084
1085         QETH_CARD_TEXT(card, 4, "simassp6");
1086         iob = qeth_l3_get_setassparms_cmd(card, ipa_func, cmd_code,
1087                                        0, QETH_PROT_IPV6);
1088         rc = qeth_l3_send_setassparms(card, iob, 0, 0,
1089                                    qeth_l3_default_setassparms_cb, NULL);
1090         return rc;
1091 }
1092 #endif
1093
1094 static int qeth_l3_send_simple_setassparms(struct qeth_card *card,
1095                 enum qeth_ipa_funcs ipa_func, __u16 cmd_code, long data)
1096 {
1097         int rc;
1098         int length = 0;
1099         struct qeth_cmd_buffer *iob;
1100
1101         QETH_CARD_TEXT(card, 4, "simassp4");
1102         if (data)
1103                 length = sizeof(__u32);
1104         iob = qeth_l3_get_setassparms_cmd(card, ipa_func, cmd_code,
1105                                        length, QETH_PROT_IPV4);
1106         rc = qeth_l3_send_setassparms(card, iob, length, data,
1107                                    qeth_l3_default_setassparms_cb, NULL);
1108         return rc;
1109 }
1110
1111 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
1112 {
1113         int rc;
1114
1115         QETH_CARD_TEXT(card, 3, "ipaarp");
1116
1117         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1118                 dev_info(&card->gdev->dev,
1119                         "ARP processing not supported on %s!\n",
1120                         QETH_CARD_IFNAME(card));
1121                 return 0;
1122         }
1123         rc = qeth_l3_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1124                                         IPA_CMD_ASS_START, 0);
1125         if (rc) {
1126                 dev_warn(&card->gdev->dev,
1127                         "Starting ARP processing support for %s failed\n",
1128                         QETH_CARD_IFNAME(card));
1129         }
1130         return rc;
1131 }
1132
1133 static int qeth_l3_start_ipa_ip_fragmentation(struct qeth_card *card)
1134 {
1135         int rc;
1136
1137         QETH_CARD_TEXT(card, 3, "ipaipfrg");
1138
1139         if (!qeth_is_supported(card, IPA_IP_FRAGMENTATION)) {
1140                 dev_info(&card->gdev->dev,
1141                         "Hardware IP fragmentation not supported on %s\n",
1142                         QETH_CARD_IFNAME(card));
1143                 return  -EOPNOTSUPP;
1144         }
1145
1146         rc = qeth_l3_send_simple_setassparms(card, IPA_IP_FRAGMENTATION,
1147                                           IPA_CMD_ASS_START, 0);
1148         if (rc) {
1149                 dev_warn(&card->gdev->dev,
1150                         "Starting IP fragmentation support for %s failed\n",
1151                         QETH_CARD_IFNAME(card));
1152         } else
1153                 dev_info(&card->gdev->dev,
1154                         "Hardware IP fragmentation enabled \n");
1155         return rc;
1156 }
1157
1158 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
1159 {
1160         int rc;
1161
1162         QETH_CARD_TEXT(card, 3, "stsrcmac");
1163
1164         if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
1165                 dev_info(&card->gdev->dev,
1166                         "Inbound source MAC-address not supported on %s\n",
1167                         QETH_CARD_IFNAME(card));
1168                 return -EOPNOTSUPP;
1169         }
1170
1171         rc = qeth_l3_send_simple_setassparms(card, IPA_SOURCE_MAC,
1172                                           IPA_CMD_ASS_START, 0);
1173         if (rc)
1174                 dev_warn(&card->gdev->dev,
1175                         "Starting source MAC-address support for %s failed\n",
1176                         QETH_CARD_IFNAME(card));
1177         return rc;
1178 }
1179
1180 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
1181 {
1182         int rc = 0;
1183
1184         QETH_CARD_TEXT(card, 3, "strtvlan");
1185
1186         if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
1187                 dev_info(&card->gdev->dev,
1188                         "VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
1189                 return -EOPNOTSUPP;
1190         }
1191
1192         rc = qeth_l3_send_simple_setassparms(card, IPA_VLAN_PRIO,
1193                                           IPA_CMD_ASS_START, 0);
1194         if (rc) {
1195                 dev_warn(&card->gdev->dev,
1196                         "Starting VLAN support for %s failed\n",
1197                         QETH_CARD_IFNAME(card));
1198         } else {
1199                 dev_info(&card->gdev->dev, "VLAN enabled\n");
1200         }
1201         return rc;
1202 }
1203
1204 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
1205 {
1206         int rc;
1207
1208         QETH_CARD_TEXT(card, 3, "stmcast");
1209
1210         if (!qeth_is_supported(card, IPA_MULTICASTING)) {
1211                 dev_info(&card->gdev->dev,
1212                         "Multicast not supported on %s\n",
1213                         QETH_CARD_IFNAME(card));
1214                 return -EOPNOTSUPP;
1215         }
1216
1217         rc = qeth_l3_send_simple_setassparms(card, IPA_MULTICASTING,
1218                                           IPA_CMD_ASS_START, 0);
1219         if (rc) {
1220                 dev_warn(&card->gdev->dev,
1221                         "Starting multicast support for %s failed\n",
1222                         QETH_CARD_IFNAME(card));
1223         } else {
1224                 dev_info(&card->gdev->dev, "Multicast enabled\n");
1225                 card->dev->flags |= IFF_MULTICAST;
1226         }
1227         return rc;
1228 }
1229
1230 #ifdef CONFIG_QETH_IPV6
1231 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
1232 {
1233         int rc;
1234
1235         QETH_CARD_TEXT(card, 3, "softipv6");
1236
1237         if (card->info.type == QETH_CARD_TYPE_IQD)
1238                 goto out;
1239
1240         rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
1241         if (rc) {
1242                 dev_err(&card->gdev->dev,
1243                         "Activating IPv6 support for %s failed\n",
1244                         QETH_CARD_IFNAME(card));
1245                 return rc;
1246         }
1247         rc = qeth_l3_send_simple_setassparms(card, IPA_IPV6,
1248                                           IPA_CMD_ASS_START, 3);
1249         if (rc) {
1250                 dev_err(&card->gdev->dev,
1251                         "Activating IPv6 support for %s failed\n",
1252                         QETH_CARD_IFNAME(card));
1253                 return rc;
1254         }
1255         rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_IPV6,
1256                                                IPA_CMD_ASS_START);
1257         if (rc) {
1258                 dev_err(&card->gdev->dev,
1259                         "Activating IPv6 support for %s failed\n",
1260                          QETH_CARD_IFNAME(card));
1261                 return rc;
1262         }
1263         rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_PASSTHRU,
1264                                                IPA_CMD_ASS_START);
1265         if (rc) {
1266                 dev_warn(&card->gdev->dev,
1267                         "Enabling the passthrough mode for %s failed\n",
1268                         QETH_CARD_IFNAME(card));
1269                 return rc;
1270         }
1271 out:
1272         dev_info(&card->gdev->dev, "IPV6 enabled\n");
1273         return 0;
1274 }
1275 #endif
1276
1277 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
1278 {
1279         int rc = 0;
1280
1281         QETH_CARD_TEXT(card, 3, "strtipv6");
1282
1283         if (!qeth_is_supported(card, IPA_IPV6)) {
1284                 dev_info(&card->gdev->dev,
1285                         "IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
1286                 return 0;
1287         }
1288 #ifdef CONFIG_QETH_IPV6
1289         rc = qeth_l3_softsetup_ipv6(card);
1290 #endif
1291         return rc ;
1292 }
1293
1294 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
1295 {
1296         int rc;
1297
1298         QETH_CARD_TEXT(card, 3, "stbrdcst");
1299         card->info.broadcast_capable = 0;
1300         if (!qeth_is_supported(card, IPA_FILTERING)) {
1301                 dev_info(&card->gdev->dev,
1302                         "Broadcast not supported on %s\n",
1303                         QETH_CARD_IFNAME(card));
1304                 rc = -EOPNOTSUPP;
1305                 goto out;
1306         }
1307         rc = qeth_l3_send_simple_setassparms(card, IPA_FILTERING,
1308                                           IPA_CMD_ASS_START, 0);
1309         if (rc) {
1310                 dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
1311                         "%s failed\n", QETH_CARD_IFNAME(card));
1312                 goto out;
1313         }
1314
1315         rc = qeth_l3_send_simple_setassparms(card, IPA_FILTERING,
1316                                           IPA_CMD_ASS_CONFIGURE, 1);
1317         if (rc) {
1318                 dev_warn(&card->gdev->dev,
1319                         "Setting up broadcast filtering for %s failed\n",
1320                         QETH_CARD_IFNAME(card));
1321                 goto out;
1322         }
1323         card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
1324         dev_info(&card->gdev->dev, "Broadcast enabled\n");
1325         rc = qeth_l3_send_simple_setassparms(card, IPA_FILTERING,
1326                                           IPA_CMD_ASS_ENABLE, 1);
1327         if (rc) {
1328                 dev_warn(&card->gdev->dev, "Setting up broadcast echo "
1329                         "filtering for %s failed\n", QETH_CARD_IFNAME(card));
1330                 goto out;
1331         }
1332         card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
1333 out:
1334         if (card->info.broadcast_capable)
1335                 card->dev->flags |= IFF_BROADCAST;
1336         else
1337                 card->dev->flags &= ~IFF_BROADCAST;
1338         return rc;
1339 }
1340
1341 static int qeth_l3_send_checksum_command(struct qeth_card *card)
1342 {
1343         int rc;
1344
1345         rc = qeth_l3_send_simple_setassparms(card, IPA_INBOUND_CHECKSUM,
1346                                           IPA_CMD_ASS_START, 0);
1347         if (rc) {
1348                 dev_warn(&card->gdev->dev, "Starting HW checksumming for %s "
1349                         "failed, using SW checksumming\n",
1350                         QETH_CARD_IFNAME(card));
1351                 return rc;
1352         }
1353         rc = qeth_l3_send_simple_setassparms(card, IPA_INBOUND_CHECKSUM,
1354                                           IPA_CMD_ASS_ENABLE,
1355                                           card->info.csum_mask);
1356         if (rc) {
1357                 dev_warn(&card->gdev->dev, "Enabling HW checksumming for %s "
1358                         "failed, using SW checksumming\n",
1359                         QETH_CARD_IFNAME(card));
1360                 return rc;
1361         }
1362         return 0;
1363 }
1364
1365 static int qeth_l3_set_rx_csum(struct qeth_card *card, int on)
1366 {
1367         int rc = 0;
1368
1369         if (on) {
1370                 rc = qeth_l3_send_checksum_command(card);
1371                 if (rc)
1372                         return -EIO;
1373                 dev_info(&card->gdev->dev,
1374                         "HW Checksumming (inbound) enabled\n");
1375         } else {
1376                 rc = qeth_l3_send_simple_setassparms(card,
1377                         IPA_INBOUND_CHECKSUM, IPA_CMD_ASS_STOP, 0);
1378                 if (rc)
1379                         return -EIO;
1380         }
1381
1382         return 0;
1383 }
1384
1385 static int qeth_l3_start_ipa_checksum(struct qeth_card *card)
1386 {
1387         QETH_CARD_TEXT(card, 3, "strtcsum");
1388
1389         if (card->dev->features & NETIF_F_RXCSUM) {
1390                 rtnl_lock();
1391                 /* force set_features call */
1392                 card->dev->features &= ~NETIF_F_RXCSUM;
1393                 netdev_update_features(card->dev);
1394                 rtnl_unlock();
1395         }
1396         return 0;
1397 }
1398
1399 static int qeth_l3_start_ipa_tx_checksum(struct qeth_card *card)
1400 {
1401         int rc = 0;
1402
1403         if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
1404                 return rc;
1405         rc = qeth_l3_send_simple_setassparms(card, IPA_OUTBOUND_CHECKSUM,
1406                           IPA_CMD_ASS_START, 0);
1407         if (rc)
1408                 goto err_out;
1409         rc = qeth_l3_send_simple_setassparms(card, IPA_OUTBOUND_CHECKSUM,
1410                           IPA_CMD_ASS_ENABLE, card->info.tx_csum_mask);
1411         if (rc)
1412                 goto err_out;
1413         dev_info(&card->gdev->dev, "HW TX Checksumming enabled\n");
1414         return rc;
1415 err_out:
1416         dev_warn(&card->gdev->dev, "Enabling HW TX checksumming for %s "
1417                 "failed, using SW TX checksumming\n", QETH_CARD_IFNAME(card));
1418         return rc;
1419 }
1420
1421 static int qeth_l3_start_ipa_tso(struct qeth_card *card)
1422 {
1423         int rc;
1424
1425         QETH_CARD_TEXT(card, 3, "sttso");
1426
1427         if (!qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
1428                 dev_info(&card->gdev->dev,
1429                         "Outbound TSO not supported on %s\n",
1430                         QETH_CARD_IFNAME(card));
1431                 rc = -EOPNOTSUPP;
1432         } else {
1433                 rc = qeth_l3_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
1434                                                 IPA_CMD_ASS_START, 0);
1435                 if (rc)
1436                         dev_warn(&card->gdev->dev, "Starting outbound TCP "
1437                                 "segmentation offload for %s failed\n",
1438                                 QETH_CARD_IFNAME(card));
1439                 else
1440                         dev_info(&card->gdev->dev,
1441                                 "Outbound TSO enabled\n");
1442         }
1443         if (rc)
1444                 card->dev->features &= ~NETIF_F_TSO;
1445         return rc;
1446 }
1447
1448 static int qeth_l3_start_ipassists(struct qeth_card *card)
1449 {
1450         QETH_CARD_TEXT(card, 3, "strtipas");
1451
1452         qeth_set_access_ctrl_online(card);      /* go on*/
1453         qeth_l3_start_ipa_arp_processing(card); /* go on*/
1454         qeth_l3_start_ipa_ip_fragmentation(card);       /* go on*/
1455         qeth_l3_start_ipa_source_mac(card);     /* go on*/
1456         qeth_l3_start_ipa_vlan(card);           /* go on*/
1457         qeth_l3_start_ipa_multicast(card);              /* go on*/
1458         qeth_l3_start_ipa_ipv6(card);           /* go on*/
1459         qeth_l3_start_ipa_broadcast(card);              /* go on*/
1460         qeth_l3_start_ipa_checksum(card);               /* go on*/
1461         qeth_l3_start_ipa_tx_checksum(card);
1462         qeth_l3_start_ipa_tso(card);            /* go on*/
1463         return 0;
1464 }
1465
1466 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
1467                 struct qeth_reply *reply, unsigned long data)
1468 {
1469         struct qeth_ipa_cmd *cmd;
1470
1471         cmd = (struct qeth_ipa_cmd *) data;
1472         if (cmd->hdr.return_code == 0)
1473                 memcpy(card->dev->dev_addr,
1474                         cmd->data.create_destroy_addr.unique_id, ETH_ALEN);
1475         else
1476                 random_ether_addr(card->dev->dev_addr);
1477
1478         return 0;
1479 }
1480
1481 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
1482 {
1483         int rc = 0;
1484         struct qeth_cmd_buffer *iob;
1485         struct qeth_ipa_cmd *cmd;
1486
1487         QETH_DBF_TEXT(SETUP, 2, "hsrmac");
1488
1489         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1490                                      QETH_PROT_IPV6);
1491         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1492         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1493                         card->info.unique_id;
1494
1495         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
1496                                 NULL);
1497         return rc;
1498 }
1499
1500 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
1501                 struct qeth_reply *reply, unsigned long data)
1502 {
1503         struct qeth_ipa_cmd *cmd;
1504
1505         cmd = (struct qeth_ipa_cmd *) data;
1506         if (cmd->hdr.return_code == 0)
1507                 card->info.unique_id = *((__u16 *)
1508                                 &cmd->data.create_destroy_addr.unique_id[6]);
1509         else {
1510                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1511                                         UNIQUE_ID_NOT_BY_CARD;
1512                 dev_warn(&card->gdev->dev, "The network adapter failed to "
1513                         "generate a unique ID\n");
1514         }
1515         return 0;
1516 }
1517
1518 static int qeth_l3_get_unique_id(struct qeth_card *card)
1519 {
1520         int rc = 0;
1521         struct qeth_cmd_buffer *iob;
1522         struct qeth_ipa_cmd *cmd;
1523
1524         QETH_DBF_TEXT(SETUP, 2, "guniqeid");
1525
1526         if (!qeth_is_supported(card, IPA_IPV6)) {
1527                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1528                                         UNIQUE_ID_NOT_BY_CARD;
1529                 return 0;
1530         }
1531
1532         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1533                                      QETH_PROT_IPV6);
1534         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1535         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1536                         card->info.unique_id;
1537
1538         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
1539         return rc;
1540 }
1541
1542 static int
1543 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
1544                             unsigned long data)
1545 {
1546         struct qeth_ipa_cmd        *cmd;
1547         __u16 rc;
1548
1549         QETH_DBF_TEXT(SETUP, 2, "diastrcb");
1550
1551         cmd = (struct qeth_ipa_cmd *)data;
1552         rc = cmd->hdr.return_code;
1553         if (rc)
1554                 QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1555         switch (cmd->data.diagass.action) {
1556         case QETH_DIAGS_CMD_TRACE_QUERY:
1557                 break;
1558         case QETH_DIAGS_CMD_TRACE_DISABLE:
1559                 switch (rc) {
1560                 case 0:
1561                 case IPA_RC_INVALID_SUBCMD:
1562                         card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1563                         dev_info(&card->gdev->dev, "The HiperSockets network "
1564                                 "traffic analyzer is deactivated\n");
1565                         break;
1566                 default:
1567                         break;
1568                 }
1569                 break;
1570         case QETH_DIAGS_CMD_TRACE_ENABLE:
1571                 switch (rc) {
1572                 case 0:
1573                         card->info.promisc_mode = SET_PROMISC_MODE_ON;
1574                         dev_info(&card->gdev->dev, "The HiperSockets network "
1575                                 "traffic analyzer is activated\n");
1576                         break;
1577                 case IPA_RC_HARDWARE_AUTH_ERROR:
1578                         dev_warn(&card->gdev->dev, "The device is not "
1579                                 "authorized to run as a HiperSockets network "
1580                                 "traffic analyzer\n");
1581                         break;
1582                 case IPA_RC_TRACE_ALREADY_ACTIVE:
1583                         dev_warn(&card->gdev->dev, "A HiperSockets "
1584                                 "network traffic analyzer is already "
1585                                 "active in the HiperSockets LAN\n");
1586                         break;
1587                 default:
1588                         break;
1589                 }
1590                 break;
1591         default:
1592                 QETH_DBF_MESSAGE(2, "Unknown sniffer action (0x%04x) on %s\n",
1593                         cmd->data.diagass.action, QETH_CARD_IFNAME(card));
1594         }
1595
1596         return 0;
1597 }
1598
1599 static int
1600 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1601 {
1602         struct qeth_cmd_buffer *iob;
1603         struct qeth_ipa_cmd    *cmd;
1604
1605         QETH_DBF_TEXT(SETUP, 2, "diagtrac");
1606
1607         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
1608         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1609         cmd->data.diagass.subcmd_len = 16;
1610         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRACE;
1611         cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1612         cmd->data.diagass.action = diags_cmd;
1613         return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1614 }
1615
1616 static void qeth_l3_get_mac_for_ipm(__u32 ipm, char *mac,
1617                                 struct net_device *dev)
1618 {
1619         ip_eth_mc_map(ipm, mac);
1620 }
1621
1622 static void qeth_l3_add_mc(struct qeth_card *card, struct in_device *in4_dev)
1623 {
1624         struct qeth_ipaddr *ipm;
1625         struct ip_mc_list *im4;
1626         char buf[MAX_ADDR_LEN];
1627
1628         QETH_CARD_TEXT(card, 4, "addmc");
1629         for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
1630              im4 = rcu_dereference(im4->next_rcu)) {
1631                 qeth_l3_get_mac_for_ipm(im4->multiaddr, buf, in4_dev->dev);
1632                 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1633                 if (!ipm)
1634                         continue;
1635                 ipm->u.a4.addr = im4->multiaddr;
1636                 memcpy(ipm->mac, buf, OSA_ADDR_LEN);
1637                 ipm->is_multicast = 1;
1638                 if (!qeth_l3_add_ip(card, ipm))
1639                         kfree(ipm);
1640         }
1641 }
1642
1643 static void qeth_l3_add_vlan_mc(struct qeth_card *card)
1644 {
1645         struct in_device *in_dev;
1646         u16 vid;
1647
1648         QETH_CARD_TEXT(card, 4, "addmcvl");
1649         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1650                 return;
1651
1652         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1653                 struct net_device *netdev;
1654
1655                 rcu_read_lock();
1656                 netdev = __vlan_find_dev_deep(card->dev, vid);
1657                 rcu_read_unlock();
1658                 if (netdev == NULL ||
1659                     !(netdev->flags & IFF_UP))
1660                         continue;
1661                 in_dev = in_dev_get(netdev);
1662                 if (!in_dev)
1663                         continue;
1664                 rcu_read_lock();
1665                 qeth_l3_add_mc(card, in_dev);
1666                 rcu_read_unlock();
1667                 in_dev_put(in_dev);
1668         }
1669 }
1670
1671 static void qeth_l3_add_multicast_ipv4(struct qeth_card *card)
1672 {
1673         struct in_device *in4_dev;
1674
1675         QETH_CARD_TEXT(card, 4, "chkmcv4");
1676         in4_dev = in_dev_get(card->dev);
1677         if (in4_dev == NULL)
1678                 return;
1679         rcu_read_lock();
1680         qeth_l3_add_mc(card, in4_dev);
1681         qeth_l3_add_vlan_mc(card);
1682         rcu_read_unlock();
1683         in_dev_put(in4_dev);
1684 }
1685
1686 #ifdef CONFIG_QETH_IPV6
1687 static void qeth_l3_add_mc6(struct qeth_card *card, struct inet6_dev *in6_dev)
1688 {
1689         struct qeth_ipaddr *ipm;
1690         struct ifmcaddr6 *im6;
1691         char buf[MAX_ADDR_LEN];
1692
1693         QETH_CARD_TEXT(card, 4, "addmc6");
1694         for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1695                 ndisc_mc_map(&im6->mca_addr, buf, in6_dev->dev, 0);
1696                 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1697                 if (!ipm)
1698                         continue;
1699                 ipm->is_multicast = 1;
1700                 memcpy(ipm->mac, buf, OSA_ADDR_LEN);
1701                 memcpy(&ipm->u.a6.addr, &im6->mca_addr.s6_addr,
1702                        sizeof(struct in6_addr));
1703                 if (!qeth_l3_add_ip(card, ipm))
1704                         kfree(ipm);
1705         }
1706 }
1707
1708 static void qeth_l3_add_vlan_mc6(struct qeth_card *card)
1709 {
1710         struct inet6_dev *in_dev;
1711         u16 vid;
1712
1713         QETH_CARD_TEXT(card, 4, "admc6vl");
1714         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1715                 return;
1716
1717         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1718                 struct net_device *netdev;
1719
1720                 netdev = __vlan_find_dev_deep(card->dev, vid);
1721                 if (netdev == NULL ||
1722                     !(netdev->flags & IFF_UP))
1723                         continue;
1724                 in_dev = in6_dev_get(netdev);
1725                 if (!in_dev)
1726                         continue;
1727                 read_lock_bh(&in_dev->lock);
1728                 qeth_l3_add_mc6(card, in_dev);
1729                 read_unlock_bh(&in_dev->lock);
1730                 in6_dev_put(in_dev);
1731         }
1732 }
1733
1734 static void qeth_l3_add_multicast_ipv6(struct qeth_card *card)
1735 {
1736         struct inet6_dev *in6_dev;
1737
1738         QETH_CARD_TEXT(card, 4, "chkmcv6");
1739         if (!qeth_is_supported(card, IPA_IPV6))
1740                 return ;
1741         in6_dev = in6_dev_get(card->dev);
1742         if (in6_dev == NULL)
1743                 return;
1744         read_lock_bh(&in6_dev->lock);
1745         qeth_l3_add_mc6(card, in6_dev);
1746         qeth_l3_add_vlan_mc6(card);
1747         read_unlock_bh(&in6_dev->lock);
1748         in6_dev_put(in6_dev);
1749 }
1750 #endif /* CONFIG_QETH_IPV6 */
1751
1752 static void qeth_l3_free_vlan_addresses4(struct qeth_card *card,
1753                         unsigned short vid)
1754 {
1755         struct in_device *in_dev;
1756         struct in_ifaddr *ifa;
1757         struct qeth_ipaddr *addr;
1758         struct net_device *netdev;
1759
1760         QETH_CARD_TEXT(card, 4, "frvaddr4");
1761
1762         netdev = __vlan_find_dev_deep(card->dev, vid);
1763         in_dev = in_dev_get(netdev);
1764         if (!in_dev)
1765                 return;
1766         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1767                 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1768                 if (addr) {
1769                         addr->u.a4.addr = ifa->ifa_address;
1770                         addr->u.a4.mask = ifa->ifa_mask;
1771                         addr->type = QETH_IP_TYPE_NORMAL;
1772                         if (!qeth_l3_delete_ip(card, addr))
1773                                 kfree(addr);
1774                 }
1775         }
1776         in_dev_put(in_dev);
1777 }
1778
1779 static void qeth_l3_free_vlan_addresses6(struct qeth_card *card,
1780                         unsigned short vid)
1781 {
1782 #ifdef CONFIG_QETH_IPV6
1783         struct inet6_dev *in6_dev;
1784         struct inet6_ifaddr *ifa;
1785         struct qeth_ipaddr *addr;
1786         struct net_device *netdev;
1787
1788         QETH_CARD_TEXT(card, 4, "frvaddr6");
1789
1790         netdev = __vlan_find_dev_deep(card->dev, vid);
1791         in6_dev = in6_dev_get(netdev);
1792         if (!in6_dev)
1793                 return;
1794         list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
1795                 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1796                 if (addr) {
1797                         memcpy(&addr->u.a6.addr, &ifa->addr,
1798                                sizeof(struct in6_addr));
1799                         addr->u.a6.pfxlen = ifa->prefix_len;
1800                         addr->type = QETH_IP_TYPE_NORMAL;
1801                         if (!qeth_l3_delete_ip(card, addr))
1802                                 kfree(addr);
1803                 }
1804         }
1805         in6_dev_put(in6_dev);
1806 #endif /* CONFIG_QETH_IPV6 */
1807 }
1808
1809 static void qeth_l3_free_vlan_addresses(struct qeth_card *card,
1810                         unsigned short vid)
1811 {
1812         qeth_l3_free_vlan_addresses4(card, vid);
1813         qeth_l3_free_vlan_addresses6(card, vid);
1814 }
1815
1816 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
1817 {
1818         struct qeth_card *card = dev->ml_priv;
1819
1820         set_bit(vid, card->active_vlans);
1821         return 0;
1822 }
1823
1824 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
1825 {
1826         struct qeth_card *card = dev->ml_priv;
1827         unsigned long flags;
1828
1829         QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1830         if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
1831                 QETH_CARD_TEXT(card, 3, "kidREC");
1832                 return 0;
1833         }
1834         spin_lock_irqsave(&card->vlanlock, flags);
1835         /* unregister IP addresses of vlan device */
1836         qeth_l3_free_vlan_addresses(card, vid);
1837         clear_bit(vid, card->active_vlans);
1838         spin_unlock_irqrestore(&card->vlanlock, flags);
1839         qeth_l3_set_multicast_list(card->dev);
1840         return 0;
1841 }
1842
1843 static inline int qeth_l3_rebuild_skb(struct qeth_card *card,
1844                         struct sk_buff *skb, struct qeth_hdr *hdr,
1845                         unsigned short *vlan_id)
1846 {
1847         __be16 prot;
1848         struct iphdr *ip_hdr;
1849         unsigned char tg_addr[MAX_ADDR_LEN];
1850         int is_vlan = 0;
1851
1852         if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1853                 prot = htons((hdr->hdr.l3.flags & QETH_HDR_IPV6)? ETH_P_IPV6 :
1854                               ETH_P_IP);
1855                 switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
1856                 case QETH_CAST_MULTICAST:
1857                         switch (prot) {
1858 #ifdef CONFIG_QETH_IPV6
1859                         case __constant_htons(ETH_P_IPV6):
1860                                 ndisc_mc_map((struct in6_addr *)
1861                                      skb->data + 24,
1862                                      tg_addr, card->dev, 0);
1863                                 break;
1864 #endif
1865                         case __constant_htons(ETH_P_IP):
1866                                 ip_hdr = (struct iphdr *)skb->data;
1867                                 ip_eth_mc_map(ip_hdr->daddr, tg_addr);
1868                                 break;
1869                         default:
1870                                 memcpy(tg_addr, card->dev->broadcast,
1871                                         card->dev->addr_len);
1872                         }
1873                         card->stats.multicast++;
1874                         skb->pkt_type = PACKET_MULTICAST;
1875                         break;
1876                 case QETH_CAST_BROADCAST:
1877                         memcpy(tg_addr, card->dev->broadcast,
1878                                 card->dev->addr_len);
1879                         card->stats.multicast++;
1880                         skb->pkt_type = PACKET_BROADCAST;
1881                         break;
1882                 case QETH_CAST_UNICAST:
1883                 case QETH_CAST_ANYCAST:
1884                 case QETH_CAST_NOCAST:
1885                 default:
1886                         if (card->options.sniffer)
1887                                 skb->pkt_type = PACKET_OTHERHOST;
1888                         else
1889                                 skb->pkt_type = PACKET_HOST;
1890                         memcpy(tg_addr, card->dev->dev_addr,
1891                                 card->dev->addr_len);
1892                 }
1893                 if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
1894                         card->dev->header_ops->create(skb, card->dev, prot,
1895                                 tg_addr, &hdr->hdr.l3.dest_addr[2],
1896                                 card->dev->addr_len);
1897                 else
1898                         card->dev->header_ops->create(skb, card->dev, prot,
1899                                 tg_addr, "FAKELL", card->dev->addr_len);
1900         }
1901
1902         skb->protocol = eth_type_trans(skb, card->dev);
1903
1904         if (hdr->hdr.l3.ext_flags &
1905             (QETH_HDR_EXT_VLAN_FRAME | QETH_HDR_EXT_INCLUDE_VLAN_TAG)) {
1906                 *vlan_id = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
1907                  hdr->hdr.l3.vlan_id : *((u16 *)&hdr->hdr.l3.dest_addr[12]);
1908                 is_vlan = 1;
1909         }
1910
1911         if (card->dev->features & NETIF_F_RXCSUM) {
1912                 if ((hdr->hdr.l3.ext_flags &
1913                     (QETH_HDR_EXT_CSUM_HDR_REQ |
1914                      QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
1915                     (QETH_HDR_EXT_CSUM_HDR_REQ |
1916                      QETH_HDR_EXT_CSUM_TRANSP_REQ))
1917                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1918                 else
1919                         skb->ip_summed = CHECKSUM_NONE;
1920         } else
1921                 skb->ip_summed = CHECKSUM_NONE;
1922
1923         return is_vlan;
1924 }
1925
1926 static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
1927                                 int budget, int *done)
1928 {
1929         int work_done = 0;
1930         struct sk_buff *skb;
1931         struct qeth_hdr *hdr;
1932         __u16 vlan_tag = 0;
1933         int is_vlan;
1934         unsigned int len;
1935         __u16 magic;
1936
1937         *done = 0;
1938         BUG_ON(!budget);
1939         while (budget) {
1940                 skb = qeth_core_get_next_skb(card,
1941                         &card->qdio.in_q->bufs[card->rx.b_index],
1942                         &card->rx.b_element, &card->rx.e_offset, &hdr);
1943                 if (!skb) {
1944                         *done = 1;
1945                         break;
1946                 }
1947                 skb->dev = card->dev;
1948                 switch (hdr->hdr.l3.id) {
1949                 case QETH_HEADER_TYPE_LAYER3:
1950                         magic = *(__u16 *)skb->data;
1951                         if ((card->info.type == QETH_CARD_TYPE_IQD) &&
1952                             (magic == ETH_P_AF_IUCV)) {
1953                                 skb->protocol = ETH_P_AF_IUCV;
1954                                 skb->pkt_type = PACKET_HOST;
1955                                 skb->mac_header = NET_SKB_PAD;
1956                                 skb->dev = card->dev;
1957                                 len = skb->len;
1958                                 card->dev->header_ops->create(skb, card->dev, 0,
1959                                         card->dev->dev_addr, "FAKELL",
1960                                         card->dev->addr_len);
1961                                 netif_receive_skb(skb);
1962                         } else {
1963                                 is_vlan = qeth_l3_rebuild_skb(card, skb, hdr,
1964                                                       &vlan_tag);
1965                                 len = skb->len;
1966                                 if (is_vlan && !card->options.sniffer)
1967                                         __vlan_hwaccel_put_tag(skb, vlan_tag);
1968                                 napi_gro_receive(&card->napi, skb);
1969                         }
1970                         break;
1971                 case QETH_HEADER_TYPE_LAYER2: /* for HiperSockets sniffer */
1972                         skb->pkt_type = PACKET_HOST;
1973                         skb->protocol = eth_type_trans(skb, skb->dev);
1974                         len = skb->len;
1975                         netif_receive_skb(skb);
1976                         break;
1977                 default:
1978                         dev_kfree_skb_any(skb);
1979                         QETH_CARD_TEXT(card, 3, "inbunkno");
1980                         QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
1981                         continue;
1982                 }
1983                 work_done++;
1984                 budget--;
1985                 card->stats.rx_packets++;
1986                 card->stats.rx_bytes += len;
1987         }
1988         return work_done;
1989 }
1990
1991 static int qeth_l3_poll(struct napi_struct *napi, int budget)
1992 {
1993         struct qeth_card *card = container_of(napi, struct qeth_card, napi);
1994         int work_done = 0;
1995         struct qeth_qdio_buffer *buffer;
1996         int done;
1997         int new_budget = budget;
1998
1999         if (card->options.performance_stats) {
2000                 card->perf_stats.inbound_cnt++;
2001                 card->perf_stats.inbound_start_time = qeth_get_micros();
2002         }
2003
2004         while (1) {
2005                 if (!card->rx.b_count) {
2006                         card->rx.qdio_err = 0;
2007                         card->rx.b_count = qdio_get_next_buffers(
2008                                 card->data.ccwdev, 0, &card->rx.b_index,
2009                                 &card->rx.qdio_err);
2010                         if (card->rx.b_count <= 0) {
2011                                 card->rx.b_count = 0;
2012                                 break;
2013                         }
2014                         card->rx.b_element =
2015                                 &card->qdio.in_q->bufs[card->rx.b_index]
2016                                 .buffer->element[0];
2017                         card->rx.e_offset = 0;
2018                 }
2019
2020                 while (card->rx.b_count) {
2021                         buffer = &card->qdio.in_q->bufs[card->rx.b_index];
2022                         if (!(card->rx.qdio_err &&
2023                             qeth_check_qdio_errors(card, buffer->buffer,
2024                             card->rx.qdio_err, "qinerr")))
2025                                 work_done += qeth_l3_process_inbound_buffer(
2026                                         card, new_budget, &done);
2027                         else
2028                                 done = 1;
2029
2030                         if (done) {
2031                                 if (card->options.performance_stats)
2032                                         card->perf_stats.bufs_rec++;
2033                                 qeth_put_buffer_pool_entry(card,
2034                                         buffer->pool_entry);
2035                                 qeth_queue_input_buffer(card, card->rx.b_index);
2036                                 card->rx.b_count--;
2037                                 if (card->rx.b_count) {
2038                                         card->rx.b_index =
2039                                                 (card->rx.b_index + 1) %
2040                                                 QDIO_MAX_BUFFERS_PER_Q;
2041                                         card->rx.b_element =
2042                                                 &card->qdio.in_q
2043                                                 ->bufs[card->rx.b_index]
2044                                                 .buffer->element[0];
2045                                         card->rx.e_offset = 0;
2046                                 }
2047                         }
2048
2049                         if (work_done >= budget)
2050                                 goto out;
2051                         else
2052                                 new_budget = budget - work_done;
2053                 }
2054         }
2055
2056         napi_complete(napi);
2057         if (qdio_start_irq(card->data.ccwdev, 0))
2058                 napi_schedule(&card->napi);
2059 out:
2060         if (card->options.performance_stats)
2061                 card->perf_stats.inbound_time += qeth_get_micros() -
2062                         card->perf_stats.inbound_start_time;
2063         return work_done;
2064 }
2065
2066 static int qeth_l3_verify_vlan_dev(struct net_device *dev,
2067                         struct qeth_card *card)
2068 {
2069         int rc = 0;
2070         u16 vid;
2071
2072         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
2073                 struct net_device *netdev;
2074
2075                 rcu_read_lock();
2076                 netdev = __vlan_find_dev_deep(card->dev, vid);
2077                 rcu_read_unlock();
2078                 if (netdev == dev) {
2079                         rc = QETH_VLAN_CARD;
2080                         break;
2081                 }
2082         }
2083
2084         if (rc && !(vlan_dev_real_dev(dev)->ml_priv == (void *)card))
2085                 return 0;
2086
2087         return rc;
2088 }
2089
2090 static int qeth_l3_verify_dev(struct net_device *dev)
2091 {
2092         struct qeth_card *card;
2093         unsigned long flags;
2094         int rc = 0;
2095
2096         read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
2097         list_for_each_entry(card, &qeth_core_card_list.list, list) {
2098                 if (card->dev == dev) {
2099                         rc = QETH_REAL_CARD;
2100                         break;
2101                 }
2102                 rc = qeth_l3_verify_vlan_dev(dev, card);
2103                 if (rc)
2104                         break;
2105         }
2106         read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
2107
2108         return rc;
2109 }
2110
2111 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2112 {
2113         struct qeth_card *card = NULL;
2114         int rc;
2115
2116         rc = qeth_l3_verify_dev(dev);
2117         if (rc == QETH_REAL_CARD)
2118                 card = dev->ml_priv;
2119         else if (rc == QETH_VLAN_CARD)
2120                 card = vlan_dev_real_dev(dev)->ml_priv;
2121         if (card && card->options.layer2)
2122                 card = NULL;
2123         if (card)
2124                 QETH_CARD_TEXT_(card, 4, "%d", rc);
2125         return card ;
2126 }
2127
2128 static int qeth_l3_stop_card(struct qeth_card *card, int recovery_mode)
2129 {
2130         int rc = 0;
2131
2132         QETH_DBF_TEXT(SETUP, 2, "stopcard");
2133         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
2134
2135         qeth_set_allowed_threads(card, 0, 1);
2136         if (card->options.sniffer &&
2137             (card->info.promisc_mode == SET_PROMISC_MODE_ON))
2138                 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
2139         if (card->read.state == CH_STATE_UP &&
2140             card->write.state == CH_STATE_UP &&
2141             (card->state == CARD_STATE_UP)) {
2142                 if (recovery_mode)
2143                         qeth_l3_stop(card->dev);
2144                 else {
2145                         rtnl_lock();
2146                         dev_close(card->dev);
2147                         rtnl_unlock();
2148                 }
2149                 card->state = CARD_STATE_SOFTSETUP;
2150         }
2151         if (card->state == CARD_STATE_SOFTSETUP) {
2152                 qeth_l3_clear_ip_list(card, 1);
2153                 qeth_clear_ipacmd_list(card);
2154                 card->state = CARD_STATE_HARDSETUP;
2155         }
2156         if (card->state == CARD_STATE_HARDSETUP) {
2157                 qeth_qdio_clear_card(card, 0);
2158                 qeth_clear_qdio_buffers(card);
2159                 qeth_clear_working_pool_list(card);
2160                 card->state = CARD_STATE_DOWN;
2161         }
2162         if (card->state == CARD_STATE_DOWN) {
2163                 qeth_clear_cmd_buffers(&card->read);
2164                 qeth_clear_cmd_buffers(&card->write);
2165         }
2166         return rc;
2167 }
2168
2169 /*
2170  * test for and Switch promiscuous mode (on or off)
2171  *  either for guestlan or HiperSocket Sniffer
2172  */
2173 static void
2174 qeth_l3_handle_promisc_mode(struct qeth_card *card)
2175 {
2176         struct net_device *dev = card->dev;
2177
2178         if (((dev->flags & IFF_PROMISC) &&
2179              (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
2180             (!(dev->flags & IFF_PROMISC) &&
2181              (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
2182                 return;
2183
2184         if (card->info.guestlan) {              /* Guestlan trace */
2185                 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
2186                         qeth_setadp_promisc_mode(card);
2187         } else if (card->options.sniffer &&     /* HiperSockets trace */
2188                    qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
2189                 if (dev->flags & IFF_PROMISC) {
2190                         QETH_CARD_TEXT(card, 3, "+promisc");
2191                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
2192                 } else {
2193                         QETH_CARD_TEXT(card, 3, "-promisc");
2194                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
2195                 }
2196         }
2197 }
2198
2199 static void qeth_l3_set_multicast_list(struct net_device *dev)
2200 {
2201         struct qeth_card *card = dev->ml_priv;
2202
2203         QETH_CARD_TEXT(card, 3, "setmulti");
2204         if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
2205             (card->state != CARD_STATE_UP))
2206                 return;
2207         if (!card->options.sniffer) {
2208                 qeth_l3_delete_mc_addresses(card);
2209                 qeth_l3_add_multicast_ipv4(card);
2210 #ifdef CONFIG_QETH_IPV6
2211                 qeth_l3_add_multicast_ipv6(card);
2212 #endif
2213                 qeth_l3_set_ip_addr_list(card);
2214                 if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
2215                         return;
2216         }
2217         qeth_l3_handle_promisc_mode(card);
2218 }
2219
2220 static const char *qeth_l3_arp_get_error_cause(int *rc)
2221 {
2222         switch (*rc) {
2223         case QETH_IPA_ARP_RC_FAILED:
2224                 *rc = -EIO;
2225                 return "operation failed";
2226         case QETH_IPA_ARP_RC_NOTSUPP:
2227                 *rc = -EOPNOTSUPP;
2228                 return "operation not supported";
2229         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
2230                 *rc = -EINVAL;
2231                 return "argument out of range";
2232         case QETH_IPA_ARP_RC_Q_NOTSUPP:
2233                 *rc = -EOPNOTSUPP;
2234                 return "query operation not supported";
2235         case QETH_IPA_ARP_RC_Q_NO_DATA:
2236                 *rc = -ENOENT;
2237                 return "no query data available";
2238         default:
2239                 return "unknown error";
2240         }
2241 }
2242
2243 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
2244 {
2245         int tmp;
2246         int rc;
2247
2248         QETH_CARD_TEXT(card, 3, "arpstnoe");
2249
2250         /*
2251          * currently GuestLAN only supports the ARP assist function
2252          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
2253          * thus we say EOPNOTSUPP for this ARP function
2254          */
2255         if (card->info.guestlan)
2256                 return -EOPNOTSUPP;
2257         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2258                 return -EOPNOTSUPP;
2259         }
2260         rc = qeth_l3_send_simple_setassparms(card, IPA_ARP_PROCESSING,
2261                                           IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
2262                                           no_entries);
2263         if (rc) {
2264                 tmp = rc;
2265                 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on "
2266                         "%s: %s (0x%x/%d)\n", QETH_CARD_IFNAME(card),
2267                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2268         }
2269         return rc;
2270 }
2271
2272 static __u32 get_arp_entry_size(struct qeth_card *card,
2273                         struct qeth_arp_query_data *qdata,
2274                         struct qeth_arp_entrytype *type, __u8 strip_entries)
2275 {
2276         __u32 rc;
2277         __u8 is_hsi;
2278
2279         is_hsi = qdata->reply_bits == 5;
2280         if (type->ip == QETHARP_IP_ADDR_V4) {
2281                 QETH_CARD_TEXT(card, 4, "arpev4");
2282                 if (strip_entries) {
2283                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
2284                                 sizeof(struct qeth_arp_qi_entry7_short);
2285                 } else {
2286                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
2287                                 sizeof(struct qeth_arp_qi_entry7);
2288                 }
2289         } else if (type->ip == QETHARP_IP_ADDR_V6) {
2290                 QETH_CARD_TEXT(card, 4, "arpev6");
2291                 if (strip_entries) {
2292                         rc = is_hsi ?
2293                                 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
2294                                 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
2295                 } else {
2296                         rc = is_hsi ?
2297                                 sizeof(struct qeth_arp_qi_entry5_ipv6) :
2298                                 sizeof(struct qeth_arp_qi_entry7_ipv6);
2299                 }
2300         } else {
2301                 QETH_CARD_TEXT(card, 4, "arpinv");
2302                 rc = 0;
2303         }
2304
2305         return rc;
2306 }
2307
2308 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
2309 {
2310         return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
2311                 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
2312 }
2313
2314 static int qeth_l3_arp_query_cb(struct qeth_card *card,
2315                 struct qeth_reply *reply, unsigned long data)
2316 {
2317         struct qeth_ipa_cmd *cmd;
2318         struct qeth_arp_query_data *qdata;
2319         struct qeth_arp_query_info *qinfo;
2320         int i;
2321         int e;
2322         int entrybytes_done;
2323         int stripped_bytes;
2324         __u8 do_strip_entries;
2325
2326         QETH_CARD_TEXT(card, 3, "arpquecb");
2327
2328         qinfo = (struct qeth_arp_query_info *) reply->param;
2329         cmd = (struct qeth_ipa_cmd *) data;
2330         QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
2331         if (cmd->hdr.return_code) {
2332                 QETH_CARD_TEXT(card, 4, "arpcberr");
2333                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
2334                 return 0;
2335         }
2336         if (cmd->data.setassparms.hdr.return_code) {
2337                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
2338                 QETH_CARD_TEXT(card, 4, "setaperr");
2339                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
2340                 return 0;
2341         }
2342         qdata = &cmd->data.setassparms.data.query_arp;
2343         QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
2344
2345         do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
2346         stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
2347         entrybytes_done = 0;
2348         for (e = 0; e < qdata->no_entries; ++e) {
2349                 char *cur_entry;
2350                 __u32 esize;
2351                 struct qeth_arp_entrytype *etype;
2352
2353                 cur_entry = &qdata->data + entrybytes_done;
2354                 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
2355                 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
2356                         QETH_CARD_TEXT(card, 4, "pmis");
2357                         QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
2358                         break;
2359                 }
2360                 esize = get_arp_entry_size(card, qdata, etype,
2361                         do_strip_entries);
2362                 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
2363                 if (!esize)
2364                         break;
2365
2366                 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
2367                         QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOMEM);
2368                         cmd->hdr.return_code = IPA_RC_ENOMEM;
2369                         goto out_error;
2370                 }
2371
2372                 memcpy(qinfo->udata + qinfo->udata_offset,
2373                         &qdata->data + entrybytes_done + stripped_bytes,
2374                         esize);
2375                 entrybytes_done += esize + stripped_bytes;
2376                 qinfo->udata_offset += esize;
2377                 ++qinfo->no_entries;
2378         }
2379         /* check if all replies received ... */
2380         if (cmd->data.setassparms.hdr.seq_no <
2381             cmd->data.setassparms.hdr.number_of_replies)
2382                 return 1;
2383         QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
2384         memcpy(qinfo->udata, &qinfo->no_entries, 4);
2385         /* keep STRIP_ENTRIES flag so the user program can distinguish
2386          * stripped entries from normal ones */
2387         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
2388                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
2389         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
2390         QETH_CARD_TEXT_(card, 4, "rc%i", 0);
2391         return 0;
2392 out_error:
2393         i = 0;
2394         memcpy(qinfo->udata, &i, 4);
2395         return 0;
2396 }
2397
2398 static int qeth_l3_send_ipa_arp_cmd(struct qeth_card *card,
2399                 struct qeth_cmd_buffer *iob, int len,
2400                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
2401                         unsigned long),
2402                 void *reply_param)
2403 {
2404         QETH_CARD_TEXT(card, 4, "sendarp");
2405
2406         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2407         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2408                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2409         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
2410                                       reply_cb, reply_param);
2411 }
2412
2413 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
2414         enum qeth_prot_versions prot,
2415         struct qeth_arp_query_info *qinfo)
2416 {
2417         struct qeth_cmd_buffer *iob;
2418         struct qeth_ipa_cmd *cmd;
2419         int tmp;
2420         int rc;
2421
2422         QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
2423
2424         iob = qeth_l3_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2425                         IPA_CMD_ASS_ARP_QUERY_INFO,
2426                         sizeof(struct qeth_arp_query_data) - sizeof(char),
2427                         prot);
2428         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2429         cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
2430         cmd->data.setassparms.data.query_arp.reply_bits = 0;
2431         cmd->data.setassparms.data.query_arp.no_entries = 0;
2432         rc = qeth_l3_send_ipa_arp_cmd(card, iob,
2433                            QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
2434                            qeth_l3_arp_query_cb, (void *)qinfo);
2435         if (rc) {
2436                 tmp = rc;
2437                 QETH_DBF_MESSAGE(2,
2438                         "Error while querying ARP cache on %s: %s "
2439                         "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2440                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2441         }
2442
2443         return rc;
2444 }
2445
2446 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
2447 {
2448         struct qeth_arp_query_info qinfo = {0, };
2449         int rc;
2450
2451         QETH_CARD_TEXT(card, 3, "arpquery");
2452
2453         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
2454                                IPA_ARP_PROCESSING)) {
2455                 QETH_CARD_TEXT(card, 3, "arpqnsup");
2456                 rc = -EOPNOTSUPP;
2457                 goto out;
2458         }
2459         /* get size of userspace buffer and mask_bits -> 6 bytes */
2460         if (copy_from_user(&qinfo, udata, 6)) {
2461                 rc = -EFAULT;
2462                 goto out;
2463         }
2464         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
2465         if (!qinfo.udata) {
2466                 rc = -ENOMEM;
2467                 goto out;
2468         }
2469         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
2470         rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
2471         if (rc) {
2472                 if (copy_to_user(udata, qinfo.udata, 4))
2473                         rc = -EFAULT;
2474                         goto free_and_out;
2475         } else {
2476 #ifdef CONFIG_QETH_IPV6
2477                 if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
2478                         /* fails in case of GuestLAN QDIO mode */
2479                         qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6,
2480                                 &qinfo);
2481                 }
2482 #endif
2483                 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
2484                         QETH_CARD_TEXT(card, 4, "qactf");
2485                         rc = -EFAULT;
2486                         goto free_and_out;
2487                 }
2488                 QETH_CARD_TEXT_(card, 4, "qacts");
2489         }
2490 free_and_out:
2491         kfree(qinfo.udata);
2492 out:
2493         return rc;
2494 }
2495
2496 static int qeth_l3_arp_add_entry(struct qeth_card *card,
2497                                 struct qeth_arp_cache_entry *entry)
2498 {
2499         struct qeth_cmd_buffer *iob;
2500         char buf[16];
2501         int tmp;
2502         int rc;
2503
2504         QETH_CARD_TEXT(card, 3, "arpadent");
2505
2506         /*
2507          * currently GuestLAN only supports the ARP assist function
2508          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
2509          * thus we say EOPNOTSUPP for this ARP function
2510          */
2511         if (card->info.guestlan)
2512                 return -EOPNOTSUPP;
2513         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2514                 return -EOPNOTSUPP;
2515         }
2516
2517         iob = qeth_l3_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2518                                        IPA_CMD_ASS_ARP_ADD_ENTRY,
2519                                        sizeof(struct qeth_arp_cache_entry),
2520                                        QETH_PROT_IPV4);
2521         rc = qeth_l3_send_setassparms(card, iob,
2522                                    sizeof(struct qeth_arp_cache_entry),
2523                                    (unsigned long) entry,
2524                                    qeth_l3_default_setassparms_cb, NULL);
2525         if (rc) {
2526                 tmp = rc;
2527                 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2528                 QETH_DBF_MESSAGE(2, "Could not add ARP entry for address %s "
2529                         "on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2530                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2531         }
2532         return rc;
2533 }
2534
2535 static int qeth_l3_arp_remove_entry(struct qeth_card *card,
2536                                 struct qeth_arp_cache_entry *entry)
2537 {
2538         struct qeth_cmd_buffer *iob;
2539         char buf[16] = {0, };
2540         int tmp;
2541         int rc;
2542
2543         QETH_CARD_TEXT(card, 3, "arprment");
2544
2545         /*
2546          * currently GuestLAN only supports the ARP assist function
2547          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
2548          * thus we say EOPNOTSUPP for this ARP function
2549          */
2550         if (card->info.guestlan)
2551                 return -EOPNOTSUPP;
2552         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2553                 return -EOPNOTSUPP;
2554         }
2555         memcpy(buf, entry, 12);
2556         iob = qeth_l3_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2557                                        IPA_CMD_ASS_ARP_REMOVE_ENTRY,
2558                                        12,
2559                                        QETH_PROT_IPV4);
2560         rc = qeth_l3_send_setassparms(card, iob,
2561                                    12, (unsigned long)buf,
2562                                    qeth_l3_default_setassparms_cb, NULL);
2563         if (rc) {
2564                 tmp = rc;
2565                 memset(buf, 0, 16);
2566                 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2567                 QETH_DBF_MESSAGE(2, "Could not delete ARP entry for address %s"
2568                         " on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2569                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2570         }
2571         return rc;
2572 }
2573
2574 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
2575 {
2576         int rc;
2577         int tmp;
2578
2579         QETH_CARD_TEXT(card, 3, "arpflush");
2580
2581         /*
2582          * currently GuestLAN only supports the ARP assist function
2583          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
2584          * thus we say EOPNOTSUPP for this ARP function
2585         */
2586         if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
2587                 return -EOPNOTSUPP;
2588         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2589                 return -EOPNOTSUPP;
2590         }
2591         rc = qeth_l3_send_simple_setassparms(card, IPA_ARP_PROCESSING,
2592                                           IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
2593         if (rc) {
2594                 tmp = rc;
2595                 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on %s: %s "
2596                         "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2597                         qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2598         }
2599         return rc;
2600 }
2601
2602 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2603 {
2604         struct qeth_card *card = dev->ml_priv;
2605         struct qeth_arp_cache_entry arp_entry;
2606         struct mii_ioctl_data *mii_data;
2607         int rc = 0;
2608
2609         if (!card)
2610                 return -ENODEV;
2611
2612         if ((card->state != CARD_STATE_UP) &&
2613                 (card->state != CARD_STATE_SOFTSETUP))
2614                 return -ENODEV;
2615
2616         switch (cmd) {
2617         case SIOC_QETH_ARP_SET_NO_ENTRIES:
2618                 if (!capable(CAP_NET_ADMIN)) {
2619                         rc = -EPERM;
2620                         break;
2621                 }
2622                 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
2623                 break;
2624         case SIOC_QETH_ARP_QUERY_INFO:
2625                 if (!capable(CAP_NET_ADMIN)) {
2626                         rc = -EPERM;
2627                         break;
2628                 }
2629                 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
2630                 break;
2631         case SIOC_QETH_ARP_ADD_ENTRY:
2632                 if (!capable(CAP_NET_ADMIN)) {
2633                         rc = -EPERM;
2634                         break;
2635                 }
2636                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2637                                    sizeof(struct qeth_arp_cache_entry)))
2638                         rc = -EFAULT;
2639                 else
2640                         rc = qeth_l3_arp_add_entry(card, &arp_entry);
2641                 break;
2642         case SIOC_QETH_ARP_REMOVE_ENTRY:
2643                 if (!capable(CAP_NET_ADMIN)) {
2644                         rc = -EPERM;
2645                         break;
2646                 }
2647                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2648                                    sizeof(struct qeth_arp_cache_entry)))
2649                         rc = -EFAULT;
2650                 else
2651                         rc = qeth_l3_arp_remove_entry(card, &arp_entry);
2652                 break;
2653         case SIOC_QETH_ARP_FLUSH_CACHE:
2654                 if (!capable(CAP_NET_ADMIN)) {
2655                         rc = -EPERM;
2656                         break;
2657                 }
2658                 rc = qeth_l3_arp_flush_cache(card);
2659                 break;
2660         case SIOC_QETH_ADP_SET_SNMP_CONTROL:
2661                 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
2662                 break;
2663         case SIOC_QETH_GET_CARD_TYPE:
2664                 if ((card->info.type == QETH_CARD_TYPE_OSD ||
2665                      card->info.type == QETH_CARD_TYPE_OSX) &&
2666                     !card->info.guestlan)
2667                         return 1;
2668                 return 0;
2669                 break;
2670         case SIOCGMIIPHY:
2671                 mii_data = if_mii(rq);
2672                 mii_data->phy_id = 0;
2673                 break;
2674         case SIOCGMIIREG:
2675                 mii_data = if_mii(rq);
2676                 if (mii_data->phy_id != 0)
2677                         rc = -EINVAL;
2678                 else
2679                         mii_data->val_out = qeth_mdio_read(dev,
2680                                                         mii_data->phy_id,
2681                                                         mii_data->reg_num);
2682                 break;
2683         case SIOC_QETH_QUERY_OAT:
2684                 rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
2685                 break;
2686         default:
2687                 rc = -EOPNOTSUPP;
2688         }
2689         if (rc)
2690                 QETH_CARD_TEXT_(card, 2, "ioce%d", rc);
2691         return rc;
2692 }
2693
2694 int inline qeth_l3_get_cast_type(struct qeth_card *card, struct sk_buff *skb)
2695 {
2696         int cast_type = RTN_UNSPEC;
2697         struct neighbour *n = NULL;
2698         struct dst_entry *dst;
2699
2700         rcu_read_lock();
2701         dst = skb_dst(skb);
2702         if (dst)
2703                 n = dst_get_neighbour_noref(dst);
2704         if (n) {
2705                 cast_type = n->type;
2706                 rcu_read_unlock();
2707                 if ((cast_type == RTN_BROADCAST) ||
2708                     (cast_type == RTN_MULTICAST) ||
2709                     (cast_type == RTN_ANYCAST))
2710                         return cast_type;
2711                 else
2712                         return RTN_UNSPEC;
2713         }
2714         rcu_read_unlock();
2715
2716         /* try something else */
2717         if (skb->protocol == ETH_P_IPV6)
2718                 return (skb_network_header(skb)[24] == 0xff) ?
2719                                 RTN_MULTICAST : 0;
2720         else if (skb->protocol == ETH_P_IP)
2721                 return ((skb_network_header(skb)[16] & 0xf0) == 0xe0) ?
2722                                 RTN_MULTICAST : 0;
2723         /* ... */
2724         if (!memcmp(skb->data, skb->dev->broadcast, 6))
2725                 return RTN_BROADCAST;
2726         else {
2727                 u16 hdr_mac;
2728
2729                 hdr_mac = *((u16 *)skb->data);
2730                 /* tr multicast? */
2731                 switch (card->info.link_type) {
2732                 case QETH_LINK_TYPE_HSTR:
2733                 case QETH_LINK_TYPE_LANE_TR:
2734                         if ((hdr_mac == QETH_TR_MAC_NC) ||
2735                             (hdr_mac == QETH_TR_MAC_C))
2736                                 return RTN_MULTICAST;
2737                         break;
2738                 /* eth or so multicast? */
2739                 default:
2740                 if ((hdr_mac == QETH_ETH_MAC_V4) ||
2741                             (hdr_mac == QETH_ETH_MAC_V6))
2742                                 return RTN_MULTICAST;
2743                 }
2744         }
2745         return cast_type;
2746 }
2747
2748 static void qeth_l3_fill_af_iucv_hdr(struct qeth_card *card,
2749                 struct qeth_hdr *hdr, struct sk_buff *skb)
2750 {
2751         char daddr[16];
2752         struct af_iucv_trans_hdr *iucv_hdr;
2753
2754         skb_pull(skb, 14);
2755         card->dev->header_ops->create(skb, card->dev, 0,
2756                                       card->dev->dev_addr, card->dev->dev_addr,
2757                                       card->dev->addr_len);
2758         skb_pull(skb, 14);
2759         iucv_hdr = (struct af_iucv_trans_hdr *)skb->data;
2760         memset(hdr, 0, sizeof(struct qeth_hdr));
2761         hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2762         hdr->hdr.l3.ext_flags = 0;
2763         hdr->hdr.l3.length = skb->len;
2764         hdr->hdr.l3.flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
2765         memset(daddr, 0, sizeof(daddr));
2766         daddr[0] = 0xfe;
2767         daddr[1] = 0x80;
2768         memcpy(&daddr[8], iucv_hdr->destUserID, 8);
2769         memcpy(hdr->hdr.l3.dest_addr, daddr, 16);
2770 }
2771
2772 static void qeth_l3_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
2773                 struct sk_buff *skb, int ipv, int cast_type)
2774 {
2775         struct dst_entry *dst;
2776
2777         memset(hdr, 0, sizeof(struct qeth_hdr));
2778         hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2779         hdr->hdr.l3.ext_flags = 0;
2780
2781         /*
2782          * before we're going to overwrite this location with next hop ip.
2783          * v6 uses passthrough, v4 sets the tag in the QDIO header.
2784          */
2785         if (vlan_tx_tag_present(skb)) {
2786                 if ((ipv == 4) || (card->info.type == QETH_CARD_TYPE_IQD))
2787                         hdr->hdr.l3.ext_flags = QETH_HDR_EXT_VLAN_FRAME;
2788                 else
2789                         hdr->hdr.l3.ext_flags = QETH_HDR_EXT_INCLUDE_VLAN_TAG;
2790                 hdr->hdr.l3.vlan_id = vlan_tx_tag_get(skb);
2791         }
2792
2793         hdr->hdr.l3.length = skb->len - sizeof(struct qeth_hdr);
2794
2795         rcu_read_lock();
2796         dst = skb_dst(skb);
2797         if (ipv == 4) {
2798                 struct rtable *rt = (struct rtable *) dst;
2799                 __be32 *pkey = &ip_hdr(skb)->daddr;
2800
2801                 if (rt->rt_gateway)
2802                         pkey = &rt->rt_gateway;
2803
2804                 /* IPv4 */
2805                 hdr->hdr.l3.flags = qeth_l3_get_qeth_hdr_flags4(cast_type);
2806                 memset(hdr->hdr.l3.dest_addr, 0, 12);
2807                 *((__be32 *) (&hdr->hdr.l3.dest_addr[12])) = *pkey;
2808         } else if (ipv == 6) {
2809                 struct rt6_info *rt = (struct rt6_info *) dst;
2810                 struct in6_addr *pkey = &ipv6_hdr(skb)->daddr;
2811
2812                 if (!ipv6_addr_any(&rt->rt6i_gateway))
2813                         pkey = &rt->rt6i_gateway;
2814
2815                 /* IPv6 */
2816                 hdr->hdr.l3.flags = qeth_l3_get_qeth_hdr_flags6(cast_type);
2817                 if (card->info.type == QETH_CARD_TYPE_IQD)
2818                         hdr->hdr.l3.flags &= ~QETH_HDR_PASSTHRU;
2819                 memcpy(hdr->hdr.l3.dest_addr, pkey, 16);
2820         } else {
2821                 if (!memcmp(skb->data + sizeof(struct qeth_hdr),
2822                             skb->dev->broadcast, 6)) {
2823                         /* broadcast? */
2824                         hdr->hdr.l3.flags = QETH_CAST_BROADCAST |
2825                                                 QETH_HDR_PASSTHRU;
2826                 } else {
2827                         hdr->hdr.l3.flags = (cast_type == RTN_MULTICAST) ?
2828                                 QETH_CAST_MULTICAST | QETH_HDR_PASSTHRU :
2829                                 QETH_CAST_UNICAST | QETH_HDR_PASSTHRU;
2830                 }
2831         }
2832         rcu_read_unlock();
2833 }
2834
2835 static inline void qeth_l3_hdr_csum(struct qeth_card *card,
2836                 struct qeth_hdr *hdr, struct sk_buff *skb)
2837 {
2838         struct iphdr *iph = ip_hdr(skb);
2839
2840         /* tcph->check contains already the pseudo hdr checksum
2841          * so just set the header flags
2842          */
2843         if (iph->protocol == IPPROTO_UDP)
2844                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_UDP;
2845         hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_TRANSP_REQ |
2846                 QETH_HDR_EXT_CSUM_HDR_REQ;
2847         iph->check = 0;
2848         if (card->options.performance_stats)
2849                 card->perf_stats.tx_csum++;
2850 }
2851
2852 static void qeth_tso_fill_header(struct qeth_card *card,
2853                 struct qeth_hdr *qhdr, struct sk_buff *skb)
2854 {
2855         struct qeth_hdr_tso *hdr = (struct qeth_hdr_tso *)qhdr;
2856         struct tcphdr *tcph = tcp_hdr(skb);
2857         struct iphdr *iph = ip_hdr(skb);
2858         struct ipv6hdr *ip6h = ipv6_hdr(skb);
2859
2860         /*fix header to TSO values ...*/
2861         hdr->hdr.hdr.l3.id = QETH_HEADER_TYPE_TSO;
2862         hdr->hdr.hdr.l3.length = skb->len - sizeof(struct qeth_hdr_tso);
2863         /*set values which are fix for the first approach ...*/
2864         hdr->ext.hdr_tot_len = (__u16) sizeof(struct qeth_hdr_ext_tso);
2865         hdr->ext.imb_hdr_no  = 1;
2866         hdr->ext.hdr_type    = 1;
2867         hdr->ext.hdr_version = 1;
2868         hdr->ext.hdr_len     = 28;
2869         /*insert non-fix values */
2870         hdr->ext.mss = skb_shinfo(skb)->gso_size;
2871         hdr->ext.dg_hdr_len = (__u16)(iph->ihl*4 + tcph->doff*4);
2872         hdr->ext.payload_len = (__u16)(skb->len - hdr->ext.dg_hdr_len -
2873                                        sizeof(struct qeth_hdr_tso));
2874         tcph->check = 0;
2875         if (skb->protocol == ETH_P_IPV6) {
2876                 ip6h->payload_len = 0;
2877                 tcph->check = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
2878                                                0, IPPROTO_TCP, 0);
2879         } else {
2880                 /*OSA want us to set these values ...*/
2881                 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
2882                                          0, IPPROTO_TCP, 0);
2883                 iph->tot_len = 0;
2884                 iph->check = 0;
2885         }
2886 }
2887
2888 static inline int qeth_l3_tso_elements(struct sk_buff *skb)
2889 {
2890         unsigned long tcpd = (unsigned long)tcp_hdr(skb) +
2891                 tcp_hdr(skb)->doff * 4;
2892         int tcpd_len = skb->len - (tcpd - (unsigned long)skb->data);
2893         int elements = PFN_UP(tcpd + tcpd_len - 1) - PFN_DOWN(tcpd);
2894         elements += skb_shinfo(skb)->nr_frags;
2895         return elements;
2896 }
2897
2898 static int qeth_l3_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
2899 {
2900         int rc;
2901         u16 *tag;
2902         struct qeth_hdr *hdr = NULL;
2903         int elements_needed = 0;
2904         int elems;
2905         struct qeth_card *card = dev->ml_priv;
2906         struct sk_buff *new_skb = NULL;
2907         int ipv = qeth_get_ip_version(skb);
2908         int cast_type = qeth_l3_get_cast_type(card, skb);
2909         struct qeth_qdio_out_q *queue = card->qdio.out_qs
2910                 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
2911         int tx_bytes = skb->len;
2912         bool large_send;
2913         int data_offset = -1;
2914         int nr_frags;
2915
2916         if (((card->info.type == QETH_CARD_TYPE_IQD) &&
2917              (((card->options.cq != QETH_CQ_ENABLED) && !ipv) ||
2918               ((card->options.cq == QETH_CQ_ENABLED) &&
2919                (skb->protocol != ETH_P_AF_IUCV)))) ||
2920             card->options.sniffer)
2921                         goto tx_drop;
2922
2923         if ((card->state != CARD_STATE_UP) || !card->lan_online) {
2924                 card->stats.tx_carrier_errors++;
2925                 goto tx_drop;
2926         }
2927
2928         if ((cast_type == RTN_BROADCAST) &&
2929             (card->info.broadcast_capable == 0))
2930                 goto tx_drop;
2931
2932         if (card->options.performance_stats) {
2933                 card->perf_stats.outbound_cnt++;
2934                 card->perf_stats.outbound_start_time = qeth_get_micros();
2935         }
2936
2937         large_send = skb_is_gso(skb);
2938
2939         if ((card->info.type == QETH_CARD_TYPE_IQD) && (!large_send) &&
2940             (skb_shinfo(skb)->nr_frags == 0)) {
2941                 new_skb = skb;
2942                 if (new_skb->protocol == ETH_P_AF_IUCV)
2943                         data_offset = 0;
2944                 else
2945                         data_offset = ETH_HLEN;
2946                 hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
2947                 if (!hdr)
2948                         goto tx_drop;
2949                 elements_needed++;
2950         } else {
2951                 /* create a clone with writeable headroom */
2952                 new_skb = skb_realloc_headroom(skb, sizeof(struct qeth_hdr_tso)
2953                                         + VLAN_HLEN);
2954                 if (!new_skb)
2955                         goto tx_drop;
2956         }
2957
2958         if (card->info.type == QETH_CARD_TYPE_IQD) {
2959                 if (data_offset < 0)
2960                         skb_pull(new_skb, ETH_HLEN);
2961         } else {
2962                 if (ipv == 4) {
2963                         skb_pull(new_skb, ETH_HLEN);
2964                 }
2965
2966                 if (ipv != 4 && vlan_tx_tag_present(new_skb)) {
2967                         skb_push(new_skb, VLAN_HLEN);
2968                         skb_copy_to_linear_data(new_skb, new_skb->data + 4, 4);
2969                         skb_copy_to_linear_data_offset(new_skb, 4,
2970                                 new_skb->data + 8, 4);
2971                         skb_copy_to_linear_data_offset(new_skb, 8,
2972                                 new_skb->data + 12, 4);
2973                         tag = (u16 *)(new_skb->data + 12);
2974                         *tag = __constant_htons(ETH_P_8021Q);
2975                         *(tag + 1) = htons(vlan_tx_tag_get(new_skb));
2976                 }
2977         }
2978
2979         netif_stop_queue(dev);
2980
2981         /* fix hardware limitation: as long as we do not have sbal
2982          * chaining we can not send long frag lists
2983          */
2984         if (large_send) {
2985                 if (qeth_l3_tso_elements(new_skb) + 1 > 16) {
2986                         if (skb_linearize(new_skb))
2987                                 goto tx_drop;
2988                         if (card->options.performance_stats)
2989                                 card->perf_stats.tx_lin++;
2990                 }
2991         }
2992
2993         if (large_send && (cast_type == RTN_UNSPEC)) {
2994                 hdr = (struct qeth_hdr *)skb_push(new_skb,
2995                                                 sizeof(struct qeth_hdr_tso));
2996                 memset(hdr, 0, sizeof(struct qeth_hdr_tso));
2997                 qeth_l3_fill_header(card, hdr, new_skb, ipv, cast_type);
2998                 qeth_tso_fill_header(card, hdr, new_skb);
2999                 elements_needed++;
3000         } else {
3001                 if (data_offset < 0) {
3002                         hdr = (struct qeth_hdr *)skb_push(new_skb,
3003                                                 sizeof(struct qeth_hdr));
3004                         qeth_l3_fill_header(card, hdr, new_skb, ipv,
3005                                                 cast_type);
3006                 } else {
3007                         if (new_skb->protocol == ETH_P_AF_IUCV)
3008                                 qeth_l3_fill_af_iucv_hdr(card, hdr, new_skb);
3009                         else {
3010                                 qeth_l3_fill_header(card, hdr, new_skb, ipv,
3011                                                         cast_type);
3012                                 hdr->hdr.l3.length = new_skb->len - data_offset;
3013                         }
3014                 }
3015
3016                 if (skb->ip_summed == CHECKSUM_PARTIAL)
3017                         qeth_l3_hdr_csum(card, hdr, new_skb);
3018         }
3019
3020         elems = qeth_get_elements_no(card, (void *)hdr, new_skb,
3021                                                  elements_needed);
3022         if (!elems) {
3023                 if (data_offset >= 0)
3024                         kmem_cache_free(qeth_core_header_cache, hdr);
3025                 goto tx_drop;
3026         }
3027         elements_needed += elems;
3028         nr_frags = skb_shinfo(new_skb)->nr_frags;
3029
3030         if (card->info.type != QETH_CARD_TYPE_IQD) {
3031                 int len;
3032                 if (large_send)
3033                         len = ((unsigned long)tcp_hdr(new_skb) +
3034                                 tcp_hdr(new_skb)->doff * 4) -
3035                                 (unsigned long)new_skb->data;
3036                 else
3037                         len = sizeof(struct qeth_hdr_layer3);
3038
3039                 if (qeth_hdr_chk_and_bounce(new_skb, len))
3040                         goto tx_drop;
3041                 rc = qeth_do_send_packet(card, queue, new_skb, hdr,
3042                                          elements_needed);
3043         } else
3044                 rc = qeth_do_send_packet_fast(card, queue, new_skb, hdr,
3045                                         elements_needed, data_offset, 0);
3046
3047         if (!rc) {
3048                 card->stats.tx_packets++;
3049                 card->stats.tx_bytes += tx_bytes;
3050                 if (new_skb != skb)
3051                         dev_kfree_skb_any(skb);
3052                 if (card->options.performance_stats) {
3053                         if (large_send) {
3054                                 card->perf_stats.large_send_bytes += tx_bytes;
3055                                 card->perf_stats.large_send_cnt++;
3056                         }
3057                         if (nr_frags) {
3058                                 card->perf_stats.sg_skbs_sent++;
3059                                 /* nr_frags + skb->data */
3060                                 card->perf_stats.sg_frags_sent += nr_frags + 1;
3061                         }
3062                 }
3063                 rc = NETDEV_TX_OK;
3064         } else {
3065                 if (data_offset >= 0)
3066                         kmem_cache_free(qeth_core_header_cache, hdr);
3067
3068                 if (rc == -EBUSY) {
3069                         if (new_skb != skb)
3070                                 dev_kfree_skb_any(new_skb);
3071                         return NETDEV_TX_BUSY;
3072                 } else
3073                         goto tx_drop;
3074         }
3075
3076         netif_wake_queue(dev);
3077         if (card->options.performance_stats)
3078                 card->perf_stats.outbound_time += qeth_get_micros() -
3079                         card->perf_stats.outbound_start_time;
3080         return rc;
3081
3082 tx_drop:
3083         card->stats.tx_dropped++;
3084         card->stats.tx_errors++;
3085         if ((new_skb != skb) && new_skb)
3086                 dev_kfree_skb_any(new_skb);
3087         dev_kfree_skb_any(skb);
3088         netif_wake_queue(dev);
3089         return NETDEV_TX_OK;
3090 }
3091
3092 static int __qeth_l3_open(struct net_device *dev)
3093 {
3094         struct qeth_card *card = dev->ml_priv;
3095         int rc = 0;
3096
3097         QETH_CARD_TEXT(card, 4, "qethopen");
3098         if (card->state == CARD_STATE_UP)
3099                 return rc;
3100         if (card->state != CARD_STATE_SOFTSETUP)
3101                 return -ENODEV;
3102         card->data.state = CH_STATE_UP;
3103         card->state = CARD_STATE_UP;
3104         netif_start_queue(dev);
3105
3106         if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
3107                 napi_enable(&card->napi);
3108                 napi_schedule(&card->napi);
3109         } else
3110                 rc = -EIO;
3111         return rc;
3112 }
3113
3114 static int qeth_l3_open(struct net_device *dev)
3115 {
3116         struct qeth_card *card = dev->ml_priv;
3117
3118         QETH_CARD_TEXT(card, 5, "qethope_");
3119         if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
3120                 QETH_CARD_TEXT(card, 3, "openREC");
3121                 return -ERESTARTSYS;
3122         }
3123         return __qeth_l3_open(dev);
3124 }
3125
3126 static int qeth_l3_stop(struct net_device *dev)
3127 {
3128         struct qeth_card *card = dev->ml_priv;
3129
3130         QETH_CARD_TEXT(card, 4, "qethstop");
3131         netif_tx_disable(dev);
3132         if (card->state == CARD_STATE_UP) {
3133                 card->state = CARD_STATE_SOFTSETUP;
3134                 napi_disable(&card->napi);
3135         }
3136         return 0;
3137 }
3138
3139 static netdev_features_t qeth_l3_fix_features(struct net_device *dev,
3140         netdev_features_t features)
3141 {
3142         struct qeth_card *card = dev->ml_priv;
3143
3144         if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
3145                 features &= ~NETIF_F_IP_CSUM;
3146         if (!qeth_is_supported(card, IPA_OUTBOUND_TSO))
3147                 features &= ~NETIF_F_TSO;
3148         if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM))
3149                 features &= ~NETIF_F_RXCSUM;
3150
3151         return features;
3152 }
3153
3154 static int qeth_l3_set_features(struct net_device *dev,
3155         netdev_features_t features)
3156 {
3157         struct qeth_card *card = dev->ml_priv;
3158         u32 changed = dev->features ^ features;
3159         int err;
3160
3161         if (!(changed & NETIF_F_RXCSUM))
3162                 return 0;
3163
3164         if (card->state == CARD_STATE_DOWN ||
3165             card->state == CARD_STATE_RECOVER)
3166                 return 0;
3167
3168         err = qeth_l3_set_rx_csum(card, features & NETIF_F_RXCSUM);
3169         if (err)
3170                 dev->features = features ^ NETIF_F_RXCSUM;
3171
3172         return err;
3173 }
3174
3175 static const struct ethtool_ops qeth_l3_ethtool_ops = {
3176         .get_link = ethtool_op_get_link,
3177         .get_strings = qeth_core_get_strings,
3178         .get_ethtool_stats = qeth_core_get_ethtool_stats,
3179         .get_sset_count = qeth_core_get_sset_count,
3180         .get_drvinfo = qeth_core_get_drvinfo,
3181         .get_settings = qeth_core_ethtool_get_settings,
3182 };
3183
3184 /*
3185  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
3186  * NOARP on the netdevice is no option because it also turns off neighbor
3187  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
3188  * arp resolution but we want the hard header (packet socket will work
3189  * e.g. tcpdump)
3190  */
3191 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
3192 {
3193         n->nud_state = NUD_NOARP;
3194         memcpy(n->ha, "FAKELL", 6);
3195         n->output = n->ops->connected_output;
3196         return 0;
3197 }
3198
3199 static int
3200 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
3201 {
3202         if (np->tbl->family == AF_INET)
3203                 np->neigh_setup = qeth_l3_neigh_setup_noarp;
3204
3205         return 0;
3206 }
3207
3208 static const struct net_device_ops qeth_l3_netdev_ops = {
3209         .ndo_open               = qeth_l3_open,
3210         .ndo_stop               = qeth_l3_stop,
3211         .ndo_get_stats          = qeth_get_stats,
3212         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
3213         .ndo_validate_addr      = eth_validate_addr,
3214         .ndo_set_rx_mode        = qeth_l3_set_multicast_list,
3215         .ndo_do_ioctl           = qeth_l3_do_ioctl,
3216         .ndo_change_mtu         = qeth_change_mtu,
3217         .ndo_fix_features       = qeth_l3_fix_features,
3218         .ndo_set_features       = qeth_l3_set_features,
3219         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
3220         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
3221         .ndo_tx_timeout         = qeth_tx_timeout,
3222 };
3223
3224 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
3225         .ndo_open               = qeth_l3_open,
3226         .ndo_stop               = qeth_l3_stop,
3227         .ndo_get_stats          = qeth_get_stats,
3228         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
3229         .ndo_validate_addr      = eth_validate_addr,
3230         .ndo_set_rx_mode        = qeth_l3_set_multicast_list,
3231         .ndo_do_ioctl           = qeth_l3_do_ioctl,
3232         .ndo_change_mtu         = qeth_change_mtu,
3233         .ndo_fix_features       = qeth_l3_fix_features,
3234         .ndo_set_features       = qeth_l3_set_features,
3235         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
3236         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
3237         .ndo_tx_timeout         = qeth_tx_timeout,
3238         .ndo_neigh_setup        = qeth_l3_neigh_setup,
3239 };
3240
3241 static int qeth_l3_setup_netdev(struct qeth_card *card)
3242 {
3243         if (card->info.type == QETH_CARD_TYPE_OSD ||
3244             card->info.type == QETH_CARD_TYPE_OSX) {
3245                 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
3246                     (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
3247                         pr_info("qeth_l3: ignoring TR device\n");
3248                         return -ENODEV;
3249                 } else {
3250                         card->dev = alloc_etherdev(0);
3251                         if (!card->dev)
3252                                 return -ENODEV;
3253                         card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
3254
3255                         /*IPv6 address autoconfiguration stuff*/
3256                         qeth_l3_get_unique_id(card);
3257                         if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
3258                                 card->dev->dev_id = card->info.unique_id &
3259                                                          0xffff;
3260                         if (!card->info.guestlan) {
3261                                 card->dev->hw_features = NETIF_F_SG |
3262                                         NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
3263                                         NETIF_F_TSO;
3264                                 card->dev->features = NETIF_F_RXCSUM;
3265                         }
3266                 }
3267         } else if (card->info.type == QETH_CARD_TYPE_IQD) {
3268                 card->dev = alloc_netdev(0, "hsi%d", ether_setup);
3269                 if (!card->dev)
3270                         return -ENODEV;
3271                 card->dev->flags |= IFF_NOARP;
3272                 card->dev->netdev_ops = &qeth_l3_netdev_ops;
3273                 qeth_l3_iqd_read_initial_mac(card);
3274                 if (card->options.hsuid[0])
3275                         memcpy(card->dev->perm_addr, card->options.hsuid, 9);
3276         } else
3277                 return -ENODEV;
3278
3279         card->dev->ml_priv = card;
3280         card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
3281         card->dev->mtu = card->info.initial_mtu;
3282         SET_ETHTOOL_OPS(card->dev, &qeth_l3_ethtool_ops);
3283         card->dev->features |=  NETIF_F_HW_VLAN_TX |
3284                                 NETIF_F_HW_VLAN_RX |
3285                                 NETIF_F_HW_VLAN_FILTER;
3286         card->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
3287         card->dev->gso_max_size = 15 * PAGE_SIZE;
3288
3289         SET_NETDEV_DEV(card->dev, &card->gdev->dev);
3290         netif_napi_add(card->dev, &card->napi, qeth_l3_poll, QETH_NAPI_WEIGHT);
3291         return register_netdev(card->dev);
3292 }
3293
3294 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
3295 {
3296         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3297
3298         qeth_l3_create_device_attributes(&gdev->dev);
3299         card->options.layer2 = 0;
3300         card->info.hwtrap = 0;
3301         card->discipline.start_poll = qeth_qdio_start_poll;
3302         card->discipline.input_handler = (qdio_handler_t *)
3303                 qeth_qdio_input_handler;
3304         card->discipline.output_handler = (qdio_handler_t *)
3305                 qeth_qdio_output_handler;
3306         card->discipline.recover = qeth_l3_recover;
3307         return 0;
3308 }
3309
3310 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
3311 {
3312         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
3313
3314         qeth_l3_remove_device_attributes(&cgdev->dev);
3315
3316         qeth_set_allowed_threads(card, 0, 1);
3317         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
3318
3319         if (cgdev->state == CCWGROUP_ONLINE)
3320                 qeth_l3_set_offline(cgdev);
3321
3322         if (card->dev) {
3323                 unregister_netdev(card->dev);
3324                 card->dev = NULL;
3325         }
3326
3327         qeth_l3_clear_ip_list(card, 0);
3328         qeth_l3_clear_ipato_list(card);
3329         return;
3330 }
3331
3332 static int __qeth_l3_set_online(struct ccwgroup_device *gdev, int recovery_mode)
3333 {
3334         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3335         int rc = 0;
3336         enum qeth_card_states recover_flag;
3337
3338         BUG_ON(!card);
3339         mutex_lock(&card->discipline_mutex);
3340         mutex_lock(&card->conf_mutex);
3341         QETH_DBF_TEXT(SETUP, 2, "setonlin");
3342         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
3343
3344         recover_flag = card->state;
3345         rc = qeth_core_hardsetup_card(card);
3346         if (rc) {
3347                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
3348                 rc = -ENODEV;
3349                 goto out_remove;
3350         }
3351
3352         if (!card->dev && qeth_l3_setup_netdev(card)) {
3353                 rc = -ENODEV;
3354                 goto out_remove;
3355         }
3356
3357         if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
3358                 if (card->info.hwtrap &&
3359                     qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
3360                         card->info.hwtrap = 0;
3361         } else
3362                 card->info.hwtrap = 0;
3363
3364         card->state = CARD_STATE_HARDSETUP;
3365         memset(&card->rx, 0, sizeof(struct qeth_rx));
3366         qeth_print_status_message(card);
3367
3368         /* softsetup */
3369         QETH_DBF_TEXT(SETUP, 2, "softsetp");
3370
3371         rc = qeth_send_startlan(card);
3372         if (rc) {
3373                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
3374                 if (rc == 0xe080) {
3375                         dev_warn(&card->gdev->dev,
3376                                 "The LAN is offline\n");
3377                         card->lan_online = 0;
3378                         goto contin;
3379                 }
3380                 rc = -ENODEV;
3381                 goto out_remove;
3382         } else
3383                 card->lan_online = 1;
3384
3385 contin:
3386         rc = qeth_l3_setadapter_parms(card);
3387         if (rc)
3388                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
3389         if (!card->options.sniffer) {
3390                 rc = qeth_l3_start_ipassists(card);
3391                 if (rc)
3392                         QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
3393                 rc = qeth_l3_setrouting_v4(card);
3394                 if (rc)
3395                         QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
3396                 rc = qeth_l3_setrouting_v6(card);
3397                 if (rc)
3398                         QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
3399         }
3400         netif_tx_disable(card->dev);
3401
3402         rc = qeth_init_qdio_queues(card);
3403         if (rc) {
3404                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
3405                 rc = -ENODEV;
3406                 goto out_remove;
3407         }
3408         card->state = CARD_STATE_SOFTSETUP;
3409
3410         qeth_set_allowed_threads(card, 0xffffffff, 0);
3411         qeth_l3_set_ip_addr_list(card);
3412         if (card->lan_online)
3413                 netif_carrier_on(card->dev);
3414         else
3415                 netif_carrier_off(card->dev);
3416         if (recover_flag == CARD_STATE_RECOVER) {
3417                 rtnl_lock();
3418                 if (recovery_mode)
3419                         __qeth_l3_open(card->dev);
3420                 else
3421                         dev_open(card->dev);
3422                 qeth_l3_set_multicast_list(card->dev);
3423                 rtnl_unlock();
3424         }
3425         /* let user_space know that device is online */
3426         kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
3427         mutex_unlock(&card->conf_mutex);
3428         mutex_unlock(&card->discipline_mutex);
3429         return 0;
3430 out_remove:
3431         qeth_l3_stop_card(card, 0);
3432         ccw_device_set_offline(CARD_DDEV(card));
3433         ccw_device_set_offline(CARD_WDEV(card));
3434         ccw_device_set_offline(CARD_RDEV(card));
3435         if (recover_flag == CARD_STATE_RECOVER)
3436                 card->state = CARD_STATE_RECOVER;
3437         else
3438                 card->state = CARD_STATE_DOWN;
3439         mutex_unlock(&card->conf_mutex);
3440         mutex_unlock(&card->discipline_mutex);
3441         return rc;
3442 }
3443
3444 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
3445 {
3446         return __qeth_l3_set_online(gdev, 0);
3447 }
3448
3449 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
3450                         int recovery_mode)
3451 {
3452         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
3453         int rc = 0, rc2 = 0, rc3 = 0;
3454         enum qeth_card_states recover_flag;
3455
3456         mutex_lock(&card->discipline_mutex);
3457         mutex_lock(&card->conf_mutex);
3458         QETH_DBF_TEXT(SETUP, 3, "setoffl");
3459         QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
3460
3461         if (card->dev && netif_carrier_ok(card->dev))
3462                 netif_carrier_off(card->dev);
3463         recover_flag = card->state;
3464         if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
3465                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3466                 card->info.hwtrap = 1;
3467         }
3468         qeth_l3_stop_card(card, recovery_mode);
3469         if ((card->options.cq == QETH_CQ_ENABLED) && card->dev) {
3470                 rtnl_lock();
3471                 call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
3472                 rtnl_unlock();
3473         }
3474         rc  = ccw_device_set_offline(CARD_DDEV(card));
3475         rc2 = ccw_device_set_offline(CARD_WDEV(card));
3476         rc3 = ccw_device_set_offline(CARD_RDEV(card));
3477         if (!rc)
3478                 rc = (rc2) ? rc2 : rc3;
3479         if (rc)
3480                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
3481         if (recover_flag == CARD_STATE_UP)
3482                 card->state = CARD_STATE_RECOVER;
3483         /* let user_space know that device is offline */
3484         kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
3485         mutex_unlock(&card->conf_mutex);
3486         mutex_unlock(&card->discipline_mutex);
3487         return 0;
3488 }
3489
3490 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
3491 {
3492         return __qeth_l3_set_offline(cgdev, 0);
3493 }
3494
3495 static int qeth_l3_recover(void *ptr)
3496 {
3497         struct qeth_card *card;
3498         int rc = 0;
3499
3500         card = (struct qeth_card *) ptr;
3501         QETH_CARD_TEXT(card, 2, "recover1");
3502         QETH_CARD_HEX(card, 2, &card, sizeof(void *));
3503         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
3504                 return 0;
3505         QETH_CARD_TEXT(card, 2, "recover2");
3506         dev_warn(&card->gdev->dev,
3507                 "A recovery process has been started for the device\n");
3508         __qeth_l3_set_offline(card->gdev, 1);
3509         rc = __qeth_l3_set_online(card->gdev, 1);
3510         if (!rc)
3511                 dev_info(&card->gdev->dev,
3512                         "Device successfully recovered!\n");
3513         else {
3514                 rtnl_lock();
3515                 dev_close(card->dev);
3516                 rtnl_unlock();
3517                 dev_warn(&card->gdev->dev, "The qeth device driver "
3518                         "failed to recover an error on the device\n");
3519         }
3520         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
3521         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
3522         return 0;
3523 }
3524
3525 static void qeth_l3_shutdown(struct ccwgroup_device *gdev)
3526 {
3527         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3528         qeth_set_allowed_threads(card, 0, 1);
3529         if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
3530                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3531         qeth_qdio_clear_card(card, 0);
3532         qeth_clear_qdio_buffers(card);
3533 }
3534
3535 static int qeth_l3_pm_suspend(struct ccwgroup_device *gdev)
3536 {
3537         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3538
3539         if (card->dev)
3540                 netif_device_detach(card->dev);
3541         qeth_set_allowed_threads(card, 0, 1);
3542         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
3543         if (gdev->state == CCWGROUP_OFFLINE)
3544                 return 0;
3545         if (card->state == CARD_STATE_UP) {
3546                 if (card->info.hwtrap)
3547                         qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3548                 __qeth_l3_set_offline(card->gdev, 1);
3549         } else
3550                 __qeth_l3_set_offline(card->gdev, 0);
3551         return 0;
3552 }
3553
3554 static int qeth_l3_pm_resume(struct ccwgroup_device *gdev)
3555 {
3556         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3557         int rc = 0;
3558
3559         if (gdev->state == CCWGROUP_OFFLINE)
3560                 goto out;
3561
3562         if (card->state == CARD_STATE_RECOVER) {
3563                 rc = __qeth_l3_set_online(card->gdev, 1);
3564                 if (rc) {
3565                         rtnl_lock();
3566                         dev_close(card->dev);
3567                         rtnl_unlock();
3568                 }
3569         } else
3570                 rc = __qeth_l3_set_online(card->gdev, 0);
3571 out:
3572         qeth_set_allowed_threads(card, 0xffffffff, 0);
3573         if (card->dev)
3574                 netif_device_attach(card->dev);
3575         if (rc)
3576                 dev_warn(&card->gdev->dev, "The qeth device driver "
3577                         "failed to recover an error on the device\n");
3578         return rc;
3579 }
3580
3581 struct ccwgroup_driver qeth_l3_ccwgroup_driver = {
3582         .probe = qeth_l3_probe_device,
3583         .remove = qeth_l3_remove_device,
3584         .set_online = qeth_l3_set_online,
3585         .set_offline = qeth_l3_set_offline,
3586         .shutdown = qeth_l3_shutdown,
3587         .freeze = qeth_l3_pm_suspend,
3588         .thaw = qeth_l3_pm_resume,
3589         .restore = qeth_l3_pm_resume,
3590 };
3591 EXPORT_SYMBOL_GPL(qeth_l3_ccwgroup_driver);
3592
3593 static int qeth_l3_ip_event(struct notifier_block *this,
3594                             unsigned long event, void *ptr)
3595 {
3596         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3597         struct net_device *dev = (struct net_device *)ifa->ifa_dev->dev;
3598         struct qeth_ipaddr *addr;
3599         struct qeth_card *card;
3600
3601         if (dev_net(dev) != &init_net)
3602                 return NOTIFY_DONE;
3603
3604         card = qeth_l3_get_card_from_dev(dev);
3605         if (!card)
3606                 return NOTIFY_DONE;
3607         QETH_CARD_TEXT(card, 3, "ipevent");
3608
3609         addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
3610         if (addr != NULL) {
3611                 addr->u.a4.addr = ifa->ifa_address;
3612                 addr->u.a4.mask = ifa->ifa_mask;
3613                 addr->type = QETH_IP_TYPE_NORMAL;
3614         } else
3615                 goto out;
3616
3617         switch (event) {
3618         case NETDEV_UP:
3619                 if (!qeth_l3_add_ip(card, addr))
3620                         kfree(addr);
3621                 break;
3622         case NETDEV_DOWN:
3623                 if (!qeth_l3_delete_ip(card, addr))
3624                         kfree(addr);
3625                 break;
3626         default:
3627                 break;
3628         }
3629         qeth_l3_set_ip_addr_list(card);
3630 out:
3631         return NOTIFY_DONE;
3632 }
3633
3634 static struct notifier_block qeth_l3_ip_notifier = {
3635         qeth_l3_ip_event,
3636         NULL,
3637 };
3638
3639 #ifdef CONFIG_QETH_IPV6
3640 /**
3641  * IPv6 event handler
3642  */
3643 static int qeth_l3_ip6_event(struct notifier_block *this,
3644                              unsigned long event, void *ptr)
3645 {
3646         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
3647         struct net_device *dev = (struct net_device *)ifa->idev->dev;
3648         struct qeth_ipaddr *addr;
3649         struct qeth_card *card;
3650
3651         card = qeth_l3_get_card_from_dev(dev);
3652         if (!card)
3653                 return NOTIFY_DONE;
3654         QETH_CARD_TEXT(card, 3, "ip6event");
3655         if (!qeth_is_supported(card, IPA_IPV6))
3656                 return NOTIFY_DONE;
3657
3658         addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
3659         if (addr != NULL) {
3660                 memcpy(&addr->u.a6.addr, &ifa->addr, sizeof(struct in6_addr));
3661                 addr->u.a6.pfxlen = ifa->prefix_len;
3662                 addr->type = QETH_IP_TYPE_NORMAL;
3663         } else
3664                 goto out;
3665
3666         switch (event) {
3667         case NETDEV_UP:
3668                 if (!qeth_l3_add_ip(card, addr))
3669                         kfree(addr);
3670                 break;
3671         case NETDEV_DOWN:
3672                 if (!qeth_l3_delete_ip(card, addr))
3673                         kfree(addr);
3674                 break;
3675         default:
3676                 break;
3677         }
3678         qeth_l3_set_ip_addr_list(card);
3679 out:
3680         return NOTIFY_DONE;
3681 }
3682
3683 static struct notifier_block qeth_l3_ip6_notifier = {
3684         qeth_l3_ip6_event,
3685         NULL,
3686 };
3687 #endif
3688
3689 static int qeth_l3_register_notifiers(void)
3690 {
3691         int rc;
3692
3693         QETH_DBF_TEXT(SETUP, 5, "regnotif");
3694         rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
3695         if (rc)
3696                 return rc;
3697 #ifdef CONFIG_QETH_IPV6
3698         rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
3699         if (rc) {
3700                 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
3701                 return rc;
3702         }
3703 #else
3704         pr_warning("There is no IPv6 support for the layer 3 discipline\n");
3705 #endif
3706         return 0;
3707 }
3708
3709 static void qeth_l3_unregister_notifiers(void)
3710 {
3711
3712         QETH_DBF_TEXT(SETUP, 5, "unregnot");
3713         BUG_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
3714 #ifdef CONFIG_QETH_IPV6
3715         BUG_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
3716 #endif /* QETH_IPV6 */
3717 }
3718
3719 static int __init qeth_l3_init(void)
3720 {
3721         int rc = 0;
3722
3723         pr_info("register layer 3 discipline\n");
3724         rc = qeth_l3_register_notifiers();
3725         return rc;
3726 }
3727
3728 static void __exit qeth_l3_exit(void)
3729 {
3730         qeth_l3_unregister_notifiers();
3731         pr_info("unregister layer 3 discipline\n");
3732 }
3733
3734 module_init(qeth_l3_init);
3735 module_exit(qeth_l3_exit);
3736 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
3737 MODULE_DESCRIPTION("qeth layer 3 discipline");
3738 MODULE_LICENSE("GPL");