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