Merge branch 'master' of /usr/src/ntfs-2.6/
[linux-2.6] / drivers / s390 / net / qeth_main.c
1 /*
2  *
3  * linux/drivers/s390/net/qeth_main.c ($Revision: 1.224 $)
4  *
5  * Linux on zSeries OSA Express and HiperSockets support
6  *
7  * Copyright 2000,2003 IBM Corporation
8  *
9  *    Author(s): Original Code written by
10  *                        Utz Bacher (utz.bacher@de.ibm.com)
11  *               Rewritten by
12  *                        Frank Pavlic (pavlic@de.ibm.com) and
13  *                        Thomas Spatzier <tspat@de.ibm.com>
14  *
15  *    $Revision: 1.224 $         $Date: 2005/05/04 20:19:18 $
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2, or (at your option)
20  * any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30  */
31
32
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/mm.h>
39 #include <linux/ip.h>
40 #include <linux/inetdevice.h>
41 #include <linux/netdevice.h>
42 #include <linux/sched.h>
43 #include <linux/workqueue.h>
44 #include <linux/kernel.h>
45 #include <linux/slab.h>
46 #include <linux/interrupt.h>
47 #include <linux/tcp.h>
48 #include <linux/icmp.h>
49 #include <linux/skbuff.h>
50 #include <linux/in.h>
51 #include <linux/igmp.h>
52 #include <linux/init.h>
53 #include <linux/reboot.h>
54 #include <linux/mii.h>
55 #include <linux/rcupdate.h>
56 #include <linux/ethtool.h>
57
58 #include <net/arp.h>
59 #include <net/ip.h>
60 #include <net/route.h>
61
62 #include <asm/ebcdic.h>
63 #include <asm/io.h>
64 #include <asm/qeth.h>
65 #include <asm/timex.h>
66 #include <asm/semaphore.h>
67 #include <asm/uaccess.h>
68
69 #include "qeth.h"
70 #include "qeth_mpc.h"
71 #include "qeth_fs.h"
72 #include "qeth_eddp.h"
73 #include "qeth_tso.h"
74
75 #define VERSION_QETH_C "$Revision: 1.224 $"
76 static const char *version = "qeth S/390 OSA-Express driver";
77
78 /**
79  * Debug Facility Stuff
80  */
81 static debug_info_t *qeth_dbf_setup = NULL;
82 static debug_info_t *qeth_dbf_data = NULL;
83 static debug_info_t *qeth_dbf_misc = NULL;
84 static debug_info_t *qeth_dbf_control = NULL;
85 debug_info_t *qeth_dbf_trace = NULL;
86 static debug_info_t *qeth_dbf_sense = NULL;
87 static debug_info_t *qeth_dbf_qerr = NULL;
88
89 DEFINE_PER_CPU(char[256], qeth_dbf_txt_buf);
90
91 /**
92  * some more definitions and declarations
93  */
94 static unsigned int known_devices[][10] = QETH_MODELLIST_ARRAY;
95
96 /* list of our cards */
97 struct qeth_card_list_struct qeth_card_list;
98 /*process list want to be notified*/
99 spinlock_t qeth_notify_lock;
100 struct list_head qeth_notify_list;
101
102 static void qeth_send_control_data_cb(struct qeth_channel *,
103                                       struct qeth_cmd_buffer *);
104
105 /**
106  * here we go with function implementation
107  */
108 static void
109 qeth_init_qdio_info(struct qeth_card *card);
110
111 static int
112 qeth_init_qdio_queues(struct qeth_card *card);
113
114 static int
115 qeth_alloc_qdio_buffers(struct qeth_card *card);
116
117 static void
118 qeth_free_qdio_buffers(struct qeth_card *);
119
120 static void
121 qeth_clear_qdio_buffers(struct qeth_card *);
122
123 static void
124 qeth_clear_ip_list(struct qeth_card *, int, int);
125
126 static void
127 qeth_clear_ipacmd_list(struct qeth_card *);
128
129 static int
130 qeth_qdio_clear_card(struct qeth_card *, int);
131
132 static void
133 qeth_clear_working_pool_list(struct qeth_card *);
134
135 static void
136 qeth_clear_cmd_buffers(struct qeth_channel *);
137
138 static int
139 qeth_stop(struct net_device *);
140
141 static void
142 qeth_clear_ipato_list(struct qeth_card *);
143
144 static int
145 qeth_is_addr_covered_by_ipato(struct qeth_card *, struct qeth_ipaddr *);
146
147 static void
148 qeth_irq_tasklet(unsigned long);
149
150 static int
151 qeth_set_online(struct ccwgroup_device *);
152
153 static int
154 __qeth_set_online(struct ccwgroup_device *gdev, int recovery_mode);
155
156 static struct qeth_ipaddr *
157 qeth_get_addr_buffer(enum qeth_prot_versions);
158
159 static void
160 qeth_set_multicast_list(struct net_device *);
161
162 static void
163 qeth_notify_processes(void)
164 {
165         /*notify all  registered processes */
166         struct qeth_notify_list_struct *n_entry;
167
168         QETH_DBF_TEXT(trace,3,"procnoti");
169         spin_lock(&qeth_notify_lock);
170         list_for_each_entry(n_entry, &qeth_notify_list, list) {
171                 send_sig(n_entry->signum, n_entry->task, 1);
172         }
173         spin_unlock(&qeth_notify_lock);
174
175 }
176 int
177 qeth_notifier_unregister(struct task_struct *p)
178 {
179         struct qeth_notify_list_struct *n_entry, *tmp;
180
181         QETH_DBF_TEXT(trace, 2, "notunreg");
182         spin_lock(&qeth_notify_lock);
183         list_for_each_entry_safe(n_entry, tmp, &qeth_notify_list, list) {
184                 if (n_entry->task == p) {
185                         list_del(&n_entry->list);
186                         kfree(n_entry);
187                         goto out;
188                 }
189         }
190 out:
191         spin_unlock(&qeth_notify_lock);
192         return 0;
193 }
194 int
195 qeth_notifier_register(struct task_struct *p, int signum)
196 {
197         struct qeth_notify_list_struct *n_entry;
198
199         /*check first if entry already exists*/
200         spin_lock(&qeth_notify_lock);
201         list_for_each_entry(n_entry, &qeth_notify_list, list) {
202                 if (n_entry->task == p) {
203                         n_entry->signum = signum;
204                         spin_unlock(&qeth_notify_lock);
205                         return 0;
206                 }
207         }
208         spin_unlock(&qeth_notify_lock);
209
210         n_entry = (struct qeth_notify_list_struct *)
211                 kmalloc(sizeof(struct qeth_notify_list_struct),GFP_KERNEL);
212         if (!n_entry)
213                 return -ENOMEM;
214         n_entry->task = p;
215         n_entry->signum = signum;
216         spin_lock(&qeth_notify_lock);
217         list_add(&n_entry->list,&qeth_notify_list);
218         spin_unlock(&qeth_notify_lock);
219         return 0;
220 }
221
222
223 /**
224  * free channel command buffers
225  */
226 static void
227 qeth_clean_channel(struct qeth_channel *channel)
228 {
229         int cnt;
230
231         QETH_DBF_TEXT(setup, 2, "freech");
232         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
233                 kfree(channel->iob[cnt].data);
234 }
235
236 /**
237  * free card
238  */
239 static void
240 qeth_free_card(struct qeth_card *card)
241 {
242
243         QETH_DBF_TEXT(setup, 2, "freecrd");
244         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
245         qeth_clean_channel(&card->read);
246         qeth_clean_channel(&card->write);
247         if (card->dev)
248                 free_netdev(card->dev);
249         qeth_clear_ip_list(card, 0, 0);
250         qeth_clear_ipato_list(card);
251         kfree(card->ip_tbd_list);
252         qeth_free_qdio_buffers(card);
253         kfree(card);
254 }
255
256 /**
257  * alloc memory for command buffer per channel
258  */
259 static int
260 qeth_setup_channel(struct qeth_channel *channel)
261 {
262         int cnt;
263
264         QETH_DBF_TEXT(setup, 2, "setupch");
265         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
266                 channel->iob[cnt].data = (char *)
267                         kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
268                 if (channel->iob[cnt].data == NULL)
269                         break;
270                 channel->iob[cnt].state = BUF_STATE_FREE;
271                 channel->iob[cnt].channel = channel;
272                 channel->iob[cnt].callback = qeth_send_control_data_cb;
273                 channel->iob[cnt].rc = 0;
274         }
275         if (cnt < QETH_CMD_BUFFER_NO) {
276                 while (cnt-- > 0)
277                         kfree(channel->iob[cnt].data);
278                 return -ENOMEM;
279         }
280         channel->buf_no = 0;
281         channel->io_buf_no = 0;
282         atomic_set(&channel->irq_pending, 0);
283         spin_lock_init(&channel->iob_lock);
284
285         init_waitqueue_head(&channel->wait_q);
286         channel->irq_tasklet.data = (unsigned long) channel;
287         channel->irq_tasklet.func = qeth_irq_tasklet;
288         return 0;
289 }
290
291 /**
292  * alloc memory for card structure
293  */
294 static struct qeth_card *
295 qeth_alloc_card(void)
296 {
297         struct qeth_card *card;
298
299         QETH_DBF_TEXT(setup, 2, "alloccrd");
300         card = (struct qeth_card *) kmalloc(sizeof(struct qeth_card),
301                                             GFP_DMA|GFP_KERNEL);
302         if (!card)
303                 return NULL;
304         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
305         memset(card, 0, sizeof(struct qeth_card));
306         if (qeth_setup_channel(&card->read)) {
307                 kfree(card);
308                 return NULL;
309         }
310         if (qeth_setup_channel(&card->write)) {
311                 qeth_clean_channel(&card->read);
312                 kfree(card);
313                 return NULL;
314         }
315         return card;
316 }
317
318 static long
319 __qeth_check_irb_error(struct ccw_device *cdev, struct irb *irb)
320 {
321         if (!IS_ERR(irb))
322                 return 0;
323
324         switch (PTR_ERR(irb)) {
325         case -EIO:
326                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
327                 QETH_DBF_TEXT(trace, 2, "ckirberr");
328                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
329                 break;
330         case -ETIMEDOUT:
331                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
332                 QETH_DBF_TEXT(trace, 2, "ckirberr");
333                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -ETIMEDOUT);
334                 break;
335         default:
336                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
337                            cdev->dev.bus_id);
338                 QETH_DBF_TEXT(trace, 2, "ckirberr");
339                 QETH_DBF_TEXT(trace, 2, "  rc???");
340         }
341         return PTR_ERR(irb);
342 }
343
344 static int
345 qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
346 {
347         int dstat,cstat;
348         char *sense;
349
350         sense = (char *) irb->ecw;
351         cstat = irb->scsw.cstat;
352         dstat = irb->scsw.dstat;
353
354         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
355                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
356                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
357                 QETH_DBF_TEXT(trace,2, "CGENCHK");
358                 PRINT_WARN("check on device %s, dstat=x%x, cstat=x%x ",
359                            cdev->dev.bus_id, dstat, cstat);
360                 HEXDUMP16(WARN, "irb: ", irb);
361                 HEXDUMP16(WARN, "irb: ", ((char *) irb) + 32);
362                 return 1;
363         }
364
365         if (dstat & DEV_STAT_UNIT_CHECK) {
366                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
367                     SENSE_RESETTING_EVENT_FLAG) {
368                         QETH_DBF_TEXT(trace,2,"REVIND");
369                         return 1;
370                 }
371                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
372                     SENSE_COMMAND_REJECT_FLAG) {
373                         QETH_DBF_TEXT(trace,2,"CMDREJi");
374                         return 0;
375                 }
376                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
377                         QETH_DBF_TEXT(trace,2,"AFFE");
378                         return 1;
379                 }
380                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
381                         QETH_DBF_TEXT(trace,2,"ZEROSEN");
382                         return 0;
383                 }
384                 QETH_DBF_TEXT(trace,2,"DGENCHK");
385                         return 1;
386         }
387         return 0;
388 }
389 static int qeth_issue_next_read(struct qeth_card *);
390
391 /**
392  * interrupt handler
393  */
394 static void
395 qeth_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
396 {
397         int rc;
398         int cstat,dstat;
399         struct qeth_cmd_buffer *buffer;
400         struct qeth_channel *channel;
401         struct qeth_card *card;
402
403         QETH_DBF_TEXT(trace,5,"irq");
404
405         if (__qeth_check_irb_error(cdev, irb))
406                 return;
407         cstat = irb->scsw.cstat;
408         dstat = irb->scsw.dstat;
409
410         card = CARD_FROM_CDEV(cdev);
411         if (!card)
412                 return;
413
414         if (card->read.ccwdev == cdev){
415                 channel = &card->read;
416                 QETH_DBF_TEXT(trace,5,"read");
417         } else if (card->write.ccwdev == cdev) {
418                 channel = &card->write;
419                 QETH_DBF_TEXT(trace,5,"write");
420         } else {
421                 channel = &card->data;
422                 QETH_DBF_TEXT(trace,5,"data");
423         }
424         atomic_set(&channel->irq_pending, 0);
425
426         if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC))
427                 channel->state = CH_STATE_STOPPED;
428
429         if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC))
430                 channel->state = CH_STATE_HALTED;
431
432         /*let's wake up immediately on data channel*/
433         if ((channel == &card->data) && (intparm != 0))
434                 goto out;
435
436         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
437                 QETH_DBF_TEXT(trace, 6, "clrchpar");
438                 /* we don't have to handle this further */
439                 intparm = 0;
440         }
441         if (intparm == QETH_HALT_CHANNEL_PARM) {
442                 QETH_DBF_TEXT(trace, 6, "hltchpar");
443                 /* we don't have to handle this further */
444                 intparm = 0;
445         }
446         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
447             (dstat & DEV_STAT_UNIT_CHECK) ||
448             (cstat)) {
449                 if (irb->esw.esw0.erw.cons) {
450                         /* TODO: we should make this s390dbf */
451                         PRINT_WARN("sense data available on channel %s.\n",
452                                    CHANNEL_ID(channel));
453                         PRINT_WARN(" cstat 0x%X\n dstat 0x%X\n", cstat, dstat);
454                         HEXDUMP16(WARN,"irb: ",irb);
455                         HEXDUMP16(WARN,"sense data: ",irb->ecw);
456                 }
457                 rc = qeth_get_problem(cdev,irb);
458                 if (rc) {
459                         qeth_schedule_recovery(card);
460                         goto out;
461                 }
462         }
463
464         if (intparm) {
465                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
466                 buffer->state = BUF_STATE_PROCESSED;
467         }
468         if (channel == &card->data)
469                 return;
470
471         if (channel == &card->read &&
472             channel->state == CH_STATE_UP)
473                 qeth_issue_next_read(card);
474
475         tasklet_schedule(&channel->irq_tasklet);
476         return;
477 out:
478         wake_up(&card->wait_q);
479 }
480
481 /**
482  * tasklet function scheduled from irq handler
483  */
484 static void
485 qeth_irq_tasklet(unsigned long data)
486 {
487         struct qeth_card *card;
488         struct qeth_channel *channel;
489         struct qeth_cmd_buffer *iob;
490         __u8 index;
491
492         QETH_DBF_TEXT(trace,5,"irqtlet");
493         channel = (struct qeth_channel *) data;
494         iob = channel->iob;
495         index = channel->buf_no;
496         card = CARD_FROM_CDEV(channel->ccwdev);
497         while (iob[index].state == BUF_STATE_PROCESSED) {
498                 if (iob[index].callback !=NULL) {
499                         iob[index].callback(channel,iob + index);
500                 }
501                 index = (index + 1) % QETH_CMD_BUFFER_NO;
502         }
503         channel->buf_no = index;
504         wake_up(&card->wait_q);
505 }
506
507 static int qeth_stop_card(struct qeth_card *, int);
508
509 static int
510 __qeth_set_offline(struct ccwgroup_device *cgdev, int recovery_mode)
511 {
512         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
513         int rc = 0, rc2 = 0, rc3 = 0;
514         enum qeth_card_states recover_flag;
515
516         QETH_DBF_TEXT(setup, 3, "setoffl");
517         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
518
519         recover_flag = card->state;
520         if (qeth_stop_card(card, recovery_mode) == -ERESTARTSYS){
521                 PRINT_WARN("Stopping card %s interrupted by user!\n",
522                            CARD_BUS_ID(card));
523                 return -ERESTARTSYS;
524         }
525         rc  = ccw_device_set_offline(CARD_DDEV(card));
526         rc2 = ccw_device_set_offline(CARD_WDEV(card));
527         rc3 = ccw_device_set_offline(CARD_RDEV(card));
528         if (!rc)
529                 rc = (rc2) ? rc2 : rc3;
530         if (rc)
531                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
532         if (recover_flag == CARD_STATE_UP)
533                 card->state = CARD_STATE_RECOVER;
534         qeth_notify_processes();
535         return 0;
536 }
537
538 static int
539 qeth_set_offline(struct ccwgroup_device *cgdev)
540 {
541         return  __qeth_set_offline(cgdev, 0);
542 }
543
544 static int
545 qeth_wait_for_threads(struct qeth_card *card, unsigned long threads);
546
547
548 static void
549 qeth_remove_device(struct ccwgroup_device *cgdev)
550 {
551         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
552         unsigned long flags;
553
554         QETH_DBF_TEXT(setup, 3, "rmdev");
555         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
556
557         if (!card)
558                 return;
559
560         if (qeth_wait_for_threads(card, 0xffffffff))
561                 return;
562
563         if (cgdev->state == CCWGROUP_ONLINE){
564                 card->use_hard_stop = 1;
565                 qeth_set_offline(cgdev);
566         }
567         /* remove form our internal list */
568         write_lock_irqsave(&qeth_card_list.rwlock, flags);
569         list_del(&card->list);
570         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
571         if (card->dev)
572                 unregister_netdev(card->dev);
573         qeth_remove_device_attributes(&cgdev->dev);
574         qeth_free_card(card);
575         cgdev->dev.driver_data = NULL;
576         put_device(&cgdev->dev);
577 }
578
579 static int
580 qeth_register_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
581 static int
582 qeth_deregister_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
583
584 /**
585  * Add/remove address to/from card's ip list, i.e. try to add or remove
586  * reference to/from an IP address that is already registered on the card.
587  * Returns:
588  *      0  address was on card and its reference count has been adjusted,
589  *         but is still > 0, so nothing has to be done
590  *         also returns 0 if card was not on card and the todo was to delete
591  *         the address -> there is also nothing to be done
592  *      1  address was not on card and the todo is to add it to the card's ip
593  *         list
594  *      -1 address was on card and its reference count has been decremented
595  *         to <= 0 by the todo -> address must be removed from card
596  */
597 static int
598 __qeth_ref_ip_on_card(struct qeth_card *card, struct qeth_ipaddr *todo,
599                       struct qeth_ipaddr **__addr)
600 {
601         struct qeth_ipaddr *addr;
602         int found = 0;
603
604         list_for_each_entry(addr, &card->ip_list, entry) {
605                 if ((addr->proto     == QETH_PROT_IPV4)  &&
606                     (todo->proto     == QETH_PROT_IPV4)  &&
607                     (addr->type      == todo->type)      &&
608                     (addr->u.a4.addr == todo->u.a4.addr) &&
609                     (addr->u.a4.mask == todo->u.a4.mask)   ){
610                         found = 1;
611                         break;
612                 }
613                 if ((addr->proto       == QETH_PROT_IPV6)     &&
614                     (todo->proto       == QETH_PROT_IPV6)     &&
615                     (addr->type        == todo->type)         &&
616                     (addr->u.a6.pfxlen == todo->u.a6.pfxlen)  &&
617                     (memcmp(&addr->u.a6.addr, &todo->u.a6.addr,
618                             sizeof(struct in6_addr)) == 0))     {
619                         found = 1;
620                         break;
621                 }
622         }
623         if (found){
624                 addr->users += todo->users;
625                 if (addr->users <= 0){
626                         *__addr = addr;
627                         return -1;
628                 } else {
629                         /* for VIPA and RXIP limit refcount to 1 */
630                         if (addr->type != QETH_IP_TYPE_NORMAL)
631                                 addr->users = 1;
632                         return 0;
633                 }
634         }
635         if (todo->users > 0){
636                 /* for VIPA and RXIP limit refcount to 1 */
637                 if (todo->type != QETH_IP_TYPE_NORMAL)
638                         todo->users = 1;
639                 return 1;
640         } else
641                 return 0;
642 }
643
644 static inline int
645 __qeth_address_exists_in_list(struct list_head *list, struct qeth_ipaddr *addr,
646                               int same_type)
647 {
648         struct qeth_ipaddr *tmp;
649
650         list_for_each_entry(tmp, list, entry) {
651                 if ((tmp->proto     == QETH_PROT_IPV4)            &&
652                     (addr->proto    == QETH_PROT_IPV4)            &&
653                     ((same_type && (tmp->type == addr->type)) ||
654                      (!same_type && (tmp->type != addr->type))  ) &&
655                     (tmp->u.a4.addr == addr->u.a4.addr)             ){
656                         return 1;
657                 }
658                 if ((tmp->proto  == QETH_PROT_IPV6)               &&
659                     (addr->proto == QETH_PROT_IPV6)               &&
660                     ((same_type && (tmp->type == addr->type)) ||
661                      (!same_type && (tmp->type != addr->type))  ) &&
662                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
663                             sizeof(struct in6_addr)) == 0)          ) {
664                         return 1;
665                 }
666         }
667         return 0;
668 }
669
670 /*
671  * Add IP to be added to todo list. If there is already an "add todo"
672  * in this list we just incremenent the reference count.
673  * Returns 0 if we  just incremented reference count.
674  */
675 static int
676 __qeth_insert_ip_todo(struct qeth_card *card, struct qeth_ipaddr *addr, int add)
677 {
678         struct qeth_ipaddr *tmp, *t;
679         int found = 0;
680
681         list_for_each_entry_safe(tmp, t, card->ip_tbd_list, entry) {
682                 if ((addr->type == QETH_IP_TYPE_DEL_ALL_MC) &&
683                     (tmp->type == QETH_IP_TYPE_DEL_ALL_MC))
684                         return 0;
685                 if ((tmp->proto        == QETH_PROT_IPV4)     &&
686                     (addr->proto       == QETH_PROT_IPV4)     &&
687                     (tmp->type         == addr->type)         &&
688                     (tmp->is_multicast == addr->is_multicast) &&
689                     (tmp->u.a4.addr    == addr->u.a4.addr)    &&
690                     (tmp->u.a4.mask    == addr->u.a4.mask)      ){
691                         found = 1;
692                         break;
693                 }
694                 if ((tmp->proto        == QETH_PROT_IPV6)      &&
695                     (addr->proto       == QETH_PROT_IPV6)      &&
696                     (tmp->type         == addr->type)          &&
697                     (tmp->is_multicast == addr->is_multicast)  &&
698                     (tmp->u.a6.pfxlen  == addr->u.a6.pfxlen)   &&
699                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
700                             sizeof(struct in6_addr)) == 0)        ){
701                         found = 1;
702                         break;
703                 }
704         }
705         if (found){
706                 if (addr->users != 0)
707                         tmp->users += addr->users;
708                 else
709                         tmp->users += add? 1:-1;
710                 if (tmp->users == 0){
711                         list_del(&tmp->entry);
712                         kfree(tmp);
713                 }
714                 return 0;
715         } else {
716                 if (addr->type == QETH_IP_TYPE_DEL_ALL_MC)
717                         list_add(&addr->entry, card->ip_tbd_list);
718                 else {
719                         if (addr->users == 0)
720                                 addr->users += add? 1:-1;
721                         if (add && (addr->type == QETH_IP_TYPE_NORMAL) &&
722                             qeth_is_addr_covered_by_ipato(card, addr)){
723                                 QETH_DBF_TEXT(trace, 2, "tkovaddr");
724                                 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
725                         }
726                         list_add_tail(&addr->entry, card->ip_tbd_list);
727                 }
728                 return 1;
729         }
730 }
731
732 /**
733  * Remove IP address from list
734  */
735 static int
736 qeth_delete_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
737 {
738         unsigned long flags;
739         int rc = 0;
740
741         QETH_DBF_TEXT(trace,4,"delip");
742         if (addr->proto == QETH_PROT_IPV4)
743                 QETH_DBF_HEX(trace,4,&addr->u.a4.addr,4);
744         else {
745                 QETH_DBF_HEX(trace,4,&addr->u.a6.addr,8);
746                 QETH_DBF_HEX(trace,4,((char *)&addr->u.a6.addr)+8,8);
747         }
748         spin_lock_irqsave(&card->ip_lock, flags);
749         rc = __qeth_insert_ip_todo(card, addr, 0);
750         spin_unlock_irqrestore(&card->ip_lock, flags);
751         return rc;
752 }
753
754 static int
755 qeth_add_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
756 {
757         unsigned long flags;
758         int rc = 0;
759
760         QETH_DBF_TEXT(trace,4,"addip");
761         if (addr->proto == QETH_PROT_IPV4)
762                 QETH_DBF_HEX(trace,4,&addr->u.a4.addr,4);
763         else {
764                 QETH_DBF_HEX(trace,4,&addr->u.a6.addr,8);
765                 QETH_DBF_HEX(trace,4,((char *)&addr->u.a6.addr)+8,8);
766         }
767         spin_lock_irqsave(&card->ip_lock, flags);
768         rc = __qeth_insert_ip_todo(card, addr, 1);
769         spin_unlock_irqrestore(&card->ip_lock, flags);
770         return rc;
771 }
772
773 static inline void
774 __qeth_delete_all_mc(struct qeth_card *card, unsigned long *flags)
775 {
776         struct qeth_ipaddr *addr, *tmp;
777         int rc;
778
779         list_for_each_entry_safe(addr, tmp, &card->ip_list, entry) {
780                 if (addr->is_multicast) {
781                         spin_unlock_irqrestore(&card->ip_lock, *flags);
782                         rc = qeth_deregister_addr_entry(card, addr);
783                         spin_lock_irqsave(&card->ip_lock, *flags);
784                         if (!rc) {
785                                 list_del(&addr->entry);
786                                 kfree(addr);
787                         }
788                 }
789         }
790 }
791
792 static void
793 qeth_set_ip_addr_list(struct qeth_card *card)
794 {
795         struct list_head *tbd_list;
796         struct qeth_ipaddr *todo, *addr;
797         unsigned long flags;
798         int rc;
799
800         QETH_DBF_TEXT(trace, 2, "sdiplist");
801         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
802
803         spin_lock_irqsave(&card->ip_lock, flags);
804         tbd_list = card->ip_tbd_list;
805         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
806         if (!card->ip_tbd_list) {
807                 QETH_DBF_TEXT(trace, 0, "silnomem");
808                 card->ip_tbd_list = tbd_list;
809                 spin_unlock_irqrestore(&card->ip_lock, flags);
810                 return;
811         } else
812                 INIT_LIST_HEAD(card->ip_tbd_list);
813
814         while (!list_empty(tbd_list)){
815                 todo = list_entry(tbd_list->next, struct qeth_ipaddr, entry);
816                 list_del(&todo->entry);
817                 if (todo->type == QETH_IP_TYPE_DEL_ALL_MC){
818                         __qeth_delete_all_mc(card, &flags);
819                         kfree(todo);
820                         continue;
821                 }
822                 rc = __qeth_ref_ip_on_card(card, todo, &addr);
823                 if (rc == 0) {
824                         /* nothing to be done; only adjusted refcount */
825                         kfree(todo);
826                 } else if (rc == 1) {
827                         /* new entry to be added to on-card list */
828                         spin_unlock_irqrestore(&card->ip_lock, flags);
829                         rc = qeth_register_addr_entry(card, todo);
830                         spin_lock_irqsave(&card->ip_lock, flags);
831                         if (!rc)
832                                 list_add_tail(&todo->entry, &card->ip_list);
833                         else
834                                 kfree(todo);
835                 } else if (rc == -1) {
836                         /* on-card entry to be removed */
837                         list_del_init(&addr->entry);
838                         spin_unlock_irqrestore(&card->ip_lock, flags);
839                         rc = qeth_deregister_addr_entry(card, addr);
840                         spin_lock_irqsave(&card->ip_lock, flags);
841                         if (!rc)
842                                 kfree(addr);
843                         else
844                                 list_add_tail(&addr->entry, &card->ip_list);
845                         kfree(todo);
846                 }
847         }
848         spin_unlock_irqrestore(&card->ip_lock, flags);
849         kfree(tbd_list);
850 }
851
852 static void qeth_delete_mc_addresses(struct qeth_card *);
853 static void qeth_add_multicast_ipv4(struct qeth_card *);
854 #ifdef CONFIG_QETH_IPV6
855 static void qeth_add_multicast_ipv6(struct qeth_card *);
856 #endif
857
858 static inline int
859 qeth_set_thread_start_bit(struct qeth_card *card, unsigned long thread)
860 {
861         unsigned long flags;
862
863         spin_lock_irqsave(&card->thread_mask_lock, flags);
864         if ( !(card->thread_allowed_mask & thread) ||
865               (card->thread_start_mask & thread) ) {
866                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
867                 return -EPERM;
868         }
869         card->thread_start_mask |= thread;
870         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
871         return 0;
872 }
873
874 static void
875 qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
876 {
877         unsigned long flags;
878
879         spin_lock_irqsave(&card->thread_mask_lock, flags);
880         card->thread_start_mask &= ~thread;
881         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
882         wake_up(&card->wait_q);
883 }
884
885 static void
886 qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
887 {
888         unsigned long flags;
889
890         spin_lock_irqsave(&card->thread_mask_lock, flags);
891         card->thread_running_mask &= ~thread;
892         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
893         wake_up(&card->wait_q);
894 }
895
896 static inline int
897 __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
898 {
899         unsigned long flags;
900         int rc = 0;
901
902         spin_lock_irqsave(&card->thread_mask_lock, flags);
903         if (card->thread_start_mask & thread){
904                 if ((card->thread_allowed_mask & thread) &&
905                     !(card->thread_running_mask & thread)){
906                         rc = 1;
907                         card->thread_start_mask &= ~thread;
908                         card->thread_running_mask |= thread;
909                 } else
910                         rc = -EPERM;
911         }
912         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
913         return rc;
914 }
915
916 static int
917 qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
918 {
919         int rc = 0;
920
921         wait_event(card->wait_q,
922                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
923         return rc;
924 }
925
926 static int
927 qeth_register_ip_addresses(void *ptr)
928 {
929         struct qeth_card *card;
930
931         card = (struct qeth_card *) ptr;
932         daemonize("qeth_reg_ip");
933         QETH_DBF_TEXT(trace,4,"regipth1");
934         if (!qeth_do_run_thread(card, QETH_SET_IP_THREAD))
935                 return 0;
936         QETH_DBF_TEXT(trace,4,"regipth2");
937         qeth_set_ip_addr_list(card);
938         qeth_clear_thread_running_bit(card, QETH_SET_IP_THREAD);
939         return 0;
940 }
941
942 static int
943 qeth_recover(void *ptr)
944 {
945         struct qeth_card *card;
946         int rc = 0;
947
948         card = (struct qeth_card *) ptr;
949         daemonize("qeth_recover");
950         QETH_DBF_TEXT(trace,2,"recover1");
951         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
952         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
953                 return 0;
954         QETH_DBF_TEXT(trace,2,"recover2");
955         PRINT_WARN("Recovery of device %s started ...\n",
956                    CARD_BUS_ID(card));
957         card->use_hard_stop = 1;
958         __qeth_set_offline(card->gdev,1);
959         rc = __qeth_set_online(card->gdev,1);
960         if (!rc)
961                 PRINT_INFO("Device %s successfully recovered!\n",
962                            CARD_BUS_ID(card));
963         else
964                 PRINT_INFO("Device %s could not be recovered!\n",
965                            CARD_BUS_ID(card));
966         /* don't run another scheduled recovery */
967         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
968         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
969         return 0;
970 }
971
972 void
973 qeth_schedule_recovery(struct qeth_card *card)
974 {
975         QETH_DBF_TEXT(trace,2,"startrec");
976
977         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
978                 schedule_work(&card->kernel_thread_starter);
979 }
980
981 static int
982 qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
983 {
984         unsigned long flags;
985         int rc = 0;
986
987         spin_lock_irqsave(&card->thread_mask_lock, flags);
988         QETH_DBF_TEXT_(trace, 4, "  %02x%02x%02x",
989                         (u8) card->thread_start_mask,
990                         (u8) card->thread_allowed_mask,
991                         (u8) card->thread_running_mask);
992         rc = (card->thread_start_mask & thread);
993         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
994         return rc;
995 }
996
997 static void
998 qeth_start_kernel_thread(struct qeth_card *card)
999 {
1000         QETH_DBF_TEXT(trace , 2, "strthrd");
1001
1002         if (card->read.state != CH_STATE_UP &&
1003             card->write.state != CH_STATE_UP)
1004                 return;
1005
1006         if (qeth_do_start_thread(card, QETH_SET_IP_THREAD))
1007                 kernel_thread(qeth_register_ip_addresses, (void *)card,SIGCHLD);
1008         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1009                 kernel_thread(qeth_recover, (void *) card, SIGCHLD);
1010 }
1011
1012
1013 static void
1014 qeth_set_intial_options(struct qeth_card *card)
1015 {
1016         card->options.route4.type = NO_ROUTER;
1017 #ifdef CONFIG_QETH_IPV6
1018         card->options.route6.type = NO_ROUTER;
1019 #endif /* QETH_IPV6 */
1020         card->options.checksum_type = QETH_CHECKSUM_DEFAULT;
1021         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1022         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1023         card->options.fake_broadcast = 0;
1024         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1025         card->options.fake_ll = 0;
1026         if (card->info.type == QETH_CARD_TYPE_OSN)
1027                 card->options.layer2 = 1;
1028         else
1029                 card->options.layer2 = 0;
1030 }
1031
1032 /**
1033  * initialize channels ,card and all state machines
1034  */
1035 static int
1036 qeth_setup_card(struct qeth_card *card)
1037 {
1038
1039         QETH_DBF_TEXT(setup, 2, "setupcrd");
1040         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
1041
1042         card->read.state  = CH_STATE_DOWN;
1043         card->write.state = CH_STATE_DOWN;
1044         card->data.state  = CH_STATE_DOWN;
1045         card->state = CARD_STATE_DOWN;
1046         card->lan_online = 0;
1047         card->use_hard_stop = 0;
1048         card->dev = NULL;
1049 #ifdef CONFIG_QETH_VLAN
1050         spin_lock_init(&card->vlanlock);
1051         card->vlangrp = NULL;
1052 #endif
1053         spin_lock_init(&card->lock);
1054         spin_lock_init(&card->ip_lock);
1055         spin_lock_init(&card->thread_mask_lock);
1056         card->thread_start_mask = 0;
1057         card->thread_allowed_mask = 0;
1058         card->thread_running_mask = 0;
1059         INIT_WORK(&card->kernel_thread_starter,
1060                   (void *)qeth_start_kernel_thread,card);
1061         INIT_LIST_HEAD(&card->ip_list);
1062         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1063         if (!card->ip_tbd_list) {
1064                 QETH_DBF_TEXT(setup, 0, "iptbdnom");
1065                 return -ENOMEM;
1066         }
1067         INIT_LIST_HEAD(card->ip_tbd_list);
1068         INIT_LIST_HEAD(&card->cmd_waiter_list);
1069         init_waitqueue_head(&card->wait_q);
1070         /* intial options */
1071         qeth_set_intial_options(card);
1072         /* IP address takeover */
1073         INIT_LIST_HEAD(&card->ipato.entries);
1074         card->ipato.enabled = 0;
1075         card->ipato.invert4 = 0;
1076         card->ipato.invert6 = 0;
1077         /* init QDIO stuff */
1078         qeth_init_qdio_info(card);
1079         return 0;
1080 }
1081
1082 static int
1083 is_1920_device (struct qeth_card *card)
1084 {
1085         int single_queue = 0;
1086         struct ccw_device *ccwdev;
1087         struct channelPath_dsc {
1088                 u8 flags;
1089                 u8 lsn;
1090                 u8 desc;
1091                 u8 chpid;
1092                 u8 swla;
1093                 u8 zeroes;
1094                 u8 chla;
1095                 u8 chpp;
1096         } *chp_dsc;
1097
1098         QETH_DBF_TEXT(setup, 2, "chk_1920");
1099
1100         ccwdev = card->data.ccwdev;
1101         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1102         if (chp_dsc != NULL) {
1103                 /* CHPP field bit 6 == 1 -> single queue */
1104                 single_queue = ((chp_dsc->chpp & 0x02) == 0x02);
1105                 kfree(chp_dsc);
1106         }
1107         QETH_DBF_TEXT_(setup, 2, "rc:%x", single_queue);
1108         return single_queue;
1109 }
1110
1111 static int
1112 qeth_determine_card_type(struct qeth_card *card)
1113 {
1114         int i = 0;
1115
1116         QETH_DBF_TEXT(setup, 2, "detcdtyp");
1117
1118         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1119         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1120         while (known_devices[i][4]) {
1121                 if ((CARD_RDEV(card)->id.dev_type == known_devices[i][2]) &&
1122                     (CARD_RDEV(card)->id.dev_model == known_devices[i][3])) {
1123                         card->info.type = known_devices[i][4];
1124                         card->qdio.no_out_queues = known_devices[i][8];
1125                         card->info.is_multicast_different = known_devices[i][9];
1126                         if (is_1920_device(card)) {
1127                                 PRINT_INFO("Priority Queueing not able "
1128                                            "due to hardware limitations!\n");
1129                                 card->qdio.no_out_queues = 1;
1130                                 card->qdio.default_out_queue = 0;
1131                         } 
1132                         return 0;
1133                 }
1134                 i++;
1135         }
1136         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1137         PRINT_ERR("unknown card type on device %s\n", CARD_BUS_ID(card));
1138         return -ENOENT;
1139 }
1140
1141 static int
1142 qeth_probe_device(struct ccwgroup_device *gdev)
1143 {
1144         struct qeth_card *card;
1145         struct device *dev;
1146         unsigned long flags;
1147         int rc;
1148
1149         QETH_DBF_TEXT(setup, 2, "probedev");
1150
1151         dev = &gdev->dev;
1152         if (!get_device(dev))
1153                 return -ENODEV;
1154
1155         QETH_DBF_TEXT_(setup, 2, "%s", gdev->dev.bus_id);
1156         
1157         card = qeth_alloc_card();
1158         if (!card) {
1159                 put_device(dev);
1160                 QETH_DBF_TEXT_(setup, 2, "1err%d", -ENOMEM);
1161                 return -ENOMEM;
1162         }
1163         card->read.ccwdev  = gdev->cdev[0];
1164         card->write.ccwdev = gdev->cdev[1];
1165         card->data.ccwdev  = gdev->cdev[2];
1166         gdev->dev.driver_data = card;
1167         card->gdev = gdev;
1168         gdev->cdev[0]->handler = qeth_irq;
1169         gdev->cdev[1]->handler = qeth_irq;
1170         gdev->cdev[2]->handler = qeth_irq;
1171
1172         if ((rc = qeth_determine_card_type(card))){
1173                 PRINT_WARN("%s: not a valid card type\n", __func__);
1174                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1175                 put_device(dev);
1176                 qeth_free_card(card);
1177                 return rc;
1178         }                           
1179         if ((rc = qeth_setup_card(card))){
1180                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1181                 put_device(dev);
1182                 qeth_free_card(card);
1183                 return rc;
1184         }
1185         rc = qeth_create_device_attributes(dev);
1186         if (rc) {
1187                 put_device(dev);
1188                 qeth_free_card(card);
1189                 return rc;
1190         }
1191         /* insert into our internal list */
1192         write_lock_irqsave(&qeth_card_list.rwlock, flags);
1193         list_add_tail(&card->list, &qeth_card_list.list);
1194         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
1195         return rc;
1196 }
1197
1198
1199 static int
1200 qeth_get_unitaddr(struct qeth_card *card)
1201 {
1202         int length;
1203         char *prcd;
1204         int rc;
1205
1206         QETH_DBF_TEXT(setup, 2, "getunit");
1207         rc = read_conf_data(CARD_DDEV(card), (void **) &prcd, &length);
1208         if (rc) {
1209                 PRINT_ERR("read_conf_data for device %s returned %i\n",
1210                           CARD_DDEV_ID(card), rc);
1211                 return rc;
1212         }
1213         card->info.chpid = prcd[30];
1214         card->info.unit_addr2 = prcd[31];
1215         card->info.cula = prcd[63];
1216         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1217                                (prcd[0x11] == _ascebc['M']));
1218         return 0;
1219 }
1220
1221 static void
1222 qeth_init_tokens(struct qeth_card *card)
1223 {
1224         card->token.issuer_rm_w = 0x00010103UL;
1225         card->token.cm_filter_w = 0x00010108UL;
1226         card->token.cm_connection_w = 0x0001010aUL;
1227         card->token.ulp_filter_w = 0x0001010bUL;
1228         card->token.ulp_connection_w = 0x0001010dUL;
1229 }
1230
1231 static inline __u16
1232 raw_devno_from_bus_id(char *id)
1233 {
1234         id += (strlen(id) - 4);
1235         return (__u16) simple_strtoul(id, &id, 16);
1236 }
1237 /**
1238  * setup channel
1239  */
1240 static void
1241 qeth_setup_ccw(struct qeth_channel *channel,unsigned char *iob, __u32 len)
1242 {
1243         struct qeth_card *card;
1244
1245         QETH_DBF_TEXT(trace, 4, "setupccw");
1246         card = CARD_FROM_CDEV(channel->ccwdev);
1247         if (channel == &card->read)
1248                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1249         else
1250                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1251         channel->ccw.count = len;
1252         channel->ccw.cda = (__u32) __pa(iob);
1253 }
1254
1255 /**
1256  * get free buffer for ccws (IDX activation, lancmds,ipassists...)
1257  */
1258 static struct qeth_cmd_buffer *
1259 __qeth_get_buffer(struct qeth_channel *channel)
1260 {
1261         __u8 index;
1262
1263         QETH_DBF_TEXT(trace, 6, "getbuff");
1264         index = channel->io_buf_no;
1265         do {
1266                 if (channel->iob[index].state == BUF_STATE_FREE) {
1267                         channel->iob[index].state = BUF_STATE_LOCKED;
1268                         channel->io_buf_no = (channel->io_buf_no + 1) %
1269                                 QETH_CMD_BUFFER_NO;
1270                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
1271                         return channel->iob + index;
1272                 }
1273                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1274         } while(index != channel->io_buf_no);
1275
1276         return NULL;
1277 }
1278
1279 /**
1280  * release command buffer
1281  */
1282 static void
1283 qeth_release_buffer(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1284 {
1285         unsigned long flags;
1286
1287         QETH_DBF_TEXT(trace, 6, "relbuff");
1288         spin_lock_irqsave(&channel->iob_lock, flags);
1289         memset(iob->data, 0, QETH_BUFSIZE);
1290         iob->state = BUF_STATE_FREE;
1291         iob->callback = qeth_send_control_data_cb;
1292         iob->rc = 0;
1293         spin_unlock_irqrestore(&channel->iob_lock, flags);
1294 }
1295
1296 static struct qeth_cmd_buffer *
1297 qeth_get_buffer(struct qeth_channel *channel)
1298 {
1299         struct qeth_cmd_buffer *buffer = NULL;
1300         unsigned long flags;
1301
1302         spin_lock_irqsave(&channel->iob_lock, flags);
1303         buffer = __qeth_get_buffer(channel);
1304         spin_unlock_irqrestore(&channel->iob_lock, flags);
1305         return buffer;
1306 }
1307
1308 static struct qeth_cmd_buffer *
1309 qeth_wait_for_buffer(struct qeth_channel *channel)
1310 {
1311         struct qeth_cmd_buffer *buffer;
1312         wait_event(channel->wait_q,
1313                    ((buffer = qeth_get_buffer(channel)) != NULL));
1314         return buffer;
1315 }
1316
1317 static void
1318 qeth_clear_cmd_buffers(struct qeth_channel *channel)
1319 {
1320         int cnt = 0;
1321
1322         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1323                 qeth_release_buffer(channel,&channel->iob[cnt]);
1324         channel->buf_no = 0;
1325         channel->io_buf_no = 0;
1326 }
1327
1328 /**
1329  * start IDX for read and write channel
1330  */
1331 static int
1332 qeth_idx_activate_get_answer(struct qeth_channel *channel,
1333                               void (*idx_reply_cb)(struct qeth_channel *,
1334                                                    struct qeth_cmd_buffer *))
1335 {
1336         struct qeth_cmd_buffer *iob;
1337         unsigned long flags;
1338         int rc;
1339         struct qeth_card *card;
1340
1341         QETH_DBF_TEXT(setup, 2, "idxanswr");
1342         card = CARD_FROM_CDEV(channel->ccwdev);
1343         iob = qeth_get_buffer(channel);
1344         iob->callback = idx_reply_cb;
1345         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1346         channel->ccw.count = QETH_BUFSIZE;
1347         channel->ccw.cda = (__u32) __pa(iob->data);
1348
1349         wait_event(card->wait_q,
1350                    atomic_compare_and_swap(0,1,&channel->irq_pending) == 0);
1351         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1352         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1353         rc = ccw_device_start(channel->ccwdev,
1354                               &channel->ccw,(addr_t) iob, 0, 0);
1355         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1356
1357         if (rc) {
1358                 PRINT_ERR("qeth: Error2 in activating channel rc=%d\n",rc);
1359                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1360                 atomic_set(&channel->irq_pending, 0);
1361                 wake_up(&card->wait_q);
1362                 return rc;
1363         }
1364         rc = wait_event_interruptible_timeout(card->wait_q,
1365                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1366         if (rc == -ERESTARTSYS)
1367                 return rc;
1368         if (channel->state != CH_STATE_UP){
1369                 rc = -ETIME;
1370                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1371                 qeth_clear_cmd_buffers(channel);
1372         } else
1373                 rc = 0;
1374         return rc;
1375 }
1376
1377 static int
1378 qeth_idx_activate_channel(struct qeth_channel *channel,
1379                            void (*idx_reply_cb)(struct qeth_channel *,
1380                                                 struct qeth_cmd_buffer *))
1381 {
1382         struct qeth_card *card;
1383         struct qeth_cmd_buffer *iob;
1384         unsigned long flags;
1385         __u16 temp;
1386         int rc;
1387
1388         card = CARD_FROM_CDEV(channel->ccwdev);
1389
1390         QETH_DBF_TEXT(setup, 2, "idxactch");
1391
1392         iob = qeth_get_buffer(channel);
1393         iob->callback = idx_reply_cb;
1394         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1395         channel->ccw.count = IDX_ACTIVATE_SIZE;
1396         channel->ccw.cda = (__u32) __pa(iob->data);
1397         if (channel == &card->write) {
1398                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1399                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1400                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1401                 card->seqno.trans_hdr++;
1402         } else {
1403                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1404                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1405                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1406         }
1407         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1408                &card->token.issuer_rm_w,QETH_MPC_TOKEN_LENGTH);
1409         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1410                &card->info.func_level,sizeof(__u16));
1411         temp = raw_devno_from_bus_id(CARD_DDEV_ID(card));
1412         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp, 2);
1413         temp = (card->info.cula << 8) + card->info.unit_addr2;
1414         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1415
1416         wait_event(card->wait_q,
1417                    atomic_compare_and_swap(0,1,&channel->irq_pending) == 0);
1418         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1419         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1420         rc = ccw_device_start(channel->ccwdev,
1421                               &channel->ccw,(addr_t) iob, 0, 0);
1422         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1423
1424         if (rc) {
1425                 PRINT_ERR("qeth: Error1 in activating channel. rc=%d\n",rc);
1426                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
1427                 atomic_set(&channel->irq_pending, 0);
1428                 wake_up(&card->wait_q);
1429                 return rc;
1430         }
1431         rc = wait_event_interruptible_timeout(card->wait_q,
1432                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1433         if (rc == -ERESTARTSYS)
1434                 return rc;
1435         if (channel->state != CH_STATE_ACTIVATING) {
1436                 PRINT_WARN("qeth: IDX activate timed out!\n");
1437                 QETH_DBF_TEXT_(setup, 2, "2err%d", -ETIME);
1438                 qeth_clear_cmd_buffers(channel);
1439                 return -ETIME;
1440         }
1441         return qeth_idx_activate_get_answer(channel,idx_reply_cb);
1442 }
1443
1444 static int
1445 qeth_peer_func_level(int level)
1446 {
1447         if ((level & 0xff) == 8)
1448                 return (level & 0xff) + 0x400;
1449         if (((level >> 8) & 3) == 1)
1450                 return (level & 0xff) + 0x200;
1451         return level;
1452 }
1453
1454 static void
1455 qeth_idx_write_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1456 {
1457         struct qeth_card *card;
1458         __u16 temp;
1459
1460         QETH_DBF_TEXT(setup ,2, "idxwrcb");
1461
1462         if (channel->state == CH_STATE_DOWN) {
1463                 channel->state = CH_STATE_ACTIVATING;
1464                 goto out;
1465         }
1466         card = CARD_FROM_CDEV(channel->ccwdev);
1467
1468         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1469                 PRINT_ERR("IDX_ACTIVATE on write channel device %s: negative "
1470                           "reply\n", CARD_WDEV_ID(card));
1471                 goto out;
1472         }
1473         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1474         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1475                 PRINT_WARN("IDX_ACTIVATE on write channel device %s: "
1476                            "function level mismatch "
1477                            "(sent: 0x%x, received: 0x%x)\n",
1478                            CARD_WDEV_ID(card), card->info.func_level, temp);
1479                 goto out;
1480         }
1481         channel->state = CH_STATE_UP;
1482 out:
1483         qeth_release_buffer(channel, iob);
1484 }
1485
1486 static int
1487 qeth_check_idx_response(unsigned char *buffer)
1488 {
1489         if (!buffer)
1490                 return 0;
1491
1492         QETH_DBF_HEX(control, 2, buffer, QETH_DBF_CONTROL_LEN);
1493         if ((buffer[2] & 0xc0) == 0xc0) {
1494                 PRINT_WARN("received an IDX TERMINATE "
1495                            "with cause code 0x%02x%s\n",
1496                            buffer[4],
1497                            ((buffer[4] == 0x22) ?
1498                             " -- try another portname" : ""));
1499                 QETH_DBF_TEXT(trace, 2, "ckidxres");
1500                 QETH_DBF_TEXT(trace, 2, " idxterm");
1501                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
1502                 return -EIO;
1503         }
1504         return 0;
1505 }
1506
1507 static void
1508 qeth_idx_read_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1509 {
1510         struct qeth_card *card;
1511         __u16 temp;
1512
1513         QETH_DBF_TEXT(setup , 2, "idxrdcb");
1514         if (channel->state == CH_STATE_DOWN) {
1515                 channel->state = CH_STATE_ACTIVATING;
1516                 goto out;
1517         }
1518
1519         card = CARD_FROM_CDEV(channel->ccwdev);
1520         if (qeth_check_idx_response(iob->data)) {
1521                         goto out;
1522         }
1523         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1524                 PRINT_ERR("IDX_ACTIVATE on read channel device %s: negative "
1525                           "reply\n", CARD_RDEV_ID(card));
1526                 goto out;
1527         }
1528
1529 /**
1530  * temporary fix for microcode bug
1531  * to revert it,replace OR by AND
1532  */
1533         if ( (!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1534              (card->info.type == QETH_CARD_TYPE_OSAE) )
1535                 card->info.portname_required = 1;
1536
1537         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1538         if (temp != qeth_peer_func_level(card->info.func_level)) {
1539                 PRINT_WARN("IDX_ACTIVATE on read channel device %s: function "
1540                            "level mismatch (sent: 0x%x, received: 0x%x)\n",
1541                            CARD_RDEV_ID(card), card->info.func_level, temp);
1542                 goto out;
1543         }
1544         memcpy(&card->token.issuer_rm_r,
1545                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1546                QETH_MPC_TOKEN_LENGTH);
1547         memcpy(&card->info.mcl_level[0],
1548                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1549         channel->state = CH_STATE_UP;
1550 out:
1551         qeth_release_buffer(channel,iob);
1552 }
1553
1554 static int
1555 qeth_issue_next_read(struct qeth_card *card)
1556 {
1557         int rc;
1558         struct qeth_cmd_buffer *iob;
1559
1560         QETH_DBF_TEXT(trace,5,"issnxrd");
1561         if (card->read.state != CH_STATE_UP)
1562                 return -EIO;
1563         iob = qeth_get_buffer(&card->read);
1564         if (!iob) {
1565                 PRINT_WARN("issue_next_read failed: no iob available!\n");
1566                 return -ENOMEM;
1567         }
1568         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
1569         wait_event(card->wait_q,
1570                    atomic_compare_and_swap(0,1,&card->read.irq_pending) == 0);
1571         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1572         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
1573                               (addr_t) iob, 0, 0);
1574         if (rc) {
1575                 PRINT_ERR("Error in starting next read ccw! rc=%i\n", rc);
1576                 atomic_set(&card->read.irq_pending, 0);
1577                 qeth_schedule_recovery(card);
1578                 wake_up(&card->wait_q);
1579         }
1580         return rc;
1581 }
1582
1583 static struct qeth_reply *
1584 qeth_alloc_reply(struct qeth_card *card)
1585 {
1586         struct qeth_reply *reply;
1587
1588         reply = kmalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
1589         if (reply){
1590                 memset(reply, 0, sizeof(struct qeth_reply));
1591                 atomic_set(&reply->refcnt, 1);
1592                 reply->card = card;
1593         };
1594         return reply;
1595 }
1596
1597 static void
1598 qeth_get_reply(struct qeth_reply *reply)
1599 {
1600         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1601         atomic_inc(&reply->refcnt);
1602 }
1603
1604 static void
1605 qeth_put_reply(struct qeth_reply *reply)
1606 {
1607         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1608         if (atomic_dec_and_test(&reply->refcnt))
1609                 kfree(reply);
1610 }
1611
1612 static void
1613 qeth_cmd_timeout(unsigned long data)
1614 {
1615         struct qeth_reply *reply, *list_reply, *r;
1616         unsigned long flags;
1617
1618         reply = (struct qeth_reply *) data;
1619         spin_lock_irqsave(&reply->card->lock, flags);
1620         list_for_each_entry_safe(list_reply, r,
1621                                  &reply->card->cmd_waiter_list, list) {
1622                 if (reply == list_reply){
1623                         qeth_get_reply(reply);
1624                         list_del_init(&reply->list);
1625                         spin_unlock_irqrestore(&reply->card->lock, flags);
1626                         reply->rc = -ETIME;
1627                         reply->received = 1;
1628                         wake_up(&reply->wait_q);
1629                         qeth_put_reply(reply);
1630                         return;
1631                 }
1632         }
1633         spin_unlock_irqrestore(&reply->card->lock, flags);
1634 }
1635
1636 static struct qeth_ipa_cmd *
1637 qeth_check_ipa_data(struct qeth_card *card, struct qeth_cmd_buffer *iob)
1638 {
1639         struct qeth_ipa_cmd *cmd = NULL;
1640
1641         QETH_DBF_TEXT(trace,5,"chkipad");
1642         if (IS_IPA(iob->data)){
1643                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
1644                 if (IS_IPA_REPLY(cmd))
1645                         return cmd;
1646                 else {
1647                         switch (cmd->hdr.command) {
1648                         case IPA_CMD_STOPLAN:
1649                                 PRINT_WARN("Link failure on %s (CHPID 0x%X) - "
1650                                            "there is a network problem or "
1651                                            "someone pulled the cable or "
1652                                            "disabled the port.\n",
1653                                            QETH_CARD_IFNAME(card),
1654                                            card->info.chpid);
1655                                 card->lan_online = 0;
1656                                 netif_carrier_off(card->dev);
1657                                 return NULL;
1658                         case IPA_CMD_STARTLAN:
1659                                 PRINT_INFO("Link reestablished on %s "
1660                                            "(CHPID 0x%X). Scheduling "
1661                                            "IP address reset.\n",
1662                                            QETH_CARD_IFNAME(card),
1663                                            card->info.chpid);
1664                                 netif_carrier_on(card->dev);
1665                                 qeth_schedule_recovery(card);
1666                                 return NULL;
1667                         case IPA_CMD_MODCCID:
1668                                 return cmd;
1669                         case IPA_CMD_REGISTER_LOCAL_ADDR:
1670                                 QETH_DBF_TEXT(trace,3, "irla");
1671                                 break;
1672                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
1673                                 QETH_DBF_TEXT(trace,3, "urla");
1674                                 break;
1675                         default:
1676                                 PRINT_WARN("Received data is IPA "
1677                                            "but not a reply!\n");
1678                                 break;
1679                         }
1680                 }
1681         }
1682         return cmd;
1683 }
1684
1685 /**
1686  * wake all waiting ipa commands
1687  */
1688 static void
1689 qeth_clear_ipacmd_list(struct qeth_card *card)
1690 {
1691         struct qeth_reply *reply, *r;
1692         unsigned long flags;
1693
1694         QETH_DBF_TEXT(trace, 4, "clipalst");
1695
1696         spin_lock_irqsave(&card->lock, flags);
1697         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1698                 qeth_get_reply(reply);
1699                 reply->rc = -EIO;
1700                 reply->received = 1;
1701                 list_del_init(&reply->list);
1702                 wake_up(&reply->wait_q);
1703                 qeth_put_reply(reply);
1704         }
1705         spin_unlock_irqrestore(&card->lock, flags);
1706 }
1707
1708 static void
1709 qeth_send_control_data_cb(struct qeth_channel *channel,
1710                           struct qeth_cmd_buffer *iob)
1711 {
1712         struct qeth_card *card;
1713         struct qeth_reply *reply, *r;
1714         struct qeth_ipa_cmd *cmd;
1715         unsigned long flags;
1716         int keep_reply;
1717
1718         QETH_DBF_TEXT(trace,4,"sndctlcb");
1719
1720         card = CARD_FROM_CDEV(channel->ccwdev);
1721         if (qeth_check_idx_response(iob->data)) {
1722                 qeth_clear_ipacmd_list(card);
1723                 qeth_schedule_recovery(card);
1724                 goto out;
1725         }
1726
1727         cmd = qeth_check_ipa_data(card, iob);
1728         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
1729                 goto out;
1730         /*in case of OSN : check if cmd is set */
1731         if (card->info.type == QETH_CARD_TYPE_OSN &&
1732             cmd &&
1733             cmd->hdr.command != IPA_CMD_STARTLAN &&
1734             card->osn_info.assist_cb != NULL) {
1735                 card->osn_info.assist_cb(card->dev, cmd);
1736                 goto out;
1737         }
1738
1739         spin_lock_irqsave(&card->lock, flags);
1740         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1741                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
1742                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
1743                         qeth_get_reply(reply);
1744                         list_del_init(&reply->list);
1745                         spin_unlock_irqrestore(&card->lock, flags);
1746                         keep_reply = 0;
1747                         if (reply->callback != NULL) {
1748                                 if (cmd) {
1749                                         reply->offset = (__u16)((char*)cmd -
1750                                                                 (char *)iob->data);
1751                                         keep_reply = reply->callback(card,
1752                                                         reply,
1753                                                         (unsigned long)cmd);
1754                                 } else
1755                                         keep_reply = reply->callback(card,
1756                                                         reply,
1757                                                         (unsigned long)iob);
1758                         }
1759                         if (cmd)
1760                                 reply->rc = (u16) cmd->hdr.return_code;
1761                         else if (iob->rc)
1762                                 reply->rc = iob->rc;
1763                         if (keep_reply) {
1764                                 spin_lock_irqsave(&card->lock, flags);
1765                                 list_add_tail(&reply->list,
1766                                               &card->cmd_waiter_list);
1767                                 spin_unlock_irqrestore(&card->lock, flags);
1768                         } else {
1769                                 reply->received = 1;
1770                                 wake_up(&reply->wait_q);
1771                         }
1772                         qeth_put_reply(reply);
1773                         goto out;
1774                 }
1775         }
1776         spin_unlock_irqrestore(&card->lock, flags);
1777 out:
1778         memcpy(&card->seqno.pdu_hdr_ack,
1779                 QETH_PDU_HEADER_SEQ_NO(iob->data),
1780                 QETH_SEQ_NO_LENGTH);
1781         qeth_release_buffer(channel,iob);
1782 }
1783
1784 static inline void
1785 qeth_prepare_control_data(struct qeth_card *card, int len,
1786 struct qeth_cmd_buffer *iob)
1787 {
1788         qeth_setup_ccw(&card->write,iob->data,len);
1789         iob->callback = qeth_release_buffer;
1790
1791         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1792                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1793         card->seqno.trans_hdr++;
1794         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1795                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1796         card->seqno.pdu_hdr++;
1797         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1798                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1799         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1800 }
1801                                                     
1802 static int
1803 qeth_send_control_data(struct qeth_card *card, int len,
1804                        struct qeth_cmd_buffer *iob,
1805                        int (*reply_cb)
1806                        (struct qeth_card *, struct qeth_reply*, unsigned long),
1807                        void *reply_param)
1808
1809 {
1810         int rc;
1811         unsigned long flags;
1812         struct qeth_reply *reply = NULL;
1813         struct timer_list timer;
1814
1815         QETH_DBF_TEXT(trace, 2, "sendctl");
1816
1817         reply = qeth_alloc_reply(card);
1818         if (!reply) {
1819                 PRINT_WARN("Could no alloc qeth_reply!\n");
1820                 return -ENOMEM;
1821         }
1822         reply->callback = reply_cb;
1823         reply->param = reply_param;
1824         if (card->state == CARD_STATE_DOWN)
1825                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1826         else
1827                 reply->seqno = card->seqno.ipa++;
1828         init_timer(&timer);
1829         timer.function = qeth_cmd_timeout;
1830         timer.data = (unsigned long) reply;
1831         init_waitqueue_head(&reply->wait_q);
1832         spin_lock_irqsave(&card->lock, flags);
1833         list_add_tail(&reply->list, &card->cmd_waiter_list);
1834         spin_unlock_irqrestore(&card->lock, flags);
1835         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1836         wait_event(card->wait_q,
1837                    atomic_compare_and_swap(0,1,&card->write.irq_pending) == 0);
1838         qeth_prepare_control_data(card, len, iob);
1839         if (IS_IPA(iob->data))
1840                 timer.expires = jiffies + QETH_IPA_TIMEOUT;
1841         else
1842                 timer.expires = jiffies + QETH_TIMEOUT;
1843         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1844         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1845         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1846                               (addr_t) iob, 0, 0);
1847         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1848         if (rc){
1849                 PRINT_WARN("qeth_send_control_data: "
1850                            "ccw_device_start rc = %i\n", rc);
1851                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1852                 spin_lock_irqsave(&card->lock, flags);
1853                 list_del_init(&reply->list);
1854                 qeth_put_reply(reply);
1855                 spin_unlock_irqrestore(&card->lock, flags);
1856                 qeth_release_buffer(iob->channel, iob);
1857                 atomic_set(&card->write.irq_pending, 0);
1858                 wake_up(&card->wait_q);
1859                 return rc;
1860         }
1861         add_timer(&timer);
1862         wait_event(reply->wait_q, reply->received);
1863         del_timer_sync(&timer);
1864         rc = reply->rc;
1865         qeth_put_reply(reply);
1866         return rc;
1867 }
1868
1869 static int
1870 qeth_osn_send_control_data(struct qeth_card *card, int len,
1871                            struct qeth_cmd_buffer *iob)
1872 {
1873         unsigned long flags;
1874         int rc = 0;
1875
1876         QETH_DBF_TEXT(trace, 5, "osndctrd");
1877
1878         wait_event(card->wait_q,
1879                    atomic_compare_and_swap(0,1,&card->write.irq_pending) == 0);
1880         qeth_prepare_control_data(card, len, iob);
1881         QETH_DBF_TEXT(trace, 6, "osnoirqp");
1882         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1883         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1884                               (addr_t) iob, 0, 0);
1885         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1886         if (rc){
1887                 PRINT_WARN("qeth_osn_send_control_data: "
1888                            "ccw_device_start rc = %i\n", rc);
1889                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1890                 qeth_release_buffer(iob->channel, iob);
1891                 atomic_set(&card->write.irq_pending, 0);
1892                 wake_up(&card->wait_q);
1893         }
1894         return rc;
1895 }                                       
1896
1897 static inline void
1898 qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1899                      char prot_type)
1900 {
1901         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1902         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data),&prot_type,1);
1903         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1904                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1905 }
1906
1907 static int
1908 qeth_osn_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1909                       int data_len)
1910 {
1911         u16 s1, s2;
1912
1913 QETH_DBF_TEXT(trace,4,"osndipa");
1914
1915         qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1916         s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1917         s2 = (u16)data_len;
1918         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1919         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1920         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1921         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1922         return qeth_osn_send_control_data(card, s1, iob);
1923 }
1924                                                             
1925 static int
1926 qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1927                   int (*reply_cb)
1928                   (struct qeth_card *,struct qeth_reply*, unsigned long),
1929                   void *reply_param)
1930 {
1931         int rc;
1932         char prot_type;
1933
1934         QETH_DBF_TEXT(trace,4,"sendipa");
1935
1936         if (card->options.layer2)
1937                 if (card->info.type == QETH_CARD_TYPE_OSN)
1938                         prot_type = QETH_PROT_OSN2;
1939                 else
1940                         prot_type = QETH_PROT_LAYER2;
1941         else
1942                 prot_type = QETH_PROT_TCPIP;
1943         qeth_prepare_ipa_cmd(card,iob,prot_type);
1944         rc = qeth_send_control_data(card, IPA_CMD_LENGTH, iob,
1945                                     reply_cb, reply_param);
1946         return rc;
1947 }
1948
1949
1950 static int
1951 qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1952                   unsigned long data)
1953 {
1954         struct qeth_cmd_buffer *iob;
1955
1956         QETH_DBF_TEXT(setup, 2, "cmenblcb");
1957
1958         iob = (struct qeth_cmd_buffer *) data;
1959         memcpy(&card->token.cm_filter_r,
1960                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
1961                QETH_MPC_TOKEN_LENGTH);
1962         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
1963         return 0;
1964 }
1965
1966 static int
1967 qeth_cm_enable(struct qeth_card *card)
1968 {
1969         int rc;
1970         struct qeth_cmd_buffer *iob;
1971
1972         QETH_DBF_TEXT(setup,2,"cmenable");
1973
1974         iob = qeth_wait_for_buffer(&card->write);
1975         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
1976         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
1977                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1978         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
1979                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
1980
1981         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
1982                                     qeth_cm_enable_cb, NULL);
1983         return rc;
1984 }
1985
1986 static int
1987 qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1988                  unsigned long data)
1989 {
1990
1991         struct qeth_cmd_buffer *iob;
1992
1993         QETH_DBF_TEXT(setup, 2, "cmsetpcb");
1994
1995         iob = (struct qeth_cmd_buffer *) data;
1996         memcpy(&card->token.cm_connection_r,
1997                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
1998                QETH_MPC_TOKEN_LENGTH);
1999         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2000         return 0;
2001 }
2002
2003 static int
2004 qeth_cm_setup(struct qeth_card *card)
2005 {
2006         int rc;
2007         struct qeth_cmd_buffer *iob;
2008
2009         QETH_DBF_TEXT(setup,2,"cmsetup");
2010
2011         iob = qeth_wait_for_buffer(&card->write);
2012         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2013         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2014                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2015         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2016                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2017         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2018                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2019         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2020                                     qeth_cm_setup_cb, NULL);
2021         return rc;
2022
2023 }
2024
2025 static int
2026 qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2027                    unsigned long data)
2028 {
2029
2030         __u16 mtu, framesize;
2031         __u16 len;
2032         __u8 link_type;
2033         struct qeth_cmd_buffer *iob;
2034
2035         QETH_DBF_TEXT(setup, 2, "ulpenacb");
2036
2037         iob = (struct qeth_cmd_buffer *) data;
2038         memcpy(&card->token.ulp_filter_r,
2039                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2040                QETH_MPC_TOKEN_LENGTH);
2041         if (qeth_get_mtu_out_of_mpc(card->info.type)) {
2042                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2043                 mtu = qeth_get_mtu_outof_framesize(framesize);
2044                 if (!mtu) {
2045                         iob->rc = -EINVAL;
2046                         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2047                         return 0;
2048                 }
2049                 card->info.max_mtu = mtu;
2050                 card->info.initial_mtu = mtu;
2051                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2052         } else {
2053                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
2054                 card->info.max_mtu = qeth_get_max_mtu_for_card(card->info.type);
2055                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2056         }
2057
2058         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2059         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2060                 memcpy(&link_type,
2061                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2062                 card->info.link_type = link_type;
2063         } else
2064                 card->info.link_type = 0;
2065         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2066         return 0;
2067 }
2068
2069 static int
2070 qeth_ulp_enable(struct qeth_card *card)
2071 {
2072         int rc;
2073         char prot_type;
2074         struct qeth_cmd_buffer *iob;
2075
2076         /*FIXME: trace view callbacks*/
2077         QETH_DBF_TEXT(setup,2,"ulpenabl");
2078
2079         iob = qeth_wait_for_buffer(&card->write);
2080         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2081
2082         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2083                 (__u8) card->info.portno;
2084         if (card->options.layer2)
2085                 if (card->info.type == QETH_CARD_TYPE_OSN)
2086                         prot_type = QETH_PROT_OSN2;
2087                 else
2088                         prot_type = QETH_PROT_LAYER2;
2089         else
2090                 prot_type = QETH_PROT_TCPIP;
2091
2092         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data),&prot_type,1);
2093         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2094                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2095         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2096                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2097         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2098                card->info.portname, 9);
2099         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2100                                     qeth_ulp_enable_cb, NULL);
2101         return rc;
2102
2103 }
2104
2105 static inline __u16
2106 __raw_devno_from_bus_id(char *id)
2107 {
2108         id += (strlen(id) - 4);
2109         return (__u16) simple_strtoul(id, &id, 16);
2110 }
2111
2112 static int
2113 qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2114                   unsigned long data)
2115 {
2116         struct qeth_cmd_buffer *iob;
2117
2118         QETH_DBF_TEXT(setup, 2, "ulpstpcb");
2119
2120         iob = (struct qeth_cmd_buffer *) data;
2121         memcpy(&card->token.ulp_connection_r,
2122                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2123                QETH_MPC_TOKEN_LENGTH);
2124         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2125         return 0;
2126 }
2127
2128 static int
2129 qeth_ulp_setup(struct qeth_card *card)
2130 {
2131         int rc;
2132         __u16 temp;
2133         struct qeth_cmd_buffer *iob;
2134
2135         QETH_DBF_TEXT(setup,2,"ulpsetup");
2136
2137         iob = qeth_wait_for_buffer(&card->write);
2138         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2139
2140         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2141                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2142         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2143                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2144         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2145                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2146
2147         temp = __raw_devno_from_bus_id(CARD_DDEV_ID(card));
2148         memcpy(QETH_ULP_SETUP_CUA(iob->data), &temp, 2);
2149         temp = (card->info.cula << 8) + card->info.unit_addr2;
2150         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2151         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2152                                     qeth_ulp_setup_cb, NULL);
2153         return rc;
2154 }
2155
2156 static inline int
2157 qeth_check_for_inbound_error(struct qeth_qdio_buffer *buf,
2158                              unsigned int qdio_error,
2159                              unsigned int siga_error)
2160 {
2161         int rc = 0;
2162
2163         if (qdio_error || siga_error) {
2164                 QETH_DBF_TEXT(trace, 2, "qdinerr");
2165                 QETH_DBF_TEXT(qerr, 2, "qdinerr");
2166                 QETH_DBF_TEXT_(qerr, 2, " F15=%02X",
2167                                buf->buffer->element[15].flags & 0xff);
2168                 QETH_DBF_TEXT_(qerr, 2, " F14=%02X",
2169                                buf->buffer->element[14].flags & 0xff);
2170                 QETH_DBF_TEXT_(qerr, 2, " qerr=%X", qdio_error);
2171                 QETH_DBF_TEXT_(qerr, 2, " serr=%X", siga_error);
2172                 rc = 1;
2173         }
2174         return rc;
2175 }
2176
2177 static inline struct sk_buff *
2178 qeth_get_skb(unsigned int length, struct qeth_hdr *hdr)
2179 {
2180         struct sk_buff* skb;
2181         int add_len;
2182
2183         add_len = 0;
2184         if (hdr->hdr.osn.id == QETH_HEADER_TYPE_OSN)
2185                 add_len = sizeof(struct qeth_hdr);
2186 #ifdef CONFIG_QETH_VLAN
2187         else
2188                 add_len = VLAN_HLEN;
2189 #endif
2190         skb = dev_alloc_skb(length + add_len);
2191         if (skb && add_len)
2192                 skb_reserve(skb, add_len);
2193         return skb;
2194 }
2195
2196 static inline struct sk_buff *
2197 qeth_get_next_skb(struct qeth_card *card, struct qdio_buffer *buffer,
2198                   struct qdio_buffer_element **__element, int *__offset,
2199                   struct qeth_hdr **hdr)
2200 {
2201         struct qdio_buffer_element *element = *__element;
2202         int offset = *__offset;
2203         struct sk_buff *skb = NULL;
2204         int skb_len;
2205         void *data_ptr;
2206         int data_len;
2207
2208         QETH_DBF_TEXT(trace,6,"nextskb");
2209         /* qeth_hdr must not cross element boundaries */
2210         if (element->length < offset + sizeof(struct qeth_hdr)){
2211                 if (qeth_is_last_sbale(element))
2212                         return NULL;
2213                 element++;
2214                 offset = 0;
2215                 if (element->length < sizeof(struct qeth_hdr))
2216                         return NULL;
2217         }
2218         *hdr = element->addr + offset;
2219
2220         offset += sizeof(struct qeth_hdr);
2221         if (card->options.layer2)
2222                 if (card->info.type == QETH_CARD_TYPE_OSN)
2223                         skb_len = (*hdr)->hdr.osn.pdu_length;
2224                 else
2225                         skb_len = (*hdr)->hdr.l2.pkt_length;
2226         else
2227                 skb_len = (*hdr)->hdr.l3.length;
2228
2229         if (!skb_len)
2230                 return NULL;
2231         if (card->options.fake_ll){
2232                 if(card->dev->type == ARPHRD_IEEE802_TR){
2233                         if (!(skb = qeth_get_skb(skb_len+QETH_FAKE_LL_LEN_TR, *hdr)))
2234                                 goto no_mem;
2235                         skb_reserve(skb,QETH_FAKE_LL_LEN_TR);
2236                 } else {
2237                         if (!(skb = qeth_get_skb(skb_len+QETH_FAKE_LL_LEN_ETH, *hdr)))
2238                                 goto no_mem;
2239                         skb_reserve(skb,QETH_FAKE_LL_LEN_ETH);
2240                 }
2241         } else if (!(skb = qeth_get_skb(skb_len, *hdr)))
2242                 goto no_mem;
2243         data_ptr = element->addr + offset;
2244         while (skb_len) {
2245                 data_len = min(skb_len, (int)(element->length - offset));
2246                 if (data_len)
2247                         memcpy(skb_put(skb, data_len), data_ptr, data_len);
2248                 skb_len -= data_len;
2249                 if (skb_len){
2250                         if (qeth_is_last_sbale(element)){
2251                                 QETH_DBF_TEXT(trace,4,"unexeob");
2252                                 QETH_DBF_TEXT_(trace,4,"%s",CARD_BUS_ID(card));
2253                                 QETH_DBF_TEXT(qerr,2,"unexeob");
2254                                 QETH_DBF_TEXT_(qerr,2,"%s",CARD_BUS_ID(card));
2255                                 QETH_DBF_HEX(misc,4,buffer,sizeof(*buffer));
2256                                 dev_kfree_skb_any(skb);
2257                                 card->stats.rx_errors++;
2258                                 return NULL;
2259                         }
2260                         element++;
2261                         offset = 0;
2262                         data_ptr = element->addr;
2263                 } else {
2264                         offset += data_len;
2265                 }
2266         }
2267         *__element = element;
2268         *__offset = offset;
2269         return skb;
2270 no_mem:
2271         if (net_ratelimit()){
2272                 PRINT_WARN("No memory for packet received on %s.\n",
2273                            QETH_CARD_IFNAME(card));
2274                 QETH_DBF_TEXT(trace,2,"noskbmem");
2275                 QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2276         }
2277         card->stats.rx_dropped++;
2278         return NULL;
2279 }
2280
2281 static inline __be16
2282 qeth_type_trans(struct sk_buff *skb, struct net_device *dev)
2283 {
2284         struct qeth_card *card;
2285         struct ethhdr *eth;
2286
2287         QETH_DBF_TEXT(trace,6,"typtrans");
2288
2289         card = (struct qeth_card *)dev->priv;
2290 #ifdef CONFIG_TR
2291         if ((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
2292             (card->info.link_type == QETH_LINK_TYPE_LANE_TR))
2293                 return tr_type_trans(skb,dev);
2294 #endif /* CONFIG_TR */
2295         skb->mac.raw = skb->data;
2296         skb_pull(skb, ETH_HLEN );
2297         eth = eth_hdr(skb);
2298
2299         if (*eth->h_dest & 1) {
2300                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
2301                         skb->pkt_type = PACKET_BROADCAST;
2302                 else
2303                         skb->pkt_type = PACKET_MULTICAST;
2304         } else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
2305                 skb->pkt_type = PACKET_OTHERHOST;
2306
2307         if (ntohs(eth->h_proto) >= 1536)
2308                 return eth->h_proto;
2309         if (*(unsigned short *) (skb->data) == 0xFFFF)
2310                 return htons(ETH_P_802_3);
2311         return htons(ETH_P_802_2);
2312 }
2313
2314 static inline void
2315 qeth_rebuild_skb_fake_ll_tr(struct qeth_card *card, struct sk_buff *skb,
2316                          struct qeth_hdr *hdr)
2317 {
2318         struct trh_hdr *fake_hdr;
2319         struct trllc *fake_llc;
2320         struct iphdr *ip_hdr;
2321
2322         QETH_DBF_TEXT(trace,5,"skbfktr");
2323         skb->mac.raw = skb->data - QETH_FAKE_LL_LEN_TR;
2324         /* this is a fake ethernet header */
2325         fake_hdr = (struct trh_hdr *) skb->mac.raw;
2326
2327         /* the destination MAC address */
2328         switch (skb->pkt_type){
2329         case PACKET_MULTICAST:
2330                 switch (skb->protocol){
2331 #ifdef CONFIG_QETH_IPV6
2332                 case __constant_htons(ETH_P_IPV6):
2333                         ndisc_mc_map((struct in6_addr *)
2334                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2335                                      fake_hdr->daddr, card->dev, 0);
2336                         break;
2337 #endif /* CONFIG_QETH_IPV6 */
2338                 case __constant_htons(ETH_P_IP):
2339                         ip_hdr = (struct iphdr *)skb->data;
2340                         ip_tr_mc_map(ip_hdr->daddr, fake_hdr->daddr);
2341                         break;
2342                 default:
2343                         memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2344                 }
2345                 break;
2346         case PACKET_BROADCAST:
2347                 memset(fake_hdr->daddr, 0xff, TR_ALEN);
2348                 break;
2349         default:
2350                 memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2351         }
2352         /* the source MAC address */
2353         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2354                 memcpy(fake_hdr->saddr, &hdr->hdr.l3.dest_addr[2], TR_ALEN);
2355         else
2356                 memset(fake_hdr->saddr, 0, TR_ALEN);
2357         fake_hdr->rcf=0;
2358         fake_llc = (struct trllc*)&(fake_hdr->rcf);
2359         fake_llc->dsap = EXTENDED_SAP;
2360         fake_llc->ssap = EXTENDED_SAP;
2361         fake_llc->llc  = UI_CMD;
2362         fake_llc->protid[0] = 0;
2363         fake_llc->protid[1] = 0;
2364         fake_llc->protid[2] = 0;
2365         fake_llc->ethertype = ETH_P_IP;
2366 }
2367
2368 static inline void
2369 qeth_rebuild_skb_fake_ll_eth(struct qeth_card *card, struct sk_buff *skb,
2370                          struct qeth_hdr *hdr)
2371 {
2372         struct ethhdr *fake_hdr;
2373         struct iphdr *ip_hdr;
2374
2375         QETH_DBF_TEXT(trace,5,"skbfketh");
2376         skb->mac.raw = skb->data - QETH_FAKE_LL_LEN_ETH;
2377         /* this is a fake ethernet header */
2378         fake_hdr = (struct ethhdr *) skb->mac.raw;
2379
2380         /* the destination MAC address */
2381         switch (skb->pkt_type){
2382         case PACKET_MULTICAST:
2383                 switch (skb->protocol){
2384 #ifdef CONFIG_QETH_IPV6
2385                 case __constant_htons(ETH_P_IPV6):
2386                         ndisc_mc_map((struct in6_addr *)
2387                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2388                                      fake_hdr->h_dest, card->dev, 0);
2389                         break;
2390 #endif /* CONFIG_QETH_IPV6 */
2391                 case __constant_htons(ETH_P_IP):
2392                         ip_hdr = (struct iphdr *)skb->data;
2393                         ip_eth_mc_map(ip_hdr->daddr, fake_hdr->h_dest);
2394                         break;
2395                 default:
2396                         memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2397                 }
2398                 break;
2399         case PACKET_BROADCAST:
2400                 memset(fake_hdr->h_dest, 0xff, ETH_ALEN);
2401                 break;
2402         default:
2403                 memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2404         }
2405         /* the source MAC address */
2406         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2407                 memcpy(fake_hdr->h_source, &hdr->hdr.l3.dest_addr[2], ETH_ALEN);
2408         else
2409                 memset(fake_hdr->h_source, 0, ETH_ALEN);
2410         /* the protocol */
2411         fake_hdr->h_proto = skb->protocol;
2412 }
2413
2414 static inline void
2415 qeth_rebuild_skb_fake_ll(struct qeth_card *card, struct sk_buff *skb,
2416                         struct qeth_hdr *hdr)
2417 {
2418         if (card->dev->type == ARPHRD_IEEE802_TR)
2419                 qeth_rebuild_skb_fake_ll_tr(card, skb, hdr);
2420         else
2421                 qeth_rebuild_skb_fake_ll_eth(card, skb, hdr);
2422 }
2423
2424 static inline void
2425 qeth_rebuild_skb_vlan(struct qeth_card *card, struct sk_buff *skb,
2426                       struct qeth_hdr *hdr)
2427 {
2428 #ifdef CONFIG_QETH_VLAN
2429         u16 *vlan_tag;
2430
2431         if (hdr->hdr.l3.ext_flags &
2432             (QETH_HDR_EXT_VLAN_FRAME | QETH_HDR_EXT_INCLUDE_VLAN_TAG)) {
2433                 vlan_tag = (u16 *) skb_push(skb, VLAN_HLEN);
2434                 *vlan_tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME)?
2435                         hdr->hdr.l3.vlan_id : *((u16 *)&hdr->hdr.l3.dest_addr[12]);
2436                 *(vlan_tag + 1) = skb->protocol;
2437                 skb->protocol = __constant_htons(ETH_P_8021Q);
2438         }
2439 #endif /* CONFIG_QETH_VLAN */
2440 }
2441
2442 static inline __u16
2443 qeth_layer2_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2444                         struct qeth_hdr *hdr)
2445 {
2446         unsigned short vlan_id = 0;
2447 #ifdef CONFIG_QETH_VLAN
2448         struct vlan_hdr *vhdr;
2449 #endif
2450
2451         skb->pkt_type = PACKET_HOST;
2452         skb->protocol = qeth_type_trans(skb, skb->dev);
2453         if (card->options.checksum_type == NO_CHECKSUMMING)
2454                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2455         else
2456                 skb->ip_summed = CHECKSUM_NONE;
2457 #ifdef CONFIG_QETH_VLAN
2458         if (hdr->hdr.l2.flags[2] & (QETH_LAYER2_FLAG_VLAN)) {
2459                 vhdr = (struct vlan_hdr *) skb->data;
2460                 skb->protocol =
2461                         __constant_htons(vhdr->h_vlan_encapsulated_proto);
2462                 vlan_id = hdr->hdr.l2.vlan_id;
2463                 skb_pull(skb, VLAN_HLEN);
2464         }
2465 #endif
2466         *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
2467         return vlan_id;
2468 }
2469
2470 static inline void
2471 qeth_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2472                  struct qeth_hdr *hdr)
2473 {
2474 #ifdef CONFIG_QETH_IPV6
2475         if (hdr->hdr.l3.flags & QETH_HDR_PASSTHRU) {
2476                 skb->pkt_type = PACKET_HOST;
2477                 skb->protocol = qeth_type_trans(skb, card->dev);
2478                 return;
2479         }
2480 #endif /* CONFIG_QETH_IPV6 */
2481         skb->protocol = htons((hdr->hdr.l3.flags & QETH_HDR_IPV6)? ETH_P_IPV6 :
2482                               ETH_P_IP);
2483         switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK){
2484         case QETH_CAST_UNICAST:
2485                 skb->pkt_type = PACKET_HOST;
2486                 break;
2487         case QETH_CAST_MULTICAST:
2488                 skb->pkt_type = PACKET_MULTICAST;
2489                 card->stats.multicast++;
2490                 break;
2491         case QETH_CAST_BROADCAST:
2492                 skb->pkt_type = PACKET_BROADCAST;
2493                 card->stats.multicast++;
2494                 break;
2495         case QETH_CAST_ANYCAST:
2496         case QETH_CAST_NOCAST:
2497         default:
2498                 skb->pkt_type = PACKET_HOST;
2499         }
2500         qeth_rebuild_skb_vlan(card, skb, hdr);
2501         if (card->options.fake_ll)
2502                 qeth_rebuild_skb_fake_ll(card, skb, hdr);
2503         else
2504                 skb->mac.raw = skb->data;
2505         skb->ip_summed = card->options.checksum_type;
2506         if (card->options.checksum_type == HW_CHECKSUMMING){
2507                 if ( (hdr->hdr.l3.ext_flags &
2508                       (QETH_HDR_EXT_CSUM_HDR_REQ |
2509                        QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
2510                      (QETH_HDR_EXT_CSUM_HDR_REQ |
2511                       QETH_HDR_EXT_CSUM_TRANSP_REQ) )
2512                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2513                 else
2514                         skb->ip_summed = SW_CHECKSUMMING;
2515         }
2516 }
2517
2518 static inline void
2519 qeth_process_inbound_buffer(struct qeth_card *card,
2520                             struct qeth_qdio_buffer *buf, int index)
2521 {
2522         struct qdio_buffer_element *element;
2523         struct sk_buff *skb;
2524         struct qeth_hdr *hdr;
2525         int offset;
2526         int rxrc;
2527         __u16 vlan_tag = 0;
2528
2529         /* get first element of current buffer */
2530         element = (struct qdio_buffer_element *)&buf->buffer->element[0];
2531         offset = 0;
2532 #ifdef CONFIG_QETH_PERF_STATS
2533         card->perf_stats.bufs_rec++;
2534 #endif
2535         while((skb = qeth_get_next_skb(card, buf->buffer, &element,
2536                                        &offset, &hdr))) {
2537                 skb->dev = card->dev;
2538                 if (hdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
2539                         vlan_tag = qeth_layer2_rebuild_skb(card, skb, hdr);
2540                 else if (hdr->hdr.l3.id == QETH_HEADER_TYPE_LAYER3)     
2541                         qeth_rebuild_skb(card, skb, hdr);
2542                 else { /*in case of OSN*/
2543                         skb_push(skb, sizeof(struct qeth_hdr));
2544                         memcpy(skb->data, hdr, sizeof(struct qeth_hdr));
2545                 }
2546                 /* is device UP ? */
2547                 if (!(card->dev->flags & IFF_UP)){
2548                         dev_kfree_skb_any(skb);
2549                         continue;
2550                 }
2551 #ifdef CONFIG_QETH_VLAN
2552                 if (vlan_tag)
2553                         vlan_hwaccel_rx(skb, card->vlangrp, vlan_tag);
2554                 else
2555 #endif
2556                 if (card->info.type == QETH_CARD_TYPE_OSN)
2557                         rxrc = card->osn_info.data_cb(skb);
2558                 else
2559                         rxrc = netif_rx(skb);
2560                 card->dev->last_rx = jiffies;
2561                 card->stats.rx_packets++;
2562                 card->stats.rx_bytes += skb->len;
2563         }
2564 }
2565
2566 static inline struct qeth_buffer_pool_entry *
2567 qeth_get_buffer_pool_entry(struct qeth_card *card)
2568 {
2569         struct qeth_buffer_pool_entry *entry;
2570
2571         QETH_DBF_TEXT(trace, 6, "gtbfplen");
2572         if (!list_empty(&card->qdio.in_buf_pool.entry_list)) {
2573                 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2574                                 struct qeth_buffer_pool_entry, list);
2575                 list_del_init(&entry->list);
2576                 return entry;
2577         }
2578         return NULL;
2579 }
2580
2581 static inline void
2582 qeth_init_input_buffer(struct qeth_card *card, struct qeth_qdio_buffer *buf)
2583 {
2584         struct qeth_buffer_pool_entry *pool_entry;
2585         int i;
2586
2587         pool_entry = qeth_get_buffer_pool_entry(card);
2588         /*
2589          * since the buffer is accessed only from the input_tasklet
2590          * there shouldn't be a need to synchronize; also, since we use
2591          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2592          * buffers
2593          */
2594         BUG_ON(!pool_entry);
2595
2596         buf->pool_entry = pool_entry;
2597         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i){
2598                 buf->buffer->element[i].length = PAGE_SIZE;
2599                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2600                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2601                         buf->buffer->element[i].flags = SBAL_FLAGS_LAST_ENTRY;
2602                 else
2603                         buf->buffer->element[i].flags = 0;
2604         }
2605         buf->state = QETH_QDIO_BUF_EMPTY;
2606 }
2607
2608 static inline void
2609 qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
2610                          struct qeth_qdio_out_buffer *buf)
2611 {
2612         int i;
2613         struct sk_buff *skb;
2614
2615         /* is PCI flag set on buffer? */
2616         if (buf->buffer->element[0].flags & 0x40)
2617                 atomic_dec(&queue->set_pci_flags_count);
2618
2619         while ((skb = skb_dequeue(&buf->skb_list))){
2620                 atomic_dec(&skb->users);
2621                 dev_kfree_skb_any(skb);
2622         }
2623         qeth_eddp_buf_release_contexts(buf);
2624         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i){
2625                 buf->buffer->element[i].length = 0;
2626                 buf->buffer->element[i].addr = NULL;
2627                 buf->buffer->element[i].flags = 0;
2628         }
2629         buf->next_element_to_fill = 0;
2630         atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
2631 }
2632
2633 static inline void
2634 qeth_queue_input_buffer(struct qeth_card *card, int index)
2635 {
2636         struct qeth_qdio_q *queue = card->qdio.in_q;
2637         int count;
2638         int i;
2639         int rc;
2640
2641         QETH_DBF_TEXT(trace,6,"queinbuf");
2642         count = (index < queue->next_buf_to_init)?
2643                 card->qdio.in_buf_pool.buf_count -
2644                 (queue->next_buf_to_init - index) :
2645                 card->qdio.in_buf_pool.buf_count -
2646                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
2647         /* only requeue at a certain threshold to avoid SIGAs */
2648         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)){
2649                 for (i = queue->next_buf_to_init;
2650                      i < queue->next_buf_to_init + count; ++i)
2651                         qeth_init_input_buffer(card,
2652                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q]);
2653                 /*
2654                  * according to old code it should be avoided to requeue all
2655                  * 128 buffers in order to benefit from PCI avoidance.
2656                  * this function keeps at least one buffer (the buffer at
2657                  * 'index') un-requeued -> this buffer is the first buffer that
2658                  * will be requeued the next time
2659                  */
2660 #ifdef CONFIG_QETH_PERF_STATS
2661                 card->perf_stats.inbound_do_qdio_cnt++;
2662                 card->perf_stats.inbound_do_qdio_start_time = qeth_get_micros();
2663 #endif
2664                 rc = do_QDIO(CARD_DDEV(card),
2665                              QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
2666                              0, queue->next_buf_to_init, count, NULL);
2667 #ifdef CONFIG_QETH_PERF_STATS
2668                 card->perf_stats.inbound_do_qdio_time += qeth_get_micros() -
2669                         card->perf_stats.inbound_do_qdio_start_time;
2670 #endif
2671                 if (rc){
2672                         PRINT_WARN("qeth_queue_input_buffer's do_QDIO "
2673                                    "return %i (device %s).\n",
2674                                    rc, CARD_DDEV_ID(card));
2675                         QETH_DBF_TEXT(trace,2,"qinberr");
2676                         QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2677                 }
2678                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
2679                                           QDIO_MAX_BUFFERS_PER_Q;
2680         }
2681 }
2682
2683 static inline void
2684 qeth_put_buffer_pool_entry(struct qeth_card *card,
2685                            struct qeth_buffer_pool_entry *entry)
2686 {
2687         QETH_DBF_TEXT(trace, 6, "ptbfplen");
2688         list_add_tail(&entry->list, &card->qdio.in_buf_pool.entry_list);
2689 }
2690
2691 static void
2692 qeth_qdio_input_handler(struct ccw_device * ccwdev, unsigned int status,
2693                         unsigned int qdio_err, unsigned int siga_err,
2694                         unsigned int queue, int first_element, int count,
2695                         unsigned long card_ptr)
2696 {
2697         struct net_device *net_dev;
2698         struct qeth_card *card;
2699         struct qeth_qdio_buffer *buffer;
2700         int index;
2701         int i;
2702
2703         QETH_DBF_TEXT(trace, 6, "qdinput");
2704         card = (struct qeth_card *) card_ptr;
2705         net_dev = card->dev;
2706 #ifdef CONFIG_QETH_PERF_STATS
2707         card->perf_stats.inbound_cnt++;
2708         card->perf_stats.inbound_start_time = qeth_get_micros();
2709 #endif
2710         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
2711                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
2712                         QETH_DBF_TEXT(trace, 1,"qdinchk");
2713                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2714                         QETH_DBF_TEXT_(trace,1,"%04X%04X",first_element,count);
2715                         QETH_DBF_TEXT_(trace,1,"%04X%04X", queue, status);
2716                         qeth_schedule_recovery(card);
2717                         return;
2718                 }
2719         }
2720         for (i = first_element; i < (first_element + count); ++i) {
2721                 index = i % QDIO_MAX_BUFFERS_PER_Q;
2722                 buffer = &card->qdio.in_q->bufs[index];
2723                 if (!((status == QDIO_STATUS_LOOK_FOR_ERROR) &&
2724                       qeth_check_for_inbound_error(buffer, qdio_err, siga_err)))
2725                         qeth_process_inbound_buffer(card, buffer, index);
2726                 /* clear buffer and give back to hardware */
2727                 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
2728                 qeth_queue_input_buffer(card, index);
2729         }
2730 #ifdef CONFIG_QETH_PERF_STATS
2731         card->perf_stats.inbound_time += qeth_get_micros() -
2732                 card->perf_stats.inbound_start_time;
2733 #endif
2734 }
2735
2736 static inline int
2737 qeth_handle_send_error(struct qeth_card *card,
2738                        struct qeth_qdio_out_buffer *buffer,
2739                        int qdio_err, int siga_err)
2740 {
2741         int sbalf15 = buffer->buffer->element[15].flags & 0xff;
2742         int cc = siga_err & 3;
2743
2744         QETH_DBF_TEXT(trace, 6, "hdsnderr");
2745         switch (cc) {
2746         case 0:
2747                 if (qdio_err){
2748                         QETH_DBF_TEXT(trace, 1,"lnkfail");
2749                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2750                         QETH_DBF_TEXT_(trace,1,"%04x %02x",
2751                                        (u16)qdio_err, (u8)sbalf15);
2752                         return QETH_SEND_ERROR_LINK_FAILURE;
2753                 }
2754                 return QETH_SEND_ERROR_NONE;
2755         case 2:
2756                 if (siga_err & QDIO_SIGA_ERROR_B_BIT_SET) {
2757                         QETH_DBF_TEXT(trace, 1, "SIGAcc2B");
2758                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2759                         return QETH_SEND_ERROR_KICK_IT;
2760                 }
2761                 if ((sbalf15 >= 15) && (sbalf15 <= 31))
2762                         return QETH_SEND_ERROR_RETRY;
2763                 return QETH_SEND_ERROR_LINK_FAILURE;
2764                 /* look at qdio_error and sbalf 15 */
2765         case 1:
2766                 QETH_DBF_TEXT(trace, 1, "SIGAcc1");
2767                 QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2768                 return QETH_SEND_ERROR_LINK_FAILURE;
2769         case 3:
2770                 QETH_DBF_TEXT(trace, 1, "SIGAcc3");
2771                 QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2772                 return QETH_SEND_ERROR_KICK_IT;
2773         }
2774         return QETH_SEND_ERROR_LINK_FAILURE;
2775 }
2776
2777 void
2778 qeth_flush_buffers(struct qeth_qdio_out_q *queue, int under_int,
2779                    int index, int count)
2780 {
2781         struct qeth_qdio_out_buffer *buf;
2782         int rc;
2783         int i;
2784
2785         QETH_DBF_TEXT(trace, 6, "flushbuf");
2786
2787         for (i = index; i < index + count; ++i) {
2788                 buf = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2789                 buf->buffer->element[buf->next_element_to_fill - 1].flags |=
2790                                 SBAL_FLAGS_LAST_ENTRY;
2791
2792                 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
2793                         continue;
2794
2795                 if (!queue->do_pack){
2796                         if ((atomic_read(&queue->used_buffers) >=
2797                                 (QETH_HIGH_WATERMARK_PACK -
2798                                  QETH_WATERMARK_PACK_FUZZ)) &&
2799                             !atomic_read(&queue->set_pci_flags_count)){
2800                                 /* it's likely that we'll go to packing
2801                                  * mode soon */
2802                                 atomic_inc(&queue->set_pci_flags_count);
2803                                 buf->buffer->element[0].flags |= 0x40;
2804                         }
2805                 } else {
2806                         if (!atomic_read(&queue->set_pci_flags_count)){
2807                                 /*
2808                                  * there's no outstanding PCI any more, so we
2809                                  * have to request a PCI to be sure the the PCI
2810                                  * will wake at some time in the future then we
2811                                  * can flush packed buffers that might still be
2812                                  * hanging around, which can happen if no
2813                                  * further send was requested by the stack
2814                                  */
2815                                 atomic_inc(&queue->set_pci_flags_count);
2816                                 buf->buffer->element[0].flags |= 0x40;
2817                         }
2818                 }
2819         }
2820
2821         queue->card->dev->trans_start = jiffies;
2822 #ifdef CONFIG_QETH_PERF_STATS
2823         queue->card->perf_stats.outbound_do_qdio_cnt++;
2824         queue->card->perf_stats.outbound_do_qdio_start_time = qeth_get_micros();
2825 #endif
2826         if (under_int)
2827                 rc = do_QDIO(CARD_DDEV(queue->card),
2828                              QDIO_FLAG_SYNC_OUTPUT | QDIO_FLAG_UNDER_INTERRUPT,
2829                              queue->queue_no, index, count, NULL);
2830         else
2831                 rc = do_QDIO(CARD_DDEV(queue->card), QDIO_FLAG_SYNC_OUTPUT,
2832                              queue->queue_no, index, count, NULL);
2833 #ifdef CONFIG_QETH_PERF_STATS
2834         queue->card->perf_stats.outbound_do_qdio_time += qeth_get_micros() -
2835                 queue->card->perf_stats.outbound_do_qdio_start_time;
2836 #endif
2837         if (rc){
2838                 QETH_DBF_TEXT(trace, 2, "flushbuf");
2839                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
2840                 QETH_DBF_TEXT_(trace, 2, "%s", CARD_DDEV_ID(queue->card));
2841                 queue->card->stats.tx_errors += count;
2842                 /* this must not happen under normal circumstances. if it
2843                  * happens something is really wrong -> recover */
2844                 qeth_schedule_recovery(queue->card);
2845                 return;
2846         }
2847         atomic_add(count, &queue->used_buffers);
2848 #ifdef CONFIG_QETH_PERF_STATS
2849         queue->card->perf_stats.bufs_sent += count;
2850 #endif
2851 }
2852
2853 /*
2854  * Switched to packing state if the number of used buffers on a queue
2855  * reaches a certain limit.
2856  */
2857 static inline void
2858 qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
2859 {
2860         if (!queue->do_pack) {
2861                 if (atomic_read(&queue->used_buffers)
2862                     >= QETH_HIGH_WATERMARK_PACK){
2863                         /* switch non-PACKING -> PACKING */
2864                         QETH_DBF_TEXT(trace, 6, "np->pack");
2865 #ifdef CONFIG_QETH_PERF_STATS
2866                         queue->card->perf_stats.sc_dp_p++;
2867 #endif
2868                         queue->do_pack = 1;
2869                 }
2870         }
2871 }
2872
2873 /*
2874  * Switches from packing to non-packing mode. If there is a packing
2875  * buffer on the queue this buffer will be prepared to be flushed.
2876  * In that case 1 is returned to inform the caller. If no buffer
2877  * has to be flushed, zero is returned.
2878  */
2879 static inline int
2880 qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
2881 {
2882         struct qeth_qdio_out_buffer *buffer;
2883         int flush_count = 0;
2884
2885         if (queue->do_pack) {
2886                 if (atomic_read(&queue->used_buffers)
2887                     <= QETH_LOW_WATERMARK_PACK) {
2888                         /* switch PACKING -> non-PACKING */
2889                         QETH_DBF_TEXT(trace, 6, "pack->np");
2890 #ifdef CONFIG_QETH_PERF_STATS
2891                         queue->card->perf_stats.sc_p_dp++;
2892 #endif
2893                         queue->do_pack = 0;
2894                         /* flush packing buffers */
2895                         buffer = &queue->bufs[queue->next_buf_to_fill];
2896                         if ((atomic_read(&buffer->state) ==
2897                                                 QETH_QDIO_BUF_EMPTY) &&
2898                             (buffer->next_element_to_fill > 0)) {
2899                                 atomic_set(&buffer->state,QETH_QDIO_BUF_PRIMED);
2900                                 flush_count++;
2901                                 queue->next_buf_to_fill =
2902                                         (queue->next_buf_to_fill + 1) %
2903                                         QDIO_MAX_BUFFERS_PER_Q;
2904                         }
2905                 }
2906         }
2907         return flush_count;
2908 }
2909
2910 /*
2911  * Called to flush a packing buffer if no more pci flags are on the queue.
2912  * Checks if there is a packing buffer and prepares it to be flushed.
2913  * In that case returns 1, otherwise zero.
2914  */
2915 static inline int
2916 qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2917 {
2918         struct qeth_qdio_out_buffer *buffer;
2919
2920         buffer = &queue->bufs[queue->next_buf_to_fill];
2921         if((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
2922            (buffer->next_element_to_fill > 0)){
2923                 /* it's a packing buffer */
2924                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
2925                 queue->next_buf_to_fill =
2926                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
2927                 return 1;
2928         }
2929         return 0;
2930 }
2931
2932 static inline void
2933 qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2934 {
2935         int index;
2936         int flush_cnt = 0;
2937         int q_was_packing = 0;
2938
2939         /*
2940          * check if weed have to switch to non-packing mode or if
2941          * we have to get a pci flag out on the queue
2942          */
2943         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
2944             !atomic_read(&queue->set_pci_flags_count)){
2945                 if (atomic_swap(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
2946                                 QETH_OUT_Q_UNLOCKED) {
2947                         /*
2948                          * If we get in here, there was no action in
2949                          * do_send_packet. So, we check if there is a
2950                          * packing buffer to be flushed here.
2951                          */
2952                         netif_stop_queue(queue->card->dev);
2953                         index = queue->next_buf_to_fill;
2954                         q_was_packing = queue->do_pack;
2955                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
2956                         if (!flush_cnt &&
2957                             !atomic_read(&queue->set_pci_flags_count))
2958                                 flush_cnt +=
2959                                         qeth_flush_buffers_on_no_pci(queue);
2960 #ifdef CONFIG_QETH_PERF_STATS
2961                         if (q_was_packing)
2962                                 queue->card->perf_stats.bufs_sent_pack +=
2963                                         flush_cnt;
2964 #endif
2965                         if (flush_cnt)
2966                                 qeth_flush_buffers(queue, 1, index, flush_cnt);
2967                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
2968                 }
2969         }
2970 }
2971
2972 static void
2973 qeth_qdio_output_handler(struct ccw_device * ccwdev, unsigned int status,
2974                         unsigned int qdio_error, unsigned int siga_error,
2975                         unsigned int __queue, int first_element, int count,
2976                         unsigned long card_ptr)
2977 {
2978         struct qeth_card *card        = (struct qeth_card *) card_ptr;
2979         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
2980         struct qeth_qdio_out_buffer *buffer;
2981         int i;
2982
2983         QETH_DBF_TEXT(trace, 6, "qdouhdl");
2984         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
2985                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
2986                         QETH_DBF_TEXT(trace, 2, "achkcond");
2987                         QETH_DBF_TEXT_(trace, 2, "%s", CARD_BUS_ID(card));
2988                         QETH_DBF_TEXT_(trace, 2, "%08x", status);
2989                         netif_stop_queue(card->dev);
2990                         qeth_schedule_recovery(card);
2991                         return;
2992                 }
2993         }
2994 #ifdef CONFIG_QETH_PERF_STATS
2995         card->perf_stats.outbound_handler_cnt++;
2996         card->perf_stats.outbound_handler_start_time = qeth_get_micros();
2997 #endif
2998         for(i = first_element; i < (first_element + count); ++i){
2999                 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
3000                 /*we only handle the KICK_IT error by doing a recovery */
3001                 if (qeth_handle_send_error(card, buffer, qdio_error, siga_error)
3002                                 == QETH_SEND_ERROR_KICK_IT){
3003                         netif_stop_queue(card->dev);
3004                         qeth_schedule_recovery(card);
3005                         return;
3006                 }
3007                 qeth_clear_output_buffer(queue, buffer);
3008         }
3009         atomic_sub(count, &queue->used_buffers);
3010         /* check if we need to do something on this outbound queue */
3011         if (card->info.type != QETH_CARD_TYPE_IQD)
3012                 qeth_check_outbound_queue(queue);
3013
3014         netif_wake_queue(queue->card->dev);
3015 #ifdef CONFIG_QETH_PERF_STATS
3016         card->perf_stats.outbound_handler_time += qeth_get_micros() -
3017                 card->perf_stats.outbound_handler_start_time;
3018 #endif
3019 }
3020
3021 static void
3022 qeth_create_qib_param_field(struct qeth_card *card, char *param_field)
3023 {
3024
3025         param_field[0] = _ascebc['P'];
3026         param_field[1] = _ascebc['C'];
3027         param_field[2] = _ascebc['I'];
3028         param_field[3] = _ascebc['T'];
3029         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
3030         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
3031         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
3032 }
3033
3034 static void
3035 qeth_create_qib_param_field_blkt(struct qeth_card *card, char *param_field)
3036 {
3037         param_field[16] = _ascebc['B'];
3038         param_field[17] = _ascebc['L'];
3039         param_field[18] = _ascebc['K'];
3040         param_field[19] = _ascebc['T'];
3041         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
3042         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
3043         *((unsigned int *) (&param_field[28])) = card->info.blkt.inter_packet_jumbo;
3044 }
3045
3046 static void
3047 qeth_initialize_working_pool_list(struct qeth_card *card)
3048 {
3049         struct qeth_buffer_pool_entry *entry;
3050
3051         QETH_DBF_TEXT(trace,5,"inwrklst");
3052
3053         list_for_each_entry(entry,
3054                             &card->qdio.init_pool.entry_list, init_list) {
3055                 qeth_put_buffer_pool_entry(card,entry);
3056         }
3057 }
3058
3059 static void
3060 qeth_clear_working_pool_list(struct qeth_card *card)
3061 {
3062         struct qeth_buffer_pool_entry *pool_entry, *tmp;
3063
3064         QETH_DBF_TEXT(trace,5,"clwrklst");
3065         list_for_each_entry_safe(pool_entry, tmp,
3066                             &card->qdio.in_buf_pool.entry_list, list){
3067                         list_del(&pool_entry->list);
3068         }
3069 }
3070
3071 static void
3072 qeth_free_buffer_pool(struct qeth_card *card)
3073 {
3074         struct qeth_buffer_pool_entry *pool_entry, *tmp;
3075         int i=0;
3076         QETH_DBF_TEXT(trace,5,"freepool");
3077         list_for_each_entry_safe(pool_entry, tmp,
3078                                  &card->qdio.init_pool.entry_list, init_list){
3079                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
3080                         free_page((unsigned long)pool_entry->elements[i]);
3081                 list_del(&pool_entry->init_list);
3082                 kfree(pool_entry);
3083         }
3084 }
3085
3086 static int
3087 qeth_alloc_buffer_pool(struct qeth_card *card)
3088 {
3089         struct qeth_buffer_pool_entry *pool_entry;
3090         void *ptr;
3091         int i, j;
3092
3093         QETH_DBF_TEXT(trace,5,"alocpool");
3094         for (i = 0; i < card->qdio.init_pool.buf_count; ++i){
3095                 pool_entry = kmalloc(sizeof(*pool_entry), GFP_KERNEL);
3096                 if (!pool_entry){
3097                         qeth_free_buffer_pool(card);
3098                         return -ENOMEM;
3099                 }
3100                 for(j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j){
3101                         ptr = (void *) __get_free_page(GFP_KERNEL|GFP_DMA);
3102                         if (!ptr) {
3103                                 while (j > 0)
3104                                         free_page((unsigned long)
3105                                                   pool_entry->elements[--j]);
3106                                 kfree(pool_entry);
3107                                 qeth_free_buffer_pool(card);
3108                                 return -ENOMEM;
3109                         }
3110                         pool_entry->elements[j] = ptr;
3111                 }
3112                 list_add(&pool_entry->init_list,
3113                          &card->qdio.init_pool.entry_list);
3114         }
3115         return 0;
3116 }
3117
3118 int
3119 qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
3120 {
3121         QETH_DBF_TEXT(trace, 2, "realcbp");
3122
3123         if ((card->state != CARD_STATE_DOWN) &&
3124             (card->state != CARD_STATE_RECOVER))
3125                 return -EPERM;
3126
3127         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
3128         qeth_clear_working_pool_list(card);
3129         qeth_free_buffer_pool(card);
3130         card->qdio.in_buf_pool.buf_count = bufcnt;
3131         card->qdio.init_pool.buf_count = bufcnt;
3132         return qeth_alloc_buffer_pool(card);
3133 }
3134
3135 static int
3136 qeth_alloc_qdio_buffers(struct qeth_card *card)
3137 {
3138         int i, j;
3139
3140         QETH_DBF_TEXT(setup, 2, "allcqdbf");
3141
3142         if (card->qdio.state == QETH_QDIO_ALLOCATED)
3143                 return 0;
3144
3145         card->qdio.in_q = kmalloc(sizeof(struct qeth_qdio_q), 
3146                                   GFP_KERNEL|GFP_DMA);
3147         if (!card->qdio.in_q)
3148                 return - ENOMEM;
3149         QETH_DBF_TEXT(setup, 2, "inq");
3150         QETH_DBF_HEX(setup, 2, &card->qdio.in_q, sizeof(void *));
3151         memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
3152         /* give inbound qeth_qdio_buffers their qdio_buffers */
3153         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3154                 card->qdio.in_q->bufs[i].buffer =
3155                         &card->qdio.in_q->qdio_bufs[i];
3156         /* inbound buffer pool */
3157         if (qeth_alloc_buffer_pool(card)){
3158                 kfree(card->qdio.in_q);
3159                 return -ENOMEM;
3160         }
3161         /* outbound */
3162         card->qdio.out_qs =
3163                 kmalloc(card->qdio.no_out_queues *
3164                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
3165         if (!card->qdio.out_qs){
3166                 qeth_free_buffer_pool(card);
3167                 return -ENOMEM;
3168         }
3169         for (i = 0; i < card->qdio.no_out_queues; ++i){
3170                 card->qdio.out_qs[i] = kmalloc(sizeof(struct qeth_qdio_out_q),
3171                                                GFP_KERNEL|GFP_DMA);
3172                 if (!card->qdio.out_qs[i]){
3173                         while (i > 0)
3174                                 kfree(card->qdio.out_qs[--i]);
3175                         kfree(card->qdio.out_qs);
3176                         return -ENOMEM;
3177                 }
3178                 QETH_DBF_TEXT_(setup, 2, "outq %i", i);
3179                 QETH_DBF_HEX(setup, 2, &card->qdio.out_qs[i], sizeof(void *));
3180                 memset(card->qdio.out_qs[i], 0, sizeof(struct qeth_qdio_out_q));
3181                 card->qdio.out_qs[i]->queue_no = i;
3182                 /* give outbound qeth_qdio_buffers their qdio_buffers */
3183                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3184                         card->qdio.out_qs[i]->bufs[j].buffer =
3185                                 &card->qdio.out_qs[i]->qdio_bufs[j];
3186                         skb_queue_head_init(&card->qdio.out_qs[i]->bufs[j].
3187                                             skb_list);
3188                         INIT_LIST_HEAD(&card->qdio.out_qs[i]->bufs[j].ctx_list);
3189                 }
3190         }
3191         card->qdio.state = QETH_QDIO_ALLOCATED;
3192         return 0;
3193 }
3194
3195 static void
3196 qeth_free_qdio_buffers(struct qeth_card *card)
3197 {
3198         int i, j;
3199
3200         QETH_DBF_TEXT(trace, 2, "freeqdbf");
3201         if (card->qdio.state == QETH_QDIO_UNINITIALIZED)
3202                 return;
3203         kfree(card->qdio.in_q);
3204         /* inbound buffer pool */
3205         qeth_free_buffer_pool(card);
3206         /* free outbound qdio_qs */
3207         for (i = 0; i < card->qdio.no_out_queues; ++i){
3208                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3209                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3210                                         &card->qdio.out_qs[i]->bufs[j]);
3211                 kfree(card->qdio.out_qs[i]);
3212         }
3213         kfree(card->qdio.out_qs);
3214         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3215 }
3216
3217 static void
3218 qeth_clear_qdio_buffers(struct qeth_card *card)
3219 {
3220         int i, j;
3221
3222         QETH_DBF_TEXT(trace, 2, "clearqdbf");
3223         /* clear outbound buffers to free skbs */
3224         for (i = 0; i < card->qdio.no_out_queues; ++i)
3225                 if (card->qdio.out_qs[i]){
3226                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3227                                 qeth_clear_output_buffer(card->qdio.out_qs[i],
3228                                                 &card->qdio.out_qs[i]->bufs[j]);
3229                 }
3230 }
3231
3232 static void
3233 qeth_init_qdio_info(struct qeth_card *card)
3234 {
3235         QETH_DBF_TEXT(setup, 4, "intqdinf");
3236         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3237         /* inbound */
3238         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
3239         card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
3240         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
3241         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
3242         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
3243         /* outbound */
3244 }
3245
3246 static int
3247 qeth_init_qdio_queues(struct qeth_card *card)
3248 {
3249         int i, j;
3250         int rc;
3251
3252         QETH_DBF_TEXT(setup, 2, "initqdqs");
3253
3254         /* inbound queue */
3255         memset(card->qdio.in_q->qdio_bufs, 0,
3256                QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3257         qeth_initialize_working_pool_list(card);
3258         /*give only as many buffers to hardware as we have buffer pool entries*/
3259         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
3260                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
3261         card->qdio.in_q->next_buf_to_init = card->qdio.in_buf_pool.buf_count - 1;
3262         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
3263                      card->qdio.in_buf_pool.buf_count - 1, NULL);
3264         if (rc) {
3265                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
3266                 return rc;
3267         }
3268         rc = qdio_synchronize(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0);
3269         if (rc) {
3270                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
3271                 return rc;
3272         }
3273         /* outbound queue */
3274         for (i = 0; i < card->qdio.no_out_queues; ++i){
3275                 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
3276                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3277                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3278                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3279                                         &card->qdio.out_qs[i]->bufs[j]);
3280                 }
3281                 card->qdio.out_qs[i]->card = card;
3282                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
3283                 card->qdio.out_qs[i]->do_pack = 0;
3284                 atomic_set(&card->qdio.out_qs[i]->used_buffers,0);
3285                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
3286                 atomic_set(&card->qdio.out_qs[i]->state,
3287                            QETH_OUT_Q_UNLOCKED);
3288         }
3289         return 0;
3290 }
3291
3292 static int
3293 qeth_qdio_establish(struct qeth_card *card)
3294 {
3295         struct qdio_initialize init_data;
3296         char *qib_param_field;
3297         struct qdio_buffer **in_sbal_ptrs;
3298         struct qdio_buffer **out_sbal_ptrs;
3299         int i, j, k;
3300         int rc;
3301
3302         QETH_DBF_TEXT(setup, 2, "qdioest");
3303
3304         qib_param_field = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
3305                               GFP_KERNEL);
3306         if (!qib_param_field)
3307                 return -ENOMEM;
3308
3309         memset(qib_param_field, 0, QDIO_MAX_BUFFERS_PER_Q * sizeof(char));
3310
3311         qeth_create_qib_param_field(card, qib_param_field);
3312         qeth_create_qib_param_field_blkt(card, qib_param_field);
3313
3314         in_sbal_ptrs = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
3315                                GFP_KERNEL);
3316         if (!in_sbal_ptrs) {
3317                 kfree(qib_param_field);
3318                 return -ENOMEM;
3319         }
3320         for(i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3321                 in_sbal_ptrs[i] = (struct qdio_buffer *)
3322                         virt_to_phys(card->qdio.in_q->bufs[i].buffer);
3323
3324         out_sbal_ptrs =
3325                 kmalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
3326                         sizeof(void *), GFP_KERNEL);
3327         if (!out_sbal_ptrs) {
3328                 kfree(in_sbal_ptrs);
3329                 kfree(qib_param_field);
3330                 return -ENOMEM;
3331         }
3332         for(i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
3333                 for(j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k){
3334                         out_sbal_ptrs[k] = (struct qdio_buffer *)
3335                                 virt_to_phys(card->qdio.out_qs[i]->
3336                                              bufs[j].buffer);
3337                 }
3338
3339         memset(&init_data, 0, sizeof(struct qdio_initialize));
3340         init_data.cdev                   = CARD_DDEV(card);
3341         init_data.q_format               = qeth_get_qdio_q_format(card);
3342         init_data.qib_param_field_format = 0;
3343         init_data.qib_param_field        = qib_param_field;
3344         init_data.min_input_threshold    = QETH_MIN_INPUT_THRESHOLD;
3345         init_data.max_input_threshold    = QETH_MAX_INPUT_THRESHOLD;
3346         init_data.min_output_threshold   = QETH_MIN_OUTPUT_THRESHOLD;
3347         init_data.max_output_threshold   = QETH_MAX_OUTPUT_THRESHOLD;
3348         init_data.no_input_qs            = 1;
3349         init_data.no_output_qs           = card->qdio.no_out_queues;
3350         init_data.input_handler          = (qdio_handler_t *)
3351                                            qeth_qdio_input_handler;
3352         init_data.output_handler         = (qdio_handler_t *)
3353                                            qeth_qdio_output_handler;
3354         init_data.int_parm               = (unsigned long) card;
3355         init_data.flags                  = QDIO_INBOUND_0COPY_SBALS |
3356                                            QDIO_OUTBOUND_0COPY_SBALS |
3357                                            QDIO_USE_OUTBOUND_PCIS;
3358         init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
3359         init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
3360
3361         if (!(rc = qdio_initialize(&init_data)))
3362                 card->qdio.state = QETH_QDIO_ESTABLISHED;
3363
3364         kfree(out_sbal_ptrs);
3365         kfree(in_sbal_ptrs);
3366         kfree(qib_param_field);
3367         return rc;
3368 }
3369
3370 static int
3371 qeth_qdio_activate(struct qeth_card *card)
3372 {
3373         QETH_DBF_TEXT(setup,3,"qdioact");
3374         return qdio_activate(CARD_DDEV(card), 0);
3375 }
3376
3377 static int
3378 qeth_clear_channel(struct qeth_channel *channel)
3379 {
3380         unsigned long flags;
3381         struct qeth_card *card;
3382         int rc;
3383
3384         QETH_DBF_TEXT(trace,3,"clearch");
3385         card = CARD_FROM_CDEV(channel->ccwdev);
3386         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
3387         rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
3388         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
3389
3390         if (rc)
3391                 return rc;
3392         rc = wait_event_interruptible_timeout(card->wait_q,
3393                         channel->state==CH_STATE_STOPPED, QETH_TIMEOUT);
3394         if (rc == -ERESTARTSYS)
3395                 return rc;
3396         if (channel->state != CH_STATE_STOPPED)
3397                 return -ETIME;
3398         channel->state = CH_STATE_DOWN;
3399         return 0;
3400 }
3401
3402 static int
3403 qeth_halt_channel(struct qeth_channel *channel)
3404 {
3405         unsigned long flags;
3406         struct qeth_card *card;
3407         int rc;
3408
3409         QETH_DBF_TEXT(trace,3,"haltch");
3410         card = CARD_FROM_CDEV(channel->ccwdev);
3411         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
3412         rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
3413         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
3414
3415         if (rc)
3416                 return rc;
3417         rc = wait_event_interruptible_timeout(card->wait_q,
3418                         channel->state==CH_STATE_HALTED, QETH_TIMEOUT);
3419         if (rc == -ERESTARTSYS)
3420                 return rc;
3421         if (channel->state != CH_STATE_HALTED)
3422                 return -ETIME;
3423         return 0;
3424 }
3425
3426 static int
3427 qeth_halt_channels(struct qeth_card *card)
3428 {
3429         int rc1 = 0, rc2=0, rc3 = 0;
3430
3431         QETH_DBF_TEXT(trace,3,"haltchs");
3432         rc1 = qeth_halt_channel(&card->read);
3433         rc2 = qeth_halt_channel(&card->write);
3434         rc3 = qeth_halt_channel(&card->data);
3435         if (rc1)
3436                 return rc1;
3437         if (rc2) 
3438                 return rc2;
3439         return rc3;
3440 }
3441 static int
3442 qeth_clear_channels(struct qeth_card *card)
3443 {
3444         int rc1 = 0, rc2=0, rc3 = 0;
3445
3446         QETH_DBF_TEXT(trace,3,"clearchs");
3447         rc1 = qeth_clear_channel(&card->read);
3448         rc2 = qeth_clear_channel(&card->write);
3449         rc3 = qeth_clear_channel(&card->data);
3450         if (rc1)
3451                 return rc1;
3452         if (rc2) 
3453                 return rc2;
3454         return rc3;
3455 }
3456
3457 static int
3458 qeth_clear_halt_card(struct qeth_card *card, int halt)
3459 {
3460         int rc = 0;
3461
3462         QETH_DBF_TEXT(trace,3,"clhacrd");
3463         QETH_DBF_HEX(trace, 3, &card, sizeof(void *));
3464
3465         if (halt)
3466                 rc = qeth_halt_channels(card);
3467         if (rc)
3468                 return rc;
3469         return qeth_clear_channels(card);
3470 }
3471
3472 static int
3473 qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
3474 {
3475         int rc = 0;
3476
3477         QETH_DBF_TEXT(trace,3,"qdioclr");
3478         if (card->qdio.state == QETH_QDIO_ESTABLISHED){
3479                 if ((rc = qdio_cleanup(CARD_DDEV(card),
3480                              (card->info.type == QETH_CARD_TYPE_IQD) ?
3481                              QDIO_FLAG_CLEANUP_USING_HALT :
3482                              QDIO_FLAG_CLEANUP_USING_CLEAR)))
3483                         QETH_DBF_TEXT_(trace, 3, "1err%d", rc);
3484                 card->qdio.state = QETH_QDIO_ALLOCATED;
3485         }
3486         if ((rc = qeth_clear_halt_card(card, use_halt)))
3487                 QETH_DBF_TEXT_(trace, 3, "2err%d", rc);
3488         card->state = CARD_STATE_DOWN;
3489         return rc;
3490 }
3491
3492 static int
3493 qeth_dm_act(struct qeth_card *card)
3494 {
3495         int rc;
3496         struct qeth_cmd_buffer *iob;
3497
3498         QETH_DBF_TEXT(setup,2,"dmact");
3499
3500         iob = qeth_wait_for_buffer(&card->write);
3501         memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
3502
3503         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
3504                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
3505         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
3506                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3507         rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
3508         return rc;
3509 }
3510
3511 static int
3512 qeth_mpc_initialize(struct qeth_card *card)
3513 {
3514         int rc;
3515
3516         QETH_DBF_TEXT(setup,2,"mpcinit");
3517
3518         if ((rc = qeth_issue_next_read(card))){
3519                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
3520                 return rc;
3521         }
3522         if ((rc = qeth_cm_enable(card))){
3523                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
3524                 goto out_qdio;
3525         }
3526         if ((rc = qeth_cm_setup(card))){
3527                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
3528                 goto out_qdio;
3529         }
3530         if ((rc = qeth_ulp_enable(card))){
3531                 QETH_DBF_TEXT_(setup, 2, "4err%d", rc);
3532                 goto out_qdio;
3533         }
3534         if ((rc = qeth_ulp_setup(card))){
3535                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
3536                 goto out_qdio;
3537         }
3538         if ((rc = qeth_alloc_qdio_buffers(card))){
3539                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
3540                 goto out_qdio;
3541         }
3542         if ((rc = qeth_qdio_establish(card))){
3543                 QETH_DBF_TEXT_(setup, 2, "6err%d", rc);
3544                 qeth_free_qdio_buffers(card);
3545                 goto out_qdio;
3546         }
3547         if ((rc = qeth_qdio_activate(card))){
3548                 QETH_DBF_TEXT_(setup, 2, "7err%d", rc);
3549                 goto out_qdio;
3550         }
3551         if ((rc = qeth_dm_act(card))){
3552                 QETH_DBF_TEXT_(setup, 2, "8err%d", rc);
3553                 goto out_qdio;
3554         }
3555
3556         return 0;
3557 out_qdio:
3558         qeth_qdio_clear_card(card, card->info.type!=QETH_CARD_TYPE_IQD);
3559         return rc;
3560 }
3561
3562 static struct net_device *
3563 qeth_get_netdevice(enum qeth_card_types type, enum qeth_link_types linktype)
3564 {
3565         struct net_device *dev = NULL;
3566
3567         switch (type) {
3568         case QETH_CARD_TYPE_OSAE:
3569                 switch (linktype) {
3570                 case QETH_LINK_TYPE_LANE_TR:
3571                 case QETH_LINK_TYPE_HSTR:
3572 #ifdef CONFIG_TR
3573                         dev = alloc_trdev(0);
3574 #endif /* CONFIG_TR */
3575                         break;
3576                 default:
3577                         dev = alloc_etherdev(0);
3578                 }
3579                 break;
3580         case QETH_CARD_TYPE_IQD:
3581                 dev = alloc_netdev(0, "hsi%d", ether_setup);
3582                 break;
3583         case QETH_CARD_TYPE_OSN:
3584                 dev = alloc_netdev(0, "osn%d", ether_setup);
3585                 break;
3586         default:
3587                 dev = alloc_etherdev(0);
3588         }
3589         return dev;
3590 }
3591
3592 /*hard_header fake function; used in case fake_ll is set */
3593 static int
3594 qeth_fake_header(struct sk_buff *skb, struct net_device *dev,
3595                      unsigned short type, void *daddr, void *saddr,
3596                      unsigned len)
3597 {
3598         if(dev->type == ARPHRD_IEEE802_TR){
3599                 struct trh_hdr *hdr;
3600                 hdr = (struct trh_hdr *)skb_push(skb, QETH_FAKE_LL_LEN_TR);
3601                 memcpy(hdr->saddr, dev->dev_addr, TR_ALEN);
3602                 memcpy(hdr->daddr, "FAKELL", TR_ALEN);
3603                 return QETH_FAKE_LL_LEN_TR;
3604
3605         } else {
3606                 struct ethhdr *hdr;
3607                 hdr = (struct ethhdr *)skb_push(skb, QETH_FAKE_LL_LEN_ETH);
3608                 memcpy(hdr->h_source, dev->dev_addr, ETH_ALEN);
3609                 memcpy(hdr->h_dest, "FAKELL", ETH_ALEN);
3610                 if (type != ETH_P_802_3)
3611                         hdr->h_proto = htons(type);
3612                 else
3613                         hdr->h_proto = htons(len);
3614                 return QETH_FAKE_LL_LEN_ETH;
3615
3616         }
3617 }
3618
3619 static inline int
3620 qeth_send_packet(struct qeth_card *, struct sk_buff *);
3621
3622 static int
3623 qeth_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
3624 {
3625         int rc;
3626         struct qeth_card *card;
3627
3628         QETH_DBF_TEXT(trace, 6, "hrdstxmi");
3629         card = (struct qeth_card *)dev->priv;
3630         if (skb==NULL) {
3631                 card->stats.tx_dropped++;
3632                 card->stats.tx_errors++;
3633                 /* return OK; otherwise ksoftirqd goes to 100% */
3634                 return NETDEV_TX_OK;
3635         }
3636         if ((card->state != CARD_STATE_UP) || !card->lan_online) {
3637                 card->stats.tx_dropped++;
3638                 card->stats.tx_errors++;
3639                 card->stats.tx_carrier_errors++;
3640                 dev_kfree_skb_any(skb);
3641                 /* return OK; otherwise ksoftirqd goes to 100% */
3642                 return NETDEV_TX_OK;
3643         }
3644 #ifdef CONFIG_QETH_PERF_STATS
3645         card->perf_stats.outbound_cnt++;
3646         card->perf_stats.outbound_start_time = qeth_get_micros();
3647 #endif
3648         netif_stop_queue(dev);
3649         if ((rc = qeth_send_packet(card, skb))) {
3650                 if (rc == -EBUSY) {
3651                         return NETDEV_TX_BUSY;
3652                 } else {
3653                         card->stats.tx_errors++;
3654                         card->stats.tx_dropped++;
3655                         dev_kfree_skb_any(skb);
3656                         /*set to OK; otherwise ksoftirqd goes to 100% */
3657                         rc = NETDEV_TX_OK;
3658                 }
3659         }
3660         netif_wake_queue(dev);
3661 #ifdef CONFIG_QETH_PERF_STATS
3662         card->perf_stats.outbound_time += qeth_get_micros() -
3663                 card->perf_stats.outbound_start_time;
3664 #endif
3665         return rc;
3666 }
3667
3668 static int
3669 qeth_verify_vlan_dev(struct net_device *dev, struct qeth_card *card)
3670 {
3671         int rc = 0;
3672 #ifdef CONFIG_QETH_VLAN
3673         struct vlan_group *vg;
3674         int i;
3675
3676         if (!(vg = card->vlangrp))
3677                 return rc;
3678
3679         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++){
3680                 if (vg->vlan_devices[i] == dev){
3681                         rc = QETH_VLAN_CARD;
3682                         break;
3683                 }
3684         }
3685 #endif
3686         return rc;
3687 }
3688
3689 static int
3690 qeth_verify_dev(struct net_device *dev)
3691 {
3692         struct qeth_card *card;
3693         unsigned long flags;
3694         int rc = 0;
3695
3696         read_lock_irqsave(&qeth_card_list.rwlock, flags);
3697         list_for_each_entry(card, &qeth_card_list.list, list){
3698                 if (card->dev == dev){
3699                         rc = QETH_REAL_CARD;
3700                         break;
3701                 }
3702                 rc = qeth_verify_vlan_dev(dev, card);
3703                 if (rc)
3704                         break;
3705         }
3706         read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
3707
3708         return rc;
3709 }
3710
3711 static struct qeth_card *
3712 qeth_get_card_from_dev(struct net_device *dev)
3713 {
3714         struct qeth_card *card = NULL;
3715         int rc;
3716
3717         rc = qeth_verify_dev(dev);
3718         if (rc == QETH_REAL_CARD)
3719                 card = (struct qeth_card *)dev->priv;
3720         else if (rc == QETH_VLAN_CARD)
3721                 card = (struct qeth_card *)
3722                         VLAN_DEV_INFO(dev)->real_dev->priv;
3723
3724         QETH_DBF_TEXT_(trace, 4, "%d", rc);
3725         return card ;
3726 }
3727
3728 static void
3729 qeth_tx_timeout(struct net_device *dev)
3730 {
3731         struct qeth_card *card;
3732
3733         card = (struct qeth_card *) dev->priv;
3734         card->stats.tx_errors++;
3735         qeth_schedule_recovery(card);
3736 }
3737
3738 static int
3739 qeth_open(struct net_device *dev)
3740 {
3741         struct qeth_card *card;
3742
3743         QETH_DBF_TEXT(trace, 4, "qethopen");
3744
3745         card = (struct qeth_card *) dev->priv;
3746
3747         if (card->state != CARD_STATE_SOFTSETUP)
3748                 return -ENODEV;
3749
3750         if ( (card->info.type != QETH_CARD_TYPE_OSN) &&
3751              (card->options.layer2) &&
3752              (!card->info.layer2_mac_registered)) {
3753                 QETH_DBF_TEXT(trace,4,"nomacadr");
3754                 return -EPERM;
3755         }
3756         card->dev->flags |= IFF_UP;
3757         netif_start_queue(dev);
3758         card->data.state = CH_STATE_UP;
3759         card->state = CARD_STATE_UP;
3760
3761         if (!card->lan_online){
3762                 if (netif_carrier_ok(dev))
3763                         netif_carrier_off(dev);
3764         }
3765         return 0;
3766 }
3767
3768 static int
3769 qeth_stop(struct net_device *dev)
3770 {
3771         struct qeth_card *card;
3772
3773         QETH_DBF_TEXT(trace, 4, "qethstop");
3774
3775         card = (struct qeth_card *) dev->priv;
3776
3777         netif_stop_queue(dev);
3778         card->dev->flags &= ~IFF_UP;
3779         if (card->state == CARD_STATE_UP)
3780                 card->state = CARD_STATE_SOFTSETUP;
3781         return 0;
3782 }
3783
3784 static inline int
3785 qeth_get_cast_type(struct qeth_card *card, struct sk_buff *skb)
3786 {
3787         int cast_type = RTN_UNSPEC;
3788
3789         if (card->info.type == QETH_CARD_TYPE_OSN)
3790                 return cast_type;
3791
3792         if (skb->dst && skb->dst->neighbour){
3793                 cast_type = skb->dst->neighbour->type;
3794                 if ((cast_type == RTN_BROADCAST) ||
3795                     (cast_type == RTN_MULTICAST) ||
3796                     (cast_type == RTN_ANYCAST))
3797                         return cast_type;
3798                 else
3799                         return RTN_UNSPEC;
3800         }
3801         /* try something else */
3802         if (skb->protocol == ETH_P_IPV6)
3803                 return (skb->nh.raw[24] == 0xff) ? RTN_MULTICAST : 0;
3804         else if (skb->protocol == ETH_P_IP)
3805                 return ((skb->nh.raw[16] & 0xf0) == 0xe0) ? RTN_MULTICAST : 0;
3806         /* ... */
3807         if (!memcmp(skb->data, skb->dev->broadcast, 6))
3808                 return RTN_BROADCAST;
3809         else {
3810                 u16 hdr_mac;
3811
3812                 hdr_mac = *((u16 *)skb->data);
3813                 /* tr multicast? */
3814                 switch (card->info.link_type) {
3815                 case QETH_LINK_TYPE_HSTR:
3816                 case QETH_LINK_TYPE_LANE_TR:
3817                         if ((hdr_mac == QETH_TR_MAC_NC) ||
3818                             (hdr_mac == QETH_TR_MAC_C))
3819                                 return RTN_MULTICAST;
3820                 /* eth or so multicast? */
3821                 default:
3822                         if ((hdr_mac == QETH_ETH_MAC_V4) ||
3823                             (hdr_mac == QETH_ETH_MAC_V6))
3824                                 return RTN_MULTICAST;
3825                 }
3826         }
3827         return cast_type;
3828 }
3829
3830 static inline int
3831 qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
3832                         int ipv, int cast_type)
3833 {
3834         if (!ipv && (card->info.type == QETH_CARD_TYPE_OSAE))
3835                 return card->qdio.default_out_queue;
3836         switch (card->qdio.no_out_queues) {
3837         case 4:
3838                 if (cast_type && card->info.is_multicast_different)
3839                         return card->info.is_multicast_different &
3840                                 (card->qdio.no_out_queues - 1);
3841                 if (card->qdio.do_prio_queueing && (ipv == 4)) {
3842                         if (card->qdio.do_prio_queueing==QETH_PRIO_Q_ING_TOS){
3843                                 if (skb->nh.iph->tos & IP_TOS_NOTIMPORTANT)
3844                                         return 3;
3845                                 if (skb->nh.iph->tos & IP_TOS_HIGHRELIABILITY)
3846                                         return 2;
3847                                 if (skb->nh.iph->tos & IP_TOS_HIGHTHROUGHPUT)
3848                                         return 1;
3849                                 if (skb->nh.iph->tos & IP_TOS_LOWDELAY)
3850                                         return 0;
3851                         }
3852                         if (card->qdio.do_prio_queueing==QETH_PRIO_Q_ING_PREC)
3853                                 return 3 - (skb->nh.iph->tos >> 6);
3854                 } else if (card->qdio.do_prio_queueing && (ipv == 6)) {
3855                         /* TODO: IPv6!!! */
3856                 }
3857                 return card->qdio.default_out_queue;
3858         case 1: /* fallthrough for single-out-queue 1920-device */
3859         default:
3860                 return card->qdio.default_out_queue;
3861         }
3862 }
3863
3864 static inline int
3865 qeth_get_ip_version(struct sk_buff *skb)
3866 {
3867         switch (skb->protocol) {
3868         case ETH_P_IPV6:
3869                 return 6;
3870         case ETH_P_IP:
3871                 return 4;
3872         default:
3873                 return 0;
3874         }
3875 }
3876
3877 static inline int
3878 qeth_prepare_skb(struct qeth_card *card, struct sk_buff **skb,
3879                  struct qeth_hdr **hdr, int ipv)
3880 {
3881         int rc = 0;
3882 #ifdef CONFIG_QETH_VLAN
3883         u16 *tag;
3884 #endif
3885
3886         QETH_DBF_TEXT(trace, 6, "prepskb");
3887         if (card->info.type == QETH_CARD_TYPE_OSN) {
3888                 *hdr = (struct qeth_hdr *)(*skb)->data;
3889                 return rc;
3890         }
3891         rc = qeth_realloc_headroom(card, skb, sizeof(struct qeth_hdr));
3892         if (rc)
3893                 return rc;
3894 #ifdef CONFIG_QETH_VLAN
3895         if (card->vlangrp && vlan_tx_tag_present(*skb) &&
3896             ((ipv == 6) || card->options.layer2) ) {
3897                 /*
3898                  * Move the mac addresses (6 bytes src, 6 bytes dest)
3899                  * to the beginning of the new header.  We are using three
3900                  * memcpys instead of one memmove to save cycles.
3901                  */
3902                 skb_push(*skb, VLAN_HLEN);
3903                 memcpy((*skb)->data, (*skb)->data + 4, 4);
3904                 memcpy((*skb)->data + 4, (*skb)->data + 8, 4);
3905                 memcpy((*skb)->data + 8, (*skb)->data + 12, 4);
3906                 tag = (u16 *)((*skb)->data + 12);
3907                 /*
3908                  * first two bytes  = ETH_P_8021Q (0x8100)
3909                  * second two bytes = VLANID
3910                  */
3911                 *tag = __constant_htons(ETH_P_8021Q);
3912                 *(tag + 1) = htons(vlan_tx_tag_get(*skb));
3913         }
3914 #endif
3915         *hdr = (struct qeth_hdr *)
3916                 qeth_push_skb(card, skb, sizeof(struct qeth_hdr));
3917         if (hdr == NULL)
3918                 return -EINVAL;
3919         return 0;
3920 }
3921
3922 static inline u8
3923 qeth_get_qeth_hdr_flags4(int cast_type)
3924 {
3925         if (cast_type == RTN_MULTICAST)
3926                 return QETH_CAST_MULTICAST;
3927         if (cast_type == RTN_BROADCAST)
3928                 return QETH_CAST_BROADCAST;
3929         return QETH_CAST_UNICAST;
3930 }
3931
3932 static inline u8
3933 qeth_get_qeth_hdr_flags6(int cast_type)
3934 {
3935         u8 ct = QETH_HDR_PASSTHRU | QETH_HDR_IPV6;
3936         if (cast_type == RTN_MULTICAST)
3937                 return ct | QETH_CAST_MULTICAST;
3938         if (cast_type == RTN_ANYCAST)
3939                 return ct | QETH_CAST_ANYCAST;
3940         if (cast_type == RTN_BROADCAST)
3941                 return ct | QETH_CAST_BROADCAST;
3942         return ct | QETH_CAST_UNICAST;
3943 }
3944
3945 static inline void
3946 qeth_layer2_get_packet_type(struct qeth_card *card, struct qeth_hdr *hdr,
3947                             struct sk_buff *skb)
3948 {
3949         __u16 hdr_mac;
3950
3951         if (!memcmp(skb->data+QETH_HEADER_SIZE,
3952                     skb->dev->broadcast,6)) { /* broadcast? */
3953                 *(__u32 *)hdr->hdr.l2.flags |=
3954                          QETH_LAYER2_FLAG_BROADCAST << 8;
3955                 return;
3956         }
3957         hdr_mac=*((__u16*)skb->data);
3958         /* tr multicast? */
3959         switch (card->info.link_type) {
3960         case QETH_LINK_TYPE_HSTR:
3961         case QETH_LINK_TYPE_LANE_TR:
3962                 if ((hdr_mac == QETH_TR_MAC_NC) ||
3963                     (hdr_mac == QETH_TR_MAC_C) )
3964                         *(__u32 *)hdr->hdr.l2.flags |=
3965                                 QETH_LAYER2_FLAG_MULTICAST << 8;
3966                 else
3967                         *(__u32 *)hdr->hdr.l2.flags |=
3968                                 QETH_LAYER2_FLAG_UNICAST << 8;
3969                 break;
3970                 /* eth or so multicast? */
3971         default:
3972                 if ( (hdr_mac==QETH_ETH_MAC_V4) ||
3973                      (hdr_mac==QETH_ETH_MAC_V6) )
3974                         *(__u32 *)hdr->hdr.l2.flags |=
3975                                 QETH_LAYER2_FLAG_MULTICAST << 8;
3976                 else
3977                         *(__u32 *)hdr->hdr.l2.flags |=
3978                                 QETH_LAYER2_FLAG_UNICAST << 8;
3979         }
3980 }
3981
3982 static inline void
3983 qeth_layer2_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
3984                         struct sk_buff *skb, int cast_type)
3985 {
3986         memset(hdr, 0, sizeof(struct qeth_hdr));
3987         hdr->hdr.l2.id = QETH_HEADER_TYPE_LAYER2;
3988
3989         /* set byte 0 to "0x02" and byte 3 to casting flags */
3990         if (cast_type==RTN_MULTICAST)
3991                 *(__u32 *)hdr->hdr.l2.flags |= QETH_LAYER2_FLAG_MULTICAST << 8;
3992         else if (cast_type==RTN_BROADCAST)
3993                 *(__u32 *)hdr->hdr.l2.flags |= QETH_LAYER2_FLAG_BROADCAST << 8;
3994          else
3995                 qeth_layer2_get_packet_type(card, hdr, skb);
3996
3997         hdr->hdr.l2.pkt_length = skb->len-QETH_HEADER_SIZE;
3998 #ifdef CONFIG_QETH_VLAN
3999         /* VSWITCH relies on the VLAN
4000          * information to be present in
4001          * the QDIO header */
4002         if ((card->vlangrp != NULL) &&
4003             vlan_tx_tag_present(skb)) {
4004                 *(__u32 *)hdr->hdr.l2.flags |= QETH_LAYER2_FLAG_VLAN << 8;
4005                 hdr->hdr.l2.vlan_id = vlan_tx_tag_get(skb);
4006         }
4007 #endif
4008 }
4009
4010 void
4011 qeth_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
4012                 struct sk_buff *skb, int ipv, int cast_type)
4013 {
4014         QETH_DBF_TEXT(trace, 6, "fillhdr");
4015
4016         memset(hdr, 0, sizeof(struct qeth_hdr));
4017         if (card->options.layer2) {
4018                 qeth_layer2_fill_header(card, hdr, skb, cast_type);
4019                 return;
4020         }
4021         hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
4022         hdr->hdr.l3.ext_flags = 0;
4023 #ifdef CONFIG_QETH_VLAN
4024         /*
4025          * before we're going to overwrite this location with next hop ip.
4026          * v6 uses passthrough, v4 sets the tag in the QDIO header.
4027          */
4028         if (card->vlangrp && vlan_tx_tag_present(skb)) {
4029                 hdr->hdr.l3.ext_flags = (ipv == 4) ?
4030                         QETH_HDR_EXT_VLAN_FRAME :
4031                         QETH_HDR_EXT_INCLUDE_VLAN_TAG;
4032                 hdr->hdr.l3.vlan_id = vlan_tx_tag_get(skb);
4033         }
4034 #endif /* CONFIG_QETH_VLAN */
4035         hdr->hdr.l3.length = skb->len - sizeof(struct qeth_hdr);
4036         if (ipv == 4) {  /* IPv4 */
4037                 hdr->hdr.l3.flags = qeth_get_qeth_hdr_flags4(cast_type);
4038                 memset(hdr->hdr.l3.dest_addr, 0, 12);
4039                 if ((skb->dst) && (skb->dst->neighbour)) {
4040                         *((u32 *) (&hdr->hdr.l3.dest_addr[12])) =
4041                             *((u32 *) skb->dst->neighbour->primary_key);
4042                 } else {
4043                         /* fill in destination address used in ip header */
4044                         *((u32 *) (&hdr->hdr.l3.dest_addr[12])) = skb->nh.iph->daddr;
4045                 }
4046         } else if (ipv == 6) { /* IPv6 or passthru */
4047                 hdr->hdr.l3.flags = qeth_get_qeth_hdr_flags6(cast_type);
4048                 if ((skb->dst) && (skb->dst->neighbour)) {
4049                         memcpy(hdr->hdr.l3.dest_addr,
4050                                skb->dst->neighbour->primary_key, 16);
4051                 } else {
4052                         /* fill in destination address used in ip header */
4053                         memcpy(hdr->hdr.l3.dest_addr, &skb->nh.ipv6h->daddr, 16);
4054                 }
4055         } else { /* passthrough */
4056                 if((skb->dev->type == ARPHRD_IEEE802_TR) &&
4057                     !memcmp(skb->data + sizeof(struct qeth_hdr) + 
4058                     sizeof(__u16), skb->dev->broadcast, 6)) {
4059                         hdr->hdr.l3.flags = QETH_CAST_BROADCAST |
4060                                                 QETH_HDR_PASSTHRU;
4061                 } else if (!memcmp(skb->data + sizeof(struct qeth_hdr),
4062                             skb->dev->broadcast, 6)) {   /* broadcast? */
4063                         hdr->hdr.l3.flags = QETH_CAST_BROADCAST |
4064                                                 QETH_HDR_PASSTHRU;
4065                 } else {
4066                         hdr->hdr.l3.flags = (cast_type == RTN_MULTICAST) ?
4067                                 QETH_CAST_MULTICAST | QETH_HDR_PASSTHRU :
4068                                 QETH_CAST_UNICAST | QETH_HDR_PASSTHRU;
4069                 }
4070         }
4071 }
4072
4073 static inline void
4074 __qeth_fill_buffer(struct sk_buff *skb, struct qdio_buffer *buffer,
4075                    int is_tso, int *next_element_to_fill)
4076 {
4077         int length = skb->len;
4078         int length_here;
4079         int element;
4080         char *data;
4081         int first_lap ;
4082
4083         element = *next_element_to_fill;
4084         data = skb->data;
4085         first_lap = (is_tso == 0 ? 1 : 0);
4086
4087         while (length > 0) {
4088                 /* length_here is the remaining amount of data in this page */
4089                 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
4090                 if (length < length_here)
4091                         length_here = length;
4092
4093                 buffer->element[element].addr = data;
4094                 buffer->element[element].length = length_here;
4095                 length -= length_here;
4096                 if (!length) {
4097                         if (first_lap)
4098                                 buffer->element[element].flags = 0;
4099                         else
4100                                 buffer->element[element].flags =
4101                                     SBAL_FLAGS_LAST_FRAG;
4102                 } else {
4103                         if (first_lap)
4104                                 buffer->element[element].flags =
4105                                     SBAL_FLAGS_FIRST_FRAG;
4106                         else
4107                                 buffer->element[element].flags =
4108                                     SBAL_FLAGS_MIDDLE_FRAG;
4109                 }
4110                 data += length_here;
4111                 element++;
4112                 first_lap = 0;
4113         }
4114         *next_element_to_fill = element;
4115 }
4116
4117 static inline int
4118 qeth_fill_buffer(struct qeth_qdio_out_q *queue,
4119                  struct qeth_qdio_out_buffer *buf,
4120                  struct sk_buff *skb)
4121 {
4122         struct qdio_buffer *buffer;
4123         struct qeth_hdr_tso *hdr;
4124         int flush_cnt = 0, hdr_len, large_send = 0;
4125
4126         QETH_DBF_TEXT(trace, 6, "qdfillbf");
4127
4128         buffer = buf->buffer;
4129         atomic_inc(&skb->users);
4130         skb_queue_tail(&buf->skb_list, skb);
4131
4132         hdr  = (struct qeth_hdr_tso *) skb->data;
4133         /*check first on TSO ....*/
4134         if (hdr->hdr.hdr.l3.id == QETH_HEADER_TYPE_TSO) {
4135                 int element = buf->next_element_to_fill;
4136
4137                 hdr_len = sizeof(struct qeth_hdr_tso) + hdr->ext.dg_hdr_len;
4138                 /*fill first buffer entry only with header information */
4139                 buffer->element[element].addr = skb->data;
4140                 buffer->element[element].length = hdr_len;
4141                 buffer->element[element].flags = SBAL_FLAGS_FIRST_FRAG;
4142                 buf->next_element_to_fill++;
4143                 skb->data += hdr_len;
4144                 skb->len  -= hdr_len;
4145                 large_send = 1;
4146         }
4147         if (skb_shinfo(skb)->nr_frags == 0)
4148                 __qeth_fill_buffer(skb, buffer, large_send,
4149                                    (int *)&buf->next_element_to_fill);
4150         else
4151                 __qeth_fill_buffer_frag(skb, buffer, large_send,
4152                                         (int *)&buf->next_element_to_fill);
4153
4154         if (!queue->do_pack) {
4155                 QETH_DBF_TEXT(trace, 6, "fillbfnp");
4156                 /* set state to PRIMED -> will be flushed */
4157                 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4158                 flush_cnt = 1;
4159         } else {
4160                 QETH_DBF_TEXT(trace, 6, "fillbfpa");
4161 #ifdef CONFIG_QETH_PERF_STATS
4162                 queue->card->perf_stats.skbs_sent_pack++;
4163 #endif
4164                 if (buf->next_element_to_fill >=
4165                                 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
4166                         /*
4167                          * packed buffer if full -> set state PRIMED
4168                          * -> will be flushed
4169                          */
4170                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4171                         flush_cnt = 1;
4172                 }
4173         }
4174         return flush_cnt;
4175 }
4176
4177 static inline int
4178 qeth_do_send_packet_fast(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4179                          struct sk_buff *skb, struct qeth_hdr *hdr,
4180                          int elements_needed,
4181                          struct qeth_eddp_context *ctx)
4182 {
4183         struct qeth_qdio_out_buffer *buffer;
4184         int buffers_needed = 0;
4185         int flush_cnt = 0;
4186         int index;
4187
4188         QETH_DBF_TEXT(trace, 6, "dosndpfa");
4189
4190         /* spin until we get the queue ... */
4191         while (atomic_compare_and_swap(QETH_OUT_Q_UNLOCKED,
4192                                        QETH_OUT_Q_LOCKED,
4193                                        &queue->state));
4194         /* ... now we've got the queue */
4195         index = queue->next_buf_to_fill;
4196         buffer = &queue->bufs[queue->next_buf_to_fill];
4197         /*
4198          * check if buffer is empty to make sure that we do not 'overtake'
4199          * ourselves and try to fill a buffer that is already primed
4200          */
4201         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
4202                 card->stats.tx_dropped++;
4203                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4204                 return -EBUSY;
4205         }
4206         if (ctx == NULL)
4207                 queue->next_buf_to_fill = (queue->next_buf_to_fill + 1) %
4208                                           QDIO_MAX_BUFFERS_PER_Q;
4209         else {
4210                 buffers_needed = qeth_eddp_check_buffers_for_context(queue,ctx);
4211                 if (buffers_needed < 0) {
4212                         card->stats.tx_dropped++;
4213                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4214                         return -EBUSY;
4215                 }
4216                 queue->next_buf_to_fill =
4217                         (queue->next_buf_to_fill + buffers_needed) %
4218                         QDIO_MAX_BUFFERS_PER_Q;
4219         }
4220         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4221         if (ctx == NULL) {
4222                 qeth_fill_buffer(queue, buffer, skb);
4223                 qeth_flush_buffers(queue, 0, index, 1);
4224         } else {
4225                 flush_cnt = qeth_eddp_fill_buffer(queue, ctx, index);
4226                 WARN_ON(buffers_needed != flush_cnt);
4227                 qeth_flush_buffers(queue, 0, index, flush_cnt);
4228         }
4229         return 0;
4230 }
4231
4232 static inline int
4233 qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4234                     struct sk_buff *skb, struct qeth_hdr *hdr,
4235                     int elements_needed, struct qeth_eddp_context *ctx)
4236 {
4237         struct qeth_qdio_out_buffer *buffer;
4238         int start_index;
4239         int flush_count = 0;
4240         int do_pack = 0;
4241         int tmp;
4242         int rc = 0;
4243
4244         QETH_DBF_TEXT(trace, 6, "dosndpkt");
4245
4246         /* spin until we get the queue ... */
4247         while (atomic_compare_and_swap(QETH_OUT_Q_UNLOCKED,
4248                                        QETH_OUT_Q_LOCKED,
4249                                        &queue->state));
4250         start_index = queue->next_buf_to_fill;
4251         buffer = &queue->bufs[queue->next_buf_to_fill];
4252         /*
4253          * check if buffer is empty to make sure that we do not 'overtake'
4254          * ourselves and try to fill a buffer that is already primed
4255          */
4256         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY){
4257                 card->stats.tx_dropped++;
4258                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4259                 return -EBUSY;
4260         }
4261         /* check if we need to switch packing state of this queue */
4262         qeth_switch_to_packing_if_needed(queue);
4263         if (queue->do_pack){
4264                 do_pack = 1;
4265                 if (ctx == NULL) {
4266                         /* does packet fit in current buffer? */
4267                         if((QETH_MAX_BUFFER_ELEMENTS(card) -
4268                             buffer->next_element_to_fill) < elements_needed){
4269                                 /* ... no -> set state PRIMED */
4270                                 atomic_set(&buffer->state,QETH_QDIO_BUF_PRIMED);
4271                                 flush_count++;
4272                                 queue->next_buf_to_fill =
4273                                         (queue->next_buf_to_fill + 1) %
4274                                         QDIO_MAX_BUFFERS_PER_Q;
4275                                 buffer = &queue->bufs[queue->next_buf_to_fill];
4276                                 /* we did a step forward, so check buffer state
4277                                  * again */
4278                                 if (atomic_read(&buffer->state) !=
4279                                                 QETH_QDIO_BUF_EMPTY){
4280                                         card->stats.tx_dropped++;
4281                                         qeth_flush_buffers(queue, 0, start_index, flush_count);
4282                                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4283                                         return -EBUSY;
4284                                 }
4285                         }
4286                 } else {
4287                         /* check if we have enough elements (including following
4288                          * free buffers) to handle eddp context */
4289                         if (qeth_eddp_check_buffers_for_context(queue,ctx) < 0){
4290                                 printk("eddp tx_dropped 1\n");
4291                                 card->stats.tx_dropped++;
4292                                 rc = -EBUSY;
4293                                 goto out;
4294                         }
4295                 }
4296         }
4297         if (ctx == NULL)
4298                 tmp = qeth_fill_buffer(queue, buffer, skb);
4299         else {
4300                 tmp = qeth_eddp_fill_buffer(queue,ctx,queue->next_buf_to_fill);
4301                 if (tmp < 0) {
4302                         printk("eddp tx_dropped 2\n");
4303                         card->stats.tx_dropped++;
4304                         rc = - EBUSY;
4305                         goto out;
4306                 }
4307         }
4308         queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
4309                                   QDIO_MAX_BUFFERS_PER_Q;
4310         flush_count += tmp;
4311 out:
4312         if (flush_count)
4313                 qeth_flush_buffers(queue, 0, start_index, flush_count);
4314         /*
4315          * queue->state will go from LOCKED -> UNLOCKED or from
4316          * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
4317          * (switch packing state or flush buffer to get another pci flag out).
4318          * In that case we will enter this loop
4319          */
4320         while (atomic_dec_return(&queue->state)){
4321                 flush_count = 0;
4322                 start_index = queue->next_buf_to_fill;
4323                 /* check if we can go back to non-packing state */
4324                 flush_count += qeth_switch_to_nonpacking_if_needed(queue);
4325                 /*
4326                  * check if we need to flush a packing buffer to get a pci
4327                  * flag out on the queue
4328                  */
4329                 if (!flush_count && !atomic_read(&queue->set_pci_flags_count))
4330                         flush_count += qeth_flush_buffers_on_no_pci(queue);
4331                 if (flush_count)
4332                         qeth_flush_buffers(queue, 0, start_index, flush_count);
4333         }
4334         /* at this point the queue is UNLOCKED again */
4335 #ifdef CONFIG_QETH_PERF_STATS
4336         if (do_pack)
4337                 queue->card->perf_stats.bufs_sent_pack += flush_count;
4338 #endif /* CONFIG_QETH_PERF_STATS */
4339
4340         return rc;
4341 }
4342
4343 static inline int
4344 qeth_get_elements_no(struct qeth_card *card, void *hdr, 
4345                      struct sk_buff *skb, int elems)
4346 {
4347         int elements_needed = 0;
4348
4349         if (skb_shinfo(skb)->nr_frags > 0) {
4350                 elements_needed = (skb_shinfo(skb)->nr_frags + 1);
4351         }
4352         if (elements_needed == 0 )
4353                 elements_needed = 1 + (((((unsigned long) hdr) % PAGE_SIZE)
4354                                         + skb->len) >> PAGE_SHIFT);
4355         if ((elements_needed + elems) > QETH_MAX_BUFFER_ELEMENTS(card)){
4356                 PRINT_ERR("qeth_do_send_packet: invalid size of "
4357                           "IP packet (Number=%d / Length=%d). Discarded.\n",
4358                           (elements_needed+elems), skb->len);
4359                 return 0;
4360         }
4361         return elements_needed;
4362 }
4363
4364 static inline int
4365 qeth_send_packet(struct qeth_card *card, struct sk_buff *skb)
4366 {
4367         int ipv = 0;
4368         int cast_type;
4369         struct qeth_qdio_out_q *queue;
4370         struct qeth_hdr *hdr = NULL;
4371         int elements_needed = 0;
4372         enum qeth_large_send_types large_send = QETH_LARGE_SEND_NO;
4373         struct qeth_eddp_context *ctx = NULL;
4374         int rc;
4375
4376         QETH_DBF_TEXT(trace, 6, "sendpkt");
4377
4378         if (!card->options.layer2) {
4379                 ipv = qeth_get_ip_version(skb);
4380                 if ((card->dev->hard_header == qeth_fake_header) && ipv) {
4381                         if ((skb = qeth_pskb_unshare(skb,GFP_ATOMIC)) == NULL) {
4382                                 card->stats.tx_dropped++;
4383                                 dev_kfree_skb_irq(skb);
4384                                 return 0;
4385                         }
4386                         if(card->dev->type == ARPHRD_IEEE802_TR){
4387                                 skb_pull(skb, QETH_FAKE_LL_LEN_TR);
4388                         } else {
4389                                 skb_pull(skb, QETH_FAKE_LL_LEN_ETH);
4390                         }
4391                 }
4392         }
4393         if ((card->info.type == QETH_CARD_TYPE_OSN) &&
4394                 (skb->protocol == htons(ETH_P_IPV6))) {
4395                 dev_kfree_skb_any(skb);
4396                 return 0;
4397         }
4398         cast_type = qeth_get_cast_type(card, skb);
4399         if ((cast_type == RTN_BROADCAST) && 
4400             (card->info.broadcast_capable == 0)){
4401                 card->stats.tx_dropped++;
4402                 card->stats.tx_errors++;
4403                 dev_kfree_skb_any(skb);
4404                 return NETDEV_TX_OK;
4405         }
4406         queue = card->qdio.out_qs
4407                 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
4408
4409         if (skb_shinfo(skb)->tso_size)
4410                 large_send = card->options.large_send;
4411
4412         /*are we able to do TSO ? If so ,prepare and send it from here */
4413         if ((large_send == QETH_LARGE_SEND_TSO) &&
4414             (cast_type == RTN_UNSPEC)) {
4415                 rc = qeth_tso_prepare_packet(card, skb, ipv, cast_type);
4416                 if (rc) {
4417                         card->stats.tx_dropped++;
4418                         card->stats.tx_errors++;
4419                         dev_kfree_skb_any(skb);
4420                         return NETDEV_TX_OK;
4421                 } 
4422                 elements_needed++;
4423         } else {
4424                 if ((rc = qeth_prepare_skb(card, &skb, &hdr, ipv))) {
4425                         QETH_DBF_TEXT_(trace, 4, "pskbe%d", rc);
4426                         return rc;
4427                 }
4428                 if (card->info.type != QETH_CARD_TYPE_OSN)
4429                         qeth_fill_header(card, hdr, skb, ipv, cast_type);
4430         }
4431
4432         if (large_send == QETH_LARGE_SEND_EDDP) {
4433                 ctx = qeth_eddp_create_context(card, skb, hdr);
4434                 if (ctx == NULL) {
4435                         PRINT_WARN("could not create eddp context\n");
4436                         return -EINVAL;
4437                 }
4438         } else {
4439                 int elems = qeth_get_elements_no(card,(void*) hdr, skb,
4440                                                  elements_needed);
4441                 if (!elems)
4442                         return -EINVAL;
4443                 elements_needed += elems;
4444         }
4445
4446         if (card->info.type != QETH_CARD_TYPE_IQD)
4447                 rc = qeth_do_send_packet(card, queue, skb, hdr,
4448                                          elements_needed, ctx);
4449         else
4450                 rc = qeth_do_send_packet_fast(card, queue, skb, hdr,
4451                                               elements_needed, ctx);
4452         if (!rc){
4453                 card->stats.tx_packets++;
4454                 card->stats.tx_bytes += skb->len;
4455 #ifdef CONFIG_QETH_PERF_STATS
4456                 if (skb_shinfo(skb)->tso_size &&
4457                    !(large_send == QETH_LARGE_SEND_NO)) {
4458                         card->perf_stats.large_send_bytes += skb->len;
4459                         card->perf_stats.large_send_cnt++;
4460                 }
4461                 if (skb_shinfo(skb)->nr_frags > 0){
4462                         card->perf_stats.sg_skbs_sent++;
4463                         /* nr_frags + skb->data */
4464                         card->perf_stats.sg_frags_sent +=
4465                                 skb_shinfo(skb)->nr_frags + 1;
4466                 }
4467 #endif /* CONFIG_QETH_PERF_STATS */
4468         }
4469         if (ctx != NULL) {
4470                 /* drop creator's reference */
4471                 qeth_eddp_put_context(ctx);
4472                 /* free skb; it's not referenced by a buffer */
4473                 if (rc == 0)
4474                         dev_kfree_skb_any(skb);
4475
4476         }
4477         return rc;
4478 }
4479
4480 static int
4481 qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4482 {
4483         struct qeth_card *card = (struct qeth_card *) dev->priv;
4484         int rc = 0;
4485
4486         switch(regnum){
4487         case MII_BMCR: /* Basic mode control register */
4488                 rc = BMCR_FULLDPLX;
4489                 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH)&&
4490                     (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4491                     (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
4492                         rc |= BMCR_SPEED100;
4493                 break;
4494         case MII_BMSR: /* Basic mode status register */
4495                 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4496                      BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4497                      BMSR_100BASE4;
4498                 break;
4499         case MII_PHYSID1: /* PHYS ID 1 */
4500                 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4501                      dev->dev_addr[2];
4502                 rc = (rc >> 5) & 0xFFFF;
4503                 break;
4504         case MII_PHYSID2: /* PHYS ID 2 */
4505                 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4506                 break;
4507         case MII_ADVERTISE: /* Advertisement control reg */
4508                 rc = ADVERTISE_ALL;
4509                 break;
4510         case MII_LPA: /* Link partner ability reg */
4511                 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4512                      LPA_100BASE4 | LPA_LPACK;
4513                 break;
4514         case MII_EXPANSION: /* Expansion register */
4515                 break;
4516         case MII_DCOUNTER: /* disconnect counter */
4517                 break;
4518         case MII_FCSCOUNTER: /* false carrier counter */
4519                 break;
4520         case MII_NWAYTEST: /* N-way auto-neg test register */
4521                 break;
4522         case MII_RERRCOUNTER: /* rx error counter */
4523                 rc = card->stats.rx_errors;
4524                 break;
4525         case MII_SREVISION: /* silicon revision */
4526                 break;
4527         case MII_RESV1: /* reserved 1 */
4528                 break;
4529         case MII_LBRERROR: /* loopback, rx, bypass error */
4530                 break;
4531         case MII_PHYADDR: /* physical address */
4532                 break;
4533         case MII_RESV2: /* reserved 2 */
4534                 break;
4535         case MII_TPISTATUS: /* TPI status for 10mbps */
4536                 break;
4537         case MII_NCONFIG: /* network interface config */
4538                 break;
4539         default:
4540                 rc = 0;
4541                 break;
4542         }
4543         return rc;
4544 }
4545
4546 static void
4547 qeth_mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
4548 {
4549         switch(regnum){
4550         case MII_BMCR: /* Basic mode control register */
4551         case MII_BMSR: /* Basic mode status register */
4552         case MII_PHYSID1: /* PHYS ID 1 */
4553         case MII_PHYSID2: /* PHYS ID 2 */
4554         case MII_ADVERTISE: /* Advertisement control reg */
4555         case MII_LPA: /* Link partner ability reg */
4556         case MII_EXPANSION: /* Expansion register */
4557         case MII_DCOUNTER: /* disconnect counter */
4558         case MII_FCSCOUNTER: /* false carrier counter */
4559         case MII_NWAYTEST: /* N-way auto-neg test register */
4560         case MII_RERRCOUNTER: /* rx error counter */
4561         case MII_SREVISION: /* silicon revision */
4562         case MII_RESV1: /* reserved 1 */
4563         case MII_LBRERROR: /* loopback, rx, bypass error */
4564         case MII_PHYADDR: /* physical address */
4565         case MII_RESV2: /* reserved 2 */
4566         case MII_TPISTATUS: /* TPI status for 10mbps */
4567         case MII_NCONFIG: /* network interface config */
4568         default:
4569                 break;
4570         }
4571 }
4572
4573 static inline const char *
4574 qeth_arp_get_error_cause(int *rc)
4575 {
4576         switch (*rc) {
4577         case QETH_IPA_ARP_RC_FAILED:
4578                 *rc = -EIO;
4579                 return "operation failed";
4580         case QETH_IPA_ARP_RC_NOTSUPP:
4581                 *rc = -EOPNOTSUPP;
4582                 return "operation not supported";
4583         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
4584                 *rc = -EINVAL;
4585                 return "argument out of range";
4586         case QETH_IPA_ARP_RC_Q_NOTSUPP:
4587                 *rc = -EOPNOTSUPP;
4588                 return "query operation not supported";
4589         case QETH_IPA_ARP_RC_Q_NO_DATA:
4590                 *rc = -ENOENT;
4591                 return "no query data available";
4592         default:
4593                 return "unknown error";
4594         }
4595 }
4596
4597 static int
4598 qeth_send_simple_setassparms(struct qeth_card *, enum qeth_ipa_funcs,
4599                              __u16, long);
4600
4601 static int
4602 qeth_arp_set_no_entries(struct qeth_card *card, int no_entries)
4603 {
4604         int tmp;
4605         int rc;
4606
4607         QETH_DBF_TEXT(trace,3,"arpstnoe");
4608
4609         /*
4610          * currently GuestLAN only supports the ARP assist function
4611          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
4612          * thus we say EOPNOTSUPP for this ARP function
4613          */
4614         if (card->info.guestlan)
4615                 return -EOPNOTSUPP;
4616         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
4617                 PRINT_WARN("ARP processing not supported "
4618                            "on %s!\n", QETH_CARD_IFNAME(card));
4619                 return -EOPNOTSUPP;
4620         }
4621         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
4622                                           IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
4623                                           no_entries);
4624         if (rc) {
4625                 tmp = rc;
4626                 PRINT_WARN("Could not set number of ARP entries on %s: "
4627                            "%s (0x%x/%d)\n",
4628                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
4629                            tmp, tmp);
4630         }
4631         return rc;
4632 }
4633
4634 static inline void
4635 qeth_copy_arp_entries_stripped(struct qeth_arp_query_info *qinfo,
4636                                struct qeth_arp_query_data *qdata,
4637                                int entry_size, int uentry_size)
4638 {
4639         char *entry_ptr;
4640         char *uentry_ptr;
4641         int i;
4642
4643         entry_ptr = (char *)&qdata->data;
4644         uentry_ptr = (char *)(qinfo->udata + qinfo->udata_offset);
4645         for (i = 0; i < qdata->no_entries; ++i){
4646                 /* strip off 32 bytes "media specific information" */
4647                 memcpy(uentry_ptr, (entry_ptr + 32), entry_size - 32);
4648                 entry_ptr += entry_size;
4649                 uentry_ptr += uentry_size;
4650         }
4651 }
4652
4653 static int
4654 qeth_arp_query_cb(struct qeth_card *card, struct qeth_reply *reply,
4655                   unsigned long data)
4656 {
4657         struct qeth_ipa_cmd *cmd;
4658         struct qeth_arp_query_data *qdata;
4659         struct qeth_arp_query_info *qinfo;
4660         int entry_size;
4661         int uentry_size;
4662         int i;
4663
4664         QETH_DBF_TEXT(trace,4,"arpquecb");
4665
4666         qinfo = (struct qeth_arp_query_info *) reply->param;
4667         cmd = (struct qeth_ipa_cmd *) data;
4668         if (cmd->hdr.return_code) {
4669                 QETH_DBF_TEXT_(trace,4,"qaer1%i", cmd->hdr.return_code);
4670                 return 0;
4671         }
4672         if (cmd->data.setassparms.hdr.return_code) {
4673                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
4674                 QETH_DBF_TEXT_(trace,4,"qaer2%i", cmd->hdr.return_code);
4675                 return 0;
4676         }
4677         qdata = &cmd->data.setassparms.data.query_arp;
4678         switch(qdata->reply_bits){
4679         case 5:
4680                 uentry_size = entry_size = sizeof(struct qeth_arp_qi_entry5);
4681                 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4682                         uentry_size = sizeof(struct qeth_arp_qi_entry5_short);
4683                 break;
4684         case 7:
4685                 /* fall through to default */
4686         default:
4687                 /* tr is the same as eth -> entry7 */
4688                 uentry_size = entry_size = sizeof(struct qeth_arp_qi_entry7);
4689                 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4690                         uentry_size = sizeof(struct qeth_arp_qi_entry7_short);
4691                 break;
4692         }
4693         /* check if there is enough room in userspace */
4694         if ((qinfo->udata_len - qinfo->udata_offset) <
4695                         qdata->no_entries * uentry_size){
4696                 QETH_DBF_TEXT_(trace, 4, "qaer3%i", -ENOMEM);
4697                 cmd->hdr.return_code = -ENOMEM;
4698                 PRINT_WARN("query ARP user space buffer is too small for "
4699                            "the returned number of ARP entries. "
4700                            "Aborting query!\n");
4701                 goto out_error;
4702         }
4703         QETH_DBF_TEXT_(trace, 4, "anore%i",
4704                        cmd->data.setassparms.hdr.number_of_replies);
4705         QETH_DBF_TEXT_(trace, 4, "aseqn%i", cmd->data.setassparms.hdr.seq_no);
4706         QETH_DBF_TEXT_(trace, 4, "anoen%i", qdata->no_entries);
4707
4708         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) {
4709                 /* strip off "media specific information" */
4710                 qeth_copy_arp_entries_stripped(qinfo, qdata, entry_size,
4711                                                uentry_size);
4712         } else
4713                 /*copy entries to user buffer*/
4714                 memcpy(qinfo->udata + qinfo->udata_offset,
4715                        (char *)&qdata->data, qdata->no_entries*uentry_size);
4716
4717         qinfo->no_entries += qdata->no_entries;
4718         qinfo->udata_offset += (qdata->no_entries*uentry_size);
4719         /* check if all replies received ... */
4720         if (cmd->data.setassparms.hdr.seq_no <
4721             cmd->data.setassparms.hdr.number_of_replies)
4722                 return 1;
4723         memcpy(qinfo->udata, &qinfo->no_entries, 4);
4724         /* keep STRIP_ENTRIES flag so the user program can distinguish
4725          * stripped entries from normal ones */
4726         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4727                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
4728         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET,&qdata->reply_bits,2);
4729         return 0;
4730 out_error:
4731         i = 0;
4732         memcpy(qinfo->udata, &i, 4);
4733         return 0;
4734 }
4735
4736 static int
4737 qeth_send_ipa_arp_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
4738                       int len, int (*reply_cb)(struct qeth_card *,
4739                                                struct qeth_reply *,
4740                                                unsigned long),
4741                       void *reply_param)
4742 {
4743         QETH_DBF_TEXT(trace,4,"sendarp");
4744
4745         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4746         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4747                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4748         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4749                                       reply_cb, reply_param);
4750 }
4751
4752 static int
4753 qeth_send_ipa_snmp_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
4754                       int len, int (*reply_cb)(struct qeth_card *,
4755                                                struct qeth_reply *,
4756                                                unsigned long),
4757                       void *reply_param)
4758 {
4759         u16 s1, s2;
4760
4761         QETH_DBF_TEXT(trace,4,"sendsnmp");
4762
4763         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4764         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4765                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4766         /* adjust PDU length fields in IPA_PDU_HEADER */
4767         s1 = (u32) IPA_PDU_HEADER_SIZE + len;
4768         s2 = (u32) len;
4769         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
4770         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
4771         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
4772         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
4773         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4774                                       reply_cb, reply_param);
4775 }
4776
4777 static struct qeth_cmd_buffer *
4778 qeth_get_setassparms_cmd(struct qeth_card *, enum qeth_ipa_funcs,
4779                          __u16, __u16, enum qeth_prot_versions);
4780 static int
4781 qeth_arp_query(struct qeth_card *card, char *udata)
4782 {
4783         struct qeth_cmd_buffer *iob;
4784         struct qeth_arp_query_info qinfo = {0, };
4785         int tmp;
4786         int rc;
4787
4788         QETH_DBF_TEXT(trace,3,"arpquery");
4789
4790         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
4791                                IPA_ARP_PROCESSING)) {
4792                 PRINT_WARN("ARP processing not supported "
4793                            "on %s!\n", QETH_CARD_IFNAME(card));
4794                 return -EOPNOTSUPP;
4795         }
4796         /* get size of userspace buffer and mask_bits -> 6 bytes */
4797         if (copy_from_user(&qinfo, udata, 6))
4798                 return -EFAULT;
4799         if (!(qinfo.udata = kmalloc(qinfo.udata_len, GFP_KERNEL)))
4800                 return -ENOMEM;
4801         memset(qinfo.udata, 0, qinfo.udata_len);
4802         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
4803         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
4804                                        IPA_CMD_ASS_ARP_QUERY_INFO,
4805                                        sizeof(int),QETH_PROT_IPV4);
4806
4807         rc = qeth_send_ipa_arp_cmd(card, iob,
4808                                    QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
4809                                    qeth_arp_query_cb, (void *)&qinfo);
4810         if (rc) {
4811                 tmp = rc;
4812                 PRINT_WARN("Error while querying ARP cache on %s: %s "
4813                            "(0x%x/%d)\n",
4814                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
4815                            tmp, tmp);
4816                 copy_to_user(udata, qinfo.udata, 4);
4817         } else {
4818                 copy_to_user(udata, qinfo.udata, qinfo.udata_len);
4819         }
4820         kfree(qinfo.udata);
4821         return rc;
4822 }
4823
4824 /**
4825  * SNMP command callback
4826  */
4827 static int
4828 qeth_snmp_command_cb(struct qeth_card *card, struct qeth_reply *reply,
4829                      unsigned long sdata)
4830 {
4831         struct qeth_ipa_cmd *cmd;
4832         struct qeth_arp_query_info *qinfo;
4833         struct qeth_snmp_cmd *snmp;
4834         unsigned char *data;
4835         __u16 data_len;
4836
4837         QETH_DBF_TEXT(trace,3,"snpcmdcb");
4838
4839         cmd = (struct qeth_ipa_cmd *) sdata;
4840         data = (unsigned char *)((char *)cmd - reply->offset);
4841         qinfo = (struct qeth_arp_query_info *) reply->param;
4842         snmp = &cmd->data.setadapterparms.data.snmp;
4843
4844         if (cmd->hdr.return_code) {
4845                 QETH_DBF_TEXT_(trace,4,"scer1%i", cmd->hdr.return_code);
4846                 return 0;
4847         }
4848         if (cmd->data.setadapterparms.hdr.return_code) {
4849                 cmd->hdr.return_code = cmd->data.setadapterparms.hdr.return_code;
4850                 QETH_DBF_TEXT_(trace,4,"scer2%i", cmd->hdr.return_code);
4851                 return 0;
4852         }
4853         data_len = *((__u16*)QETH_IPA_PDU_LEN_PDU1(data));
4854         if (cmd->data.setadapterparms.hdr.seq_no == 1)
4855                 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
4856         else
4857                 data_len -= (__u16)((char*)&snmp->request - (char *)cmd);
4858
4859         /* check if there is enough room in userspace */
4860         if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4861                 QETH_DBF_TEXT_(trace, 4, "scer3%i", -ENOMEM);
4862                 cmd->hdr.return_code = -ENOMEM;
4863                 return 0;
4864         }
4865         QETH_DBF_TEXT_(trace, 4, "snore%i",
4866                        cmd->data.setadapterparms.hdr.used_total);
4867         QETH_DBF_TEXT_(trace, 4, "sseqn%i", cmd->data.setadapterparms.hdr.seq_no);
4868         /*copy entries to user buffer*/
4869         if (cmd->data.setadapterparms.hdr.seq_no == 1) {
4870                 memcpy(qinfo->udata + qinfo->udata_offset,
4871                        (char *)snmp,
4872                        data_len + offsetof(struct qeth_snmp_cmd,data));
4873                 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
4874         } else {
4875                 memcpy(qinfo->udata + qinfo->udata_offset,
4876                        (char *)&snmp->request, data_len);
4877         }
4878         qinfo->udata_offset += data_len;
4879         /* check if all replies received ... */
4880                 QETH_DBF_TEXT_(trace, 4, "srtot%i",
4881                                cmd->data.setadapterparms.hdr.used_total);
4882                 QETH_DBF_TEXT_(trace, 4, "srseq%i",
4883                                cmd->data.setadapterparms.hdr.seq_no);
4884         if (cmd->data.setadapterparms.hdr.seq_no <
4885             cmd->data.setadapterparms.hdr.used_total)
4886                 return 1;
4887         return 0;
4888 }
4889
4890 static struct qeth_cmd_buffer *
4891 qeth_get_ipacmd_buffer(struct qeth_card *, enum qeth_ipa_cmds,
4892                        enum qeth_prot_versions );
4893
4894 static struct qeth_cmd_buffer *
4895 qeth_get_adapter_cmd(struct qeth_card *card, __u32 command, __u32 cmdlen)
4896 {
4897         struct qeth_cmd_buffer *iob;
4898         struct qeth_ipa_cmd *cmd;
4899
4900         iob = qeth_get_ipacmd_buffer(card,IPA_CMD_SETADAPTERPARMS,
4901                                      QETH_PROT_IPV4);
4902         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4903         cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
4904         cmd->data.setadapterparms.hdr.command_code = command;
4905         cmd->data.setadapterparms.hdr.used_total = 1;
4906         cmd->data.setadapterparms.hdr.seq_no = 1;
4907
4908         return iob;
4909 }
4910
4911 /**
4912  * function to send SNMP commands to OSA-E card
4913  */
4914 static int
4915 qeth_snmp_command(struct qeth_card *card, char *udata)
4916 {
4917         struct qeth_cmd_buffer *iob;
4918         struct qeth_ipa_cmd *cmd;
4919         struct qeth_snmp_ureq *ureq;
4920         int req_len;
4921         struct qeth_arp_query_info qinfo = {0, };
4922         int rc = 0;
4923
4924         QETH_DBF_TEXT(trace,3,"snmpcmd");
4925
4926         if (card->info.guestlan)
4927                 return -EOPNOTSUPP;
4928
4929         if ((!qeth_adp_supported(card,IPA_SETADP_SET_SNMP_CONTROL)) &&
4930             (!card->options.layer2) ) {
4931                 PRINT_WARN("SNMP Query MIBS not supported "
4932                            "on %s!\n", QETH_CARD_IFNAME(card));
4933                 return -EOPNOTSUPP;
4934         }
4935         /* skip 4 bytes (data_len struct member) to get req_len */
4936         if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
4937                 return -EFAULT;
4938         ureq = kmalloc(req_len+sizeof(struct qeth_snmp_ureq_hdr), GFP_KERNEL);
4939         if (!ureq) {
4940                 QETH_DBF_TEXT(trace, 2, "snmpnome");
4941                 return -ENOMEM;
4942         }
4943         if (copy_from_user(ureq, udata,
4944                         req_len+sizeof(struct qeth_snmp_ureq_hdr))){
4945                 kfree(ureq);
4946                 return -EFAULT;
4947         }
4948         qinfo.udata_len = ureq->hdr.data_len;
4949         if (!(qinfo.udata = kmalloc(qinfo.udata_len, GFP_KERNEL))){
4950                 kfree(ureq);
4951                 return -ENOMEM;
4952         }
4953         memset(qinfo.udata, 0, qinfo.udata_len);
4954         qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4955
4956         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
4957                                    QETH_SNMP_SETADP_CMDLENGTH + req_len);
4958         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4959         memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
4960         rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
4961                                     qeth_snmp_command_cb, (void *)&qinfo);
4962         if (rc)
4963                 PRINT_WARN("SNMP command failed on %s: (0x%x)\n",
4964                            QETH_CARD_IFNAME(card), rc);
4965          else
4966                 copy_to_user(udata, qinfo.udata, qinfo.udata_len);
4967
4968         kfree(ureq);
4969         kfree(qinfo.udata);
4970         return rc;
4971 }
4972
4973 static int
4974 qeth_default_setassparms_cb(struct qeth_card *, struct qeth_reply *,
4975                             unsigned long);
4976
4977 static int
4978 qeth_send_setassparms(struct qeth_card *, struct qeth_cmd_buffer *,
4979                       __u16, long,
4980                       int (*reply_cb)
4981                       (struct qeth_card *, struct qeth_reply *, unsigned long),
4982                       void *reply_param);
4983
4984 static int
4985 qeth_arp_add_entry(struct qeth_card *card, struct qeth_arp_cache_entry *entry)
4986 {
4987         struct qeth_cmd_buffer *iob;
4988         char buf[16];
4989         int tmp;
4990         int rc;
4991
4992         QETH_DBF_TEXT(trace,3,"arpadent");
4993
4994         /*
4995          * currently GuestLAN only supports the ARP assist function
4996          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
4997          * thus we say EOPNOTSUPP for this ARP function
4998          */
4999         if (card->info.guestlan)
5000                 return -EOPNOTSUPP;
5001         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5002                 PRINT_WARN("ARP processing not supported "
5003                            "on %s!\n", QETH_CARD_IFNAME(card));
5004                 return -EOPNOTSUPP;
5005         }
5006
5007         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
5008                                        IPA_CMD_ASS_ARP_ADD_ENTRY,
5009                                        sizeof(struct qeth_arp_cache_entry),
5010                                        QETH_PROT_IPV4);
5011         rc = qeth_send_setassparms(card, iob,
5012                                    sizeof(struct qeth_arp_cache_entry),
5013                                    (unsigned long) entry,
5014                                    qeth_default_setassparms_cb, NULL);
5015         if (rc) {
5016                 tmp = rc;
5017                 qeth_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
5018                 PRINT_WARN("Could not add ARP entry for address %s on %s: "
5019                            "%s (0x%x/%d)\n",
5020                            buf, QETH_CARD_IFNAME(card),
5021                            qeth_arp_get_error_cause(&rc), tmp, tmp);
5022         }
5023         return rc;
5024 }
5025
5026 static int
5027 qeth_arp_remove_entry(struct qeth_card *card, struct qeth_arp_cache_entry *entry)
5028 {
5029         struct qeth_cmd_buffer *iob;
5030         char buf[16] = {0, };
5031         int tmp;
5032         int rc;
5033
5034         QETH_DBF_TEXT(trace,3,"arprment");
5035
5036         /*
5037          * currently GuestLAN only supports the ARP assist function
5038          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
5039          * thus we say EOPNOTSUPP for this ARP function
5040          */
5041         if (card->info.guestlan)
5042                 return -EOPNOTSUPP;
5043         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5044                 PRINT_WARN("ARP processing not supported "
5045                            "on %s!\n", QETH_CARD_IFNAME(card));
5046                 return -EOPNOTSUPP;
5047         }
5048         memcpy(buf, entry, 12);
5049         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
5050                                        IPA_CMD_ASS_ARP_REMOVE_ENTRY,
5051                                        12,
5052                                        QETH_PROT_IPV4);
5053         rc = qeth_send_setassparms(card, iob,
5054                                    12, (unsigned long)buf,
5055                                    qeth_default_setassparms_cb, NULL);
5056         if (rc) {
5057                 tmp = rc;
5058                 memset(buf, 0, 16);
5059                 qeth_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
5060                 PRINT_WARN("Could not delete ARP entry for address %s on %s: "
5061                            "%s (0x%x/%d)\n",
5062                            buf, QETH_CARD_IFNAME(card),
5063                            qeth_arp_get_error_cause(&rc), tmp, tmp);
5064         }
5065         return rc;
5066 }
5067
5068 static int
5069 qeth_arp_flush_cache(struct qeth_card *card)
5070 {
5071         int rc;
5072         int tmp;
5073
5074         QETH_DBF_TEXT(trace,3,"arpflush");
5075
5076         /*
5077          * currently GuestLAN only supports the ARP assist function
5078          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
5079          * thus we say EOPNOTSUPP for this ARP function
5080         */
5081         if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
5082                 return -EOPNOTSUPP;
5083         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5084                 PRINT_WARN("ARP processing not supported "
5085                            "on %s!\n", QETH_CARD_IFNAME(card));
5086                 return -EOPNOTSUPP;
5087         }
5088         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
5089                                           IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
5090         if (rc){
5091                 tmp = rc;
5092                 PRINT_WARN("Could not flush ARP cache on %s: %s (0x%x/%d)\n",
5093                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
5094                            tmp, tmp);
5095         }
5096         return rc;
5097 }
5098
5099 static int
5100 qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5101 {
5102         struct qeth_card *card = (struct qeth_card *)dev->priv;
5103         struct qeth_arp_cache_entry arp_entry;
5104         struct mii_ioctl_data *mii_data;
5105         int rc = 0;
5106
5107         if (!card)
5108                 return -ENODEV;
5109
5110         if ((card->state != CARD_STATE_UP) &&
5111             (card->state != CARD_STATE_SOFTSETUP))
5112                 return -ENODEV;
5113
5114         if (card->info.type == QETH_CARD_TYPE_OSN)
5115                 return -EPERM;
5116
5117         switch (cmd){
5118         case SIOC_QETH_ARP_SET_NO_ENTRIES:
5119                 if ( !capable(CAP_NET_ADMIN) ||
5120                      (card->options.layer2) ) {
5121                         rc = -EPERM;
5122                         break;
5123                 }
5124                 rc = qeth_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
5125                 break;
5126         case SIOC_QETH_ARP_QUERY_INFO:
5127                 if ( !capable(CAP_NET_ADMIN) ||
5128                      (card->options.layer2) ) {
5129                         rc = -EPERM;
5130                         break;
5131                 }
5132                 rc = qeth_arp_query(card, rq->ifr_ifru.ifru_data);
5133                 break;
5134         case SIOC_QETH_ARP_ADD_ENTRY:
5135                 if ( !capable(CAP_NET_ADMIN) ||
5136                      (card->options.layer2) ) {
5137                         rc = -EPERM;
5138                         break;
5139                 }
5140                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
5141                                    sizeof(struct qeth_arp_cache_entry)))
5142                         rc = -EFAULT;
5143                 else
5144                         rc = qeth_arp_add_entry(card, &arp_entry);
5145                 break;
5146         case SIOC_QETH_ARP_REMOVE_ENTRY:
5147                 if ( !capable(CAP_NET_ADMIN) ||
5148                      (card->options.layer2) ) {
5149                         rc = -EPERM;
5150                         break;
5151                 }
5152                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
5153                                    sizeof(struct qeth_arp_cache_entry)))
5154                         rc = -EFAULT;
5155                 else
5156                         rc = qeth_arp_remove_entry(card, &arp_entry);
5157                 break;
5158         case SIOC_QETH_ARP_FLUSH_CACHE:
5159                 if ( !capable(CAP_NET_ADMIN) ||
5160                      (card->options.layer2) ) {
5161                         rc = -EPERM;
5162                         break;
5163                 }
5164                 rc = qeth_arp_flush_cache(card);
5165                 break;
5166         case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5167                 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5168                 break;
5169         case SIOC_QETH_GET_CARD_TYPE:
5170                 if ((card->info.type == QETH_CARD_TYPE_OSAE) &&
5171                     !card->info.guestlan)
5172                         return 1;
5173                 return 0;
5174                 break;
5175         case SIOCGMIIPHY:
5176                 mii_data = if_mii(rq);
5177                 mii_data->phy_id = 0;
5178                 break;
5179         case SIOCGMIIREG:
5180                 mii_data = if_mii(rq);
5181                 if (mii_data->phy_id != 0)
5182                         rc = -EINVAL;
5183                 else
5184                         mii_data->val_out = qeth_mdio_read(dev,mii_data->phy_id,
5185                                                            mii_data->reg_num);
5186                 break;
5187         case SIOCSMIIREG:
5188                 rc = -EOPNOTSUPP;
5189                 break;
5190                 /* TODO: remove return if qeth_mdio_write does something */
5191                 if (!capable(CAP_NET_ADMIN)){
5192                         rc = -EPERM;
5193                         break;
5194                 }
5195                 mii_data = if_mii(rq);
5196                 if (mii_data->phy_id != 0)
5197                         rc = -EINVAL;
5198                 else
5199                         qeth_mdio_write(dev, mii_data->phy_id, mii_data->reg_num,
5200                                         mii_data->val_in);
5201                 break;
5202         default:
5203                 rc = -EOPNOTSUPP;
5204         }
5205         if (rc)
5206                 QETH_DBF_TEXT_(trace, 2, "ioce%d", rc);
5207         return rc;
5208 }
5209
5210 static struct net_device_stats *
5211 qeth_get_stats(struct net_device *dev)
5212 {
5213         struct qeth_card *card;
5214
5215         card = (struct qeth_card *) (dev->priv);
5216
5217         QETH_DBF_TEXT(trace,5,"getstat");
5218
5219         return &card->stats;
5220 }
5221
5222 static int
5223 qeth_change_mtu(struct net_device *dev, int new_mtu)
5224 {
5225         struct qeth_card *card;
5226         char dbf_text[15];
5227
5228         card = (struct qeth_card *) (dev->priv);
5229
5230         QETH_DBF_TEXT(trace,4,"chgmtu");
5231         sprintf(dbf_text, "%8x", new_mtu);
5232         QETH_DBF_TEXT(trace,4,dbf_text);
5233
5234         if (new_mtu < 64)
5235                 return -EINVAL;
5236         if (new_mtu > 65535)
5237                 return -EINVAL;
5238         if ((!qeth_is_supported(card,IPA_IP_FRAGMENTATION)) &&
5239             (!qeth_mtu_is_valid(card, new_mtu)))
5240                 return -EINVAL;
5241         dev->mtu = new_mtu;
5242         return 0;
5243 }
5244
5245 #ifdef CONFIG_QETH_VLAN
5246 static void
5247 qeth_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
5248 {
5249         struct qeth_card *card;
5250         unsigned long flags;
5251
5252         QETH_DBF_TEXT(trace,4,"vlanreg");
5253
5254         card = (struct qeth_card *) dev->priv;
5255         spin_lock_irqsave(&card->vlanlock, flags);
5256         card->vlangrp = grp;
5257         spin_unlock_irqrestore(&card->vlanlock, flags);
5258 }
5259
5260 static inline void
5261 qeth_free_vlan_buffer(struct qeth_card *card, struct qeth_qdio_out_buffer *buf,
5262                       unsigned short vid)
5263 {
5264         int i;
5265         struct sk_buff *skb;
5266         struct sk_buff_head tmp_list;
5267
5268         skb_queue_head_init(&tmp_list);
5269         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i){
5270                 while ((skb = skb_dequeue(&buf->skb_list))){
5271                         if (vlan_tx_tag_present(skb) &&
5272                             (vlan_tx_tag_get(skb) == vid)) {
5273                                 atomic_dec(&skb->users);
5274                                 dev_kfree_skb(skb);
5275                         } else
5276                                 skb_queue_tail(&tmp_list, skb);
5277                 }
5278         }
5279         while ((skb = skb_dequeue(&tmp_list)))
5280                 skb_queue_tail(&buf->skb_list, skb);
5281 }
5282
5283 static void
5284 qeth_free_vlan_skbs(struct qeth_card *card, unsigned short vid)
5285 {
5286         int i, j;
5287
5288         QETH_DBF_TEXT(trace, 4, "frvlskbs");
5289         for (i = 0; i < card->qdio.no_out_queues; ++i){
5290                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
5291                         qeth_free_vlan_buffer(card, &card->qdio.
5292                                               out_qs[i]->bufs[j], vid);
5293         }
5294 }
5295
5296 static void
5297 qeth_free_vlan_addresses4(struct qeth_card *card, unsigned short vid)
5298 {
5299         struct in_device *in_dev;
5300         struct in_ifaddr *ifa;
5301         struct qeth_ipaddr *addr;
5302
5303         QETH_DBF_TEXT(trace, 4, "frvaddr4");
5304         if (!card->vlangrp)
5305                 return;
5306         rcu_read_lock();
5307         in_dev = __in_dev_get_rcu(card->vlangrp->vlan_devices[vid]);
5308         if (!in_dev)
5309                 goto out;
5310         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
5311                 addr = qeth_get_addr_buffer(QETH_PROT_IPV4);
5312                 if (addr){
5313                         addr->u.a4.addr = ifa->ifa_address;
5314                         addr->u.a4.mask = ifa->ifa_mask;
5315                         addr->type = QETH_IP_TYPE_NORMAL;
5316                         if (!qeth_delete_ip(card, addr))
5317                                 kfree(addr);
5318                 }
5319         }
5320 out:
5321         rcu_read_unlock();
5322 }
5323
5324 static void
5325 qeth_free_vlan_addresses6(struct qeth_card *card, unsigned short vid)
5326 {
5327 #ifdef CONFIG_QETH_IPV6
5328         struct inet6_dev *in6_dev;
5329         struct inet6_ifaddr *ifa;
5330         struct qeth_ipaddr *addr;
5331
5332         QETH_DBF_TEXT(trace, 4, "frvaddr6");
5333         if (!card->vlangrp)
5334                 return;
5335         in6_dev = in6_dev_get(card->vlangrp->vlan_devices[vid]);
5336         if (!in6_dev)
5337                 return;
5338         for (ifa = in6_dev->addr_list; ifa; ifa = ifa->lst_next){
5339                 addr = qeth_get_addr_buffer(QETH_PROT_IPV6);
5340                 if (addr){
5341                         memcpy(&addr->u.a6.addr, &ifa->addr,
5342                                sizeof(struct in6_addr));
5343                         addr->u.a6.pfxlen = ifa->prefix_len;
5344                         addr->type = QETH_IP_TYPE_NORMAL;
5345                         if (!qeth_delete_ip(card, addr))
5346                                 kfree(addr);
5347                 }
5348         }
5349         in6_dev_put(in6_dev);
5350 #endif /* CONFIG_QETH_IPV6 */
5351 }
5352
5353 static void
5354 qeth_layer2_send_setdelvlan(struct qeth_card *card, __u16 i,
5355                             enum qeth_ipa_cmds ipacmd)
5356 {
5357         int rc;
5358         struct qeth_ipa_cmd *cmd;
5359         struct qeth_cmd_buffer *iob;
5360
5361         QETH_DBF_TEXT_(trace, 4, "L2sdv%x",ipacmd);
5362         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
5363         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5364         cmd->data.setdelvlan.vlan_id = i;
5365
5366         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
5367         if (rc) {
5368                 PRINT_ERR("Error in processing VLAN %i on %s: 0x%x. "
5369                           "Continuing\n",i, QETH_CARD_IFNAME(card), rc);
5370                 QETH_DBF_TEXT_(trace, 2, "L2VL%4x", ipacmd);
5371                 QETH_DBF_TEXT_(trace, 2, "L2%s", CARD_BUS_ID(card));
5372                 QETH_DBF_TEXT_(trace, 2, "err%d", rc);
5373         }
5374 }
5375
5376 static void
5377 qeth_layer2_process_vlans(struct qeth_card *card, int clear)
5378 {
5379         unsigned short  i;
5380
5381         QETH_DBF_TEXT(trace, 3, "L2prcvln");
5382
5383         if (!card->vlangrp)
5384                 return;
5385         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5386                 if (card->vlangrp->vlan_devices[i] == NULL)
5387                         continue;
5388                 if (clear)
5389                         qeth_layer2_send_setdelvlan(card, i, IPA_CMD_DELVLAN);
5390                 else
5391                         qeth_layer2_send_setdelvlan(card, i, IPA_CMD_SETVLAN);
5392         }
5393 }
5394
5395 /*add_vid is layer 2 used only ....*/
5396 static void
5397 qeth_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
5398 {
5399         struct qeth_card *card;
5400
5401         QETH_DBF_TEXT_(trace, 4, "aid:%d", vid);
5402
5403         card = (struct qeth_card *) dev->priv;
5404         if (!card->options.layer2)
5405                 return;
5406         qeth_layer2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
5407 }
5408
5409 /*... kill_vid used for both modes*/
5410 static void
5411 qeth_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
5412 {
5413         struct qeth_card *card;
5414         unsigned long flags;
5415
5416         QETH_DBF_TEXT_(trace, 4, "kid:%d", vid);
5417
5418         card = (struct qeth_card *) dev->priv;
5419         /* free all skbs for the vlan device */
5420         qeth_free_vlan_skbs(card, vid);
5421         spin_lock_irqsave(&card->vlanlock, flags);
5422         /* unregister IP addresses of vlan device */
5423         qeth_free_vlan_addresses4(card, vid);
5424         qeth_free_vlan_addresses6(card, vid);
5425         if (card->vlangrp)
5426                 card->vlangrp->vlan_devices[vid] = NULL;
5427         spin_unlock_irqrestore(&card->vlanlock, flags);
5428         if (card->options.layer2)
5429                 qeth_layer2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
5430         qeth_set_multicast_list(card->dev);
5431 }
5432 #endif
5433
5434 /**
5435  * set multicast address on card
5436  */
5437 static void
5438 qeth_set_multicast_list(struct net_device *dev)
5439 {
5440         struct qeth_card *card = (struct qeth_card *) dev->priv;
5441
5442         if (card->info.type == QETH_CARD_TYPE_OSN)
5443                 return ;
5444          
5445         QETH_DBF_TEXT(trace,3,"setmulti");
5446         qeth_delete_mc_addresses(card);
5447         qeth_add_multicast_ipv4(card);
5448 #ifdef CONFIG_QETH_IPV6
5449         qeth_add_multicast_ipv6(card);
5450 #endif
5451         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
5452                 schedule_work(&card->kernel_thread_starter);
5453 }
5454
5455 static int
5456 qeth_neigh_setup(struct net_device *dev, struct neigh_parms *np)
5457 {
5458         return 0;
5459 }
5460
5461 static void
5462 qeth_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
5463 {
5464         if (dev->type == ARPHRD_IEEE802_TR)
5465                 ip_tr_mc_map(ipm, mac);
5466         else
5467                 ip_eth_mc_map(ipm, mac);
5468 }
5469
5470 static struct qeth_ipaddr *
5471 qeth_get_addr_buffer(enum qeth_prot_versions prot)
5472 {
5473         struct qeth_ipaddr *addr;
5474
5475         addr = kmalloc(sizeof(struct qeth_ipaddr), GFP_ATOMIC);
5476         if (addr == NULL) {
5477                 PRINT_WARN("Not enough memory to add address\n");
5478                 return NULL;
5479         }
5480         memset(addr,0,sizeof(struct qeth_ipaddr));
5481         addr->type = QETH_IP_TYPE_NORMAL;
5482         addr->proto = prot;
5483         return addr;
5484 }
5485
5486 int
5487 qeth_osn_assist(struct net_device *dev,
5488                 void *data,
5489                 int data_len)
5490 {
5491         struct qeth_cmd_buffer *iob;
5492         struct qeth_card *card;
5493         int rc;
5494         
5495         QETH_DBF_TEXT(trace, 2, "osnsdmc");
5496         if (!dev)
5497                 return -ENODEV;
5498         card = (struct qeth_card *)dev->priv;
5499         if (!card)
5500                 return -ENODEV;
5501         if ((card->state != CARD_STATE_UP) &&
5502             (card->state != CARD_STATE_SOFTSETUP))
5503                 return -ENODEV;
5504         iob = qeth_wait_for_buffer(&card->write);
5505         memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
5506         rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
5507         return rc;
5508 }
5509
5510 static struct net_device *
5511 qeth_netdev_by_devno(unsigned char *read_dev_no)
5512 {
5513         struct qeth_card *card;
5514         struct net_device *ndev;
5515         unsigned char *readno;
5516         __u16 temp_dev_no, card_dev_no;
5517         char *endp;
5518         unsigned long flags;
5519
5520         ndev = NULL;
5521         memcpy(&temp_dev_no, read_dev_no, 2);
5522         read_lock_irqsave(&qeth_card_list.rwlock, flags);
5523         list_for_each_entry(card, &qeth_card_list.list, list) {
5524                 readno = CARD_RDEV_ID(card);
5525                 readno += (strlen(readno) - 4);
5526                 card_dev_no = simple_strtoul(readno, &endp, 16);
5527                 if (card_dev_no == temp_dev_no) {
5528                         ndev = card->dev;
5529                         break;
5530                 }
5531         }
5532         read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
5533         return ndev;
5534 }
5535
5536 int
5537 qeth_osn_register(unsigned char *read_dev_no,
5538                   struct net_device **dev,
5539                   int (*assist_cb)(struct net_device *, void *),
5540                   int (*data_cb)(struct sk_buff *))
5541 {
5542         struct qeth_card * card;
5543
5544         QETH_DBF_TEXT(trace, 2, "osnreg");
5545         *dev = qeth_netdev_by_devno(read_dev_no);
5546         if (*dev == NULL)
5547                 return -ENODEV;
5548         card = (struct qeth_card *)(*dev)->priv;
5549         if (!card)
5550                 return -ENODEV;
5551         if ((assist_cb == NULL) || (data_cb == NULL))
5552                 return -EINVAL;
5553         card->osn_info.assist_cb = assist_cb;
5554         card->osn_info.data_cb = data_cb;
5555         return 0;
5556 }
5557
5558 void
5559 qeth_osn_deregister(struct net_device * dev)
5560 {
5561         struct qeth_card *card;
5562
5563         QETH_DBF_TEXT(trace, 2, "osndereg");
5564         if (!dev)
5565                 return;
5566         card = (struct qeth_card *)dev->priv;
5567         if (!card)
5568                 return;
5569         card->osn_info.assist_cb = NULL;
5570         card->osn_info.data_cb = NULL;
5571         return;
5572 }
5573                                            
5574 static void
5575 qeth_delete_mc_addresses(struct qeth_card *card)
5576 {
5577         struct qeth_ipaddr *iptodo;
5578         unsigned long flags;
5579
5580         QETH_DBF_TEXT(trace,4,"delmc");
5581         iptodo = qeth_get_addr_buffer(QETH_PROT_IPV4);
5582         if (!iptodo) {
5583                 QETH_DBF_TEXT(trace, 2, "dmcnomem");
5584                 return;
5585         }
5586         iptodo->type = QETH_IP_TYPE_DEL_ALL_MC;
5587         spin_lock_irqsave(&card->ip_lock, flags);
5588         if (!__qeth_insert_ip_todo(card, iptodo, 0))
5589                 kfree(iptodo);
5590         spin_unlock_irqrestore(&card->ip_lock, flags);
5591 }
5592
5593 static inline void
5594 qeth_add_mc(struct qeth_card *card, struct in_device *in4_dev)
5595 {
5596         struct qeth_ipaddr *ipm;
5597         struct ip_mc_list *im4;
5598         char buf[MAX_ADDR_LEN];
5599
5600         QETH_DBF_TEXT(trace,4,"addmc");
5601         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
5602                 qeth_get_mac_for_ipm(im4->multiaddr, buf, in4_dev->dev);
5603                 ipm = qeth_get_addr_buffer(QETH_PROT_IPV4);
5604                 if (!ipm)
5605                         continue;
5606                 ipm->u.a4.addr = im4->multiaddr;
5607                 memcpy(ipm->mac,buf,OSA_ADDR_LEN);
5608                 ipm->is_multicast = 1;
5609                 if (!qeth_add_ip(card,ipm))
5610                         kfree(ipm);
5611         }
5612 }
5613
5614 static inline void
5615 qeth_add_vlan_mc(struct qeth_card *card)
5616 {
5617 #ifdef CONFIG_QETH_VLAN
5618         struct in_device *in_dev;
5619         struct vlan_group *vg;
5620         int i;
5621
5622         QETH_DBF_TEXT(trace,4,"addmcvl");
5623         if ( ((card->options.layer2 == 0) &&
5624               (!qeth_is_supported(card,IPA_FULL_VLAN))) ||
5625              (card->vlangrp == NULL) )
5626                 return ;
5627
5628         vg = card->vlangrp;
5629         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5630                 if (vg->vlan_devices[i] == NULL ||
5631                     !(vg->vlan_devices[i]->flags & IFF_UP))
5632                         continue;
5633                 in_dev = in_dev_get(vg->vlan_devices[i]);
5634                 if (!in_dev)
5635                         continue;
5636                 read_lock(&in_dev->mc_list_lock);
5637                 qeth_add_mc(card,in_dev);
5638                 read_unlock(&in_dev->mc_list_lock);
5639                 in_dev_put(in_dev);
5640         }
5641 #endif
5642 }
5643
5644 static void
5645 qeth_add_multicast_ipv4(struct qeth_card *card)
5646 {
5647         struct in_device *in4_dev;
5648
5649         QETH_DBF_TEXT(trace,4,"chkmcv4");
5650         in4_dev = in_dev_get(card->dev);
5651         if (in4_dev == NULL)
5652                 return;
5653         read_lock(&in4_dev->mc_list_lock);
5654         qeth_add_mc(card, in4_dev);
5655         qeth_add_vlan_mc(card);
5656         read_unlock(&in4_dev->mc_list_lock);
5657         in_dev_put(in4_dev);
5658 }
5659
5660 #ifdef CONFIG_QETH_IPV6
5661 static inline void
5662 qeth_add_mc6(struct qeth_card *card, struct inet6_dev *in6_dev)
5663 {
5664         struct qeth_ipaddr *ipm;
5665         struct ifmcaddr6 *im6;
5666         char buf[MAX_ADDR_LEN];
5667
5668         QETH_DBF_TEXT(trace,4,"addmc6");
5669         for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
5670                 ndisc_mc_map(&im6->mca_addr, buf, in6_dev->dev, 0);
5671                 ipm = qeth_get_addr_buffer(QETH_PROT_IPV6);
5672                 if (!ipm)
5673                         continue;
5674                 ipm->is_multicast = 1;
5675                 memcpy(ipm->mac,buf,OSA_ADDR_LEN);
5676                 memcpy(&ipm->u.a6.addr,&im6->mca_addr.s6_addr,
5677                        sizeof(struct in6_addr));
5678                 if (!qeth_add_ip(card,ipm))
5679                         kfree(ipm);
5680         }
5681 }
5682
5683 static inline void
5684 qeth_add_vlan_mc6(struct qeth_card *card)
5685 {
5686 #ifdef CONFIG_QETH_VLAN
5687         struct inet6_dev *in_dev;
5688         struct vlan_group *vg;
5689         int i;
5690
5691         QETH_DBF_TEXT(trace,4,"admc6vl");
5692         if ( ((card->options.layer2 == 0) &&
5693               (!qeth_is_supported(card,IPA_FULL_VLAN))) ||
5694              (card->vlangrp == NULL))
5695                 return ;
5696
5697         vg = card->vlangrp;
5698         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5699                 if (vg->vlan_devices[i] == NULL ||
5700                     !(vg->vlan_devices[i]->flags & IFF_UP))
5701                         continue;
5702                 in_dev = in6_dev_get(vg->vlan_devices[i]);
5703                 if (!in_dev)
5704                         continue;
5705                 read_lock(&in_dev->lock);
5706                 qeth_add_mc6(card,in_dev);
5707                 read_unlock(&in_dev->lock);
5708                 in6_dev_put(in_dev);
5709         }
5710 #endif /* CONFIG_QETH_VLAN */
5711 }
5712
5713 static void
5714 qeth_add_multicast_ipv6(struct qeth_card *card)
5715 {
5716         struct inet6_dev *in6_dev;
5717
5718         QETH_DBF_TEXT(trace,4,"chkmcv6");
5719         if ((card->options.layer2 == 0) &&
5720             (!qeth_is_supported(card, IPA_IPV6)) )
5721                 return ;
5722
5723         in6_dev = in6_dev_get(card->dev);
5724         if (in6_dev == NULL)
5725                 return;
5726         read_lock(&in6_dev->lock);
5727         qeth_add_mc6(card, in6_dev);
5728         qeth_add_vlan_mc6(card);
5729         read_unlock(&in6_dev->lock);
5730         in6_dev_put(in6_dev);
5731 }
5732 #endif /* CONFIG_QETH_IPV6 */
5733
5734 static int
5735 qeth_layer2_send_setdelmac(struct qeth_card *card, __u8 *mac,
5736                            enum qeth_ipa_cmds ipacmd,
5737                            int (*reply_cb) (struct qeth_card *,
5738                                             struct qeth_reply*,
5739                                             unsigned long))
5740 {
5741         struct qeth_ipa_cmd *cmd;
5742         struct qeth_cmd_buffer *iob;
5743
5744         QETH_DBF_TEXT(trace, 2, "L2sdmac");
5745         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
5746         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5747         cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
5748         memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
5749         return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
5750 }
5751
5752 static int
5753 qeth_layer2_send_setgroupmac_cb(struct qeth_card *card,
5754                                 struct qeth_reply *reply,
5755                                 unsigned long data)
5756 {
5757         struct qeth_ipa_cmd *cmd;
5758         __u8 *mac;
5759
5760         QETH_DBF_TEXT(trace, 2, "L2Sgmacb");
5761         cmd = (struct qeth_ipa_cmd *) data;
5762         mac = &cmd->data.setdelmac.mac[0];
5763         /* MAC already registered, needed in couple/uncouple case */
5764         if (cmd->hdr.return_code == 0x2005) {
5765                 PRINT_WARN("Group MAC %02x:%02x:%02x:%02x:%02x:%02x " \
5766                           "already existing on %s \n",
5767                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5768                           QETH_CARD_IFNAME(card));
5769                 cmd->hdr.return_code = 0;
5770         }
5771         if (cmd->hdr.return_code)
5772                 PRINT_ERR("Could not set group MAC " \
5773                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
5774                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5775                           QETH_CARD_IFNAME(card),cmd->hdr.return_code);
5776         return 0;
5777 }
5778
5779 static int
5780 qeth_layer2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
5781 {
5782         QETH_DBF_TEXT(trace, 2, "L2Sgmac");
5783         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
5784                                           qeth_layer2_send_setgroupmac_cb);
5785 }
5786
5787 static int
5788 qeth_layer2_send_delgroupmac_cb(struct qeth_card *card,
5789                                 struct qeth_reply *reply,
5790                                 unsigned long data)
5791 {
5792         struct qeth_ipa_cmd *cmd;
5793         __u8 *mac;
5794
5795         QETH_DBF_TEXT(trace, 2, "L2Dgmacb");
5796         cmd = (struct qeth_ipa_cmd *) data;
5797         mac = &cmd->data.setdelmac.mac[0];
5798         if (cmd->hdr.return_code)
5799                 PRINT_ERR("Could not delete group MAC " \
5800                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
5801                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5802                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
5803         return 0;
5804 }
5805
5806 static int
5807 qeth_layer2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
5808 {
5809         QETH_DBF_TEXT(trace, 2, "L2Dgmac");
5810         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
5811                                           qeth_layer2_send_delgroupmac_cb);
5812 }
5813
5814 static int
5815 qeth_layer2_send_setmac_cb(struct qeth_card *card,
5816                            struct qeth_reply *reply,
5817                            unsigned long data)
5818 {
5819         struct qeth_ipa_cmd *cmd;
5820
5821         QETH_DBF_TEXT(trace, 2, "L2Smaccb");
5822         cmd = (struct qeth_ipa_cmd *) data;
5823         if (cmd->hdr.return_code) {
5824                 QETH_DBF_TEXT_(trace, 2, "L2er%x", cmd->hdr.return_code);
5825                 PRINT_WARN("Error in registering MAC address on " \
5826                            "device %s: x%x\n", CARD_BUS_ID(card),
5827                            cmd->hdr.return_code);
5828                 card->info.layer2_mac_registered = 0;
5829                 cmd->hdr.return_code = -EIO;
5830         } else {
5831                 card->info.layer2_mac_registered = 1;
5832                 memcpy(card->dev->dev_addr,cmd->data.setdelmac.mac,
5833                        OSA_ADDR_LEN);
5834                 PRINT_INFO("MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
5835                            "successfully registered on device %s\n",
5836                            card->dev->dev_addr[0], card->dev->dev_addr[1],
5837                            card->dev->dev_addr[2], card->dev->dev_addr[3],
5838                            card->dev->dev_addr[4], card->dev->dev_addr[5],
5839                            card->dev->name);
5840         }
5841         return 0;
5842 }
5843
5844 static int
5845 qeth_layer2_send_setmac(struct qeth_card *card, __u8 *mac)
5846 {
5847         QETH_DBF_TEXT(trace, 2, "L2Setmac");
5848         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
5849                                           qeth_layer2_send_setmac_cb);
5850 }
5851
5852 static int
5853 qeth_layer2_send_delmac_cb(struct qeth_card *card,
5854                            struct qeth_reply *reply,
5855                            unsigned long data)
5856 {
5857         struct qeth_ipa_cmd *cmd;
5858
5859         QETH_DBF_TEXT(trace, 2, "L2Dmaccb");
5860         cmd = (struct qeth_ipa_cmd *) data;
5861         if (cmd->hdr.return_code) {
5862                 PRINT_WARN("Error in deregistering MAC address on " \
5863                            "device %s: x%x\n", CARD_BUS_ID(card),
5864                            cmd->hdr.return_code);
5865                 QETH_DBF_TEXT_(trace, 2, "err%d", cmd->hdr.return_code);
5866                 cmd->hdr.return_code = -EIO;
5867                 return 0;
5868         }
5869         card->info.layer2_mac_registered = 0;
5870
5871         return 0;
5872 }
5873 static int
5874 qeth_layer2_send_delmac(struct qeth_card *card, __u8 *mac)
5875 {
5876         QETH_DBF_TEXT(trace, 2, "L2Delmac");
5877         if (!card->info.layer2_mac_registered)
5878                 return 0;
5879         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
5880                                           qeth_layer2_send_delmac_cb);
5881 }
5882
5883 static int
5884 qeth_layer2_set_mac_address(struct net_device *dev, void *p)
5885 {
5886         struct sockaddr *addr = p;
5887         struct qeth_card *card;
5888         int rc = 0;
5889
5890         QETH_DBF_TEXT(trace, 3, "setmac");
5891
5892         if (qeth_verify_dev(dev) != QETH_REAL_CARD) {
5893                 QETH_DBF_TEXT(trace, 3, "setmcINV");
5894                 return -EOPNOTSUPP;
5895         }
5896         card = (struct qeth_card *) dev->priv;
5897
5898         if (!card->options.layer2) {
5899                 PRINT_WARN("Setting MAC address on %s is not supported"
5900                            "in Layer 3 mode.\n", dev->name);
5901                 QETH_DBF_TEXT(trace, 3, "setmcLY3");
5902                 return -EOPNOTSUPP;
5903         }
5904         if (card->info.type == QETH_CARD_TYPE_OSN) {
5905                 PRINT_WARN("Setting MAC address on %s is not supported.\n",
5906                            dev->name);
5907                 QETH_DBF_TEXT(trace, 3, "setmcOSN");
5908                 return -EOPNOTSUPP;
5909         }
5910         QETH_DBF_TEXT_(trace, 3, "%s", CARD_BUS_ID(card));
5911         QETH_DBF_HEX(trace, 3, addr->sa_data, OSA_ADDR_LEN);
5912         rc = qeth_layer2_send_delmac(card, &card->dev->dev_addr[0]);
5913         if (!rc)
5914                 rc = qeth_layer2_send_setmac(card, addr->sa_data);
5915         return rc;
5916 }
5917
5918 static void
5919 qeth_fill_ipacmd_header(struct qeth_card *card, struct qeth_ipa_cmd *cmd,
5920                         __u8 command, enum qeth_prot_versions prot)
5921 {
5922         memset(cmd, 0, sizeof (struct qeth_ipa_cmd));
5923         cmd->hdr.command = command;
5924         cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
5925         cmd->hdr.seqno = card->seqno.ipa;
5926         cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
5927         cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
5928         if (card->options.layer2)
5929                 cmd->hdr.prim_version_no = 2;
5930         else
5931                 cmd->hdr.prim_version_no = 1;
5932         cmd->hdr.param_count = 1;
5933         cmd->hdr.prot_version = prot;
5934         cmd->hdr.ipa_supported = 0;
5935         cmd->hdr.ipa_enabled = 0;
5936 }
5937
5938 static struct qeth_cmd_buffer *
5939 qeth_get_ipacmd_buffer(struct qeth_card *card, enum qeth_ipa_cmds ipacmd,
5940                        enum qeth_prot_versions prot)
5941 {
5942         struct qeth_cmd_buffer *iob;
5943         struct qeth_ipa_cmd *cmd;
5944
5945         iob = qeth_wait_for_buffer(&card->write);
5946         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5947         qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
5948
5949         return iob;
5950 }
5951
5952 static int
5953 qeth_send_setdelmc(struct qeth_card *card, struct qeth_ipaddr *addr, int ipacmd)
5954 {
5955         int rc;
5956         struct qeth_cmd_buffer *iob;
5957         struct qeth_ipa_cmd *cmd;
5958
5959         QETH_DBF_TEXT(trace,4,"setdelmc");
5960
5961         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
5962         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5963         memcpy(&cmd->data.setdelipm.mac,addr->mac, OSA_ADDR_LEN);
5964         if (addr->proto == QETH_PROT_IPV6)
5965                 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
5966                        sizeof(struct in6_addr));
5967         else
5968                 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr,4);
5969
5970         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
5971
5972         return rc;
5973 }
5974 static inline void
5975 qeth_fill_netmask(u8 *netmask, unsigned int len)
5976 {
5977         int i,j;
5978         for (i=0;i<16;i++) {
5979                 j=(len)-(i*8);
5980                 if (j >= 8)
5981                         netmask[i] = 0xff;
5982                 else if (j > 0)
5983                         netmask[i] = (u8)(0xFF00>>j);
5984                 else
5985                         netmask[i] = 0;
5986         }
5987 }
5988
5989 static int
5990 qeth_send_setdelip(struct qeth_card *card, struct qeth_ipaddr *addr,
5991                    int ipacmd, unsigned int flags)
5992 {
5993         int rc;
5994         struct qeth_cmd_buffer *iob;
5995         struct qeth_ipa_cmd *cmd;
5996         __u8 netmask[16];
5997
5998         QETH_DBF_TEXT(trace,4,"setdelip");
5999         QETH_DBF_TEXT_(trace,4,"flags%02X", flags);
6000
6001         iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
6002         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
6003         if (addr->proto == QETH_PROT_IPV6) {
6004                 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
6005                        sizeof(struct in6_addr));
6006                 qeth_fill_netmask(netmask,addr->u.a6.pfxlen);
6007                 memcpy(cmd->data.setdelip6.mask, netmask,
6008                        sizeof(struct in6_addr));
6009                 cmd->data.setdelip6.flags = flags;
6010         } else {
6011                 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
6012                 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
6013                 cmd->data.setdelip4.flags = flags;
6014         }
6015
6016         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
6017
6018         return rc;
6019 }
6020
6021 static int
6022 qeth_layer2_register_addr_entry(struct qeth_card *card,
6023                                 struct qeth_ipaddr *addr)
6024 {
6025         if (!addr->is_multicast)
6026                 return 0;
6027         QETH_DBF_TEXT(trace, 2, "setgmac");
6028         QETH_DBF_HEX(trace,3,&addr->mac[0],OSA_ADDR_LEN);
6029         return qeth_layer2_send_setgroupmac(card, &addr->mac[0]);
6030 }
6031
6032 static int
6033 qeth_layer2_deregister_addr_entry(struct qeth_card *card,
6034                                   struct qeth_ipaddr *addr)
6035 {
6036         if (!addr->is_multicast)
6037                 return 0;
6038         QETH_DBF_TEXT(trace, 2, "delgmac");
6039         QETH_DBF_HEX(trace,3,&addr->mac[0],OSA_ADDR_LEN);
6040         return qeth_layer2_send_delgroupmac(card, &addr->mac[0]);
6041 }
6042
6043 static int
6044 qeth_layer3_register_addr_entry(struct qeth_card *card,
6045                                 struct qeth_ipaddr *addr)
6046 {
6047         char buf[50];
6048         int rc;
6049         int cnt = 3;
6050
6051         if (addr->proto == QETH_PROT_IPV4) {
6052                 QETH_DBF_TEXT(trace, 2,"setaddr4");
6053                 QETH_DBF_HEX(trace, 3, &addr->u.a4.addr, sizeof(int));
6054         } else if (addr->proto == QETH_PROT_IPV6) {
6055                 QETH_DBF_TEXT(trace, 2, "setaddr6");
6056                 QETH_DBF_HEX(trace,3,&addr->u.a6.addr,8);
6057                 QETH_DBF_HEX(trace,3,((char *)&addr->u.a6.addr)+8,8);
6058         } else {
6059                 QETH_DBF_TEXT(trace, 2, "setaddr?");
6060                 QETH_DBF_HEX(trace, 3, addr, sizeof(struct qeth_ipaddr));
6061         }
6062         do {
6063                 if (addr->is_multicast)
6064                         rc =  qeth_send_setdelmc(card, addr, IPA_CMD_SETIPM);
6065                 else
6066                         rc = qeth_send_setdelip(card, addr, IPA_CMD_SETIP,
6067                                         addr->set_flags);
6068                 if (rc)
6069                         QETH_DBF_TEXT(trace, 2, "failed");
6070         } while ((--cnt > 0) && rc);
6071         if (rc){
6072                 QETH_DBF_TEXT(trace, 2, "FAILED");
6073                 qeth_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
6074                 PRINT_WARN("Could not register IP address %s (rc=0x%x/%d)\n",
6075                            buf, rc, rc);
6076         }
6077         return rc;
6078 }
6079
6080 static int
6081 qeth_layer3_deregister_addr_entry(struct qeth_card *card,
6082                                   struct qeth_ipaddr *addr)
6083 {
6084         //char buf[50];
6085         int rc;
6086
6087         if (addr->proto == QETH_PROT_IPV4) {
6088                 QETH_DBF_TEXT(trace, 2,"deladdr4");
6089                 QETH_DBF_HEX(trace, 3, &addr->u.a4.addr, sizeof(int));
6090         } else if (addr->proto == QETH_PROT_IPV6) {
6091                 QETH_DBF_TEXT(trace, 2, "deladdr6");
6092                 QETH_DBF_HEX(trace,3,&addr->u.a6.addr,8);
6093                 QETH_DBF_HEX(trace,3,((char *)&addr->u.a6.addr)+8,8);
6094         } else {
6095                 QETH_DBF_TEXT(trace, 2, "deladdr?");
6096                 QETH_DBF_HEX(trace, 3, addr, sizeof(struct qeth_ipaddr));
6097         }
6098         if (addr->is_multicast)
6099                 rc = qeth_send_setdelmc(card, addr, IPA_CMD_DELIPM);
6100         else
6101                 rc = qeth_send_setdelip(card, addr, IPA_CMD_DELIP,
6102                                         addr->del_flags);
6103         if (rc) {
6104                 QETH_DBF_TEXT(trace, 2, "failed");
6105                 /* TODO: re-activate this warning as soon as we have a
6106                  * clean mirco code
6107                 qeth_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
6108                 PRINT_WARN("Could not deregister IP address %s (rc=%x)\n",
6109                            buf, rc);
6110                 */
6111         }
6112         return rc;
6113 }
6114
6115 static int
6116 qeth_register_addr_entry(struct qeth_card *card, struct qeth_ipaddr *addr)
6117 {
6118         if (card->options.layer2)
6119                 return qeth_layer2_register_addr_entry(card, addr);
6120
6121         return qeth_layer3_register_addr_entry(card, addr);
6122 }
6123
6124 static int
6125 qeth_deregister_addr_entry(struct qeth_card *card, struct qeth_ipaddr *addr)
6126 {
6127         if (card->options.layer2)
6128                 return qeth_layer2_deregister_addr_entry(card, addr);
6129
6130         return qeth_layer3_deregister_addr_entry(card, addr);
6131 }
6132
6133 static u32
6134 qeth_ethtool_get_tx_csum(struct net_device *dev)
6135 {
6136         /* We may need to say that we support tx csum offload if
6137          * we do EDDP or TSO. There are discussions going on to
6138          * enforce rules in the stack and in ethtool that make
6139          * SG and TSO depend on HW_CSUM. At the moment there are
6140          * no such rules....
6141          * If we say yes here, we have to checksum outbound packets
6142          * any time. */
6143         return 0;
6144 }
6145
6146 static int
6147 qeth_ethtool_set_tx_csum(struct net_device *dev, u32 data)
6148 {
6149         return -EINVAL;
6150 }
6151
6152 static u32
6153 qeth_ethtool_get_rx_csum(struct net_device *dev)
6154 {
6155         struct qeth_card *card = (struct qeth_card *)dev->priv;
6156
6157         return (card->options.checksum_type == HW_CHECKSUMMING);
6158 }
6159
6160 static int
6161 qeth_ethtool_set_rx_csum(struct net_device *dev, u32 data)
6162 {
6163         struct qeth_card *card = (struct qeth_card *)dev->priv;
6164
6165         if ((card->state != CARD_STATE_DOWN) &&
6166             (card->state != CARD_STATE_RECOVER))
6167                 return -EPERM;
6168         if (data)
6169                 card->options.checksum_type = HW_CHECKSUMMING;
6170         else
6171                 card->options.checksum_type = SW_CHECKSUMMING;
6172         return 0;
6173 }
6174
6175 static u32
6176 qeth_ethtool_get_sg(struct net_device *dev)
6177 {
6178         struct qeth_card *card = (struct qeth_card *)dev->priv;
6179
6180         return ((card->options.large_send != QETH_LARGE_SEND_NO) &&
6181                 (dev->features & NETIF_F_SG));
6182 }
6183
6184 static int
6185 qeth_ethtool_set_sg(struct net_device *dev, u32 data)
6186 {
6187         struct qeth_card *card = (struct qeth_card *)dev->priv;
6188
6189         if (data) {
6190                 if (card->options.large_send != QETH_LARGE_SEND_NO)
6191                         dev->features |= NETIF_F_SG;
6192                 else {
6193                         dev->features &= ~NETIF_F_SG;
6194                         return -EINVAL;
6195                 }
6196         } else
6197                 dev->features &= ~NETIF_F_SG;
6198         return 0;
6199 }
6200
6201 static u32
6202 qeth_ethtool_get_tso(struct net_device *dev)
6203 {
6204         struct qeth_card *card = (struct qeth_card *)dev->priv;
6205
6206         return ((card->options.large_send != QETH_LARGE_SEND_NO) &&
6207                 (dev->features & NETIF_F_TSO));
6208 }
6209
6210 static int
6211 qeth_ethtool_set_tso(struct net_device *dev, u32 data)
6212 {
6213         struct qeth_card *card = (struct qeth_card *)dev->priv;
6214
6215         if (data) {
6216                 if (card->options.large_send != QETH_LARGE_SEND_NO)
6217                         dev->features |= NETIF_F_TSO;
6218                 else {
6219                         dev->features &= ~NETIF_F_TSO;
6220                         return -EINVAL;
6221                 }
6222         } else
6223                 dev->features &= ~NETIF_F_TSO;
6224         return 0;
6225 }
6226
6227 static struct ethtool_ops qeth_ethtool_ops = {
6228         .get_tx_csum = qeth_ethtool_get_tx_csum,
6229         .set_tx_csum = qeth_ethtool_set_tx_csum,
6230         .get_rx_csum = qeth_ethtool_get_rx_csum,
6231         .set_rx_csum = qeth_ethtool_set_rx_csum,
6232         .get_sg      = qeth_ethtool_get_sg,
6233         .set_sg      = qeth_ethtool_set_sg,
6234         .get_tso     = qeth_ethtool_get_tso,
6235         .set_tso     = qeth_ethtool_set_tso,
6236 };
6237
6238 static int
6239 qeth_netdev_init(struct net_device *dev)
6240 {
6241         struct qeth_card *card;
6242
6243         card = (struct qeth_card *) dev->priv;
6244
6245         QETH_DBF_TEXT(trace,3,"initdev");
6246
6247         dev->tx_timeout = &qeth_tx_timeout;
6248         dev->watchdog_timeo = QETH_TX_TIMEOUT;
6249         dev->open = qeth_open;
6250         dev->stop = qeth_stop;
6251         dev->hard_start_xmit = qeth_hard_start_xmit;
6252         dev->do_ioctl = qeth_do_ioctl;
6253         dev->get_stats = qeth_get_stats;
6254         dev->change_mtu = qeth_change_mtu;
6255         dev->neigh_setup = qeth_neigh_setup;
6256         dev->set_multicast_list = qeth_set_multicast_list;
6257 #ifdef CONFIG_QETH_VLAN
6258         dev->vlan_rx_register = qeth_vlan_rx_register;
6259         dev->vlan_rx_kill_vid = qeth_vlan_rx_kill_vid;
6260         dev->vlan_rx_add_vid = qeth_vlan_rx_add_vid;
6261 #endif
6262         dev->hard_header = card->orig_hard_header;
6263         if (qeth_get_netdev_flags(card) & IFF_NOARP) {
6264                 dev->rebuild_header = NULL;
6265                 dev->hard_header = NULL;
6266                 if (card->options.fake_ll)
6267                         dev->hard_header = qeth_fake_header;
6268                 dev->header_cache_update = NULL;
6269                 dev->hard_header_cache = NULL;
6270         }
6271 #ifdef CONFIG_QETH_IPV6
6272         /*IPv6 address autoconfiguration stuff*/
6273         if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
6274                 card->dev->dev_id = card->info.unique_id & 0xffff;
6275 #endif
6276         dev->hard_header_parse = NULL;
6277         dev->set_mac_address = qeth_layer2_set_mac_address;
6278         dev->flags |= qeth_get_netdev_flags(card);
6279         if ((card->options.fake_broadcast) ||
6280             (card->info.broadcast_capable))
6281                 dev->flags |= IFF_BROADCAST;
6282         dev->hard_header_len =
6283                         qeth_get_hlen(card->info.link_type) + card->options.add_hhlen;
6284         dev->addr_len = OSA_ADDR_LEN;
6285         dev->mtu = card->info.initial_mtu;
6286         if (card->info.type != QETH_CARD_TYPE_OSN)
6287                 SET_ETHTOOL_OPS(dev, &qeth_ethtool_ops);
6288         SET_MODULE_OWNER(dev);
6289         return 0;
6290 }
6291
6292 static void
6293 qeth_init_func_level(struct qeth_card *card)
6294 {
6295         if (card->ipato.enabled) {
6296                 if (card->info.type == QETH_CARD_TYPE_IQD)
6297                                 card->info.func_level =
6298                                         QETH_IDX_FUNC_LEVEL_IQD_ENA_IPAT;
6299                 else
6300                                 card->info.func_level =
6301                                         QETH_IDX_FUNC_LEVEL_OSAE_ENA_IPAT;
6302         } else {
6303                 if (card->info.type == QETH_CARD_TYPE_IQD)
6304                 /*FIXME:why do we have same values for  dis and ena for osae??? */
6305                         card->info.func_level =
6306                                 QETH_IDX_FUNC_LEVEL_IQD_DIS_IPAT;
6307                 else
6308                         card->info.func_level =
6309                                 QETH_IDX_FUNC_LEVEL_OSAE_DIS_IPAT;
6310         }
6311 }
6312
6313 /**
6314  * hardsetup card, initialize MPC and QDIO stuff
6315  */
6316 static int
6317 qeth_hardsetup_card(struct qeth_card *card)
6318 {
6319         int retries = 3;
6320         int rc;
6321
6322         QETH_DBF_TEXT(setup, 2, "hrdsetup");
6323
6324 retry:
6325         if (retries < 3){
6326                 PRINT_WARN("Retrying to do IDX activates.\n");
6327                 ccw_device_set_offline(CARD_DDEV(card));
6328                 ccw_device_set_offline(CARD_WDEV(card));
6329                 ccw_device_set_offline(CARD_RDEV(card));
6330                 ccw_device_set_online(CARD_RDEV(card));
6331                 ccw_device_set_online(CARD_WDEV(card));
6332                 ccw_device_set_online(CARD_DDEV(card));
6333         }
6334         rc = qeth_qdio_clear_card(card,card->info.type!=QETH_CARD_TYPE_IQD);
6335         if (rc == -ERESTARTSYS) {
6336                 QETH_DBF_TEXT(setup, 2, "break1");
6337                 return rc;
6338         } else if (rc) {
6339                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
6340                 if (--retries < 0)
6341                         goto out;
6342                 else
6343                         goto retry;
6344         }
6345         if ((rc = qeth_get_unitaddr(card))){
6346                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
6347                 return rc;
6348         }
6349         qeth_init_tokens(card);
6350         qeth_init_func_level(card);
6351         rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
6352         if (rc == -ERESTARTSYS) {
6353                 QETH_DBF_TEXT(setup, 2, "break2");
6354                 return rc;
6355         } else if (rc) {
6356                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
6357                 if (--retries < 0)
6358                         goto out;
6359                 else
6360                         goto retry;
6361         }
6362         rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
6363         if (rc == -ERESTARTSYS) {
6364                 QETH_DBF_TEXT(setup, 2, "break3");
6365                 return rc;
6366         } else if (rc) {
6367                 QETH_DBF_TEXT_(setup, 2, "4err%d", rc);
6368                 if (--retries < 0)
6369                         goto out;
6370                 else
6371                         goto retry;
6372         }
6373         if ((rc = qeth_mpc_initialize(card))){
6374                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
6375                 goto out;
6376         }
6377         /*network device will be recovered*/
6378         if (card->dev) {
6379                 card->dev->hard_header = card->orig_hard_header;
6380                 return 0;
6381         }
6382         /* at first set_online allocate netdev */
6383         card->dev = qeth_get_netdevice(card->info.type,
6384                                        card->info.link_type);
6385         if (!card->dev){
6386                 qeth_qdio_clear_card(card, card->info.type !=
6387                                      QETH_CARD_TYPE_IQD);
6388                 rc = -ENODEV;
6389                 QETH_DBF_TEXT_(setup, 2, "6err%d", rc);
6390                 goto out;
6391         }
6392         card->dev->priv = card;
6393         card->orig_hard_header = card->dev->hard_header;
6394         card->dev->type = qeth_get_arphdr_type(card->info.type,
6395                                                card->info.link_type);
6396         card->dev->init = qeth_netdev_init;
6397         return 0;
6398 out:
6399         PRINT_ERR("Initialization in hardsetup failed! rc=%d\n", rc);
6400         return rc;
6401 }
6402
6403 static int
6404 qeth_default_setassparms_cb(struct qeth_card *card, struct qeth_reply *reply,
6405                             unsigned long data)
6406 {
6407         struct qeth_ipa_cmd *cmd;
6408
6409         QETH_DBF_TEXT(trace,4,"defadpcb");
6410
6411         cmd = (struct qeth_ipa_cmd *) data;
6412         if (cmd->hdr.return_code == 0){
6413                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
6414                 if (cmd->hdr.prot_version == QETH_PROT_IPV4)
6415                         card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
6416 #ifdef CONFIG_QETH_IPV6
6417                 if (cmd->hdr.prot_version == QETH_PROT_IPV6)
6418                         card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
6419 #endif
6420         }
6421         if (cmd->data.setassparms.hdr.assist_no == IPA_INBOUND_CHECKSUM &&
6422             cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
6423                 card->info.csum_mask = cmd->data.setassparms.data.flags_32bit;
6424                 QETH_DBF_TEXT_(trace, 3, "csum:%d", card->info.csum_mask);
6425         }
6426         return 0;
6427 }
6428
6429 static int
6430 qeth_default_setadapterparms_cb(struct qeth_card *card,
6431                                 struct qeth_reply *reply,
6432                                 unsigned long data)
6433 {
6434         struct qeth_ipa_cmd *cmd;
6435
6436         QETH_DBF_TEXT(trace,4,"defadpcb");
6437
6438         cmd = (struct qeth_ipa_cmd *) data;
6439         if (cmd->hdr.return_code == 0)
6440                 cmd->hdr.return_code = cmd->data.setadapterparms.hdr.return_code;
6441         return 0;
6442 }
6443
6444 static int
6445 qeth_query_setadapterparms_cb(struct qeth_card *card, struct qeth_reply *reply,
6446                               unsigned long data)
6447 {
6448         struct qeth_ipa_cmd *cmd;
6449
6450         QETH_DBF_TEXT(trace,3,"quyadpcb");
6451
6452         cmd = (struct qeth_ipa_cmd *) data;
6453         if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f)
6454                 card->info.link_type =
6455                       cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
6456         card->options.adp.supported_funcs =
6457                 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
6458         return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
6459 }
6460
6461 static int
6462 qeth_query_setadapterparms(struct qeth_card *card)
6463 {
6464         int rc;
6465         struct qeth_cmd_buffer *iob;
6466
6467         QETH_DBF_TEXT(trace,3,"queryadp");
6468         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
6469                                    sizeof(struct qeth_ipacmd_setadpparms));
6470         rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
6471         return rc;
6472 }
6473
6474 static int
6475 qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
6476                                    struct qeth_reply *reply,
6477                                    unsigned long data)
6478 {
6479         struct qeth_ipa_cmd *cmd;
6480
6481         QETH_DBF_TEXT(trace,4,"chgmaccb");
6482
6483         cmd = (struct qeth_ipa_cmd *) data;
6484         memcpy(card->dev->dev_addr,
6485                &cmd->data.setadapterparms.data.change_addr.addr,OSA_ADDR_LEN);
6486         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
6487         return 0;
6488 }
6489
6490 static int
6491 qeth_setadpparms_change_macaddr(struct qeth_card *card)
6492 {
6493         int rc;
6494         struct qeth_cmd_buffer *iob;
6495         struct qeth_ipa_cmd *cmd;
6496
6497         QETH_DBF_TEXT(trace,4,"chgmac");
6498
6499         iob = qeth_get_adapter_cmd(card,IPA_SETADP_ALTER_MAC_ADDRESS,
6500                                    sizeof(struct qeth_ipacmd_setadpparms));
6501         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
6502         cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
6503         cmd->data.setadapterparms.data.change_addr.addr_size = OSA_ADDR_LEN;
6504         memcpy(&cmd->data.setadapterparms.data.change_addr.addr,
6505                card->dev->dev_addr, OSA_ADDR_LEN);
6506         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
6507                                NULL);
6508         return rc;
6509 }
6510
6511 static int
6512 qeth_send_setadp_mode(struct qeth_card *card, __u32 command, __u32 mode)
6513 {
6514         int rc;
6515         struct qeth_cmd_buffer *iob;
6516         struct qeth_ipa_cmd *cmd;
6517
6518         QETH_DBF_TEXT(trace,4,"adpmode");
6519
6520         iob = qeth_get_adapter_cmd(card, command,
6521                                    sizeof(struct qeth_ipacmd_setadpparms));
6522         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
6523         cmd->data.setadapterparms.data.mode = mode;
6524         rc = qeth_send_ipa_cmd(card, iob, qeth_default_setadapterparms_cb,
6525                                NULL);
6526         return rc;
6527 }
6528
6529 static inline int
6530 qeth_setadapter_hstr(struct qeth_card *card)
6531 {
6532         int rc;
6533
6534         QETH_DBF_TEXT(trace,4,"adphstr");
6535
6536         if (qeth_adp_supported(card,IPA_SETADP_SET_BROADCAST_MODE)) {
6537                 rc = qeth_send_setadp_mode(card, IPA_SETADP_SET_BROADCAST_MODE,
6538                                            card->options.broadcast_mode);
6539                 if (rc)
6540                         PRINT_WARN("couldn't set broadcast mode on "
6541                                    "device %s: x%x\n",
6542                                    CARD_BUS_ID(card), rc);
6543                 rc = qeth_send_setadp_mode(card, IPA_SETADP_ALTER_MAC_ADDRESS,
6544                                            card->options.macaddr_mode);
6545                 if (rc)
6546                         PRINT_WARN("couldn't set macaddr mode on "
6547                                    "device %s: x%x\n", CARD_BUS_ID(card), rc);
6548                 return rc;
6549         }
6550         if (card->options.broadcast_mode == QETH_TR_BROADCAST_LOCAL)
6551                 PRINT_WARN("set adapter parameters not available "
6552                            "to set broadcast mode, using ALLRINGS "
6553                            "on device %s:\n", CARD_BUS_ID(card));
6554         if (card->options.macaddr_mode == QETH_TR_MACADDR_CANONICAL)
6555                 PRINT_WARN("set adapter parameters not available "
6556                            "to set macaddr mode, using NONCANONICAL "
6557                            "on device %s:\n", CARD_BUS_ID(card));
6558         return 0;
6559 }
6560
6561 static int
6562 qeth_setadapter_parms(struct qeth_card *card)
6563 {
6564         int rc;
6565
6566         QETH_DBF_TEXT(setup, 2, "setadprm");
6567
6568         if (!qeth_is_supported(card, IPA_SETADAPTERPARMS)){
6569                 PRINT_WARN("set adapter parameters not supported "
6570                            "on device %s.\n",
6571                            CARD_BUS_ID(card));
6572                 QETH_DBF_TEXT(setup, 2, " notsupp");
6573                 return 0;
6574         }
6575         rc = qeth_query_setadapterparms(card);
6576         if (rc) {
6577                 PRINT_WARN("couldn't set adapter parameters on device %s: "
6578                            "x%x\n", CARD_BUS_ID(card), rc);
6579                 return rc;
6580         }
6581         if (qeth_adp_supported(card,IPA_SETADP_ALTER_MAC_ADDRESS)) {
6582                 rc = qeth_setadpparms_change_macaddr(card);
6583                 if (rc)
6584                         PRINT_WARN("couldn't get MAC address on "
6585                                    "device %s: x%x\n",
6586                                    CARD_BUS_ID(card), rc);
6587         }
6588
6589         if ((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
6590             (card->info.link_type == QETH_LINK_TYPE_LANE_TR))
6591                 rc = qeth_setadapter_hstr(card);
6592
6593         return rc;
6594 }
6595
6596 static int
6597 qeth_layer2_initialize(struct qeth_card *card)
6598 {
6599         int rc = 0;
6600
6601
6602         QETH_DBF_TEXT(setup, 2, "doL2init");
6603         QETH_DBF_TEXT_(setup, 2, "doL2%s", CARD_BUS_ID(card));
6604
6605         rc = qeth_setadpparms_change_macaddr(card);
6606         if (rc) {
6607                 PRINT_WARN("couldn't get MAC address on "
6608                            "device %s: x%x\n",
6609                            CARD_BUS_ID(card), rc);
6610                 QETH_DBF_TEXT_(setup, 2,"1err%d",rc);
6611                 return rc;
6612         }
6613         QETH_DBF_HEX(setup,2, card->dev->dev_addr, OSA_ADDR_LEN);
6614
6615         rc = qeth_layer2_send_setmac(card, &card->dev->dev_addr[0]);
6616         if (rc)
6617                 QETH_DBF_TEXT_(setup, 2,"2err%d",rc);
6618         return 0;
6619 }
6620
6621
6622 static int
6623 qeth_send_startstoplan(struct qeth_card *card, enum qeth_ipa_cmds ipacmd,
6624                        enum qeth_prot_versions prot)
6625 {
6626         int rc;
6627         struct qeth_cmd_buffer *iob;
6628
6629         iob = qeth_get_ipacmd_buffer(card,ipacmd,prot);
6630         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
6631
6632         return rc;
6633 }
6634
6635 static int
6636 qeth_send_startlan(struct qeth_card *card, enum qeth_prot_versions prot)
6637 {
6638         int rc;
6639
6640         QETH_DBF_TEXT_(setup, 2, "strtlan%i", prot);
6641
6642         rc = qeth_send_startstoplan(card, IPA_CMD_STARTLAN, prot);
6643         return rc;
6644 }
6645
6646 static int
6647 qeth_send_stoplan(struct qeth_card *card)
6648 {
6649         int rc = 0;
6650
6651         /*
6652          * TODO: according to the IPA format document page 14,
6653          * TCP/IP (we!) never issue a STOPLAN
6654          * is this right ?!?
6655          */
6656         QETH_DBF_TEXT(trace, 2, "stoplan");
6657
6658         rc = qeth_send_startstoplan(card, IPA_CMD_STOPLAN, QETH_PROT_IPV4);
6659         return rc;
6660 }
6661
6662 static int
6663 qeth_query_ipassists_cb(struct qeth_card *card, struct qeth_reply *reply,
6664                         unsigned long data)
6665 {
6666         struct qeth_ipa_cmd *cmd;
6667
6668         QETH_DBF_TEXT(setup, 2, "qipasscb");
6669
6670         cmd = (struct qeth_ipa_cmd *) data;
6671         if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
6672                 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
6673                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
6674                 /* Disable IPV6 support hard coded for Hipersockets */
6675                 if(card->info.type == QETH_CARD_TYPE_IQD)
6676                         card->options.ipa4.supported_funcs &= ~IPA_IPV6;
6677         } else {
6678 #ifdef CONFIG_QETH_IPV6
6679                 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
6680                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
6681 #endif
6682         }
6683         QETH_DBF_TEXT(setup, 2, "suppenbl");
6684         QETH_DBF_TEXT_(setup, 2, "%x",cmd->hdr.ipa_supported);
6685         QETH_DBF_TEXT_(setup, 2, "%x",cmd->hdr.ipa_enabled);
6686         return 0;
6687 }
6688
6689 static int
6690 qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
6691 {
6692         int rc;
6693         struct qeth_cmd_buffer *iob;
6694
6695         QETH_DBF_TEXT_(setup, 2, "qipassi%i", prot);
6696         if (card->options.layer2) {
6697                 QETH_DBF_TEXT(setup, 2, "noprmly2");
6698                 return -EPERM;
6699         }
6700
6701         iob = qeth_get_ipacmd_buffer(card,IPA_CMD_QIPASSIST,prot);
6702         rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
6703         return rc;
6704 }
6705
6706 static struct qeth_cmd_buffer *
6707 qeth_get_setassparms_cmd(struct qeth_card *card, enum qeth_ipa_funcs ipa_func,
6708                          __u16 cmd_code, __u16 len,
6709                          enum qeth_prot_versions prot)
6710 {
6711         struct qeth_cmd_buffer *iob;
6712         struct qeth_ipa_cmd *cmd;
6713
6714         QETH_DBF_TEXT(trace,4,"getasscm");
6715         iob = qeth_get_ipacmd_buffer(card,IPA_CMD_SETASSPARMS,prot);
6716
6717         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
6718         cmd->data.setassparms.hdr.assist_no = ipa_func;
6719         cmd->data.setassparms.hdr.length = 8 + len;
6720         cmd->data.setassparms.hdr.command_code = cmd_code;
6721         cmd->data.setassparms.hdr.return_code = 0;
6722         cmd->data.setassparms.hdr.seq_no = 0;
6723
6724         return iob;
6725 }
6726
6727 static int
6728 qeth_send_setassparms(struct qeth_card *card, struct qeth_cmd_buffer *iob,
6729                       __u16 len, long data,
6730                       int (*reply_cb)
6731                       (struct qeth_card *,struct qeth_reply *,unsigned long),
6732                       void *reply_param)
6733 {
6734         int rc;
6735         struct qeth_ipa_cmd *cmd;
6736
6737         QETH_DBF_TEXT(trace,4,"sendassp");
6738
6739         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
6740         if (len <= sizeof(__u32))
6741                 cmd->data.setassparms.data.flags_32bit = (__u32) data;
6742         else if (len > sizeof(__u32))
6743                 memcpy(&cmd->data.setassparms.data, (void *) data, len);
6744
6745         rc = qeth_send_ipa_cmd(card, iob, reply_cb, reply_param);
6746         return rc;
6747 }
6748
6749 #ifdef CONFIG_QETH_IPV6
6750 static int
6751 qeth_send_simple_setassparms_ipv6(struct qeth_card *card,
6752                                   enum qeth_ipa_funcs ipa_func, __u16 cmd_code)
6753
6754 {
6755         int rc;
6756         struct qeth_cmd_buffer *iob;
6757
6758         QETH_DBF_TEXT(trace,4,"simassp6");
6759         iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
6760                                        0, QETH_PROT_IPV6);
6761         rc = qeth_send_setassparms(card, iob, 0, 0,
6762                                    qeth_default_setassparms_cb, NULL);
6763         return rc;
6764 }
6765 #endif
6766
6767 static int
6768 qeth_send_simple_setassparms(struct qeth_card *card,
6769                              enum qeth_ipa_funcs ipa_func,
6770                              __u16 cmd_code, long data)
6771 {
6772         int rc;
6773         int length = 0;
6774         struct qeth_cmd_buffer *iob;
6775
6776         QETH_DBF_TEXT(trace,4,"simassp4");
6777         if (data)
6778                 length = sizeof(__u32);
6779         iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
6780                                        length, QETH_PROT_IPV4);
6781         rc = qeth_send_setassparms(card, iob, length, data,
6782                                    qeth_default_setassparms_cb, NULL);
6783         return rc;
6784 }
6785
6786 static inline int
6787 qeth_start_ipa_arp_processing(struct qeth_card *card)
6788 {
6789         int rc;
6790
6791         QETH_DBF_TEXT(trace,3,"ipaarp");
6792
6793         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
6794                 PRINT_WARN("ARP processing not supported "
6795                            "on %s!\n", QETH_CARD_IFNAME(card));
6796                 return 0;
6797         }
6798         rc = qeth_send_simple_setassparms(card,IPA_ARP_PROCESSING,
6799                                           IPA_CMD_ASS_START, 0);
6800         if (rc) {
6801                 PRINT_WARN("Could not start ARP processing "
6802                            "assist on %s: 0x%x\n",
6803                            QETH_CARD_IFNAME(card), rc);
6804         }
6805         return rc;
6806 }
6807
6808 static int
6809 qeth_start_ipa_ip_fragmentation(struct qeth_card *card)
6810 {
6811         int rc;
6812
6813         QETH_DBF_TEXT(trace,3,"ipaipfrg");
6814
6815         if (!qeth_is_supported(card, IPA_IP_FRAGMENTATION)) {
6816                 PRINT_INFO("Hardware IP fragmentation not supported on %s\n",
6817                            QETH_CARD_IFNAME(card));
6818                 return  -EOPNOTSUPP;
6819         }
6820
6821         rc = qeth_send_simple_setassparms(card, IPA_IP_FRAGMENTATION,
6822                                           IPA_CMD_ASS_START, 0);
6823         if (rc) {
6824                 PRINT_WARN("Could not start Hardware IP fragmentation "
6825                            "assist on %s: 0x%x\n",
6826                            QETH_CARD_IFNAME(card), rc);
6827         } else
6828                 PRINT_INFO("Hardware IP fragmentation enabled \n");
6829         return rc;
6830 }
6831
6832 static int
6833 qeth_start_ipa_source_mac(struct qeth_card *card)
6834 {
6835         int rc;
6836
6837         QETH_DBF_TEXT(trace,3,"stsrcmac");
6838
6839         if (!card->options.fake_ll)
6840                 return -EOPNOTSUPP;
6841
6842         if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
6843                 PRINT_INFO("Inbound source address not "
6844                            "supported on %s\n", QETH_CARD_IFNAME(card));
6845                 return -EOPNOTSUPP;
6846         }
6847
6848         rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
6849                                           IPA_CMD_ASS_START, 0);
6850         if (rc)
6851                 PRINT_WARN("Could not start inbound source "
6852                            "assist on %s: 0x%x\n",
6853                            QETH_CARD_IFNAME(card), rc);
6854         return rc;
6855 }
6856
6857 static int
6858 qeth_start_ipa_vlan(struct qeth_card *card)
6859 {
6860         int rc = 0;
6861
6862         QETH_DBF_TEXT(trace,3,"strtvlan");
6863
6864 #ifdef CONFIG_QETH_VLAN
6865         if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
6866                 PRINT_WARN("VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
6867                 return -EOPNOTSUPP;
6868         }
6869
6870         rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
6871                                           IPA_CMD_ASS_START,0);
6872         if (rc) {
6873                 PRINT_WARN("Could not start vlan "
6874                            "assist on %s: 0x%x\n",
6875                            QETH_CARD_IFNAME(card), rc);
6876         } else {
6877                 PRINT_INFO("VLAN enabled \n");
6878                 card->dev->features |=
6879                         NETIF_F_HW_VLAN_FILTER |
6880                         NETIF_F_HW_VLAN_TX |
6881                         NETIF_F_HW_VLAN_RX;
6882         }
6883 #endif /* QETH_VLAN */
6884         return rc;
6885 }
6886
6887 static int
6888 qeth_start_ipa_multicast(struct qeth_card *card)
6889 {
6890         int rc;
6891
6892         QETH_DBF_TEXT(trace,3,"stmcast");
6893
6894         if (!qeth_is_supported(card, IPA_MULTICASTING)) {
6895                 PRINT_WARN("Multicast not supported on %s\n",
6896                            QETH_CARD_IFNAME(card));
6897                 return -EOPNOTSUPP;
6898         }
6899
6900         rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
6901                                           IPA_CMD_ASS_START,0);
6902         if (rc) {
6903                 PRINT_WARN("Could not start multicast "
6904                            "assist on %s: rc=%i\n",
6905                            QETH_CARD_IFNAME(card), rc);
6906         } else {
6907                 PRINT_INFO("Multicast enabled\n");
6908                 card->dev->flags |= IFF_MULTICAST;
6909         }
6910         return rc;
6911 }
6912
6913 #ifdef CONFIG_QETH_IPV6
6914 static int
6915 qeth_softsetup_ipv6(struct qeth_card *card)
6916 {
6917         int rc;
6918
6919         QETH_DBF_TEXT(trace,3,"softipv6");
6920
6921         netif_stop_queue(card->dev);
6922         rc = qeth_send_startlan(card, QETH_PROT_IPV6);
6923         if (rc) {
6924                 PRINT_ERR("IPv6 startlan failed on %s\n",
6925                           QETH_CARD_IFNAME(card));
6926                 return rc;
6927         }
6928         netif_wake_queue(card->dev);
6929         rc = qeth_query_ipassists(card,QETH_PROT_IPV6);
6930         if (rc) {
6931                 PRINT_ERR("IPv6 query ipassist failed on %s\n",
6932                           QETH_CARD_IFNAME(card));
6933                 return rc;
6934         }
6935         rc = qeth_send_simple_setassparms(card, IPA_IPV6,
6936                                           IPA_CMD_ASS_START, 3);
6937         if (rc) {
6938                 PRINT_WARN("IPv6 start assist (version 4) failed "
6939                            "on %s: 0x%x\n",
6940                            QETH_CARD_IFNAME(card), rc);
6941                 return rc;
6942         }
6943         rc = qeth_send_simple_setassparms_ipv6(card, IPA_IPV6,
6944                                                IPA_CMD_ASS_START);
6945         if (rc) {
6946                 PRINT_WARN("IPV6 start assist (version 6) failed  "
6947                            "on %s: 0x%x\n",
6948                            QETH_CARD_IFNAME(card), rc);
6949                 return rc;
6950         }
6951         rc = qeth_send_simple_setassparms_ipv6(card, IPA_PASSTHRU,
6952                                                IPA_CMD_ASS_START);
6953         if (rc) {
6954                 PRINT_WARN("Could not enable passthrough "
6955                            "on %s: 0x%x\n",
6956                            QETH_CARD_IFNAME(card), rc);
6957                 return rc;
6958         }
6959         PRINT_INFO("IPV6 enabled \n");
6960         return 0;
6961 }
6962
6963 #endif
6964
6965 static int
6966 qeth_start_ipa_ipv6(struct qeth_card *card)
6967 {
6968         int rc = 0;
6969 #ifdef CONFIG_QETH_IPV6
6970         QETH_DBF_TEXT(trace,3,"strtipv6");
6971
6972         if (!qeth_is_supported(card, IPA_IPV6)) {
6973                 PRINT_WARN("IPv6 not supported on %s\n",
6974                            QETH_CARD_IFNAME(card));
6975                 return 0;
6976         }
6977         rc = qeth_softsetup_ipv6(card);
6978 #endif
6979         return rc ;
6980 }
6981
6982 static int
6983 qeth_start_ipa_broadcast(struct qeth_card *card)
6984 {
6985         int rc;
6986
6987         QETH_DBF_TEXT(trace,3,"stbrdcst");
6988         card->info.broadcast_capable = 0;
6989         if (!qeth_is_supported(card, IPA_FILTERING)) {
6990                 PRINT_WARN("Broadcast not supported on %s\n",
6991                            QETH_CARD_IFNAME(card));
6992                 rc = -EOPNOTSUPP;
6993                 goto out;
6994         }
6995         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
6996                                           IPA_CMD_ASS_START, 0);
6997         if (rc) {
6998                 PRINT_WARN("Could not enable broadcasting filtering "
6999                            "on %s: 0x%x\n",
7000                            QETH_CARD_IFNAME(card), rc);
7001                 goto out;
7002         }
7003
7004         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
7005                                           IPA_CMD_ASS_CONFIGURE, 1);
7006         if (rc) {
7007                 PRINT_WARN("Could not set up broadcast filtering on %s: 0x%x\n",
7008                            QETH_CARD_IFNAME(card), rc);
7009                 goto out;
7010         }
7011         card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
7012         PRINT_INFO("Broadcast enabled \n");
7013         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
7014                                           IPA_CMD_ASS_ENABLE, 1);
7015         if (rc) {
7016                 PRINT_WARN("Could not set up broadcast echo filtering on "
7017                            "%s: 0x%x\n", QETH_CARD_IFNAME(card), rc);
7018                 goto out;
7019         }
7020         card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
7021 out:
7022         if (card->info.broadcast_capable)
7023                 card->dev->flags |= IFF_BROADCAST;
7024         else
7025                 card->dev->flags &= ~IFF_BROADCAST;
7026         return rc;
7027 }
7028
7029 static int
7030 qeth_send_checksum_command(struct qeth_card *card)
7031 {
7032         int rc;
7033
7034         rc = qeth_send_simple_setassparms(card, IPA_INBOUND_CHECKSUM,
7035                                           IPA_CMD_ASS_START, 0);
7036         if (rc) {
7037                 PRINT_WARN("Starting Inbound HW Checksumming failed on %s: "
7038                            "0x%x,\ncontinuing using Inbound SW Checksumming\n",
7039                            QETH_CARD_IFNAME(card), rc);
7040                 return rc;
7041         }
7042         rc = qeth_send_simple_setassparms(card, IPA_INBOUND_CHECKSUM,
7043                                           IPA_CMD_ASS_ENABLE,
7044                                           card->info.csum_mask);
7045         if (rc) {
7046                 PRINT_WARN("Enabling Inbound HW Checksumming failed on %s: "
7047                            "0x%x,\ncontinuing using Inbound SW Checksumming\n",
7048                            QETH_CARD_IFNAME(card), rc);
7049                 return rc;
7050         }
7051         return 0;
7052 }
7053
7054 static int
7055 qeth_start_ipa_checksum(struct qeth_card *card)
7056 {
7057         int rc = 0;
7058
7059         QETH_DBF_TEXT(trace,3,"strtcsum");
7060
7061         if (card->options.checksum_type == NO_CHECKSUMMING) {
7062                 PRINT_WARN("Using no checksumming on %s.\n",
7063                            QETH_CARD_IFNAME(card));
7064                 return 0;
7065         }
7066         if (card->options.checksum_type == SW_CHECKSUMMING) {
7067                 PRINT_WARN("Using SW checksumming on %s.\n",
7068                            QETH_CARD_IFNAME(card));
7069                 return 0;
7070         }
7071         if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM)) {
7072                 PRINT_WARN("Inbound HW Checksumming not "
7073                            "supported on %s,\ncontinuing "
7074                            "using Inbound SW Checksumming\n",
7075                            QETH_CARD_IFNAME(card));
7076                 card->options.checksum_type = SW_CHECKSUMMING;
7077                 return 0;
7078         }
7079         rc = qeth_send_checksum_command(card);
7080         if (!rc) {
7081                 PRINT_INFO("HW Checksumming (inbound) enabled \n");
7082         }
7083         return rc;
7084 }
7085
7086 static int
7087 qeth_start_ipa_tso(struct qeth_card *card)
7088 {
7089         int rc;
7090
7091         QETH_DBF_TEXT(trace,3,"sttso");
7092
7093         if (!qeth_is_supported(card, IPA_OUTBOUND_TSO)) {
7094                 PRINT_WARN("Outbound TSO not supported on %s\n",
7095                            QETH_CARD_IFNAME(card));
7096                 rc = -EOPNOTSUPP;
7097         } else {
7098                 rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
7099                                                   IPA_CMD_ASS_START,0);
7100                 if (rc)
7101                         PRINT_WARN("Could not start outbound TSO "
7102                                    "assist on %s: rc=%i\n",
7103                                    QETH_CARD_IFNAME(card), rc);
7104                 else
7105                         PRINT_INFO("Outbound TSO enabled\n");
7106         }
7107         if (rc && (card->options.large_send == QETH_LARGE_SEND_TSO)){
7108                 card->options.large_send = QETH_LARGE_SEND_NO;
7109                 card->dev->features &= ~ (NETIF_F_TSO | NETIF_F_SG);
7110         }
7111         return rc;
7112 }
7113
7114 static int
7115 qeth_start_ipassists(struct qeth_card *card)
7116 {
7117         QETH_DBF_TEXT(trace,3,"strtipas");
7118         qeth_start_ipa_arp_processing(card);    /* go on*/
7119         qeth_start_ipa_ip_fragmentation(card);  /* go on*/
7120         qeth_start_ipa_source_mac(card);        /* go on*/
7121         qeth_start_ipa_vlan(card);              /* go on*/
7122         qeth_start_ipa_multicast(card);         /* go on*/
7123         qeth_start_ipa_ipv6(card);              /* go on*/
7124         qeth_start_ipa_broadcast(card);         /* go on*/
7125         qeth_start_ipa_checksum(card);          /* go on*/
7126         qeth_start_ipa_tso(card);               /* go on*/
7127         return 0;
7128 }
7129
7130 static int
7131 qeth_send_setrouting(struct qeth_card *card, enum qeth_routing_types type,
7132                      enum qeth_prot_versions prot)
7133 {
7134         int rc;
7135         struct qeth_ipa_cmd *cmd;
7136         struct qeth_cmd_buffer *iob;
7137
7138         QETH_DBF_TEXT(trace,4,"setroutg");
7139         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
7140         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
7141         cmd->data.setrtg.type = (type);
7142         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
7143
7144         return rc;
7145
7146 }
7147
7148 static void
7149 qeth_correct_routing_type(struct qeth_card *card, enum qeth_routing_types *type,
7150                         enum qeth_prot_versions prot)
7151 {
7152         if (card->info.type == QETH_CARD_TYPE_IQD) {
7153                 switch (*type) {
7154                 case NO_ROUTER:
7155                 case PRIMARY_CONNECTOR:
7156                 case SECONDARY_CONNECTOR:
7157                 case MULTICAST_ROUTER:
7158                         return;
7159                 default:
7160                         goto out_inval;
7161                 }
7162         } else {
7163                 switch (*type) {
7164                 case NO_ROUTER:
7165                 case PRIMARY_ROUTER:
7166                 case SECONDARY_ROUTER:
7167                         return;
7168                 case MULTICAST_ROUTER:
7169                         if (qeth_is_ipafunc_supported(card, prot,
7170                                                       IPA_OSA_MC_ROUTER))
7171                                 return;
7172                 default:
7173                         goto out_inval;
7174                 }
7175         }
7176 out_inval:
7177         PRINT_WARN("Routing type '%s' not supported for interface %s.\n"
7178                    "Router status set to 'no router'.\n",
7179                    ((*type == PRIMARY_ROUTER)? "primary router" :
7180                     (*type == SECONDARY_ROUTER)? "secondary router" :
7181                     (*type == PRIMARY_CONNECTOR)? "primary connector" :
7182                     (*type == SECONDARY_CONNECTOR)? "secondary connector" :
7183                     (*type == MULTICAST_ROUTER)? "multicast router" :
7184                     "unknown"),
7185                    card->dev->name);
7186         *type = NO_ROUTER;
7187 }
7188
7189 int
7190 qeth_setrouting_v4(struct qeth_card *card)
7191 {
7192         int rc;
7193
7194         QETH_DBF_TEXT(trace,3,"setrtg4");
7195
7196         qeth_correct_routing_type(card, &card->options.route4.type,
7197                                   QETH_PROT_IPV4);
7198
7199         rc = qeth_send_setrouting(card, card->options.route4.type,
7200                                   QETH_PROT_IPV4);
7201         if (rc) {
7202                 card->options.route4.type = NO_ROUTER;
7203                 PRINT_WARN("Error (0x%04x) while setting routing type on %s. "
7204                            "Type set to 'no router'.\n",
7205                            rc, QETH_CARD_IFNAME(card));
7206         }
7207         return rc;
7208 }
7209
7210 int
7211 qeth_setrouting_v6(struct qeth_card *card)
7212 {
7213         int rc = 0;
7214
7215         QETH_DBF_TEXT(trace,3,"setrtg6");
7216 #ifdef CONFIG_QETH_IPV6
7217
7218         qeth_correct_routing_type(card, &card->options.route6.type,
7219                                   QETH_PROT_IPV6);
7220
7221         if ((card->options.route6.type == NO_ROUTER) ||
7222             ((card->info.type == QETH_CARD_TYPE_OSAE) &&
7223              (card->options.route6.type == MULTICAST_ROUTER) &&
7224              !qeth_is_supported6(card,IPA_OSA_MC_ROUTER)))
7225                 return 0;
7226         rc = qeth_send_setrouting(card, card->options.route6.type,
7227                                   QETH_PROT_IPV6);
7228         if (rc) {
7229                 card->options.route6.type = NO_ROUTER;
7230                 PRINT_WARN("Error (0x%04x) while setting routing type on %s. "
7231                            "Type set to 'no router'.\n",
7232                            rc, QETH_CARD_IFNAME(card));
7233         }
7234 #endif
7235         return rc;
7236 }
7237
7238 int
7239 qeth_set_large_send(struct qeth_card *card, enum qeth_large_send_types type)
7240 {
7241         int rc = 0;
7242
7243         if (card->dev == NULL) {
7244                 card->options.large_send = type;
7245                 return 0;
7246         }
7247         netif_stop_queue(card->dev);
7248         card->options.large_send = type;
7249         switch (card->options.large_send) {
7250         case QETH_LARGE_SEND_EDDP:
7251                 card->dev->features |= NETIF_F_TSO | NETIF_F_SG;
7252                 break;
7253         case QETH_LARGE_SEND_TSO:
7254                 if (qeth_is_supported(card, IPA_OUTBOUND_TSO)){
7255                         card->dev->features |= NETIF_F_TSO | NETIF_F_SG;
7256                 } else {
7257                         PRINT_WARN("TSO not supported on %s. "
7258                                    "large_send set to 'no'.\n",
7259                                    card->dev->name);
7260                         card->dev->features &= ~(NETIF_F_TSO | NETIF_F_SG);
7261                         card->options.large_send = QETH_LARGE_SEND_NO;
7262                         rc = -EOPNOTSUPP;
7263                 }
7264                 break;
7265         default: /* includes QETH_LARGE_SEND_NO */
7266                 card->dev->features &= ~(NETIF_F_TSO | NETIF_F_SG);
7267                 break;
7268         }
7269         netif_wake_queue(card->dev);
7270         return rc;
7271 }
7272
7273 /*
7274  * softsetup card: init IPA stuff
7275  */
7276 static int
7277 qeth_softsetup_card(struct qeth_card *card)
7278 {
7279         int rc;
7280
7281         QETH_DBF_TEXT(setup, 2, "softsetp");
7282
7283         if ((rc = qeth_send_startlan(card, QETH_PROT_IPV4))){
7284                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
7285                 if (rc == 0xe080){
7286                         PRINT_WARN("LAN on card %s if offline! "
7287                                    "Continuing softsetup.\n",
7288                                    CARD_BUS_ID(card));
7289                         card->lan_online = 0;
7290                 } else
7291                         return rc;
7292         } else
7293                 card->lan_online = 1;
7294         if (card->info.type==QETH_CARD_TYPE_OSN)
7295                 goto out;
7296         if (card->options.layer2) {
7297                 card->dev->features |=
7298                         NETIF_F_HW_VLAN_FILTER |
7299                         NETIF_F_HW_VLAN_TX |
7300                         NETIF_F_HW_VLAN_RX;
7301                 card->dev->flags|=IFF_MULTICAST|IFF_BROADCAST;
7302                 card->info.broadcast_capable=1;
7303                 if ((rc = qeth_layer2_initialize(card))) {
7304                         QETH_DBF_TEXT_(setup, 2, "L2err%d", rc);
7305                         return rc;
7306                 }
7307 #ifdef CONFIG_QETH_VLAN
7308                 qeth_layer2_process_vlans(card, 0);
7309 #endif
7310                 goto out;
7311         }
7312         if ((card->options.large_send == QETH_LARGE_SEND_EDDP) ||
7313             (card->options.large_send == QETH_LARGE_SEND_TSO))
7314                 card->dev->features |= NETIF_F_TSO | NETIF_F_SG;
7315         else
7316                 card->dev->features &= ~(NETIF_F_TSO | NETIF_F_SG);
7317
7318         if ((rc = qeth_setadapter_parms(card)))
7319                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
7320         if ((rc = qeth_start_ipassists(card)))
7321                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
7322         if ((rc = qeth_setrouting_v4(card)))
7323                 QETH_DBF_TEXT_(setup, 2, "4err%d", rc);
7324         if ((rc = qeth_setrouting_v6(card)))
7325                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
7326 out:
7327         netif_stop_queue(card->dev);
7328         return 0;
7329 }
7330
7331 #ifdef CONFIG_QETH_IPV6
7332 static int
7333 qeth_get_unique_id_cb(struct qeth_card *card, struct qeth_reply *reply,
7334                       unsigned long data)
7335 {
7336         struct qeth_ipa_cmd *cmd;
7337
7338         cmd = (struct qeth_ipa_cmd *) data;
7339         if (cmd->hdr.return_code == 0)
7340                 card->info.unique_id = *((__u16 *)
7341                                 &cmd->data.create_destroy_addr.unique_id[6]);
7342         else {
7343                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
7344                                         UNIQUE_ID_NOT_BY_CARD;
7345                 PRINT_WARN("couldn't get a unique id from the card on device "
7346                            "%s (result=x%x), using default id. ipv6 "
7347                            "autoconfig on other lpars may lead to duplicate "
7348                            "ip addresses. please use manually "
7349                            "configured ones.\n",
7350                            CARD_BUS_ID(card), cmd->hdr.return_code);
7351         }
7352         return 0;
7353 }
7354 #endif
7355
7356 static int
7357 qeth_put_unique_id(struct qeth_card *card)
7358 {
7359
7360         int rc = 0;
7361 #ifdef CONFIG_QETH_IPV6
7362         struct qeth_cmd_buffer *iob;
7363         struct qeth_ipa_cmd *cmd;
7364
7365         QETH_DBF_TEXT(trace,2,"puniqeid");
7366
7367         if ((card->info.unique_id & UNIQUE_ID_NOT_BY_CARD) ==
7368                 UNIQUE_ID_NOT_BY_CARD)
7369                 return -1;
7370         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_DESTROY_ADDR,
7371                                      QETH_PROT_IPV6);
7372         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
7373         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
7374                             card->info.unique_id;
7375         memcpy(&cmd->data.create_destroy_addr.unique_id[0],
7376                card->dev->dev_addr, OSA_ADDR_LEN);
7377         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
7378 #else
7379         card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
7380                                 UNIQUE_ID_NOT_BY_CARD;
7381 #endif
7382         return rc;
7383 }
7384
7385 /**
7386  * Clear IP List
7387  */
7388 static void
7389 qeth_clear_ip_list(struct qeth_card *card, int clean, int recover)
7390 {
7391         struct qeth_ipaddr *addr, *tmp;
7392         unsigned long flags;
7393
7394         QETH_DBF_TEXT(trace,4,"clearip");
7395         spin_lock_irqsave(&card->ip_lock, flags);
7396         /* clear todo list */
7397         list_for_each_entry_safe(addr, tmp, card->ip_tbd_list, entry){
7398                 list_del(&addr->entry);
7399                 kfree(addr);
7400         }
7401
7402         while (!list_empty(&card->ip_list)) {
7403                 addr = list_entry(card->ip_list.next,
7404                                   struct qeth_ipaddr, entry);
7405                 list_del_init(&addr->entry);
7406                 if (clean) {
7407                         spin_unlock_irqrestore(&card->ip_lock, flags);
7408                         qeth_deregister_addr_entry(card, addr);
7409                         spin_lock_irqsave(&card->ip_lock, flags);
7410                 }
7411                 if (!recover || addr->is_multicast) {
7412                         kfree(addr);
7413                         continue;
7414                 }
7415                 list_add_tail(&addr->entry, card->ip_tbd_list);
7416         }
7417         spin_unlock_irqrestore(&card->ip_lock, flags);
7418 }
7419
7420 static void
7421 qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
7422                          int clear_start_mask)
7423 {
7424         unsigned long flags;
7425
7426         spin_lock_irqsave(&card->thread_mask_lock, flags);
7427         card->thread_allowed_mask = threads;
7428         if (clear_start_mask)
7429                 card->thread_start_mask &= threads;
7430         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
7431         wake_up(&card->wait_q);
7432 }
7433
7434 static inline int
7435 qeth_threads_running(struct qeth_card *card, unsigned long threads)
7436 {
7437         unsigned long flags;
7438         int rc = 0;
7439
7440         spin_lock_irqsave(&card->thread_mask_lock, flags);
7441         rc = (card->thread_running_mask & threads);
7442         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
7443         return rc;
7444 }
7445
7446 static int
7447 qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
7448 {
7449         return wait_event_interruptible(card->wait_q,
7450                         qeth_threads_running(card, threads) == 0);
7451 }
7452
7453 static int
7454 qeth_stop_card(struct qeth_card *card, int recovery_mode)
7455 {
7456         int rc = 0;
7457
7458         QETH_DBF_TEXT(setup ,2,"stopcard");
7459         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
7460
7461         qeth_set_allowed_threads(card, 0, 1);
7462         if (qeth_wait_for_threads(card, ~QETH_RECOVER_THREAD))
7463                 return -ERESTARTSYS;
7464         if (card->read.state == CH_STATE_UP &&
7465             card->write.state == CH_STATE_UP &&
7466             (card->state == CARD_STATE_UP)) {
7467                 if (recovery_mode && 
7468                     card->info.type != QETH_CARD_TYPE_OSN) {
7469                         qeth_stop(card->dev);
7470                 } else {
7471                         rtnl_lock();
7472                         dev_close(card->dev);
7473                         rtnl_unlock();
7474                 }
7475                 if (!card->use_hard_stop) {
7476                         __u8 *mac = &card->dev->dev_addr[0];
7477                         rc = qeth_layer2_send_delmac(card, mac);
7478                         QETH_DBF_TEXT_(setup, 2, "Lerr%d", rc);
7479                         if ((rc = qeth_send_stoplan(card)))
7480                                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
7481                 }
7482                 card->state = CARD_STATE_SOFTSETUP;
7483         }
7484         if (card->state == CARD_STATE_SOFTSETUP) {
7485 #ifdef CONFIG_QETH_VLAN
7486                 if (card->options.layer2)
7487                         qeth_layer2_process_vlans(card, 1);
7488 #endif
7489                 qeth_clear_ip_list(card, !card->use_hard_stop, 1);
7490                 qeth_clear_ipacmd_list(card);
7491                 card->state = CARD_STATE_HARDSETUP;
7492         }
7493         if (card->state == CARD_STATE_HARDSETUP) {
7494                 if ((!card->use_hard_stop) &&
7495                     (!card->options.layer2))
7496                         if ((rc = qeth_put_unique_id(card)))
7497                                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
7498                 qeth_qdio_clear_card(card, 0);
7499                 qeth_clear_qdio_buffers(card);
7500                 qeth_clear_working_pool_list(card);
7501                 card->state = CARD_STATE_DOWN;
7502         }
7503         if (card->state == CARD_STATE_DOWN) {
7504                 qeth_clear_cmd_buffers(&card->read);
7505                 qeth_clear_cmd_buffers(&card->write);
7506         }
7507         card->use_hard_stop = 0;
7508         return rc;
7509 }
7510
7511
7512 static int
7513 qeth_get_unique_id(struct qeth_card *card)
7514 {
7515         int rc = 0;
7516 #ifdef CONFIG_QETH_IPV6
7517         struct qeth_cmd_buffer *iob;
7518         struct qeth_ipa_cmd *cmd;
7519
7520         QETH_DBF_TEXT(setup, 2, "guniqeid");
7521
7522         if (!qeth_is_supported(card,IPA_IPV6)) {
7523                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
7524                                         UNIQUE_ID_NOT_BY_CARD;
7525                 return 0;
7526         }
7527
7528         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
7529                                      QETH_PROT_IPV6);
7530         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
7531         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
7532                             card->info.unique_id;
7533
7534         rc = qeth_send_ipa_cmd(card, iob, qeth_get_unique_id_cb, NULL);
7535 #else
7536         card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
7537                                 UNIQUE_ID_NOT_BY_CARD;
7538 #endif
7539         return rc;
7540 }
7541 static void
7542 qeth_print_status_with_portname(struct qeth_card *card)
7543 {
7544         char dbf_text[15];
7545         int i;
7546
7547         sprintf(dbf_text, "%s", card->info.portname + 1);
7548         for (i = 0; i < 8; i++)
7549                 dbf_text[i] =
7550                         (char) _ebcasc[(__u8) dbf_text[i]];
7551         dbf_text[8] = 0;
7552         printk("qeth: Device %s/%s/%s is a%s card%s%s%s\n"
7553                "with link type %s (portname: %s)\n",
7554                CARD_RDEV_ID(card),
7555                CARD_WDEV_ID(card),
7556                CARD_DDEV_ID(card),
7557                qeth_get_cardname(card),
7558                (card->info.mcl_level[0]) ? " (level: " : "",
7559                (card->info.mcl_level[0]) ? card->info.mcl_level : "",
7560                (card->info.mcl_level[0]) ? ")" : "",
7561                qeth_get_cardname_short(card),
7562                dbf_text);
7563
7564 }
7565
7566 static void
7567 qeth_print_status_no_portname(struct qeth_card *card)
7568 {
7569         if (card->info.portname[0])
7570                 printk("qeth: Device %s/%s/%s is a%s "
7571                        "card%s%s%s\nwith link type %s "
7572                        "(no portname needed by interface).\n",
7573                        CARD_RDEV_ID(card),
7574                        CARD_WDEV_ID(card),
7575                        CARD_DDEV_ID(card),
7576                        qeth_get_cardname(card),
7577                        (card->info.mcl_level[0]) ? " (level: " : "",
7578                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
7579                        (card->info.mcl_level[0]) ? ")" : "",
7580                        qeth_get_cardname_short(card));
7581         else
7582                 printk("qeth: Device %s/%s/%s is a%s "
7583                        "card%s%s%s\nwith link type %s.\n",
7584                        CARD_RDEV_ID(card),
7585                        CARD_WDEV_ID(card),
7586                        CARD_DDEV_ID(card),
7587                        qeth_get_cardname(card),
7588                        (card->info.mcl_level[0]) ? " (level: " : "",
7589                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
7590                        (card->info.mcl_level[0]) ? ")" : "",
7591                        qeth_get_cardname_short(card));
7592 }
7593
7594 static void
7595 qeth_print_status_message(struct qeth_card *card)
7596 {
7597         switch (card->info.type) {
7598         case QETH_CARD_TYPE_OSAE:
7599                 /* VM will use a non-zero first character
7600                  * to indicate a HiperSockets like reporting
7601                  * of the level OSA sets the first character to zero
7602                  * */
7603                 if (!card->info.mcl_level[0]) {
7604                         sprintf(card->info.mcl_level,"%02x%02x",
7605                                 card->info.mcl_level[2],
7606                                 card->info.mcl_level[3]);
7607
7608                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
7609                         break;
7610                 }
7611                 /* fallthrough */
7612         case QETH_CARD_TYPE_IQD:
7613                 card->info.mcl_level[0] = (char) _ebcasc[(__u8)
7614                         card->info.mcl_level[0]];
7615                 card->info.mcl_level[1] = (char) _ebcasc[(__u8)
7616                         card->info.mcl_level[1]];
7617                 card->info.mcl_level[2] = (char) _ebcasc[(__u8)
7618                         card->info.mcl_level[2]];
7619                 card->info.mcl_level[3] = (char) _ebcasc[(__u8)
7620                         card->info.mcl_level[3]];
7621                 card->info.mcl_level[QETH_MCL_LENGTH] = 0;
7622                 break;
7623         default:
7624                 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
7625         }
7626         if (card->info.portname_required)
7627                 qeth_print_status_with_portname(card);
7628         else
7629                 qeth_print_status_no_portname(card);
7630 }
7631
7632 static int
7633 qeth_register_netdev(struct qeth_card *card)
7634 {
7635         QETH_DBF_TEXT(setup, 3, "regnetd");
7636         if (card->dev->reg_state != NETREG_UNINITIALIZED) {
7637                 qeth_netdev_init(card->dev);
7638                 return 0;
7639         }
7640         /* sysfs magic */
7641         SET_NETDEV_DEV(card->dev, &card->gdev->dev);
7642         return register_netdev(card->dev);
7643 }
7644
7645 static void
7646 qeth_start_again(struct qeth_card *card, int recovery_mode)
7647 {
7648         QETH_DBF_TEXT(setup ,2, "startag");
7649
7650         if (recovery_mode && 
7651             card->info.type != QETH_CARD_TYPE_OSN) {
7652                 qeth_open(card->dev);
7653         } else {
7654                 rtnl_lock();
7655                 dev_open(card->dev);
7656                 rtnl_unlock();
7657         }
7658         /* this also sets saved unicast addresses */
7659         qeth_set_multicast_list(card->dev);
7660 }
7661
7662
7663 /* Layer 2 specific stuff */
7664 #define IGNORE_PARAM_EQ(option,value,reset_value,msg) \
7665         if (card->options.option == value) { \
7666                 PRINT_ERR("%s not supported with layer 2 " \
7667                           "functionality, ignoring option on read" \
7668                           "channel device %s .\n",msg,CARD_RDEV_ID(card)); \
7669                 card->options.option = reset_value; \
7670         }
7671 #define IGNORE_PARAM_NEQ(option,value,reset_value,msg) \
7672         if (card->options.option != value) { \
7673                 PRINT_ERR("%s not supported with layer 2 " \
7674                           "functionality, ignoring option on read" \
7675                           "channel device %s .\n",msg,CARD_RDEV_ID(card)); \
7676                 card->options.option = reset_value; \
7677         }
7678
7679
7680 static void qeth_make_parameters_consistent(struct qeth_card *card)
7681 {
7682
7683         if (card->options.layer2 == 0)
7684                 return;
7685         if (card->info.type == QETH_CARD_TYPE_OSN)
7686                 return;
7687         if (card->info.type == QETH_CARD_TYPE_IQD) {
7688                 PRINT_ERR("Device %s does not support layer 2 functionality." \
7689                           " Ignoring layer2 option.\n",CARD_BUS_ID(card));
7690                 card->options.layer2 = 0;
7691                 return;
7692         }
7693         IGNORE_PARAM_NEQ(route4.type, NO_ROUTER, NO_ROUTER,
7694                          "Routing options are");
7695 #ifdef CONFIG_QETH_IPV6
7696         IGNORE_PARAM_NEQ(route6.type, NO_ROUTER, NO_ROUTER,
7697                          "Routing options are");
7698 #endif
7699         IGNORE_PARAM_EQ(checksum_type, HW_CHECKSUMMING,
7700                         QETH_CHECKSUM_DEFAULT,
7701                         "Checksumming options are");
7702         IGNORE_PARAM_NEQ(broadcast_mode, QETH_TR_BROADCAST_ALLRINGS,
7703                          QETH_TR_BROADCAST_ALLRINGS,
7704                          "Broadcast mode options are");
7705         IGNORE_PARAM_NEQ(macaddr_mode, QETH_TR_MACADDR_NONCANONICAL,
7706                          QETH_TR_MACADDR_NONCANONICAL,
7707                          "Canonical MAC addr options are");
7708         IGNORE_PARAM_NEQ(fake_broadcast, 0, 0,
7709                          "Broadcast faking options are");
7710         IGNORE_PARAM_NEQ(add_hhlen, DEFAULT_ADD_HHLEN,
7711                          DEFAULT_ADD_HHLEN,"Option add_hhlen is");
7712         IGNORE_PARAM_NEQ(fake_ll, 0, 0,"Option fake_ll is");
7713 }
7714
7715
7716 static int
7717 __qeth_set_online(struct ccwgroup_device *gdev, int recovery_mode)
7718 {
7719         struct qeth_card *card = gdev->dev.driver_data;
7720         int rc = 0;
7721         enum qeth_card_states recover_flag;
7722
7723         BUG_ON(!card);
7724         QETH_DBF_TEXT(setup ,2, "setonlin");
7725         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
7726
7727         qeth_set_allowed_threads(card, QETH_RECOVER_THREAD, 1);
7728         if (qeth_wait_for_threads(card, ~QETH_RECOVER_THREAD)){
7729                 PRINT_WARN("set_online of card %s interrupted by user!\n",
7730                            CARD_BUS_ID(card));
7731                 return -ERESTARTSYS;
7732         }
7733
7734         recover_flag = card->state;
7735         if ((rc = ccw_device_set_online(CARD_RDEV(card))) ||
7736             (rc = ccw_device_set_online(CARD_WDEV(card))) ||
7737             (rc = ccw_device_set_online(CARD_DDEV(card)))){
7738                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
7739                 return -EIO;
7740         }
7741
7742         qeth_make_parameters_consistent(card);
7743
7744         if ((rc = qeth_hardsetup_card(card))){
7745                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
7746                 goto out_remove;
7747         }
7748         card->state = CARD_STATE_HARDSETUP;
7749
7750         if (!(rc = qeth_query_ipassists(card,QETH_PROT_IPV4)))
7751                 rc = qeth_get_unique_id(card);
7752
7753         if (rc && card->options.layer2 == 0) {
7754                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
7755                 goto out_remove;
7756         }
7757         qeth_print_status_message(card);
7758         if ((rc = qeth_register_netdev(card))){
7759                 QETH_DBF_TEXT_(setup, 2, "4err%d", rc);
7760                 goto out_remove;
7761         }
7762         if ((rc = qeth_softsetup_card(card))){
7763                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
7764                 goto out_remove;
7765         }
7766         card->state = CARD_STATE_SOFTSETUP;
7767
7768         if ((rc = qeth_init_qdio_queues(card))){
7769                 QETH_DBF_TEXT_(setup, 2, "6err%d", rc);
7770                 goto out_remove;
7771         }
7772 /*maybe it was set offline without ifconfig down
7773  * we can also use this state for recovery purposes*/
7774         qeth_set_allowed_threads(card, 0xffffffff, 0);
7775         if (recover_flag == CARD_STATE_RECOVER)
7776                 qeth_start_again(card, recovery_mode);
7777         qeth_notify_processes();
7778         return 0;
7779 out_remove:
7780         card->use_hard_stop = 1;
7781         qeth_stop_card(card, 0);
7782         ccw_device_set_offline(CARD_DDEV(card));
7783         ccw_device_set_offline(CARD_WDEV(card));
7784         ccw_device_set_offline(CARD_RDEV(card));
7785         if (recover_flag == CARD_STATE_RECOVER)
7786                 card->state = CARD_STATE_RECOVER;
7787         else
7788                 card->state = CARD_STATE_DOWN;
7789         return -ENODEV;
7790 }
7791
7792 static int
7793 qeth_set_online(struct ccwgroup_device *gdev)
7794 {
7795         return __qeth_set_online(gdev, 0);
7796 }
7797
7798 static struct ccw_device_id qeth_ids[] = {
7799         {CCW_DEVICE(0x1731, 0x01), driver_info:QETH_CARD_TYPE_OSAE},
7800         {CCW_DEVICE(0x1731, 0x05), driver_info:QETH_CARD_TYPE_IQD},
7801         {CCW_DEVICE(0x1731, 0x06), driver_info:QETH_CARD_TYPE_OSN},
7802         {},
7803 };
7804 MODULE_DEVICE_TABLE(ccw, qeth_ids);
7805
7806 struct device *qeth_root_dev = NULL;
7807
7808 struct ccwgroup_driver qeth_ccwgroup_driver = {
7809         .owner = THIS_MODULE,
7810         .name = "qeth",
7811         .driver_id = 0xD8C5E3C8,
7812         .probe = qeth_probe_device,
7813         .remove = qeth_remove_device,
7814         .set_online = qeth_set_online,
7815         .set_offline = qeth_set_offline,
7816 };
7817
7818 struct ccw_driver qeth_ccw_driver = {
7819         .name = "qeth",
7820         .ids = qeth_ids,
7821         .probe = ccwgroup_probe_ccwdev,
7822         .remove = ccwgroup_remove_ccwdev,
7823 };
7824
7825
7826 static void
7827 qeth_unregister_dbf_views(void)
7828 {
7829         if (qeth_dbf_setup)
7830                 debug_unregister(qeth_dbf_setup);
7831         if (qeth_dbf_qerr)
7832                 debug_unregister(qeth_dbf_qerr);
7833         if (qeth_dbf_sense)
7834                 debug_unregister(qeth_dbf_sense);
7835         if (qeth_dbf_misc)
7836                 debug_unregister(qeth_dbf_misc);
7837         if (qeth_dbf_data)
7838                 debug_unregister(qeth_dbf_data);
7839         if (qeth_dbf_control)
7840                 debug_unregister(qeth_dbf_control);
7841         if (qeth_dbf_trace)
7842                 debug_unregister(qeth_dbf_trace);
7843 }
7844 static int
7845 qeth_register_dbf_views(void)
7846 {
7847         qeth_dbf_setup = debug_register(QETH_DBF_SETUP_NAME,
7848                                         QETH_DBF_SETUP_PAGES,
7849                                         QETH_DBF_SETUP_NR_AREAS,
7850                                         QETH_DBF_SETUP_LEN);
7851         qeth_dbf_misc = debug_register(QETH_DBF_MISC_NAME,
7852                                        QETH_DBF_MISC_PAGES,
7853                                        QETH_DBF_MISC_NR_AREAS,
7854                                        QETH_DBF_MISC_LEN);
7855         qeth_dbf_data = debug_register(QETH_DBF_DATA_NAME,
7856                                        QETH_DBF_DATA_PAGES,
7857                                        QETH_DBF_DATA_NR_AREAS,
7858                                        QETH_DBF_DATA_LEN);
7859         qeth_dbf_control = debug_register(QETH_DBF_CONTROL_NAME,
7860                                           QETH_DBF_CONTROL_PAGES,
7861                                           QETH_DBF_CONTROL_NR_AREAS,
7862                                           QETH_DBF_CONTROL_LEN);
7863         qeth_dbf_sense = debug_register(QETH_DBF_SENSE_NAME,
7864                                         QETH_DBF_SENSE_PAGES,
7865                                         QETH_DBF_SENSE_NR_AREAS,
7866                                         QETH_DBF_SENSE_LEN);
7867         qeth_dbf_qerr = debug_register(QETH_DBF_QERR_NAME,
7868                                        QETH_DBF_QERR_PAGES,
7869                                        QETH_DBF_QERR_NR_AREAS,
7870                                        QETH_DBF_QERR_LEN);
7871         qeth_dbf_trace = debug_register(QETH_DBF_TRACE_NAME,
7872                                         QETH_DBF_TRACE_PAGES,
7873                                         QETH_DBF_TRACE_NR_AREAS,
7874                                         QETH_DBF_TRACE_LEN);
7875
7876         if ((qeth_dbf_setup == NULL) || (qeth_dbf_misc == NULL)    ||
7877             (qeth_dbf_data == NULL)  || (qeth_dbf_control == NULL) ||
7878             (qeth_dbf_sense == NULL) || (qeth_dbf_qerr == NULL)    ||
7879             (qeth_dbf_trace == NULL)) {
7880                 qeth_unregister_dbf_views();
7881                 return -ENOMEM;
7882         }
7883         debug_register_view(qeth_dbf_setup, &debug_hex_ascii_view);
7884         debug_set_level(qeth_dbf_setup, QETH_DBF_SETUP_LEVEL);
7885
7886         debug_register_view(qeth_dbf_misc, &debug_hex_ascii_view);
7887         debug_set_level(qeth_dbf_misc, QETH_DBF_MISC_LEVEL);
7888
7889         debug_register_view(qeth_dbf_data, &debug_hex_ascii_view);
7890         debug_set_level(qeth_dbf_data, QETH_DBF_DATA_LEVEL);
7891
7892         debug_register_view(qeth_dbf_control, &debug_hex_ascii_view);
7893         debug_set_level(qeth_dbf_control, QETH_DBF_CONTROL_LEVEL);
7894
7895         debug_register_view(qeth_dbf_sense, &debug_hex_ascii_view);
7896         debug_set_level(qeth_dbf_sense, QETH_DBF_SENSE_LEVEL);
7897
7898         debug_register_view(qeth_dbf_qerr, &debug_hex_ascii_view);
7899         debug_set_level(qeth_dbf_qerr, QETH_DBF_QERR_LEVEL);
7900
7901         debug_register_view(qeth_dbf_trace, &debug_hex_ascii_view);
7902         debug_set_level(qeth_dbf_trace, QETH_DBF_TRACE_LEVEL);
7903
7904         return 0;
7905 }
7906
7907 #ifdef CONFIG_QETH_IPV6
7908 extern struct neigh_table arp_tbl;
7909 static struct neigh_ops *arp_direct_ops;
7910 static int (*qeth_old_arp_constructor) (struct neighbour *);
7911
7912 static struct neigh_ops arp_direct_ops_template = {
7913         .family = AF_INET,
7914         .destructor = NULL,
7915         .solicit = NULL,
7916         .error_report = NULL,
7917         .output = dev_queue_xmit,
7918         .connected_output = dev_queue_xmit,
7919         .hh_output = dev_queue_xmit,
7920         .queue_xmit = dev_queue_xmit
7921 };
7922
7923 static int
7924 qeth_arp_constructor(struct neighbour *neigh)
7925 {
7926         struct net_device *dev = neigh->dev;
7927         struct in_device *in_dev;
7928         struct neigh_parms *parms;
7929         struct qeth_card *card;
7930
7931         card = qeth_get_card_from_dev(dev);
7932         if (card == NULL)
7933                 goto out;
7934         if((card->options.layer2) ||
7935            (card->dev->hard_header == qeth_fake_header))
7936                 goto out;
7937
7938         rcu_read_lock();
7939         in_dev = __in_dev_get_rcu(dev);
7940         if (in_dev == NULL) {
7941                 rcu_read_unlock();
7942                 return -EINVAL;
7943         }
7944
7945         parms = in_dev->arp_parms;
7946         __neigh_parms_put(neigh->parms);
7947         neigh->parms = neigh_parms_clone(parms);
7948         rcu_read_unlock();
7949
7950         neigh->type = inet_addr_type(*(u32 *) neigh->primary_key);
7951         neigh->nud_state = NUD_NOARP;
7952         neigh->ops = arp_direct_ops;
7953         neigh->output = neigh->ops->queue_xmit;
7954         return 0;
7955 out:
7956         return qeth_old_arp_constructor(neigh);
7957 }
7958 #endif  /*CONFIG_QETH_IPV6*/
7959
7960 /*
7961  * IP address takeover related functions
7962  */
7963 static void
7964 qeth_clear_ipato_list(struct qeth_card *card)
7965 {
7966         struct qeth_ipato_entry *ipatoe, *tmp;
7967         unsigned long flags;
7968
7969         spin_lock_irqsave(&card->ip_lock, flags);
7970         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
7971                 list_del(&ipatoe->entry);
7972                 kfree(ipatoe);
7973         }
7974         spin_unlock_irqrestore(&card->ip_lock, flags);
7975 }
7976
7977 int
7978 qeth_add_ipato_entry(struct qeth_card *card, struct qeth_ipato_entry *new)
7979 {
7980         struct qeth_ipato_entry *ipatoe;
7981         unsigned long flags;
7982         int rc = 0;
7983
7984         QETH_DBF_TEXT(trace, 2, "addipato");
7985         spin_lock_irqsave(&card->ip_lock, flags);
7986         list_for_each_entry(ipatoe, &card->ipato.entries, entry){
7987                 if (ipatoe->proto != new->proto)
7988                         continue;
7989                 if (!memcmp(ipatoe->addr, new->addr,
7990                             (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
7991                     (ipatoe->mask_bits == new->mask_bits)){
7992                         PRINT_WARN("ipato entry already exists!\n");
7993                         rc = -EEXIST;
7994                         break;
7995                 }
7996         }
7997         if (!rc) {
7998                 list_add_tail(&new->entry, &card->ipato.entries);
7999         }
8000         spin_unlock_irqrestore(&card->ip_lock, flags);
8001         return rc;
8002 }
8003
8004 void
8005 qeth_del_ipato_entry(struct qeth_card *card, enum qeth_prot_versions proto,
8006                      u8 *addr, int mask_bits)
8007 {
8008         struct qeth_ipato_entry *ipatoe, *tmp;
8009         unsigned long flags;
8010
8011         QETH_DBF_TEXT(trace, 2, "delipato");
8012         spin_lock_irqsave(&card->ip_lock, flags);
8013         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry){
8014                 if (ipatoe->proto != proto)
8015                         continue;
8016                 if (!memcmp(ipatoe->addr, addr,
8017                             (proto == QETH_PROT_IPV4)? 4:16) &&
8018                     (ipatoe->mask_bits == mask_bits)){
8019                         list_del(&ipatoe->entry);
8020                         kfree(ipatoe);
8021                 }
8022         }
8023         spin_unlock_irqrestore(&card->ip_lock, flags);
8024 }
8025
8026 static inline void
8027 qeth_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
8028 {
8029         int i, j;
8030         u8 octet;
8031
8032         for (i = 0; i < len; ++i){
8033                 octet = addr[i];
8034                 for (j = 7; j >= 0; --j){
8035                         bits[i*8 + j] = octet & 1;
8036                         octet >>= 1;
8037                 }
8038         }
8039 }
8040
8041 static int
8042 qeth_is_addr_covered_by_ipato(struct qeth_card *card, struct qeth_ipaddr *addr)
8043 {
8044         struct qeth_ipato_entry *ipatoe;
8045         u8 addr_bits[128] = {0, };
8046         u8 ipatoe_bits[128] = {0, };
8047         int rc = 0;
8048
8049         if (!card->ipato.enabled)
8050                 return 0;
8051
8052         qeth_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
8053                                   (addr->proto == QETH_PROT_IPV4)? 4:16);
8054         list_for_each_entry(ipatoe, &card->ipato.entries, entry){
8055                 if (addr->proto != ipatoe->proto)
8056                         continue;
8057                 qeth_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
8058                                           (ipatoe->proto==QETH_PROT_IPV4) ?
8059                                           4:16);
8060                 if (addr->proto == QETH_PROT_IPV4)
8061                         rc = !memcmp(addr_bits, ipatoe_bits,
8062                                      min(32, ipatoe->mask_bits));
8063                 else
8064                         rc = !memcmp(addr_bits, ipatoe_bits,
8065                                      min(128, ipatoe->mask_bits));
8066                 if (rc)
8067                         break;
8068         }
8069         /* invert? */
8070         if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
8071                 rc = !rc;
8072         else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
8073                 rc = !rc;
8074
8075         return rc;
8076 }
8077
8078 /*
8079  * VIPA related functions
8080  */
8081 int
8082 qeth_add_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
8083               const u8 *addr)
8084 {
8085         struct qeth_ipaddr *ipaddr;
8086         unsigned long flags;
8087         int rc = 0;
8088
8089         ipaddr = qeth_get_addr_buffer(proto);
8090         if (ipaddr){
8091                 if (proto == QETH_PROT_IPV4){
8092                         QETH_DBF_TEXT(trace, 2, "addvipa4");
8093                         memcpy(&ipaddr->u.a4.addr, addr, 4);
8094                         ipaddr->u.a4.mask = 0;
8095 #ifdef CONFIG_QETH_IPV6
8096                 } else if (proto == QETH_PROT_IPV6){
8097                         QETH_DBF_TEXT(trace, 2, "addvipa6");
8098                         memcpy(&ipaddr->u.a6.addr, addr, 16);
8099                         ipaddr->u.a6.pfxlen = 0;
8100 #endif
8101                 }
8102                 ipaddr->type = QETH_IP_TYPE_VIPA;
8103                 ipaddr->set_flags = QETH_IPA_SETIP_VIPA_FLAG;
8104                 ipaddr->del_flags = QETH_IPA_DELIP_VIPA_FLAG;
8105         } else
8106                 return -ENOMEM;
8107         spin_lock_irqsave(&card->ip_lock, flags);
8108         if (__qeth_address_exists_in_list(&card->ip_list, ipaddr, 0) ||
8109             __qeth_address_exists_in_list(card->ip_tbd_list, ipaddr, 0))
8110                 rc = -EEXIST;
8111         spin_unlock_irqrestore(&card->ip_lock, flags);
8112         if (rc){
8113                 PRINT_WARN("Cannot add VIPA. Address already exists!\n");
8114                 return rc;
8115         }
8116         if (!qeth_add_ip(card, ipaddr))
8117                 kfree(ipaddr);
8118         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8119                 schedule_work(&card->kernel_thread_starter);
8120         return rc;
8121 }
8122
8123 void
8124 qeth_del_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
8125               const u8 *addr)
8126 {
8127         struct qeth_ipaddr *ipaddr;
8128
8129         ipaddr = qeth_get_addr_buffer(proto);
8130         if (ipaddr){
8131                 if (proto == QETH_PROT_IPV4){
8132                         QETH_DBF_TEXT(trace, 2, "delvipa4");
8133                         memcpy(&ipaddr->u.a4.addr, addr, 4);
8134                         ipaddr->u.a4.mask = 0;
8135 #ifdef CONFIG_QETH_IPV6
8136                 } else if (proto == QETH_PROT_IPV6){
8137                         QETH_DBF_TEXT(trace, 2, "delvipa6");
8138                         memcpy(&ipaddr->u.a6.addr, addr, 16);
8139                         ipaddr->u.a6.pfxlen = 0;
8140 #endif
8141                 }
8142                 ipaddr->type = QETH_IP_TYPE_VIPA;
8143         } else
8144                 return;
8145         if (!qeth_delete_ip(card, ipaddr))
8146                 kfree(ipaddr);
8147         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8148                 schedule_work(&card->kernel_thread_starter);
8149 }
8150
8151 /*
8152  * proxy ARP related functions
8153  */
8154 int
8155 qeth_add_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
8156               const u8 *addr)
8157 {
8158         struct qeth_ipaddr *ipaddr;
8159         unsigned long flags;
8160         int rc = 0;
8161
8162         ipaddr = qeth_get_addr_buffer(proto);
8163         if (ipaddr){
8164                 if (proto == QETH_PROT_IPV4){
8165                         QETH_DBF_TEXT(trace, 2, "addrxip4");
8166                         memcpy(&ipaddr->u.a4.addr, addr, 4);
8167                         ipaddr->u.a4.mask = 0;
8168 #ifdef CONFIG_QETH_IPV6
8169                 } else if (proto == QETH_PROT_IPV6){
8170                         QETH_DBF_TEXT(trace, 2, "addrxip6");
8171                         memcpy(&ipaddr->u.a6.addr, addr, 16);
8172                         ipaddr->u.a6.pfxlen = 0;
8173 #endif
8174                 }
8175                 ipaddr->type = QETH_IP_TYPE_RXIP;
8176                 ipaddr->set_flags = QETH_IPA_SETIP_TAKEOVER_FLAG;
8177                 ipaddr->del_flags = 0;
8178         } else
8179                 return -ENOMEM;
8180         spin_lock_irqsave(&card->ip_lock, flags);
8181         if (__qeth_address_exists_in_list(&card->ip_list, ipaddr, 0) ||
8182             __qeth_address_exists_in_list(card->ip_tbd_list, ipaddr, 0))
8183                 rc = -EEXIST;
8184         spin_unlock_irqrestore(&card->ip_lock, flags);
8185         if (rc){
8186                 PRINT_WARN("Cannot add RXIP. Address already exists!\n");
8187                 return rc;
8188         }
8189         if (!qeth_add_ip(card, ipaddr))
8190                 kfree(ipaddr);
8191         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8192                 schedule_work(&card->kernel_thread_starter);
8193         return 0;
8194 }
8195
8196 void
8197 qeth_del_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
8198               const u8 *addr)
8199 {
8200         struct qeth_ipaddr *ipaddr;
8201
8202         ipaddr = qeth_get_addr_buffer(proto);
8203         if (ipaddr){
8204                 if (proto == QETH_PROT_IPV4){
8205                         QETH_DBF_TEXT(trace, 2, "addrxip4");
8206                         memcpy(&ipaddr->u.a4.addr, addr, 4);
8207                         ipaddr->u.a4.mask = 0;
8208 #ifdef CONFIG_QETH_IPV6
8209                 } else if (proto == QETH_PROT_IPV6){
8210                         QETH_DBF_TEXT(trace, 2, "addrxip6");
8211                         memcpy(&ipaddr->u.a6.addr, addr, 16);
8212                         ipaddr->u.a6.pfxlen = 0;
8213 #endif
8214                 }
8215                 ipaddr->type = QETH_IP_TYPE_RXIP;
8216         } else
8217                 return;
8218         if (!qeth_delete_ip(card, ipaddr))
8219                 kfree(ipaddr);
8220         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8221                 schedule_work(&card->kernel_thread_starter);
8222 }
8223
8224 /**
8225  * IP event handler
8226  */
8227 static int
8228 qeth_ip_event(struct notifier_block *this,
8229               unsigned long event,void *ptr)
8230 {
8231         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
8232         struct net_device *dev =(struct net_device *) ifa->ifa_dev->dev;
8233         struct qeth_ipaddr *addr;
8234         struct qeth_card *card;
8235
8236         QETH_DBF_TEXT(trace,3,"ipevent");
8237         card = qeth_get_card_from_dev(dev);
8238         if (!card)
8239                 return NOTIFY_DONE;
8240         if (card->options.layer2)
8241                 return NOTIFY_DONE;
8242
8243         addr = qeth_get_addr_buffer(QETH_PROT_IPV4);
8244         if (addr != NULL) {
8245                 addr->u.a4.addr = ifa->ifa_address;
8246                 addr->u.a4.mask = ifa->ifa_mask;
8247                 addr->type = QETH_IP_TYPE_NORMAL;
8248         } else
8249                 goto out;
8250
8251         switch(event) {
8252         case NETDEV_UP:
8253                 if (!qeth_add_ip(card, addr))
8254                         kfree(addr);
8255                 break;
8256         case NETDEV_DOWN:
8257                 if (!qeth_delete_ip(card, addr))
8258                         kfree(addr);
8259                 break;
8260         default:
8261                 break;
8262         }
8263         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8264                 schedule_work(&card->kernel_thread_starter);
8265 out:
8266         return NOTIFY_DONE;
8267 }
8268
8269 static struct notifier_block qeth_ip_notifier = {
8270         qeth_ip_event,
8271         0
8272 };
8273
8274 #ifdef CONFIG_QETH_IPV6
8275 /**
8276  * IPv6 event handler
8277  */
8278 static int
8279 qeth_ip6_event(struct notifier_block *this,
8280               unsigned long event,void *ptr)
8281 {
8282
8283         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
8284         struct net_device *dev = (struct net_device *)ifa->idev->dev;
8285         struct qeth_ipaddr *addr;
8286         struct qeth_card *card;
8287
8288         QETH_DBF_TEXT(trace,3,"ip6event");
8289
8290         card = qeth_get_card_from_dev(dev);
8291         if (!card)
8292                 return NOTIFY_DONE;
8293         if (!qeth_is_supported(card, IPA_IPV6))
8294                 return NOTIFY_DONE;
8295
8296         addr = qeth_get_addr_buffer(QETH_PROT_IPV6);
8297         if (addr != NULL) {
8298                 memcpy(&addr->u.a6.addr, &ifa->addr, sizeof(struct in6_addr));
8299                 addr->u.a6.pfxlen = ifa->prefix_len;
8300                 addr->type = QETH_IP_TYPE_NORMAL;
8301         } else
8302                 goto out;
8303
8304         switch(event) {
8305         case NETDEV_UP:
8306                 if (!qeth_add_ip(card, addr))
8307                         kfree(addr);
8308                 break;
8309         case NETDEV_DOWN:
8310                 if (!qeth_delete_ip(card, addr))
8311                         kfree(addr);
8312                 break;
8313         default:
8314                 break;
8315         }
8316         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
8317                 schedule_work(&card->kernel_thread_starter);
8318 out:
8319         return NOTIFY_DONE;
8320 }
8321
8322 static struct notifier_block qeth_ip6_notifier = {
8323         qeth_ip6_event,
8324         0
8325 };
8326 #endif
8327
8328 static int
8329 __qeth_reboot_event_card(struct device *dev, void *data)
8330 {
8331         struct qeth_card *card;
8332
8333         card = (struct qeth_card *) dev->driver_data;
8334         qeth_clear_ip_list(card, 0, 0);
8335         qeth_qdio_clear_card(card, 0);
8336         return 0;
8337 }
8338
8339 static int
8340 qeth_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
8341 {
8342
8343         driver_for_each_device(&qeth_ccwgroup_driver.driver, NULL, NULL,
8344                                __qeth_reboot_event_card);
8345         return NOTIFY_DONE;
8346 }
8347
8348
8349 static struct notifier_block qeth_reboot_notifier = {
8350         qeth_reboot_event,
8351         0
8352 };
8353
8354 static int
8355 qeth_register_notifiers(void)
8356 {
8357         int r;
8358
8359         QETH_DBF_TEXT(trace,5,"regnotif");
8360         if ((r = register_reboot_notifier(&qeth_reboot_notifier)))
8361                 return r;
8362         if ((r = register_inetaddr_notifier(&qeth_ip_notifier)))
8363                 goto out_reboot;
8364 #ifdef CONFIG_QETH_IPV6
8365         if ((r = register_inet6addr_notifier(&qeth_ip6_notifier)))
8366                 goto out_ipv4;
8367 #endif
8368         return 0;
8369
8370 #ifdef CONFIG_QETH_IPV6
8371 out_ipv4:
8372         unregister_inetaddr_notifier(&qeth_ip_notifier);
8373 #endif
8374 out_reboot:
8375         unregister_reboot_notifier(&qeth_reboot_notifier);
8376         return r;
8377 }
8378
8379 /**
8380  * unregister all event notifiers
8381  */
8382 static void
8383 qeth_unregister_notifiers(void)
8384 {
8385
8386         QETH_DBF_TEXT(trace,5,"unregnot");
8387         BUG_ON(unregister_reboot_notifier(&qeth_reboot_notifier));
8388         BUG_ON(unregister_inetaddr_notifier(&qeth_ip_notifier));
8389 #ifdef CONFIG_QETH_IPV6
8390         BUG_ON(unregister_inet6addr_notifier(&qeth_ip6_notifier));
8391 #endif /* QETH_IPV6 */
8392
8393 }
8394
8395 #ifdef CONFIG_QETH_IPV6
8396 static int
8397 qeth_ipv6_init(void)
8398 {
8399         qeth_old_arp_constructor = arp_tbl.constructor;
8400         write_lock(&arp_tbl.lock);
8401         arp_tbl.constructor = qeth_arp_constructor;
8402         write_unlock(&arp_tbl.lock);
8403
8404         arp_direct_ops = (struct neigh_ops*)
8405                 kmalloc(sizeof(struct neigh_ops), GFP_KERNEL);
8406         if (!arp_direct_ops)
8407                 return -ENOMEM;
8408
8409         memcpy(arp_direct_ops, &arp_direct_ops_template,
8410                sizeof(struct neigh_ops));
8411
8412         return 0;
8413 }
8414
8415 static void
8416 qeth_ipv6_uninit(void)
8417 {
8418         write_lock(&arp_tbl.lock);
8419         arp_tbl.constructor = qeth_old_arp_constructor;
8420         write_unlock(&arp_tbl.lock);
8421         kfree(arp_direct_ops);
8422 }
8423 #endif /* CONFIG_QETH_IPV6 */
8424
8425 static void
8426 qeth_sysfs_unregister(void)
8427 {
8428         qeth_remove_driver_attributes();
8429         ccw_driver_unregister(&qeth_ccw_driver);
8430         ccwgroup_driver_unregister(&qeth_ccwgroup_driver);
8431         s390_root_dev_unregister(qeth_root_dev);
8432 }
8433 /**
8434  * register qeth at sysfs
8435  */
8436 static int
8437 qeth_sysfs_register(void)
8438 {
8439         int rc=0;
8440
8441         rc = ccwgroup_driver_register(&qeth_ccwgroup_driver);
8442         if (rc)
8443                 return rc;
8444         rc = ccw_driver_register(&qeth_ccw_driver);
8445         if (rc)
8446                 return rc;
8447         rc = qeth_create_driver_attributes();
8448         if (rc)
8449                 return rc;
8450         qeth_root_dev = s390_root_dev_register("qeth");
8451         if (IS_ERR(qeth_root_dev)) {
8452                 rc = PTR_ERR(qeth_root_dev);
8453                 return rc;
8454         }
8455         return 0;
8456 }
8457
8458 /***
8459  * init function
8460  */
8461 static int __init
8462 qeth_init(void)
8463 {
8464         int rc=0;
8465
8466         PRINT_INFO("loading %s (%s/%s/%s/%s/%s/%s/%s %s %s)\n",
8467                    version, VERSION_QETH_C, VERSION_QETH_H,
8468                    VERSION_QETH_MPC_H, VERSION_QETH_MPC_C,
8469                    VERSION_QETH_FS_H, VERSION_QETH_PROC_C,
8470                    VERSION_QETH_SYS_C, QETH_VERSION_IPV6,
8471                    QETH_VERSION_VLAN);
8472
8473         INIT_LIST_HEAD(&qeth_card_list.list);
8474         INIT_LIST_HEAD(&qeth_notify_list);
8475         spin_lock_init(&qeth_notify_lock);
8476         rwlock_init(&qeth_card_list.rwlock);
8477
8478         if (qeth_register_dbf_views())
8479                 goto out_err;
8480         if (qeth_sysfs_register())
8481                 goto out_sysfs;
8482
8483 #ifdef CONFIG_QETH_IPV6
8484         if (qeth_ipv6_init()) {
8485                 PRINT_ERR("Out of memory during ipv6 init.\n");
8486                 goto out_sysfs;
8487         }
8488 #endif /* QETH_IPV6 */
8489         if (qeth_register_notifiers())
8490                 goto out_ipv6;
8491         if (qeth_create_procfs_entries())
8492                 goto out_notifiers;
8493
8494         return rc;
8495
8496 out_notifiers:
8497         qeth_unregister_notifiers();
8498 out_ipv6:
8499 #ifdef CONFIG_QETH_IPV6
8500         qeth_ipv6_uninit();
8501 #endif /* QETH_IPV6 */
8502 out_sysfs:
8503         qeth_sysfs_unregister();
8504         qeth_unregister_dbf_views();
8505 out_err:
8506         PRINT_ERR("Initialization failed");
8507         return rc;
8508 }
8509
8510 static void
8511 __exit qeth_exit(void)
8512 {
8513         struct qeth_card *card, *tmp;
8514         unsigned long flags;
8515
8516         QETH_DBF_TEXT(trace,1, "cleanup.");
8517
8518         /*
8519          * Weed would not need to clean up our devices here, because the
8520          * common device layer calls qeth_remove_device for each device
8521          * as soon as we unregister our driver (done in qeth_sysfs_unregister).
8522          * But we do cleanup here so we can do a "soft" shutdown of our cards.
8523          * qeth_remove_device called by the common device layer would otherwise
8524          * do a "hard" shutdown (card->use_hard_stop is set to one in
8525          * qeth_remove_device).
8526          */
8527 again:
8528         read_lock_irqsave(&qeth_card_list.rwlock, flags);
8529         list_for_each_entry_safe(card, tmp, &qeth_card_list.list, list){
8530                 read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
8531                 qeth_set_offline(card->gdev);
8532                 qeth_remove_device(card->gdev);
8533                 goto again;
8534         }
8535         read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
8536 #ifdef CONFIG_QETH_IPV6
8537         qeth_ipv6_uninit();
8538 #endif
8539         qeth_unregister_notifiers();
8540         qeth_remove_procfs_entries();
8541         qeth_sysfs_unregister();
8542         qeth_unregister_dbf_views();
8543         printk("qeth: removed\n");
8544 }
8545
8546 EXPORT_SYMBOL(qeth_osn_register);
8547 EXPORT_SYMBOL(qeth_osn_deregister);
8548 EXPORT_SYMBOL(qeth_osn_assist);
8549 module_init(qeth_init);
8550 module_exit(qeth_exit);
8551 MODULE_AUTHOR("Frank Pavlic <pavlic@de.ibm.com>");
8552 MODULE_DESCRIPTION("Linux on zSeries OSA Express and HiperSockets support\n" \
8553                                       "Copyright 2000,2003 IBM Corporation\n");
8554
8555 MODULE_LICENSE("GPL");