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