qeth: remove header_ops bug
[efikamx:linux-kernel.git] / drivers / s390 / net / qeth_main.c
1 /*
2  * linux/drivers/s390/net/qeth_main.c
3  *
4  * Linux on zSeries OSA Express and HiperSockets support
5  *
6  * Copyright 2000,2003 IBM Corporation
7  *
8  *    Author(s): Original Code written by
9  *                        Utz Bacher (utz.bacher@de.ibm.com)
10  *               Rewritten by
11  *                        Frank Pavlic (fpavlic@de.ibm.com) and
12  *                        Thomas Spatzier <tspat@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/string.h>
33 #include <linux/errno.h>
34 #include <linux/mm.h>
35 #include <linux/ip.h>
36 #include <linux/inetdevice.h>
37 #include <linux/netdevice.h>
38 #include <linux/sched.h>
39 #include <linux/workqueue.h>
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/interrupt.h>
43 #include <linux/tcp.h>
44 #include <linux/icmp.h>
45 #include <linux/skbuff.h>
46 #include <linux/in.h>
47 #include <linux/igmp.h>
48 #include <linux/init.h>
49 #include <linux/reboot.h>
50 #include <linux/mii.h>
51 #include <linux/rcupdate.h>
52 #include <linux/ethtool.h>
53
54 #include <net/arp.h>
55 #include <net/ip.h>
56 #include <net/route.h>
57
58 #include <asm/ebcdic.h>
59 #include <asm/io.h>
60 #include <asm/qeth.h>
61 #include <asm/timex.h>
62 #include <asm/semaphore.h>
63 #include <asm/uaccess.h>
64 #include <asm/s390_rdev.h>
65
66 #include "qeth.h"
67 #include "qeth_mpc.h"
68 #include "qeth_fs.h"
69 #include "qeth_eddp.h"
70 #include "qeth_tso.h"
71
72 static const char *version = "qeth S/390 OSA-Express driver";
73
74 /**
75  * Debug Facility Stuff
76  */
77 static debug_info_t *qeth_dbf_setup = NULL;
78 static debug_info_t *qeth_dbf_data = NULL;
79 static debug_info_t *qeth_dbf_misc = NULL;
80 static debug_info_t *qeth_dbf_control = NULL;
81 debug_info_t *qeth_dbf_trace = NULL;
82 static debug_info_t *qeth_dbf_sense = NULL;
83 static debug_info_t *qeth_dbf_qerr = NULL;
84
85 DEFINE_PER_CPU(char[256], qeth_dbf_txt_buf);
86
87 static struct lock_class_key qdio_out_skb_queue_key;
88
89 /**
90  * some more definitions and declarations
91  */
92 static unsigned int known_devices[][10] = QETH_MODELLIST_ARRAY;
93
94 /* list of our cards */
95 struct qeth_card_list_struct qeth_card_list;
96 /*process list want to be notified*/
97 spinlock_t qeth_notify_lock;
98 struct list_head qeth_notify_list;
99
100 static void qeth_send_control_data_cb(struct qeth_channel *,
101                                       struct qeth_cmd_buffer *);
102
103 /**
104  * here we go with function implementation
105  */
106 static void
107 qeth_init_qdio_info(struct qeth_card *card);
108
109 static int
110 qeth_init_qdio_queues(struct qeth_card *card);
111
112 static int
113 qeth_alloc_qdio_buffers(struct qeth_card *card);
114
115 static void
116 qeth_free_qdio_buffers(struct qeth_card *);
117
118 static void
119 qeth_clear_qdio_buffers(struct qeth_card *);
120
121 static void
122 qeth_clear_ip_list(struct qeth_card *, int, int);
123
124 static void
125 qeth_clear_ipacmd_list(struct qeth_card *);
126
127 static int
128 qeth_qdio_clear_card(struct qeth_card *, int);
129
130 static void
131 qeth_clear_working_pool_list(struct qeth_card *);
132
133 static void
134 qeth_clear_cmd_buffers(struct qeth_channel *);
135
136 static int
137 qeth_stop(struct net_device *);
138
139 static void
140 qeth_clear_ipato_list(struct qeth_card *);
141
142 static int
143 qeth_is_addr_covered_by_ipato(struct qeth_card *, struct qeth_ipaddr *);
144
145 static void
146 qeth_irq_tasklet(unsigned long);
147
148 static int
149 qeth_set_online(struct ccwgroup_device *);
150
151 static int
152 __qeth_set_online(struct ccwgroup_device *gdev, int recovery_mode);
153
154 static struct qeth_ipaddr *
155 qeth_get_addr_buffer(enum qeth_prot_versions);
156
157 static void
158 qeth_set_multicast_list(struct net_device *);
159
160 static void
161 qeth_setadp_promisc_mode(struct qeth_card *);
162
163 static int
164 qeth_hard_header_parse(const struct sk_buff *skb, unsigned char *haddr);
165
166 static void
167 qeth_notify_processes(void)
168 {
169         /*notify all  registered processes */
170         struct qeth_notify_list_struct *n_entry;
171
172         QETH_DBF_TEXT(trace,3,"procnoti");
173         spin_lock(&qeth_notify_lock);
174         list_for_each_entry(n_entry, &qeth_notify_list, list) {
175                 send_sig(n_entry->signum, n_entry->task, 1);
176         }
177         spin_unlock(&qeth_notify_lock);
178
179 }
180 int
181 qeth_notifier_unregister(struct task_struct *p)
182 {
183         struct qeth_notify_list_struct *n_entry, *tmp;
184
185         QETH_DBF_TEXT(trace, 2, "notunreg");
186         spin_lock(&qeth_notify_lock);
187         list_for_each_entry_safe(n_entry, tmp, &qeth_notify_list, list) {
188                 if (n_entry->task == p) {
189                         list_del(&n_entry->list);
190                         kfree(n_entry);
191                         goto out;
192                 }
193         }
194 out:
195         spin_unlock(&qeth_notify_lock);
196         return 0;
197 }
198 int
199 qeth_notifier_register(struct task_struct *p, int signum)
200 {
201         struct qeth_notify_list_struct *n_entry;
202
203         /*check first if entry already exists*/
204         spin_lock(&qeth_notify_lock);
205         list_for_each_entry(n_entry, &qeth_notify_list, list) {
206                 if (n_entry->task == p) {
207                         n_entry->signum = signum;
208                         spin_unlock(&qeth_notify_lock);
209                         return 0;
210                 }
211         }
212         spin_unlock(&qeth_notify_lock);
213
214         n_entry = (struct qeth_notify_list_struct *)
215                 kmalloc(sizeof(struct qeth_notify_list_struct),GFP_KERNEL);
216         if (!n_entry)
217                 return -ENOMEM;
218         n_entry->task = p;
219         n_entry->signum = signum;
220         spin_lock(&qeth_notify_lock);
221         list_add(&n_entry->list,&qeth_notify_list);
222         spin_unlock(&qeth_notify_lock);
223         return 0;
224 }
225
226
227 /**
228  * free channel command buffers
229  */
230 static void
231 qeth_clean_channel(struct qeth_channel *channel)
232 {
233         int cnt;
234
235         QETH_DBF_TEXT(setup, 2, "freech");
236         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
237                 kfree(channel->iob[cnt].data);
238 }
239
240 /**
241  * free card
242  */
243 static void
244 qeth_free_card(struct qeth_card *card)
245 {
246
247         QETH_DBF_TEXT(setup, 2, "freecrd");
248         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
249         qeth_clean_channel(&card->read);
250         qeth_clean_channel(&card->write);
251         if (card->dev)
252                 free_netdev(card->dev);
253         qeth_clear_ip_list(card, 0, 0);
254         qeth_clear_ipato_list(card);
255         kfree(card->ip_tbd_list);
256         qeth_free_qdio_buffers(card);
257         kfree(card);
258 }
259
260 /**
261  * alloc memory for command buffer per channel
262  */
263 static int
264 qeth_setup_channel(struct qeth_channel *channel)
265 {
266         int cnt;
267
268         QETH_DBF_TEXT(setup, 2, "setupch");
269         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
270                 channel->iob[cnt].data = (char *)
271                         kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
272                 if (channel->iob[cnt].data == NULL)
273                         break;
274                 channel->iob[cnt].state = BUF_STATE_FREE;
275                 channel->iob[cnt].channel = channel;
276                 channel->iob[cnt].callback = qeth_send_control_data_cb;
277                 channel->iob[cnt].rc = 0;
278         }
279         if (cnt < QETH_CMD_BUFFER_NO) {
280                 while (cnt-- > 0)
281                         kfree(channel->iob[cnt].data);
282                 return -ENOMEM;
283         }
284         channel->buf_no = 0;
285         channel->io_buf_no = 0;
286         atomic_set(&channel->irq_pending, 0);
287         spin_lock_init(&channel->iob_lock);
288
289         init_waitqueue_head(&channel->wait_q);
290         channel->irq_tasklet.data = (unsigned long) channel;
291         channel->irq_tasklet.func = qeth_irq_tasklet;
292         return 0;
293 }
294
295 /**
296  * alloc memory for card structure
297  */
298 static struct qeth_card *
299 qeth_alloc_card(void)
300 {
301         struct qeth_card *card;
302
303         QETH_DBF_TEXT(setup, 2, "alloccrd");
304         card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
305         if (!card)
306                 return NULL;
307         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
308         if (qeth_setup_channel(&card->read)) {
309                 kfree(card);
310                 return NULL;
311         }
312         if (qeth_setup_channel(&card->write)) {
313                 qeth_clean_channel(&card->read);
314                 kfree(card);
315                 return NULL;
316         }
317         return card;
318 }
319
320 static long
321 __qeth_check_irb_error(struct ccw_device *cdev, unsigned long intparm,
322                        struct irb *irb)
323 {
324         if (!IS_ERR(irb))
325                 return 0;
326
327         switch (PTR_ERR(irb)) {
328         case -EIO:
329                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
330                 QETH_DBF_TEXT(trace, 2, "ckirberr");
331                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
332                 break;
333         case -ETIMEDOUT:
334                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
335                 QETH_DBF_TEXT(trace, 2, "ckirberr");
336                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -ETIMEDOUT);
337                 if (intparm == QETH_RCD_PARM) {
338                         struct qeth_card *card = CARD_FROM_CDEV(cdev);
339
340                         if (card && (card->data.ccwdev == cdev)) {
341                                 card->data.state = CH_STATE_DOWN;
342                                 wake_up(&card->wait_q);
343                         }
344                 }
345                 break;
346         default:
347                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
348                            cdev->dev.bus_id);
349                 QETH_DBF_TEXT(trace, 2, "ckirberr");
350                 QETH_DBF_TEXT(trace, 2, "  rc???");
351         }
352         return PTR_ERR(irb);
353 }
354
355 static int
356 qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
357 {
358         int dstat,cstat;
359         char *sense;
360
361         sense = (char *) irb->ecw;
362         cstat = irb->scsw.cstat;
363         dstat = irb->scsw.dstat;
364
365         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
366                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
367                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
368                 QETH_DBF_TEXT(trace,2, "CGENCHK");
369                 PRINT_WARN("check on device %s, dstat=x%x, cstat=x%x ",
370                            cdev->dev.bus_id, dstat, cstat);
371                 HEXDUMP16(WARN, "irb: ", irb);
372                 HEXDUMP16(WARN, "irb: ", ((char *) irb) + 32);
373                 return 1;
374         }
375
376         if (dstat & DEV_STAT_UNIT_CHECK) {
377                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
378                     SENSE_RESETTING_EVENT_FLAG) {
379                         QETH_DBF_TEXT(trace,2,"REVIND");
380                         return 1;
381                 }
382                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
383                     SENSE_COMMAND_REJECT_FLAG) {
384                         QETH_DBF_TEXT(trace,2,"CMDREJi");
385                         return 0;
386                 }
387                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
388                         QETH_DBF_TEXT(trace,2,"AFFE");
389                         return 1;
390                 }
391                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
392                         QETH_DBF_TEXT(trace,2,"ZEROSEN");
393                         return 0;
394                 }
395                 QETH_DBF_TEXT(trace,2,"DGENCHK");
396                         return 1;
397         }
398         return 0;
399 }
400 static int qeth_issue_next_read(struct qeth_card *);
401
402 /**
403  * interrupt handler
404  */
405 static void
406 qeth_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
407 {
408         int rc;
409         int cstat,dstat;
410         struct qeth_cmd_buffer *buffer;
411         struct qeth_channel *channel;
412         struct qeth_card *card;
413
414         QETH_DBF_TEXT(trace,5,"irq");
415
416         if (__qeth_check_irb_error(cdev, intparm, irb))
417                 return;
418         cstat = irb->scsw.cstat;
419         dstat = irb->scsw.dstat;
420
421         card = CARD_FROM_CDEV(cdev);
422         if (!card)
423                 return;
424
425         if (card->read.ccwdev == cdev){
426                 channel = &card->read;
427                 QETH_DBF_TEXT(trace,5,"read");
428         } else if (card->write.ccwdev == cdev) {
429                 channel = &card->write;
430                 QETH_DBF_TEXT(trace,5,"write");
431         } else {
432                 channel = &card->data;
433                 QETH_DBF_TEXT(trace,5,"data");
434         }
435         atomic_set(&channel->irq_pending, 0);
436
437         if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC))
438                 channel->state = CH_STATE_STOPPED;
439
440         if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC))
441                 channel->state = CH_STATE_HALTED;
442
443         /*let's wake up immediately on data channel*/
444         if ((channel == &card->data) && (intparm != 0) &&
445             (intparm != QETH_RCD_PARM))
446                 goto out;
447
448         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
449                 QETH_DBF_TEXT(trace, 6, "clrchpar");
450                 /* we don't have to handle this further */
451                 intparm = 0;
452         }
453         if (intparm == QETH_HALT_CHANNEL_PARM) {
454                 QETH_DBF_TEXT(trace, 6, "hltchpar");
455                 /* we don't have to handle this further */
456                 intparm = 0;
457         }
458         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
459             (dstat & DEV_STAT_UNIT_CHECK) ||
460             (cstat)) {
461                 if (irb->esw.esw0.erw.cons) {
462                         /* TODO: we should make this s390dbf */
463                         PRINT_WARN("sense data available on channel %s.\n",
464                                    CHANNEL_ID(channel));
465                         PRINT_WARN(" cstat 0x%X\n dstat 0x%X\n", cstat, dstat);
466                         HEXDUMP16(WARN,"irb: ",irb);
467                         HEXDUMP16(WARN,"sense data: ",irb->ecw);
468                 }
469                 if (intparm == QETH_RCD_PARM) {
470                         channel->state = CH_STATE_DOWN;
471                         goto out;
472                 }
473                 rc = qeth_get_problem(cdev,irb);
474                 if (rc) {
475                         qeth_schedule_recovery(card);
476                         goto out;
477                 }
478         }
479
480         if (intparm == QETH_RCD_PARM) {
481                 channel->state = CH_STATE_RCD_DONE;
482                 goto out;
483         }
484         if (intparm) {
485                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
486                 buffer->state = BUF_STATE_PROCESSED;
487         }
488         if (channel == &card->data)
489                 return;
490
491         if (channel == &card->read &&
492             channel->state == CH_STATE_UP)
493                 qeth_issue_next_read(card);
494
495         qeth_irq_tasklet((unsigned long)channel);
496         return;
497 out:
498         wake_up(&card->wait_q);
499 }
500
501 /**
502  * tasklet function scheduled from irq handler
503  */
504 static void
505 qeth_irq_tasklet(unsigned long data)
506 {
507         struct qeth_card *card;
508         struct qeth_channel *channel;
509         struct qeth_cmd_buffer *iob;
510         __u8 index;
511
512         QETH_DBF_TEXT(trace,5,"irqtlet");
513         channel = (struct qeth_channel *) data;
514         iob = channel->iob;
515         index = channel->buf_no;
516         card = CARD_FROM_CDEV(channel->ccwdev);
517         while (iob[index].state == BUF_STATE_PROCESSED) {
518                 if (iob[index].callback !=NULL) {
519                         iob[index].callback(channel,iob + index);
520                 }
521                 index = (index + 1) % QETH_CMD_BUFFER_NO;
522         }
523         channel->buf_no = index;
524         wake_up(&card->wait_q);
525 }
526
527 static int qeth_stop_card(struct qeth_card *, int);
528
529 static int
530 __qeth_set_offline(struct ccwgroup_device *cgdev, int recovery_mode)
531 {
532         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
533         int rc = 0, rc2 = 0, rc3 = 0;
534         enum qeth_card_states recover_flag;
535
536         QETH_DBF_TEXT(setup, 3, "setoffl");
537         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
538
539         if (card->dev && netif_carrier_ok(card->dev))
540                 netif_carrier_off(card->dev);
541         recover_flag = card->state;
542         if (qeth_stop_card(card, recovery_mode) == -ERESTARTSYS){
543                 PRINT_WARN("Stopping card %s interrupted by user!\n",
544                            CARD_BUS_ID(card));
545                 return -ERESTARTSYS;
546         }
547         rc  = ccw_device_set_offline(CARD_DDEV(card));
548         rc2 = ccw_device_set_offline(CARD_WDEV(card));
549         rc3 = ccw_device_set_offline(CARD_RDEV(card));
550         if (!rc)
551                 rc = (rc2) ? rc2 : rc3;
552         if (rc)
553                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
554         if (recover_flag == CARD_STATE_UP)
555                 card->state = CARD_STATE_RECOVER;
556         qeth_notify_processes();
557         return 0;
558 }
559
560 static int
561 qeth_set_offline(struct ccwgroup_device *cgdev)
562 {
563         return  __qeth_set_offline(cgdev, 0);
564 }
565
566 static int
567 qeth_threads_running(struct qeth_card *card, unsigned long threads);
568
569
570 static void
571 qeth_remove_device(struct ccwgroup_device *cgdev)
572 {
573         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
574         unsigned long flags;
575
576         QETH_DBF_TEXT(setup, 3, "rmdev");
577         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
578
579         if (!card)
580                 return;
581
582         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
583
584         if (cgdev->state == CCWGROUP_ONLINE){
585                 card->use_hard_stop = 1;
586                 qeth_set_offline(cgdev);
587         }
588         /* remove form our internal list */
589         write_lock_irqsave(&qeth_card_list.rwlock, flags);
590         list_del(&card->list);
591         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
592         if (card->dev)
593                 unregister_netdev(card->dev);
594         qeth_remove_device_attributes(&cgdev->dev);
595         qeth_free_card(card);
596         cgdev->dev.driver_data = NULL;
597         put_device(&cgdev->dev);
598 }
599
600 static int
601 qeth_register_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
602 static int
603 qeth_deregister_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
604
605 /**
606  * Add/remove address to/from card's ip list, i.e. try to add or remove
607  * reference to/from an IP address that is already registered on the card.
608  * Returns:
609  *      0  address was on card and its reference count has been adjusted,
610  *         but is still > 0, so nothing has to be done
611  *         also returns 0 if card was not on card and the todo was to delete
612  *         the address -> there is also nothing to be done
613  *      1  address was not on card and the todo is to add it to the card's ip
614  *         list
615  *      -1 address was on card and its reference count has been decremented
616  *         to <= 0 by the todo -> address must be removed from card
617  */
618 static int
619 __qeth_ref_ip_on_card(struct qeth_card *card, struct qeth_ipaddr *todo,
620                       struct qeth_ipaddr **__addr)
621 {
622         struct qeth_ipaddr *addr;
623         int found = 0;
624
625         list_for_each_entry(addr, &card->ip_list, entry) {
626                 if (card->options.layer2) {
627                         if ((addr->type == todo->type) &&
628                             (memcmp(&addr->mac, &todo->mac,
629                                     OSA_ADDR_LEN) == 0)) {
630                                 found = 1;
631                                 break;
632                         }
633                         continue;
634                 }
635                 if ((addr->proto     == QETH_PROT_IPV4)  &&
636                     (todo->proto     == QETH_PROT_IPV4)  &&
637                     (addr->type      == todo->type)      &&
638                     (addr->u.a4.addr == todo->u.a4.addr) &&
639                     (addr->u.a4.mask == todo->u.a4.mask)) {
640                         found = 1;
641                         break;
642                 }
643                 if ((addr->proto       == QETH_PROT_IPV6)     &&
644                     (todo->proto       == QETH_PROT_IPV6)     &&
645                     (addr->type        == todo->type)         &&
646                     (addr->u.a6.pfxlen == todo->u.a6.pfxlen)  &&
647                     (memcmp(&addr->u.a6.addr, &todo->u.a6.addr,
648                             sizeof(struct in6_addr)) == 0)) {
649                         found = 1;
650                         break;
651                 }
652         }
653         if (found) {
654                 addr->users += todo->users;
655                 if (addr->users <= 0){
656                         *__addr = addr;
657                         return -1;
658                 } else {
659                         /* for VIPA and RXIP limit refcount to 1 */
660                         if (addr->type != QETH_IP_TYPE_NORMAL)
661                                 addr->users = 1;
662                         return 0;
663                 }
664         }
665         if (todo->users > 0) {
666                 /* for VIPA and RXIP limit refcount to 1 */
667                 if (todo->type != QETH_IP_TYPE_NORMAL)
668                         todo->users = 1;
669                 return 1;
670         } else
671                 return 0;
672 }
673
674 static int
675 __qeth_address_exists_in_list(struct list_head *list, struct qeth_ipaddr *addr,
676                               int same_type)
677 {
678         struct qeth_ipaddr *tmp;
679
680         list_for_each_entry(tmp, list, entry) {
681                 if ((tmp->proto     == QETH_PROT_IPV4)            &&
682                     (addr->proto    == QETH_PROT_IPV4)            &&
683                     ((same_type && (tmp->type == addr->type)) ||
684                      (!same_type && (tmp->type != addr->type))  ) &&
685                     (tmp->u.a4.addr == addr->u.a4.addr)             ){
686                         return 1;
687                 }
688                 if ((tmp->proto  == QETH_PROT_IPV6)               &&
689                     (addr->proto == QETH_PROT_IPV6)               &&
690                     ((same_type && (tmp->type == addr->type)) ||
691                      (!same_type && (tmp->type != addr->type))  ) &&
692                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
693                             sizeof(struct in6_addr)) == 0)          ) {
694                         return 1;
695                 }
696         }
697         return 0;
698 }
699
700 /*
701  * Add IP to be added to todo list. If there is already an "add todo"
702  * in this list we just incremenent the reference count.
703  * Returns 0 if we  just incremented reference count.
704  */
705 static int
706 __qeth_insert_ip_todo(struct qeth_card *card, struct qeth_ipaddr *addr, int add)
707 {
708         struct qeth_ipaddr *tmp, *t;
709         int found = 0;
710
711         list_for_each_entry_safe(tmp, t, card->ip_tbd_list, entry) {
712                 if ((addr->type == QETH_IP_TYPE_DEL_ALL_MC) &&
713                     (tmp->type == QETH_IP_TYPE_DEL_ALL_MC))
714                         return 0;
715                 if (card->options.layer2) {
716                         if ((tmp->type  == addr->type)  &&
717                             (tmp->is_multicast == addr->is_multicast) &&
718                             (memcmp(&tmp->mac, &addr->mac,
719                                     OSA_ADDR_LEN) == 0)) {
720                                 found = 1;
721                                 break;
722                         }
723                         continue;
724                 }
725                 if ((tmp->proto        == QETH_PROT_IPV4)     &&
726                     (addr->proto       == QETH_PROT_IPV4)     &&
727                     (tmp->type         == addr->type)         &&
728                     (tmp->is_multicast == addr->is_multicast) &&
729                     (tmp->u.a4.addr    == addr->u.a4.addr)    &&
730                     (tmp->u.a4.mask    == addr->u.a4.mask)) {
731                         found = 1;
732                         break;
733                 }
734                 if ((tmp->proto        == QETH_PROT_IPV6)      &&
735                     (addr->proto       == QETH_PROT_IPV6)      &&
736                     (tmp->type         == addr->type)          &&
737                     (tmp->is_multicast == addr->is_multicast)  &&
738                     (tmp->u.a6.pfxlen  == addr->u.a6.pfxlen)   &&
739                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
740                             sizeof(struct in6_addr)) == 0)) {
741                         found = 1;
742                         break;
743                 }
744         }
745         if (found){
746                 if (addr->users != 0)
747                         tmp->users += addr->users;
748                 else
749                         tmp->users += add? 1:-1;
750                 if (tmp->users == 0) {
751                         list_del(&tmp->entry);
752                         kfree(tmp);
753                 }
754                 return 0;
755         } else {
756                 if (addr->type == QETH_IP_TYPE_DEL_ALL_MC)
757                         list_add(&addr->entry, card->ip_tbd_list);
758                 else {
759                         if (addr->users == 0)
760                                 addr->users += add? 1:-1;
761                         if (add && (addr->type == QETH_IP_TYPE_NORMAL) &&
762                             qeth_is_addr_covered_by_ipato(card, addr)){
763                                 QETH_DBF_TEXT(trace, 2, "tkovaddr");
764                                 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
765                         }
766                         list_add_tail(&addr->entry, card->ip_tbd_list);
767                 }
768                 return 1;
769         }
770 }
771
772 /**
773  * Remove IP address from list
774  */
775 static int
776 qeth_delete_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
777 {
778         unsigned long flags;
779         int rc = 0;
780
781         QETH_DBF_TEXT(trace, 4, "delip");
782
783         if (card->options.layer2)
784                 QETH_DBF_HEX(trace, 4, &addr->mac, 6);
785         else if (addr->proto == QETH_PROT_IPV4)
786                 QETH_DBF_HEX(trace, 4, &addr->u.a4.addr, 4);
787         else {
788                 QETH_DBF_HEX(trace, 4, &addr->u.a6.addr, 8);
789                 QETH_DBF_HEX(trace, 4, ((char *)&addr->u.a6.addr) + 8, 8);
790         }
791         spin_lock_irqsave(&card->ip_lock, flags);
792         rc = __qeth_insert_ip_todo(card, addr, 0);
793         spin_unlock_irqrestore(&card->ip_lock, flags);
794         return rc;
795 }
796
797 static int
798 qeth_add_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
799 {
800         unsigned long flags;
801         int rc = 0;
802
803         QETH_DBF_TEXT(trace, 4, "addip");
804         if (card->options.layer2)
805                 QETH_DBF_HEX(trace, 4, &addr->mac, 6);
806         else if (addr->proto == QETH_PROT_IPV4)
807                 QETH_DBF_HEX(trace, 4, &addr->u.a4.addr, 4);
808         else {
809                 QETH_DBF_HEX(trace, 4, &addr->u.a6.addr, 8);
810                 QETH_DBF_HEX(trace, 4, ((char *)&addr->u.a6.addr) + 8, 8);
811         }
812         spin_lock_irqsave(&card->ip_lock, flags);
813         rc = __qeth_insert_ip_todo(card, addr, 1);
814         spin_unlock_irqrestore(&card->ip_lock, flags);
815         return rc;
816 }
817
818 static void
819 __qeth_delete_all_mc(struct qeth_card *card, unsigned long *flags)
820 {
821         struct qeth_ipaddr *addr, *tmp;
822         int rc;
823 again:
824         list_for_each_entry_safe(addr, tmp, &card->ip_list, entry) {
825                 if (addr->is_multicast) {
826                         list_del(&addr->entry);
827                         spin_unlock_irqrestore(&card->ip_lock, *flags);
828                         rc = qeth_deregister_addr_entry(card, addr);
829                         spin_lock_irqsave(&card->ip_lock, *flags);
830                         if (!rc) {
831                                 kfree(addr);
832                                 goto again;
833                         } else
834                                 list_add(&addr->entry, &card->ip_list);
835                 }
836         }
837 }
838
839 static void
840 qeth_set_ip_addr_list(struct qeth_card *card)
841 {
842         struct list_head *tbd_list;
843         struct qeth_ipaddr *todo, *addr;
844         unsigned long flags;
845         int rc;
846
847         QETH_DBF_TEXT(trace, 2, "sdiplist");
848         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
849
850         spin_lock_irqsave(&card->ip_lock, flags);
851         tbd_list = card->ip_tbd_list;
852         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
853         if (!card->ip_tbd_list) {
854                 QETH_DBF_TEXT(trace, 0, "silnomem");
855                 card->ip_tbd_list = tbd_list;
856                 spin_unlock_irqrestore(&card->ip_lock, flags);
857                 return;
858         } else
859                 INIT_LIST_HEAD(card->ip_tbd_list);
860
861         while (!list_empty(tbd_list)){
862                 todo = list_entry(tbd_list->next, struct qeth_ipaddr, entry);
863                 list_del(&todo->entry);
864                 if (todo->type == QETH_IP_TYPE_DEL_ALL_MC){
865                         __qeth_delete_all_mc(card, &flags);
866                         kfree(todo);
867                         continue;
868                 }
869                 rc = __qeth_ref_ip_on_card(card, todo, &addr);
870                 if (rc == 0) {
871                         /* nothing to be done; only adjusted refcount */
872                         kfree(todo);
873                 } else if (rc == 1) {
874                         /* new entry to be added to on-card list */
875                         spin_unlock_irqrestore(&card->ip_lock, flags);
876                         rc = qeth_register_addr_entry(card, todo);
877                         spin_lock_irqsave(&card->ip_lock, flags);
878                         if (!rc)
879                                 list_add_tail(&todo->entry, &card->ip_list);
880                         else
881                                 kfree(todo);
882                 } else if (rc == -1) {
883                         /* on-card entry to be removed */
884                         list_del_init(&addr->entry);
885                         spin_unlock_irqrestore(&card->ip_lock, flags);
886                         rc = qeth_deregister_addr_entry(card, addr);
887                         spin_lock_irqsave(&card->ip_lock, flags);
888                         if (!rc)
889                                 kfree(addr);
890                         else
891                                 list_add_tail(&addr->entry, &card->ip_list);
892                         kfree(todo);
893                 }
894         }
895         spin_unlock_irqrestore(&card->ip_lock, flags);
896         kfree(tbd_list);
897 }
898
899 static void qeth_delete_mc_addresses(struct qeth_card *);
900 static void qeth_add_multicast_ipv4(struct qeth_card *);
901 static void qeth_layer2_add_multicast(struct qeth_card *);
902 #ifdef CONFIG_QETH_IPV6
903 static void qeth_add_multicast_ipv6(struct qeth_card *);
904 #endif
905
906 static int
907 qeth_set_thread_start_bit(struct qeth_card *card, unsigned long thread)
908 {
909         unsigned long flags;
910
911         spin_lock_irqsave(&card->thread_mask_lock, flags);
912         if ( !(card->thread_allowed_mask & thread) ||
913               (card->thread_start_mask & thread) ) {
914                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
915                 return -EPERM;
916         }
917         card->thread_start_mask |= thread;
918         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
919         return 0;
920 }
921
922 static void
923 qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
924 {
925         unsigned long flags;
926
927         spin_lock_irqsave(&card->thread_mask_lock, flags);
928         card->thread_start_mask &= ~thread;
929         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
930         wake_up(&card->wait_q);
931 }
932
933 static void
934 qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
935 {
936         unsigned long flags;
937
938         spin_lock_irqsave(&card->thread_mask_lock, flags);
939         card->thread_running_mask &= ~thread;
940         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
941         wake_up(&card->wait_q);
942 }
943
944 static int
945 __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
946 {
947         unsigned long flags;
948         int rc = 0;
949
950         spin_lock_irqsave(&card->thread_mask_lock, flags);
951         if (card->thread_start_mask & thread){
952                 if ((card->thread_allowed_mask & thread) &&
953                     !(card->thread_running_mask & thread)){
954                         rc = 1;
955                         card->thread_start_mask &= ~thread;
956                         card->thread_running_mask |= thread;
957                 } else
958                         rc = -EPERM;
959         }
960         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
961         return rc;
962 }
963
964 static int
965 qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
966 {
967         int rc = 0;
968
969         wait_event(card->wait_q,
970                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
971         return rc;
972 }
973
974 static int
975 qeth_recover(void *ptr)
976 {
977         struct qeth_card *card;
978         int rc = 0;
979
980         card = (struct qeth_card *) ptr;
981         daemonize("qeth_recover");
982         QETH_DBF_TEXT(trace,2,"recover1");
983         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
984         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
985                 return 0;
986         QETH_DBF_TEXT(trace,2,"recover2");
987         PRINT_WARN("Recovery of device %s started ...\n",
988                    CARD_BUS_ID(card));
989         card->use_hard_stop = 1;
990         __qeth_set_offline(card->gdev,1);
991         rc = __qeth_set_online(card->gdev,1);
992         /* don't run another scheduled recovery */
993         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
994         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
995         if (!rc)
996                 PRINT_INFO("Device %s successfully recovered!\n",
997                            CARD_BUS_ID(card));
998         else
999                 PRINT_INFO("Device %s could not be recovered!\n",
1000                            CARD_BUS_ID(card));
1001         return 0;
1002 }
1003
1004 void
1005 qeth_schedule_recovery(struct qeth_card *card)
1006 {
1007         QETH_DBF_TEXT(trace,2,"startrec");
1008         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
1009                 schedule_work(&card->kernel_thread_starter);
1010 }
1011
1012 static int
1013 qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1014 {
1015         unsigned long flags;
1016         int rc = 0;
1017
1018         spin_lock_irqsave(&card->thread_mask_lock, flags);
1019         QETH_DBF_TEXT_(trace, 4, "  %02x%02x%02x",
1020                         (u8) card->thread_start_mask,
1021                         (u8) card->thread_allowed_mask,
1022                         (u8) card->thread_running_mask);
1023         rc = (card->thread_start_mask & thread);
1024         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1025         return rc;
1026 }
1027
1028 static void
1029 qeth_start_kernel_thread(struct work_struct *work)
1030 {
1031         struct qeth_card *card = container_of(work, struct qeth_card, kernel_thread_starter);
1032         QETH_DBF_TEXT(trace , 2, "strthrd");
1033
1034         if (card->read.state != CH_STATE_UP &&
1035             card->write.state != CH_STATE_UP)
1036                 return;
1037         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1038                 kernel_thread(qeth_recover, (void *) card, SIGCHLD);
1039 }
1040
1041
1042 static void
1043 qeth_set_intial_options(struct qeth_card *card)
1044 {
1045         card->options.route4.type = NO_ROUTER;
1046 #ifdef CONFIG_QETH_IPV6
1047         card->options.route6.type = NO_ROUTER;
1048 #endif /* QETH_IPV6 */
1049         card->options.checksum_type = QETH_CHECKSUM_DEFAULT;
1050         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1051         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1052         card->options.fake_broadcast = 0;
1053         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1054         card->options.fake_ll = 0;
1055         if (card->info.type == QETH_CARD_TYPE_OSN)
1056                 card->options.layer2 = 1;
1057         else
1058                 card->options.layer2 = 0;
1059         card->options.performance_stats = 0;
1060         card->options.rx_sg_cb = QETH_RX_SG_CB;
1061 }
1062
1063 /**
1064  * initialize channels ,card and all state machines
1065  */
1066 static int
1067 qeth_setup_card(struct qeth_card *card)
1068 {
1069
1070         QETH_DBF_TEXT(setup, 2, "setupcrd");
1071         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
1072
1073         card->read.state  = CH_STATE_DOWN;
1074         card->write.state = CH_STATE_DOWN;
1075         card->data.state  = CH_STATE_DOWN;
1076         card->state = CARD_STATE_DOWN;
1077         card->lan_online = 0;
1078         card->use_hard_stop = 0;
1079         card->dev = NULL;
1080 #ifdef CONFIG_QETH_VLAN
1081         spin_lock_init(&card->vlanlock);
1082         card->vlangrp = NULL;
1083 #endif
1084         spin_lock_init(&card->lock);
1085         spin_lock_init(&card->ip_lock);
1086         spin_lock_init(&card->thread_mask_lock);
1087         card->thread_start_mask = 0;
1088         card->thread_allowed_mask = 0;
1089         card->thread_running_mask = 0;
1090         INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1091         INIT_LIST_HEAD(&card->ip_list);
1092         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1093         if (!card->ip_tbd_list) {
1094                 QETH_DBF_TEXT(setup, 0, "iptbdnom");
1095                 return -ENOMEM;
1096         }
1097         INIT_LIST_HEAD(card->ip_tbd_list);
1098         INIT_LIST_HEAD(&card->cmd_waiter_list);
1099         init_waitqueue_head(&card->wait_q);
1100         /* intial options */
1101         qeth_set_intial_options(card);
1102         /* IP address takeover */
1103         INIT_LIST_HEAD(&card->ipato.entries);
1104         card->ipato.enabled = 0;
1105         card->ipato.invert4 = 0;
1106         card->ipato.invert6 = 0;
1107         /* init QDIO stuff */
1108         qeth_init_qdio_info(card);
1109         return 0;
1110 }
1111
1112 static int
1113 is_1920_device (struct qeth_card *card)
1114 {
1115         int single_queue = 0;
1116         struct ccw_device *ccwdev;
1117         struct channelPath_dsc {
1118                 u8 flags;
1119                 u8 lsn;
1120                 u8 desc;
1121                 u8 chpid;
1122                 u8 swla;
1123                 u8 zeroes;
1124                 u8 chla;
1125                 u8 chpp;
1126         } *chp_dsc;
1127
1128         QETH_DBF_TEXT(setup, 2, "chk_1920");
1129
1130         ccwdev = card->data.ccwdev;
1131         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1132         if (chp_dsc != NULL) {
1133                 /* CHPP field bit 6 == 1 -> single queue */
1134                 single_queue = ((chp_dsc->chpp & 0x02) == 0x02);
1135                 kfree(chp_dsc);
1136         }
1137         QETH_DBF_TEXT_(setup, 2, "rc:%x", single_queue);
1138         return single_queue;
1139 }
1140
1141 static int
1142 qeth_determine_card_type(struct qeth_card *card)
1143 {
1144         int i = 0;
1145
1146         QETH_DBF_TEXT(setup, 2, "detcdtyp");
1147
1148         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1149         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1150         while (known_devices[i][4]) {
1151                 if ((CARD_RDEV(card)->id.dev_type == known_devices[i][2]) &&
1152                     (CARD_RDEV(card)->id.dev_model == known_devices[i][3])) {
1153                         card->info.type = known_devices[i][4];
1154                         card->qdio.no_out_queues = known_devices[i][8];
1155                         card->info.is_multicast_different = known_devices[i][9];
1156                         if (is_1920_device(card)) {
1157                                 PRINT_INFO("Priority Queueing not able "
1158                                            "due to hardware limitations!\n");
1159                                 card->qdio.no_out_queues = 1;
1160                                 card->qdio.default_out_queue = 0;
1161                         }
1162                         return 0;
1163                 }
1164                 i++;
1165         }
1166         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1167         PRINT_ERR("unknown card type on device %s\n", CARD_BUS_ID(card));
1168         return -ENOENT;
1169 }
1170
1171 static int
1172 qeth_probe_device(struct ccwgroup_device *gdev)
1173 {
1174         struct qeth_card *card;
1175         struct device *dev;
1176         unsigned long flags;
1177         int rc;
1178
1179         QETH_DBF_TEXT(setup, 2, "probedev");
1180
1181         dev = &gdev->dev;
1182         if (!get_device(dev))
1183                 return -ENODEV;
1184
1185         QETH_DBF_TEXT_(setup, 2, "%s", gdev->dev.bus_id);
1186
1187         card = qeth_alloc_card();
1188         if (!card) {
1189                 put_device(dev);
1190                 QETH_DBF_TEXT_(setup, 2, "1err%d", -ENOMEM);
1191                 return -ENOMEM;
1192         }
1193         card->read.ccwdev  = gdev->cdev[0];
1194         card->write.ccwdev = gdev->cdev[1];
1195         card->data.ccwdev  = gdev->cdev[2];
1196         gdev->dev.driver_data = card;
1197         card->gdev = gdev;
1198         gdev->cdev[0]->handler = qeth_irq;
1199         gdev->cdev[1]->handler = qeth_irq;
1200         gdev->cdev[2]->handler = qeth_irq;
1201
1202         if ((rc = qeth_determine_card_type(card))){
1203                 PRINT_WARN("%s: not a valid card type\n", __func__);
1204                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1205                 put_device(dev);
1206                 qeth_free_card(card);
1207                 return rc;
1208         }
1209         if ((rc = qeth_setup_card(card))){
1210                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1211                 put_device(dev);
1212                 qeth_free_card(card);
1213                 return rc;
1214         }
1215         rc = qeth_create_device_attributes(dev);
1216         if (rc) {
1217                 put_device(dev);
1218                 qeth_free_card(card);
1219                 return rc;
1220         }
1221         /* insert into our internal list */
1222         write_lock_irqsave(&qeth_card_list.rwlock, flags);
1223         list_add_tail(&card->list, &qeth_card_list.list);
1224         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
1225         return rc;
1226 }
1227
1228
1229 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1230                                int *length)
1231 {
1232         struct ciw *ciw;
1233         char *rcd_buf;
1234         int ret;
1235         struct qeth_channel *channel = &card->data;
1236         unsigned long flags;
1237
1238         /*
1239          * scan for RCD command in extended SenseID data
1240          */
1241         ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1242         if (!ciw || ciw->cmd == 0)
1243                 return -EOPNOTSUPP;
1244         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1245         if (!rcd_buf)
1246                 return -ENOMEM;
1247
1248         channel->ccw.cmd_code = ciw->cmd;
1249         channel->ccw.cda = (__u32) __pa (rcd_buf);
1250         channel->ccw.count = ciw->count;
1251         channel->ccw.flags = CCW_FLAG_SLI;
1252         channel->state = CH_STATE_RCD;
1253         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1254         ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1255                                        QETH_RCD_PARM, LPM_ANYPATH, 0,
1256                                        QETH_RCD_TIMEOUT);
1257         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1258         if (!ret)
1259                 wait_event(card->wait_q,
1260                            (channel->state == CH_STATE_RCD_DONE ||
1261                             channel->state == CH_STATE_DOWN));
1262         if (channel->state == CH_STATE_DOWN)
1263                 ret = -EIO;
1264         else
1265                 channel->state = CH_STATE_DOWN;
1266         if (ret) {
1267                 kfree(rcd_buf);
1268                 *buffer = NULL;
1269                 *length = 0;
1270         } else {
1271                 *length = ciw->count;
1272                 *buffer = rcd_buf;
1273         }
1274         return ret;
1275 }
1276
1277 static int
1278 qeth_get_unitaddr(struct qeth_card *card)
1279 {
1280         int length;
1281         char *prcd;
1282         int rc;
1283
1284         QETH_DBF_TEXT(setup, 2, "getunit");
1285         rc = qeth_read_conf_data(card, (void **) &prcd, &length);
1286         if (rc) {
1287                 PRINT_ERR("qeth_read_conf_data for device %s returned %i\n",
1288                           CARD_DDEV_ID(card), rc);
1289                 return rc;
1290         }
1291         card->info.chpid = prcd[30];
1292         card->info.unit_addr2 = prcd[31];
1293         card->info.cula = prcd[63];
1294         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1295                                (prcd[0x11] == _ascebc['M']));
1296         kfree(prcd);
1297         return 0;
1298 }
1299
1300 static void
1301 qeth_init_tokens(struct qeth_card *card)
1302 {
1303         card->token.issuer_rm_w = 0x00010103UL;
1304         card->token.cm_filter_w = 0x00010108UL;
1305         card->token.cm_connection_w = 0x0001010aUL;
1306         card->token.ulp_filter_w = 0x0001010bUL;
1307         card->token.ulp_connection_w = 0x0001010dUL;
1308 }
1309
1310 static inline __u16
1311 raw_devno_from_bus_id(char *id)
1312 {
1313         id += (strlen(id) - 4);
1314         return (__u16) simple_strtoul(id, &id, 16);
1315 }
1316 /**
1317  * setup channel
1318  */
1319 static void
1320 qeth_setup_ccw(struct qeth_channel *channel,unsigned char *iob, __u32 len)
1321 {
1322         struct qeth_card *card;
1323
1324         QETH_DBF_TEXT(trace, 4, "setupccw");
1325         card = CARD_FROM_CDEV(channel->ccwdev);
1326         if (channel == &card->read)
1327                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1328         else
1329                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1330         channel->ccw.count = len;
1331         channel->ccw.cda = (__u32) __pa(iob);
1332 }
1333
1334 /**
1335  * get free buffer for ccws (IDX activation, lancmds,ipassists...)
1336  */
1337 static struct qeth_cmd_buffer *
1338 __qeth_get_buffer(struct qeth_channel *channel)
1339 {
1340         __u8 index;
1341
1342         QETH_DBF_TEXT(trace, 6, "getbuff");
1343         index = channel->io_buf_no;
1344         do {
1345                 if (channel->iob[index].state == BUF_STATE_FREE) {
1346                         channel->iob[index].state = BUF_STATE_LOCKED;
1347                         channel->io_buf_no = (channel->io_buf_no + 1) %
1348                                 QETH_CMD_BUFFER_NO;
1349                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
1350                         return channel->iob + index;
1351                 }
1352                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1353         } while(index != channel->io_buf_no);
1354
1355         return NULL;
1356 }
1357
1358 /**
1359  * release command buffer
1360  */
1361 static void
1362 qeth_release_buffer(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1363 {
1364         unsigned long flags;
1365
1366         QETH_DBF_TEXT(trace, 6, "relbuff");
1367         spin_lock_irqsave(&channel->iob_lock, flags);
1368         memset(iob->data, 0, QETH_BUFSIZE);
1369         iob->state = BUF_STATE_FREE;
1370         iob->callback = qeth_send_control_data_cb;
1371         iob->rc = 0;
1372         spin_unlock_irqrestore(&channel->iob_lock, flags);
1373 }
1374
1375 static struct qeth_cmd_buffer *
1376 qeth_get_buffer(struct qeth_channel *channel)
1377 {
1378         struct qeth_cmd_buffer *buffer = NULL;
1379         unsigned long flags;
1380
1381         spin_lock_irqsave(&channel->iob_lock, flags);
1382         buffer = __qeth_get_buffer(channel);
1383         spin_unlock_irqrestore(&channel->iob_lock, flags);
1384         return buffer;
1385 }
1386
1387 static struct qeth_cmd_buffer *
1388 qeth_wait_for_buffer(struct qeth_channel *channel)
1389 {
1390         struct qeth_cmd_buffer *buffer;
1391         wait_event(channel->wait_q,
1392                    ((buffer = qeth_get_buffer(channel)) != NULL));
1393         return buffer;
1394 }
1395
1396 static void
1397 qeth_clear_cmd_buffers(struct qeth_channel *channel)
1398 {
1399         int cnt;
1400
1401         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1402                 qeth_release_buffer(channel,&channel->iob[cnt]);
1403         channel->buf_no = 0;
1404         channel->io_buf_no = 0;
1405 }
1406
1407 /**
1408  * start IDX for read and write channel
1409  */
1410 static int
1411 qeth_idx_activate_get_answer(struct qeth_channel *channel,
1412                               void (*idx_reply_cb)(struct qeth_channel *,
1413                                                    struct qeth_cmd_buffer *))
1414 {
1415         struct qeth_cmd_buffer *iob;
1416         unsigned long flags;
1417         int rc;
1418         struct qeth_card *card;
1419
1420         QETH_DBF_TEXT(setup, 2, "idxanswr");
1421         card = CARD_FROM_CDEV(channel->ccwdev);
1422         iob = qeth_get_buffer(channel);
1423         iob->callback = idx_reply_cb;
1424         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1425         channel->ccw.count = QETH_BUFSIZE;
1426         channel->ccw.cda = (__u32) __pa(iob->data);
1427
1428         wait_event(card->wait_q,
1429                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1430         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1431         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1432         rc = ccw_device_start(channel->ccwdev,
1433                               &channel->ccw,(addr_t) iob, 0, 0);
1434         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1435
1436         if (rc) {
1437                 PRINT_ERR("qeth: Error2 in activating channel rc=%d\n",rc);
1438                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1439                 atomic_set(&channel->irq_pending, 0);
1440                 wake_up(&card->wait_q);
1441                 return rc;
1442         }
1443         rc = wait_event_interruptible_timeout(card->wait_q,
1444                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1445         if (rc == -ERESTARTSYS)
1446                 return rc;
1447         if (channel->state != CH_STATE_UP){
1448                 rc = -ETIME;
1449                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1450                 qeth_clear_cmd_buffers(channel);
1451         } else
1452                 rc = 0;
1453         return rc;
1454 }
1455
1456 static int
1457 qeth_idx_activate_channel(struct qeth_channel *channel,
1458                            void (*idx_reply_cb)(struct qeth_channel *,
1459                                                 struct qeth_cmd_buffer *))
1460 {
1461         struct qeth_card *card;
1462         struct qeth_cmd_buffer *iob;
1463         unsigned long flags;
1464         __u16 temp;
1465         int rc;
1466
1467         card = CARD_FROM_CDEV(channel->ccwdev);
1468
1469         QETH_DBF_TEXT(setup, 2, "idxactch");
1470
1471         iob = qeth_get_buffer(channel);
1472         iob->callback = idx_reply_cb;
1473         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1474         channel->ccw.count = IDX_ACTIVATE_SIZE;
1475         channel->ccw.cda = (__u32) __pa(iob->data);
1476         if (channel == &card->write) {
1477                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1478                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1479                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1480                 card->seqno.trans_hdr++;
1481         } else {
1482                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1483                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1484                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1485         }
1486         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1487                &card->token.issuer_rm_w,QETH_MPC_TOKEN_LENGTH);
1488         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1489                &card->info.func_level,sizeof(__u16));
1490         temp = raw_devno_from_bus_id(CARD_DDEV_ID(card));
1491         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp, 2);
1492         temp = (card->info.cula << 8) + card->info.unit_addr2;
1493         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1494
1495         wait_event(card->wait_q,
1496                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1497         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1498         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1499         rc = ccw_device_start(channel->ccwdev,
1500                               &channel->ccw,(addr_t) iob, 0, 0);
1501         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1502
1503         if (rc) {
1504                 PRINT_ERR("qeth: Error1 in activating channel. rc=%d\n",rc);
1505                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
1506                 atomic_set(&channel->irq_pending, 0);
1507                 wake_up(&card->wait_q);
1508                 return rc;
1509         }
1510         rc = wait_event_interruptible_timeout(card->wait_q,
1511                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1512         if (rc == -ERESTARTSYS)
1513                 return rc;
1514         if (channel->state != CH_STATE_ACTIVATING) {
1515                 PRINT_WARN("qeth: IDX activate timed out!\n");
1516                 QETH_DBF_TEXT_(setup, 2, "2err%d", -ETIME);
1517                 qeth_clear_cmd_buffers(channel);
1518                 return -ETIME;
1519         }
1520         return qeth_idx_activate_get_answer(channel,idx_reply_cb);
1521 }
1522
1523 static int
1524 qeth_peer_func_level(int level)
1525 {
1526         if ((level & 0xff) == 8)
1527                 return (level & 0xff) + 0x400;
1528         if (((level >> 8) & 3) == 1)
1529                 return (level & 0xff) + 0x200;
1530         return level;
1531 }
1532
1533 static void
1534 qeth_idx_write_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1535 {
1536         struct qeth_card *card;
1537         __u16 temp;
1538
1539         QETH_DBF_TEXT(setup ,2, "idxwrcb");
1540
1541         if (channel->state == CH_STATE_DOWN) {
1542                 channel->state = CH_STATE_ACTIVATING;
1543                 goto out;
1544         }
1545         card = CARD_FROM_CDEV(channel->ccwdev);
1546
1547         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1548                 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == 0x19)
1549                         PRINT_ERR("IDX_ACTIVATE on write channel device %s: "
1550                                 "adapter exclusively used by another host\n",
1551                                 CARD_WDEV_ID(card));
1552                 else
1553                         PRINT_ERR("IDX_ACTIVATE on write channel device %s: "
1554                                 "negative reply\n", CARD_WDEV_ID(card));
1555                 goto out;
1556         }
1557         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1558         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1559                 PRINT_WARN("IDX_ACTIVATE on write channel device %s: "
1560                         "function level mismatch "
1561                         "(sent: 0x%x, received: 0x%x)\n",
1562                         CARD_WDEV_ID(card), card->info.func_level, temp);
1563                 goto out;
1564         }
1565         channel->state = CH_STATE_UP;
1566 out:
1567         qeth_release_buffer(channel, iob);
1568 }
1569
1570 static int
1571 qeth_check_idx_response(unsigned char *buffer)
1572 {
1573         if (!buffer)
1574                 return 0;
1575
1576         QETH_DBF_HEX(control, 2, buffer, QETH_DBF_CONTROL_LEN);
1577         if ((buffer[2] & 0xc0) == 0xc0) {
1578                 PRINT_WARN("received an IDX TERMINATE "
1579                            "with cause code 0x%02x%s\n",
1580                            buffer[4],
1581                            ((buffer[4] == 0x22) ?
1582                             " -- try another portname" : ""));
1583                 QETH_DBF_TEXT(trace, 2, "ckidxres");
1584                 QETH_DBF_TEXT(trace, 2, " idxterm");
1585                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
1586                 return -EIO;
1587         }
1588         return 0;
1589 }
1590
1591 static void
1592 qeth_idx_read_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1593 {
1594         struct qeth_card *card;
1595         __u16 temp;
1596
1597         QETH_DBF_TEXT(setup , 2, "idxrdcb");
1598         if (channel->state == CH_STATE_DOWN) {
1599                 channel->state = CH_STATE_ACTIVATING;
1600                 goto out;
1601         }
1602
1603         card = CARD_FROM_CDEV(channel->ccwdev);
1604         if (qeth_check_idx_response(iob->data)) {
1605                         goto out;
1606         }
1607         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1608                 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == 0x19)
1609                         PRINT_ERR("IDX_ACTIVATE on read channel device %s: "
1610                                 "adapter exclusively used by another host\n",
1611                                 CARD_RDEV_ID(card));
1612                 else
1613                         PRINT_ERR("IDX_ACTIVATE on read channel device %s: "
1614                                 "negative reply\n", CARD_RDEV_ID(card));
1615                 goto out;
1616         }
1617
1618 /**
1619  * temporary fix for microcode bug
1620  * to revert it,replace OR by AND
1621  */
1622         if ( (!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1623              (card->info.type == QETH_CARD_TYPE_OSAE) )
1624                 card->info.portname_required = 1;
1625
1626         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1627         if (temp != qeth_peer_func_level(card->info.func_level)) {
1628                 PRINT_WARN("IDX_ACTIVATE on read channel device %s: function "
1629                         "level mismatch (sent: 0x%x, received: 0x%x)\n",
1630                         CARD_RDEV_ID(card), card->info.func_level, temp);
1631                 goto out;
1632         }
1633         memcpy(&card->token.issuer_rm_r,
1634                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1635                QETH_MPC_TOKEN_LENGTH);
1636         memcpy(&card->info.mcl_level[0],
1637                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1638         channel->state = CH_STATE_UP;
1639 out:
1640         qeth_release_buffer(channel,iob);
1641 }
1642
1643 static int
1644 qeth_issue_next_read(struct qeth_card *card)
1645 {
1646         int rc;
1647         struct qeth_cmd_buffer *iob;
1648
1649         QETH_DBF_TEXT(trace,5,"issnxrd");
1650         if (card->read.state != CH_STATE_UP)
1651                 return -EIO;
1652         iob = qeth_get_buffer(&card->read);
1653         if (!iob) {
1654                 PRINT_WARN("issue_next_read failed: no iob available!\n");
1655                 return -ENOMEM;
1656         }
1657         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
1658         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1659         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
1660                               (addr_t) iob, 0, 0);
1661         if (rc) {
1662                 PRINT_ERR("Error in starting next read ccw! rc=%i\n", rc);
1663                 atomic_set(&card->read.irq_pending, 0);
1664                 qeth_schedule_recovery(card);
1665                 wake_up(&card->wait_q);
1666         }
1667         return rc;
1668 }
1669
1670 static struct qeth_reply *
1671 qeth_alloc_reply(struct qeth_card *card)
1672 {
1673         struct qeth_reply *reply;
1674
1675         reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
1676         if (reply){
1677                 atomic_set(&reply->refcnt, 1);
1678                 atomic_set(&reply->received, 0);
1679                 reply->card = card;
1680         };
1681         return reply;
1682 }
1683
1684 static void
1685 qeth_get_reply(struct qeth_reply *reply)
1686 {
1687         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1688         atomic_inc(&reply->refcnt);
1689 }
1690
1691 static void
1692 qeth_put_reply(struct qeth_reply *reply)
1693 {
1694         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1695         if (atomic_dec_and_test(&reply->refcnt))
1696                 kfree(reply);
1697 }
1698
1699 static void
1700 qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, struct qeth_card *card)
1701 {
1702         int rc;
1703         int com;
1704         char * ipa_name;
1705
1706         com = cmd->hdr.command;
1707         rc  = cmd->hdr.return_code;
1708         ipa_name = qeth_get_ipa_cmd_name(com);
1709
1710         PRINT_ERR("%s(x%X) for %s returned x%X \"%s\"\n", ipa_name, com,
1711                    QETH_CARD_IFNAME(card), rc, qeth_get_ipa_msg(rc));
1712 }
1713
1714 static struct qeth_ipa_cmd *
1715 qeth_check_ipa_data(struct qeth_card *card, struct qeth_cmd_buffer *iob)
1716 {
1717         struct qeth_ipa_cmd *cmd = NULL;
1718
1719         QETH_DBF_TEXT(trace,5,"chkipad");
1720         if (IS_IPA(iob->data)){
1721                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
1722                 if (IS_IPA_REPLY(cmd)) {
1723                         if (cmd->hdr.return_code)
1724                                 qeth_issue_ipa_msg(cmd, card);
1725                         return cmd;
1726                 }
1727                 else {
1728                         switch (cmd->hdr.command) {
1729                         case IPA_CMD_STOPLAN:
1730                                 PRINT_WARN("Link failure on %s (CHPID 0x%X) - "
1731                                            "there is a network problem or "
1732                                            "someone pulled the cable or "
1733                                            "disabled the port.\n",
1734                                            QETH_CARD_IFNAME(card),
1735                                            card->info.chpid);
1736                                 card->lan_online = 0;
1737                                 if (card->dev && netif_carrier_ok(card->dev))
1738                                         netif_carrier_off(card->dev);
1739                                 return NULL;
1740                         case IPA_CMD_STARTLAN:
1741                                 PRINT_INFO("Link reestablished on %s "
1742                                            "(CHPID 0x%X). Scheduling "
1743                                            "IP address reset.\n",
1744                                            QETH_CARD_IFNAME(card),
1745                                            card->info.chpid);
1746                                 netif_carrier_on(card->dev);
1747                                 qeth_schedule_recovery(card);
1748                                 return NULL;
1749                         case IPA_CMD_MODCCID:
1750                                 return cmd;
1751                         case IPA_CMD_REGISTER_LOCAL_ADDR:
1752                                 QETH_DBF_TEXT(trace,3, "irla");
1753                                 break;
1754                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
1755                                 QETH_DBF_TEXT(trace,3, "urla");
1756                                 break;
1757                         default:
1758                                 PRINT_WARN("Received data is IPA "
1759                                            "but not a reply!\n");
1760                                 break;
1761                         }
1762                 }
1763         }
1764         return cmd;
1765 }
1766
1767 /**
1768  * wake all waiting ipa commands
1769  */
1770 static void
1771 qeth_clear_ipacmd_list(struct qeth_card *card)
1772 {
1773         struct qeth_reply *reply, *r;
1774         unsigned long flags;
1775
1776         QETH_DBF_TEXT(trace, 4, "clipalst");
1777
1778         spin_lock_irqsave(&card->lock, flags);
1779         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1780                 qeth_get_reply(reply);
1781                 reply->rc = -EIO;
1782                 atomic_inc(&reply->received);
1783                 list_del_init(&reply->list);
1784                 wake_up(&reply->wait_q);
1785                 qeth_put_reply(reply);
1786         }
1787         spin_unlock_irqrestore(&card->lock, flags);
1788 }
1789
1790 static void
1791 qeth_send_control_data_cb(struct qeth_channel *channel,
1792                           struct qeth_cmd_buffer *iob)
1793 {
1794         struct qeth_card *card;
1795         struct qeth_reply *reply, *r;
1796         struct qeth_ipa_cmd *cmd;
1797         unsigned long flags;
1798         int keep_reply;
1799
1800         QETH_DBF_TEXT(trace,4,"sndctlcb");
1801
1802         card = CARD_FROM_CDEV(channel->ccwdev);
1803         if (qeth_check_idx_response(iob->data)) {
1804                 qeth_clear_ipacmd_list(card);
1805                 qeth_schedule_recovery(card);
1806                 goto out;
1807         }
1808
1809         cmd = qeth_check_ipa_data(card, iob);
1810         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
1811                 goto out;
1812         /*in case of OSN : check if cmd is set */
1813         if (card->info.type == QETH_CARD_TYPE_OSN &&
1814             cmd &&
1815             cmd->hdr.command != IPA_CMD_STARTLAN &&
1816             card->osn_info.assist_cb != NULL) {
1817                 card->osn_info.assist_cb(card->dev, cmd);
1818                 goto out;
1819         }
1820
1821         spin_lock_irqsave(&card->lock, flags);
1822         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1823                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
1824                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
1825                         qeth_get_reply(reply);
1826                         list_del_init(&reply->list);
1827                         spin_unlock_irqrestore(&card->lock, flags);
1828                         keep_reply = 0;
1829                         if (reply->callback != NULL) {
1830                                 if (cmd) {
1831                                         reply->offset = (__u16)((char*)cmd -
1832                                                                 (char *)iob->data);
1833                                         keep_reply = reply->callback(card,
1834                                                         reply,
1835                                                         (unsigned long)cmd);
1836                                 } else
1837                                         keep_reply = reply->callback(card,
1838                                                         reply,
1839                                                         (unsigned long)iob);
1840                         }
1841                         if (cmd)
1842                                 reply->rc = (u16) cmd->hdr.return_code;
1843                         else if (iob->rc)
1844                                 reply->rc = iob->rc;
1845                         if (keep_reply) {
1846                                 spin_lock_irqsave(&card->lock, flags);
1847                                 list_add_tail(&reply->list,
1848                                               &card->cmd_waiter_list);
1849                                 spin_unlock_irqrestore(&card->lock, flags);
1850                         } else {
1851                                 atomic_inc(&reply->received);
1852                                 wake_up(&reply->wait_q);
1853                         }
1854                         qeth_put_reply(reply);
1855                         goto out;
1856                 }
1857         }
1858         spin_unlock_irqrestore(&card->lock, flags);
1859 out:
1860         memcpy(&card->seqno.pdu_hdr_ack,
1861                 QETH_PDU_HEADER_SEQ_NO(iob->data),
1862                 QETH_SEQ_NO_LENGTH);
1863         qeth_release_buffer(channel,iob);
1864 }
1865
1866 static void
1867 qeth_prepare_control_data(struct qeth_card *card, int len,
1868                           struct qeth_cmd_buffer *iob)
1869 {
1870         qeth_setup_ccw(&card->write,iob->data,len);
1871         iob->callback = qeth_release_buffer;
1872
1873         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1874                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1875         card->seqno.trans_hdr++;
1876         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1877                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1878         card->seqno.pdu_hdr++;
1879         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1880                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1881         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1882 }
1883
1884 static int
1885 qeth_send_control_data(struct qeth_card *card, int len,
1886                        struct qeth_cmd_buffer *iob,
1887                        int (*reply_cb)
1888                        (struct qeth_card *, struct qeth_reply*, unsigned long),
1889                        void *reply_param)
1890
1891 {
1892         int rc;
1893         unsigned long flags;
1894         struct qeth_reply *reply = NULL;
1895         unsigned long timeout;
1896
1897         QETH_DBF_TEXT(trace, 2, "sendctl");
1898
1899         reply = qeth_alloc_reply(card);
1900         if (!reply) {
1901                 PRINT_WARN("Could no alloc qeth_reply!\n");
1902                 return -ENOMEM;
1903         }
1904         reply->callback = reply_cb;
1905         reply->param = reply_param;
1906         if (card->state == CARD_STATE_DOWN)
1907                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1908         else
1909                 reply->seqno = card->seqno.ipa++;
1910         init_waitqueue_head(&reply->wait_q);
1911         spin_lock_irqsave(&card->lock, flags);
1912         list_add_tail(&reply->list, &card->cmd_waiter_list);
1913         spin_unlock_irqrestore(&card->lock, flags);
1914         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1915
1916         while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
1917         qeth_prepare_control_data(card, len, iob);
1918
1919         if (IS_IPA(iob->data))
1920                 timeout = jiffies + QETH_IPA_TIMEOUT;
1921         else
1922                 timeout = jiffies + QETH_TIMEOUT;
1923
1924         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1925         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1926         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1927                               (addr_t) iob, 0, 0);
1928         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1929         if (rc){
1930                 PRINT_WARN("qeth_send_control_data: "
1931                            "ccw_device_start rc = %i\n", rc);
1932                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1933                 spin_lock_irqsave(&card->lock, flags);
1934                 list_del_init(&reply->list);
1935                 qeth_put_reply(reply);
1936                 spin_unlock_irqrestore(&card->lock, flags);
1937                 qeth_release_buffer(iob->channel, iob);
1938                 atomic_set(&card->write.irq_pending, 0);
1939                 wake_up(&card->wait_q);
1940                 return rc;
1941         }
1942         while (!atomic_read(&reply->received)) {
1943                 if (time_after(jiffies, timeout)) {
1944                         spin_lock_irqsave(&reply->card->lock, flags);
1945                         list_del_init(&reply->list);
1946                         spin_unlock_irqrestore(&reply->card->lock, flags);
1947                         reply->rc = -ETIME;
1948                         atomic_inc(&reply->received);
1949                         wake_up(&reply->wait_q);
1950                 }
1951                 cpu_relax();
1952         };
1953         rc = reply->rc;
1954         qeth_put_reply(reply);
1955         return rc;
1956 }
1957
1958 static int
1959 qeth_osn_send_control_data(struct qeth_card *card, int len,
1960                            struct qeth_cmd_buffer *iob)
1961 {
1962         unsigned long flags;
1963         int rc = 0;
1964
1965         QETH_DBF_TEXT(trace, 5, "osndctrd");
1966
1967         wait_event(card->wait_q,
1968                    atomic_cmpxchg(&card->write.irq_pending, 0, 1) == 0);
1969         qeth_prepare_control_data(card, len, iob);
1970         QETH_DBF_TEXT(trace, 6, "osnoirqp");
1971         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1972         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1973                               (addr_t) iob, 0, 0);
1974         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1975         if (rc){
1976                 PRINT_WARN("qeth_osn_send_control_data: "
1977                            "ccw_device_start rc = %i\n", rc);
1978                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1979                 qeth_release_buffer(iob->channel, iob);
1980                 atomic_set(&card->write.irq_pending, 0);
1981                 wake_up(&card->wait_q);
1982         }
1983         return rc;
1984 }
1985
1986 static inline void
1987 qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1988                      char prot_type)
1989 {
1990         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1991         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data),&prot_type,1);
1992         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1993                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1994 }
1995
1996 static int
1997 qeth_osn_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1998                       int data_len)
1999 {
2000         u16 s1, s2;
2001
2002         QETH_DBF_TEXT(trace,4,"osndipa");
2003
2004         qeth_prepare_ipa_cmd(card, iob, QETH_PROT_OSN2);
2005         s1 = (u16)(IPA_PDU_HEADER_SIZE + data_len);
2006         s2 = (u16)data_len;
2007         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
2008         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
2009         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
2010         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
2011         return qeth_osn_send_control_data(card, s1, iob);
2012 }
2013
2014 static int
2015 qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2016                   int (*reply_cb)
2017                   (struct qeth_card *,struct qeth_reply*, unsigned long),
2018                   void *reply_param)
2019 {
2020         int rc;
2021         char prot_type;
2022
2023         QETH_DBF_TEXT(trace,4,"sendipa");
2024
2025         if (card->options.layer2)
2026                 if (card->info.type == QETH_CARD_TYPE_OSN)
2027                         prot_type = QETH_PROT_OSN2;
2028                 else
2029                         prot_type = QETH_PROT_LAYER2;
2030         else
2031                 prot_type = QETH_PROT_TCPIP;
2032         qeth_prepare_ipa_cmd(card,iob,prot_type);
2033         rc = qeth_send_control_data(card, IPA_CMD_LENGTH, iob,
2034                                     reply_cb, reply_param);
2035         return rc;
2036 }
2037
2038
2039 static int
2040 qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2041                   unsigned long data)
2042 {
2043         struct qeth_cmd_buffer *iob;
2044
2045         QETH_DBF_TEXT(setup, 2, "cmenblcb");
2046
2047         iob = (struct qeth_cmd_buffer *) data;
2048         memcpy(&card->token.cm_filter_r,
2049                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2050                QETH_MPC_TOKEN_LENGTH);
2051         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2052         return 0;
2053 }
2054
2055 static int
2056 qeth_cm_enable(struct qeth_card *card)
2057 {
2058         int rc;
2059         struct qeth_cmd_buffer *iob;
2060
2061         QETH_DBF_TEXT(setup,2,"cmenable");
2062
2063         iob = qeth_wait_for_buffer(&card->write);
2064         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2065         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2066                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2067         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2068                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2069
2070         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2071                                     qeth_cm_enable_cb, NULL);
2072         return rc;
2073 }
2074
2075 static int
2076 qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2077                  unsigned long data)
2078 {
2079
2080         struct qeth_cmd_buffer *iob;
2081
2082         QETH_DBF_TEXT(setup, 2, "cmsetpcb");
2083
2084         iob = (struct qeth_cmd_buffer *) data;
2085         memcpy(&card->token.cm_connection_r,
2086                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2087                QETH_MPC_TOKEN_LENGTH);
2088         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2089         return 0;
2090 }
2091
2092 static int
2093 qeth_cm_setup(struct qeth_card *card)
2094 {
2095         int rc;
2096         struct qeth_cmd_buffer *iob;
2097
2098         QETH_DBF_TEXT(setup,2,"cmsetup");
2099
2100         iob = qeth_wait_for_buffer(&card->write);
2101         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2102         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2103                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2104         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2105                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2106         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2107                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2108         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2109                                     qeth_cm_setup_cb, NULL);
2110         return rc;
2111
2112 }
2113
2114 static int
2115 qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2116                    unsigned long data)
2117 {
2118
2119         __u16 mtu, framesize;
2120         __u16 len;
2121         __u8 link_type;
2122         struct qeth_cmd_buffer *iob;
2123
2124         QETH_DBF_TEXT(setup, 2, "ulpenacb");
2125
2126         iob = (struct qeth_cmd_buffer *) data;
2127         memcpy(&card->token.ulp_filter_r,
2128                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2129                QETH_MPC_TOKEN_LENGTH);
2130         if (qeth_get_mtu_out_of_mpc(card->info.type)) {
2131                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2132                 mtu = qeth_get_mtu_outof_framesize(framesize);
2133                 if (!mtu) {
2134                         iob->rc = -EINVAL;
2135                         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2136                         return 0;
2137                 }
2138                 card->info.max_mtu = mtu;
2139                 card->info.initial_mtu = mtu;
2140                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2141         } else {
2142                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
2143                 card->info.max_mtu = qeth_get_max_mtu_for_card(card->info.type);
2144                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2145         }
2146
2147         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2148         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2149                 memcpy(&link_type,
2150                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2151                 card->info.link_type = link_type;
2152         } else
2153                 card->info.link_type = 0;
2154         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2155         return 0;
2156 }
2157
2158 static int
2159 qeth_ulp_enable(struct qeth_card *card)
2160 {
2161         int rc;
2162         char prot_type;
2163         struct qeth_cmd_buffer *iob;
2164
2165         /*FIXME: trace view callbacks*/
2166         QETH_DBF_TEXT(setup,2,"ulpenabl");
2167
2168         iob = qeth_wait_for_buffer(&card->write);
2169         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2170
2171         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2172                 (__u8) card->info.portno;
2173         if (card->options.layer2)
2174                 if (card->info.type == QETH_CARD_TYPE_OSN)
2175                         prot_type = QETH_PROT_OSN2;
2176                 else
2177                         prot_type = QETH_PROT_LAYER2;
2178         else
2179                 prot_type = QETH_PROT_TCPIP;
2180
2181         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data),&prot_type,1);
2182         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2183                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2184         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2185                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2186         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2187                card->info.portname, 9);
2188         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2189                                     qeth_ulp_enable_cb, NULL);
2190         return rc;
2191
2192 }
2193
2194 static int
2195 qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2196                   unsigned long data)
2197 {
2198         struct qeth_cmd_buffer *iob;
2199
2200         QETH_DBF_TEXT(setup, 2, "ulpstpcb");
2201
2202         iob = (struct qeth_cmd_buffer *) data;
2203         memcpy(&card->token.ulp_connection_r,
2204                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2205                QETH_MPC_TOKEN_LENGTH);
2206         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2207         return 0;
2208 }
2209
2210 static int
2211 qeth_ulp_setup(struct qeth_card *card)
2212 {
2213         int rc;
2214         __u16 temp;
2215         struct qeth_cmd_buffer *iob;
2216         struct ccw_dev_id dev_id;
2217
2218         QETH_DBF_TEXT(setup,2,"ulpsetup");
2219
2220         iob = qeth_wait_for_buffer(&card->write);
2221         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2222
2223         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2224                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2225         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2226                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2227         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2228                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2229
2230         ccw_device_get_id(CARD_DDEV(card), &dev_id);
2231         memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2232         temp = (card->info.cula << 8) + card->info.unit_addr2;
2233         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2234         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2235                                     qeth_ulp_setup_cb, NULL);
2236         return rc;
2237 }
2238
2239 static inline int
2240 qeth_check_qdio_errors(struct qdio_buffer *buf, unsigned int qdio_error,
2241                        unsigned int siga_error, const char *dbftext)
2242 {
2243         if (qdio_error || siga_error) {
2244                 QETH_DBF_TEXT(trace, 2, dbftext);
2245                 QETH_DBF_TEXT(qerr, 2, dbftext);
2246                 QETH_DBF_TEXT_(qerr, 2, " F15=%02X",
2247                                buf->element[15].flags & 0xff);
2248                 QETH_DBF_TEXT_(qerr, 2, " F14=%02X",
2249                                buf->element[14].flags & 0xff);
2250                 QETH_DBF_TEXT_(qerr, 2, " qerr=%X", qdio_error);
2251                 QETH_DBF_TEXT_(qerr, 2, " serr=%X", siga_error);
2252                 return 1;
2253         }
2254         return 0;
2255 }
2256
2257 static struct sk_buff *
2258 qeth_get_skb(unsigned int length, struct qeth_hdr *hdr)
2259 {
2260         struct sk_buff* skb;
2261         int add_len;
2262
2263         add_len = 0;
2264         if (hdr->hdr.osn.id == QETH_HEADER_TYPE_OSN)
2265                 add_len = sizeof(struct qeth_hdr);
2266 #ifdef CONFIG_QETH_VLAN
2267         else
2268                 add_len = VLAN_HLEN;
2269 #endif
2270         skb = dev_alloc_skb(length + add_len);
2271         if (skb && add_len)
2272                 skb_reserve(skb, add_len);
2273         return skb;
2274 }
2275
2276 static inline int
2277 qeth_create_skb_frag(struct qdio_buffer_element *element,
2278                      struct sk_buff **pskb,
2279                      int offset, int *pfrag, int data_len)
2280 {
2281         struct page *page = virt_to_page(element->addr);
2282         if (*pfrag == 0) {
2283                 /* the upper protocol layers assume that there is data in the
2284                  * skb itself. Copy a small amount (64 bytes) to make them
2285                  * happy. */
2286                 *pskb = dev_alloc_skb(64 + QETH_FAKE_LL_LEN_ETH);
2287                 if (!(*pskb))
2288                         return -ENOMEM;
2289                 skb_reserve(*pskb, QETH_FAKE_LL_LEN_ETH);
2290                 if (data_len <= 64) {
2291                         memcpy(skb_put(*pskb, data_len), element->addr + offset,
2292                                 data_len);
2293                 } else {
2294                         get_page(page);
2295                         memcpy(skb_put(*pskb, 64), element->addr + offset, 64);
2296                         skb_fill_page_desc(*pskb, *pfrag, page, offset + 64,
2297                                 data_len - 64);
2298                         (*pskb)->data_len += data_len - 64;
2299                         (*pskb)->len      += data_len - 64;
2300                         (*pskb)->truesize += data_len - 64;
2301                 }
2302         } else {
2303                 get_page(page);
2304                 skb_fill_page_desc(*pskb, *pfrag, page, offset, data_len);
2305                 (*pskb)->data_len += data_len;
2306                 (*pskb)->len      += data_len;
2307                 (*pskb)->truesize += data_len;
2308         }
2309         (*pfrag)++;
2310         return 0;
2311 }
2312
2313 static inline struct qeth_buffer_pool_entry *
2314 qeth_find_free_buffer_pool_entry(struct qeth_card *card)
2315 {
2316         struct list_head *plh;
2317         struct qeth_buffer_pool_entry *entry;
2318         int i, free;
2319         struct page *page;
2320
2321         if (list_empty(&card->qdio.in_buf_pool.entry_list))
2322                 return NULL;
2323
2324         list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2325                 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2326                 free = 1;
2327                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2328                         if (page_count(virt_to_page(entry->elements[i])) > 1) {
2329                                 free = 0;
2330                                 break;
2331                         }
2332                 }
2333                 if (free) {
2334                         list_del_init(&entry->list);
2335                         return entry;
2336                 }
2337         }
2338
2339         /* no free buffer in pool so take first one and swap pages */
2340         entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2341                         struct qeth_buffer_pool_entry, list);
2342         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2343                 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2344                         page = alloc_page(GFP_ATOMIC|GFP_DMA);
2345                         if (!page) {
2346                                 return NULL;
2347                         } else {
2348                                 free_page((unsigned long)entry->elements[i]);
2349                                 entry->elements[i] = page_address(page);
2350                                 if (card->options.performance_stats)
2351                                         card->perf_stats.sg_alloc_page_rx++;
2352                         }
2353                 }
2354         }
2355         list_del_init(&entry->list);
2356         return entry;
2357 }
2358
2359 static struct sk_buff *
2360 qeth_get_next_skb(struct qeth_card *card, struct qdio_buffer *buffer,
2361                   struct qdio_buffer_element **__element, int *__offset,
2362                   struct qeth_hdr **hdr)
2363 {
2364         struct qdio_buffer_element *element = *__element;
2365         int offset = *__offset;
2366         struct sk_buff *skb = NULL;
2367         int skb_len;
2368         void *data_ptr;
2369         int data_len;
2370         int use_rx_sg = 0;
2371         int frag = 0;
2372
2373         QETH_DBF_TEXT(trace,6,"nextskb");
2374         /* qeth_hdr must not cross element boundaries */
2375         if (element->length < offset + sizeof(struct qeth_hdr)){
2376                 if (qeth_is_last_sbale(element))
2377                         return NULL;
2378                 element++;
2379                 offset = 0;
2380                 if (element->length < sizeof(struct qeth_hdr))
2381                         return NULL;
2382         }
2383         *hdr = element->addr + offset;
2384
2385         offset += sizeof(struct qeth_hdr);
2386         if (card->options.layer2)
2387                 if (card->info.type == QETH_CARD_TYPE_OSN)
2388                         skb_len = (*hdr)->hdr.osn.pdu_length;
2389                 else
2390                         skb_len = (*hdr)->hdr.l2.pkt_length;
2391         else
2392                 skb_len = (*hdr)->hdr.l3.length;
2393
2394         if (!skb_len)
2395                 return NULL;
2396         if ((skb_len >= card->options.rx_sg_cb) &&
2397             (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
2398             (!atomic_read(&card->force_alloc_skb))) {
2399                 use_rx_sg = 1;
2400         } else {
2401                 if (card->options.fake_ll) {
2402                         if (card->dev->type == ARPHRD_IEEE802_TR) {
2403                                 if (!(skb = qeth_get_skb(skb_len +
2404                                                 QETH_FAKE_LL_LEN_TR, *hdr)))
2405                                         goto no_mem;
2406                                 skb_reserve(skb, QETH_FAKE_LL_LEN_TR);
2407                         } else {
2408                                 if (!(skb = qeth_get_skb(skb_len +
2409                                                 QETH_FAKE_LL_LEN_ETH, *hdr)))
2410                                         goto no_mem;
2411                                 skb_reserve(skb, QETH_FAKE_LL_LEN_ETH);
2412                         }
2413                 } else {
2414                         skb = qeth_get_skb(skb_len, *hdr);
2415                         if (!skb)
2416                                 goto no_mem;
2417                 }
2418         }
2419
2420         data_ptr = element->addr + offset;
2421         while (skb_len) {
2422                 data_len = min(skb_len, (int)(element->length - offset));
2423                 if (data_len) {
2424                         if (use_rx_sg) {
2425                                 if (qeth_create_skb_frag(element, &skb, offset,
2426                                     &frag, data_len))
2427                                         goto no_mem;
2428                         } else {
2429                                 memcpy(skb_put(skb, data_len), data_ptr,
2430                                         data_len);
2431                         }
2432                 }
2433                 skb_len -= data_len;
2434                 if (skb_len){
2435                         if (qeth_is_last_sbale(element)){
2436                                 QETH_DBF_TEXT(trace,4,"unexeob");
2437                                 QETH_DBF_TEXT_(trace,4,"%s",CARD_BUS_ID(card));
2438                                 QETH_DBF_TEXT(qerr,2,"unexeob");
2439                                 QETH_DBF_TEXT_(qerr,2,"%s",CARD_BUS_ID(card));
2440                                 QETH_DBF_HEX(misc,4,buffer,sizeof(*buffer));
2441                                 dev_kfree_skb_any(skb);
2442                                 card->stats.rx_errors++;
2443                                 return NULL;
2444                         }
2445                         element++;
2446                         offset = 0;
2447                         data_ptr = element->addr;
2448                 } else {
2449                         offset += data_len;
2450                 }
2451         }
2452         *__element = element;
2453         *__offset = offset;
2454         if (use_rx_sg && card->options.performance_stats) {
2455                 card->perf_stats.sg_skbs_rx++;
2456                 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
2457         }
2458         return skb;
2459 no_mem:
2460         if (net_ratelimit()){
2461                 PRINT_WARN("No memory for packet received on %s.\n",
2462                            QETH_CARD_IFNAME(card));
2463                 QETH_DBF_TEXT(trace,2,"noskbmem");
2464                 QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2465         }
2466         card->stats.rx_dropped++;
2467         return NULL;
2468 }
2469
2470 static __be16
2471 qeth_type_trans(struct sk_buff *skb, struct net_device *dev)
2472 {
2473         struct qeth_card *card;
2474         struct ethhdr *eth;
2475
2476         QETH_DBF_TEXT(trace,6,"typtrans");
2477
2478         card = (struct qeth_card *)dev->priv;
2479 #ifdef CONFIG_TR
2480         if ((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
2481             (card->info.link_type == QETH_LINK_TYPE_LANE_TR))
2482                 return tr_type_trans(skb,dev);
2483 #endif /* CONFIG_TR */
2484         skb_reset_mac_header(skb);
2485         skb_pull(skb, ETH_HLEN );
2486         eth = eth_hdr(skb);
2487
2488         if (*eth->h_dest & 1) {
2489                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
2490                         skb->pkt_type = PACKET_BROADCAST;
2491                 else
2492                         skb->pkt_type = PACKET_MULTICAST;
2493         } else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
2494                 skb->pkt_type = PACKET_OTHERHOST;
2495
2496         if (ntohs(eth->h_proto) >= 1536)
2497                 return eth->h_proto;
2498         if (*(unsigned short *) (skb->data) == 0xFFFF)
2499                 return htons(ETH_P_802_3);
2500         return htons(ETH_P_802_2);
2501 }
2502
2503 static void
2504 qeth_rebuild_skb_fake_ll_tr(struct qeth_card *card, struct sk_buff *skb,
2505                          struct qeth_hdr *hdr)
2506 {
2507         struct trh_hdr *fake_hdr;
2508         struct trllc *fake_llc;
2509         struct iphdr *ip_hdr;
2510
2511         QETH_DBF_TEXT(trace,5,"skbfktr");
2512         skb_set_mac_header(skb, (int)-QETH_FAKE_LL_LEN_TR);
2513         /* this is a fake ethernet header */
2514         fake_hdr = tr_hdr(skb);
2515
2516         /* the destination MAC address */
2517         switch (skb->pkt_type){
2518         case PACKET_MULTICAST:
2519                 switch (skb->protocol){
2520 #ifdef CONFIG_QETH_IPV6
2521                 case __constant_htons(ETH_P_IPV6):
2522                         ndisc_mc_map((struct in6_addr *)
2523                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2524                                      fake_hdr->daddr, card->dev, 0);
2525                         break;
2526 #endif /* CONFIG_QETH_IPV6 */
2527                 case __constant_htons(ETH_P_IP):
2528                         ip_hdr = (struct iphdr *)skb->data;
2529                         ip_tr_mc_map(ip_hdr->daddr, fake_hdr->daddr);
2530                         break;
2531                 default:
2532                         memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2533                 }
2534                 break;
2535         case PACKET_BROADCAST:
2536                 memset(fake_hdr->daddr, 0xff, TR_ALEN);
2537                 break;
2538         default:
2539                 memcpy(fake_hdr->daddr, card->dev->dev_addr, TR_ALEN);
2540         }
2541         /* the source MAC address */
2542         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2543                 memcpy(fake_hdr->saddr, &hdr->hdr.l3.dest_addr[2], TR_ALEN);
2544         else
2545                 memset(fake_hdr->saddr, 0, TR_ALEN);
2546         fake_hdr->rcf=0;
2547         fake_llc = (struct trllc*)&(fake_hdr->rcf);
2548         fake_llc->dsap = EXTENDED_SAP;
2549         fake_llc->ssap = EXTENDED_SAP;
2550         fake_llc->llc  = UI_CMD;
2551         fake_llc->protid[0] = 0;
2552         fake_llc->protid[1] = 0;
2553         fake_llc->protid[2] = 0;
2554         fake_llc->ethertype = ETH_P_IP;
2555 }
2556
2557 static void
2558 qeth_rebuild_skb_fake_ll_eth(struct qeth_card *card, struct sk_buff *skb,
2559                          struct qeth_hdr *hdr)
2560 {
2561         struct ethhdr *fake_hdr;
2562         struct iphdr *ip_hdr;
2563
2564         QETH_DBF_TEXT(trace,5,"skbfketh");
2565         skb_set_mac_header(skb, -QETH_FAKE_LL_LEN_ETH);
2566         /* this is a fake ethernet header */
2567         fake_hdr = eth_hdr(skb);
2568
2569         /* the destination MAC address */
2570         switch (skb->pkt_type){
2571         case PACKET_MULTICAST:
2572                 switch (skb->protocol){
2573 #ifdef CONFIG_QETH_IPV6
2574                 case __constant_htons(ETH_P_IPV6):
2575                         ndisc_mc_map((struct in6_addr *)
2576                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2577                                      fake_hdr->h_dest, card->dev, 0);
2578                         break;
2579 #endif /* CONFIG_QETH_IPV6 */
2580                 case __constant_htons(ETH_P_IP):
2581                         ip_hdr = (struct iphdr *)skb->data;
2582                         ip_eth_mc_map(ip_hdr->daddr, fake_hdr->h_dest);
2583                         break;
2584                 default:
2585                         memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2586                 }
2587                 break;
2588         case PACKET_BROADCAST:
2589                 memset(fake_hdr->h_dest, 0xff, ETH_ALEN);
2590                 break;
2591         default:
2592                 memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2593         }
2594         /* the source MAC address */
2595         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2596                 memcpy(fake_hdr->h_source, &hdr->hdr.l3.dest_addr[2], ETH_ALEN);
2597         else
2598                 memset(fake_hdr->h_source, 0, ETH_ALEN);
2599         /* the protocol */
2600         fake_hdr->h_proto = skb->protocol;
2601 }
2602
2603 static inline void
2604 qeth_rebuild_skb_fake_ll(struct qeth_card *card, struct sk_buff *skb,
2605                         struct qeth_hdr *hdr)
2606 {
2607         if (card->dev->type == ARPHRD_IEEE802_TR)
2608                 qeth_rebuild_skb_fake_ll_tr(card, skb, hdr);
2609         else
2610                 qeth_rebuild_skb_fake_ll_eth(card, skb, hdr);
2611 }
2612
2613 static inline void
2614 qeth_layer2_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2615                         struct qeth_hdr *hdr)
2616 {
2617         skb->pkt_type = PACKET_HOST;
2618         skb->protocol = qeth_type_trans(skb, skb->dev);
2619         if (card->options.checksum_type == NO_CHECKSUMMING)
2620                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2621         else
2622                 skb->ip_summed = CHECKSUM_NONE;
2623         *((__u32 *)skb->cb) = ++card->seqno.pkt_seqno;
2624 }
2625
2626 static __u16
2627 qeth_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2628                  struct qeth_hdr *hdr)
2629 {
2630         unsigned short vlan_id = 0;
2631 #ifdef CONFIG_QETH_IPV6
2632         if (hdr->hdr.l3.flags & QETH_HDR_PASSTHRU) {
2633                 skb->pkt_type = PACKET_HOST;
2634                 skb->protocol = qeth_type_trans(skb, card->dev);
2635                 return 0;
2636         }
2637 #endif /* CONFIG_QETH_IPV6 */
2638         skb->protocol = htons((hdr->hdr.l3.flags & QETH_HDR_IPV6)? ETH_P_IPV6 :
2639                               ETH_P_IP);
2640         switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK){
2641         case QETH_CAST_UNICAST:
2642                 skb->pkt_type = PACKET_HOST;
2643                 break;
2644         case QETH_CAST_MULTICAST:
2645                 skb->pkt_type = PACKET_MULTICAST;
2646                 card->stats.multicast++;
2647                 break;
2648         case QETH_CAST_BROADCAST:
2649                 skb->pkt_type = PACKET_BROADCAST;
2650                 card->stats.multicast++;
2651                 break;
2652         case QETH_CAST_ANYCAST:
2653         case QETH_CAST_NOCAST:
2654         default:
2655                 skb->pkt_type = PACKET_HOST;
2656         }
2657
2658         if (hdr->hdr.l3.ext_flags &
2659             (QETH_HDR_EXT_VLAN_FRAME | QETH_HDR_EXT_INCLUDE_VLAN_TAG)) {
2660                 vlan_id = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME)?
2661                         hdr->hdr.l3.vlan_id : *((u16&nbs