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