qeth: do not apply priority queuing to HiperSockets
[linux-2.6.git] / drivers / s390 / net / qeth_core_main.c
1 /*
2  *  drivers/s390/net/qeth_core_main.c
3  *
4  *    Copyright IBM Corp. 2007, 2009
5  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6  *               Frank Pavlic <fpavlic@de.ibm.com>,
7  *               Thomas Spatzier <tspat@de.ibm.com>,
8  *               Frank Blaschka <frank.blaschka@de.ibm.com>
9  */
10
11 #define KMSG_COMPONENT "qeth"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/ip.h>
20 #include <linux/tcp.h>
21 #include <linux/mii.h>
22 #include <linux/kthread.h>
23 #include <linux/slab.h>
24
25 #include <asm/ebcdic.h>
26 #include <asm/io.h>
27 #include <asm/sysinfo.h>
28
29 #include "qeth_core.h"
30
31 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
32         /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
33         /*                   N  P  A    M  L  V                      H  */
34         [QETH_DBF_SETUP] = {"qeth_setup",
35                                 8, 1,   8, 5, &debug_hex_ascii_view, NULL},
36         [QETH_DBF_MSG]   = {"qeth_msg",
37                                 8, 1, 128, 3, &debug_sprintf_view,   NULL},
38         [QETH_DBF_CTRL]  = {"qeth_control",
39                 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
40 };
41 EXPORT_SYMBOL_GPL(qeth_dbf);
42
43 struct qeth_card_list_struct qeth_core_card_list;
44 EXPORT_SYMBOL_GPL(qeth_core_card_list);
45 struct kmem_cache *qeth_core_header_cache;
46 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
47
48 static struct device *qeth_core_root_dev;
49 static unsigned int known_devices[][6] = QETH_MODELLIST_ARRAY;
50 static struct lock_class_key qdio_out_skb_queue_key;
51
52 static void qeth_send_control_data_cb(struct qeth_channel *,
53                         struct qeth_cmd_buffer *);
54 static int qeth_issue_next_read(struct qeth_card *);
55 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
56 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
57 static void qeth_free_buffer_pool(struct qeth_card *);
58 static int qeth_qdio_establish(struct qeth_card *);
59
60
61 static inline const char *qeth_get_cardname(struct qeth_card *card)
62 {
63         if (card->info.guestlan) {
64                 switch (card->info.type) {
65                 case QETH_CARD_TYPE_OSD:
66                         return " Guest LAN QDIO";
67                 case QETH_CARD_TYPE_IQD:
68                         return " Guest LAN Hiper";
69                 case QETH_CARD_TYPE_OSM:
70                         return " Guest LAN QDIO - OSM";
71                 case QETH_CARD_TYPE_OSX:
72                         return " Guest LAN QDIO - OSX";
73                 default:
74                         return " unknown";
75                 }
76         } else {
77                 switch (card->info.type) {
78                 case QETH_CARD_TYPE_OSD:
79                         return " OSD Express";
80                 case QETH_CARD_TYPE_IQD:
81                         return " HiperSockets";
82                 case QETH_CARD_TYPE_OSN:
83                         return " OSN QDIO";
84                 case QETH_CARD_TYPE_OSM:
85                         return " OSM QDIO";
86                 case QETH_CARD_TYPE_OSX:
87                         return " OSX QDIO";
88                 default:
89                         return " unknown";
90                 }
91         }
92         return " n/a";
93 }
94
95 /* max length to be returned: 14 */
96 const char *qeth_get_cardname_short(struct qeth_card *card)
97 {
98         if (card->info.guestlan) {
99                 switch (card->info.type) {
100                 case QETH_CARD_TYPE_OSD:
101                         return "GuestLAN QDIO";
102                 case QETH_CARD_TYPE_IQD:
103                         return "GuestLAN Hiper";
104                 case QETH_CARD_TYPE_OSM:
105                         return "GuestLAN OSM";
106                 case QETH_CARD_TYPE_OSX:
107                         return "GuestLAN OSX";
108                 default:
109                         return "unknown";
110                 }
111         } else {
112                 switch (card->info.type) {
113                 case QETH_CARD_TYPE_OSD:
114                         switch (card->info.link_type) {
115                         case QETH_LINK_TYPE_FAST_ETH:
116                                 return "OSD_100";
117                         case QETH_LINK_TYPE_HSTR:
118                                 return "HSTR";
119                         case QETH_LINK_TYPE_GBIT_ETH:
120                                 return "OSD_1000";
121                         case QETH_LINK_TYPE_10GBIT_ETH:
122                                 return "OSD_10GIG";
123                         case QETH_LINK_TYPE_LANE_ETH100:
124                                 return "OSD_FE_LANE";
125                         case QETH_LINK_TYPE_LANE_TR:
126                                 return "OSD_TR_LANE";
127                         case QETH_LINK_TYPE_LANE_ETH1000:
128                                 return "OSD_GbE_LANE";
129                         case QETH_LINK_TYPE_LANE:
130                                 return "OSD_ATM_LANE";
131                         default:
132                                 return "OSD_Express";
133                         }
134                 case QETH_CARD_TYPE_IQD:
135                         return "HiperSockets";
136                 case QETH_CARD_TYPE_OSN:
137                         return "OSN";
138                 case QETH_CARD_TYPE_OSM:
139                         return "OSM_1000";
140                 case QETH_CARD_TYPE_OSX:
141                         return "OSX_10GIG";
142                 default:
143                         return "unknown";
144                 }
145         }
146         return "n/a";
147 }
148
149 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
150                          int clear_start_mask)
151 {
152         unsigned long flags;
153
154         spin_lock_irqsave(&card->thread_mask_lock, flags);
155         card->thread_allowed_mask = threads;
156         if (clear_start_mask)
157                 card->thread_start_mask &= threads;
158         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
159         wake_up(&card->wait_q);
160 }
161 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
162
163 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
164 {
165         unsigned long flags;
166         int rc = 0;
167
168         spin_lock_irqsave(&card->thread_mask_lock, flags);
169         rc = (card->thread_running_mask & threads);
170         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
171         return rc;
172 }
173 EXPORT_SYMBOL_GPL(qeth_threads_running);
174
175 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
176 {
177         return wait_event_interruptible(card->wait_q,
178                         qeth_threads_running(card, threads) == 0);
179 }
180 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
181
182 void qeth_clear_working_pool_list(struct qeth_card *card)
183 {
184         struct qeth_buffer_pool_entry *pool_entry, *tmp;
185
186         QETH_CARD_TEXT(card, 5, "clwrklst");
187         list_for_each_entry_safe(pool_entry, tmp,
188                             &card->qdio.in_buf_pool.entry_list, list){
189                         list_del(&pool_entry->list);
190         }
191 }
192 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
193
194 static int qeth_alloc_buffer_pool(struct qeth_card *card)
195 {
196         struct qeth_buffer_pool_entry *pool_entry;
197         void *ptr;
198         int i, j;
199
200         QETH_CARD_TEXT(card, 5, "alocpool");
201         for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
202                 pool_entry = kmalloc(sizeof(*pool_entry), GFP_KERNEL);
203                 if (!pool_entry) {
204                         qeth_free_buffer_pool(card);
205                         return -ENOMEM;
206                 }
207                 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
208                         ptr = (void *) __get_free_page(GFP_KERNEL);
209                         if (!ptr) {
210                                 while (j > 0)
211                                         free_page((unsigned long)
212                                                   pool_entry->elements[--j]);
213                                 kfree(pool_entry);
214                                 qeth_free_buffer_pool(card);
215                                 return -ENOMEM;
216                         }
217                         pool_entry->elements[j] = ptr;
218                 }
219                 list_add(&pool_entry->init_list,
220                          &card->qdio.init_pool.entry_list);
221         }
222         return 0;
223 }
224
225 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
226 {
227         QETH_CARD_TEXT(card, 2, "realcbp");
228
229         if ((card->state != CARD_STATE_DOWN) &&
230             (card->state != CARD_STATE_RECOVER))
231                 return -EPERM;
232
233         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
234         qeth_clear_working_pool_list(card);
235         qeth_free_buffer_pool(card);
236         card->qdio.in_buf_pool.buf_count = bufcnt;
237         card->qdio.init_pool.buf_count = bufcnt;
238         return qeth_alloc_buffer_pool(card);
239 }
240 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
241
242 static int qeth_issue_next_read(struct qeth_card *card)
243 {
244         int rc;
245         struct qeth_cmd_buffer *iob;
246
247         QETH_CARD_TEXT(card, 5, "issnxrd");
248         if (card->read.state != CH_STATE_UP)
249                 return -EIO;
250         iob = qeth_get_buffer(&card->read);
251         if (!iob) {
252                 dev_warn(&card->gdev->dev, "The qeth device driver "
253                         "failed to recover an error on the device\n");
254                 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
255                         "available\n", dev_name(&card->gdev->dev));
256                 return -ENOMEM;
257         }
258         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
259         QETH_CARD_TEXT(card, 6, "noirqpnd");
260         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
261                               (addr_t) iob, 0, 0);
262         if (rc) {
263                 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
264                         "rc=%i\n", dev_name(&card->gdev->dev), rc);
265                 atomic_set(&card->read.irq_pending, 0);
266                 card->read_or_write_problem = 1;
267                 qeth_schedule_recovery(card);
268                 wake_up(&card->wait_q);
269         }
270         return rc;
271 }
272
273 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
274 {
275         struct qeth_reply *reply;
276
277         reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
278         if (reply) {
279                 atomic_set(&reply->refcnt, 1);
280                 atomic_set(&reply->received, 0);
281                 reply->card = card;
282         };
283         return reply;
284 }
285
286 static void qeth_get_reply(struct qeth_reply *reply)
287 {
288         WARN_ON(atomic_read(&reply->refcnt) <= 0);
289         atomic_inc(&reply->refcnt);
290 }
291
292 static void qeth_put_reply(struct qeth_reply *reply)
293 {
294         WARN_ON(atomic_read(&reply->refcnt) <= 0);
295         if (atomic_dec_and_test(&reply->refcnt))
296                 kfree(reply);
297 }
298
299 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
300                 struct qeth_card *card)
301 {
302         char *ipa_name;
303         int com = cmd->hdr.command;
304         ipa_name = qeth_get_ipa_cmd_name(com);
305         if (rc)
306                 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
307                                 "x%X \"%s\"\n",
308                                 ipa_name, com, dev_name(&card->gdev->dev),
309                                 QETH_CARD_IFNAME(card), rc,
310                                 qeth_get_ipa_msg(rc));
311         else
312                 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
313                                 ipa_name, com, dev_name(&card->gdev->dev),
314                                 QETH_CARD_IFNAME(card));
315 }
316
317 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
318                 struct qeth_cmd_buffer *iob)
319 {
320         struct qeth_ipa_cmd *cmd = NULL;
321
322         QETH_CARD_TEXT(card, 5, "chkipad");
323         if (IS_IPA(iob->data)) {
324                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
325                 if (IS_IPA_REPLY(cmd)) {
326                         if (cmd->hdr.command != IPA_CMD_SETCCID &&
327                             cmd->hdr.command != IPA_CMD_DELCCID &&
328                             cmd->hdr.command != IPA_CMD_MODCCID &&
329                             cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
330                                 qeth_issue_ipa_msg(cmd,
331                                                 cmd->hdr.return_code, card);
332                         return cmd;
333                 } else {
334                         switch (cmd->hdr.command) {
335                         case IPA_CMD_STOPLAN:
336                                 dev_warn(&card->gdev->dev,
337                                            "The link for interface %s on CHPID"
338                                            " 0x%X failed\n",
339                                            QETH_CARD_IFNAME(card),
340                                            card->info.chpid);
341                                 card->lan_online = 0;
342                                 if (card->dev && netif_carrier_ok(card->dev))
343                                         netif_carrier_off(card->dev);
344                                 return NULL;
345                         case IPA_CMD_STARTLAN:
346                                 dev_info(&card->gdev->dev,
347                                            "The link for %s on CHPID 0x%X has"
348                                            " been restored\n",
349                                            QETH_CARD_IFNAME(card),
350                                            card->info.chpid);
351                                 netif_carrier_on(card->dev);
352                                 card->lan_online = 1;
353                                 if (card->info.hwtrap)
354                                         card->info.hwtrap = 2;
355                                 qeth_schedule_recovery(card);
356                                 return NULL;
357                         case IPA_CMD_MODCCID:
358                                 return cmd;
359                         case IPA_CMD_REGISTER_LOCAL_ADDR:
360                                 QETH_CARD_TEXT(card, 3, "irla");
361                                 break;
362                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
363                                 QETH_CARD_TEXT(card, 3, "urla");
364                                 break;
365                         default:
366                                 QETH_DBF_MESSAGE(2, "Received data is IPA "
367                                            "but not a reply!\n");
368                                 break;
369                         }
370                 }
371         }
372         return cmd;
373 }
374
375 void qeth_clear_ipacmd_list(struct qeth_card *card)
376 {
377         struct qeth_reply *reply, *r;
378         unsigned long flags;
379
380         QETH_CARD_TEXT(card, 4, "clipalst");
381
382         spin_lock_irqsave(&card->lock, flags);
383         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
384                 qeth_get_reply(reply);
385                 reply->rc = -EIO;
386                 atomic_inc(&reply->received);
387                 list_del_init(&reply->list);
388                 wake_up(&reply->wait_q);
389                 qeth_put_reply(reply);
390         }
391         spin_unlock_irqrestore(&card->lock, flags);
392         atomic_set(&card->write.irq_pending, 0);
393 }
394 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
395
396 static int qeth_check_idx_response(struct qeth_card *card,
397         unsigned char *buffer)
398 {
399         if (!buffer)
400                 return 0;
401
402         QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
403         if ((buffer[2] & 0xc0) == 0xc0) {
404                 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
405                            "with cause code 0x%02x%s\n",
406                            buffer[4],
407                            ((buffer[4] == 0x22) ?
408                             " -- try another portname" : ""));
409                 QETH_CARD_TEXT(card, 2, "ckidxres");
410                 QETH_CARD_TEXT(card, 2, " idxterm");
411                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
412                 if (buffer[4] == 0xf6) {
413                         dev_err(&card->gdev->dev,
414                         "The qeth device is not configured "
415                         "for the OSI layer required by z/VM\n");
416                         return -EPERM;
417                 }
418                 return -EIO;
419         }
420         return 0;
421 }
422
423 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
424                 __u32 len)
425 {
426         struct qeth_card *card;
427
428         card = CARD_FROM_CDEV(channel->ccwdev);
429         QETH_CARD_TEXT(card, 4, "setupccw");
430         if (channel == &card->read)
431                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
432         else
433                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
434         channel->ccw.count = len;
435         channel->ccw.cda = (__u32) __pa(iob);
436 }
437
438 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
439 {
440         __u8 index;
441
442         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
443         index = channel->io_buf_no;
444         do {
445                 if (channel->iob[index].state == BUF_STATE_FREE) {
446                         channel->iob[index].state = BUF_STATE_LOCKED;
447                         channel->io_buf_no = (channel->io_buf_no + 1) %
448                                 QETH_CMD_BUFFER_NO;
449                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
450                         return channel->iob + index;
451                 }
452                 index = (index + 1) % QETH_CMD_BUFFER_NO;
453         } while (index != channel->io_buf_no);
454
455         return NULL;
456 }
457
458 void qeth_release_buffer(struct qeth_channel *channel,
459                 struct qeth_cmd_buffer *iob)
460 {
461         unsigned long flags;
462
463         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
464         spin_lock_irqsave(&channel->iob_lock, flags);
465         memset(iob->data, 0, QETH_BUFSIZE);
466         iob->state = BUF_STATE_FREE;
467         iob->callback = qeth_send_control_data_cb;
468         iob->rc = 0;
469         spin_unlock_irqrestore(&channel->iob_lock, flags);
470 }
471 EXPORT_SYMBOL_GPL(qeth_release_buffer);
472
473 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
474 {
475         struct qeth_cmd_buffer *buffer = NULL;
476         unsigned long flags;
477
478         spin_lock_irqsave(&channel->iob_lock, flags);
479         buffer = __qeth_get_buffer(channel);
480         spin_unlock_irqrestore(&channel->iob_lock, flags);
481         return buffer;
482 }
483
484 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
485 {
486         struct qeth_cmd_buffer *buffer;
487         wait_event(channel->wait_q,
488                    ((buffer = qeth_get_buffer(channel)) != NULL));
489         return buffer;
490 }
491 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
492
493 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
494 {
495         int cnt;
496
497         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
498                 qeth_release_buffer(channel, &channel->iob[cnt]);
499         channel->buf_no = 0;
500         channel->io_buf_no = 0;
501 }
502 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
503
504 static void qeth_send_control_data_cb(struct qeth_channel *channel,
505                   struct qeth_cmd_buffer *iob)
506 {
507         struct qeth_card *card;
508         struct qeth_reply *reply, *r;
509         struct qeth_ipa_cmd *cmd;
510         unsigned long flags;
511         int keep_reply;
512         int rc = 0;
513
514         card = CARD_FROM_CDEV(channel->ccwdev);
515         QETH_CARD_TEXT(card, 4, "sndctlcb");
516         rc = qeth_check_idx_response(card, iob->data);
517         switch (rc) {
518         case 0:
519                 break;
520         case -EIO:
521                 qeth_clear_ipacmd_list(card);
522                 qeth_schedule_recovery(card);
523                 /* fall through */
524         default:
525                 goto out;
526         }
527
528         cmd = qeth_check_ipa_data(card, iob);
529         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
530                 goto out;
531         /*in case of OSN : check if cmd is set */
532         if (card->info.type == QETH_CARD_TYPE_OSN &&
533             cmd &&
534             cmd->hdr.command != IPA_CMD_STARTLAN &&
535             card->osn_info.assist_cb != NULL) {
536                 card->osn_info.assist_cb(card->dev, cmd);
537                 goto out;
538         }
539
540         spin_lock_irqsave(&card->lock, flags);
541         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
542                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
543                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
544                         qeth_get_reply(reply);
545                         list_del_init(&reply->list);
546                         spin_unlock_irqrestore(&card->lock, flags);
547                         keep_reply = 0;
548                         if (reply->callback != NULL) {
549                                 if (cmd) {
550                                         reply->offset = (__u16)((char *)cmd -
551                                                         (char *)iob->data);
552                                         keep_reply = reply->callback(card,
553                                                         reply,
554                                                         (unsigned long)cmd);
555                                 } else
556                                         keep_reply = reply->callback(card,
557                                                         reply,
558                                                         (unsigned long)iob);
559                         }
560                         if (cmd)
561                                 reply->rc = (u16) cmd->hdr.return_code;
562                         else if (iob->rc)
563                                 reply->rc = iob->rc;
564                         if (keep_reply) {
565                                 spin_lock_irqsave(&card->lock, flags);
566                                 list_add_tail(&reply->list,
567                                               &card->cmd_waiter_list);
568                                 spin_unlock_irqrestore(&card->lock, flags);
569                         } else {
570                                 atomic_inc(&reply->received);
571                                 wake_up(&reply->wait_q);
572                         }
573                         qeth_put_reply(reply);
574                         goto out;
575                 }
576         }
577         spin_unlock_irqrestore(&card->lock, flags);
578 out:
579         memcpy(&card->seqno.pdu_hdr_ack,
580                 QETH_PDU_HEADER_SEQ_NO(iob->data),
581                 QETH_SEQ_NO_LENGTH);
582         qeth_release_buffer(channel, iob);
583 }
584
585 static int qeth_setup_channel(struct qeth_channel *channel)
586 {
587         int cnt;
588
589         QETH_DBF_TEXT(SETUP, 2, "setupch");
590         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
591                 channel->iob[cnt].data =
592                         kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
593                 if (channel->iob[cnt].data == NULL)
594                         break;
595                 channel->iob[cnt].state = BUF_STATE_FREE;
596                 channel->iob[cnt].channel = channel;
597                 channel->iob[cnt].callback = qeth_send_control_data_cb;
598                 channel->iob[cnt].rc = 0;
599         }
600         if (cnt < QETH_CMD_BUFFER_NO) {
601                 while (cnt-- > 0)
602                         kfree(channel->iob[cnt].data);
603                 return -ENOMEM;
604         }
605         channel->buf_no = 0;
606         channel->io_buf_no = 0;
607         atomic_set(&channel->irq_pending, 0);
608         spin_lock_init(&channel->iob_lock);
609
610         init_waitqueue_head(&channel->wait_q);
611         return 0;
612 }
613
614 static int qeth_set_thread_start_bit(struct qeth_card *card,
615                 unsigned long thread)
616 {
617         unsigned long flags;
618
619         spin_lock_irqsave(&card->thread_mask_lock, flags);
620         if (!(card->thread_allowed_mask & thread) ||
621               (card->thread_start_mask & thread)) {
622                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
623                 return -EPERM;
624         }
625         card->thread_start_mask |= thread;
626         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
627         return 0;
628 }
629
630 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
631 {
632         unsigned long flags;
633
634         spin_lock_irqsave(&card->thread_mask_lock, flags);
635         card->thread_start_mask &= ~thread;
636         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
637         wake_up(&card->wait_q);
638 }
639 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
640
641 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
642 {
643         unsigned long flags;
644
645         spin_lock_irqsave(&card->thread_mask_lock, flags);
646         card->thread_running_mask &= ~thread;
647         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
648         wake_up(&card->wait_q);
649 }
650 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
651
652 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
653 {
654         unsigned long flags;
655         int rc = 0;
656
657         spin_lock_irqsave(&card->thread_mask_lock, flags);
658         if (card->thread_start_mask & thread) {
659                 if ((card->thread_allowed_mask & thread) &&
660                     !(card->thread_running_mask & thread)) {
661                         rc = 1;
662                         card->thread_start_mask &= ~thread;
663                         card->thread_running_mask |= thread;
664                 } else
665                         rc = -EPERM;
666         }
667         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
668         return rc;
669 }
670
671 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
672 {
673         int rc = 0;
674
675         wait_event(card->wait_q,
676                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
677         return rc;
678 }
679 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
680
681 void qeth_schedule_recovery(struct qeth_card *card)
682 {
683         QETH_CARD_TEXT(card, 2, "startrec");
684         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
685                 schedule_work(&card->kernel_thread_starter);
686 }
687 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
688
689 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
690 {
691         int dstat, cstat;
692         char *sense;
693         struct qeth_card *card;
694
695         sense = (char *) irb->ecw;
696         cstat = irb->scsw.cmd.cstat;
697         dstat = irb->scsw.cmd.dstat;
698         card = CARD_FROM_CDEV(cdev);
699
700         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
701                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
702                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
703                 QETH_CARD_TEXT(card, 2, "CGENCHK");
704                 dev_warn(&cdev->dev, "The qeth device driver "
705                         "failed to recover an error on the device\n");
706                 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
707                         dev_name(&cdev->dev), dstat, cstat);
708                 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
709                                 16, 1, irb, 64, 1);
710                 return 1;
711         }
712
713         if (dstat & DEV_STAT_UNIT_CHECK) {
714                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
715                     SENSE_RESETTING_EVENT_FLAG) {
716                         QETH_CARD_TEXT(card, 2, "REVIND");
717                         return 1;
718                 }
719                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
720                     SENSE_COMMAND_REJECT_FLAG) {
721                         QETH_CARD_TEXT(card, 2, "CMDREJi");
722                         return 1;
723                 }
724                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
725                         QETH_CARD_TEXT(card, 2, "AFFE");
726                         return 1;
727                 }
728                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
729                         QETH_CARD_TEXT(card, 2, "ZEROSEN");
730                         return 0;
731                 }
732                 QETH_CARD_TEXT(card, 2, "DGENCHK");
733                         return 1;
734         }
735         return 0;
736 }
737
738 static long __qeth_check_irb_error(struct ccw_device *cdev,
739                 unsigned long intparm, struct irb *irb)
740 {
741         struct qeth_card *card;
742
743         card = CARD_FROM_CDEV(cdev);
744
745         if (!IS_ERR(irb))
746                 return 0;
747
748         switch (PTR_ERR(irb)) {
749         case -EIO:
750                 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
751                         dev_name(&cdev->dev));
752                 QETH_CARD_TEXT(card, 2, "ckirberr");
753                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
754                 break;
755         case -ETIMEDOUT:
756                 dev_warn(&cdev->dev, "A hardware operation timed out"
757                         " on the device\n");
758                 QETH_CARD_TEXT(card, 2, "ckirberr");
759                 QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
760                 if (intparm == QETH_RCD_PARM) {
761                         if (card && (card->data.ccwdev == cdev)) {
762                                 card->data.state = CH_STATE_DOWN;
763                                 wake_up(&card->wait_q);
764                         }
765                 }
766                 break;
767         default:
768                 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
769                         dev_name(&cdev->dev), PTR_ERR(irb));
770                 QETH_CARD_TEXT(card, 2, "ckirberr");
771                 QETH_CARD_TEXT(card, 2, "  rc???");
772         }
773         return PTR_ERR(irb);
774 }
775
776 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
777                 struct irb *irb)
778 {
779         int rc;
780         int cstat, dstat;
781         struct qeth_cmd_buffer *buffer;
782         struct qeth_channel *channel;
783         struct qeth_card *card;
784         struct qeth_cmd_buffer *iob;
785         __u8 index;
786
787         if (__qeth_check_irb_error(cdev, intparm, irb))
788                 return;
789         cstat = irb->scsw.cmd.cstat;
790         dstat = irb->scsw.cmd.dstat;
791
792         card = CARD_FROM_CDEV(cdev);
793         if (!card)
794                 return;
795
796         QETH_CARD_TEXT(card, 5, "irq");
797
798         if (card->read.ccwdev == cdev) {
799                 channel = &card->read;
800                 QETH_CARD_TEXT(card, 5, "read");
801         } else if (card->write.ccwdev == cdev) {
802                 channel = &card->write;
803                 QETH_CARD_TEXT(card, 5, "write");
804         } else {
805                 channel = &card->data;
806                 QETH_CARD_TEXT(card, 5, "data");
807         }
808         atomic_set(&channel->irq_pending, 0);
809
810         if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
811                 channel->state = CH_STATE_STOPPED;
812
813         if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
814                 channel->state = CH_STATE_HALTED;
815
816         /*let's wake up immediately on data channel*/
817         if ((channel == &card->data) && (intparm != 0) &&
818             (intparm != QETH_RCD_PARM))
819                 goto out;
820
821         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
822                 QETH_CARD_TEXT(card, 6, "clrchpar");
823                 /* we don't have to handle this further */
824                 intparm = 0;
825         }
826         if (intparm == QETH_HALT_CHANNEL_PARM) {
827                 QETH_CARD_TEXT(card, 6, "hltchpar");
828                 /* we don't have to handle this further */
829                 intparm = 0;
830         }
831         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
832             (dstat & DEV_STAT_UNIT_CHECK) ||
833             (cstat)) {
834                 if (irb->esw.esw0.erw.cons) {
835                         dev_warn(&channel->ccwdev->dev,
836                                 "The qeth device driver failed to recover "
837                                 "an error on the device\n");
838                         QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
839                                 "0x%X dstat 0x%X\n",
840                                 dev_name(&channel->ccwdev->dev), cstat, dstat);
841                         print_hex_dump(KERN_WARNING, "qeth: irb ",
842                                 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
843                         print_hex_dump(KERN_WARNING, "qeth: sense data ",
844                                 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
845                 }
846                 if (intparm == QETH_RCD_PARM) {
847                         channel->state = CH_STATE_DOWN;
848                         goto out;
849                 }
850                 rc = qeth_get_problem(cdev, irb);
851                 if (rc) {
852                         qeth_clear_ipacmd_list(card);
853                         qeth_schedule_recovery(card);
854                         goto out;
855                 }
856         }
857
858         if (intparm == QETH_RCD_PARM) {
859                 channel->state = CH_STATE_RCD_DONE;
860                 goto out;
861         }
862         if (intparm) {
863                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
864                 buffer->state = BUF_STATE_PROCESSED;
865         }
866         if (channel == &card->data)
867                 return;
868         if (channel == &card->read &&
869             channel->state == CH_STATE_UP)
870                 qeth_issue_next_read(card);
871
872         iob = channel->iob;
873         index = channel->buf_no;
874         while (iob[index].state == BUF_STATE_PROCESSED) {
875                 if (iob[index].callback != NULL)
876                         iob[index].callback(channel, iob + index);
877
878                 index = (index + 1) % QETH_CMD_BUFFER_NO;
879         }
880         channel->buf_no = index;
881 out:
882         wake_up(&card->wait_q);
883         return;
884 }
885
886 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
887                 struct qeth_qdio_out_buffer *buf)
888 {
889         int i;
890         struct sk_buff *skb;
891
892         /* is PCI flag set on buffer? */
893         if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
894                 atomic_dec(&queue->set_pci_flags_count);
895
896         skb = skb_dequeue(&buf->skb_list);
897         while (skb) {
898                 atomic_dec(&skb->users);
899                 dev_kfree_skb_any(skb);
900                 skb = skb_dequeue(&buf->skb_list);
901         }
902         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
903                 if (buf->buffer->element[i].addr && buf->is_header[i])
904                         kmem_cache_free(qeth_core_header_cache,
905                                 buf->buffer->element[i].addr);
906                 buf->is_header[i] = 0;
907                 buf->buffer->element[i].length = 0;
908                 buf->buffer->element[i].addr = NULL;
909                 buf->buffer->element[i].eflags = 0;
910                 buf->buffer->element[i].sflags = 0;
911         }
912         buf->buffer->element[15].eflags = 0;
913         buf->buffer->element[15].sflags = 0;
914         buf->next_element_to_fill = 0;
915         atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
916 }
917
918 void qeth_clear_qdio_buffers(struct qeth_card *card)
919 {
920         int i, j;
921
922         QETH_CARD_TEXT(card, 2, "clearqdbf");
923         /* clear outbound buffers to free skbs */
924         for (i = 0; i < card->qdio.no_out_queues; ++i)
925                 if (card->qdio.out_qs[i]) {
926                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
927                                 qeth_clear_output_buffer(card->qdio.out_qs[i],
928                                                 &card->qdio.out_qs[i]->bufs[j]);
929                 }
930 }
931 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
932
933 static void qeth_free_buffer_pool(struct qeth_card *card)
934 {
935         struct qeth_buffer_pool_entry *pool_entry, *tmp;
936         int i = 0;
937         list_for_each_entry_safe(pool_entry, tmp,
938                                  &card->qdio.init_pool.entry_list, init_list){
939                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
940                         free_page((unsigned long)pool_entry->elements[i]);
941                 list_del(&pool_entry->init_list);
942                 kfree(pool_entry);
943         }
944 }
945
946 static void qeth_free_qdio_buffers(struct qeth_card *card)
947 {
948         int i, j;
949
950         if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
951                 QETH_QDIO_UNINITIALIZED)
952                 return;
953         kfree(card->qdio.in_q);
954         card->qdio.in_q = NULL;
955         /* inbound buffer pool */
956         qeth_free_buffer_pool(card);
957         /* free outbound qdio_qs */
958         if (card->qdio.out_qs) {
959                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
960                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
961                                 qeth_clear_output_buffer(card->qdio.out_qs[i],
962                                                 &card->qdio.out_qs[i]->bufs[j]);
963                         kfree(card->qdio.out_qs[i]);
964                 }
965                 kfree(card->qdio.out_qs);
966                 card->qdio.out_qs = NULL;
967         }
968 }
969
970 static void qeth_clean_channel(struct qeth_channel *channel)
971 {
972         int cnt;
973
974         QETH_DBF_TEXT(SETUP, 2, "freech");
975         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
976                 kfree(channel->iob[cnt].data);
977 }
978
979 static void qeth_get_channel_path_desc(struct qeth_card *card)
980 {
981         struct ccw_device *ccwdev;
982         struct channelPath_dsc {
983                 u8 flags;
984                 u8 lsn;
985                 u8 desc;
986                 u8 chpid;
987                 u8 swla;
988                 u8 zeroes;
989                 u8 chla;
990                 u8 chpp;
991         } *chp_dsc;
992
993         QETH_DBF_TEXT(SETUP, 2, "chp_desc");
994
995         ccwdev = card->data.ccwdev;
996         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
997         if (chp_dsc != NULL) {
998                 if (card->info.type != QETH_CARD_TYPE_IQD) {
999                         /* CHPP field bit 6 == 1 -> single queue */
1000                         if ((chp_dsc->chpp & 0x02) == 0x02) {
1001                                 if ((atomic_read(&card->qdio.state) !=
1002                                         QETH_QDIO_UNINITIALIZED) &&
1003                                     (card->qdio.no_out_queues == 4))
1004                                         /* change from 4 to 1 outbound queues */
1005                                         qeth_free_qdio_buffers(card);
1006                                 card->qdio.no_out_queues = 1;
1007                                 if (card->qdio.default_out_queue != 0)
1008                                         dev_info(&card->gdev->dev,
1009                                         "Priority Queueing not supported\n");
1010                                 card->qdio.default_out_queue = 0;
1011                         } else {
1012                                 if ((atomic_read(&card->qdio.state) !=
1013                                         QETH_QDIO_UNINITIALIZED) &&
1014                                     (card->qdio.no_out_queues == 1)) {
1015                                         /* change from 1 to 4 outbound queues */
1016                                         qeth_free_qdio_buffers(card);
1017                                         card->qdio.default_out_queue = 2;
1018                                 }
1019                                 card->qdio.no_out_queues = 4;
1020                         }
1021                 }
1022                 card->info.func_level = 0x4100 + chp_dsc->desc;
1023                 kfree(chp_dsc);
1024         }
1025         QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1026         QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1027         return;
1028 }
1029
1030 static void qeth_init_qdio_info(struct qeth_card *card)
1031 {
1032         QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1033         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1034         /* inbound */
1035         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1036         if (card->info.type == QETH_CARD_TYPE_IQD)
1037                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1038         else
1039                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1040         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1041         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1042         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1043 }
1044
1045 static void qeth_set_intial_options(struct qeth_card *card)
1046 {
1047         card->options.route4.type = NO_ROUTER;
1048         card->options.route6.type = NO_ROUTER;
1049         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1050         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1051         card->options.fake_broadcast = 0;
1052         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1053         card->options.performance_stats = 0;
1054         card->options.rx_sg_cb = QETH_RX_SG_CB;
1055         card->options.isolation = ISOLATION_MODE_NONE;
1056 }
1057
1058 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1059 {
1060         unsigned long flags;
1061         int rc = 0;
1062
1063         spin_lock_irqsave(&card->thread_mask_lock, flags);
1064         QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1065                         (u8) card->thread_start_mask,
1066                         (u8) card->thread_allowed_mask,
1067                         (u8) card->thread_running_mask);
1068         rc = (card->thread_start_mask & thread);
1069         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1070         return rc;
1071 }
1072
1073 static void qeth_start_kernel_thread(struct work_struct *work)
1074 {
1075         struct qeth_card *card = container_of(work, struct qeth_card,
1076                                         kernel_thread_starter);
1077         QETH_CARD_TEXT(card , 2, "strthrd");
1078
1079         if (card->read.state != CH_STATE_UP &&
1080             card->write.state != CH_STATE_UP)
1081                 return;
1082         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1083                 kthread_run(card->discipline.recover, (void *) card,
1084                                 "qeth_recover");
1085 }
1086
1087 static int qeth_setup_card(struct qeth_card *card)
1088 {
1089
1090         QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1091         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1092
1093         card->read.state  = CH_STATE_DOWN;
1094         card->write.state = CH_STATE_DOWN;
1095         card->data.state  = CH_STATE_DOWN;
1096         card->state = CARD_STATE_DOWN;
1097         card->lan_online = 0;
1098         card->read_or_write_problem = 0;
1099         card->dev = NULL;
1100         spin_lock_init(&card->vlanlock);
1101         spin_lock_init(&card->mclock);
1102         spin_lock_init(&card->lock);
1103         spin_lock_init(&card->ip_lock);
1104         spin_lock_init(&card->thread_mask_lock);
1105         mutex_init(&card->conf_mutex);
1106         mutex_init(&card->discipline_mutex);
1107         card->thread_start_mask = 0;
1108         card->thread_allowed_mask = 0;
1109         card->thread_running_mask = 0;
1110         INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1111         INIT_LIST_HEAD(&card->ip_list);
1112         INIT_LIST_HEAD(card->ip_tbd_list);
1113         INIT_LIST_HEAD(&card->cmd_waiter_list);
1114         init_waitqueue_head(&card->wait_q);
1115         /* initial options */
1116         qeth_set_intial_options(card);
1117         /* IP address takeover */
1118         INIT_LIST_HEAD(&card->ipato.entries);
1119         card->ipato.enabled = 0;
1120         card->ipato.invert4 = 0;
1121         card->ipato.invert6 = 0;
1122         /* init QDIO stuff */
1123         qeth_init_qdio_info(card);
1124         return 0;
1125 }
1126
1127 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1128 {
1129         struct qeth_card *card = container_of(slr, struct qeth_card,
1130                                         qeth_service_level);
1131         if (card->info.mcl_level[0])
1132                 seq_printf(m, "qeth: %s firmware level %s\n",
1133                         CARD_BUS_ID(card), card->info.mcl_level);
1134 }
1135
1136 static struct qeth_card *qeth_alloc_card(void)
1137 {
1138         struct qeth_card *card;
1139
1140         QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1141         card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1142         if (!card)
1143                 goto out;
1144         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1145         card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1146         if (!card->ip_tbd_list) {
1147                 QETH_DBF_TEXT(SETUP, 0, "iptbdnom");
1148                 goto out_card;
1149         }
1150         if (qeth_setup_channel(&card->read))
1151                 goto out_ip;
1152         if (qeth_setup_channel(&card->write))
1153                 goto out_channel;
1154         card->options.layer2 = -1;
1155         card->qeth_service_level.seq_print = qeth_core_sl_print;
1156         register_service_level(&card->qeth_service_level);
1157         return card;
1158
1159 out_channel:
1160         qeth_clean_channel(&card->read);
1161 out_ip:
1162         kfree(card->ip_tbd_list);
1163 out_card:
1164         kfree(card);
1165 out:
1166         return NULL;
1167 }
1168
1169 static int qeth_determine_card_type(struct qeth_card *card)
1170 {
1171         int i = 0;
1172
1173         QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1174
1175         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1176         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1177         while (known_devices[i][QETH_DEV_MODEL_IND]) {
1178                 if ((CARD_RDEV(card)->id.dev_type ==
1179                                 known_devices[i][QETH_DEV_TYPE_IND]) &&
1180                     (CARD_RDEV(card)->id.dev_model ==
1181                                 known_devices[i][QETH_DEV_MODEL_IND])) {
1182                         card->info.type = known_devices[i][QETH_DEV_MODEL_IND];
1183                         card->qdio.no_out_queues =
1184                                 known_devices[i][QETH_QUEUE_NO_IND];
1185                         card->info.is_multicast_different =
1186                                 known_devices[i][QETH_MULTICAST_IND];
1187                         qeth_get_channel_path_desc(card);
1188                         return 0;
1189                 }
1190                 i++;
1191         }
1192         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1193         dev_err(&card->gdev->dev, "The adapter hardware is of an "
1194                 "unknown type\n");
1195         return -ENOENT;
1196 }
1197
1198 static int qeth_clear_channel(struct qeth_channel *channel)
1199 {
1200         unsigned long flags;
1201         struct qeth_card *card;
1202         int rc;
1203
1204         card = CARD_FROM_CDEV(channel->ccwdev);
1205         QETH_CARD_TEXT(card, 3, "clearch");
1206         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1207         rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1208         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1209
1210         if (rc)
1211                 return rc;
1212         rc = wait_event_interruptible_timeout(card->wait_q,
1213                         channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1214         if (rc == -ERESTARTSYS)
1215                 return rc;
1216         if (channel->state != CH_STATE_STOPPED)
1217                 return -ETIME;
1218         channel->state = CH_STATE_DOWN;
1219         return 0;
1220 }
1221
1222 static int qeth_halt_channel(struct qeth_channel *channel)
1223 {
1224         unsigned long flags;
1225         struct qeth_card *card;
1226         int rc;
1227
1228         card = CARD_FROM_CDEV(channel->ccwdev);
1229         QETH_CARD_TEXT(card, 3, "haltch");
1230         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1231         rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1232         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1233
1234         if (rc)
1235                 return rc;
1236         rc = wait_event_interruptible_timeout(card->wait_q,
1237                         channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1238         if (rc == -ERESTARTSYS)
1239                 return rc;
1240         if (channel->state != CH_STATE_HALTED)
1241                 return -ETIME;
1242         return 0;
1243 }
1244
1245 static int qeth_halt_channels(struct qeth_card *card)
1246 {
1247         int rc1 = 0, rc2 = 0, rc3 = 0;
1248
1249         QETH_CARD_TEXT(card, 3, "haltchs");
1250         rc1 = qeth_halt_channel(&card->read);
1251         rc2 = qeth_halt_channel(&card->write);
1252         rc3 = qeth_halt_channel(&card->data);
1253         if (rc1)
1254                 return rc1;
1255         if (rc2)
1256                 return rc2;
1257         return rc3;
1258 }
1259
1260 static int qeth_clear_channels(struct qeth_card *card)
1261 {
1262         int rc1 = 0, rc2 = 0, rc3 = 0;
1263
1264         QETH_CARD_TEXT(card, 3, "clearchs");
1265         rc1 = qeth_clear_channel(&card->read);
1266         rc2 = qeth_clear_channel(&card->write);
1267         rc3 = qeth_clear_channel(&card->data);
1268         if (rc1)
1269                 return rc1;
1270         if (rc2)
1271                 return rc2;
1272         return rc3;
1273 }
1274
1275 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1276 {
1277         int rc = 0;
1278
1279         QETH_CARD_TEXT(card, 3, "clhacrd");
1280
1281         if (halt)
1282                 rc = qeth_halt_channels(card);
1283         if (rc)
1284                 return rc;
1285         return qeth_clear_channels(card);
1286 }
1287
1288 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1289 {
1290         int rc = 0;
1291
1292         QETH_CARD_TEXT(card, 3, "qdioclr");
1293         switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1294                 QETH_QDIO_CLEANING)) {
1295         case QETH_QDIO_ESTABLISHED:
1296                 if (card->info.type == QETH_CARD_TYPE_IQD)
1297                         rc = qdio_shutdown(CARD_DDEV(card),
1298                                 QDIO_FLAG_CLEANUP_USING_HALT);
1299                 else
1300                         rc = qdio_shutdown(CARD_DDEV(card),
1301                                 QDIO_FLAG_CLEANUP_USING_CLEAR);
1302                 if (rc)
1303                         QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1304                 qdio_free(CARD_DDEV(card));
1305                 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1306                 break;
1307         case QETH_QDIO_CLEANING:
1308                 return rc;
1309         default:
1310                 break;
1311         }
1312         rc = qeth_clear_halt_card(card, use_halt);
1313         if (rc)
1314                 QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1315         card->state = CARD_STATE_DOWN;
1316         return rc;
1317 }
1318 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1319
1320 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1321                                int *length)
1322 {
1323         struct ciw *ciw;
1324         char *rcd_buf;
1325         int ret;
1326         struct qeth_channel *channel = &card->data;
1327         unsigned long flags;
1328
1329         /*
1330          * scan for RCD command in extended SenseID data
1331          */
1332         ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1333         if (!ciw || ciw->cmd == 0)
1334                 return -EOPNOTSUPP;
1335         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1336         if (!rcd_buf)
1337                 return -ENOMEM;
1338
1339         channel->ccw.cmd_code = ciw->cmd;
1340         channel->ccw.cda = (__u32) __pa(rcd_buf);
1341         channel->ccw.count = ciw->count;
1342         channel->ccw.flags = CCW_FLAG_SLI;
1343         channel->state = CH_STATE_RCD;
1344         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1345         ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1346                                        QETH_RCD_PARM, LPM_ANYPATH, 0,
1347                                        QETH_RCD_TIMEOUT);
1348         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1349         if (!ret)
1350                 wait_event(card->wait_q,
1351                            (channel->state == CH_STATE_RCD_DONE ||
1352                             channel->state == CH_STATE_DOWN));
1353         if (channel->state == CH_STATE_DOWN)
1354                 ret = -EIO;
1355         else
1356                 channel->state = CH_STATE_DOWN;
1357         if (ret) {
1358                 kfree(rcd_buf);
1359                 *buffer = NULL;
1360                 *length = 0;
1361         } else {
1362                 *length = ciw->count;
1363                 *buffer = rcd_buf;
1364         }
1365         return ret;
1366 }
1367
1368 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1369 {
1370         QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1371         card->info.chpid = prcd[30];
1372         card->info.unit_addr2 = prcd[31];
1373         card->info.cula = prcd[63];
1374         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1375                                (prcd[0x11] == _ascebc['M']));
1376 }
1377
1378 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1379 {
1380         QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1381
1382         if (prcd[74] == 0xF0 && prcd[75] == 0xF0 && prcd[76] == 0xF5) {
1383                 card->info.blkt.time_total = 250;
1384                 card->info.blkt.inter_packet = 5;
1385                 card->info.blkt.inter_packet_jumbo = 15;
1386         } else {
1387                 card->info.blkt.time_total = 0;
1388                 card->info.blkt.inter_packet = 0;
1389                 card->info.blkt.inter_packet_jumbo = 0;
1390         }
1391 }
1392
1393 static void qeth_init_tokens(struct qeth_card *card)
1394 {
1395         card->token.issuer_rm_w = 0x00010103UL;
1396         card->token.cm_filter_w = 0x00010108UL;
1397         card->token.cm_connection_w = 0x0001010aUL;
1398         card->token.ulp_filter_w = 0x0001010bUL;
1399         card->token.ulp_connection_w = 0x0001010dUL;
1400 }
1401
1402 static void qeth_init_func_level(struct qeth_card *card)
1403 {
1404         switch (card->info.type) {
1405         case QETH_CARD_TYPE_IQD:
1406                 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1407                 break;
1408         case QETH_CARD_TYPE_OSD:
1409         case QETH_CARD_TYPE_OSN:
1410                 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1411                 break;
1412         default:
1413                 break;
1414         }
1415 }
1416
1417 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1418                 void (*idx_reply_cb)(struct qeth_channel *,
1419                         struct qeth_cmd_buffer *))
1420 {
1421         struct qeth_cmd_buffer *iob;
1422         unsigned long flags;
1423         int rc;
1424         struct qeth_card *card;
1425
1426         QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1427         card = CARD_FROM_CDEV(channel->ccwdev);
1428         iob = qeth_get_buffer(channel);
1429         iob->callback = idx_reply_cb;
1430         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1431         channel->ccw.count = QETH_BUFSIZE;
1432         channel->ccw.cda = (__u32) __pa(iob->data);
1433
1434         wait_event(card->wait_q,
1435                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1436         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1437         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1438         rc = ccw_device_start(channel->ccwdev,
1439                               &channel->ccw, (addr_t) iob, 0, 0);
1440         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1441
1442         if (rc) {
1443                 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1444                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1445                 atomic_set(&channel->irq_pending, 0);
1446                 wake_up(&card->wait_q);
1447                 return rc;
1448         }
1449         rc = wait_event_interruptible_timeout(card->wait_q,
1450                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1451         if (rc == -ERESTARTSYS)
1452                 return rc;
1453         if (channel->state != CH_STATE_UP) {
1454                 rc = -ETIME;
1455                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1456                 qeth_clear_cmd_buffers(channel);
1457         } else
1458                 rc = 0;
1459         return rc;
1460 }
1461
1462 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1463                 void (*idx_reply_cb)(struct qeth_channel *,
1464                         struct qeth_cmd_buffer *))
1465 {
1466         struct qeth_card *card;
1467         struct qeth_cmd_buffer *iob;
1468         unsigned long flags;
1469         __u16 temp;
1470         __u8 tmp;
1471         int rc;
1472         struct ccw_dev_id temp_devid;
1473
1474         card = CARD_FROM_CDEV(channel->ccwdev);
1475
1476         QETH_DBF_TEXT(SETUP, 2, "idxactch");
1477
1478         iob = qeth_get_buffer(channel);
1479         iob->callback = idx_reply_cb;
1480         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1481         channel->ccw.count = IDX_ACTIVATE_SIZE;
1482         channel->ccw.cda = (__u32) __pa(iob->data);
1483         if (channel == &card->write) {
1484                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1485                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1486                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1487                 card->seqno.trans_hdr++;
1488         } else {
1489                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1490                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1491                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1492         }
1493         tmp = ((__u8)card->info.portno) | 0x80;
1494         memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1495         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1496                &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1497         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1498                &card->info.func_level, sizeof(__u16));
1499         ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1500         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1501         temp = (card->info.cula << 8) + card->info.unit_addr2;
1502         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1503
1504         wait_event(card->wait_q,
1505                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1506         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1507         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1508         rc = ccw_device_start(channel->ccwdev,
1509                               &channel->ccw, (addr_t) iob, 0, 0);
1510         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1511
1512         if (rc) {
1513                 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1514                         rc);
1515                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1516                 atomic_set(&channel->irq_pending, 0);
1517                 wake_up(&card->wait_q);
1518                 return rc;
1519         }
1520         rc = wait_event_interruptible_timeout(card->wait_q,
1521                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1522         if (rc == -ERESTARTSYS)
1523                 return rc;
1524         if (channel->state != CH_STATE_ACTIVATING) {
1525                 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1526                         " failed to recover an error on the device\n");
1527                 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1528                         dev_name(&channel->ccwdev->dev));
1529                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1530                 qeth_clear_cmd_buffers(channel);
1531                 return -ETIME;
1532         }
1533         return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1534 }
1535
1536 static int qeth_peer_func_level(int level)
1537 {
1538         if ((level & 0xff) == 8)
1539                 return (level & 0xff) + 0x400;
1540         if (((level >> 8) & 3) == 1)
1541                 return (level & 0xff) + 0x200;
1542         return level;
1543 }
1544
1545 static void qeth_idx_write_cb(struct qeth_channel *channel,
1546                 struct qeth_cmd_buffer *iob)
1547 {
1548         struct qeth_card *card;
1549         __u16 temp;
1550
1551         QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1552
1553         if (channel->state == CH_STATE_DOWN) {
1554                 channel->state = CH_STATE_ACTIVATING;
1555                 goto out;
1556         }
1557         card = CARD_FROM_CDEV(channel->ccwdev);
1558
1559         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1560                 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
1561                         dev_err(&card->write.ccwdev->dev,
1562                                 "The adapter is used exclusively by another "
1563                                 "host\n");
1564                 else
1565                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1566                                 " negative reply\n",
1567                                 dev_name(&card->write.ccwdev->dev));
1568                 goto out;
1569         }
1570         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1571         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1572                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
1573                         "function level mismatch (sent: 0x%x, received: "
1574                         "0x%x)\n", dev_name(&card->write.ccwdev->dev),
1575                         card->info.func_level, temp);
1576                 goto out;
1577         }
1578         channel->state = CH_STATE_UP;
1579 out:
1580         qeth_release_buffer(channel, iob);
1581 }
1582
1583 static void qeth_idx_read_cb(struct qeth_channel *channel,
1584                 struct qeth_cmd_buffer *iob)
1585 {
1586         struct qeth_card *card;
1587         __u16 temp;
1588
1589         QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
1590         if (channel->state == CH_STATE_DOWN) {
1591                 channel->state = CH_STATE_ACTIVATING;
1592                 goto out;
1593         }
1594
1595         card = CARD_FROM_CDEV(channel->ccwdev);
1596         if (qeth_check_idx_response(card, iob->data))
1597                         goto out;
1598
1599         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1600                 switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
1601                 case QETH_IDX_ACT_ERR_EXCL:
1602                         dev_err(&card->write.ccwdev->dev,
1603                                 "The adapter is used exclusively by another "
1604                                 "host\n");
1605                         break;
1606                 case QETH_IDX_ACT_ERR_AUTH:
1607                 case QETH_IDX_ACT_ERR_AUTH_USER:
1608                         dev_err(&card->read.ccwdev->dev,
1609                                 "Setting the device online failed because of "
1610                                 "insufficient authorization\n");
1611                         break;
1612                 default:
1613                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
1614                                 " negative reply\n",
1615                                 dev_name(&card->read.ccwdev->dev));
1616                 }
1617                 QETH_CARD_TEXT_(card, 2, "idxread%c",
1618                         QETH_IDX_ACT_CAUSE_CODE(iob->data));
1619                 goto out;
1620         }
1621
1622 /**
1623  *  * temporary fix for microcode bug
1624  *   * to revert it,replace OR by AND
1625  *    */
1626         if ((!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1627              (card->info.type == QETH_CARD_TYPE_OSD))
1628                 card->info.portname_required = 1;
1629
1630         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1631         if (temp != qeth_peer_func_level(card->info.func_level)) {
1632                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
1633                         "level mismatch (sent: 0x%x, received: 0x%x)\n",
1634                         dev_name(&card->read.ccwdev->dev),
1635                         card->info.func_level, temp);
1636                 goto out;
1637         }
1638         memcpy(&card->token.issuer_rm_r,
1639                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1640                QETH_MPC_TOKEN_LENGTH);
1641         memcpy(&card->info.mcl_level[0],
1642                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1643         channel->state = CH_STATE_UP;
1644 out:
1645         qeth_release_buffer(channel, iob);
1646 }
1647
1648 void qeth_prepare_control_data(struct qeth_card *card, int len,
1649                 struct qeth_cmd_buffer *iob)
1650 {
1651         qeth_setup_ccw(&card->write, iob->data, len);
1652         iob->callback = qeth_release_buffer;
1653
1654         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1655                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1656         card->seqno.trans_hdr++;
1657         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1658                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1659         card->seqno.pdu_hdr++;
1660         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1661                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1662         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1663 }
1664 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
1665
1666 int qeth_send_control_data(struct qeth_card *card, int len,
1667                 struct qeth_cmd_buffer *iob,
1668                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1669                         unsigned long),
1670                 void *reply_param)
1671 {
1672         int rc;
1673         unsigned long flags;
1674         struct qeth_reply *reply = NULL;
1675         unsigned long timeout, event_timeout;
1676         struct qeth_ipa_cmd *cmd;
1677
1678         QETH_CARD_TEXT(card, 2, "sendctl");
1679
1680         if (card->read_or_write_problem) {
1681                 qeth_release_buffer(iob->channel, iob);
1682                 return -EIO;
1683         }
1684         reply = qeth_alloc_reply(card);
1685         if (!reply) {
1686                 return -ENOMEM;
1687         }
1688         reply->callback = reply_cb;
1689         reply->param = reply_param;
1690         if (card->state == CARD_STATE_DOWN)
1691                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1692         else
1693                 reply->seqno = card->seqno.ipa++;
1694         init_waitqueue_head(&reply->wait_q);
1695         spin_lock_irqsave(&card->lock, flags);
1696         list_add_tail(&reply->list, &card->cmd_waiter_list);
1697         spin_unlock_irqrestore(&card->lock, flags);
1698         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1699
1700         while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
1701         qeth_prepare_control_data(card, len, iob);
1702
1703         if (IS_IPA(iob->data))
1704                 event_timeout = QETH_IPA_TIMEOUT;
1705         else
1706                 event_timeout = QETH_TIMEOUT;
1707         timeout = jiffies + event_timeout;
1708
1709         QETH_CARD_TEXT(card, 6, "noirqpnd");
1710         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1711         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1712                               (addr_t) iob, 0, 0);
1713         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1714         if (rc) {
1715                 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
1716                         "ccw_device_start rc = %i\n",
1717                         dev_name(&card->write.ccwdev->dev), rc);
1718                 QETH_CARD_TEXT_(card, 2, " err%d", rc);
1719                 spin_lock_irqsave(&card->lock, flags);
1720                 list_del_init(&reply->list);
1721                 qeth_put_reply(reply);
1722                 spin_unlock_irqrestore(&card->lock, flags);
1723                 qeth_release_buffer(iob->channel, iob);
1724                 atomic_set(&card->write.irq_pending, 0);
1725                 wake_up(&card->wait_q);
1726                 return rc;
1727         }
1728
1729         /* we have only one long running ipassist, since we can ensure
1730            process context of this command we can sleep */
1731         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1732         if ((cmd->hdr.command == IPA_CMD_SETIP) &&
1733             (cmd->hdr.prot_version == QETH_PROT_IPV4)) {
1734                 if (!wait_event_timeout(reply->wait_q,
1735                     atomic_read(&reply->received), event_timeout))
1736                         goto time_err;
1737         } else {
1738                 while (!atomic_read(&reply->received)) {
1739                         if (time_after(jiffies, timeout))
1740                                 goto time_err;
1741                         cpu_relax();
1742                 };
1743         }
1744
1745         if (reply->rc == -EIO)
1746                 goto error;
1747         rc = reply->rc;
1748         qeth_put_reply(reply);
1749         return rc;
1750
1751 time_err:
1752         reply->rc = -ETIME;
1753         spin_lock_irqsave(&reply->card->lock, flags);
1754         list_del_init(&reply->list);
1755         spin_unlock_irqrestore(&reply->card->lock, flags);
1756         atomic_inc(&reply->received);
1757 error:
1758         atomic_set(&card->write.irq_pending, 0);
1759         qeth_release_buffer(iob->channel, iob);
1760         card->write.buf_no = (card->write.buf_no + 1) % QETH_CMD_BUFFER_NO;
1761         rc = reply->rc;
1762         qeth_put_reply(reply);
1763         return rc;
1764 }
1765 EXPORT_SYMBOL_GPL(qeth_send_control_data);
1766
1767 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1768                 unsigned long data)
1769 {
1770         struct qeth_cmd_buffer *iob;
1771
1772         QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
1773
1774         iob = (struct qeth_cmd_buffer *) data;
1775         memcpy(&card->token.cm_filter_r,
1776                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
1777                QETH_MPC_TOKEN_LENGTH);
1778         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
1779         return 0;
1780 }
1781
1782 static int qeth_cm_enable(struct qeth_card *card)
1783 {
1784         int rc;
1785         struct qeth_cmd_buffer *iob;
1786
1787         QETH_DBF_TEXT(SETUP, 2, "cmenable");
1788
1789         iob = qeth_wait_for_buffer(&card->write);
1790         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
1791         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
1792                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1793         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
1794                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
1795
1796         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
1797                                     qeth_cm_enable_cb, NULL);
1798         return rc;
1799 }
1800
1801 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1802                 unsigned long data)
1803 {
1804
1805         struct qeth_cmd_buffer *iob;
1806
1807         QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
1808
1809         iob = (struct qeth_cmd_buffer *) data;
1810         memcpy(&card->token.cm_connection_r,
1811                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
1812                QETH_MPC_TOKEN_LENGTH);
1813         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
1814         return 0;
1815 }
1816
1817 static int qeth_cm_setup(struct qeth_card *card)
1818 {
1819         int rc;
1820         struct qeth_cmd_buffer *iob;
1821
1822         QETH_DBF_TEXT(SETUP, 2, "cmsetup");
1823
1824         iob = qeth_wait_for_buffer(&card->write);
1825         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
1826         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
1827                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1828         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
1829                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
1830         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
1831                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
1832         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
1833                                     qeth_cm_setup_cb, NULL);
1834         return rc;
1835
1836 }
1837
1838 static inline int qeth_get_initial_mtu_for_card(struct qeth_card *card)
1839 {
1840         switch (card->info.type) {
1841         case QETH_CARD_TYPE_UNKNOWN:
1842                 return 1500;
1843         case QETH_CARD_TYPE_IQD:
1844                 return card->info.max_mtu;
1845         case QETH_CARD_TYPE_OSD:
1846                 switch (card->info.link_type) {
1847                 case QETH_LINK_TYPE_HSTR:
1848                 case QETH_LINK_TYPE_LANE_TR:
1849                         return 2000;
1850                 default:
1851                         return 1492;
1852                 }
1853         case QETH_CARD_TYPE_OSM:
1854         case QETH_CARD_TYPE_OSX:
1855                 return 1492;
1856         default:
1857                 return 1500;
1858         }
1859 }
1860
1861 static inline int qeth_get_mtu_outof_framesize(int framesize)
1862 {
1863         switch (framesize) {
1864         case 0x4000:
1865                 return 8192;
1866         case 0x6000:
1867                 return 16384;
1868         case 0xa000:
1869                 return 32768;
1870         case 0xffff:
1871                 return 57344;
1872         default:
1873                 return 0;
1874         }
1875 }
1876
1877 static inline int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
1878 {
1879         switch (card->info.type) {
1880         case QETH_CARD_TYPE_OSD:
1881         case QETH_CARD_TYPE_OSM:
1882         case QETH_CARD_TYPE_OSX:
1883         case QETH_CARD_TYPE_IQD:
1884                 return ((mtu >= 576) &&
1885                         (mtu <= card->info.max_mtu));
1886         case QETH_CARD_TYPE_OSN:
1887         case QETH_CARD_TYPE_UNKNOWN:
1888         default:
1889                 return 1;
1890         }
1891 }
1892
1893 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1894                 unsigned long data)
1895 {
1896
1897         __u16 mtu, framesize;
1898         __u16 len;
1899         __u8 link_type;
1900         struct qeth_cmd_buffer *iob;
1901
1902         QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
1903
1904         iob = (struct qeth_cmd_buffer *) data;
1905         memcpy(&card->token.ulp_filter_r,
1906                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
1907                QETH_MPC_TOKEN_LENGTH);
1908         if (card->info.type == QETH_CARD_TYPE_IQD) {
1909                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
1910                 mtu = qeth_get_mtu_outof_framesize(framesize);
1911                 if (!mtu) {
1912                         iob->rc = -EINVAL;
1913                         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
1914                         return 0;
1915                 }
1916                 if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
1917                         /* frame size has changed */
1918                         if (card->dev &&
1919                             ((card->dev->mtu == card->info.initial_mtu) ||
1920                              (card->dev->mtu > mtu)))
1921                                 card->dev->mtu = mtu;
1922                         qeth_free_qdio_buffers(card);
1923                 }
1924                 card->info.initial_mtu = mtu;
1925                 card->info.max_mtu = mtu;
1926                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
1927         } else {
1928                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
1929                 card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
1930                         iob->data);
1931                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1932         }
1933
1934         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
1935         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
1936                 memcpy(&link_type,
1937                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
1938                 card->info.link_type = link_type;
1939         } else
1940                 card->info.link_type = 0;
1941         QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
1942         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
1943         return 0;
1944 }
1945
1946 static int qeth_ulp_enable(struct qeth_card *card)
1947 {
1948         int rc;
1949         char prot_type;
1950         struct qeth_cmd_buffer *iob;
1951
1952         /*FIXME: trace view callbacks*/
1953         QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
1954
1955         iob = qeth_wait_for_buffer(&card->write);
1956         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
1957
1958         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
1959                 (__u8) card->info.portno;
1960         if (card->options.layer2)
1961                 if (card->info.type == QETH_CARD_TYPE_OSN)
1962                         prot_type = QETH_PROT_OSN2;
1963                 else
1964                         prot_type = QETH_PROT_LAYER2;
1965         else
1966                 prot_type = QETH_PROT_TCPIP;
1967
1968         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
1969         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
1970                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
1971         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
1972                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
1973         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
1974                card->info.portname, 9);
1975         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
1976                                     qeth_ulp_enable_cb, NULL);
1977         return rc;
1978
1979 }
1980
1981 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1982                 unsigned long data)
1983 {
1984         struct qeth_cmd_buffer *iob;
1985         int rc = 0;
1986
1987         QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
1988
1989         iob = (struct qeth_cmd_buffer *) data;
1990         memcpy(&card->token.ulp_connection_r,
1991                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
1992                QETH_MPC_TOKEN_LENGTH);
1993         if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
1994                      3)) {
1995                 QETH_DBF_TEXT(SETUP, 2, "olmlimit");
1996                 dev_err(&card->gdev->dev, "A connection could not be "
1997                         "established because of an OLM limit\n");
1998                 iob->rc = -EMLINK;
1999         }
2000         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2001         return rc;
2002 }
2003
2004 static int qeth_ulp_setup(struct qeth_card *card)
2005 {
2006         int rc;
2007         __u16 temp;
2008         struct qeth_cmd_buffer *iob;
2009         struct ccw_dev_id dev_id;
2010
2011         QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2012
2013         iob = qeth_wait_for_buffer(&card->write);
2014         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2015
2016         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2017                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2018         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2019                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2020         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2021                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2022
2023         ccw_device_get_id(CARD_DDEV(card), &dev_id);
2024         memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2025         temp = (card->info.cula << 8) + card->info.unit_addr2;
2026         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2027         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2028                                     qeth_ulp_setup_cb, NULL);
2029         return rc;
2030 }
2031
2032 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2033 {
2034         int i, j;
2035
2036         QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2037
2038         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2039                 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2040                 return 0;
2041
2042         card->qdio.in_q = kmalloc(sizeof(struct qeth_qdio_q),
2043                                   GFP_KERNEL);
2044         if (!card->qdio.in_q)
2045                 goto out_nomem;
2046         QETH_DBF_TEXT(SETUP, 2, "inq");
2047         QETH_DBF_HEX(SETUP, 2, &card->qdio.in_q, sizeof(void *));
2048         memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
2049         /* give inbound qeth_qdio_buffers their qdio_buffers */
2050         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
2051                 card->qdio.in_q->bufs[i].buffer =
2052                         &card->qdio.in_q->qdio_bufs[i];
2053         /* inbound buffer pool */
2054         if (qeth_alloc_buffer_pool(card))
2055                 goto out_freeinq;
2056         /* outbound */
2057         card->qdio.out_qs =
2058                 kmalloc(card->qdio.no_out_queues *
2059                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2060         if (!card->qdio.out_qs)
2061                 goto out_freepool;
2062         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2063                 card->qdio.out_qs[i] = kmalloc(sizeof(struct qeth_qdio_out_q),
2064                                                GFP_KERNEL);
2065                 if (!card->qdio.out_qs[i])
2066                         goto out_freeoutq;
2067                 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2068                 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2069                 memset(card->qdio.out_qs[i], 0, sizeof(struct qeth_qdio_out_q));
2070                 card->qdio.out_qs[i]->queue_no = i;
2071                 /* give outbound qeth_qdio_buffers their qdio_buffers */
2072                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2073                         card->qdio.out_qs[i]->bufs[j].buffer =
2074                                 &card->qdio.out_qs[i]->qdio_bufs[j];
2075                         skb_queue_head_init(&card->qdio.out_qs[i]->bufs[j].
2076                                             skb_list);
2077                         lockdep_set_class(
2078                                 &card->qdio.out_qs[i]->bufs[j].skb_list.lock,
2079                                 &qdio_out_skb_queue_key);
2080                         INIT_LIST_HEAD(&card->qdio.out_qs[i]->bufs[j].ctx_list);
2081                 }
2082         }
2083         return 0;
2084
2085 out_freeoutq:
2086         while (i > 0)
2087                 kfree(card->qdio.out_qs[--i]);
2088         kfree(card->qdio.out_qs);
2089         card->qdio.out_qs = NULL;
2090 out_freepool:
2091         qeth_free_buffer_pool(card);
2092 out_freeinq:
2093         kfree(card->qdio.in_q);
2094         card->qdio.in_q = NULL;
2095 out_nomem:
2096         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2097         return -ENOMEM;
2098 }
2099
2100 static void qeth_create_qib_param_field(struct qeth_card *card,
2101                 char *param_field)
2102 {
2103
2104         param_field[0] = _ascebc['P'];
2105         param_field[1] = _ascebc['C'];
2106         param_field[2] = _ascebc['I'];
2107         param_field[3] = _ascebc['T'];
2108         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2109         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2110         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2111 }
2112
2113 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2114                 char *param_field)
2115 {
2116         param_field[16] = _ascebc['B'];
2117         param_field[17] = _ascebc['L'];
2118         param_field[18] = _ascebc['K'];
2119         param_field[19] = _ascebc['T'];
2120         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2121         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2122         *((unsigned int *) (&param_field[28])) =
2123                 card->info.blkt.inter_packet_jumbo;
2124 }
2125
2126 static int qeth_qdio_activate(struct qeth_card *card)
2127 {
2128         QETH_DBF_TEXT(SETUP, 3, "qdioact");
2129         return qdio_activate(CARD_DDEV(card));
2130 }
2131
2132 static int qeth_dm_act(struct qeth_card *card)
2133 {
2134         int rc;
2135         struct qeth_cmd_buffer *iob;
2136
2137         QETH_DBF_TEXT(SETUP, 2, "dmact");
2138
2139         iob = qeth_wait_for_buffer(&card->write);
2140         memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2141
2142         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2143                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2144         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2145                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2146         rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2147         return rc;
2148 }
2149
2150 static int qeth_mpc_initialize(struct qeth_card *card)
2151 {
2152         int rc;
2153
2154         QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2155
2156         rc = qeth_issue_next_read(card);
2157         if (rc) {
2158                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2159                 return rc;
2160         }
2161         rc = qeth_cm_enable(card);
2162         if (rc) {
2163                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2164                 goto out_qdio;
2165         }
2166         rc = qeth_cm_setup(card);
2167         if (rc) {
2168                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2169                 goto out_qdio;
2170         }
2171         rc = qeth_ulp_enable(card);
2172         if (rc) {
2173                 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2174                 goto out_qdio;
2175         }
2176         rc = qeth_ulp_setup(card);
2177         if (rc) {
2178                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2179                 goto out_qdio;
2180         }
2181         rc = qeth_alloc_qdio_buffers(card);
2182         if (rc) {
2183                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2184                 goto out_qdio;
2185         }
2186         rc = qeth_qdio_establish(card);
2187         if (rc) {
2188                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2189                 qeth_free_qdio_buffers(card);
2190                 goto out_qdio;
2191         }
2192         rc = qeth_qdio_activate(card);
2193         if (rc) {
2194                 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2195                 goto out_qdio;
2196         }
2197         rc = qeth_dm_act(card);
2198         if (rc) {
2199                 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2200                 goto out_qdio;
2201         }
2202
2203         return 0;
2204 out_qdio:
2205         qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2206         return rc;
2207 }
2208
2209 static void qeth_print_status_with_portname(struct qeth_card *card)
2210 {
2211         char dbf_text[15];
2212         int i;
2213
2214         sprintf(dbf_text, "%s", card->info.portname + 1);
2215         for (i = 0; i < 8; i++)
2216                 dbf_text[i] =
2217                         (char) _ebcasc[(__u8) dbf_text[i]];
2218         dbf_text[8] = 0;
2219         dev_info(&card->gdev->dev, "Device is a%s card%s%s%s\n"
2220                "with link type %s (portname: %s)\n",
2221                qeth_get_cardname(card),
2222                (card->info.mcl_level[0]) ? " (level: " : "",
2223                (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2224                (card->info.mcl_level[0]) ? ")" : "",
2225                qeth_get_cardname_short(card),
2226                dbf_text);
2227
2228 }
2229
2230 static void qeth_print_status_no_portname(struct qeth_card *card)
2231 {
2232         if (card->info.portname[0])
2233                 dev_info(&card->gdev->dev, "Device is a%s "
2234                        "card%s%s%s\nwith link type %s "
2235                        "(no portname needed by interface).\n",
2236                        qeth_get_cardname(card),
2237                        (card->info.mcl_level[0]) ? " (level: " : "",
2238                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2239                        (card->info.mcl_level[0]) ? ")" : "",
2240                        qeth_get_cardname_short(card));
2241         else
2242                 dev_info(&card->gdev->dev, "Device is a%s "
2243                        "card%s%s%s\nwith link type %s.\n",
2244                        qeth_get_cardname(card),
2245                        (card->info.mcl_level[0]) ? " (level: " : "",
2246                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2247                        (card->info.mcl_level[0]) ? ")" : "",
2248                        qeth_get_cardname_short(card));
2249 }
2250
2251 void qeth_print_status_message(struct qeth_card *card)
2252 {
2253         switch (card->info.type) {
2254         case QETH_CARD_TYPE_OSD:
2255         case QETH_CARD_TYPE_OSM:
2256         case QETH_CARD_TYPE_OSX:
2257                 /* VM will use a non-zero first character
2258                  * to indicate a HiperSockets like reporting
2259                  * of the level OSA sets the first character to zero
2260                  * */
2261                 if (!card->info.mcl_level[0]) {
2262                         sprintf(card->info.mcl_level, "%02x%02x",
2263                                 card->info.mcl_level[2],
2264                                 card->info.mcl_level[3]);
2265
2266                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2267                         break;
2268                 }
2269                 /* fallthrough */
2270         case QETH_CARD_TYPE_IQD:
2271                 if ((card->info.guestlan) ||
2272                     (card->info.mcl_level[0] & 0x80)) {
2273                         card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2274                                 card->info.mcl_level[0]];
2275                         card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2276                                 card->info.mcl_level[1]];
2277                         card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2278                                 card->info.mcl_level[2]];
2279                         card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2280                                 card->info.mcl_level[3]];
2281                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2282                 }
2283                 break;
2284         default:
2285                 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2286         }
2287         if (card->info.portname_required)
2288                 qeth_print_status_with_portname(card);
2289         else
2290                 qeth_print_status_no_portname(card);
2291 }
2292 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2293
2294 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2295 {
2296         struct qeth_buffer_pool_entry *entry;
2297
2298         QETH_CARD_TEXT(card, 5, "inwrklst");
2299
2300         list_for_each_entry(entry,
2301                             &card->qdio.init_pool.entry_list, init_list) {
2302                 qeth_put_buffer_pool_entry(card, entry);
2303         }
2304 }
2305
2306 static inline struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2307                 struct qeth_card *card)
2308 {
2309         struct list_head *plh;
2310         struct qeth_buffer_pool_entry *entry;
2311         int i, free;
2312         struct page *page;
2313
2314         if (list_empty(&card->qdio.in_buf_pool.entry_list))
2315                 return NULL;
2316
2317         list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2318                 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2319                 free = 1;
2320                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2321                         if (page_count(virt_to_page(entry->elements[i])) > 1) {
2322                                 free = 0;
2323                                 break;
2324                         }
2325                 }
2326                 if (free) {
2327                         list_del_init(&entry->list);
2328                         return entry;
2329                 }
2330         }
2331
2332         /* no free buffer in pool so take first one and swap pages */
2333         entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2334                         struct qeth_buffer_pool_entry, list);
2335         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2336                 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2337                         page = alloc_page(GFP_ATOMIC);
2338                         if (!page) {
2339                                 return NULL;
2340                         } else {
2341                                 free_page((unsigned long)entry->elements[i]);
2342                                 entry->elements[i] = page_address(page);
2343                                 if (card->options.performance_stats)
2344                                         card->perf_stats.sg_alloc_page_rx++;
2345                         }
2346                 }
2347         }
2348         list_del_init(&entry->list);
2349         return entry;
2350 }
2351
2352 static int qeth_init_input_buffer(struct qeth_card *card,
2353                 struct qeth_qdio_buffer *buf)
2354 {
2355         struct qeth_buffer_pool_entry *pool_entry;
2356         int i;
2357
2358         pool_entry = qeth_find_free_buffer_pool_entry(card);
2359         if (!pool_entry)
2360                 return 1;
2361
2362         /*
2363          * since the buffer is accessed only from the input_tasklet
2364          * there shouldn't be a need to synchronize; also, since we use
2365          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2366          * buffers
2367          */
2368
2369         buf->pool_entry = pool_entry;
2370         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2371                 buf->buffer->element[i].length = PAGE_SIZE;
2372                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2373                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2374                         buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2375                 else
2376                         buf->buffer->element[i].eflags = 0;
2377                 buf->buffer->element[i].sflags = 0;
2378         }
2379         return 0;
2380 }
2381
2382 int qeth_init_qdio_queues(struct qeth_card *card)
2383 {
2384         int i, j;
2385         int rc;
2386
2387         QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2388
2389         /* inbound queue */
2390         memset(card->qdio.in_q->qdio_bufs, 0,
2391                QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2392         qeth_initialize_working_pool_list(card);
2393         /*give only as many buffers to hardware as we have buffer pool entries*/
2394         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2395                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2396         card->qdio.in_q->next_buf_to_init =
2397                 card->qdio.in_buf_pool.buf_count - 1;
2398         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2399                      card->qdio.in_buf_pool.buf_count - 1);
2400         if (rc) {
2401                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2402                 return rc;
2403         }
2404         /* outbound queue */
2405         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2406                 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
2407                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2408                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2409                         qeth_clear_output_buffer(card->qdio.out_qs[i],
2410                                         &card->qdio.out_qs[i]->bufs[j]);
2411                 }
2412                 card->qdio.out_qs[i]->card = card;
2413                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2414                 card->qdio.out_qs[i]->do_pack = 0;
2415                 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2416                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2417                 atomic_set(&card->qdio.out_qs[i]->state,
2418                            QETH_OUT_Q_UNLOCKED);
2419         }
2420         return 0;
2421 }
2422 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2423
2424 static inline __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2425 {
2426         switch (link_type) {
2427         case QETH_LINK_TYPE_HSTR:
2428                 return 2;
2429         default:
2430                 return 1;
2431         }
2432 }
2433
2434 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2435                 struct qeth_ipa_cmd *cmd, __u8 command,
2436                 enum qeth_prot_versions prot)
2437 {
2438         memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2439         cmd->hdr.command = command;
2440         cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2441         cmd->hdr.seqno = card->seqno.ipa;
2442         cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2443         cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2444         if (card->options.layer2)
2445                 cmd->hdr.prim_version_no = 2;
2446         else
2447                 cmd->hdr.prim_version_no = 1;
2448         cmd->hdr.param_count = 1;
2449         cmd->hdr.prot_version = prot;
2450         cmd->hdr.ipa_supported = 0;
2451         cmd->hdr.ipa_enabled = 0;
2452 }
2453
2454 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2455                 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2456 {
2457         struct qeth_cmd_buffer *iob;
2458         struct qeth_ipa_cmd *cmd;
2459
2460         iob = qeth_wait_for_buffer(&card->write);
2461         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2462         qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2463
2464         return iob;
2465 }
2466 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2467
2468 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2469                 char prot_type)
2470 {
2471         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2472         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2473         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2474                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2475 }
2476 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2477
2478 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2479                 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2480                         unsigned long),
2481                 void *reply_param)
2482 {
2483         int rc;
2484         char prot_type;
2485
2486         QETH_CARD_TEXT(card, 4, "sendipa");
2487
2488         if (card->options.layer2)
2489                 if (card->info.type == QETH_CARD_TYPE_OSN)
2490                         prot_type = QETH_PROT_OSN2;
2491                 else
2492                         prot_type = QETH_PROT_LAYER2;
2493         else
2494                 prot_type = QETH_PROT_TCPIP;
2495         qeth_prepare_ipa_cmd(card, iob, prot_type);
2496         rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
2497                                                 iob, reply_cb, reply_param);
2498         if (rc == -ETIME) {
2499                 qeth_clear_ipacmd_list(card);
2500                 qeth_schedule_recovery(card);
2501         }
2502         return rc;
2503 }
2504 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
2505
2506 int qeth_send_startlan(struct qeth_card *card)
2507 {
2508         int rc;
2509         struct qeth_cmd_buffer *iob;
2510
2511         QETH_DBF_TEXT(SETUP, 2, "strtlan");
2512
2513         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
2514         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
2515         return rc;
2516 }
2517 EXPORT_SYMBOL_GPL(qeth_send_startlan);
2518
2519 int qeth_default_setadapterparms_cb(struct qeth_card *card,
2520                 struct qeth_reply *reply, unsigned long data)
2521 {
2522         struct qeth_ipa_cmd *cmd;
2523
2524         QETH_CARD_TEXT(card, 4, "defadpcb");
2525
2526         cmd = (struct qeth_ipa_cmd *) data;
2527         if (cmd->hdr.return_code == 0)
2528                 cmd->hdr.return_code =
2529                         cmd->data.setadapterparms.hdr.return_code;
2530         return 0;
2531 }
2532 EXPORT_SYMBOL_GPL(qeth_default_setadapterparms_cb);
2533
2534 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
2535                 struct qeth_reply *reply, unsigned long data)
2536 {
2537         struct qeth_ipa_cmd *cmd;
2538
2539         QETH_CARD_TEXT(card, 3, "quyadpcb");
2540
2541         cmd = (struct qeth_ipa_cmd *) data;
2542         if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
2543                 card->info.link_type =
2544                       cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
2545                 QETH_DBF_TEXT_(SETUP, 2, "lnk %d", card->info.link_type);
2546         }
2547         card->options.adp.supported_funcs =
2548                 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
2549         return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
2550 }
2551
2552 struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
2553                 __u32 command, __u32 cmdlen)
2554 {
2555         struct qeth_cmd_buffer *iob;
2556         struct qeth_ipa_cmd *cmd;
2557
2558         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
2559                                      QETH_PROT_IPV4);
2560         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2561         cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
2562         cmd->data.setadapterparms.hdr.command_code = command;
2563         cmd->data.setadapterparms.hdr.used_total = 1;
2564         cmd->data.setadapterparms.hdr.seq_no = 1;
2565
2566         return iob;
2567 }
2568 EXPORT_SYMBOL_GPL(qeth_get_adapter_cmd);
2569
2570 int qeth_query_setadapterparms(struct qeth_card *card)
2571 {
2572         int rc;
2573         struct qeth_cmd_buffer *iob;
2574
2575         QETH_CARD_TEXT(card, 3, "queryadp");
2576         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
2577                                    sizeof(struct qeth_ipacmd_setadpparms));
2578         rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
2579         return rc;
2580 }
2581 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
2582
2583 static int qeth_query_ipassists_cb(struct qeth_card *card,
2584                 struct qeth_reply *reply, unsigned long data)
2585 {
2586         struct qeth_ipa_cmd *cmd;
2587
2588         QETH_DBF_TEXT(SETUP, 2, "qipasscb");
2589
2590         cmd = (struct qeth_ipa_cmd *) data;
2591         if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
2592                 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
2593                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
2594         } else {
2595                 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
2596                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
2597         }
2598         QETH_DBF_TEXT(SETUP, 2, "suppenbl");
2599         QETH_DBF_TEXT_(SETUP, 2, "%x", cmd->hdr.ipa_supported);
2600         QETH_DBF_TEXT_(SETUP, 2, "%x", cmd->hdr.ipa_enabled);
2601         return 0;
2602 }
2603
2604 int qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
2605 {
2606         int rc;
2607         struct qeth_cmd_buffer *iob;
2608
2609         QETH_DBF_TEXT_(SETUP, 2, "qipassi%i", prot);
2610         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_QIPASSIST, prot);
2611         rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
2612         return rc;
2613 }
2614 EXPORT_SYMBOL_GPL(qeth_query_ipassists);
2615
2616 static int qeth_query_setdiagass_cb(struct qeth_card *card,
2617                 struct qeth_reply *reply, unsigned long data)
2618 {
2619         struct qeth_ipa_cmd *cmd;
2620         __u16 rc;
2621
2622         cmd = (struct qeth_ipa_cmd *)data;
2623         rc = cmd->hdr.return_code;
2624         if (rc)
2625                 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
2626         else
2627                 card->info.diagass_support = cmd->data.diagass.ext;
2628         return 0;
2629 }
2630
2631 static int qeth_query_setdiagass(struct qeth_card *card)
2632 {
2633         struct qeth_cmd_buffer *iob;
2634         struct qeth_ipa_cmd    *cmd;
2635
2636         QETH_DBF_TEXT(SETUP, 2, "qdiagass");
2637         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
2638         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2639         cmd->data.diagass.subcmd_len = 16;
2640         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
2641         return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
2642 }
2643
2644 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
2645 {
2646         unsigned long info = get_zeroed_page(GFP_KERNEL);
2647         struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
2648         struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
2649         struct ccw_dev_id ccwid;
2650         int level, rc;
2651
2652         tid->chpid = card->info.chpid;
2653         ccw_device_get_id(CARD_RDEV(card), &ccwid);
2654         tid->ssid = ccwid.ssid;
2655         tid->devno = ccwid.devno;
2656         if (!info)
2657                 return;
2658
2659         rc = stsi(NULL, 0, 0, 0);
2660         if (rc == -ENOSYS)
2661                 level = rc;
2662         else
2663                 level = (((unsigned int) rc) >> 28);
2664
2665         if ((level >= 2) && (stsi(info222, 2, 2, 2) != -ENOSYS))
2666                 tid->lparnr = info222->lpar_number;
2667
2668         if ((level >= 3) && (stsi(info322, 3, 2, 2) != -ENOSYS)) {
2669                 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
2670                 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
2671         }
2672         free_page(info);
2673         return;
2674 }
2675
2676 static int qeth_hw_trap_cb(struct qeth_card *card,
2677                 struct qeth_reply *reply, unsigned long data)
2678 {
2679         struct qeth_ipa_cmd *cmd;
2680         __u16 rc;
2681
2682         cmd = (struct qeth_ipa_cmd *)data;
2683         rc = cmd->hdr.return_code;
2684         if (rc)
2685                 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
2686         return 0;
2687 }
2688
2689 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
2690 {
2691         struct qeth_cmd_buffer *iob;
2692         struct qeth_ipa_cmd *cmd;
2693
2694         QETH_DBF_TEXT(SETUP, 2, "diagtrap");
2695         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
2696         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2697         cmd->data.diagass.subcmd_len = 80;
2698         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
2699         cmd->data.diagass.type = 1;
2700         cmd->data.diagass.action = action;
2701         switch (action) {
2702         case QETH_DIAGS_TRAP_ARM:
2703                 cmd->data.diagass.options = 0x0003;
2704                 cmd->data.diagass.ext = 0x00010000 +
2705                         sizeof(struct qeth_trap_id);
2706                 qeth_get_trap_id(card,
2707                         (struct qeth_trap_id *)cmd->data.diagass.cdata);
2708                 break;
2709         case QETH_DIAGS_TRAP_DISARM:
2710                 cmd->data.diagass.options = 0x0001;
2711                 break;
2712         case QETH_DIAGS_TRAP_CAPTURE:
2713                 break;
2714         }
2715         return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
2716 }
2717 EXPORT_SYMBOL_GPL(qeth_hw_trap);
2718
2719 int qeth_check_qdio_errors(struct qeth_card *card, struct qdio_buffer *buf,
2720                 unsigned int qdio_error, const char *dbftext)
2721 {
2722         if (qdio_error) {
2723                 QETH_CARD_TEXT(card, 2, dbftext);
2724                 QETH_CARD_TEXT_(card, 2, " F15=%02X",
2725                                buf->element[15].sflags);
2726                 QETH_CARD_TEXT_(card, 2, " F14=%02X",
2727                                buf->element[14].sflags);
2728                 QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
2729                 if ((buf->element[15].sflags) == 0x12) {
2730                         card->stats.rx_dropped++;
2731                         return 0;
2732                 } else
2733                         return 1;
2734         }
2735         return 0;
2736 }
2737 EXPORT_SYMBOL_GPL(qeth_check_qdio_errors);
2738
2739 void qeth_queue_input_buffer(struct qeth_card *card, int index)
2740 {
2741         struct qeth_qdio_q *queue = card->qdio.in_q;
2742         int count;
2743         int i;
2744         int rc;
2745         int newcount = 0;
2746
2747         count = (index < queue->next_buf_to_init)?
2748                 card->qdio.in_buf_pool.buf_count -
2749                 (queue->next_buf_to_init - index) :
2750                 card->qdio.in_buf_pool.buf_count -
2751                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
2752         /* only requeue at a certain threshold to avoid SIGAs */
2753         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
2754                 for (i = queue->next_buf_to_init;
2755                      i < queue->next_buf_to_init + count; ++i) {
2756                         if (qeth_init_input_buffer(card,
2757                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
2758                                 break;
2759                         } else {
2760                                 newcount++;
2761                         }
2762                 }
2763
2764                 if (newcount < count) {
2765                         /* we are in memory shortage so we switch back to
2766                            traditional skb allocation and drop packages */
2767                         atomic_set(&card->force_alloc_skb, 3);
2768                         count = newcount;
2769                 } else {
2770                         atomic_add_unless(&card->force_alloc_skb, -1, 0);
2771                 }
2772
2773                 /*
2774                  * according to old code it should be avoided to requeue all
2775                  * 128 buffers in order to benefit from PCI avoidance.
2776                  * this function keeps at least one buffer (the buffer at
2777                  * 'index') un-requeued -> this buffer is the first buffer that
2778                  * will be requeued the next time
2779                  */
2780                 if (card->options.performance_stats) {
2781                         card->perf_stats.inbound_do_qdio_cnt++;
2782                         card->perf_stats.inbound_do_qdio_start_time =
2783                                 qeth_get_micros();
2784                 }
2785                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
2786                              queue->next_buf_to_init, count);
2787                 if (card->options.performance_stats)
2788                         card->perf_stats.inbound_do_qdio_time +=
2789                                 qeth_get_micros() -
2790                                 card->perf_stats.inbound_do_qdio_start_time;
2791                 if (rc) {
2792                         dev_warn(&card->gdev->dev,
2793                                 "QDIO reported an error, rc=%i\n", rc);
2794                         QETH_CARD_TEXT(card, 2, "qinberr");
2795                 }
2796                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
2797                                           QDIO_MAX_BUFFERS_PER_Q;
2798         }
2799 }
2800 EXPORT_SYMBOL_GPL(qeth_queue_input_buffer);
2801
2802 static int qeth_handle_send_error(struct qeth_card *card,
2803                 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
2804 {
2805         int sbalf15 = buffer->buffer->element[15].sflags;
2806
2807         QETH_CARD_TEXT(card, 6, "hdsnderr");
2808         if (card->info.type == QETH_CARD_TYPE_IQD) {
2809                 if (sbalf15 == 0) {
2810                         qdio_err = 0;
2811                 } else {
2812                         qdio_err = 1;
2813                 }
2814         }
2815         qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
2816
2817         if (!qdio_err)
2818                 return QETH_SEND_ERROR_NONE;
2819
2820         if ((sbalf15 >= 15) && (sbalf15 <= 31))
2821                 return QETH_SEND_ERROR_RETRY;
2822
2823         QETH_CARD_TEXT(card, 1, "lnkfail");
2824         QETH_CARD_TEXT_(card, 1, "%04x %02x",
2825                        (u16)qdio_err, (u8)sbalf15);
2826         return QETH_SEND_ERROR_LINK_FAILURE;
2827 }
2828
2829 /*
2830  * Switched to packing state if the number of used buffers on a queue
2831  * reaches a certain limit.
2832  */
2833 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
2834 {
2835         if (!queue->do_pack) {
2836                 if (atomic_read(&queue->used_buffers)
2837                     >= QETH_HIGH_WATERMARK_PACK){
2838                         /* switch non-PACKING -> PACKING */
2839                         QETH_CARD_TEXT(queue->card, 6, "np->pack");
2840                         if (queue->card->options.performance_stats)
2841                                 queue->card->perf_stats.sc_dp_p++;
2842                         queue->do_pack = 1;
2843                 }
2844         }
2845 }
2846
2847 /*
2848  * Switches from packing to non-packing mode. If there is a packing
2849  * buffer on the queue this buffer will be prepared to be flushed.
2850  * In that case 1 is returned to inform the caller. If no buffer
2851  * has to be flushed, zero is returned.
2852  */
2853 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
2854 {
2855         struct qeth_qdio_out_buffer *buffer;
2856         int flush_count = 0;
2857
2858         if (queue->do_pack) {
2859                 if (atomic_read(&queue->used_buffers)
2860                     <= QETH_LOW_WATERMARK_PACK) {
2861                         /* switch PACKING -> non-PACKING */
2862                         QETH_CARD_TEXT(queue->card, 6, "pack->np");
2863                         if (queue->card->options.performance_stats)
2864                                 queue->card->perf_stats.sc_p_dp++;
2865                         queue->do_pack = 0;
2866                         /* flush packing buffers */
2867                         buffer = &queue->bufs[queue->next_buf_to_fill];
2868                         if ((atomic_read(&buffer->state) ==
2869                                                 QETH_QDIO_BUF_EMPTY) &&
2870                             (buffer->next_element_to_fill > 0)) {
2871                                 atomic_set(&buffer->state,
2872                                                 QETH_QDIO_BUF_PRIMED);
2873                                 flush_count++;
2874                                 queue->next_buf_to_fill =
2875                                         (queue->next_buf_to_fill + 1) %
2876                                         QDIO_MAX_BUFFERS_PER_Q;
2877                         }
2878                 }
2879         }
2880         return flush_count;
2881 }
2882
2883 /*
2884  * Called to flush a packing buffer if no more pci flags are on the queue.
2885  * Checks if there is a packing buffer and prepares it to be flushed.
2886  * In that case returns 1, otherwise zero.
2887  */
2888 static int qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2889 {
2890         struct qeth_qdio_out_buffer *buffer;
2891
2892         buffer = &queue->bufs[queue->next_buf_to_fill];
2893         if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
2894            (buffer->next_element_to_fill > 0)) {
2895                 /* it's a packing buffer */
2896                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
2897                 queue->next_buf_to_fill =
2898                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
2899                 return 1;
2900         }
2901         return 0;
2902 }
2903
2904 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
2905                                int count)
2906 {
2907         struct qeth_qdio_out_buffer *buf;
2908         int rc;
2909         int i;
2910         unsigned int qdio_flags;
2911
2912         for (i = index; i < index + count; ++i) {
2913                 buf = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2914                 buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
2915                                 SBAL_EFLAGS_LAST_ENTRY;
2916
2917                 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
2918                         continue;
2919
2920                 if (!queue->do_pack) {
2921                         if ((atomic_read(&queue->used_buffers) >=
2922                                 (QETH_HIGH_WATERMARK_PACK -
2923                                  QETH_WATERMARK_PACK_FUZZ)) &&
2924                             !atomic_read(&queue->set_pci_flags_count)) {
2925                                 /* it's likely that we'll go to packing
2926                                  * mode soon */
2927                                 atomic_inc(&queue->set_pci_flags_count);
2928                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
2929                         }
2930                 } else {
2931                         if (!atomic_read(&queue->set_pci_flags_count)) {
2932                                 /*
2933                                  * there's no outstanding PCI any more, so we
2934                                  * have to request a PCI to be sure the the PCI
2935                                  * will wake at some time in the future then we
2936                                  * can flush packed buffers that might still be
2937                                  * hanging around, which can happen if no
2938                                  * further send was requested by the stack
2939                                  */
2940                                 atomic_inc(&queue->set_pci_flags_count);
2941                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
2942                         }
2943                 }
2944         }
2945
2946         queue->card->dev->trans_start = jiffies;
2947         if (queue->card->options.performance_stats) {
2948                 queue->card->perf_stats.outbound_do_qdio_cnt++;
2949                 queue->card->perf_stats.outbound_do_qdio_start_time =
2950                         qeth_get_micros();
2951         }
2952         qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
2953         if (atomic_read(&queue->set_pci_flags_count))
2954                 qdio_flags |= QDIO_FLAG_PCI_OUT;
2955         rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
2956                      queue->queue_no, index, count);
2957         if (queue->card->options.performance_stats)
2958                 queue->card->perf_stats.outbound_do_qdio_time +=
2959                         qeth_get_micros() -
2960                         queue->card->perf_stats.outbound_do_qdio_start_time;
2961         atomic_add(count, &queue->used_buffers);
2962         if (rc) {
2963                 queue->card->stats.tx_errors += count;
2964                 /* ignore temporary SIGA errors without busy condition */
2965                 if (rc == QDIO_ERROR_SIGA_TARGET)
2966                         return;
2967                 QETH_CARD_TEXT(queue->card, 2, "flushbuf");
2968                 QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
2969
2970                 /* this must not happen under normal circumstances. if it
2971                  * happens something is really wrong -> recover */
2972                 qeth_schedule_recovery(queue->card);
2973                 return;
2974         }
2975         if (queue->card->options.performance_stats)
2976                 queue->card->perf_stats.bufs_sent += count;
2977 }
2978
2979 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2980 {
2981         int index;
2982         int flush_cnt = 0;
2983         int q_was_packing = 0;
2984
2985         /*
2986          * check if weed have to switch to non-packing mode or if
2987          * we have to get a pci flag out on the queue
2988          */
2989         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
2990             !atomic_read(&queue->set_pci_flags_count)) {
2991                 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
2992                                 QETH_OUT_Q_UNLOCKED) {
2993                         /*
2994                          * If we get in here, there was no action in
2995                          * do_send_packet. So, we check if there is a
2996                          * packing buffer to be flushed here.
2997                          */
2998                         netif_stop_queue(queue->card->dev);
2999                         index = queue->next_buf_to_fill;
3000                         q_was_packing = queue->do_pack;
3001                         /* queue->do_pack may change */
3002                         barrier();
3003                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3004                         if (!flush_cnt &&
3005                             !atomic_read(&queue->set_pci_flags_count))
3006                                 flush_cnt +=
3007                                         qeth_flush_buffers_on_no_pci(queue);
3008                         if (queue->card->options.performance_stats &&
3009                             q_was_packing)
3010                                 queue->card->perf_stats.bufs_sent_pack +=
3011                                         flush_cnt;
3012                         if (flush_cnt)
3013                                 qeth_flush_buffers(queue, index, flush_cnt);
3014                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3015                 }
3016         }
3017 }
3018
3019 void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3020                 unsigned long card_ptr)
3021 {
3022         struct qeth_card *card = (struct qeth_card *)card_ptr;
3023
3024         if (card->dev && (card->dev->flags & IFF_UP))
3025                 napi_schedule(&card->napi);
3026 }
3027 EXPORT_SYMBOL_GPL(qeth_qdio_start_poll);
3028
3029 void qeth_qdio_input_handler(struct ccw_device *ccwdev, unsigned int qdio_err,
3030                 unsigned int queue, int first_element, int count,
3031                 unsigned long card_ptr)
3032 {
3033         struct qeth_card *card = (struct qeth_card *)card_ptr;
3034
3035         if (qdio_err)
3036                 qeth_schedule_recovery(card);
3037 }
3038 EXPORT_SYMBOL_GPL(qeth_qdio_input_handler);
3039
3040 void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3041                 unsigned int qdio_error, int __queue, int first_element,
3042                 int count, unsigned long card_ptr)
3043 {
3044         struct qeth_card *card        = (struct qeth_card *) card_ptr;
3045         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3046         struct qeth_qdio_out_buffer *buffer;
3047         int i;
3048
3049         QETH_CARD_TEXT(card, 6, "qdouhdl");
3050         if (qdio_error & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
3051                 QETH_CARD_TEXT(card, 2, "achkcond");
3052                 netif_stop_queue(card->dev);
3053                 qeth_schedule_recovery(card);
3054                 return;
3055         }
3056         if (card->options.performance_stats) {
3057                 card->perf_stats.outbound_handler_cnt++;
3058                 card->perf_stats.outbound_handler_start_time =
3059                         qeth_get_micros();
3060         }
3061         for (i = first_element; i < (first_element + count); ++i) {
3062                 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
3063                 qeth_handle_send_error(card, buffer, qdio_error);
3064                 qeth_clear_output_buffer(queue, buffer);
3065         }
3066         atomic_sub(count, &queue->used_buffers);
3067         /* check if we need to do something on this outbound queue */
3068         if (card->info.type != QETH_CARD_TYPE_IQD)
3069                 qeth_check_outbound_queue(queue);
3070
3071         netif_wake_queue(queue->card->dev);
3072         if (card->options.performance_stats)
3073                 card->perf_stats.outbound_handler_time += qeth_get_micros() -
3074                         card->perf_stats.outbound_handler_start_time;
3075 }
3076 EXPORT_SYMBOL_GPL(qeth_qdio_output_handler);
3077
3078 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
3079                         int ipv, int cast_type)
3080 {
3081         if (!ipv && (card->info.type == QETH_CARD_TYPE_OSD ||
3082                      card->info.type == QETH_CARD_TYPE_OSX))
3083                 return card->qdio.default_out_queue;
3084         switch (card->qdio.no_out_queues) {
3085         case 4:
3086                 if (cast_type && card->info.is_multicast_different)
3087                         return card->info.is_multicast_different &
3088                                 (card->qdio.no_out_queues - 1);
3089                 if (card->qdio.do_prio_queueing && (ipv == 4)) {
3090                         const u8 tos = ip_hdr(skb)->tos;
3091
3092                         if (card->qdio.do_prio_queueing ==
3093                                 QETH_PRIO_Q_ING_TOS) {
3094                                 if (tos & IP_TOS_NOTIMPORTANT)
3095                                         return 3;
3096                                 if (tos & IP_TOS_HIGHRELIABILITY)
3097                                         return 2;
3098                                 if (tos & IP_TOS_HIGHTHROUGHPUT)
3099                                         return 1;
3100                                 if (tos & IP_TOS_LOWDELAY)
3101                                         return 0;
3102                         }
3103                         if (card->qdio.do_prio_queueing ==
3104                                 QETH_PRIO_Q_ING_PREC)
3105                                 return 3 - (tos >> 6);
3106                 } else if (card->qdio.do_prio_queueing && (ipv == 6)) {
3107                         /* TODO: IPv6!!! */
3108                 }
3109                 return card->qdio.default_out_queue;
3110         case 1: /* fallthrough for single-out-queue 1920-device */
3111         default:
3112                 return card->qdio.default_out_queue;
3113         }
3114 }
3115 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
3116
3117 int qeth_get_elements_no(struct qeth_card *card, void *hdr,
3118                      struct sk_buff *skb, int elems)
3119 {
3120         int dlen = skb->len - skb->data_len;
3121         int elements_needed = PFN_UP((unsigned long)skb->data + dlen - 1) -
3122                 PFN_DOWN((unsigned long)skb->data);
3123
3124         elements_needed += skb_shinfo(skb)->nr_frags;
3125         if ((elements_needed + elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
3126                 QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
3127                         "(Number=%d / Length=%d). Discarded.\n",
3128                         (elements_needed+elems), skb->len);
3129                 return 0;
3130         }
3131         return elements_needed;
3132 }
3133 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
3134
3135 int qeth_hdr_chk_and_bounce(struct sk_buff *skb, int len)
3136 {
3137         int hroom, inpage, rest;
3138
3139         if (((unsigned long)skb->data & PAGE_MASK) !=
3140             (((unsigned long)skb->data + len - 1) & PAGE_MASK)) {
3141                 hroom = skb_headroom(skb);
3142                 inpage = PAGE_SIZE - ((unsigned long) skb->data % PAGE_SIZE);
3143                 rest = len - inpage;
3144                 if (rest > hroom)
3145                         return 1;
3146                 memmove(skb->data - rest, skb->data, skb->len - skb->data_len);
3147                 skb->data -= rest;
3148                 QETH_DBF_MESSAGE(2, "skb bounce len: %d rest: %d\n", len, rest);
3149         }
3150         return 0;
3151 }
3152 EXPORT_SYMBOL_GPL(qeth_hdr_chk_and_bounce);
3153
3154 static inline void __qeth_fill_buffer(struct sk_buff *skb,
3155         struct qdio_buffer *buffer, int is_tso, int *next_element_to_fill,
3156         int offset)
3157 {
3158         int length = skb->len - skb->data_len;
3159         int length_here;
3160         int element;
3161         char *data;
3162         int first_lap, cnt;
3163         struct skb_frag_struct *frag;
3164
3165         element = *next_element_to_fill;
3166         data = skb->data;
3167         first_lap = (is_tso == 0 ? 1 : 0);
3168
3169         if (offset >= 0) {
3170                 data = skb->data + offset;
3171                 length -= offset;
3172                 first_lap = 0;
3173         }
3174
3175         while (length > 0) {
3176                 /* length_here is the remaining amount of data in this page */
3177                 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3178                 if (length < length_here)
3179                         length_here = length;
3180
3181                 buffer->element[element].addr = data;
3182                 buffer->element[element].length = length_here;
3183                 length -= length_here;
3184                 if (!length) {
3185                         if (first_lap)
3186                                 if (skb_shinfo(skb)->nr_frags)
3187                                         buffer->element[element].eflags =
3188                                                 SBAL_EFLAGS_FIRST_FRAG;
3189                                 else
3190                                         buffer->element[element].eflags = 0;
3191                         else
3192                                 buffer->element[element].eflags =
3193                                     SBAL_EFLAGS_MIDDLE_FRAG;
3194                 } else {
3195                         if (first_lap)
3196                                 buffer->element[element].eflags =
3197                                     SBAL_EFLAGS_FIRST_FRAG;
3198                         else
3199                                 buffer->element[element].eflags =
3200                                     SBAL_EFLAGS_MIDDLE_FRAG;
3201                 }
3202                 data += length_here;
3203                 element++;
3204                 first_lap = 0;
3205         }
3206
3207         for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3208                 frag = &skb_shinfo(skb)->frags[cnt];
3209                 buffer->element[element].addr = (char *)page_to_phys(frag->page)
3210                         + frag->page_offset;
3211                 buffer->element[element].length = frag->size;
3212                 buffer->element[element].eflags = SBAL_EFLAGS_MIDDLE_FRAG;
3213                 element++;
3214         }
3215
3216         if (buffer->element[element - 1].eflags)
3217                 buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
3218         *next_element_to_fill = element;
3219 }
3220
3221 static inline int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
3222                 struct qeth_qdio_out_buffer *buf, struct sk_buff *skb,
3223                 struct qeth_hdr *hdr, int offset, int hd_len)
3224 {
3225         struct qdio_buffer *buffer;
3226         int flush_cnt = 0, hdr_len, large_send = 0;
3227
3228         buffer = buf->buffer;
3229         atomic_inc(&skb->users);
3230         skb_queue_tail(&buf->skb_list, skb);
3231
3232         /*check first on TSO ....*/
3233         if (hdr->hdr.l3.id == QETH_HEADER_TYPE_TSO) {
3234                 int element = buf->next_element_to_fill;
3235
3236                 hdr_len = sizeof(struct qeth_hdr_tso) +
3237                         ((struct qeth_hdr_tso *)hdr)->ext.dg_hdr_len;
3238                 /*fill first buffer entry only with header information */
3239                 buffer->element[element].addr = skb->data;
3240                 buffer->element[element].length = hdr_len;
3241                 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
3242                 buf->next_element_to_fill++;
3243                 skb->data += hdr_len;
3244                 skb->len  -= hdr_len;
3245                 large_send = 1;
3246         }
3247
3248         if (offset >= 0) {
3249                 int element = buf->next_element_to_fill;
3250                 buffer->element[element].addr = hdr;
3251                 buffer->element[element].length = sizeof(struct qeth_hdr) +
3252                                                         hd_len;
3253                 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
3254                 buf->is_header[element] = 1;
3255                 buf->next_element_to_fill++;
3256         }
3257
3258         __qeth_fill_buffer(skb, buffer, large_send,
3259                 (int *)&buf->next_element_to_fill, offset);
3260
3261         if (!queue->do_pack) {
3262                 QETH_CARD_TEXT(queue->card, 6, "fillbfnp");
3263                 /* set state to PRIMED -> will be flushed */
3264                 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3265                 flush_cnt = 1;
3266         } else {
3267                 QETH_CARD_TEXT(queue->card, 6, "fillbfpa");
3268                 if (queue->card->options.performance_stats)
3269                         queue->card->perf_stats.skbs_sent_pack++;
3270                 if (buf->next_element_to_fill >=
3271                                 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
3272                         /*
3273                          * packed buffer if full -> set state PRIMED
3274                          * -> will be flushed
3275                          */
3276                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3277                         flush_cnt = 1;
3278                 }
3279         }
3280         return flush_cnt;
3281 }
3282
3283 int qeth_do_send_packet_fast(struct qeth_card *card,
3284                 struct qeth_qdio_out_q *queue, struct sk_buff *skb,
3285                 struct qeth_hdr *hdr, int elements_needed,
3286                 int offset, int hd_len)
3287 {
3288         struct qeth_qdio_out_buffer *buffer;
3289         int index;
3290
3291         /* spin until we get the queue ... */
3292         while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
3293                               QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
3294         /* ... now we've got the queue */
3295         index = queue->next_buf_to_fill;
3296         buffer = &queue->bufs[queue->next_buf_to_fill];
3297         /*
3298          * check if buffer is empty to make sure that we do not 'overtake'
3299          * ourselves and try to fill a buffer that is already primed
3300          */
3301         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
3302                 goto out;
3303         queue->next_buf_to_fill = (queue->next_buf_to_fill + 1) %
3304                                           QDIO_MAX_BUFFERS_PER_Q;
3305         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3306         qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
3307         qeth_flush_buffers(queue, index, 1);
3308         return 0;
3309 out:
3310         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3311         return -EBUSY;
3312 }
3313 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
3314
3315 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3316                 struct sk_buff *skb, struct qeth_hdr *hdr,
3317                 int elements_needed)
3318 {
3319         struct qeth_qdio_out_buffer *buffer;
3320         int start_index;
3321         int flush_count = 0;
3322         int do_pack = 0;
3323         int tmp;
3324         int rc = 0;
3325
3326         /* spin until we get the queue ... */
3327         while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
3328                               QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
3329         start_index = queue->next_buf_to_fill;
3330         buffer = &queue->bufs[queue->next_buf_to_fill];
3331         /*
3332          * check if buffer is empty to make sure that we do not 'overtake'
3333          * ourselves and try to fill a buffer that is already primed
3334          */
3335         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
3336                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3337                 return -EBUSY;
3338         }
3339         /* check if we need to switch packing state of this queue */
3340         qeth_switch_to_packing_if_needed(queue);
3341         if (queue->do_pack) {
3342                 do_pack = 1;
3343                 /* does packet fit in current buffer? */
3344                 if ((QETH_MAX_BUFFER_ELEMENTS(card) -
3345                     buffer->next_element_to_fill) < elements_needed) {
3346                         /* ... no -> set state PRIMED */
3347                         atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3348                         flush_count++;
3349                         queue->next_buf_to_fill =
3350                                 (queue->next_buf_to_fill + 1) %
3351                                 QDIO_MAX_BUFFERS_PER_Q;
3352                         buffer = &queue->bufs[queue->next_buf_to_fill];
3353                         /* we did a step forward, so check buffer state
3354                          * again */
3355                         if (atomic_read(&buffer->state) !=
3356                             QETH_QDIO_BUF_EMPTY) {
3357                                 qeth_flush_buffers(queue, start_index,
3358                                                            flush_count);
3359                                 atomic_set(&queue->state,
3360                                                 QETH_OUT_Q_UNLOCKED);
3361                                 return -EBUSY;
3362                         }
3363                 }
3364         }
3365         tmp = qeth_fill_buffer(queue, buffer, skb, hdr, -1, 0);
3366         queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
3367                                   QDIO_MAX_BUFFERS_PER_Q;
3368         flush_count += tmp;
3369         if (flush_count)
3370                 qeth_flush_buffers(queue, start_index, flush_count);
3371         else if (!atomic_read(&queue->set_pci_flags_count))
3372                 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
3373         /*
3374          * queue->state will go from LOCKED -> UNLOCKED or from
3375          * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
3376          * (switch packing state or flush buffer to get another pci flag out).
3377          * In that case we will enter this loop
3378          */
3379         while (atomic_dec_return(&queue->state)) {
3380                 flush_count = 0;
3381                 start_index = queue->next_buf_to_fill;
3382                 /* check if we can go back to non-packing state */
3383                 flush_count += qeth_switch_to_nonpacking_if_needed(queue);
3384                 /*
3385                  * check if we need to flush a packing buffer to get a pci
3386                  * flag out on the queue
3387                  */
3388                 if (!flush_count && !atomic_read(&queue->set_pci_flags_count))
3389                         flush_count += qeth_flush_buffers_on_no_pci(queue);
3390                 if (flush_count)
3391                         qeth_flush_buffers(queue, start_index, flush_count);
3392         }
3393         /* at this point the queue is UNLOCKED again */
3394         if (queue->card->options.performance_stats && do_pack)
3395                 queue->card->perf_stats.bufs_sent_pack += flush_count;
3396
3397         return rc;
3398 }
3399 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
3400
3401 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
3402                 struct qeth_reply *reply, unsigned long data)
3403 {
3404         struct qeth_ipa_cmd *cmd;
3405         struct qeth_ipacmd_setadpparms *setparms;
3406
3407         QETH_CARD_TEXT(card, 4, "prmadpcb");
3408
3409         cmd = (struct qeth_ipa_cmd *) data;
3410         setparms = &(cmd->data.setadapterparms);
3411
3412         qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
3413         if (cmd->hdr.return_code) {
3414                 QETH_CARD_TEXT_(card, 4, "prmrc%2.2x", cmd->hdr.return_code);
3415                 setparms->data.mode = SET_PROMISC_MODE_OFF;
3416         }
3417         card->info.promisc_mode = setparms->data.mode;
3418         return 0;
3419 }
3420
3421 void qeth_setadp_promisc_mode(struct qeth_card *card)
3422 {
3423         enum qeth_ipa_promisc_modes mode;
3424         struct net_device *dev = card->dev;
3425         struct qeth_cmd_buffer *iob;
3426         struct qeth_ipa_cmd *cmd;
3427
3428         QETH_CARD_TEXT(card, 4, "setprom");
3429
3430         if (((dev->flags & IFF_PROMISC) &&
3431              (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
3432             (!(dev->flags & IFF_PROMISC) &&
3433              (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
3434                 return;
3435         mode = SET_PROMISC_MODE_OFF;
3436         if (dev->flags & IFF_PROMISC)
3437                 mode = SET_PROMISC_MODE_ON;
3438         QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
3439
3440         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
3441                         sizeof(struct qeth_ipacmd_setadpparms));
3442         cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
3443         cmd->data.setadapterparms.data.mode = mode;
3444         qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
3445 }
3446 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
3447
3448 int qeth_change_mtu(struct net_device *dev, int new_mtu)
3449 {
3450         struct qeth_card *card;
3451         char dbf_text[15];
3452
3453         card = dev->ml_priv;
3454
3455         QETH_CARD_TEXT(card, 4, "chgmtu");
3456         sprintf(dbf_text, "%8x", new_mtu);
3457         QETH_CARD_TEXT(card, 4, dbf_text);
3458
3459         if (new_mtu < 64)
3460                 return -EINVAL;
3461         if (new_mtu > 65535)
3462                 return -EINVAL;
3463         if ((!qeth_is_supported(card, IPA_IP_FRAGMENTATION)) &&
3464             (!qeth_mtu_is_valid(card, new_mtu)))
3465                 return -EINVAL;
3466         dev->mtu = new_mtu;
3467         return 0;
3468 }
3469 EXPORT_SYMBOL_GPL(qeth_change_mtu);
3470
3471 struct net_device_stats *qeth_get_stats(struct net_device *dev)
3472 {
3473         struct qeth_card *card;
3474
3475         card = dev->ml_priv;
3476
3477         QETH_CARD_TEXT(card, 5, "getstat");
3478
3479         return &card->stats;
3480 }
3481 EXPORT_SYMBOL_GPL(qeth_get_stats);
3482
3483 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
3484                 struct qeth_reply *reply, unsigned long data)
3485 {
3486         struct qeth_ipa_cmd *cmd;
3487
3488         QETH_CARD_TEXT(card, 4, "chgmaccb");
3489
3490         cmd = (struct qeth_ipa_cmd *) data;
3491         if (!card->options.layer2 ||
3492             !(card->info.mac_bits & QETH_LAYER2_MAC_READ)) {
3493                 memcpy(card->dev->dev_addr,
3494                        &cmd->data.setadapterparms.data.change_addr.addr,
3495                        OSA_ADDR_LEN);
3496                 card->info.mac_bits |= QETH_LAYER2_MAC_READ;
3497         }
3498         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3499         return 0;
3500 }
3501
3502 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
3503 {
3504         int rc;
3505         struct qeth_cmd_buffer *iob;
3506         struct qeth_ipa_cmd *cmd;
3507
3508         QETH_CARD_TEXT(card, 4, "chgmac");
3509
3510         iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
3511                                    sizeof(struct qeth_ipacmd_setadpparms));
3512         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3513         cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
3514         cmd->data.setadapterparms.data.change_addr.addr_size = OSA_ADDR_LEN;
3515         memcpy(&cmd->data.setadapterparms.data.change_addr.addr,
3516                card->dev->dev_addr, OSA_ADDR_LEN);
3517         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
3518                                NULL);
3519         return rc;
3520 }
3521 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
3522
3523 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
3524                 struct qeth_reply *reply, unsigned long data)
3525 {
3526         struct qeth_ipa_cmd *cmd;
3527         struct qeth_set_access_ctrl *access_ctrl_req;
3528
3529         QETH_CARD_TEXT(card, 4, "setaccb");
3530
3531         cmd = (struct qeth_ipa_cmd *) data;
3532         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
3533         QETH_DBF_TEXT_(SETUP, 2, "setaccb");
3534         QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
3535         QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
3536                 cmd->data.setadapterparms.hdr.return_code);
3537         switch (cmd->data.setadapterparms.hdr.return_code) {
3538         case SET_ACCESS_CTRL_RC_SUCCESS:
3539         case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
3540         case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
3541         {
3542                 card->options.isolation = access_ctrl_req->subcmd_code;
3543                 if (card->options.isolation == ISOLATION_MODE_NONE) {
3544                         dev_info(&card->gdev->dev,
3545                             "QDIO data connection isolation is deactivated\n");
3546                 } else {
3547                         dev_info(&card->gdev->dev,
3548                             "QDIO data connection isolation is activated\n");
3549                 }
3550                 QETH_DBF_MESSAGE(3, "OK:SET_ACCESS_CTRL(%s, %d)==%d\n",
3551                         card->gdev->dev.kobj.name,
3552                         access_ctrl_req->subcmd_code,
3553                         cmd->data.setadapterparms.hdr.return_code);
3554                 break;
3555         }
3556         case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
3557         {
3558                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
3559                         card->gdev->dev.kobj.name,
3560                         access_ctrl_req->subcmd_code,
3561                         cmd->data.setadapterparms.hdr.return_code);
3562                 dev_err(&card->gdev->dev, "Adapter does not "
3563                         "support QDIO data connection isolation\n");
3564
3565                 /* ensure isolation mode is "none" */
3566                 card->options.isolation = ISOLATION_MODE_NONE;
3567                 break;
3568         }
3569         case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
3570         {
3571                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d\n",
3572                         card->gdev->dev.kobj.name,
3573                         access_ctrl_req->subcmd_code,
3574                         cmd->data.setadapterparms.hdr.return_code);
3575                 dev_err(&card->gdev->dev,
3576                         "Adapter is dedicated. "
3577                         "QDIO data connection isolation not supported\n");
3578
3579                 /* ensure isolation mode is "none" */
3580                 card->options.isolation = ISOLATION_MODE_NONE;
3581                 break;
3582         }
3583         case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
3584         {
3585                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d\n",
3586                         card->gdev->dev.kobj.name,
3587                         access_ctrl_req->subcmd_code,
3588                         cmd->data.setadapterparms.hdr.return_code);
3589                 dev_err(&card->gdev->dev,
3590                         "TSO does not permit QDIO data connection isolation\n");
3591
3592                 /* ensure isolation mode is "none" */
3593                 card->options.isolation = ISOLATION_MODE_NONE;
3594                 break;
3595         }
3596         default:
3597         {
3598                 /* this should never happen */
3599                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d"
3600                         "==UNKNOWN\n",
3601                         card->gdev->dev.kobj.name,
3602                         access_ctrl_req->subcmd_code,
3603                         cmd->data.setadapterparms.hdr.return_code);
3604
3605                 /* ensure isolation mode is "none" */
3606                 card->options.isolation = ISOLATION_MODE_NONE;
3607                 break;
3608         }
3609         }
3610         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3611         return 0;
3612 }
3613
3614 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
3615                 enum qeth_ipa_isolation_modes isolation)
3616 {
3617         int rc;
3618         struct qeth_cmd_buffer *iob;
3619         struct qeth_ipa_cmd *cmd;
3620         struct qeth_set_access_ctrl *access_ctrl_req;
3621
3622         QETH_CARD_TEXT(card, 4, "setacctl");
3623
3624         QETH_DBF_TEXT_(SETUP, 2, "setacctl");
3625         QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
3626
3627         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
3628                                    sizeof(struct qeth_ipacmd_setadpparms_hdr) +
3629                                    sizeof(struct qeth_set_access_ctrl));
3630         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3631         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
3632         access_ctrl_req->subcmd_code = isolation;
3633
3634         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
3635                                NULL);
3636         QETH_DBF_TEXT_(SETUP, 2, "rc=%d", rc);
3637         return rc;
3638 }
3639
3640 int qeth_set_access_ctrl_online(struct qeth_card *card)
3641 {
3642         int rc = 0;
3643
3644         QETH_CARD_TEXT(card, 4, "setactlo");
3645
3646         if ((card->info.type == QETH_CARD_TYPE_OSD ||
3647              card->info.type == QETH_CARD_TYPE_OSX) &&
3648              qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
3649                 rc = qeth_setadpparms_set_access_ctrl(card,
3650                         card->options.isolation);
3651                 if (rc) {
3652                         QETH_DBF_MESSAGE(3,
3653                                 "IPA(SET_ACCESS_CTRL,%s,%d) sent failed\n",
3654                                 card->gdev->dev.kobj.name,
3655                                 rc);
3656                 }
3657         } else if (card->options.isolation != ISOLATION_MODE_NONE) {
3658                 card->options.isolation = ISOLATION_MODE_NONE;
3659
3660                 dev_err(&card->gdev->dev, "Adapter does not "
3661                         "support QDIO data connection isolation\n");
3662                 rc = -EOPNOTSUPP;
3663         }
3664         return rc;
3665 }
3666 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
3667
3668 void qeth_tx_timeout(struct net_device *dev)
3669 {
3670         struct qeth_card *card;
3671
3672         card = dev->ml_priv;
3673         QETH_CARD_TEXT(card, 4, "txtimeo");
3674         card->stats.tx_errors++;
3675         qeth_schedule_recovery(card);
3676 }
3677 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
3678
3679 int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
3680 {
3681         struct qeth_card *card = dev->ml_priv;
3682         int rc = 0;
3683
3684         switch (regnum) {
3685         case MII_BMCR: /* Basic mode control register */
3686                 rc = BMCR_FULLDPLX;
3687                 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
3688                     (card->info.link_type != QETH_LINK_TYPE_OSN) &&
3689                     (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
3690                         rc |= BMCR_SPEED100;
3691                 break;
3692         case MII_BMSR: /* Basic mode status register */
3693                 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
3694                      BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
3695                      BMSR_100BASE4;
3696                 break;
3697         case MII_PHYSID1: /* PHYS ID 1 */
3698                 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
3699                      dev->dev_addr[2];
3700                 rc = (rc >> 5) & 0xFFFF;
3701                 break;
3702         case MII_PHYSID2: /* PHYS ID 2 */
3703                 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
3704                 break;
3705         case MII_ADVERTISE: /* Advertisement control reg */
3706                 rc = ADVERTISE_ALL;
3707                 break;
3708         case MII_LPA: /* Link partner ability reg */
3709                 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
3710                      LPA_100BASE4 | LPA_LPACK;
3711                 break;
3712         case MII_EXPANSION: /* Expansion register */
3713                 break;
3714         case MII_DCOUNTER: /* disconnect counter */
3715                 break;
3716         case MII_FCSCOUNTER: /* false carrier counter */
3717                 break;
3718         case MII_NWAYTEST: /* N-way auto-neg test register */
3719                 break;
3720         case MII_RERRCOUNTER: /* rx error counter */
3721                 rc = card->stats.rx_errors;
3722                 break;
3723         case MII_SREVISION: /* silicon revision */
3724                 break;
3725         case MII_RESV1: /* reserved 1 */
3726                 break;
3727         case MII_LBRERROR: /* loopback, rx, bypass error */
3728                 break;
3729         case MII_PHYADDR: /* physical address */
3730                 break;
3731         case MII_RESV2: /* reserved 2 */
3732                 break;
3733         case MII_TPISTATUS: /* TPI status for 10mbps */
3734                 break;
3735         case MII_NCONFIG: /* network interface config */
3736                 break;
3737         default:
3738                 break;
3739         }
3740         return rc;
3741 }
3742 EXPORT_SYMBOL_GPL(qeth_mdio_read);
3743
3744 static int qeth_send_ipa_snmp_cmd(struct qeth_card *card,
3745                 struct qeth_cmd_buffer *iob, int len,
3746                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
3747                         unsigned long),
3748                 void *reply_param)
3749 {
3750         u16 s1, s2;
3751
3752         QETH_CARD_TEXT(card, 4, "sendsnmp");
3753
3754         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
3755         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
3756                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3757         /* adjust PDU length fields in IPA_PDU_HEADER */
3758         s1 = (u32) IPA_PDU_HEADER_SIZE + len;
3759         s2 = (u32) len;
3760         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
3761         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
3762         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
3763         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
3764         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
3765                                       reply_cb, reply_param);
3766 }
3767
3768 static int qeth_snmp_command_cb(struct qeth_card *card,
3769                 struct qeth_reply *reply, unsigned long sdata)
3770 {
3771         struct qeth_ipa_cmd *cmd;
3772         struct qeth_arp_query_info *qinfo;
3773         struct qeth_snmp_cmd *snmp;
3774         unsigned char *data;
3775         __u16 data_len;
3776
3777         QETH_CARD_TEXT(card, 3, "snpcmdcb");
3778
3779         cmd = (struct qeth_ipa_cmd *) sdata;
3780         data = (unsigned char *)((char *)cmd - reply->offset);
3781         qinfo = (struct qeth_arp_query_info *) reply->param;
3782         snmp = &cmd->data.setadapterparms.data.snmp;
3783
3784         if (cmd->hdr.return_code) {
3785                 QETH_CARD_TEXT_(card, 4, "scer1%i", cmd->hdr.return_code);
3786                 return 0;
3787         }
3788         if (cmd->data.setadapterparms.hdr.return_code) {
3789                 cmd->hdr.return_code =
3790                         cmd->data.setadapterparms.hdr.return_code;
3791                 QETH_CARD_TEXT_(card, 4, "scer2%i", cmd->hdr.return_code);
3792                 return 0;
3793         }
3794         data_len = *((__u16 *)QETH_IPA_PDU_LEN_PDU1(data));
3795         if (cmd->data.setadapterparms.hdr.seq_no == 1)
3796                 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
3797         else
3798                 data_len -= (__u16)((char *)&snmp->request - (char *)cmd);
3799
3800         /* check if there is enough room in userspace */
3801         if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
3802                 QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOMEM);
3803                 cmd->hdr.return_code = -ENOMEM;
3804                 return 0;
3805         }
3806         QETH_CARD_TEXT_(card, 4, "snore%i",
3807                        cmd->data.setadapterparms.hdr.used_total);
3808         QETH_CARD_TEXT_(card, 4, "sseqn%i",
3809                 cmd->data.setadapterparms.hdr.seq_no);
3810         /*copy entries to user buffer*/
3811         if (cmd->data.setadapterparms.hdr.seq_no == 1) {
3812                 memcpy(qinfo->udata + qinfo->udata_offset,
3813                        (char *)snmp,
3814                        data_len + offsetof(struct qeth_snmp_cmd, data));
3815                 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
3816         } else {
3817                 memcpy(qinfo->udata + qinfo->udata_offset,
3818                        (char *)&snmp->request, data_len);
3819         }
3820         qinfo->udata_offset += data_len;
3821         /* check if all replies received ... */
3822                 QETH_CARD_TEXT_(card, 4, "srtot%i",
3823                                cmd->data.setadapterparms.hdr.used_total);
3824                 QETH_CARD_TEXT_(card, 4, "srseq%i",
3825                                cmd->data.setadapterparms.hdr.seq_no);
3826         if (cmd->data.setadapterparms.hdr.seq_no <
3827             cmd->data.setadapterparms.hdr.used_total)
3828                 return 1;
3829         return 0;
3830 }
3831
3832 int qeth_snmp_command(struct qeth_card *card, char __user *udata)
3833 {
3834         struct qeth_cmd_buffer *iob;
3835         struct qeth_ipa_cmd *cmd;
3836         struct qeth_snmp_ureq *ureq;
3837         int req_len;
3838         struct qeth_arp_query_info qinfo = {0, };
3839         int rc = 0;
3840
3841         QETH_CARD_TEXT(card, 3, "snmpcmd");
3842
3843         if (card->info.guestlan)
3844                 return -EOPNOTSUPP;
3845
3846         if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
3847             (!card->options.layer2)) {
3848                 return -EOPNOTSUPP;
3849         }
3850         /* skip 4 bytes (data_len struct member) to get req_len */
3851         if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
3852                 return -EFAULT;
3853         ureq = memdup_user(udata, req_len + sizeof(struct qeth_snmp_ureq_hdr));
3854         if (IS_ERR(ureq)) {
3855                 QETH_CARD_TEXT(card, 2, "snmpnome");
3856                 return PTR_ERR(ureq);
3857         }
3858         qinfo.udata_len = ureq->hdr.data_len;
3859         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
3860         if (!qinfo.udata) {
3861                 kfree(ureq);
3862                 return -ENOMEM;
3863         }
3864         qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
3865
3866         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
3867                                    QETH_SNMP_SETADP_CMDLENGTH + req_len);
3868         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3869         memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
3870         rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
3871                                     qeth_snmp_command_cb, (void *)&qinfo);
3872         if (rc)
3873                 QETH_DBF_MESSAGE(2, "SNMP command failed on %s: (0x%x)\n",
3874                            QETH_CARD_IFNAME(card), rc);
3875         else {
3876                 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
3877                         rc = -EFAULT;
3878         }
3879
3880         kfree(ureq);
3881         kfree(qinfo.udata);
3882         return rc;
3883 }
3884 EXPORT_SYMBOL_GPL(qeth_snmp_command);
3885
3886 static inline int qeth_get_qdio_q_format(struct qeth_card *card)
3887 {
3888         switch (card->info.type) {
3889         case QETH_CARD_TYPE_IQD:
3890                 return 2;
3891         default:
3892                 return 0;
3893         }
3894 }
3895
3896 static void qeth_determine_capabilities(struct qeth_card *card)
3897 {
3898         int rc;
3899         int length;
3900         char *prcd;
3901         struct ccw_device *ddev;
3902         int ddev_offline = 0;
3903
3904         QETH_DBF_TEXT(SETUP, 2, "detcapab");
3905         ddev = CARD_DDEV(card);
3906         if (!ddev->online) {
3907                 ddev_offline = 1;
3908                 rc = ccw_device_set_online(ddev);
3909                 if (rc) {
3910                         QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
3911                         goto out;
3912                 }
3913         }
3914
3915         rc = qeth_read_conf_data(card, (void **) &prcd, &length);
3916         if (rc) {
3917                 QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
3918                         dev_name(&card->gdev->dev), rc);
3919                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
3920                 goto out_offline;
3921         }
3922         qeth_configure_unitaddr(card, prcd);
3923         qeth_configure_blkt_default(card, prcd);
3924         kfree(prcd);
3925
3926         rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
3927         if (rc)
3928                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
3929
3930 out_offline:
3931         if (ddev_offline == 1)
3932                 ccw_device_set_offline(ddev);
3933 out:
3934         return;
3935 }
3936
3937 static int qeth_qdio_establish(struct qeth_card *card)
3938 {
3939         struct qdio_initialize init_data;
3940         char *qib_param_field;
3941         struct qdio_buffer **in_sbal_ptrs;
3942         struct qdio_buffer **out_sbal_ptrs;
3943         int i, j, k;
3944         int rc = 0;
3945
3946         QETH_DBF_TEXT(SETUP, 2, "qdioest");
3947
3948         qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
3949                               GFP_KERNEL);
3950         if (!qib_param_field)
3951                 return -ENOMEM;
3952
3953         qeth_create_qib_param_field(card, qib_param_field);
3954         qeth_create_qib_param_field_blkt(card, qib_param_field);
3955
3956         in_sbal_ptrs = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
3957                                GFP_KERNEL);
3958         if (!in_sbal_ptrs) {
3959                 kfree(qib_param_field);
3960                 return -ENOMEM;
3961         }
3962         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3963                 in_sbal_ptrs[i] = (struct qdio_buffer *)
3964                         virt_to_phys(card->qdio.in_q->bufs[i].buffer);
3965
3966         out_sbal_ptrs =
3967                 kmalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
3968                         sizeof(void *), GFP_KERNEL);
3969         if (!out_sbal_ptrs) {
3970                 kfree(in_sbal_ptrs);
3971                 kfree(qib_param_field);
3972                 return -ENOMEM;
3973         }
3974         for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
3975                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
3976                         out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
3977                                 card->qdio.out_qs[i]->bufs[j].buffer);
3978                 }
3979
3980         memset(&init_data, 0, sizeof(struct qdio_initialize));
3981         init_data.cdev                   = CARD_DDEV(card);
3982         init_data.q_format               = qeth_get_qdio_q_format(card);
3983         init_data.qib_param_field_format = 0;
3984         init_data.qib_param_field        = qib_param_field;
3985         init_data.no_input_qs            = 1;
3986         init_data.no_output_qs           = card->qdio.no_out_queues;
3987         init_data.input_handler          = card->discipline.input_handler;
3988         init_data.output_handler         = card->discipline.output_handler;
3989         init_data.queue_start_poll       = card->discipline.start_poll;
3990         init_data.int_parm               = (unsigned long) card;
3991         init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
3992         init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
3993         init_data.scan_threshold =
3994                 (card->info.type == QETH_CARD_TYPE_IQD) ? 8 : 32;
3995
3996         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
3997                 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
3998                 rc = qdio_allocate(&init_data);
3999                 if (rc) {
4000                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4001                         goto out;
4002                 }
4003                 rc = qdio_establish(&init_data);
4004                 if (rc) {
4005                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4006                         qdio_free(CARD_DDEV(card));
4007                 }
4008         }
4009 out:
4010         kfree(out_sbal_ptrs);
4011         kfree(in_sbal_ptrs);
4012         kfree(qib_param_field);
4013         return rc;
4014 }
4015
4016 static void qeth_core_free_card(struct qeth_card *card)
4017 {
4018
4019         QETH_DBF_TEXT(SETUP, 2, "freecrd");
4020         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
4021         qeth_clean_channel(&card->read);
4022         qeth_clean_channel(&card->write);
4023         if (card->dev)
4024                 free_netdev(card->dev);
4025         kfree(card->ip_tbd_list);
4026         qeth_free_qdio_buffers(card);
4027         unregister_service_level(&card->qeth_service_level);
4028         kfree(card);
4029 }
4030
4031 static struct ccw_device_id qeth_ids[] = {
4032         {CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
4033                                         .driver_info = QETH_CARD_TYPE_OSD},
4034         {CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
4035                                         .driver_info = QETH_CARD_TYPE_IQD},
4036         {CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
4037                                         .driver_info = QETH_CARD_TYPE_OSN},
4038         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
4039                                         .driver_info = QETH_CARD_TYPE_OSM},
4040         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
4041                                         .driver_info = QETH_CARD_TYPE_OSX},
4042         {},
4043 };
4044 MODULE_DEVICE_TABLE(ccw, qeth_ids);
4045
4046 static struct ccw_driver qeth_ccw_driver = {
4047         .driver = {
4048                 .owner = THIS_MODULE,
4049                 .name = "qeth",
4050         },
4051         .ids = qeth_ids,
4052         .probe = ccwgroup_probe_ccwdev,
4053         .remove = ccwgroup_remove_ccwdev,
4054 };
4055
4056 static int qeth_core_driver_group(const char *buf, struct device *root_dev,
4057                                 unsigned long driver_id)
4058 {
4059         return ccwgroup_create_from_string(root_dev, driver_id,
4060                                            &qeth_ccw_driver, 3, buf);
4061 }
4062
4063 int qeth_core_hardsetup_card(struct qeth_card *card)
4064 {
4065         int retries = 0;
4066         int rc;
4067
4068         QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
4069         atomic_set(&card->force_alloc_skb, 0);
4070         qeth_get_channel_path_desc(card);
4071 retry:
4072         if (retries)
4073                 QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
4074                         dev_name(&card->gdev->dev));
4075         ccw_device_set_offline(CARD_DDEV(card));
4076         ccw_device_set_offline(CARD_WDEV(card));
4077         ccw_device_set_offline(CARD_RDEV(card));
4078         rc = ccw_device_set_online(CARD_RDEV(card));
4079         if (rc)
4080                 goto retriable;
4081         rc = ccw_device_set_online(CARD_WDEV(card));
4082         if (rc)
4083                 goto retriable;
4084         rc = ccw_device_set_online(CARD_DDEV(card));
4085         if (rc)
4086                 goto retriable;
4087         rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
4088 retriable:
4089         if (rc == -ERESTARTSYS) {
4090                 QETH_DBF_TEXT(SETUP, 2, "break1");
4091                 return rc;
4092         } else if (rc) {
4093                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
4094                 if (++retries > 3)
4095                         goto out;
4096                 else
4097                         goto retry;
4098         }
4099         qeth_determine_capabilities(card);
4100         qeth_init_tokens(card);
4101         qeth_init_func_level(card);
4102         rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
4103         if (rc == -ERESTARTSYS) {
4104                 QETH_DBF_TEXT(SETUP, 2, "break2");
4105                 return rc;
4106         } else if (rc) {
4107                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4108                 if (--retries < 0)
4109                         goto out;
4110                 else
4111                         goto retry;
4112         }
4113         rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
4114         if (rc == -ERESTARTSYS) {
4115                 QETH_DBF_TEXT(SETUP, 2, "break3");
4116                 return rc;
4117         } else if (rc) {
4118                 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
4119                 if (--retries < 0)
4120                         goto out;
4121                 else
4122                         goto retry;
4123         }
4124         card->read_or_write_problem = 0;
4125         rc = qeth_mpc_initialize(card);
4126         if (rc) {
4127                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4128                 goto out;
4129         }
4130
4131         card->options.ipa4.supported_funcs = 0;
4132         card->options.adp.supported_funcs = 0;
4133         card->info.diagass_support = 0;
4134         qeth_query_ipassists(card, QETH_PROT_IPV4);
4135         if (qeth_is_supported(card, IPA_SETADAPTERPARMS))
4136                 qeth_query_setadapterparms(card);
4137         if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST))
4138                 qeth_query_setdiagass(card);
4139         return 0;
4140 out:
4141         dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
4142                 "an error on the device\n");
4143         QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
4144                 dev_name(&card->gdev->dev), rc);
4145         return rc;
4146 }
4147 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
4148
4149 static inline int qeth_create_skb_frag(struct qdio_buffer_element *element,
4150                 struct sk_buff **pskb, int offset, int *pfrag, int data_len)
4151 {
4152         struct page *page = virt_to_page(element->addr);
4153         if (*pskb == NULL) {
4154                 /* the upper protocol layers assume that there is data in the
4155                  * skb itself. Copy a small amount (64 bytes) to make them
4156                  * happy. */
4157                 *pskb = dev_alloc_skb(64 + ETH_HLEN);
4158                 if (!(*pskb))
4159                         return -ENOMEM;
4160                 skb_reserve(*pskb, ETH_HLEN);
4161                 if (data_len <= 64) {
4162                         memcpy(skb_put(*pskb, data_len), element->addr + offset,
4163                                 data_len);
4164                 } else {
4165                         get_page(page);
4166                         memcpy(skb_put(*pskb, 64), element->addr + offset, 64);
4167                         skb_fill_page_desc(*pskb, *pfrag, page, offset + 64,
4168                                 data_len - 64);
4169                         (*pskb)->data_len += data_len - 64;
4170                         (*pskb)->len      += data_len - 64;
4171                         (*pskb)->truesize += data_len - 64;
4172                         (*pfrag)++;
4173                 }
4174         } else {
4175                 get_page(page);
4176                 skb_fill_page_desc(*pskb, *pfrag, page, offset, data_len);
4177                 (*pskb)->data_len += data_len;
4178                 (*pskb)->len      += data_len;
4179                 (*pskb)->truesize += data_len;
4180                 (*pfrag)++;
4181         }
4182         return 0;
4183 }
4184
4185 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
4186                 struct qdio_buffer *buffer,
4187                 struct qdio_buffer_element **__element, int *__offset,
4188                 struct qeth_hdr **hdr)
4189 {
4190         struct qdio_buffer_element *element = *__element;
4191         int offset = *__offset;
4192         struct sk_buff *skb = NULL;
4193         int skb_len = 0;
4194         void *data_ptr;
4195         int data_len;
4196         int headroom = 0;
4197         int use_rx_sg = 0;
4198         int frag = 0;
4199
4200         /* qeth_hdr must not cross element boundaries */
4201         if (element->length < offset + sizeof(struct qeth_hdr)) {
4202                 if (qeth_is_last_sbale(element))
4203                         return NULL;
4204                 element++;
4205                 offset = 0;
4206                 if (element->length < sizeof(struct qeth_hdr))
4207                         return NULL;
4208         }
4209         *hdr = element->addr + offset;
4210
4211         offset += sizeof(struct qeth_hdr);
4212         switch ((*hdr)->hdr.l2.id) {
4213         case QETH_HEADER_TYPE_LAYER2:
4214                 skb_len = (*hdr)->hdr.l2.pkt_length;
4215                 break;
4216         case QETH_HEADER_TYPE_LAYER3:
4217                 skb_len = (*hdr)->hdr.l3.length;
4218                 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
4219                     (card->info.link_type == QETH_LINK_TYPE_HSTR))
4220                         headroom = TR_HLEN;
4221                 else
4222                         headroom = ETH_HLEN;
4223                 break;
4224         case QETH_HEADER_TYPE_OSN:
4225                 skb_len = (*hdr)->hdr.osn.pdu_length;
4226                 headroom = sizeof(struct qeth_hdr);
4227                 break;
4228         default:
4229                 break;
4230         }
4231
4232         if (!skb_len)
4233                 return NULL;
4234
4235         if ((skb_len >= card->options.rx_sg_cb) &&
4236             (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
4237             (!atomic_read(&card->force_alloc_skb))) {
4238                 use_rx_sg = 1;
4239         } else {
4240                 skb = dev_alloc_skb(skb_len + headroom);
4241                 if (!skb)
4242                         goto no_mem;
4243                 if (headroom)
4244                         skb_reserve(skb, headroom);
4245         }
4246
4247         data_ptr = element->addr + offset;
4248         while (skb_len) {
4249                 data_len = min(skb_len, (int)(element->length - offset));
4250                 if (data_len) {
4251                         if (use_rx_sg) {
4252                                 if (qeth_create_skb_frag(element, &skb, offset,
4253                                     &frag, data_len))
4254                                         goto no_mem;
4255                         } else {
4256                                 memcpy(skb_put(skb, data_len), data_ptr,
4257                                         data_len);
4258                         }
4259                 }
4260                 skb_len -= data_len;
4261                 if (skb_len) {
4262                         if (qeth_is_last_sbale(element)) {
4263                                 QETH_CARD_TEXT(card, 4, "unexeob");
4264                                 QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
4265                                 dev_kfree_skb_any(skb);
4266                                 card->stats.rx_errors++;
4267                                 return NULL;
4268                         }
4269                         element++;
4270                         offset = 0;
4271                         data_ptr = element->addr;
4272                 } else {
4273                         offset += data_len;
4274                 }
4275         }
4276         *__element = element;
4277         *__offset = offset;
4278         if (use_rx_sg && card->options.performance_stats) {
4279                 card->perf_stats.sg_skbs_rx++;
4280                 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
4281         }
4282         return skb;
4283 no_mem:
4284         if (net_ratelimit()) {
4285                 QETH_CARD_TEXT(card, 2, "noskbmem");
4286         }
4287         card->stats.rx_dropped++;
4288         return NULL;
4289 }
4290 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
4291
4292 static void qeth_unregister_dbf_views(void)
4293 {
4294         int x;
4295         for (x = 0; x < QETH_DBF_INFOS; x++) {
4296                 debug_unregister(qeth_dbf[x].id);
4297                 qeth_dbf[x].id = NULL;
4298         }
4299 }
4300
4301 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
4302 {
4303         char dbf_txt_buf[32];
4304         va_list args;
4305
4306         if (level > id->level)
4307                 return;
4308         va_start(args, fmt);
4309         vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
4310         va_end(args);
4311         debug_text_event(id, level, dbf_txt_buf);
4312 }
4313 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
4314
4315 static int qeth_register_dbf_views(void)
4316 {
4317         int ret;
4318         int x;
4319
4320         for (x = 0; x < QETH_DBF_INFOS; x++) {
4321                 /* register the areas */
4322                 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
4323                                                 qeth_dbf[x].pages,
4324                                                 qeth_dbf[x].areas,
4325                                                 qeth_dbf[x].len);
4326                 if (qeth_dbf[x].id == NULL) {
4327                         qeth_unregister_dbf_views();
4328                         return -ENOMEM;
4329                 }
4330
4331                 /* register a view */
4332                 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
4333                 if (ret) {
4334                         qeth_unregister_dbf_views();
4335                         return ret;
4336                 }
4337
4338                 /* set a passing level */
4339                 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
4340         }
4341
4342         return 0;
4343 }
4344
4345 int qeth_core_load_discipline(struct qeth_card *card,
4346                 enum qeth_discipline_id discipline)
4347 {
4348         int rc = 0;
4349         switch (discipline) {
4350         case QETH_DISCIPLINE_LAYER3:
4351                 card->discipline.ccwgdriver = try_then_request_module(
4352                         symbol_get(qeth_l3_ccwgroup_driver),
4353                         "qeth_l3");
4354                 break;
4355         case QETH_DISCIPLINE_LAYER2:
4356                 card->discipline.ccwgdriver = try_then_request_module(
4357                         symbol_get(qeth_l2_ccwgroup_driver),
4358                         "qeth_l2");
4359                 break;
4360         }
4361         if (!card->discipline.ccwgdriver) {
4362                 dev_err(&card->gdev->dev, "There is no kernel module to "
4363                         "support discipline %d\n", discipline);
4364                 rc = -EINVAL;
4365         }
4366         return rc;
4367 }
4368
4369 void qeth_core_free_discipline(struct qeth_card *card)
4370 {
4371         if (card->options.layer2)
4372                 symbol_put(qeth_l2_ccwgroup_driver);
4373         else
4374                 symbol_put(qeth_l3_ccwgroup_driver);
4375         card->discipline.ccwgdriver = NULL;
4376 }
4377
4378 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
4379 {
4380         struct qeth_card *card;
4381         struct device *dev;
4382         int rc;
4383         unsigned long flags;
4384         char dbf_name[20];
4385
4386         QETH_DBF_TEXT(SETUP, 2, "probedev");
4387
4388         dev = &gdev->dev;
4389         if (!get_device(dev))
4390                 return -ENODEV;
4391
4392         QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
4393
4394         card = qeth_alloc_card();
4395         if (!card) {
4396                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
4397                 rc = -ENOMEM;
4398                 goto err_dev;
4399         }
4400
4401         snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
4402                 dev_name(&gdev->dev));
4403         card->debug = debug_register(dbf_name, 2, 1, 8);
4404         if (!card->debug) {
4405                 QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
4406                 rc = -ENOMEM;
4407                 goto err_card;
4408         }
4409         debug_register_view(card->debug, &debug_hex_ascii_view);
4410
4411         card->read.ccwdev  = gdev->cdev[0];
4412         card->write.ccwdev = gdev->cdev[1];
4413         card->data.ccwdev  = gdev->cdev[2];
4414         dev_set_drvdata(&gdev->dev, card);
4415         card->gdev = gdev;
4416         gdev->cdev[0]->handler = qeth_irq;
4417         gdev->cdev[1]->handler = qeth_irq;
4418         gdev->cdev[2]->handler = qeth_irq;
4419
4420         rc = qeth_determine_card_type(card);
4421         if (rc) {
4422                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4423                 goto err_dbf;
4424         }
4425         rc = qeth_setup_card(card);
4426         if (rc) {
4427                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
4428                 goto err_dbf;
4429         }
4430
4431         if (card->info.type == QETH_CARD_TYPE_OSN)
4432                 rc = qeth_core_create_osn_attributes(dev);
4433         else
4434                 rc = qeth_core_create_device_attributes(dev);
4435         if (rc)
4436                 goto err_dbf;
4437         switch (card->info.type) {
4438         case QETH_CARD_TYPE_OSN:
4439         case QETH_CARD_TYPE_OSM:
4440                 rc = qeth_core_load_discipline(card, QETH_DISCIPLINE_LAYER2);
4441                 if (rc)
4442                         goto err_attr;
4443                 rc = card->discipline.ccwgdriver->probe(card->gdev);
4444                 if (rc)
4445                         goto err_disc;
4446         case QETH_CARD_TYPE_OSD:
4447         case QETH_CARD_TYPE_OSX:
4448         default:
4449                 break;
4450         }
4451
4452         write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
4453         list_add_tail(&card->list, &qeth_core_card_list.list);
4454         write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
4455
4456         qeth_determine_capabilities(card);
4457         return 0;
4458
4459 err_disc:
4460         qeth_core_free_discipline(card);
4461 err_attr:
4462         if (card->info.type == QETH_CARD_TYPE_OSN)
4463                 qeth_core_remove_osn_attributes(dev);
4464         else
4465                 qeth_core_remove_device_attributes(dev);
4466 err_dbf:
4467         debug_unregister(card->debug);
4468 err_card:
4469         qeth_core_free_card(card);
4470 err_dev:
4471         put_device(dev);
4472         return rc;
4473 }
4474
4475 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
4476 {
4477         unsigned long flags;
4478         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4479
4480         QETH_DBF_TEXT(SETUP, 2, "removedv");
4481
4482         if (card->info.type == QETH_CARD_TYPE_OSN) {
4483                 qeth_core_remove_osn_attributes(&gdev->dev);
4484         } else {
4485                 qeth_core_remove_device_attributes(&gdev->dev);
4486         }
4487
4488         if (card->discipline.ccwgdriver) {
4489                 card->discipline.ccwgdriver->remove(gdev);
4490                 qeth_core_free_discipline(card);
4491         }
4492
4493         debug_unregister(card->debug);
4494         write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
4495         list_del(&card->list);
4496         write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
4497         qeth_core_free_card(card);
4498         dev_set_drvdata(&gdev->dev, NULL);
4499         put_device(&gdev->dev);
4500         return;
4501 }
4502
4503 static int qeth_core_set_online(struct ccwgroup_device *gdev)
4504 {
4505         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4506         int rc = 0;
4507         int def_discipline;
4508
4509         if (!card->discipline.ccwgdriver) {
4510                 if (card->info.type == QETH_CARD_TYPE_IQD)
4511                         def_discipline = QETH_DISCIPLINE_LAYER3;
4512                 else
4513                         def_discipline = QETH_DISCIPLINE_LAYER2;
4514                 rc = qeth_core_load_discipline(card, def_discipline);
4515                 if (rc)
4516                         goto err;
4517                 rc = card->discipline.ccwgdriver->probe(card->gdev);
4518                 if (rc)
4519                         goto err;
4520         }
4521         rc = card->discipline.ccwgdriver->set_online(gdev);
4522 err:
4523         return rc;
4524 }
4525
4526 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
4527 {
4528         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4529         return card->discipline.ccwgdriver->set_offline(gdev);
4530 }
4531
4532 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
4533 {
4534         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4535         if (card->discipline.ccwgdriver &&
4536             card->discipline.ccwgdriver->shutdown)
4537                 card->discipline.ccwgdriver->shutdown(gdev);
4538 }
4539
4540 static int qeth_core_prepare(struct ccwgroup_device *gdev)
4541 {
4542         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4543         if (card->discipline.ccwgdriver &&
4544             card->discipline.ccwgdriver->prepare)
4545                 return card->discipline.ccwgdriver->prepare(gdev);
4546         return 0;
4547 }
4548
4549 static void qeth_core_complete(struct ccwgroup_device *gdev)
4550 {
4551         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4552         if (card->discipline.ccwgdriver &&
4553             card->discipline.ccwgdriver->complete)
4554                 card->discipline.ccwgdriver->complete(gdev);
4555 }
4556
4557 static int qeth_core_freeze(struct ccwgroup_device *gdev)
4558 {
4559         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4560         if (card->discipline.ccwgdriver &&
4561             card->discipline.ccwgdriver->freeze)
4562                 return card->discipline.ccwgdriver->freeze(gdev);
4563         return 0;
4564 }
4565
4566 static int qeth_core_thaw(struct ccwgroup_device *gdev)
4567 {
4568         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4569         if (card->discipline.ccwgdriver &&
4570             card->discipline.ccwgdriver->thaw)
4571                 return card->discipline.ccwgdriver->thaw(gdev);
4572         return 0;
4573 }
4574
4575 static int qeth_core_restore(struct ccwgroup_device *gdev)
4576 {
4577         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4578         if (card->discipline.ccwgdriver &&
4579             card->discipline.ccwgdriver->restore)
4580                 return card->discipline.ccwgdriver->restore(gdev);
4581         return 0;
4582 }
4583
4584 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
4585         .driver = {
4586                 .owner = THIS_MODULE,
4587                 .name = "qeth",
4588         },
4589         .driver_id = 0xD8C5E3C8,
4590         .probe = qeth_core_probe_device,
4591         .remove = qeth_core_remove_device,
4592         .set_online = qeth_core_set_online,
4593         .set_offline = qeth_core_set_offline,
4594         .shutdown = qeth_core_shutdown,
4595         .prepare = qeth_core_prepare,
4596         .complete = qeth_core_complete,
4597         .freeze = qeth_core_freeze,
4598         .thaw = qeth_core_thaw,
4599         .restore = qeth_core_restore,
4600 };
4601
4602 static ssize_t
4603 qeth_core_driver_group_store(struct device_driver *ddrv, const char *buf,
4604                            size_t count)
4605 {
4606         int err;
4607         err = qeth_core_driver_group(buf, qeth_core_root_dev,
4608                                         qeth_core_ccwgroup_driver.driver_id);
4609         if (err)
4610                 return err;
4611         else
4612                 return count;
4613 }
4614
4615 static DRIVER_ATTR(group, 0200, NULL, qeth_core_driver_group_store);
4616
4617 static struct {
4618         const char str[ETH_GSTRING_LEN];
4619 } qeth_ethtool_stats_keys[] = {
4620 /*  0 */{"rx skbs"},
4621         {"rx buffers"},
4622         {"tx skbs"},
4623         {"tx buffers"},
4624         {"tx skbs no packing"},
4625         {"tx buffers no packing"},
4626         {"tx skbs packing"},
4627         {"tx buffers packing"},
4628         {"tx sg skbs"},
4629         {"tx sg frags"},
4630 /* 10 */{"rx sg skbs"},
4631         {"rx sg frags"},
4632         {"rx sg page allocs"},
4633         {"tx large kbytes"},
4634         {"tx large count"},
4635         {"tx pk state ch n->p"},
4636         {"tx pk state ch p->n"},
4637         {"tx pk watermark low"},
4638         {"tx pk watermark high"},
4639         {"queue 0 buffer usage"},
4640 /* 20 */{"queue 1 buffer usage"},
4641         {"queue 2 buffer usage"},
4642         {"queue 3 buffer usage"},
4643         {"rx poll time"},
4644         {"rx poll count"},
4645         {"rx do_QDIO time"},
4646         {"rx do_QDIO count"},
4647         {"tx handler time"},
4648         {"tx handler count"},
4649         {"tx time"},
4650 /* 30 */{"tx count"},
4651         {"tx do_QDIO time"},
4652         {"tx do_QDIO count"},
4653         {"tx csum"},
4654         {"tx lin"},
4655 };
4656
4657 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
4658 {
4659         switch (stringset) {
4660         case ETH_SS_STATS:
4661                 return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
4662         default:
4663                 return -EINVAL;
4664         }
4665 }
4666 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
4667
4668 void qeth_core_get_ethtool_stats(struct net_device *dev,
4669                 struct ethtool_stats *stats, u64 *data)
4670 {
4671         struct qeth_card *card = dev->ml_priv;
4672         data[0] = card->stats.rx_packets -
4673                                 card->perf_stats.initial_rx_packets;
4674         data[1] = card->perf_stats.bufs_rec;
4675         data[2] = card->stats.tx_packets -
4676                                 card->perf_stats.initial_tx_packets;
4677         data[3] = card->perf_stats.bufs_sent;
4678         data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
4679                         - card->perf_stats.skbs_sent_pack;
4680         data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
4681         data[6] = card->perf_stats.skbs_sent_pack;
4682         data[7] = card->perf_stats.bufs_sent_pack;
4683         data[8] = card->perf_stats.sg_skbs_sent;
4684         data[9] = card->perf_stats.sg_frags_sent;
4685         data[10] = card->perf_stats.sg_skbs_rx;
4686         data[11] = card->perf_stats.sg_frags_rx;
4687         data[12] = card->perf_stats.sg_alloc_page_rx;
4688         data[13] = (card->perf_stats.large_send_bytes >> 10);
4689         data[14] = card->perf_stats.large_send_cnt;
4690         data[15] = card->perf_stats.sc_dp_p;
4691         data[16] = card->perf_stats.sc_p_dp;
4692         data[17] = QETH_LOW_WATERMARK_PACK;
4693         data[18] = QETH_HIGH_WATERMARK_PACK;
4694         data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
4695         data[20] = (card->qdio.no_out_queues > 1) ?
4696                         atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
4697         data[21] = (card->qdio.no_out_queues > 2) ?
4698                         atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
4699         data[22] = (card->qdio.no_out_queues > 3) ?
4700                         atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
4701         data[23] = card->perf_stats.inbound_time;
4702         data[24] = card->perf_stats.inbound_cnt;
4703         data[25] = card->perf_stats.inbound_do_qdio_time;
4704         data[26] = card->perf_stats.inbound_do_qdio_cnt;
4705         data[27] = card->perf_stats.outbound_handler_time;
4706         data[28] = card->perf_stats.outbound_handler_cnt;
4707         data[29] = card->perf_stats.outbound_time;
4708         data[30] = card->perf_stats.outbound_cnt;
4709         data[31] = card->perf_stats.outbound_do_qdio_time;
4710         data[32] = card->perf_stats.outbound_do_qdio_cnt;
4711         data[33] = card->perf_stats.tx_csum;
4712         data[34] = card->perf_stats.tx_lin;
4713 }
4714 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
4715
4716 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4717 {
4718         switch (stringset) {
4719         case ETH_SS_STATS:
4720                 memcpy(data, &qeth_ethtool_stats_keys,
4721                         sizeof(qeth_ethtool_stats_keys));
4722                 break;
4723         default:
4724                 WARN_ON(1);
4725                 break;
4726         }
4727 }
4728 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
4729
4730 void qeth_core_get_drvinfo(struct net_device *dev,
4731                 struct ethtool_drvinfo *info)
4732 {
4733         struct qeth_card *card = dev->ml_priv;
4734         if (card->options.layer2)
4735                 strcpy(info->driver, "qeth_l2");
4736         else
4737                 strcpy(info->driver, "qeth_l3");
4738
4739         strcpy(info->version, "1.0");
4740         strcpy(info->fw_version, card->info.mcl_level);
4741         sprintf(info->bus_info, "%s/%s/%s",
4742                         CARD_RDEV_ID(card),
4743                         CARD_WDEV_ID(card),
4744                         CARD_DDEV_ID(card));
4745 }
4746 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
4747
4748 int qeth_core_ethtool_get_settings(struct net_device *netdev,
4749                                         struct ethtool_cmd *ecmd)
4750 {
4751         struct qeth_card *card = netdev->ml_priv;
4752         enum qeth_link_types link_type;
4753
4754         if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
4755                 link_type = QETH_LINK_TYPE_10GBIT_ETH;
4756         else
4757                 link_type = card->info.link_type;
4758
4759         ecmd->transceiver = XCVR_INTERNAL;
4760         ecmd->supported = SUPPORTED_Autoneg;
4761         ecmd->advertising = ADVERTISED_Autoneg;
4762         ecmd->duplex = DUPLEX_FULL;
4763         ecmd->autoneg = AUTONEG_ENABLE;
4764
4765         switch (link_type) {
4766         case QETH_LINK_TYPE_FAST_ETH:
4767         case QETH_LINK_TYPE_LANE_ETH100:
4768                 ecmd->supported |= SUPPORTED_10baseT_Half |
4769                                         SUPPORTED_10baseT_Full |
4770                                         SUPPORTED_100baseT_Half |
4771                                         SUPPORTED_100baseT_Full |
4772                                         SUPPORTED_TP;
4773                 ecmd->advertising |= ADVERTISED_10baseT_Half |
4774                                         ADVERTISED_10baseT_Full |
4775                                         ADVERTISED_100baseT_Half |
4776                                         ADVERTISED_100baseT_Full |
4777                                         ADVERTISED_TP;
4778                 ecmd->speed = SPEED_100;
4779                 ecmd->port = PORT_TP;
4780                 break;
4781
4782         case QETH_LINK_TYPE_GBIT_ETH:
4783         case QETH_LINK_TYPE_LANE_ETH1000:
4784                 ecmd->supported |= SUPPORTED_10baseT_Half |
4785                                         SUPPORTED_10baseT_Full |
4786                                         SUPPORTED_100baseT_Half |
4787                                         SUPPORTED_100baseT_Full |
4788                                         SUPPORTED_1000baseT_Half |
4789                                         SUPPORTED_1000baseT_Full |
4790                                         SUPPORTED_FIBRE;
4791                 ecmd->advertising |= ADVERTISED_10baseT_Half |
4792                                         ADVERTISED_10baseT_Full |
4793                                         ADVERTISED_100baseT_Half |
4794                                         ADVERTISED_100baseT_Full |
4795                                         ADVERTISED_1000baseT_Half |
4796                                         ADVERTISED_1000baseT_Full |
4797                                         ADVERTISED_FIBRE;
4798                 ecmd->speed = SPEED_1000;
4799                 ecmd->port = PORT_FIBRE;
4800                 break;
4801
4802         case QETH_LINK_TYPE_10GBIT_ETH:
4803                 ecmd->supported |= SUPPORTED_10baseT_Half |
4804                                         SUPPORTED_10baseT_Full |
4805                                         SUPPORTED_100baseT_Half |
4806                                         SUPPORTED_100baseT_Full |
4807                                         SUPPORTED_1000baseT_Half |
4808                                         SUPPORTED_1000baseT_Full |
4809                                         SUPPORTED_10000baseT_Full |
4810                                         SUPPORTED_FIBRE;
4811                 ecmd->advertising |= ADVERTISED_10baseT_Half |
4812                                         ADVERTISED_10baseT_Full |
4813                                         ADVERTISED_100baseT_Half |
4814                                         ADVERTISED_100baseT_Full |
4815                                         ADVERTISED_1000baseT_Half |
4816                                         ADVERTISED_1000baseT_Full |
4817                                         ADVERTISED_10000baseT_Full |
4818                                         ADVERTISED_FIBRE;
4819                 ecmd->speed = SPEED_10000;
4820                 ecmd->port = PORT_FIBRE;
4821                 break;
4822
4823         default:
4824                 ecmd->supported |= SUPPORTED_10baseT_Half |
4825                                         SUPPORTED_10baseT_Full |
4826                                         SUPPORTED_TP;
4827                 ecmd->advertising |= ADVERTISED_10baseT_Half |
4828                                         ADVERTISED_10baseT_Full |
4829                                         ADVERTISED_TP;
4830                 ecmd->speed = SPEED_10;
4831                 ecmd->port = PORT_TP;
4832         }
4833
4834         return 0;
4835 }
4836 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_settings);
4837
4838 static int __init qeth_core_init(void)
4839 {
4840         int rc;
4841
4842         pr_info("loading core functions\n");
4843         INIT_LIST_HEAD(&qeth_core_card_list.list);
4844         rwlock_init(&qeth_core_card_list.rwlock);
4845
4846         rc = qeth_register_dbf_views();
4847         if (rc)
4848                 goto out_err;
4849         rc = ccw_driver_register(&qeth_ccw_driver);
4850         if (rc)
4851                 goto ccw_err;
4852         rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
4853         if (rc)
4854                 goto ccwgroup_err;
4855         rc = driver_create_file(&qeth_core_ccwgroup_driver.driver,
4856                                 &driver_attr_group);
4857         if (rc)
4858                 goto driver_err;
4859         qeth_core_root_dev = root_device_register("qeth");
4860         rc = IS_ERR(qeth_core_root_dev) ? PTR_ERR(qeth_core_root_dev) : 0;
4861         if (rc)
4862                 goto register_err;
4863
4864         qeth_core_header_cache = kmem_cache_create("qeth_hdr",
4865                         sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
4866         if (!qeth_core_header_cache) {
4867                 rc = -ENOMEM;
4868                 goto slab_err;
4869         }
4870
4871         return 0;
4872 slab_err:
4873         root_device_unregister(qeth_core_root_dev);
4874 register_err:
4875         driver_remove_file(&qeth_core_ccwgroup_driver.driver,
4876                            &driver_attr_group);
4877 driver_err:
4878         ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
4879 ccwgroup_err:
4880         ccw_driver_unregister(&qeth_ccw_driver);
4881 ccw_err:
4882         QETH_DBF_MESSAGE(2, "Initialization failed with code %d\n", rc);
4883         qeth_unregister_dbf_views();
4884 out_err:
4885         pr_err("Initializing the qeth device driver failed\n");
4886         return rc;
4887 }
4888
4889 static void __exit qeth_core_exit(void)
4890 {
4891         root_device_unregister(qeth_core_root_dev);
4892         driver_remove_file(&qeth_core_ccwgroup_driver.driver,
4893                            &driver_attr_group);
4894         ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
4895         ccw_driver_unregister(&qeth_ccw_driver);
4896         kmem_cache_destroy(qeth_core_header_cache);
4897         qeth_unregister_dbf_views();
4898         pr_info("core functions removed\n");
4899 }
4900
4901 module_init(qeth_core_init);
4902 module_exit(qeth_core_exit);
4903 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
4904 MODULE_DESCRIPTION("qeth core functions");
4905 MODULE_LICENSE("GPL");