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