Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

Linux/drivers/s390/net/qeth_core_main.c

  1 /*
  2  *    Copyright IBM Corp. 2007, 2009
  3  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
  4  *               Frank Pavlic <fpavlic@de.ibm.com>,
  5  *               Thomas Spatzier <tspat@de.ibm.com>,
  6  *               Frank Blaschka <frank.blaschka@de.ibm.com>
  7  */
  8 
  9 #define KMSG_COMPONENT "qeth"
 10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 11 
 12 #include <linux/module.h>
 13 #include <linux/moduleparam.h>
 14 #include <linux/string.h>
 15 #include <linux/errno.h>
 16 #include <linux/kernel.h>
 17 #include <linux/ip.h>
 18 #include <linux/tcp.h>
 19 #include <linux/mii.h>
 20 #include <linux/kthread.h>
 21 #include <linux/slab.h>
 22 #include <net/iucv/af_iucv.h>
 23 #include <net/dsfield.h>
 24 
 25 #include <asm/ebcdic.h>
 26 #include <asm/chpid.h>
 27 #include <asm/io.h>
 28 #include <asm/sysinfo.h>
 29 #include <asm/compat.h>
 30 
 31 #include "qeth_core.h"
 32 
 33 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
 34         /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
 35         /*                   N  P  A    M  L  V                      H  */
 36         [QETH_DBF_SETUP] = {"qeth_setup",
 37                                 8, 1,   8, 5, &debug_hex_ascii_view, NULL},
 38         [QETH_DBF_MSG]   = {"qeth_msg", 8, 1, 11 * sizeof(long), 3,
 39                             &debug_sprintf_view, NULL},
 40         [QETH_DBF_CTRL]  = {"qeth_control",
 41                 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
 42 };
 43 EXPORT_SYMBOL_GPL(qeth_dbf);
 44 
 45 struct qeth_card_list_struct qeth_core_card_list;
 46 EXPORT_SYMBOL_GPL(qeth_core_card_list);
 47 struct kmem_cache *qeth_core_header_cache;
 48 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
 49 static struct kmem_cache *qeth_qdio_outbuf_cache;
 50 
 51 static struct device *qeth_core_root_dev;
 52 static unsigned int known_devices[][6] = QETH_MODELLIST_ARRAY;
 53 static struct lock_class_key qdio_out_skb_queue_key;
 54 static struct mutex qeth_mod_mutex;
 55 
 56 static void qeth_send_control_data_cb(struct qeth_channel *,
 57                         struct qeth_cmd_buffer *);
 58 static int qeth_issue_next_read(struct qeth_card *);
 59 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
 60 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
 61 static void qeth_free_buffer_pool(struct qeth_card *);
 62 static int qeth_qdio_establish(struct qeth_card *);
 63 static void qeth_free_qdio_buffers(struct qeth_card *);
 64 static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
 65                 struct qeth_qdio_out_buffer *buf,
 66                 enum iucv_tx_notify notification);
 67 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf);
 68 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
 69                 struct qeth_qdio_out_buffer *buf,
 70                 enum qeth_qdio_buffer_states newbufstate);
 71 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *, int);
 72 
 73 struct workqueue_struct *qeth_wq;
 74 EXPORT_SYMBOL_GPL(qeth_wq);
 75 
 76 int qeth_card_hw_is_reachable(struct qeth_card *card)
 77 {
 78         return (card->state == CARD_STATE_SOFTSETUP) ||
 79                 (card->state == CARD_STATE_UP);
 80 }
 81 EXPORT_SYMBOL_GPL(qeth_card_hw_is_reachable);
 82 
 83 static void qeth_close_dev_handler(struct work_struct *work)
 84 {
 85         struct qeth_card *card;
 86 
 87         card = container_of(work, struct qeth_card, close_dev_work);
 88         QETH_CARD_TEXT(card, 2, "cldevhdl");
 89         rtnl_lock();
 90         dev_close(card->dev);
 91         rtnl_unlock();
 92         ccwgroup_set_offline(card->gdev);
 93 }
 94 
 95 void qeth_close_dev(struct qeth_card *card)
 96 {
 97         QETH_CARD_TEXT(card, 2, "cldevsubm");
 98         queue_work(qeth_wq, &card->close_dev_work);
 99 }
100 EXPORT_SYMBOL_GPL(qeth_close_dev);
101 
102 static inline const char *qeth_get_cardname(struct qeth_card *card)
103 {
104         if (card->info.guestlan) {
105                 switch (card->info.type) {
106                 case QETH_CARD_TYPE_OSD:
107                         return " Virtual NIC QDIO";
108                 case QETH_CARD_TYPE_IQD:
109                         return " Virtual NIC Hiper";
110                 case QETH_CARD_TYPE_OSM:
111                         return " Virtual NIC QDIO - OSM";
112                 case QETH_CARD_TYPE_OSX:
113                         return " Virtual NIC QDIO - OSX";
114                 default:
115                         return " unknown";
116                 }
117         } else {
118                 switch (card->info.type) {
119                 case QETH_CARD_TYPE_OSD:
120                         return " OSD Express";
121                 case QETH_CARD_TYPE_IQD:
122                         return " HiperSockets";
123                 case QETH_CARD_TYPE_OSN:
124                         return " OSN QDIO";
125                 case QETH_CARD_TYPE_OSM:
126                         return " OSM QDIO";
127                 case QETH_CARD_TYPE_OSX:
128                         return " OSX QDIO";
129                 default:
130                         return " unknown";
131                 }
132         }
133         return " n/a";
134 }
135 
136 /* max length to be returned: 14 */
137 const char *qeth_get_cardname_short(struct qeth_card *card)
138 {
139         if (card->info.guestlan) {
140                 switch (card->info.type) {
141                 case QETH_CARD_TYPE_OSD:
142                         return "Virt.NIC QDIO";
143                 case QETH_CARD_TYPE_IQD:
144                         return "Virt.NIC Hiper";
145                 case QETH_CARD_TYPE_OSM:
146                         return "Virt.NIC OSM";
147                 case QETH_CARD_TYPE_OSX:
148                         return "Virt.NIC OSX";
149                 default:
150                         return "unknown";
151                 }
152         } else {
153                 switch (card->info.type) {
154                 case QETH_CARD_TYPE_OSD:
155                         switch (card->info.link_type) {
156                         case QETH_LINK_TYPE_FAST_ETH:
157                                 return "OSD_100";
158                         case QETH_LINK_TYPE_HSTR:
159                                 return "HSTR";
160                         case QETH_LINK_TYPE_GBIT_ETH:
161                                 return "OSD_1000";
162                         case QETH_LINK_TYPE_10GBIT_ETH:
163                                 return "OSD_10GIG";
164                         case QETH_LINK_TYPE_LANE_ETH100:
165                                 return "OSD_FE_LANE";
166                         case QETH_LINK_TYPE_LANE_TR:
167                                 return "OSD_TR_LANE";
168                         case QETH_LINK_TYPE_LANE_ETH1000:
169                                 return "OSD_GbE_LANE";
170                         case QETH_LINK_TYPE_LANE:
171                                 return "OSD_ATM_LANE";
172                         default:
173                                 return "OSD_Express";
174                         }
175                 case QETH_CARD_TYPE_IQD:
176                         return "HiperSockets";
177                 case QETH_CARD_TYPE_OSN:
178                         return "OSN";
179                 case QETH_CARD_TYPE_OSM:
180                         return "OSM_1000";
181                 case QETH_CARD_TYPE_OSX:
182                         return "OSX_10GIG";
183                 default:
184                         return "unknown";
185                 }
186         }
187         return "n/a";
188 }
189 
190 void qeth_set_recovery_task(struct qeth_card *card)
191 {
192         card->recovery_task = current;
193 }
194 EXPORT_SYMBOL_GPL(qeth_set_recovery_task);
195 
196 void qeth_clear_recovery_task(struct qeth_card *card)
197 {
198         card->recovery_task = NULL;
199 }
200 EXPORT_SYMBOL_GPL(qeth_clear_recovery_task);
201 
202 static bool qeth_is_recovery_task(const struct qeth_card *card)
203 {
204         return card->recovery_task == current;
205 }
206 
207 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
208                          int clear_start_mask)
209 {
210         unsigned long flags;
211 
212         spin_lock_irqsave(&card->thread_mask_lock, flags);
213         card->thread_allowed_mask = threads;
214         if (clear_start_mask)
215                 card->thread_start_mask &= threads;
216         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
217         wake_up(&card->wait_q);
218 }
219 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
220 
221 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
222 {
223         unsigned long flags;
224         int rc = 0;
225 
226         spin_lock_irqsave(&card->thread_mask_lock, flags);
227         rc = (card->thread_running_mask & threads);
228         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
229         return rc;
230 }
231 EXPORT_SYMBOL_GPL(qeth_threads_running);
232 
233 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
234 {
235         if (qeth_is_recovery_task(card))
236                 return 0;
237         return wait_event_interruptible(card->wait_q,
238                         qeth_threads_running(card, threads) == 0);
239 }
240 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
241 
242 void qeth_clear_working_pool_list(struct qeth_card *card)
243 {
244         struct qeth_buffer_pool_entry *pool_entry, *tmp;
245 
246         QETH_CARD_TEXT(card, 5, "clwrklst");
247         list_for_each_entry_safe(pool_entry, tmp,
248                             &card->qdio.in_buf_pool.entry_list, list){
249                         list_del(&pool_entry->list);
250         }
251 }
252 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
253 
254 static int qeth_alloc_buffer_pool(struct qeth_card *card)
255 {
256         struct qeth_buffer_pool_entry *pool_entry;
257         void *ptr;
258         int i, j;
259 
260         QETH_CARD_TEXT(card, 5, "alocpool");
261         for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
262                 pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
263                 if (!pool_entry) {
264                         qeth_free_buffer_pool(card);
265                         return -ENOMEM;
266                 }
267                 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
268                         ptr = (void *) __get_free_page(GFP_KERNEL);
269                         if (!ptr) {
270                                 while (j > 0)
271                                         free_page((unsigned long)
272                                                   pool_entry->elements[--j]);
273                                 kfree(pool_entry);
274                                 qeth_free_buffer_pool(card);
275                                 return -ENOMEM;
276                         }
277                         pool_entry->elements[j] = ptr;
278                 }
279                 list_add(&pool_entry->init_list,
280                          &card->qdio.init_pool.entry_list);
281         }
282         return 0;
283 }
284 
285 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
286 {
287         QETH_CARD_TEXT(card, 2, "realcbp");
288 
289         if ((card->state != CARD_STATE_DOWN) &&
290             (card->state != CARD_STATE_RECOVER))
291                 return -EPERM;
292 
293         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
294         qeth_clear_working_pool_list(card);
295         qeth_free_buffer_pool(card);
296         card->qdio.in_buf_pool.buf_count = bufcnt;
297         card->qdio.init_pool.buf_count = bufcnt;
298         return qeth_alloc_buffer_pool(card);
299 }
300 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
301 
302 static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
303 {
304         if (!q)
305                 return;
306 
307         qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
308         kfree(q);
309 }
310 
311 static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
312 {
313         struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
314         int i;
315 
316         if (!q)
317                 return NULL;
318 
319         if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
320                 kfree(q);
321                 return NULL;
322         }
323 
324         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
325                 q->bufs[i].buffer = q->qdio_bufs[i];
326 
327         QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
328         return q;
329 }
330 
331 static inline int qeth_cq_init(struct qeth_card *card)
332 {
333         int rc;
334 
335         if (card->options.cq == QETH_CQ_ENABLED) {
336                 QETH_DBF_TEXT(SETUP, 2, "cqinit");
337                 qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
338                                    QDIO_MAX_BUFFERS_PER_Q);
339                 card->qdio.c_q->next_buf_to_init = 127;
340                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
341                              card->qdio.no_in_queues - 1, 0,
342                              127);
343                 if (rc) {
344                         QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
345                         goto out;
346                 }
347         }
348         rc = 0;
349 out:
350         return rc;
351 }
352 
353 static inline int qeth_alloc_cq(struct qeth_card *card)
354 {
355         int rc;
356 
357         if (card->options.cq == QETH_CQ_ENABLED) {
358                 int i;
359                 struct qdio_outbuf_state *outbuf_states;
360 
361                 QETH_DBF_TEXT(SETUP, 2, "cqon");
362                 card->qdio.c_q = qeth_alloc_qdio_queue();
363                 if (!card->qdio.c_q) {
364                         rc = -1;
365                         goto kmsg_out;
366                 }
367                 card->qdio.no_in_queues = 2;
368                 card->qdio.out_bufstates =
369                         kzalloc(card->qdio.no_out_queues *
370                                 QDIO_MAX_BUFFERS_PER_Q *
371                                 sizeof(struct qdio_outbuf_state), GFP_KERNEL);
372                 outbuf_states = card->qdio.out_bufstates;
373                 if (outbuf_states == NULL) {
374                         rc = -1;
375                         goto free_cq_out;
376                 }
377                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
378                         card->qdio.out_qs[i]->bufstates = outbuf_states;
379                         outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
380                 }
381         } else {
382                 QETH_DBF_TEXT(SETUP, 2, "nocq");
383                 card->qdio.c_q = NULL;
384                 card->qdio.no_in_queues = 1;
385         }
386         QETH_DBF_TEXT_(SETUP, 2, "iqc%d", card->qdio.no_in_queues);
387         rc = 0;
388 out:
389         return rc;
390 free_cq_out:
391         qeth_free_qdio_queue(card->qdio.c_q);
392         card->qdio.c_q = NULL;
393 kmsg_out:
394         dev_err(&card->gdev->dev, "Failed to create completion queue\n");
395         goto out;
396 }
397 
398 static inline void qeth_free_cq(struct qeth_card *card)
399 {
400         if (card->qdio.c_q) {
401                 --card->qdio.no_in_queues;
402                 qeth_free_qdio_queue(card->qdio.c_q);
403                 card->qdio.c_q = NULL;
404         }
405         kfree(card->qdio.out_bufstates);
406         card->qdio.out_bufstates = NULL;
407 }
408 
409 static inline enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
410         int delayed) {
411         enum iucv_tx_notify n;
412 
413         switch (sbalf15) {
414         case 0:
415                 n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
416                 break;
417         case 4:
418         case 16:
419         case 17:
420         case 18:
421                 n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
422                         TX_NOTIFY_UNREACHABLE;
423                 break;
424         default:
425                 n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
426                         TX_NOTIFY_GENERALERROR;
427                 break;
428         }
429 
430         return n;
431 }
432 
433 static inline void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q,
434         int bidx, int forced_cleanup)
435 {
436         if (q->card->options.cq != QETH_CQ_ENABLED)
437                 return;
438 
439         if (q->bufs[bidx]->next_pending != NULL) {
440                 struct qeth_qdio_out_buffer *head = q->bufs[bidx];
441                 struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
442 
443                 while (c) {
444                         if (forced_cleanup ||
445                             atomic_read(&c->state) ==
446                               QETH_QDIO_BUF_HANDLED_DELAYED) {
447                                 struct qeth_qdio_out_buffer *f = c;
448                                 QETH_CARD_TEXT(f->q->card, 5, "fp");
449                                 QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
450                                 /* release here to avoid interleaving between
451                                    outbound tasklet and inbound tasklet
452                                    regarding notifications and lifecycle */
453                                 qeth_release_skbs(c);
454 
455                                 c = f->next_pending;
456                                 WARN_ON_ONCE(head->next_pending != f);
457                                 head->next_pending = c;
458                                 kmem_cache_free(qeth_qdio_outbuf_cache, f);
459                         } else {
460                                 head = c;
461                                 c = c->next_pending;
462                         }
463 
464                 }
465         }
466         if (forced_cleanup && (atomic_read(&(q->bufs[bidx]->state)) ==
467                                         QETH_QDIO_BUF_HANDLED_DELAYED)) {
468                 /* for recovery situations */
469                 q->bufs[bidx]->aob = q->bufstates[bidx].aob;
470                 qeth_init_qdio_out_buf(q, bidx);
471                 QETH_CARD_TEXT(q->card, 2, "clprecov");
472         }
473 }
474 
475 
476 static inline void qeth_qdio_handle_aob(struct qeth_card *card,
477                 unsigned long phys_aob_addr) {
478         struct qaob *aob;
479         struct qeth_qdio_out_buffer *buffer;
480         enum iucv_tx_notify notification;
481 
482         aob = (struct qaob *) phys_to_virt(phys_aob_addr);
483         QETH_CARD_TEXT(card, 5, "haob");
484         QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
485         buffer = (struct qeth_qdio_out_buffer *) aob->user1;
486         QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
487 
488         if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
489                            QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
490                 notification = TX_NOTIFY_OK;
491         } else {
492                 WARN_ON_ONCE(atomic_read(&buffer->state) !=
493                                                         QETH_QDIO_BUF_PENDING);
494                 atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
495                 notification = TX_NOTIFY_DELAYED_OK;
496         }
497 
498         if (aob->aorc != 0)  {
499                 QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
500                 notification = qeth_compute_cq_notification(aob->aorc, 1);
501         }
502         qeth_notify_skbs(buffer->q, buffer, notification);
503 
504         buffer->aob = NULL;
505         qeth_clear_output_buffer(buffer->q, buffer,
506                                  QETH_QDIO_BUF_HANDLED_DELAYED);
507 
508         /* from here on: do not touch buffer anymore */
509         qdio_release_aob(aob);
510 }
511 
512 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
513 {
514         return card->options.cq == QETH_CQ_ENABLED &&
515             card->qdio.c_q != NULL &&
516             queue != 0 &&
517             queue == card->qdio.no_in_queues - 1;
518 }
519 
520 
521 static int qeth_issue_next_read(struct qeth_card *card)
522 {
523         int rc;
524         struct qeth_cmd_buffer *iob;
525 
526         QETH_CARD_TEXT(card, 5, "issnxrd");
527         if (card->read.state != CH_STATE_UP)
528                 return -EIO;
529         iob = qeth_get_buffer(&card->read);
530         if (!iob) {
531                 dev_warn(&card->gdev->dev, "The qeth device driver "
532                         "failed to recover an error on the device\n");
533                 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
534                         "available\n", dev_name(&card->gdev->dev));
535                 return -ENOMEM;
536         }
537         qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
538         QETH_CARD_TEXT(card, 6, "noirqpnd");
539         rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
540                               (addr_t) iob, 0, 0);
541         if (rc) {
542                 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
543                         "rc=%i\n", dev_name(&card->gdev->dev), rc);
544                 atomic_set(&card->read.irq_pending, 0);
545                 card->read_or_write_problem = 1;
546                 qeth_schedule_recovery(card);
547                 wake_up(&card->wait_q);
548         }
549         return rc;
550 }
551 
552 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
553 {
554         struct qeth_reply *reply;
555 
556         reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
557         if (reply) {
558                 atomic_set(&reply->refcnt, 1);
559                 atomic_set(&reply->received, 0);
560                 reply->card = card;
561         }
562         return reply;
563 }
564 
565 static void qeth_get_reply(struct qeth_reply *reply)
566 {
567         WARN_ON(atomic_read(&reply->refcnt) <= 0);
568         atomic_inc(&reply->refcnt);
569 }
570 
571 static void qeth_put_reply(struct qeth_reply *reply)
572 {
573         WARN_ON(atomic_read(&reply->refcnt) <= 0);
574         if (atomic_dec_and_test(&reply->refcnt))
575                 kfree(reply);
576 }
577 
578 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
579                 struct qeth_card *card)
580 {
581         char *ipa_name;
582         int com = cmd->hdr.command;
583         ipa_name = qeth_get_ipa_cmd_name(com);
584         if (rc)
585                 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
586                                 "x%X \"%s\"\n",
587                                 ipa_name, com, dev_name(&card->gdev->dev),
588                                 QETH_CARD_IFNAME(card), rc,
589                                 qeth_get_ipa_msg(rc));
590         else
591                 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
592                                 ipa_name, com, dev_name(&card->gdev->dev),
593                                 QETH_CARD_IFNAME(card));
594 }
595 
596 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
597                 struct qeth_cmd_buffer *iob)
598 {
599         struct qeth_ipa_cmd *cmd = NULL;
600 
601         QETH_CARD_TEXT(card, 5, "chkipad");
602         if (IS_IPA(iob->data)) {
603                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
604                 if (IS_IPA_REPLY(cmd)) {
605                         if (cmd->hdr.command != IPA_CMD_SETCCID &&
606                             cmd->hdr.command != IPA_CMD_DELCCID &&
607                             cmd->hdr.command != IPA_CMD_MODCCID &&
608                             cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
609                                 qeth_issue_ipa_msg(cmd,
610                                                 cmd->hdr.return_code, card);
611                         return cmd;
612                 } else {
613                         switch (cmd->hdr.command) {
614                         case IPA_CMD_STOPLAN:
615                                 if (cmd->hdr.return_code ==
616                                                 IPA_RC_VEPA_TO_VEB_TRANSITION) {
617                                         dev_err(&card->gdev->dev,
618                                            "Interface %s is down because the "
619                                            "adjacent port is no longer in "
620                                            "reflective relay mode\n",
621                                            QETH_CARD_IFNAME(card));
622                                         qeth_close_dev(card);
623                                 } else {
624                                         dev_warn(&card->gdev->dev,
625                                            "The link for interface %s on CHPID"
626                                            " 0x%X failed\n",
627                                            QETH_CARD_IFNAME(card),
628                                            card->info.chpid);
629                                         qeth_issue_ipa_msg(cmd,
630                                                 cmd->hdr.return_code, card);
631                                 }
632                                 card->lan_online = 0;
633                                 if (card->dev && netif_carrier_ok(card->dev))
634                                         netif_carrier_off(card->dev);
635                                 return NULL;
636                         case IPA_CMD_STARTLAN:
637                                 dev_info(&card->gdev->dev,
638                                            "The link for %s on CHPID 0x%X has"
639                                            " been restored\n",
640                                            QETH_CARD_IFNAME(card),
641                                            card->info.chpid);
642                                 netif_carrier_on(card->dev);
643                                 card->lan_online = 1;
644                                 if (card->info.hwtrap)
645                                         card->info.hwtrap = 2;
646                                 qeth_schedule_recovery(card);
647                                 return NULL;
648                         case IPA_CMD_SETBRIDGEPORT:
649                         case IPA_CMD_ADDRESS_CHANGE_NOTIF:
650                                 if (card->discipline->control_event_handler
651                                                                 (card, cmd))
652                                         return cmd;
653                                 else
654                                         return NULL;
655                         case IPA_CMD_MODCCID:
656                                 return cmd;
657                         case IPA_CMD_REGISTER_LOCAL_ADDR:
658                                 QETH_CARD_TEXT(card, 3, "irla");
659                                 break;
660                         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
661                                 QETH_CARD_TEXT(card, 3, "urla");
662                                 break;
663                         default:
664                                 QETH_DBF_MESSAGE(2, "Received data is IPA "
665                                            "but not a reply!\n");
666                                 break;
667                         }
668                 }
669         }
670         return cmd;
671 }
672 
673 void qeth_clear_ipacmd_list(struct qeth_card *card)
674 {
675         struct qeth_reply *reply, *r;
676         unsigned long flags;
677 
678         QETH_CARD_TEXT(card, 4, "clipalst");
679 
680         spin_lock_irqsave(&card->lock, flags);
681         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
682                 qeth_get_reply(reply);
683                 reply->rc = -EIO;
684                 atomic_inc(&reply->received);
685                 list_del_init(&reply->list);
686                 wake_up(&reply->wait_q);
687                 qeth_put_reply(reply);
688         }
689         spin_unlock_irqrestore(&card->lock, flags);
690         atomic_set(&card->write.irq_pending, 0);
691 }
692 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
693 
694 static int qeth_check_idx_response(struct qeth_card *card,
695         unsigned char *buffer)
696 {
697         if (!buffer)
698                 return 0;
699 
700         QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
701         if ((buffer[2] & 0xc0) == 0xc0) {
702                 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
703                            "with cause code 0x%02x%s\n",
704                            buffer[4],
705                            ((buffer[4] == 0x22) ?
706                             " -- try another portname" : ""));
707                 QETH_CARD_TEXT(card, 2, "ckidxres");
708                 QETH_CARD_TEXT(card, 2, " idxterm");
709                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
710                 if (buffer[4] == 0xf6) {
711                         dev_err(&card->gdev->dev,
712                         "The qeth device is not configured "
713                         "for the OSI layer required by z/VM\n");
714                         return -EPERM;
715                 }
716                 return -EIO;
717         }
718         return 0;
719 }
720 
721 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
722                 __u32 len)
723 {
724         struct qeth_card *card;
725 
726         card = CARD_FROM_CDEV(channel->ccwdev);
727         QETH_CARD_TEXT(card, 4, "setupccw");
728         if (channel == &card->read)
729                 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
730         else
731                 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
732         channel->ccw.count = len;
733         channel->ccw.cda = (__u32) __pa(iob);
734 }
735 
736 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
737 {
738         __u8 index;
739 
740         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
741         index = channel->io_buf_no;
742         do {
743                 if (channel->iob[index].state == BUF_STATE_FREE) {
744                         channel->iob[index].state = BUF_STATE_LOCKED;
745                         channel->io_buf_no = (channel->io_buf_no + 1) %
746                                 QETH_CMD_BUFFER_NO;
747                         memset(channel->iob[index].data, 0, QETH_BUFSIZE);
748                         return channel->iob + index;
749                 }
750                 index = (index + 1) % QETH_CMD_BUFFER_NO;
751         } while (index != channel->io_buf_no);
752 
753         return NULL;
754 }
755 
756 void qeth_release_buffer(struct qeth_channel *channel,
757                 struct qeth_cmd_buffer *iob)
758 {
759         unsigned long flags;
760 
761         QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
762         spin_lock_irqsave(&channel->iob_lock, flags);
763         memset(iob->data, 0, QETH_BUFSIZE);
764         iob->state = BUF_STATE_FREE;
765         iob->callback = qeth_send_control_data_cb;
766         iob->rc = 0;
767         spin_unlock_irqrestore(&channel->iob_lock, flags);
768         wake_up(&channel->wait_q);
769 }
770 EXPORT_SYMBOL_GPL(qeth_release_buffer);
771 
772 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
773 {
774         struct qeth_cmd_buffer *buffer = NULL;
775         unsigned long flags;
776 
777         spin_lock_irqsave(&channel->iob_lock, flags);
778         buffer = __qeth_get_buffer(channel);
779         spin_unlock_irqrestore(&channel->iob_lock, flags);
780         return buffer;
781 }
782 
783 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
784 {
785         struct qeth_cmd_buffer *buffer;
786         wait_event(channel->wait_q,
787                    ((buffer = qeth_get_buffer(channel)) != NULL));
788         return buffer;
789 }
790 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
791 
792 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
793 {
794         int cnt;
795 
796         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
797                 qeth_release_buffer(channel, &channel->iob[cnt]);
798         channel->buf_no = 0;
799         channel->io_buf_no = 0;
800 }
801 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
802 
803 static void qeth_send_control_data_cb(struct qeth_channel *channel,
804                   struct qeth_cmd_buffer *iob)
805 {
806         struct qeth_card *card;
807         struct qeth_reply *reply, *r;
808         struct qeth_ipa_cmd *cmd;
809         unsigned long flags;
810         int keep_reply;
811         int rc = 0;
812 
813         card = CARD_FROM_CDEV(channel->ccwdev);
814         QETH_CARD_TEXT(card, 4, "sndctlcb");
815         rc = qeth_check_idx_response(card, iob->data);
816         switch (rc) {
817         case 0:
818                 break;
819         case -EIO:
820                 qeth_clear_ipacmd_list(card);
821                 qeth_schedule_recovery(card);
822                 /* fall through */
823         default:
824                 goto out;
825         }
826 
827         cmd = qeth_check_ipa_data(card, iob);
828         if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
829                 goto out;
830         /*in case of OSN : check if cmd is set */
831         if (card->info.type == QETH_CARD_TYPE_OSN &&
832             cmd &&
833             cmd->hdr.command != IPA_CMD_STARTLAN &&
834             card->osn_info.assist_cb != NULL) {
835                 card->osn_info.assist_cb(card->dev, cmd);
836                 goto out;
837         }
838 
839         spin_lock_irqsave(&card->lock, flags);
840         list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
841                 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
842                     ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
843                         qeth_get_reply(reply);
844                         list_del_init(&reply->list);
845                         spin_unlock_irqrestore(&card->lock, flags);
846                         keep_reply = 0;
847                         if (reply->callback != NULL) {
848                                 if (cmd) {
849                                         reply->offset = (__u16)((char *)cmd -
850                                                         (char *)iob->data);
851                                         keep_reply = reply->callback(card,
852                                                         reply,
853                                                         (unsigned long)cmd);
854                                 } else
855                                         keep_reply = reply->callback(card,
856                                                         reply,
857                                                         (unsigned long)iob);
858                         }
859                         if (cmd)
860                                 reply->rc = (u16) cmd->hdr.return_code;
861                         else if (iob->rc)
862                                 reply->rc = iob->rc;
863                         if (keep_reply) {
864                                 spin_lock_irqsave(&card->lock, flags);
865                                 list_add_tail(&reply->list,
866                                               &card->cmd_waiter_list);
867                                 spin_unlock_irqrestore(&card->lock, flags);
868                         } else {
869                                 atomic_inc(&reply->received);
870                                 wake_up(&reply->wait_q);
871                         }
872                         qeth_put_reply(reply);
873                         goto out;
874                 }
875         }
876         spin_unlock_irqrestore(&card->lock, flags);
877 out:
878         memcpy(&card->seqno.pdu_hdr_ack,
879                 QETH_PDU_HEADER_SEQ_NO(iob->data),
880                 QETH_SEQ_NO_LENGTH);
881         qeth_release_buffer(channel, iob);
882 }
883 
884 static int qeth_setup_channel(struct qeth_channel *channel)
885 {
886         int cnt;
887 
888         QETH_DBF_TEXT(SETUP, 2, "setupch");
889         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
890                 channel->iob[cnt].data =
891                         kzalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
892                 if (channel->iob[cnt].data == NULL)
893                         break;
894                 channel->iob[cnt].state = BUF_STATE_FREE;
895                 channel->iob[cnt].channel = channel;
896                 channel->iob[cnt].callback = qeth_send_control_data_cb;
897                 channel->iob[cnt].rc = 0;
898         }
899         if (cnt < QETH_CMD_BUFFER_NO) {
900                 while (cnt-- > 0)
901                         kfree(channel->iob[cnt].data);
902                 return -ENOMEM;
903         }
904         channel->buf_no = 0;
905         channel->io_buf_no = 0;
906         atomic_set(&channel->irq_pending, 0);
907         spin_lock_init(&channel->iob_lock);
908 
909         init_waitqueue_head(&channel->wait_q);
910         return 0;
911 }
912 
913 static int qeth_set_thread_start_bit(struct qeth_card *card,
914                 unsigned long thread)
915 {
916         unsigned long flags;
917 
918         spin_lock_irqsave(&card->thread_mask_lock, flags);
919         if (!(card->thread_allowed_mask & thread) ||
920               (card->thread_start_mask & thread)) {
921                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
922                 return -EPERM;
923         }
924         card->thread_start_mask |= thread;
925         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
926         return 0;
927 }
928 
929 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
930 {
931         unsigned long flags;
932 
933         spin_lock_irqsave(&card->thread_mask_lock, flags);
934         card->thread_start_mask &= ~thread;
935         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
936         wake_up(&card->wait_q);
937 }
938 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
939 
940 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
941 {
942         unsigned long flags;
943 
944         spin_lock_irqsave(&card->thread_mask_lock, flags);
945         card->thread_running_mask &= ~thread;
946         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
947         wake_up(&card->wait_q);
948 }
949 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
950 
951 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
952 {
953         unsigned long flags;
954         int rc = 0;
955 
956         spin_lock_irqsave(&card->thread_mask_lock, flags);
957         if (card->thread_start_mask & thread) {
958                 if ((card->thread_allowed_mask & thread) &&
959                     !(card->thread_running_mask & thread)) {
960                         rc = 1;
961                         card->thread_start_mask &= ~thread;
962                         card->thread_running_mask |= thread;
963                 } else
964                         rc = -EPERM;
965         }
966         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
967         return rc;
968 }
969 
970 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
971 {
972         int rc = 0;
973 
974         wait_event(card->wait_q,
975                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
976         return rc;
977 }
978 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
979 
980 void qeth_schedule_recovery(struct qeth_card *card)
981 {
982         QETH_CARD_TEXT(card, 2, "startrec");
983         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
984                 schedule_work(&card->kernel_thread_starter);
985 }
986 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
987 
988 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
989 {
990         int dstat, cstat;
991         char *sense;
992         struct qeth_card *card;
993 
994         sense = (char *) irb->ecw;
995         cstat = irb->scsw.cmd.cstat;
996         dstat = irb->scsw.cmd.dstat;
997         card = CARD_FROM_CDEV(cdev);
998 
999         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1000                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1001                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1002                 QETH_CARD_TEXT(card, 2, "CGENCHK");
1003                 dev_warn(&cdev->dev, "The qeth device driver "
1004                         "failed to recover an error on the device\n");
1005                 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
1006                         dev_name(&cdev->dev), dstat, cstat);
1007                 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
1008                                 16, 1, irb, 64, 1);
1009                 return 1;
1010         }
1011 
1012         if (dstat & DEV_STAT_UNIT_CHECK) {
1013                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
1014                     SENSE_RESETTING_EVENT_FLAG) {
1015                         QETH_CARD_TEXT(card, 2, "REVIND");
1016                         return 1;
1017                 }
1018                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
1019                     SENSE_COMMAND_REJECT_FLAG) {
1020                         QETH_CARD_TEXT(card, 2, "CMDREJi");
1021                         return 1;
1022                 }
1023                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
1024                         QETH_CARD_TEXT(card, 2, "AFFE");
1025                         return 1;
1026                 }
1027                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
1028                         QETH_CARD_TEXT(card, 2, "ZEROSEN");
1029                         return 0;
1030                 }
1031                 QETH_CARD_TEXT(card, 2, "DGENCHK");
1032                         return 1;
1033         }
1034         return 0;
1035 }
1036 
1037 static long __qeth_check_irb_error(struct ccw_device *cdev,
1038                 unsigned long intparm, struct irb *irb)
1039 {
1040         struct qeth_card *card;
1041 
1042         card = CARD_FROM_CDEV(cdev);
1043 
1044         if (!card || !IS_ERR(irb))
1045                 return 0;
1046 
1047         switch (PTR_ERR(irb)) {
1048         case -EIO:
1049                 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
1050                         dev_name(&cdev->dev));
1051                 QETH_CARD_TEXT(card, 2, "ckirberr");
1052                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
1053                 break;
1054         case -ETIMEDOUT:
1055                 dev_warn(&cdev->dev, "A hardware operation timed out"
1056                         " on the device\n");
1057                 QETH_CARD_TEXT(card, 2, "ckirberr");
1058                 QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
1059                 if (intparm == QETH_RCD_PARM) {
1060                         if (card->data.ccwdev == cdev) {
1061                                 card->data.state = CH_STATE_DOWN;
1062                                 wake_up(&card->wait_q);
1063                         }
1064                 }
1065                 break;
1066         default:
1067                 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
1068                         dev_name(&cdev->dev), PTR_ERR(irb));
1069                 QETH_CARD_TEXT(card, 2, "ckirberr");
1070                 QETH_CARD_TEXT(card, 2, "  rc???");
1071         }
1072         return PTR_ERR(irb);
1073 }
1074 
1075 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
1076                 struct irb *irb)
1077 {
1078         int rc;
1079         int cstat, dstat;
1080         struct qeth_cmd_buffer *buffer;
1081         struct qeth_channel *channel;
1082         struct qeth_card *card;
1083         struct qeth_cmd_buffer *iob;
1084         __u8 index;
1085 
1086         if (__qeth_check_irb_error(cdev, intparm, irb))
1087                 return;
1088         cstat = irb->scsw.cmd.cstat;
1089         dstat = irb->scsw.cmd.dstat;
1090 
1091         card = CARD_FROM_CDEV(cdev);
1092         if (!card)
1093                 return;
1094 
1095         QETH_CARD_TEXT(card, 5, "irq");
1096 
1097         if (card->read.ccwdev == cdev) {
1098                 channel = &card->read;
1099                 QETH_CARD_TEXT(card, 5, "read");
1100         } else if (card->write.ccwdev == cdev) {
1101                 channel = &card->write;
1102                 QETH_CARD_TEXT(card, 5, "write");
1103         } else {
1104                 channel = &card->data;
1105                 QETH_CARD_TEXT(card, 5, "data");
1106         }
1107         atomic_set(&channel->irq_pending, 0);
1108 
1109         if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1110                 channel->state = CH_STATE_STOPPED;
1111 
1112         if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1113                 channel->state = CH_STATE_HALTED;
1114 
1115         /*let's wake up immediately on data channel*/
1116         if ((channel == &card->data) && (intparm != 0) &&
1117             (intparm != QETH_RCD_PARM))
1118                 goto out;
1119 
1120         if (intparm == QETH_CLEAR_CHANNEL_PARM) {
1121                 QETH_CARD_TEXT(card, 6, "clrchpar");
1122                 /* we don't have to handle this further */
1123                 intparm = 0;
1124         }
1125         if (intparm == QETH_HALT_CHANNEL_PARM) {
1126                 QETH_CARD_TEXT(card, 6, "hltchpar");
1127                 /* we don't have to handle this further */
1128                 intparm = 0;
1129         }
1130         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1131             (dstat & DEV_STAT_UNIT_CHECK) ||
1132             (cstat)) {
1133                 if (irb->esw.esw0.erw.cons) {
1134                         dev_warn(&channel->ccwdev->dev,
1135                                 "The qeth device driver failed to recover "
1136                                 "an error on the device\n");
1137                         QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
1138                                 "0x%X dstat 0x%X\n",
1139                                 dev_name(&channel->ccwdev->dev), cstat, dstat);
1140                         print_hex_dump(KERN_WARNING, "qeth: irb ",
1141                                 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1142                         print_hex_dump(KERN_WARNING, "qeth: sense data ",
1143                                 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1144                 }
1145                 if (intparm == QETH_RCD_PARM) {
1146                         channel->state = CH_STATE_DOWN;
1147                         goto out;
1148                 }
1149                 rc = qeth_get_problem(cdev, irb);
1150                 if (rc) {
1151                         qeth_clear_ipacmd_list(card);
1152                         qeth_schedule_recovery(card);
1153                         goto out;
1154                 }
1155         }
1156 
1157         if (intparm == QETH_RCD_PARM) {
1158                 channel->state = CH_STATE_RCD_DONE;
1159                 goto out;
1160         }
1161         if (intparm) {
1162                 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
1163                 buffer->state = BUF_STATE_PROCESSED;
1164         }
1165         if (channel == &card->data)
1166                 return;
1167         if (channel == &card->read &&
1168             channel->state == CH_STATE_UP)
1169                 qeth_issue_next_read(card);
1170 
1171         iob = channel->iob;
1172         index = channel->buf_no;
1173         while (iob[index].state == BUF_STATE_PROCESSED) {
1174                 if (iob[index].callback != NULL)
1175                         iob[index].callback(channel, iob + index);
1176 
1177                 index = (index + 1) % QETH_CMD_BUFFER_NO;
1178         }
1179         channel->buf_no = index;
1180 out:
1181         wake_up(&card->wait_q);
1182         return;
1183 }
1184 
1185 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1186                 struct qeth_qdio_out_buffer *buf,
1187                 enum iucv_tx_notify notification)
1188 {
1189         struct sk_buff *skb;
1190 
1191         if (skb_queue_empty(&buf->skb_list))
1192                 goto out;
1193         skb = skb_peek(&buf->skb_list);
1194         while (skb) {
1195                 QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1196                 QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1197                 if (skb->protocol == ETH_P_AF_IUCV) {
1198                         if (skb->sk) {
1199                                 struct iucv_sock *iucv = iucv_sk(skb->sk);
1200                                 iucv->sk_txnotify(skb, notification);
1201                         }
1202                 }
1203                 if (skb_queue_is_last(&buf->skb_list, skb))
1204                         skb = NULL;
1205                 else
1206                         skb = skb_queue_next(&buf->skb_list, skb);
1207         }
1208 out:
1209         return;
1210 }
1211 
1212 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf)
1213 {
1214         struct sk_buff *skb;
1215         struct iucv_sock *iucv;
1216         int notify_general_error = 0;
1217 
1218         if (atomic_read(&buf->state) == QETH_QDIO_BUF_PENDING)
1219                 notify_general_error = 1;
1220 
1221         /* release may never happen from within CQ tasklet scope */
1222         WARN_ON_ONCE(atomic_read(&buf->state) == QETH_QDIO_BUF_IN_CQ);
1223 
1224         skb = skb_dequeue(&buf->skb_list);
1225         while (skb) {
1226                 QETH_CARD_TEXT(buf->q->card, 5, "skbr");
1227                 QETH_CARD_TEXT_(buf->q->card, 5, "%lx", (long) skb);
1228                 if (notify_general_error && skb->protocol == ETH_P_AF_IUCV) {
1229                         if (skb->sk) {
1230                                 iucv = iucv_sk(skb->sk);
1231                                 iucv->sk_txnotify(skb, TX_NOTIFY_GENERALERROR);
1232                         }
1233                 }
1234                 atomic_dec(&skb->users);
1235                 dev_kfree_skb_any(skb);
1236                 skb = skb_dequeue(&buf->skb_list);
1237         }
1238 }
1239 
1240 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1241                 struct qeth_qdio_out_buffer *buf,
1242                 enum qeth_qdio_buffer_states newbufstate)
1243 {
1244         int i;
1245 
1246         /* is PCI flag set on buffer? */
1247         if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1248                 atomic_dec(&queue->set_pci_flags_count);
1249 
1250         if (newbufstate == QETH_QDIO_BUF_EMPTY) {
1251                 qeth_release_skbs(buf);
1252         }
1253         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
1254                 if (buf->buffer->element[i].addr && buf->is_header[i])
1255                         kmem_cache_free(qeth_core_header_cache,
1256                                 buf->buffer->element[i].addr);
1257                 buf->is_header[i] = 0;
1258                 buf->buffer->element[i].length = 0;
1259                 buf->buffer->element[i].addr = NULL;
1260                 buf->buffer->element[i].eflags = 0;
1261                 buf->buffer->element[i].sflags = 0;
1262         }
1263         buf->buffer->element[15].eflags = 0;
1264         buf->buffer->element[15].sflags = 0;
1265         buf->next_element_to_fill = 0;
1266         atomic_set(&buf->state, newbufstate);
1267 }
1268 
1269 static void qeth_clear_outq_buffers(struct qeth_qdio_out_q *q, int free)
1270 {
1271         int j;
1272 
1273         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1274                 if (!q->bufs[j])
1275                         continue;
1276                 qeth_cleanup_handled_pending(q, j, 1);
1277                 qeth_clear_output_buffer(q, q->bufs[j], QETH_QDIO_BUF_EMPTY);
1278                 if (free) {
1279                         kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1280                         q->bufs[j] = NULL;
1281                 }
1282         }
1283 }
1284 
1285 void qeth_clear_qdio_buffers(struct qeth_card *card)
1286 {
1287         int i;
1288 
1289         QETH_CARD_TEXT(card, 2, "clearqdbf");
1290         /* clear outbound buffers to free skbs */
1291         for (i = 0; i < card->qdio.no_out_queues; ++i) {
1292                 if (card->qdio.out_qs[i]) {
1293                         qeth_clear_outq_buffers(card->qdio.out_qs[i], 0);
1294                 }
1295         }
1296 }
1297 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
1298 
1299 static void qeth_free_buffer_pool(struct qeth_card *card)
1300 {
1301         struct qeth_buffer_pool_entry *pool_entry, *tmp;
1302         int i = 0;
1303         list_for_each_entry_safe(pool_entry, tmp,
1304                                  &card->qdio.init_pool.entry_list, init_list){
1305                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1306                         free_page((unsigned long)pool_entry->elements[i]);
1307                 list_del(&pool_entry->init_list);
1308                 kfree(pool_entry);
1309         }
1310 }
1311 
1312 static void qeth_clean_channel(struct qeth_channel *channel)
1313 {
1314         int cnt;
1315 
1316         QETH_DBF_TEXT(SETUP, 2, "freech");
1317         for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1318                 kfree(channel->iob[cnt].data);
1319 }
1320 
1321 static void qeth_set_single_write_queues(struct qeth_card *card)
1322 {
1323         if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1324             (card->qdio.no_out_queues == 4))
1325                 qeth_free_qdio_buffers(card);
1326 
1327         card->qdio.no_out_queues = 1;
1328         if (card->qdio.default_out_queue != 0)
1329                 dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1330 
1331         card->qdio.default_out_queue = 0;
1332 }
1333 
1334 static void qeth_set_multiple_write_queues(struct qeth_card *card)
1335 {
1336         if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1337             (card->qdio.no_out_queues == 1)) {
1338                 qeth_free_qdio_buffers(card);
1339                 card->qdio.default_out_queue = 2;
1340         }
1341         card->qdio.no_out_queues = 4;
1342 }
1343 
1344 static void qeth_update_from_chp_desc(struct qeth_card *card)
1345 {
1346         struct ccw_device *ccwdev;
1347         struct channel_path_desc *chp_dsc;
1348 
1349         QETH_DBF_TEXT(SETUP, 2, "chp_desc");
1350 
1351         ccwdev = card->data.ccwdev;
1352         chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1353         if (!chp_dsc)
1354                 goto out;
1355 
1356         card->info.func_level = 0x4100 + chp_dsc->desc;
1357         if (card->info.type == QETH_CARD_TYPE_IQD)
1358                 goto out;
1359 
1360         /* CHPP field bit 6 == 1 -> single queue */
1361         if ((chp_dsc->chpp & 0x02) == 0x02)
1362                 qeth_set_single_write_queues(card);
1363         else
1364                 qeth_set_multiple_write_queues(card);
1365 out:
1366         kfree(chp_dsc);
1367         QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1368         QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1369 }
1370 
1371 static void qeth_init_qdio_info(struct qeth_card *card)
1372 {
1373         QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1374         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1375         /* inbound */
1376         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1377         if (card->info.type == QETH_CARD_TYPE_IQD)
1378                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1379         else
1380                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1381         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1382         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1383         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1384 }
1385 
1386 static void qeth_set_intial_options(struct qeth_card *card)
1387 {
1388         card->options.route4.type = NO_ROUTER;
1389         card->options.route6.type = NO_ROUTER;
1390         card->options.fake_broadcast = 0;
1391         card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1392         card->options.performance_stats = 0;
1393         card->options.rx_sg_cb = QETH_RX_SG_CB;
1394         card->options.isolation = ISOLATION_MODE_NONE;
1395         card->options.cq = QETH_CQ_DISABLED;
1396 }
1397 
1398 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1399 {
1400         unsigned long flags;
1401         int rc = 0;
1402 
1403         spin_lock_irqsave(&card->thread_mask_lock, flags);
1404         QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1405                         (u8) card->thread_start_mask,
1406                         (u8) card->thread_allowed_mask,
1407                         (u8) card->thread_running_mask);
1408         rc = (card->thread_start_mask & thread);
1409         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1410         return rc;
1411 }
1412 
1413 static void qeth_start_kernel_thread(struct work_struct *work)
1414 {
1415         struct task_struct *ts;
1416         struct qeth_card *card = container_of(work, struct qeth_card,
1417                                         kernel_thread_starter);
1418         QETH_CARD_TEXT(card , 2, "strthrd");
1419 
1420         if (card->read.state != CH_STATE_UP &&
1421             card->write.state != CH_STATE_UP)
1422                 return;
1423         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1424                 ts = kthread_run(card->discipline->recover, (void *)card,
1425                                 "qeth_recover");
1426                 if (IS_ERR(ts)) {
1427                         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1428                         qeth_clear_thread_running_bit(card,
1429                                 QETH_RECOVER_THREAD);
1430                 }
1431         }
1432 }
1433 
1434 static int qeth_setup_card(struct qeth_card *card)
1435 {
1436 
1437         QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1438         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1439 
1440         card->read.state  = CH_STATE_DOWN;
1441         card->write.state = CH_STATE_DOWN;
1442         card->data.state  = CH_STATE_DOWN;
1443         card->state = CARD_STATE_DOWN;
1444         card->lan_online = 0;
1445         card->read_or_write_problem = 0;
1446         card->dev = NULL;
1447         spin_lock_init(&card->vlanlock);
1448         spin_lock_init(&card->mclock);
1449         spin_lock_init(&card->lock);
1450         spin_lock_init(&card->ip_lock);
1451         spin_lock_init(&card->thread_mask_lock);
1452         mutex_init(&card->conf_mutex);
1453         mutex_init(&card->discipline_mutex);
1454         card->thread_start_mask = 0;
1455         card->thread_allowed_mask = 0;
1456         card->thread_running_mask = 0;
1457         INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1458         INIT_LIST_HEAD(&card->ip_list);
1459         INIT_LIST_HEAD(card->ip_tbd_list);
1460         INIT_LIST_HEAD(&card->cmd_waiter_list);
1461         init_waitqueue_head(&card->wait_q);
1462         /* initial options */
1463         qeth_set_intial_options(card);
1464         /* IP address takeover */
1465         INIT_LIST_HEAD(&card->ipato.entries);
1466         card->ipato.enabled = 0;
1467         card->ipato.invert4 = 0;
1468         card->ipato.invert6 = 0;
1469         /* init QDIO stuff */
1470         qeth_init_qdio_info(card);
1471         INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1472         INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1473         return 0;
1474 }
1475 
1476 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1477 {
1478         struct qeth_card *card = container_of(slr, struct qeth_card,
1479                                         qeth_service_level);
1480         if (card->info.mcl_level[0])
1481                 seq_printf(m, "qeth: %s firmware level %s\n",
1482                         CARD_BUS_ID(card), card->info.mcl_level);
1483 }
1484 
1485 static struct qeth_card *qeth_alloc_card(void)
1486 {
1487         struct qeth_card *card;
1488 
1489         QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1490         card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1491         if (!card)
1492                 goto out;
1493         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1494         card->ip_tbd_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
1495         if (!card->ip_tbd_list) {
1496                 QETH_DBF_TEXT(SETUP, 0, "iptbdnom");
1497                 goto out_card;
1498         }
1499         if (qeth_setup_channel(&card->read))
1500                 goto out_ip;
1501         if (qeth_setup_channel(&card->write))
1502                 goto out_channel;
1503         card->options.layer2 = -1;
1504         card->qeth_service_level.seq_print = qeth_core_sl_print;
1505         register_service_level(&card->qeth_service_level);
1506         return card;
1507 
1508 out_channel:
1509         qeth_clean_channel(&card->read);
1510 out_ip:
1511         kfree(card->ip_tbd_list);
1512 out_card:
1513         kfree(card);
1514 out:
1515         return NULL;
1516 }
1517 
1518 static int qeth_determine_card_type(struct qeth_card *card)
1519 {
1520         int i = 0;
1521 
1522         QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1523 
1524         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1525         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1526         while (known_devices[i][QETH_DEV_MODEL_IND]) {
1527                 if ((CARD_RDEV(card)->id.dev_type ==
1528                                 known_devices[i][QETH_DEV_TYPE_IND]) &&
1529                     (CARD_RDEV(card)->id.dev_model ==
1530                                 known_devices[i][QETH_DEV_MODEL_IND])) {
1531                         card->info.type = known_devices[i][QETH_DEV_MODEL_IND];
1532                         card->qdio.no_out_queues =
1533                                 known_devices[i][QETH_QUEUE_NO_IND];
1534                         card->qdio.no_in_queues = 1;
1535                         card->info.is_multicast_different =
1536                                 known_devices[i][QETH_MULTICAST_IND];
1537                         qeth_update_from_chp_desc(card);
1538                         return 0;
1539                 }
1540                 i++;
1541         }
1542         card->info.type = QETH_CARD_TYPE_UNKNOWN;
1543         dev_err(&card->gdev->dev, "The adapter hardware is of an "
1544                 "unknown type\n");
1545         return -ENOENT;
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 void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1728 {
1729         QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1730 
1731         if (prcd[74] == 0xF0 && prcd[75] == 0xF0 &&
1732             prcd[76] >= 0xF1 && prcd[76] <= 0xF4) {
1733                 card->info.blkt.time_total = 0;
1734                 card->info.blkt.inter_packet = 0;
1735                 card->info.blkt.inter_packet_jumbo = 0;
1736         } else {
1737                 card->info.blkt.time_total = 250;
1738                 card->info.blkt.inter_packet = 5;
1739                 card->info.blkt.inter_packet_jumbo = 15;
1740         }
1741 }
1742 
1743 static void qeth_init_tokens(struct qeth_card *card)
1744 {
1745         card->token.issuer_rm_w = 0x00010103UL;
1746         card->token.cm_filter_w = 0x00010108UL;
1747         card->token.cm_connection_w = 0x0001010aUL;
1748         card->token.ulp_filter_w = 0x0001010bUL;
1749         card->token.ulp_connection_w = 0x0001010dUL;
1750 }
1751 
1752 static void qeth_init_func_level(struct qeth_card *card)
1753 {
1754         switch (card->info.type) {
1755         case QETH_CARD_TYPE_IQD:
1756                 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1757                 break;
1758         case QETH_CARD_TYPE_OSD:
1759         case QETH_CARD_TYPE_OSN:
1760                 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1761                 break;
1762         default:
1763                 break;
1764         }
1765 }
1766 
1767 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1768                 void (*idx_reply_cb)(struct qeth_channel *,
1769                         struct qeth_cmd_buffer *))
1770 {
1771         struct qeth_cmd_buffer *iob;
1772         unsigned long flags;
1773         int rc;
1774         struct qeth_card *card;
1775 
1776         QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1777         card = CARD_FROM_CDEV(channel->ccwdev);
1778         iob = qeth_get_buffer(channel);
1779         iob->callback = idx_reply_cb;
1780         memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1781         channel->ccw.count = QETH_BUFSIZE;
1782         channel->ccw.cda = (__u32) __pa(iob->data);
1783 
1784         wait_event(card->wait_q,
1785                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1786         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1787         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1788         rc = ccw_device_start(channel->ccwdev,
1789                               &channel->ccw, (addr_t) iob, 0, 0);
1790         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1791 
1792         if (rc) {
1793                 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1794                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1795                 atomic_set(&channel->irq_pending, 0);
1796                 wake_up(&card->wait_q);
1797                 return rc;
1798         }
1799         rc = wait_event_interruptible_timeout(card->wait_q,
1800                          channel->state == CH_STATE_UP, QETH_TIMEOUT);
1801         if (rc == -ERESTARTSYS)
1802                 return rc;
1803         if (channel->state != CH_STATE_UP) {
1804                 rc = -ETIME;
1805                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1806                 qeth_clear_cmd_buffers(channel);
1807         } else
1808                 rc = 0;
1809         return rc;
1810 }
1811 
1812 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1813                 void (*idx_reply_cb)(struct qeth_channel *,
1814                         struct qeth_cmd_buffer *))
1815 {
1816         struct qeth_card *card;
1817         struct qeth_cmd_buffer *iob;
1818         unsigned long flags;
1819         __u16 temp;
1820         __u8 tmp;
1821         int rc;
1822         struct ccw_dev_id temp_devid;
1823 
1824         card = CARD_FROM_CDEV(channel->ccwdev);
1825 
1826         QETH_DBF_TEXT(SETUP, 2, "idxactch");
1827 
1828         iob = qeth_get_buffer(channel);
1829         iob->callback = idx_reply_cb;
1830         memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1831         channel->ccw.count = IDX_ACTIVATE_SIZE;
1832         channel->ccw.cda = (__u32) __pa(iob->data);
1833         if (channel == &card->write) {
1834                 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1835                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1836                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1837                 card->seqno.trans_hdr++;
1838         } else {
1839                 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1840                 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1841                        &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1842         }
1843         tmp = ((__u8)card->info.portno) | 0x80;
1844         memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1845         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1846                &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1847         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1848                &card->info.func_level, sizeof(__u16));
1849         ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1850         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1851         temp = (card->info.cula << 8) + card->info.unit_addr2;
1852         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1853 
1854         wait_event(card->wait_q,
1855                    atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1856         QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1857         spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1858         rc = ccw_device_start(channel->ccwdev,
1859                               &channel->ccw, (addr_t) iob, 0, 0);
1860         spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1861 
1862         if (rc) {
1863                 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1864                         rc);
1865                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1866                 atomic_set(&channel->irq_pending, 0);
1867                 wake_up(&card->wait_q);
1868                 return rc;
1869         }
1870         rc = wait_event_interruptible_timeout(card->wait_q,
1871                         channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1872         if (rc == -ERESTARTSYS)
1873                 return rc;
1874         if (channel->state != CH_STATE_ACTIVATING) {
1875                 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1876                         " failed to recover an error on the device\n");
1877                 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1878                         dev_name(&channel->ccwdev->dev));
1879                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1880                 qeth_clear_cmd_buffers(channel);
1881                 return -ETIME;
1882         }
1883         return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1884 }
1885 
1886 static int qeth_peer_func_level(int level)
1887 {
1888         if ((level & 0xff) == 8)
1889                 return (level & 0xff) + 0x400;
1890         if (((level >> 8) & 3) == 1)
1891                 return (level & 0xff) + 0x200;
1892         return level;
1893 }
1894 
1895 static void qeth_idx_write_cb(struct qeth_channel *channel,
1896                 struct qeth_cmd_buffer *iob)
1897 {
1898         struct qeth_card *card;
1899         __u16 temp;
1900 
1901         QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1902 
1903         if (channel->state == CH_STATE_DOWN) {
1904                 channel->state = CH_STATE_ACTIVATING;
1905                 goto out;
1906         }
1907         card = CARD_FROM_CDEV(channel->ccwdev);
1908 
1909         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1910                 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
1911                         dev_err(&card->write.ccwdev->dev,
1912                                 "The adapter is used exclusively by another "
1913                                 "host\n");
1914                 else
1915                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1916                                 " negative reply\n",
1917                                 dev_name(&card->write.ccwdev->dev));
1918                 goto out;
1919         }
1920         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1921         if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1922                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
1923                         "function level mismatch (sent: 0x%x, received: "
1924                         "0x%x)\n", dev_name(&card->write.ccwdev->dev),
1925                         card->info.func_level, temp);
1926                 goto out;
1927         }
1928         channel->state = CH_STATE_UP;
1929 out:
1930         qeth_release_buffer(channel, iob);
1931 }
1932 
1933 static void qeth_idx_read_cb(struct qeth_channel *channel,
1934                 struct qeth_cmd_buffer *iob)
1935 {
1936         struct qeth_card *card;
1937         __u16 temp;
1938 
1939         QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
1940         if (channel->state == CH_STATE_DOWN) {
1941                 channel->state = CH_STATE_ACTIVATING;
1942                 goto out;
1943         }
1944 
1945         card = CARD_FROM_CDEV(channel->ccwdev);
1946         if (qeth_check_idx_response(card, iob->data))
1947                         goto out;
1948 
1949         if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1950                 switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
1951                 case QETH_IDX_ACT_ERR_EXCL:
1952                         dev_err(&card->write.ccwdev->dev,
1953                                 "The adapter is used exclusively by another "
1954                                 "host\n");
1955                         break;
1956                 case QETH_IDX_ACT_ERR_AUTH:
1957                 case QETH_IDX_ACT_ERR_AUTH_USER:
1958                         dev_err(&card->read.ccwdev->dev,
1959                                 "Setting the device online failed because of "
1960                                 "insufficient authorization\n");
1961                         break;
1962                 default:
1963                         QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
1964                                 " negative reply\n",
1965                                 dev_name(&card->read.ccwdev->dev));
1966                 }
1967                 QETH_CARD_TEXT_(card, 2, "idxread%c",
1968                         QETH_IDX_ACT_CAUSE_CODE(iob->data));
1969                 goto out;
1970         }
1971 
1972 /**
1973  *  * temporary fix for microcode bug
1974  *   * to revert it,replace OR by AND
1975  *    */
1976         if ((!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1977              (card->info.type == QETH_CARD_TYPE_OSD))
1978                 card->info.portname_required = 1;
1979 
1980         memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1981         if (temp != qeth_peer_func_level(card->info.func_level)) {
1982                 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
1983                         "level mismatch (sent: 0x%x, received: 0x%x)\n",
1984                         dev_name(&card->read.ccwdev->dev),
1985                         card->info.func_level, temp);
1986                 goto out;
1987         }
1988         memcpy(&card->token.issuer_rm_r,
1989                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1990                QETH_MPC_TOKEN_LENGTH);
1991         memcpy(&card->info.mcl_level[0],
1992                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1993         channel->state = CH_STATE_UP;
1994 out:
1995         qeth_release_buffer(channel, iob);
1996 }
1997 
1998 void qeth_prepare_control_data(struct qeth_card *card, int len,
1999                 struct qeth_cmd_buffer *iob)
2000 {
2001         qeth_setup_ccw(&card->write, iob->data, len);
2002         iob->callback = qeth_release_buffer;
2003 
2004         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
2005                &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
2006         card->seqno.trans_hdr++;
2007         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
2008                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
2009         card->seqno.pdu_hdr++;
2010         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
2011                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
2012         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
2013 }
2014 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
2015 
2016 int qeth_send_control_data(struct qeth_card *card, int len,
2017                 struct qeth_cmd_buffer *iob,
2018                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
2019                         unsigned long),
2020                 void *reply_param)
2021 {
2022         int rc;
2023         unsigned long flags;
2024         struct qeth_reply *reply = NULL;
2025         unsigned long timeout, event_timeout;
2026         struct qeth_ipa_cmd *cmd;
2027 
2028         QETH_CARD_TEXT(card, 2, "sendctl");
2029 
2030         if (card->read_or_write_problem) {
2031                 qeth_release_buffer(iob->channel, iob);
2032                 return -EIO;
2033         }
2034         reply = qeth_alloc_reply(card);
2035         if (!reply) {
2036                 return -ENOMEM;
2037         }
2038         reply->callback = reply_cb;
2039         reply->param = reply_param;
2040         if (card->state == CARD_STATE_DOWN)
2041                 reply->seqno = QETH_IDX_COMMAND_SEQNO;
2042         else
2043                 reply->seqno = card->seqno.ipa++;
2044         init_waitqueue_head(&reply->wait_q);
2045         spin_lock_irqsave(&card->lock, flags);
2046         list_add_tail(&reply->list, &card->cmd_waiter_list);
2047         spin_unlock_irqrestore(&card->lock, flags);
2048         QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
2049 
2050         while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
2051         qeth_prepare_control_data(card, len, iob);
2052 
2053         if (IS_IPA(iob->data))
2054                 event_timeout = QETH_IPA_TIMEOUT;
2055         else
2056                 event_timeout = QETH_TIMEOUT;
2057         timeout = jiffies + event_timeout;
2058 
2059         QETH_CARD_TEXT(card, 6, "noirqpnd");
2060         spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
2061         rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
2062                               (addr_t) iob, 0, 0);
2063         spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
2064         if (rc) {
2065                 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
2066                         "ccw_device_start rc = %i\n",
2067                         dev_name(&card->write.ccwdev->dev), rc);
2068                 QETH_CARD_TEXT_(card, 2, " err%d", rc);
2069                 spin_lock_irqsave(&card->lock, flags);
2070                 list_del_init(&reply->list);
2071                 qeth_put_reply(reply);
2072                 spin_unlock_irqrestore(&card->lock, flags);
2073                 qeth_release_buffer(iob->channel, iob);
2074                 atomic_set(&card->write.irq_pending, 0);
2075                 wake_up(&card->wait_q);
2076                 return rc;
2077         }
2078 
2079         /* we have only one long running ipassist, since we can ensure
2080            process context of this command we can sleep */
2081         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2082         if ((cmd->hdr.command == IPA_CMD_SETIP) &&
2083             (cmd->hdr.prot_version == QETH_PROT_IPV4)) {
2084                 if (!wait_event_timeout(reply->wait_q,
2085                     atomic_read(&reply->received), event_timeout))
2086                         goto time_err;
2087         } else {
2088                 while (!atomic_read(&reply->received)) {
2089                         if (time_after(jiffies, timeout))
2090                                 goto time_err;
2091                         cpu_relax();
2092                 }
2093         }
2094 
2095         if (reply->rc == -EIO)
2096                 goto error;
2097         rc = reply->rc;
2098         qeth_put_reply(reply);
2099         return rc;
2100 
2101 time_err:
2102         reply->rc = -ETIME;
2103         spin_lock_irqsave(&reply->card->lock, flags);
2104         list_del_init(&reply->list);
2105         spin_unlock_irqrestore(&reply->card->lock, flags);
2106         atomic_inc(&reply->received);
2107 error:
2108         atomic_set(&card->write.irq_pending, 0);
2109         qeth_release_buffer(iob->channel, iob);
2110         card->write.buf_no = (card->write.buf_no + 1) % QETH_CMD_BUFFER_NO;
2111         rc = reply->rc;
2112         qeth_put_reply(reply);
2113         return rc;
2114 }
2115 EXPORT_SYMBOL_GPL(qeth_send_control_data);
2116 
2117 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2118                 unsigned long data)
2119 {
2120         struct qeth_cmd_buffer *iob;
2121 
2122         QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
2123 
2124         iob = (struct qeth_cmd_buffer *) data;
2125         memcpy(&card->token.cm_filter_r,
2126                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2127                QETH_MPC_TOKEN_LENGTH);
2128         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2129         return 0;
2130 }
2131 
2132 static int qeth_cm_enable(struct qeth_card *card)
2133 {
2134         int rc;
2135         struct qeth_cmd_buffer *iob;
2136 
2137         QETH_DBF_TEXT(SETUP, 2, "cmenable");
2138 
2139         iob = qeth_wait_for_buffer(&card->write);
2140         memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2141         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2142                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2143         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2144                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2145 
2146         rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2147                                     qeth_cm_enable_cb, NULL);
2148         return rc;
2149 }
2150 
2151 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2152                 unsigned long data)
2153 {
2154 
2155         struct qeth_cmd_buffer *iob;
2156 
2157         QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
2158 
2159         iob = (struct qeth_cmd_buffer *) data;
2160         memcpy(&card->token.cm_connection_r,
2161                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2162                QETH_MPC_TOKEN_LENGTH);
2163         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2164         return 0;
2165 }
2166 
2167 static int qeth_cm_setup(struct qeth_card *card)
2168 {
2169         int rc;
2170         struct qeth_cmd_buffer *iob;
2171 
2172         QETH_DBF_TEXT(SETUP, 2, "cmsetup");
2173 
2174         iob = qeth_wait_for_buffer(&card->write);
2175         memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2176         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2177                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2178         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2179                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2180         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2181                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2182         rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2183                                     qeth_cm_setup_cb, NULL);
2184         return rc;
2185 
2186 }
2187 
2188 static inline int qeth_get_initial_mtu_for_card(struct qeth_card *card)
2189 {
2190         switch (card->info.type) {
2191         case QETH_CARD_TYPE_UNKNOWN:
2192                 return 1500;
2193         case QETH_CARD_TYPE_IQD:
2194                 return card->info.max_mtu;
2195         case QETH_CARD_TYPE_OSD:
2196                 switch (card->info.link_type) {
2197                 case QETH_LINK_TYPE_HSTR:
2198                 case QETH_LINK_TYPE_LANE_TR:
2199                         return 2000;
2200                 default:
2201                         return card->options.layer2 ? 1500 : 1492;
2202                 }
2203         case QETH_CARD_TYPE_OSM:
2204         case QETH_CARD_TYPE_OSX:
2205                 return card->options.layer2 ? 1500 : 1492;
2206         default:
2207                 return 1500;
2208         }
2209 }
2210 
2211 static inline int qeth_get_mtu_outof_framesize(int framesize)
2212 {
2213         switch (framesize) {
2214         case 0x4000:
2215                 return 8192;
2216         case 0x6000:
2217                 return 16384;
2218         case 0xa000:
2219                 return 32768;
2220         case 0xffff:
2221                 return 57344;
2222         default:
2223                 return 0;
2224         }
2225 }
2226 
2227 static inline int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
2228 {
2229         switch (card->info.type) {
2230         case QETH_CARD_TYPE_OSD:
2231         case QETH_CARD_TYPE_OSM:
2232         case QETH_CARD_TYPE_OSX:
2233         case QETH_CARD_TYPE_IQD:
2234                 return ((mtu >= 576) &&
2235                         (mtu <= card->info.max_mtu));
2236         case QETH_CARD_TYPE_OSN:
2237         case QETH_CARD_TYPE_UNKNOWN:
2238         default:
2239                 return 1;
2240         }
2241 }
2242 
2243 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2244                 unsigned long data)
2245 {
2246 
2247         __u16 mtu, framesize;
2248         __u16 len;
2249         __u8 link_type;
2250         struct qeth_cmd_buffer *iob;
2251 
2252         QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
2253 
2254         iob = (struct qeth_cmd_buffer *) data;
2255         memcpy(&card->token.ulp_filter_r,
2256                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2257                QETH_MPC_TOKEN_LENGTH);
2258         if (card->info.type == QETH_CARD_TYPE_IQD) {
2259                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2260                 mtu = qeth_get_mtu_outof_framesize(framesize);
2261                 if (!mtu) {
2262                         iob->rc = -EINVAL;
2263                         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2264                         return 0;
2265                 }
2266                 if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
2267                         /* frame size has changed */
2268                         if (card->dev &&
2269                             ((card->dev->mtu == card->info.initial_mtu) ||
2270                              (card->dev->mtu > mtu)))
2271                                 card->dev->mtu = mtu;
2272                         qeth_free_qdio_buffers(card);
2273                 }
2274                 card->info.initial_mtu = mtu;
2275                 card->info.max_mtu = mtu;
2276                 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2277         } else {
2278                 card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
2279                         iob->data);
2280                 card->info.initial_mtu = min(card->info.max_mtu,
2281                                         qeth_get_initial_mtu_for_card(card));
2282                 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2283         }
2284 
2285         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2286         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2287                 memcpy(&link_type,
2288                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2289                 card->info.link_type = link_type;
2290         } else
2291                 card->info.link_type = 0;
2292         QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
2293         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2294         return 0;
2295 }
2296 
2297 static int qeth_ulp_enable(struct qeth_card *card)
2298 {
2299         int rc;
2300         char prot_type;
2301         struct qeth_cmd_buffer *iob;
2302 
2303         /*FIXME: trace view callbacks*/
2304         QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
2305 
2306         iob = qeth_wait_for_buffer(&card->write);
2307         memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2308 
2309         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2310                 (__u8) card->info.portno;
2311         if (card->options.layer2)
2312                 if (card->info.type == QETH_CARD_TYPE_OSN)
2313                         prot_type = QETH_PROT_OSN2;
2314                 else
2315                         prot_type = QETH_PROT_LAYER2;
2316         else
2317                 prot_type = QETH_PROT_TCPIP;
2318 
2319         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2320         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2321                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2322         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2323                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2324         memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
2325                card->info.portname, 9);
2326         rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2327                                     qeth_ulp_enable_cb, NULL);
2328         return rc;
2329 
2330 }
2331 
2332 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2333                 unsigned long data)
2334 {
2335         struct qeth_cmd_buffer *iob;
2336 
2337         QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
2338 
2339         iob = (struct qeth_cmd_buffer *) data;
2340         memcpy(&card->token.ulp_connection_r,
2341                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2342                QETH_MPC_TOKEN_LENGTH);
2343         if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2344                      3)) {
2345                 QETH_DBF_TEXT(SETUP, 2, "olmlimit");
2346                 dev_err(&card->gdev->dev, "A connection could not be "
2347                         "established because of an OLM limit\n");
2348                 iob->rc = -EMLINK;
2349         }
2350         QETH_DBF_TEXT_(SETUP, 2, "  rc%d", iob->rc);
2351         return 0;
2352 }
2353 
2354 static int qeth_ulp_setup(struct qeth_card *card)
2355 {
2356         int rc;
2357         __u16 temp;
2358         struct qeth_cmd_buffer *iob;
2359         struct ccw_dev_id dev_id;
2360 
2361         QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2362 
2363         iob = qeth_wait_for_buffer(&card->write);
2364         memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2365 
2366         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2367                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2368         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2369                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2370         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2371                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2372 
2373         ccw_device_get_id(CARD_DDEV(card), &dev_id);
2374         memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2375         temp = (card->info.cula << 8) + card->info.unit_addr2;
2376         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2377         rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2378                                     qeth_ulp_setup_cb, NULL);
2379         return rc;
2380 }
2381 
2382 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2383 {
2384         int rc;
2385         struct qeth_qdio_out_buffer *newbuf;
2386 
2387         rc = 0;
2388         newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2389         if (!newbuf) {
2390                 rc = -ENOMEM;
2391                 goto out;
2392         }
2393         newbuf->buffer = q->qdio_bufs[bidx];
2394         skb_queue_head_init(&newbuf->skb_list);
2395         lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2396         newbuf->q = q;
2397         newbuf->aob = NULL;
2398         newbuf->next_pending = q->bufs[bidx];
2399         atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2400         q->bufs[bidx] = newbuf;
2401         if (q->bufstates) {
2402                 q->bufstates[bidx].user = newbuf;
2403                 QETH_CARD_TEXT_(q->card, 2, "nbs%d", bidx);
2404                 QETH_CARD_TEXT_(q->card, 2, "%lx", (long) newbuf);
2405                 QETH_CARD_TEXT_(q->card, 2, "%lx",
2406                                 (long) newbuf->next_pending);
2407         }
2408 out:
2409         return rc;
2410 }
2411 
2412 static void qeth_free_qdio_out_buf(struct qeth_qdio_out_q *q)
2413 {
2414         if (!q)
2415                 return;
2416 
2417         qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2418         kfree(q);
2419 }
2420 
2421 static struct qeth_qdio_out_q *qeth_alloc_qdio_out_buf(void)
2422 {
2423         struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2424 
2425         if (!q)
2426                 return NULL;
2427 
2428         if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
2429                 kfree(q);
2430                 return NULL;
2431         }
2432         return q;
2433 }
2434 
2435 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2436 {
2437         int i, j;
2438 
2439         QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2440 
2441         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2442                 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2443                 return 0;
2444 
2445         QETH_DBF_TEXT(SETUP, 2, "inq");
2446         card->qdio.in_q = qeth_alloc_qdio_queue();
2447         if (!card->qdio.in_q)
2448                 goto out_nomem;
2449 
2450         /* inbound buffer pool */
2451         if (qeth_alloc_buffer_pool(card))
2452                 goto out_freeinq;
2453 
2454         /* outbound */
2455         card->qdio.out_qs =
2456                 kzalloc(card->qdio.no_out_queues *
2457                         sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2458         if (!card->qdio.out_qs)
2459                 goto out_freepool;
2460         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2461                 card->qdio.out_qs[i] = qeth_alloc_qdio_out_buf();
2462                 if (!card->qdio.out_qs[i])
2463                         goto out_freeoutq;
2464                 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2465                 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2466                 card->qdio.out_qs[i]->queue_no = i;
2467                 /* give outbound qeth_qdio_buffers their qdio_buffers */
2468                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2469                         WARN_ON(card->qdio.out_qs[i]->bufs[j] != NULL);
2470                         if (qeth_init_qdio_out_buf(card->qdio.out_qs[i], j))
2471                                 goto out_freeoutqbufs;
2472                 }
2473         }
2474 
2475         /* completion */
2476         if (qeth_alloc_cq(card))
2477                 goto out_freeoutq;
2478 
2479         return 0;
2480 
2481 out_freeoutqbufs:
2482         while (j > 0) {
2483                 --j;
2484                 kmem_cache_free(qeth_qdio_outbuf_cache,
2485                                 card->qdio.out_qs[i]->bufs[j]);
2486                 card->qdio.out_qs[i]->bufs[j] = NULL;
2487         }
2488 out_freeoutq:
2489         while (i > 0) {
2490                 qeth_free_qdio_out_buf(card->qdio.out_qs[--i]);
2491                 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2492         }
2493         kfree(card->qdio.out_qs);
2494         card->qdio.out_qs = NULL;
2495 out_freepool:
2496         qeth_free_buffer_pool(card);
2497 out_freeinq:
2498         qeth_free_qdio_queue(card->qdio.in_q);
2499         card->qdio.in_q = NULL;
2500 out_nomem:
2501         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2502         return -ENOMEM;
2503 }
2504 
2505 static void qeth_free_qdio_buffers(struct qeth_card *card)
2506 {
2507         int i, j;
2508 
2509         if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2510                 QETH_QDIO_UNINITIALIZED)
2511                 return;
2512 
2513         qeth_free_cq(card);
2514         cancel_delayed_work_sync(&card->buffer_reclaim_work);
2515         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2516                 if (card->qdio.in_q->bufs[j].rx_skb)
2517                         dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2518         }
2519         qeth_free_qdio_queue(card->qdio.in_q);
2520         card->qdio.in_q = NULL;
2521         /* inbound buffer pool */
2522         qeth_free_buffer_pool(card);
2523         /* free outbound qdio_qs */
2524         if (card->qdio.out_qs) {
2525                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2526                         qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2527                         qeth_free_qdio_out_buf(card->qdio.out_qs[i]);
2528                 }
2529                 kfree(card->qdio.out_qs);
2530                 card->qdio.out_qs = NULL;
2531         }
2532 }
2533 
2534 static void qeth_create_qib_param_field(struct qeth_card *card,
2535                 char *param_field)
2536 {
2537 
2538         param_field[0] = _ascebc['P'];
2539         param_field[1] = _ascebc['C'];
2540         param_field[2] = _ascebc['I'];
2541         param_field[3] = _ascebc['T'];
2542         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2543         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2544         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2545 }
2546 
2547 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2548                 char *param_field)
2549 {
2550         param_field[16] = _ascebc['B'];
2551         param_field[17] = _ascebc['L'];
2552         param_field[18] = _ascebc['K'];
2553         param_field[19] = _ascebc['T'];
2554         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2555         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2556         *((unsigned int *) (&param_field[28])) =
2557                 card->info.blkt.inter_packet_jumbo;
2558 }
2559 
2560 static int qeth_qdio_activate(struct qeth_card *card)
2561 {
2562         QETH_DBF_TEXT(SETUP, 3, "qdioact");
2563         return qdio_activate(CARD_DDEV(card));
2564 }
2565 
2566 static int qeth_dm_act(struct qeth_card *card)
2567 {
2568         int rc;
2569         struct qeth_cmd_buffer *iob;
2570 
2571         QETH_DBF_TEXT(SETUP, 2, "dmact");
2572 
2573         iob = qeth_wait_for_buffer(&card->write);
2574         memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2575 
2576         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2577                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2578         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2579                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2580         rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2581         return rc;
2582 }
2583 
2584 static int qeth_mpc_initialize(struct qeth_card *card)
2585 {
2586         int rc;
2587 
2588         QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2589 
2590         rc = qeth_issue_next_read(card);
2591         if (rc) {
2592                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2593                 return rc;
2594         }
2595         rc = qeth_cm_enable(card);
2596         if (rc) {
2597                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2598                 goto out_qdio;
2599         }
2600         rc = qeth_cm_setup(card);
2601         if (rc) {
2602                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2603                 goto out_qdio;
2604         }
2605         rc = qeth_ulp_enable(card);
2606         if (rc) {
2607                 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2608                 goto out_qdio;
2609         }
2610         rc = qeth_ulp_setup(card);
2611         if (rc) {
2612                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2613                 goto out_qdio;
2614         }
2615         rc = qeth_alloc_qdio_buffers(card);
2616         if (rc) {
2617                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2618                 goto out_qdio;
2619         }
2620         rc = qeth_qdio_establish(card);
2621         if (rc) {
2622                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2623                 qeth_free_qdio_buffers(card);
2624                 goto out_qdio;
2625         }
2626         rc = qeth_qdio_activate(card);
2627         if (rc) {
2628                 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2629                 goto out_qdio;
2630         }
2631         rc = qeth_dm_act(card);
2632         if (rc) {
2633                 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2634                 goto out_qdio;
2635         }
2636 
2637         return 0;
2638 out_qdio:
2639         qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2640         qdio_free(CARD_DDEV(card));
2641         return rc;
2642 }
2643 
2644 static void qeth_print_status_with_portname(struct qeth_card *card)
2645 {
2646         char dbf_text[15];
2647         int i;
2648 
2649         sprintf(dbf_text, "%s", card->info.portname + 1);
2650         for (i = 0; i < 8; i++)
2651                 dbf_text[i] =
2652                         (char) _ebcasc[(__u8) dbf_text[i]];
2653         dbf_text[8] = 0;
2654         dev_info(&card->gdev->dev, "Device is a%s card%s%s%s\n"
2655                "with link type %s (portname: %s)\n",
2656                qeth_get_cardname(card),
2657                (card->info.mcl_level[0]) ? " (level: " : "",
2658                (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2659                (card->info.mcl_level[0]) ? ")" : "",
2660                qeth_get_cardname_short(card),
2661                dbf_text);
2662 
2663 }
2664 
2665 static void qeth_print_status_no_portname(struct qeth_card *card)
2666 {
2667         if (card->info.portname[0])
2668                 dev_info(&card->gdev->dev, "Device is a%s "
2669                        "card%s%s%s\nwith link type %s "
2670                        "(no portname needed by interface).\n",
2671                        qeth_get_cardname(card),
2672                        (card->info.mcl_level[0]) ? " (level: " : "",
2673                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2674                        (card->info.mcl_level[0]) ? ")" : "",
2675                        qeth_get_cardname_short(card));
2676         else
2677                 dev_info(&card->gdev->dev, "Device is a%s "
2678                        "card%s%s%s\nwith link type %s.\n",
2679                        qeth_get_cardname(card),
2680                        (card->info.mcl_level[0]) ? " (level: " : "",
2681                        (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2682                        (card->info.mcl_level[0]) ? ")" : "",
2683                        qeth_get_cardname_short(card));
2684 }
2685 
2686 void qeth_print_status_message(struct qeth_card *card)
2687 {
2688         switch (card->info.type) {
2689         case QETH_CARD_TYPE_OSD:
2690         case QETH_CARD_TYPE_OSM:
2691         case QETH_CARD_TYPE_OSX:
2692                 /* VM will use a non-zero first character
2693                  * to indicate a HiperSockets like reporting
2694                  * of the level OSA sets the first character to zero
2695                  * */
2696                 if (!card->info.mcl_level[0]) {
2697                         sprintf(card->info.mcl_level, "%02x%02x",
2698                                 card->info.mcl_level[2],
2699                                 card->info.mcl_level[3]);
2700 
2701                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2702                         break;
2703                 }
2704                 /* fallthrough */
2705         case QETH_CARD_TYPE_IQD:
2706                 if ((card->info.guestlan) ||
2707                     (card->info.mcl_level[0] & 0x80)) {
2708                         card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2709                                 card->info.mcl_level[0]];
2710                         card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2711                                 card->info.mcl_level[1]];
2712                         card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2713                                 card->info.mcl_level[2]];
2714                         card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2715                                 card->info.mcl_level[3]];
2716                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2717                 }
2718                 break;
2719         default:
2720                 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2721         }
2722         if (card->info.portname_required)
2723                 qeth_print_status_with_portname(card);
2724         else
2725                 qeth_print_status_no_portname(card);
2726 }
2727 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2728 
2729 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2730 {
2731         struct qeth_buffer_pool_entry *entry;
2732 
2733         QETH_CARD_TEXT(card, 5, "inwrklst");
2734 
2735         list_for_each_entry(entry,
2736                             &card->qdio.init_pool.entry_list, init_list) {
2737                 qeth_put_buffer_pool_entry(card, entry);
2738         }
2739 }
2740 
2741 static inline struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2742                 struct qeth_card *card)
2743 {
2744         struct list_head *plh;
2745         struct qeth_buffer_pool_entry *entry;
2746         int i, free;
2747         struct page *page;
2748 
2749         if (list_empty(&card->qdio.in_buf_pool.entry_list))
2750                 return NULL;
2751 
2752         list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2753                 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2754                 free = 1;
2755                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2756                         if (page_count(virt_to_page(entry->elements[i])) > 1) {
2757                                 free = 0;
2758                                 break;
2759                         }
2760                 }
2761                 if (free) {
2762                         list_del_init(&entry->list);
2763                         return entry;
2764                 }
2765         }
2766 
2767         /* no free buffer in pool so take first one and swap pages */
2768         entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2769                         struct qeth_buffer_pool_entry, list);
2770         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2771                 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2772                         page = alloc_page(GFP_ATOMIC);
2773                         if (!page) {
2774                                 return NULL;
2775                         } else {
2776                                 free_page((unsigned long)entry->elements[i]);
2777                                 entry->elements[i] = page_address(page);
2778                                 if (card->options.performance_stats)
2779                                         card->perf_stats.sg_alloc_page_rx++;
2780                         }
2781                 }
2782         }
2783         list_del_init(&entry->list);
2784         return entry;
2785 }
2786 
2787 static int qeth_init_input_buffer(struct qeth_card *card,
2788                 struct qeth_qdio_buffer *buf)
2789 {
2790         struct qeth_buffer_pool_entry *pool_entry;
2791         int i;
2792 
2793         if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2794                 buf->rx_skb = dev_alloc_skb(QETH_RX_PULL_LEN + ETH_HLEN);
2795                 if (!buf->rx_skb)
2796                         return 1;
2797         }
2798 
2799         pool_entry = qeth_find_free_buffer_pool_entry(card);
2800         if (!pool_entry)
2801                 return 1;
2802 
2803         /*
2804          * since the buffer is accessed only from the input_tasklet
2805          * there shouldn't be a need to synchronize; also, since we use
2806          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2807          * buffers
2808          */
2809 
2810         buf->pool_entry = pool_entry;
2811         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2812                 buf->buffer->element[i].length = PAGE_SIZE;
2813                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2814                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2815                         buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2816                 else
2817                         buf->buffer->element[i].eflags = 0;
2818                 buf->buffer->element[i].sflags = 0;
2819         }
2820         return 0;
2821 }
2822 
2823 int qeth_init_qdio_queues(struct qeth_card *card)
2824 {
2825         int i, j;
2826         int rc;
2827 
2828         QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2829 
2830         /* inbound queue */
2831         qdio_reset_buffers(card->qdio.in_q->qdio_bufs,
2832                            QDIO_MAX_BUFFERS_PER_Q);
2833         qeth_initialize_working_pool_list(card);
2834         /*give only as many buffers to hardware as we have buffer pool entries*/
2835         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2836                 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2837         card->qdio.in_q->next_buf_to_init =
2838                 card->qdio.in_buf_pool.buf_count - 1;
2839         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2840                      card->qdio.in_buf_pool.buf_count - 1);
2841         if (rc) {
2842                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2843                 return rc;
2844         }
2845 
2846         /* completion */
2847         rc = qeth_cq_init(card);
2848         if (rc) {
2849                 return rc;
2850         }
2851 
2852         /* outbound queue */
2853         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2854                 qdio_reset_buffers(card->qdio.out_qs[i]->qdio_bufs,
2855                                    QDIO_MAX_BUFFERS_PER_Q);
2856                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2857                         qeth_clear_output_buffer(card->qdio.out_qs[i],
2858                                         card->qdio.out_qs[i]->bufs[j],
2859                                         QETH_QDIO_BUF_EMPTY);
2860                 }
2861                 card->qdio.out_qs[i]->card = card;
2862                 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2863                 card->qdio.out_qs[i]->do_pack = 0;
2864                 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2865                 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2866                 atomic_set(&card->qdio.out_qs[i]->state,
2867                            QETH_OUT_Q_UNLOCKED);
2868         }
2869         return 0;
2870 }
2871 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2872 
2873 static inline __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2874 {
2875         switch (link_type) {
2876         case QETH_LINK_TYPE_HSTR:
2877                 return 2;
2878         default:
2879                 return 1;
2880         }
2881 }
2882 
2883 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2884                 struct qeth_ipa_cmd *cmd, __u8 command,
2885                 enum qeth_prot_versions prot)
2886 {
2887         memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2888         cmd->hdr.command = command;
2889         cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2890         cmd->hdr.seqno = card->seqno.ipa;
2891         cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2892         cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2893         if (card->options.layer2)
2894                 cmd->hdr.prim_version_no = 2;
2895         else
2896                 cmd->hdr.prim_version_no = 1;
2897         cmd->hdr.param_count = 1;
2898         cmd->hdr.prot_version = prot;
2899         cmd->hdr.ipa_supported = 0;
2900         cmd->hdr.ipa_enabled = 0;
2901 }
2902 
2903 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2904                 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2905 {
2906         struct qeth_cmd_buffer *iob;
2907         struct qeth_ipa_cmd *cmd;
2908 
2909         iob = qeth_wait_for_buffer(&card->write);
2910         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2911         qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2912 
2913         return iob;
2914 }
2915 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2916 
2917 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2918                 char prot_type)
2919 {
2920         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2921         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2922         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2923                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2924 }
2925 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2926 
2927 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2928                 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2929                         unsigned long),
2930                 void *reply_param)
2931 {
2932         int rc;
2933         char prot_type;
2934 
2935         QETH_CARD_TEXT(card, 4, "sendipa");
2936 
2937         if (card->options.layer2)
2938                 if (card->info.type == QETH_CARD_TYPE_OSN)
2939                         prot_type = QETH_PROT_OSN2;
2940                 else
2941                         prot_type = QETH_PROT_LAYER2;
2942         else
2943                 prot_type = QETH_PROT_TCPIP;
2944         qeth_prepare_ipa_cmd(card, iob, prot_type);
2945         rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
2946                                                 iob, reply_cb, reply_param);
2947         if (rc == -ETIME) {
2948                 qeth_clear_ipacmd_list(card);
2949                 qeth_schedule_recovery(card);
2950         }
2951         return rc;
2952 }
2953 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
2954 
2955 int qeth_send_startlan(struct qeth_card *card)
2956 {
2957         int rc;
2958         struct qeth_cmd_buffer *iob;
2959 
2960         QETH_DBF_TEXT(SETUP, 2, "strtlan");
2961 
2962         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
2963         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
2964         return rc;
2965 }
2966 EXPORT_SYMBOL_GPL(qeth_send_startlan);
2967 
2968 static int qeth_default_setadapterparms_cb(struct qeth_card *card,
2969                 struct qeth_reply *reply, unsigned long data)
2970 {
2971         struct qeth_ipa_cmd *cmd;
2972 
2973         QETH_CARD_TEXT(card, 4, "defadpcb");
2974 
2975         cmd = (struct qeth_ipa_cmd *) data;
2976         if (cmd->hdr.return_code == 0)
2977                 cmd->hdr.return_code =
2978                         cmd->data.setadapterparms.hdr.return_code;
2979         return 0;
2980 }
2981 
2982 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
2983                 struct qeth_reply *reply, unsigned long data)
2984 {
2985         struct qeth_ipa_cmd *cmd;
2986 
2987         QETH_CARD_TEXT(card, 3, "quyadpcb");
2988 
2989         cmd = (struct qeth_ipa_cmd *) data;
2990         if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
2991                 card->info.link_type =
2992                       cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
2993                 QETH_DBF_TEXT_(SETUP, 2, "lnk %d", card->info.link_type);
2994         }
2995         card->options.adp.supported_funcs =
2996                 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
2997         return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
2998 }
2999 
3000 static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
3001                 __u32 command, __u32 cmdlen)
3002 {
3003         struct qeth_cmd_buffer *iob;
3004         struct qeth_ipa_cmd *cmd;
3005 
3006         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
3007                                      QETH_PROT_IPV4);
3008         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3009         cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
3010         cmd->data.setadapterparms.hdr.command_code = command;
3011         cmd->data.setadapterparms.hdr.used_total = 1;
3012         cmd->data.setadapterparms.hdr.seq_no = 1;
3013 
3014         return iob;
3015 }
3016 
3017 int qeth_query_setadapterparms(struct qeth_card *card)
3018 {
3019         int rc;
3020         struct qeth_cmd_buffer *iob;
3021 
3022         QETH_CARD_TEXT(card, 3, "queryadp");
3023         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
3024                                    sizeof(struct qeth_ipacmd_setadpparms));
3025         rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
3026         return rc;
3027 }
3028 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
3029 
3030 static int qeth_query_ipassists_cb(struct qeth_card *card,
3031                 struct qeth_reply *reply, unsigned long data)
3032 {
3033         struct qeth_ipa_cmd *cmd;
3034 
3035         QETH_DBF_TEXT(SETUP, 2, "qipasscb");
3036 
3037         cmd = (struct qeth_ipa_cmd *) data;
3038 
3039         switch (cmd->hdr.return_code) {
3040         case IPA_RC_NOTSUPP:
3041         case IPA_RC_L2_UNSUPPORTED_CMD:
3042                 QETH_DBF_TEXT(SETUP, 2, "ipaunsup");
3043                 card->options.ipa4.supported_funcs |= IPA_SETADAPTERPARMS;
3044                 card->options.ipa6.supported_funcs |= IPA_SETADAPTERPARMS;
3045                 return -0;
3046         default:
3047                 if (cmd->hdr.return_code) {
3048                         QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Unhandled "
3049                                                 "rc=%d\n",
3050                                                 dev_name(&card->gdev->dev),
3051                                                 cmd->hdr.return_code);
3052                         return 0;
3053                 }
3054         }
3055 
3056         if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
3057                 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
3058                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
3059         } else if (cmd->hdr.prot_version == QETH_PROT_IPV6) {
3060                 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
3061                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
3062         } else
3063                 QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Flawed LIC detected"
3064                                         "\n", dev_name(&card->gdev->dev));
3065         return 0;
3066 }
3067 
3068 int qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
3069 {
3070         int rc;
3071         struct qeth_cmd_buffer *iob;
3072 
3073         QETH_DBF_TEXT_(SETUP, 2, "qipassi%i", prot);
3074         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_QIPASSIST, prot);
3075         rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
3076         return rc;
3077 }
3078 EXPORT_SYMBOL_GPL(qeth_query_ipassists);
3079 
3080 static int qeth_query_switch_attributes_cb(struct qeth_card *card,
3081                                 struct qeth_reply *reply, unsigned long data)
3082 {
3083         struct qeth_ipa_cmd *cmd;
3084         struct qeth_switch_info *sw_info;
3085         struct qeth_query_switch_attributes *attrs;
3086 
3087         QETH_CARD_TEXT(card, 2, "qswiatcb");
3088         cmd = (struct qeth_ipa_cmd *) data;
3089         sw_info = (struct qeth_switch_info *)reply->param;
3090         if (cmd->data.setadapterparms.hdr.return_code == 0) {
3091                 attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3092                 sw_info->capabilities = attrs->capabilities;
3093                 sw_info->settings = attrs->settings;
3094                 QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3095                                                         sw_info->settings);
3096         }
3097         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3098 
3099         return 0;
3100 }
3101 
3102 int qeth_query_switch_attributes(struct qeth_card *card,
3103                                  struct qeth_switch_info *sw_info)
3104 {
3105         struct qeth_cmd_buffer *iob;
3106 
3107         QETH_CARD_TEXT(card, 2, "qswiattr");
3108         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3109                 return -EOPNOTSUPP;
3110         if (!netif_carrier_ok(card->dev))
3111                 return -ENOMEDIUM;
3112         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES,
3113                                 sizeof(struct qeth_ipacmd_setadpparms_hdr));
3114         return qeth_send_ipa_cmd(card, iob,
3115                                 qeth_query_switch_attributes_cb, sw_info);
3116 }
3117 EXPORT_SYMBOL_GPL(qeth_query_switch_attributes);
3118 
3119 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3120                 struct qeth_reply *reply, unsigned long data)
3121 {
3122         struct qeth_ipa_cmd *cmd;
3123         __u16 rc;
3124 
3125         cmd = (struct qeth_ipa_cmd *)data;
3126         rc = cmd->hdr.return_code;
3127         if (rc)
3128                 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3129         else
3130                 card->info.diagass_support = cmd->data.diagass.ext;
3131         return 0;
3132 }
3133 
3134 static int qeth_query_setdiagass(struct qeth_card *card)
3135 {
3136         struct qeth_cmd_buffer *iob;
3137         struct qeth_ipa_cmd    *cmd;
3138 
3139         QETH_DBF_TEXT(SETUP, 2, "qdiagass");
3140         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3141         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3142         cmd->data.diagass.subcmd_len = 16;
3143         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
3144         return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3145 }
3146 
3147 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3148 {
3149         unsigned long info = get_zeroed_page(GFP_KERNEL);
3150         struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3151         struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3152         struct ccw_dev_id ccwid;
3153         int level;
3154 
3155         tid->chpid = card->info.chpid;
3156         ccw_device_get_id(CARD_RDEV(card), &ccwid);
3157         tid->ssid = ccwid.ssid;
3158         tid->devno = ccwid.devno;
3159         if (!info)
3160                 return;
3161         level = stsi(NULL, 0, 0, 0);
3162         if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3163                 tid->lparnr = info222->lpar_number;
3164         if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3165                 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3166                 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3167         }
3168         free_page(info);
3169         return;
3170 }
3171 
3172 static int qeth_hw_trap_cb(struct qeth_card *card,
3173                 struct qeth_reply *reply, unsigned long data)
3174 {
3175         struct qeth_ipa_cmd *cmd;
3176         __u16 rc;
3177 
3178         cmd = (struct qeth_ipa_cmd *)data;
3179         rc = cmd->hdr.return_code;
3180         if (rc)
3181                 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3182         return 0;
3183 }
3184 
3185 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3186 {
3187         struct qeth_cmd_buffer *iob;
3188         struct qeth_ipa_cmd *cmd;
3189 
3190         QETH_DBF_TEXT(SETUP, 2, "diagtrap");
3191         iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3192         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3193         cmd->data.diagass.subcmd_len = 80;
3194         cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
3195         cmd->data.diagass.type = 1;
3196         cmd->data.diagass.action = action;
3197         switch (action) {
3198         case QETH_DIAGS_TRAP_ARM:
3199                 cmd->data.diagass.options = 0x0003;
3200                 cmd->data.diagass.ext = 0x00010000 +
3201                         sizeof(struct qeth_trap_id);
3202                 qeth_get_trap_id(card,
3203                         (struct qeth_trap_id *)cmd->data.diagass.cdata);
3204                 break;
3205         case QETH_DIAGS_TRAP_DISARM:
3206                 cmd->data.diagass.options = 0x0001;
3207                 break;
3208         case QETH_DIAGS_TRAP_CAPTURE:
3209                 break;
3210         }
3211         return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3212 }
3213 EXPORT_SYMBOL_GPL(qeth_hw_trap);
3214 
3215 int qeth_check_qdio_errors(struct qeth_card *card, struct qdio_buffer *buf,
3216                 unsigned int qdio_error, const char *dbftext)
3217 {
3218         if (qdio_error) {
3219                 QETH_CARD_TEXT(card, 2, dbftext);
3220                 QETH_CARD_TEXT_(card, 2, " F15=%02X",
3221                                buf->element[15].sflags);
3222                 QETH_CARD_TEXT_(card, 2, " F14=%02X",
3223                                buf->element[14].sflags);
3224                 QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3225                 if ((buf->element[15].sflags) == 0x12) {
3226                         card->stats.rx_dropped++;
3227                         return 0;
3228                 } else
3229                         return 1;
3230         }
3231         return 0;
3232 }
3233 EXPORT_SYMBOL_GPL(qeth_check_qdio_errors);
3234 
3235 void qeth_buffer_reclaim_work(struct work_struct *work)
3236 {
3237         struct qeth_card *card = container_of(work, struct qeth_card,
3238                 buffer_reclaim_work.work);
3239 
3240         QETH_CARD_TEXT_(card, 2, "brw:%x", card->reclaim_index);
3241         qeth_queue_input_buffer(card, card->reclaim_index);
3242 }
3243 
3244 void qeth_queue_input_buffer(struct qeth_card *card, int index)
3245 {
3246         struct qeth_qdio_q *queue = card->qdio.in_q;
3247         struct list_head *lh;
3248         int count;
3249         int i;
3250         int rc;
3251         int newcount = 0;
3252 
3253         count = (index < queue->next_buf_to_init)?
3254                 card->qdio.in_buf_pool.buf_count -
3255                 (queue->next_buf_to_init - index) :
3256                 card->qdio.in_buf_pool.buf_count -
3257                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
3258         /* only requeue at a certain threshold to avoid SIGAs */
3259         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3260                 for (i = queue->next_buf_to_init;
3261                      i < queue->next_buf_to_init + count; ++i) {
3262                         if (qeth_init_input_buffer(card,
3263                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
3264                                 break;
3265                         } else {
3266                                 newcount++;
3267                         }
3268                 }
3269 
3270                 if (newcount < count) {
3271                         /* we are in memory shortage so we switch back to
3272                            traditional skb allocation and drop packages */
3273                         atomic_set(&card->force_alloc_skb, 3);
3274                         count = newcount;
3275                 } else {
3276                         atomic_add_unless(&card->force_alloc_skb, -1, 0);
3277                 }
3278 
3279                 if (!count) {
3280                         i = 0;
3281                         list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3282                                 i++;
3283                         if (i == card->qdio.in_buf_pool.buf_count) {
3284                                 QETH_CARD_TEXT(card, 2, "qsarbw");
3285                                 card->reclaim_index = index;
3286                                 schedule_delayed_work(
3287                                         &card->buffer_reclaim_work,
3288                                         QETH_RECLAIM_WORK_TIME);
3289                         }
3290                         return;
3291                 }
3292 
3293                 /*
3294                  * according to old code it should be avoided to requeue all
3295                  * 128 buffers in order to benefit from PCI avoidance.
3296                  * this function keeps at least one buffer (the buffer at
3297                  * 'index') un-requeued -> this buffer is the first buffer that
3298                  * will be requeued the next time
3299                  */
3300                 if (card->options.performance_stats) {
3301                         card->perf_stats.inbound_do_qdio_cnt++;
3302                         card->perf_stats.inbound_do_qdio_start_time =
3303                                 qeth_get_micros();
3304                 }
3305                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3306                              queue->next_buf_to_init, count);
3307                 if (card->options.performance_stats)
3308                         card->perf_stats.inbound_do_qdio_time +=
3309                                 qeth_get_micros() -
3310                                 card->perf_stats.inbound_do_qdio_start_time;
3311                 if (rc) {
3312                         QETH_CARD_TEXT(card, 2, "qinberr");
3313                 }
3314                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
3315                                           QDIO_MAX_BUFFERS_PER_Q;
3316         }
3317 }
3318 EXPORT_SYMBOL_GPL(qeth_queue_input_buffer);
3319 
3320 static int qeth_handle_send_error(struct qeth_card *card,
3321                 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3322 {
3323         int sbalf15 = buffer->buffer->element[15].sflags;
3324 
3325         QETH_CARD_TEXT(card, 6, "hdsnderr");
3326         if (card->info.type == QETH_CARD_TYPE_IQD) {
3327                 if (sbalf15 == 0) {
3328                         qdio_err = 0;
3329                 } else {
3330                         qdio_err = 1;
3331                 }
3332         }
3333         qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3334 
3335         if (!qdio_err)
3336                 return QETH_SEND_ERROR_NONE;
3337 
3338         if ((sbalf15 >= 15) && (sbalf15 <= 31))
3339                 return QETH_SEND_ERROR_RETRY;
3340 
3341         QETH_CARD_TEXT(card, 1, "lnkfail");
3342         QETH_CARD_TEXT_(card, 1, "%04x %02x",
3343                        (u16)qdio_err, (u8)sbalf15);
3344         return QETH_SEND_ERROR_LINK_FAILURE;
3345 }
3346 
3347 /*
3348  * Switched to packing state if the number of used buffers on a queue
3349  * reaches a certain limit.
3350  */
3351 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3352 {
3353         if (!queue->do_pack) {
3354                 if (atomic_read(&queue->used_buffers)
3355                     >= QETH_HIGH_WATERMARK_PACK){
3356                         /* switch non-PACKING -> PACKING */
3357                         QETH_CARD_TEXT(queue->card, 6, "np->pack");
3358                         if (queue->card->options.performance_stats)
3359                                 queue->card->perf_stats.sc_dp_p++;
3360                         queue->do_pack = 1;
3361                 }
3362         }
3363 }
3364 
3365 /*
3366  * Switches from packing to non-packing mode. If there is a packing
3367  * buffer on the queue this buffer will be prepared to be flushed.
3368  * In that case 1 is returned to inform the caller. If no buffer
3369  * has to be flushed, zero is returned.
3370  */
3371 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3372 {
3373         struct qeth_qdio_out_buffer *buffer;
3374         int flush_count = 0;
3375 
3376         if (queue->do_pack) {
3377                 if (atomic_read(&queue->used_buffers)
3378                     <= QETH_LOW_WATERMARK_PACK) {
3379                         /* switch PACKING -> non-PACKING */
3380                         QETH_CARD_TEXT(queue->card, 6, "pack->np");
3381                         if (queue->card->options.performance_stats)
3382                                 queue->card->perf_stats.sc_p_dp++;
3383                         queue->do_pack = 0;
3384                         /* flush packing buffers */
3385                         buffer = queue->bufs[queue->next_buf_to_fill];
3386                         if ((atomic_read(&buffer->state) ==
3387                                                 QETH_QDIO_BUF_EMPTY) &&
3388                             (buffer->next_element_to_fill > 0)) {
3389                                 atomic_set(&buffer->state,
3390                                            QETH_QDIO_BUF_PRIMED);
3391                                 flush_count++;
3392                                 queue->next_buf_to_fill =
3393                                         (queue->next_buf_to_fill + 1) %
3394                                         QDIO_MAX_BUFFERS_PER_Q;
3395                         }
3396                 }
3397         }
3398         return flush_count;
3399 }
3400 
3401 
3402 /*
3403  * Called to flush a packing buffer if no more pci flags are on the queue.
3404  * Checks if there is a packing buffer and prepares it to be flushed.
3405  * In that case returns 1, otherwise zero.
3406  */
3407 static int qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
3408 {
3409         struct qeth_qdio_out_buffer *buffer;
3410 
3411         buffer = queue->bufs[queue->next_buf_to_fill];
3412         if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3413            (buffer->next_element_to_fill > 0)) {
3414                 /* it's a packing buffer */
3415                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3416                 queue->next_buf_to_fill =
3417                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
3418                 return 1;
3419         }
3420         return 0;
3421 }
3422 
3423 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3424                                int count)
3425 {
3426         struct qeth_qdio_out_buffer *buf;
3427         int rc;
3428         int i;
3429         unsigned int qdio_flags;
3430 
3431         for (i = index; i < index + count; ++i) {
3432                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3433                 buf = queue->bufs[bidx];
3434                 buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3435                                 SBAL_EFLAGS_LAST_ENTRY;
3436 
3437                 if (queue->bufstates)
3438                         queue->bufstates[bidx].user = buf;
3439 
3440                 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
3441                         continue;
3442 
3443                 if (!queue->do_pack) {
3444                         if ((atomic_read(&queue->used_buffers) >=
3445                                 (QETH_HIGH_WATERMARK_PACK -
3446                                  QETH_WATERMARK_PACK_FUZZ)) &&
3447                             !atomic_read(&queue->set_pci_flags_count)) {
3448                                 /* it's likely that we'll go to packing
3449                                  * mode soon */
3450                                 atomic_inc(&queue->set_pci_flags_count);
3451                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3452                         }
3453                 } else {
3454                         if (!atomic_read(&queue->set_pci_flags_count)) {
3455                                 /*
3456                                  * there's no outstanding PCI any more, so we
3457                                  * have to request a PCI to be sure the the PCI
3458                                  * will wake at some time in the future then we
3459                                  * can flush packed buffers that might still be
3460                                  * hanging around, which can happen if no
3461                                  * further send was requested by the stack
3462                                  */
3463                                 atomic_inc(&queue->set_pci_flags_count);
3464                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3465                         }
3466                 }
3467         }
3468 
3469         queue->card->dev->trans_start = jiffies;
3470         if (queue->card->options.performance_stats) {
3471                 queue->card->perf_stats.outbound_do_qdio_cnt++;
3472                 queue->card->perf_stats.outbound_do_qdio_start_time =
3473                         qeth_get_micros();
3474         }
3475         qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3476         if (atomic_read(&queue->set_pci_flags_count))
3477                 qdio_flags |= QDIO_FLAG_PCI_OUT;
3478         rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
3479                      queue->queue_no, index, count);
3480         if (queue->card->options.performance_stats)
3481                 queue->card->perf_stats.outbound_do_qdio_time +=
3482                         qeth_get_micros() -
3483                         queue->card->perf_stats.outbound_do_qdio_start_time;
3484         atomic_add(count, &queue->used_buffers);
3485         if (rc) {
3486                 queue->card->stats.tx_errors += count;
3487                 /* ignore temporary SIGA errors without busy condition */
3488                 if (rc == -ENOBUFS)
3489                         return;
3490                 QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3491                 QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3492                 QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3493                 QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3494                 QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3495 
3496                 /* this must not happen under normal circumstances. if it
3497                  * happens something is really wrong -> recover */
3498                 qeth_schedule_recovery(queue->card);
3499                 return;
3500         }
3501         if (queue->card->options.performance_stats)
3502                 queue->card->perf_stats.bufs_sent += count;
3503 }
3504 
3505 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3506 {
3507         int index;
3508         int flush_cnt = 0;
3509         int q_was_packing = 0;
3510 
3511         /*
3512          * check if weed have to switch to non-packing mode or if
3513          * we have to get a pci flag out on the queue
3514          */
3515         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3516             !atomic_read(&queue->set_pci_flags_count)) {
3517                 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
3518                                 QETH_OUT_Q_UNLOCKED) {
3519                         /*
3520                          * If we get in here, there was no action in
3521                          * do_send_packet. So, we check if there is a
3522                          * packing buffer to be flushed here.
3523                          */
3524                         netif_stop_queue(queue->card->dev);
3525                         index = queue->next_buf_to_fill;
3526                         q_was_packing = queue->do_pack;
3527                         /* queue->do_pack may change */
3528                         barrier();
3529                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3530                         if (!flush_cnt &&
3531                             !atomic_read(&queue->set_pci_flags_count))
3532                                 flush_cnt +=
3533                                         qeth_flush_buffers_on_no_pci(queue);
3534                         if (queue->card->options.performance_stats &&
3535                             q_was_packing)
3536                                 queue->card->perf_stats.bufs_sent_pack +=
3537                                         flush_cnt;
3538                         if (flush_cnt)
3539                                 qeth_flush_buffers(queue, index, flush_cnt);
3540                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3541                 }
3542         }
3543 }
3544 
3545 void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3546                 unsigned long card_ptr)
3547 {
3548         struct qeth_card *card = (struct qeth_card *)card_ptr;
3549 
3550         if (card->dev && (card->dev->flags & IFF_UP))
3551                 napi_schedule(&card->napi);
3552 }
3553 EXPORT_SYMBOL_GPL(qeth_qdio_start_poll);
3554 
3555 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3556 {
3557         int rc;
3558 
3559         if (card->options.cq ==  QETH_CQ_NOTAVAILABLE) {
3560                 rc = -1;
3561                 goto out;
3562         } else {
3563                 if (card->options.cq == cq) {
3564                         rc = 0;
3565                         goto out;
3566                 }
3567 
3568                 if (card->state != CARD_STATE_DOWN &&
3569                     card->state != CARD_STATE_RECOVER) {
3570                         rc = -1;
3571                         goto out;
3572                 }
3573 
3574                 qeth_free_qdio_buffers(card);
3575                 card->options.cq = cq;
3576                 rc = 0;
3577         }
3578 out:
3579         return rc;
3580 
3581 }
3582 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3583 
3584 
3585 static void qeth_qdio_cq_handler(struct qeth_card *card,
3586                 unsigned int qdio_err,
3587                 unsigned int queue, int first_element, int count) {
3588         struct qeth_qdio_q *cq = card->qdio.c_q;
3589         int i;
3590         int rc;
3591 
3592         if (!qeth_is_cq(card, queue))
3593                 goto out;
3594 
3595         QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3596         QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3597         QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3598 
3599         if (qdio_err) {
3600                 netif_stop_queue(card->dev);
3601                 qeth_schedule_recovery(card);
3602                 goto out;
3603         }
3604 
3605         if (card->options.performance_stats) {
3606                 card->perf_stats.cq_cnt++;
3607                 card->perf_stats.cq_start_time = qeth_get_micros();
3608         }
3609 
3610         for (i = first_element; i < first_element + count; ++i) {
3611                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3612                 struct qdio_buffer *buffer = cq->qdio_bufs[bidx];
3613                 int e;
3614 
3615                 e = 0;
3616                 while (buffer->element[e].addr) {
3617                         unsigned long phys_aob_addr;
3618 
3619                         phys_aob_addr = (unsigned long) buffer->element[e].addr;
3620                         qeth_qdio_handle_aob(card, phys_aob_addr);
3621                         buffer->element[e].addr = NULL;
3622                         buffer->element[e].eflags = 0;
3623                         buffer->element[e].sflags = 0;
3624                         buffer->element[e].length = 0;
3625 
3626                         ++e;
3627                 }
3628 
3629                 buffer->element[15].eflags = 0;
3630                 buffer->element[15].sflags = 0;
3631         }
3632         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3633                     card->qdio.c_q->next_buf_to_init,
3634                     count);
3635         if (rc) {
3636                 dev_warn(&card->gdev->dev,
3637                         "QDIO reported an error, rc=%i\n", rc);
3638                 QETH_CARD_TEXT(card, 2, "qcqherr");
3639         }
3640         card->qdio.c_q->next_buf_to_init = (card->qdio.c_q->next_buf_to_init
3641                                    + count) % QDIO_MAX_BUFFERS_PER_Q;
3642 
3643         netif_wake_queue(card->dev);
3644 
3645         if (card->options.performance_stats) {
3646                 int delta_t = qeth_get_micros();
3647                 delta_t -= card->perf_stats.cq_start_time;
3648                 card->perf_stats.cq_time += delta_t;
3649         }
3650 out:
3651         return;
3652 }
3653 
3654 void qeth_qdio_input_handler(struct ccw_device *ccwdev, unsigned int qdio_err,
3655                 unsigned int queue, int first_elem, int count,
3656                 unsigned long card_ptr)
3657 {
3658         struct qeth_card *card = (struct qeth_card *)card_ptr;
3659 
3660         QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3661         QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3662 
3663         if (qeth_is_cq(card, queue))
3664                 qeth_qdio_cq_handler(card, qdio_err, queue, first_elem, count);
3665         else if (qdio_err)
3666                 qeth_schedule_recovery(card);
3667 
3668 
3669 }
3670 EXPORT_SYMBOL_GPL(qeth_qdio_input_handler);
3671 
3672 void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3673                 unsigned int qdio_error, int __queue, int first_element,
3674                 int count, unsigned long card_ptr)
3675 {
3676         struct qeth_card *card        = (struct qeth_card *) card_ptr;
3677         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3678         struct qeth_qdio_out_buffer *buffer;
3679         int i;
3680 
3681         QETH_CARD_TEXT(card, 6, "qdouhdl");
3682         if (qdio_error & QDIO_ERROR_FATAL) {
3683                 QETH_CARD_TEXT(card, 2, "achkcond");
3684                 netif_stop_queue(card->dev);
3685                 qeth_schedule_recovery(card);
3686                 return;
3687         }
3688         if (card->options.performance_stats) {
3689                 card->perf_stats.outbound_handler_cnt++;
3690                 card->perf_stats.outbound_handler_start_time =
3691                         qeth_get_micros();
3692         }
3693         for (i = first_element; i < (first_element + count); ++i) {
3694                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3695                 buffer = queue->bufs[bidx];
3696                 qeth_handle_send_error(card, buffer, qdio_error);
3697 
3698                 if (queue->bufstates &&
3699                     (queue->bufstates[bidx].flags &
3700                      QDIO_OUTBUF_STATE_FLAG_PENDING) != 0) {
3701                         WARN_ON_ONCE(card->options.cq != QETH_CQ_ENABLED);
3702 
3703                         if (atomic_cmpxchg(&buffer->state,
3704                                            QETH_QDIO_BUF_PRIMED,
3705                                            QETH_QDIO_BUF_PENDING) ==
3706                                 QETH_QDIO_BUF_PRIMED) {
3707                                 qeth_notify_skbs(queue, buffer,
3708                                                  TX_NOTIFY_PENDING);
3709                         }
3710                         buffer->aob = queue->bufstates[bidx].aob;
3711                         QETH_CARD_TEXT_(queue->card, 5, "pel%d", bidx);
3712                         QETH_CARD_TEXT(queue->card, 5, "aob");
3713                         QETH_CARD_TEXT_(queue->card, 5, "%lx",
3714                                         virt_to_phys(buffer->aob));
3715                         if (qeth_init_qdio_out_buf(queue, bidx)) {
3716                                 QETH_CARD_TEXT(card, 2, "outofbuf");
3717                                 qeth_schedule_recovery(card);
3718                         }
3719                 } else {
3720                         if (card->options.cq == QETH_CQ_ENABLED) {
3721                                 enum iucv_tx_notify n;
3722 
3723                                 n = qeth_compute_cq_notification(
3724                                         buffer->buffer->element[15].sflags, 0);
3725                                 qeth_notify_skbs(queue, buffer, n);
3726                         }
3727 
3728                         qeth_clear_output_buffer(queue, buffer,
3729                                                 QETH_QDIO_BUF_EMPTY);
3730                 }
3731                 qeth_cleanup_handled_pending(queue, bidx, 0);
3732         }
3733         atomic_sub(count, &queue->used_buffers);
3734         /* check if we need to do something on this outbound queue */
3735         if (card->info.type != QETH_CARD_TYPE_IQD)
3736                 qeth_check_outbound_queue(queue);
3737 
3738         netif_wake_queue(queue->card->dev);
3739         if (card->options.performance_stats)
3740                 card->perf_stats.outbound_handler_time += qeth_get_micros() -
3741                         card->perf_stats.outbound_handler_start_time;
3742 }
3743 EXPORT_SYMBOL_GPL(qeth_qdio_output_handler);
3744 
3745 /**
3746  * Note: Function assumes that we have 4 outbound queues.
3747  */
3748 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
3749                         int ipv, int cast_type)
3750 {
3751         __be16 *tci;
3752         u8 tos;
3753 
3754         if (cast_type && card->info.is_multicast_different)
3755                 return card->info.is_multicast_different &
3756                         (card->qdio.no_out_queues - 1);
3757 
3758         switch (card->qdio.do_prio_queueing) {
3759         case QETH_PRIO_Q_ING_TOS:
3760         case QETH_PRIO_Q_ING_PREC:
3761                 switch (ipv) {
3762                 case 4:
3763                         tos = ipv4_get_dsfield(ip_hdr(skb));
3764                         break;
3765                 case 6:
3766                         tos = ipv6_get_dsfield(ipv6_hdr(skb));
3767                         break;
3768                 default:
3769                         return card->qdio.default_out_queue;
3770                 }
3771                 if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3772                         return ~tos >> 6 & 3;
3773                 if (tos & IPTOS_MINCOST)
3774                         return 3;
3775                 if (tos & IPTOS_RELIABILITY)
3776                         return 2;
3777                 if (tos & IPTOS_THROUGHPUT)
3778                         return 1;
3779                 if (tos & IPTOS_LOWDELAY)
3780                         return 0;
3781                 break;
3782         case QETH_PRIO_Q_ING_SKB:
3783                 if (skb->priority > 5)
3784                         return 0;
3785                 return ~skb->priority >> 1 & 3;
3786         case QETH_PRIO_Q_ING_VLAN:
3787                 tci = &((struct ethhdr *)skb->data)->h_proto;
3788                 if (*tci == ETH_P_8021Q)
3789                         return ~*(tci + 1) >> (VLAN_PRIO_SHIFT + 1) & 3;
3790                 break;
3791         default:
3792                 break;
3793         }
3794         return card->qdio.default_out_queue;
3795 }
3796 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
3797 
3798 int qeth_get_elements_for_frags(struct sk_buff *skb)
3799 {
3800         int cnt, length, e, elements = 0;
3801         struct skb_frag_struct *frag;
3802         char *data;
3803 
3804         for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3805                 frag = &skb_shinfo(skb)->frags[cnt];
3806                 data = (char *)page_to_phys(skb_frag_page(frag)) +
3807                         frag->page_offset;
3808                 length = frag->size;
3809                 e = PFN_UP((unsigned long)data + length - 1) -
3810                         PFN_DOWN((unsigned long)data);
3811                 elements += e;
3812         }
3813         return elements;
3814 }
3815 EXPORT_SYMBOL_GPL(qeth_get_elements_for_frags);
3816 
3817 int qeth_get_elements_no(struct qeth_card *card,
3818                      struct sk_buff *skb, int elems)
3819 {
3820         int dlen = skb->len - skb->data_len;
3821         int elements_needed = PFN_UP((unsigned long)skb->data + dlen - 1) -
3822                 PFN_DOWN((unsigned long)skb->data);
3823 
3824         elements_needed += qeth_get_elements_for_frags(skb);
3825 
3826         if ((elements_needed + elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
3827                 QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
3828                         "(Number=%d / Length=%d). Discarded.\n",
3829                         (elements_needed+elems), skb->len);
3830                 return 0;
3831         }
3832         return elements_needed;
3833 }
3834 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
3835 
3836 int qeth_hdr_chk_and_bounce(struct sk_buff *skb, struct qeth_hdr **hdr, int len)
3837 {
3838         int hroom, inpage, rest;
3839 
3840         if (((unsigned long)skb->data & PAGE_MASK) !=
3841             (((unsigned long)skb->data + len - 1) & PAGE_MASK)) {
3842                 hroom = skb_headroom(skb);
3843                 inpage = PAGE_SIZE - ((unsigned long) skb->data % PAGE_SIZE);
3844                 rest = len - inpage;
3845                 if (rest > hroom)
3846                         return 1;
3847                 memmove(skb->data - rest, skb->data, skb->len - skb->data_len);
3848                 skb->data -= rest;
3849                 skb->tail -= rest;
3850                 *hdr = (struct qeth_hdr *)skb->data;
3851                 QETH_DBF_MESSAGE(2, "skb bounce len: %d rest: %d\n", len, rest);
3852         }
3853         return 0;
3854 }
3855 EXPORT_SYMBOL_GPL(qeth_hdr_chk_and_bounce);
3856 
3857 static inline void __qeth_fill_buffer(struct sk_buff *skb,
3858         struct qdio_buffer *buffer, int is_tso, int *next_element_to_fill,
3859         int offset)
3860 {
3861         int length = skb->len - skb->data_len;
3862         int length_here;
3863         int element;
3864         char *data;
3865         int first_lap, cnt;
3866         struct skb_frag_struct *frag;
3867 
3868         element = *next_element_to_fill;
3869         data = skb->data;
3870         first_lap = (is_tso == 0 ? 1 : 0);
3871 
3872         if (offset >= 0) {
3873                 data = skb->data + offset;
3874                 length -= offset;
3875                 first_lap = 0;
3876         }
3877 
3878         while (length > 0) {
3879                 /* length_here is the remaining amount of data in this page */
3880                 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3881                 if (length < length_here)
3882                         length_here = length;
3883 
3884                 buffer->element[element].addr = data;
3885                 buffer->element[element].length = length_here;
3886                 length -= length_here;
3887                 if (!length) {
3888                         if (first_lap)
3889                                 if (skb_shinfo(skb)->nr_frags)
3890                                         buffer->element[element].eflags =
3891                                                 SBAL_EFLAGS_FIRST_FRAG;
3892                                 else
3893                                         buffer->element[element].eflags = 0;
3894                         else
3895                                 buffer->element[element].eflags =
3896                                     SBAL_EFLAGS_MIDDLE_FRAG;
3897                 } else {
3898                         if (first_lap)
3899                                 buffer->element[element].eflags =
3900                                     SBAL_EFLAGS_FIRST_FRAG;
3901                         else
3902                                 buffer->element[element].eflags =
3903                                     SBAL_EFLAGS_MIDDLE_FRAG;
3904                 }
3905                 data += length_here;
3906                 element++;
3907                 first_lap = 0;
3908         }
3909 
3910         for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3911                 frag = &skb_shinfo(skb)->frags[cnt];
3912                 data = (char *)page_to_phys(skb_frag_page(frag)) +
3913                         frag->page_offset;
3914                 length = frag->size;
3915                 while (length > 0) {
3916                         length_here = PAGE_SIZE -
3917                                 ((unsigned long) data % PAGE_SIZE);
3918                         if (length < length_here)
3919                                 length_here = length;
3920 
3921                         buffer->element[element].addr = data;
3922                         buffer->element[element].length = length_here;
3923                         buffer->element[element].eflags =
3924                                 SBAL_EFLAGS_MIDDLE_FRAG;
3925                         length -= length_here;
3926                         data += length_here;
3927                         element++;
3928                 }
3929         }
3930 
3931         if (buffer->element[element - 1].eflags)
3932                 buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
3933         *next_element_to_fill = element;
3934 }
3935 
3936 static inline int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
3937                 struct qeth_qdio_out_buffer *buf, struct sk_buff *skb,
3938                 struct qeth_hdr *hdr, int offset, int hd_len)
3939 {
3940         struct qdio_buffer *buffer;
3941         int flush_cnt = 0, hdr_len, large_send = 0;
3942 
3943         buffer = buf->buffer;
3944         atomic_inc(&skb->users);
3945         skb_queue_tail(&buf->skb_list, skb);
3946 
3947         /*check first on TSO ....*/
3948         if (hdr->hdr.l3.id == QETH_HEADER_TYPE_TSO) {
3949                 int element = buf->next_element_to_fill;
3950 
3951                 hdr_len = sizeof(struct qeth_hdr_tso) +
3952                         ((struct qeth_hdr_tso *)hdr)->ext.dg_hdr_len;
3953                 /*fill first buffer entry only with header information */
3954                 buffer->element[element].addr = skb->data;
3955                 buffer->element[element].length = hdr_len;
3956                 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
3957                 buf->next_element_to_fill++;
3958                 skb->data += hdr_len;
3959                 skb->len  -= hdr_len;
3960                 large_send = 1;
3961         }
3962 
3963         if (offset >= 0) {
3964                 int element = buf->next_element_to_fill;
3965                 buffer->element[element].addr = hdr;
3966                 buffer->element[element].length = sizeof(struct qeth_hdr) +
3967                                                         hd_len;
3968                 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
3969                 buf->is_header[element] = 1;
3970                 buf->next_element_to_fill++;
3971         }
3972 
3973         __qeth_fill_buffer(skb, buffer, large_send,
3974                 (int *)&buf->next_element_to_fill, offset);
3975 
3976         if (!queue->do_pack) {
3977                 QETH_CARD_TEXT(queue->card, 6, "fillbfnp");
3978                 /* set state to PRIMED -> will be flushed */
3979                 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3980                 flush_cnt = 1;
3981         } else {
3982                 QETH_CARD_TEXT(queue->card, 6, "fillbfpa");
3983                 if (queue->card->options.performance_stats)
3984                         queue->card->perf_stats.skbs_sent_pack++;
3985                 if (buf->next_element_to_fill >=
3986                                 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
3987                         /*
3988                          * packed buffer if full -> set state PRIMED
3989                          * -> will be flushed
3990                          */
3991                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3992                         flush_cnt = 1;
3993                 }
3994         }
3995         return flush_cnt;
3996 }
3997 
3998 int qeth_do_send_packet_fast(struct qeth_card *card,
3999                 struct qeth_qdio_out_q *queue, struct sk_buff *skb,
4000                 struct qeth_hdr *hdr, int elements_needed,
4001                 int offset, int hd_len)
4002 {
4003         struct qeth_qdio_out_buffer *buffer;
4004         int index;
4005 
4006         /* spin until we get the queue ... */
4007         while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
4008                               QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
4009         /* ... now we've got the queue */
4010         index = queue->next_buf_to_fill;
4011         buffer = queue->bufs[queue->next_buf_to_fill];
4012         /*
4013          * check if buffer is empty to make sure that we do not 'overtake'
4014          * ourselves and try to fill a buffer that is already primed
4015          */
4016         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4017                 goto out;
4018         queue->next_buf_to_fill = (queue->next_buf_to_fill + 1) %
4019                                           QDIO_MAX_BUFFERS_PER_Q;
4020         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4021         qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4022         qeth_flush_buffers(queue, index, 1);
4023         return 0;
4024 out:
4025         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4026         return -EBUSY;
4027 }
4028 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
4029 
4030 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4031                 struct sk_buff *skb, struct qeth_hdr *hdr,
4032                 int elements_needed)
4033 {
4034         struct qeth_qdio_out_buffer *buffer;
4035         int start_index;
4036         int flush_count = 0;
4037         int do_pack = 0;
4038         int tmp;
4039         int rc = 0;
4040 
4041         /* spin until we get the queue ... */
4042         while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
4043                               QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
4044         start_index = queue->next_buf_to_fill;
4045         buffer = queue->bufs[queue->next_buf_to_fill];
4046         /*
4047          * check if buffer is empty to make sure that we do not 'overtake'
4048          * ourselves and try to fill a buffer that is already primed
4049          */
4050         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
4051                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4052                 return -EBUSY;
4053         }
4054         /* check if we need to switch packing state of this queue */
4055         qeth_switch_to_packing_if_needed(queue);
4056         if (queue->do_pack) {
4057                 do_pack = 1;
4058                 /* does packet fit in current buffer? */
4059                 if ((QETH_MAX_BUFFER_ELEMENTS(card) -
4060                     buffer->next_element_to_fill) < elements_needed) {
4061                         /* ... no -> set state PRIMED */
4062                         atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4063                         flush_count++;
4064                         queue->next_buf_to_fill =
4065                                 (queue->next_buf_to_fill + 1) %
4066                                 QDIO_MAX_BUFFERS_PER_Q;
4067                         buffer = queue->bufs[queue->next_buf_to_fill];
4068                         /* we did a step forward, so check buffer state
4069                          * again */
4070                         if (atomic_read(&buffer->state) !=
4071                             QETH_QDIO_BUF_EMPTY) {
4072                                 qeth_flush_buffers(queue, start_index,
4073                                                            flush_count);
4074                                 atomic_set(&queue->state,
4075                                                 QETH_OUT_Q_UNLOCKED);
4076                                 return -EBUSY;
4077                         }
4078                 }
4079         }
4080         tmp = qeth_fill_buffer(queue, buffer, skb, hdr, -1, 0);
4081         queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
4082                                   QDIO_MAX_BUFFERS_PER_Q;
4083         flush_count += tmp;
4084         if (flush_count)
4085                 qeth_flush_buffers(queue, start_index, flush_count);
4086         else if (!atomic_read(&queue->set_pci_flags_count))
4087                 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
4088         /*
4089          * queue->state will go from LOCKED -> UNLOCKED or from
4090          * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
4091          * (switch packing state or flush buffer to get another pci flag out).
4092          * In that case we will enter this loop
4093          */
4094         while (atomic_dec_return(&queue->state)) {
4095                 flush_count = 0;
4096                 start_index = queue->next_buf_to_fill;
4097                 /* check if we can go back to non-packing state */
4098                 flush_count += qeth_switch_to_nonpacking_if_needed(queue);
4099                 /*
4100                  * check if we need to flush a packing buffer to get a pci
4101                  * flag out on the queue
4102                  */
4103                 if (!flush_count && !atomic_read(&queue->set_pci_flags_count))
4104                         flush_count += qeth_flush_buffers_on_no_pci(queue);
4105                 if (flush_count)
4106                         qeth_flush_buffers(queue, start_index, flush_count);
4107         }
4108         /* at this point the queue is UNLOCKED again */
4109         if (queue->card->options.performance_stats && do_pack)
4110                 queue->card->perf_stats.bufs_sent_pack += flush_count;
4111 
4112         return rc;
4113 }
4114 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
4115 
4116 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4117                 struct qeth_reply *reply, unsigned long data)
4118 {
4119         struct qeth_ipa_cmd *cmd;
4120         struct qeth_ipacmd_setadpparms *setparms;
4121 
4122         QETH_CARD_TEXT(card, 4, "prmadpcb");
4123 
4124         cmd = (struct qeth_ipa_cmd *) data;
4125         setparms = &(cmd->data.setadapterparms);
4126 
4127         qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
4128         if (cmd->hdr.return_code) {
4129                 QETH_CARD_TEXT_(card, 4, "prmrc%2.2x", cmd->hdr.return_code);
4130                 setparms->data.mode = SET_PROMISC_MODE_OFF;
4131         }
4132         card->info.promisc_mode = setparms->data.mode;
4133         return 0;
4134 }
4135 
4136 void qeth_setadp_promisc_mode(struct qeth_card *card)
4137 {
4138         enum qeth_ipa_promisc_modes mode;
4139         struct net_device *dev = card->dev;
4140         struct qeth_cmd_buffer *iob;
4141         struct qeth_ipa_cmd *cmd;
4142 
4143         QETH_CARD_TEXT(card, 4, "setprom");
4144 
4145         if (((dev->flags & IFF_PROMISC) &&
4146              (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
4147             (!(dev->flags & IFF_PROMISC) &&
4148              (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
4149                 return;
4150         mode = SET_PROMISC_MODE_OFF;
4151         if (dev->flags & IFF_PROMISC)
4152                 mode = SET_PROMISC_MODE_ON;
4153         QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4154 
4155         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4156                         sizeof(struct qeth_ipacmd_setadpparms));
4157         cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
4158         cmd->data.setadapterparms.data.mode = mode;
4159         qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4160 }
4161 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4162 
4163 int qeth_change_mtu(struct net_device *dev, int new_mtu)
4164 {
4165         struct qeth_card *card;
4166         char dbf_text[15];
4167 
4168         card = dev->ml_priv;
4169 
4170         QETH_CARD_TEXT(card, 4, "chgmtu");
4171         sprintf(dbf_text, "%8x", new_mtu);
4172         QETH_CARD_TEXT(card, 4, dbf_text);
4173 
4174         if (new_mtu < 64)
4175                 return -EINVAL;
4176         if (new_mtu > 65535)
4177                 return -EINVAL;
4178         if ((!qeth_is_supported(card, IPA_IP_FRAGMENTATION)) &&
4179             (!qeth_mtu_is_valid(card, new_mtu)))
4180                 return -EINVAL;
4181         dev->mtu = new_mtu;
4182         return 0;
4183 }
4184 EXPORT_SYMBOL_GPL(qeth_change_mtu);
4185 
4186 struct net_device_stats *qeth_get_stats(struct net_device *dev)
4187 {
4188         struct qeth_card *card;
4189 
4190         card = dev->ml_priv;
4191 
4192         QETH_CARD_TEXT(card, 5, "getstat");
4193 
4194         return &card->stats;
4195 }
4196 EXPORT_SYMBOL_GPL(qeth_get_stats);
4197 
4198 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4199                 struct qeth_reply *reply, unsigned long data)
4200 {
4201         struct qeth_ipa_cmd *cmd;
4202 
4203         QETH_CARD_TEXT(card, 4, "chgmaccb");
4204 
4205         cmd = (struct qeth_ipa_cmd *) data;
4206         if (!card->options.layer2 ||
4207             !(card->info.mac_bits & QETH_LAYER2_MAC_READ)) {
4208                 memcpy(card->dev->dev_addr,
4209                        &cmd->data.setadapterparms.data.change_addr.addr,
4210                        OSA_ADDR_LEN);
4211                 card->info.mac_bits |= QETH_LAYER2_MAC_READ;
4212         }
4213         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4214         return 0;
4215 }
4216 
4217 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4218 {
4219         int rc;
4220         struct qeth_cmd_buffer *iob;
4221         struct qeth_ipa_cmd *cmd;
4222 
4223         QETH_CARD_TEXT(card, 4, "chgmac");
4224 
4225         iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4226                                    sizeof(struct qeth_ipacmd_setadpparms));
4227         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4228         cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4229         cmd->data.setadapterparms.data.change_addr.addr_size = OSA_ADDR_LEN;
4230         memcpy(&cmd->data.setadapterparms.data.change_addr.addr,
4231                card->dev->dev_addr, OSA_ADDR_LEN);
4232         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4233                                NULL);
4234         return rc;
4235 }
4236 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4237 
4238 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4239                 struct qeth_reply *reply, unsigned long data)
4240 {
4241         struct qeth_ipa_cmd *cmd;
4242         struct qeth_set_access_ctrl *access_ctrl_req;
4243         int fallback = *(int *)reply->param;
4244 
4245         QETH_CARD_TEXT(card, 4, "setaccb");
4246 
4247         cmd = (struct qeth_ipa_cmd *) data;
4248         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4249         QETH_DBF_TEXT_(SETUP, 2, "setaccb");
4250         QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4251         QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
4252                 cmd->data.setadapterparms.hdr.return_code);
4253         if (cmd->data.setadapterparms.hdr.return_code !=
4254                                                 SET_ACCESS_CTRL_RC_SUCCESS)
4255                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
4256                                 card->gdev->dev.kobj.name,
4257                                 access_ctrl_req->subcmd_code,
4258                                 cmd->data.setadapterparms.hdr.return_code);
4259         switch (cmd->data.setadapterparms.hdr.return_code) {
4260         case SET_ACCESS_CTRL_RC_SUCCESS:
4261                 if (card->options.isolation == ISOLATION_MODE_NONE) {
4262                         dev_info(&card->gdev->dev,
4263                             "QDIO data connection isolation is deactivated\n");
4264                 } else {
4265                         dev_info(&card->gdev->dev,
4266                             "QDIO data connection isolation is activated\n");
4267                 }
4268                 break;
4269         case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4270                 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already "
4271                                 "deactivated\n", dev_name(&card->gdev->dev));
4272                 if (fallback)
4273                         card->options.isolation = card->options.prev_isolation;
4274                 break;
4275         case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4276                 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already"
4277                                 " activated\n", dev_name(&card->gdev->dev));
4278                 if (fallback)
4279                         card->options.isolation = card->options.prev_isolation;
4280                 break;
4281         case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4282                 dev_err(&card->gdev->dev, "Adapter does not "
4283                         "support QDIO data connection isolation\n");
4284                 break;
4285         case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4286                 dev_err(&card->gdev->dev,
4287                         "Adapter is dedicated. "
4288                         "QDIO data connection isolation not supported\n");
4289                 if (fallback)
4290                         card->options.isolation = card->options.prev_isolation;
4291                 break;
4292         case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4293                 dev_err(&card->gdev->dev,
4294                         "TSO does not permit QDIO data connection isolation\n");
4295                 if (fallback)
4296                         card->options.isolation = card->options.prev_isolation;
4297                 break;
4298         case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4299                 dev_err(&card->gdev->dev, "The adjacent switch port does not "
4300                         "support reflective relay mode\n");
4301                 if (fallback)
4302                         card->options.isolation = card->options.prev_isolation;
4303                 break;
4304         case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4305                 dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4306                                         "enabled at the adjacent switch port");
4307                 if (fallback)
4308                         card->options.isolation = card->options.prev_isolation;
4309                 break;
4310         case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4311                 dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4312                                         "at the adjacent switch failed\n");
4313                 break;
4314         default:
4315                 /* this should never happen */
4316                 if (fallback)
4317                         card->options.isolation = card->options.prev_isolation;
4318                 break;
4319         }
4320         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4321         return 0;
4322 }
4323 
4324 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
4325                 enum qeth_ipa_isolation_modes isolation, int fallback)
4326 {
4327         int rc;
4328         struct qeth_cmd_buffer *iob;
4329         struct qeth_ipa_cmd *cmd;
4330         struct qeth_set_access_ctrl *access_ctrl_req;
4331 
4332         QETH_CARD_TEXT(card, 4, "setacctl");
4333 
4334         QETH_DBF_TEXT_(SETUP, 2, "setacctl");
4335         QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4336 
4337         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
4338                                    sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4339                                    sizeof(struct qeth_set_access_ctrl));
4340         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4341         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4342         access_ctrl_req->subcmd_code = isolation;
4343 
4344         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
4345                                &fallback);
4346         QETH_DBF_TEXT_(SETUP, 2, "rc=%d", rc);
4347         return rc;
4348 }
4349 
4350 int qeth_set_access_ctrl_online(struct qeth_card *card, int fallback)
4351 {
4352         int rc = 0;
4353 
4354         QETH_CARD_TEXT(card, 4, "setactlo");
4355 
4356         if ((card->info.type == QETH_CARD_TYPE_OSD ||
4357              card->info.type == QETH_CARD_TYPE_OSX) &&
4358              qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
4359                 rc = qeth_setadpparms_set_access_ctrl(card,
4360                         card->options.isolation, fallback);
4361                 if (rc) {
4362                         QETH_DBF_MESSAGE(3,
4363                                 "IPA(SET_ACCESS_CTRL,%s,%d) sent failed\n",
4364                                 card->gdev->dev.kobj.name,
4365                                 rc);
4366                         rc = -EOPNOTSUPP;
4367                 }
4368         } else if (card->options.isolation != ISOLATION_MODE_NONE) {
4369                 card->options.isolation = ISOLATION_MODE_NONE;
4370 
4371                 dev_err(&card->gdev->dev, "Adapter does not "
4372                         "support QDIO data connection isolation\n");
4373                 rc = -EOPNOTSUPP;
4374         }
4375         return rc;
4376 }
4377 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
4378 
4379 void qeth_tx_timeout(struct net_device *dev)
4380 {
4381         struct qeth_card *card;
4382 
4383         card = dev->ml_priv;
4384         QETH_CARD_TEXT(card, 4, "txtimeo");
4385         card->stats.tx_errors++;
4386         qeth_schedule_recovery(card);
4387 }
4388 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
4389 
4390 int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4391 {
4392         struct qeth_card *card = dev->ml_priv;
4393         int rc = 0;
4394 
4395         switch (regnum) {
4396         case MII_BMCR: /* Basic mode control register */
4397                 rc = BMCR_FULLDPLX;
4398                 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
4399                     (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4400                     (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
4401                         rc |= BMCR_SPEED100;
4402                 break;
4403         case MII_BMSR: /* Basic mode status register */
4404                 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4405                      BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4406                      BMSR_100BASE4;
4407                 break;
4408         case MII_PHYSID1: /* PHYS ID 1 */
4409                 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4410                      dev->dev_addr[2];
4411                 rc = (rc >> 5) & 0xFFFF;
4412                 break;
4413         case MII_PHYSID2: /* PHYS ID 2 */
4414                 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4415                 break;
4416         case MII_ADVERTISE: /* Advertisement control reg */
4417                 rc = ADVERTISE_ALL;
4418                 break;
4419         case MII_LPA: /* Link partner ability reg */
4420                 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4421                      LPA_100BASE4 | LPA_LPACK;
4422                 break;
4423         case MII_EXPANSION: /* Expansion register */
4424                 break;
4425         case MII_DCOUNTER: /* disconnect counter */
4426                 break;
4427         case MII_FCSCOUNTER: /* false carrier counter */
4428                 break;
4429         case MII_NWAYTEST: /* N-way auto-neg test register */
4430                 break;
4431         case MII_RERRCOUNTER: /* rx error counter */
4432                 rc = card->stats.rx_errors;
4433                 break;
4434         case MII_SREVISION: /* silicon revision */
4435                 break;
4436         case MII_RESV1: /* reserved 1 */
4437                 break;
4438         case MII_LBRERROR: /* loopback, rx, bypass error */
4439                 break;
4440         case MII_PHYADDR: /* physical address */
4441                 break;
4442         case MII_RESV2: /* reserved 2 */
4443                 break;
4444         case MII_TPISTATUS: /* TPI status for 10mbps */
4445                 break;
4446         case MII_NCONFIG: /* network interface config */
4447                 break;
4448         default:
4449                 break;
4450         }
4451         return rc;
4452 }
4453 EXPORT_SYMBOL_GPL(qeth_mdio_read);
4454 
4455 static int qeth_send_ipa_snmp_cmd(struct qeth_card *card,
4456                 struct qeth_cmd_buffer *iob, int len,
4457                 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
4458                         unsigned long),
4459                 void *reply_param)
4460 {
4461         u16 s1, s2;
4462 
4463         QETH_CARD_TEXT(card, 4, "sendsnmp");
4464 
4465         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4466         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4467                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4468         /* adjust PDU length fields in IPA_PDU_HEADER */
4469         s1 = (u32) IPA_PDU_HEADER_SIZE + len;
4470         s2 = (u32) len;
4471         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
4472         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
4473         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
4474         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
4475         return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4476                                       reply_cb, reply_param);
4477 }
4478 
4479 static int qeth_snmp_command_cb(struct qeth_card *card,
4480                 struct qeth_reply *reply, unsigned long sdata)
4481 {
4482         struct qeth_ipa_cmd *cmd;
4483         struct qeth_arp_query_info *qinfo;
4484         struct qeth_snmp_cmd *snmp;
4485         unsigned char *data;
4486         __u16 data_len;
4487 
4488         QETH_CARD_TEXT(card, 3, "snpcmdcb");
4489 
4490         cmd = (struct qeth_ipa_cmd *) sdata;
4491         data = (unsigned char *)((char *)cmd - reply->offset);
4492         qinfo = (struct qeth_arp_query_info *) reply->param;
4493         snmp = &cmd->data.setadapterparms.data.snmp;
4494 
4495         if (cmd->hdr.return_code) {
4496                 QETH_CARD_TEXT_(card, 4, "scer1%i", cmd->hdr.return_code);
4497                 return 0;
4498         }
4499         if (cmd->data.setadapterparms.hdr.return_code) {
4500                 cmd->hdr.return_code =
4501                         cmd->data.setadapterparms.hdr.return_code;
4502                 QETH_CARD_TEXT_(card, 4, "scer2%i", cmd->hdr.return_code);
4503                 return 0;
4504         }
4505         data_len = *((__u16 *)QETH_IPA_PDU_LEN_PDU1(data));
4506         if (cmd->data.setadapterparms.hdr.seq_no == 1)
4507                 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
4508         else
4509                 data_len -= (__u16)((char *)&snmp->request - (char *)cmd);
4510 
4511         /* check if there is enough room in userspace */
4512         if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4513                 QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOMEM);
4514                 cmd->hdr.return_code = IPA_RC_ENOMEM;
4515                 return 0;
4516         }
4517         QETH_CARD_TEXT_(card, 4, "snore%i",
4518                        cmd->data.setadapterparms.hdr.used_total);
4519         QETH_CARD_TEXT_(card, 4, "sseqn%i",
4520                 cmd->data.setadapterparms.hdr.seq_no);
4521         /*copy entries to user buffer*/
4522         if (cmd->data.setadapterparms.hdr.seq_no == 1) {
4523                 memcpy(qinfo->udata + qinfo->udata_offset,
4524                        (char *)snmp,
4525                        data_len + offsetof(struct qeth_snmp_cmd, data));
4526                 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
4527         } else {
4528                 memcpy(qinfo->udata + qinfo->udata_offset,
4529                        (char *)&snmp->request, data_len);
4530         }
4531         qinfo->udata_offset += data_len;
4532         /* check if all replies received ... */
4533                 QETH_CARD_TEXT_(card, 4, "srtot%i",
4534                                cmd->data.setadapterparms.hdr.used_total);
4535                 QETH_CARD_TEXT_(card, 4, "srseq%i",
4536                                cmd->data.setadapterparms.hdr.seq_no);
4537         if (cmd->data.setadapterparms.hdr.seq_no <
4538             cmd->data.setadapterparms.hdr.used_total)
4539                 return 1;
4540         return 0;
4541 }
4542 
4543 int qeth_snmp_command(struct qeth_card *card, char __user *udata)
4544 {
4545         struct qeth_cmd_buffer *iob;
4546         struct qeth_ipa_cmd *cmd;
4547         struct qeth_snmp_ureq *ureq;
4548         unsigned int req_len;
4549         struct qeth_arp_query_info qinfo = {0, };
4550         int rc = 0;
4551 
4552         QETH_CARD_TEXT(card, 3, "snmpcmd");
4553 
4554         if (card->info.guestlan)
4555                 return -EOPNOTSUPP;
4556 
4557         if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
4558             (!card->options.layer2)) {
4559                 return -EOPNOTSUPP;
4560         }
4561         /* skip 4 bytes (data_len struct member) to get req_len */
4562         if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
4563                 return -EFAULT;
4564         if (req_len > (QETH_BUFSIZE - IPA_PDU_HEADER_SIZE -
4565                        sizeof(struct qeth_ipacmd_hdr) -
4566                        sizeof(struct qeth_ipacmd_setadpparms_hdr)))
4567                 return -EINVAL;
4568         ureq = memdup_user(udata, req_len + sizeof(struct qeth_snmp_ureq_hdr));
4569         if (IS_ERR(ureq)) {
4570                 QETH_CARD_TEXT(card, 2, "snmpnome");
4571                 return PTR_ERR(ureq);
4572         }
4573         qinfo.udata_len = ureq->hdr.data_len;
4574         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
4575         if (!qinfo.udata) {
4576                 kfree(ureq);
4577                 return -ENOMEM;
4578         }
4579         qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4580 
4581         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
4582                                    QETH_SNMP_SETADP_CMDLENGTH + req_len);
4583         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4584         memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
4585         rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
4586                                     qeth_snmp_command_cb, (void *)&qinfo);
4587         if (rc)
4588                 QETH_DBF_MESSAGE(2, "SNMP command failed on %s: (0x%x)\n",
4589                            QETH_CARD_IFNAME(card), rc);
4590         else {
4591                 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
4592                         rc = -EFAULT;
4593         }
4594 
4595         kfree(ureq);
4596         kfree(qinfo.udata);
4597         return rc;
4598 }
4599 EXPORT_SYMBOL_GPL(qeth_snmp_command);
4600 
4601 static int qeth_setadpparms_query_oat_cb(struct qeth_card *card,
4602                 struct qeth_reply *reply, unsigned long data)
4603 {
4604         struct qeth_ipa_cmd *cmd;
4605         struct qeth_qoat_priv *priv;
4606         char *resdata;
4607         int resdatalen;
4608 
4609         QETH_CARD_TEXT(card, 3, "qoatcb");
4610 
4611         cmd = (struct qeth_ipa_cmd *)data;
4612         priv = (struct qeth_qoat_priv *)reply->param;
4613         resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4614         resdata = (char *)data + 28;
4615 
4616         if (resdatalen > (priv->buffer_len - priv->response_len)) {
4617                 cmd->hdr.return_code = IPA_RC_FFFF;
4618                 return 0;
4619         }
4620 
4621         memcpy((priv->buffer + priv->response_len), resdata,
4622                 resdatalen);
4623         priv->response_len += resdatalen;
4624 
4625         if (cmd->data.setadapterparms.hdr.seq_no <
4626             cmd->data.setadapterparms.hdr.used_total)
4627                 return 1;
4628         return 0;
4629 }
4630 
4631 int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4632 {
4633         int rc = 0;
4634         struct qeth_cmd_buffer *iob;
4635         struct qeth_ipa_cmd *cmd;
4636         struct qeth_query_oat *oat_req;
4637         struct qeth_query_oat_data oat_data;
4638         struct qeth_qoat_priv priv;
4639         void __user *tmp;
4640 
4641         QETH_CARD_TEXT(card, 3, "qoatcmd");
4642 
4643         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4644                 rc = -EOPNOTSUPP;
4645                 goto out;
4646         }
4647 
4648         if (copy_from_user(&oat_data, udata,
4649             sizeof(struct qeth_query_oat_data))) {
4650                         rc = -EFAULT;
4651                         goto out;
4652         }
4653 
4654         priv.buffer_len = oat_data.buffer_len;
4655         priv.response_len = 0;
4656         priv.buffer =  kzalloc(oat_data.buffer_len, GFP_KERNEL);
4657         if (!priv.buffer) {
4658                 rc = -ENOMEM;
4659                 goto out;
4660         }
4661 
4662         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4663                                    sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4664                                    sizeof(struct qeth_query_oat));
4665         cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4666         oat_req = &cmd->data.setadapterparms.data.query_oat;
4667         oat_req->subcmd_code = oat_data.command;
4668 
4669         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb,
4670                                &priv);
4671         if (!rc) {
4672                 if (is_compat_task())
4673                         tmp = compat_ptr(oat_data.ptr);
4674                 else
4675                         tmp = (void __user *)(unsigned long)oat_data.ptr;
4676 
4677                 if (copy_to_user(tmp, priv.buffer,
4678                     priv.response_len)) {
4679                         rc = -EFAULT;
4680                         goto out_free;
4681                 }
4682 
4683                 oat_data.response_len = priv.response_len;
4684 
4685                 if (copy_to_user(udata, &oat_data,
4686                     sizeof(struct qeth_query_oat_data)))
4687                         rc = -EFAULT;
4688         } else
4689                 if (rc == IPA_RC_FFFF)
4690                         rc = -EFAULT;
4691 
4692 out_free:
4693         kfree(priv.buffer);
4694 out:
4695         return rc;
4696 }
4697 EXPORT_SYMBOL_GPL(qeth_query_oat_command);
4698 
4699 static int qeth_query_card_info_cb(struct qeth_card *card,
4700                                    struct qeth_reply *reply, unsigned long data)
4701 {
4702         struct qeth_ipa_cmd *cmd;
4703         struct qeth_query_card_info *card_info;
4704         struct carrier_info *carrier_info;
4705 
4706         QETH_CARD_TEXT(card, 2, "qcrdincb");
4707         carrier_info = (struct carrier_info *)reply->param;
4708         cmd = (struct qeth_ipa_cmd *)data;
4709         card_info = &cmd->data.setadapterparms.data.card_info;
4710         if (cmd->data.setadapterparms.hdr.return_code == 0) {
4711                 carrier_info->card_type = card_info->card_type;
4712                 carrier_info->port_mode = card_info->port_mode;
4713                 carrier_info->port_speed = card_info->port_speed;
4714         }
4715 
4716         qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4717         return 0;
4718 }
4719 
4720 int qeth_query_card_info(struct qeth_card *card,
4721                                 struct carrier_info *carrier_info)
4722 {
4723         struct qeth_cmd_buffer *iob;
4724 
4725         QETH_CARD_TEXT(card, 2, "qcrdinfo");
4726         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_CARD_INFO))
4727                 return -EOPNOTSUPP;
4728         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_CARD_INFO,
4729                 sizeof(struct qeth_ipacmd_setadpparms_hdr));
4730         return qeth_send_ipa_cmd(card, iob, qeth_query_card_info_cb,
4731                                         (void *)carrier_info);
4732 }
4733 EXPORT_SYMBOL_GPL(qeth_query_card_info);
4734 
4735 static inline int qeth_get_qdio_q_format(struct qeth_card *card)
4736 {
4737         switch (card->info.type) {
4738         case QETH_CARD_TYPE_IQD:
4739                 return 2;
4740         default:
4741                 return 0;
4742         }
4743 }
4744 
4745 static void qeth_determine_capabilities(struct qeth_card *card)
4746 {
4747         int rc;
4748         int length;
4749         char *prcd;
4750         struct ccw_device *ddev;
4751         int ddev_offline = 0;
4752 
4753         QETH_DBF_TEXT(SETUP, 2, "detcapab");
4754         ddev = CARD_DDEV(card);
4755         if (!ddev->online) {
4756                 ddev_offline = 1;
4757                 rc = ccw_device_set_online(ddev);
4758                 if (rc) {
4759                         QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4760                         goto out;
4761                 }
4762         }
4763 
4764         rc = qeth_read_conf_data(card, (void **) &prcd, &length);
4765         if (rc) {
4766                 QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
4767                         dev_name(&card->gdev->dev), rc);
4768                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4769                 goto out_offline;
4770         }
4771         qeth_configure_unitaddr(card, prcd);
4772         if (ddev_offline)
4773                 qeth_configure_blkt_default(card, prcd);
4774         kfree(prcd);
4775 
4776         rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4777         if (rc)
4778                 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
4779 
4780         QETH_DBF_TEXT_(SETUP, 2, "qfmt%d", card->ssqd.qfmt);
4781         QETH_DBF_TEXT_(SETUP, 2, "%d", card->ssqd.qdioac1);
4782         QETH_DBF_TEXT_(SETUP, 2, "%d", card->ssqd.qdioac3);
4783         QETH_DBF_TEXT_(SETUP, 2, "icnt%d", card->ssqd.icnt);
4784         if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
4785             ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
4786             ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
4787                 dev_info(&card->gdev->dev,
4788                         "Completion Queueing supported\n");
4789         } else {
4790                 card->options.cq = QETH_CQ_NOTAVAILABLE;
4791         }
4792 
4793 
4794 out_offline:
4795         if (ddev_offline == 1)
4796                 ccw_device_set_offline(ddev);
4797 out:
4798         return;
4799 }
4800 
4801 static inline void qeth_qdio_establish_cq(struct qeth_card *card,
4802         struct qdio_buffer **in_sbal_ptrs,
4803         void (**queue_start_poll) (struct ccw_device *, int, unsigned long)) {
4804         int i;
4805 
4806         if (card->options.cq == QETH_CQ_ENABLED) {
4807                 int offset = QDIO_MAX_BUFFERS_PER_Q *
4808                              (card->qdio.no_in_queues - 1);
4809                 i = QDIO_MAX_BUFFERS_PER_Q * (card->qdio.no_in_queues - 1);
4810                 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4811                         in_sbal_ptrs[offset + i] = (struct qdio_buffer *)
4812                                 virt_to_phys(card->qdio.c_q->bufs[i].buffer);
4813                 }
4814 
4815                 queue_start_poll[card->qdio.no_in_queues - 1] = NULL;
4816         }
4817 }
4818 
4819 static int qeth_qdio_establish(struct qeth_card *card)
4820 {
4821         struct qdio_initialize init_data;
4822         char *qib_param_field;
4823         struct qdio_buffer **in_sbal_ptrs;
4824         void (**queue_start_poll) (struct ccw_device *, int, unsigned long);
4825         struct qdio_buffer **out_sbal_ptrs;
4826         int i, j, k;
4827         int rc = 0;
4828 
4829         QETH_DBF_TEXT(SETUP, 2, "qdioest");
4830 
4831         qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
4832                               GFP_KERNEL);
4833         if (!qib_param_field) {
4834                 rc =  -ENOMEM;
4835                 goto out_free_nothing;
4836         }
4837 
4838         qeth_create_qib_param_field(card, qib_param_field);
4839         qeth_create_qib_param_field_blkt(card, qib_param_field);
4840 
4841         in_sbal_ptrs = kzalloc(card->qdio.no_in_queues *
4842                                QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
4843                                GFP_KERNEL);
4844         if (!in_sbal_ptrs) {
4845                 rc = -ENOMEM;
4846                 goto out_free_qib_param;
4847         }
4848         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4849                 in_sbal_ptrs[i] = (struct qdio_buffer *)
4850                         virt_to_phys(card->qdio.in_q->bufs[i].buffer);
4851         }
4852 
4853         queue_start_poll = kzalloc(sizeof(void *) * card->qdio.no_in_queues,
4854                                    GFP_KERNEL);
4855         if (!queue_start_poll) {
4856                 rc = -ENOMEM;
4857                 goto out_free_in_sbals;
4858         }
4859         for (i = 0; i < card->qdio.no_in_queues; ++i)
4860                 queue_start_poll[i] = card->discipline->start_poll;
4861 
4862         qeth_qdio_establish_cq(card, in_sbal_ptrs, queue_start_poll);
4863 
4864         out_sbal_ptrs =
4865                 kzalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
4866                         sizeof(void *), GFP_KERNEL);
4867         if (!out_sbal_ptrs) {
4868                 rc = -ENOMEM;
4869                 goto out_free_queue_start_poll;
4870         }
4871         for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
4872                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
4873                         out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
4874                                 card->qdio.out_qs[i]->bufs[j]->buffer);
4875                 }
4876 
4877         memset(&init_data, 0, sizeof(struct qdio_initialize));
4878         init_data.cdev                   = CARD_DDEV(card);
4879         init_data.q_format               = qeth_get_qdio_q_format(card);
4880         init_data.qib_param_field_format = 0;
4881         init_data.qib_param_field        = qib_param_field;
4882         init_data.no_input_qs            = card->qdio.no_in_queues;
4883         init_data.no_output_qs           = card->qdio.no_out_queues;
4884         init_data.input_handler          = card->discipline->input_handler;
4885         init_data.output_handler         = card->discipline->output_handler;
4886         init_data.queue_start_poll_array = queue_start_poll;
4887         init_data.int_parm               = (unsigned long) card;
4888         init_data.input_sbal_addr_array  = (void **) in_sbal_ptrs;
4889         init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
4890         init_data.output_sbal_state_array = card->qdio.out_bufstates;
4891         init_data.scan_threshold =
4892                 (card->info.type == QETH_CARD_TYPE_IQD) ? 1 : 32;
4893 
4894         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
4895                 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
4896                 rc = qdio_allocate(&init_data);
4897                 if (rc) {
4898                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4899                         goto out;
4900                 }
4901                 rc = qdio_establish(&init_data);
4902                 if (rc) {
4903                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4904                         qdio_free(CARD_DDEV(card));
4905                 }
4906         }
4907 
4908         switch (card->options.cq) {
4909         case QETH_CQ_ENABLED:
4910                 dev_info(&card->gdev->dev, "Completion Queue support enabled");
4911                 break;
4912         case QETH_CQ_DISABLED:
4913                 dev_info(&card->gdev->dev, "Completion Queue support disabled");
4914                 break;
4915         default:
4916                 break;
4917         }
4918 out:
4919         kfree(out_sbal_ptrs);
4920 out_free_queue_start_poll:
4921         kfree(queue_start_poll);
4922 out_free_in_sbals:
4923         kfree(in_sbal_ptrs);
4924 out_free_qib_param:
4925         kfree(qib_param_field);
4926 out_free_nothing:
4927         return rc;
4928 }
4929 
4930 static void qeth_core_free_card(struct qeth_card *card)
4931 {
4932 
4933         QETH_DBF_TEXT(SETUP, 2, "freecrd");
4934         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
4935         qeth_clean_channel(&card->read);
4936         qeth_clean_channel(&card->write);
4937         if (card->dev)
4938                 free_netdev(card->dev);
4939         kfree(card->ip_tbd_list);
4940         qeth_free_qdio_buffers(card);
4941         unregister_service_level(&card->qeth_service_level);
4942         kfree(card);
4943 }
4944 
4945 void qeth_trace_features(struct qeth_card *card)
4946 {
4947         QETH_CARD_TEXT(card, 2, "features");
4948         QETH_CARD_TEXT_(card, 2, "%x", card->options.ipa4.supported_funcs);
4949         QETH_CARD_TEXT_(card, 2, "%x", card->options.ipa4.enabled_funcs);
4950         QETH_CARD_TEXT_(card, 2, "%x", card->options.ipa6.supported_funcs);
4951         QETH_CARD_TEXT_(card, 2, "%x", card->options.ipa6.enabled_funcs);
4952         QETH_CARD_TEXT_(card, 2, "%x", card->options.adp.supported_funcs);
4953         QETH_CARD_TEXT_(card, 2, "%x", card->options.adp.enabled_funcs);
4954         QETH_CARD_TEXT_(card, 2, "%x", card->info.diagass_support);
4955 }
4956 EXPORT_SYMBOL_GPL(qeth_trace_features);
4957 
4958 static struct ccw_device_id qeth_ids[] = {
4959         {CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
4960                                         .driver_info = QETH_CARD_TYPE_OSD},
4961         {CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
4962                                         .driver_info = QETH_CARD_TYPE_IQD},
4963         {CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
4964                                         .driver_info = QETH_CARD_TYPE_OSN},
4965         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
4966                                         .driver_info = QETH_CARD_TYPE_OSM},
4967         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
4968                                         .driver_info = QETH_CARD_TYPE_OSX},
4969         {},
4970 };
4971 MODULE_DEVICE_TABLE(ccw, qeth_ids);
4972 
4973 static struct ccw_driver qeth_ccw_driver = {
4974         .driver = {
4975                 .owner = THIS_MODULE,
4976                 .name = "qeth",
4977         },
4978         .ids = qeth_ids,
4979         .probe = ccwgroup_probe_ccwdev,
4980         .remove = ccwgroup_remove_ccwdev,
4981 };
4982 
4983 int qeth_core_hardsetup_card(struct qeth_card *card)
4984 {
4985         int retries = 3;
4986         int rc;
4987 
4988         QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
4989         atomic_set(&card->force_alloc_skb, 0);
4990         qeth_update_from_chp_desc(card);
4991 retry:
4992         if (retries < 3)
4993                 QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
4994                         dev_name(&card->gdev->dev));
4995         rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
4996         ccw_device_set_offline(CARD_DDEV(card));
4997         ccw_device_set_offline(CARD_WDEV(card));
4998         ccw_device_set_offline(CARD_RDEV(card));
4999         qdio_free(CARD_DDEV(card));
5000         rc = ccw_device_set_online(CARD_RDEV(card));
5001         if (rc)
5002                 goto retriable;
5003         rc = ccw_device_set_online(CARD_WDEV(card));
5004         if (rc)
5005                 goto retriable;
5006         rc = ccw_device_set_online(CARD_DDEV(card));
5007         if (rc)
5008                 goto retriable;
5009 retriable:
5010         if (rc == -ERESTARTSYS) {
5011                 QETH_DBF_TEXT(SETUP, 2, "break1");
5012                 return rc;
5013         } else if (rc) {
5014                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
5015                 if (--retries < 0)
5016                         goto out;
5017                 else
5018                         goto retry;
5019         }
5020         qeth_determine_capabilities(card);
5021         qeth_init_tokens(card);
5022         qeth_init_func_level(card);
5023         rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
5024         if (rc == -ERESTARTSYS) {
5025                 QETH_DBF_TEXT(SETUP, 2, "break2");
5026                 return rc;
5027         } else if (rc) {
5028                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
5029                 if (--retries < 0)
5030                         goto out;
5031                 else
5032                         goto retry;
5033         }
5034         rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
5035         if (rc == -ERESTARTSYS) {
5036                 QETH_DBF_TEXT(SETUP, 2, "break3");
5037                 return rc;
5038         } else if (rc) {
5039                 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
5040                 if (--retries < 0)
5041                         goto out;
5042                 else
5043                         goto retry;
5044         }
5045         card->read_or_write_problem = 0;
5046         rc = qeth_mpc_initialize(card);
5047         if (rc) {
5048                 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
5049                 goto out;
5050         }
5051 
5052         card->options.ipa4.supported_funcs = 0;
5053         card->options.adp.supported_funcs = 0;
5054         card->options.sbp.supported_funcs = 0;
5055         card->info.diagass_support = 0;
5056         qeth_query_ipassists(card, QETH_PROT_IPV4);
5057         if (qeth_is_supported(card, IPA_SETADAPTERPARMS))
5058                 qeth_query_setadapterparms(card);
5059         if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST))
5060                 qeth_query_setdiagass(card);
5061         return 0;
5062 out:
5063         dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5064                 "an error on the device\n");
5065         QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
5066                 dev_name(&card->gdev->dev), rc);
5067         return rc;
5068 }
5069 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
5070 
5071 static inline int qeth_create_skb_frag(struct qeth_qdio_buffer *qethbuffer,
5072                 struct qdio_buffer_element *element,
5073                 struct sk_buff **pskb, int offset, int *pfrag, int data_len)
5074 {
5075         struct page *page = virt_to_page(element->addr);
5076         if (*pskb == NULL) {
5077                 if (qethbuffer->rx_skb) {
5078                         /* only if qeth_card.options.cq == QETH_CQ_ENABLED */
5079                         *pskb = qethbuffer->rx_skb;
5080                         qethbuffer->rx_skb = NULL;
5081                 } else {
5082                         *pskb = dev_alloc_skb(QETH_RX_PULL_LEN + ETH_HLEN);
5083                         if (!(*pskb))
5084                                 return -ENOMEM;
5085                 }
5086 
5087                 skb_reserve(*pskb, ETH_HLEN);
5088                 if (data_len <= QETH_RX_PULL_LEN) {
5089                         memcpy(skb_put(*pskb, data_len), element->addr + offset,
5090                                 data_len);
5091                 } else {
5092                         get_page(page);
5093                         memcpy(skb_put(*pskb, QETH_RX_PULL_LEN),
5094                                element->addr + offset, QETH_RX_PULL_LEN);
5095                         skb_fill_page_desc(*pskb, *pfrag, page,
5096                                 offset + QETH_RX_PULL_LEN,
5097                                 data_len - QETH_RX_PULL_LEN);
5098                         (*pskb)->data_len += data_len - QETH_RX_PULL_LEN;
5099                         (*pskb)->len      += data_len - QETH_RX_PULL_LEN;
5100                         (*pskb)->truesize += data_len - QETH_RX_PULL_LEN;
5101                         (*pfrag)++;
5102                 }
5103         } else {
5104                 get_page(page);
5105                 skb_fill_page_desc(*pskb, *pfrag, page, offset, data_len);
5106                 (*pskb)->data_len += data_len;
5107                 (*pskb)->len      += data_len;
5108                 (*pskb)->truesize += data_len;
5109                 (*pfrag)++;
5110         }
5111 
5112 
5113         return 0;
5114 }
5115 
5116 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
5117                 struct qeth_qdio_buffer *qethbuffer,
5118                 struct qdio_buffer_element **__element, int *__offset,
5119                 struct qeth_hdr **hdr)
5120 {
5121         struct qdio_buffer_element *element = *__element;
5122         struct qdio_buffer *buffer = qethbuffer->buffer;
5123         int offset = *__offset;
5124         struct sk_buff *skb = NULL;
5125         int skb_len = 0;
5126         void *data_ptr;
5127         int data_len;
5128         int headroom = 0;
5129         int use_rx_sg = 0;
5130         int frag = 0;
5131 
5132         /* qeth_hdr must not cross element boundaries */
5133         if (element->length < offset + sizeof(struct qeth_hdr)) {
5134                 if (qeth_is_last_sbale(element))
5135                         return NULL;
5136                 element++;
5137                 offset = 0;
5138                 if (element->length < sizeof(struct qeth_hdr))
5139                         return NULL;
5140         }
5141         *hdr = element->addr + offset;
5142 
5143         offset += sizeof(struct qeth_hdr);
5144         switch ((*hdr)->hdr.l2.id) {
5145         case QETH_HEADER_TYPE_LAYER2:
5146                 skb_len = (*hdr)->hdr.l2.pkt_length;
5147                 break;
5148         case QETH_HEADER_TYPE_LAYER3:
5149                 skb_len = (*hdr)->hdr.l3.length;
5150                 headroom = ETH_HLEN;
5151                 break;
5152         case QETH_HEADER_TYPE_OSN:
5153                 skb_len = (*hdr)->hdr.osn.pdu_length;
5154                 headroom = sizeof(struct qeth_hdr);
5155                 break;
5156         default:
5157                 break;
5158         }
5159 
5160         if (!skb_len)
5161                 return NULL;
5162 
5163         if (((skb_len >= card->options.rx_sg_cb) &&
5164              (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
5165              (!atomic_read(&card->force_alloc_skb))) ||
5166             (card->options.cq == QETH_CQ_ENABLED)) {
5167                 use_rx_sg = 1;
5168         } else {
5169                 skb = dev_alloc_skb(skb_len + headroom);
5170                 if (!skb)
5171                         goto no_mem;
5172                 if (headroom)
5173                         skb_reserve(skb, headroom);
5174         }
5175 
5176         data_ptr = element->addr + offset;
5177         while (skb_len) {
5178                 data_len = min(skb_len, (int)(element->length - offset));
5179                 if (data_len) {
5180                         if (use_rx_sg) {
5181                                 if (qeth_create_skb_frag(qethbuffer, element,
5182                                     &skb, offset, &frag, data_len))
5183                                         goto no_mem;
5184                         } else {
5185                                 memcpy(skb_put(skb, data_len), data_ptr,
5186                                         data_len);
5187                         }
5188                 }
5189                 skb_len -= data_len;
5190                 if (skb_len) {
5191                         if (qeth_is_last_sbale(element)) {
5192                                 QETH_CARD_TEXT(card, 4, "unexeob");
5193                                 QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
5194                                 dev_kfree_skb_any(skb);
5195                                 card->stats.rx_errors++;
5196                                 return NULL;
5197                         }
5198                         element++;
5199                         offset = 0;
5200                         data_ptr = element->addr;
5201                 } else {
5202                         offset += data_len;
5203                 }
5204         }
5205         *__element = element;
5206         *__offset = offset;
5207         if (use_rx_sg && card->options.performance_stats) {
5208                 card->perf_stats.sg_skbs_rx++;
5209                 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
5210         }
5211         return skb;
5212 no_mem:
5213         if (net_ratelimit()) {
5214                 QETH_CARD_TEXT(card, 2, "noskbmem");
5215         }
5216         card->stats.rx_dropped++;
5217         return NULL;
5218 }
5219 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
5220 
5221 static void qeth_unregister_dbf_views(void)
5222 {
5223         int x;
5224         for (x = 0; x < QETH_DBF_INFOS; x++) {
5225                 debug_unregister(qeth_dbf[x].id);
5226                 qeth_dbf[x].id = NULL;
5227         }
5228 }
5229 
5230 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
5231 {
5232         char dbf_txt_buf[32];
5233         va_list args;
5234 
5235         if (!debug_level_enabled(id, level))
5236                 return;
5237         va_start(args, fmt);
5238         vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
5239         va_end(args);
5240         debug_text_event(id, level, dbf_txt_buf);
5241 }
5242 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
5243 
5244 static int qeth_register_dbf_views(void)
5245 {
5246         int ret;
5247         int x;
5248 
5249         for (x = 0; x < QETH_DBF_INFOS; x++) {
5250                 /* register the areas */
5251                 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
5252                                                 qeth_dbf[x].pages,
5253                                                 qeth_dbf[x].areas,
5254                                                 qeth_dbf[x].len);
5255                 if (qeth_dbf[x].id == NULL) {
5256                         qeth_unregister_dbf_views();
5257                         return -ENOMEM;
5258                 }
5259 
5260                 /* register a view */
5261                 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
5262                 if (ret) {
5263                         qeth_unregister_dbf_views();
5264                         return ret;
5265                 }
5266 
5267                 /* set a passing level */
5268                 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
5269         }
5270 
5271         return 0;
5272 }
5273 
5274 int qeth_core_load_discipline(struct qeth_card *card,
5275                 enum qeth_discipline_id discipline)
5276 {
5277         int rc = 0;
5278         mutex_lock(&qeth_mod_mutex);
5279         switch (discipline) {
5280         case QETH_DISCIPLINE_LAYER3:
5281                 card->discipline = try_then_request_module(
5282                         symbol_get(qeth_l3_discipline), "qeth_l3");
5283                 break;
5284         case QETH_DISCIPLINE_LAYER2:
5285                 card->discipline = try_then_request_module(
5286                         symbol_get(qeth_l2_discipline), "qeth_l2");
5287                 break;
5288         }
5289         if (!card->discipline) {
5290                 dev_err(&card->gdev->dev, "There is no kernel module to "
5291                         "support discipline %d\n", discipline);
5292                 rc = -EINVAL;
5293         }
5294         mutex_unlock(&qeth_mod_mutex);
5295         return rc;
5296 }
5297 
5298 void qeth_core_free_discipline(struct qeth_card *card)
5299 {
5300         if (card->options.layer2)
5301                 symbol_put(qeth_l2_discipline);
5302         else
5303                 symbol_put(qeth_l3_discipline);
5304         card->discipline = NULL;
5305 }
5306 
5307 static const struct device_type qeth_generic_devtype = {
5308         .name = "qeth_generic",
5309         .groups = qeth_generic_attr_groups,
5310 };
5311 static const struct device_type qeth_osn_devtype = {
5312         .name = "qeth_osn",
5313         .groups = qeth_osn_attr_groups,
5314 };
5315 
5316 #define DBF_NAME_LEN    20
5317 
5318 struct qeth_dbf_entry {
5319         char dbf_name[DBF_NAME_LEN];
5320         debug_info_t *dbf_info;
5321         struct list_head dbf_list;
5322 };
5323 
5324 static LIST_HEAD(qeth_dbf_list);
5325 static DEFINE_MUTEX(qeth_dbf_list_mutex);
5326 
5327 static debug_info_t *qeth_get_dbf_entry(char *name)
5328 {
5329         struct qeth_dbf_entry *entry;
5330         debug_info_t *rc = NULL;
5331 
5332         mutex_lock(&qeth_dbf_list_mutex);
5333         list_for_each_entry(entry, &qeth_dbf_list, dbf_list) {
5334                 if (strcmp(entry->dbf_name, name) == 0) {
5335                         rc = entry->dbf_info;
5336                         break;
5337                 }
5338         }
5339         mutex_unlock(&qeth_dbf_list_mutex);
5340         return rc;
5341 }
5342 
5343 static int qeth_add_dbf_entry(struct qeth_card *card, char *name)
5344 {
5345         struct qeth_dbf_entry *new_entry;
5346 
5347         card->debug = debug_register(name, 2, 1, 8);
5348         if (!card->debug) {
5349                 QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
5350                 goto err;
5351         }
5352         if (debug_register_view(card->debug, &debug_hex_ascii_view))
5353                 goto err_dbg;
5354         new_entry = kzalloc(sizeof(struct qeth_dbf_entry), GFP_KERNEL);
5355         if (!new_entry)
5356                 goto err_dbg;
5357         strncpy(new_entry->dbf_name, name, DBF_NAME_LEN);
5358         new_entry->dbf_info = card->debug;
5359         mutex_lock(&qeth_dbf_list_mutex);
5360         list_add(&new_entry->dbf_list, &qeth_dbf_list);
5361         mutex_unlock(&qeth_dbf_list_mutex);
5362 
5363         return 0;
5364 
5365 err_dbg:
5366         debug_unregister(card->debug);
5367 err:
5368         return -ENOMEM;
5369 }
5370 
5371 static void qeth_clear_dbf_list(void)
5372 {
5373         struct qeth_dbf_entry *entry, *tmp;
5374 
5375         mutex_lock(&qeth_dbf_list_mutex);
5376         list_for_each_entry_safe(entry, tmp, &qeth_dbf_list, dbf_list) {
5377                 list_del(&entry->dbf_list);
5378                 debug_unregister(entry->dbf_info);
5379                 kfree(entry);
5380         }
5381         mutex_unlock(&qeth_dbf_list_mutex);
5382 }
5383 
5384 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
5385 {
5386         struct qeth_card *card;
5387         struct device *dev;
5388         int rc;
5389         unsigned long flags;
5390         char dbf_name[DBF_NAME_LEN];
5391 
5392         QETH_DBF_TEXT(SETUP, 2, "probedev");
5393 
5394         dev = &gdev->dev;
5395         if (!get_device(dev))
5396                 return -ENODEV;
5397 
5398         QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
5399 
5400         card = qeth_alloc_card();
5401         if (!card) {
5402                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
5403                 rc = -ENOMEM;
5404                 goto err_dev;
5405         }
5406 
5407         snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
5408                 dev_name(&gdev->dev));
5409         card->debug = qeth_get_dbf_entry(dbf_name);
5410         if (!card->debug) {
5411                 rc = qeth_add_dbf_entry(card, dbf_name);
5412                 if (rc)
5413                         goto err_card;
5414         }
5415 
5416         card->read.ccwdev  = gdev->cdev[0];
5417         card->write.ccwdev = gdev->cdev[1];
5418         card->data.ccwdev  = gdev->cdev[2];
5419         dev_set_drvdata(&gdev->dev, card);
5420         card->gdev = gdev;
5421         gdev->cdev[0]->handler = qeth_irq;
5422         gdev->cdev[1]->handler = qeth_irq;
5423         gdev->cdev[2]->handler = qeth_irq;
5424 
5425         rc = qeth_determine_card_type(card);
5426         if (rc) {
5427                 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
5428                 goto err_card;
5429         }
5430         rc = qeth_setup_card(card);
5431         if (rc) {
5432                 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
5433                 goto err_card;
5434         }
5435 
5436         if (card->info.type == QETH_CARD_TYPE_OSN)
5437                 gdev->dev.type = &qeth_osn_devtype;
5438         else
5439                 gdev->dev.type = &qeth_generic_devtype;
5440 
5441         switch (card->info.type) {
5442         case QETH_CARD_TYPE_OSN:
5443         case QETH_CARD_TYPE_OSM:
5444                 rc = qeth_core_load_discipline(card, QETH_DISCIPLINE_LAYER2);
5445                 if (rc)
5446                         goto err_card;
5447                 rc = card->discipline->setup(card->gdev);
5448                 if (rc)
5449                         goto err_disc;
5450         case QETH_CARD_TYPE_OSD:
5451         case QETH_CARD_TYPE_OSX:
5452         default:
5453                 break;
5454         }
5455 
5456         write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5457         list_add_tail(&card->list, &qeth_core_card_list.list);
5458         write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5459 
5460         qeth_determine_capabilities(card);
5461         return 0;
5462 
5463 err_disc:
5464         qeth_core_free_discipline(card);
5465 err_card:
5466         qeth_core_free_card(card);
5467 err_dev:
5468         put_device(dev);
5469         return rc;
5470 }
5471 
5472 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
5473 {
5474         unsigned long flags;
5475         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5476 
5477         QETH_DBF_TEXT(SETUP, 2, "removedv");
5478 
5479         if (card->discipline) {
5480                 card->discipline->remove(gdev);
5481                 qeth_core_free_discipline(card);
5482         }
5483 
5484         write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5485         list_del(&card->list);
5486         write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5487         qeth_core_free_card(card);
5488         dev_set_drvdata(&gdev->dev, NULL);
5489         put_device(&gdev->dev);
5490         return;
5491 }
5492 
5493 static int qeth_core_set_online(struct ccwgroup_device *gdev)
5494 {
5495         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5496         int rc = 0;
5497         int def_discipline;
5498 
5499         if (!card->discipline) {
5500                 if (card->info.type == QETH_CARD_TYPE_IQD)
5501                         def_discipline = QETH_DISCIPLINE_LAYER3;
5502                 else
5503                         def_discipline = QETH_DISCIPLINE_LAYER2;
5504                 rc = qeth_core_load_discipline(card, def_discipline);
5505                 if (rc)
5506                         goto err;
5507                 rc = card->discipline->setup(card->gdev);
5508                 if (rc)
5509                         goto err;
5510         }
5511         rc = card->discipline->set_online(gdev);
5512 err:
5513         return rc;
5514 }
5515 
5516 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
5517 {
5518         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5519         return card->discipline->set_offline(gdev);
5520 }
5521 
5522 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
5523 {
5524         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5525         if (card->discipline && card->discipline->shutdown)
5526                 card->discipline->shutdown(gdev);
5527 }
5528 
5529 static int qeth_core_prepare(struct ccwgroup_device *gdev)
5530 {
5531         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5532         if (card->discipline && card->discipline->prepare)
5533                 return card->discipline->prepare(gdev);
5534         return 0;
5535 }
5536 
5537 static void qeth_core_complete(struct ccwgroup_device *gdev)
5538 {
5539         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5540         if (card->discipline && card->discipline->complete)
5541                 card->discipline->complete(gdev);
5542 }
5543 
5544 static int qeth_core_freeze(struct ccwgroup_device *gdev)
5545 {
5546         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5547         if (card->discipline && card->discipline->freeze)
5548                 return card->discipline->freeze(gdev);
5549         return 0;
5550 }
5551 
5552 static int qeth_core_thaw(struct ccwgroup_device *gdev)
5553 {
5554         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5555         if (card->discipline && card->discipline->thaw)
5556                 return card->discipline->thaw(gdev);
5557         return 0;
5558 }
5559 
5560 static int qeth_core_restore(struct ccwgroup_device *gdev)
5561 {
5562         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5563         if (card->discipline && card->discipline->restore)
5564                 return card->discipline->restore(gdev);
5565         return 0;
5566 }
5567 
5568 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
5569         .driver = {
5570                 .owner = THIS_MODULE,
5571                 .name = "qeth",
5572         },
5573         .setup = qeth_core_probe_device,
5574         .remove = qeth_core_remove_device,
5575         .set_online = qeth_core_set_online,
5576         .set_offline = qeth_core_set_offline,
5577         .shutdown = qeth_core_shutdown,
5578         .prepare = qeth_core_prepare,
5579         .complete = qeth_core_complete,
5580         .freeze = qeth_core_freeze,
5581         .thaw = qeth_core_thaw,
5582         .restore = qeth_core_restore,
5583 };
5584 
5585 static ssize_t qeth_core_driver_group_store(struct device_driver *ddrv,
5586                                             const char *buf, size_t count)
5587 {
5588         int err;
5589 
5590         err = ccwgroup_create_dev(qeth_core_root_dev,
5591                                   &qeth_core_ccwgroup_driver, 3, buf);
5592 
5593         return err ? err : count;
5594 }
5595 static DRIVER_ATTR(group, 0200, NULL, qeth_core_driver_group_store);
5596 
5597 static struct attribute *qeth_drv_attrs[] = {
5598         &driver_attr_group.attr,
5599         NULL,
5600 };
5601 static struct attribute_group qeth_drv_attr_group = {
5602         .attrs = qeth_drv_attrs,
5603 };
5604 static const struct attribute_group *qeth_drv_attr_groups[] = {
5605         &qeth_drv_attr_group,
5606         NULL,
5607 };
5608 
5609 static struct {
5610         const char str[ETH_GSTRING_LEN];
5611 } qeth_ethtool_stats_keys[] = {
5612 /*  0 */{"rx skbs"},
5613         {"rx buffers"},
5614         {"tx skbs"},
5615         {"tx buffers"},
5616         {"tx skbs no packing"},
5617         {"tx buffers no packing"},
5618         {"tx skbs packing"},
5619         {"tx buffers packing"},
5620         {"tx sg skbs"},
5621         {"tx sg frags"},
5622 /* 10 */{"rx sg skbs"},
5623         {"rx sg frags"},
5624         {"rx sg page allocs"},
5625         {"tx large kbytes"},
5626         {"tx large count"},
5627         {"tx pk state ch n->p"},
5628         {"tx pk state ch p->n"},
5629         {"tx pk watermark low"},
5630         {"tx pk watermark high"},
5631         {"queue 0 buffer usage"},
5632 /* 20 */{"queue 1 buffer usage"},
5633         {"queue 2 buffer usage"},
5634         {"queue 3 buffer usage"},
5635         {"rx poll time"},
5636         {"rx poll count"},
5637         {"rx do_QDIO time"},
5638         {"rx do_QDIO count"},
5639         {"tx handler time"},
5640         {"tx handler count"},
5641         {"tx time"},
5642 /* 30 */{"tx count"},
5643         {"tx do_QDIO time"},
5644         {"tx do_QDIO count"},
5645         {"tx csum"},
5646         {"tx lin"},
5647         {"cq handler count"},
5648         {"cq handler time"}
5649 };
5650 
5651 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
5652 {
5653         switch (stringset) {
5654         case ETH_SS_STATS:
5655                 return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
5656         default:
5657                 return -EINVAL;
5658         }
5659 }
5660 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
5661 
5662 void qeth_core_get_ethtool_stats(struct net_device *dev,
5663                 struct ethtool_stats *stats, u64 *data)
5664 {
5665         struct qeth_card *card = dev->ml_priv;
5666         data[0] = card->stats.rx_packets -
5667                                 card->perf_stats.initial_rx_packets;
5668         data[1] = card->perf_stats.bufs_rec;
5669         data[2] = card->stats.tx_packets -
5670                                 card->perf_stats.initial_tx_packets;
5671         data[3] = card->perf_stats.bufs_sent;
5672         data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
5673                         - card->perf_stats.skbs_sent_pack;
5674         data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
5675         data[6] = card->perf_stats.skbs_sent_pack;
5676         data[7] = card->perf_stats.bufs_sent_pack;
5677         data[8] = card->perf_stats.sg_skbs_sent;
5678         data[9] = card->perf_stats.sg_frags_sent;
5679         data[10] = card->perf_stats.sg_skbs_rx;
5680         data[11] = card->perf_stats.sg_frags_rx;
5681         data[12] = card->perf_stats.sg_alloc_page_rx;
5682         data[13] = (card->perf_stats.large_send_bytes >> 10);
5683         data[14] = card->perf_stats.large_send_cnt;
5684         data[15] = card->perf_stats.sc_dp_p;
5685         data[16] = card->perf_stats.sc_p_dp;
5686         data[17] = QETH_LOW_WATERMARK_PACK;
5687         data[18] = QETH_HIGH_WATERMARK_PACK;
5688         data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
5689         data[20] = (card->qdio.no_out_queues > 1) ?
5690                         atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
5691         data[21] = (card->qdio.no_out_queues > 2) ?
5692                         atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
5693         data[22] = (card->qdio.no_out_queues > 3) ?
5694                         atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
5695         data[23] = card->perf_stats.inbound_time;
5696         data[24] = card->perf_stats.inbound_cnt;
5697         data[25] = card->perf_stats.inbound_do_qdio_time;
5698         data[26] = card->perf_stats.inbound_do_qdio_cnt;
5699         data[27] = card->perf_stats.outbound_handler_time;
5700         data[28] = card->perf_stats.outbound_handler_cnt;
5701         data[29] = card->perf_stats.outbound_time;
5702         data[30] = card->perf_stats.outbound_cnt;
5703         data[31] = card->perf_stats.outbound_do_qdio_time;
5704         data[32] = card->perf_stats.outbound_do_qdio_cnt;
5705         data[33] = card->perf_stats.tx_csum;
5706         data[34] = card->perf_stats.tx_lin;
5707         data[35] = card->perf_stats.cq_cnt;
5708         data[36] = card->perf_stats.cq_time;
5709 }
5710 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
5711 
5712 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
5713 {
5714         switch (stringset) {
5715         case ETH_SS_STATS:
5716                 memcpy(data, &qeth_ethtool_stats_keys,
5717                         sizeof(qeth_ethtool_stats_keys));
5718                 break;
5719         default:
5720                 WARN_ON(1);
5721                 break;
5722         }
5723 }
5724 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
5725 
5726 void qeth_core_get_drvinfo(struct net_device *dev,
5727                 struct ethtool_drvinfo *info)
5728 {
5729         struct qeth_card *card = dev->ml_priv;
5730 
5731         strlcpy(info->driver, card->options.layer2 ? "qeth_l2" : "qeth_l3",
5732                 sizeof(info->driver));
5733         strlcpy(info->version, "1.0", sizeof(info->version));
5734         strlcpy(info->fw_version, card->info.mcl_level,
5735                 sizeof(info->fw_version));
5736         snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
5737                  CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
5738 }
5739 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
5740 
5741 /* Helper function to fill 'advertizing' and 'supported' which are the same. */
5742 /* Autoneg and full-duplex are supported and advertized uncondionally.       */
5743 /* Always advertize and support all speeds up to specified, and only one     */
5744 /* specified port type.                                                      */
5745 static void qeth_set_ecmd_adv_sup(struct ethtool_cmd *ecmd,
5746                                 int maxspeed, int porttype)
5747 {
5748         int port_sup, port_adv, spd_sup, spd_adv;
5749 
5750         switch (porttype) {
5751         case PORT_TP:
5752                 port_sup = SUPPORTED_TP;
5753                 port_adv = ADVERTISED_TP;
5754                 break;
5755         case PORT_FIBRE:
5756                 port_sup = SUPPORTED_FIBRE;
5757                 port_adv = ADVERTISED_FIBRE;
5758                 break;
5759         default:
5760                 port_sup = SUPPORTED_TP;
5761                 port_adv = ADVERTISED_TP;
5762                 WARN_ON_ONCE(1);
5763         }
5764 
5765         /* "Fallthrough" case'es ordered from high to low result in setting  */
5766         /* flags cumulatively, starting from the specified speed and down to */
5767         /* the lowest possible.                                              */
5768         spd_sup = 0;
5769         spd_adv = 0;
5770         switch (maxspeed) {
5771         case SPEED_10000:
5772                 spd_sup |= SUPPORTED_10000baseT_Full;
5773                 spd_adv |= ADVERTISED_10000baseT_Full;
5774         case SPEED_1000:
5775                 spd_sup |= SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
5776                 spd_adv |= ADVERTISED_1000baseT_Half |
5777                                                 ADVERTISED_1000baseT_Full;
5778         case SPEED_100:
5779                 spd_sup |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
5780                 spd_adv |= ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
5781         case SPEED_10:
5782                 spd_sup |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
5783                 spd_adv |= ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
5784         break;
5785         default:
5786                 spd_sup = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
5787                 spd_adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
5788                 WARN_ON_ONCE(1);
5789         }
5790         ecmd->advertising = ADVERTISED_Autoneg | port_adv | spd_adv;
5791         ecmd->supported = SUPPORTED_Autoneg | port_sup | spd_sup;
5792 }
5793 
5794 int qeth_core_ethtool_get_settings(struct net_device *netdev,
5795                                         struct ethtool_cmd *ecmd)
5796 {
5797         struct qeth_card *card = netdev->ml_priv;
5798         enum qeth_link_types link_type;
5799         struct carrier_info carrier_info;
5800         int rc;
5801         u32 speed;
5802 
5803         if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
5804                 link_type = QETH_LINK_TYPE_10GBIT_ETH;
5805         else
5806                 link_type = card->info.link_type;
5807 
5808         ecmd->transceiver = XCVR_INTERNAL;
5809         ecmd->duplex = DUPLEX_FULL;
5810         ecmd->autoneg = AUTONEG_ENABLE;
5811 
5812         switch (link_type) {
5813         case QETH_LINK_TYPE_FAST_ETH:
5814         case QETH_LINK_TYPE_LANE_ETH100:
5815                 qeth_set_ecmd_adv_sup(ecmd, SPEED_100, PORT_TP);
5816                 speed = SPEED_100;
5817                 ecmd->port = PORT_TP;
5818                 break;
5819 
5820         case QETH_LINK_TYPE_GBIT_ETH:
5821         case QETH_LINK_TYPE_LANE_ETH1000:
5822                 qeth_set_ecmd_adv_sup(ecmd, SPEED_1000, PORT_FIBRE);
5823                 speed = SPEED_1000;
5824                 ecmd->port = PORT_FIBRE;
5825                 break;
5826 
5827         case QETH_LINK_TYPE_10GBIT_ETH:
5828                 qeth_set_ecmd_adv_sup(ecmd, SPEED_10000, PORT_FIBRE);
5829                 speed = SPEED_10000;
5830                 ecmd->port = PORT_FIBRE;
5831                 break;
5832 
5833         default:
5834                 qeth_set_ecmd_adv_sup(ecmd, SPEED_10, PORT_TP);
5835                 speed = SPEED_10;
5836                 ecmd->port = PORT_TP;
5837         }
5838         ethtool_cmd_speed_set(ecmd, speed);
5839 
5840         /* Check if we can obtain more accurate information.     */
5841         /* If QUERY_CARD_INFO command is not supported or fails, */
5842         /* just return the heuristics that was filled above.     */
5843         if (!qeth_card_hw_is_reachable(card))
5844                 return -ENODEV;
5845         rc = qeth_query_card_info(card, &carrier_info);
5846         if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
5847                 return 0;
5848         if (rc) /* report error from the hardware operation */
5849                 return rc;
5850         /* on success, fill in the information got from the hardware */
5851 
5852         netdev_dbg(netdev,
5853         "card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
5854                         carrier_info.card_type,
5855                         carrier_info.port_mode,
5856                         carrier_info.port_speed);
5857 
5858         /* Update attributes for which we've obtained more authoritative */
5859         /* information, leave the rest the way they where filled above.  */
5860         switch (carrier_info.card_type) {
5861         case CARD_INFO_TYPE_1G_COPPER_A:
5862         case CARD_INFO_TYPE_1G_COPPER_B:
5863                 qeth_set_ecmd_adv_sup(ecmd, SPEED_1000, PORT_TP);
5864                 ecmd->port = PORT_TP;
5865                 break;
5866         case CARD_INFO_TYPE_1G_FIBRE_A:
5867         case CARD_INFO_TYPE_1G_FIBRE_B:
5868                 qeth_set_ecmd_adv_sup(ecmd, SPEED_1000, PORT_FIBRE);
5869                 ecmd->port = PORT_FIBRE;
5870                 break;
5871         case CARD_INFO_TYPE_10G_FIBRE_A:
5872         case CARD_INFO_TYPE_10G_FIBRE_B:
5873                 qeth_set_ecmd_adv_sup(ecmd, SPEED_10000, PORT_FIBRE);
5874                 ecmd->port = PORT_FIBRE;
5875                 break;
5876         }
5877 
5878         switch (carrier_info.port_mode) {
5879         case CARD_INFO_PORTM_FULLDUPLEX:
5880                 ecmd->duplex = DUPLEX_FULL;
5881                 break;
5882         case CARD_INFO_PORTM_HALFDUPLEX:
5883                 ecmd->duplex = DUPLEX_HALF;
5884                 break;
5885         }
5886 
5887         switch (carrier_info.port_speed) {
5888         case CARD_INFO_PORTS_10M:
5889                 speed = SPEED_10;
5890                 break;
5891         case CARD_INFO_PORTS_100M:
5892                 speed = SPEED_100;
5893                 break;
5894         case CARD_INFO_PORTS_1G:
5895                 speed = SPEED_1000;
5896                 break;
5897         case CARD_INFO_PORTS_10G:
5898                 speed = SPEED_10000;
5899                 break;
5900         }
5901         ethtool_cmd_speed_set(ecmd, speed);
5902 
5903         return 0;
5904 }
5905 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_settings);
5906 
5907 static int __init qeth_core_init(void)
5908 {
5909         int rc;
5910 
5911         pr_info("loading core functions\n");
5912         INIT_LIST_HEAD(&qeth_core_card_list.list);
5913         INIT_LIST_HEAD(&qeth_dbf_list);
5914         rwlock_init(&qeth_core_card_list.rwlock);
5915         mutex_init(&qeth_mod_mutex);
5916 
5917         qeth_wq = create_singlethread_workqueue("qeth_wq");
5918 
5919         rc = qeth_register_dbf_views();
5920         if (rc)
5921                 goto out_err;
5922         qeth_core_root_dev = root_device_register("qeth");
5923         rc = PTR_ERR_OR_ZERO(qeth_core_root_dev);
5924         if (rc)
5925                 goto register_err;
5926         qeth_core_header_cache = kmem_cache_create("qeth_hdr",
5927                         sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
5928         if (!qeth_core_header_cache) {
5929                 rc = -ENOMEM;
5930                 goto slab_err;
5931         }
5932         qeth_qdio_outbuf_cache = kmem_cache_create("qeth_buf",
5933                         sizeof(struct qeth_qdio_out_buffer), 0, 0, NULL);
5934         if (!qeth_qdio_outbuf_cache) {
5935                 rc = -ENOMEM;
5936                 goto cqslab_err;
5937         }
5938         rc = ccw_driver_register(&qeth_ccw_driver);
5939         if (rc)
5940                 goto ccw_err;
5941         qeth_core_ccwgroup_driver.driver.groups = qeth_drv_attr_groups;
5942         rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
5943         if (rc)
5944                 goto ccwgroup_err;
5945 
5946         return 0;
5947 
5948 ccwgroup_err:
5949         ccw_driver_unregister(&qeth_ccw_driver);
5950 ccw_err:
5951         kmem_cache_destroy(qeth_qdio_outbuf_cache);
5952 cqslab_err:
5953         kmem_cache_destroy(qeth_core_header_cache);
5954 slab_err:
5955         root_device_unregister(qeth_core_root_dev);
5956 register_err:
5957         qeth_unregister_dbf_views();
5958 out_err:
5959         pr_err("Initializing the qeth device driver failed\n");
5960         return rc;
5961 }
5962 
5963 static void __exit qeth_core_exit(void)
5964 {
5965         qeth_clear_dbf_list();
5966         destroy_workqueue(qeth_wq);
5967         ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
5968         ccw_driver_unregister(&qeth_ccw_driver);
5969         kmem_cache_destroy(qeth_qdio_outbuf_cache);
5970         kmem_cache_destroy(qeth_core_header_cache);
5971         root_device_unregister(qeth_core_root_dev);
5972         qeth_unregister_dbf_views();
5973         pr_info("core functions removed\n");
5974 }
5975 
5976 module_init(qeth_core_init);
5977 module_exit(qeth_core_exit);
5978 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
5979 MODULE_DESCRIPTION("qeth core functions");
5980 MODULE_LICENSE("GPL");
5981 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us