s390/qeth: free netdevice when removing a card
[pandora-kernel.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 #include <net/iucv/af_iucv.h>
25
26 #include <asm/ebcdic.h>
27 #include <asm/io.h>
28 #include <asm/sysinfo.h>
29
30 #include "qeth_core.h"
31
32 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
33         /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
34         /*                   N  P  A    M  L  V                      H  */
35         [QETH_DBF_SETUP] = {"qeth_setup",
36                                 8, 1,   8, 5, &debug_hex_ascii_view, NULL},
37         [QETH_DBF_MSG]   = {"qeth_msg",
38                                 8, 1, 128, 3, &debug_sprintf_view,   NULL},
39         [QETH_DBF_CTRL]  = {"qeth_control",
40                 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
41 };
42 EXPORT_SYMBOL_GPL(qeth_dbf);
43
44 struct qeth_card_list_struct qeth_core_card_list;
45 EXPORT_SYMBOL_GPL(qeth_core_card_list);
46 struct kmem_cache *qeth_core_header_cache;
47 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
48 static struct kmem_cache *qeth_qdio_outbuf_cache;
49
50 static struct device *qeth_core_root_dev;
51 static unsigned int known_devices[][6] = QETH_MODELLIST_ARRAY;
52 static struct lock_class_key qdio_out_skb_queue_key;
53
54 static void qeth_send_control_data_cb(struct qeth_channel *,
55                         struct qeth_cmd_buffer *);
56 static int qeth_issue_next_read(struct qeth_card *);
57 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
58 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
59 static void qeth_free_buffer_pool(struct qeth_card *);
60 static int qeth_qdio_establish(struct qeth_card *);
61 static void qeth_free_qdio_buffers(struct qeth_card *);
62 static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
63                 struct qeth_qdio_out_buffer *buf,
64                 enum iucv_tx_notify notification);
65 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf);
66 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
67                 struct qeth_qdio_out_buffer *buf,
68                 enum qeth_qdio_buffer_states newbufstate);
69
70
71 static inline const char *qeth_get_cardname(struct qeth_card *card)
72 {
73         if (card->info.guestlan) {
74                 switch (card->info.type) {
75                 case QETH_CARD_TYPE_OSD:
76                         return " Guest LAN QDIO";
77                 case QETH_CARD_TYPE_IQD:
78                         return " Guest LAN Hiper";
79                 case QETH_CARD_TYPE_OSM:
80                         return " Guest LAN QDIO - OSM";
81                 case QETH_CARD_TYPE_OSX:
82                         return " Guest LAN QDIO - OSX";
83                 default:
84                         return " unknown";
85                 }
86         } else {
87                 switch (card->info.type) {
88                 case QETH_CARD_TYPE_OSD:
89                         return " OSD Express";
90                 case QETH_CARD_TYPE_IQD:
91                         return " HiperSockets";
92                 case QETH_CARD_TYPE_OSN:
93                         return " OSN QDIO";
94                 case QETH_CARD_TYPE_OSM:
95                         return " OSM QDIO";
96                 case QETH_CARD_TYPE_OSX:
97                         return " OSX QDIO";
98                 default:
99                         return " unknown";
100                 }
101         }
102         return " n/a";
103 }
104
105 /* max length to be returned: 14 */
106 const char *qeth_get_cardname_short(struct qeth_card *card)
107 {
108         if (card->info.guestlan) {
109                 switch (card->info.type) {
110                 case QETH_CARD_TYPE_OSD:
111                         return "GuestLAN QDIO";
112                 case QETH_CARD_TYPE_IQD:
113                         return "GuestLAN Hiper";
114                 case QETH_CARD_TYPE_OSM:
115                         return "GuestLAN OSM";
116                 case QETH_CARD_TYPE_OSX:
117                         return "GuestLAN OSX";
118                 default:
119                         return "unknown";
120                 }
121         } else {
122                 switch (card->info.type) {
123                 case QETH_CARD_TYPE_OSD:
124                         switch (card->info.link_type) {
125                         case QETH_LINK_TYPE_FAST_ETH:
126                                 return "OSD_100";
127                         case QETH_LINK_TYPE_HSTR:
128                                 return "HSTR";
129                         case QETH_LINK_TYPE_GBIT_ETH:
130                                 return "OSD_1000";
131                         case QETH_LINK_TYPE_10GBIT_ETH:
132                                 return "OSD_10GIG";
133                         case QETH_LINK_TYPE_LANE_ETH100:
134                                 return "OSD_FE_LANE";
135                         case QETH_LINK_TYPE_LANE_TR:
136                                 return "OSD_TR_LANE";
137                         case QETH_LINK_TYPE_LANE_ETH1000:
138                                 return "OSD_GbE_LANE";
139                         case QETH_LINK_TYPE_LANE:
140                                 return "OSD_ATM_LANE";
141                         default:
142                                 return "OSD_Express";
143                         }
144                 case QETH_CARD_TYPE_IQD:
145                         return "HiperSockets";
146                 case QETH_CARD_TYPE_OSN:
147                         return "OSN";
148                 case QETH_CARD_TYPE_OSM:
149                         return "OSM_1000";
150                 case QETH_CARD_TYPE_OSX:
151                         return "OSX_10GIG";
152                 default:
153                         return "unknown";
154                 }
155         }
156         return "n/a";
157 }
158
159 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
160                          int clear_start_mask)
161 {
162         unsigned long flags;
163
164         spin_lock_irqsave(&card->thread_mask_lock, flags);
165         card->thread_allowed_mask = threads;
166         if (clear_start_mask)
167                 card->thread_start_mask &= threads;
168         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
169         wake_up(&card->wait_q);
170 }
171 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
172
173 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
174 {
175         unsigned long flags;
176         int rc = 0;
177
178         spin_lock_irqsave(&card->thread_mask_lock, flags);
179         rc = (card->thread_running_mask & threads);
180         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
181         return rc;
182 }
183 EXPORT_SYMBOL_GPL(qeth_threads_running);
184
185 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
186 {
187         return wait_event_interruptible(card->wait_q,
188                         qeth_threads_running(card, threads) == 0);
189 }
190 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
191
192 void qeth_clear_working_pool_list(struct qeth_card *card)
193 {
194         struct qeth_buffer_pool_entry *pool_entry, *tmp;
195
196         QETH_CARD_TEXT(card, 5, "clwrklst");
197         list_for_each_entry_safe(pool_entry, tmp,
198                             &card->qdio.in_buf_pool.entry_list, list){
199                         list_del(&pool_entry->list);
200         }
201 }
202 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
203
204 static int qeth_alloc_buffer_pool(struct qeth_card *card)
205 {
206         struct qeth_buffer_pool_entry *pool_entry;
207         void *ptr;
208         int i, j;
209
210         QETH_CARD_TEXT(card, 5, "alocpool");
211         for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
212                 pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
213                 if (!pool_entry) {
214                         qeth_free_buffer_pool(card);
215                         return -ENOMEM;
216                 }
217                 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
218                         ptr = (void *) __get_free_page(GFP_KERNEL);
219                         if (!ptr) {
220                                 while (j > 0)
221                                         free_page((unsigned long)
222                                                   pool_entry->elements[--j]);
223                                 kfree(pool_entry);
224                                 qeth_free_buffer_pool(card);
225                                 return -ENOMEM;
226                         }
227                         pool_entry->elements[j] = ptr;
228                 }
229                 list_add(&pool_entry->init_list,
230                          &card->qdio.init_pool.entry_list);
231         }
232         return 0;
233 }
234
235 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
236 {
237         QETH_CARD_TEXT(card, 2, "realcbp");
238
239         if ((card->state != CARD_STATE_DOWN) &&
240             (card->state != CARD_STATE_RECOVER))
241                 return -EPERM;
242
243         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
244         qeth_clear_working_pool_list(card);
245         qeth_free_buffer_pool(card);
246         card->qdio.in_buf_pool.buf_count = bufcnt;
247         card->qdio.init_pool.buf_count = bufcnt;
248         return qeth_alloc_buffer_pool(card);
249 }
250 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
251
252 static inline int qeth_cq_init(struct qeth_card *card)
253 {
254         int rc;
255
256         if (card->options.cq == QETH_CQ_ENABLED) {
257                 QETH_DBF_TEXT(SETUP, 2, "cqinit");
258                 memset(card->qdio.c_q->qdio_bufs, 0,
259                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
260                 card->qdio.c_q->next_buf_to_init = 127;
261                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
262                              card->qdio.no_in_queues - 1, 0,
263                              127);
264                 if (rc) {
265                         QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
266                         goto out;
267                 }
268         }
269         rc = 0;
270 out:
271         return rc;
272 }
273
274 static inline int qeth_alloc_cq(struct qeth_card *card)
275 {
276         int rc;
277
278         if (card->options.cq == QETH_CQ_ENABLED) {
279                 int i;
280                 struct qdio_outbuf_state *outbuf_states;
281
282                 QETH_DBF_TEXT(SETUP, 2, "cqon");
283                 card->qdio.c_q = kzalloc(sizeof(struct qeth_qdio_q),
284                                          GFP_KERNEL);
285                 if (!card->qdio.c_q) {
286                         rc = -1;
287                         goto kmsg_out;
288                 }
289                 QETH_DBF_HEX(SETUP, 2, &card->qdio.c_q, sizeof(void *));
290
291                 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
292                         card->qdio.c_q->bufs[i].buffer =
293                                 &card->qdio.c_q->qdio_bufs[i];
294                 }
295
296                 card->qdio.no_in_queues = 2;
297
298                 card->qdio.out_bufstates = (struct qdio_outbuf_state *)
299                         kzalloc(card->qdio.no_out_queues *
300                                 QDIO_MAX_BUFFERS_PER_Q *
301                                 sizeof(struct qdio_outbuf_state), GFP_KERNEL);
302                 outbuf_states = card->qdio.out_bufstates;
303                 if (outbuf_states == NULL) {
304                         rc = -1;
305                         goto free_cq_out;
306                 }
307                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
308                         card->qdio.out_qs[i]->bufstates = outbuf_states;
309                         outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
310                 }
311         } else {
312                 QETH_DBF_TEXT(SETUP, 2, "nocq");
313                 card->qdio.c_q = NULL;
314                 card->qdio.no_in_queues = 1;
315         }
316         QETH_DBF_TEXT_(SETUP, 2, "iqc%d", card->qdio.no_in_queues);
317         rc = 0;
318 out:
319         return rc;
320 free_cq_out:
321         kfree(card->qdio.c_q);
322         card->qdio.c_q = NULL;
323 kmsg_out:
324         dev_err(&card->gdev->dev, "Failed to create completion queue\n");
325         goto out;
326 }
327
328 static inline void qeth_free_cq(struct qeth_card *card)
329 {
330         if (card->qdio.c_q) {
331                 --card->qdio.no_in_queues;
332                 kfree(card->qdio.c_q);
333                 card->qdio.c_q = NULL;
334         }
335         kfree(card->qdio.out_bufstates);
336         card->qdio.out_bufstates = NULL;
337 }
338
339 static inline enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
340         int delayed) {
341         enum iucv_tx_notify n;
342
343         switch (sbalf15) {
344         case 0:
345                 n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
346                 break;
347         case 4:
348         case 16:
349         case 17:
350         case 18:
351                 n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
352                         TX_NOTIFY_UNREACHABLE;
353                 break;
354         default:
355                 n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
356                         TX_NOTIFY_GENERALERROR;
357                 break;
358         }
359
360         return n;
361 }
362
363 static inline void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q,
364         int bidx, int forced_cleanup)
365 {
366         if (q->bufs[bidx]->next_pending != NULL) {
367                 struct qeth_qdio_out_buffer *head = q->bufs[bidx];
368                 struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
369
370                 while (c) {
371                         if (forced_cleanup ||
372                             atomic_read(&c->state) ==
373                               QETH_QDIO_BUF_HANDLED_DELAYED) {
374                                 struct qeth_qdio_out_buffer *f = c;
375                                 QETH_CARD_TEXT(f->q->card, 5, "fp");
376                                 QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
377                                 /* release here to avoid interleaving between
378                                    outbound tasklet and inbound tasklet
379                                    regarding notifications and lifecycle */
380                                 qeth_release_skbs(c);
381
382                                 c = f->next_pending;
383                                 BUG_ON(head->next_pending != f);
384                                 head->next_pending = c;
385                                 kmem_cache_free(qeth_qdio_outbuf_cache, f);
386                         } else {
387                                 head = c;
388                                 c = c->next_pending;
389                         }
390
391                 }
392         }
393 }
394
395
396 static inline void qeth_qdio_handle_aob(struct qeth_card *card,
397                 unsigned long phys_aob_addr) {
398         struct qaob *aob;
399         struct qeth_qdio_out_buffer *buffer;
400         enum iucv_tx_notify notification;
401
402         aob = (struct qaob *) phys_to_virt(phys_aob_addr);
403         QETH_CARD_TEXT(card, 5, "haob");
404         QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
405         buffer = (struct qeth_qdio_out_buffer *) aob->user1;
406         QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
407
408         BUG_ON(buffer == NULL);
409
410         if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
411                            QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
412                 notification = TX_NOTIFY_OK;
413         } else {
414                 BUG_ON(atomic_read(&buffer->state) != QETH_QDIO_BUF_PENDING);
415
416                 atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
417                 notification = TX_NOTIFY_DELAYED_OK;
418         }
419
420         if (aob->aorc != 0)  {
421                 QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
422                 notification = qeth_compute_cq_notification(aob->aorc, 1);
423         }
424         qeth_notify_skbs(buffer->q, buffer, notification);
425
426         buffer->aob = NULL;
427         qeth_clear_output_buffer(buffer->q, buffer,
428                                 QETH_QDIO_BUF_HANDLED_DELAYED);
429         /* from here on: do not touch buffer anymore */
430         qdio_release_aob(aob);
431 }
432
433 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
434 {
435         return card->options.cq == QETH_CQ_ENABLED &&
436             card->qdio.c_q != NULL &&
437             queue != 0 &&
438             queue == card->qdio.no_in_queues - 1;
439 }
440
441
442 static int qeth_issue_next_read(struct qeth_card *card)
443 {
444         int rc;
445         struct qeth_cmd_buffer *iob;
446
447         QETH_CARD_TEXT(card, 5, "issnxrd");
448         if (card->read.state != CH_STATE_UP)
449                 return -EIO;
450         iob = qeth_get_buffer(&card->read);
451         if (!iob) {
452                 dev_warn(&card->gdev->dev, "The qeth device driver "
453                         "failed to recover an error on the device\n");
454                 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
455                         "available\n", dev_name(&card->gdev->dev));
456                 return -ENOMEM;
457         }
458         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
459         QETH_CARD_TEXT(card, 6, "noirqpnd");
460         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
461                               (addr_t) iob, 0, 0);
462         if (rc) {
463                 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
464                         "rc=%i\n", dev_name(&card->gdev->dev), rc);
465                 atomic_set(&card->read.irq_pending, 0);
466                 card->read_or_write_problem = 1;
467                 qeth_schedule_recovery(card);
468                 wake_up(&card->wait_q);
469         }
470         return rc;
471 }
472
473 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
474 {
475         struct qeth_reply *reply;
476
477         reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
478         if (reply) {
479                 atomic_set(&reply->refcnt, 1);
480                 atomic_set(&reply->received, 0);
481                 reply->card = card;
482         };
483         return reply;
484 }
485
486 static void qeth_get_reply(struct qeth_reply *reply)
487 {
488         WARN_ON(atomic_read(&reply->refcnt) <= 0);
489         atomic_inc(&reply->refcnt);
490 }
491
492 static void qeth_put_reply(struct qeth_reply *reply)
493 {
494         WARN_ON(atomic_read(&reply->refcnt) <= 0);
495         if (atomic_dec_and_test(&reply->refcnt))
496                 kfree(reply);
497 }
498
499 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
500                 struct qeth_card *card)
501 {
502         char *ipa_name;
503         int com = cmd->hdr.command;
504         ipa_name = qeth_get_ipa_cmd_name(com);
505         if (rc)
506                 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
507                                 "x%X \"%s\"\n",
508                                 ipa_name, com, dev_name(&card->gdev->dev),
509                                 QETH_CARD_IFNAME(card), rc,
510                                 qeth_get_ipa_msg(rc));
511         else
512                 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
513                                 ipa_name, com, dev_name(&card->gdev->dev),
514                                 QETH_CARD_IFNAME(card));
515 }
516
517 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
518                 struct qeth_cmd_buffer *iob)
519 {
520         struct qeth_ipa_cmd *cmd = NULL;
521
522         QETH_CARD_TEXT(card, 5, "chkipad");
523         if (IS_IPA(iob->data)) {
524                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
525                 if (IS_IPA_REPLY(cmd)) {
526                         if (cmd->hdr.command != IPA_CMD_SETCCID &&
527                             cmd->hdr.command != IPA_CMD_DELCCID &&
528                             cmd->hdr.command != IPA_CMD_MODCCID &&
529                             cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
530                                 qeth_issue_ipa_msg(cmd,
531                                                 cmd->hdr.return_code, card);
532                         return cmd;
533                 } else {
534                         switch (cmd->hdr.command) {
535                         case IPA_CMD_STOPLAN:
536                                 dev_warn(&card->gdev->dev,
537                                            "The link for interface %s on CHPID"
538                                            " 0x%X failed\n",
539                                            QETH_CARD_IFNAME(card),
540                                            card->info.chpid);
541                                 card->lan_online = 0;
542                                 if (card->dev && netif_carrier_ok(card->dev))
543                                         netif_carrier_off(card->dev);
544                                 return NULL;
545                         case IPA_CMD_STARTLAN:
546                                 dev_info(&card->gdev->dev,
547                                            "The link for %s on CHPID 0x%X has"
548                                            " been restored\n",
549                                            QETH_CARD_IFNAME(card),
550                                            card->info.chpid);
551                                 netif_carrier_on(card->dev);
552                                 card->lan_online = 1;
553                                 if (card->info.hwtrap)
554                                         card->info.hwtrap = 2;
555                                 qeth_schedule_recovery(card);
556                                 return NULL;
557                         case IPA_CMD_MODCCID:
558                                 return cmd;
559                         case IPA_CMD_REGISTER_LOCAL_ADDR:
560                                 QETH_CARD_TEXT(card, 3, "irla");
561                                 break;
562                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
563                                 QETH_CARD_TEXT(card, 3, "urla");
564                                 break;
565                         default:
566                                 QETH_DBF_MESSAGE(2, "Received data is IPA "
567                                            "but not a reply!\n");
568                                 break;
569                         }
570                 }
571         }
572         return cmd;
573 }
574
575 void qeth_clear_ipacmd_list(struct qeth_card *card)
576 {
577         struct qeth_reply *reply, *r;
578         unsigned long flags;
579
580         QETH_CARD_TEXT(card, 4, "clipalst");
581
582         spin_lock_irqsave(&card->lock, flags);
583         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
584                 qeth_get_reply(reply);
585                 reply->rc = -EIO;
586                 atomic_inc(&reply->received);
587                 list_del_init(&reply->list);
588                 wake_up(&reply->wait_q);
589                 qeth_put_reply(reply);
590         }
591         spin_unlock_irqrestore(&card->lock, flags);
592         atomic_set(&card->write.irq_pending, 0);
593 }
594 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
595
596 static int qeth_check_idx_response(struct qeth_card *card,
597         unsigned char *buffer)
598 {
599         if (!buffer)
600                 return 0;
601
602         QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
603         if ((buffer[2] & 0xc0) == 0xc0) {
604                 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
605                            "with cause code 0x%02x%s\n",
606                            buffer[4],
607                            ((buffer[4] == 0x22) ?
608                             " -- try another portname" : ""));
609                 QETH_CARD_TEXT(card, 2, "ckidxres");
610                 QETH_CARD_TEXT(card, 2, " idxterm");
611                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
612                 if (buffer[4] == 0xf6) {
613                         dev_err(&card->gdev->dev,
614                         "The qeth device is not configured "
615                         "for the OSI layer required by z/VM\n");
616                         return -EPERM;
617                 }
618                 return -EIO;
619         }
620         return 0;
621 }
622
623 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
624                 __u32 len)
625 {
626         struct qeth_card *card;
627
628         card = CARD_FROM_CDEV(channel->ccwdev);
629         QETH_CARD_TEXT(card, 4, "setupccw");
630         if (channel == &card->read)
631                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
632         else
633                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
634         channel->ccw.count = len;
635         channel->ccw.cda = (__u32) __pa(iob);
636 }
637
638 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
639 {
640         __u8 index;
641
642         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
643         index = channel->io_buf_no;
644         do {
645                 if (channel->iob[index].state == BUF_STATE_FREE) {
646                         channel->iob[index].state = BUF_STATE_LOCKED;
647                         channel->io_buf_no = (channel->io_buf_no + 1) %
648                                 QETH_CMD_BUFFER_NO;
649                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
650                         return channel->iob + index;
651                 }
652                 index = (index + 1) % QETH_CMD_BUFFER_NO;
653         } while (index != channel->io_buf_no);
654
655         return NULL;
656 }
657
658 void qeth_release_buffer(struct qeth_channel *channel,
659                 struct qeth_cmd_buffer *iob)
660 {
661         unsigned long flags;
662
663         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
664         spin_lock_irqsave(&channel->iob_lock, flags);
665         memset(iob->data, 0, QETH_BUFSIZE);
666         iob->state = BUF_STATE_FREE;
667         iob->callback = qeth_send_control_data_cb;
668         iob->rc = 0;
669         spin_unlock_irqrestore(&channel->iob_lock, flags);
670 }
671 EXPORT_SYMBOL_GPL(qeth_release_buffer);
672
673 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
674 {
675         struct qeth_cmd_buffer *buffer = NULL;
676         unsigned long flags;
677
678         spin_lock_irqsave(&channel->iob_lock, flags);
679         buffer = __qeth_get_buffer(channel);
680         spin_unlock_irqrestore(&channel->iob_lock, flags);
681         return buffer;
682 }
683
684 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
685 {
686         struct qeth_cmd_buffer *buffer;
687         wait_event(channel->wait_q,
688                    ((buffer = qeth_get_buffer(channel)) != NULL));
689         return buffer;
690 }
691 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
692
693 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
694 {
695         int cnt;
696
697         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
698                 qeth_release_buffer(channel, &channel->iob[cnt]);
699         channel->buf_no = 0;
700         channel->io_buf_no = 0;
701 }
702 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
703
704 static void qeth_send_control_data_cb(struct qeth_channel *channel,
705                   struct qeth_cmd_buffer *iob)
706 {
707         struct qeth_card *card;
708         struct qeth_reply *reply, *r;
709         struct qeth_ipa_cmd *cmd;
710         unsigned long flags;
711         int keep_reply;
712         int rc = 0;
713
714         card = CARD_FROM_CDEV(channel->ccwdev);
715         QETH_CARD_TEXT(card, 4, "sndctlcb");
716         rc = qeth_check_idx_response(card, iob->data);
717         switch (rc) {
718         case 0:
719                 break;
720         case -EIO:
721                 qeth_clear_ipacmd_list(card);
722                 qeth_schedule_recovery(card);
723                 /* fall through */
724         default:
725                 goto out;
726         }
727
728         cmd = qeth_check_ipa_data(card, iob);
729         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
730                 goto out;
731         /*in case of OSN : check if cmd is set */
732         if (card->info.type == QETH_CARD_TYPE_OSN &&
733             cmd &&
734             cmd->hdr.command != IPA_CMD_STARTLAN &&
735             card->osn_info.assist_cb != NULL) {
736                 card->osn_info.assist_cb(card->dev, cmd);
737                 goto out;
738         }
739
740         spin_lock_irqsave(&card->lock, flags);
741         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
742                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
743                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
744                         qeth_get_reply(reply);
745                         list_del_init(&reply->list);
746                         spin_unlock_irqrestore(&card->lock, flags);
747                         keep_reply = 0;
748                         if (reply->callback != NULL) {
749                                 if (cmd) {
750                                         reply->offset = (__u16)((char *)cmd -
751                                                         (char *)iob->data);
752                                         keep_reply = reply->callback(card,
753                                                         reply,
754                                                         (unsigned long)cmd);
755                                 } else
756                                         keep_reply = reply->callback(card,
757                                                         reply,
758                                                         (unsigned long)iob);
759                         }
760                         if (cmd)
761                                 reply->rc = (u16) cmd->hdr.return_code;
762                         else if (iob->rc)
763                                 reply->rc = iob->rc;
764                         if (keep_reply) {
765                                 spin_lock_irqsave(&card->lock, flags);
766                                 list_add_tail(&reply->list,
767                                               &card->cmd_waiter_list);
768                                 spin_unlock_irqrestore(&card->lock, flags);
769                         } else {
770                                 atomic_inc(&reply->received);
771                                 wake_up(&reply->wait_q);
772                         }
773                         qeth_put_reply(reply);
774                         goto out;
775                 }
776         }
777         spin_unlock_irqrestore(&card->lock, flags);
778 out:
779         memcpy(&card->seqno.pdu_hdr_ack,
780                 QETH_PDU_HEADER_SEQ_NO(iob->data),
781                 QETH_SEQ_NO_LENGTH);
782         qeth_release_buffer(channel, iob);
783 }
784
785 static int qeth_setup_channel(struct qeth_channel *channel)
786 {
787         int cnt;
788
789         QETH_DBF_TEXT(SETUP, 2, "setupch");
790         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
791                 channel->iob[cnt].data =
792                         kzalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
793                 if (channel->iob[cnt].data == NULL)
794                         break;
795                 channel->iob[cnt].state = BUF_STATE_FREE;
796                 channel->iob[cnt].channel = channel;
797                 channel->iob[cnt].callback = qeth_send_control_data_cb;
798                 channel->iob[cnt].rc = 0;
799         }
800         if (cnt < QETH_CMD_BUFFER_NO) {
801                 while (cnt-- > 0)
802                         kfree(channel->iob[cnt].data);
803                 return -ENOMEM;
804         }
805         channel->buf_no = 0;
806         channel->io_buf_no = 0;
807         atomic_set(&channel->irq_pending, 0);
808         spin_lock_init(&channel->iob_lock);
809
810         init_waitqueue_head(&channel->wait_q);
811         return 0;
812 }
813
814 static int qeth_set_thread_start_bit(struct qeth_card *card,
815                 unsigned long thread)
816 {
817         unsigned long flags;
818
819         spin_lock_irqsave(&card->thread_mask_lock, flags);
820         if (!(card->thread_allowed_mask & thread) ||
821               (card->thread_start_mask & thread)) {
822                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
823                 return -EPERM;
824         }
825         card->thread_start_mask |= thread;
826         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
827         return 0;
828 }
829
830 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
831 {
832         unsigned long flags;
833
834         spin_lock_irqsave(&card->thread_mask_lock, flags);
835         card->thread_start_mask &= ~thread;
836         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
837         wake_up(&card->wait_q);
838 }
839 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
840
841 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
842 {
843         unsigned long flags;
844
845         spin_lock_irqsave(&card->thread_mask_lock, flags);
846         card->thread_running_mask &= ~thread;
847         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
848         wake_up(&card->wait_q);
849 }
850 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
851
852 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
853 {
854         unsigned long flags;
855         int rc = 0;
856
857         spin_lock_irqsave(&card->thread_mask_lock, flags);
858         if (card->thread_start_mask & thread) {
859                 if ((card->thread_allowed_mask & thread) &&
860                     !(card->thread_running_mask & thread)) {
861                         rc = 1;
862                         card->thread_start_mask &= ~thread;
863                         card->thread_running_mask |= thread;
864                 } else
865                         rc = -EPERM;
866         }
867         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
868         return rc;
869 }
870
871 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
872 {
873         int rc = 0;
874
875         wait_event(card->wait_q,
876                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
877         return rc;
878 }
879 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
880
881 void qeth_schedule_recovery(struct qeth_card *card)
882 {
883         QETH_CARD_TEXT(card, 2, "startrec");
884         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
885                 schedule_work(&card->kernel_thread_starter);
886 }
887 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
888
889 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
890 {
891         int dstat, cstat;
892         char *sense;
893         struct qeth_card *card;
894
895         sense = (char *) irb->ecw;
896         cstat = irb->scsw.cmd.cstat;
897         dstat = irb->scsw.cmd.dstat;
898         card = CARD_FROM_CDEV(cdev);
899
900         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
901                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
902                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
903                 QETH_CARD_TEXT(card, 2, "CGENCHK");
904                 dev_warn(&cdev->dev, "The qeth device driver "
905                         "failed to recover an error on the device\n");
906                 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
907                         dev_name(&cdev->dev), dstat, cstat);
908                 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
909                                 16, 1, irb, 64, 1);
910                 return 1;
911         }
912
913         if (dstat & DEV_STAT_UNIT_CHECK) {
914                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
915                     SENSE_RESETTING_EVENT_FLAG) {
916                         QETH_CARD_TEXT(card, 2, "REVIND");
917                         return 1;
918                 }
919                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
920                     SENSE_COMMAND_REJECT_FLAG) {
921                         QETH_CARD_TEXT(card, 2, "CMDREJi");
922                         return 1;
923                 }
924                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
925                         QETH_CARD_TEXT(card, 2, "AFFE");
926                         return 1;
927                 }
928                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
929                         QETH_CARD_TEXT(card, 2, "ZEROSEN");
930                         return 0;
931                 }
932                 QETH_CARD_TEXT(card, 2, "DGENCHK");
933                         return 1;
934         }
935         return 0;
936 }
937
938 static long __qeth_check_irb_error(struct ccw_device *cdev,
939                 unsigned long intparm, struct irb *irb)
940 {
941         struct qeth_card *card;
942
943         card = CARD_FROM_CDEV(cdev);
944
945         if (!IS_ERR(irb))
946                 return 0;
947
948         switch (PTR_ERR(irb)) {
949         case -EIO:
950                 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
951                         dev_name(&cdev->dev));
952                 QETH_CARD_TEXT(card, 2, "ckirberr");
953                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
954                 break;
955         case -ETIMEDOUT:
956                 dev_warn(&cdev->dev, "A hardware operation timed out"
957                         " on the device\n");
958                 QETH_CARD_TEXT(card, 2, "ckirberr");
959                 QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
960                 if (intparm == QETH_RCD_PARM) {
961                         if (card && (card->data.ccwdev == cdev)) {
962                                 card->data.state = CH_STATE_DOWN;
963                                 wake_up(&card->wait_q);
964                         }
965                 }
966                 break;
967         default:
968                 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
969                         dev_name(&cdev->dev), PTR_ERR(irb));
970                 QETH_CARD_TEXT(card, 2, "ckirberr");
971                 QETH_CARD_TEXT(card, 2, "  rc???");
972         }
973         return PTR_ERR(irb);
974 }
975
976 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
977                 struct irb *irb)
978 {
979         int rc;
980         int cstat, dstat;
981         struct qeth_cmd_buffer *buffer;
982         struct qeth_channel *channel;
983         struct qeth_card *card;
984         struct qeth_cmd_buffer *iob;
985         __u8 index;
986
987         if (__qeth_check_irb_error(cdev, intparm, irb))
988                 return;
989         cstat = irb->scsw.cmd.cstat;
990         dstat = irb->scsw.cmd.dstat;
991
992         card = CARD_FROM_CDEV(cdev);
993         if (!card)
994                 return;
995
996         QETH_CARD_TEXT(card, 5, "irq");
997
998         if (card->read.ccwdev == cdev) {
999                 channel = &card->read;
1000                 QETH_CARD_TEXT(card, 5, "read");
1001         } else if (card->write.ccwdev == cdev) {
1002                 channel = &card->write;
1003                 QETH_CARD_TEXT(card, 5, "write");
1004         } else {
1005                 channel = &card->data;
1006                 QETH_CARD_TEXT(card, 5, "data");
1007         }
1008         atomic_set(&channel->irq_pending, 0);
1009
1010         if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1011                 channel->state = CH_STATE_STOPPED;
1012
1013         if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1014                 channel->state = CH_STATE_HALTED;
1015
1016         /*let's wake up immediately on data channel*/
1017         if ((channel == &card->data) && (intparm != 0) &&
1018             (intparm != QETH_RCD_PARM))
1019                 goto out;
1020
1021         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
1022                 QETH_CARD_TEXT(card, 6, "clrchpar");
1023                 /* we don't have to handle this further */
1024                 intparm = 0;
1025         }
1026         if (intparm == QETH_HALT_CHANNEL_PARM) {
1027                 QETH_CARD_TEXT(card, 6, "hltchpar");
1028                 /* we don't have to handle this further */
1029                 intparm = 0;
1030         }
1031         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1032             (dstat & DEV_STAT_UNIT_CHECK) ||
1033             (cstat)) {
1034                 if (irb->esw.esw0.erw.cons) {
1035                         dev_warn(&channel->ccwdev->dev,
1036                                 "The qeth device driver failed to recover "
1037                                 "an error on the device\n");
1038                         QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
1039                                 "0x%X dstat 0x%X\n",
1040                                 dev_name(&channel->ccwdev->dev), cstat, dstat);
1041                         print_hex_dump(KERN_WARNING, "qeth: irb ",
1042                                 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1043                         print_hex_dump(KERN_WARNING, "qeth: sense data ",
1044                                 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1045                 }
1046                 if (intparm == QETH_RCD_PARM) {
1047                         channel->state = CH_STATE_DOWN;
1048                         goto out;
1049                 }
1050                 rc = qeth_get_problem(cdev, irb);
1051                 if (rc) {
1052                         qeth_clear_ipacmd_list(card);
1053                         qeth_schedule_recovery(card);
1054                         goto out;
1055                 }
1056         }
1057
1058         if (intparm == QETH_RCD_PARM) {
1059                 channel->state = CH_STATE_RCD_DONE;
1060                 goto out;
1061         }
1062         if (intparm) {
1063                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
1064                 buffer->state = BUF_STATE_PROCESSED;
1065         }
1066         if (channel == &card->data)
1067                 return;
1068         if (channel == &card->read &&
1069             channel->state == CH_STATE_UP)
1070                 qeth_issue_next_read(card);
1071
1072         iob = channel->iob;
1073         index = channel->buf_no;
1074         while (iob[index].state == BUF_STATE_PROCESSED) {
1075                 if (iob[index].callback != NULL)
1076                         iob[index].callback(channel, iob + index);
1077
1078                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1079         }
1080         channel->buf_no = index;
1081 out:
1082         wake_up(&card->wait_q);
1083         return;
1084 }
1085
1086 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1087                 struct qeth_qdio_out_buffer *buf,
1088                 enum iucv_tx_notify notification)
1089 {
1090         struct sk_buff *skb;
1091
1092         if (skb_queue_empty(&buf->skb_list))
1093                 goto out;
1094         skb = skb_peek(&buf->skb_list);
1095         while (skb) {
1096                 QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1097                 QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1098                 if (skb->protocol == ETH_P_AF_IUCV) {
1099                         if (skb->sk) {
1100                                 struct iucv_sock *iucv = iucv_sk(skb->sk);
1101                                 iucv->sk_txnotify(skb, notification);
1102                         }
1103                 }
1104                 if (skb_queue_is_last(&buf->skb_list, skb))
1105                         skb = NULL;
1106                 else
1107                         skb = skb_queue_next(&buf->skb_list, skb);
1108         }
1109 out:
1110         return;
1111 }
1112
1113 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf)
1114 {
1115         struct sk_buff *skb;
1116
1117         skb = skb_dequeue(&buf->skb_list);
1118         while (skb) {
1119                 QETH_CARD_TEXT(buf->q->card, 5, "skbr");
1120                 QETH_CARD_TEXT_(buf->q->card, 5, "%lx", (long) skb);
1121                 atomic_dec(&skb->users);
1122                 dev_kfree_skb_any(skb);
1123                 skb = skb_dequeue(&buf->skb_list);
1124         }
1125 }
1126
1127 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1128                 struct qeth_qdio_out_buffer *buf,
1129                 enum qeth_qdio_buffer_states newbufstate)
1130 {
1131         int i;
1132
1133         /* is PCI flag set on buffer? */
1134         if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1135                 atomic_dec(&queue->set_pci_flags_count);
1136
1137         if (newbufstate == QETH_QDIO_BUF_EMPTY) {
1138                 qeth_release_skbs(buf);
1139         }
1140         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
1141                 if (buf->buffer->element[i].addr && buf->is_header[i])
1142                         kmem_cache_free(qeth_core_header_cache,
1143                                 buf->buffer->element[i].addr);
1144                 buf->is_header[i] = 0;
1145                 buf->buffer->element[i].length = 0;
1146                 buf->buffer->element[i].addr = NULL;
1147                 buf->buffer->element[i].eflags = 0;
1148                 buf->buffer->element[i].sflags = 0;
1149         }
1150         buf->buffer->element[15].eflags = 0;
1151         buf->buffer->element[15].sflags = 0;
1152         buf->next_element_to_fill = 0;
1153         atomic_set(&buf->state, newbufstate);
1154 }
1155
1156 static void qeth_clear_outq_buffers(struct qeth_qdio_out_q *q, int free)
1157 {
1158         int j;
1159
1160         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1161                 if (!q->bufs[j])
1162                         continue;
1163                 qeth_cleanup_handled_pending(q, j, free);
1164                 qeth_clear_output_buffer(q, q->bufs[j], QETH_QDIO_BUF_EMPTY);
1165                 if (free) {
1166                         kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1167                         q->bufs[j] = NULL;
1168                 }
1169         }
1170 }
1171
1172 void qeth_clear_qdio_buffers(struct qeth_card *card)
1173 {
1174         int i;
1175
1176         QETH_CARD_TEXT(card, 2, "clearqdbf");
1177         /* clear outbound buffers to free skbs */
1178         for (i = 0; i < card->qdio.no_out_queues; ++i) {
1179                 if (card->qdio.out_qs[i]) {
1180                         qeth_clear_outq_buffers(card->qdio.out_qs[i], 0);
1181                 }
1182         }
1183 }
1184 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
1185
1186 static void qeth_free_buffer_pool(struct qeth_card *card)
1187 {
1188         struct qeth_buffer_pool_entry *pool_entry, *tmp;
1189         int i = 0;
1190         list_for_each_entry_safe(pool_entry, tmp,
1191                                  &card->qdio.init_pool.entry_list, init_list){
1192                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1193                         free_page((unsigned long)pool_entry->elements[i]);
1194                 list_del(&pool_entry->init_list);
1195                 kfree(pool_entry);
1196         }
1197 }
1198
1199 static void qeth_free_qdio_buffers(struct qeth_card *card)
1200 {
1201         int i, j;
1202
1203         if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
1204                 QETH_QDIO_UNINITIALIZED)
1205                 return;
1206
1207         qeth_free_cq(card);
1208         cancel_delayed_work_sync(&card->buffer_reclaim_work);
1209         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
1210                 kfree_skb(card->qdio.in_q->bufs[j].rx_skb);
1211         kfree(card->qdio.in_q);
1212         card->qdio.in_q = NULL;
1213         /* inbound buffer pool */
1214         qeth_free_buffer_pool(card);
1215         /* free outbound qdio_qs */
1216         if (card->qdio.out_qs) {
1217                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
1218                         qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
1219                         kfree(card->qdio.out_qs[i]);
1220                 }
1221                 kfree(card->qdio.out_qs);
1222                 card->qdio.out_qs = NULL;
1223         }
1224 }
1225
1226 static void qeth_clean_channel(struct qeth_channel *channel)
1227 {
1228         int cnt;
1229
1230         QETH_DBF_TEXT(SETUP, 2, "freech");
1231         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1232                 kfree(channel->iob[cnt].data);
1233 }
1234
1235 static void qeth_get_channel_path_desc(struct qeth_card *card)
1236 {
1237         struct ccw_device *ccwdev;
1238         struct channelPath_dsc {
1239                 u8 flags;
1240                 u8 lsn;
1241                 u8 desc;
1242                 u8 chpid;
1243                 u8 swla;
1244                 u8 zeroes;
1245                 u8 chla;
1246                 u8 chpp;
1247         } *chp_dsc;
1248
1249         QETH_DBF_TEXT(SETUP, 2, "chp_desc");
1250
1251         ccwdev = card->data.ccwdev;
1252         chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1253         if (chp_dsc != NULL) {
1254                 if (card->info.type != QETH_CARD_TYPE_IQD) {
1255                         /* CHPP field bit 6 == 1 -> single queue */
1256                         if ((chp_dsc->chpp & 0x02) == 0x02) {
1257                                 if ((atomic_read(&card->qdio.state) !=
1258                                         QETH_QDIO_UNINITIALIZED) &&
1259                                     (card->qdio.no_out_queues == 4))
1260                                         /* change from 4 to 1 outbound queues */
1261                                         qeth_free_qdio_buffers(card);
1262                                 card->qdio.no_out_queues = 1;
1263                                 if (card->qdio.default_out_queue != 0)
1264                                         dev_info(&card->gdev->dev,
1265                                         "Priority Queueing not supported\n");
1266                                 card->qdio.default_out_queue = 0;
1267                         } else {
1268                                 if ((atomic_read(&card->qdio.state) !=
1269                                         QETH_QDIO_UNINITIALIZED) &&
1270                                     (card->qdio.no_out_queues == 1)) {
1271                                         /* change from 1 to 4 outbound queues */
1272                                         qeth_free_qdio_buffers(card);
1273                                         card->qdio.default_out_queue = 2;
1274                                 }
1275                                 card->qdio.no_out_queues = 4;
1276                         }
1277                 }
1278                 card->info.func_level = 0x4100 + chp_dsc->desc;
1279                 kfree(chp_dsc);
1280         }
1281         QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1282         QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1283         return;
1284 }
1285
1286 static void qeth_init_qdio_info(struct qeth_card *card)
1287 {
1288         QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1289         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1290         /* inbound */
1291         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1292         if (card->info.type == QETH_CARD_TYPE_IQD)
1293                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1294         else
1295                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1296         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1297         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1298         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1299 }
1300
1301 static void qeth_set_intial_options(struct qeth_card *card)
1302 {
1303         card->options.route4.type = NO_ROUTER;
1304         card->options.route6.type = NO_ROUTER;
1305         card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1306         card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1307         card->options.fake_broadcast = 0;
1308         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1309         card->options.performance_stats = 0;
1310         card->options.rx_sg_cb = QETH_RX_SG_CB;
1311         card->options.isolation = ISOLATION_MODE_NONE;
1312         card->options.cq = QETH_CQ_DISABLED;
1313 }
1314
1315 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1316 {
1317         unsigned long flags;
1318         int rc = 0;
1319
1320         spin_lock_irqsave(&card->thread_mask_lock, flags);
1321         QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1322                         (u8) card->thread_start_mask,
1323                         (u8) card->thread_allowed_mask,
1324                         (u8) card->thread_running_mask);
1325         rc = (card->thread_start_mask & thread);
1326         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1327         return rc;
1328 }
1329
1330 static void qeth_start_kernel_thread(struct work_struct *work)
1331 {
1332         struct qeth_card *card = container_of(work, struct qeth_card,
1333                                         kernel_thread_starter);
1334         QETH_CARD_TEXT(card , 2, "strthrd");
1335
1336         if (card->read.state != CH_STATE_UP &&
1337             card->write.state != CH_STATE_UP)
1338                 return;
1339         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1340                 kthread_run(card->discipline.recover, (void *) card,
1341                                 "qeth_recover");
1342 }
1343
1344 static int qeth_setup_card(struct qeth_card *card)
1345 {
1346
1347         QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1348         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1349
1350         card->read.state  = CH_STATE_DOWN;
1351         card->write.state = CH_STATE_DOWN;
1352         card->data.state  = CH_STATE_DOWN;
1353         card->state = CARD_STATE_DOWN;
1354         card->lan_online = 0;
1355         card->read_or_write_problem = 0;
1356         card->dev = NULL;
1357         spin_lock_init(&card->vlanlock);
1358         spin_lock_init(&card->mclock);
1359         spin_lock_init(&card->lock);
1360         spin_lock_init(&card->ip_lock);
1361         spin_lock_init(&card->thread_mask_lock);
1362         mutex_init(&card->conf_mutex);
1363         mutex_init(&card->discipline_mutex);
1364         card->thread_start_mask = 0;
1365         card->thread_allowed_mask = 0;
1366         card->thread_running_mask = 0;
1367         INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1368         INIT_LIST_HEAD(&card->ip_list);
1369         INIT_LIST_HEAD(card->ip_tbd_list);
1370         INIT_LIST_HEAD(&card->cmd_waiter_list);
1371         init_waitqueue_head(&card->wait_q);
1372         /* initial options */
1373         qeth_set_intial_options(card);
1374         /* IP address takeover */
1375         INIT_LIST_HEAD(&card->ipato.entries);
1376         card->ipato.enabled = 0;
1377         card->ipato.invert4 = 0;
1378         card->ipato.invert6 = 0;
1379         /* init QDIO stuff */
1380         qeth_init_qdio_info(card);
1381         INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1382         return 0;
1383 }
1384
1385 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1386 {
1387         struct qeth_card *card = container_of(slr, struct qeth_card,
1388                                         qeth_service_level);
1389         if (card->info.mcl_level[0])
1390                 seq_printf(m, "qeth: %s firmware level %s\n",
1391                         CARD_BUS_ID(card), card->info.mcl_level);
1392 }
1393
1394 static struct qeth_card *qeth_alloc_card(void)
1395 {
1396         struct qeth_card *card;
1397
1398         QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1399         card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1400         if (!card)
1401                 goto out;
1402         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1403         card->ip_tbd_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
1404         if (!card->ip_tbd_list) {
1405                 QETH_DBF_TEXT(SETUP, 0, "iptbdnom");
1406                 goto out_card;
1407         }
1408         if (qeth_setup_channel(&card->read))
1409                 goto out_ip;
1410         if (qeth_setup_channel(&card->write))
1411                 goto out_channel;
1412         card->options.layer2 = -1;
1413         card->qeth_service_level.seq_print = qeth_core_sl_print;
1414         register_service_level(&card->qeth_service_level);
1415         return card;
1416
1417 out_channel:
1418         qeth_clean_channel(&card->read);
1419 out_ip:
1420         kfree(card->ip_tbd_list);
1421 out_card:
1422         kfree(card);
1423 out:
1424         return NULL;
1425 }
1426
1427 static int qeth_determine_card_type(struct qeth_card *card)
1428 {
1429         int i = 0;
1430
1431         QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1432
1433         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1434         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1435         while (known_devices[i][QETH_DEV_MODEL_IND]) {
1436                 if ((CARD_RDEV(card)->id.dev_type ==
1437                                 known_devices[i][QETH_DEV_TYPE_IND]) &&
1438                     (CARD_RDEV(card)->id.dev_model ==
1439                                 known_devices[i][QETH_DEV_MODEL_IND])) {
1440                         card->info.type = known_devices[i][QETH_DEV_MODEL_IND];
1441                         card->qdio.no_out_queues =
1442                                 known_devices[i][QETH_QUEUE_NO_IND];
1443                         card->qdio.no_in_queues = 1;
1444                         card->info.is_multicast_different =
1445                                 known_devices[i][QETH_MULTICAST_IND];
1446                         qeth_get_channel_path_desc(card);
1447                         return 0;
1448                 }
1449                 i++;
1450         }
1451         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1452         dev_err(&card->gdev->dev, "The adapter hardware is of an "
1453                 "unknown type\n");
1454         return -ENOENT;
1455 }
1456
1457 static int qeth_clear_channel(struct qeth_channel *channel)
1458 {
1459         unsigned long flags;
1460         struct qeth_card *card;
1461         int rc;
1462
1463         card = CARD_FROM_CDEV(channel->ccwdev);
1464         QETH_CARD_TEXT(card, 3, "clearch");
1465         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1466         rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1467         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1468
1469         if (rc)
1470                 return rc;
1471         rc = wait_event_interruptible_timeout(card->wait_q,
1472                         channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1473         if (rc == -ERESTARTSYS)
1474                 return rc;
1475         if (channel->state != CH_STATE_STOPPED)
1476                 return -ETIME;
1477         channel->state = CH_STATE_DOWN;
1478         return 0;
1479 }
1480
1481 static int qeth_halt_channel(struct qeth_channel *channel)
1482 {
1483         unsigned long flags;
1484         struct qeth_card *card;
1485         int rc;
1486
1487         card = CARD_FROM_CDEV(channel->ccwdev);
1488         QETH_CARD_TEXT(card, 3, "haltch");
1489         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1490         rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1491         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1492
1493         if (rc)
1494                 return rc;
1495         rc = wait_event_interruptible_timeout(card->wait_q,
1496                         channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1497         if (rc == -ERESTARTSYS)
1498                 return rc;
1499         if (channel->state != CH_STATE_HALTED)
1500                 return -ETIME;
1501         return 0;
1502 }
1503
1504 static int qeth_halt_channels(struct qeth_card *card)
1505 {
1506         int rc1 = 0, rc2 = 0, rc3 = 0;
1507
1508         QETH_CARD_TEXT(card, 3, "haltchs");
1509         rc1 = qeth_halt_channel(&card->read);
1510         rc2 = qeth_halt_channel(&card->write);
1511         rc3 = qeth_halt_channel(&card->data);
1512         if (rc1)
1513                 return rc1;
1514         if (rc2)
1515                 return rc2;
1516         return rc3;
1517 }
1518
1519 static int qeth_clear_channels(struct qeth_card *card)
1520 {
1521         int rc1 = 0, rc2 = 0, rc3 = 0;
1522
1523         QETH_CARD_TEXT(card, 3, "clearchs");
1524         rc1 = qeth_clear_channel(&card->read);
1525         rc2 = qeth_clear_channel(&card->write);
1526         rc3 = qeth_clear_channel(&card->data);
1527         if (rc1)
1528                 return rc1;
1529         if (rc2)
1530                 return rc2;
1531         return rc3;
1532 }
1533
1534 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1535 {
1536         int rc = 0;
1537
1538         QETH_CARD_TEXT(card, 3, "clhacrd");
1539
1540         if (halt)
1541                 rc = qeth_halt_channels(card);
1542         if (rc)
1543                 return rc;
1544         return qeth_clear_channels(card);
1545 }
1546
1547 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1548 {
1549         int rc = 0;
1550
1551         QETH_CARD_TEXT(card, 3, "qdioclr");
1552         switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1553                 QETH_QDIO_CLEANING)) {
1554         case QETH_QDIO_ESTABLISHED:
1555                 if (card->info.type == QETH_CARD_TYPE_IQD)
1556                         rc = qdio_shutdown(CARD_DDEV(card),
1557                                 QDIO_FLAG_CLEANUP_USING_HALT);
1558                 else
1559                         rc = qdio_shutdown(CARD_DDEV(card),
1560                                 QDIO_FLAG_CLEANUP_USING_CLEAR);
1561                 if (rc)
1562                         QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1563                 qdio_free(CARD_DDEV(card));
1564                 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1565                 break;
1566         case QETH_QDIO_CLEANING:
1567                 return rc;
1568         default:
1569                 break;
1570         }
1571         rc = qeth_clear_halt_card(card, use_halt);
1572         if (rc)
1573                 QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1574         card->state = CARD_STATE_DOWN;
1575         return rc;
1576 }
1577 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1578
1579 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1580                                int *length)
1581 {
1582         struct ciw *ciw;
1583         char *rcd_buf;
1584         int ret;
1585         struct qeth_channel *channel = &card->data;
1586         unsigned long flags;
1587
1588         /*
1589          * scan for RCD command in extended SenseID data
1590          */
1591         ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1592         if (!ciw || ciw->cmd == 0)
1593                 return -EOPNOTSUPP;
1594         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1595         if (!rcd_buf)
1596                 return -ENOMEM;
1597
1598         channel->ccw.cmd_code = ciw->cmd;
1599         channel->ccw.cda = (__u32) __pa(rcd_buf);
1600         channel->ccw.count = ciw->count;
1601         channel->ccw.flags = CCW_FLAG_SLI;
1602         channel->state = CH_STATE_RCD;
1603         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1604         ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1605                                        QETH_RCD_PARM, LPM_ANYPATH, 0,
1606                                        QETH_RCD_TIMEOUT);
1607         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1608         if (!ret)
1609                 wait_event(card->wait_q,
1610                            (channel->state == CH_STATE_RCD_DONE ||
1611                             channel->state == CH_STATE_DOWN));
1612         if (channel->state == CH_STATE_DOWN)
1613                 ret = -EIO;
1614         else
1615                 channel->state = CH_STATE_DOWN;
1616         if (ret) {
1617                 kfree(rcd_buf);
1618                 *buffer = NULL;
1619                 *length = 0;
1620         } else {
1621                 *length = ciw->count;
1622                 *buffer = rcd_buf;
1623         }
1624         return ret;
1625 }
1626
1627 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1628 {
1629         QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1630         card->info.chpid = prcd[30];
1631         card->info.unit_addr2 = prcd[31];
1632         card->info.cula = prcd[63];
1633         card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1634                                (prcd[0x11] == _ascebc['M']));
1635 }
1636
1637 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1638 {
1639         QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1640
1641         if (prcd[74] == 0xF0 && prcd[75] == 0xF0 && prcd[76] == 0xF5) {
1642                 card->info.blkt.time_total = 250;
1643                 card->info.blkt.inter_packet = 5;
1644                 card->info.blkt.inter_packet_jumbo = 15;
1645         } else {
1646                 card->info.blkt.time_total = 0;
1647                 card->info.blkt.inter_packet = 0;
1648                 card->info.blkt.inter_packet_jumbo = 0;
1649         }
1650 }
1651
1652 static void qeth_init_tokens(struct qeth_card *card)
1653 {
1654         card->token.issuer_rm_w = 0x00010103UL;
1655         card->token.cm_filter_w = 0x00010108UL;
1656         card->token.cm_connection_w = 0x0001010aUL;
1657         card->token.ulp_filter_w = 0x0001010bUL;
1658         card->token.ulp_connection_w = 0x0001010dUL;
1659 }
1660
1661 static void qeth_init_func_level(struct qeth_card *card)
1662 {
1663         switch (card->info.type) {
1664         case QETH_CARD_TYPE_IQD:
1665                 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1666                 break;
1667         case QETH_CARD_TYPE_OSD:
1668         case QETH_CARD_TYPE_OSN:
1669                 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1670                 break;
1671         default:
1672                 break;
1673         }
1674 }
1675
1676 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1677                 void (*idx_reply_cb)(struct qeth_channel *,
1678                         struct qeth_cmd_buffer *))
1679 {
1680         struct qeth_cmd_buffer *iob;
1681         unsigned long flags;
1682         int rc;
1683         struct qeth_card *card;
1684
1685         QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1686         card = CARD_FROM_CDEV(channel->ccwdev);
1687         iob = qeth_get_buffer(channel);
1688         iob->callback = idx_reply_cb;
1689         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1690         channel->ccw.count = QETH_BUFSIZE;
1691         channel->ccw.cda = (__u32) __pa(iob->data);
1692
1693         wait_event(card->wait_q,
1694                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1695         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1696         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1697         rc = ccw_device_start(channel->ccwdev,
1698                               &channel->ccw, (addr_t) iob, 0, 0);
1699         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1700
1701         if (rc) {
1702                 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1703                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1704                 atomic_set(&channel->irq_pending, 0);
1705                 wake_up(&card->wait_q);
1706                 return rc;
1707         }
1708         rc = wait_event_interruptible_timeout(card->wait_q,
1709                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1710         if (rc == -ERESTARTSYS)
1711                 return rc;
1712         if (channel->state != CH_STATE_UP) {
1713                 rc = -ETIME;
1714                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1715                 qeth_clear_cmd_buffers(channel);
1716         } else
1717                 rc = 0;
1718         return rc;
1719 }
1720
1721 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1722                 void (*idx_reply_cb)(struct qeth_channel *,
1723                         struct qeth_cmd_buffer *))
1724 {
1725         struct qeth_card *card;
1726         struct qeth_cmd_buffer *iob;
1727         unsigned long flags;
1728         __u16 temp;
1729         __u8 tmp;
1730         int rc;
1731         struct ccw_dev_id temp_devid;
1732
1733         card = CARD_FROM_CDEV(channel->ccwdev);
1734
1735         QETH_DBF_TEXT(SETUP, 2, "idxactch");
1736
1737         iob = qeth_get_buffer(channel);
1738         iob->callback = idx_reply_cb;
1739         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1740         channel->ccw.count = IDX_ACTIVATE_SIZE;
1741         channel->ccw.cda = (__u32) __pa(iob->data);
1742         if (channel == &card->write) {
1743                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1744                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1745                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1746                 card->seqno.trans_hdr++;
1747         } else {
1748                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1749                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1750                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1751         }
1752         tmp = ((__u8)card->info.portno) | 0x80;
1753         memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1754         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1755                &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1756         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1757                &card->info.func_level, sizeof(__u16));
1758         ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1759         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1760         temp = (card->info.cula << 8) + card->info.unit_addr2;
1761         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1762
1763         wait_event(card->wait_q,
1764                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1765         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1766         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1767         rc = ccw_device_start(channel->ccwdev,
1768                               &channel->ccw, (addr_t) iob, 0, 0);
1769         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1770
1771         if (rc) {
1772                 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1773                         rc);
1774                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1775                 atomic_set(&channel->irq_pending, 0);
1776                 wake_up(&card->wait_q);
1777                 return rc;
1778         }
1779         rc = wait_event_interruptible_timeout(card->wait_q,
1780                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1781         if (rc == -ERESTARTSYS)
1782                 return rc;
1783         if (channel->state != CH_STATE_ACTIVATING) {
1784                 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1785                         " failed to recover an error on the device\n");
1786                 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1787                         dev_name(&channel->ccwdev->dev));
1788                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1789                 qeth_clear_cmd_buffers(channel);
1790                 return -ETIME;
1791         }
1792         return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1793 }
1794
1795 static int qeth_peer_func_level(int level)
1796 {
1797         if ((level & 0xff) == 8)
1798                 return (level & 0xff) + 0x400;
1799         if (((level >> 8) & 3) == 1)
1800                 return (level & 0xff) + 0x200;
1801         return level;
1802 }
1803
1804 static void qeth_idx_write_cb(struct qeth_channel *channel,
1805                 struct qeth_cmd_buffer *iob)
1806 {
1807         struct qeth_card *card;
1808         __u16 temp;
1809
1810         QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1811
1812         if (channel->state == CH_STATE_DOWN) {
1813                 channel->state = CH_STATE_ACTIVATING;
1814                 goto out;
1815         }
1816         card = CARD_FROM_CDEV(channel->ccwdev);
1817
1818         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1819                 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
1820                         dev_err(&card->write.ccwdev->dev,
1821                                 "The adapter is used exclusively by another "
1822                                 "host\n");
1823                 else
1824                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1825                                 " negative reply\n",
1826                                 dev_name(&card->write.ccwdev->dev));
1827                 goto out;
1828         }
1829         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1830         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1831                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
1832                         "function level mismatch (sent: 0x%x, received: "
1833                         "0x%x)\n", dev_name(&card->write.ccwdev->dev),
1834                         card->info.func_level, temp);
1835                 goto out;
1836         }
1837         channel->state = CH_STATE_UP;
1838 out:
1839         qeth_release_buffer(channel, iob);
1840 }
1841
1842 static void qeth_idx_read_cb(struct qeth_channel *channel,
1843                 struct qeth_cmd_buffer *iob)
1844 {
1845         struct qeth_card *card;
1846         __u16 temp;
1847
1848         QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
1849         if (channel->state == CH_STATE_DOWN) {
1850                 channel->state = CH_STATE_ACTIVATING;
1851                 goto out;
1852         }
1853
1854         card = CARD_FROM_CDEV(channel->ccwdev);
1855         if (qeth_check_idx_response(card, iob->data))
1856                         goto out;
1857
1858         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1859                 switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
1860                 case QETH_IDX_ACT_ERR_EXCL:
1861                         dev_err(&card->write.ccwdev->dev,
1862                                 "The adapter is used exclusively by another "
1863                                 "host\n");
1864                         break;
1865                 case QETH_IDX_ACT_ERR_AUTH:
1866                 case QETH_IDX_ACT_ERR_AUTH_USER:
1867                         dev_err(&card->read.ccwdev->dev,
1868                                 "Setting the device online failed because of "
1869                                 "insufficient authorization\n");
1870                         break;
1871                 default:
1872                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
1873                                 " negative reply\n",
1874                                 dev_name(&card->read.ccwdev->dev));
1875                 }
1876                 QETH_CARD_TEXT_(card, 2, "idxread%c",
1877                         QETH_IDX_ACT_CAUSE_CODE(iob->data));
1878                 goto out;
1879         }
1880
1881 /**
1882  *  * temporary fix for microcode bug
1883  *   * to revert it,replace OR by AND
1884  *    */
1885         if ((!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1886              (card->info.type == QETH_CARD_TYPE_OSD))
1887                 card->info.portname_required = 1;
1888
1889         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1890         if (temp != qeth_peer_func_level(card->info.func_level)) {
1891                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
1892                         "level mismatch (sent: 0x%x, received: 0x%x)\n",
1893                         dev_name(&card->read.ccwdev->dev),
1894                         card->info.func_level, temp);
1895                 goto out;
1896         }
1897         memcpy(&card->token.issuer_rm_r,
1898                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1899                QETH_MPC_TOKEN_LENGTH);
1900         memcpy(&card->info.mcl_level[0],
1901                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1902         channel->state = CH_STATE_UP;
1903 out:
1904         qeth_release_buffer(channel, iob);
1905 }
1906
1907 void qeth_prepare_control_data(struct qeth_card *card, int len,
1908                 struct qeth_cmd_buffer *iob)
1909 {
1910         qeth_setup_ccw(&card->write, iob->data, len);
1911         iob->callback = qeth_release_buffer;
1912
1913         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1914                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1915         card->seqno.trans_hdr++;
1916         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1917                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1918         card->seqno.pdu_hdr++;
1919         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1920                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1921         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1922 }
1923 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
1924
1925 int qeth_send_control_data(struct qeth_card *card, int len,
1926                 struct qeth_cmd_buffer *iob,
1927                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1928                         unsigned long),
1929                 void *reply_param)
1930 {
1931         int rc;
1932         unsigned long flags;
1933         struct qeth_reply *reply = NULL;
1934         unsigned long timeout, event_timeout;
1935         struct qeth_ipa_cmd *cmd = NULL;
1936
1937         QETH_CARD_TEXT(card, 2, "sendctl");
1938
1939         if (card->read_or_write_problem) {
1940                 qeth_release_buffer(iob->channel, iob);
1941                 return -EIO;
1942         }
1943         reply = qeth_alloc_reply(card);
1944         if (!reply) {
1945                 return -ENOMEM;
1946         }
1947         reply->callback = reply_cb;
1948         reply->param = reply_param;
1949         if (card->state == CARD_STATE_DOWN)
1950                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1951         else
1952                 reply->seqno = card->seqno.ipa++;
1953         init_waitqueue_head(&reply->wait_q);
1954         spin_lock_irqsave(&card->lock, flags);
1955         list_add_tail(&reply->list, &card->cmd_waiter_list);
1956         spin_unlock_irqrestore(&card->lock, flags);
1957         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1958
1959         while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
1960         qeth_prepare_control_data(card, len, iob);
1961
1962         if (IS_IPA(iob->data)) {
1963                 cmd = __ipa_cmd(iob);
1964                 event_timeout = QETH_IPA_TIMEOUT;
1965         } else {
1966                 event_timeout = QETH_TIMEOUT;
1967         }
1968
1969         timeout = jiffies + event_timeout;
1970
1971         QETH_CARD_TEXT(card, 6, "noirqpnd");
1972         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1973         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1974                               (addr_t) iob, 0, 0);
1975         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1976         if (rc) {
1977                 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
1978                         "ccw_device_start rc = %i\n",
1979                         dev_name(&card->write.ccwdev->dev), rc);
1980                 QETH_CARD_TEXT_(card, 2, " err%d", rc);
1981                 spin_lock_irqsave(&card->lock, flags);
1982                 list_del_init(&reply->list);
1983                 qeth_put_reply(reply);
1984                 spin_unlock_irqrestore(&card->lock, flags);
1985                 qeth_release_buffer(iob->channel, iob);
1986                 atomic_set(&card->write.irq_pending, 0);
1987                 wake_up(&card->wait_q);
1988                 return rc;
1989         }
1990
1991         /* we have only one long running ipassist, since we can ensure
1992            process context of this command we can sleep */
1993         if (cmd && cmd->hdr.command == IPA_CMD_SETIP &&
1994             cmd->hdr.prot_version == QETH_PROT_IPV4) {
1995                 if (!wait_event_timeout(reply->wait_q,
1996                     atomic_read(&reply->received), event_timeout))
1997                         goto time_err;
1998         } else {
1999                 while (!atomic_read(&reply->received)) {
2000                         if (time_after(jiffies, timeout))
2001                                 goto time_err;
2002                         cpu_relax();
2003                 };
2004         }
2005
2006         if (reply->rc == -EIO)
2007                 goto error;
2008         rc = reply->rc;
2009         qeth_put_reply(reply);
2010         return rc;
2011
2012 time_err:
2013         reply->rc = -ETIME;
2014         spin_lock_irqsave(&reply->card->lock, flags);
2015         list_del_init(&reply->list);
2016         spin_unlock_irqrestore(&reply->card->lock, flags);
2017         atomic_inc(&reply->received);
2018 error:
2019         atomic_set(&card->write.irq_pending, 0);
2020         qeth_release_buffer(iob->channel, iob);
2021         card->write.buf_no = (card->write.buf_no + 1) % QETH_CMD_BUFFER_NO;
2022         rc = reply->rc;
2023         qeth_put_reply(reply);
2024         return rc;
2025 }
2026 EXPORT_SYMBOL_GPL(qeth_send_control_data);
2027
2028 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2029                 unsigned long data)
2030 {
2031         struct qeth_cmd_buffer *iob;
2032
2033         QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
2034
2035         iob = (struct qeth_cmd_buffer *) data;
2036         memcpy(&card->token.cm_filter_r,
2037                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2038                QETH_MPC_TOKEN_LENGTH);
2039         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2040         return 0;
2041 }
2042
2043 static int qeth_cm_enable(struct qeth_card *card)
2044 {
2045         int rc;
2046         struct qeth_cmd_buffer *iob;
2047
2048         QETH_DBF_TEXT(SETUP, 2, "cmenable");
2049
2050         iob = qeth_wait_for_buffer(&card->write);
2051         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2052         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2053                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2054         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2055                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2056
2057         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2058                                     qeth_cm_enable_cb, NULL);
2059         return rc;
2060 }
2061
2062 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2063                 unsigned long data)
2064 {
2065
2066         struct qeth_cmd_buffer *iob;
2067
2068         QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
2069
2070         iob = (struct qeth_cmd_buffer *) data;
2071         memcpy(&card->token.cm_connection_r,
2072                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2073                QETH_MPC_TOKEN_LENGTH);
2074         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2075         return 0;
2076 }
2077
2078 static int qeth_cm_setup(struct qeth_card *card)
2079 {
2080         int rc;
2081         struct qeth_cmd_buffer *iob;
2082
2083         QETH_DBF_TEXT(SETUP, 2, "cmsetup");
2084
2085         iob = qeth_wait_for_buffer(&card->write);
2086         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2087         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2088                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2089         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2090                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2091         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2092                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2093         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2094                                     qeth_cm_setup_cb, NULL);
2095         return rc;
2096
2097 }
2098
2099 static inline int qeth_get_initial_mtu_for_card(struct qeth_card *card)
2100 {
2101         switch (card->info.type) {
2102         case QETH_CARD_TYPE_UNKNOWN:
2103                 return 1500;
2104         case QETH_CARD_TYPE_IQD:
2105                 return card->info.max_mtu;
2106         case QETH_CARD_TYPE_OSD:
2107                 switch (card->info.link_type) {
2108                 case QETH_LINK_TYPE_HSTR:
2109                 case QETH_LINK_TYPE_LANE_TR:
2110                         return 2000;
2111                 default:
2112                         return 1492;
2113                 }
2114         case QETH_CARD_TYPE_OSM:
2115         case QETH_CARD_TYPE_OSX:
2116                 return 1492;
2117         default:
2118                 return 1500;
2119         }
2120 }
2121
2122 static inline int qeth_get_mtu_outof_framesize(int framesize)
2123 {
2124         switch (framesize) {
2125         case 0x4000:
2126                 return 8192;
2127         case 0x6000:
2128                 return 16384;
2129         case 0xa000:
2130                 return 32768;
2131         case 0xffff:
2132                 return 57344;
2133         default:
2134                 return 0;
2135         }
2136 }
2137
2138 static inline int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
2139 {
2140         switch (card->info.type) {
2141         case QETH_CARD_TYPE_OSD:
2142         case QETH_CARD_TYPE_OSM:
2143         case QETH_CARD_TYPE_OSX:
2144         case QETH_CARD_TYPE_IQD:
2145                 return ((mtu >= 576) &&
2146                         (mtu <= card->info.max_mtu));
2147         case QETH_CARD_TYPE_OSN:
2148         case QETH_CARD_TYPE_UNKNOWN:
2149         default:
2150                 return 1;
2151         }
2152 }
2153
2154 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2155                 unsigned long data)
2156 {
2157
2158         __u16 mtu, framesize;
2159         __u16 len;
2160         __u8 link_type;
2161         struct qeth_cmd_buffer *iob;
2162
2163         QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
2164
2165         iob = (struct qeth_cmd_buffer *) data;
2166         memcpy(&card->token.ulp_filter_r,
2167                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2168                QETH_MPC_TOKEN_LENGTH);
2169         if (card->info.type == QETH_CARD_TYPE_IQD) {
2170                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2171                 mtu = qeth_get_mtu_outof_framesize(framesize);
2172                 if (!mtu) {
2173                         iob->rc = -EINVAL;
2174                         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2175                         return 0;
2176                 }
2177                 if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
2178                         /* frame size has changed */
2179                         if (card->dev &&
2180                             ((card->dev->mtu == card->info.initial_mtu) ||
2181                              (card->dev->mtu > mtu)))
2182                                 card->dev->mtu = mtu;
2183                         qeth_free_qdio_buffers(card);
2184                 }
2185                 card->info.initial_mtu = mtu;
2186                 card->info.max_mtu = mtu;
2187                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2188         } else {
2189                 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
2190                 card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
2191                         iob->data);
2192                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2193         }
2194
2195         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2196         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2197                 memcpy(&link_type,
2198                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2199                 card->info.link_type = link_type;
2200         } else
2201                 card->info.link_type = 0;
2202         QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
2203         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2204         return 0;
2205 }
2206
2207 static int qeth_ulp_enable(struct qeth_card *card)
2208 {
2209         int rc;
2210         char prot_type;
2211         struct qeth_cmd_buffer *iob;
2212
2213         /*FIXME: trace view callbacks*/
2214         QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
2215
2216         iob = qeth_wait_for_buffer(&card->write);
2217         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2218
2219         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2220                 (__u8) card->info.portno;
2221         if (card->options.layer2)
2222                 if (card->info.type == QETH_CARD_TYPE_OSN)
2223                         prot_type = QETH_PROT_OSN2;
2224                 else
2225                         prot_type = QETH_PROT_LAYER2;
2226         else
2227                 prot_type = QETH_PROT_TCPIP;
2228
2229         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2230         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2231                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2232         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2233                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2234         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2235                card->info.portname, 9);
2236         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2237                                     qeth_ulp_enable_cb, NULL);
2238         return rc;
2239
2240 }
2241
2242 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2243                 unsigned long data)
2244 {
2245         struct qeth_cmd_buffer *iob;
2246         int rc = 0;
2247
2248         QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
2249
2250         iob = (struct qeth_cmd_buffer *) data;
2251         memcpy(&card->token.ulp_connection_r,
2252                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2253                QETH_MPC_TOKEN_LENGTH);
2254         if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2255                      3)) {
2256                 QETH_DBF_TEXT(SETUP, 2, "olmlimit");
2257                 dev_err(&card->gdev->dev, "A connection could not be "
2258                         "established because of an OLM limit\n");
2259                 iob->rc = -EMLINK;
2260         }
2261         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2262         return rc;
2263 }
2264
2265 static int qeth_ulp_setup(struct qeth_card *card)
2266 {
2267         int rc;
2268         __u16 temp;
2269         struct qeth_cmd_buffer *iob;
2270         struct ccw_dev_id dev_id;
2271
2272         QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2273
2274         iob = qeth_wait_for_buffer(&card->write);
2275         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2276
2277         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2278                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2279         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2280                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2281         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2282                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2283
2284         ccw_device_get_id(CARD_DDEV(card), &dev_id);
2285         memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2286         temp = (card->info.cula << 8) + card->info.unit_addr2;
2287         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2288         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2289                                     qeth_ulp_setup_cb, NULL);
2290         return rc;
2291 }
2292
2293 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2294 {
2295         int rc;
2296         struct qeth_qdio_out_buffer *newbuf;
2297
2298         rc = 0;
2299         newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2300         if (!newbuf) {
2301                 rc = -ENOMEM;
2302                 goto out;
2303         }
2304         newbuf->buffer = &q->qdio_bufs[bidx];
2305         skb_queue_head_init(&newbuf->skb_list);
2306         lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2307         newbuf->q = q;
2308         newbuf->aob = NULL;
2309         newbuf->next_pending = q->bufs[bidx];
2310         atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2311         q->bufs[bidx] = newbuf;
2312         if (q->bufstates) {
2313                 q->bufstates[bidx].user = newbuf;
2314                 QETH_CARD_TEXT_(q->card, 2, "nbs%d", bidx);
2315                 QETH_CARD_TEXT_(q->card, 2, "%lx", (long) newbuf);
2316                 QETH_CARD_TEXT_(q->card, 2, "%lx",
2317                                 (long) newbuf->next_pending);
2318         }
2319 out:
2320         return rc;
2321 }
2322
2323
2324 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2325 {
2326         int i, j;
2327
2328         QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2329
2330         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2331                 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2332                 return 0;
2333
2334         card->qdio.in_q = kzalloc(sizeof(struct qeth_qdio_q),
2335                                    GFP_KERNEL);
2336         if (!card->qdio.in_q)
2337                 goto out_nomem;
2338         QETH_DBF_TEXT(SETUP, 2, "inq");
2339         QETH_DBF_HEX(SETUP, 2, &card->qdio.in_q, sizeof(void *));
2340         memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
2341         /* give inbound qeth_qdio_buffers their qdio_buffers */
2342         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
2343                 card->qdio.in_q->bufs[i].buffer =
2344                         &card->qdio.in_q->qdio_bufs[i];
2345                 card->qdio.in_q->bufs[i].rx_skb = NULL;
2346         }
2347         /* inbound buffer pool */
2348         if (qeth_alloc_buffer_pool(card))
2349                 goto out_freeinq;
2350
2351         /* outbound */
2352         card->qdio.out_qs =
2353                 kzalloc(card->qdio.no_out_queues *
2354                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2355         if (!card->qdio.out_qs)
2356                 goto out_freepool;
2357         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2358                 card->qdio.out_qs[i] = kzalloc(sizeof(struct qeth_qdio_out_q),
2359                                                GFP_KERNEL);
2360                 if (!card->qdio.out_qs[i])
2361                         goto out_freeoutq;
2362                 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2363                 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2364                 card->qdio.out_qs[i]->queue_no = i;
2365                 /* give outbound qeth_qdio_buffers their qdio_buffers */
2366                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2367                         BUG_ON(card->qdio.out_qs[i]->bufs[j] != NULL);
2368                         if (qeth_init_qdio_out_buf(card->qdio.out_qs[i], j))
2369                                 goto out_freeoutqbufs;
2370                 }
2371         }
2372
2373         /* completion */
2374         if (qeth_alloc_cq(card))
2375                 goto out_freeoutq;
2376
2377         return 0;
2378
2379 out_freeoutqbufs:
2380         while (j > 0) {
2381                 --j;
2382                 kmem_cache_free(qeth_qdio_outbuf_cache,
2383                                 card->qdio.out_qs[i]->bufs[j]);
2384                 card->qdio.out_qs[i]->bufs[j] = NULL;
2385         }
2386 out_freeoutq:
2387         while (i > 0) {
2388                 kfree(card->qdio.out_qs[--i]);
2389                 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2390         }
2391         kfree(card->qdio.out_qs);
2392         card->qdio.out_qs = NULL;
2393 out_freepool:
2394         qeth_free_buffer_pool(card);
2395 out_freeinq:
2396         kfree(card->qdio.in_q);
2397         card->qdio.in_q = NULL;
2398 out_nomem:
2399         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2400         return -ENOMEM;
2401 }
2402
2403 static void qeth_create_qib_param_field(struct qeth_card *card,
2404                 char *param_field)
2405 {
2406
2407         param_field[0] = _ascebc['P'];
2408         param_field[1] = _ascebc['C'];
2409         param_field[2] = _ascebc['I'];
2410         param_field[3] = _ascebc['T'];
2411         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2412         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2413         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2414 }
2415
2416 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2417                 char *param_field)
2418 {
2419         param_field[16] = _ascebc['B'];
2420         param_field[17] = _ascebc['L'];
2421         param_field[18] = _ascebc['K'];
2422         param_field[19] = _ascebc['T'];
2423         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2424         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2425         *((unsigned int *) (&param_field[28])) =
2426                 card->info.blkt.inter_packet_jumbo;
2427 }
2428
2429 static int qeth_qdio_activate(struct qeth_card *card)
2430 {
2431         QETH_DBF_TEXT(SETUP, 3, "qdioact");
2432         return qdio_activate(CARD_DDEV(card));
2433 }
2434
2435 static int qeth_dm_act(struct qeth_card *card)
2436 {
2437         int rc;
2438         struct qeth_cmd_buffer *iob;
2439
2440         QETH_DBF_TEXT(SETUP, 2, "dmact");
2441
2442         iob = qeth_wait_for_buffer(&card->write);
2443         memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2444
2445         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2446                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2447         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2448                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2449         rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2450         return rc;
2451 }
2452
2453 static int qeth_mpc_initialize(struct qeth_card *card)
2454 {
2455         int rc;
2456
2457         QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2458
2459         rc = qeth_issue_next_read(card);
2460         if (rc) {
2461                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2462                 return rc;
2463         }
2464         rc = qeth_cm_enable(card);
2465         if (rc) {
2466                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2467                 goto out_qdio;
2468         }
2469         rc = qeth_cm_setup(card);
2470         if (rc) {
2471                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2472                 goto out_qdio;
2473         }
2474         rc = qeth_ulp_enable(card);
2475         if (rc) {
2476                 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2477                 goto out_qdio;
2478         }
2479         rc = qeth_ulp_setup(card);
2480         if (rc) {
2481                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2482                 goto out_qdio;
2483         }
2484         rc = qeth_alloc_qdio_buffers(card);
2485         if (rc) {
2486                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2487                 goto out_qdio;
2488         }
2489         rc = qeth_qdio_establish(card);
2490         if (rc) {
2491                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2492                 qeth_free_qdio_buffers(card);
2493                 goto out_qdio;
2494         }
2495         rc = qeth_qdio_activate(card);
2496         if (rc) {
2497                 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2498                 goto out_qdio;
2499         }
2500         rc = qeth_dm_act(card);
2501         if (rc) {
2502                 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2503                 goto out_qdio;
2504         }
2505
2506         return 0;
2507 out_qdio:
2508         qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2509         return rc;
2510 }
2511
2512 static void qeth_print_status_with_portname(struct qeth_card *card)
2513 {
2514         char dbf_text[15];
2515         int i;
2516
2517         sprintf(dbf_text, "%s", card->info.portname + 1);
2518         for (i = 0; i < 8; i++)
2519                 dbf_text[i] =
2520                         (char) _ebcasc[(__u8) dbf_text[i]];
2521         dbf_text[8] = 0;
2522         dev_info(&card->gdev->dev, "Device is a%s card%s%s%s\n"
2523                "with link type %s (portname: %s)\n",
2524                qeth_get_cardname(card),
2525                (card->info.mcl_level[0]) ? " (level: " : "",
2526                (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2527                (card->info.mcl_level[0]) ? ")" : "",
2528                qeth_get_cardname_short(card),
2529                dbf_text);
2530
2531 }
2532
2533 static void qeth_print_status_no_portname(struct qeth_card *card)
2534 {
2535         if (card->info.portname[0])
2536                 dev_info(&card->gdev->dev, "Device is a%s "
2537                        "card%s%s%s\nwith link type %s "
2538                        "(no portname needed by interface).\n",
2539                        qeth_get_cardname(card),
2540                        (card->info.mcl_level[0]) ? " (level: " : "",
2541                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2542                        (card->info.mcl_level[0]) ? ")" : "",
2543                        qeth_get_cardname_short(card));
2544         else
2545                 dev_info(&card->gdev->dev, "Device is a%s "
2546                        "card%s%s%s\nwith link type %s.\n",
2547                        qeth_get_cardname(card),
2548                        (card->info.mcl_level[0]) ? " (level: " : "",
2549                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2550                        (card->info.mcl_level[0]) ? ")" : "",
2551                        qeth_get_cardname_short(card));
2552 }
2553
2554 void qeth_print_status_message(struct qeth_card *card)
2555 {
2556         switch (card->info.type) {
2557         case QETH_CARD_TYPE_OSD:
2558         case QETH_CARD_TYPE_OSM:
2559         case QETH_CARD_TYPE_OSX:
2560                 /* VM will use a non-zero first character
2561                  * to indicate a HiperSockets like reporting
2562                  * of the level OSA sets the first character to zero
2563                  * */
2564                 if (!card->info.mcl_level[0]) {
2565                         sprintf(card->info.mcl_level, "%02x%02x",
2566                                 card->info.mcl_level[2],
2567                                 card->info.mcl_level[3]);
2568
2569                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2570                         break;
2571                 }
2572                 /* fallthrough */
2573         case QETH_CARD_TYPE_IQD:
2574                 if ((card->info.guestlan) ||
2575                     (card->info.mcl_level[0] & 0x80)) {
2576                         card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2577                                 card->info.mcl_level[0]];
2578                         card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2579                                 card->info.mcl_level[1]];
2580                         card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2581                                 card->info.mcl_level[2]];
2582                         card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2583                                 card->info.mcl_level[3]];
2584                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2585                 }
2586                 break;
2587         default:
2588                 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2589         }
2590         if (card->info.portname_required)
2591                 qeth_print_status_with_portname(card);
2592         else
2593                 qeth_print_status_no_portname(card);
2594 }
2595 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2596
2597 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2598 {
2599         struct qeth_buffer_pool_entry *entry;
2600
2601         QETH_CARD_TEXT(card, 5, "inwrklst");
2602
2603         list_for_each_entry(entry,
2604                             &card->qdio.init_pool.entry_list, init_list) {
2605                 qeth_put_buffer_pool_entry(card, entry);
2606         }
2607 }
2608
2609 static inline struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2610                 struct qeth_card *card)
2611 {
2612         struct list_head *plh;
2613         struct qeth_buffer_pool_entry *entry;
2614         int i, free;
2615         struct page *page;
2616
2617         if (list_empty(&card->qdio.in_buf_pool.entry_list))
2618                 return NULL;
2619
2620         list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2621                 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2622                 free = 1;
2623                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2624                         if (page_count(virt_to_page(entry->elements[i])) > 1) {
2625                                 free = 0;
2626                                 break;
2627                         }
2628                 }
2629                 if (free) {
2630                         list_del_init(&entry->list);
2631                         return entry;
2632                 }
2633         }
2634
2635         /* no free buffer in pool so take first one and swap pages */
2636         entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2637                         struct qeth_buffer_pool_entry, list);
2638         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2639                 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2640                         page = alloc_page(GFP_ATOMIC);
2641                         if (!page) {
2642                                 return NULL;
2643                         } else {
2644                                 free_page((unsigned long)entry->elements[i]);
2645                                 entry->elements[i] = page_address(page);
2646                                 if (card->options.performance_stats)
2647                                         card->perf_stats.sg_alloc_page_rx++;
2648                         }
2649                 }
2650         }
2651         list_del_init(&entry->list);
2652         return entry;
2653 }
2654
2655 static int qeth_init_input_buffer(struct qeth_card *card,
2656                 struct qeth_qdio_buffer *buf)
2657 {
2658         struct qeth_buffer_pool_entry *pool_entry;
2659         int i;
2660
2661         if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2662                 buf->rx_skb = dev_alloc_skb(QETH_RX_PULL_LEN + ETH_HLEN);
2663                 if (!buf->rx_skb)
2664                         return 1;
2665         }
2666
2667         pool_entry = qeth_find_free_buffer_pool_entry(card);
2668         if (!pool_entry)
2669                 return 1;
2670
2671         /*
2672          * since the buffer is accessed only from the input_tasklet
2673          * there shouldn't be a need to synchronize; also, since we use
2674          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2675          * buffers
2676          */
2677
2678         buf->pool_entry = pool_entry;
2679         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2680                 buf->buffer->element[i].length = PAGE_SIZE;
2681                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2682                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2683                         buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2684                 else
2685                         buf->buffer->element[i].eflags = 0;
2686                 buf->buffer->element[i].sflags = 0;
2687         }
2688         return 0;
2689 }
2690
2691 int qeth_init_qdio_queues(struct qeth_card *card)
2692 {
2693         int i, j;
2694         int rc;
2695
2696         QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2697
2698         /* inbound queue */
2699         memset(card->qdio.in_q->qdio_bufs, 0,
2700                QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2701         qeth_initialize_working_pool_list(card);
2702         /*give only as many buffers to hardware as we have buffer pool entries*/
2703         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2704                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2705         card->qdio.in_q->next_buf_to_init =
2706                 card->qdio.in_buf_pool.buf_count - 1;
2707         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2708                      card->qdio.in_buf_pool.buf_count - 1);
2709         if (rc) {
2710                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2711                 return rc;
2712         }
2713
2714         /* completion */
2715         rc = qeth_cq_init(card);
2716         if (rc) {
2717                 return rc;
2718         }
2719
2720         /* outbound queue */
2721         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2722                 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
2723                        QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2724                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2725                         qeth_clear_output_buffer(card->qdio.out_qs[i],
2726                                         card->qdio.out_qs[i]->bufs[j],
2727                                         QETH_QDIO_BUF_EMPTY);
2728                 }
2729                 card->qdio.out_qs[i]->card = card;
2730                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2731                 card->qdio.out_qs[i]->do_pack = 0;
2732                 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2733                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2734                 atomic_set(&card->qdio.out_qs[i]->state,
2735                            QETH_OUT_Q_UNLOCKED);
2736         }
2737         return 0;
2738 }
2739 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2740
2741 static inline __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2742 {
2743         switch (link_type) {
2744         case QETH_LINK_TYPE_HSTR:
2745                 return 2;
2746         default:
2747                 return 1;
2748         }
2749 }
2750
2751 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2752                 struct qeth_ipa_cmd *cmd, __u8 command,
2753                 enum qeth_prot_versions prot)
2754 {
2755         memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2756         cmd->hdr.command = command;
2757         cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2758         cmd->hdr.seqno = card->seqno.ipa;
2759         cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2760         cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2761         if (card->options.layer2)
2762                 cmd->hdr.prim_version_no = 2;
2763         else
2764                 cmd->hdr.prim_version_no = 1;
2765         cmd->hdr.param_count = 1;
2766         cmd->hdr.prot_version = prot;
2767         cmd->hdr.ipa_supported = 0;
2768         cmd->hdr.ipa_enabled = 0;
2769 }
2770
2771 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2772                 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2773 {
2774         struct qeth_cmd_buffer *iob;
2775         struct qeth_ipa_cmd *cmd;
2776
2777         iob = qeth_wait_for_buffer(&card->write);
2778         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2779         qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2780
2781         return iob;
2782 }
2783 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2784
2785 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2786                 char prot_type)
2787 {
2788         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2789         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2790         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2791                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2792 }
2793 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2794
2795 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2796                 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2797                         unsigned long),
2798                 void *reply_param)
2799 {
2800         int rc;
2801         char prot_type;
2802
2803         QETH_CARD_TEXT(card, 4, "sendipa");
2804
2805         if (card->options.layer2)
2806                 if (card->info.type == QETH_CARD_TYPE_OSN)
2807                         prot_type = QETH_PROT_OSN2;
2808                 else
2809                         prot_type = QETH_PROT_LAYER2;
2810         else
2811                 prot_type = QETH_PROT_TCPIP;
2812         qeth_prepare_ipa_cmd(card, iob, prot_type);
2813         rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
2814                                                 iob, reply_cb, reply_param);
2815         if (rc == -ETIME) {
2816                 qeth_clear_ipacmd_list(card);
2817                 qeth_schedule_recovery(card);
2818         }
2819         return rc;
2820 }
2821 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
2822
2823 int qeth_send_startlan(struct qeth_card *card)
2824 {
2825         int rc;
2826         struct qeth_cmd_buffer *iob;
2827
2828         QETH_DBF_TEXT(SETUP, 2, "strtlan");
2829
2830         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
2831         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
2832         return rc;
2833 }
2834 EXPORT_SYMBOL_GPL(qeth_send_startlan);
2835
2836 int qeth_default_setadapterparms_cb(struct qeth_card *card,
2837                 struct qeth_reply *reply, unsigned long data)
2838 {
2839         struct qeth_ipa_cmd *cmd;
2840
2841         QETH_CARD_TEXT(card, 4, "defadpcb");
2842
2843         cmd = (struct qeth_ipa_cmd *) data;
2844         if (cmd->hdr.return_code == 0)
2845                 cmd->hdr.return_code =
2846                         cmd->data.setadapterparms.hdr.return_code;
2847         return 0;
2848 }
2849 EXPORT_SYMBOL_GPL(qeth_default_setadapterparms_cb);
2850
2851 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
2852                 struct qeth_reply *reply, unsigned long data)
2853 {
2854         struct qeth_ipa_cmd *cmd;
2855
2856         QETH_CARD_TEXT(card, 3, "quyadpcb");
2857
2858         cmd = (struct qeth_ipa_cmd *) data;
2859         if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
2860                 card->info.link_type =
2861                       cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
2862                 QETH_DBF_TEXT_(SETUP, 2, "lnk %d", card->info.link_type);
2863         }
2864         card->options.adp.supported_funcs =
2865                 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
2866         return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
2867 }
2868
2869 struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
2870                 __u32 command, __u32 cmdlen)
2871 {
2872         struct qeth_cmd_buffer *iob;
2873         struct qeth_ipa_cmd *cmd;
2874
2875         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
2876                                      QETH_PROT_IPV4);
2877         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2878         cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
2879         cmd->data.setadapterparms.hdr.command_code = command;
2880         cmd->data.setadapterparms.hdr.used_total = 1;
2881         cmd->data.setadapterparms.hdr.seq_no = 1;
2882
2883         return iob;
2884 }
2885 EXPORT_SYMBOL_GPL(qeth_get_adapter_cmd);
2886
2887 int qeth_query_setadapterparms(struct qeth_card *card)
2888 {
2889         int rc;
2890         struct qeth_cmd_buffer *iob;
2891
2892         QETH_CARD_TEXT(card, 3, "queryadp");
2893         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
2894                                    sizeof(struct qeth_ipacmd_setadpparms));
2895         rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
2896         return rc;
2897 }
2898 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
2899
2900 static int qeth_query_ipassists_cb(struct qeth_card *card,
2901                 struct qeth_reply *reply, unsigned long data)
2902 {
2903         struct qeth_ipa_cmd *cmd;
2904
2905         QETH_DBF_TEXT(SETUP, 2, "qipasscb");
2906
2907         cmd = (struct qeth_ipa_cmd *) data;
2908         if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
2909                 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
2910                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
2911         } else {
2912                 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
2913                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
2914         }
2915         QETH_DBF_TEXT(SETUP, 2, "suppenbl");
2916         QETH_DBF_TEXT_(SETUP, 2, "%x", cmd->hdr.ipa_supported);
2917         QETH_DBF_TEXT_(SETUP, 2, "%x", cmd->hdr.ipa_enabled);
2918         return 0;
2919 }
2920
2921 int qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
2922 {
2923         int rc;
2924         struct qeth_cmd_buffer *iob;
2925
2926         QETH_DBF_TEXT_(SETUP, 2, "qipassi%i", prot);
2927         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_QIPASSIST, prot);
2928         rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
2929         return rc;
2930 }
2931 EXPORT_SYMBOL_GPL(qeth_query_ipassists);
2932
2933 static int qeth_query_setdiagass_cb(struct qeth_card *card,
2934                 struct qeth_reply *reply, unsigned long data)
2935 {
2936         struct qeth_ipa_cmd *cmd;
2937         __u16 rc;
2938
2939         cmd = (struct qeth_ipa_cmd *)data;
2940         rc = cmd->hdr.return_code;
2941         if (rc)
2942                 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
2943         else
2944                 card->info.diagass_support = cmd->data.diagass.ext;
2945         return 0;
2946 }
2947
2948 static int qeth_query_setdiagass(struct qeth_card *card)
2949 {
2950         struct qeth_cmd_buffer *iob;
2951         struct qeth_ipa_cmd    *cmd;
2952
2953         QETH_DBF_TEXT(SETUP, 2, "qdiagass");
2954         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
2955         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2956         cmd->data.diagass.subcmd_len = 16;
2957         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
2958         return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
2959 }
2960
2961 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
2962 {
2963         unsigned long info = get_zeroed_page(GFP_KERNEL);
2964         struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
2965         struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
2966         struct ccw_dev_id ccwid;
2967         int level, rc;
2968
2969         tid->chpid = card->info.chpid;
2970         ccw_device_get_id(CARD_RDEV(card), &ccwid);
2971         tid->ssid = ccwid.ssid;
2972         tid->devno = ccwid.devno;
2973         if (!info)
2974                 return;
2975
2976         rc = stsi(NULL, 0, 0, 0);
2977         if (rc == -ENOSYS)
2978                 level = rc;
2979         else
2980                 level = (((unsigned int) rc) >> 28);
2981
2982         if ((level >= 2) && (stsi(info222, 2, 2, 2) != -ENOSYS))
2983                 tid->lparnr = info222->lpar_number;
2984
2985         if ((level >= 3) && (stsi(info322, 3, 2, 2) != -ENOSYS)) {
2986                 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
2987                 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
2988         }
2989         free_page(info);
2990         return;
2991 }
2992
2993 static int qeth_hw_trap_cb(struct qeth_card *card,
2994                 struct qeth_reply *reply, unsigned long data)
2995 {
2996         struct qeth_ipa_cmd *cmd;
2997         __u16 rc;
2998
2999         cmd = (struct qeth_ipa_cmd *)data;
3000         rc = cmd->hdr.return_code;
3001         if (rc)
3002                 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3003         return 0;
3004 }
3005
3006 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3007 {
3008         struct qeth_cmd_buffer *iob;
3009         struct qeth_ipa_cmd *cmd;
3010
3011         QETH_DBF_TEXT(SETUP, 2, "diagtrap");
3012         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3013         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3014         cmd->data.diagass.subcmd_len = 80;
3015         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
3016         cmd->data.diagass.type = 1;
3017         cmd->data.diagass.action = action;
3018         switch (action) {
3019         case QETH_DIAGS_TRAP_ARM:
3020                 cmd->data.diagass.options = 0x0003;
3021                 cmd->data.diagass.ext = 0x00010000 +
3022                         sizeof(struct qeth_trap_id);