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