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