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