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