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