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