Automatic merge of /spare/repo/netdev-2.6 branch ns83820
[linux-2.6.git] / drivers / s390 / net / qeth_main.c
1 /*
2  *
3  * linux/drivers/s390/net/qeth_main.c ($Revision: 1.206 $)
4  *
5  * Linux on zSeries OSA Express and HiperSockets support
6  *
7  * Copyright 2000,2003 IBM Corporation
8  *
9  *    Author(s): Original Code written by
10  *                        Utz Bacher (utz.bacher@de.ibm.com)
11  *               Rewritten by
12  *                        Frank Pavlic (pavlic@de.ibm.com) and
13  *                        Thomas Spatzier <tspat@de.ibm.com>
14  *
15  *    $Revision: 1.206 $         $Date: 2005/03/24 09:04:18 $
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2, or (at your option)
20  * any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30  */
31
32 /***
33  * eye catcher; just for debugging purposes
34  */
35 void volatile
36 qeth_eyecatcher(void)
37 {
38         return;
39 }
40
41 #include <linux/config.h>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/string.h>
45 #include <linux/errno.h>
46 #include <linux/mm.h>
47 #include <linux/ip.h>
48 #include <linux/inetdevice.h>
49 #include <linux/netdevice.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/kernel.h>
53 #include <linux/slab.h>
54 #include <linux/interrupt.h>
55 #include <linux/tcp.h>
56 #include <linux/icmp.h>
57 #include <linux/skbuff.h>
58 #include <linux/in.h>
59 #include <linux/igmp.h>
60 #include <linux/init.h>
61 #include <linux/reboot.h>
62 #include <linux/mii.h>
63 #include <linux/rcupdate.h>
64 #include <linux/ethtool.h>
65
66 #include <net/arp.h>
67 #include <net/ip.h>
68 #include <net/route.h>
69
70 #include <asm/ebcdic.h>
71 #include <asm/io.h>
72 #include <asm/qeth.h>
73 #include <asm/timex.h>
74 #include <asm/semaphore.h>
75 #include <asm/uaccess.h>
76
77 #include "qeth.h"
78 #include "qeth_mpc.h"
79 #include "qeth_fs.h"
80 #include "qeth_eddp.h"
81 #include "qeth_tso.h"
82
83 #define VERSION_QETH_C "$Revision: 1.206 $"
84 static const char *version = "qeth S/390 OSA-Express driver";
85
86 /**
87  * Debug Facility Stuff
88  */
89 static debug_info_t *qeth_dbf_setup = NULL;
90 static debug_info_t *qeth_dbf_data = NULL;
91 static debug_info_t *qeth_dbf_misc = NULL;
92 static debug_info_t *qeth_dbf_control = NULL;
93 debug_info_t *qeth_dbf_trace = NULL;
94 static debug_info_t *qeth_dbf_sense = NULL;
95 static debug_info_t *qeth_dbf_qerr = NULL;
96
97 DEFINE_PER_CPU(char[256], qeth_dbf_txt_buf);
98
99 /**
100  * some more definitions and declarations
101  */
102 static unsigned int known_devices[][10] = QETH_MODELLIST_ARRAY;
103
104 /* list of our cards */
105 struct qeth_card_list_struct qeth_card_list;
106 /*process list want to be notified*/
107 spinlock_t qeth_notify_lock;
108 struct list_head qeth_notify_list;
109
110 static void qeth_send_control_data_cb(struct qeth_channel *,
111                                       struct qeth_cmd_buffer *);
112
113 /**
114  * here we go with function implementation
115  */
116 static void
117 qeth_init_qdio_info(struct qeth_card *card);
118
119 static int
120 qeth_init_qdio_queues(struct qeth_card *card);
121
122 static int
123 qeth_alloc_qdio_buffers(struct qeth_card *card);
124
125 static void
126 qeth_free_qdio_buffers(struct qeth_card *);
127
128 static void
129 qeth_clear_qdio_buffers(struct qeth_card *);
130
131 static void
132 qeth_clear_ip_list(struct qeth_card *, int, int);
133
134 static void
135 qeth_clear_ipacmd_list(struct qeth_card *);
136
137 static int
138 qeth_qdio_clear_card(struct qeth_card *, int);
139
140 static void
141 qeth_clear_working_pool_list(struct qeth_card *);
142
143 static void
144 qeth_clear_cmd_buffers(struct qeth_channel *);
145
146 static int
147 qeth_stop(struct net_device *);
148
149 static void
150 qeth_clear_ipato_list(struct qeth_card *);
151
152 static int
153 qeth_is_addr_covered_by_ipato(struct qeth_card *, struct qeth_ipaddr *);
154
155 static void
156 qeth_irq_tasklet(unsigned long);
157
158 static int
159 qeth_set_online(struct ccwgroup_device *);
160
161 static struct qeth_ipaddr *
162 qeth_get_addr_buffer(enum qeth_prot_versions);
163
164 static void
165 qeth_set_multicast_list(struct net_device *);
166
167 static void
168 qeth_notify_processes(void)
169 {
170         /*notify all  registered processes */
171         struct qeth_notify_list_struct *n_entry;
172
173         QETH_DBF_TEXT(trace,3,"procnoti");
174         spin_lock(&qeth_notify_lock);
175         list_for_each_entry(n_entry, &qeth_notify_list, list) {
176                 send_sig(n_entry->signum, n_entry->task, 1);
177         }
178         spin_unlock(&qeth_notify_lock);
179
180 }
181 int
182 qeth_notifier_unregister(struct task_struct *p)
183 {
184         struct qeth_notify_list_struct *n_entry, *tmp;
185
186         QETH_DBF_TEXT(trace, 2, "notunreg");
187         spin_lock(&qeth_notify_lock);
188         list_for_each_entry_safe(n_entry, tmp, &qeth_notify_list, list) {
189                 if (n_entry->task == p) {
190                         list_del(&n_entry->list);
191                         kfree(n_entry);
192                         goto out;
193                 }
194         }
195 out:
196         spin_unlock(&qeth_notify_lock);
197         return 0;
198 }
199 int
200 qeth_notifier_register(struct task_struct *p, int signum)
201 {
202         struct qeth_notify_list_struct *n_entry;
203
204
205         /*check first if entry already exists*/
206         spin_lock(&qeth_notify_lock);
207         list_for_each_entry(n_entry, &qeth_notify_list, list) {
208                 if (n_entry->task == p) {
209                         n_entry->signum = signum;
210                         spin_unlock(&qeth_notify_lock);
211                         return 0;
212                 }
213         }
214         spin_unlock(&qeth_notify_lock);
215
216         n_entry = (struct qeth_notify_list_struct *)
217                 kmalloc(sizeof(struct qeth_notify_list_struct),GFP_KERNEL);
218         if (!n_entry)
219                 return -ENOMEM;
220         n_entry->task = p;
221         n_entry->signum = signum;
222         spin_lock(&qeth_notify_lock);
223         list_add(&n_entry->list,&qeth_notify_list);
224         spin_unlock(&qeth_notify_lock);
225         return 0;
226 }
227
228
229 /**
230  * free channel command buffers
231  */
232 static void
233 qeth_clean_channel(struct qeth_channel *channel)
234 {
235         int cnt;
236
237         QETH_DBF_TEXT(setup, 2, "freech");
238         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
239                 kfree(channel->iob[cnt].data);
240 }
241
242 /**
243  * free card
244  */
245 static void
246 qeth_free_card(struct qeth_card *card)
247 {
248
249         QETH_DBF_TEXT(setup, 2, "freecrd");
250         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
251         qeth_clean_channel(&card->read);
252         qeth_clean_channel(&card->write);
253         if (card->dev)
254                 free_netdev(card->dev);
255         qeth_clear_ip_list(card, 0, 0);
256         qeth_clear_ipato_list(card);
257         kfree(card->ip_tbd_list);
258         qeth_free_qdio_buffers(card);
259         kfree(card);
260 }
261
262 /**
263  * alloc memory for command buffer per channel
264  */
265 static int
266 qeth_setup_channel(struct qeth_channel *channel)
267 {
268         int cnt;
269
270         QETH_DBF_TEXT(setup, 2, "setupch");
271         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
272                 channel->iob[cnt].data = (char *)
273                         kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
274                 if (channel->iob[cnt].data == NULL)
275                         break;
276                 channel->iob[cnt].state = BUF_STATE_FREE;
277                 channel->iob[cnt].channel = channel;
278                 channel->iob[cnt].callback = qeth_send_control_data_cb;
279                 channel->iob[cnt].rc = 0;
280         }
281         if (cnt < QETH_CMD_BUFFER_NO) {
282                 while (cnt-- > 0)
283                         kfree(channel->iob[cnt].data);
284                 return -ENOMEM;
285         }
286         channel->buf_no = 0;
287         channel->io_buf_no = 0;
288         atomic_set(&channel->irq_pending, 0);
289         spin_lock_init(&channel->iob_lock);
290
291         init_waitqueue_head(&channel->wait_q);
292         channel->irq_tasklet.data = (unsigned long) channel;
293         channel->irq_tasklet.func = qeth_irq_tasklet;
294         return 0;
295 }
296
297 /**
298  * alloc memory for card structure
299  */
300 static struct qeth_card *
301 qeth_alloc_card(void)
302 {
303         struct qeth_card *card;
304
305         QETH_DBF_TEXT(setup, 2, "alloccrd");
306         card = (struct qeth_card *) kmalloc(sizeof(struct qeth_card),
307                                             GFP_DMA|GFP_KERNEL);
308         if (!card)
309                 return NULL;
310         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
311         memset(card, 0, sizeof(struct qeth_card));
312         if (qeth_setup_channel(&card->read)) {
313                 kfree(card);
314                 return NULL;
315         }
316         if (qeth_setup_channel(&card->write)) {
317                 qeth_clean_channel(&card->read);
318                 kfree(card);
319                 return NULL;
320         }
321         return card;
322 }
323
324 static long
325 __qeth_check_irb_error(struct ccw_device *cdev, struct irb *irb)
326 {
327         if (!IS_ERR(irb))
328                 return 0;
329
330         switch (PTR_ERR(irb)) {
331         case -EIO:
332                 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
333                 QETH_DBF_TEXT(trace, 2, "ckirberr");
334                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
335                 break;
336         case -ETIMEDOUT:
337                 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
338                 QETH_DBF_TEXT(trace, 2, "ckirberr");
339                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -ETIMEDOUT);
340                 break;
341         default:
342                 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
343                            cdev->dev.bus_id);
344                 QETH_DBF_TEXT(trace, 2, "ckirberr");
345                 QETH_DBF_TEXT(trace, 2, "  rc???");
346         }
347         return PTR_ERR(irb);
348 }
349
350 static int
351 qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
352 {
353         int dstat,cstat;
354         char *sense;
355
356         sense = (char *) irb->ecw;
357         cstat = irb->scsw.cstat;
358         dstat = irb->scsw.dstat;
359
360         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
361                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
362                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
363                 QETH_DBF_TEXT(trace,2, "CGENCHK");
364                 PRINT_WARN("check on device %s, dstat=x%x, cstat=x%x ",
365                            cdev->dev.bus_id, dstat, cstat);
366                 HEXDUMP16(WARN, "irb: ", irb);
367                 HEXDUMP16(WARN, "irb: ", ((char *) irb) + 32);
368                 return 1;
369         }
370
371         if (dstat & DEV_STAT_UNIT_CHECK) {
372                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
373                     SENSE_RESETTING_EVENT_FLAG) {
374                         QETH_DBF_TEXT(trace,2,"REVIND");
375                         return 1;
376                 }
377                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
378                     SENSE_COMMAND_REJECT_FLAG) {
379                         QETH_DBF_TEXT(trace,2,"CMDREJi");
380                         return 0;
381                 }
382                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
383                         QETH_DBF_TEXT(trace,2,"AFFE");
384                         return 1;
385                 }
386                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
387                         QETH_DBF_TEXT(trace,2,"ZEROSEN");
388                         return 0;
389                 }
390                 QETH_DBF_TEXT(trace,2,"DGENCHK");
391                         return 1;
392         }
393         return 0;
394 }
395 static int qeth_issue_next_read(struct qeth_card *);
396
397 /**
398  * interrupt handler
399  */
400 static void
401 qeth_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
402 {
403         int rc;
404         int cstat,dstat;
405         struct qeth_cmd_buffer *buffer;
406         struct qeth_channel *channel;
407         struct qeth_card *card;
408
409         QETH_DBF_TEXT(trace,5,"irq");
410
411         if (__qeth_check_irb_error(cdev, irb))
412                 return;
413         cstat = irb->scsw.cstat;
414         dstat = irb->scsw.dstat;
415
416         card = CARD_FROM_CDEV(cdev);
417         if (!card)
418                 return;
419
420         if (card->read.ccwdev == cdev){
421                 channel = &card->read;
422                 QETH_DBF_TEXT(trace,5,"read");
423         } else if (card->write.ccwdev == cdev) {
424                 channel = &card->write;
425                 QETH_DBF_TEXT(trace,5,"write");
426         } else {
427                 channel = &card->data;
428                 QETH_DBF_TEXT(trace,5,"data");
429         }
430         atomic_set(&channel->irq_pending, 0);
431
432         if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC))
433                 channel->state = CH_STATE_STOPPED;
434
435         if (irb->scsw.fctl & (SCSW_FCTL_HALT_FUNC))
436                 channel->state = CH_STATE_HALTED;
437
438         /*let's wake up immediately on data channel*/
439         if ((channel == &card->data) && (intparm != 0))
440                 goto out;
441
442         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
443                 QETH_DBF_TEXT(trace, 6, "clrchpar");
444                 /* we don't have to handle this further */
445                 intparm = 0;
446         }
447         if (intparm == QETH_HALT_CHANNEL_PARM) {
448                 QETH_DBF_TEXT(trace, 6, "hltchpar");
449                 /* we don't have to handle this further */
450                 intparm = 0;
451         }
452         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
453             (dstat & DEV_STAT_UNIT_CHECK) ||
454             (cstat)) {
455                 if (irb->esw.esw0.erw.cons) {
456                         /* TODO: we should make this s390dbf */
457                         PRINT_WARN("sense data available on channel %s.\n",
458                                    CHANNEL_ID(channel));
459                         PRINT_WARN(" cstat 0x%X\n dstat 0x%X\n", cstat, dstat);
460                         HEXDUMP16(WARN,"irb: ",irb);
461                         HEXDUMP16(WARN,"sense data: ",irb->ecw);
462                 }
463                 rc = qeth_get_problem(cdev,irb);
464                 if (rc) {
465                         qeth_schedule_recovery(card);
466                         goto out;
467                 }
468         }
469
470         if (intparm) {
471                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
472                 buffer->state = BUF_STATE_PROCESSED;
473         }
474         if (channel == &card->data)
475                 return;
476
477         if (channel == &card->read &&
478             channel->state == CH_STATE_UP)
479                 qeth_issue_next_read(card);
480
481         tasklet_schedule(&channel->irq_tasklet);
482         return;
483 out:
484         wake_up(&card->wait_q);
485 }
486
487 /**
488  * tasklet function scheduled from irq handler
489  */
490 static void
491 qeth_irq_tasklet(unsigned long data)
492 {
493         struct qeth_card *card;
494         struct qeth_channel *channel;
495         struct qeth_cmd_buffer *iob;
496         __u8 index;
497
498         QETH_DBF_TEXT(trace,5,"irqtlet");
499         channel = (struct qeth_channel *) data;
500         iob = channel->iob;
501         index = channel->buf_no;
502         card = CARD_FROM_CDEV(channel->ccwdev);
503         while (iob[index].state == BUF_STATE_PROCESSED) {
504                 if (iob[index].callback !=NULL) {
505                         iob[index].callback(channel,iob + index);
506                 }
507                 index = (index + 1) % QETH_CMD_BUFFER_NO;
508         }
509         channel->buf_no = index;
510         wake_up(&card->wait_q);
511 }
512
513 static int qeth_stop_card(struct qeth_card *);
514
515 static int
516 qeth_set_offline(struct ccwgroup_device *cgdev)
517 {
518         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
519         int rc = 0;
520         enum qeth_card_states recover_flag;
521
522         QETH_DBF_TEXT(setup, 3, "setoffl");
523         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
524
525         recover_flag = card->state;
526         if (qeth_stop_card(card) == -ERESTARTSYS){
527                 PRINT_WARN("Stopping card %s interrupted by user!\n",
528                            CARD_BUS_ID(card));
529                 return -ERESTARTSYS;
530         }
531         if ((rc = ccw_device_set_offline(CARD_DDEV(card))) ||
532             (rc = ccw_device_set_offline(CARD_WDEV(card))) ||
533             (rc = ccw_device_set_offline(CARD_RDEV(card)))) {
534                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
535         }
536         if (recover_flag == CARD_STATE_UP)
537                 card->state = CARD_STATE_RECOVER;
538         qeth_notify_processes();
539         return 0;
540 }
541
542 static int
543 qeth_wait_for_threads(struct qeth_card *card, unsigned long threads);
544
545
546 static void
547 qeth_remove_device(struct ccwgroup_device *cgdev)
548 {
549         struct qeth_card *card = (struct qeth_card *) cgdev->dev.driver_data;
550         unsigned long flags;
551
552         QETH_DBF_TEXT(setup, 3, "rmdev");
553         QETH_DBF_HEX(setup, 3, &card, sizeof(void *));
554
555         if (!card)
556                 return;
557
558         if (qeth_wait_for_threads(card, 0xffffffff))
559                 return;
560
561         if (cgdev->state == CCWGROUP_ONLINE){
562                 card->use_hard_stop = 1;
563                 qeth_set_offline(cgdev);
564         }
565         /* remove form our internal list */
566         write_lock_irqsave(&qeth_card_list.rwlock, flags);
567         list_del(&card->list);
568         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
569         if (card->dev)
570                 unregister_netdev(card->dev);
571         qeth_remove_device_attributes(&cgdev->dev);
572         qeth_free_card(card);
573         cgdev->dev.driver_data = NULL;
574         put_device(&cgdev->dev);
575 }
576
577 static int
578 qeth_register_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
579 static int
580 qeth_deregister_addr_entry(struct qeth_card *, struct qeth_ipaddr *);
581
582 /**
583  * Add/remove address to/from card's ip list, i.e. try to add or remove
584  * reference to/from an IP address that is already registered on the card.
585  * Returns:
586  *      0  address was on card and its reference count has been adjusted,
587  *         but is still > 0, so nothing has to be done
588  *         also returns 0 if card was not on card and the todo was to delete
589  *         the address -> there is also nothing to be done
590  *      1  address was not on card and the todo is to add it to the card's ip
591  *         list
592  *      -1 address was on card and its reference count has been decremented
593  *         to <= 0 by the todo -> address must be removed from card
594  */
595 static int
596 __qeth_ref_ip_on_card(struct qeth_card *card, struct qeth_ipaddr *todo,
597                       struct qeth_ipaddr **__addr)
598 {
599         struct qeth_ipaddr *addr;
600         int found = 0;
601
602         list_for_each_entry(addr, &card->ip_list, entry) {
603                 if ((addr->proto     == QETH_PROT_IPV4)  &&
604                     (todo->proto     == QETH_PROT_IPV4)  &&
605                     (addr->type      == todo->type)      &&
606                     (addr->u.a4.addr == todo->u.a4.addr) &&
607                     (addr->u.a4.mask == todo->u.a4.mask)   ){
608                         found = 1;
609                         break;
610                 }
611                 if ((addr->proto       == QETH_PROT_IPV6)     &&
612                     (todo->proto       == QETH_PROT_IPV6)     &&
613                     (addr->type        == todo->type)         &&
614                     (addr->u.a6.pfxlen == todo->u.a6.pfxlen)  &&
615                     (memcmp(&addr->u.a6.addr, &todo->u.a6.addr,
616                             sizeof(struct in6_addr)) == 0))     {
617                         found = 1;
618                         break;
619                 }
620         }
621         if (found){
622                 addr->users += todo->users;
623                 if (addr->users <= 0){
624                         *__addr = addr;
625                         return -1;
626                 } else {
627                         /* for VIPA and RXIP limit refcount to 1 */
628                         if (addr->type != QETH_IP_TYPE_NORMAL)
629                                 addr->users = 1;
630                         return 0;
631                 }
632         }
633         if (todo->users > 0){
634                 /* for VIPA and RXIP limit refcount to 1 */
635                 if (todo->type != QETH_IP_TYPE_NORMAL)
636                         todo->users = 1;
637                 return 1;
638         } else
639                 return 0;
640 }
641
642 static inline int
643 __qeth_address_exists_in_list(struct list_head *list, struct qeth_ipaddr *addr,
644                               int same_type)
645 {
646         struct qeth_ipaddr *tmp;
647
648         list_for_each_entry(tmp, list, entry) {
649                 if ((tmp->proto     == QETH_PROT_IPV4)            &&
650                     (addr->proto    == QETH_PROT_IPV4)            &&
651                     ((same_type && (tmp->type == addr->type)) ||
652                      (!same_type && (tmp->type != addr->type))  ) &&
653                     (tmp->u.a4.addr == addr->u.a4.addr)             ){
654                         return 1;
655                 }
656                 if ((tmp->proto  == QETH_PROT_IPV6)               &&
657                     (addr->proto == QETH_PROT_IPV6)               &&
658                     ((same_type && (tmp->type == addr->type)) ||
659                      (!same_type && (tmp->type != addr->type))  ) &&
660                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
661                             sizeof(struct in6_addr)) == 0)          ) {
662                         return 1;
663                 }
664         }
665         return 0;
666 }
667
668 /*
669  * Add IP to be added to todo list. If there is already an "add todo"
670  * in this list we just incremenent the reference count.
671  * Returns 0 if we  just incremented reference count.
672  */
673 static int
674 __qeth_insert_ip_todo(struct qeth_card *card, struct qeth_ipaddr *addr, int add)
675 {
676         struct qeth_ipaddr *tmp, *t;
677         int found = 0;
678
679         list_for_each_entry_safe(tmp, t, card->ip_tbd_list, entry) {
680                 if ((addr->type == QETH_IP_TYPE_DEL_ALL_MC) &&
681                     (tmp->type == QETH_IP_TYPE_DEL_ALL_MC))
682                         return 0;
683                 if ((tmp->proto        == QETH_PROT_IPV4)     &&
684                     (addr->proto       == QETH_PROT_IPV4)     &&
685                     (tmp->type         == addr->type)         &&
686                     (tmp->is_multicast == addr->is_multicast) &&
687                     (tmp->u.a4.addr    == addr->u.a4.addr)    &&
688                     (tmp->u.a4.mask    == addr->u.a4.mask)      ){
689                         found = 1;
690                         break;
691                 }
692                 if ((tmp->proto        == QETH_PROT_IPV6)      &&
693                     (addr->proto       == QETH_PROT_IPV6)      &&
694                     (tmp->type         == addr->type)          &&
695                     (tmp->is_multicast == addr->is_multicast)  &&
696                     (tmp->u.a6.pfxlen  == addr->u.a6.pfxlen)   &&
697                     (memcmp(&tmp->u.a6.addr, &addr->u.a6.addr,
698                             sizeof(struct in6_addr)) == 0)        ){
699                         found = 1;
700                         break;
701                 }
702         }
703         if (found){
704                 if (addr->users != 0)
705                         tmp->users += addr->users;
706                 else
707                         tmp->users += add? 1:-1;
708                 if (tmp->users == 0){
709                         list_del(&tmp->entry);
710                         kfree(tmp);
711                 }
712                 return 0;
713         } else {
714                 if (addr->type == QETH_IP_TYPE_DEL_ALL_MC)
715                         list_add(&addr->entry, card->ip_tbd_list);
716                 else {
717                         if (addr->users == 0)
718                                 addr->users += add? 1:-1;
719                         if (add && (addr->type == QETH_IP_TYPE_NORMAL) &&
720                             qeth_is_addr_covered_by_ipato(card, addr)){
721                                 QETH_DBF_TEXT(trace, 2, "tkovaddr");
722                                 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
723                         }
724                         list_add_tail(&addr->entry, card->ip_tbd_list);
725                 }
726                 return 1;
727         }
728 }
729
730 /**
731  * Remove IP address from list
732  */
733 static int
734 qeth_delete_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
735 {
736         unsigned long flags;
737         int rc = 0;
738
739         QETH_DBF_TEXT(trace,4,"delip");
740         if (addr->proto == QETH_PROT_IPV4)
741                 QETH_DBF_HEX(trace,4,&addr->u.a4.addr,4);
742         else {
743                 QETH_DBF_HEX(trace,4,&addr->u.a6.addr,8);
744                 QETH_DBF_HEX(trace,4,((char *)&addr->u.a6.addr)+8,8);
745         }
746         spin_lock_irqsave(&card->ip_lock, flags);
747         rc = __qeth_insert_ip_todo(card, addr, 0);
748         spin_unlock_irqrestore(&card->ip_lock, flags);
749         return rc;
750 }
751
752 static int
753 qeth_add_ip(struct qeth_card *card, struct qeth_ipaddr *addr)
754 {
755         unsigned long flags;
756         int rc = 0;
757
758         QETH_DBF_TEXT(trace,4,"addip");
759         if (addr->proto == QETH_PROT_IPV4)
760                 QETH_DBF_HEX(trace,4,&addr->u.a4.addr,4);
761         else {
762                 QETH_DBF_HEX(trace,4,&addr->u.a6.addr,8);
763                 QETH_DBF_HEX(trace,4,((char *)&addr->u.a6.addr)+8,8);
764         }
765         spin_lock_irqsave(&card->ip_lock, flags);
766         rc = __qeth_insert_ip_todo(card, addr, 1);
767         spin_unlock_irqrestore(&card->ip_lock, flags);
768         return rc;
769 }
770
771 static inline void
772 __qeth_delete_all_mc(struct qeth_card *card, unsigned long *flags)
773 {
774         struct qeth_ipaddr *addr, *tmp;
775         int rc;
776
777         list_for_each_entry_safe(addr, tmp, &card->ip_list, entry) {
778                 if (addr->is_multicast) {
779                         spin_unlock_irqrestore(&card->ip_lock, *flags);
780                         rc = qeth_deregister_addr_entry(card, addr);
781                         spin_lock_irqsave(&card->ip_lock, *flags);
782                         if (!rc) {
783                                 list_del(&addr->entry);
784                                 kfree(addr);
785                         }
786                 }
787         }
788 }
789
790 static void
791 qeth_set_ip_addr_list(struct qeth_card *card)
792 {
793         struct list_head *tbd_list;
794         struct qeth_ipaddr *todo, *addr;
795         unsigned long flags;
796         int rc;
797
798         QETH_DBF_TEXT(trace, 2, "sdiplist");
799         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
800
801         spin_lock_irqsave(&card->ip_lock, flags);
802         tbd_list = card->ip_tbd_list;
803         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_ATOMIC);
804         if (!card->ip_tbd_list) {
805                 QETH_DBF_TEXT(trace, 0, "silnomem");
806                 card->ip_tbd_list = tbd_list;
807                 spin_unlock_irqrestore(&card->ip_lock, flags);
808                 return;
809         } else
810                 INIT_LIST_HEAD(card->ip_tbd_list);
811
812         while (!list_empty(tbd_list)){
813                 todo = list_entry(tbd_list->next, struct qeth_ipaddr, entry);
814                 list_del(&todo->entry);
815                 if (todo->type == QETH_IP_TYPE_DEL_ALL_MC){
816                         __qeth_delete_all_mc(card, &flags);
817                         kfree(todo);
818                         continue;
819                 }
820                 rc = __qeth_ref_ip_on_card(card, todo, &addr);
821                 if (rc == 0) {
822                         /* nothing to be done; only adjusted refcount */
823                         kfree(todo);
824                 } else if (rc == 1) {
825                         /* new entry to be added to on-card list */
826                         spin_unlock_irqrestore(&card->ip_lock, flags);
827                         rc = qeth_register_addr_entry(card, todo);
828                         spin_lock_irqsave(&card->ip_lock, flags);
829                         if (!rc)
830                                 list_add_tail(&todo->entry, &card->ip_list);
831                         else
832                                 kfree(todo);
833                 } else if (rc == -1) {
834                         /* on-card entry to be removed */
835                         list_del_init(&addr->entry);
836                         spin_unlock_irqrestore(&card->ip_lock, flags);
837                         rc = qeth_deregister_addr_entry(card, addr);
838                         spin_lock_irqsave(&card->ip_lock, flags);
839                         if (!rc)
840                                 kfree(addr);
841                         else
842                                 list_add_tail(&addr->entry, &card->ip_list);
843                         kfree(todo);
844                 }
845         }
846         spin_unlock_irqrestore(&card->ip_lock, flags);
847         kfree(tbd_list);
848 }
849
850 static void qeth_delete_mc_addresses(struct qeth_card *);
851 static void qeth_add_multicast_ipv4(struct qeth_card *);
852 #ifdef CONFIG_QETH_IPV6
853 static void qeth_add_multicast_ipv6(struct qeth_card *);
854 #endif
855
856 static inline int
857 qeth_set_thread_start_bit(struct qeth_card *card, unsigned long thread)
858 {
859         unsigned long flags;
860
861         spin_lock_irqsave(&card->thread_mask_lock, flags);
862         if ( !(card->thread_allowed_mask & thread) ||
863               (card->thread_start_mask & thread) ) {
864                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
865                 return -EPERM;
866         }
867         card->thread_start_mask |= thread;
868         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
869         return 0;
870 }
871
872 static void
873 qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
874 {
875         unsigned long flags;
876
877         spin_lock_irqsave(&card->thread_mask_lock, flags);
878         card->thread_start_mask &= ~thread;
879         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
880         wake_up(&card->wait_q);
881 }
882
883 static void
884 qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
885 {
886         unsigned long flags;
887
888         spin_lock_irqsave(&card->thread_mask_lock, flags);
889         card->thread_running_mask &= ~thread;
890         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
891         wake_up(&card->wait_q);
892 }
893
894 static inline int
895 __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
896 {
897         unsigned long flags;
898         int rc = 0;
899
900         spin_lock_irqsave(&card->thread_mask_lock, flags);
901         if (card->thread_start_mask & thread){
902                 if ((card->thread_allowed_mask & thread) &&
903                     !(card->thread_running_mask & thread)){
904                         rc = 1;
905                         card->thread_start_mask &= ~thread;
906                         card->thread_running_mask |= thread;
907                 } else
908                         rc = -EPERM;
909         }
910         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
911         return rc;
912 }
913
914 static int
915 qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
916 {
917         int rc = 0;
918
919         wait_event(card->wait_q,
920                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
921         return rc;
922 }
923
924 static int
925 qeth_register_ip_addresses(void *ptr)
926 {
927         struct qeth_card *card;
928
929         card = (struct qeth_card *) ptr;
930         daemonize("qeth_reg_ip");
931         QETH_DBF_TEXT(trace,4,"regipth1");
932         if (!qeth_do_run_thread(card, QETH_SET_IP_THREAD))
933                 return 0;
934         QETH_DBF_TEXT(trace,4,"regipth2");
935         qeth_set_ip_addr_list(card);
936         qeth_clear_thread_running_bit(card, QETH_SET_IP_THREAD);
937         return 0;
938 }
939
940 static int
941 qeth_recover(void *ptr)
942 {
943         struct qeth_card *card;
944         int rc = 0;
945
946         card = (struct qeth_card *) ptr;
947         daemonize("qeth_recover");
948         QETH_DBF_TEXT(trace,2,"recover1");
949         QETH_DBF_HEX(trace, 2, &card, sizeof(void *));
950         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
951                 return 0;
952         QETH_DBF_TEXT(trace,2,"recover2");
953         PRINT_WARN("Recovery of device %s started ...\n",
954                    CARD_BUS_ID(card));
955         card->use_hard_stop = 1;
956         qeth_set_offline(card->gdev);
957         rc = qeth_set_online(card->gdev);
958         if (!rc)
959                 PRINT_INFO("Device %s successfully recovered!\n",
960                            CARD_BUS_ID(card));
961         else
962                 PRINT_INFO("Device %s could not be recovered!\n",
963                            CARD_BUS_ID(card));
964         /* don't run another scheduled recovery */
965         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
966         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
967         return 0;
968 }
969
970 void
971 qeth_schedule_recovery(struct qeth_card *card)
972 {
973         QETH_DBF_TEXT(trace,2,"startrec");
974
975         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
976                 schedule_work(&card->kernel_thread_starter);
977 }
978
979 static int
980 qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
981 {
982         unsigned long flags;
983         int rc = 0;
984
985         spin_lock_irqsave(&card->thread_mask_lock, flags);
986         QETH_DBF_TEXT_(trace, 4, "  %02x%02x%02x",
987                         (u8) card->thread_start_mask,
988                         (u8) card->thread_allowed_mask,
989                         (u8) card->thread_running_mask);
990         rc = (card->thread_start_mask & thread);
991         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
992         return rc;
993 }
994
995 static void
996 qeth_start_kernel_thread(struct qeth_card *card)
997 {
998         QETH_DBF_TEXT(trace , 2, "strthrd");
999
1000         if (card->read.state != CH_STATE_UP &&
1001             card->write.state != CH_STATE_UP)
1002                 return;
1003
1004         if (qeth_do_start_thread(card, QETH_SET_IP_THREAD))
1005                 kernel_thread(qeth_register_ip_addresses, (void *)card,SIGCHLD);
1006         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1007                 kernel_thread(qeth_recover, (void *) card, SIGCHLD);
1008 }
1009
1010
1011 static void
1012 qeth_set_intial_options(struct qeth_card *card)
1013 {
1014         card->options.route4.type = NO_ROUTER;
1015 #ifdef CONFIG_QETH_IPV6
1016         card->options.route6.type = NO_ROUTER;
1017 #endif /* QETH_IPV6 */
1018         card->options.checksum_type = QETH_CHECKSUM_DEFAULT;
1019         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1020         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1021         card->options.fake_broadcast = 0;
1022         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1023         card->options.fake_ll = 0;
1024         card->options.layer2 = 0;
1025 }
1026
1027 /**
1028  * initialize channels ,card and all state machines
1029  */
1030 static int
1031 qeth_setup_card(struct qeth_card *card)
1032 {
1033
1034         QETH_DBF_TEXT(setup, 2, "setupcrd");
1035         QETH_DBF_HEX(setup, 2, &card, sizeof(void *));
1036
1037         card->read.state  = CH_STATE_DOWN;
1038         card->write.state = CH_STATE_DOWN;
1039         card->data.state  = CH_STATE_DOWN;
1040         card->state = CARD_STATE_DOWN;
1041         card->lan_online = 0;
1042         card->use_hard_stop = 0;
1043         card->dev = NULL;
1044 #ifdef CONFIG_QETH_VLAN
1045         spin_lock_init(&card->vlanlock);
1046         card->vlangrp = NULL;
1047 #endif
1048         spin_lock_init(&card->ip_lock);
1049         spin_lock_init(&card->thread_mask_lock);
1050         card->thread_start_mask = 0;
1051         card->thread_allowed_mask = 0;
1052         card->thread_running_mask = 0;
1053         INIT_WORK(&card->kernel_thread_starter,
1054                   (void *)qeth_start_kernel_thread,card);
1055         INIT_LIST_HEAD(&card->ip_list);
1056         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1057         if (!card->ip_tbd_list) {
1058                 QETH_DBF_TEXT(setup, 0, "iptbdnom");
1059                 return -ENOMEM;
1060         }
1061         INIT_LIST_HEAD(card->ip_tbd_list);
1062         INIT_LIST_HEAD(&card->cmd_waiter_list);
1063         init_waitqueue_head(&card->wait_q);
1064         /* intial options */
1065         qeth_set_intial_options(card);
1066         /* IP address takeover */
1067         INIT_LIST_HEAD(&card->ipato.entries);
1068         card->ipato.enabled = 0;
1069         card->ipato.invert4 = 0;
1070         card->ipato.invert6 = 0;
1071         /* init QDIO stuff */
1072         qeth_init_qdio_info(card);
1073         return 0;
1074 }
1075
1076 static int
1077 is_1920_device (struct qeth_card *card)
1078 {
1079         int single_queue = 0;
1080         struct ccw_device *ccwdev;
1081         struct channelPath_dsc {
1082                 u8 flags;
1083                 u8 lsn;
1084                 u8 desc;
1085                 u8 chpid;
1086                 u8 swla;
1087                 u8 zeroes;
1088                 u8 chla;
1089                 u8 chpp;
1090         } *chp_dsc;
1091
1092         QETH_DBF_TEXT(setup, 2, "chk_1920");
1093
1094         ccwdev = card->data.ccwdev;
1095         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1096         if (chp_dsc != NULL) {
1097                 /* CHPP field bit 6 == 1 -> single queue */
1098                 single_queue = ((chp_dsc->chpp & 0x02) == 0x02);
1099                 kfree(chp_dsc);
1100         }
1101         QETH_DBF_TEXT_(setup, 2, "rc:%x", single_queue);
1102         return single_queue;
1103 }
1104
1105 static int
1106 qeth_determine_card_type(struct qeth_card *card)
1107 {
1108         int i = 0;
1109
1110         QETH_DBF_TEXT(setup, 2, "detcdtyp");
1111
1112         while (known_devices[i][4]) {
1113                 if ((CARD_RDEV(card)->id.dev_type == known_devices[i][2]) &&
1114                     (CARD_RDEV(card)->id.dev_model == known_devices[i][3])) {
1115                         card->info.type = known_devices[i][4];
1116                         if (is_1920_device(card)) {
1117                                 PRINT_INFO("Priority Queueing not able "
1118                                            "due to hardware limitations!\n");
1119                                 card->qdio.no_out_queues = 1;
1120                                 card->qdio.default_out_queue = 0;
1121                         } else {
1122                                 card->qdio.no_out_queues = known_devices[i][8];
1123                         }
1124                         card->info.is_multicast_different = known_devices[i][9];
1125                         return 0;
1126                 }
1127                 i++;
1128         }
1129         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1130         PRINT_ERR("unknown card type on device %s\n", CARD_BUS_ID(card));
1131         return -ENOENT;
1132 }
1133
1134 static int
1135 qeth_probe_device(struct ccwgroup_device *gdev)
1136 {
1137         struct qeth_card *card;
1138         struct device *dev;
1139         unsigned long flags;
1140         int rc;
1141
1142         QETH_DBF_TEXT(setup, 2, "probedev");
1143
1144         dev = &gdev->dev;
1145         if (!get_device(dev))
1146                 return -ENODEV;
1147
1148         card = qeth_alloc_card();
1149         if (!card) {
1150                 put_device(dev);
1151                 QETH_DBF_TEXT_(setup, 2, "1err%d", -ENOMEM);
1152                 return -ENOMEM;
1153         }
1154         card->read.ccwdev  = gdev->cdev[0];
1155         card->write.ccwdev = gdev->cdev[1];
1156         card->data.ccwdev  = gdev->cdev[2];
1157
1158         if ((rc = qeth_setup_card(card))){
1159                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1160                 put_device(dev);
1161                 qeth_free_card(card);
1162                 return rc;
1163         }
1164         gdev->dev.driver_data = card;
1165         card->gdev = gdev;
1166         gdev->cdev[0]->handler = qeth_irq;
1167         gdev->cdev[1]->handler = qeth_irq;
1168         gdev->cdev[2]->handler = qeth_irq;
1169
1170         rc = qeth_create_device_attributes(dev);
1171         if (rc) {
1172                 put_device(dev);
1173                 qeth_free_card(card);
1174                 return rc;
1175         }
1176         if ((rc = qeth_determine_card_type(card))){
1177                 PRINT_WARN("%s: not a valid card type\n", __func__);
1178                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1179                 put_device(dev);
1180                 qeth_free_card(card);
1181                 return rc;
1182         }
1183         /* insert into our internal list */
1184         write_lock_irqsave(&qeth_card_list.rwlock, flags);
1185         list_add_tail(&card->list, &qeth_card_list.list);
1186         write_unlock_irqrestore(&qeth_card_list.rwlock, flags);
1187         return rc;
1188 }
1189
1190
1191 static int
1192 qeth_get_unitaddr(struct qeth_card *card)
1193 {
1194         int length;
1195         char *prcd;
1196         int rc;
1197
1198         QETH_DBF_TEXT(setup, 2, "getunit");
1199         rc = read_conf_data(CARD_DDEV(card), (void **) &prcd, &length);
1200         if (rc) {
1201                 PRINT_ERR("read_conf_data for device %s returned %i\n",
1202                           CARD_DDEV_ID(card), rc);
1203                 return rc;
1204         }
1205         card->info.chpid = prcd[30];
1206         card->info.unit_addr2 = prcd[31];
1207         card->info.cula = prcd[63];
1208         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1209                                (prcd[0x11] == _ascebc['M']));
1210         return 0;
1211 }
1212
1213 static void
1214 qeth_init_tokens(struct qeth_card *card)
1215 {
1216         card->token.issuer_rm_w = 0x00010103UL;
1217         card->token.cm_filter_w = 0x00010108UL;
1218         card->token.cm_connection_w = 0x0001010aUL;
1219         card->token.ulp_filter_w = 0x0001010bUL;
1220         card->token.ulp_connection_w = 0x0001010dUL;
1221 }
1222
1223 static inline __u16
1224 raw_devno_from_bus_id(char *id)
1225 {
1226         id += (strlen(id) - 4);
1227         return (__u16) simple_strtoul(id, &id, 16);
1228 }
1229 /**
1230  * setup channel
1231  */
1232 static void
1233 qeth_setup_ccw(struct qeth_channel *channel,unsigned char *iob, __u32 len)
1234 {
1235         struct qeth_card *card;
1236
1237         QETH_DBF_TEXT(trace, 4, "setupccw");
1238         card = CARD_FROM_CDEV(channel->ccwdev);
1239         if (channel == &card->read)
1240                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1241         else
1242                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1243         channel->ccw.count = len;
1244         channel->ccw.cda = (__u32) __pa(iob);
1245 }
1246
1247 /**
1248  * get free buffer for ccws (IDX activation, lancmds,ipassists...)
1249  */
1250 static struct qeth_cmd_buffer *
1251 __qeth_get_buffer(struct qeth_channel *channel)
1252 {
1253         __u8 index;
1254
1255         QETH_DBF_TEXT(trace, 6, "getbuff");
1256         index = channel->io_buf_no;
1257         do {
1258                 if (channel->iob[index].state == BUF_STATE_FREE) {
1259                         channel->iob[index].state = BUF_STATE_LOCKED;
1260                         channel->io_buf_no = (channel->io_buf_no + 1) %
1261                                 QETH_CMD_BUFFER_NO;
1262                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
1263                         return channel->iob + index;
1264                 }
1265                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1266         } while(index != channel->io_buf_no);
1267
1268         return NULL;
1269 }
1270
1271 /**
1272  * release command buffer
1273  */
1274 static void
1275 qeth_release_buffer(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1276 {
1277         unsigned long flags;
1278
1279         QETH_DBF_TEXT(trace, 6, "relbuff");
1280         spin_lock_irqsave(&channel->iob_lock, flags);
1281         memset(iob->data, 0, QETH_BUFSIZE);
1282         iob->state = BUF_STATE_FREE;
1283         iob->callback = qeth_send_control_data_cb;
1284         iob->rc = 0;
1285         spin_unlock_irqrestore(&channel->iob_lock, flags);
1286 }
1287
1288 static struct qeth_cmd_buffer *
1289 qeth_get_buffer(struct qeth_channel *channel)
1290 {
1291         struct qeth_cmd_buffer *buffer = NULL;
1292         unsigned long flags;
1293
1294         spin_lock_irqsave(&channel->iob_lock, flags);
1295         buffer = __qeth_get_buffer(channel);
1296         spin_unlock_irqrestore(&channel->iob_lock, flags);
1297         return buffer;
1298 }
1299
1300 static struct qeth_cmd_buffer *
1301 qeth_wait_for_buffer(struct qeth_channel *channel)
1302 {
1303         struct qeth_cmd_buffer *buffer;
1304         wait_event(channel->wait_q,
1305                    ((buffer = qeth_get_buffer(channel)) != NULL));
1306         return buffer;
1307 }
1308
1309 static void
1310 qeth_clear_cmd_buffers(struct qeth_channel *channel)
1311 {
1312         int cnt = 0;
1313
1314         for (cnt=0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1315                 qeth_release_buffer(channel,&channel->iob[cnt]);
1316         channel->buf_no = 0;
1317         channel->io_buf_no = 0;
1318 }
1319
1320 /**
1321  * start IDX for read and write channel
1322  */
1323 static int
1324 qeth_idx_activate_get_answer(struct qeth_channel *channel,
1325                               void (*idx_reply_cb)(struct qeth_channel *,
1326                                                    struct qeth_cmd_buffer *))
1327 {
1328         struct qeth_cmd_buffer *iob;
1329         unsigned long flags;
1330         int rc;
1331         struct qeth_card *card;
1332
1333         QETH_DBF_TEXT(setup, 2, "idxanswr");
1334         card = CARD_FROM_CDEV(channel->ccwdev);
1335         iob = qeth_get_buffer(channel);
1336         iob->callback = idx_reply_cb;
1337         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1338         channel->ccw.count = QETH_BUFSIZE;
1339         channel->ccw.cda = (__u32) __pa(iob->data);
1340
1341         wait_event(card->wait_q,
1342                    atomic_compare_and_swap(0,1,&channel->irq_pending) == 0);
1343         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1344         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1345         rc = ccw_device_start(channel->ccwdev,
1346                               &channel->ccw,(addr_t) iob, 0, 0);
1347         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1348
1349         if (rc) {
1350                 PRINT_ERR("qeth: Error2 in activating channel rc=%d\n",rc);
1351                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
1352                 atomic_set(&channel->irq_pending, 0);
1353                 wake_up(&card->wait_q);
1354                 return rc;
1355         }
1356         rc = wait_event_interruptible_timeout(card->wait_q,
1357                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1358         if (rc == -ERESTARTSYS)
1359                 return rc;
1360         if (channel->state != CH_STATE_UP){
1361                 rc = -ETIME;
1362                 QETH_DBF_TEXT_(setup, 2, "3err%d", rc);
1363                 qeth_clear_cmd_buffers(channel);
1364         } else
1365                 rc = 0;
1366         return rc;
1367 }
1368
1369 static int
1370 qeth_idx_activate_channel(struct qeth_channel *channel,
1371                            void (*idx_reply_cb)(struct qeth_channel *,
1372                                                 struct qeth_cmd_buffer *))
1373 {
1374         struct qeth_card *card;
1375         struct qeth_cmd_buffer *iob;
1376         unsigned long flags;
1377         __u16 temp;
1378         int rc;
1379
1380         card = CARD_FROM_CDEV(channel->ccwdev);
1381
1382         QETH_DBF_TEXT(setup, 2, "idxactch");
1383
1384         iob = qeth_get_buffer(channel);
1385         iob->callback = idx_reply_cb;
1386         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1387         channel->ccw.count = IDX_ACTIVATE_SIZE;
1388         channel->ccw.cda = (__u32) __pa(iob->data);
1389         if (channel == &card->write) {
1390                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1391                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1392                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1393                 card->seqno.trans_hdr++;
1394         } else {
1395                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1396                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1397                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1398         }
1399         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1400                &card->token.issuer_rm_w,QETH_MPC_TOKEN_LENGTH);
1401         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1402                &card->info.func_level,sizeof(__u16));
1403         temp = raw_devno_from_bus_id(CARD_DDEV_ID(card));
1404         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp, 2);
1405         temp = (card->info.cula << 8) + card->info.unit_addr2;
1406         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1407
1408         wait_event(card->wait_q,
1409                    atomic_compare_and_swap(0,1,&channel->irq_pending) == 0);
1410         QETH_DBF_TEXT(setup, 6, "noirqpnd");
1411         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1412         rc = ccw_device_start(channel->ccwdev,
1413                               &channel->ccw,(addr_t) iob, 0, 0);
1414         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1415
1416         if (rc) {
1417                 PRINT_ERR("qeth: Error1 in activating channel. rc=%d\n",rc);
1418                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
1419                 atomic_set(&channel->irq_pending, 0);
1420                 wake_up(&card->wait_q);
1421                 return rc;
1422         }
1423         rc = wait_event_interruptible_timeout(card->wait_q,
1424                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1425         if (rc == -ERESTARTSYS)
1426                 return rc;
1427         if (channel->state != CH_STATE_ACTIVATING) {
1428                 PRINT_WARN("qeth: IDX activate timed out!\n");
1429                 QETH_DBF_TEXT_(setup, 2, "2err%d", -ETIME);
1430                 qeth_clear_cmd_buffers(channel);
1431                 return -ETIME;
1432         }
1433         return qeth_idx_activate_get_answer(channel,idx_reply_cb);
1434 }
1435
1436 static int
1437 qeth_peer_func_level(int level)
1438 {
1439         if ((level & 0xff) == 8)
1440                 return (level & 0xff) + 0x400;
1441         if (((level >> 8) & 3) == 1)
1442                 return (level & 0xff) + 0x200;
1443         return level;
1444 }
1445
1446 static void
1447 qeth_idx_write_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1448 {
1449         struct qeth_card *card;
1450         __u16 temp;
1451
1452         QETH_DBF_TEXT(setup ,2, "idxwrcb");
1453
1454         if (channel->state == CH_STATE_DOWN) {
1455                 channel->state = CH_STATE_ACTIVATING;
1456                 goto out;
1457         }
1458         card = CARD_FROM_CDEV(channel->ccwdev);
1459
1460         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1461                 PRINT_ERR("IDX_ACTIVATE on write channel device %s: negative "
1462                           "reply\n", CARD_WDEV_ID(card));
1463                 goto out;
1464         }
1465         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1466         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1467                 PRINT_WARN("IDX_ACTIVATE on write channel device %s: "
1468                            "function level mismatch "
1469                            "(sent: 0x%x, received: 0x%x)\n",
1470                            CARD_WDEV_ID(card), card->info.func_level, temp);
1471                 goto out;
1472         }
1473         channel->state = CH_STATE_UP;
1474 out:
1475         qeth_release_buffer(channel, iob);
1476 }
1477
1478 static int
1479 qeth_check_idx_response(unsigned char *buffer)
1480 {
1481         if (!buffer)
1482                 return 0;
1483
1484         QETH_DBF_HEX(control, 2, buffer, QETH_DBF_CONTROL_LEN);
1485         if ((buffer[2] & 0xc0) == 0xc0) {
1486                 PRINT_WARN("received an IDX TERMINATE "
1487                            "with cause code 0x%02x%s\n",
1488                            buffer[4],
1489                            ((buffer[4] == 0x22) ?
1490                             " -- try another portname" : ""));
1491                 QETH_DBF_TEXT(trace, 2, "ckidxres");
1492                 QETH_DBF_TEXT(trace, 2, " idxterm");
1493                 QETH_DBF_TEXT_(trace, 2, "  rc%d", -EIO);
1494                 return -EIO;
1495         }
1496         return 0;
1497 }
1498
1499 static void
1500 qeth_idx_read_cb(struct qeth_channel *channel, struct qeth_cmd_buffer *iob)
1501 {
1502         struct qeth_card *card;
1503         __u16 temp;
1504
1505         QETH_DBF_TEXT(setup , 2, "idxrdcb");
1506         if (channel->state == CH_STATE_DOWN) {
1507                 channel->state = CH_STATE_ACTIVATING;
1508                 goto out;
1509         }
1510
1511         card = CARD_FROM_CDEV(channel->ccwdev);
1512         if (qeth_check_idx_response(iob->data)) {
1513                         goto out;
1514         }
1515         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1516                 PRINT_ERR("IDX_ACTIVATE on read channel device %s: negative "
1517                           "reply\n", CARD_RDEV_ID(card));
1518                 goto out;
1519         }
1520
1521 /**
1522  * temporary fix for microcode bug
1523  * to revert it,replace OR by AND
1524  */
1525         if ( (!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1526              (card->info.type == QETH_CARD_TYPE_OSAE) )
1527                 card->info.portname_required = 1;
1528
1529         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1530         if (temp != qeth_peer_func_level(card->info.func_level)) {
1531                 PRINT_WARN("IDX_ACTIVATE on read channel device %s: function "
1532                            "level mismatch (sent: 0x%x, received: 0x%x)\n",
1533                            CARD_RDEV_ID(card), card->info.func_level, temp);
1534                 goto out;
1535         }
1536         memcpy(&card->token.issuer_rm_r,
1537                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1538                QETH_MPC_TOKEN_LENGTH);
1539         memcpy(&card->info.mcl_level[0],
1540                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1541         channel->state = CH_STATE_UP;
1542 out:
1543         qeth_release_buffer(channel,iob);
1544 }
1545
1546 static int
1547 qeth_issue_next_read(struct qeth_card *card)
1548 {
1549         int rc;
1550         struct qeth_cmd_buffer *iob;
1551
1552         QETH_DBF_TEXT(trace,5,"issnxrd");
1553         if (card->read.state != CH_STATE_UP)
1554                 return -EIO;
1555         iob = qeth_get_buffer(&card->read);
1556         if (!iob) {
1557                 PRINT_WARN("issue_next_read failed: no iob available!\n");
1558                 return -ENOMEM;
1559         }
1560         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
1561         wait_event(card->wait_q,
1562                    atomic_compare_and_swap(0,1,&card->read.irq_pending) == 0);
1563         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1564         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
1565                               (addr_t) iob, 0, 0);
1566         if (rc) {
1567                 PRINT_ERR("Error in starting next read ccw! rc=%i\n", rc);
1568                 atomic_set(&card->read.irq_pending, 0);
1569                 qeth_schedule_recovery(card);
1570                 wake_up(&card->wait_q);
1571         }
1572         return rc;
1573 }
1574
1575 static struct qeth_reply *
1576 qeth_alloc_reply(struct qeth_card *card)
1577 {
1578         struct qeth_reply *reply;
1579
1580         reply = kmalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
1581         if (reply){
1582                 memset(reply, 0, sizeof(struct qeth_reply));
1583                 atomic_set(&reply->refcnt, 1);
1584                 reply->card = card;
1585         };
1586         return reply;
1587 }
1588
1589 static void
1590 qeth_get_reply(struct qeth_reply *reply)
1591 {
1592         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1593         atomic_inc(&reply->refcnt);
1594 }
1595
1596 static void
1597 qeth_put_reply(struct qeth_reply *reply)
1598 {
1599         WARN_ON(atomic_read(&reply->refcnt) <= 0);
1600         if (atomic_dec_and_test(&reply->refcnt))
1601                 kfree(reply);
1602 }
1603
1604 static void
1605 qeth_cmd_timeout(unsigned long data)
1606 {
1607         struct qeth_reply *reply, *list_reply, *r;
1608         unsigned long flags;
1609
1610         reply = (struct qeth_reply *) data;
1611         spin_lock_irqsave(&reply->card->lock, flags);
1612         list_for_each_entry_safe(list_reply, r,
1613                                  &reply->card->cmd_waiter_list, list) {
1614                 if (reply == list_reply){
1615                         qeth_get_reply(reply);
1616                         list_del_init(&reply->list);
1617                         spin_unlock_irqrestore(&reply->card->lock, flags);
1618                         reply->rc = -ETIME;
1619                         reply->received = 1;
1620                         wake_up(&reply->wait_q);
1621                         qeth_put_reply(reply);
1622                         return;
1623                 }
1624         }
1625         spin_unlock_irqrestore(&reply->card->lock, flags);
1626 }
1627
1628 static void
1629 qeth_reset_ip_addresses(struct qeth_card *card)
1630 {
1631         QETH_DBF_TEXT(trace, 2, "rstipadd");
1632
1633         qeth_clear_ip_list(card, 0, 1);
1634         /* this function will also schedule the SET_IP_THREAD */
1635         qeth_set_multicast_list(card->dev);
1636 }
1637
1638 static struct qeth_ipa_cmd *
1639 qeth_check_ipa_data(struct qeth_card *card, struct qeth_cmd_buffer *iob)
1640 {
1641         struct qeth_ipa_cmd *cmd = NULL;
1642
1643         QETH_DBF_TEXT(trace,5,"chkipad");
1644         if (IS_IPA(iob->data)){
1645                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
1646                 if (IS_IPA_REPLY(cmd))
1647                         return cmd;
1648                 else {
1649                         switch (cmd->hdr.command) {
1650                         case IPA_CMD_STOPLAN:
1651                                 PRINT_WARN("Link failure on %s (CHPID 0x%X) - "
1652                                            "there is a network problem or "
1653                                            "someone pulled the cable or "
1654                                            "disabled the port.\n",
1655                                            QETH_CARD_IFNAME(card),
1656                                            card->info.chpid);
1657                                 card->lan_online = 0;
1658                                 netif_carrier_off(card->dev);
1659                                 return NULL;
1660                         case IPA_CMD_STARTLAN:
1661                                 PRINT_INFO("Link reestablished on %s "
1662                                            "(CHPID 0x%X). Scheduling "
1663                                            "IP address reset.\n",
1664                                            QETH_CARD_IFNAME(card),
1665                                            card->info.chpid);
1666                                 card->lan_online = 1;
1667                                 netif_carrier_on(card->dev);
1668                                 qeth_reset_ip_addresses(card);
1669                                 return NULL;
1670                         case IPA_CMD_REGISTER_LOCAL_ADDR:
1671                                 QETH_DBF_TEXT(trace,3, "irla");
1672                                 break;
1673                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
1674                                 QETH_DBF_TEXT(trace,3, "urla");
1675                                 break;
1676                         default:
1677                                 PRINT_WARN("Received data is IPA "
1678                                            "but not a reply!\n");
1679                                 break;
1680                         }
1681                 }
1682         }
1683         return cmd;
1684 }
1685
1686 /**
1687  * wake all waiting ipa commands
1688  */
1689 static void
1690 qeth_clear_ipacmd_list(struct qeth_card *card)
1691 {
1692         struct qeth_reply *reply, *r;
1693         unsigned long flags;
1694
1695         QETH_DBF_TEXT(trace, 4, "clipalst");
1696
1697         spin_lock_irqsave(&card->lock, flags);
1698         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1699                 qeth_get_reply(reply);
1700                 reply->rc = -EIO;
1701                 reply->received = 1;
1702                 list_del_init(&reply->list);
1703                 wake_up(&reply->wait_q);
1704                 qeth_put_reply(reply);
1705         }
1706         spin_unlock_irqrestore(&card->lock, flags);
1707 }
1708
1709 static void
1710 qeth_send_control_data_cb(struct qeth_channel *channel,
1711                           struct qeth_cmd_buffer *iob)
1712 {
1713         struct qeth_card *card;
1714         struct qeth_reply *reply, *r;
1715         struct qeth_ipa_cmd *cmd;
1716         unsigned long flags;
1717         int keep_reply;
1718
1719         QETH_DBF_TEXT(trace,4,"sndctlcb");
1720
1721         card = CARD_FROM_CDEV(channel->ccwdev);
1722         if (qeth_check_idx_response(iob->data)) {
1723                 qeth_clear_ipacmd_list(card);
1724                 qeth_schedule_recovery(card);
1725                 goto out;
1726         }
1727
1728         cmd = qeth_check_ipa_data(card, iob);
1729         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
1730                 goto out;
1731
1732         spin_lock_irqsave(&card->lock, flags);
1733         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
1734                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
1735                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
1736                         qeth_get_reply(reply);
1737                         list_del_init(&reply->list);
1738                         spin_unlock_irqrestore(&card->lock, flags);
1739                         keep_reply = 0;
1740                         if (reply->callback != NULL) {
1741                                 if (cmd) {
1742                                         reply->offset = (__u16)((char*)cmd -
1743                                                                 (char *)iob->data);
1744                                         keep_reply = reply->callback(card,
1745                                                         reply,
1746                                                         (unsigned long)cmd);
1747                                 }
1748                                 else
1749                                         keep_reply = reply->callback(card,
1750                                                         reply,
1751                                                         (unsigned long)iob);
1752                         }
1753                         if (cmd)
1754                                 reply->rc = (u16) cmd->hdr.return_code;
1755                         else if (iob->rc)
1756                                 reply->rc = iob->rc;
1757                         if (keep_reply) {
1758                                 spin_lock_irqsave(&card->lock, flags);
1759                                 list_add_tail(&reply->list,
1760                                               &card->cmd_waiter_list);
1761                                 spin_unlock_irqrestore(&card->lock, flags);
1762                         } else {
1763                                 reply->received = 1;
1764                                 wake_up(&reply->wait_q);
1765                         }
1766                         qeth_put_reply(reply);
1767                         goto out;
1768                 }
1769         }
1770         spin_unlock_irqrestore(&card->lock, flags);
1771 out:
1772         memcpy(&card->seqno.pdu_hdr_ack,
1773                 QETH_PDU_HEADER_SEQ_NO(iob->data),
1774                 QETH_SEQ_NO_LENGTH);
1775         qeth_release_buffer(channel,iob);
1776 }
1777
1778 static int
1779 qeth_send_control_data(struct qeth_card *card, int len,
1780                        struct qeth_cmd_buffer *iob,
1781                        int (*reply_cb)
1782                        (struct qeth_card *, struct qeth_reply*, unsigned long),
1783                        void *reply_param)
1784
1785 {
1786         int rc;
1787         unsigned long flags;
1788         struct qeth_reply *reply;
1789         struct timer_list timer;
1790
1791         QETH_DBF_TEXT(trace, 2, "sendctl");
1792
1793         qeth_setup_ccw(&card->write,iob->data,len);
1794
1795         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1796                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1797         card->seqno.trans_hdr++;
1798
1799         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1800                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1801         card->seqno.pdu_hdr++;
1802         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1803                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1804         iob->callback = qeth_release_buffer;
1805
1806         reply = qeth_alloc_reply(card);
1807         if (!reply) {
1808                 PRINT_WARN("Could no alloc qeth_reply!\n");
1809                 return -ENOMEM;
1810         }
1811         reply->callback = reply_cb;
1812         reply->param = reply_param;
1813         if (card->state == CARD_STATE_DOWN)
1814                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1815         else
1816                 reply->seqno = card->seqno.ipa++;
1817         init_timer(&timer);
1818         timer.function = qeth_cmd_timeout;
1819         timer.data = (unsigned long) reply;
1820         if (IS_IPA(iob->data))
1821                 timer.expires = jiffies + QETH_IPA_TIMEOUT;
1822         else
1823                 timer.expires = jiffies + QETH_TIMEOUT;
1824         init_waitqueue_head(&reply->wait_q);
1825         spin_lock_irqsave(&card->lock, flags);
1826         list_add_tail(&reply->list, &card->cmd_waiter_list);
1827         spin_unlock_irqrestore(&card->lock, flags);
1828         QETH_DBF_HEX(control, 2, iob->data, QETH_DBF_CONTROL_LEN);
1829         wait_event(card->wait_q,
1830                    atomic_compare_and_swap(0,1,&card->write.irq_pending) == 0);
1831         QETH_DBF_TEXT(trace, 6, "noirqpnd");
1832         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1833         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1834                               (addr_t) iob, 0, 0);
1835         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1836         if (rc){
1837                 PRINT_WARN("qeth_send_control_data: "
1838                            "ccw_device_start rc = %i\n", rc);
1839                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
1840                 spin_lock_irqsave(&card->lock, flags);
1841                 list_del_init(&reply->list);
1842                 qeth_put_reply(reply);
1843                 spin_unlock_irqrestore(&card->lock, flags);
1844                 qeth_release_buffer(iob->channel, iob);
1845                 atomic_set(&card->write.irq_pending, 0);
1846                 wake_up(&card->wait_q);
1847                 return rc;
1848         }
1849         add_timer(&timer);
1850         wait_event(reply->wait_q, reply->received);
1851         del_timer_sync(&timer);
1852         rc = reply->rc;
1853         qeth_put_reply(reply);
1854         return rc;
1855 }
1856
1857 static int
1858 qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1859                   int (*reply_cb)
1860                   (struct qeth_card *,struct qeth_reply*, unsigned long),
1861                   void *reply_param)
1862 {
1863         int rc;
1864         char prot_type;
1865
1866         QETH_DBF_TEXT(trace,4,"sendipa");
1867
1868         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1869
1870         if (card->options.layer2)
1871                 prot_type = QETH_PROT_LAYER2;
1872         else
1873                 prot_type = QETH_PROT_TCPIP;
1874
1875         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data),&prot_type,1);
1876         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1877                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1878
1879         rc = qeth_send_control_data(card, IPA_CMD_LENGTH, iob,
1880                                     reply_cb, reply_param);
1881         return rc;
1882 }
1883
1884
1885 static int
1886 qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1887                   unsigned long data)
1888 {
1889         struct qeth_cmd_buffer *iob;
1890
1891         QETH_DBF_TEXT(setup, 2, "cmenblcb");
1892
1893         iob = (struct qeth_cmd_buffer *) data;
1894         memcpy(&card->token.cm_filter_r,
1895                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
1896                QETH_MPC_TOKEN_LENGTH);
1897         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
1898         return 0;
1899 }
1900
1901 static int
1902 qeth_cm_enable(struct qeth_card *card)
1903 {
1904         int rc;
1905         struct qeth_cmd_buffer *iob;
1906
1907         QETH_DBF_TEXT(setup,2,"cmenable");
1908
1909         iob = qeth_wait_for_buffer(&card->write);
1910         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
1911         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
1912                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1913         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
1914                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
1915
1916         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
1917                                     qeth_cm_enable_cb, NULL);
1918         return rc;
1919 }
1920
1921 static int
1922 qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1923                  unsigned long data)
1924 {
1925
1926         struct qeth_cmd_buffer *iob;
1927
1928         QETH_DBF_TEXT(setup, 2, "cmsetpcb");
1929
1930         iob = (struct qeth_cmd_buffer *) data;
1931         memcpy(&card->token.cm_connection_r,
1932                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
1933                QETH_MPC_TOKEN_LENGTH);
1934         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
1935         return 0;
1936 }
1937
1938 static int
1939 qeth_cm_setup(struct qeth_card *card)
1940 {
1941         int rc;
1942         struct qeth_cmd_buffer *iob;
1943
1944         QETH_DBF_TEXT(setup,2,"cmsetup");
1945
1946         iob = qeth_wait_for_buffer(&card->write);
1947         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
1948         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
1949                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1950         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
1951                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
1952         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
1953                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
1954         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
1955                                     qeth_cm_setup_cb, NULL);
1956         return rc;
1957
1958 }
1959
1960 static int
1961 qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1962                    unsigned long data)
1963 {
1964
1965         __u16 mtu, framesize;
1966         __u16 len;
1967         __u8 link_type;
1968         struct qeth_cmd_buffer *iob;
1969
1970         QETH_DBF_TEXT(setup, 2, "ulpenacb");
1971
1972         iob = (struct qeth_cmd_buffer *) data;
1973         memcpy(&card->token.ulp_filter_r,
1974                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
1975                QETH_MPC_TOKEN_LENGTH);
1976         if (qeth_get_mtu_out_of_mpc(card->info.type)) {
1977                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
1978                 mtu = qeth_get_mtu_outof_framesize(framesize);
1979                 if (!mtu) {
1980                         iob->rc = -EINVAL;
1981                         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
1982                         return 0;
1983                 }
1984                 card->info.max_mtu = mtu;
1985                 card->info.initial_mtu = mtu;
1986                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
1987         } else {
1988                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
1989                 card->info.max_mtu = qeth_get_max_mtu_for_card(card->info.type);
1990                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1991         }
1992
1993         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
1994         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
1995                 memcpy(&link_type,
1996                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
1997                 card->info.link_type = link_type;
1998         } else
1999                 card->info.link_type = 0;
2000         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2001         return 0;
2002 }
2003
2004 static int
2005 qeth_ulp_enable(struct qeth_card *card)
2006 {
2007         int rc;
2008         char prot_type;
2009         struct qeth_cmd_buffer *iob;
2010
2011         /*FIXME: trace view callbacks*/
2012         QETH_DBF_TEXT(setup,2,"ulpenabl");
2013
2014         iob = qeth_wait_for_buffer(&card->write);
2015         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2016
2017         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2018                 (__u8) card->info.portno;
2019         if (card->options.layer2)
2020                 prot_type = QETH_PROT_LAYER2;
2021         else
2022                 prot_type = QETH_PROT_TCPIP;
2023
2024         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data),&prot_type,1);
2025         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2026                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2027         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2028                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2029         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2030                card->info.portname, 9);
2031         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2032                                     qeth_ulp_enable_cb, NULL);
2033         return rc;
2034
2035 }
2036
2037 static inline __u16
2038 __raw_devno_from_bus_id(char *id)
2039 {
2040         id += (strlen(id) - 4);
2041         return (__u16) simple_strtoul(id, &id, 16);
2042 }
2043
2044 static int
2045 qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2046                   unsigned long data)
2047 {
2048         struct qeth_cmd_buffer *iob;
2049
2050         QETH_DBF_TEXT(setup, 2, "ulpstpcb");
2051
2052         iob = (struct qeth_cmd_buffer *) data;
2053         memcpy(&card->token.ulp_connection_r,
2054                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2055                QETH_MPC_TOKEN_LENGTH);
2056         QETH_DBF_TEXT_(setup, 2, "  rc%d", iob->rc);
2057         return 0;
2058 }
2059
2060 static int
2061 qeth_ulp_setup(struct qeth_card *card)
2062 {
2063         int rc;
2064         __u16 temp;
2065         struct qeth_cmd_buffer *iob;
2066
2067         QETH_DBF_TEXT(setup,2,"ulpsetup");
2068
2069         iob = qeth_wait_for_buffer(&card->write);
2070         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2071
2072         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2073                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2074         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2075                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2076         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2077                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2078
2079         temp = __raw_devno_from_bus_id(CARD_DDEV_ID(card));
2080         memcpy(QETH_ULP_SETUP_CUA(iob->data), &temp, 2);
2081         temp = (card->info.cula << 8) + card->info.unit_addr2;
2082         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2083         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2084                                     qeth_ulp_setup_cb, NULL);
2085         return rc;
2086 }
2087
2088 static inline int
2089 qeth_check_for_inbound_error(struct qeth_qdio_buffer *buf,
2090                              unsigned int qdio_error,
2091                              unsigned int siga_error)
2092 {
2093         int rc = 0;
2094
2095         if (qdio_error || siga_error) {
2096                 QETH_DBF_TEXT(trace, 2, "qdinerr");
2097                 QETH_DBF_TEXT(qerr, 2, "qdinerr");
2098                 QETH_DBF_TEXT_(qerr, 2, " F15=%02X",
2099                                buf->buffer->element[15].flags & 0xff);
2100                 QETH_DBF_TEXT_(qerr, 2, " F14=%02X",
2101                                buf->buffer->element[14].flags & 0xff);
2102                 QETH_DBF_TEXT_(qerr, 2, " qerr=%X", qdio_error);
2103                 QETH_DBF_TEXT_(qerr, 2, " serr=%X", siga_error);
2104                 rc = 1;
2105         }
2106         return rc;
2107 }
2108
2109 static inline struct sk_buff *
2110 qeth_get_skb(unsigned int length)
2111 {
2112         struct sk_buff* skb;
2113 #ifdef CONFIG_QETH_VLAN
2114         if ((skb = dev_alloc_skb(length + VLAN_HLEN)))
2115                 skb_reserve(skb, VLAN_HLEN);
2116 #else
2117         skb = dev_alloc_skb(length);
2118 #endif
2119         return skb;
2120 }
2121
2122 static inline struct sk_buff *
2123 qeth_get_next_skb(struct qeth_card *card, struct qdio_buffer *buffer,
2124                   struct qdio_buffer_element **__element, int *__offset,
2125                   struct qeth_hdr **hdr)
2126 {
2127         struct qdio_buffer_element *element = *__element;
2128         int offset = *__offset;
2129         struct sk_buff *skb = NULL;
2130         int skb_len;
2131         void *data_ptr;
2132         int data_len;
2133
2134         QETH_DBF_TEXT(trace,6,"nextskb");
2135         /* qeth_hdr must not cross element boundaries */
2136         if (element->length < offset + sizeof(struct qeth_hdr)){
2137                 if (qeth_is_last_sbale(element))
2138                         return NULL;
2139                 element++;
2140                 offset = 0;
2141                 if (element->length < sizeof(struct qeth_hdr))
2142                         return NULL;
2143         }
2144         *hdr = element->addr + offset;
2145
2146         offset += sizeof(struct qeth_hdr);
2147         if (card->options.layer2)
2148                 skb_len = (*hdr)->hdr.l2.pkt_length;
2149         else
2150                 skb_len = (*hdr)->hdr.l3.length;
2151
2152         if (!skb_len)
2153                 return NULL;
2154         if (card->options.fake_ll){
2155                 if (!(skb = qeth_get_skb(skb_len + QETH_FAKE_LL_LEN)))
2156                         goto no_mem;
2157                 skb_pull(skb, QETH_FAKE_LL_LEN);
2158         } else if (!(skb = qeth_get_skb(skb_len)))
2159                 goto no_mem;
2160         data_ptr = element->addr + offset;
2161         while (skb_len) {
2162                 data_len = min(skb_len, (int)(element->length - offset));
2163                 if (data_len)
2164                         memcpy(skb_put(skb, data_len), data_ptr, data_len);
2165                 skb_len -= data_len;
2166                 if (skb_len){
2167                         if (qeth_is_last_sbale(element)){
2168                                 QETH_DBF_TEXT(trace,4,"unexeob");
2169                                 QETH_DBF_TEXT_(trace,4,"%s",CARD_BUS_ID(card));
2170                                 QETH_DBF_TEXT(qerr,2,"unexeob");
2171                                 QETH_DBF_TEXT_(qerr,2,"%s",CARD_BUS_ID(card));
2172                                 QETH_DBF_HEX(misc,4,buffer,sizeof(*buffer));
2173                                 dev_kfree_skb_any(skb);
2174                                 card->stats.rx_errors++;
2175                                 return NULL;
2176                         }
2177                         element++;
2178                         offset = 0;
2179                         data_ptr = element->addr;
2180                 } else {
2181                         offset += data_len;
2182                 }
2183         }
2184         *__element = element;
2185         *__offset = offset;
2186         return skb;
2187 no_mem:
2188         if (net_ratelimit()){
2189                 PRINT_WARN("No memory for packet received on %s.\n",
2190                            QETH_CARD_IFNAME(card));
2191                 QETH_DBF_TEXT(trace,2,"noskbmem");
2192                 QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2193         }
2194         card->stats.rx_dropped++;
2195         return NULL;
2196 }
2197
2198 static inline unsigned short
2199 qeth_type_trans(struct sk_buff *skb, struct net_device *dev)
2200 {
2201         struct qeth_card *card;
2202         struct ethhdr *eth;
2203
2204         QETH_DBF_TEXT(trace,6,"typtrans");
2205
2206         card = (struct qeth_card *)dev->priv;
2207 #ifdef CONFIG_TR
2208         if ((card->info.link_type == QETH_LINK_TYPE_HSTR) ||
2209             (card->info.link_type == QETH_LINK_TYPE_LANE_TR))
2210                 return tr_type_trans(skb,dev);
2211 #endif /* CONFIG_TR */
2212         skb->mac.raw = skb->data;
2213         skb_pull(skb, ETH_HLEN );
2214         eth = eth_hdr(skb);
2215
2216         if (*eth->h_dest & 1) {
2217                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
2218                         skb->pkt_type = PACKET_BROADCAST;
2219                 else
2220                         skb->pkt_type = PACKET_MULTICAST;
2221         } else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
2222                 skb->pkt_type = PACKET_OTHERHOST;
2223
2224         if (ntohs(eth->h_proto) >= 1536)
2225                 return eth->h_proto;
2226         if (*(unsigned short *) (skb->data) == 0xFFFF)
2227                 return htons(ETH_P_802_3);
2228         return htons(ETH_P_802_2);
2229 }
2230
2231 static inline void
2232 qeth_rebuild_skb_fake_ll(struct qeth_card *card, struct sk_buff *skb,
2233                          struct qeth_hdr *hdr)
2234 {
2235         struct ethhdr *fake_hdr;
2236         struct iphdr *ip_hdr;
2237
2238         QETH_DBF_TEXT(trace,5,"skbfake");
2239         skb->mac.raw = skb->data - QETH_FAKE_LL_LEN;
2240         /* this is a fake ethernet header */
2241         fake_hdr = (struct ethhdr *) skb->mac.raw;
2242
2243         /* the destination MAC address */
2244         switch (skb->pkt_type){
2245         case PACKET_MULTICAST:
2246                 switch (skb->protocol){
2247 #ifdef CONFIG_QETH_IPV6
2248                 case __constant_htons(ETH_P_IPV6):
2249                         ndisc_mc_map((struct in6_addr *)
2250                                      skb->data + QETH_FAKE_LL_V6_ADDR_POS,
2251                                      fake_hdr->h_dest, card->dev, 0);
2252                         break;
2253 #endif /* CONFIG_QETH_IPV6 */
2254                 case __constant_htons(ETH_P_IP):
2255                         ip_hdr = (struct iphdr *)skb->data;
2256                         if (card->dev->type == ARPHRD_IEEE802_TR)
2257                                 ip_tr_mc_map(ip_hdr->daddr, fake_hdr->h_dest);
2258                         else
2259                                 ip_eth_mc_map(ip_hdr->daddr, fake_hdr->h_dest);
2260                         break;
2261                 default:
2262                         memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2263                 }
2264                 break;
2265         case PACKET_BROADCAST:
2266                 memset(fake_hdr->h_dest, 0xff, ETH_ALEN);
2267                 break;
2268         default:
2269                 memcpy(fake_hdr->h_dest, card->dev->dev_addr, ETH_ALEN);
2270         }
2271         /* the source MAC address */
2272         if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
2273                 memcpy(fake_hdr->h_source, &hdr->hdr.l3.dest_addr[2], ETH_ALEN);
2274         else
2275                 memset(fake_hdr->h_source, 0, ETH_ALEN);
2276         /* the protocol */
2277         fake_hdr->h_proto = skb->protocol;
2278 }
2279
2280 static inline void
2281 qeth_rebuild_skb_vlan(struct qeth_card *card, struct sk_buff *skb,
2282                       struct qeth_hdr *hdr)
2283 {
2284 #ifdef CONFIG_QETH_VLAN
2285         u16 *vlan_tag;
2286
2287         if (hdr->hdr.l3.ext_flags &
2288             (QETH_HDR_EXT_VLAN_FRAME | QETH_HDR_EXT_INCLUDE_VLAN_TAG)) {
2289                 vlan_tag = (u16 *) skb_push(skb, VLAN_HLEN);
2290                 *vlan_tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME)?
2291                         hdr->hdr.l3.vlan_id : *((u16 *)&hdr->hdr.l3.dest_addr[12]);
2292                 *(vlan_tag + 1) = skb->protocol;
2293                 skb->protocol = __constant_htons(ETH_P_8021Q);
2294         }
2295 #endif /* CONFIG_QETH_VLAN */
2296 }
2297
2298 static inline __u16
2299 qeth_layer2_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2300                         struct qeth_hdr *hdr)
2301 {
2302         unsigned short vlan_id = 0;
2303 #ifdef CONFIG_QETH_VLAN
2304         struct vlan_hdr *vhdr;
2305 #endif
2306
2307         skb->pkt_type = PACKET_HOST;
2308         skb->protocol = qeth_type_trans(skb, skb->dev);
2309         if (card->options.checksum_type == NO_CHECKSUMMING)
2310                 skb->ip_summed = CHECKSUM_UNNECESSARY;
2311         else
2312                 skb->ip_summed = CHECKSUM_NONE;
2313 #ifdef CONFIG_QETH_VLAN
2314         if (hdr->hdr.l2.flags[2] & (QETH_LAYER2_FLAG_VLAN)) {
2315                 vhdr = (struct vlan_hdr *) skb->data;
2316                 skb->protocol =
2317                         __constant_htons(vhdr->h_vlan_encapsulated_proto);
2318                 vlan_id = hdr->hdr.l2.vlan_id;
2319                 skb_pull(skb, VLAN_HLEN);
2320         }
2321 #endif
2322         return vlan_id;
2323 }
2324
2325 static inline void
2326 qeth_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
2327                  struct qeth_hdr *hdr)
2328 {
2329 #ifdef CONFIG_QETH_IPV6
2330         if (hdr->hdr.l3.flags & QETH_HDR_PASSTHRU) {
2331                 skb->pkt_type = PACKET_HOST;
2332                 skb->protocol = qeth_type_trans(skb, card->dev);
2333                 return;
2334         }
2335 #endif /* CONFIG_QETH_IPV6 */
2336         skb->protocol = htons((hdr->hdr.l3.flags & QETH_HDR_IPV6)? ETH_P_IPV6 :
2337                               ETH_P_IP);
2338         switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK){
2339         case QETH_CAST_UNICAST:
2340                 skb->pkt_type = PACKET_HOST;
2341                 break;
2342         case QETH_CAST_MULTICAST:
2343                 skb->pkt_type = PACKET_MULTICAST;
2344                 card->stats.multicast++;
2345                 break;
2346         case QETH_CAST_BROADCAST:
2347                 skb->pkt_type = PACKET_BROADCAST;
2348                 card->stats.multicast++;
2349                 break;
2350         case QETH_CAST_ANYCAST:
2351         case QETH_CAST_NOCAST:
2352         default:
2353                 skb->pkt_type = PACKET_HOST;
2354         }
2355         qeth_rebuild_skb_vlan(card, skb, hdr);
2356         if (card->options.fake_ll)
2357                 qeth_rebuild_skb_fake_ll(card, skb, hdr);
2358         else
2359                 skb->mac.raw = skb->data;
2360         skb->ip_summed = card->options.checksum_type;
2361         if (card->options.checksum_type == HW_CHECKSUMMING){
2362                 if ( (hdr->hdr.l3.ext_flags &
2363                       (QETH_HDR_EXT_CSUM_HDR_REQ |
2364                        QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
2365                      (QETH_HDR_EXT_CSUM_HDR_REQ |
2366                       QETH_HDR_EXT_CSUM_TRANSP_REQ) )
2367                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2368                 else
2369                         skb->ip_summed = SW_CHECKSUMMING;
2370         }
2371 }
2372
2373 static inline void
2374 qeth_process_inbound_buffer(struct qeth_card *card,
2375                             struct qeth_qdio_buffer *buf, int index)
2376 {
2377         struct qdio_buffer_element *element;
2378         struct sk_buff *skb;
2379         struct qeth_hdr *hdr;
2380         int offset;
2381         int rxrc;
2382         __u16 vlan_tag = 0;
2383
2384         /* get first element of current buffer */
2385         element = (struct qdio_buffer_element *)&buf->buffer->element[0];
2386         offset = 0;
2387 #ifdef CONFIG_QETH_PERF_STATS
2388         card->perf_stats.bufs_rec++;
2389 #endif
2390         while((skb = qeth_get_next_skb(card, buf->buffer, &element,
2391                                        &offset, &hdr))) {
2392                 skb->dev = card->dev;
2393                 if (hdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
2394                         vlan_tag = qeth_layer2_rebuild_skb(card, skb, hdr);
2395                 else
2396                         qeth_rebuild_skb(card, skb, hdr);
2397                 /* is device UP ? */
2398                 if (!(card->dev->flags & IFF_UP)){
2399                         dev_kfree_skb_any(skb);
2400                         continue;
2401                 }
2402 #ifdef CONFIG_QETH_VLAN
2403                 if (vlan_tag)
2404                         vlan_hwaccel_rx(skb, card->vlangrp, vlan_tag);
2405                 else
2406 #endif
2407                 rxrc = netif_rx(skb);
2408                 card->dev->last_rx = jiffies;
2409                 card->stats.rx_packets++;
2410                 card->stats.rx_bytes += skb->len;
2411         }
2412 }
2413
2414 static inline struct qeth_buffer_pool_entry *
2415 qeth_get_buffer_pool_entry(struct qeth_card *card)
2416 {
2417         struct qeth_buffer_pool_entry *entry;
2418
2419         QETH_DBF_TEXT(trace, 6, "gtbfplen");
2420         if (!list_empty(&card->qdio.in_buf_pool.entry_list)) {
2421                 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2422                                 struct qeth_buffer_pool_entry, list);
2423                 list_del_init(&entry->list);
2424                 return entry;
2425         }
2426         return NULL;
2427 }
2428
2429 static inline void
2430 qeth_init_input_buffer(struct qeth_card *card, struct qeth_qdio_buffer *buf)
2431 {
2432         struct qeth_buffer_pool_entry *pool_entry;
2433         int i;
2434
2435         pool_entry = qeth_get_buffer_pool_entry(card);
2436         /*
2437          * since the buffer is accessed only from the input_tasklet
2438          * there shouldn't be a need to synchronize; also, since we use
2439          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2440          * buffers
2441          */
2442         BUG_ON(!pool_entry);
2443
2444         buf->pool_entry = pool_entry;
2445         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i){
2446                 buf->buffer->element[i].length = PAGE_SIZE;
2447                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2448                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2449                         buf->buffer->element[i].flags = SBAL_FLAGS_LAST_ENTRY;
2450                 else
2451                         buf->buffer->element[i].flags = 0;
2452         }
2453         buf->state = QETH_QDIO_BUF_EMPTY;
2454 }
2455
2456 static inline void
2457 qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
2458                          struct qeth_qdio_out_buffer *buf)
2459 {
2460         int i;
2461         struct sk_buff *skb;
2462
2463         /* is PCI flag set on buffer? */
2464         if (buf->buffer->element[0].flags & 0x40)
2465                 atomic_dec(&queue->set_pci_flags_count);
2466
2467         while ((skb = skb_dequeue(&buf->skb_list))){
2468                 atomic_dec(&skb->users);
2469                 dev_kfree_skb_any(skb);
2470         }
2471         qeth_eddp_buf_release_contexts(buf);
2472         for(i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i){
2473                 buf->buffer->element[i].length = 0;
2474                 buf->buffer->element[i].addr = NULL;
2475                 buf->buffer->element[i].flags = 0;
2476         }
2477         buf->next_element_to_fill = 0;
2478         atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
2479 }
2480
2481 static inline void
2482 qeth_queue_input_buffer(struct qeth_card *card, int index)
2483 {
2484         struct qeth_qdio_q *queue = card->qdio.in_q;
2485         int count;
2486         int i;
2487         int rc;
2488
2489         QETH_DBF_TEXT(trace,6,"queinbuf");
2490         count = (index < queue->next_buf_to_init)?
2491                 card->qdio.in_buf_pool.buf_count -
2492                 (queue->next_buf_to_init - index) :
2493                 card->qdio.in_buf_pool.buf_count -
2494                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
2495         /* only requeue at a certain threshold to avoid SIGAs */
2496         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)){
2497                 for (i = queue->next_buf_to_init;
2498                      i < queue->next_buf_to_init + count; ++i)
2499                         qeth_init_input_buffer(card,
2500                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q]);
2501                 /*
2502                  * according to old code it should be avoided to requeue all
2503                  * 128 buffers in order to benefit from PCI avoidance.
2504                  * this function keeps at least one buffer (the buffer at
2505                  * 'index') un-requeued -> this buffer is the first buffer that
2506                  * will be requeued the next time
2507                  */
2508 #ifdef CONFIG_QETH_PERF_STATS
2509                 card->perf_stats.inbound_do_qdio_cnt++;
2510                 card->perf_stats.inbound_do_qdio_start_time = qeth_get_micros();
2511 #endif
2512                 rc = do_QDIO(CARD_DDEV(card),
2513                              QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
2514                              0, queue->next_buf_to_init, count, NULL);
2515 #ifdef CONFIG_QETH_PERF_STATS
2516                 card->perf_stats.inbound_do_qdio_time += qeth_get_micros() -
2517                         card->perf_stats.inbound_do_qdio_start_time;
2518 #endif
2519                 if (rc){
2520                         PRINT_WARN("qeth_queue_input_buffer's do_QDIO "
2521                                    "return %i (device %s).\n",
2522                                    rc, CARD_DDEV_ID(card));
2523                         QETH_DBF_TEXT(trace,2,"qinberr");
2524                         QETH_DBF_TEXT_(trace,2,"%s",CARD_BUS_ID(card));
2525                 }
2526                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
2527                                           QDIO_MAX_BUFFERS_PER_Q;
2528         }
2529 }
2530
2531 static inline void
2532 qeth_put_buffer_pool_entry(struct qeth_card *card,
2533                            struct qeth_buffer_pool_entry *entry)
2534 {
2535         QETH_DBF_TEXT(trace, 6, "ptbfplen");
2536         list_add_tail(&entry->list, &card->qdio.in_buf_pool.entry_list);
2537 }
2538
2539 static void
2540 qeth_qdio_input_handler(struct ccw_device * ccwdev, unsigned int status,
2541                         unsigned int qdio_err, unsigned int siga_err,
2542                         unsigned int queue, int first_element, int count,
2543                         unsigned long card_ptr)
2544 {
2545         struct net_device *net_dev;
2546         struct qeth_card *card;
2547         struct qeth_qdio_buffer *buffer;
2548         int index;
2549         int i;
2550
2551         QETH_DBF_TEXT(trace, 6, "qdinput");
2552         card = (struct qeth_card *) card_ptr;
2553         net_dev = card->dev;
2554 #ifdef CONFIG_QETH_PERF_STATS
2555         card->perf_stats.inbound_cnt++;
2556         card->perf_stats.inbound_start_time = qeth_get_micros();
2557 #endif
2558         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
2559                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
2560                         QETH_DBF_TEXT(trace, 1,"qdinchk");
2561                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2562                         QETH_DBF_TEXT_(trace,1,"%04X%04X",first_element,count);
2563                         QETH_DBF_TEXT_(trace,1,"%04X%04X", queue, status);
2564                         qeth_schedule_recovery(card);
2565                         return;
2566                 }
2567         }
2568         for (i = first_element; i < (first_element + count); ++i) {
2569                 index = i % QDIO_MAX_BUFFERS_PER_Q;
2570                 buffer = &card->qdio.in_q->bufs[index];
2571                 if (!((status == QDIO_STATUS_LOOK_FOR_ERROR) &&
2572                       qeth_check_for_inbound_error(buffer, qdio_err, siga_err)))
2573                         qeth_process_inbound_buffer(card, buffer, index);
2574                 /* clear buffer and give back to hardware */
2575                 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
2576                 qeth_queue_input_buffer(card, index);
2577         }
2578 #ifdef CONFIG_QETH_PERF_STATS
2579         card->perf_stats.inbound_time += qeth_get_micros() -
2580                 card->perf_stats.inbound_start_time;
2581 #endif
2582 }
2583
2584 static inline int
2585 qeth_handle_send_error(struct qeth_card *card,
2586                        struct qeth_qdio_out_buffer *buffer,
2587                        int qdio_err, int siga_err)
2588 {
2589         int sbalf15 = buffer->buffer->element[15].flags & 0xff;
2590         int cc = siga_err & 3;
2591
2592         QETH_DBF_TEXT(trace, 6, "hdsnderr");
2593         switch (cc) {
2594         case 0:
2595                 if (qdio_err){
2596                         QETH_DBF_TEXT(trace, 1,"lnkfail");
2597                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2598                         QETH_DBF_TEXT_(trace,1,"%04x %02x",
2599                                        (u16)qdio_err, (u8)sbalf15);
2600                         return QETH_SEND_ERROR_LINK_FAILURE;
2601                 }
2602                 return QETH_SEND_ERROR_NONE;
2603         case 2:
2604                 if (siga_err & QDIO_SIGA_ERROR_B_BIT_SET) {
2605                         QETH_DBF_TEXT(trace, 1, "SIGAcc2B");
2606                         QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2607                         return QETH_SEND_ERROR_KICK_IT;
2608                 }
2609                 if ((sbalf15 >= 15) && (sbalf15 <= 31))
2610                         return QETH_SEND_ERROR_RETRY;
2611                 return QETH_SEND_ERROR_LINK_FAILURE;
2612                 /* look at qdio_error and sbalf 15 */
2613         case 1:
2614                 QETH_DBF_TEXT(trace, 1, "SIGAcc1");
2615                 QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2616                 return QETH_SEND_ERROR_LINK_FAILURE;
2617         case 3:
2618                 QETH_DBF_TEXT(trace, 1, "SIGAcc3");
2619                 QETH_DBF_TEXT_(trace,1,"%s",CARD_BUS_ID(card));
2620                 return QETH_SEND_ERROR_KICK_IT;
2621         }
2622         return QETH_SEND_ERROR_LINK_FAILURE;
2623 }
2624
2625 void
2626 qeth_flush_buffers(struct qeth_qdio_out_q *queue, int under_int,
2627                    int index, int count)
2628 {
2629         struct qeth_qdio_out_buffer *buf;
2630         int rc;
2631         int i;
2632
2633         QETH_DBF_TEXT(trace, 6, "flushbuf");
2634
2635         for (i = index; i < index + count; ++i) {
2636                 buf = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2637                 buf->buffer->element[buf->next_element_to_fill - 1].flags |=
2638                                 SBAL_FLAGS_LAST_ENTRY;
2639
2640                 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
2641                         continue;
2642
2643                 if (!queue->do_pack){
2644                         if ((atomic_read(&queue->used_buffers) >=
2645                                 (QETH_HIGH_WATERMARK_PACK -
2646                                  QETH_WATERMARK_PACK_FUZZ)) &&
2647                             !atomic_read(&queue->set_pci_flags_count)){
2648                                 /* it's likely that we'll go to packing
2649                                  * mode soon */
2650                                 atomic_inc(&queue->set_pci_flags_count);
2651                                 buf->buffer->element[0].flags |= 0x40;
2652                         }
2653                 } else {
2654                         if (!atomic_read(&queue->set_pci_flags_count)){
2655                                 /*
2656                                  * there's no outstanding PCI any more, so we
2657                                  * have to request a PCI to be sure the the PCI
2658                                  * will wake at some time in the future then we
2659                                  * can flush packed buffers that might still be
2660                                  * hanging around, which can happen if no
2661                                  * further send was requested by the stack
2662                                  */
2663                                 atomic_inc(&queue->set_pci_flags_count);
2664                                 buf->buffer->element[0].flags |= 0x40;
2665                         }
2666                 }
2667         }
2668
2669         queue->card->dev->trans_start = jiffies;
2670 #ifdef CONFIG_QETH_PERF_STATS
2671         queue->card->perf_stats.outbound_do_qdio_cnt++;
2672         queue->card->perf_stats.outbound_do_qdio_start_time = qeth_get_micros();
2673 #endif
2674         if (under_int)
2675                 rc = do_QDIO(CARD_DDEV(queue->card),
2676                              QDIO_FLAG_SYNC_OUTPUT | QDIO_FLAG_UNDER_INTERRUPT,
2677                              queue->queue_no, index, count, NULL);
2678         else
2679                 rc = do_QDIO(CARD_DDEV(queue->card), QDIO_FLAG_SYNC_OUTPUT,
2680                              queue->queue_no, index, count, NULL);
2681 #ifdef CONFIG_QETH_PERF_STATS
2682         queue->card->perf_stats.outbound_do_qdio_time += qeth_get_micros() -
2683                 queue->card->perf_stats.outbound_do_qdio_start_time;
2684 #endif
2685         if (rc){
2686                 QETH_DBF_SPRINTF(trace, 0, "qeth_flush_buffers: do_QDIO "
2687                                  "returned error (%i) on device %s.",
2688                                  rc, CARD_DDEV_ID(queue->card));
2689                 QETH_DBF_TEXT(trace, 2, "flushbuf");
2690                 QETH_DBF_TEXT_(trace, 2, " err%d", rc);
2691                 queue->card->stats.tx_errors += count;
2692                 /* this must not happen under normal circumstances. if it
2693                  * happens something is really wrong -> recover */
2694                 qeth_schedule_recovery(queue->card);
2695                 return;
2696         }
2697         atomic_add(count, &queue->used_buffers);
2698 #ifdef CONFIG_QETH_PERF_STATS
2699         queue->card->perf_stats.bufs_sent += count;
2700 #endif
2701 }
2702
2703 /*
2704  * Switched to packing state if the number of used buffers on a queue
2705  * reaches a certain limit.
2706  */
2707 static inline void
2708 qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
2709 {
2710         if (!queue->do_pack) {
2711                 if (atomic_read(&queue->used_buffers)
2712                     >= QETH_HIGH_WATERMARK_PACK){
2713                         /* switch non-PACKING -> PACKING */
2714                         QETH_DBF_TEXT(trace, 6, "np->pack");
2715 #ifdef CONFIG_QETH_PERF_STATS
2716                         queue->card->perf_stats.sc_dp_p++;
2717 #endif
2718                         queue->do_pack = 1;
2719                 }
2720         }
2721 }
2722
2723 /*
2724  * Switches from packing to non-packing mode. If there is a packing
2725  * buffer on the queue this buffer will be prepared to be flushed.
2726  * In that case 1 is returned to inform the caller. If no buffer
2727  * has to be flushed, zero is returned.
2728  */
2729 static inline int
2730 qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
2731 {
2732         struct qeth_qdio_out_buffer *buffer;
2733         int flush_count = 0;
2734
2735         if (queue->do_pack) {
2736                 if (atomic_read(&queue->used_buffers)
2737                     <= QETH_LOW_WATERMARK_PACK) {
2738                         /* switch PACKING -> non-PACKING */
2739                         QETH_DBF_TEXT(trace, 6, "pack->np");
2740 #ifdef CONFIG_QETH_PERF_STATS
2741                         queue->card->perf_stats.sc_p_dp++;
2742 #endif
2743                         queue->do_pack = 0;
2744                         /* flush packing buffers */
2745                         buffer = &queue->bufs[queue->next_buf_to_fill];
2746                         if ((atomic_read(&buffer->state) ==
2747                                                 QETH_QDIO_BUF_EMPTY) &&
2748                             (buffer->next_element_to_fill > 0)) {
2749                                 atomic_set(&buffer->state,QETH_QDIO_BUF_PRIMED);
2750                                 flush_count++;
2751                                 queue->next_buf_to_fill =
2752                                         (queue->next_buf_to_fill + 1) %
2753                                         QDIO_MAX_BUFFERS_PER_Q;
2754                         }
2755                 }
2756         }
2757         return flush_count;
2758 }
2759
2760 /*
2761  * Called to flush a packing buffer if no more pci flags are on the queue.
2762  * Checks if there is a packing buffer and prepares it to be flushed.
2763  * In that case returns 1, otherwise zero.
2764  */
2765 static inline int
2766 qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2767 {
2768         struct qeth_qdio_out_buffer *buffer;
2769
2770         buffer = &queue->bufs[queue->next_buf_to_fill];
2771         if((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
2772            (buffer->next_element_to_fill > 0)){
2773                 /* it's a packing buffer */
2774                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
2775                 queue->next_buf_to_fill =
2776                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
2777                 return 1;
2778         }
2779         return 0;
2780 }
2781
2782 static inline void
2783 qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2784 {
2785         int index;
2786         int flush_cnt = 0;
2787         int q_was_packing = 0;
2788
2789         /*
2790          * check if weed have to switch to non-packing mode or if
2791          * we have to get a pci flag out on the queue
2792          */
2793         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
2794             !atomic_read(&queue->set_pci_flags_count)){
2795                 if (atomic_swap(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
2796                                 QETH_OUT_Q_UNLOCKED) {
2797                         /*
2798                          * If we get in here, there was no action in
2799                          * do_send_packet. So, we check if there is a
2800                          * packing buffer to be flushed here.
2801                          */
2802                         netif_stop_queue(queue->card->dev);
2803                         index = queue->next_buf_to_fill;
2804                         q_was_packing = queue->do_pack;
2805                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
2806                         if (!flush_cnt &&
2807                             !atomic_read(&queue->set_pci_flags_count))
2808                                 flush_cnt +=
2809                                         qeth_flush_buffers_on_no_pci(queue);
2810 #ifdef CONFIG_QETH_PERF_STATS
2811                         if (q_was_packing)
2812                                 queue->card->perf_stats.bufs_sent_pack +=
2813                                         flush_cnt;
2814 #endif
2815                         if (flush_cnt)
2816                                 qeth_flush_buffers(queue, 1, index, flush_cnt);
2817                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
2818                 }
2819         }
2820 }
2821
2822 static void
2823 qeth_qdio_output_handler(struct ccw_device * ccwdev, unsigned int status,
2824                         unsigned int qdio_error, unsigned int siga_error,
2825                         unsigned int __queue, int first_element, int count,
2826                         unsigned long card_ptr)
2827 {
2828         struct qeth_card *card        = (struct qeth_card *) card_ptr;
2829         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
2830         struct qeth_qdio_out_buffer *buffer;
2831         int i;
2832
2833         QETH_DBF_TEXT(trace, 6, "qdouhdl");
2834         if (status & QDIO_STATUS_LOOK_FOR_ERROR) {
2835                 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION){
2836                         QETH_DBF_SPRINTF(trace, 2, "On device %s: "
2837                                          "received active check "
2838                                          "condition (0x%08x).",
2839                                          CARD_BUS_ID(card), status);
2840                         QETH_DBF_TEXT(trace, 2, "chkcond");
2841                         QETH_DBF_TEXT_(trace, 2, "%08x", status);
2842                         netif_stop_queue(card->dev);
2843                         qeth_schedule_recovery(card);
2844                         return;
2845                 }
2846         }
2847 #ifdef CONFIG_QETH_PERF_STATS
2848         card->perf_stats.outbound_handler_cnt++;
2849         card->perf_stats.outbound_handler_start_time = qeth_get_micros();
2850 #endif
2851         for(i = first_element; i < (first_element + count); ++i){
2852                 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2853                 /*we only handle the KICK_IT error by doing a recovery */
2854                 if (qeth_handle_send_error(card, buffer, qdio_error, siga_error)
2855                                 == QETH_SEND_ERROR_KICK_IT){
2856                         netif_stop_queue(card->dev);
2857                         qeth_schedule_recovery(card);
2858                         return;
2859                 }
2860                 qeth_clear_output_buffer(queue, buffer);
2861         }
2862         atomic_sub(count, &queue->used_buffers);
2863         /* check if we need to do something on this outbound queue */
2864         if (card->info.type != QETH_CARD_TYPE_IQD)
2865                 qeth_check_outbound_queue(queue);
2866
2867         netif_wake_queue(queue->card->dev);
2868 #ifdef CONFIG_QETH_PERF_STATS
2869         card->perf_stats.outbound_handler_time += qeth_get_micros() -
2870                 card->perf_stats.outbound_handler_start_time;
2871 #endif
2872 }
2873
2874 static void
2875 qeth_create_qib_param_field(struct qeth_card *card, char *param_field)
2876 {
2877
2878         param_field[0] = _ascebc['P'];
2879         param_field[1] = _ascebc['C'];
2880         param_field[2] = _ascebc['I'];
2881         param_field[3] = _ascebc['T'];
2882         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2883         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2884         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2885 }
2886
2887 static void
2888 qeth_create_qib_param_field_blkt(struct qeth_card *card, char *param_field)
2889 {
2890         param_field[16] = _ascebc['B'];
2891         param_field[17] = _ascebc['L'];
2892         param_field[18] = _ascebc['K'];
2893         param_field[19] = _ascebc['T'];
2894         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2895         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2896         *((unsigned int *) (&param_field[28])) = card->info.blkt.inter_packet_jumbo;
2897 }
2898
2899 static void
2900 qeth_initialize_working_pool_list(struct qeth_card *card)
2901 {
2902         struct qeth_buffer_pool_entry *entry;
2903
2904         QETH_DBF_TEXT(trace,5,"inwrklst");
2905
2906         list_for_each_entry(entry,
2907                             &card->qdio.init_pool.entry_list, init_list) {
2908                 qeth_put_buffer_pool_entry(card,entry);
2909         }
2910 }
2911
2912 static void
2913 qeth_clear_working_pool_list(struct qeth_card *card)
2914 {
2915         struct qeth_buffer_pool_entry *pool_entry, *tmp;
2916
2917         QETH_DBF_TEXT(trace,5,"clwrklst");
2918         list_for_each_entry_safe(pool_entry, tmp,
2919                             &card->qdio.in_buf_pool.entry_list, list){
2920                         list_del(&pool_entry->list);
2921         }
2922 }
2923
2924 static void
2925 qeth_free_buffer_pool(struct qeth_card *card)
2926 {
2927         struct qeth_buffer_pool_entry *pool_entry, *tmp;
2928         int i=0;
2929         QETH_DBF_TEXT(trace,5,"freepool");
2930         list_for_each_entry_safe(pool_entry, tmp,
2931                                  &card->qdio.init_pool.entry_list, init_list){
2932                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
2933                         free_page((unsigned long)pool_entry->elements[i]);
2934                 list_del(&pool_entry->init_list);
2935                 kfree(pool_entry);
2936         }
2937 }
2938
2939 static int
2940 qeth_alloc_buffer_pool(struct qeth_card *card)
2941 {
2942         struct qeth_buffer_pool_entry *pool_entry;
2943         void *ptr;
2944         int i, j;
2945
2946         QETH_DBF_TEXT(trace,5,"alocpool");
2947         for (i = 0; i < card->qdio.init_pool.buf_count; ++i){
2948                 pool_entry = kmalloc(sizeof(*pool_entry), GFP_KERNEL);
2949                 if (!pool_entry){
2950                         qeth_free_buffer_pool(card);
2951                         return -ENOMEM;
2952                 }
2953                 for(j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j){
2954                         ptr = (void *) __get_free_page(GFP_KERNEL);
2955                         if (!ptr) {
2956                                 while (j > 0)
2957                                         free_page((unsigned long)
2958                                                   pool_entry->elements[--j]);
2959                                 kfree(pool_entry);
2960                                 qeth_free_buffer_pool(card);
2961                                 return -ENOMEM;
2962                         }
2963                         pool_entry->elements[j] = ptr;
2964                 }
2965                 list_add(&pool_entry->init_list,
2966                          &card->qdio.init_pool.entry_list);
2967         }
2968         return 0;
2969 }
2970
2971 int
2972 qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
2973 {
2974         QETH_DBF_TEXT(trace, 2, "realcbp");
2975
2976         if ((card->state != CARD_STATE_DOWN) &&
2977             (card->state != CARD_STATE_RECOVER))
2978                 return -EPERM;
2979
2980         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
2981         qeth_clear_working_pool_list(card);
2982         qeth_free_buffer_pool(card);
2983         card->qdio.in_buf_pool.buf_count = bufcnt;
2984         card->qdio.init_pool.buf_count = bufcnt;
2985         return qeth_alloc_buffer_pool(card);
2986 }
2987
2988 static int
2989 qeth_alloc_qdio_buffers(struct qeth_card *card)
2990 {
2991         int i, j;
2992
2993         QETH_DBF_TEXT(setup, 2, "allcqdbf");
2994
2995         if (card->qdio.state == QETH_QDIO_ALLOCATED)
2996                 return 0;
2997
2998         card->qdio.in_q = kmalloc(sizeof(struct qeth_qdio_q), GFP_KERNEL);
2999         if (!card->qdio.in_q)
3000                 return - ENOMEM;
3001         QETH_DBF_TEXT(setup, 2, "inq");
3002         QETH_DBF_HEX(setup, 2, &card->qdio.in_q, sizeof(void *));
3003         memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
3004         /* give inbound qeth_qdio_buffers their qdio_buffers */
3005         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3006                 card->qdio.in_q->bufs[i].buffer =
3007                         &card->qdio.in_q->qdio_bufs[i];
3008         /* inbound buffer pool */
3009         if (qeth_alloc_buffer_pool(card)){
3010                 kfree(card->qdio.in_q);
3011                 return -ENOMEM;
3012         }
3013         /* outbound */
3014         card->qdio.out_qs =
3015                 kmalloc(card->qdio.no_out_queues *
3016                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
3017         if (!card->qdio.out_qs){
3018                 qeth_free_buffer_pool(card);
3019                 return -ENOMEM;
3020         }
3021         for (i = 0; i < card->qdio.no_out_queues; ++i){
3022                 card->qdio.out_qs[i] = kmalloc(sizeof(struct qeth_qdio_out_q),
3023                                                GFP_KERNEL);
3024                 if (!card->qdio.out_qs[i]){
3025                         while (i > 0)
3026                                 kfree(card->qdio.out_qs[--i]);
3027                         kfree(card->qdio.out_qs);
3028                         return -ENOMEM;
3029                 }
3030                 QETH_DBF_TEXT_(setup, 2, "outq %i", i);
3031                 QETH_DBF_HEX(setup, 2, &card->qdio.out_qs[i], sizeof(void *));
3032                 memset(card->qdio.out_qs[i], 0, sizeof(struct qeth_qdio_out_q));
3033                 card->qdio.out_qs[i]->queue_no = i;
3034                 /* give outbound qeth_qdio_buffers their qdio_buffers */
3035                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3036                         card->qdio.out_qs[i]->bufs[j].buffer =
3037                                 &card->qdio.out_qs[i]->qdio_bufs[j];
3038                         skb_queue_head_init(&card->qdio.out_qs[i]->bufs[j].
3039                                             skb_list);
3040                         INIT_LIST_HEAD(&card->qdio.out_qs[i]->bufs[j].ctx_list);
3041                 }
3042         }
3043         card->qdio.state = QETH_QDIO_ALLOCATED;
3044         return 0;
3045 }
3046
3047 static void
3048 qeth_free_qdio_buffers(struct qeth_card *card)
3049 {
3050         int i, j;
3051
3052         QETH_DBF_TEXT(trace, 2, "freeqdbf");
3053         if (card->qdio.state == QETH_QDIO_UNINITIALIZED)
3054                 return;
3055         kfree(card->qdio.in_q);
3056         /* inbound buffer pool */
3057         qeth_free_buffer_pool(card);
3058         /* free outbound qdio_qs */
3059         for (i = 0; i < card->qdio.no_out_queues; ++i){
3060                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3061                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3062                                         &card->qdio.out_qs[i]->bufs[j]);
3063                 kfree(card->qdio.out_qs[i]);
3064         }
3065         kfree(card->qdio.out_qs);
3066         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3067 }
3068
3069 static void
3070 qeth_clear_qdio_buffers(struct qeth_card *card)
3071 {
3072         int i, j;
3073
3074         QETH_DBF_TEXT(trace, 2, "clearqdbf");
3075         /* clear outbound buffers to free skbs */
3076         for (i = 0; i < card->qdio.no_out_queues; ++i)
3077                 if (card->qdio.out_qs[i]){
3078                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
3079                                 qeth_clear_output_buffer(card->qdio.out_qs[i],
3080                                                 &card->qdio.out_qs[i]->bufs[j]);
3081                 }
3082 }
3083
3084 static void
3085 qeth_init_qdio_info(struct qeth_card *card)
3086 {
3087         QETH_DBF_TEXT(setup, 4, "intqdinf");
3088         card->qdio.state = QETH_QDIO_UNINITIALIZED;
3089         /* inbound */
3090         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
3091         card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
3092         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
3093         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
3094         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
3095         /* outbound */
3096         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
3097         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
3098 }
3099
3100 static int
3101 qeth_init_qdio_queues(struct qeth_card *card)
3102 {
3103         int i, j;
3104         int rc;
3105
3106         QETH_DBF_TEXT(setup, 2, "initqdqs");
3107
3108         /* inbound queue */
3109         memset(card->qdio.in_q->qdio_bufs, 0,
3110                QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3111         qeth_initialize_working_pool_list(card);
3112         /*give only as many buffers to hardware as we have buffer pool entries*/
3113         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
3114                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
3115         card->qdio.in_q->next_buf_to_init = card->qdio.in_buf_pool.buf_count - 1;
3116         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
3117                      card->qdio.in_buf_pool.buf_count - 1, NULL);
3118         if (rc) {
3119                 QETH_DBF_TEXT_(setup, 2, "1err%d", rc);
3120                 return rc;
3121         }
3122         rc = qdio_synchronize(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0);
3123         if (rc) {
3124                 QETH_DBF_TEXT_(setup, 2, "2err%d", rc);
3125                 return rc;
3126         }
3127         /* outbound queue */
3128         for (i = 0; i < card->qdio.no_out_queues; ++i){
3129                 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
3130                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
3131                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j){
3132                         qeth_clear_output_buffer(card->qdio.out_qs[i],
3133                                         &card->qdio.out_qs[i]->bufs[j]);
3134                 }
3135                 card->qdio.out_qs[i]->card = card;
3136                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
3137                 card->qdio.out_qs[i]->do_pack = 0;
3138                 atomic_set(&card->qdio.out_qs[i]->used_buffers,0);
3139                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
3140                 atomic_set(&card->qdio.out_qs[i]->state,
3141                            QETH_OUT_Q_UNLOCKED);
3142         }
3143         return 0;
3144 }
3145
3146 static int
3147 qeth_qdio_establish(struct qeth_card *card)
3148 {
3149         struct qdio_initialize init_data;
3150         char *qib_param_field;
3151         struct qdio_buffer **in_sbal_ptrs;
3152         struct qdio_buffer **out_sbal_ptrs;
3153         int i, j, k;
3154         int rc;
3155
3156         QETH_DBF_TEXT(setup, 2, "qdioest");
3157
3158         qib_param_field = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
3159                               GFP_KERNEL);
3160         if (!qib_param_field)
3161                 return -ENOMEM;
3162
3163         memset(qib_param_field, 0, QDIO_MAX_BUFFERS_PER_Q * sizeof(char));
3164
3165         qeth_create_qib_param_field(card, qib_param_field);
3166         qeth_create_qib_param_field_blkt(card, qib_param_field);
3167
3168         in_sbal_ptrs = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
3169                                GFP_KERNEL);
3170         if (!in_sbal_ptrs) {
3171                 kfree(qib_param_field);
3172                 return -ENOMEM;
3173         }
3174         for(i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3175                 in_sbal_ptrs[i] = (struct qdio_buffer *)
3176                         virt_to_phys(card->qdio.in_q->bufs[i].buffer);
3177
3178         out_sbal_ptrs =
3179                 kmalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
3180                         sizeof(void *), GFP_KERNEL);
3181         if (!out_sbal_ptrs) {
3182                 kfree(in_sbal_ptrs);
3183                 kfree(qib_param_field);
3184                 return -ENOMEM;
3185         }
3186         for(i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
3187                 for(j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k){
3188                         out_sbal_ptrs[k] = (struct qdio_buffer *)
3189                                 virt_to_phys(card->qdio.out_qs[i]->
3190                                              bufs[j].buffer);
3191                 }
3192
3193         memset(&init_data, 0, sizeof(struct qdio_initialize));
3194         init_data.cdev                   = CARD_DDEV(card);
3195         init_data.q_format               = qeth_get_qdio_q_format(card);
3196         init_data.qib_param_field_format = 0;
3197         init_data.qib_param_field        = qib_param_field;
3198         init_data.min_input_threshold    = QETH_MIN_INPUT_THRESHOLD;
3199         init_data.max_input_threshold    = QETH_MAX_INPUT_THRESHOLD;
3200         init_data.min_output_threshold   = QETH_MIN_OUTPUT_THRESHOLD;
3201         init_data.max_output_threshold   = QETH_MAX_OUTPUT_THRESHOLD;
3202         init_data.no_input_qs            = 1;
3203         init_data.no_output_qs           = card->qdio.no_out_queues;
3204         init_data.input_handler          = (qdio_handler_t *)
3205                                            qeth_qdio_input_handler;
3206         init_data.output_handler         = (qdio_handler_t *)
3207                                            qeth_qdio_output_handler;
3208         init_data.int_parm               = (unsigned long) card;
3209         init_data.flags                  = QDIO_INBOUND_0COPY_SBALS |
3210                                            QDIO_OUTBOUND_0COPY_SBALS |
3211                                            QDIO_USE_OUTBOUND_PCIS;
3212         init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
3213         init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
3214
3215         if (!(rc = qdio_initialize(&init_data)))
3216                 card->qdio.state = QETH_QDIO_ESTABLISHED;
3217
3218         kfree(out_sbal_ptrs);
3219         kfree(in_sbal_ptrs);
3220         kfree(qib_param_field);
3221         return rc;
3222 }
3223
3224 static int
3225 qeth_qdio_activate(struct qeth_card *card)
3226 {
3227         QETH_DBF_TEXT(setup,3,"qdioact");
3228         return qdio_activate(CARD_DDEV(card), 0);
3229 }
3230
3231 static int
3232 qeth_clear_channel(struct qeth_channel *channel)
3233 {
3234         unsigned long flags;
3235         struct qeth_card *card;
3236         int rc;
3237
3238         QETH_DBF_TEXT(trace,3,"clearch");
3239         card = CARD_FROM_CDEV(channel->ccwdev);
3240         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
3241         rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
3242         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
3243
3244         if (rc)
3245                 return rc;
3246         rc = wait_event_interruptible_timeout(card->wait_q,
3247                         channel->state==CH_STATE_STOPPED, QETH_TIMEOUT);
3248         if (rc == -ERESTARTSYS)