Version:  2.0.40 2.2.26 2.4.37 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

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

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