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