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