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