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