[PATCH] s390: qeth :allow setting of attribute "route6" to "no_router".
[linux-2.6.git] / drivers / s390 / net / qeth_main.c
1 /*
2  * linux/drivers/s390/net/qeth_main.c
3  *
4  * Linux on zSeries OSA Express and HiperSockets support
5  *
6  * Copyright 2000,2003 IBM Corporation
7  *
8  *    Author(s): Original Code written by
9  *                        Utz Bacher (utz.bacher@de.ibm.com)
10  *               Rewritten by
11  *                        Frank Pavlic (fpavlic@de.ibm.com) and
12  *                        Thomas Spatzier <tspat@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/string.h>
34 #include <linux/errno.h>
35 #include <linux/mm.h>
36 #include <linux/ip.h>
37 #include <linux/inetdevice.h>
38 #include <linux/netdevice.h>
39 #include <linux/sched.h>
40 #include <linux/workqueue.h>
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/interrupt.h>
44 #include <linux/tcp.h>
45 #include <linux/icmp.h>
46 #include <linux/skbuff.h>
47 #include <linux/in.h>
48 #include <linux/igmp.h>
49 #include <linux/init.h>
50 #include <linux/reboot.h>
51 #include <linux/mii.h>
52 #include <linux/rcupdate.h>
53 #include <linux/ethtool.h>
54
55 #include <net/arp.h>
56 #include <net/ip.h>
57 #include <net/route.h>
58
59 #include <asm/ebcdic.h>
60 #include <asm/io.h>
61 #include <asm/qeth.h>
62 #include <asm/timex.h>
63 #include <asm/semaphore.h>
64 #include <asm/uaccess.h>
65 #include <asm/s390_rdev.h>
66
67 #include "qeth.h"
68 #include "qeth_mpc.h"
69 #include "qeth_fs.h"
70 #include "qeth_eddp.h"
71 #include "qeth_tso.h"
72
73 static const char *version = "qeth S/390 OSA-Express driver";
74
75 /**
76  * Debug Facility Stuff
77  */
78 static debug_info_t *qeth_dbf_setup = NULL;
79 static debug_info_t *qeth_dbf_data = NULL;
80 static debug_info_t *qeth_dbf_misc = NULL;
81 static debug_info_t *qeth_dbf_control = NULL;
82 debug_info_t *qeth_dbf_trace = NULL;
83 static debug_info_t *qeth_dbf_sense = NULL;
84 static debug_info_t *qeth_dbf_qerr = NULL;
85
86 DEFINE_PER_CPU(char[256], qeth_dbf_txt_buf);
87
88 /**
89  * some more definitions and declarations
90  */
91 static unsigned int known_devices[][10] = QETH_MODELLIST_ARRAY;
92
93 /* list of our cards */
94 struct qeth_card_list_struct qeth_card_list;
95 /*process list want to be notified*/
96 spinlock_t qeth_notify_lock;
97 struct list_head qeth_notify_list;
98
99 static void qeth_send_control_data_cb(struct qeth_channel *,
100                                       struct qeth_cmd_buffer *);
101
102 /**
103  * here we go with function implementation
104  */
105 static void
106 qeth_init_qdio_info(struct qeth_card *card);
107
108 static int
109 qeth_init_qdio_queues(struct qeth_card *card);
110
111 static int
112 qeth_alloc_qdio_buffers(struct qeth_card *card);
113
114 static void
115 qeth_free_qdio_buffers(struct qeth_card *);
116
117 static void
118 qeth_clear_qdio_buffers(struct qeth_card *);
119
120 static void
121 qeth_clear_ip_list(struct qeth_card *, int, int);
122
123 static void
124 qeth_clear_ipacmd_list(struct qeth_card *);
125
126 static int
127 qeth_qdio_clear_card(struct qeth_card *, int);
128
129 static void
130 qeth_clear_working_pool_list(struct qeth_card *);
131
132 static void
133 qeth_clear_cmd_buffers(struct qeth_channel *);
134
135 static int
136 qeth_stop(struct net_device *);
137
138 static void
139 qeth_clear_ipato_list(struct qeth_card *);
140
141 static int
142 qeth_is_addr_covered_by_ipato(struct qeth_card *, struct qeth_ipaddr *);
143
144 static void
145 qeth_irq_tasklet(unsigned long);
146
147 static int
148 qeth_set_online(struct ccwgroup_device *);
149
150 static int
151 __qeth_set_online(struct ccwgroup_device *gdev, int recovery_mode);
152
153 static struct qeth_ipaddr *
154 qeth_get_addr_buffer(enum qeth_prot_versions);
155
156 static void
157 qeth_set_multicast_list(struct net_device *);
158
159 static void
160 qeth_setadp_promisc_mode(struct qeth_card *);
161
162 static void
163 qeth_notify_processes(void)
164 {
165         /*notify all  registered processes */
166         struct qeth_notify_list_struct *n_entry;
167
168         QETH_DBF_TEXT(trace,3,"procnoti");
169         spin_lock(&qeth_notify_lock);
170         list_for_each_entry(n_entry, &qeth_notify_list, list) {
171                 send_sig(n_entry->signum, n_entry->task, 1);
172         }
173         spin_unlock(&qeth_notify_lock);
174
175 }
176 int
177 qeth_notifier_unregister(struct task_struct *p)
178 {
179         struct qeth_notify_list_struct *n_entry, *tmp;
180
181         QETH_DBF_TEXT(trace, 2, "notunreg");
182         spin_lock(&qeth_notify_lock);
183         list_for_each_entry_safe(n_entry, tmp, &qeth_notify_list, list) {
184                 if (n_entry->task == p) {
185                         list_del(&n_entry->list);
186                         kfree(n_entry);
187                         goto out;
188                 }
189         }
190 out:
191         spin_unlock(&qeth_notify_lock);
192         return 0;
193 }
194 int
195 qeth_notifier_register(struct task_struct *p, int signum)
196 {
197         struct qeth_notify_list_struct *n_entry;
198
199         /*check first if entry already exists*/
200         spin_lock(&qeth_notify_lock);
201         list_for_each_entry(n_entry, &qeth_notify_list, list) {
202                 if (n_entry->task == p) {
203                         n_entry->signum = signum;
204                         spin_unlock(&qeth_notify_lock);
205                         return 0;
206                 }
207         }
208         spin_unlock(&qeth_notify_lock);
209
210         n_entry = (struct qeth_notify_list_struct *)
211                 kmalloc(sizeof(struct qeth_notify_list_struct),GFP_KERNEL);
212         if (!n_entry)
213                 return -ENOMEM;
214         n_entry->task = p;
215         n_entry->signum = signum;
216         spin_lock(&qeth_notify_lock);
217         list_add(&n_entry->list,&qeth_notify_list);
218         spin_unlock(&qeth_notify_lock);
219         return 0;
220 }
221
222
223 /**
224  * free channel command buffers
225  */
226 static void
227 qeth_clean_channel(struct qeth_channel *channel)
228 {
229         int cnt;
230
231         QETH_DBF_TEXT(setup, 2, "freech");
232         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
233                 kfree(channel->iob[cnt].data);
234 }
235
236 /**
237  * free card
238  */
239 static void
240 qeth_free_card(struct qeth_card *card)
241 {
242
243         QETH_DBF_TEXT(setup, 2, "freecrd");
244         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
245         qeth_clean_channel(&card->read);
246         qeth_clean_channel(&card->write);
247         if (card->dev)
248                 free_netdev(card->dev);
249         qeth_clear_ip_list(card, 0, 0);
250         qeth_clear_ipato_list(card);
251         kfree(card->ip_tbd_list);
252         qeth_free_qdio_buffers(card);
253         kfree(card);
254 }
255
256 /**
257  * alloc memory for command buffer per channel
258  */
259 static int
260 qeth_setup_channel(struct qeth_channel *channel)
261 {
262         int cnt;
263
264         QETH_DBF_TEXT(setup, 2, "setupch");
265         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
266                 channel->iob[cnt].data = (char *)
267                         kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
268                 if (channel->iob[cnt].data == NULL)
269                         break;
270                 channel->iob[cnt].state = BUF_STATE_FREE;
271                 channel->iob[cnt].channel = channel;
272                 channel->iob[cnt].callback = qeth_send_control_data_cb;
273                 channel->iob[cnt].rc = 0;
274         }
275         if (cnt < QETH_CMD_BUFFER_NO) {
276                 while (cnt-- > 0)
277                         kfree(channel->iob[cnt].data);
278                 return -ENOMEM;
279         }
280         channel->buf_no = 0;
281         channel->io_buf_no = 0;
282         atomic_set(&channel->irq_pending, 0);
283         spin_lock_init(&channel->iob_lock);
284
285         init_waitqueue_head(&channel->wait_q);
286         channel->irq_tasklet.data = (unsigned long) channel;
287         channel->irq_tasklet.func = qeth_irq_tasklet;
288         return 0;
289 }
290
291 /**
292  * alloc memory for card structure
293  */
294 static struct qeth_card *
295 qeth_alloc_card(void)
296 {
297         struct qeth_card *card;
298
299         QETH_DBF_TEXT(setup, 2, "alloccrd");
300         card = (struct qeth_card *) kmalloc(sizeof(struct qeth_card),
301                                             GFP_DMA|GFP_KERNEL);
302         if (!card)
303                 return NULL;
304         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
305         memset(card, 0, sizeof(struct qeth_card));
306         if (qeth_setup_channel(&card->read)) {
307                 kfree(card);
308                 return NULL;
309         }
310         if (qeth_setup_channel(&card->write)) {
311                 qeth_clean_channel(&card->read);
312                 kfree(card);
313                 return NULL;
314         }
315         return card;
316 }
317
318 static long
319 __qeth_check_irb_error(struct ccw_device *cdev, struct irb *irb)
320 {
321         if (!IS_ERR(irb))
322                 return 0;
323
324         switch (PTR_ERR(irb)) {
325         case -EIO:
326                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
327                 QETH_DBF_TEXT(trace, 2, "ckirberr");
328                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
329                 break;
330         case -ETIMEDOUT:
331                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
332                 QETH_DBF_TEXT(trace, 2, "ckirberr");
333                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -ETIMEDOUT);
334                 break;
335         default:
336                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
337                            cdev->dev.bus_id);
338                 QETH_DBF_TEXT(trace, 2, "ckirberr");
339                 QETH_DBF_TEXT(trace, 2, "  rc???");
340         }
341         return PTR_ERR(irb);
342 }
343
344 static int
345 qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
346 {
347         int dstat,cstat;
348         char *sense;
349
350         sense = (char *) irb->ecw;
351         cstat = irb->scsw.cstat;
352         dstat = irb->scsw.dstat;
353
354         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
355                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
356                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
357                 QETH_DBF_TEXT(trace,2, "CGENCHK");
358                 PRINT_WARN("check on device %s, dstat=x%x, cstat=x%x ",
359                            cdev->dev.bus_id, dstat, cstat);
360                 HEXDUMP16(WARN, "irb: ", irb);
361                 HEXDUMP16(WARN, "irb: ", ((char *) irb) + 32);
362                 return 1;
363         }
364
365         if (dstat & DEV_STAT_UNIT_CHECK) {
366                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
367                     SENSE_RESETTING_EVENT_FLAG) {
368                         QETH_DBF_TEXT(trace,2,"REVIND");
369                         return 1;
370                 }
371                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
372                     SENSE_COMMAND_REJECT_FLAG) {
373                         QETH_DBF_TEXT(trace,2,"CMDREJi");
374                         return 0;
375                 }
376                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
377                         QETH_DBF_TEXT(trace,2,"AFFE");
378                         return 1;
379                 }
380                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
381                         QETH_DBF_TEXT(trace,2,"ZEROSEN");
382                         return 0;
383                 }
384                 QETH_DBF_TEXT(trace,2,"DGENCHK");
385                         return 1;
386         }
387         return 0;
388 }
389 static int qeth_issue_next_read(struct qeth_card *);
390
391 /**
392  * interrupt handler
393  */
394 static void
395 qeth_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
396 {
397         int rc;
398         int cstat,dstat;
399         struct qeth_cmd_buffer *buffer;
400         struct qeth_channel *channel;
401         struct qeth_card *card;
402
403         QETH_DBF_TEXT(trace,5,"irq");
404
405         if (__qeth_check_irb_error(cdev, irb))
406                 return;
407         cstat = irb->scsw.cstat;
408         dstat = irb->scsw.dstat;
409
410         card = CARD_FROM_CDEV(cdev);
411         if (!card)
412                 return;
413
414         if (card->read.ccwdev == cdev){
415                 channel = &card->read;
416                 QETH_DBF_TEXT(trace,5,"read");
417         } else if (card->write.ccwdev == cdev) {
418                 channel = &card->write;
419                 QETH_DBF_TEXT(trace,5,"write");
420         } else {
421                 channel = &card->data;
422                 QETH_DBF_TEXT(trace,5,"data");
423         }
424         atomic_set(&channel->irq_pending, 0);
425
426         if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC))
427                 channel->state = CH_STATE_STOPPED;
428
429         if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC))
430                 channel->state = CH_STATE_HALTED;
431
432         /*let's wake up immediately on data channel*/
433         if ((channel == &card->data) && (intparm != 0))
434                 goto out;
435
436         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
437                 QETH_DBF_TEXT(trace, 6, "clrchpar");
438                 /* we don't have to handle this further */
439                 intparm = 0;
440         }
441         if (intparm == QETH_HALT_CHANNEL_PARM) {
442                 QETH_DBF_TEXT(trace, 6, "hltchpar");
443                 /* we don't have to handle this further */
444                 intparm = 0;
445         }
446         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
447             (dstat & DEV_STAT_UNIT_CHECK) ||
448             (cstat)) {
449                 if (irb->esw.esw0.erw.cons) {
450                         /* TODO: we should make this s390dbf */
451                         PRINT_WARN("sense data available on channel %s.\n",
452                                    CHANNEL_ID(channel));
453                         PRINT_WARN(" cstat 0x%X\n dstat 0x%X\n", cstat, dstat);
454                         HEXDUMP16(WARN,"irb: ",irb);
455                         HEXDUMP16(WARN,"sense data: ",irb->ecw);
456                 }
457                 rc = qeth_get_problem(cdev,irb);
458                 if (rc) {
459                         qeth_schedule_recovery(card);
460                         goto out;
461                 }
462         }
463
464         if (intparm) {
465                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
466                 buffer->state = BUF_STATE_PROCESSED;
467         }
468         if (channel == &card->data)
469                 return;
470
471         if (channel == &card->read &&
472             channel->state == CH_STATE_UP)
473                 qeth_issue_next_read(card);
474
475         tasklet_schedule(&channel->irq_tasklet);
476         return;
477 out:
478         wake_up(&card->wait_q);
479 }
480
481 /**
482  * tasklet function scheduled from irq handler
483  */
484 static void
485 qeth_irq_tasklet(unsigned long data)
486 {
487         struct qeth_card *card;
488         struct qeth_channel *channel;
489         struct qeth_cmd_buffer *iob;
490         __u8 index;
491
492         QETH_DBF_TEXT(trace,5,"irqtlet");
493         channel = (struct qeth_channel *) data;
494         iob = channel->iob;
495         index = channel->buf_no;
496         card = CARD_FROM_CDEV(channel->ccwdev);
497         while (iob[index].state == BUF_STATE_PROCESSED) {
498                 if (iob[index].callback !=NULL) {
499                         iob[index].callback(channel,iob + index);
500                 }
501                 index = (index + 1) % QETH_CMD_BUFFER_NO;
502         }
503         channel->buf_no = index;
504         wake_up(&card->wait_q);
505 }
506
507 static int qeth_stop_card(struct qeth_card *, int);
508
509 static int
510 __qeth_set_offline(struct ccwgroup_device *cgdev, int recovery_mode)
511 {
512         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
513         int rc = 0, rc2 = 0, rc3 = 0;
514         enum qeth_card_states recover_flag;
515
516         QETH_DBF_TEXT(setup, 3, "setoffl");
517         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
518         
519         if (card->dev && netif_carrier_ok(card->dev))
520                 netif_carrier_off(card->dev);
521         recover_flag = card->state;
522         if (qeth_stop_card(card, recovery_mode) == -ERESTARTSYS){
523                 PRINT_WARN("Stopping card %s interrupted by user!\n",
524                            CARD_BUS_ID(card));
525                 return -ERESTARTSYS;
526         }
527         rc  = ccw_device_set_offline(CARD_DDEV(card));
528         rc2 = ccw_device_set_offline(CARD_WDEV(card));
529         rc3 = ccw_device_set_offline(CARD_RDEV(card));
530         if (!rc)
531                 rc = (rc2) ? rc2 : rc3;
532         if (rc)
533                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
534         if (recover_flag == CARD_STATE_UP)
535                 card->state = CARD_STATE_RECOVER;
536         qeth_notify_processes();
537         return 0;
538 }
539
540 static int
541 qeth_set_offline(struct ccwgroup_device *cgdev)
542 {
543         return  __qeth_set_offline(cgdev, 0);
544 }
545
546 static int
547 qeth_wait_for_threads(struct qeth_card *card, unsigned long threads);
548
549
550 static void
551 qeth_remove_device(struct ccwgroup_device *cgdev)
552 {
553         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
554         unsigned long flags;
555
556         QETH_DBF_TEXT(setup, 3, "rmdev");
557         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
558
559         if (!card)
560                 return;
561
562         if (qeth_wait_for_threads(card, 0xffffffff))
563                 return;
564
565         if (cgdev->state == CCWGROUP_ONLINE){
566                 card->use_hard_stop = 1;
567                 qeth_set_offline(cgdev);
568         }
569         /* remove form our internal list */
570         write_lock_irqsave(&qeth_card_list.rwlock, flags);
571         list_del(&card->list);
572         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
573         if (card->dev)
574                 unregister_netdev(card->dev);
575         qeth_remove_device_attributes(&cgdev->dev);
576         qeth_free_card(card);
577         cgdev->dev.driver_data = NULL;
578         put_device(&cgdev->dev);
579 }
580
581 static int
582 qeth_register_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
583 static int
584 qeth_deregister_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
585
586 /**
587  * Add/remove address to/from card's ip list, i.e. try to add or remove
588  * reference to/from an IP address that is already registered on the card.
589  * Returns:
590  *      0  address was on card and its reference count has been adjusted,
591  *         but is still > 0, so nothing has to be done
592  *         also returns 0 if card was not on card and the todo was to delete
593  *         the address -> there is also nothing to be done
594  *      1  address was not on card and the todo is to add it to the card's ip
595  *         list
596  *      -1 address was on card and its reference count has been decremented
597  *         to <= 0 by the todo -> address must be removed from card
598  */
599 static int
600 __qeth_ref_ip_on_card(struct qeth_card *card, struct qeth_ipaddr *todo,
601                       struct qeth_ipaddr **__addr)
602 {
603         struct qeth_ipaddr *addr;
604         int found = 0;
605
606         list_for_each_entry(addr, &card->ip_list, entry) {
607                 if (card->options.layer2) {
608                         if ((addr->type == todo->type) &&
609                             (memcmp(&addr->mac, &todo->mac, 
610                                     OSA_ADDR_LEN) == 0)) {
611                                 found = 1;
612                                 break;
613                         }
614                         continue;
615                 } 
616                 if ((addr->proto     == QETH_PROT_IPV4)  &&
617                     (todo->proto     == QETH_PROT_IPV4)  &&
618                     (addr->type      == todo->type)      &&
619                     (addr->u.a4.addr == todo->u.a4.addr) &&
620                     (addr->u.a4.mask == todo->u.a4.mask)) {
621                         found = 1;
622                         break;
623                 }
624                 if ((addr->proto       == QETH_PROT_IPV6)     &&
625                     (todo->proto       == QETH_PROT_IPV6)     &&
626                     (addr->type        == todo->type)         &&
627                     (addr->u.a6.pfxlen == todo->u.a6.pfxlen)  &&
628                     (memcmp(&addr->u.a6.addr, &todo->u.a6.addr,
629                             sizeof(struct in6_addr)) == 0)) {
630                         found = 1;
631                         break;
632                 }
633         }
634         if (found) {
635                 addr->users += todo->users;
636                 if (addr->users <= 0){
637                         *__addr = addr;
638                         return -1;
639                 } else {
640                         /* for VIPA and RXIP limit refcount to 1 */
641                         if (addr->type != QETH_IP_TYPE_NORMAL)
642                                 addr->users = 1;
643                         return 0;
644                 }
645         }
646         if (todo->users > 0) {
647                 /* for VIPA and RXIP limit refcount to 1 */
648                 if (todo->type != QETH_IP_TYPE_NORMAL)
649                         todo->users = 1;
650                 return 1;
651         } else
652                 return 0;
653 }
654
655 static inline int
656 __qeth_address_exists_in_list(struct list_head *list, struct qeth_ipaddr *addr,
657                               int same_type)
658 {
659         struct qeth_ipaddr *tmp;
660
661         list_for_each_entry(tmp, list, entry) {
662                 if ((tmp->proto     == QETH_PROT_IPV4)            &&
663                     (addr->proto    == QETH_PROT_IPV4)            &&
664                     ((same_type && (tmp->type == addr->type)) ||
665                      (!same_type && (tmp->type != addr->type))  ) &&
666                     (tmp->u.a4.addr == addr->u.a4.addr)             ){
667                         return 1;
668                 }
669                 if ((tmp->proto  == QETH_PROT_IPV6)               &&
670                     (addr->proto == QETH_PROT_IPV6)               &&
671                     ((same_type && (tmp->type == addr->type)) ||
672                      (!same_type && (tmp->type != addr->type))  ) &&
673                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
674                             sizeof(struct in6_addr)) == 0)          ) {
675                         return 1;
676                 }
677         }
678         return 0;
679 }
680
681 /*
682  * Add IP to be added to todo list. If there is already an "add todo"
683  * in this list we just incremenent the reference count.
684  * Returns 0 if we  just incremented reference count.
685  */
686 static int
687 __qeth_insert_ip_todo(struct qeth_card *card, struct qeth_ipaddr *addr, int add)
688 {
689         struct qeth_ipaddr *tmp, *t;
690         int found = 0;
691
692         list_for_each_entry_safe(tmp, t, card->ip_tbd_list, entry) {
693                 if ((addr->type == QETH_IP_TYPE_DEL_ALL_MC) &&
694                     (tmp->type == QETH_IP_TYPE_DEL_ALL_MC))
695                         return 0;
696                 if (card->options.layer2) {
697                         if ((tmp->type  == addr->type)  &&
698                             (tmp->is_multicast == addr->is_multicast) &&
699                             (memcmp(&tmp->mac, &addr->mac, 
700                                     OSA_ADDR_LEN) == 0)) {
701                                 found = 1;
702                                 break;
703                         }
704                         continue;
705                 }        
706                 if ((tmp->proto        == QETH_PROT_IPV4)     &&
707                     (addr->proto       == QETH_PROT_IPV4)     &&
708                     (tmp->type         == addr->type)         &&
709                     (tmp->is_multicast == addr->is_multicast) &&
710                     (tmp->u.a4.addr    == addr->u.a4.addr)    &&
711                     (tmp->u.a4.mask    == addr->u.a4.mask)) {
712                         found = 1;
713                         break;
714                 }
715                 if ((tmp->proto        == QETH_PROT_IPV6)      &&
716                     (addr->proto       == QETH_PROT_IPV6)      &&
717                     (tmp->type         == addr->type)          &&
718                     (tmp->is_multicast == addr->is_multicast)  &&
719                     (tmp->u.a6.pfxlen  == addr->u.a6.pfxlen)   &&
720                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
721                             sizeof(struct in6_addr)) == 0)) {
722                         found = 1;
723                         break;
724                 }
725         }
726         if (found){
727                 if (addr->users != 0)
728                         tmp->users += addr->users;
729                 else
730                         tmp->users += add? 1:-1;
731                 if (tmp->users == 0) {
732                         list_del(&tmp->entry);
733                         kfree(tmp);
734                 }
735                 return 0;
736         } else {
737                 if (addr->type == QETH_IP_TYPE_DEL_ALL_MC)
738                         list_add(&addr->entry, card->ip_tbd_list);
739                 else {
740                         if (addr->users == 0)
741                                 addr->users += add? 1:-1;
742                         if (add && (addr->type == QETH_IP_TYPE_NORMAL) &&
743                             qeth_is_addr_covered_by_ipato(card, addr)){
744                                 QETH_DBF_TEXT(trace, 2, "tkovaddr");
745                                 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
746                         }
747                         list_add_tail(&addr->entry, card->ip_tbd_list);
748                 }
749                 return 1;
750         }
751 }
752
753 /**
754  * Remove IP address from list
755  */
756 static int
757 qeth_delete_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
758 {
759         unsigned long flags;
760         int rc = 0;
761
762         QETH_DBF_TEXT(trace, 4, "delip");
763
764         if (card->options.layer2)
765                 QETH_DBF_HEX(trace, 4, &addr->mac, 6);
766         else if (addr->proto == QETH_PROT_IPV4)
767                 QETH_DBF_HEX(trace, 4, &addr->u.a4.addr, 4);
768         else {
769                 QETH_DBF_HEX(trace, 4, &addr->u.a6.addr, 8);
770                 QETH_DBF_HEX(trace, 4, ((char *)&addr->u.a6.addr) + 8, 8);
771         }
772         spin_lock_irqsave(&card->ip_lock, flags);
773         rc = __qeth_insert_ip_todo(card, addr, 0);
774         spin_unlock_irqrestore(&card->ip_lock, flags);
775         return rc;
776 }
777
778 static int
779 qeth_add_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
780 {
781         unsigned long flags;
782         int rc = 0;
783
784         QETH_DBF_TEXT(trace, 4, "addip");
785         if (card->options.layer2)
786                 QETH_DBF_HEX(trace, 4, &addr->mac, 6);
787         else if (addr->proto == QETH_PROT_IPV4)
788                 QETH_DBF_HEX(trace, 4, &addr->u.a4.addr, 4);
789         else {
790                 QETH_DBF_HEX(trace, 4, &addr->u.a6.addr, 8);
791                 QETH_DBF_HEX(trace, 4, ((char *)&addr->u.a6.addr) + 8, 8);
792         }
793         spin_lock_irqsave(&card->ip_lock, flags);
794         rc = __qeth_insert_ip_todo(card, addr, 1);
795         spin_unlock_irqrestore(&card->ip_lock, flags);
796         return rc;
797 }
798
799 static inline void
800 __qeth_delete_all_mc(struct qeth_card *card, unsigned long *flags)
801 {
802         struct qeth_ipaddr *addr, *tmp;
803         int rc;
804 again:
805         list_for_each_entry_safe(addr, tmp, &card->ip_list, entry) {
806                 if (addr->is_multicast) {
807                         spin_unlock_irqrestore(&card->ip_lock, *flags);
808                         rc = qeth_deregister_addr_entry(card, addr);
809                         spin_lock_irqsave(&card->ip_lock, *flags);
810                         if (!rc) {
811                                 list_del(&addr->entry);
812                                 kfree(addr);
813                                 goto again;
814                         }
815                 }
816         }
817 }
818
819 static void
820 qeth_set_ip_addr_list(struct qeth_card *card)
821 {
822         struct list_head *tbd_list;
823         struct qeth_ipaddr *todo, *addr;
824         unsigned long flags;
825         int rc;
826
827         QETH_DBF_TEXT(trace, 2, "sdiplist");
828         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
829
830         spin_lock_irqsave(&card->ip_lock, flags);
831         tbd_list = card->ip_tbd_list;
832         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
833         if (!card->ip_tbd_list) {
834                 QETH_DBF_TEXT(trace, 0, "silnomem");
835                 card->ip_tbd_list = tbd_list;
836                 spin_unlock_irqrestore(&card->ip_lock, flags);
837                 return;
838         } else
839                 INIT_LIST_HEAD(card->ip_tbd_list);
840
841         while (!list_empty(tbd_list)){
842                 todo = list_entry(tbd_list->next, struct qeth_ipaddr, entry);
843                 list_del(&todo->entry);
844                 if (todo->type == QETH_IP_TYPE_DEL_ALL_MC){
845                         __qeth_delete_all_mc(card, &flags);
846                         kfree(todo);
847                         continue;
848                 }
849                 rc = __qeth_ref_ip_on_card(card, todo, &addr);
850                 if (rc == 0) {
851                         /* nothing to be done; only adjusted refcount */
852                         kfree(todo);
853                 } else if (rc == 1) {
854                         /* new entry to be added to on-card list */
855                         spin_unlock_irqrestore(&card->ip_lock, flags);
856                         rc = qeth_register_addr_entry(card, todo);
857                         spin_lock_irqsave(&card->ip_lock, flags);
858                         if (!rc)
859                                 list_add_tail(&todo->entry, &card->ip_list);
860                         else
861                                 kfree(todo);
862                 } else if (rc == -1) {
863                         /* on-card entry to be removed */
864                         list_del_init(&addr->entry);
865                         spin_unlock_irqrestore(&card->ip_lock, flags);
866                         rc = qeth_deregister_addr_entry(card, addr);
867                         spin_lock_irqsave(&card->ip_lock, flags);
868                         if (!rc)
869                                 kfree(addr);
870                         else
871                                 list_add_tail(&addr->entry, &card->ip_list);
872                         kfree(todo);
873                 }
874         }
875         spin_unlock_irqrestore(&card->ip_lock, flags);
876         kfree(tbd_list);
877 }
878
879 static void qeth_delete_mc_addresses(struct qeth_card *);
880 static void qeth_add_multicast_ipv4(struct qeth_card *);
881 static void qeth_layer2_add_multicast(struct qeth_card *);
882 #ifdef CONFIG_QETH_IPV6
883 static void qeth_add_multicast_ipv6(struct qeth_card *);
884 #endif
885
886 static inline int
887 qeth_set_thread_start_bit(struct qeth_card *card, unsigned long thread)
888 {
889         unsigned long flags;
890
891         spin_lock_irqsave(&card->thread_mask_lock, flags);
892         if ( !(card->thread_allowed_mask & thread) ||
893               (card->thread_start_mask & thread) ) {
894                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
895                 return -EPERM;
896         }
897         card->thread_start_mask |= thread;
898         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
899         return 0;
900 }
901
902 static void
903 qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
904 {
905         unsigned long flags;
906
907         spin_lock_irqsave(&card->thread_mask_lock, flags);
908         card->thread_start_mask &= ~thread;
909         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
910         wake_up(&card->wait_q);
911 }
912
913 static void
914 qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
915 {
916         unsigned long flags;
917
918         spin_lock_irqsave(&card->thread_mask_lock, flags);
919         card->thread_running_mask &= ~thread;
920         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
921         wake_up(&card->wait_q);
922 }
923
924 static inline int
925 __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
926 {
927         unsigned long flags;
928         int rc = 0;
929
930         spin_lock_irqsave(&card->thread_mask_lock, flags);
931         if (card->thread_start_mask & thread){
932                 if ((card->thread_allowed_mask & thread) &&
933                     !(card->thread_running_mask & thread)){
934                         rc = 1;
935                         card->thread_start_mask &= ~thread;
936                         card->thread_running_mask |= thread;
937                 } else
938                         rc = -EPERM;
939         }
940         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
941         return rc;
942 }
943
944 static int
945 qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
946 {
947         int rc = 0;
948
949         wait_event(card->wait_q,
950                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
951         return rc;
952 }
953
954 static int
955 qeth_register_ip_addresses(void *ptr)
956 {
957         struct qeth_card *card;
958
959         card = (struct qeth_card *) ptr;
960         daemonize("qeth_reg_ip");
961         QETH_DBF_TEXT(trace,4,"regipth1");
962         if (!qeth_do_run_thread(card, QETH_SET_IP_THREAD))
963                 return 0;
964         QETH_DBF_TEXT(trace,4,"regipth2");
965         qeth_set_ip_addr_list(card);
966         qeth_clear_thread_running_bit(card, QETH_SET_IP_THREAD);
967         return 0;
968 }
969
970 /*
971  * Drive the SET_PROMISC_MODE thread
972  */
973 static int
974 qeth_set_promisc_mode(void *ptr)
975 {
976         struct qeth_card *card = (struct qeth_card *) ptr;
977
978         daemonize("qeth_setprm");
979         QETH_DBF_TEXT(trace,4,"setprm1");
980         if (!qeth_do_run_thread(card, QETH_SET_PROMISC_MODE_THREAD))
981                 return 0;
982         QETH_DBF_TEXT(trace,4,"setprm2");
983         qeth_setadp_promisc_mode(card);
984         qeth_clear_thread_running_bit(card, QETH_SET_PROMISC_MODE_THREAD);
985         return 0;
986 }
987
988 static int
989 qeth_recover(void *ptr)
990 {
991         struct qeth_card *card;
992         int rc = 0;
993
994         card = (struct qeth_card *) ptr;
995         daemonize("qeth_recover");
996         QETH_DBF_TEXT(trace,2,"recover1");
997         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
998         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
999                 return 0;
1000         QETH_DBF_TEXT(trace,2,"recover2");
1001         PRINT_WARN("Recovery of device %s started ...\n",
1002                    CARD_BUS_ID(card));
1003         card->use_hard_stop = 1;
1004         __qeth_set_offline(card->gdev,1);
1005         rc = __qeth_set_online(card->gdev,1);
1006         if (!rc)
1007                 PRINT_INFO("Device %s successfully recovered!\n",
1008                            CARD_BUS_ID(card));
1009         else
1010                 PRINT_INFO("Device %s could not be recovered!\n",
1011                            CARD_BUS_ID(card));
1012         /* don't run another scheduled recovery */
1013         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1014         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
1015         return 0;
1016 }
1017
1018 void
1019 qeth_schedule_recovery(struct qeth_card *card)
1020 {
1021         QETH_DBF_TEXT(trace,2,"startrec");
1022         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
1023                 schedule_work(&card->kernel_thread_starter);
1024 }
1025
1026 static int
1027 qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1028 {
1029         unsigned long flags;
1030         int rc = 0;
1031
1032         spin_lock_irqsave(&card->thread_mask_lock, flags);
1033         QETH_DBF_TEXT_(trace, 4, "  %02x%02x%02x",
1034                         (u8) card->thread_start_mask,
1035                         (u8) card->thread_allowed_mask,
1036                         (u8) card->thread_running_mask);
1037         rc = (card->thread_start_mask & thread);
1038         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1039         return rc;
1040 }
1041
1042 static void
1043 qeth_start_kernel_thread(struct qeth_card *card)
1044 {
1045         QETH_DBF_TEXT(trace , 2, "strthrd");
1046
1047         if (card->read.state != CH_STATE_UP &&
1048             card->write.state != CH_STATE_UP)
1049                 return;
1050
1051         if (qeth_do_start_thread(card, QETH_SET_IP_THREAD))
1052                 kernel_thread(qeth_register_ip_addresses, (void *)card,SIGCHLD);
1053         if (qeth_do_start_thread(card, QETH_SET_PROMISC_MODE_THREAD))
1054                 kernel_thread(qeth_set_promisc_mode, (void *)card, SIGCHLD);
1055         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1056                 kernel_thread(qeth_recover, (void *) card, SIGCHLD);
1057 }
1058
1059
1060 static void
1061 qeth_set_intial_options(struct qeth_card *card)
1062 {
1063         card->options.route4.type = NO_ROUTER;
1064 #ifdef CONFIG_QETH_IPV6
1065         card->options.route6.type = NO_ROUTER;
1066 #endif /* QETH_IPV6 */
1067         card->options.checksum_type = QETH_CHECKSUM_DEFAULT;
1068         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1069         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1070         card->options.fake_broadcast = 0;
1071         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1072         card->options.fake_ll = 0;
1073         if (card->info.type == QETH_CARD_TYPE_OSN)
1074                 card->options.layer2 = 1;
1075         else
1076                 card->options.layer2 = 0;
1077 }
1078
1079 /**
1080  * initialize channels ,card and all state machines
1081  */
1082 static int
1083 qeth_setup_card(struct qeth_card *card)
1084 {
1085
1086         QETH_DBF_TEXT(setup, 2, "setupcrd");
1087         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
1088
1089         card->read.state  = CH_STATE_DOWN;
1090         card->write.state = CH_STATE_DOWN;
1091         card->data.state  = CH_STATE_DOWN;
1092         card->state = CARD_STATE_DOWN;
1093         card->lan_online = 0;
1094         card->use_hard_stop = 0;
1095         card->dev = NULL;
1096 #ifdef CONFIG_QETH_VLAN
1097         spin_lock_init(&card->vlanlock);
1098         card->vlangrp = NULL;
1099 #endif
1100         spin_lock_init(&card->lock);
1101         spin_lock_init(&card->ip_lock);
1102         spin_lock_init(&card->thread_mask_lock);
1103         card->thread_start_mask = 0;
1104         card->thread_allowed_mask = 0;
1105         card->thread_running_mask = 0;
1106         INIT_WORK(&card->kernel_thread_starter,
1107                   (void *)qeth_start_kernel_thread,card);
1108         INIT_LIST_HEAD(&card->ip_list);
1109         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1110         if (!card->ip_tbd_list) {
1111                 QETH_DBF_TEXT(setup, 0, "iptbdnom");
1112                 return -ENOMEM;
1113         }
1114         INIT_LIST_HEAD(card->ip_tbd_list);
1115         INIT_LIST_HEAD(&card->cmd_waiter_list);
1116         init_waitqueue_head(&card->wait_q);
1117         /* intial options */
1118         qeth_set_intial_options(card);
1119         /* IP address takeover */
1120         INIT_LIST_HEAD(&card->ipato.entries);
1121         card->ipato.enabled = 0;
1122         card->ipato.invert4 = 0;
1123         card->ipato.invert6 = 0;
1124         /* init QDIO stuff */
1125         qeth_init_qdio_info(card);
1126         return 0;
1127 }
1128
1129 static int
1130 is_1920_device (struct qeth_card *card)
1131 {
1132         int single_queue = 0;
1133         struct ccw_device *ccwdev;
1134         struct channelPath_dsc {
1135                 u8 flags;
1136                 u8 lsn;
1137                 u8 desc;
1138                 u8 chpid;
1139                 u8 swla;
1140                 u8 zeroes;
1141                 u8 chla;
1142                 u8 chpp;
1143         } *chp_dsc;
1144
1145         QETH_DBF_TEXT(setup, 2, "chk_1920");
1146
1147         ccwdev = card->data.ccwdev;
1148         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1149         if (chp_dsc != NULL) {
1150                 /* CHPP field bit 6 == 1 -> single queue */
1151                 single_queue = ((chp_dsc->chpp & 0x02) == 0x02);
1152                 kfree(chp_dsc);
1153         }
1154         QETH_DBF_TEXT_(setup, 2, "rc:%x", single_queue);
1155         return single_queue;
1156 }
1157
1158 static int
1159 qeth_determine_card_type(struct qeth_card *card)
1160 {
1161         int i = 0;
1162
1163         QETH_DBF_TEXT(setup, 2, "detcdtyp");
1164
1165         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1166         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1167         while (known_devices[i][4]) {
1168                 if ((CARD_RDEV(card)->id.dev_type == known_devices[i][2]) &&
1169                     (CARD_RDEV(card)->id.dev_model == known_devices[i][3])) {
1170                         card->info.type = known_devices[i][4];
1171                         card->qdio.no_out_queues = known_devices[i][8];
1172                         card->info.is_multicast_different = known_devices[i][9];
1173                         if (is_1920_device(card)) {
1174                                 PRINT_INFO("Priority Queueing not able "
1175                                            "due to hardware limitations!\n");
1176                                 card->qdio.no_out_queues = 1;
1177                                 card->qdio.default_out_queue = 0;
1178                         } 
1179                         return 0;
1180                 }
1181                 i++;
1182         }
1183         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1184         PRINT_ERR("unknown card type on device %s\n", CARD_BUS_ID(card));
1185         return -ENOENT;
1186 }
1187
1188 static int
1189 qeth_probe_device(struct ccwgroup_device *gdev)
1190 {
1191         struct qeth_card *card;
1192         struct device *dev;
1193         unsigned long flags;
1194         int rc;
1195
1196         QETH_DBF_TEXT(setup, 2, "probedev");
1197
1198         dev = &gdev->dev;
1199         if (!get_device(dev))
1200                 return -ENODEV;
1201
1202         QETH_DBF_TEXT_(setup, 2, "%s", gdev->dev.bus_id);
1203         
1204         card = qeth_alloc_card();
1205         if (!card) {
1206                 put_device(dev);
1207                 QETH_DBF_TEXT_(setup, 2, "1err%d", -ENOMEM);
1208                 return -ENOMEM;
1209         }
1210         card->read.ccwdev  = gdev->cdev[0];
1211         card->write.ccwdev = gdev->cdev[1];
1212         card->data.ccwdev  = gdev->cdev[2];
1213         gdev->dev.driver_data = card;
1214         card->gdev = gdev;
1215         gdev->cdev[0]->handler = qeth_irq;
1216         gdev->cdev[1]->handler = qeth_irq;
1217         gdev->cdev[2]->handler = qeth_irq;
1218
1219         if ((rc = qeth_determine_card_type(card))){
1220                 PRINT_WARN("%s: not a valid card type\n", __func__);
1221                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1222                 put_device(dev);
1223                 qeth_free_card(card);
1224                 return rc;
1225         }                           
1226         if ((rc = qeth_setup_card(card))){
1227                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1228                 put_device(dev);
1229                 qeth_free_card(card);
1230                 return rc;
1231         }
1232         rc = qeth_create_device_attributes(dev);
1233         if (rc) {
1234                 put_device(dev);
1235                 qeth_free_card(card);
1236                 return rc;
1237         }
1238         /* insert into our internal list */
1239         write_lock_irqsave(&qeth_card_list.rwlock, flags);
1240         list_add_tail(&card->list, &qeth_card_list.list);
1241         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
1242         return rc;
1243 }
1244
1245
1246 static int
1247 qeth_get_unitaddr(struct qeth_card *card)
1248 {
1249         int length;
1250         char *prcd;
1251         int rc;
1252
1253         QETH_DBF_TEXT(setup, 2, "getunit");
1254         rc = read_conf_data(CARD_DDEV(card), (void **) &prcd, &length);
1255         if (rc) {
1256                 PRINT_ERR("read_conf_data for device %s returned %i\n",
1257                           CARD_DDEV_ID(card), rc);
1258                 return rc;
1259         }
1260         card->info.chpid = prcd[30];
1261         card->info.unit_addr2 = prcd[31];
1262         card->info.cula = prcd[63];
1263         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1264                                (prcd[0x11] == _ascebc['M']));
1265         return 0;
1266 }
1267
1268 static void
1269 qeth_init_tokens(struct qeth_card *card)
1270 {
1271         card->token.issuer_rm_w = 0x00010103UL;
1272         card->token.cm_filter_w = 0x00010108UL;
1273         card->token.cm_connection_w = 0x0001010aUL;
1274         card->token.ulp_filter_w = 0x0001010bUL;
1275         card->token.ulp_connection_w = 0x0001010dUL;
1276 }
1277
1278 static inline __u16
1279 raw_devno_from_bus_id(char *id)
1280 {
1281         id += (strlen(id) - 4);
1282         return (__u16) simple_strtoul(id, &id, 16);
1283 }
1284 /**
1285  * setup channel
1286  */
1287 static void
1288 qeth_setup_ccw(struct qeth_channel *channel,unsigned char *iob, __u32 len)
1289 {
1290         struct qeth_card *card;
1291
1292         QETH_DBF_TEXT(trace, 4, "setupccw");
1293         card = CARD_FROM_CDEV(channel->ccwdev);
1294         if (channel == &card->read)
1295                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1296         else
1297                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1298         channel->ccw.count = len;
1299         channel->ccw.cda = (__u32) __pa(iob);
1300 }
1301
1302 /**
1303  * get free buffer for ccws (IDX activation, lancmds,ipassists...)
1304  */
1305 static struct qeth_cmd_buffer *
1306 __qeth_get_buffer(struct qeth_channel *channel)
1307 {
1308         __u8 index;
1309
1310         QETH_DBF_TEXT(trace, 6, "getbuff");
1311         index = channel->io_buf_no;
1312         do {
1313                 if (channel->iob[index].state == BUF_STATE_FREE) {
1314                         channel->iob[index].state = BUF_STATE_LOCKED;
1315                         channel->io_buf_no = (channel->io_buf_no + 1) %
1316                                 QETH_CMD_BUFFER_NO;
1317                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
1318                         return channel->iob + index;
1319                 }
1320                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1321         } while(index != channel->io_buf_no);
1322
1323         return NULL;
1324 }
1325
1326 /**
1327  * release command buffer
1328  */
1329 static void
1330 qeth_release_buffer(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1331 {
1332         unsigned long flags;
1333
1334         QETH_DBF_TEXT(trace, 6, "relbuff");
1335         spin_lock_irqsave(&channel->iob_lock, flags);
1336         memset(iob->data, 0, QETH_BUFSIZE);
1337         iob->state = BUF_STATE_FREE;
1338         iob->callback = qeth_send_control_data_cb;
1339         iob->rc = 0;
1340         spin_unlock_irqrestore(&channel->iob_lock, flags);
1341 }
1342
1343 static struct qeth_cmd_buffer *
1344 qeth_get_buffer(struct qeth_channel *channel)
1345 {
1346         struct qeth_cmd_buffer *buffer = NULL;
1347         unsigned long flags;
1348
1349         spin_lock_irqsave(&channel->iob_lock, flags);
1350         buffer = __qeth_get_buffer(channel);
1351         spin_unlock_irqrestore(&channel->iob_lock, flags);
1352         return buffer;
1353 }
1354
1355 static struct qeth_cmd_buffer *
1356 qeth_wait_for_buffer(struct qeth_channel *channel)
1357 {
1358         struct qeth_cmd_buffer *buffer;
1359         wait_event(channel->wait_q,
1360                    ((buffer = qeth_get_buffer(channel)) != NULL));
1361         return buffer;
1362 }
1363
1364 static void
1365 qeth_clear_cmd_buffers(struct qeth_channel *channel)
1366 {
1367         int cnt;
1368
1369         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1370                 qeth_release_buffer(channel,&channel->iob[cnt]);
1371         channel->buf_no = 0;
1372         channel->io_buf_no = 0;
1373 }
1374
1375 /**
1376  * start IDX for read and write channel
1377  */
1378 static int
1379 qeth_idx_activate_get_answer(struct qeth_channel *channel,
1380                               void (*idx_reply_cb)(struct qeth_channel *,
1381                                                    struct qeth_cmd_buffer *))
1382 {
1383         struct qeth_cmd_buffer *iob;
1384         unsigned long flags;
1385         int rc;
1386         struct qeth_card *card;
1387
1388         QETH_DBF_TEXT(setup, 2, "idxanswr");
1389         card = CARD_FROM_CDEV(channel->ccwdev);
1390         iob = qeth_get_buffer(channel);
1391         iob->callback = idx_reply_cb;
1392         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1393         channel->ccw.count = QETH_BUFSIZE;
1394         channel->ccw.cda = (__u32) __pa(iob->data);
1395
1396         wait_event(card->wait_q,
1397                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1398         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1399         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1400         rc = ccw_device_start(channel->ccwdev,
1401                               &channel->ccw,(addr_t) iob, 0, 0);
1402         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1403
1404         if (rc) {
1405                 PRINT_ERR("qeth: Error2 in activating channel rc=%d\n",rc);
1406                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1407                 atomic_set(&channel->irq_pending, 0);
1408                 wake_up(&card->wait_q);
1409                 return rc;
1410         }
1411         rc = wait_event_interruptible_timeout(card->wait_q,
1412                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1413         if (rc == -ERESTARTSYS)
1414                 return rc;
1415         if (channel->state != CH_STATE_UP){
1416                 rc = -ETIME;
1417                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1418                 qeth_clear_cmd_buffers(channel);
1419         } else
1420                 rc = 0;
1421         return rc;
1422 }
1423
1424 static int
1425 qeth_idx_activate_channel(struct qeth_channel *channel,
1426                            void (*idx_reply_cb)(struct qeth_channel *,
1427                                                 struct qeth_cmd_buffer *))
1428 {
1429         struct qeth_card *card;
1430         struct qeth_cmd_buffer *iob;
1431         unsigned long flags;
1432         __u16 temp;
1433         int rc;
1434
1435         card = CARD_FROM_CDEV(channel->ccwdev);
1436
1437         QETH_DBF_TEXT(setup, 2, "idxactch");
1438
1439         iob = qeth_get_buffer(channel);
1440         iob->callback = idx_reply_cb;
1441         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1442         channel->ccw.count = IDX_ACTIVATE_SIZE;
1443         channel->ccw.cda = (__u32) __pa(iob->data);
1444         if (channel == &card->write) {
1445                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1446                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1447                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1448                 card->seqno.trans_hdr++;
1449         } else {
1450                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1451                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1452                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1453         }
1454         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1455                &card->token.issuer_rm_w,QETH_MPC_TOKEN_LENGTH);
1456         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1457                &card->info.func_level,sizeof(__u16));
1458         temp = raw_devno_from_bus_id(CARD_DDEV_ID(card));
1459         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp, 2);
1460         temp = (card->info.cula << 8) + card->info.unit_addr2;
1461         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1462
1463         wait_event(card->wait_q,
1464                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1465         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1466         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1467         rc = ccw_device_start(channel->ccwdev,
1468                               &channel->ccw,(addr_t) iob, 0, 0);
1469         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1470
1471         if (rc) {
1472                 PRINT_ERR("qeth: Error1 in activating channel. rc=%d\n",rc);
1473                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
1474                 atomic_set(&channel->irq_pending, 0);
1475                 wake_up(&card->wait_q);
1476                 return rc;
1477         }
1478         rc = wait_event_interruptible_timeout(card->wait_q,
1479                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1480         if (rc == -ERESTARTSYS)
1481                 return rc;
1482         if (channel->state != CH_STATE_ACTIVATING) {
1483                 PRINT_WARN("qeth: IDX activate timed out!\n");
1484                 QETH_DBF_TEXT_(setup, 2, "2err%d", -ETIME);
1485                 qeth_clear_cmd_buffers(channel);
1486                 return -ETIME;
1487         }
1488         return qeth_idx_activate_get_answer(channel,idx_reply_cb);
1489 }
1490
1491 static int
1492 qeth_peer_func_level(int level)
1493 {
1494         if ((level & 0xff) == 8)
1495                 return (level & 0xff) + 0x400;
1496         if (((level >> 8) & 3) == 1)
1497                 return (level & 0xff) + 0x200;
1498         return level;
1499 }
1500
1501 static void
1502 qeth_idx_write_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1503 {
1504         struct qeth_card *card;
1505         __u16 temp;
1506
1507         QETH_DBF_TEXT(setup ,2, "idxwrcb");
1508
1509         if (channel->state == CH_STATE_DOWN) {
1510                 channel->state = CH_STATE_ACTIVATING;
1511                 goto out;
1512         }
1513         card = CARD_FROM_CDEV(channel->ccwdev);
1514
1515         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1516                 PRINT_ERR("IDX_ACTIVATE on write channel device %s: negative "
1517                           "reply\n", CARD_WDEV_ID(card));
1518                 goto out;
1519         }
1520         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1521         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1522                 PRINT_WARN("IDX_ACTIVATE on write channel device %s: "
1523                            "function level mismatch "
1524                            "(sent: 0x%x, received: 0x%x)\n",
1525                            CARD_WDEV_ID(card), card->info.func_level, temp);
1526                 goto out;
1527         }
1528         channel->state = CH_STATE_UP;
1529 out:
1530         qeth_release_buffer(channel, iob);
1531 }
1532
1533 static int
1534 qeth_check_idx_response(unsigned char *buffer)
1535 {
1536         if (!buffer)
1537                 return 0;
1538
1539         QETH_DBF_HEX(control, 2, buffer, QETH_DBF_CONTROL_LEN);
1540         if ((buffer[2] & 0xc0) == 0xc0) {
1541                 PRINT_WARN("received an IDX TERMINATE "
1542                            "with cause code 0x%02x%s\n",
1543                            buffer[4],
1544                            ((buffer[4] == 0x22) ?
1545                             " -- try another portname" : ""));
1546                 QETH_DBF_TEXT(trace, 2, "ckidxres");
1547                 QETH_DBF_TEXT(trace, 2, " idxterm");
1548                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
1549                 return -EIO;
1550         }
1551         return 0;
1552 }
1553
1554 static void
1555 qeth_idx_read_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1556 {
1557         struct qeth_card *card;
1558         __u16 temp;
1559
1560         QETH_DBF_TEXT(setup , 2, "idxrdcb");
1561         if (channel->state == CH_STATE_DOWN) {
1562                 channel->state = CH_STATE_ACTIVATING;
1563                 goto out;
1564         }
1565
1566         card = CARD_FROM_CDEV(channel->ccwdev);
1567         if (qeth_check_idx_response(iob->data)) {
1568                         goto out;
1569         }
1570         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1571                 PRINT_ERR("IDX_ACTIVATE on read channel device %s: negative "
1572                           "reply\n", CARD_RDEV_ID(card));
1573                 goto out;
1574         }
1575
1576 /**
1577  * temporary fix for microcode bug
1578  * to revert it,replace OR by AND
1579  */
1580         if ( (!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1581              (card->info.type == QETH_CARD_TYPE_OSAE) )
1582                 card->info.portname_required = 1;
1583
1584         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1585         if (temp != qeth_peer_func_level(card->info.func_level)) {
1586                 PRINT_WARN("IDX_ACTIVATE on read channel device %s: function "
1587                            "level mismatch (sent: 0x%x, received: 0x%x)\n",
1588                            CARD_RDEV_ID(card), card->info.func_level, temp);
1589                 goto out;
1590         }
1591         memcpy(&card->token.issuer_rm_r,
1592                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1593                QETH_MPC_TOKEN_LENGTH);
1594         memcpy(&card->info.mcl_level[0],
1595                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1596         channel->state = CH_STATE_UP;
1597 out:
1598         qeth_release_buffer(channel,iob);
1599 }
1600
1601 static int
1602 qeth_issue_next_read(struct qeth_card *card)
1603 {
1604         int rc;
1605         struct qeth_cmd_buffer *iob;
1606
1607         QETH_DBF_TEXT(trace,5,"issnxrd");
1608         if (card->read.state != CH_STATE_UP)
1609                 return -EIO;
1610         iob = qeth_get_buffer(&card->read);
1611         if (!iob) {
1612                 PRINT_WARN("issue_next_read failed: no iob available!\n");
1613                 return -ENOMEM;
1614         }
1615         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
1616         wait_event(card->wait_q,
1617                    atomic_cmpxchg(&card->read.irq_pending, 0, 1) == 0);
1618         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1619         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
1620                               (addr_t) iob, 0, 0);
1621         if (rc) {
1622                 PRINT_ERR("Error in starting next read ccw! rc=%i\n", rc);
1623                 atomic_set(&card->read.irq_pending, 0);
1624                 qeth_schedule_recovery(card);
1625                 wake_up(&card->wait_q);
1626         }
1627         return rc;
1628 }
1629
1630 static struct qeth_reply *
1631 qeth_alloc_reply(struct qeth_card *card)
1632 {
1633         struct qeth_reply *reply;
1634
1635         reply = kmalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
1636         if (reply){
1637                 memset(reply, 0, sizeof(struct qeth_reply));
1638                 atomic_set(&reply->refcnt, 1);
1639                 reply->card = card;
1640         };
1641         return reply;
1642 }
1643
1644 static void
1645 qeth_get_reply(struct qeth_reply *reply)
1646 {
1647         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1648         atomic_inc(&reply->refcnt);
1649 }
1650
1651 static void
1652 qeth_put_reply(struct qeth_reply *reply)
1653 {
1654         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1655         if (atomic_dec_and_test(&reply->refcnt))
1656                 kfree(reply);
1657 }
1658
1659 static void
1660 qeth_cmd_timeout(unsigned long data)
1661 {
1662         struct qeth_reply *reply, *list_reply, *r;
1663         unsigned long flags;
1664
1665         reply = (struct qeth_reply *) data;
1666         spin_lock_irqsave(&reply->card->lock, flags);
1667         list_for_each_entry_safe(list_reply, r,
1668                                  &reply->card->cmd_waiter_list, list) {
1669                 if (reply == list_reply){
1670                         qeth_get_reply(reply);
1671                         list_del_init(&reply->list);
1672                         spin_unlock_irqrestore(&reply->card->lock, flags);
1673                         reply->rc = -ETIME;
1674                         reply->received = 1;
1675                         wake_up(&reply->wait_q);
1676                         qeth_put_reply(reply);
1677                         return;
1678                 }
1679         }
1680         spin_unlock_irqrestore(&reply->card->lock, flags);
1681 }
1682
1683
1684 static struct qeth_ipa_cmd *
1685 qeth_check_ipa_data(struct qeth_card *card, struct qeth_cmd_buffer *iob)
1686 {
1687         struct qeth_ipa_cmd *cmd = NULL;
1688
1689         QETH_DBF_TEXT(trace,5,"chkipad");
1690         if (IS_IPA(iob->data)){
1691                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
1692                 if (IS_IPA_REPLY(cmd))
1693                         return cmd;
1694                 else {
1695                         switch (cmd->hdr.command) {
1696                         case IPA_CMD_STOPLAN:
1697                                 PRINT_WARN("Link failure on %s (CHPID 0x%X) - "
1698                                            "there is a network problem or "
1699                                            "someone pulled the cable or "
1700                                            "disabled the port.\n",
1701                                            QETH_CARD_IFNAME(card),
1702                                            card->info.chpid);
1703                                 card->lan_online = 0;
1704                                 if (card->dev && netif_carrier_ok(card->dev))
1705                                         netif_carrier_off(card->dev);
1706                                 return NULL;
1707                         case IPA_CMD_STARTLAN:
1708                                 PRINT_INFO("Link reestablished on %s "
1709                                            "(CHPID 0x%X). Scheduling "
1710                                            "IP address reset.\n",
1711                                            QETH_CARD_IFNAME(card),
1712                                            card->info.chpid);
1713                                 qeth_schedule_recovery(card);
1714                                 return NULL;
1715                         case IPA_CMD_MODCCID:
1716                                 return cmd;
1717                         case IPA_CMD_REGISTER_LOCAL_ADDR:
1718                                 QETH_DBF_TEXT(trace,3, "irla");
1719                                 break;
1720                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
1721                                 QETH_DBF_TEXT(trace,3, "urla");
1722                                 break;
1723                         default:
1724                                 PRINT_WARN("Received data is IPA "
1725                                            "but not a reply!\n");
1726                                 break;
1727                         }
1728                 }
1729         }
1730         return cmd;
1731 }
1732
1733 /**
1734  * wake all waiting ipa commands
1735  */
1736 static void
1737 qeth_clear_ipacmd_list(struct qeth_card *card)
1738 {
1739         struct qeth_reply *reply, *r;
1740         unsigned long flags;
1741
1742         QETH_DBF_TEXT(trace, 4, "clipalst");
1743
1744         spin_lock_irqsave(&card->lock, flags);
1745         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1746                 qeth_get_reply(reply);
1747                 reply->rc = -EIO;
1748                 reply->received = 1;
1749                 list_del_init(&reply->list);
1750                 wake_up(&reply->wait_q);
1751                 qeth_put_reply(reply);
1752         }
1753         spin_unlock_irqrestore(&card->lock, flags);
1754 }
1755
1756 static void
1757 qeth_send_control_data_cb(struct qeth_channel *channel,
1758                           struct qeth_cmd_buffer *iob)
1759 {
1760         struct qeth_card *card;
1761         struct qeth_reply *reply, *r;
1762         struct qeth_ipa_cmd *cmd;
1763         unsigned long flags;
1764         int keep_reply;
1765
1766         QETH_DBF_TEXT(trace,4,"sndctlcb");
1767
1768         card = CARD_FROM_CDEV(channel->ccwdev);
1769         if (qeth_check_idx_response(iob->data)) {
1770                 qeth_clear_ipacmd_list(card);
1771                 qeth_schedule_recovery(card);
1772                 goto out;
1773         }
1774
1775         cmd = qeth_check_ipa_data(card, iob);
1776         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
1777                 goto out;
1778         /*in case of OSN : check if cmd is set */
1779         if (card->info.type == QETH_CARD_TYPE_OSN &&
1780             cmd &&
1781             cmd->hdr.command != IPA_CMD_STARTLAN &&
1782             card->osn_info.assist_cb != NULL) {
1783                 card->osn_info.assist_cb(card->dev, cmd);
1784                 goto out;
1785         }
1786
1787         spin_lock_irqsave(&card->lock, flags);
1788         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1789                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
1790                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
1791                         qeth_get_reply(reply);
1792                         list_del_init(&reply->list);
1793                         spin_unlock_irqrestore(&card->lock, flags);
1794                         keep_reply = 0;
1795                         if (reply->callback != NULL) {
1796                                 if (cmd) {
1797                                         reply->offset = (__u16)((char*)cmd -
1798                                                                 (char *)iob->data);
1799                                         keep_reply = reply->callback(card,
1800                                                         reply,
1801                                                         (unsigned long)cmd);
1802                                 } else
1803                                         keep_reply = reply->callback(card,
1804                                                         reply,
1805                                                         (unsigned long)iob);
1806                         }
1807                         if (cmd)
1808                                 reply->rc = (u16) cmd->hdr.return_code;
1809                         else if (iob->rc)
1810                                 reply->rc = iob->rc;
1811                         if (keep_reply) {
1812                                 spin_lock_irqsave(&card->lock, flags);
1813                                 list_add_tail(&reply->list,
1814                                               &card->cmd_waiter_list);
1815                                 spin_unlock_irqrestore(&card->lock, flags);
1816                         } else {
1817                                 reply->received = 1;
1818                                 wake_up(&reply->wait_q);
1819                         }
1820                         qeth_put_reply(reply);
1821                         goto out;
1822                 }
1823         }
1824         spin_unlock_irqrestore(&card->lock, flags);
1825 out:
1826         memcpy(&card->seqno.pdu_hdr_ack,
1827                 QETH_PDU_HEADER_SEQ_NO(iob->data),
1828                 QETH_SEQ_NO_LENGTH);
1829         qeth_release_buffer(channel,iob);
1830 }
1831
1832 static inline void
1833 qeth_prepare_control_data(struct qeth_card *card, int len,
1834 struct qeth_cmd_buffer *iob)
1835 {
1836         qeth_setup_ccw(&card->write,iob->data,len);
1837         iob->callback = qeth_release_buffer;
1838
1839         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1840                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1841         card->seqno.trans_hdr++;
1842         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1843                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1844         card->seqno.pdu_hdr++;
1845         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1846                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1847         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1848 }
1849                                                     
1850 static int
1851 qeth_send_control_data(struct qeth_card *card, int len,
1852                        struct qeth_cmd_buffer *iob,
1853                        int (*reply_cb)
1854                        (struct qeth_card *, struct qeth_reply*, unsigned long),
1855                        void *reply_param)
1856
1857 {
1858         int rc;
1859         unsigned long flags;
1860         struct qeth_reply *reply = NULL;
1861         struct timer_list timer;
1862
1863         QETH_DBF_TEXT(trace, 2, "sendctl");
1864
1865         reply = qeth_alloc_reply(card);
1866         if (!reply) {
1867                 PRINT_WARN("Could no alloc qeth_reply!\n");
1868                 return -ENOMEM;
1869         }
1870         reply->callback = reply_cb;
1871         reply->param = reply_param;
1872         if (card->state == CARD_STATE_DOWN)
1873                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1874         else
1875                 reply->seqno = card->seqno.ipa++;
1876         init_timer(&timer);
1877         timer.function = qeth_cmd_timeout;
1878         timer.data = (unsigned long) reply;
1879         init_waitqueue_head(&reply->wait_q);
1880         spin_lock_irqsave(&card->lock, flags);
1881         list_add_tail(&reply->list, &card->cmd_waiter_list);
1882         spin_unlock_irqrestore(&card->lock, flags);
1883         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1884         wait_event(card->wait_q,
1885                    atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1886         qeth_prepare_control_data(card, len, iob);
1887         if (IS_IPA(iob->data))
1888                 timer.expires = jiffies + QETH_IPA_TIMEOUT;
1889         else
1890                 timer.expires = jiffies + QETH_TIMEOUT;
1891         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1892         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1893         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1894                               (addr_t) iob, 0, 0);
1895         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1896         if (rc){
1897                 PRINT_WARN("qeth_send_control_data: "
1898                            "ccw_device_start rc = %i\n", rc);
1899                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1900                 spin_lock_irqsave(&card->lock, flags);
1901                 list_del_init(&reply->list);
1902                 qeth_put_reply(reply);
1903                 spin_unlock_irqrestore(&card->lock, flags);
1904                 qeth_release_buffer(iob->channel, iob);
1905                 atomic_set(&card->write.irq_pending, 0);
1906                 wake_up(&card->wait_q);
1907                 return rc;
1908         }
1909         add_timer(&timer);
1910         wait_event(reply->wait_q, reply->received);
1911         del_timer_sync(&timer);
1912         rc = reply->rc;
1913         qeth_put_reply(reply);
1914         return rc;
1915 }
1916
1917 static int
1918 qeth_osn_send_control_data(struct qeth_card *card, int len,
1919                            struct qeth_cmd_buffer *iob)
1920 {
1921         unsigned long flags;
1922         int rc = 0;
1923
1924         QETH_DBF_TEXT(trace, 5, "osndctrd");
1925
1926         wait_event(card->wait_q,
1927                    atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1928         qeth_prepare_control_data(card, len, iob);
1929         QETH_DBF_TEXT(trace, 6, "osnoirqp");
1930         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1931         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1932                               (addr_t) iob, 0, 0);
1933         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1934         if (rc){
1935                 PRINT_WARN("qeth_osn_send_control_data: "
1936                            "ccw_device_start rc = %i\n", rc);
1937                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1938                 qeth_release_buffer(iob->channel, iob);
1939                 atomic_set(&card->write.irq_pending, 0);
1940                 wake_up(&card->wait_q);
1941         }
1942         return rc;
1943 }                                       
1944
1945 static inline void
1946 qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1947                      char prot_type)
1948 {
1949         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1950         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data),&prot_type,1);
1951         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1952                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1953 }
1954
1955 static int
1956 qeth_osn_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1957                       int data_len)
1958 {
1959         u16 s1, s2;
1960
1961         QETH_DBF_TEXT(trace,4,"osndipa");
1962
1963         qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
1964         s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
1965         s2 = (u16)data_len;
1966         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
1967         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
1968         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
1969         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
1970         return qeth_osn_send_control_data(card, s1, iob);
1971 }
1972                                                             
1973 static int
1974 qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1975                   int (*reply_cb)
1976                   (struct qeth_card *,struct qeth_reply*, unsigned long),
1977                   void *reply_param)
1978 {
1979         int rc;
1980         char prot_type;
1981
1982         QETH_DBF_TEXT(trace,4,"sendipa");
1983
1984         if (card->options.layer2)
1985                 if (card->info.type == QETH_CARD_TYPE_OSN)
1986                         prot_type = QETH_PROT_OSN2;
1987                 else
1988                         prot_type = QETH_PROT_LAYER2;
1989         else
1990                 prot_type = QETH_PROT_TCPIP;
1991         qeth_prepare_ipa_cmd(card,iob,prot_type);
1992         rc = qeth_send_control_data(card, IPA_CMD_LENGTH, iob,
1993                                     reply_cb, reply_param);
1994         return rc;
1995 }
1996
1997
1998 static int
1999 qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2000                   unsigned long data)
2001 {
2002         struct qeth_cmd_buffer *iob;
2003
2004         QETH_DBF_TEXT(setup, 2, "cmenblcb");
2005
2006         iob = (struct qeth_cmd_buffer *) data;
2007         memcpy(&card->token.cm_filter_r,
2008                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2009                QETH_MPC_TOKEN_LENGTH);
2010         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2011         return 0;
2012 }
2013
2014 static int
2015 qeth_cm_enable(struct qeth_card *card)
2016 {
2017         int rc;
2018         struct qeth_cmd_buffer *iob;
2019
2020         QETH_DBF_TEXT(setup,2,"cmenable");
2021
2022         iob = qeth_wait_for_buffer(&card->write);
2023         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2024         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2025                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2026         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2027                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2028
2029         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2030                                     qeth_cm_enable_cb, NULL);
2031         return rc;
2032 }
2033
2034 static int
2035 qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2036                  unsigned long data)
2037 {
2038
2039         struct qeth_cmd_buffer *iob;
2040
2041         QETH_DBF_TEXT(setup, 2, "cmsetpcb");
2042
2043         iob = (struct qeth_cmd_buffer *) data;
2044         memcpy(&card->token.cm_connection_r,
2045                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2046                QETH_MPC_TOKEN_LENGTH);
2047         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2048         return 0;
2049 }
2050
2051 static int
2052 qeth_cm_setup(struct qeth_card *card)
2053 {
2054         int rc;
2055         struct qeth_cmd_buffer *iob;
2056
2057         QETH_DBF_TEXT(setup,2,"cmsetup");
2058
2059         iob = qeth_wait_for_buffer(&card->write);
2060         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2061         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2062                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2063         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2064                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2065         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2066                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2067         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2068                                     qeth_cm_setup_cb, NULL);
2069         return rc;
2070
2071 }
2072
2073 static int
2074 qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2075                    unsigned long data)
2076 {
2077
2078         __u16 mtu, framesize;
2079         __u16 len;
2080         __u8 link_type;
2081         struct qeth_cmd_buffer *iob;
2082
2083         QETH_DBF_TEXT(setup, 2, "ulpenacb");
2084
2085         iob = (struct qeth_cmd_buffer *) data;
2086         memcpy(&card->token.ulp_filter_r,
2087                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2088                QETH_MPC_TOKEN_LENGTH);
2089         if (qeth_get_mtu_out_of_mpc(card->info.type)) {
2090                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2091                 mtu = qeth_get_mtu_outof_framesize(framesize);
2092                 if (!mtu) {
2093                         iob->rc = -EINVAL;
2094                         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2095                         return 0;
2096                 }
2097                 card->info.max_mtu = mtu;
2098                 card->info.initial_mtu = mtu;
2099                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2100         } else {
2101                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
2102                 card->info.max_mtu = qeth_get_max_mtu_for_card(card->info.type);
2103                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2104         }
2105
2106         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2107         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2108                 memcpy(&link_type,
2109                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2110                 card->info.link_type = link_type;
2111         } else
2112                 card->info.link_type = 0;
2113         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2114         return 0;
2115 }
2116
2117 static int
2118 qeth_ulp_enable(struct qeth_card *card)
2119 {
2120         int rc;
2121         char prot_type;
2122         struct qeth_cmd_buffer *iob;
2123
2124         /*FIXME: trace view callbacks*/
2125         QETH_DBF_TEXT(setup,2,"ulpenabl");
2126
2127         iob = qeth_wait_for_buffer(&card->write);
2128         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2129
2130         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2131                 (__u8) card->info.portno;
2132         if (card->options.layer2)
2133                 if (card->info.type == QETH_CARD_TYPE_OSN)
2134                         prot_type = QETH_PROT_OSN2;
2135                 else
2136                         prot_type = QETH_PROT_LAYER2;
2137         else
2138                 prot_type = QETH_PROT_TCPIP;
2139
2140         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data),&prot_type,1);
2141         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2142                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2143         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2144                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2145         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2146                card->info.portname, 9);
2147         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2148                                     qeth_ulp_enable_cb, NULL);
2149         return rc;
2150
2151 }
2152
2153 static inline __u16
2154 __raw_devno_from_bus_id(char *id)
2155 {
2156         id += (strlen(id) - 4);
2157         return (__u16) simple_strtoul(id, &id, 16);
2158 }
2159
2160 static int
2161 qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2162                   unsigned long data)
2163 {
2164         struct qeth_cmd_buffer *iob;
2165
2166         QETH_DBF_TEXT(setup, 2, "ulpstpcb");
2167
2168         iob = (struct qeth_cmd_buffer *) data;
2169         memcpy(&card->token.ulp_connection_r,
2170                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2171                QETH_MPC_TOKEN_LENGTH);
2172         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2173         return 0;
2174 }
2175
2176 static int
2177 qeth_ulp_setup(struct qeth_card *card)
2178 {
2179         int rc;
2180         __u16 temp;
2181         struct qeth_cmd_buffer *iob;
2182
2183         QETH_DBF_TEXT(setup,2,"ulpsetup");
2184
2185         iob = qeth_wait_for_buffer(&card->write);
2186         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2187
2188         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2189                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2190         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2191                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2192         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2193                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2194
2195         temp = __raw_devno_from_bus_id(CARD_DDEV_ID(card));
2196         memcpy(QETH_ULP_SETUP_CUA(iob->data), &temp, 2);
2197         temp = (card->info.cula << 8) + card->info.unit_addr2;
2198         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2199         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2200                                     qeth_ulp_setup_cb, NULL);
2201         return rc;
2202 }
2203
2204 static inline int
2205 qeth_check_qdio_errors(struct qdio_buffer *buf, unsigned int qdio_error,
2206                        unsigned int siga_error, const char *dbftext)
2207 {
2208         if (qdio_error || siga_error) {
2209                 QETH_DBF_TEXT(trace, 2, dbftext);
2210                 QETH_DBF_TEXT(qerr, 2, dbftext);
2211                 QETH_DBF_TEXT_(qerr, 2, " F15=%02X",
2212                                buf->element[15].flags & 0xff);
2213                 QETH_DBF_TEXT_(qerr, 2, " F14=%02X",
2214                                buf->element[14].flags & 0xff);
2215                 QETH_DBF_TEXT_(qerr, 2, " qerr=%X", qdio_error);
2216                 QETH_DBF_TEXT_(qerr, 2, " serr=%X", siga_error);
2217                 return 1;
2218         }
2219         return 0;
2220 }
2221
2222 static inline struct sk_buff *
2223 qeth_get_skb(unsigned int length, struct qeth_hdr *hdr)
2224 {
2225         struct sk_buff* skb;
2226         int add_len;
2227
2228         add_len = 0;
2229         if (hdr->hdr.osn.id == QETH_HEADER_TYPE_OSN)
2230                 add_len = sizeof(struct qeth_hdr);
2231 #ifdef CONFIG_QETH_VLAN
2232         else
2233                 add_len = VLAN_HLEN;
2234 #endif
2235         skb = dev_alloc_skb(length + add_len);
2236         if (skb && add_len)
2237                 skb_reserve(skb, add_len);
2238         return skb;
2239 }
2240
2241 static inline struct sk_buff *
2242 qeth_get_next_skb(struct qeth_card *card, struct qdio_buffer *buffer,
2243                   struct qdio_buffer_element **__element, int *__offset,
2244                   struct qeth_hdr **hdr)
2245 {
2246         struct qdio_buffer_element *element = *__element;
2247         int offset = *__offset;
2248         struct sk_buff *skb = NULL;
2249         int skb_len;
2250         void *data_ptr;
2251         int data_len;
2252
2253         QETH_DBF_TEXT(trace,6,"nextskb");
2254         /* qeth_hdr must not cross element boundaries */
2255         if (element->length < offset + sizeof(struct qeth_hdr)){
2256                 if (qeth_is_last_sbale(element))
2257                         return NULL;
2258                 element++;
2259                 offset = 0;
2260                 if (element->length < sizeof(struct qeth_hdr))
2261                         return NULL;
2262         }
2263         *hdr = element->addr + offset;
2264
2265         offset += sizeof(struct qeth_hdr);
2266         if (card->options.layer2)
2267                 if (card->info.type == QETH_CARD_TYPE_OSN)
2268                         skb_len = (*hdr)->hdr.osn.pdu_length;
2269                 else
2270                         skb_len = (*hdr)->hdr.l2.pkt_length;
2271         else
2272                 skb_len = (*hdr)->hdr.l3.length;
2273
2274         if (!skb_len)
2275                 return NULL;
2276         if (card->options.fake_ll){
2277                 if(card->dev->type == ARPHRD_IEEE802_TR){
2278                         if (!(skb = qeth_get_skb(skb_len+QETH_FAKE_LL_LEN_TR, *hdr)))
2279                                 goto no_mem;
2280                         skb_reserve(skb,QETH_FAKE_LL_LEN_TR);
2281                 } else {
2282                         if (!(skb = qeth_get_skb(skb_len+QETH_FAKE_LL_LEN_ETH, *hdr)))
2283                                 goto no_mem;
2284                         skb_reserve(skb,QETH_FAKE_LL_LEN_ETH);
2285                 }
2286         } else if (!(skb = qeth_get_skb(skb_len, *hdr)))
2287                 goto no_mem;
2288         data_ptr = element->addr + offset;
2289         while (skb_len) {
2290                 data_len = min(skb_len, (int)(element->length - offset));
2291                 if (data_len)
2292                         memcpy(skb_put(skb, data_len), data_ptr, data_len);
2293                 skb_len -= data_len;
2294                 if (skb_len){
2295                         if (qeth_is_last_sbale(element)){
2296                                 QETH_DBF_TEXT(trace,4,"unexeob");
2297                                 QETH_DBF_TEXT_(trace,4,"%s",CARD_BUS_ID(card));
2298                                 QETH_DBF_TEXT(qerr,2,"unexeob");
2299                                 QETH_DBF_TEXT_(qerr,2,"%s",CARD_BUS_ID(card));
2300                                 QETH_DBF_HEX(misc,4,buffer,sizeof(*buffer));
2301                                 dev_kfree_skb_any(skb);
2302                                 card->stats.rx_errors++;
2303                                 return NULL;
2304                         }
2305                         element++;
2306                         offset = 0;
2307                         data_ptr = element->addr;
2308                 } else {
2309                         offset += data_len;
2310                 }
2311         }
2312         *__element = element;
2313         *__offset = offset;
2314         return skb;
2315 no_mem:
2316         if (net_ratelimit()){
2317                 PRINT_WARN("No memory for packet received on %s.\n",
2318                            QETH_CARD_IFNAME(card));
2319                 QETH_DBF_TEXT(trace,2,"noskbmem");
2320                 QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2321         }
2322         card->stats.rx_dropped++;
2323         return NULL;
2324 }
2325
2326 static inline __be16
2327 qeth_type_trans(struct sk_buff *skb, struct net_device *dev)
2328 {
2329         struct qeth_card *card;
2330         struct ethhdr *eth;
2331
2332         QETH_DBF_TEXT(trace,6,"typtrans");
2333
2334         card = (struct qeth_card *)dev->priv;
2335 #ifdef CONFIG_TR
2336         if ((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
2337             (card->info.link_type == QETH_LINK_TYPE_LANE_TR))
2338                 return tr_type_trans(skb,dev);
2339 #endif /* CONFIG_TR */
2340         skb->mac.raw = skb->data;
2341         skb_pull(skb, ETH_HLEN );
2342         eth = eth_hdr(skb);
2343
2344         if (*eth->h_dest & 1) {
2345                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
2346                         skb->pkt_type = PACKET_BROADCAST;
2347                 else
2348                         skb->pkt_type = PACKET_MULTICAST;
2349         } else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
2350                 skb->pkt_type = PACKET_OTHERHOST;
2351
2352         if (ntohs(eth->h_proto) >= 1536)
2353                 return eth->h_proto;
2354         if (*(unsigned short *) (skb->data) == 0xFFFF)
2355                 return htons(ETH_P_802_3);
2356         return htons(ETH_P_802_2);
2357 }
2358
2359 static inline void
2360 qeth_rebuild_skb_fake_ll_tr(struct qeth_card *card, struct sk_buff *skb,
2361                          struct qeth_hdr *hdr)
2362 {
2363         struct trh_hdr *fake_hdr;
2364         struct trllc *fake_llc;
2365         struct iphdr *ip_hdr;
2366
2367         QETH_DBF_TEXT(trace,5,"skbfktr");
2368         skb->mac.raw = skb->data - QETH_FAKE_LL_LEN_TR;
2369         /* this is a fake ethernet header */
2370         fake_hdr = (struct trh_hdr *) skb->mac.raw;
2371
2372         /* the destination MAC address */
2373         switch (skb->pkt_type){
2374         case PACKET_MULTICAST:
2375                 switch (skb->protocol){
2376 #ifdef CONFIG_QETH_IPV6
2377                 case __constant_htons(ETH_P_IPV6):
2378                         ndisc_mc_map((struct in6_addr *)
2379                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2380                                      fake_hdr->daddr, card->dev, 0);
2381                         break;
2382 #endif /* CONFIG_QETH_IPV6 */
2383                 case __constant_htons(ETH_P_IP):
2384                         ip_hdr = (struct iphdr *)skb->data;
2385                         ip_tr_mc_map(ip_hdr->daddr, fake_hdr->daddr);
2386                         break;
2387                 default:
2388                         memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2389                 }
2390                 break;
2391         case PACKET_BROADCAST:
2392                 memset(fake_hdr->daddr, 0xff, TR_ALEN);
2393                 break;
2394         default:
2395                 memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2396         }
2397         /* the source MAC address */
2398         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2399                 memcpy(fake_hdr->saddr, &hdr->hdr.l3.dest_addr[2], TR_ALEN);
2400         else
2401                 memset(fake_hdr->saddr, 0, TR_ALEN);
2402         fake_hdr->rcf=0;
2403         fake_llc = (struct trllc*)&(fake_hdr->rcf);
2404         fake_llc->dsap = EXTENDED_SAP;
2405         fake_llc->ssap = EXTENDED_SAP;
2406         fake_llc->llc  = UI_CMD;
2407         fake_llc->protid[0] = 0;
2408         fake_llc->protid[1] = 0;
2409         fake_llc->protid[2] = 0;
2410         fake_llc->ethertype = ETH_P_IP;
2411 }
2412
2413 static inline void
2414 qeth_rebuild_skb_fake_ll_eth(struct qeth_card *card, struct sk_buff *skb,
2415                          struct qeth_hdr *hdr)
2416 {
2417         struct ethhdr *fake_hdr;
2418         struct iphdr *ip_hdr;
2419
2420         QETH_DBF_TEXT(trace,5,"skbfketh");
2421         skb->mac.raw = skb->data - QETH_FAKE_LL_LEN_ETH;
2422         /* this is a fake ethernet header */
2423         fake_hdr = (struct ethhdr *) skb->mac.raw;
2424
2425         /* the destination MAC address */
2426         switch (skb->pkt_type){
2427         case PACKET_MULTICAST:
2428                 switch (skb->protocol){
2429 #ifdef CONFIG_QETH_IPV6
2430                 case __constant_htons(ETH_P_IPV6):
2431                         ndisc_mc_map((struct in6_addr *)
2432                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2433                                      fake_hdr->h_dest, card->dev, 0);
2434                         break;
2435 #endif /* CONFIG_QETH_IPV6 */
2436                 case __constant_htons(ETH_P_IP):
2437                         ip_hdr = (struct iphdr *)skb->data;
2438                         ip_eth_mc_map(ip_hdr->daddr, fake_hdr->h_dest);
2439                         break;
2440                 default:
2441                         memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2442                 }
2443                 break;
2444         case PACKET_BROADCAST:
2445                 memset(fake_hdr->h_dest, 0xff, ETH_ALEN);
2446                 break;
2447         default:
2448                 memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2449         }
2450         /* the source MAC address */
2451         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2452                 memcpy(fake_hdr->h_source, &hdr->hdr.l3.dest_addr[2], ETH_ALEN);
2453         else
2454                 memset(fake_hdr->h_source, 0, ETH_ALEN);
2455         /* the protocol */
2456         fake_hdr->h_proto = skb->protocol;
2457 }
2458
2459 static inline void
2460 qeth_rebuild_skb_fake_ll(struct qeth_card *card, struct sk_buff *skb,
2461                         struct qeth_hdr *hdr)
2462 {
2463         if (card->dev->type == ARPHRD_IEEE802_TR)
2464                 qeth_rebuild_skb_fake_ll_tr(card, skb, hdr);
2465         else
2466                 qeth_rebuild_skb_fake_ll_eth(card, skb, hdr);
2467 }
2468
2469 static inline void
2470 qeth_rebuild_skb_vlan(struct qeth_card *card, struct sk_buff *skb,
2471                       struct qeth_hdr *hdr)
2472 {
2473 #ifdef CONFIG_QETH_VLAN
2474         u16 *vlan_tag;
2475
2476         if (hdr->hdr.l3.ext_flags &
2477             (QETH_HDR_EXT_VLAN_FRAME | QETH_HDR_EXT_INCLUDE_VLAN_TAG)) {
2478                 vlan_tag = (u16 *) skb_push(skb, VLAN_HLEN);
2479                 *vlan_tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME)?
2480                         hdr->hdr.l3.vlan_id : *((u16 *)&hdr->hdr.l3.dest_addr[12]);
2481                 *(vlan_tag + 1) = skb->protocol;
2482                 skb->protocol = __constant_htons(ETH_P_8021Q);
2483         }
2484 #endif /* CONFIG_QETH_VLAN */
2485 }
2486
2487 static inline __u16
2488 qeth_layer2_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2489                         struct qeth_hdr *hdr)
2490 {
2491         unsigned short vlan_id = 0;
2492 #ifdef CONFIG_QETH_VLAN
2493         struct vlan_hdr *vhdr;
2494 #endif
2495
2496         skb->pkt_type = PACKET_HOST;
2497         skb->protocol = qeth_type_trans(skb, skb->dev);
2498         if (card->options.checksum_type == NO_CHECKSUMMING)
2499                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2500         else
2501                 skb->ip_summed = CHECKSUM_NONE;
2502 #ifdef CONFIG_QETH_VLAN
2503         if (hdr->hdr.l2.flags[2] & (QETH_LAYER2_FLAG_VLAN)) {
2504                 vhdr = (struct vlan_hdr *) skb->data;
2505                 skb->protocol =
2506                         __constant_htons(vhdr->h_vlan_encapsulated_proto);
2507                 vlan_id = hdr->hdr.l2.vlan_id;
2508                 skb_pull(skb, VLAN_HLEN);
2509         }
2510 #endif
2511         *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
2512         return vlan_id;
2513 }
2514
2515 static inline void
2516 qeth_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2517                  struct qeth_hdr *hdr)
2518 {
2519 #ifdef CONFIG_QETH_IPV6
2520         if (hdr->hdr.l3.flags & QETH_HDR_PASSTHRU) {
2521                 skb->pkt_type = PACKET_HOST;
2522                 skb->protocol = qeth_type_trans(skb, card->dev);
2523                 return;
2524         }
2525 #endif /* CONFIG_QETH_IPV6 */
2526         skb->protocol = htons((hdr->hdr.l3.flags & QETH_HDR_IPV6)? ETH_P_IPV6 :
2527                               ETH_P_IP);
2528         switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK){
2529         case QETH_CAST_UNICAST:
2530                 skb->pkt_type = PACKET_HOST;
2531                 break;
2532         case QETH_CAST_MULTICAST:
2533                 skb->pkt_type = PACKET_MULTICAST;
2534                 card->stats.multicast++;
2535                 break;
2536         case QETH_CAST_BROADCAST:
2537                 skb->pkt_type = PACKET_BROADCAST;
2538                 card->stats.multicast++;
2539                 break;
2540         case QETH_CAST_ANYCAST:
2541         case QETH_CAST_NOCAST:
2542         default:
2543                 skb->pkt_type = PACKET_HOST;
2544         }
2545         qeth_rebuild_skb_vlan(card, skb, hdr);
2546         if (card->options.fake_ll)
2547                 qeth_rebuild_skb_fake_ll(card, skb, hdr);
2548         else
2549                 skb->mac.raw = skb->data;
2550         skb->ip_summed = card->options.checksum_type;
2551         if (card->options.checksum_type == HW_CHECKSUMMING){
2552                 if ( (hdr->hdr.l3.ext_flags &
2553                       (QETH_HDR_EXT_CSUM_HDR_REQ |
2554                        QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
2555                      (QETH_HDR_EXT_CSUM_HDR_REQ |
2556                       QETH_HDR_EXT_CSUM_TRANSP_REQ) )
2557                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2558                 else
2559                         skb->ip_summed = SW_CHECKSUMMING;
2560         }
2561 }
2562
2563 static inline void
2564 qeth_process_inbound_buffer(struct qeth_card *card,
2565                             struct qeth_qdio_buffer *buf, int index)
2566 {
2567         struct qdio_buffer_element *element;
2568         struct sk_buff *skb;
2569         struct qeth_hdr *hdr;
2570         int offset;
2571         int rxrc;
2572         __u16 vlan_tag = 0;
2573
2574         /* get first element of current buffer */
2575         element = (struct qdio_buffer_element *)&buf->buffer->element[0];
2576         offset = 0;
2577 #ifdef CONFIG_QETH_PERF_STATS
2578         card->perf_stats.bufs_rec++;
2579 #endif
2580         while((skb = qeth_get_next_skb(card, buf->buffer, &element,
2581                                        &offset, &hdr))) {
2582                 skb->dev = card->dev;
2583                 if (hdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
2584                         vlan_tag = qeth_layer2_rebuild_skb(card, skb, hdr);
2585                 else if (hdr->hdr.l3.id == QETH_HEADER_TYPE_LAYER3)     
2586                         qeth_rebuild_skb(card, skb, hdr);
2587                 else { /*in case of OSN*/
2588                         skb_push(skb, sizeof(struct qeth_hdr));
2589                         memcpy(skb->data, hdr, sizeof(struct qeth_hdr));
2590                 }
2591                 /* is device UP ? */
2592                 if (!(card->dev->flags & IFF_UP)){
2593                         dev_kfree_skb_any(skb);
2594                         continue;
2595                 }
2596 #ifdef CONFIG_QETH_VLAN
2597                 if (vlan_tag)
2598                         vlan_hwaccel_rx(skb, card->vlangrp, vlan_tag);
2599                 else
2600 #endif
2601                 if (card->info.type == QETH_CARD_TYPE_OSN)
2602                         rxrc = card->osn_info.data_cb(skb);
2603                 else
2604                         rxrc = netif_rx(skb);
2605                 card->dev->last_rx = jiffies;
2606                 card->stats.rx_packets++;
2607                 card->stats.rx_bytes += skb->len;
2608         }
2609 }
2610
2611 static inline struct qeth_buffer_pool_entry *
2612 qeth_get_buffer_pool_entry(struct qeth_card *card)
2613 {
2614         struct qeth_buffer_pool_entry *entry;
2615
2616         QETH_DBF_TEXT(trace, 6, "gtbfplen");
2617         if (!list_empty(&card->qdio.in_buf_pool.entry_list)) {
2618                 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2619                                 struct qeth_buffer_pool_entry, list);
2620                 list_del_init(&entry->list);
2621                 return entry;
2622         }
2623         return NULL;
2624 }
2625
2626 static inline void
2627 qeth_init_input_buffer(struct qeth_card *card, struct qeth_qdio_buffer *buf)
2628 {
2629         struct qeth_buffer_pool_entry *pool_entry;
2630         int i;
2631
2632         pool_entry = qeth_get_buffer_pool_entry(card);
2633         /*
2634          * since the buffer is accessed only from the input_tasklet
2635          * there shouldn't be a need to synchronize; also, since we use
2636          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2637          * buffers
2638          */
2639         BUG_ON(!pool_entry);
2640
2641         buf->pool_entry = pool_entry;
2642         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i){
2643                 buf->buffer->element[i].length = PAGE_SIZE;
2644                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2645                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2646                         buf->buffer->element[i].flags = SBAL_FLAGS_LAST_ENTRY;
2647                 else
2648                         buf->buffer->element[i].flags = 0;
2649         }
2650         buf->state = QETH_QDIO_BUF_EMPTY;
2651 }
2652
2653 static inline void
2654 qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
2655                          struct qeth_qdio_out_buffer *buf)
2656 {
2657         int i;
2658         struct sk_buff *skb;
2659
2660         /* is PCI flag set on buffer? */
2661         if (buf->buffer->element[0].flags & 0x40)
2662                 atomic_dec(&queue->set_pci_flags_count);
2663
2664         while ((skb = skb_dequeue(&buf->skb_list))){
2665                 atomic_dec(&skb->users);
2666                 dev_kfree_skb_any(skb);
2667         }
2668         qeth_eddp_buf_release_contexts(buf);
2669         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i){
2670                 buf->buffer->element[i].length = 0;
2671                 buf->buffer->element[i].addr = NULL;
2672                 buf->buffer->element[i].flags = 0;
2673         }
2674         buf->next_element_to_fill = 0;
2675         atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
2676 }
2677
2678 static inline void
2679 qeth_queue_input_buffer(struct qeth_card *card, int index)
2680 {
2681         struct qeth_qdio_q *queue = card->qdio.in_q;
2682         int count;
2683         int i;
2684         int rc;
2685
2686         QETH_DBF_TEXT(trace,6,"queinbuf");
2687         count = (index < queue->next_buf_to_init)?
2688                 card->qdio.in_buf_pool.buf_count -
2689                 (queue->next_buf_to_init - index) :
2690                 card->qdio.in_buf_pool.buf_count -
2691                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
2692         /* only requeue at a certain threshold to avoid SIGAs */
2693         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)){
2694                 for (i = queue->next_buf_to_init;
2695                      i < queue->next_buf_to_init + count; ++i)
2696                         qeth_init_input_buffer(card,
2697                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q]);
2698                 /*
2699                  * according to old code it should be avoided to requeue all
2700                  * 128 buffers in order to benefit from PCI avoidance.
2701                  * this function keeps at least one buffer (the buffer at
2702                  * 'index') un-requeued -> this buffer is the first buffer that
2703                  * will be requeued the next time
2704                  */
2705 #ifdef CONFIG_QETH_PERF_STATS
2706                 card->perf_stats.inbound_do_qdio_cnt++;
2707                 card->perf_stats.inbound_do_qdio_start_time = qeth_get_micros();
2708 #endif
2709                 rc = do_QDIO(CARD_DDEV(card),
2710                              QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
2711                              0, queue->next_buf_to_init, count, NULL);
2712 #ifdef CONFIG_QETH_PERF_STATS
2713                 card->perf_stats.inbound_do_qdio_time += qeth_get_micros() -
2714                         card->perf_stats.inbound_do_qdio_start_time;
2715 #endif
2716                 if (rc){
2717                         PRINT_WARN("qeth_queue_input_buffer's do_QDIO "
2718                                    "return %i (device %s).\n",
2719                                    rc, CARD_DDEV_ID(card));
2720                         QETH_DBF_TEXT(trace,2,"qinberr");
2721                         QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2722                 }
2723                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
2724                                           QDIO_MAX_BUFFERS_PER_Q;
2725         }
2726 }
2727
2728 static inline void
2729 qeth_put_buffer_pool_entry(struct qeth_card *card,
2730                            struct qeth_buffer_pool_entry *entry)
2731 {
2732         QETH_DBF_TEXT(trace, 6, "ptbfplen");
2733         list_add_tail(&entry->list, &card->qdio.in_buf_pool.entry_list);
2734 }
2735
2736 static void
2737 qeth_qdio_input_handler(struct ccw_device * ccwdev, unsigned int status,
2738                         unsigned int qdio_err, unsigned int siga_err,
2739                         unsigned int queue, int first_element, int count,
2740                         unsigned long card_ptr)
2741 {
2742         struct net_device *net_dev;
2743         struct qeth_card *card;
2744         struct qeth_qdio_buffer *buffer;
2745         int index;
2746         int i;
2747
2748         QETH_DBF_TEXT(trace, 6, "qdinput");
2749         card = (struct qeth_card *) card_ptr;
2750         net_dev = card->dev;
2751 #ifdef CONFIG_QETH_PERF_STATS
2752         card->perf_stats.inbound_cnt++;
2753         card->perf_stats.inbound_start_time = qeth_get_micros();
2754 #endif
2755         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
2756                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
2757                         QETH_DBF_TEXT(trace, 1,"qdinchk");
2758                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2759                         QETH_DBF_TEXT_(trace,1,"%04X%04X",first_element,count);
2760                         QETH_DBF_TEXT_(trace,1,"%04X%04X", queue, status);
2761                         qeth_schedule_recovery(card);
2762                         return;
2763                 }
2764         }
2765         for (i = first_element; i < (first_element + count); ++i) {
2766                 index = i % QDIO_MAX_BUFFERS_PER_Q;
2767                 buffer = &card->qdio.in_q->bufs[index];
2768                 if (!((status & QDIO_STATUS_LOOK_FOR_ERROR) &&
2769                       qeth_check_qdio_errors(buffer->buffer, 
2770                                              qdio_err, siga_err,"qinerr")))
2771                         qeth_process_inbound_buffer(card, buffer, index);
2772                 /* clear buffer and give back to hardware */
2773                 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
2774                 qeth_queue_input_buffer(card, index);
2775         }
2776 #ifdef CONFIG_QETH_PERF_STATS
2777         card->perf_stats.inbound_time += qeth_get_micros() -
2778                 card->perf_stats.inbound_start_time;
2779 #endif
2780 }
2781
2782 static inline int
2783 qeth_handle_send_error(struct qeth_card *card,
2784                        struct qeth_qdio_out_buffer *buffer,
2785                        unsigned int qdio_err, unsigned int siga_err)
2786 {
2787         int sbalf15 = buffer->buffer->element[15].flags & 0xff;
2788         int cc = siga_err & 3;
2789
2790         QETH_DBF_TEXT(trace, 6, "hdsnderr");
2791         qeth_check_qdio_errors(buffer->buffer, qdio_err, siga_err, "qouterr");
2792         switch (cc) {
2793         case 0:
2794                 if (qdio_err){
2795                         QETH_DBF_TEXT(trace, 1,"lnkfail");
2796                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2797                         QETH_DBF_TEXT_(trace,1,"%04x %02x",
2798                                        (u16)qdio_err, (u8)sbalf15);
2799                         return QETH_SEND_ERROR_LINK_FAILURE;
2800                 }
2801                 return QETH_SEND_ERROR_NONE;
2802         case 2:
2803                 if (siga_err & QDIO_SIGA_ERROR_B_BIT_SET) {
2804                         QETH_DBF_TEXT(trace, 1, "SIGAcc2B");
2805                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2806                         return QETH_SEND_ERROR_KICK_IT;
2807                 }
2808                 if ((sbalf15 >= 15) && (sbalf15 <= 31))
2809                         return QETH_SEND_ERROR_RETRY;
2810                 return QETH_SEND_ERROR_LINK_FAILURE;
2811                 /* look at qdio_error and sbalf 15 */
2812         case 1:
2813                 QETH_DBF_TEXT(trace, 1, "SIGAcc1");
2814                 QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2815                 return QETH_SEND_ERROR_LINK_FAILURE;
2816         case 3:
2817         default:
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 }
2823
2824 void
2825 qeth_flush_buffers(struct qeth_qdio_out_q *queue, int under_int,
2826                    int index, int count)
2827 {
2828         struct qeth_qdio_out_buffer *buf;
2829         int rc;
2830         int i;
2831
2832         QETH_DBF_TEXT(trace, 6, "flushbuf");
2833
2834         for (i = index; i < index + count; ++i) {
2835                 buf = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2836                 buf->buffer->element[buf->next_element_to_fill - 1].flags |=
2837                                 SBAL_FLAGS_LAST_ENTRY;
2838
2839                 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
2840                         continue;
2841
2842                 if (!queue->do_pack){
2843                         if ((atomic_read(&queue->used_buffers) >=
2844                                 (QETH_HIGH_WATERMARK_PACK -
2845                                  QETH_WATERMARK_PACK_FUZZ)) &&
2846                             !atomic_read(&queue->set_pci_flags_count)){
2847                                 /* it's likely that we'll go to packing
2848                                  * mode soon */
2849                                 atomic_inc(&queue->set_pci_flags_count);
2850                                 buf->buffer->element[0].flags |= 0x40;
2851                         }
2852                 } else {
2853                         if (!atomic_read(&queue->set_pci_flags_count)){
2854                                 /*
2855                                  * there's no outstanding PCI any more, so we
2856                                  * have to request a PCI to be sure the the PCI
2857                                  * will wake at some time in the future then we
2858                                  * can flush packed buffers that might still be
2859                                  * hanging around, which can happen if no
2860                                  * further send was requested by the stack
2861                                  */
2862                                 atomic_inc(&queue->set_pci_flags_count);
2863                                 buf->buffer->element[0].flags |= 0x40;
2864                         }
2865                 }
2866         }
2867
2868         queue->card->dev->trans_start = jiffies;
2869 #ifdef CONFIG_QETH_PERF_STATS
2870         queue->card->perf_stats.outbound_do_qdio_cnt++;
2871         queue->card->perf_stats.outbound_do_qdio_start_time = qeth_get_micros();
2872 #endif
2873         if (under_int)
2874                 rc = do_QDIO(CARD_DDEV(queue->card),
2875                              QDIO_FLAG_SYNC_OUTPUT | QDIO_FLAG_UNDER_INTERRUPT,
2876                              queue->queue_no, index, count, NULL);
2877         else
2878                 rc = do_QDIO(CARD_DDEV(queue->card), QDIO_FLAG_SYNC_OUTPUT,
2879                              queue->queue_no, index, count, NULL);
2880 #ifdef CONFIG_QETH_PERF_STATS
2881         queue->card->perf_stats.outbound_do_qdio_time += qeth_get_micros() -
2882                 queue->card->perf_stats.outbound_do_qdio_start_time;
2883 #endif
2884         if (rc){
2885                 QETH_DBF_TEXT(trace, 2, "flushbuf");
2886                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
2887                 QETH_DBF_TEXT_(trace, 2, "%s", CARD_DDEV_ID(queue->card));
2888                 queue->card->stats.tx_errors += count;
2889                 /* this must not happen under normal circumstances. if it
2890                  * happens something is really wrong -> recover */
2891                 qeth_schedule_recovery(queue->card);
2892                 return;
2893         }
2894         atomic_add(count, &queue->used_buffers);
2895 #ifdef CONFIG_QETH_PERF_STATS
2896         queue->card->perf_stats.bufs_sent += count;
2897 #endif
2898 }
2899
2900 /*
2901  * Switched to packing state if the number of used buffers on a queue
2902  * reaches a certain limit.
2903  */
2904 static inline void
2905 qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
2906 {
2907         if (!queue->do_pack) {
2908                 if (atomic_read(&queue->used_buffers)
2909                     >= QETH_HIGH_WATERMARK_PACK){
2910                         /* switch non-PACKING -> PACKING */
2911                         QETH_DBF_TEXT(trace, 6, "np->pack");
2912 #ifdef CONFIG_QETH_PERF_STATS
2913                         queue->card->perf_stats.sc_dp_p++;
2914 #endif
2915                         queue->do_pack = 1;
2916                 }
2917         }
2918 }
2919
2920 /*
2921  * Switches from packing to non-packing mode. If there is a packing
2922  * buffer on the queue this buffer will be prepared to be flushed.
2923  * In that case 1 is returned to inform the caller. If no buffer
2924  * has to be flushed, zero is returned.
2925  */
2926 static inline int
2927 qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
2928 {
2929         struct qeth_qdio_out_buffer *buffer;
2930         int flush_count = 0;
2931
2932         if (queue->do_pack) {
2933                 if (atomic_read(&queue->used_buffers)
2934                     <= QETH_LOW_WATERMARK_PACK) {
2935                         /* switch PACKING -> non-PACKING */
2936                         QETH_DBF_TEXT(trace, 6, "pack->np");
2937 #ifdef CONFIG_QETH_PERF_STATS
2938                         queue->card->perf_stats.sc_p_dp++;
2939 #endif
2940                         queue->do_pack = 0;
2941                         /* flush packing buffers */
2942                         buffer = &queue->bufs[queue->next_buf_to_fill];
2943                         if ((atomic_read(&buffer->state) ==
2944                                                 QETH_QDIO_BUF_EMPTY) &&
2945                             (buffer->next_element_to_fill > 0)) {
2946                                 atomic_set(&buffer->state,QETH_QDIO_BUF_PRIMED);
2947                                 flush_count++;
2948                                 queue->next_buf_to_fill =
2949                                         (queue->next_buf_to_fill + 1) %
2950                                         QDIO_MAX_BUFFERS_PER_Q;
2951                         }
2952                 }
2953         }
2954         return flush_count;
2955 }
2956
2957 /*
2958  * Called to flush a packing buffer if no more pci flags are on the queue.
2959  * Checks if there is a packing buffer and prepares it to be flushed.
2960  * In that case returns 1, otherwise zero.
2961  */
2962 static inline int
2963 qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2964 {
2965         struct qeth_qdio_out_buffer *buffer;
2966
2967         buffer = &queue->bufs[queue->next_buf_to_fill];
2968         if((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
2969            (buffer->next_element_to_fill > 0)){
2970                 /* it's a packing buffer */
2971                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
2972                 queue->next_buf_to_fill =
2973                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
2974                 return 1;
2975         }
2976         return 0;
2977 }
2978
2979 static inline void
2980 qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2981 {
2982         int index;
2983         int flush_cnt = 0;
2984         int q_was_packing = 0;
2985
2986         /*
2987          * check if weed have to switch to non-packing mode or if
2988          * we have to get a pci flag out on the queue
2989          */
2990         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
2991             !atomic_read(&queue->set_pci_flags_count)){
2992                 if (atomic_swap(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
2993                                 QETH_OUT_Q_UNLOCKED) {
2994                         /*
2995                          * If we get in here, there was no action in
2996                          * do_send_packet. So, we check if there is a
2997                          * packing buffer to be flushed here.
2998                          */
2999                         netif_stop_queue(queue->card->dev);
3000                         index = queue->next_buf_to_fill;
3001                         q_was_packing = queue->do_pack;
3002                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3003                         if (!flush_cnt &&
3004                             !atomic_read(&queue->set_pci_flags_count))
3005                                 flush_cnt +=
3006                                         qeth_flush_buffers_on_no_pci(queue);
3007 #ifdef CONFIG_QETH_PERF_STATS
3008                         if (q_was_packing)
3009                                 queue->card->perf_stats.bufs_sent_pack +=
3010                                         flush_cnt;
3011 #endif
3012                         if (flush_cnt)
3013                                 qeth_flush_buffers(queue, 1, index, flush_cnt);
3014                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3015                 }
3016         }
3017 }
3018
3019 static void
3020 qeth_qdio_output_handler(struct ccw_device * ccwdev, unsigned int status,
3021                         unsigned int qdio_error, unsigned int siga_error,
3022                         unsigned int __queue, int first_element, int count,
3023                         unsigned long card_ptr)
3024 {
3025         struct qeth_card *card        = (struct qeth_card *) card_ptr;
3026         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3027         struct qeth_qdio_out_buffer *buffer;
3028         int i;
3029
3030         QETH_DBF_TEXT(trace, 6, "qdouhdl");
3031         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
3032                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
3033                         QETH_DBF_TEXT(trace, 2, "achkcond");
3034                         QETH_DBF_TEXT_(trace, 2, "%s", CARD_BUS_ID(card));
3035                         QETH_DBF_TEXT_(trace, 2, "%08x", status);
3036                         netif_stop_queue(card->dev);
3037                         qeth_schedule_recovery(card);
3038                         return;
3039                 }
3040         }
3041 #ifdef CONFIG_QETH_PERF_STATS
3042         card->perf_stats.outbound_handler_cnt++;
3043         card->perf_stats.outbound_handler_start_time = qeth_get_micros();
3044 #endif
3045         for(i = first_element; i < (first_element + count); ++i){
3046                 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
3047                 /*we only handle the KICK_IT error by doing a recovery */
3048                 if (qeth_handle_send_error(card, buffer,
3049                                            qdio_error, siga_error)
3050                                 == QETH_SEND_ERROR_KICK_IT){
3051                         netif_stop_queue(card->dev);
3052                         qeth_schedule_recovery(card);
3053                         return;
3054                 }
3055                 qeth_clear_output_buffer(queue, buffer);
3056         }
3057         atomic_sub(count, &queue->used_buffers);
3058         /* check if we need to do something on this outbound queue */
3059         if (card->info.type != QETH_CARD_TYPE_IQD)
3060                 qeth_check_outbound_queue(queue);
3061
3062         netif_wake_queue(queue->card->dev);
3063 #ifdef CONFIG_QETH_PERF_STATS
3064         card->perf_stats.outbound_handler_time += qeth_get_micros() -
3065                 card->perf_stats.outbound_handler_start_time;
3066 #endif
3067 }
3068
3069 static void
3070 qeth_create_qib_param_field(struct qeth_card *card, char *param_field)
3071 {
3072
3073         param_field[0] = _ascebc['P'];
3074         param_field[1] = _ascebc['C'];
3075         param_field[2] = _ascebc['I'];
3076         param_field[3] = _ascebc['T'];
3077         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
3078         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
3079         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
3080 }
3081
3082 static void
3083 qeth_create_qib_param_field_blkt(struct qeth_card *card, char *param_field)
3084 {
3085         param_field[16] = _ascebc['B'];
3086         param_field[17] = _ascebc['L'];
3087         param_field[18] = _ascebc['K'];
3088         param_field[19] = _ascebc['T'];
3089         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
3090         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
3091         *((unsigned int *) (&param_field[28])) = card->info.blkt.inter_packet_jumbo;
3092 }
3093
3094 static void
3095 qeth_initialize_working_pool_list(struct qeth_card *card)
3096 {
3097         struct qeth_buffer_pool_entry *entry;
3098
3099         QETH_DBF_TEXT(trace,5,"inwrklst");
3100
3101         list_for_each_entry(entry,
3102                             &card->qdio.init_pool.entry_list, init_list) {
3103                 qeth_put_buffer_pool_entry(card,entry);
3104         }
3105 }
3106
3107 static void
3108 qeth_clear_working_pool_list(struct qeth_card *card)
3109 {
3110         struct qeth_buffer_pool_entry *pool_entry, *tmp;
3111
3112         QETH_DBF_TEXT(trace,5,"clwrklst");
3113         list_for_each_entry_safe(pool_entry, tmp,
3114                             &card->qdio.in_buf_pool.entry_list, list){
3115                         list_del(&pool_entry->list);
3116         }
3117 }
3118
3119 static void
3120 qeth_free_buffer_pool(struct qeth_card *card)
3121 {
3122         struct qeth_buffer_pool_entry *pool_entry, *tmp;
3123         int i=0;
3124         QETH_DBF_TEXT(trace,5,"freepool");
3125         list_for_each_entry_safe(pool_entry, tmp,
3126                                  &card->qdio.init_pool.entry_list, init_list){
3127                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
3128                         free_page((unsigned long)pool_entry->elements[i]);
3129                 list_del(&pool_entry->init_list);
3130                 kfree(pool_entry);
3131         }
3132 }
3133
3134 static int
3135 qeth_alloc_buffer_pool(struct qeth_card *card)
3136 {
3137         struct qeth_buffer_pool_entry *pool_entry;
3138         void *ptr;
3139         int i, j;
3140
3141         QETH_DBF_TEXT(trace,5,"alocpool");
3142         for (i = 0; i < card->qdio.init_pool.buf_count; ++i){
3143                 pool_entry = kmalloc(sizeof(*pool_entry), GFP_KERNEL);
3144                 if (!pool_entry){
3145                         qeth_free_buffer_pool(card);
3146                         return -ENOMEM;
3147                 }
3148                 for(j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j){
3149                         ptr = (void *) __get_free_page(GFP_KERNEL|GFP_DMA);
3150                         if (!ptr) {
3151                                 while (j > 0)
3152                                         free_page((unsigned long)
3153                                                   pool_entry->elements[--j]);
3154                                 kfree(pool_entry);
3155                                 qeth_free_buffer_pool(card);
3156                                 return -ENOMEM;
3157                         }
3158                         pool_entry->elements[j] = ptr;
3159                 }
3160                 list_add(&pool_entry->init_list,
3161                          &card->qdio.init_pool.entry_list);
3162         }
3163         return 0;
3164 }
3165
3166 int
3167 qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
3168 {
3169         QETH_DBF_TEXT(trace, 2, "realcbp");
3170
3171         if ((card->state != CARD_STATE_DOWN) &&
3172             (card->state != CARD_STATE_RECOVER))
3173                 return -EPERM;
3174
3175         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
3176         qeth_clear_working_pool_list(card);
3177         qeth_free_buffer_pool(card);
3178         card->qdio.in_buf_pool.buf_count = bufcnt;
3179         card->qdio.init_pool.buf_count = bufcnt;
3180         return qeth_alloc_buffer_pool(card);
3181 }
3182
3183 static int
3184 qeth_alloc_qdio_buffers(struct qeth_card *card)
3185 {
3186         int i, j;
3187
3188         QETH_DBF_TEXT(setup, 2, "allcqdbf");
3189
3190         if (card->qdio.state == QETH_QDIO_ALLOCATED)
3191                 return 0;
3192
3193         card->qdio.in_q = kmalloc(sizeof(struct qeth_qdio_q), 
3194                                   GFP_KERNEL|GFP_DMA);
3195         if (!card->qdio.in_q)
3196                 return - ENOMEM;
3197         QETH_DBF_TEXT(setup, 2, "inq");
3198         QETH_DBF_HEX(setup, 2, &card->qdio.in_q, sizeof(void *));
3199         memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
3200         /* give inbound qeth_qdio_buffers their qdio_buffers */
3201         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3202                 card->qdio.in_q->bufs[i].buffer =
3203                         &card->qdio.in_q->qdio_bufs[i];
3204         /* inbound buffer pool */
3205         if (qeth_alloc_buffer_pool(card)){
3206                 kfree(card->qdio.in_q);
3207                 return -ENOMEM;
3208         }
3209         /* outbound */
3210         card->qdio.out_qs =
3211                 kmalloc(card->qdio.no_out_queues *
3212                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
3213         if (!card->qdio.out_qs){
3214                 qeth_free_buffer_pool(card);
3215                 return -ENOMEM;
3216         }
3217         for (i = 0; i < card->qdio.no_out_queues; ++i){
3218                 card->qdio.out_qs[i] = kmalloc(sizeof(struct qeth_qdio_out_q),
3219                                                GFP_KERNEL|GFP_DMA);
3220                 if (!card->qdio.out_qs[i]){
3221                         while (i > 0)
3222                                 kfree(card->qdio.out_qs[--i]);
3223                         kfree(card->qdio.out_qs);
3224                         return -ENOMEM;
3225                 }
3226                 QETH_DBF_TEXT_(setup, 2, "outq %i", i);
3227                 QETH_DBF_HEX(setup, 2, &card->qdio.out_qs[i], sizeof(void *));
3228                 memset(card->qdio.out_qs[i], 0, sizeof(struct qeth_qdio_out_q));
3229                 card->qdio.out_qs[i]->queue_no = i;
3230                 /* give outbound qeth_qdio_buffers their qdio_buffers */
3231                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3232                         card->qdio.out_qs[i]->bufs[j].buffer =
3233                                 &card->qdio.out_qs[i]->qdio_bufs[j];
3234                         skb_queue_head_init(&card->qdio.out_qs[i]->bufs[j].
3235                                             skb_list);
3236                         INIT_LIST_HEAD(&card->qdio.out_qs[i]->bufs[j].ctx_list);
3237                 }
3238         }
3239         card->qdio.state = QETH_QDIO_ALLOCATED;
3240         return 0;
3241 }
3242
3243 static void
3244 qeth_free_qdio_buffers(struct qeth_card *card)
3245 {
3246         int i, j;
3247
3248         QETH_DBF_TEXT(trace, 2, "freeqdbf");
3249         if (card->qdio.state == QETH_QDIO_UNINITIALIZED)
3250                 return;
3251         kfree(card->qdio.in_q);
3252         /* inbound buffer pool */
3253         qeth_free_buffer_pool(card);
3254         /* free outbound qdio_qs */
3255         for (i = 0; i < card->qdio.no_out_queues; ++i){
3256                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3257                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3258                                         &card->qdio.out_qs[i]->bufs[j]);
3259                 kfree(card->qdio.out_qs[i]);
3260         }
3261         kfree(card->qdio.out_qs);
3262         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3263 }
3264
3265 static void
3266 qeth_clear_qdio_buffers(struct qeth_card *card)
3267 {
3268         int i, j;
3269
3270         QETH_DBF_TEXT(trace, 2, "clearqdbf");
3271         /* clear outbound buffers to free skbs */
3272         for (i = 0; i < card->qdio.no_out_queues; ++i)
3273                 if (card->qdio.out_qs[i]){
3274                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3275                                 qeth_clear_output_buffer(card->qdio.out_qs[i],
3276                                                 &card->qdio.out_qs[i]->bufs[j]);
3277                 }
3278 }
3279
3280 static void
3281 qeth_init_qdio_info(struct qeth_card *card)
3282 {
3283         QETH_DBF_TEXT(setup, 4, "intqdinf");
3284         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3285         /* inbound */
3286         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
3287         card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
3288         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
3289         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
3290         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
3291 }
3292
3293 static int
3294 qeth_init_qdio_queues(struct qeth_card *card)
3295 {
3296         int i, j;
3297         int rc;
3298
3299         QETH_DBF_TEXT(setup, 2, "initqdqs");
3300
3301         /* inbound queue */
3302         memset(card->qdio.in_q->qdio_bufs, 0,
3303                QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3304         qeth_initialize_working_pool_list(card);
3305         /*give only as many buffers to hardware as we have buffer pool entries*/
3306         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
3307                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
3308         card->qdio.in_q->next_buf_to_init = card->qdio.in_buf_pool.buf_count - 1;
3309         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
3310                      card->qdio.in_buf_pool.buf_count - 1, NULL);
3311         if (rc) {
3312                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
3313                 return rc;
3314         }
3315         rc = qdio_synchronize(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0);
3316         if (rc) {
3317                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
3318                 return rc;
3319         }
3320         /* outbound queue */
3321         for (i = 0; i < card->qdio.no_out_queues; ++i){
3322                 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
3323                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3324                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3325                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3326                                         &card->qdio.out_qs[i]->bufs[j]);
3327                 }
3328                 card->qdio.out_qs[i]->card = card;
3329                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
3330                 card->qdio.out_qs[i]->do_pack = 0;
3331                 atomic_set(&card->qdio.out_qs[i]->used_buffers,0);
3332                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
3333                 atomic_set(&card->qdio.out_qs[i]->state,
3334                            QETH_OUT_Q_UNLOCKED);
3335         }
3336         return 0;
3337 }
3338
3339 static int
3340 qeth_qdio_establish(struct qeth_card *card)
3341 {
3342         struct qdio_initialize init_data;
3343         char *qib_param_field;
3344         struct qdio_buffer **in_sbal_ptrs;
3345         struct qdio_buffer **out_sbal_ptrs;
3346         int i, j, k;
3347         int rc;
3348
3349         QETH_DBF_TEXT(setup, 2, "qdioest");
3350
3351         qib_param_field = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
3352                               GFP_KERNEL);
3353         if (!qib_param_field)
3354                 return -ENOMEM;
3355
3356         memset(qib_param_field, 0, QDIO_MAX_BUFFERS_PER_Q * sizeof(char));
3357
3358         qeth_create_qib_param_field(card, qib_param_field);
3359         qeth_create_qib_param_field_blkt(card, qib_param_field);
3360
3361         in_sbal_ptrs = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
3362                                GFP_KERNEL);
3363         if (!in_sbal_ptrs) {
3364                 kfree(qib_param_field);
3365                 return -ENOMEM;
3366         }
3367         for(i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3368                 in_sbal_ptrs[i] = (struct qdio_buffer *)
3369                         virt_to_phys(card->qdio.in_q->bufs[i].buffer);
3370
3371         out_sbal_ptrs =
3372                 kmalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
3373                         sizeof(void *), GFP_KERNEL);
3374         if (!out_sbal_ptrs) {
3375                 kfree(in_sbal_ptrs);
3376                 kfree(qib_param_field);
3377                 return -ENOMEM;
3378         }
3379         for(i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
3380                 for(j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k){
3381                         out_sbal_ptrs[k] = (struct qdio_buffer *)
3382                                 virt_to_phys(card->qdio.out_qs[i]->
3383                                              bufs[j].buffer);
3384                 }
3385
3386         memset(&init_data, 0, sizeof(struct qdio_initialize));
3387         init_data.cdev                   = CARD_DDEV(card);
3388         init_data.q_format               = qeth_get_qdio_q_format(card);
3389         init_data.qib_param_field_format = 0;
3390         init_data.qib_param_field        = qib_param_field;
3391         init_data.min_input_threshold    = QETH_MIN_INPUT_THRESHOLD;
3392         init_data.max_input_threshold    = QETH_MAX_INPUT_THRESHOLD;
3393         init_data.min_output_threshold   = QETH_MIN_OUTPUT_THRESHOLD;
3394         init_data.max_output_threshold   = QETH_MAX_OUTPUT_THRESHOLD;
3395         init_data.no_input_qs            = 1;
3396         init_data.no_output_qs           = card->qdio.no_out_queues;
3397         init_data.input_handler          = (qdio_handler_t *)
3398                                            qeth_qdio_input_handler;
3399         init_data.output_handler         = (qdio_handler_t *)
3400                                            qeth_qdio_output_handler;
3401         init_data.int_parm               = (unsigned long) card;
3402         init_data.flags                  = QDIO_INBOUND_0COPY_SBALS |
3403                                            QDIO_OUTBOUND_0COPY_SBALS |
3404                                            QDIO_USE_OUTBOUND_PCIS;
3405         init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
3406         init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
3407
3408         if (!(rc = qdio_initialize(&init_data)))
3409                 card->qdio.state = QETH_QDIO_ESTABLISHED;
3410
3411         kfree(out_sbal_ptrs);
3412         kfree(in_sbal_ptrs);
3413         kfree(qib_param_field);
3414         return rc;
3415 }
3416
3417 static int
3418 qeth_qdio_activate(struct qeth_card *card)
3419 {
3420         QETH_DBF_TEXT(setup,3,"qdioact");
3421         return qdio_activate(CARD_DDEV(card), 0);
3422 }
3423
3424 static int
3425 qeth_clear_channel(struct qeth_channel *channel)
3426 {
3427         unsigned long flags;
3428         struct qeth_card *card;
3429         int rc;
3430
3431         QETH_DBF_TEXT(trace,3,"clearch");
3432         card = CARD_FROM_CDEV(channel->ccwdev);
3433         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
3434         rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
3435         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
3436
3437         if (rc)
3438                 return rc;
3439         rc = wait_event_interruptible_timeout(card->wait_q,
3440                         channel->state==CH_STATE_STOPPED, QETH_TIMEOUT);
3441         if (rc == -ERESTARTSYS)
3442                 return rc;
3443         if (channel->state != CH_STATE_STOPPED)
3444                 return -ETIME;
3445         channel->state = CH_STATE_DOWN;
3446         return 0;
3447 }
3448
3449 static int
3450 qeth_halt_channel(struct qeth_channel *channel)
3451 {
3452         unsigned long flags;
3453         struct qeth_card *card;
3454         int rc;
3455
3456         QETH_DBF_TEXT(trace,3,"haltch");
3457         card = CARD_FROM_CDEV(channel->ccwdev);
3458         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
3459         rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
3460         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
3461
3462         if (rc)
3463                 return rc;
3464         rc = wait_event_interruptible_timeout(card->wait_q,
3465                         channel->state==CH_STATE_HALTED, QETH_TIMEOUT);
3466         if (rc == -ERESTARTSYS)
3467                 return rc;
3468         if (channel->state != CH_STATE_HALTED)
3469                 return -ETIME;
3470         return 0;
3471 }
3472
3473 static int
3474 qeth_halt_channels(struct qeth_card *card)
3475 {
3476         int rc1 = 0, rc2=0, rc3 = 0;
3477
3478         QETH_DBF_TEXT(trace,3,"haltchs");
3479         rc1 = qeth_halt_channel(&card->read);
3480         rc2 = qeth_halt_channel(&card->write);
3481         rc3 = qeth_halt_channel(&card->data);
3482         if (rc1)
3483                 return rc1;
3484         if (rc2) 
3485                 return rc2;
3486         return rc3;
3487 }
3488 static int
3489 qeth_clear_channels(struct qeth_card *card)
3490 {
3491         int rc1 = 0, rc2=0, rc3 = 0;
3492
3493         QETH_DBF_TEXT(trace,3,"clearchs");
3494         rc1 = qeth_clear_channel(&card->read);
3495         rc2 = qeth_clear_channel(&card->write);
3496         rc3 = qeth_clear_channel(&card->data);
3497         if (rc1)
3498                 return rc1;
3499         if (rc2) 
3500                 return rc2;
3501         return rc3;
3502 }
3503
3504 static int
3505 qeth_clear_halt_card(struct qeth_card *card, int halt)
3506 {
3507         int rc = 0;
3508
3509         QETH_DBF_TEXT(trace,3,"clhacrd");
3510         QETH_DBF_HEX(trace, 3, &card, sizeof(void *));
3511
3512         if (halt)
3513                 rc = qeth_halt_channels(card);
3514         if (rc)
3515                 return rc;
3516         return qeth_clear_channels(card);
3517 }
3518
3519 static int
3520 qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
3521 {
3522         int rc = 0;
3523
3524         QETH_DBF_TEXT(trace,3,"qdioclr");
3525         if (card->qdio.state == QETH_QDIO_ESTABLISHED){
3526                 if ((rc = qdio_cleanup(CARD_DDEV(card),
3527                              (card->info.type == QETH_CARD_TYPE_IQD) ?
3528                              QDIO_FLAG_CLEANUP_USING_HALT :
3529                              QDIO_FLAG_CLEANUP_USING_CLEAR)))
3530                         QETH_DBF_TEXT_(trace, 3, "1err%d", rc);
3531                 card->qdio.state = QETH_QDIO_ALLOCATED;
3532         }
3533         if ((rc = qeth_clear_halt_card(card, use_halt)))
3534                 QETH_DBF_TEXT_(trace, 3, "2err%d", rc);
3535         card->state = CARD_STATE_DOWN;
3536         return rc;
3537 }
3538
3539 static int
3540 qeth_dm_act(struct qeth_card *card)
3541 {
3542         int rc;
3543         struct qeth_cmd_buffer *iob;
3544
3545         QETH_DBF_TEXT(setup,2,"dmact");
3546
3547         iob = qeth_wait_for_buffer(&card->write);
3548         memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
3549
3550         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
3551                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
3552         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
3553                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3554         rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
3555         return rc;
3556 }
3557
3558 static int
3559 qeth_mpc_initialize(struct qeth_card *card)
3560 {
3561         int rc;
3562
3563         QETH_DBF_TEXT(setup,2,"mpcinit");
3564
3565         if ((rc = qeth_issue_next_read(card))){
3566                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
3567                 return rc;
3568         }
3569         if ((rc = qeth_cm_enable(card))){
3570                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
3571                 goto out_qdio;
3572         }
3573         if ((rc = qeth_cm_setup(card))){
3574                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
3575                 goto out_qdio;
3576         }
3577         if ((rc = qeth_ulp_enable(card))){
3578                 QETH_DBF_TEXT_(setup, 2, "4err%d", rc);
3579                 goto out_qdio;
3580         }
3581         if ((rc = qeth_ulp_setup(card))){
3582                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
3583                 goto out_qdio;
3584         }
3585         if ((rc = qeth_alloc_qdio_buffers(card))){
3586                 QETH_DBF_TEXT_(setup, 2, "5err%d", rc);
3587                 goto out_qdio;
3588         }
3589         if ((rc = qeth_qdio_establish(card))){
3590                 QETH_DBF_TEXT_(setup, 2, "6err%d", rc);
3591                 qeth_free_qdio_buffers(card);
3592                 goto out_qdio;
3593         }
3594         if ((rc = qeth_qdio_activate(card))){
3595                 QETH_DBF_TEXT_(setup, 2, "7err%d", rc);
3596                 goto out_qdio;
3597         }
3598         if ((rc = qeth_dm_act(card))){
3599                 QETH_DBF_TEXT_(setup, 2, "8err%d", rc);
3600                 goto out_qdio;
3601         }
3602
3603         return 0;
3604 out_qdio:
3605         qeth_qdio_clear_card(card, card->info.type!=QETH_CARD_TYPE_IQD);
3606         return rc;
3607 }
3608
3609 static struct net_device *
3610 qeth_get_netdevice(enum qeth_card_types type, enum qeth_link_types linktype)
3611 {
3612         struct net_device *dev = NULL;
3613
3614         switch (type) {
3615         case QETH_CARD_TYPE_OSAE:
3616                 switch (linktype) {
3617                 case QETH_LINK_TYPE_LANE_TR:
3618                 case QETH_LINK_TYPE_HSTR:
3619 #ifdef CONFIG_TR
3620                         dev = alloc_trdev(0);
3621 #endif /* CONFIG_TR */
3622                         break;
3623                 default:
3624                         dev = alloc_etherdev(0);
3625                 }
3626                 break;
3627         case QETH_CARD_TYPE_IQD:
3628                 dev = alloc_netdev(0, "hsi%d", ether_setup);
3629                 break;
3630         case QETH_CARD_TYPE_OSN:
3631                 dev = alloc_netdev(0, "osn%d", ether_setup);
3632                 break;
3633         default:
3634                 dev = alloc_etherdev(0);
3635         }
3636         return dev;
3637 }
3638
3639 /*hard_header fake function; used in case fake_ll is set */
3640 static int
3641 qeth_fake_header(struct sk_buff *skb, struct net_device *dev,
3642                      unsigned short type, void *daddr, void *saddr,
3643                      unsigned len)
3644 {
3645         if(dev->type == ARPHRD_IEEE802_TR){
3646                 struct trh_hdr *hdr;
3647                 hdr = (struct trh_hdr *)skb_push(skb, QETH_FAKE_LL_LEN_TR);
3648                 memcpy(hdr->saddr, dev->dev_addr, TR_ALEN);
3649                 memcpy(hdr->daddr, "FAKELL", TR_ALEN);
3650                 return QETH_FAKE_LL_LEN_TR;
3651
3652         } else {
3653                 struct ethhdr *hdr;
3654                 hdr = (struct ethhdr *)skb_push(skb, QETH_FAKE_LL_LEN_ETH);
3655                 memcpy(hdr->h_source, dev->dev_addr, ETH_ALEN);
3656                 memcpy(hdr->h_dest, "FAKELL", ETH_ALEN);
3657                 if (type != ETH_P_802_3)
3658                         hdr->h_proto = htons(type);
3659                 else
3660                         hdr->h_proto = htons(len);
3661                 return QETH_FAKE_LL_LEN_ETH;
3662
3663         }
3664 }
3665
3666 static inline int
3667 qeth_send_packet(struct qeth_card *, struct sk_buff *);
3668
3669 static int
3670 qeth_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
3671 {
3672         int rc;
3673         struct qeth_card *card;
3674
3675         QETH_DBF_TEXT(trace, 6, "hrdstxmi");
3676         card = (struct qeth_card *)dev->priv;
3677         if (skb==NULL) {
3678                 card->stats.tx_dropped++;
3679                 card->stats.tx_errors++;
3680                 /* return OK; otherwise ksoftirqd goes to 100% */
3681                 return NETDEV_TX_OK;
3682         }
3683         if ((card->state != CARD_STATE_UP) || !card->lan_online) {
3684                 card->stats.tx_dropped++;
3685                 card->stats.tx_errors++;
3686                 card->stats.tx_carrier_errors++;
3687                 dev_kfree_skb_any(skb);
3688                 /* return OK; otherwise ksoftirqd goes to 100% */
3689                 return NETDEV_TX_OK;
3690         }
3691 #ifdef CONFIG_QETH_PERF_STATS
3692         card->perf_stats.outbound_cnt++;
3693         card->perf_stats.outbound_start_time = qeth_get_micros();
3694 #endif
3695         netif_stop_queue(dev);
3696         if ((rc = qeth_send_packet(card, skb))) {
3697                 if (rc == -EBUSY) {
3698                         return NETDEV_TX_BUSY;
3699                 } else {
3700                         card->stats.tx_errors++;
3701                         card->stats.tx_dropped++;
3702                         dev_kfree_skb_any(skb);
3703                         /*set to OK; otherwise ksoftirqd goes to 100% */
3704                         rc = NETDEV_TX_OK;
3705                 }
3706         }
3707         netif_wake_queue(dev);
3708 #ifdef CONFIG_QETH_PERF_STATS
3709         card->perf_stats.outbound_time += qeth_get_micros() -
3710                 card->perf_stats.outbound_start_time;
3711 #endif
3712         return rc;
3713 }
3714
3715 static int
3716 qeth_verify_vlan_dev(struct net_device *dev, struct qeth_card *card)
3717 {
3718         int rc = 0;
3719 #ifdef CONFIG_QETH_VLAN
3720         struct vlan_group *vg;
3721         int i;
3722
3723         if (!(vg = card->vlangrp))
3724                 return rc;
3725
3726         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++){
3727                 if (vg->vlan_devices[i] == dev){
3728                         rc = QETH_VLAN_CARD;
3729                         break;
3730                 }
3731         }
3732         if (rc && !(VLAN_DEV_INFO(dev)->real_dev->priv == (void *)card))
3733                 return 0;
3734
3735 #endif
3736         return rc;
3737 }
3738
3739 static int
3740 qeth_verify_dev(struct net_device *dev)
3741 {
3742         struct qeth_card *card;
3743         unsigned long flags;
3744         int rc = 0;
3745
3746         read_lock_irqsave(&qeth_card_list.rwlock, flags);
3747         list_for_each_entry(card, &qeth_card_list.list, list){
3748                 if (card->dev == dev){
3749                         rc = QETH_REAL_CARD;
3750                         break;
3751                 }
3752                 rc = qeth_verify_vlan_dev(dev, card);
3753                 if (rc)
3754                         break;
3755         }
3756         read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
3757
3758         return rc;
3759 }
3760
3761 static struct qeth_card *
3762 qeth_get_card_from_dev(struct net_device *dev)
3763 {
3764         struct qeth_card *card = NULL;
3765         int rc;
3766
3767         rc = qeth_verify_dev(dev);
3768         if (rc == QETH_REAL_CARD)
3769                 card = (struct qeth_card *)dev->priv;
3770         else if (rc == QETH_VLAN_CARD)
3771                 card = (struct qeth_card *)
3772                         VLAN_DEV_INFO(dev)->real_dev->priv;
3773
3774         QETH_DBF_TEXT_(trace, 4, "%d", rc);
3775         return card ;
3776 }
3777
3778 static void
3779 qeth_tx_timeout(struct net_device *dev)
3780 {
3781         struct qeth_card *card;
3782
3783         card = (struct qeth_card *) dev->priv;
3784         card->stats.tx_errors++;
3785         qeth_schedule_recovery(card);
3786 }
3787
3788 static int
3789 qeth_open(struct net_device *dev)
3790 {
3791         struct qeth_card *card;
3792
3793         QETH_DBF_TEXT(trace, 4, "qethopen");
3794
3795         card = (struct qeth_card *) dev->priv;
3796
3797         if (card->state != CARD_STATE_SOFTSETUP)
3798                 return -ENODEV;
3799
3800         if ( (card->info.type != QETH_CARD_TYPE_OSN) &&
3801              (card->options.layer2) &&
3802              (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))) {
3803                 QETH_DBF_TEXT(trace,4,"nomacadr");
3804                 return -EPERM;
3805         }
3806         card->dev->flags |= IFF_UP;
3807         netif_start_queue(dev);
3808         card->data.state = CH_STATE_UP;
3809         card->state = CARD_STATE_UP;
3810
3811         if (!card->lan_online && netif_carrier_ok(dev))
3812                 netif_carrier_off(dev);
3813         return 0;
3814 }
3815
3816 static int
3817 qeth_stop(struct net_device *dev)
3818 {
3819         struct qeth_card *card;
3820
3821         QETH_DBF_TEXT(trace, 4, "qethstop");
3822
3823         card = (struct qeth_card *) dev->priv;
3824
3825         netif_stop_queue(dev);
3826         card->dev->flags &= ~IFF_UP;
3827         if (card->state == CARD_STATE_UP)
3828                 card->state = CARD_STATE_SOFTSETUP;
3829         return 0;
3830 }
3831
3832 static inline int
3833 qeth_get_cast_type(struct qeth_card *card, struct sk_buff *skb)
3834 {
3835         int cast_type = RTN_UNSPEC;
3836
3837         if (card->info.type == QETH_CARD_TYPE_OSN)
3838                 return cast_type;
3839
3840         if (skb->dst && skb->dst->neighbour){
3841                 cast_type = skb->dst->neighbour->type;
3842                 if ((cast_type == RTN_BROADCAST) ||
3843                     (cast_type == RTN_MULTICAST) ||
3844                     (cast_type == RTN_ANYCAST))
3845                         return cast_type;
3846                 else
3847                         return RTN_UNSPEC;
3848         }
3849         /* try something else */
3850         if (skb->protocol == ETH_P_IPV6)
3851                 return (skb->nh.raw[24] == 0xff) ? RTN_MULTICAST : 0;
3852         else if (skb->protocol == ETH_P_IP)
3853                 return ((skb->nh.raw[16] & 0xf0) == 0xe0) ? RTN_MULTICAST : 0;
3854         /* ... */
3855         if (!memcmp(skb->data, skb->dev->broadcast, 6))
3856                 return RTN_BROADCAST;
3857         else {
3858                 u16 hdr_mac;
3859
3860                 hdr_mac = *((u16 *)skb->data);
3861                 /* tr multicast? */
3862                 switch (card->info.link_type) {
3863                 case QETH_LINK_TYPE_HSTR:
3864                 case QETH_LINK_TYPE_LANE_TR:
3865                         if ((hdr_mac == QETH_TR_MAC_NC) ||
3866                             (hdr_mac == QETH_TR_MAC_C))
3867                                 return RTN_MULTICAST;
3868                         break;
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 tx_bytes = skb->len;
4424         int rc;
4425
4426         QETH_DBF_TEXT(trace, 6, "sendpkt");
4427
4428         if (!card->options.layer2) {
4429                 ipv = qeth_get_ip_version(skb);
4430                 if ((card->dev->hard_header == qeth_fake_header) && ipv) {
4431                         if ((skb = qeth_pskb_unshare(skb,GFP_ATOMIC)) == NULL) {
4432                                 card->stats.tx_dropped++;
4433                                 dev_kfree_skb_irq(skb);
4434                                 return 0;
4435                         }
4436                         if(card->dev->type == ARPHRD_IEEE802_TR){
4437                                 skb_pull(skb, QETH_FAKE_LL_LEN_TR);
4438                         } else {
4439                                 skb_pull(skb, QETH_FAKE_LL_LEN_ETH);
4440                         }
4441                 }
4442         }
4443         if ((card->info.type == QETH_CARD_TYPE_OSN) &&
4444                 (skb->protocol == htons(ETH_P_IPV6))) {
4445                 dev_kfree_skb_any(skb);
4446                 return 0;
4447         }
4448         cast_type = qeth_get_cast_type(card, skb);
4449         if ((cast_type == RTN_BROADCAST) && 
4450             (card->info.broadcast_capable == 0)){
4451                 card->stats.tx_dropped++;
4452                 card->stats.tx_errors++;
4453                 dev_kfree_skb_any(skb);
4454                 return NETDEV_TX_OK;
4455         }
4456         queue = card->qdio.out_qs
4457                 [qeth_get_priority_queue(card, skb, ipv, cast_type)];
4458
4459         if (skb_shinfo(skb)->tso_size)
4460                 large_send = card->options.large_send;
4461
4462         /*are we able to do TSO ? If so ,prepare and send it from here */
4463         if ((large_send == QETH_LARGE_SEND_TSO) &&
4464             (cast_type == RTN_UNSPEC)) {
4465                 rc = qeth_tso_prepare_packet(card, skb, ipv, cast_type);
4466                 if (rc) {
4467                         card->stats.tx_dropped++;
4468                         card->stats.tx_errors++;
4469                         dev_kfree_skb_any(skb);
4470                         return NETDEV_TX_OK;
4471                 } 
4472                 elements_needed++;
4473         } else {
4474                 if ((rc = qeth_prepare_skb(card, &skb, &hdr, ipv))) {
4475                         QETH_DBF_TEXT_(trace, 4, "pskbe%d", rc);
4476                         return rc;
4477                 }
4478                 if (card->info.type != QETH_CARD_TYPE_OSN)
4479                         qeth_fill_header(card, hdr, skb, ipv, cast_type);
4480         }
4481
4482         if (large_send == QETH_LARGE_SEND_EDDP) {
4483                 ctx = qeth_eddp_create_context(card, skb, hdr);
4484                 if (ctx == NULL) {
4485                         PRINT_WARN("could not create eddp context\n");
4486                         return -EINVAL;
4487                 }
4488         } else {
4489                 int elems = qeth_get_elements_no(card,(void*) hdr, skb,
4490                                                  elements_needed);
4491                 if (!elems)
4492                         return -EINVAL;
4493                 elements_needed += elems;
4494         }
4495
4496         if (card->info.type != QETH_CARD_TYPE_IQD)
4497                 rc = qeth_do_send_packet(card, queue, skb, hdr,
4498                                          elements_needed, ctx);
4499         else
4500                 rc = qeth_do_send_packet_fast(card, queue, skb, hdr,
4501                                               elements_needed, ctx);
4502         if (!rc){
4503                 card->stats.tx_packets++;
4504                 card->stats.tx_bytes += tx_bytes;
4505 #ifdef CONFIG_QETH_PERF_STATS
4506                 if (skb_shinfo(skb)->tso_size &&
4507                    !(large_send == QETH_LARGE_SEND_NO)) {
4508                         card->perf_stats.large_send_bytes += skb->len;
4509                         card->perf_stats.large_send_cnt++;
4510                 }
4511                 if (skb_shinfo(skb)->nr_frags > 0){
4512                         card->perf_stats.sg_skbs_sent++;
4513                         /* nr_frags + skb->data */
4514                         card->perf_stats.sg_frags_sent +=
4515                                 skb_shinfo(skb)->nr_frags + 1;
4516                 }
4517 #endif /* CONFIG_QETH_PERF_STATS */
4518         }
4519         if (ctx != NULL) {
4520                 /* drop creator's reference */
4521                 qeth_eddp_put_context(ctx);
4522                 /* free skb; it's not referenced by a buffer */
4523                 if (rc == 0)
4524                         dev_kfree_skb_any(skb);
4525
4526         }
4527         return rc;
4528 }
4529
4530 static int
4531 qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4532 {
4533         struct qeth_card *card = (struct qeth_card *) dev->priv;
4534         int rc = 0;
4535
4536         switch(regnum){
4537         case MII_BMCR: /* Basic mode control register */
4538                 rc = BMCR_FULLDPLX;
4539                 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH)&&
4540                     (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4541                     (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
4542                         rc |= BMCR_SPEED100;
4543                 break;
4544         case MII_BMSR: /* Basic mode status register */
4545                 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4546                      BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4547                      BMSR_100BASE4;
4548                 break;
4549         case MII_PHYSID1: /* PHYS ID 1 */
4550                 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4551                      dev->dev_addr[2];
4552                 rc = (rc >> 5) & 0xFFFF;
4553                 break;
4554         case MII_PHYSID2: /* PHYS ID 2 */
4555                 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4556                 break;
4557         case MII_ADVERTISE: /* Advertisement control reg */
4558                 rc = ADVERTISE_ALL;
4559                 break;
4560         case MII_LPA: /* Link partner ability reg */
4561                 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4562                      LPA_100BASE4 | LPA_LPACK;
4563                 break;
4564         case MII_EXPANSION: /* Expansion register */
4565                 break;
4566         case MII_DCOUNTER: /* disconnect counter */
4567                 break;
4568         case MII_FCSCOUNTER: /* false carrier counter */
4569                 break;
4570         case MII_NWAYTEST: /* N-way auto-neg test register */
4571                 break;
4572         case MII_RERRCOUNTER: /* rx error counter */
4573                 rc = card->stats.rx_errors;
4574                 break;
4575         case MII_SREVISION: /* silicon revision */
4576                 break;
4577         case MII_RESV1: /* reserved 1 */
4578                 break;
4579         case MII_LBRERROR: /* loopback, rx, bypass error */
4580                 break;
4581         case MII_PHYADDR: /* physical address */
4582                 break;
4583         case MII_RESV2: /* reserved 2 */
4584                 break;
4585         case MII_TPISTATUS: /* TPI status for 10mbps */
4586                 break;
4587         case MII_NCONFIG: /* network interface config */
4588                 break;
4589         default:
4590                 break;
4591         }
4592         return rc;
4593 }
4594
4595
4596 static inline const char *
4597 qeth_arp_get_error_cause(int *rc)
4598 {
4599         switch (*rc) {
4600         case QETH_IPA_ARP_RC_FAILED:
4601                 *rc = -EIO;
4602                 return "operation failed";
4603         case QETH_IPA_ARP_RC_NOTSUPP:
4604                 *rc = -EOPNOTSUPP;
4605                 return "operation not supported";
4606         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
4607                 *rc = -EINVAL;
4608                 return "argument out of range";
4609         case QETH_IPA_ARP_RC_Q_NOTSUPP:
4610                 *rc = -EOPNOTSUPP;
4611                 return "query operation not supported";
4612         case QETH_IPA_ARP_RC_Q_NO_DATA:
4613                 *rc = -ENOENT;
4614                 return "no query data available";
4615         default:
4616                 return "unknown error";
4617         }
4618 }
4619
4620 static int
4621 qeth_send_simple_setassparms(struct qeth_card *, enum qeth_ipa_funcs,
4622                              __u16, long);
4623
4624 static int
4625 qeth_arp_set_no_entries(struct qeth_card *card, int no_entries)
4626 {
4627         int tmp;
4628         int rc;
4629
4630         QETH_DBF_TEXT(trace,3,"arpstnoe");
4631
4632         /*
4633          * currently GuestLAN only supports the ARP assist function
4634          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
4635          * thus we say EOPNOTSUPP for this ARP function
4636          */
4637         if (card->info.guestlan)
4638                 return -EOPNOTSUPP;
4639         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
4640                 PRINT_WARN("ARP processing not supported "
4641                            "on %s!\n", QETH_CARD_IFNAME(card));
4642                 return -EOPNOTSUPP;
4643         }
4644         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
4645                                           IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
4646                                           no_entries);
4647         if (rc) {
4648                 tmp = rc;
4649                 PRINT_WARN("Could not set number of ARP entries on %s: "
4650                            "%s (0x%x/%d)\n",
4651                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
4652                            tmp, tmp);
4653         }
4654         return rc;
4655 }
4656
4657 static inline void
4658 qeth_copy_arp_entries_stripped(struct qeth_arp_query_info *qinfo,
4659                                struct qeth_arp_query_data *qdata,
4660                                int entry_size, int uentry_size)
4661 {
4662         char *entry_ptr;
4663         char *uentry_ptr;
4664         int i;
4665
4666         entry_ptr = (char *)&qdata->data;
4667         uentry_ptr = (char *)(qinfo->udata + qinfo->udata_offset);
4668         for (i = 0; i < qdata->no_entries; ++i){
4669                 /* strip off 32 bytes "media specific information" */
4670                 memcpy(uentry_ptr, (entry_ptr + 32), entry_size - 32);
4671                 entry_ptr += entry_size;
4672                 uentry_ptr += uentry_size;
4673         }
4674 }
4675
4676 static int
4677 qeth_arp_query_cb(struct qeth_card *card, struct qeth_reply *reply,
4678                   unsigned long data)
4679 {
4680         struct qeth_ipa_cmd *cmd;
4681         struct qeth_arp_query_data *qdata;
4682         struct qeth_arp_query_info *qinfo;
4683         int entry_size;
4684         int uentry_size;
4685         int i;
4686
4687         QETH_DBF_TEXT(trace,4,"arpquecb");
4688
4689         qinfo = (struct qeth_arp_query_info *) reply->param;
4690         cmd = (struct qeth_ipa_cmd *) data;
4691         if (cmd->hdr.return_code) {
4692                 QETH_DBF_TEXT_(trace,4,"qaer1%i", cmd->hdr.return_code);
4693                 return 0;
4694         }
4695         if (cmd->data.setassparms.hdr.return_code) {
4696                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
4697                 QETH_DBF_TEXT_(trace,4,"qaer2%i", cmd->hdr.return_code);
4698                 return 0;
4699         }
4700         qdata = &cmd->data.setassparms.data.query_arp;
4701         switch(qdata->reply_bits){
4702         case 5:
4703                 uentry_size = entry_size = sizeof(struct qeth_arp_qi_entry5);
4704                 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4705                         uentry_size = sizeof(struct qeth_arp_qi_entry5_short);
4706                 break;
4707         case 7:
4708                 /* fall through to default */
4709         default:
4710                 /* tr is the same as eth -> entry7 */
4711                 uentry_size = entry_size = sizeof(struct qeth_arp_qi_entry7);
4712                 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4713                         uentry_size = sizeof(struct qeth_arp_qi_entry7_short);
4714                 break;
4715         }
4716         /* check if there is enough room in userspace */
4717         if ((qinfo->udata_len - qinfo->udata_offset) <
4718                         qdata->no_entries * uentry_size){
4719                 QETH_DBF_TEXT_(trace, 4, "qaer3%i", -ENOMEM);
4720                 cmd->hdr.return_code = -ENOMEM;
4721                 PRINT_WARN("query ARP user space buffer is too small for "
4722                            "the returned number of ARP entries. "
4723                            "Aborting query!\n");
4724                 goto out_error;
4725         }
4726         QETH_DBF_TEXT_(trace, 4, "anore%i",
4727                        cmd->data.setassparms.hdr.number_of_replies);
4728         QETH_DBF_TEXT_(trace, 4, "aseqn%i", cmd->data.setassparms.hdr.seq_no);
4729         QETH_DBF_TEXT_(trace, 4, "anoen%i", qdata->no_entries);
4730
4731         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) {
4732                 /* strip off "media specific information" */
4733                 qeth_copy_arp_entries_stripped(qinfo, qdata, entry_size,
4734                                                uentry_size);
4735         } else
4736                 /*copy entries to user buffer*/
4737                 memcpy(qinfo->udata + qinfo->udata_offset,
4738                        (char *)&qdata->data, qdata->no_entries*uentry_size);
4739
4740         qinfo->no_entries += qdata->no_entries;
4741         qinfo->udata_offset += (qdata->no_entries*uentry_size);
4742         /* check if all replies received ... */
4743         if (cmd->data.setassparms.hdr.seq_no <
4744             cmd->data.setassparms.hdr.number_of_replies)
4745                 return 1;
4746         memcpy(qinfo->udata, &qinfo->no_entries, 4);
4747         /* keep STRIP_ENTRIES flag so the user program can distinguish
4748          * stripped entries from normal ones */
4749         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
4750                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
4751         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET,&qdata->reply_bits,2);
4752         return 0;
4753 out_error:
4754         i = 0;
4755         memcpy(qinfo->udata, &i, 4);
4756         return 0;
4757 }
4758
4759 static int
4760 qeth_send_ipa_arp_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
4761                       int len, int (*reply_cb)(struct qeth_card *,
4762                                                struct qeth_reply *,
4763                                                unsigned long),
4764                       void *reply_param)
4765 {
4766         QETH_DBF_TEXT(trace,4,"sendarp");
4767
4768         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4769         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4770                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4771         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4772                                       reply_cb, reply_param);
4773 }
4774
4775 static int
4776 qeth_send_ipa_snmp_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
4777                       int len, int (*reply_cb)(struct qeth_card *,
4778                                                struct qeth_reply *,
4779                                                unsigned long),
4780                       void *reply_param)
4781 {
4782         u16 s1, s2;
4783
4784         QETH_DBF_TEXT(trace,4,"sendsnmp");
4785
4786         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4787         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4788                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4789         /* adjust PDU length fields in IPA_PDU_HEADER */
4790         s1 = (u32) IPA_PDU_HEADER_SIZE + len;
4791         s2 = (u32) len;
4792         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
4793         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
4794         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
4795         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
4796         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4797                                       reply_cb, reply_param);
4798 }
4799
4800 static struct qeth_cmd_buffer *
4801 qeth_get_setassparms_cmd(struct qeth_card *, enum qeth_ipa_funcs,
4802                          __u16, __u16, enum qeth_prot_versions);
4803 static int
4804 qeth_arp_query(struct qeth_card *card, char *udata)
4805 {
4806         struct qeth_cmd_buffer *iob;
4807         struct qeth_arp_query_info qinfo = {0, };
4808         int tmp;
4809         int rc;
4810
4811         QETH_DBF_TEXT(trace,3,"arpquery");
4812
4813         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
4814                                IPA_ARP_PROCESSING)) {
4815                 PRINT_WARN("ARP processing not supported "
4816                            "on %s!\n", QETH_CARD_IFNAME(card));
4817                 return -EOPNOTSUPP;
4818         }
4819         /* get size of userspace buffer and mask_bits -> 6 bytes */
4820         if (copy_from_user(&qinfo, udata, 6))
4821                 return -EFAULT;
4822         if (!(qinfo.udata = kmalloc(qinfo.udata_len, GFP_KERNEL)))
4823                 return -ENOMEM;
4824         memset(qinfo.udata, 0, qinfo.udata_len);
4825         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
4826         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
4827                                        IPA_CMD_ASS_ARP_QUERY_INFO,
4828                                        sizeof(int),QETH_PROT_IPV4);
4829
4830         rc = qeth_send_ipa_arp_cmd(card, iob,
4831                                    QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
4832                                    qeth_arp_query_cb, (void *)&qinfo);
4833         if (rc) {
4834                 tmp = rc;
4835                 PRINT_WARN("Error while querying ARP cache on %s: %s "
4836                            "(0x%x/%d)\n",
4837                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
4838                            tmp, tmp);
4839                 copy_to_user(udata, qinfo.udata, 4);
4840         } else {
4841                 copy_to_user(udata, qinfo.udata, qinfo.udata_len);
4842         }
4843         kfree(qinfo.udata);
4844         return rc;
4845 }
4846
4847 /**
4848  * SNMP command callback
4849  */
4850 static int
4851 qeth_snmp_command_cb(struct qeth_card *card, struct qeth_reply *reply,
4852                      unsigned long sdata)
4853 {
4854         struct qeth_ipa_cmd *cmd;
4855         struct qeth_arp_query_info *qinfo;
4856         struct qeth_snmp_cmd *snmp;
4857         unsigned char *data;
4858         __u16 data_len;
4859
4860         QETH_DBF_TEXT(trace,3,"snpcmdcb");
4861
4862         cmd = (struct qeth_ipa_cmd *) sdata;
4863         data = (unsigned char *)((char *)cmd - reply->offset);
4864         qinfo = (struct qeth_arp_query_info *) reply->param;
4865         snmp = &cmd->data.setadapterparms.data.snmp;
4866
4867         if (cmd->hdr.return_code) {
4868                 QETH_DBF_TEXT_(trace,4,"scer1%i", cmd->hdr.return_code);
4869                 return 0;
4870         }
4871         if (cmd->data.setadapterparms.hdr.return_code) {
4872                 cmd->hdr.return_code = cmd->data.setadapterparms.hdr.return_code;
4873                 QETH_DBF_TEXT_(trace,4,"scer2%i", cmd->hdr.return_code);
4874                 return 0;
4875         }
4876         data_len = *((__u16*)QETH_IPA_PDU_LEN_PDU1(data));
4877         if (cmd->data.setadapterparms.hdr.seq_no == 1)
4878                 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
4879         else
4880                 data_len -= (__u16)((char*)&snmp->request - (char *)cmd);
4881
4882         /* check if there is enough room in userspace */
4883         if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4884                 QETH_DBF_TEXT_(trace, 4, "scer3%i", -ENOMEM);
4885                 cmd->hdr.return_code = -ENOMEM;
4886                 return 0;
4887         }
4888         QETH_DBF_TEXT_(trace, 4, "snore%i",
4889                        cmd->data.setadapterparms.hdr.used_total);
4890         QETH_DBF_TEXT_(trace, 4, "sseqn%i", cmd->data.setadapterparms.hdr.seq_no);
4891         /*copy entries to user buffer*/
4892         if (cmd->data.setadapterparms.hdr.seq_no == 1) {
4893                 memcpy(qinfo->udata + qinfo->udata_offset,
4894                        (char *)snmp,
4895                        data_len + offsetof(struct qeth_snmp_cmd,data));
4896                 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
4897         } else {
4898                 memcpy(qinfo->udata + qinfo->udata_offset,
4899                        (char *)&snmp->request, data_len);
4900         }
4901         qinfo->udata_offset += data_len;
4902         /* check if all replies received ... */
4903                 QETH_DBF_TEXT_(trace, 4, "srtot%i",
4904                                cmd->data.setadapterparms.hdr.used_total);
4905                 QETH_DBF_TEXT_(trace, 4, "srseq%i",
4906                                cmd->data.setadapterparms.hdr.seq_no);
4907         if (cmd->data.setadapterparms.hdr.seq_no <
4908             cmd->data.setadapterparms.hdr.used_total)
4909                 return 1;
4910         return 0;
4911 }
4912
4913 static struct qeth_cmd_buffer *
4914 qeth_get_ipacmd_buffer(struct qeth_card *, enum qeth_ipa_cmds,
4915                        enum qeth_prot_versions );
4916
4917 static struct qeth_cmd_buffer *
4918 qeth_get_adapter_cmd(struct qeth_card *card, __u32 command, __u32 cmdlen)
4919 {
4920         struct qeth_cmd_buffer *iob;
4921         struct qeth_ipa_cmd *cmd;
4922
4923         iob = qeth_get_ipacmd_buffer(card,IPA_CMD_SETADAPTERPARMS,
4924                                      QETH_PROT_IPV4);
4925         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4926         cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
4927         cmd->data.setadapterparms.hdr.command_code = command;
4928         cmd->data.setadapterparms.hdr.used_total = 1;
4929         cmd->data.setadapterparms.hdr.seq_no = 1;
4930
4931         return iob;
4932 }
4933
4934 /**
4935  * function to send SNMP commands to OSA-E card
4936  */
4937 static int
4938 qeth_snmp_command(struct qeth_card *card, char *udata)
4939 {
4940         struct qeth_cmd_buffer *iob;
4941         struct qeth_ipa_cmd *cmd;
4942         struct qeth_snmp_ureq *ureq;
4943         int req_len;
4944         struct qeth_arp_query_info qinfo = {0, };
4945         int rc = 0;
4946
4947         QETH_DBF_TEXT(trace,3,"snmpcmd");
4948
4949         if (card->info.guestlan)
4950                 return -EOPNOTSUPP;
4951
4952         if ((!qeth_adp_supported(card,IPA_SETADP_SET_SNMP_CONTROL)) &&
4953             (!card->options.layer2) ) {
4954                 PRINT_WARN("SNMP Query MIBS not supported "
4955                            "on %s!\n", QETH_CARD_IFNAME(card));
4956                 return -EOPNOTSUPP;
4957         }
4958         /* skip 4 bytes (data_len struct member) to get req_len */
4959         if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
4960                 return -EFAULT;
4961         ureq = kmalloc(req_len+sizeof(struct qeth_snmp_ureq_hdr), GFP_KERNEL);
4962         if (!ureq) {
4963                 QETH_DBF_TEXT(trace, 2, "snmpnome");
4964                 return -ENOMEM;
4965         }
4966         if (copy_from_user(ureq, udata,
4967                         req_len+sizeof(struct qeth_snmp_ureq_hdr))){
4968                 kfree(ureq);
4969                 return -EFAULT;
4970         }
4971         qinfo.udata_len = ureq->hdr.data_len;
4972         if (!(qinfo.udata = kmalloc(qinfo.udata_len, GFP_KERNEL))){
4973                 kfree(ureq);
4974                 return -ENOMEM;
4975         }
4976         memset(qinfo.udata, 0, qinfo.udata_len);
4977         qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4978
4979         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
4980                                    QETH_SNMP_SETADP_CMDLENGTH + req_len);
4981         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4982         memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
4983         rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
4984                                     qeth_snmp_command_cb, (void *)&qinfo);
4985         if (rc)
4986                 PRINT_WARN("SNMP command failed on %s: (0x%x)\n",
4987                            QETH_CARD_IFNAME(card), rc);
4988          else
4989                 copy_to_user(udata, qinfo.udata, qinfo.udata_len);
4990
4991         kfree(ureq);
4992         kfree(qinfo.udata);
4993         return rc;
4994 }
4995
4996 static int
4997 qeth_default_setassparms_cb(struct qeth_card *, struct qeth_reply *,
4998                             unsigned long);
4999
5000 static int
5001 qeth_default_setadapterparms_cb(struct qeth_card *card,
5002                                 struct qeth_reply *reply,
5003                                 unsigned long data);
5004 static int
5005 qeth_send_setassparms(struct qeth_card *, struct qeth_cmd_buffer *,
5006                       __u16, long,
5007                       int (*reply_cb)
5008                       (struct qeth_card *, struct qeth_reply *, unsigned long),
5009                       void *reply_param);
5010
5011 static int
5012 qeth_arp_add_entry(struct qeth_card *card, struct qeth_arp_cache_entry *entry)
5013 {
5014         struct qeth_cmd_buffer *iob;
5015         char buf[16];
5016         int tmp;
5017         int rc;
5018
5019         QETH_DBF_TEXT(trace,3,"arpadent");
5020
5021         /*
5022          * currently GuestLAN only supports the ARP assist function
5023          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
5024          * thus we say EOPNOTSUPP for this ARP function
5025          */
5026         if (card->info.guestlan)
5027                 return -EOPNOTSUPP;
5028         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5029                 PRINT_WARN("ARP processing not supported "
5030                            "on %s!\n", QETH_CARD_IFNAME(card));
5031                 return -EOPNOTSUPP;
5032         }
5033
5034         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
5035                                        IPA_CMD_ASS_ARP_ADD_ENTRY,
5036                                        sizeof(struct qeth_arp_cache_entry),
5037                                        QETH_PROT_IPV4);
5038         rc = qeth_send_setassparms(card, iob,
5039                                    sizeof(struct qeth_arp_cache_entry),
5040                                    (unsigned long) entry,
5041                                    qeth_default_setassparms_cb, NULL);
5042         if (rc) {
5043                 tmp = rc;
5044                 qeth_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
5045                 PRINT_WARN("Could not add ARP entry for address %s on %s: "
5046                            "%s (0x%x/%d)\n",
5047                            buf, QETH_CARD_IFNAME(card),
5048                            qeth_arp_get_error_cause(&rc), tmp, tmp);
5049         }
5050         return rc;
5051 }
5052
5053 static int
5054 qeth_arp_remove_entry(struct qeth_card *card, struct qeth_arp_cache_entry *entry)
5055 {
5056         struct qeth_cmd_buffer *iob;
5057         char buf[16] = {0, };
5058         int tmp;
5059         int rc;
5060
5061         QETH_DBF_TEXT(trace,3,"arprment");
5062
5063         /*
5064          * currently GuestLAN only supports the ARP assist function
5065          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
5066          * thus we say EOPNOTSUPP for this ARP function
5067          */
5068         if (card->info.guestlan)
5069                 return -EOPNOTSUPP;
5070         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5071                 PRINT_WARN("ARP processing not supported "
5072                            "on %s!\n", QETH_CARD_IFNAME(card));
5073                 return -EOPNOTSUPP;
5074         }
5075         memcpy(buf, entry, 12);
5076         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
5077                                        IPA_CMD_ASS_ARP_REMOVE_ENTRY,
5078                                        12,
5079                                        QETH_PROT_IPV4);
5080         rc = qeth_send_setassparms(card, iob,
5081                                    12, (unsigned long)buf,
5082                                    qeth_default_setassparms_cb, NULL);
5083         if (rc) {
5084                 tmp = rc;
5085                 memset(buf, 0, 16);
5086                 qeth_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
5087                 PRINT_WARN("Could not delete ARP entry for address %s on %s: "
5088                            "%s (0x%x/%d)\n",
5089                            buf, QETH_CARD_IFNAME(card),
5090                            qeth_arp_get_error_cause(&rc), tmp, tmp);
5091         }
5092         return rc;
5093 }
5094
5095 static int
5096 qeth_arp_flush_cache(struct qeth_card *card)
5097 {
5098         int rc;
5099         int tmp;
5100
5101         QETH_DBF_TEXT(trace,3,"arpflush");
5102
5103         /*
5104          * currently GuestLAN only supports the ARP assist function
5105          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
5106          * thus we say EOPNOTSUPP for this ARP function
5107         */
5108         if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
5109                 return -EOPNOTSUPP;
5110         if (!qeth_is_supported(card,IPA_ARP_PROCESSING)) {
5111                 PRINT_WARN("ARP processing not supported "
5112                            "on %s!\n", QETH_CARD_IFNAME(card));
5113                 return -EOPNOTSUPP;
5114         }
5115         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
5116                                           IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
5117         if (rc){
5118                 tmp = rc;
5119                 PRINT_WARN("Could not flush ARP cache on %s: %s (0x%x/%d)\n",
5120                            QETH_CARD_IFNAME(card), qeth_arp_get_error_cause(&rc),
5121                            tmp, tmp);
5122         }
5123         return rc;
5124 }
5125
5126 static int
5127 qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5128 {
5129         struct qeth_card *card = (struct qeth_card *)dev->priv;
5130         struct qeth_arp_cache_entry arp_entry;
5131         struct mii_ioctl_data *mii_data;
5132         int rc = 0;
5133
5134         if (!card)
5135                 return -ENODEV;
5136
5137         if ((card->state != CARD_STATE_UP) &&
5138             (card->state != CARD_STATE_SOFTSETUP))
5139                 return -ENODEV;
5140
5141         if (card->info.type == QETH_CARD_TYPE_OSN)
5142                 return -EPERM;
5143
5144         switch (cmd){
5145         case SIOC_QETH_ARP_SET_NO_ENTRIES:
5146                 if ( !capable(CAP_NET_ADMIN) ||
5147                      (card->options.layer2) ) {
5148                         rc = -EPERM;
5149                         break;
5150                 }
5151                 rc = qeth_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
5152                 break;
5153         case SIOC_QETH_ARP_QUERY_INFO:
5154                 if ( !capable(CAP_NET_ADMIN) ||
5155                      (card->options.layer2) ) {
5156                         rc = -EPERM;
5157                         break;
5158                 }
5159                 rc = qeth_arp_query(card, rq->ifr_ifru.ifru_data);
5160                 break;
5161         case SIOC_QETH_ARP_ADD_ENTRY:
5162                 if ( !capable(CAP_NET_ADMIN) ||
5163                      (card->options.layer2) ) {
5164                         rc = -EPERM;
5165                         break;
5166                 }
5167                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
5168                                    sizeof(struct qeth_arp_cache_entry)))
5169                         rc = -EFAULT;
5170                 else
5171                         rc = qeth_arp_add_entry(card, &arp_entry);
5172                 break;
5173         case SIOC_QETH_ARP_REMOVE_ENTRY:
5174                 if ( !capable(CAP_NET_ADMIN) ||
5175                      (card->options.layer2) ) {
5176                         rc = -EPERM;
5177                         break;
5178                 }
5179                 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
5180                                    sizeof(struct qeth_arp_cache_entry)))
5181                         rc = -EFAULT;
5182                 else
5183                         rc = qeth_arp_remove_entry(card, &arp_entry);
5184                 break;
5185         case SIOC_QETH_ARP_FLUSH_CACHE:
5186                 if ( !capable(CAP_NET_ADMIN) ||
5187                      (card->options.layer2) ) {
5188                         rc = -EPERM;
5189                         break;
5190                 }
5191                 rc = qeth_arp_flush_cache(card);
5192                 break;
5193         case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5194                 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5195                 break;
5196         case SIOC_QETH_GET_CARD_TYPE:
5197                 if ((card->info.type == QETH_CARD_TYPE_OSAE) &&
5198                     !card->info.guestlan)
5199                         return 1;
5200                 return 0;
5201                 break;
5202         case SIOCGMIIPHY:
5203                 mii_data = if_mii(rq);
5204                 mii_data->phy_id = 0;
5205                 break;
5206         case SIOCGMIIREG:
5207                 mii_data = if_mii(rq);
5208                 if (mii_data->phy_id != 0)
5209                         rc = -EINVAL;
5210                 else
5211                         mii_data->val_out = qeth_mdio_read(dev,mii_data->phy_id,
5212                                                            mii_data->reg_num);
5213                 break;
5214         default:
5215                 rc = -EOPNOTSUPP;
5216         }
5217         if (rc)
5218                 QETH_DBF_TEXT_(trace, 2, "ioce%d", rc);
5219         return rc;
5220 }
5221
5222 static struct net_device_stats *
5223 qeth_get_stats(struct net_device *dev)
5224 {
5225         struct qeth_card *card;
5226
5227         card = (struct qeth_card *) (dev->priv);
5228
5229         QETH_DBF_TEXT(trace,5,"getstat");
5230
5231         return &card->stats;
5232 }
5233
5234 static int
5235 qeth_change_mtu(struct net_device *dev, int new_mtu)
5236 {
5237         struct qeth_card *card;
5238         char dbf_text[15];
5239
5240         card = (struct qeth_card *) (dev->priv);
5241
5242         QETH_DBF_TEXT(trace,4,"chgmtu");
5243         sprintf(dbf_text, "%8x", new_mtu);
5244         QETH_DBF_TEXT(trace,4,dbf_text);
5245
5246         if (new_mtu < 64)
5247                 return -EINVAL;
5248         if (new_mtu > 65535)
5249                 return -EINVAL;
5250         if ((!qeth_is_supported(card,IPA_IP_FRAGMENTATION)) &&
5251             (!qeth_mtu_is_valid(card, new_mtu)))
5252                 return -EINVAL;
5253         dev->mtu = new_mtu;
5254         return 0;
5255 }
5256
5257 #ifdef CONFIG_QETH_VLAN
5258 static void
5259 qeth_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
5260 {
5261         struct qeth_card *card;
5262         unsigned long flags;
5263
5264         QETH_DBF_TEXT(trace,4,"vlanreg");
5265
5266         card = (struct qeth_card *) dev->priv;
5267         spin_lock_irqsave(&card->vlanlock, flags);
5268         card->vlangrp = grp;
5269         spin_unlock_irqrestore(&card->vlanlock, flags);
5270 }
5271
5272 static inline void
5273 qeth_free_vlan_buffer(struct qeth_card *card, struct qeth_qdio_out_buffer *buf,
5274                       unsigned short vid)
5275 {
5276         int i;
5277         struct sk_buff *skb;
5278         struct sk_buff_head tmp_list;
5279
5280         skb_queue_head_init(&tmp_list);
5281         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i){
5282                 while ((skb = skb_dequeue(&buf->skb_list))){
5283                         if (vlan_tx_tag_present(skb) &&
5284                             (vlan_tx_tag_get(skb) == vid)) {
5285                                 atomic_dec(&skb->users);
5286                                 dev_kfree_skb(skb);
5287                         } else
5288                                 skb_queue_tail(&tmp_list, skb);
5289                 }
5290         }
5291         while ((skb = skb_dequeue(&tmp_list)))
5292                 skb_queue_tail(&buf->skb_list, skb);
5293 }
5294
5295 static void
5296 qeth_free_vlan_skbs(struct qeth_card *card, unsigned short vid)
5297 {
5298         int i, j;
5299
5300         QETH_DBF_TEXT(trace, 4, "frvlskbs");
5301         for (i = 0; i < card->qdio.no_out_queues; ++i){
5302                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
5303                         qeth_free_vlan_buffer(card, &card->qdio.
5304                                               out_qs[i]->bufs[j], vid);
5305         }
5306 }
5307
5308 static void
5309 qeth_free_vlan_addresses4(struct qeth_card *card, unsigned short vid)
5310 {
5311         struct in_device *in_dev;
5312         struct in_ifaddr *ifa;
5313         struct qeth_ipaddr *addr;
5314
5315         QETH_DBF_TEXT(trace, 4, "frvaddr4");
5316
5317         rcu_read_lock();
5318         in_dev = __in_dev_get_rcu(card->vlangrp->vlan_devices[vid]);
5319         if (!in_dev)
5320                 goto out;
5321         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
5322                 addr = qeth_get_addr_buffer(QETH_PROT_IPV4);
5323                 if (addr){
5324                         addr->u.a4.addr = ifa->ifa_address;
5325                         addr->u.a4.mask = ifa->ifa_mask;
5326                         addr->type = QETH_IP_TYPE_NORMAL;
5327                         if (!qeth_delete_ip(card, addr))
5328                                 kfree(addr);
5329                 }
5330         }
5331 out:
5332         rcu_read_unlock();
5333 }
5334
5335 static void
5336 qeth_free_vlan_addresses6(struct qeth_card *card, unsigned short vid)
5337 {
5338 #ifdef CONFIG_QETH_IPV6
5339         struct inet6_dev *in6_dev;
5340         struct inet6_ifaddr *ifa;
5341         struct qeth_ipaddr *addr;
5342
5343         QETH_DBF_TEXT(trace, 4, "frvaddr6");
5344
5345         in6_dev = in6_dev_get(card->vlangrp->vlan_devices[vid]);
5346         if (!in6_dev)
5347                 return;
5348         for (ifa = in6_dev->addr_list; ifa; ifa = ifa->lst_next){
5349                 addr = qeth_get_addr_buffer(QETH_PROT_IPV6);
5350                 if (addr){
5351                         memcpy(&addr->u.a6.addr, &ifa->addr,
5352                                sizeof(struct in6_addr));
5353                         addr->u.a6.pfxlen = ifa->prefix_len;
5354                         addr->type = QETH_IP_TYPE_NORMAL;
5355                         if (!qeth_delete_ip(card, addr))
5356                                 kfree(addr);
5357                 }
5358         }
5359         in6_dev_put(in6_dev);
5360 #endif /* CONFIG_QETH_IPV6 */
5361 }
5362
5363 static void
5364 qeth_free_vlan_addresses(struct qeth_card *card, unsigned short vid)
5365 {
5366         if (card->options.layer2 || !card->vlangrp)
5367                 return;
5368         qeth_free_vlan_addresses4(card, vid);
5369         qeth_free_vlan_addresses6(card, vid);
5370 }
5371
5372 static int
5373 qeth_layer2_send_setdelvlan_cb(struct qeth_card *card,
5374                                struct qeth_reply *reply,
5375                                unsigned long data)
5376 {
5377         struct qeth_ipa_cmd *cmd;
5378
5379         QETH_DBF_TEXT(trace, 2, "L2sdvcb");
5380         cmd = (struct qeth_ipa_cmd *) data;
5381         if (cmd->hdr.return_code) {
5382                 PRINT_ERR("Error in processing VLAN %i on %s: 0x%x. "
5383                           "Continuing\n",cmd->data.setdelvlan.vlan_id, 
5384                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
5385                 QETH_DBF_TEXT_(trace, 2, "L2VL%4x", cmd->hdr.command);
5386                 QETH_DBF_TEXT_(trace, 2, "L2%s", CARD_BUS_ID(card));
5387                 QETH_DBF_TEXT_(trace, 2, "err%d", cmd->hdr.return_code);
5388         }
5389         return 0;
5390 }
5391
5392 static int
5393 qeth_layer2_send_setdelvlan(struct qeth_card *card, __u16 i,
5394                             enum qeth_ipa_cmds ipacmd)
5395 {
5396         struct qeth_ipa_cmd *cmd;
5397         struct qeth_cmd_buffer *iob;
5398
5399         QETH_DBF_TEXT_(trace, 4, "L2sdv%x",ipacmd);
5400         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
5401         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5402         cmd->data.setdelvlan.vlan_id = i;
5403         return qeth_send_ipa_cmd(card, iob, 
5404                                  qeth_layer2_send_setdelvlan_cb, NULL);
5405 }
5406
5407 static void
5408 qeth_layer2_process_vlans(struct qeth_card *card, int clear)
5409 {
5410         unsigned short  i;
5411
5412         QETH_DBF_TEXT(trace, 3, "L2prcvln");
5413
5414         if (!card->vlangrp)
5415                 return;
5416         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5417                 if (card->vlangrp->vlan_devices[i] == NULL)
5418                         continue;
5419                 if (clear)
5420                         qeth_layer2_send_setdelvlan(card, i, IPA_CMD_DELVLAN);
5421                 else
5422                         qeth_layer2_send_setdelvlan(card, i, IPA_CMD_SETVLAN);
5423         }
5424 }
5425
5426 /*add_vid is layer 2 used only ....*/
5427 static void
5428 qeth_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
5429 {
5430         struct qeth_card *card;
5431
5432         QETH_DBF_TEXT_(trace, 4, "aid:%d", vid);
5433
5434         card = (struct qeth_card *) dev->priv;
5435         if (!card->options.layer2)
5436                 return;
5437         qeth_layer2_send_setdelvlan(card, vid, IPA_CMD_SETVLAN);
5438 }
5439
5440 /*... kill_vid used for both modes*/
5441 static void
5442 qeth_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
5443 {
5444         struct qeth_card *card;
5445         unsigned long flags;
5446
5447         QETH_DBF_TEXT_(trace, 4, "kid:%d", vid);
5448
5449         card = (struct qeth_card *) dev->priv;
5450         /* free all skbs for the vlan device */
5451         qeth_free_vlan_skbs(card, vid);
5452         spin_lock_irqsave(&card->vlanlock, flags);
5453         /* unregister IP addresses of vlan device */
5454         qeth_free_vlan_addresses(card, vid);
5455         if (card->vlangrp)
5456                 card->vlangrp->vlan_devices[vid] = NULL;
5457         spin_unlock_irqrestore(&card->vlanlock, flags);
5458         if (card->options.layer2)
5459                 qeth_layer2_send_setdelvlan(card, vid, IPA_CMD_DELVLAN);
5460         qeth_set_multicast_list(card->dev);
5461 }
5462 #endif
5463 /**
5464  * Examine hardware response to SET_PROMISC_MODE
5465  */
5466 static int
5467 qeth_setadp_promisc_mode_cb(struct qeth_card *card, 
5468                             struct qeth_reply *reply,
5469                             unsigned long data)
5470 {
5471         struct qeth_ipa_cmd *cmd;
5472         struct qeth_ipacmd_setadpparms *setparms;
5473
5474         QETH_DBF_TEXT(trace,4,"prmadpcb");
5475
5476         cmd = (struct qeth_ipa_cmd *) data;
5477         setparms = &(cmd->data.setadapterparms);
5478         
5479         qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
5480         if (cmd->hdr.return_code) { 
5481                 QETH_DBF_TEXT_(trace,4,"prmrc%2.2x",cmd->hdr.return_code);      
5482                 setparms->data.mode = SET_PROMISC_MODE_OFF;
5483         }
5484         card->info.promisc_mode = setparms->data.mode;
5485         return 0;
5486 }
5487 /*
5488  * Set promiscuous mode (on or off) (SET_PROMISC_MODE command)
5489  */
5490 static void
5491 qeth_setadp_promisc_mode(struct qeth_card *card)
5492 {
5493         enum qeth_ipa_promisc_modes mode;
5494         struct net_device *dev = card->dev;
5495         struct qeth_cmd_buffer *iob;
5496         struct qeth_ipa_cmd *cmd;
5497
5498         QETH_DBF_TEXT(trace, 4, "setprom");
5499
5500         if (((dev->flags & IFF_PROMISC) &&
5501              (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
5502             (!(dev->flags & IFF_PROMISC) &&
5503              (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
5504                 return;
5505         mode = SET_PROMISC_MODE_OFF;
5506         if (dev->flags & IFF_PROMISC)
5507                 mode = SET_PROMISC_MODE_ON;
5508         QETH_DBF_TEXT_(trace, 4, "mode:%x", mode);
5509
5510         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
5511                         sizeof(struct qeth_ipacmd_setadpparms));
5512         cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
5513         cmd->data.setadapterparms.data.mode = mode;
5514         qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
5515 }
5516
5517 /**
5518  * set multicast address on card
5519  */
5520 static void
5521 qeth_set_multicast_list(struct net_device *dev)
5522 {
5523         struct qeth_card *card = (struct qeth_card *) dev->priv;
5524
5525         if (card->info.type == QETH_CARD_TYPE_OSN)
5526                 return ;
5527          
5528         QETH_DBF_TEXT(trace, 3, "setmulti");
5529         qeth_delete_mc_addresses(card);
5530         if (card->options.layer2) {
5531                 qeth_layer2_add_multicast(card);
5532                 goto out;
5533         }
5534         qeth_add_multicast_ipv4(card);
5535 #ifdef CONFIG_QETH_IPV6
5536         qeth_add_multicast_ipv6(card);
5537 #endif
5538 out:
5539         if (qeth_set_thread_start_bit(card, QETH_SET_IP_THREAD) == 0)
5540                 schedule_work(&card->kernel_thread_starter);
5541         if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
5542                 return;
5543         if (qeth_set_thread_start_bit(card, QETH_SET_PROMISC_MODE_THREAD)==0)
5544                 schedule_work(&card->kernel_thread_starter);
5545 }
5546
5547 static int
5548 qeth_neigh_setup(struct net_device *dev, struct neigh_parms *np)
5549 {
5550         return 0;
5551 }
5552
5553 static void
5554 qeth_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
5555 {
5556         if (dev->type == ARPHRD_IEEE802_TR)
5557                 ip_tr_mc_map(ipm, mac);
5558         else
5559                 ip_eth_mc_map(ipm, mac);
5560 }
5561
5562 static struct qeth_ipaddr *
5563 qeth_get_addr_buffer(enum qeth_prot_versions prot)
5564 {
5565         struct qeth_ipaddr *addr;
5566
5567         addr = kmalloc(sizeof(struct qeth_ipaddr), GFP_ATOMIC);
5568         if (addr == NULL) {
5569                 PRINT_WARN("Not enough memory to add address\n");
5570                 return NULL;
5571         }
5572         memset(addr,0,sizeof(struct qeth_ipaddr));
5573         addr->type = QETH_IP_TYPE_NORMAL;
5574         addr->proto = prot;
5575         return addr;
5576 }
5577
5578 int
5579 qeth_osn_assist(struct net_device *dev,
5580                 void *data,
5581                 int data_len)
5582 {
5583         struct qeth_cmd_buffer *iob;
5584         struct qeth_card *card;
5585         int rc;
5586         
5587         QETH_DBF_TEXT(trace, 2, "osnsdmc");
5588         if (!dev)
5589                 return -ENODEV;
5590         card = (struct qeth_card *)dev->priv;
5591         if (!card)
5592                 return -ENODEV;
5593         if ((card->state != CARD_STATE_UP) &&
5594             (card->state != CARD_STATE_SOFTSETUP))
5595                 return -ENODEV;
5596         iob = qeth_wait_for_buffer(&card->write);
5597         memcpy(iob->data+IPA_PDU_HEADER_SIZE, data, data_len);
5598         rc = qeth_osn_send_ipa_cmd(card, iob, data_len);
5599         return rc;
5600 }
5601
5602 static struct net_device *
5603 qeth_netdev_by_devno(unsigned char *read_dev_no)
5604 {
5605         struct qeth_card *card;
5606         struct net_device *ndev;
5607         unsigned char *readno;
5608         __u16 temp_dev_no, card_dev_no;
5609         char *endp;
5610         unsigned long flags;
5611
5612         ndev = NULL;
5613         memcpy(&temp_dev_no, read_dev_no, 2);
5614         read_lock_irqsave(&qeth_card_list.rwlock, flags);
5615         list_for_each_entry(card, &qeth_card_list.list, list) {
5616                 readno = CARD_RDEV_ID(card);
5617                 readno += (strlen(readno) - 4);
5618                 card_dev_no = simple_strtoul(readno, &endp, 16);
5619                 if (card_dev_no == temp_dev_no) {
5620                         ndev = card->dev;
5621                         break;
5622                 }
5623         }
5624         read_unlock_irqrestore(&qeth_card_list.rwlock, flags);
5625         return ndev;
5626 }
5627
5628 int
5629 qeth_osn_register(unsigned char *read_dev_no,
5630                   struct net_device **dev,
5631                   int (*assist_cb)(struct net_device *, void *),
5632                   int (*data_cb)(struct sk_buff *))
5633 {
5634         struct qeth_card * card;
5635
5636         QETH_DBF_TEXT(trace, 2, "osnreg");
5637         *dev = qeth_netdev_by_devno(read_dev_no);
5638         if (*dev == NULL)
5639                 return -ENODEV;
5640         card = (struct qeth_card *)(*dev)->priv;
5641         if (!card)
5642                 return -ENODEV;
5643         if ((assist_cb == NULL) || (data_cb == NULL))
5644                 return -EINVAL;
5645         card->osn_info.assist_cb = assist_cb;
5646         card->osn_info.data_cb = data_cb;
5647         return 0;
5648 }
5649
5650 void
5651 qeth_osn_deregister(struct net_device * dev)
5652 {
5653         struct qeth_card *card;
5654
5655         QETH_DBF_TEXT(trace, 2, "osndereg");
5656         if (!dev)
5657                 return;
5658         card = (struct qeth_card *)dev->priv;
5659         if (!card)
5660                 return;
5661         card->osn_info.assist_cb = NULL;
5662         card->osn_info.data_cb = NULL;
5663         return;
5664 }
5665                                            
5666 static void
5667 qeth_delete_mc_addresses(struct qeth_card *card)
5668 {
5669         struct qeth_ipaddr *iptodo;
5670         unsigned long flags;
5671
5672         QETH_DBF_TEXT(trace,4,"delmc");
5673         iptodo = qeth_get_addr_buffer(QETH_PROT_IPV4);
5674         if (!iptodo) {
5675                 QETH_DBF_TEXT(trace, 2, "dmcnomem");
5676                 return;
5677         }
5678         iptodo->type = QETH_IP_TYPE_DEL_ALL_MC;
5679         spin_lock_irqsave(&card->ip_lock, flags);
5680         if (!__qeth_insert_ip_todo(card, iptodo, 0))
5681                 kfree(iptodo);
5682         spin_unlock_irqrestore(&card->ip_lock, flags);
5683 }
5684
5685 static inline void
5686 qeth_add_mc(struct qeth_card *card, struct in_device *in4_dev)
5687 {
5688         struct qeth_ipaddr *ipm;
5689         struct ip_mc_list *im4;
5690         char buf[MAX_ADDR_LEN];
5691
5692         QETH_DBF_TEXT(trace,4,"addmc");
5693         for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
5694                 qeth_get_mac_for_ipm(im4->multiaddr, buf, in4_dev->dev);
5695                 ipm = qeth_get_addr_buffer(QETH_PROT_IPV4);
5696                 if (!ipm)
5697                         continue;
5698                 ipm->u.a4.addr = im4->multiaddr;
5699                 memcpy(ipm->mac,buf,OSA_ADDR_LEN);
5700                 ipm->is_multicast = 1;
5701                 if (!qeth_add_ip(card,ipm))
5702                         kfree(ipm);
5703         }
5704 }
5705
5706 static inline void
5707 qeth_add_vlan_mc(struct qeth_card *card)
5708 {
5709 #ifdef CONFIG_QETH_VLAN
5710         struct in_device *in_dev;
5711         struct vlan_group *vg;
5712         int i;
5713
5714         QETH_DBF_TEXT(trace,4,"addmcvl");
5715         if ( ((card->options.layer2 == 0) &&
5716               (!qeth_is_supported(card,IPA_FULL_VLAN))) ||
5717              (card->vlangrp == NULL) )
5718                 return ;
5719
5720         vg = card->vlangrp;
5721         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5722                 if (vg->vlan_devices[i] == NULL ||
5723                     !(vg->vlan_devices[i]->flags & IFF_UP))
5724                         continue;
5725                 in_dev = in_dev_get(vg->vlan_devices[i]);
5726                 if (!in_dev)
5727                         continue;
5728                 read_lock(&in_dev->mc_list_lock);
5729                 qeth_add_mc(card,in_dev);
5730                 read_unlock(&in_dev->mc_list_lock);
5731                 in_dev_put(in_dev);
5732         }
5733 #endif
5734 }
5735
5736 static void
5737 qeth_add_multicast_ipv4(struct qeth_card *card)
5738 {
5739         struct in_device *in4_dev;
5740
5741         QETH_DBF_TEXT(trace,4,"chkmcv4");
5742         in4_dev = in_dev_get(card->dev);
5743         if (in4_dev == NULL)
5744                 return;
5745         read_lock(&in4_dev->mc_list_lock);
5746         qeth_add_mc(card, in4_dev);
5747         qeth_add_vlan_mc(card);
5748         read_unlock(&in4_dev->mc_list_lock);
5749         in_dev_put(in4_dev);
5750 }
5751
5752 static void
5753 qeth_layer2_add_multicast(struct qeth_card *card)
5754 {
5755         struct qeth_ipaddr *ipm;
5756         struct dev_mc_list *dm;
5757
5758         QETH_DBF_TEXT(trace,4,"L2addmc");
5759         for (dm = card->dev->mc_list; dm; dm = dm->next) {
5760                 ipm = qeth_get_addr_buffer(QETH_PROT_IPV4);
5761                 if (!ipm)
5762                         continue;
5763                 memcpy(ipm->mac,dm->dmi_addr,MAX_ADDR_LEN);
5764                 ipm->is_multicast = 1;
5765                 if (!qeth_add_ip(card, ipm))
5766                         kfree(ipm);
5767         }
5768 }
5769
5770 #ifdef CONFIG_QETH_IPV6
5771 static inline void
5772 qeth_add_mc6(struct qeth_card *card, struct inet6_dev *in6_dev)
5773 {
5774         struct qeth_ipaddr *ipm;
5775         struct ifmcaddr6 *im6;
5776         char buf[MAX_ADDR_LEN];
5777
5778         QETH_DBF_TEXT(trace,4,"addmc6");
5779         for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
5780                 ndisc_mc_map(&im6->mca_addr, buf, in6_dev->dev, 0);
5781                 ipm = qeth_get_addr_buffer(QETH_PROT_IPV6);
5782                 if (!ipm)
5783                         continue;
5784                 ipm->is_multicast = 1;
5785                 memcpy(ipm->mac,buf,OSA_ADDR_LEN);
5786                 memcpy(&ipm->u.a6.addr,&im6->mca_addr.s6_addr,
5787                        sizeof(struct in6_addr));
5788                 if (!qeth_add_ip(card,ipm))
5789                         kfree(ipm);
5790         }
5791 }
5792
5793 static inline void
5794 qeth_add_vlan_mc6(struct qeth_card *card)
5795 {
5796 #ifdef CONFIG_QETH_VLAN
5797         struct inet6_dev *in_dev;
5798         struct vlan_group *vg;
5799         int i;
5800
5801         QETH_DBF_TEXT(trace,4,"admc6vl");
5802         if ( ((card->options.layer2 == 0) &&
5803               (!qeth_is_supported(card,IPA_FULL_VLAN))) ||
5804              (card->vlangrp == NULL))
5805                 return ;
5806
5807         vg = card->vlangrp;
5808         for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5809                 if (vg->vlan_devices[i] == NULL ||
5810                     !(vg->vlan_devices[i]->flags & IFF_UP))
5811                         continue;
5812                 in_dev = in6_dev_get(vg->vlan_devices[i]);
5813                 if (!in_dev)
5814                         continue;
5815                 read_lock(&in_dev->lock);
5816                 qeth_add_mc6(card,in_dev);
5817                 read_unlock(&in_dev->lock);
5818                 in6_dev_put(in_dev);
5819         }
5820 #endif /* CONFIG_QETH_VLAN */
5821 }
5822
5823 static void
5824 qeth_add_multicast_ipv6(struct qeth_card *card)
5825 {
5826         struct inet6_dev *in6_dev;
5827
5828         QETH_DBF_TEXT(trace,4,"chkmcv6");
5829         if (!qeth_is_supported(card, IPA_IPV6)) 
5830                 return ;
5831         in6_dev = in6_dev_get(card->dev);
5832         if (in6_dev == NULL)
5833                 return;
5834         read_lock(&in6_dev->lock);
5835         qeth_add_mc6(card, in6_dev);
5836         qeth_add_vlan_mc6(card);
5837         read_unlock(&in6_dev->lock);
5838         in6_dev_put(in6_dev);
5839 }
5840 #endif /* CONFIG_QETH_IPV6 */
5841
5842 static int
5843 qeth_layer2_send_setdelmac(struct qeth_card *card, __u8 *mac,
5844                            enum qeth_ipa_cmds ipacmd,
5845                            int (*reply_cb) (struct qeth_card *,
5846                                             struct qeth_reply*,
5847                                             unsigned long))
5848 {
5849         struct qeth_ipa_cmd *cmd;
5850         struct qeth_cmd_buffer *iob;
5851
5852         QETH_DBF_TEXT(trace, 2, "L2sdmac");
5853         iob = qeth_get_ipacmd_buffer(card, ipacmd, QETH_PROT_IPV4);
5854         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5855         cmd->data.setdelmac.mac_length = OSA_ADDR_LEN;
5856         memcpy(&cmd->data.setdelmac.mac, mac, OSA_ADDR_LEN);
5857         return qeth_send_ipa_cmd(card, iob, reply_cb, NULL);
5858 }
5859
5860 static int
5861 qeth_layer2_send_setgroupmac_cb(struct qeth_card *card,
5862                                 struct qeth_reply *reply,
5863                                 unsigned long data)
5864 {
5865         struct qeth_ipa_cmd *cmd;
5866         __u8 *mac;
5867
5868         QETH_DBF_TEXT(trace, 2, "L2Sgmacb");
5869         cmd = (struct qeth_ipa_cmd *) data;
5870         mac = &cmd->data.setdelmac.mac[0];
5871         /* MAC already registered, needed in couple/uncouple case */
5872         if (cmd->hdr.return_code == 0x2005) {
5873                 PRINT_WARN("Group MAC %02x:%02x:%02x:%02x:%02x:%02x " \
5874                           "already existing on %s \n",
5875                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5876                           QETH_CARD_IFNAME(card));
5877                 cmd->hdr.return_code = 0;
5878         }
5879         if (cmd->hdr.return_code)
5880                 PRINT_ERR("Could not set group MAC " \
5881                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
5882                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5883                           QETH_CARD_IFNAME(card),cmd->hdr.return_code);
5884         return 0;
5885 }
5886
5887 static int
5888 qeth_layer2_send_setgroupmac(struct qeth_card *card, __u8 *mac)
5889 {
5890         QETH_DBF_TEXT(trace, 2, "L2Sgmac");
5891         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_SETGMAC,
5892                                           qeth_layer2_send_setgroupmac_cb);
5893 }
5894
5895 static int
5896 qeth_layer2_send_delgroupmac_cb(struct qeth_card *card,
5897                                 struct qeth_reply *reply,
5898                                 unsigned long data)
5899 {
5900         struct qeth_ipa_cmd *cmd;
5901         __u8 *mac;
5902
5903         QETH_DBF_TEXT(trace, 2, "L2Dgmacb");
5904         cmd = (struct qeth_ipa_cmd *) data;
5905         mac = &cmd->data.setdelmac.mac[0];
5906         if (cmd->hdr.return_code)
5907                 PRINT_ERR("Could not delete group MAC " \
5908                           "%02x:%02x:%02x:%02x:%02x:%02x on %s: %x\n",
5909                           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
5910                           QETH_CARD_IFNAME(card), cmd->hdr.return_code);
5911         return 0;
5912 }
5913
5914 static int
5915 qeth_layer2_send_delgroupmac(struct qeth_card *card, __u8 *mac)
5916 {
5917         QETH_DBF_TEXT(trace, 2, "L2Dgmac");
5918         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_DELGMAC,
5919                                           qeth_layer2_send_delgroupmac_cb);
5920 }
5921
5922 static int
5923 qeth_layer2_send_setmac_cb(struct qeth_card *card,
5924                            struct qeth_reply *reply,
5925                            unsigned long data)
5926 {
5927         struct qeth_ipa_cmd *cmd;
5928
5929         QETH_DBF_TEXT(trace, 2, "L2Smaccb");
5930         cmd = (struct qeth_ipa_cmd *) data;
5931         if (cmd->hdr.return_code) {
5932                 QETH_DBF_TEXT_(trace, 2, "L2er%x", cmd->hdr.return_code);
5933                 PRINT_WARN("Error in registering MAC address on " \
5934                            "device %s: x%x\n", CARD_BUS_ID(card),
5935                            cmd->hdr.return_code);
5936                 card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
5937                 cmd->hdr.return_code = -EIO;
5938         } else {
5939                 card->info.mac_bits |= QETH_LAYER2_MAC_REGISTERED;
5940                 memcpy(card->dev->dev_addr,cmd->data.setdelmac.mac,
5941                        OSA_ADDR_LEN);
5942                 PRINT_INFO("MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x "
5943                            "successfully registered on device %s\n",
5944                            card->dev->dev_addr[0], card->dev->dev_addr[1],
5945                            card->dev->dev_addr[2], card->dev->dev_addr[3],
5946                            card->dev->dev_addr[4], card->dev->dev_addr[5],
5947                            card->dev->name);
5948         }
5949         return 0;
5950 }
5951
5952 static int
5953 qeth_layer2_send_setmac(struct qeth_card *card, __u8 *mac)
5954 {
5955         QETH_DBF_TEXT(trace, 2, "L2Setmac");
5956         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_SETVMAC,
5957                                           qeth_layer2_send_setmac_cb);
5958 }
5959
5960 static int
5961 qeth_layer2_send_delmac_cb(struct qeth_card *card,
5962                            struct qeth_reply *reply,
5963                            unsigned long data)
5964 {
5965         struct qeth_ipa_cmd *cmd;
5966
5967         QETH_DBF_TEXT(trace, 2, "L2Dmaccb");
5968         cmd = (struct qeth_ipa_cmd *) data;
5969         if (cmd->hdr.return_code) {
5970                 PRINT_WARN("Error in deregistering MAC address on " \
5971                            "device %s: x%x\n", CARD_BUS_ID(card),
5972                            cmd->hdr.return_code);
5973                 QETH_DBF_TEXT_(trace, 2, "err%d", cmd->hdr.return_code);
5974                 cmd->hdr.return_code = -EIO;
5975                 return 0;
5976         }
5977         card->info.mac_bits &= ~QETH_LAYER2_MAC_REGISTERED;
5978
5979         return 0;
5980 }
5981 static int
5982 qeth_layer2_send_delmac(struct qeth_card *card, __u8 *mac)
5983 {
5984         QETH_DBF_TEXT(trace, 2, "L2Delmac");
5985         if (!(card->info.mac_bits & QETH_LAYER2_MAC_REGISTERED))
5986                 return 0;
5987         return qeth_layer2_send_setdelmac(card, mac, IPA_CMD_DELVMAC,
5988                                           qeth_layer2_send_delmac_cb);
5989 }
5990
5991 static int
5992 qeth_layer2_set_mac_address(struct net_device *dev, void *p)
5993 {
5994         struct sockaddr *addr = p;
5995         struct qeth_card *card;
5996         int rc = 0;
5997
5998         QETH_DBF_TEXT(trace, 3, "setmac");
5999
6000         if (qeth_verify_dev(dev) != QETH_REAL_CARD) {
6001                 QETH_DBF_TEXT(trace, 3, "setmcINV");
6002                 return -EOPNOTSUPP;
6003         }
6004         card = (struct qeth_card *) dev->priv;
6005