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