Linux 4.18.10
[linux/fpc-iii.git] / drivers / s390 / net / qeth_core_main.c
blobffdb78421a250a411c5c6e56fd6a4aeffde5b140
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2007, 2009
4 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5 * Frank Pavlic <fpavlic@de.ibm.com>,
6 * Thomas Spatzier <tspat@de.ibm.com>,
7 * Frank Blaschka <frank.blaschka@de.ibm.com>
8 */
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/compat.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/ip.h>
20 #include <linux/tcp.h>
21 #include <linux/mii.h>
22 #include <linux/kthread.h>
23 #include <linux/slab.h>
24 #include <linux/if_vlan.h>
25 #include <linux/netdevice.h>
26 #include <linux/netdev_features.h>
27 #include <linux/skbuff.h>
29 #include <net/iucv/af_iucv.h>
30 #include <net/dsfield.h>
32 #include <asm/ebcdic.h>
33 #include <asm/chpid.h>
34 #include <asm/io.h>
35 #include <asm/sysinfo.h>
36 #include <asm/diag.h>
37 #include <asm/cio.h>
38 #include <asm/ccwdev.h>
39 #include <asm/cpcmd.h>
41 #include "qeth_core.h"
43 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
44 /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
45 /* N P A M L V H */
46 [QETH_DBF_SETUP] = {"qeth_setup",
47 8, 1, 8, 5, &debug_hex_ascii_view, NULL},
48 [QETH_DBF_MSG] = {"qeth_msg", 8, 1, 11 * sizeof(long), 3,
49 &debug_sprintf_view, NULL},
50 [QETH_DBF_CTRL] = {"qeth_control",
51 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
53 EXPORT_SYMBOL_GPL(qeth_dbf);
55 struct qeth_card_list_struct qeth_core_card_list;
56 EXPORT_SYMBOL_GPL(qeth_core_card_list);
57 struct kmem_cache *qeth_core_header_cache;
58 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
59 static struct kmem_cache *qeth_qdio_outbuf_cache;
61 static struct device *qeth_core_root_dev;
62 static struct lock_class_key qdio_out_skb_queue_key;
63 static struct mutex qeth_mod_mutex;
65 static void qeth_send_control_data_cb(struct qeth_channel *,
66 struct qeth_cmd_buffer *);
67 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
68 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
69 static void qeth_free_buffer_pool(struct qeth_card *);
70 static int qeth_qdio_establish(struct qeth_card *);
71 static void qeth_free_qdio_buffers(struct qeth_card *);
72 static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
73 struct qeth_qdio_out_buffer *buf,
74 enum iucv_tx_notify notification);
75 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf);
76 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *, int);
78 struct workqueue_struct *qeth_wq;
79 EXPORT_SYMBOL_GPL(qeth_wq);
81 int qeth_card_hw_is_reachable(struct qeth_card *card)
83 return (card->state == CARD_STATE_SOFTSETUP) ||
84 (card->state == CARD_STATE_UP);
86 EXPORT_SYMBOL_GPL(qeth_card_hw_is_reachable);
88 static void qeth_close_dev_handler(struct work_struct *work)
90 struct qeth_card *card;
92 card = container_of(work, struct qeth_card, close_dev_work);
93 QETH_CARD_TEXT(card, 2, "cldevhdl");
94 rtnl_lock();
95 dev_close(card->dev);
96 rtnl_unlock();
97 ccwgroup_set_offline(card->gdev);
100 void qeth_close_dev(struct qeth_card *card)
102 QETH_CARD_TEXT(card, 2, "cldevsubm");
103 queue_work(qeth_wq, &card->close_dev_work);
105 EXPORT_SYMBOL_GPL(qeth_close_dev);
107 static const char *qeth_get_cardname(struct qeth_card *card)
109 if (card->info.guestlan) {
110 switch (card->info.type) {
111 case QETH_CARD_TYPE_OSD:
112 return " Virtual NIC QDIO";
113 case QETH_CARD_TYPE_IQD:
114 return " Virtual NIC Hiper";
115 case QETH_CARD_TYPE_OSM:
116 return " Virtual NIC QDIO - OSM";
117 case QETH_CARD_TYPE_OSX:
118 return " Virtual NIC QDIO - OSX";
119 default:
120 return " unknown";
122 } else {
123 switch (card->info.type) {
124 case QETH_CARD_TYPE_OSD:
125 return " OSD Express";
126 case QETH_CARD_TYPE_IQD:
127 return " HiperSockets";
128 case QETH_CARD_TYPE_OSN:
129 return " OSN QDIO";
130 case QETH_CARD_TYPE_OSM:
131 return " OSM QDIO";
132 case QETH_CARD_TYPE_OSX:
133 return " OSX QDIO";
134 default:
135 return " unknown";
138 return " n/a";
141 /* max length to be returned: 14 */
142 const char *qeth_get_cardname_short(struct qeth_card *card)
144 if (card->info.guestlan) {
145 switch (card->info.type) {
146 case QETH_CARD_TYPE_OSD:
147 return "Virt.NIC QDIO";
148 case QETH_CARD_TYPE_IQD:
149 return "Virt.NIC Hiper";
150 case QETH_CARD_TYPE_OSM:
151 return "Virt.NIC OSM";
152 case QETH_CARD_TYPE_OSX:
153 return "Virt.NIC OSX";
154 default:
155 return "unknown";
157 } else {
158 switch (card->info.type) {
159 case QETH_CARD_TYPE_OSD:
160 switch (card->info.link_type) {
161 case QETH_LINK_TYPE_FAST_ETH:
162 return "OSD_100";
163 case QETH_LINK_TYPE_HSTR:
164 return "HSTR";
165 case QETH_LINK_TYPE_GBIT_ETH:
166 return "OSD_1000";
167 case QETH_LINK_TYPE_10GBIT_ETH:
168 return "OSD_10GIG";
169 case QETH_LINK_TYPE_LANE_ETH100:
170 return "OSD_FE_LANE";
171 case QETH_LINK_TYPE_LANE_TR:
172 return "OSD_TR_LANE";
173 case QETH_LINK_TYPE_LANE_ETH1000:
174 return "OSD_GbE_LANE";
175 case QETH_LINK_TYPE_LANE:
176 return "OSD_ATM_LANE";
177 default:
178 return "OSD_Express";
180 case QETH_CARD_TYPE_IQD:
181 return "HiperSockets";
182 case QETH_CARD_TYPE_OSN:
183 return "OSN";
184 case QETH_CARD_TYPE_OSM:
185 return "OSM_1000";
186 case QETH_CARD_TYPE_OSX:
187 return "OSX_10GIG";
188 default:
189 return "unknown";
192 return "n/a";
195 void qeth_set_recovery_task(struct qeth_card *card)
197 card->recovery_task = current;
199 EXPORT_SYMBOL_GPL(qeth_set_recovery_task);
201 void qeth_clear_recovery_task(struct qeth_card *card)
203 card->recovery_task = NULL;
205 EXPORT_SYMBOL_GPL(qeth_clear_recovery_task);
207 static bool qeth_is_recovery_task(const struct qeth_card *card)
209 return card->recovery_task == current;
212 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
213 int clear_start_mask)
215 unsigned long flags;
217 spin_lock_irqsave(&card->thread_mask_lock, flags);
218 card->thread_allowed_mask = threads;
219 if (clear_start_mask)
220 card->thread_start_mask &= threads;
221 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
222 wake_up(&card->wait_q);
224 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
226 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
228 unsigned long flags;
229 int rc = 0;
231 spin_lock_irqsave(&card->thread_mask_lock, flags);
232 rc = (card->thread_running_mask & threads);
233 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
234 return rc;
236 EXPORT_SYMBOL_GPL(qeth_threads_running);
238 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
240 if (qeth_is_recovery_task(card))
241 return 0;
242 return wait_event_interruptible(card->wait_q,
243 qeth_threads_running(card, threads) == 0);
245 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
247 void qeth_clear_working_pool_list(struct qeth_card *card)
249 struct qeth_buffer_pool_entry *pool_entry, *tmp;
251 QETH_CARD_TEXT(card, 5, "clwrklst");
252 list_for_each_entry_safe(pool_entry, tmp,
253 &card->qdio.in_buf_pool.entry_list, list){
254 list_del(&pool_entry->list);
257 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
259 static int qeth_alloc_buffer_pool(struct qeth_card *card)
261 struct qeth_buffer_pool_entry *pool_entry;
262 void *ptr;
263 int i, j;
265 QETH_CARD_TEXT(card, 5, "alocpool");
266 for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
267 pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
268 if (!pool_entry) {
269 qeth_free_buffer_pool(card);
270 return -ENOMEM;
272 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
273 ptr = (void *) __get_free_page(GFP_KERNEL);
274 if (!ptr) {
275 while (j > 0)
276 free_page((unsigned long)
277 pool_entry->elements[--j]);
278 kfree(pool_entry);
279 qeth_free_buffer_pool(card);
280 return -ENOMEM;
282 pool_entry->elements[j] = ptr;
284 list_add(&pool_entry->init_list,
285 &card->qdio.init_pool.entry_list);
287 return 0;
290 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
292 QETH_CARD_TEXT(card, 2, "realcbp");
294 if ((card->state != CARD_STATE_DOWN) &&
295 (card->state != CARD_STATE_RECOVER))
296 return -EPERM;
298 /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
299 qeth_clear_working_pool_list(card);
300 qeth_free_buffer_pool(card);
301 card->qdio.in_buf_pool.buf_count = bufcnt;
302 card->qdio.init_pool.buf_count = bufcnt;
303 return qeth_alloc_buffer_pool(card);
305 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
307 static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
309 if (!q)
310 return;
312 qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
313 kfree(q);
316 static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
318 struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
319 int i;
321 if (!q)
322 return NULL;
324 if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
325 kfree(q);
326 return NULL;
329 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
330 q->bufs[i].buffer = q->qdio_bufs[i];
332 QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
333 return q;
336 static int qeth_cq_init(struct qeth_card *card)
338 int rc;
340 if (card->options.cq == QETH_CQ_ENABLED) {
341 QETH_DBF_TEXT(SETUP, 2, "cqinit");
342 qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
343 QDIO_MAX_BUFFERS_PER_Q);
344 card->qdio.c_q->next_buf_to_init = 127;
345 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
346 card->qdio.no_in_queues - 1, 0,
347 127);
348 if (rc) {
349 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
350 goto out;
353 rc = 0;
354 out:
355 return rc;
358 static int qeth_alloc_cq(struct qeth_card *card)
360 int rc;
362 if (card->options.cq == QETH_CQ_ENABLED) {
363 int i;
364 struct qdio_outbuf_state *outbuf_states;
366 QETH_DBF_TEXT(SETUP, 2, "cqon");
367 card->qdio.c_q = qeth_alloc_qdio_queue();
368 if (!card->qdio.c_q) {
369 rc = -1;
370 goto kmsg_out;
372 card->qdio.no_in_queues = 2;
373 card->qdio.out_bufstates =
374 kcalloc(card->qdio.no_out_queues *
375 QDIO_MAX_BUFFERS_PER_Q,
376 sizeof(struct qdio_outbuf_state),
377 GFP_KERNEL);
378 outbuf_states = card->qdio.out_bufstates;
379 if (outbuf_states == NULL) {
380 rc = -1;
381 goto free_cq_out;
383 for (i = 0; i < card->qdio.no_out_queues; ++i) {
384 card->qdio.out_qs[i]->bufstates = outbuf_states;
385 outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
387 } else {
388 QETH_DBF_TEXT(SETUP, 2, "nocq");
389 card->qdio.c_q = NULL;
390 card->qdio.no_in_queues = 1;
392 QETH_DBF_TEXT_(SETUP, 2, "iqc%d", card->qdio.no_in_queues);
393 rc = 0;
394 out:
395 return rc;
396 free_cq_out:
397 qeth_free_qdio_queue(card->qdio.c_q);
398 card->qdio.c_q = NULL;
399 kmsg_out:
400 dev_err(&card->gdev->dev, "Failed to create completion queue\n");
401 goto out;
404 static void qeth_free_cq(struct qeth_card *card)
406 if (card->qdio.c_q) {
407 --card->qdio.no_in_queues;
408 qeth_free_qdio_queue(card->qdio.c_q);
409 card->qdio.c_q = NULL;
411 kfree(card->qdio.out_bufstates);
412 card->qdio.out_bufstates = NULL;
415 static enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
416 int delayed)
418 enum iucv_tx_notify n;
420 switch (sbalf15) {
421 case 0:
422 n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
423 break;
424 case 4:
425 case 16:
426 case 17:
427 case 18:
428 n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
429 TX_NOTIFY_UNREACHABLE;
430 break;
431 default:
432 n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
433 TX_NOTIFY_GENERALERROR;
434 break;
437 return n;
440 static void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q, int bidx,
441 int forced_cleanup)
443 if (q->card->options.cq != QETH_CQ_ENABLED)
444 return;
446 if (q->bufs[bidx]->next_pending != NULL) {
447 struct qeth_qdio_out_buffer *head = q->bufs[bidx];
448 struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
450 while (c) {
451 if (forced_cleanup ||
452 atomic_read(&c->state) ==
453 QETH_QDIO_BUF_HANDLED_DELAYED) {
454 struct qeth_qdio_out_buffer *f = c;
455 QETH_CARD_TEXT(f->q->card, 5, "fp");
456 QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
457 /* release here to avoid interleaving between
458 outbound tasklet and inbound tasklet
459 regarding notifications and lifecycle */
460 qeth_release_skbs(c);
462 c = f->next_pending;
463 WARN_ON_ONCE(head->next_pending != f);
464 head->next_pending = c;
465 kmem_cache_free(qeth_qdio_outbuf_cache, f);
466 } else {
467 head = c;
468 c = c->next_pending;
473 if (forced_cleanup && (atomic_read(&(q->bufs[bidx]->state)) ==
474 QETH_QDIO_BUF_HANDLED_DELAYED)) {
475 /* for recovery situations */
476 q->bufs[bidx]->aob = q->bufstates[bidx].aob;
477 qeth_init_qdio_out_buf(q, bidx);
478 QETH_CARD_TEXT(q->card, 2, "clprecov");
483 static void qeth_qdio_handle_aob(struct qeth_card *card,
484 unsigned long phys_aob_addr)
486 struct qaob *aob;
487 struct qeth_qdio_out_buffer *buffer;
488 enum iucv_tx_notify notification;
489 unsigned int i;
491 aob = (struct qaob *) phys_to_virt(phys_aob_addr);
492 QETH_CARD_TEXT(card, 5, "haob");
493 QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
494 buffer = (struct qeth_qdio_out_buffer *) aob->user1;
495 QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
497 if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
498 QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
499 notification = TX_NOTIFY_OK;
500 } else {
501 WARN_ON_ONCE(atomic_read(&buffer->state) !=
502 QETH_QDIO_BUF_PENDING);
503 atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
504 notification = TX_NOTIFY_DELAYED_OK;
507 if (aob->aorc != 0) {
508 QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
509 notification = qeth_compute_cq_notification(aob->aorc, 1);
511 qeth_notify_skbs(buffer->q, buffer, notification);
513 buffer->aob = NULL;
514 /* Free dangling allocations. The attached skbs are handled by
515 * qeth_cleanup_handled_pending().
517 for (i = 0;
518 i < aob->sb_count && i < QETH_MAX_BUFFER_ELEMENTS(card);
519 i++) {
520 if (aob->sba[i] && buffer->is_header[i])
521 kmem_cache_free(qeth_core_header_cache,
522 (void *) aob->sba[i]);
524 atomic_set(&buffer->state, QETH_QDIO_BUF_HANDLED_DELAYED);
526 qdio_release_aob(aob);
529 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
531 return card->options.cq == QETH_CQ_ENABLED &&
532 card->qdio.c_q != NULL &&
533 queue != 0 &&
534 queue == card->qdio.no_in_queues - 1;
537 static int __qeth_issue_next_read(struct qeth_card *card)
539 int rc;
540 struct qeth_cmd_buffer *iob;
542 QETH_CARD_TEXT(card, 5, "issnxrd");
543 if (card->read.state != CH_STATE_UP)
544 return -EIO;
545 iob = qeth_get_buffer(&card->read);
546 if (!iob) {
547 dev_warn(&card->gdev->dev, "The qeth device driver "
548 "failed to recover an error on the device\n");
549 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
550 "available\n", dev_name(&card->gdev->dev));
551 return -ENOMEM;
553 qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
554 QETH_CARD_TEXT(card, 6, "noirqpnd");
555 rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
556 (addr_t) iob, 0, 0);
557 if (rc) {
558 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
559 "rc=%i\n", dev_name(&card->gdev->dev), rc);
560 atomic_set(&card->read.irq_pending, 0);
561 card->read_or_write_problem = 1;
562 qeth_schedule_recovery(card);
563 wake_up(&card->wait_q);
565 return rc;
568 static int qeth_issue_next_read(struct qeth_card *card)
570 int ret;
572 spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
573 ret = __qeth_issue_next_read(card);
574 spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
576 return ret;
579 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
581 struct qeth_reply *reply;
583 reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
584 if (reply) {
585 refcount_set(&reply->refcnt, 1);
586 atomic_set(&reply->received, 0);
587 reply->card = card;
589 return reply;
592 static void qeth_get_reply(struct qeth_reply *reply)
594 refcount_inc(&reply->refcnt);
597 static void qeth_put_reply(struct qeth_reply *reply)
599 if (refcount_dec_and_test(&reply->refcnt))
600 kfree(reply);
603 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
604 struct qeth_card *card)
606 char *ipa_name;
607 int com = cmd->hdr.command;
608 ipa_name = qeth_get_ipa_cmd_name(com);
609 if (rc)
610 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
611 "x%X \"%s\"\n",
612 ipa_name, com, dev_name(&card->gdev->dev),
613 QETH_CARD_IFNAME(card), rc,
614 qeth_get_ipa_msg(rc));
615 else
616 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
617 ipa_name, com, dev_name(&card->gdev->dev),
618 QETH_CARD_IFNAME(card));
621 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
622 struct qeth_cmd_buffer *iob)
624 struct qeth_ipa_cmd *cmd = NULL;
626 QETH_CARD_TEXT(card, 5, "chkipad");
627 if (IS_IPA(iob->data)) {
628 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
629 if (IS_IPA_REPLY(cmd)) {
630 if (cmd->hdr.command != IPA_CMD_SETCCID &&
631 cmd->hdr.command != IPA_CMD_DELCCID &&
632 cmd->hdr.command != IPA_CMD_MODCCID &&
633 cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
634 qeth_issue_ipa_msg(cmd,
635 cmd->hdr.return_code, card);
636 return cmd;
637 } else {
638 switch (cmd->hdr.command) {
639 case IPA_CMD_STOPLAN:
640 if (cmd->hdr.return_code ==
641 IPA_RC_VEPA_TO_VEB_TRANSITION) {
642 dev_err(&card->gdev->dev,
643 "Interface %s is down because the "
644 "adjacent port is no longer in "
645 "reflective relay mode\n",
646 QETH_CARD_IFNAME(card));
647 qeth_close_dev(card);
648 } else {
649 dev_warn(&card->gdev->dev,
650 "The link for interface %s on CHPID"
651 " 0x%X failed\n",
652 QETH_CARD_IFNAME(card),
653 card->info.chpid);
654 qeth_issue_ipa_msg(cmd,
655 cmd->hdr.return_code, card);
657 card->lan_online = 0;
658 if (card->dev && netif_carrier_ok(card->dev))
659 netif_carrier_off(card->dev);
660 return NULL;
661 case IPA_CMD_STARTLAN:
662 dev_info(&card->gdev->dev,
663 "The link for %s on CHPID 0x%X has"
664 " been restored\n",
665 QETH_CARD_IFNAME(card),
666 card->info.chpid);
667 netif_carrier_on(card->dev);
668 card->lan_online = 1;
669 if (card->info.hwtrap)
670 card->info.hwtrap = 2;
671 qeth_schedule_recovery(card);
672 return NULL;
673 case IPA_CMD_SETBRIDGEPORT_IQD:
674 case IPA_CMD_SETBRIDGEPORT_OSA:
675 case IPA_CMD_ADDRESS_CHANGE_NOTIF:
676 if (card->discipline->control_event_handler
677 (card, cmd))
678 return cmd;
679 else
680 return NULL;
681 case IPA_CMD_MODCCID:
682 return cmd;
683 case IPA_CMD_REGISTER_LOCAL_ADDR:
684 QETH_CARD_TEXT(card, 3, "irla");
685 break;
686 case IPA_CMD_UNREGISTER_LOCAL_ADDR:
687 QETH_CARD_TEXT(card, 3, "urla");
688 break;
689 default:
690 QETH_DBF_MESSAGE(2, "Received data is IPA "
691 "but not a reply!\n");
692 break;
696 return cmd;
699 void qeth_clear_ipacmd_list(struct qeth_card *card)
701 struct qeth_reply *reply, *r;
702 unsigned long flags;
704 QETH_CARD_TEXT(card, 4, "clipalst");
706 spin_lock_irqsave(&card->lock, flags);
707 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
708 qeth_get_reply(reply);
709 reply->rc = -EIO;
710 atomic_inc(&reply->received);
711 list_del_init(&reply->list);
712 wake_up(&reply->wait_q);
713 qeth_put_reply(reply);
715 spin_unlock_irqrestore(&card->lock, flags);
717 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
719 static int qeth_check_idx_response(struct qeth_card *card,
720 unsigned char *buffer)
722 if (!buffer)
723 return 0;
725 QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
726 if ((buffer[2] & 0xc0) == 0xc0) {
727 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE with cause code %#02x\n",
728 buffer[4]);
729 QETH_CARD_TEXT(card, 2, "ckidxres");
730 QETH_CARD_TEXT(card, 2, " idxterm");
731 QETH_CARD_TEXT_(card, 2, " rc%d", -EIO);
732 if (buffer[4] == 0xf6) {
733 dev_err(&card->gdev->dev,
734 "The qeth device is not configured "
735 "for the OSI layer required by z/VM\n");
736 return -EPERM;
738 return -EIO;
740 return 0;
743 static struct qeth_card *CARD_FROM_CDEV(struct ccw_device *cdev)
745 struct qeth_card *card = dev_get_drvdata(&((struct ccwgroup_device *)
746 dev_get_drvdata(&cdev->dev))->dev);
747 return card;
750 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
751 __u32 len)
753 struct qeth_card *card;
755 card = CARD_FROM_CDEV(channel->ccwdev);
756 QETH_CARD_TEXT(card, 4, "setupccw");
757 if (channel == &card->read)
758 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
759 else
760 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
761 channel->ccw.count = len;
762 channel->ccw.cda = (__u32) __pa(iob);
765 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
767 __u8 index;
769 QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
770 index = channel->io_buf_no;
771 do {
772 if (channel->iob[index].state == BUF_STATE_FREE) {
773 channel->iob[index].state = BUF_STATE_LOCKED;
774 channel->io_buf_no = (channel->io_buf_no + 1) %
775 QETH_CMD_BUFFER_NO;
776 memset(channel->iob[index].data, 0, QETH_BUFSIZE);
777 return channel->iob + index;
779 index = (index + 1) % QETH_CMD_BUFFER_NO;
780 } while (index != channel->io_buf_no);
782 return NULL;
785 void qeth_release_buffer(struct qeth_channel *channel,
786 struct qeth_cmd_buffer *iob)
788 unsigned long flags;
790 QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
791 spin_lock_irqsave(&channel->iob_lock, flags);
792 memset(iob->data, 0, QETH_BUFSIZE);
793 iob->state = BUF_STATE_FREE;
794 iob->callback = qeth_send_control_data_cb;
795 iob->rc = 0;
796 spin_unlock_irqrestore(&channel->iob_lock, flags);
797 wake_up(&channel->wait_q);
799 EXPORT_SYMBOL_GPL(qeth_release_buffer);
801 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
803 struct qeth_cmd_buffer *buffer = NULL;
804 unsigned long flags;
806 spin_lock_irqsave(&channel->iob_lock, flags);
807 buffer = __qeth_get_buffer(channel);
808 spin_unlock_irqrestore(&channel->iob_lock, flags);
809 return buffer;
812 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
814 struct qeth_cmd_buffer *buffer;
815 wait_event(channel->wait_q,
816 ((buffer = qeth_get_buffer(channel)) != NULL));
817 return buffer;
819 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
821 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
823 int cnt;
825 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
826 qeth_release_buffer(channel, &channel->iob[cnt]);
827 channel->io_buf_no = 0;
829 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
831 static void qeth_send_control_data_cb(struct qeth_channel *channel,
832 struct qeth_cmd_buffer *iob)
834 struct qeth_card *card;
835 struct qeth_reply *reply, *r;
836 struct qeth_ipa_cmd *cmd;
837 unsigned long flags;
838 int keep_reply;
839 int rc = 0;
841 card = CARD_FROM_CDEV(channel->ccwdev);
842 QETH_CARD_TEXT(card, 4, "sndctlcb");
843 rc = qeth_check_idx_response(card, iob->data);
844 switch (rc) {
845 case 0:
846 break;
847 case -EIO:
848 qeth_clear_ipacmd_list(card);
849 qeth_schedule_recovery(card);
850 /* fall through */
851 default:
852 goto out;
855 cmd = qeth_check_ipa_data(card, iob);
856 if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
857 goto out;
858 /*in case of OSN : check if cmd is set */
859 if (card->info.type == QETH_CARD_TYPE_OSN &&
860 cmd &&
861 cmd->hdr.command != IPA_CMD_STARTLAN &&
862 card->osn_info.assist_cb != NULL) {
863 card->osn_info.assist_cb(card->dev, cmd);
864 goto out;
867 spin_lock_irqsave(&card->lock, flags);
868 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
869 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
870 ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
871 qeth_get_reply(reply);
872 list_del_init(&reply->list);
873 spin_unlock_irqrestore(&card->lock, flags);
874 keep_reply = 0;
875 if (reply->callback != NULL) {
876 if (cmd) {
877 reply->offset = (__u16)((char *)cmd -
878 (char *)iob->data);
879 keep_reply = reply->callback(card,
880 reply,
881 (unsigned long)cmd);
882 } else
883 keep_reply = reply->callback(card,
884 reply,
885 (unsigned long)iob);
887 if (cmd)
888 reply->rc = (u16) cmd->hdr.return_code;
889 else if (iob->rc)
890 reply->rc = iob->rc;
891 if (keep_reply) {
892 spin_lock_irqsave(&card->lock, flags);
893 list_add_tail(&reply->list,
894 &card->cmd_waiter_list);
895 spin_unlock_irqrestore(&card->lock, flags);
896 } else {
897 atomic_inc(&reply->received);
898 wake_up(&reply->wait_q);
900 qeth_put_reply(reply);
901 goto out;
904 spin_unlock_irqrestore(&card->lock, flags);
905 out:
906 memcpy(&card->seqno.pdu_hdr_ack,
907 QETH_PDU_HEADER_SEQ_NO(iob->data),
908 QETH_SEQ_NO_LENGTH);
909 qeth_release_buffer(channel, iob);
912 static int qeth_setup_channel(struct qeth_channel *channel)
914 int cnt;
916 QETH_DBF_TEXT(SETUP, 2, "setupch");
917 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
918 channel->iob[cnt].data =
919 kzalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
920 if (channel->iob[cnt].data == NULL)
921 break;
922 channel->iob[cnt].state = BUF_STATE_FREE;
923 channel->iob[cnt].channel = channel;
924 channel->iob[cnt].callback = qeth_send_control_data_cb;
925 channel->iob[cnt].rc = 0;
927 if (cnt < QETH_CMD_BUFFER_NO) {
928 while (cnt-- > 0)
929 kfree(channel->iob[cnt].data);
930 return -ENOMEM;
932 channel->io_buf_no = 0;
933 atomic_set(&channel->irq_pending, 0);
934 spin_lock_init(&channel->iob_lock);
936 init_waitqueue_head(&channel->wait_q);
937 return 0;
940 static int qeth_set_thread_start_bit(struct qeth_card *card,
941 unsigned long thread)
943 unsigned long flags;
945 spin_lock_irqsave(&card->thread_mask_lock, flags);
946 if (!(card->thread_allowed_mask & thread) ||
947 (card->thread_start_mask & thread)) {
948 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
949 return -EPERM;
951 card->thread_start_mask |= thread;
952 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
953 return 0;
956 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
958 unsigned long flags;
960 spin_lock_irqsave(&card->thread_mask_lock, flags);
961 card->thread_start_mask &= ~thread;
962 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
963 wake_up(&card->wait_q);
965 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
967 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
969 unsigned long flags;
971 spin_lock_irqsave(&card->thread_mask_lock, flags);
972 card->thread_running_mask &= ~thread;
973 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
974 wake_up_all(&card->wait_q);
976 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
978 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
980 unsigned long flags;
981 int rc = 0;
983 spin_lock_irqsave(&card->thread_mask_lock, flags);
984 if (card->thread_start_mask & thread) {
985 if ((card->thread_allowed_mask & thread) &&
986 !(card->thread_running_mask & thread)) {
987 rc = 1;
988 card->thread_start_mask &= ~thread;
989 card->thread_running_mask |= thread;
990 } else
991 rc = -EPERM;
993 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
994 return rc;
997 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
999 int rc = 0;
1001 wait_event(card->wait_q,
1002 (rc = __qeth_do_run_thread(card, thread)) >= 0);
1003 return rc;
1005 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
1007 void qeth_schedule_recovery(struct qeth_card *card)
1009 QETH_CARD_TEXT(card, 2, "startrec");
1010 if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
1011 schedule_work(&card->kernel_thread_starter);
1013 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
1015 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
1017 int dstat, cstat;
1018 char *sense;
1019 struct qeth_card *card;
1021 sense = (char *) irb->ecw;
1022 cstat = irb->scsw.cmd.cstat;
1023 dstat = irb->scsw.cmd.dstat;
1024 card = CARD_FROM_CDEV(cdev);
1026 if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1027 SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1028 SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1029 QETH_CARD_TEXT(card, 2, "CGENCHK");
1030 dev_warn(&cdev->dev, "The qeth device driver "
1031 "failed to recover an error on the device\n");
1032 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
1033 dev_name(&cdev->dev), dstat, cstat);
1034 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
1035 16, 1, irb, 64, 1);
1036 return 1;
1039 if (dstat & DEV_STAT_UNIT_CHECK) {
1040 if (sense[SENSE_RESETTING_EVENT_BYTE] &
1041 SENSE_RESETTING_EVENT_FLAG) {
1042 QETH_CARD_TEXT(card, 2, "REVIND");
1043 return 1;
1045 if (sense[SENSE_COMMAND_REJECT_BYTE] &
1046 SENSE_COMMAND_REJECT_FLAG) {
1047 QETH_CARD_TEXT(card, 2, "CMDREJi");
1048 return 1;
1050 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
1051 QETH_CARD_TEXT(card, 2, "AFFE");
1052 return 1;
1054 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
1055 QETH_CARD_TEXT(card, 2, "ZEROSEN");
1056 return 0;
1058 QETH_CARD_TEXT(card, 2, "DGENCHK");
1059 return 1;
1061 return 0;
1064 static long __qeth_check_irb_error(struct ccw_device *cdev,
1065 unsigned long intparm, struct irb *irb)
1067 struct qeth_card *card;
1069 card = CARD_FROM_CDEV(cdev);
1071 if (!card || !IS_ERR(irb))
1072 return 0;
1074 switch (PTR_ERR(irb)) {
1075 case -EIO:
1076 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
1077 dev_name(&cdev->dev));
1078 QETH_CARD_TEXT(card, 2, "ckirberr");
1079 QETH_CARD_TEXT_(card, 2, " rc%d", -EIO);
1080 break;
1081 case -ETIMEDOUT:
1082 dev_warn(&cdev->dev, "A hardware operation timed out"
1083 " on the device\n");
1084 QETH_CARD_TEXT(card, 2, "ckirberr");
1085 QETH_CARD_TEXT_(card, 2, " rc%d", -ETIMEDOUT);
1086 if (intparm == QETH_RCD_PARM) {
1087 if (card->data.ccwdev == cdev) {
1088 card->data.state = CH_STATE_DOWN;
1089 wake_up(&card->wait_q);
1092 break;
1093 default:
1094 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
1095 dev_name(&cdev->dev), PTR_ERR(irb));
1096 QETH_CARD_TEXT(card, 2, "ckirberr");
1097 QETH_CARD_TEXT(card, 2, " rc???");
1099 return PTR_ERR(irb);
1102 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
1103 struct irb *irb)
1105 int rc;
1106 int cstat, dstat;
1107 struct qeth_cmd_buffer *iob = NULL;
1108 struct qeth_channel *channel;
1109 struct qeth_card *card;
1111 card = CARD_FROM_CDEV(cdev);
1112 if (!card)
1113 return;
1115 QETH_CARD_TEXT(card, 5, "irq");
1117 if (card->read.ccwdev == cdev) {
1118 channel = &card->read;
1119 QETH_CARD_TEXT(card, 5, "read");
1120 } else if (card->write.ccwdev == cdev) {
1121 channel = &card->write;
1122 QETH_CARD_TEXT(card, 5, "write");
1123 } else {
1124 channel = &card->data;
1125 QETH_CARD_TEXT(card, 5, "data");
1128 if (qeth_intparm_is_iob(intparm))
1129 iob = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
1131 if (__qeth_check_irb_error(cdev, intparm, irb)) {
1132 /* IO was terminated, free its resources. */
1133 if (iob)
1134 qeth_release_buffer(iob->channel, iob);
1135 atomic_set(&channel->irq_pending, 0);
1136 wake_up(&card->wait_q);
1137 return;
1140 atomic_set(&channel->irq_pending, 0);
1142 if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1143 channel->state = CH_STATE_STOPPED;
1145 if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1146 channel->state = CH_STATE_HALTED;
1148 /*let's wake up immediately on data channel*/
1149 if ((channel == &card->data) && (intparm != 0) &&
1150 (intparm != QETH_RCD_PARM))
1151 goto out;
1153 if (intparm == QETH_CLEAR_CHANNEL_PARM) {
1154 QETH_CARD_TEXT(card, 6, "clrchpar");
1155 /* we don't have to handle this further */
1156 intparm = 0;
1158 if (intparm == QETH_HALT_CHANNEL_PARM) {
1159 QETH_CARD_TEXT(card, 6, "hltchpar");
1160 /* we don't have to handle this further */
1161 intparm = 0;
1164 cstat = irb->scsw.cmd.cstat;
1165 dstat = irb->scsw.cmd.dstat;
1167 if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1168 (dstat & DEV_STAT_UNIT_CHECK) ||
1169 (cstat)) {
1170 if (irb->esw.esw0.erw.cons) {
1171 dev_warn(&channel->ccwdev->dev,
1172 "The qeth device driver failed to recover "
1173 "an error on the device\n");
1174 QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
1175 "0x%X dstat 0x%X\n",
1176 dev_name(&channel->ccwdev->dev), cstat, dstat);
1177 print_hex_dump(KERN_WARNING, "qeth: irb ",
1178 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1179 print_hex_dump(KERN_WARNING, "qeth: sense data ",
1180 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1182 if (intparm == QETH_RCD_PARM) {
1183 channel->state = CH_STATE_DOWN;
1184 goto out;
1186 rc = qeth_get_problem(cdev, irb);
1187 if (rc) {
1188 card->read_or_write_problem = 1;
1189 qeth_clear_ipacmd_list(card);
1190 qeth_schedule_recovery(card);
1191 goto out;
1195 if (intparm == QETH_RCD_PARM) {
1196 channel->state = CH_STATE_RCD_DONE;
1197 goto out;
1199 if (channel == &card->data)
1200 return;
1201 if (channel == &card->read &&
1202 channel->state == CH_STATE_UP)
1203 __qeth_issue_next_read(card);
1205 if (iob && iob->callback)
1206 iob->callback(iob->channel, iob);
1208 out:
1209 wake_up(&card->wait_q);
1210 return;
1213 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1214 struct qeth_qdio_out_buffer *buf,
1215 enum iucv_tx_notify notification)
1217 struct sk_buff *skb;
1219 if (skb_queue_empty(&buf->skb_list))
1220 goto out;
1221 skb = skb_peek(&buf->skb_list);
1222 while (skb) {
1223 QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1224 QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1225 if (be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1226 if (skb->sk) {
1227 struct iucv_sock *iucv = iucv_sk(skb->sk);
1228 iucv->sk_txnotify(skb, notification);
1231 if (skb_queue_is_last(&buf->skb_list, skb))
1232 skb = NULL;
1233 else
1234 skb = skb_queue_next(&buf->skb_list, skb);
1236 out:
1237 return;
1240 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf)
1242 struct sk_buff *skb;
1243 struct iucv_sock *iucv;
1244 int notify_general_error = 0;
1246 if (atomic_read(&buf->state) == QETH_QDIO_BUF_PENDING)
1247 notify_general_error = 1;
1249 /* release may never happen from within CQ tasklet scope */
1250 WARN_ON_ONCE(atomic_read(&buf->state) == QETH_QDIO_BUF_IN_CQ);
1252 skb = skb_dequeue(&buf->skb_list);
1253 while (skb) {
1254 QETH_CARD_TEXT(buf->q->card, 5, "skbr");
1255 QETH_CARD_TEXT_(buf->q->card, 5, "%lx", (long) skb);
1256 if (notify_general_error &&
1257 be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1258 if (skb->sk) {
1259 iucv = iucv_sk(skb->sk);
1260 iucv->sk_txnotify(skb, TX_NOTIFY_GENERALERROR);
1263 refcount_dec(&skb->users);
1264 dev_kfree_skb_any(skb);
1265 skb = skb_dequeue(&buf->skb_list);
1269 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1270 struct qeth_qdio_out_buffer *buf,
1271 enum qeth_qdio_buffer_states newbufstate)
1273 int i;
1275 /* is PCI flag set on buffer? */
1276 if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1277 atomic_dec(&queue->set_pci_flags_count);
1279 if (newbufstate == QETH_QDIO_BUF_EMPTY) {
1280 qeth_release_skbs(buf);
1282 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
1283 if (buf->buffer->element[i].addr && buf->is_header[i])
1284 kmem_cache_free(qeth_core_header_cache,
1285 buf->buffer->element[i].addr);
1286 buf->is_header[i] = 0;
1287 buf->buffer->element[i].length = 0;
1288 buf->buffer->element[i].addr = NULL;
1289 buf->buffer->element[i].eflags = 0;
1290 buf->buffer->element[i].sflags = 0;
1292 buf->buffer->element[15].eflags = 0;
1293 buf->buffer->element[15].sflags = 0;
1294 buf->next_element_to_fill = 0;
1295 atomic_set(&buf->state, newbufstate);
1298 static void qeth_clear_outq_buffers(struct qeth_qdio_out_q *q, int free)
1300 int j;
1302 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1303 if (!q->bufs[j])
1304 continue;
1305 qeth_cleanup_handled_pending(q, j, 1);
1306 qeth_clear_output_buffer(q, q->bufs[j], QETH_QDIO_BUF_EMPTY);
1307 if (free) {
1308 kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1309 q->bufs[j] = NULL;
1314 void qeth_clear_qdio_buffers(struct qeth_card *card)
1316 int i;
1318 QETH_CARD_TEXT(card, 2, "clearqdbf");
1319 /* clear outbound buffers to free skbs */
1320 for (i = 0; i < card->qdio.no_out_queues; ++i) {
1321 if (card->qdio.out_qs[i]) {
1322 qeth_clear_outq_buffers(card->qdio.out_qs[i], 0);
1326 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
1328 static void qeth_free_buffer_pool(struct qeth_card *card)
1330 struct qeth_buffer_pool_entry *pool_entry, *tmp;
1331 int i = 0;
1332 list_for_each_entry_safe(pool_entry, tmp,
1333 &card->qdio.init_pool.entry_list, init_list){
1334 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1335 free_page((unsigned long)pool_entry->elements[i]);
1336 list_del(&pool_entry->init_list);
1337 kfree(pool_entry);
1341 static void qeth_clean_channel(struct qeth_channel *channel)
1343 int cnt;
1345 QETH_DBF_TEXT(SETUP, 2, "freech");
1346 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1347 kfree(channel->iob[cnt].data);
1350 static void qeth_set_single_write_queues(struct qeth_card *card)
1352 if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1353 (card->qdio.no_out_queues == 4))
1354 qeth_free_qdio_buffers(card);
1356 card->qdio.no_out_queues = 1;
1357 if (card->qdio.default_out_queue != 0)
1358 dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1360 card->qdio.default_out_queue = 0;
1363 static void qeth_set_multiple_write_queues(struct qeth_card *card)
1365 if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1366 (card->qdio.no_out_queues == 1)) {
1367 qeth_free_qdio_buffers(card);
1368 card->qdio.default_out_queue = 2;
1370 card->qdio.no_out_queues = 4;
1373 static void qeth_update_from_chp_desc(struct qeth_card *card)
1375 struct ccw_device *ccwdev;
1376 struct channel_path_desc_fmt0 *chp_dsc;
1378 QETH_DBF_TEXT(SETUP, 2, "chp_desc");
1380 ccwdev = card->data.ccwdev;
1381 chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1382 if (!chp_dsc)
1383 goto out;
1385 card->info.func_level = 0x4100 + chp_dsc->desc;
1386 if (card->info.type == QETH_CARD_TYPE_IQD)
1387 goto out;
1389 /* CHPP field bit 6 == 1 -> single queue */
1390 if ((chp_dsc->chpp & 0x02) == 0x02)
1391 qeth_set_single_write_queues(card);
1392 else
1393 qeth_set_multiple_write_queues(card);
1394 out:
1395 kfree(chp_dsc);
1396 QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1397 QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1400 static void qeth_init_qdio_info(struct qeth_card *card)
1402 QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1403 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1404 /* inbound */
1405 card->qdio.no_in_queues = 1;
1406 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1407 if (card->info.type == QETH_CARD_TYPE_IQD)
1408 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1409 else
1410 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1411 card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1412 INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1413 INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1416 static void qeth_set_intial_options(struct qeth_card *card)
1418 card->options.route4.type = NO_ROUTER;
1419 card->options.route6.type = NO_ROUTER;
1420 card->options.fake_broadcast = 0;
1421 card->options.performance_stats = 0;
1422 card->options.rx_sg_cb = QETH_RX_SG_CB;
1423 card->options.isolation = ISOLATION_MODE_NONE;
1424 card->options.cq = QETH_CQ_DISABLED;
1427 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1429 unsigned long flags;
1430 int rc = 0;
1432 spin_lock_irqsave(&card->thread_mask_lock, flags);
1433 QETH_CARD_TEXT_(card, 4, " %02x%02x%02x",
1434 (u8) card->thread_start_mask,
1435 (u8) card->thread_allowed_mask,
1436 (u8) card->thread_running_mask);
1437 rc = (card->thread_start_mask & thread);
1438 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1439 return rc;
1442 static void qeth_start_kernel_thread(struct work_struct *work)
1444 struct task_struct *ts;
1445 struct qeth_card *card = container_of(work, struct qeth_card,
1446 kernel_thread_starter);
1447 QETH_CARD_TEXT(card , 2, "strthrd");
1449 if (card->read.state != CH_STATE_UP &&
1450 card->write.state != CH_STATE_UP)
1451 return;
1452 if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1453 ts = kthread_run(card->discipline->recover, (void *)card,
1454 "qeth_recover");
1455 if (IS_ERR(ts)) {
1456 qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1457 qeth_clear_thread_running_bit(card,
1458 QETH_RECOVER_THREAD);
1463 static void qeth_buffer_reclaim_work(struct work_struct *);
1464 static int qeth_setup_card(struct qeth_card *card)
1467 QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1468 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1470 card->read.state = CH_STATE_DOWN;
1471 card->write.state = CH_STATE_DOWN;
1472 card->data.state = CH_STATE_DOWN;
1473 card->state = CARD_STATE_DOWN;
1474 card->lan_online = 0;
1475 card->read_or_write_problem = 0;
1476 card->dev = NULL;
1477 spin_lock_init(&card->mclock);
1478 spin_lock_init(&card->lock);
1479 spin_lock_init(&card->ip_lock);
1480 spin_lock_init(&card->thread_mask_lock);
1481 mutex_init(&card->conf_mutex);
1482 mutex_init(&card->discipline_mutex);
1483 mutex_init(&card->vid_list_mutex);
1484 card->thread_start_mask = 0;
1485 card->thread_allowed_mask = 0;
1486 card->thread_running_mask = 0;
1487 INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1488 INIT_LIST_HEAD(&card->cmd_waiter_list);
1489 init_waitqueue_head(&card->wait_q);
1490 /* initial options */
1491 qeth_set_intial_options(card);
1492 /* IP address takeover */
1493 INIT_LIST_HEAD(&card->ipato.entries);
1494 card->ipato.enabled = false;
1495 card->ipato.invert4 = false;
1496 card->ipato.invert6 = false;
1497 /* init QDIO stuff */
1498 qeth_init_qdio_info(card);
1499 INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1500 INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1501 return 0;
1504 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1506 struct qeth_card *card = container_of(slr, struct qeth_card,
1507 qeth_service_level);
1508 if (card->info.mcl_level[0])
1509 seq_printf(m, "qeth: %s firmware level %s\n",
1510 CARD_BUS_ID(card), card->info.mcl_level);
1513 static struct qeth_card *qeth_alloc_card(void)
1515 struct qeth_card *card;
1517 QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1518 card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1519 if (!card)
1520 goto out;
1521 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1522 if (qeth_setup_channel(&card->read))
1523 goto out_ip;
1524 if (qeth_setup_channel(&card->write))
1525 goto out_channel;
1526 card->options.layer2 = -1;
1527 card->qeth_service_level.seq_print = qeth_core_sl_print;
1528 register_service_level(&card->qeth_service_level);
1529 return card;
1531 out_channel:
1532 qeth_clean_channel(&card->read);
1533 out_ip:
1534 kfree(card);
1535 out:
1536 return NULL;
1539 static void qeth_determine_card_type(struct qeth_card *card)
1541 QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1543 card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1544 card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1545 card->info.type = CARD_RDEV(card)->id.driver_info;
1546 card->qdio.no_out_queues = QETH_MAX_QUEUES;
1547 if (card->info.type == QETH_CARD_TYPE_IQD)
1548 card->info.is_multicast_different = 0x0103;
1549 qeth_update_from_chp_desc(card);
1552 static int qeth_clear_channel(struct qeth_channel *channel)
1554 unsigned long flags;
1555 struct qeth_card *card;
1556 int rc;
1558 card = CARD_FROM_CDEV(channel->ccwdev);
1559 QETH_CARD_TEXT(card, 3, "clearch");
1560 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1561 rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1562 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1564 if (rc)
1565 return rc;
1566 rc = wait_event_interruptible_timeout(card->wait_q,
1567 channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1568 if (rc == -ERESTARTSYS)
1569 return rc;
1570 if (channel->state != CH_STATE_STOPPED)
1571 return -ETIME;
1572 channel->state = CH_STATE_DOWN;
1573 return 0;
1576 static int qeth_halt_channel(struct qeth_channel *channel)
1578 unsigned long flags;
1579 struct qeth_card *card;
1580 int rc;
1582 card = CARD_FROM_CDEV(channel->ccwdev);
1583 QETH_CARD_TEXT(card, 3, "haltch");
1584 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1585 rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1586 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1588 if (rc)
1589 return rc;
1590 rc = wait_event_interruptible_timeout(card->wait_q,
1591 channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1592 if (rc == -ERESTARTSYS)
1593 return rc;
1594 if (channel->state != CH_STATE_HALTED)
1595 return -ETIME;
1596 return 0;
1599 static int qeth_halt_channels(struct qeth_card *card)
1601 int rc1 = 0, rc2 = 0, rc3 = 0;
1603 QETH_CARD_TEXT(card, 3, "haltchs");
1604 rc1 = qeth_halt_channel(&card->read);
1605 rc2 = qeth_halt_channel(&card->write);
1606 rc3 = qeth_halt_channel(&card->data);
1607 if (rc1)
1608 return rc1;
1609 if (rc2)
1610 return rc2;
1611 return rc3;
1614 static int qeth_clear_channels(struct qeth_card *card)
1616 int rc1 = 0, rc2 = 0, rc3 = 0;
1618 QETH_CARD_TEXT(card, 3, "clearchs");
1619 rc1 = qeth_clear_channel(&card->read);
1620 rc2 = qeth_clear_channel(&card->write);
1621 rc3 = qeth_clear_channel(&card->data);
1622 if (rc1)
1623 return rc1;
1624 if (rc2)
1625 return rc2;
1626 return rc3;
1629 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1631 int rc = 0;
1633 QETH_CARD_TEXT(card, 3, "clhacrd");
1635 if (halt)
1636 rc = qeth_halt_channels(card);
1637 if (rc)
1638 return rc;
1639 return qeth_clear_channels(card);
1642 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1644 int rc = 0;
1646 QETH_CARD_TEXT(card, 3, "qdioclr");
1647 switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1648 QETH_QDIO_CLEANING)) {
1649 case QETH_QDIO_ESTABLISHED:
1650 if (card->info.type == QETH_CARD_TYPE_IQD)
1651 rc = qdio_shutdown(CARD_DDEV(card),
1652 QDIO_FLAG_CLEANUP_USING_HALT);
1653 else
1654 rc = qdio_shutdown(CARD_DDEV(card),
1655 QDIO_FLAG_CLEANUP_USING_CLEAR);
1656 if (rc)
1657 QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1658 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1659 break;
1660 case QETH_QDIO_CLEANING:
1661 return rc;
1662 default:
1663 break;
1665 rc = qeth_clear_halt_card(card, use_halt);
1666 if (rc)
1667 QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1668 card->state = CARD_STATE_DOWN;
1669 return rc;
1671 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1673 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1674 int *length)
1676 struct ciw *ciw;
1677 char *rcd_buf;
1678 int ret;
1679 struct qeth_channel *channel = &card->data;
1680 unsigned long flags;
1683 * scan for RCD command in extended SenseID data
1685 ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1686 if (!ciw || ciw->cmd == 0)
1687 return -EOPNOTSUPP;
1688 rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1689 if (!rcd_buf)
1690 return -ENOMEM;
1692 channel->ccw.cmd_code = ciw->cmd;
1693 channel->ccw.cda = (__u32) __pa(rcd_buf);
1694 channel->ccw.count = ciw->count;
1695 channel->ccw.flags = CCW_FLAG_SLI;
1696 channel->state = CH_STATE_RCD;
1697 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1698 ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1699 QETH_RCD_PARM, LPM_ANYPATH, 0,
1700 QETH_RCD_TIMEOUT);
1701 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1702 if (!ret)
1703 wait_event(card->wait_q,
1704 (channel->state == CH_STATE_RCD_DONE ||
1705 channel->state == CH_STATE_DOWN));
1706 if (channel->state == CH_STATE_DOWN)
1707 ret = -EIO;
1708 else
1709 channel->state = CH_STATE_DOWN;
1710 if (ret) {
1711 kfree(rcd_buf);
1712 *buffer = NULL;
1713 *length = 0;
1714 } else {
1715 *length = ciw->count;
1716 *buffer = rcd_buf;
1718 return ret;
1721 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1723 QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1724 card->info.chpid = prcd[30];
1725 card->info.unit_addr2 = prcd[31];
1726 card->info.cula = prcd[63];
1727 card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1728 (prcd[0x11] == _ascebc['M']));
1731 static enum qeth_discipline_id qeth_vm_detect_layer(struct qeth_card *card)
1733 enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1734 struct diag26c_vnic_resp *response = NULL;
1735 struct diag26c_vnic_req *request = NULL;
1736 struct ccw_dev_id id;
1737 char userid[80];
1738 int rc = 0;
1740 QETH_DBF_TEXT(SETUP, 2, "vmlayer");
1742 cpcmd("QUERY USERID", userid, sizeof(userid), &rc);
1743 if (rc)
1744 goto out;
1746 request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
1747 response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
1748 if (!request || !response) {
1749 rc = -ENOMEM;
1750 goto out;
1753 ccw_device_get_id(CARD_RDEV(card), &id);
1754 request->resp_buf_len = sizeof(*response);
1755 request->resp_version = DIAG26C_VERSION6_VM65918;
1756 request->req_format = DIAG26C_VNIC_INFO;
1757 ASCEBC(userid, 8);
1758 memcpy(&request->sys_name, userid, 8);
1759 request->devno = id.devno;
1761 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1762 rc = diag26c(request, response, DIAG26C_PORT_VNIC);
1763 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1764 if (rc)
1765 goto out;
1766 QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
1768 if (request->resp_buf_len < sizeof(*response) ||
1769 response->version != request->resp_version) {
1770 rc = -EIO;
1771 goto out;
1774 if (response->protocol == VNIC_INFO_PROT_L2)
1775 disc = QETH_DISCIPLINE_LAYER2;
1776 else if (response->protocol == VNIC_INFO_PROT_L3)
1777 disc = QETH_DISCIPLINE_LAYER3;
1779 out:
1780 kfree(response);
1781 kfree(request);
1782 if (rc)
1783 QETH_DBF_TEXT_(SETUP, 2, "err%x", rc);
1784 return disc;
1787 /* Determine whether the device requires a specific layer discipline */
1788 static enum qeth_discipline_id qeth_enforce_discipline(struct qeth_card *card)
1790 enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1792 if (card->info.type == QETH_CARD_TYPE_OSM ||
1793 card->info.type == QETH_CARD_TYPE_OSN)
1794 disc = QETH_DISCIPLINE_LAYER2;
1795 else if (card->info.guestlan)
1796 disc = (card->info.type == QETH_CARD_TYPE_IQD) ?
1797 QETH_DISCIPLINE_LAYER3 :
1798 qeth_vm_detect_layer(card);
1800 switch (disc) {
1801 case QETH_DISCIPLINE_LAYER2:
1802 QETH_DBF_TEXT(SETUP, 3, "force l2");
1803 break;
1804 case QETH_DISCIPLINE_LAYER3:
1805 QETH_DBF_TEXT(SETUP, 3, "force l3");
1806 break;
1807 default:
1808 QETH_DBF_TEXT(SETUP, 3, "force no");
1811 return disc;
1814 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1816 QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1818 if (prcd[74] == 0xF0 && prcd[75] == 0xF0 &&
1819 prcd[76] >= 0xF1 && prcd[76] <= 0xF4) {
1820 card->info.blkt.time_total = 0;
1821 card->info.blkt.inter_packet = 0;
1822 card->info.blkt.inter_packet_jumbo = 0;
1823 } else {
1824 card->info.blkt.time_total = 250;
1825 card->info.blkt.inter_packet = 5;
1826 card->info.blkt.inter_packet_jumbo = 15;
1830 static void qeth_init_tokens(struct qeth_card *card)
1832 card->token.issuer_rm_w = 0x00010103UL;
1833 card->token.cm_filter_w = 0x00010108UL;
1834 card->token.cm_connection_w = 0x0001010aUL;
1835 card->token.ulp_filter_w = 0x0001010bUL;
1836 card->token.ulp_connection_w = 0x0001010dUL;
1839 static void qeth_init_func_level(struct qeth_card *card)
1841 switch (card->info.type) {
1842 case QETH_CARD_TYPE_IQD:
1843 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1844 break;
1845 case QETH_CARD_TYPE_OSD:
1846 case QETH_CARD_TYPE_OSN:
1847 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1848 break;
1849 default:
1850 break;
1854 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1855 void (*idx_reply_cb)(struct qeth_channel *,
1856 struct qeth_cmd_buffer *))
1858 struct qeth_cmd_buffer *iob;
1859 unsigned long flags;
1860 int rc;
1861 struct qeth_card *card;
1863 QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1864 card = CARD_FROM_CDEV(channel->ccwdev);
1865 iob = qeth_get_buffer(channel);
1866 if (!iob)
1867 return -ENOMEM;
1868 iob->callback = idx_reply_cb;
1869 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1870 channel->ccw.count = QETH_BUFSIZE;
1871 channel->ccw.cda = (__u32) __pa(iob->data);
1873 wait_event(card->wait_q,
1874 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1875 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1876 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1877 rc = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1878 (addr_t) iob, 0, 0, QETH_TIMEOUT);
1879 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1881 if (rc) {
1882 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1883 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1884 atomic_set(&channel->irq_pending, 0);
1885 wake_up(&card->wait_q);
1886 return rc;
1888 rc = wait_event_interruptible_timeout(card->wait_q,
1889 channel->state == CH_STATE_UP, QETH_TIMEOUT);
1890 if (rc == -ERESTARTSYS)
1891 return rc;
1892 if (channel->state != CH_STATE_UP) {
1893 rc = -ETIME;
1894 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1895 } else
1896 rc = 0;
1897 return rc;
1900 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1901 void (*idx_reply_cb)(struct qeth_channel *,
1902 struct qeth_cmd_buffer *))
1904 struct qeth_card *card;
1905 struct qeth_cmd_buffer *iob;
1906 unsigned long flags;
1907 __u16 temp;
1908 __u8 tmp;
1909 int rc;
1910 struct ccw_dev_id temp_devid;
1912 card = CARD_FROM_CDEV(channel->ccwdev);
1914 QETH_DBF_TEXT(SETUP, 2, "idxactch");
1916 iob = qeth_get_buffer(channel);
1917 if (!iob)
1918 return -ENOMEM;
1919 iob->callback = idx_reply_cb;
1920 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1921 channel->ccw.count = IDX_ACTIVATE_SIZE;
1922 channel->ccw.cda = (__u32) __pa(iob->data);
1923 if (channel == &card->write) {
1924 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1925 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1926 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1927 card->seqno.trans_hdr++;
1928 } else {
1929 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1930 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1931 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1933 tmp = ((__u8)card->info.portno) | 0x80;
1934 memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1935 memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1936 &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1937 memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1938 &card->info.func_level, sizeof(__u16));
1939 ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1940 memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1941 temp = (card->info.cula << 8) + card->info.unit_addr2;
1942 memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1944 wait_event(card->wait_q,
1945 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1946 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1947 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1948 rc = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1949 (addr_t) iob, 0, 0, QETH_TIMEOUT);
1950 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1952 if (rc) {
1953 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1954 rc);
1955 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1956 atomic_set(&channel->irq_pending, 0);
1957 wake_up(&card->wait_q);
1958 return rc;
1960 rc = wait_event_interruptible_timeout(card->wait_q,
1961 channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1962 if (rc == -ERESTARTSYS)
1963 return rc;
1964 if (channel->state != CH_STATE_ACTIVATING) {
1965 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1966 " failed to recover an error on the device\n");
1967 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1968 dev_name(&channel->ccwdev->dev));
1969 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1970 return -ETIME;
1972 return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1975 static int qeth_peer_func_level(int level)
1977 if ((level & 0xff) == 8)
1978 return (level & 0xff) + 0x400;
1979 if (((level >> 8) & 3) == 1)
1980 return (level & 0xff) + 0x200;
1981 return level;
1984 static void qeth_idx_write_cb(struct qeth_channel *channel,
1985 struct qeth_cmd_buffer *iob)
1987 struct qeth_card *card;
1988 __u16 temp;
1990 QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1992 if (channel->state == CH_STATE_DOWN) {
1993 channel->state = CH_STATE_ACTIVATING;
1994 goto out;
1996 card = CARD_FROM_CDEV(channel->ccwdev);
1998 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1999 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
2000 dev_err(&card->write.ccwdev->dev,
2001 "The adapter is used exclusively by another "
2002 "host\n");
2003 else
2004 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
2005 " negative reply\n",
2006 dev_name(&card->write.ccwdev->dev));
2007 goto out;
2009 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2010 if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
2011 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
2012 "function level mismatch (sent: 0x%x, received: "
2013 "0x%x)\n", dev_name(&card->write.ccwdev->dev),
2014 card->info.func_level, temp);
2015 goto out;
2017 channel->state = CH_STATE_UP;
2018 out:
2019 qeth_release_buffer(channel, iob);
2022 static void qeth_idx_read_cb(struct qeth_channel *channel,
2023 struct qeth_cmd_buffer *iob)
2025 struct qeth_card *card;
2026 __u16 temp;
2028 QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
2029 if (channel->state == CH_STATE_DOWN) {
2030 channel->state = CH_STATE_ACTIVATING;
2031 goto out;
2034 card = CARD_FROM_CDEV(channel->ccwdev);
2035 if (qeth_check_idx_response(card, iob->data))
2036 goto out;
2038 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
2039 switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
2040 case QETH_IDX_ACT_ERR_EXCL:
2041 dev_err(&card->write.ccwdev->dev,
2042 "The adapter is used exclusively by another "
2043 "host\n");
2044 break;
2045 case QETH_IDX_ACT_ERR_AUTH:
2046 case QETH_IDX_ACT_ERR_AUTH_USER:
2047 dev_err(&card->read.ccwdev->dev,
2048 "Setting the device online failed because of "
2049 "insufficient authorization\n");
2050 break;
2051 default:
2052 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
2053 " negative reply\n",
2054 dev_name(&card->read.ccwdev->dev));
2056 QETH_CARD_TEXT_(card, 2, "idxread%c",
2057 QETH_IDX_ACT_CAUSE_CODE(iob->data));
2058 goto out;
2061 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2062 if (temp != qeth_peer_func_level(card->info.func_level)) {
2063 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
2064 "level mismatch (sent: 0x%x, received: 0x%x)\n",
2065 dev_name(&card->read.ccwdev->dev),
2066 card->info.func_level, temp);
2067 goto out;
2069 memcpy(&card->token.issuer_rm_r,
2070 QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
2071 QETH_MPC_TOKEN_LENGTH);
2072 memcpy(&card->info.mcl_level[0],
2073 QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
2074 channel->state = CH_STATE_UP;
2075 out:
2076 qeth_release_buffer(channel, iob);
2079 void qeth_prepare_control_data(struct qeth_card *card, int len,
2080 struct qeth_cmd_buffer *iob)
2082 qeth_setup_ccw(&card->write, iob->data, len);
2083 iob->callback = qeth_release_buffer;
2085 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
2086 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
2087 card->seqno.trans_hdr++;
2088 memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
2089 &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
2090 card->seqno.pdu_hdr++;
2091 memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
2092 &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
2093 QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
2095 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
2098 * qeth_send_control_data() - send control command to the card
2099 * @card: qeth_card structure pointer
2100 * @len: size of the command buffer
2101 * @iob: qeth_cmd_buffer pointer
2102 * @reply_cb: callback function pointer
2103 * @cb_card: pointer to the qeth_card structure
2104 * @cb_reply: pointer to the qeth_reply structure
2105 * @cb_cmd: pointer to the original iob for non-IPA
2106 * commands, or to the qeth_ipa_cmd structure
2107 * for the IPA commands.
2108 * @reply_param: private pointer passed to the callback
2110 * Returns the value of the `return_code' field of the response
2111 * block returned from the hardware, or other error indication.
2112 * Value of zero indicates successful execution of the command.
2114 * Callback function gets called one or more times, with cb_cmd
2115 * pointing to the response returned by the hardware. Callback
2116 * function must return non-zero if more reply blocks are expected,
2117 * and zero if the last or only reply block is received. Callback
2118 * function can get the value of the reply_param pointer from the
2119 * field 'param' of the structure qeth_reply.
2122 int qeth_send_control_data(struct qeth_card *card, int len,
2123 struct qeth_cmd_buffer *iob,
2124 int (*reply_cb)(struct qeth_card *cb_card,
2125 struct qeth_reply *cb_reply,
2126 unsigned long cb_cmd),
2127 void *reply_param)
2129 int rc;
2130 unsigned long flags;
2131 struct qeth_reply *reply = NULL;
2132 unsigned long timeout, event_timeout;
2133 struct qeth_ipa_cmd *cmd = NULL;
2135 QETH_CARD_TEXT(card, 2, "sendctl");
2137 if (card->read_or_write_problem) {
2138 qeth_release_buffer(iob->channel, iob);
2139 return -EIO;
2141 reply = qeth_alloc_reply(card);
2142 if (!reply) {
2143 return -ENOMEM;
2145 reply->callback = reply_cb;
2146 reply->param = reply_param;
2148 init_waitqueue_head(&reply->wait_q);
2150 while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
2152 if (IS_IPA(iob->data)) {
2153 cmd = __ipa_cmd(iob);
2154 cmd->hdr.seqno = card->seqno.ipa++;
2155 reply->seqno = cmd->hdr.seqno;
2156 event_timeout = QETH_IPA_TIMEOUT;
2157 } else {
2158 reply->seqno = QETH_IDX_COMMAND_SEQNO;
2159 event_timeout = QETH_TIMEOUT;
2161 qeth_prepare_control_data(card, len, iob);
2163 spin_lock_irqsave(&card->lock, flags);
2164 list_add_tail(&reply->list, &card->cmd_waiter_list);
2165 spin_unlock_irqrestore(&card->lock, flags);
2167 timeout = jiffies + event_timeout;
2169 QETH_CARD_TEXT(card, 6, "noirqpnd");
2170 spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
2171 rc = ccw_device_start_timeout(CARD_WDEV(card), &card->write.ccw,
2172 (addr_t) iob, 0, 0, event_timeout);
2173 spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
2174 if (rc) {
2175 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
2176 "ccw_device_start rc = %i\n",
2177 dev_name(&card->write.ccwdev->dev), rc);
2178 QETH_CARD_TEXT_(card, 2, " err%d", rc);
2179 spin_lock_irqsave(&card->lock, flags);
2180 list_del_init(&reply->list);
2181 qeth_put_reply(reply);
2182 spin_unlock_irqrestore(&card->lock, flags);
2183 qeth_release_buffer(iob->channel, iob);
2184 atomic_set(&card->write.irq_pending, 0);
2185 wake_up(&card->wait_q);
2186 return rc;
2189 /* we have only one long running ipassist, since we can ensure
2190 process context of this command we can sleep */
2191 if (cmd && cmd->hdr.command == IPA_CMD_SETIP &&
2192 cmd->hdr.prot_version == QETH_PROT_IPV4) {
2193 if (!wait_event_timeout(reply->wait_q,
2194 atomic_read(&reply->received), event_timeout))
2195 goto time_err;
2196 } else {
2197 while (!atomic_read(&reply->received)) {
2198 if (time_after(jiffies, timeout))
2199 goto time_err;
2200 cpu_relax();
2204 rc = reply->rc;
2205 qeth_put_reply(reply);
2206 return rc;
2208 time_err:
2209 reply->rc = -ETIME;
2210 spin_lock_irqsave(&reply->card->lock, flags);
2211 list_del_init(&reply->list);
2212 spin_unlock_irqrestore(&reply->card->lock, flags);
2213 atomic_inc(&reply->received);
2214 rc = reply->rc;
2215 qeth_put_reply(reply);
2216 return rc;
2218 EXPORT_SYMBOL_GPL(qeth_send_control_data);
2220 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2221 unsigned long data)
2223 struct qeth_cmd_buffer *iob;
2225 QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
2227 iob = (struct qeth_cmd_buffer *) data;
2228 memcpy(&card->token.cm_filter_r,
2229 QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2230 QETH_MPC_TOKEN_LENGTH);
2231 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2232 return 0;
2235 static int qeth_cm_enable(struct qeth_card *card)
2237 int rc;
2238 struct qeth_cmd_buffer *iob;
2240 QETH_DBF_TEXT(SETUP, 2, "cmenable");
2242 iob = qeth_wait_for_buffer(&card->write);
2243 memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2244 memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2245 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2246 memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2247 &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2249 rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2250 qeth_cm_enable_cb, NULL);
2251 return rc;
2254 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2255 unsigned long data)
2258 struct qeth_cmd_buffer *iob;
2260 QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
2262 iob = (struct qeth_cmd_buffer *) data;
2263 memcpy(&card->token.cm_connection_r,
2264 QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2265 QETH_MPC_TOKEN_LENGTH);
2266 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2267 return 0;
2270 static int qeth_cm_setup(struct qeth_card *card)
2272 int rc;
2273 struct qeth_cmd_buffer *iob;
2275 QETH_DBF_TEXT(SETUP, 2, "cmsetup");
2277 iob = qeth_wait_for_buffer(&card->write);
2278 memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2279 memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2280 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2281 memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2282 &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2283 memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2284 &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2285 rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2286 qeth_cm_setup_cb, NULL);
2287 return rc;
2291 static int qeth_get_initial_mtu_for_card(struct qeth_card *card)
2293 switch (card->info.type) {
2294 case QETH_CARD_TYPE_IQD:
2295 return card->info.max_mtu;
2296 case QETH_CARD_TYPE_OSD:
2297 case QETH_CARD_TYPE_OSX:
2298 if (!card->options.layer2)
2299 return ETH_DATA_LEN - 8; /* L3: allow for LLC + SNAP */
2300 /* fall through */
2301 default:
2302 return ETH_DATA_LEN;
2306 static int qeth_get_mtu_outof_framesize(int framesize)
2308 switch (framesize) {
2309 case 0x4000:
2310 return 8192;
2311 case 0x6000:
2312 return 16384;
2313 case 0xa000:
2314 return 32768;
2315 case 0xffff:
2316 return 57344;
2317 default:
2318 return 0;
2322 static int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
2324 switch (card->info.type) {
2325 case QETH_CARD_TYPE_OSD:
2326 case QETH_CARD_TYPE_OSM:
2327 case QETH_CARD_TYPE_OSX:
2328 case QETH_CARD_TYPE_IQD:
2329 return ((mtu >= 576) &&
2330 (mtu <= card->info.max_mtu));
2331 case QETH_CARD_TYPE_OSN:
2332 default:
2333 return 1;
2337 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2338 unsigned long data)
2341 __u16 mtu, framesize;
2342 __u16 len;
2343 __u8 link_type;
2344 struct qeth_cmd_buffer *iob;
2346 QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
2348 iob = (struct qeth_cmd_buffer *) data;
2349 memcpy(&card->token.ulp_filter_r,
2350 QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2351 QETH_MPC_TOKEN_LENGTH);
2352 if (card->info.type == QETH_CARD_TYPE_IQD) {
2353 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2354 mtu = qeth_get_mtu_outof_framesize(framesize);
2355 if (!mtu) {
2356 iob->rc = -EINVAL;
2357 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2358 return 0;
2360 if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
2361 /* frame size has changed */
2362 if (card->dev &&
2363 ((card->dev->mtu == card->info.initial_mtu) ||
2364 (card->dev->mtu > mtu)))
2365 card->dev->mtu = mtu;
2366 qeth_free_qdio_buffers(card);
2368 card->info.initial_mtu = mtu;
2369 card->info.max_mtu = mtu;
2370 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2371 } else {
2372 card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
2373 iob->data);
2374 card->info.initial_mtu = min(card->info.max_mtu,
2375 qeth_get_initial_mtu_for_card(card));
2376 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2379 memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2380 if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2381 memcpy(&link_type,
2382 QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2383 card->info.link_type = link_type;
2384 } else
2385 card->info.link_type = 0;
2386 QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
2387 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2388 return 0;
2391 static int qeth_ulp_enable(struct qeth_card *card)
2393 int rc;
2394 char prot_type;
2395 struct qeth_cmd_buffer *iob;
2397 /*FIXME: trace view callbacks*/
2398 QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
2400 iob = qeth_wait_for_buffer(&card->write);
2401 memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2403 *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2404 (__u8) card->info.portno;
2405 if (card->options.layer2)
2406 if (card->info.type == QETH_CARD_TYPE_OSN)
2407 prot_type = QETH_PROT_OSN2;
2408 else
2409 prot_type = QETH_PROT_LAYER2;
2410 else
2411 prot_type = QETH_PROT_TCPIP;
2413 memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2414 memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2415 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2416 memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2417 &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2418 rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2419 qeth_ulp_enable_cb, NULL);
2420 return rc;
2424 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2425 unsigned long data)
2427 struct qeth_cmd_buffer *iob;
2429 QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
2431 iob = (struct qeth_cmd_buffer *) data;
2432 memcpy(&card->token.ulp_connection_r,
2433 QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2434 QETH_MPC_TOKEN_LENGTH);
2435 if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2436 3)) {
2437 QETH_DBF_TEXT(SETUP, 2, "olmlimit");
2438 dev_err(&card->gdev->dev, "A connection could not be "
2439 "established because of an OLM limit\n");
2440 iob->rc = -EMLINK;
2442 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2443 return 0;
2446 static int qeth_ulp_setup(struct qeth_card *card)
2448 int rc;
2449 __u16 temp;
2450 struct qeth_cmd_buffer *iob;
2451 struct ccw_dev_id dev_id;
2453 QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2455 iob = qeth_wait_for_buffer(&card->write);
2456 memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2458 memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2459 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2460 memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2461 &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2462 memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2463 &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2465 ccw_device_get_id(CARD_DDEV(card), &dev_id);
2466 memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2467 temp = (card->info.cula << 8) + card->info.unit_addr2;
2468 memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2469 rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2470 qeth_ulp_setup_cb, NULL);
2471 return rc;
2474 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2476 int rc;
2477 struct qeth_qdio_out_buffer *newbuf;
2479 rc = 0;
2480 newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2481 if (!newbuf) {
2482 rc = -ENOMEM;
2483 goto out;
2485 newbuf->buffer = q->qdio_bufs[bidx];
2486 skb_queue_head_init(&newbuf->skb_list);
2487 lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2488 newbuf->q = q;
2489 newbuf->aob = NULL;
2490 newbuf->next_pending = q->bufs[bidx];
2491 atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2492 q->bufs[bidx] = newbuf;
2493 if (q->bufstates) {
2494 q->bufstates[bidx].user = newbuf;
2495 QETH_CARD_TEXT_(q->card, 2, "nbs%d", bidx);
2496 QETH_CARD_TEXT_(q->card, 2, "%lx", (long) newbuf);
2497 QETH_CARD_TEXT_(q->card, 2, "%lx",
2498 (long) newbuf->next_pending);
2500 out:
2501 return rc;
2504 static void qeth_free_qdio_out_buf(struct qeth_qdio_out_q *q)
2506 if (!q)
2507 return;
2509 qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2510 kfree(q);
2513 static struct qeth_qdio_out_q *qeth_alloc_qdio_out_buf(void)
2515 struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2517 if (!q)
2518 return NULL;
2520 if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
2521 kfree(q);
2522 return NULL;
2524 return q;
2527 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2529 int i, j;
2531 QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2533 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2534 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2535 return 0;
2537 QETH_DBF_TEXT(SETUP, 2, "inq");
2538 card->qdio.in_q = qeth_alloc_qdio_queue();
2539 if (!card->qdio.in_q)
2540 goto out_nomem;
2542 /* inbound buffer pool */
2543 if (qeth_alloc_buffer_pool(card))
2544 goto out_freeinq;
2546 /* outbound */
2547 card->qdio.out_qs =
2548 kcalloc(card->qdio.no_out_queues,
2549 sizeof(struct qeth_qdio_out_q *),
2550 GFP_KERNEL);
2551 if (!card->qdio.out_qs)
2552 goto out_freepool;
2553 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2554 card->qdio.out_qs[i] = qeth_alloc_qdio_out_buf();
2555 if (!card->qdio.out_qs[i])
2556 goto out_freeoutq;
2557 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2558 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2559 card->qdio.out_qs[i]->queue_no = i;
2560 /* give outbound qeth_qdio_buffers their qdio_buffers */
2561 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2562 WARN_ON(card->qdio.out_qs[i]->bufs[j] != NULL);
2563 if (qeth_init_qdio_out_buf(card->qdio.out_qs[i], j))
2564 goto out_freeoutqbufs;
2568 /* completion */
2569 if (qeth_alloc_cq(card))
2570 goto out_freeoutq;
2572 return 0;
2574 out_freeoutqbufs:
2575 while (j > 0) {
2576 --j;
2577 kmem_cache_free(qeth_qdio_outbuf_cache,
2578 card->qdio.out_qs[i]->bufs[j]);
2579 card->qdio.out_qs[i]->bufs[j] = NULL;
2581 out_freeoutq:
2582 while (i > 0) {
2583 qeth_free_qdio_out_buf(card->qdio.out_qs[--i]);
2584 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2586 kfree(card->qdio.out_qs);
2587 card->qdio.out_qs = NULL;
2588 out_freepool:
2589 qeth_free_buffer_pool(card);
2590 out_freeinq:
2591 qeth_free_qdio_queue(card->qdio.in_q);
2592 card->qdio.in_q = NULL;
2593 out_nomem:
2594 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2595 return -ENOMEM;
2598 static void qeth_free_qdio_buffers(struct qeth_card *card)
2600 int i, j;
2602 if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2603 QETH_QDIO_UNINITIALIZED)
2604 return;
2606 qeth_free_cq(card);
2607 cancel_delayed_work_sync(&card->buffer_reclaim_work);
2608 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2609 if (card->qdio.in_q->bufs[j].rx_skb)
2610 dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2612 qeth_free_qdio_queue(card->qdio.in_q);
2613 card->qdio.in_q = NULL;
2614 /* inbound buffer pool */
2615 qeth_free_buffer_pool(card);
2616 /* free outbound qdio_qs */
2617 if (card->qdio.out_qs) {
2618 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2619 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2620 qeth_free_qdio_out_buf(card->qdio.out_qs[i]);
2622 kfree(card->qdio.out_qs);
2623 card->qdio.out_qs = NULL;
2627 static void qeth_create_qib_param_field(struct qeth_card *card,
2628 char *param_field)
2631 param_field[0] = _ascebc['P'];
2632 param_field[1] = _ascebc['C'];
2633 param_field[2] = _ascebc['I'];
2634 param_field[3] = _ascebc['T'];
2635 *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2636 *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2637 *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2640 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2641 char *param_field)
2643 param_field[16] = _ascebc['B'];
2644 param_field[17] = _ascebc['L'];
2645 param_field[18] = _ascebc['K'];
2646 param_field[19] = _ascebc['T'];
2647 *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2648 *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2649 *((unsigned int *) (&param_field[28])) =
2650 card->info.blkt.inter_packet_jumbo;
2653 static int qeth_qdio_activate(struct qeth_card *card)
2655 QETH_DBF_TEXT(SETUP, 3, "qdioact");
2656 return qdio_activate(CARD_DDEV(card));
2659 static int qeth_dm_act(struct qeth_card *card)
2661 int rc;
2662 struct qeth_cmd_buffer *iob;
2664 QETH_DBF_TEXT(SETUP, 2, "dmact");
2666 iob = qeth_wait_for_buffer(&card->write);
2667 memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2669 memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2670 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2671 memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2672 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2673 rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2674 return rc;
2677 static int qeth_mpc_initialize(struct qeth_card *card)
2679 int rc;
2681 QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2683 rc = qeth_issue_next_read(card);
2684 if (rc) {
2685 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2686 return rc;
2688 rc = qeth_cm_enable(card);
2689 if (rc) {
2690 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2691 goto out_qdio;
2693 rc = qeth_cm_setup(card);
2694 if (rc) {
2695 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2696 goto out_qdio;
2698 rc = qeth_ulp_enable(card);
2699 if (rc) {
2700 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2701 goto out_qdio;
2703 rc = qeth_ulp_setup(card);
2704 if (rc) {
2705 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2706 goto out_qdio;
2708 rc = qeth_alloc_qdio_buffers(card);
2709 if (rc) {
2710 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2711 goto out_qdio;
2713 rc = qeth_qdio_establish(card);
2714 if (rc) {
2715 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2716 qeth_free_qdio_buffers(card);
2717 goto out_qdio;
2719 rc = qeth_qdio_activate(card);
2720 if (rc) {
2721 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2722 goto out_qdio;
2724 rc = qeth_dm_act(card);
2725 if (rc) {
2726 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2727 goto out_qdio;
2730 return 0;
2731 out_qdio:
2732 qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2733 qdio_free(CARD_DDEV(card));
2734 return rc;
2737 void qeth_print_status_message(struct qeth_card *card)
2739 switch (card->info.type) {
2740 case QETH_CARD_TYPE_OSD:
2741 case QETH_CARD_TYPE_OSM:
2742 case QETH_CARD_TYPE_OSX:
2743 /* VM will use a non-zero first character
2744 * to indicate a HiperSockets like reporting
2745 * of the level OSA sets the first character to zero
2746 * */
2747 if (!card->info.mcl_level[0]) {
2748 sprintf(card->info.mcl_level, "%02x%02x",
2749 card->info.mcl_level[2],
2750 card->info.mcl_level[3]);
2751 break;
2753 /* fallthrough */
2754 case QETH_CARD_TYPE_IQD:
2755 if ((card->info.guestlan) ||
2756 (card->info.mcl_level[0] & 0x80)) {
2757 card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2758 card->info.mcl_level[0]];
2759 card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2760 card->info.mcl_level[1]];
2761 card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2762 card->info.mcl_level[2]];
2763 card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2764 card->info.mcl_level[3]];
2765 card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2767 break;
2768 default:
2769 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2771 dev_info(&card->gdev->dev,
2772 "Device is a%s card%s%s%s\nwith link type %s.\n",
2773 qeth_get_cardname(card),
2774 (card->info.mcl_level[0]) ? " (level: " : "",
2775 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2776 (card->info.mcl_level[0]) ? ")" : "",
2777 qeth_get_cardname_short(card));
2779 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2781 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2783 struct qeth_buffer_pool_entry *entry;
2785 QETH_CARD_TEXT(card, 5, "inwrklst");
2787 list_for_each_entry(entry,
2788 &card->qdio.init_pool.entry_list, init_list) {
2789 qeth_put_buffer_pool_entry(card, entry);
2793 static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2794 struct qeth_card *card)
2796 struct list_head *plh;
2797 struct qeth_buffer_pool_entry *entry;
2798 int i, free;
2799 struct page *page;
2801 if (list_empty(&card->qdio.in_buf_pool.entry_list))
2802 return NULL;
2804 list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2805 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2806 free = 1;
2807 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2808 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2809 free = 0;
2810 break;
2813 if (free) {
2814 list_del_init(&entry->list);
2815 return entry;
2819 /* no free buffer in pool so take first one and swap pages */
2820 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2821 struct qeth_buffer_pool_entry, list);
2822 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2823 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2824 page = alloc_page(GFP_ATOMIC);
2825 if (!page) {
2826 return NULL;
2827 } else {
2828 free_page((unsigned long)entry->elements[i]);
2829 entry->elements[i] = page_address(page);
2830 if (card->options.performance_stats)
2831 card->perf_stats.sg_alloc_page_rx++;
2835 list_del_init(&entry->list);
2836 return entry;
2839 static int qeth_init_input_buffer(struct qeth_card *card,
2840 struct qeth_qdio_buffer *buf)
2842 struct qeth_buffer_pool_entry *pool_entry;
2843 int i;
2845 if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2846 buf->rx_skb = netdev_alloc_skb(card->dev,
2847 QETH_RX_PULL_LEN + ETH_HLEN);
2848 if (!buf->rx_skb)
2849 return 1;
2852 pool_entry = qeth_find_free_buffer_pool_entry(card);
2853 if (!pool_entry)
2854 return 1;
2857 * since the buffer is accessed only from the input_tasklet
2858 * there shouldn't be a need to synchronize; also, since we use
2859 * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run out off
2860 * buffers
2863 buf->pool_entry = pool_entry;
2864 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2865 buf->buffer->element[i].length = PAGE_SIZE;
2866 buf->buffer->element[i].addr = pool_entry->elements[i];
2867 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2868 buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2869 else
2870 buf->buffer->element[i].eflags = 0;
2871 buf->buffer->element[i].sflags = 0;
2873 return 0;
2876 int qeth_init_qdio_queues(struct qeth_card *card)
2878 int i, j;
2879 int rc;
2881 QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2883 /* inbound queue */
2884 qdio_reset_buffers(card->qdio.in_q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2885 memset(&card->rx, 0, sizeof(struct qeth_rx));
2886 qeth_initialize_working_pool_list(card);
2887 /*give only as many buffers to hardware as we have buffer pool entries*/
2888 for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2889 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2890 card->qdio.in_q->next_buf_to_init =
2891 card->qdio.in_buf_pool.buf_count - 1;
2892 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2893 card->qdio.in_buf_pool.buf_count - 1);
2894 if (rc) {
2895 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2896 return rc;
2899 /* completion */
2900 rc = qeth_cq_init(card);
2901 if (rc) {
2902 return rc;
2905 /* outbound queue */
2906 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2907 qdio_reset_buffers(card->qdio.out_qs[i]->qdio_bufs,
2908 QDIO_MAX_BUFFERS_PER_Q);
2909 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2910 qeth_clear_output_buffer(card->qdio.out_qs[i],
2911 card->qdio.out_qs[i]->bufs[j],
2912 QETH_QDIO_BUF_EMPTY);
2914 card->qdio.out_qs[i]->card = card;
2915 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2916 card->qdio.out_qs[i]->do_pack = 0;
2917 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2918 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2919 atomic_set(&card->qdio.out_qs[i]->state,
2920 QETH_OUT_Q_UNLOCKED);
2922 return 0;
2924 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2926 static __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2928 switch (link_type) {
2929 case QETH_LINK_TYPE_HSTR:
2930 return 2;
2931 default:
2932 return 1;
2936 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2937 struct qeth_ipa_cmd *cmd, __u8 command,
2938 enum qeth_prot_versions prot)
2940 memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2941 cmd->hdr.command = command;
2942 cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2943 /* cmd->hdr.seqno is set by qeth_send_control_data() */
2944 cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2945 cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2946 if (card->options.layer2)
2947 cmd->hdr.prim_version_no = 2;
2948 else
2949 cmd->hdr.prim_version_no = 1;
2950 cmd->hdr.param_count = 1;
2951 cmd->hdr.prot_version = prot;
2952 cmd->hdr.ipa_supported = 0;
2953 cmd->hdr.ipa_enabled = 0;
2956 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2957 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2959 struct qeth_cmd_buffer *iob;
2961 iob = qeth_get_buffer(&card->write);
2962 if (iob) {
2963 qeth_fill_ipacmd_header(card, __ipa_cmd(iob), ipacmd, prot);
2964 } else {
2965 dev_warn(&card->gdev->dev,
2966 "The qeth driver ran out of channel command buffers\n");
2967 QETH_DBF_MESSAGE(1, "%s The qeth driver ran out of channel command buffers",
2968 dev_name(&card->gdev->dev));
2971 return iob;
2973 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2975 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2976 char prot_type)
2978 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2979 memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2980 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2981 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2983 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2986 * qeth_send_ipa_cmd() - send an IPA command
2988 * See qeth_send_control_data() for explanation of the arguments.
2991 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2992 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2993 unsigned long),
2994 void *reply_param)
2996 int rc;
2997 char prot_type;
2999 QETH_CARD_TEXT(card, 4, "sendipa");
3001 if (card->options.layer2)
3002 if (card->info.type == QETH_CARD_TYPE_OSN)
3003 prot_type = QETH_PROT_OSN2;
3004 else
3005 prot_type = QETH_PROT_LAYER2;
3006 else
3007 prot_type = QETH_PROT_TCPIP;
3008 qeth_prepare_ipa_cmd(card, iob, prot_type);
3009 rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
3010 iob, reply_cb, reply_param);
3011 if (rc == -ETIME) {
3012 qeth_clear_ipacmd_list(card);
3013 qeth_schedule_recovery(card);
3015 return rc;
3017 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
3019 static int qeth_send_startlan(struct qeth_card *card)
3021 int rc;
3022 struct qeth_cmd_buffer *iob;
3024 QETH_DBF_TEXT(SETUP, 2, "strtlan");
3026 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
3027 if (!iob)
3028 return -ENOMEM;
3029 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
3030 return rc;
3033 static int qeth_setadpparms_inspect_rc(struct qeth_ipa_cmd *cmd)
3035 if (!cmd->hdr.return_code)
3036 cmd->hdr.return_code =
3037 cmd->data.setadapterparms.hdr.return_code;
3038 return cmd->hdr.return_code;
3041 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
3042 struct qeth_reply *reply, unsigned long data)
3044 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3046 QETH_CARD_TEXT(card, 3, "quyadpcb");
3047 if (qeth_setadpparms_inspect_rc(cmd))
3048 return 0;
3050 if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
3051 card->info.link_type =
3052 cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
3053 QETH_DBF_TEXT_(SETUP, 2, "lnk %d", card->info.link_type);
3055 card->options.adp.supported_funcs =
3056 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
3057 return 0;
3060 static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
3061 __u32 command, __u32 cmdlen)
3063 struct qeth_cmd_buffer *iob;
3064 struct qeth_ipa_cmd *cmd;
3066 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
3067 QETH_PROT_IPV4);
3068 if (iob) {
3069 cmd = __ipa_cmd(iob);
3070 cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
3071 cmd->data.setadapterparms.hdr.command_code = command;
3072 cmd->data.setadapterparms.hdr.used_total = 1;
3073 cmd->data.setadapterparms.hdr.seq_no = 1;
3076 return iob;
3079 int qeth_query_setadapterparms(struct qeth_card *card)
3081 int rc;
3082 struct qeth_cmd_buffer *iob;
3084 QETH_CARD_TEXT(card, 3, "queryadp");
3085 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
3086 sizeof(struct qeth_ipacmd_setadpparms));
3087 if (!iob)
3088 return -ENOMEM;
3089 rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
3090 return rc;
3092 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
3094 static int qeth_query_ipassists_cb(struct qeth_card *card,
3095 struct qeth_reply *reply, unsigned long data)
3097 struct qeth_ipa_cmd *cmd;
3099 QETH_DBF_TEXT(SETUP, 2, "qipasscb");
3101 cmd = (struct qeth_ipa_cmd *) data;
3103 switch (cmd->hdr.return_code) {
3104 case IPA_RC_NOTSUPP:
3105 case IPA_RC_L2_UNSUPPORTED_CMD:
3106 QETH_DBF_TEXT(SETUP, 2, "ipaunsup");
3107 card->options.ipa4.supported_funcs |= IPA_SETADAPTERPARMS;
3108 card->options.ipa6.supported_funcs |= IPA_SETADAPTERPARMS;
3109 return -0;
3110 default:
3111 if (cmd->hdr.return_code) {
3112 QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Unhandled "
3113 "rc=%d\n",
3114 dev_name(&card->gdev->dev),
3115 cmd->hdr.return_code);
3116 return 0;
3120 if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
3121 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
3122 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
3123 } else if (cmd->hdr.prot_version == QETH_PROT_IPV6) {
3124 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
3125 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
3126 } else
3127 QETH_DBF_MESSAGE(1, "%s IPA_CMD_QIPASSIST: Flawed LIC detected"
3128 "\n", dev_name(&card->gdev->dev));
3129 return 0;
3132 int qeth_query_ipassists(struct qeth_card *card, enum qeth_prot_versions prot)
3134 int rc;
3135 struct qeth_cmd_buffer *iob;
3137 QETH_DBF_TEXT_(SETUP, 2, "qipassi%i", prot);
3138 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_QIPASSIST, prot);
3139 if (!iob)
3140 return -ENOMEM;
3141 rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
3142 return rc;
3144 EXPORT_SYMBOL_GPL(qeth_query_ipassists);
3146 static int qeth_query_switch_attributes_cb(struct qeth_card *card,
3147 struct qeth_reply *reply, unsigned long data)
3149 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3150 struct qeth_query_switch_attributes *attrs;
3151 struct qeth_switch_info *sw_info;
3153 QETH_CARD_TEXT(card, 2, "qswiatcb");
3154 if (qeth_setadpparms_inspect_rc(cmd))
3155 return 0;
3157 sw_info = (struct qeth_switch_info *)reply->param;
3158 attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3159 sw_info->capabilities = attrs->capabilities;
3160 sw_info->settings = attrs->settings;
3161 QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3162 sw_info->settings);
3163 return 0;
3166 int qeth_query_switch_attributes(struct qeth_card *card,
3167 struct qeth_switch_info *sw_info)
3169 struct qeth_cmd_buffer *iob;
3171 QETH_CARD_TEXT(card, 2, "qswiattr");
3172 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3173 return -EOPNOTSUPP;
3174 if (!netif_carrier_ok(card->dev))
3175 return -ENOMEDIUM;
3176 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES,
3177 sizeof(struct qeth_ipacmd_setadpparms_hdr));
3178 if (!iob)
3179 return -ENOMEM;
3180 return qeth_send_ipa_cmd(card, iob,
3181 qeth_query_switch_attributes_cb, sw_info);
3183 EXPORT_SYMBOL_GPL(qeth_query_switch_attributes);
3185 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3186 struct qeth_reply *reply, unsigned long data)
3188 struct qeth_ipa_cmd *cmd;
3189 __u16 rc;
3191 cmd = (struct qeth_ipa_cmd *)data;
3192 rc = cmd->hdr.return_code;
3193 if (rc)
3194 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3195 else
3196 card->info.diagass_support = cmd->data.diagass.ext;
3197 return 0;
3200 static int qeth_query_setdiagass(struct qeth_card *card)
3202 struct qeth_cmd_buffer *iob;
3203 struct qeth_ipa_cmd *cmd;
3205 QETH_DBF_TEXT(SETUP, 2, "qdiagass");
3206 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3207 if (!iob)
3208 return -ENOMEM;
3209 cmd = __ipa_cmd(iob);
3210 cmd->data.diagass.subcmd_len = 16;
3211 cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
3212 return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3215 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3217 unsigned long info = get_zeroed_page(GFP_KERNEL);
3218 struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3219 struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3220 struct ccw_dev_id ccwid;
3221 int level;
3223 tid->chpid = card->info.chpid;
3224 ccw_device_get_id(CARD_RDEV(card), &ccwid);
3225 tid->ssid = ccwid.ssid;
3226 tid->devno = ccwid.devno;
3227 if (!info)
3228 return;
3229 level = stsi(NULL, 0, 0, 0);
3230 if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3231 tid->lparnr = info222->lpar_number;
3232 if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3233 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3234 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3236 free_page(info);
3237 return;
3240 static int qeth_hw_trap_cb(struct qeth_card *card,
3241 struct qeth_reply *reply, unsigned long data)
3243 struct qeth_ipa_cmd *cmd;
3244 __u16 rc;
3246 cmd = (struct qeth_ipa_cmd *)data;
3247 rc = cmd->hdr.return_code;
3248 if (rc)
3249 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3250 return 0;
3253 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3255 struct qeth_cmd_buffer *iob;
3256 struct qeth_ipa_cmd *cmd;
3258 QETH_DBF_TEXT(SETUP, 2, "diagtrap");
3259 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3260 if (!iob)
3261 return -ENOMEM;
3262 cmd = __ipa_cmd(iob);
3263 cmd->data.diagass.subcmd_len = 80;
3264 cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
3265 cmd->data.diagass.type = 1;
3266 cmd->data.diagass.action = action;
3267 switch (action) {
3268 case QETH_DIAGS_TRAP_ARM:
3269 cmd->data.diagass.options = 0x0003;
3270 cmd->data.diagass.ext = 0x00010000 +
3271 sizeof(struct qeth_trap_id);
3272 qeth_get_trap_id(card,
3273 (struct qeth_trap_id *)cmd->data.diagass.cdata);
3274 break;
3275 case QETH_DIAGS_TRAP_DISARM:
3276 cmd->data.diagass.options = 0x0001;
3277 break;
3278 case QETH_DIAGS_TRAP_CAPTURE:
3279 break;
3281 return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3283 EXPORT_SYMBOL_GPL(qeth_hw_trap);
3285 static int qeth_check_qdio_errors(struct qeth_card *card,
3286 struct qdio_buffer *buf,
3287 unsigned int qdio_error,
3288 const char *dbftext)
3290 if (qdio_error) {
3291 QETH_CARD_TEXT(card, 2, dbftext);
3292 QETH_CARD_TEXT_(card, 2, " F15=%02X",
3293 buf->element[15].sflags);
3294 QETH_CARD_TEXT_(card, 2, " F14=%02X",
3295 buf->element[14].sflags);
3296 QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3297 if ((buf->element[15].sflags) == 0x12) {
3298 card->stats.rx_dropped++;
3299 return 0;
3300 } else
3301 return 1;
3303 return 0;
3306 static void qeth_queue_input_buffer(struct qeth_card *card, int index)
3308 struct qeth_qdio_q *queue = card->qdio.in_q;
3309 struct list_head *lh;
3310 int count;
3311 int i;
3312 int rc;
3313 int newcount = 0;
3315 count = (index < queue->next_buf_to_init)?
3316 card->qdio.in_buf_pool.buf_count -
3317 (queue->next_buf_to_init - index) :
3318 card->qdio.in_buf_pool.buf_count -
3319 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
3320 /* only requeue at a certain threshold to avoid SIGAs */
3321 if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3322 for (i = queue->next_buf_to_init;
3323 i < queue->next_buf_to_init + count; ++i) {
3324 if (qeth_init_input_buffer(card,
3325 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
3326 break;
3327 } else {
3328 newcount++;
3332 if (newcount < count) {
3333 /* we are in memory shortage so we switch back to
3334 traditional skb allocation and drop packages */
3335 atomic_set(&card->force_alloc_skb, 3);
3336 count = newcount;
3337 } else {
3338 atomic_add_unless(&card->force_alloc_skb, -1, 0);
3341 if (!count) {
3342 i = 0;
3343 list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3344 i++;
3345 if (i == card->qdio.in_buf_pool.buf_count) {
3346 QETH_CARD_TEXT(card, 2, "qsarbw");
3347 card->reclaim_index = index;
3348 schedule_delayed_work(
3349 &card->buffer_reclaim_work,
3350 QETH_RECLAIM_WORK_TIME);
3352 return;
3356 * according to old code it should be avoided to requeue all
3357 * 128 buffers in order to benefit from PCI avoidance.
3358 * this function keeps at least one buffer (the buffer at
3359 * 'index') un-requeued -> this buffer is the first buffer that
3360 * will be requeued the next time
3362 if (card->options.performance_stats) {
3363 card->perf_stats.inbound_do_qdio_cnt++;
3364 card->perf_stats.inbound_do_qdio_start_time =
3365 qeth_get_micros();
3367 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3368 queue->next_buf_to_init, count);
3369 if (card->options.performance_stats)
3370 card->perf_stats.inbound_do_qdio_time +=
3371 qeth_get_micros() -
3372 card->perf_stats.inbound_do_qdio_start_time;
3373 if (rc) {
3374 QETH_CARD_TEXT(card, 2, "qinberr");
3376 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
3377 QDIO_MAX_BUFFERS_PER_Q;
3381 static void qeth_buffer_reclaim_work(struct work_struct *work)
3383 struct qeth_card *card = container_of(work, struct qeth_card,
3384 buffer_reclaim_work.work);
3386 QETH_CARD_TEXT_(card, 2, "brw:%x", card->reclaim_index);
3387 qeth_queue_input_buffer(card, card->reclaim_index);
3390 static void qeth_handle_send_error(struct qeth_card *card,
3391 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3393 int sbalf15 = buffer->buffer->element[15].sflags;
3395 QETH_CARD_TEXT(card, 6, "hdsnderr");
3396 if (card->info.type == QETH_CARD_TYPE_IQD) {
3397 if (sbalf15 == 0) {
3398 qdio_err = 0;
3399 } else {
3400 qdio_err = 1;
3403 qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3405 if (!qdio_err)
3406 return;
3408 if ((sbalf15 >= 15) && (sbalf15 <= 31))
3409 return;
3411 QETH_CARD_TEXT(card, 1, "lnkfail");
3412 QETH_CARD_TEXT_(card, 1, "%04x %02x",
3413 (u16)qdio_err, (u8)sbalf15);
3417 * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3418 * @queue: queue to check for packing buffer
3420 * Returns number of buffers that were prepared for flush.
3422 static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3424 struct qeth_qdio_out_buffer *buffer;
3426 buffer = queue->bufs[queue->next_buf_to_fill];
3427 if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3428 (buffer->next_element_to_fill > 0)) {
3429 /* it's a packing buffer */
3430 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3431 queue->next_buf_to_fill =
3432 (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
3433 return 1;
3435 return 0;
3439 * Switched to packing state if the number of used buffers on a queue
3440 * reaches a certain limit.
3442 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3444 if (!queue->do_pack) {
3445 if (atomic_read(&queue->used_buffers)
3446 >= QETH_HIGH_WATERMARK_PACK){
3447 /* switch non-PACKING -> PACKING */
3448 QETH_CARD_TEXT(queue->card, 6, "np->pack");
3449 if (queue->card->options.performance_stats)
3450 queue->card->perf_stats.sc_dp_p++;
3451 queue->do_pack = 1;
3457 * Switches from packing to non-packing mode. If there is a packing
3458 * buffer on the queue this buffer will be prepared to be flushed.
3459 * In that case 1 is returned to inform the caller. If no buffer
3460 * has to be flushed, zero is returned.
3462 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3464 if (queue->do_pack) {
3465 if (atomic_read(&queue->used_buffers)
3466 <= QETH_LOW_WATERMARK_PACK) {
3467 /* switch PACKING -> non-PACKING */
3468 QETH_CARD_TEXT(queue->card, 6, "pack->np");
3469 if (queue->card->options.performance_stats)
3470 queue->card->perf_stats.sc_p_dp++;
3471 queue->do_pack = 0;
3472 return qeth_prep_flush_pack_buffer(queue);
3475 return 0;
3478 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3479 int count)
3481 struct qeth_qdio_out_buffer *buf;
3482 int rc;
3483 int i;
3484 unsigned int qdio_flags;
3486 for (i = index; i < index + count; ++i) {
3487 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3488 buf = queue->bufs[bidx];
3489 buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3490 SBAL_EFLAGS_LAST_ENTRY;
3492 if (queue->bufstates)
3493 queue->bufstates[bidx].user = buf;
3495 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
3496 continue;
3498 if (!queue->do_pack) {
3499 if ((atomic_read(&queue->used_buffers) >=
3500 (QETH_HIGH_WATERMARK_PACK -
3501 QETH_WATERMARK_PACK_FUZZ)) &&
3502 !atomic_read(&queue->set_pci_flags_count)) {
3503 /* it's likely that we'll go to packing
3504 * mode soon */
3505 atomic_inc(&queue->set_pci_flags_count);
3506 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3508 } else {
3509 if (!atomic_read(&queue->set_pci_flags_count)) {
3511 * there's no outstanding PCI any more, so we
3512 * have to request a PCI to be sure the the PCI
3513 * will wake at some time in the future then we
3514 * can flush packed buffers that might still be
3515 * hanging around, which can happen if no
3516 * further send was requested by the stack
3518 atomic_inc(&queue->set_pci_flags_count);
3519 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3524 netif_trans_update(queue->card->dev);
3525 if (queue->card->options.performance_stats) {
3526 queue->card->perf_stats.outbound_do_qdio_cnt++;
3527 queue->card->perf_stats.outbound_do_qdio_start_time =
3528 qeth_get_micros();
3530 qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3531 if (atomic_read(&queue->set_pci_flags_count))
3532 qdio_flags |= QDIO_FLAG_PCI_OUT;
3533 atomic_add(count, &queue->used_buffers);
3535 rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
3536 queue->queue_no, index, count);
3537 if (queue->card->options.performance_stats)
3538 queue->card->perf_stats.outbound_do_qdio_time +=
3539 qeth_get_micros() -
3540 queue->card->perf_stats.outbound_do_qdio_start_time;
3541 if (rc) {
3542 queue->card->stats.tx_errors += count;
3543 /* ignore temporary SIGA errors without busy condition */
3544 if (rc == -ENOBUFS)
3545 return;
3546 QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3547 QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3548 QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3549 QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3550 QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3552 /* this must not happen under normal circumstances. if it
3553 * happens something is really wrong -> recover */
3554 qeth_schedule_recovery(queue->card);
3555 return;
3557 if (queue->card->options.performance_stats)
3558 queue->card->perf_stats.bufs_sent += count;
3561 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3563 int index;
3564 int flush_cnt = 0;
3565 int q_was_packing = 0;
3568 * check if weed have to switch to non-packing mode or if
3569 * we have to get a pci flag out on the queue
3571 if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3572 !atomic_read(&queue->set_pci_flags_count)) {
3573 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
3574 QETH_OUT_Q_UNLOCKED) {
3576 * If we get in here, there was no action in
3577 * do_send_packet. So, we check if there is a
3578 * packing buffer to be flushed here.
3580 netif_stop_queue(queue->card->dev);
3581 index = queue->next_buf_to_fill;
3582 q_was_packing = queue->do_pack;
3583 /* queue->do_pack may change */
3584 barrier();
3585 flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3586 if (!flush_cnt &&
3587 !atomic_read(&queue->set_pci_flags_count))
3588 flush_cnt += qeth_prep_flush_pack_buffer(queue);
3589 if (queue->card->options.performance_stats &&
3590 q_was_packing)
3591 queue->card->perf_stats.bufs_sent_pack +=
3592 flush_cnt;
3593 if (flush_cnt)
3594 qeth_flush_buffers(queue, index, flush_cnt);
3595 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3600 static void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3601 unsigned long card_ptr)
3603 struct qeth_card *card = (struct qeth_card *)card_ptr;
3605 if (card->dev && (card->dev->flags & IFF_UP))
3606 napi_schedule(&card->napi);
3609 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3611 int rc;
3613 if (card->options.cq == QETH_CQ_NOTAVAILABLE) {
3614 rc = -1;
3615 goto out;
3616 } else {
3617 if (card->options.cq == cq) {
3618 rc = 0;
3619 goto out;
3622 if (card->state != CARD_STATE_DOWN &&
3623 card->state != CARD_STATE_RECOVER) {
3624 rc = -1;
3625 goto out;
3628 qeth_free_qdio_buffers(card);
3629 card->options.cq = cq;
3630 rc = 0;
3632 out:
3633 return rc;
3636 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3639 static void qeth_qdio_cq_handler(struct qeth_card *card,
3640 unsigned int qdio_err,
3641 unsigned int queue, int first_element, int count) {
3642 struct qeth_qdio_q *cq = card->qdio.c_q;
3643 int i;
3644 int rc;
3646 if (!qeth_is_cq(card, queue))
3647 goto out;
3649 QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3650 QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3651 QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3653 if (qdio_err) {
3654 netif_stop_queue(card->dev);
3655 qeth_schedule_recovery(card);
3656 goto out;
3659 if (card->options.performance_stats) {
3660 card->perf_stats.cq_cnt++;
3661 card->perf_stats.cq_start_time = qeth_get_micros();
3664 for (i = first_element; i < first_element + count; ++i) {
3665 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3666 struct qdio_buffer *buffer = cq->qdio_bufs[bidx];
3667 int e;
3669 e = 0;
3670 while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3671 buffer->element[e].addr) {
3672 unsigned long phys_aob_addr;
3674 phys_aob_addr = (unsigned long) buffer->element[e].addr;
3675 qeth_qdio_handle_aob(card, phys_aob_addr);
3676 buffer->element[e].addr = NULL;
3677 buffer->element[e].eflags = 0;
3678 buffer->element[e].sflags = 0;
3679 buffer->element[e].length = 0;
3681 ++e;
3684 buffer->element[15].eflags = 0;
3685 buffer->element[15].sflags = 0;
3687 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3688 card->qdio.c_q->next_buf_to_init,
3689 count);
3690 if (rc) {
3691 dev_warn(&card->gdev->dev,
3692 "QDIO reported an error, rc=%i\n", rc);
3693 QETH_CARD_TEXT(card, 2, "qcqherr");
3695 card->qdio.c_q->next_buf_to_init = (card->qdio.c_q->next_buf_to_init
3696 + count) % QDIO_MAX_BUFFERS_PER_Q;
3698 netif_wake_queue(card->dev);
3700 if (card->options.performance_stats) {
3701 int delta_t = qeth_get_micros();
3702 delta_t -= card->perf_stats.cq_start_time;
3703 card->perf_stats.cq_time += delta_t;
3705 out:
3706 return;
3709 static void qeth_qdio_input_handler(struct ccw_device *ccwdev,
3710 unsigned int qdio_err, int queue,
3711 int first_elem, int count,
3712 unsigned long card_ptr)
3714 struct qeth_card *card = (struct qeth_card *)card_ptr;
3716 QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3717 QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3719 if (qeth_is_cq(card, queue))
3720 qeth_qdio_cq_handler(card, qdio_err, queue, first_elem, count);
3721 else if (qdio_err)
3722 qeth_schedule_recovery(card);
3725 static void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3726 unsigned int qdio_error, int __queue,
3727 int first_element, int count,
3728 unsigned long card_ptr)
3730 struct qeth_card *card = (struct qeth_card *) card_ptr;
3731 struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3732 struct qeth_qdio_out_buffer *buffer;
3733 int i;
3735 QETH_CARD_TEXT(card, 6, "qdouhdl");
3736 if (qdio_error & QDIO_ERROR_FATAL) {
3737 QETH_CARD_TEXT(card, 2, "achkcond");
3738 netif_stop_queue(card->dev);
3739 qeth_schedule_recovery(card);
3740 return;
3742 if (card->options.performance_stats) {
3743 card->perf_stats.outbound_handler_cnt++;
3744 card->perf_stats.outbound_handler_start_time =
3745 qeth_get_micros();
3747 for (i = first_element; i < (first_element + count); ++i) {
3748 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3749 buffer = queue->bufs[bidx];
3750 qeth_handle_send_error(card, buffer, qdio_error);
3752 if (queue->bufstates &&
3753 (queue->bufstates[bidx].flags &
3754 QDIO_OUTBUF_STATE_FLAG_PENDING) != 0) {
3755 WARN_ON_ONCE(card->options.cq != QETH_CQ_ENABLED);
3757 if (atomic_cmpxchg(&buffer->state,
3758 QETH_QDIO_BUF_PRIMED,
3759 QETH_QDIO_BUF_PENDING) ==
3760 QETH_QDIO_BUF_PRIMED) {
3761 qeth_notify_skbs(queue, buffer,
3762 TX_NOTIFY_PENDING);
3764 buffer->aob = queue->bufstates[bidx].aob;
3765 QETH_CARD_TEXT_(queue->card, 5, "pel%d", bidx);
3766 QETH_CARD_TEXT(queue->card, 5, "aob");
3767 QETH_CARD_TEXT_(queue->card, 5, "%lx",
3768 virt_to_phys(buffer->aob));
3770 /* prepare the queue slot for re-use: */
3771 qeth_scrub_qdio_buffer(buffer->buffer,
3772 QETH_MAX_BUFFER_ELEMENTS(card));
3773 if (qeth_init_qdio_out_buf(queue, bidx)) {
3774 QETH_CARD_TEXT(card, 2, "outofbuf");
3775 qeth_schedule_recovery(card);
3777 } else {
3778 if (card->options.cq == QETH_CQ_ENABLED) {
3779 enum iucv_tx_notify n;
3781 n = qeth_compute_cq_notification(
3782 buffer->buffer->element[15].sflags, 0);
3783 qeth_notify_skbs(queue, buffer, n);
3786 qeth_clear_output_buffer(queue, buffer,
3787 QETH_QDIO_BUF_EMPTY);
3789 qeth_cleanup_handled_pending(queue, bidx, 0);
3791 atomic_sub(count, &queue->used_buffers);
3792 /* check if we need to do something on this outbound queue */
3793 if (card->info.type != QETH_CARD_TYPE_IQD)
3794 qeth_check_outbound_queue(queue);
3796 netif_wake_queue(queue->card->dev);
3797 if (card->options.performance_stats)
3798 card->perf_stats.outbound_handler_time += qeth_get_micros() -
3799 card->perf_stats.outbound_handler_start_time;
3802 /* We cannot use outbound queue 3 for unicast packets on HiperSockets */
3803 static inline int qeth_cut_iqd_prio(struct qeth_card *card, int queue_num)
3805 if ((card->info.type == QETH_CARD_TYPE_IQD) && (queue_num == 3))
3806 return 2;
3807 return queue_num;
3811 * Note: Function assumes that we have 4 outbound queues.
3813 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
3814 int ipv, int cast_type)
3816 __be16 *tci;
3817 u8 tos;
3819 if (cast_type && card->info.is_multicast_different)
3820 return card->info.is_multicast_different &
3821 (card->qdio.no_out_queues - 1);
3823 switch (card->qdio.do_prio_queueing) {
3824 case QETH_PRIO_Q_ING_TOS:
3825 case QETH_PRIO_Q_ING_PREC:
3826 switch (ipv) {
3827 case 4:
3828 tos = ipv4_get_dsfield(ip_hdr(skb));
3829 break;
3830 case 6:
3831 tos = ipv6_get_dsfield(ipv6_hdr(skb));
3832 break;
3833 default:
3834 return card->qdio.default_out_queue;
3836 if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3837 return qeth_cut_iqd_prio(card, ~tos >> 6 & 3);
3838 if (tos & IPTOS_MINCOST)
3839 return qeth_cut_iqd_prio(card, 3);
3840 if (tos & IPTOS_RELIABILITY)
3841 return 2;
3842 if (tos & IPTOS_THROUGHPUT)
3843 return 1;
3844 if (tos & IPTOS_LOWDELAY)
3845 return 0;
3846 break;
3847 case QETH_PRIO_Q_ING_SKB:
3848 if (skb->priority > 5)
3849 return 0;
3850 return qeth_cut_iqd_prio(card, ~skb->priority >> 1 & 3);
3851 case QETH_PRIO_Q_ING_VLAN:
3852 tci = &((struct ethhdr *)skb->data)->h_proto;
3853 if (be16_to_cpu(*tci) == ETH_P_8021Q)
3854 return qeth_cut_iqd_prio(card,
3855 ~be16_to_cpu(*(tci + 1)) >> (VLAN_PRIO_SHIFT + 1) & 3);
3856 break;
3857 default:
3858 break;
3860 return card->qdio.default_out_queue;
3862 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
3865 * qeth_get_elements_for_frags() - find number of SBALEs for skb frags.
3866 * @skb: SKB address
3868 * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3869 * fragmented part of the SKB. Returns zero for linear SKB.
3871 int qeth_get_elements_for_frags(struct sk_buff *skb)
3873 int cnt, elements = 0;
3875 for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3876 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[cnt];
3878 elements += qeth_get_elements_for_range(
3879 (addr_t)skb_frag_address(frag),
3880 (addr_t)skb_frag_address(frag) + skb_frag_size(frag));
3882 return elements;
3884 EXPORT_SYMBOL_GPL(qeth_get_elements_for_frags);
3887 * qeth_get_elements_no() - find number of SBALEs for skb data, inc. frags.
3888 * @card: qeth card structure, to check max. elems.
3889 * @skb: SKB address
3890 * @extra_elems: extra elems needed, to check against max.
3891 * @data_offset: range starts at skb->data + data_offset
3893 * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3894 * skb data, including linear part and fragments. Checks if the result plus
3895 * extra_elems fits under the limit for the card. Returns 0 if it does not.
3896 * Note: extra_elems is not included in the returned result.
3898 int qeth_get_elements_no(struct qeth_card *card,
3899 struct sk_buff *skb, int extra_elems, int data_offset)
3901 addr_t end = (addr_t)skb->data + skb_headlen(skb);
3902 int elements = qeth_get_elements_for_frags(skb);
3903 addr_t start = (addr_t)skb->data + data_offset;
3905 if (start != end)
3906 elements += qeth_get_elements_for_range(start, end);
3908 if ((elements + extra_elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
3909 QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
3910 "(Number=%d / Length=%d). Discarded.\n",
3911 elements + extra_elems, skb->len);
3912 return 0;
3914 return elements;
3916 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
3918 int qeth_hdr_chk_and_bounce(struct sk_buff *skb, struct qeth_hdr **hdr, int len)
3920 int hroom, inpage, rest;
3922 if (((unsigned long)skb->data & PAGE_MASK) !=
3923 (((unsigned long)skb->data + len - 1) & PAGE_MASK)) {
3924 hroom = skb_headroom(skb);
3925 inpage = PAGE_SIZE - ((unsigned long) skb->data % PAGE_SIZE);
3926 rest = len - inpage;
3927 if (rest > hroom)
3928 return 1;
3929 memmove(skb->data - rest, skb->data, skb_headlen(skb));
3930 skb->data -= rest;
3931 skb->tail -= rest;
3932 *hdr = (struct qeth_hdr *)skb->data;
3933 QETH_DBF_MESSAGE(2, "skb bounce len: %d rest: %d\n", len, rest);
3935 return 0;
3937 EXPORT_SYMBOL_GPL(qeth_hdr_chk_and_bounce);
3940 * qeth_push_hdr() - push a qeth_hdr onto an skb.
3941 * @skb: skb that the qeth_hdr should be pushed onto.
3942 * @hdr: double pointer to a qeth_hdr. When returning with >= 0,
3943 * it contains a valid pointer to a qeth_hdr.
3944 * @len: length of the hdr that needs to be pushed on.
3946 * Returns the pushed length. If the header can't be pushed on
3947 * (eg. because it would cross a page boundary), it is allocated from
3948 * the cache instead and 0 is returned.
3949 * Error to create the hdr is indicated by returning with < 0.
3951 int qeth_push_hdr(struct sk_buff *skb, struct qeth_hdr **hdr, unsigned int len)
3953 if (skb_headroom(skb) >= len &&
3954 qeth_get_elements_for_range((addr_t)skb->data - len,
3955 (addr_t)skb->data) == 1) {
3956 *hdr = skb_push(skb, len);
3957 return len;
3959 /* fall back */
3960 *hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
3961 if (!*hdr)
3962 return -ENOMEM;
3963 return 0;
3965 EXPORT_SYMBOL_GPL(qeth_push_hdr);
3967 static void __qeth_fill_buffer(struct sk_buff *skb,
3968 struct qeth_qdio_out_buffer *buf,
3969 bool is_first_elem, unsigned int offset)
3971 struct qdio_buffer *buffer = buf->buffer;
3972 int element = buf->next_element_to_fill;
3973 int length = skb_headlen(skb) - offset;
3974 char *data = skb->data + offset;
3975 int length_here, cnt;
3977 /* map linear part into buffer element(s) */
3978 while (length > 0) {
3979 /* length_here is the remaining amount of data in this page */
3980 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3981 if (length < length_here)
3982 length_here = length;
3984 buffer->element[element].addr = data;
3985 buffer->element[element].length = length_here;
3986 length -= length_here;
3987 if (is_first_elem) {
3988 is_first_elem = false;
3989 if (length || skb_is_nonlinear(skb))
3990 /* skb needs additional elements */
3991 buffer->element[element].eflags =
3992 SBAL_EFLAGS_FIRST_FRAG;
3993 else
3994 buffer->element[element].eflags = 0;
3995 } else {
3996 buffer->element[element].eflags =
3997 SBAL_EFLAGS_MIDDLE_FRAG;
3999 data += length_here;
4000 element++;
4003 /* map page frags into buffer element(s) */
4004 for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
4005 skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
4007 data = skb_frag_address(frag);
4008 length = skb_frag_size(frag);
4009 while (length > 0) {
4010 length_here = PAGE_SIZE -
4011 ((unsigned long) data % PAGE_SIZE);
4012 if (length < length_here)
4013 length_here = length;
4015 buffer->element[element].addr = data;
4016 buffer->element[element].length = length_here;
4017 buffer->element[element].eflags =
4018 SBAL_EFLAGS_MIDDLE_FRAG;
4019 length -= length_here;
4020 data += length_here;
4021 element++;
4025 if (buffer->element[element - 1].eflags)
4026 buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
4027 buf->next_element_to_fill = element;
4031 * qeth_fill_buffer() - map skb into an output buffer
4032 * @queue: QDIO queue to submit the buffer on
4033 * @buf: buffer to transport the skb
4034 * @skb: skb to map into the buffer
4035 * @hdr: qeth_hdr for this skb. Either at skb->data, or allocated
4036 * from qeth_core_header_cache.
4037 * @offset: when mapping the skb, start at skb->data + offset
4038 * @hd_len: if > 0, build a dedicated header element of this size
4040 static int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
4041 struct qeth_qdio_out_buffer *buf,
4042 struct sk_buff *skb, struct qeth_hdr *hdr,
4043 unsigned int offset, unsigned int hd_len)
4045 struct qdio_buffer *buffer = buf->buffer;
4046 bool is_first_elem = true;
4047 int flush_cnt = 0;
4049 refcount_inc(&skb->users);
4050 skb_queue_tail(&buf->skb_list, skb);
4052 /* build dedicated header element */
4053 if (hd_len) {
4054 int element = buf->next_element_to_fill;
4055 is_first_elem = false;
4057 buffer->element[element].addr = hdr;
4058 buffer->element[element].length = hd_len;
4059 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
4060 /* remember to free cache-allocated qeth_hdr: */
4061 buf->is_header[element] = ((void *)hdr != skb->data);
4062 buf->next_element_to_fill++;
4065 __qeth_fill_buffer(skb, buf, is_first_elem, offset);
4067 if (!queue->do_pack) {
4068 QETH_CARD_TEXT(queue->card, 6, "fillbfnp");
4069 /* set state to PRIMED -> will be flushed */
4070 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4071 flush_cnt = 1;
4072 } else {
4073 QETH_CARD_TEXT(queue->card, 6, "fillbfpa");
4074 if (queue->card->options.performance_stats)
4075 queue->card->perf_stats.skbs_sent_pack++;
4076 if (buf->next_element_to_fill >=
4077 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
4079 * packed buffer if full -> set state PRIMED
4080 * -> will be flushed
4082 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4083 flush_cnt = 1;
4086 return flush_cnt;
4089 int qeth_do_send_packet_fast(struct qeth_qdio_out_q *queue, struct sk_buff *skb,
4090 struct qeth_hdr *hdr, unsigned int offset,
4091 unsigned int hd_len)
4093 int index = queue->next_buf_to_fill;
4094 struct qeth_qdio_out_buffer *buffer = queue->bufs[index];
4097 * check if buffer is empty to make sure that we do not 'overtake'
4098 * ourselves and try to fill a buffer that is already primed
4100 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4101 return -EBUSY;
4102 queue->next_buf_to_fill = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
4103 qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4104 qeth_flush_buffers(queue, index, 1);
4105 return 0;
4107 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
4109 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4110 struct sk_buff *skb, struct qeth_hdr *hdr,
4111 unsigned int offset, unsigned int hd_len,
4112 int elements_needed)
4114 struct qeth_qdio_out_buffer *buffer;
4115 int start_index;
4116 int flush_count = 0;
4117 int do_pack = 0;
4118 int tmp;
4119 int rc = 0;
4121 /* spin until we get the queue ... */
4122 while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
4123 QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
4124 start_index = queue->next_buf_to_fill;
4125 buffer = queue->bufs[queue->next_buf_to_fill];
4127 * check if buffer is empty to make sure that we do not 'overtake'
4128 * ourselves and try to fill a buffer that is already primed
4130 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
4131 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4132 return -EBUSY;
4134 /* check if we need to switch packing state of this queue */
4135 qeth_switch_to_packing_if_needed(queue);
4136 if (queue->do_pack) {
4137 do_pack = 1;
4138 /* does packet fit in current buffer? */
4139 if ((QETH_MAX_BUFFER_ELEMENTS(card) -
4140 buffer->next_element_to_fill) < elements_needed) {
4141 /* ... no -> set state PRIMED */
4142 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4143 flush_count++;
4144 queue->next_buf_to_fill =
4145 (queue->next_buf_to_fill + 1) %
4146 QDIO_MAX_BUFFERS_PER_Q;
4147 buffer = queue->bufs[queue->next_buf_to_fill];
4148 /* we did a step forward, so check buffer state
4149 * again */
4150 if (atomic_read(&buffer->state) !=
4151 QETH_QDIO_BUF_EMPTY) {
4152 qeth_flush_buffers(queue, start_index,
4153 flush_count);
4154 atomic_set(&queue->state,
4155 QETH_OUT_Q_UNLOCKED);
4156 rc = -EBUSY;
4157 goto out;
4161 tmp = qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4162 queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
4163 QDIO_MAX_BUFFERS_PER_Q;
4164 flush_count += tmp;
4165 if (flush_count)
4166 qeth_flush_buffers(queue, start_index, flush_count);
4167 else if (!atomic_read(&queue->set_pci_flags_count))
4168 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
4170 * queue->state will go from LOCKED -> UNLOCKED or from
4171 * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
4172 * (switch packing state or flush buffer to get another pci flag out).
4173 * In that case we will enter this loop
4175 while (atomic_dec_return(&queue->state)) {
4176 start_index = queue->next_buf_to_fill;
4177 /* check if we can go back to non-packing state */
4178 tmp = qeth_switch_to_nonpacking_if_needed(queue);
4180 * check if we need to flush a packing buffer to get a pci
4181 * flag out on the queue
4183 if (!tmp && !atomic_read(&queue->set_pci_flags_count))
4184 tmp = qeth_prep_flush_pack_buffer(queue);
4185 if (tmp) {
4186 qeth_flush_buffers(queue, start_index, tmp);
4187 flush_count += tmp;
4190 out:
4191 /* at this point the queue is UNLOCKED again */
4192 if (queue->card->options.performance_stats && do_pack)
4193 queue->card->perf_stats.bufs_sent_pack += flush_count;
4195 return rc;
4197 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
4199 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4200 struct qeth_reply *reply, unsigned long data)
4202 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4203 struct qeth_ipacmd_setadpparms *setparms;
4205 QETH_CARD_TEXT(card, 4, "prmadpcb");
4207 setparms = &(cmd->data.setadapterparms);
4208 if (qeth_setadpparms_inspect_rc(cmd)) {
4209 QETH_CARD_TEXT_(card, 4, "prmrc%x", cmd->hdr.return_code);
4210 setparms->data.mode = SET_PROMISC_MODE_OFF;
4212 card->info.promisc_mode = setparms->data.mode;
4213 return 0;
4216 void qeth_setadp_promisc_mode(struct qeth_card *card)
4218 enum qeth_ipa_promisc_modes mode;
4219 struct net_device *dev = card->dev;
4220 struct qeth_cmd_buffer *iob;
4221 struct qeth_ipa_cmd *cmd;
4223 QETH_CARD_TEXT(card, 4, "setprom");
4225 if (((dev->flags & IFF_PROMISC) &&
4226 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
4227 (!(dev->flags & IFF_PROMISC) &&
4228 (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
4229 return;
4230 mode = SET_PROMISC_MODE_OFF;
4231 if (dev->flags & IFF_PROMISC)
4232 mode = SET_PROMISC_MODE_ON;
4233 QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4235 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4236 sizeof(struct qeth_ipacmd_setadpparms_hdr) + 8);
4237 if (!iob)
4238 return;
4239 cmd = __ipa_cmd(iob);
4240 cmd->data.setadapterparms.data.mode = mode;
4241 qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4243 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4245 int qeth_change_mtu(struct net_device *dev, int new_mtu)
4247 struct qeth_card *card;
4248 char dbf_text[15];
4250 card = dev->ml_priv;
4252 QETH_CARD_TEXT(card, 4, "chgmtu");
4253 sprintf(dbf_text, "%8x", new_mtu);
4254 QETH_CARD_TEXT(card, 4, dbf_text);
4256 if (!qeth_mtu_is_valid(card, new_mtu))
4257 return -EINVAL;
4258 dev->mtu = new_mtu;
4259 return 0;
4261 EXPORT_SYMBOL_GPL(qeth_change_mtu);
4263 struct net_device_stats *qeth_get_stats(struct net_device *dev)
4265 struct qeth_card *card;
4267 card = dev->ml_priv;
4269 QETH_CARD_TEXT(card, 5, "getstat");
4271 return &card->stats;
4273 EXPORT_SYMBOL_GPL(qeth_get_stats);
4275 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4276 struct qeth_reply *reply, unsigned long data)
4278 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4280 QETH_CARD_TEXT(card, 4, "chgmaccb");
4281 if (qeth_setadpparms_inspect_rc(cmd))
4282 return 0;
4284 if (!card->options.layer2 ||
4285 !(card->info.mac_bits & QETH_LAYER2_MAC_READ)) {
4286 ether_addr_copy(card->dev->dev_addr,
4287 cmd->data.setadapterparms.data.change_addr.addr);
4288 card->info.mac_bits |= QETH_LAYER2_MAC_READ;
4290 return 0;
4293 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4295 int rc;
4296 struct qeth_cmd_buffer *iob;
4297 struct qeth_ipa_cmd *cmd;
4299 QETH_CARD_TEXT(card, 4, "chgmac");
4301 iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4302 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4303 sizeof(struct qeth_change_addr));
4304 if (!iob)
4305 return -ENOMEM;
4306 cmd = __ipa_cmd(iob);
4307 cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4308 cmd->data.setadapterparms.data.change_addr.addr_size = ETH_ALEN;
4309 ether_addr_copy(cmd->data.setadapterparms.data.change_addr.addr,
4310 card->dev->dev_addr);
4311 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4312 NULL);
4313 return rc;
4315 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4317 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4318 struct qeth_reply *reply, unsigned long data)
4320 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4321 struct qeth_set_access_ctrl *access_ctrl_req;
4322 int fallback = *(int *)reply->param;
4324 QETH_CARD_TEXT(card, 4, "setaccb");
4325 if (cmd->hdr.return_code)
4326 return 0;
4327 qeth_setadpparms_inspect_rc(cmd);
4329 access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4330 QETH_DBF_TEXT_(SETUP, 2, "setaccb");
4331 QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4332 QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
4333 cmd->data.setadapterparms.hdr.return_code);
4334 if (cmd->data.setadapterparms.hdr.return_code !=
4335 SET_ACCESS_CTRL_RC_SUCCESS)
4336 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
4337 card->gdev->dev.kobj.name,
4338 access_ctrl_req->subcmd_code,
4339 cmd->data.setadapterparms.hdr.return_code);
4340 switch (cmd->data.setadapterparms.hdr.return_code) {
4341 case SET_ACCESS_CTRL_RC_SUCCESS:
4342 if (card->options.isolation == ISOLATION_MODE_NONE) {
4343 dev_info(&card->gdev->dev,
4344 "QDIO data connection isolation is deactivated\n");
4345 } else {
4346 dev_info(&card->gdev->dev,
4347 "QDIO data connection isolation is activated\n");
4349 break;
4350 case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4351 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already "
4352 "deactivated\n", dev_name(&card->gdev->dev));
4353 if (fallback)
4354 card->options.isolation = card->options.prev_isolation;
4355 break;
4356 case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4357 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already"
4358 " activated\n", dev_name(&card->gdev->dev));
4359 if (fallback)
4360 card->options.isolation = card->options.prev_isolation;
4361 break;
4362 case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4363 dev_err(&card->gdev->dev, "Adapter does not "
4364 "support QDIO data connection isolation\n");
4365 break;
4366 case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4367 dev_err(&card->gdev->dev,
4368 "Adapter is dedicated. "
4369 "QDIO data connection isolation not supported\n");
4370 if (fallback)
4371 card->options.isolation = card->options.prev_isolation;
4372 break;
4373 case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4374 dev_err(&card->gdev->dev,
4375 "TSO does not permit QDIO data connection isolation\n");
4376 if (fallback)
4377 card->options.isolation = card->options.prev_isolation;
4378 break;
4379 case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4380 dev_err(&card->gdev->dev, "The adjacent switch port does not "
4381 "support reflective relay mode\n");
4382 if (fallback)
4383 card->options.isolation = card->options.prev_isolation;
4384 break;
4385 case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4386 dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4387 "enabled at the adjacent switch port");
4388 if (fallback)
4389 card->options.isolation = card->options.prev_isolation;
4390 break;
4391 case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4392 dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4393 "at the adjacent switch failed\n");
4394 break;
4395 default:
4396 /* this should never happen */
4397 if (fallback)
4398 card->options.isolation = card->options.prev_isolation;
4399 break;
4401 return 0;
4404 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
4405 enum qeth_ipa_isolation_modes isolation, int fallback)
4407 int rc;
4408 struct qeth_cmd_buffer *iob;
4409 struct qeth_ipa_cmd *cmd;
4410 struct qeth_set_access_ctrl *access_ctrl_req;
4412 QETH_CARD_TEXT(card, 4, "setacctl");
4414 QETH_DBF_TEXT_(SETUP, 2, "setacctl");
4415 QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4417 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
4418 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4419 sizeof(struct qeth_set_access_ctrl));
4420 if (!iob)
4421 return -ENOMEM;
4422 cmd = __ipa_cmd(iob);
4423 access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4424 access_ctrl_req->subcmd_code = isolation;
4426 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
4427 &fallback);
4428 QETH_DBF_TEXT_(SETUP, 2, "rc=%d", rc);
4429 return rc;
4432 int qeth_set_access_ctrl_online(struct qeth_card *card, int fallback)
4434 int rc = 0;
4436 QETH_CARD_TEXT(card, 4, "setactlo");
4438 if ((card->info.type == QETH_CARD_TYPE_OSD ||
4439 card->info.type == QETH_CARD_TYPE_OSX) &&
4440 qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
4441 rc = qeth_setadpparms_set_access_ctrl(card,
4442 card->options.isolation, fallback);
4443 if (rc) {
4444 QETH_DBF_MESSAGE(3,
4445 "IPA(SET_ACCESS_CTRL,%s,%d) sent failed\n",
4446 card->gdev->dev.kobj.name,
4447 rc);
4448 rc = -EOPNOTSUPP;
4450 } else if (card->options.isolation != ISOLATION_MODE_NONE) {
4451 card->options.isolation = ISOLATION_MODE_NONE;
4453 dev_err(&card->gdev->dev, "Adapter does not "
4454 "support QDIO data connection isolation\n");
4455 rc = -EOPNOTSUPP;
4457 return rc;
4459 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
4461 void qeth_tx_timeout(struct net_device *dev)
4463 struct qeth_card *card;
4465 card = dev->ml_priv;
4466 QETH_CARD_TEXT(card, 4, "txtimeo");
4467 card->stats.tx_errors++;
4468 qeth_schedule_recovery(card);
4470 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
4472 static int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4474 struct qeth_card *card = dev->ml_priv;
4475 int rc = 0;
4477 switch (regnum) {
4478 case MII_BMCR: /* Basic mode control register */
4479 rc = BMCR_FULLDPLX;
4480 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
4481 (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4482 (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
4483 rc |= BMCR_SPEED100;
4484 break;
4485 case MII_BMSR: /* Basic mode status register */
4486 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4487 BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4488 BMSR_100BASE4;
4489 break;
4490 case MII_PHYSID1: /* PHYS ID 1 */
4491 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4492 dev->dev_addr[2];
4493 rc = (rc >> 5) & 0xFFFF;
4494 break;
4495 case MII_PHYSID2: /* PHYS ID 2 */
4496 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4497 break;
4498 case MII_ADVERTISE: /* Advertisement control reg */
4499 rc = ADVERTISE_ALL;
4500 break;
4501 case MII_LPA: /* Link partner ability reg */
4502 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4503 LPA_100BASE4 | LPA_LPACK;
4504 break;
4505 case MII_EXPANSION: /* Expansion register */
4506 break;
4507 case MII_DCOUNTER: /* disconnect counter */
4508 break;
4509 case MII_FCSCOUNTER: /* false carrier counter */
4510 break;
4511 case MII_NWAYTEST: /* N-way auto-neg test register */
4512 break;
4513 case MII_RERRCOUNTER: /* rx error counter */
4514 rc = card->stats.rx_errors;
4515 break;
4516 case MII_SREVISION: /* silicon revision */
4517 break;
4518 case MII_RESV1: /* reserved 1 */
4519 break;
4520 case MII_LBRERROR: /* loopback, rx, bypass error */
4521 break;
4522 case MII_PHYADDR: /* physical address */
4523 break;
4524 case MII_RESV2: /* reserved 2 */
4525 break;
4526 case MII_TPISTATUS: /* TPI status for 10mbps */
4527 break;
4528 case MII_NCONFIG: /* network interface config */
4529 break;
4530 default:
4531 break;
4533 return rc;
4536 static int qeth_send_ipa_snmp_cmd(struct qeth_card *card,
4537 struct qeth_cmd_buffer *iob, int len,
4538 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
4539 unsigned long),
4540 void *reply_param)
4542 u16 s1, s2;
4544 QETH_CARD_TEXT(card, 4, "sendsnmp");
4546 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
4547 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
4548 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
4549 /* adjust PDU length fields in IPA_PDU_HEADER */
4550 s1 = (u32) IPA_PDU_HEADER_SIZE + len;
4551 s2 = (u32) len;
4552 memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
4553 memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
4554 memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
4555 memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
4556 return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
4557 reply_cb, reply_param);
4560 static int qeth_snmp_command_cb(struct qeth_card *card,
4561 struct qeth_reply *reply, unsigned long sdata)
4563 struct qeth_ipa_cmd *cmd;
4564 struct qeth_arp_query_info *qinfo;
4565 struct qeth_snmp_cmd *snmp;
4566 unsigned char *data;
4567 __u16 data_len;
4569 QETH_CARD_TEXT(card, 3, "snpcmdcb");
4571 cmd = (struct qeth_ipa_cmd *) sdata;
4572 data = (unsigned char *)((char *)cmd - reply->offset);
4573 qinfo = (struct qeth_arp_query_info *) reply->param;
4574 snmp = &cmd->data.setadapterparms.data.snmp;
4576 if (cmd->hdr.return_code) {
4577 QETH_CARD_TEXT_(card, 4, "scer1%x", cmd->hdr.return_code);
4578 return 0;
4580 if (cmd->data.setadapterparms.hdr.return_code) {
4581 cmd->hdr.return_code =
4582 cmd->data.setadapterparms.hdr.return_code;
4583 QETH_CARD_TEXT_(card, 4, "scer2%x", cmd->hdr.return_code);
4584 return 0;
4586 data_len = *((__u16 *)QETH_IPA_PDU_LEN_PDU1(data));
4587 if (cmd->data.setadapterparms.hdr.seq_no == 1)
4588 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
4589 else
4590 data_len -= (__u16)((char *)&snmp->request - (char *)cmd);
4592 /* check if there is enough room in userspace */
4593 if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4594 QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOMEM);
4595 cmd->hdr.return_code = IPA_RC_ENOMEM;
4596 return 0;
4598 QETH_CARD_TEXT_(card, 4, "snore%i",
4599 cmd->data.setadapterparms.hdr.used_total);
4600 QETH_CARD_TEXT_(card, 4, "sseqn%i",
4601 cmd->data.setadapterparms.hdr.seq_no);
4602 /*copy entries to user buffer*/
4603 if (cmd->data.setadapterparms.hdr.seq_no == 1) {
4604 memcpy(qinfo->udata + qinfo->udata_offset,
4605 (char *)snmp,
4606 data_len + offsetof(struct qeth_snmp_cmd, data));
4607 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
4608 } else {
4609 memcpy(qinfo->udata + qinfo->udata_offset,
4610 (char *)&snmp->request, data_len);
4612 qinfo->udata_offset += data_len;
4613 /* check if all replies received ... */
4614 QETH_CARD_TEXT_(card, 4, "srtot%i",
4615 cmd->data.setadapterparms.hdr.used_total);
4616 QETH_CARD_TEXT_(card, 4, "srseq%i",
4617 cmd->data.setadapterparms.hdr.seq_no);
4618 if (cmd->data.setadapterparms.hdr.seq_no <
4619 cmd->data.setadapterparms.hdr.used_total)
4620 return 1;
4621 return 0;
4624 static int qeth_snmp_command(struct qeth_card *card, char __user *udata)
4626 struct qeth_cmd_buffer *iob;
4627 struct qeth_ipa_cmd *cmd;
4628 struct qeth_snmp_ureq *ureq;
4629 unsigned int req_len;
4630 struct qeth_arp_query_info qinfo = {0, };
4631 int rc = 0;
4633 QETH_CARD_TEXT(card, 3, "snmpcmd");
4635 if (card->info.guestlan)
4636 return -EOPNOTSUPP;
4638 if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
4639 (!card->options.layer2)) {
4640 return -EOPNOTSUPP;
4642 /* skip 4 bytes (data_len struct member) to get req_len */
4643 if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
4644 return -EFAULT;
4645 if (req_len > (QETH_BUFSIZE - IPA_PDU_HEADER_SIZE -
4646 sizeof(struct qeth_ipacmd_hdr) -
4647 sizeof(struct qeth_ipacmd_setadpparms_hdr)))
4648 return -EINVAL;
4649 ureq = memdup_user(udata, req_len + sizeof(struct qeth_snmp_ureq_hdr));
4650 if (IS_ERR(ureq)) {
4651 QETH_CARD_TEXT(card, 2, "snmpnome");
4652 return PTR_ERR(ureq);
4654 qinfo.udata_len = ureq->hdr.data_len;
4655 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
4656 if (!qinfo.udata) {
4657 kfree(ureq);
4658 return -ENOMEM;
4660 qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4662 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
4663 QETH_SNMP_SETADP_CMDLENGTH + req_len);
4664 if (!iob) {
4665 rc = -ENOMEM;
4666 goto out;
4668 cmd = __ipa_cmd(iob);
4669 memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
4670 rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
4671 qeth_snmp_command_cb, (void *)&qinfo);
4672 if (rc)
4673 QETH_DBF_MESSAGE(2, "SNMP command failed on %s: (0x%x)\n",
4674 QETH_CARD_IFNAME(card), rc);
4675 else {
4676 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
4677 rc = -EFAULT;
4679 out:
4680 kfree(ureq);
4681 kfree(qinfo.udata);
4682 return rc;
4685 static int qeth_setadpparms_query_oat_cb(struct qeth_card *card,
4686 struct qeth_reply *reply, unsigned long data)
4688 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4689 struct qeth_qoat_priv *priv;
4690 char *resdata;
4691 int resdatalen;
4693 QETH_CARD_TEXT(card, 3, "qoatcb");
4694 if (qeth_setadpparms_inspect_rc(cmd))
4695 return 0;
4697 priv = (struct qeth_qoat_priv *)reply->param;
4698 resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4699 resdata = (char *)data + 28;
4701 if (resdatalen > (priv->buffer_len - priv->response_len)) {
4702 cmd->hdr.return_code = IPA_RC_FFFF;
4703 return 0;
4706 memcpy((priv->buffer + priv->response_len), resdata,
4707 resdatalen);
4708 priv->response_len += resdatalen;
4710 if (cmd->data.setadapterparms.hdr.seq_no <
4711 cmd->data.setadapterparms.hdr.used_total)
4712 return 1;
4713 return 0;
4716 static int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4718 int rc = 0;
4719 struct qeth_cmd_buffer *iob;
4720 struct qeth_ipa_cmd *cmd;
4721 struct qeth_query_oat *oat_req;
4722 struct qeth_query_oat_data oat_data;
4723 struct qeth_qoat_priv priv;
4724 void __user *tmp;
4726 QETH_CARD_TEXT(card, 3, "qoatcmd");
4728 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4729 rc = -EOPNOTSUPP;
4730 goto out;
4733 if (copy_from_user(&oat_data, udata,
4734 sizeof(struct qeth_query_oat_data))) {
4735 rc = -EFAULT;
4736 goto out;
4739 priv.buffer_len = oat_data.buffer_len;
4740 priv.response_len = 0;
4741 priv.buffer = kzalloc(oat_data.buffer_len, GFP_KERNEL);
4742 if (!priv.buffer) {
4743 rc = -ENOMEM;
4744 goto out;
4747 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4748 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4749 sizeof(struct qeth_query_oat));
4750 if (!iob) {
4751 rc = -ENOMEM;
4752 goto out_free;
4754 cmd = __ipa_cmd(iob);
4755 oat_req = &cmd->data.setadapterparms.data.query_oat;
4756 oat_req->subcmd_code = oat_data.command;
4758 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb,
4759 &priv);
4760 if (!rc) {
4761 if (is_compat_task())
4762 tmp = compat_ptr(oat_data.ptr);
4763 else
4764 tmp = (void __user *)(unsigned long)oat_data.ptr;
4766 if (copy_to_user(tmp, priv.buffer,
4767 priv.response_len)) {
4768 rc = -EFAULT;
4769 goto out_free;
4772 oat_data.response_len = priv.response_len;
4774 if (copy_to_user(udata, &oat_data,
4775 sizeof(struct qeth_query_oat_data)))
4776 rc = -EFAULT;
4777 } else
4778 if (rc == IPA_RC_FFFF)
4779 rc = -EFAULT;
4781 out_free:
4782 kfree(priv.buffer);
4783 out:
4784 return rc;
4787 static int qeth_query_card_info_cb(struct qeth_card *card,
4788 struct qeth_reply *reply, unsigned long data)
4790 struct carrier_info *carrier_info = (struct carrier_info *)reply->param;
4791 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4792 struct qeth_query_card_info *card_info;
4794 QETH_CARD_TEXT(card, 2, "qcrdincb");
4795 if (qeth_setadpparms_inspect_rc(cmd))
4796 return 0;
4798 card_info = &cmd->data.setadapterparms.data.card_info;
4799 carrier_info->card_type = card_info->card_type;
4800 carrier_info->port_mode = card_info->port_mode;
4801 carrier_info->port_speed = card_info->port_speed;
4802 return 0;
4805 static int qeth_query_card_info(struct qeth_card *card,
4806 struct carrier_info *carrier_info)
4808 struct qeth_cmd_buffer *iob;
4810 QETH_CARD_TEXT(card, 2, "qcrdinfo");
4811 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_CARD_INFO))
4812 return -EOPNOTSUPP;
4813 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_CARD_INFO,
4814 sizeof(struct qeth_ipacmd_setadpparms_hdr));
4815 if (!iob)
4816 return -ENOMEM;
4817 return qeth_send_ipa_cmd(card, iob, qeth_query_card_info_cb,
4818 (void *)carrier_info);
4822 * qeth_vm_request_mac() - Request a hypervisor-managed MAC address
4823 * @card: pointer to a qeth_card
4825 * Returns
4826 * 0, if a MAC address has been set for the card's netdevice
4827 * a return code, for various error conditions
4829 int qeth_vm_request_mac(struct qeth_card *card)
4831 struct diag26c_mac_resp *response;
4832 struct diag26c_mac_req *request;
4833 struct ccw_dev_id id;
4834 int rc;
4836 QETH_DBF_TEXT(SETUP, 2, "vmreqmac");
4838 if (!card->dev)
4839 return -ENODEV;
4841 request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
4842 response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
4843 if (!request || !response) {
4844 rc = -ENOMEM;
4845 goto out;
4848 ccw_device_get_id(CARD_DDEV(card), &id);
4849 request->resp_buf_len = sizeof(*response);
4850 request->resp_version = DIAG26C_VERSION2;
4851 request->op_code = DIAG26C_GET_MAC;
4852 request->devno = id.devno;
4854 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4855 rc = diag26c(request, response, DIAG26C_MAC_SERVICES);
4856 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4857 if (rc)
4858 goto out;
4859 QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
4861 if (request->resp_buf_len < sizeof(*response) ||
4862 response->version != request->resp_version) {
4863 rc = -EIO;
4864 QETH_DBF_TEXT(SETUP, 2, "badresp");
4865 QETH_DBF_HEX(SETUP, 2, &request->resp_buf_len,
4866 sizeof(request->resp_buf_len));
4867 } else if (!is_valid_ether_addr(response->mac)) {
4868 rc = -EINVAL;
4869 QETH_DBF_TEXT(SETUP, 2, "badmac");
4870 QETH_DBF_HEX(SETUP, 2, response->mac, ETH_ALEN);
4871 } else {
4872 ether_addr_copy(card->dev->dev_addr, response->mac);
4875 out:
4876 kfree(response);
4877 kfree(request);
4878 return rc;
4880 EXPORT_SYMBOL_GPL(qeth_vm_request_mac);
4882 static int qeth_get_qdio_q_format(struct qeth_card *card)
4884 if (card->info.type == QETH_CARD_TYPE_IQD)
4885 return QDIO_IQDIO_QFMT;
4886 else
4887 return QDIO_QETH_QFMT;
4890 static void qeth_determine_capabilities(struct qeth_card *card)
4892 int rc;
4893 int length;
4894 char *prcd;
4895 struct ccw_device *ddev;
4896 int ddev_offline = 0;
4898 QETH_DBF_TEXT(SETUP, 2, "detcapab");
4899 ddev = CARD_DDEV(card);
4900 if (!ddev->online) {
4901 ddev_offline = 1;
4902 rc = ccw_device_set_online(ddev);
4903 if (rc) {
4904 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4905 goto out;
4909 rc = qeth_read_conf_data(card, (void **) &prcd, &length);
4910 if (rc) {
4911 QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
4912 dev_name(&card->gdev->dev), rc);
4913 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4914 goto out_offline;
4916 qeth_configure_unitaddr(card, prcd);
4917 if (ddev_offline)
4918 qeth_configure_blkt_default(card, prcd);
4919 kfree(prcd);
4921 rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4922 if (rc)
4923 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
4925 QETH_DBF_TEXT_(SETUP, 2, "qfmt%d", card->ssqd.qfmt);
4926 QETH_DBF_TEXT_(SETUP, 2, "ac1:%02x", card->ssqd.qdioac1);
4927 QETH_DBF_TEXT_(SETUP, 2, "ac2:%04x", card->ssqd.qdioac2);
4928 QETH_DBF_TEXT_(SETUP, 2, "ac3:%04x", card->ssqd.qdioac3);
4929 QETH_DBF_TEXT_(SETUP, 2, "icnt%d", card->ssqd.icnt);
4930 if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
4931 ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
4932 ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
4933 dev_info(&card->gdev->dev,
4934 "Completion Queueing supported\n");
4935 } else {
4936 card->options.cq = QETH_CQ_NOTAVAILABLE;
4940 out_offline:
4941 if (ddev_offline == 1)
4942 ccw_device_set_offline(ddev);
4943 out:
4944 return;
4947 static void qeth_qdio_establish_cq(struct qeth_card *card,
4948 struct qdio_buffer **in_sbal_ptrs,
4949 void (**queue_start_poll)
4950 (struct ccw_device *, int,
4951 unsigned long))
4953 int i;
4955 if (card->options.cq == QETH_CQ_ENABLED) {
4956 int offset = QDIO_MAX_BUFFERS_PER_Q *
4957 (card->qdio.no_in_queues - 1);
4958 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4959 in_sbal_ptrs[offset + i] = (struct qdio_buffer *)
4960 virt_to_phys(card->qdio.c_q->bufs[i].buffer);
4963 queue_start_poll[card->qdio.no_in_queues - 1] = NULL;
4967 static int qeth_qdio_establish(struct qeth_card *card)
4969 struct qdio_initialize init_data;
4970 char *qib_param_field;
4971 struct qdio_buffer **in_sbal_ptrs;
4972 void (**queue_start_poll) (struct ccw_device *, int, unsigned long);
4973 struct qdio_buffer **out_sbal_ptrs;
4974 int i, j, k;
4975 int rc = 0;
4977 QETH_DBF_TEXT(SETUP, 2, "qdioest");
4979 qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q,
4980 GFP_KERNEL);
4981 if (!qib_param_field) {
4982 rc = -ENOMEM;
4983 goto out_free_nothing;
4986 qeth_create_qib_param_field(card, qib_param_field);
4987 qeth_create_qib_param_field_blkt(card, qib_param_field);
4989 in_sbal_ptrs = kcalloc(card->qdio.no_in_queues * QDIO_MAX_BUFFERS_PER_Q,
4990 sizeof(void *),
4991 GFP_KERNEL);
4992 if (!in_sbal_ptrs) {
4993 rc = -ENOMEM;
4994 goto out_free_qib_param;
4996 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4997 in_sbal_ptrs[i] = (struct qdio_buffer *)
4998 virt_to_phys(card->qdio.in_q->bufs[i].buffer);
5001 queue_start_poll = kcalloc(card->qdio.no_in_queues, sizeof(void *),
5002 GFP_KERNEL);
5003 if (!queue_start_poll) {
5004 rc = -ENOMEM;
5005 goto out_free_in_sbals;
5007 for (i = 0; i < card->qdio.no_in_queues; ++i)
5008 queue_start_poll[i] = qeth_qdio_start_poll;
5010 qeth_qdio_establish_cq(card, in_sbal_ptrs, queue_start_poll);
5012 out_sbal_ptrs =
5013 kcalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q,
5014 sizeof(void *),
5015 GFP_KERNEL);
5016 if (!out_sbal_ptrs) {
5017 rc = -ENOMEM;
5018 goto out_free_queue_start_poll;
5020 for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
5021 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
5022 out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
5023 card->qdio.out_qs[i]->bufs[j]->buffer);
5026 memset(&init_data, 0, sizeof(struct qdio_initialize));
5027 init_data.cdev = CARD_DDEV(card);
5028 init_data.q_format = qeth_get_qdio_q_format(card);
5029 init_data.qib_param_field_format = 0;
5030 init_data.qib_param_field = qib_param_field;
5031 init_data.no_input_qs = card->qdio.no_in_queues;
5032 init_data.no_output_qs = card->qdio.no_out_queues;
5033 init_data.input_handler = qeth_qdio_input_handler;
5034 init_data.output_handler = qeth_qdio_output_handler;
5035 init_data.queue_start_poll_array = queue_start_poll;
5036 init_data.int_parm = (unsigned long) card;
5037 init_data.input_sbal_addr_array = (void **) in_sbal_ptrs;
5038 init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
5039 init_data.output_sbal_state_array = card->qdio.out_bufstates;
5040 init_data.scan_threshold =
5041 (card->info.type == QETH_CARD_TYPE_IQD) ? 1 : 32;
5043 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
5044 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
5045 rc = qdio_allocate(&init_data);
5046 if (rc) {
5047 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5048 goto out;
5050 rc = qdio_establish(&init_data);
5051 if (rc) {
5052 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5053 qdio_free(CARD_DDEV(card));
5057 switch (card->options.cq) {
5058 case QETH_CQ_ENABLED:
5059 dev_info(&card->gdev->dev, "Completion Queue support enabled");
5060 break;
5061 case QETH_CQ_DISABLED:
5062 dev_info(&card->gdev->dev, "Completion Queue support disabled");
5063 break;
5064 default:
5065 break;
5067 out:
5068 kfree(out_sbal_ptrs);
5069 out_free_queue_start_poll:
5070 kfree(queue_start_poll);
5071 out_free_in_sbals:
5072 kfree(in_sbal_ptrs);
5073 out_free_qib_param:
5074 kfree(qib_param_field);
5075 out_free_nothing:
5076 return rc;
5079 static void qeth_core_free_card(struct qeth_card *card)
5082 QETH_DBF_TEXT(SETUP, 2, "freecrd");
5083 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
5084 qeth_clean_channel(&card->read);
5085 qeth_clean_channel(&card->write);
5086 qeth_free_qdio_buffers(card);
5087 unregister_service_level(&card->qeth_service_level);
5088 kfree(card);
5091 void qeth_trace_features(struct qeth_card *card)
5093 QETH_CARD_TEXT(card, 2, "features");
5094 QETH_CARD_HEX(card, 2, &card->options.ipa4, sizeof(card->options.ipa4));
5095 QETH_CARD_HEX(card, 2, &card->options.ipa6, sizeof(card->options.ipa6));
5096 QETH_CARD_HEX(card, 2, &card->options.adp, sizeof(card->options.adp));
5097 QETH_CARD_HEX(card, 2, &card->info.diagass_support,
5098 sizeof(card->info.diagass_support));
5100 EXPORT_SYMBOL_GPL(qeth_trace_features);
5102 static struct ccw_device_id qeth_ids[] = {
5103 {CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
5104 .driver_info = QETH_CARD_TYPE_OSD},
5105 {CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
5106 .driver_info = QETH_CARD_TYPE_IQD},
5107 {CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
5108 .driver_info = QETH_CARD_TYPE_OSN},
5109 {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
5110 .driver_info = QETH_CARD_TYPE_OSM},
5111 {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
5112 .driver_info = QETH_CARD_TYPE_OSX},
5115 MODULE_DEVICE_TABLE(ccw, qeth_ids);
5117 static struct ccw_driver qeth_ccw_driver = {
5118 .driver = {
5119 .owner = THIS_MODULE,
5120 .name = "qeth",
5122 .ids = qeth_ids,
5123 .probe = ccwgroup_probe_ccwdev,
5124 .remove = ccwgroup_remove_ccwdev,
5127 int qeth_core_hardsetup_card(struct qeth_card *card)
5129 int retries = 3;
5130 int rc;
5132 QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
5133 atomic_set(&card->force_alloc_skb, 0);
5134 qeth_update_from_chp_desc(card);
5135 retry:
5136 if (retries < 3)
5137 QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
5138 dev_name(&card->gdev->dev));
5139 rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
5140 ccw_device_set_offline(CARD_DDEV(card));
5141 ccw_device_set_offline(CARD_WDEV(card));
5142 ccw_device_set_offline(CARD_RDEV(card));
5143 qdio_free(CARD_DDEV(card));
5144 rc = ccw_device_set_online(CARD_RDEV(card));
5145 if (rc)
5146 goto retriable;
5147 rc = ccw_device_set_online(CARD_WDEV(card));
5148 if (rc)
5149 goto retriable;
5150 rc = ccw_device_set_online(CARD_DDEV(card));
5151 if (rc)
5152 goto retriable;
5153 retriable:
5154 if (rc == -ERESTARTSYS) {
5155 QETH_DBF_TEXT(SETUP, 2, "break1");
5156 return rc;
5157 } else if (rc) {
5158 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
5159 if (--retries < 0)
5160 goto out;
5161 else
5162 goto retry;
5164 qeth_determine_capabilities(card);
5165 qeth_init_tokens(card);
5166 qeth_init_func_level(card);
5167 rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
5168 if (rc == -ERESTARTSYS) {
5169 QETH_DBF_TEXT(SETUP, 2, "break2");
5170 return rc;
5171 } else if (rc) {
5172 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
5173 if (--retries < 0)
5174 goto out;
5175 else
5176 goto retry;
5178 rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
5179 if (rc == -ERESTARTSYS) {
5180 QETH_DBF_TEXT(SETUP, 2, "break3");
5181 return rc;
5182 } else if (rc) {
5183 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
5184 if (--retries < 0)
5185 goto out;
5186 else
5187 goto retry;
5189 card->read_or_write_problem = 0;
5190 rc = qeth_mpc_initialize(card);
5191 if (rc) {
5192 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
5193 goto out;
5196 rc = qeth_send_startlan(card);
5197 if (rc) {
5198 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
5199 if (rc == IPA_RC_LAN_OFFLINE) {
5200 dev_warn(&card->gdev->dev,
5201 "The LAN is offline\n");
5202 card->lan_online = 0;
5203 } else {
5204 rc = -ENODEV;
5205 goto out;
5207 } else
5208 card->lan_online = 1;
5210 card->options.ipa4.supported_funcs = 0;
5211 card->options.ipa6.supported_funcs = 0;
5212 card->options.adp.supported_funcs = 0;
5213 card->options.sbp.supported_funcs = 0;
5214 card->info.diagass_support = 0;
5215 rc = qeth_query_ipassists(card, QETH_PROT_IPV4);
5216 if (rc == -ENOMEM)
5217 goto out;
5218 if (qeth_is_supported(card, IPA_IPV6)) {
5219 rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
5220 if (rc == -ENOMEM)
5221 goto out;
5223 if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
5224 rc = qeth_query_setadapterparms(card);
5225 if (rc < 0) {
5226 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
5227 goto out;
5230 if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
5231 rc = qeth_query_setdiagass(card);
5232 if (rc < 0) {
5233 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
5234 goto out;
5237 return 0;
5238 out:
5239 dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5240 "an error on the device\n");
5241 QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
5242 dev_name(&card->gdev->dev), rc);
5243 return rc;
5245 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
5247 static void qeth_create_skb_frag(struct qdio_buffer_element *element,
5248 struct sk_buff *skb, int offset, int data_len)
5250 struct page *page = virt_to_page(element->addr);
5251 unsigned int next_frag;
5253 /* first fill the linear space */
5254 if (!skb->len) {
5255 unsigned int linear = min(data_len, skb_tailroom(skb));
5257 skb_put_data(skb, element->addr + offset, linear);
5258 data_len -= linear;
5259 if (!data_len)
5260 return;
5261 offset += linear;
5262 /* fall through to add page frag for remaining data */
5265 next_frag = skb_shinfo(skb)->nr_frags;
5266 get_page(page);
5267 skb_add_rx_frag(skb, next_frag, page, offset, data_len, data_len);
5270 static inline int qeth_is_last_sbale(struct qdio_buffer_element *sbale)
5272 return (sbale->eflags & SBAL_EFLAGS_LAST_ENTRY);
5275 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
5276 struct qeth_qdio_buffer *qethbuffer,
5277 struct qdio_buffer_element **__element, int *__offset,
5278 struct qeth_hdr **hdr)
5280 struct qdio_buffer_element *element = *__element;
5281 struct qdio_buffer *buffer = qethbuffer->buffer;
5282 int offset = *__offset;
5283 struct sk_buff *skb;
5284 int skb_len = 0;
5285 void *data_ptr;
5286 int data_len;
5287 int headroom = 0;
5288 int use_rx_sg = 0;
5290 /* qeth_hdr must not cross element boundaries */
5291 while (element->length < offset + sizeof(struct qeth_hdr)) {
5292 if (qeth_is_last_sbale(element))
5293 return NULL;
5294 element++;
5295 offset = 0;
5297 *hdr = element->addr + offset;
5299 offset += sizeof(struct qeth_hdr);
5300 switch ((*hdr)->hdr.l2.id) {
5301 case QETH_HEADER_TYPE_LAYER2:
5302 skb_len = (*hdr)->hdr.l2.pkt_length;
5303 break;
5304 case QETH_HEADER_TYPE_LAYER3:
5305 skb_len = (*hdr)->hdr.l3.length;
5306 headroom = ETH_HLEN;
5307 break;
5308 case QETH_HEADER_TYPE_OSN:
5309 skb_len = (*hdr)->hdr.osn.pdu_length;
5310 headroom = sizeof(struct qeth_hdr);
5311 break;
5312 default:
5313 break;
5316 if (!skb_len)
5317 return NULL;
5319 if (((skb_len >= card->options.rx_sg_cb) &&
5320 (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
5321 (!atomic_read(&card->force_alloc_skb))) ||
5322 (card->options.cq == QETH_CQ_ENABLED))
5323 use_rx_sg = 1;
5325 if (use_rx_sg && qethbuffer->rx_skb) {
5326 /* QETH_CQ_ENABLED only: */
5327 skb = qethbuffer->rx_skb;
5328 qethbuffer->rx_skb = NULL;
5329 } else {
5330 unsigned int linear = (use_rx_sg) ? QETH_RX_PULL_LEN : skb_len;
5332 skb = napi_alloc_skb(&card->napi, linear + headroom);
5334 if (!skb)
5335 goto no_mem;
5336 if (headroom)
5337 skb_reserve(skb, headroom);
5339 data_ptr = element->addr + offset;
5340 while (skb_len) {
5341 data_len = min(skb_len, (int)(element->length - offset));
5342 if (data_len) {
5343 if (use_rx_sg)
5344 qeth_create_skb_frag(element, skb, offset,
5345 data_len);
5346 else
5347 skb_put_data(skb, data_ptr, data_len);
5349 skb_len -= data_len;
5350 if (skb_len) {
5351 if (qeth_is_last_sbale(element)) {
5352 QETH_CARD_TEXT(card, 4, "unexeob");
5353 QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
5354 dev_kfree_skb_any(skb);
5355 card->stats.rx_errors++;
5356 return NULL;
5358 element++;
5359 offset = 0;
5360 data_ptr = element->addr;
5361 } else {
5362 offset += data_len;
5365 *__element = element;
5366 *__offset = offset;
5367 if (use_rx_sg && card->options.performance_stats) {
5368 card->perf_stats.sg_skbs_rx++;
5369 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
5371 return skb;
5372 no_mem:
5373 if (net_ratelimit()) {
5374 QETH_CARD_TEXT(card, 2, "noskbmem");
5376 card->stats.rx_dropped++;
5377 return NULL;
5379 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
5381 int qeth_poll(struct napi_struct *napi, int budget)
5383 struct qeth_card *card = container_of(napi, struct qeth_card, napi);
5384 int work_done = 0;
5385 struct qeth_qdio_buffer *buffer;
5386 int done;
5387 int new_budget = budget;
5389 if (card->options.performance_stats) {
5390 card->perf_stats.inbound_cnt++;
5391 card->perf_stats.inbound_start_time = qeth_get_micros();
5394 while (1) {
5395 if (!card->rx.b_count) {
5396 card->rx.qdio_err = 0;
5397 card->rx.b_count = qdio_get_next_buffers(
5398 card->data.ccwdev, 0, &card->rx.b_index,
5399 &card->rx.qdio_err);
5400 if (card->rx.b_count <= 0) {
5401 card->rx.b_count = 0;
5402 break;
5404 card->rx.b_element =
5405 &card->qdio.in_q->bufs[card->rx.b_index]
5406 .buffer->element[0];
5407 card->rx.e_offset = 0;
5410 while (card->rx.b_count) {
5411 buffer = &card->qdio.in_q->bufs[card->rx.b_index];
5412 if (!(card->rx.qdio_err &&
5413 qeth_check_qdio_errors(card, buffer->buffer,
5414 card->rx.qdio_err, "qinerr")))
5415 work_done +=
5416 card->discipline->process_rx_buffer(
5417 card, new_budget, &done);
5418 else
5419 done = 1;
5421 if (done) {
5422 if (card->options.performance_stats)
5423 card->perf_stats.bufs_rec++;
5424 qeth_put_buffer_pool_entry(card,
5425 buffer->pool_entry);
5426 qeth_queue_input_buffer(card, card->rx.b_index);
5427 card->rx.b_count--;
5428 if (card->rx.b_count) {
5429 card->rx.b_index =
5430 (card->rx.b_index + 1) %
5431 QDIO_MAX_BUFFERS_PER_Q;
5432 card->rx.b_element =
5433 &card->qdio.in_q
5434 ->bufs[card->rx.b_index]
5435 .buffer->element[0];
5436 card->rx.e_offset = 0;
5440 if (work_done >= budget)
5441 goto out;
5442 else
5443 new_budget = budget - work_done;
5447 napi_complete_done(napi, work_done);
5448 if (qdio_start_irq(card->data.ccwdev, 0))
5449 napi_schedule(&card->napi);
5450 out:
5451 if (card->options.performance_stats)
5452 card->perf_stats.inbound_time += qeth_get_micros() -
5453 card->perf_stats.inbound_start_time;
5454 return work_done;
5456 EXPORT_SYMBOL_GPL(qeth_poll);
5458 static int qeth_setassparms_inspect_rc(struct qeth_ipa_cmd *cmd)
5460 if (!cmd->hdr.return_code)
5461 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5462 return cmd->hdr.return_code;
5465 int qeth_setassparms_cb(struct qeth_card *card,
5466 struct qeth_reply *reply, unsigned long data)
5468 struct qeth_ipa_cmd *cmd;
5470 QETH_CARD_TEXT(card, 4, "defadpcb");
5472 cmd = (struct qeth_ipa_cmd *) data;
5473 if (cmd->hdr.return_code == 0) {
5474 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5475 if (cmd->hdr.prot_version == QETH_PROT_IPV4)
5476 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
5477 if (cmd->hdr.prot_version == QETH_PROT_IPV6)
5478 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
5480 return 0;
5482 EXPORT_SYMBOL_GPL(qeth_setassparms_cb);
5484 struct qeth_cmd_buffer *qeth_get_setassparms_cmd(struct qeth_card *card,
5485 enum qeth_ipa_funcs ipa_func,
5486 __u16 cmd_code, __u16 len,
5487 enum qeth_prot_versions prot)
5489 struct qeth_cmd_buffer *iob;
5490 struct qeth_ipa_cmd *cmd;
5492 QETH_CARD_TEXT(card, 4, "getasscm");
5493 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETASSPARMS, prot);
5495 if (iob) {
5496 cmd = __ipa_cmd(iob);
5497 cmd->data.setassparms.hdr.assist_no = ipa_func;
5498 cmd->data.setassparms.hdr.length = 8 + len;
5499 cmd->data.setassparms.hdr.command_code = cmd_code;
5500 cmd->data.setassparms.hdr.return_code = 0;
5501 cmd->data.setassparms.hdr.seq_no = 0;
5504 return iob;
5506 EXPORT_SYMBOL_GPL(qeth_get_setassparms_cmd);
5508 int qeth_send_setassparms(struct qeth_card *card,
5509 struct qeth_cmd_buffer *iob, __u16 len, long data,
5510 int (*reply_cb)(struct qeth_card *,
5511 struct qeth_reply *, unsigned long),
5512 void *reply_param)
5514 int rc;
5515 struct qeth_ipa_cmd *cmd;
5517 QETH_CARD_TEXT(card, 4, "sendassp");
5519 cmd = __ipa_cmd(iob);
5520 if (len <= sizeof(__u32))
5521 cmd->data.setassparms.data.flags_32bit = (__u32) data;
5522 else /* (len > sizeof(__u32)) */
5523 memcpy(&cmd->data.setassparms.data, (void *) data, len);
5525 rc = qeth_send_ipa_cmd(card, iob, reply_cb, reply_param);
5526 return rc;
5528 EXPORT_SYMBOL_GPL(qeth_send_setassparms);
5530 int qeth_send_simple_setassparms_prot(struct qeth_card *card,
5531 enum qeth_ipa_funcs ipa_func,
5532 u16 cmd_code, long data,
5533 enum qeth_prot_versions prot)
5535 int rc;
5536 int length = 0;
5537 struct qeth_cmd_buffer *iob;
5539 QETH_CARD_TEXT_(card, 4, "simassp%i", prot);
5540 if (data)
5541 length = sizeof(__u32);
5542 iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code, length, prot);
5543 if (!iob)
5544 return -ENOMEM;
5545 rc = qeth_send_setassparms(card, iob, length, data,
5546 qeth_setassparms_cb, NULL);
5547 return rc;
5549 EXPORT_SYMBOL_GPL(qeth_send_simple_setassparms_prot);
5551 static void qeth_unregister_dbf_views(void)
5553 int x;
5554 for (x = 0; x < QETH_DBF_INFOS; x++) {
5555 debug_unregister(qeth_dbf[x].id);
5556 qeth_dbf[x].id = NULL;
5560 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
5562 char dbf_txt_buf[32];
5563 va_list args;
5565 if (!debug_level_enabled(id, level))
5566 return;
5567 va_start(args, fmt);
5568 vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
5569 va_end(args);
5570 debug_text_event(id, level, dbf_txt_buf);
5572 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
5574 static int qeth_register_dbf_views(void)
5576 int ret;
5577 int x;
5579 for (x = 0; x < QETH_DBF_INFOS; x++) {
5580 /* register the areas */
5581 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
5582 qeth_dbf[x].pages,
5583 qeth_dbf[x].areas,
5584 qeth_dbf[x].len);
5585 if (qeth_dbf[x].id == NULL) {
5586 qeth_unregister_dbf_views();
5587 return -ENOMEM;
5590 /* register a view */
5591 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
5592 if (ret) {
5593 qeth_unregister_dbf_views();
5594 return ret;
5597 /* set a passing level */
5598 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
5601 return 0;
5604 int qeth_core_load_discipline(struct qeth_card *card,
5605 enum qeth_discipline_id discipline)
5607 int rc = 0;
5609 mutex_lock(&qeth_mod_mutex);
5610 switch (discipline) {
5611 case QETH_DISCIPLINE_LAYER3:
5612 card->discipline = try_then_request_module(
5613 symbol_get(qeth_l3_discipline), "qeth_l3");
5614 break;
5615 case QETH_DISCIPLINE_LAYER2:
5616 card->discipline = try_then_request_module(
5617 symbol_get(qeth_l2_discipline), "qeth_l2");
5618 break;
5619 default:
5620 break;
5623 if (!card->discipline) {
5624 dev_err(&card->gdev->dev, "There is no kernel module to "
5625 "support discipline %d\n", discipline);
5626 rc = -EINVAL;
5628 mutex_unlock(&qeth_mod_mutex);
5629 return rc;
5632 void qeth_core_free_discipline(struct qeth_card *card)
5634 if (card->options.layer2)
5635 symbol_put(qeth_l2_discipline);
5636 else
5637 symbol_put(qeth_l3_discipline);
5638 card->discipline = NULL;
5641 const struct device_type qeth_generic_devtype = {
5642 .name = "qeth_generic",
5643 .groups = qeth_generic_attr_groups,
5645 EXPORT_SYMBOL_GPL(qeth_generic_devtype);
5647 static const struct device_type qeth_osn_devtype = {
5648 .name = "qeth_osn",
5649 .groups = qeth_osn_attr_groups,
5652 #define DBF_NAME_LEN 20
5654 struct qeth_dbf_entry {
5655 char dbf_name[DBF_NAME_LEN];
5656 debug_info_t *dbf_info;
5657 struct list_head dbf_list;
5660 static LIST_HEAD(qeth_dbf_list);
5661 static DEFINE_MUTEX(qeth_dbf_list_mutex);
5663 static debug_info_t *qeth_get_dbf_entry(char *name)
5665 struct qeth_dbf_entry *entry;
5666 debug_info_t *rc = NULL;
5668 mutex_lock(&qeth_dbf_list_mutex);
5669 list_for_each_entry(entry, &qeth_dbf_list, dbf_list) {
5670 if (strcmp(entry->dbf_name, name) == 0) {
5671 rc = entry->dbf_info;
5672 break;
5675 mutex_unlock(&qeth_dbf_list_mutex);
5676 return rc;
5679 static int qeth_add_dbf_entry(struct qeth_card *card, char *name)
5681 struct qeth_dbf_entry *new_entry;
5683 card->debug = debug_register(name, 2, 1, 8);
5684 if (!card->debug) {
5685 QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
5686 goto err;
5688 if (debug_register_view(card->debug, &debug_hex_ascii_view))
5689 goto err_dbg;
5690 new_entry = kzalloc(sizeof(struct qeth_dbf_entry), GFP_KERNEL);
5691 if (!new_entry)
5692 goto err_dbg;
5693 strncpy(new_entry->dbf_name, name, DBF_NAME_LEN);
5694 new_entry->dbf_info = card->debug;
5695 mutex_lock(&qeth_dbf_list_mutex);
5696 list_add(&new_entry->dbf_list, &qeth_dbf_list);
5697 mutex_unlock(&qeth_dbf_list_mutex);
5699 return 0;
5701 err_dbg:
5702 debug_unregister(card->debug);
5703 err:
5704 return -ENOMEM;
5707 static void qeth_clear_dbf_list(void)
5709 struct qeth_dbf_entry *entry, *tmp;
5711 mutex_lock(&qeth_dbf_list_mutex);
5712 list_for_each_entry_safe(entry, tmp, &qeth_dbf_list, dbf_list) {
5713 list_del(&entry->dbf_list);
5714 debug_unregister(entry->dbf_info);
5715 kfree(entry);
5717 mutex_unlock(&qeth_dbf_list_mutex);
5720 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
5722 struct qeth_card *card;
5723 struct device *dev;
5724 int rc;
5725 enum qeth_discipline_id enforced_disc;
5726 unsigned long flags;
5727 char dbf_name[DBF_NAME_LEN];
5729 QETH_DBF_TEXT(SETUP, 2, "probedev");
5731 dev = &gdev->dev;
5732 if (!get_device(dev))
5733 return -ENODEV;
5735 QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
5737 card = qeth_alloc_card();
5738 if (!card) {
5739 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
5740 rc = -ENOMEM;
5741 goto err_dev;
5744 snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
5745 dev_name(&gdev->dev));
5746 card->debug = qeth_get_dbf_entry(dbf_name);
5747 if (!card->debug) {
5748 rc = qeth_add_dbf_entry(card, dbf_name);
5749 if (rc)
5750 goto err_card;
5753 card->read.ccwdev = gdev->cdev[0];
5754 card->write.ccwdev = gdev->cdev[1];
5755 card->data.ccwdev = gdev->cdev[2];
5756 dev_set_drvdata(&gdev->dev, card);
5757 card->gdev = gdev;
5758 gdev->cdev[0]->handler = qeth_irq;
5759 gdev->cdev[1]->handler = qeth_irq;
5760 gdev->cdev[2]->handler = qeth_irq;
5762 qeth_determine_card_type(card);
5763 rc = qeth_setup_card(card);
5764 if (rc) {
5765 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
5766 goto err_card;
5769 qeth_determine_capabilities(card);
5770 enforced_disc = qeth_enforce_discipline(card);
5771 switch (enforced_disc) {
5772 case QETH_DISCIPLINE_UNDETERMINED:
5773 gdev->dev.type = &qeth_generic_devtype;
5774 break;
5775 default:
5776 card->info.layer_enforced = true;
5777 rc = qeth_core_load_discipline(card, enforced_disc);
5778 if (rc)
5779 goto err_card;
5781 gdev->dev.type = (card->info.type != QETH_CARD_TYPE_OSN)
5782 ? card->discipline->devtype
5783 : &qeth_osn_devtype;
5784 rc = card->discipline->setup(card->gdev);
5785 if (rc)
5786 goto err_disc;
5787 break;
5790 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5791 list_add_tail(&card->list, &qeth_core_card_list.list);
5792 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5793 return 0;
5795 err_disc:
5796 qeth_core_free_discipline(card);
5797 err_card:
5798 qeth_core_free_card(card);
5799 err_dev:
5800 put_device(dev);
5801 return rc;
5804 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
5806 unsigned long flags;
5807 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5809 QETH_DBF_TEXT(SETUP, 2, "removedv");
5811 if (card->discipline) {
5812 card->discipline->remove(gdev);
5813 qeth_core_free_discipline(card);
5816 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5817 list_del(&card->list);
5818 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5819 qeth_core_free_card(card);
5820 dev_set_drvdata(&gdev->dev, NULL);
5821 put_device(&gdev->dev);
5822 return;
5825 static int qeth_core_set_online(struct ccwgroup_device *gdev)
5827 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5828 int rc = 0;
5829 enum qeth_discipline_id def_discipline;
5831 if (!card->discipline) {
5832 if (card->info.type == QETH_CARD_TYPE_IQD)
5833 def_discipline = QETH_DISCIPLINE_LAYER3;
5834 else
5835 def_discipline = QETH_DISCIPLINE_LAYER2;
5836 rc = qeth_core_load_discipline(card, def_discipline);
5837 if (rc)
5838 goto err;
5839 rc = card->discipline->setup(card->gdev);
5840 if (rc) {
5841 qeth_core_free_discipline(card);
5842 goto err;
5845 rc = card->discipline->set_online(gdev);
5846 err:
5847 return rc;
5850 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
5852 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5853 return card->discipline->set_offline(gdev);
5856 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
5858 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5859 qeth_set_allowed_threads(card, 0, 1);
5860 if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
5861 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
5862 qeth_qdio_clear_card(card, 0);
5863 qeth_clear_qdio_buffers(card);
5864 qdio_free(CARD_DDEV(card));
5867 static int qeth_core_freeze(struct ccwgroup_device *gdev)
5869 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5870 if (card->discipline && card->discipline->freeze)
5871 return card->discipline->freeze(gdev);
5872 return 0;
5875 static int qeth_core_thaw(struct ccwgroup_device *gdev)
5877 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5878 if (card->discipline && card->discipline->thaw)
5879 return card->discipline->thaw(gdev);
5880 return 0;
5883 static int qeth_core_restore(struct ccwgroup_device *gdev)
5885 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5886 if (card->discipline && card->discipline->restore)
5887 return card->discipline->restore(gdev);
5888 return 0;
5891 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
5892 .driver = {
5893 .owner = THIS_MODULE,
5894 .name = "qeth",
5896 .ccw_driver = &qeth_ccw_driver,
5897 .setup = qeth_core_probe_device,
5898 .remove = qeth_core_remove_device,
5899 .set_online = qeth_core_set_online,
5900 .set_offline = qeth_core_set_offline,
5901 .shutdown = qeth_core_shutdown,
5902 .prepare = NULL,
5903 .complete = NULL,
5904 .freeze = qeth_core_freeze,
5905 .thaw = qeth_core_thaw,
5906 .restore = qeth_core_restore,
5909 static ssize_t group_store(struct device_driver *ddrv, const char *buf,
5910 size_t count)
5912 int err;
5914 err = ccwgroup_create_dev(qeth_core_root_dev,
5915 &qeth_core_ccwgroup_driver, 3, buf);
5917 return err ? err : count;
5919 static DRIVER_ATTR_WO(group);
5921 static struct attribute *qeth_drv_attrs[] = {
5922 &driver_attr_group.attr,
5923 NULL,
5925 static struct attribute_group qeth_drv_attr_group = {
5926 .attrs = qeth_drv_attrs,
5928 static const struct attribute_group *qeth_drv_attr_groups[] = {
5929 &qeth_drv_attr_group,
5930 NULL,
5933 int qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5935 struct qeth_card *card = dev->ml_priv;
5936 struct mii_ioctl_data *mii_data;
5937 int rc = 0;
5939 if (!card)
5940 return -ENODEV;
5942 if (!qeth_card_hw_is_reachable(card))
5943 return -ENODEV;
5945 if (card->info.type == QETH_CARD_TYPE_OSN)
5946 return -EPERM;
5948 switch (cmd) {
5949 case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5950 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5951 break;
5952 case SIOC_QETH_GET_CARD_TYPE:
5953 if ((card->info.type == QETH_CARD_TYPE_OSD ||
5954 card->info.type == QETH_CARD_TYPE_OSM ||
5955 card->info.type == QETH_CARD_TYPE_OSX) &&
5956 !card->info.guestlan)
5957 return 1;
5958 else
5959 return 0;
5960 case SIOCGMIIPHY:
5961 mii_data = if_mii(rq);
5962 mii_data->phy_id = 0;
5963 break;
5964 case SIOCGMIIREG:
5965 mii_data = if_mii(rq);
5966 if (mii_data->phy_id != 0)
5967 rc = -EINVAL;
5968 else
5969 mii_data->val_out = qeth_mdio_read(dev,
5970 mii_data->phy_id, mii_data->reg_num);
5971 break;
5972 case SIOC_QETH_QUERY_OAT:
5973 rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
5974 break;
5975 default:
5976 if (card->discipline->do_ioctl)
5977 rc = card->discipline->do_ioctl(dev, rq, cmd);
5978 else
5979 rc = -EOPNOTSUPP;
5981 if (rc)
5982 QETH_CARD_TEXT_(card, 2, "ioce%x", rc);
5983 return rc;
5985 EXPORT_SYMBOL_GPL(qeth_do_ioctl);
5987 static struct {
5988 const char str[ETH_GSTRING_LEN];
5989 } qeth_ethtool_stats_keys[] = {
5990 /* 0 */{"rx skbs"},
5991 {"rx buffers"},
5992 {"tx skbs"},
5993 {"tx buffers"},
5994 {"tx skbs no packing"},
5995 {"tx buffers no packing"},
5996 {"tx skbs packing"},
5997 {"tx buffers packing"},
5998 {"tx sg skbs"},
5999 {"tx sg frags"},
6000 /* 10 */{"rx sg skbs"},
6001 {"rx sg frags"},
6002 {"rx sg page allocs"},
6003 {"tx large kbytes"},
6004 {"tx large count"},
6005 {"tx pk state ch n->p"},
6006 {"tx pk state ch p->n"},
6007 {"tx pk watermark low"},
6008 {"tx pk watermark high"},
6009 {"queue 0 buffer usage"},
6010 /* 20 */{"queue 1 buffer usage"},
6011 {"queue 2 buffer usage"},
6012 {"queue 3 buffer usage"},
6013 {"rx poll time"},
6014 {"rx poll count"},
6015 {"rx do_QDIO time"},
6016 {"rx do_QDIO count"},
6017 {"tx handler time"},
6018 {"tx handler count"},
6019 {"tx time"},
6020 /* 30 */{"tx count"},
6021 {"tx do_QDIO time"},
6022 {"tx do_QDIO count"},
6023 {"tx csum"},
6024 {"tx lin"},
6025 {"tx linfail"},
6026 {"cq handler count"},
6027 {"cq handler time"},
6028 {"rx csum"}
6031 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
6033 switch (stringset) {
6034 case ETH_SS_STATS:
6035 return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
6036 default:
6037 return -EINVAL;
6040 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
6042 void qeth_core_get_ethtool_stats(struct net_device *dev,
6043 struct ethtool_stats *stats, u64 *data)
6045 struct qeth_card *card = dev->ml_priv;
6046 data[0] = card->stats.rx_packets -
6047 card->perf_stats.initial_rx_packets;
6048 data[1] = card->perf_stats.bufs_rec;
6049 data[2] = card->stats.tx_packets -
6050 card->perf_stats.initial_tx_packets;
6051 data[3] = card->perf_stats.bufs_sent;
6052 data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
6053 - card->perf_stats.skbs_sent_pack;
6054 data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
6055 data[6] = card->perf_stats.skbs_sent_pack;
6056 data[7] = card->perf_stats.bufs_sent_pack;
6057 data[8] = card->perf_stats.sg_skbs_sent;
6058 data[9] = card->perf_stats.sg_frags_sent;
6059 data[10] = card->perf_stats.sg_skbs_rx;
6060 data[11] = card->perf_stats.sg_frags_rx;
6061 data[12] = card->perf_stats.sg_alloc_page_rx;
6062 data[13] = (card->perf_stats.large_send_bytes >> 10);
6063 data[14] = card->perf_stats.large_send_cnt;
6064 data[15] = card->perf_stats.sc_dp_p;
6065 data[16] = card->perf_stats.sc_p_dp;
6066 data[17] = QETH_LOW_WATERMARK_PACK;
6067 data[18] = QETH_HIGH_WATERMARK_PACK;
6068 data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
6069 data[20] = (card->qdio.no_out_queues > 1) ?
6070 atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
6071 data[21] = (card->qdio.no_out_queues > 2) ?
6072 atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
6073 data[22] = (card->qdio.no_out_queues > 3) ?
6074 atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
6075 data[23] = card->perf_stats.inbound_time;
6076 data[24] = card->perf_stats.inbound_cnt;
6077 data[25] = card->perf_stats.inbound_do_qdio_time;
6078 data[26] = card->perf_stats.inbound_do_qdio_cnt;
6079 data[27] = card->perf_stats.outbound_handler_time;
6080 data[28] = card->perf_stats.outbound_handler_cnt;
6081 data[29] = card->perf_stats.outbound_time;
6082 data[30] = card->perf_stats.outbound_cnt;
6083 data[31] = card->perf_stats.outbound_do_qdio_time;
6084 data[32] = card->perf_stats.outbound_do_qdio_cnt;
6085 data[33] = card->perf_stats.tx_csum;
6086 data[34] = card->perf_stats.tx_lin;
6087 data[35] = card->perf_stats.tx_linfail;
6088 data[36] = card->perf_stats.cq_cnt;
6089 data[37] = card->perf_stats.cq_time;
6090 data[38] = card->perf_stats.rx_csum;
6092 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
6094 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6096 switch (stringset) {
6097 case ETH_SS_STATS:
6098 memcpy(data, &qeth_ethtool_stats_keys,
6099 sizeof(qeth_ethtool_stats_keys));
6100 break;
6101 default:
6102 WARN_ON(1);
6103 break;
6106 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
6108 void qeth_core_get_drvinfo(struct net_device *dev,
6109 struct ethtool_drvinfo *info)
6111 struct qeth_card *card = dev->ml_priv;
6113 strlcpy(info->driver, card->options.layer2 ? "qeth_l2" : "qeth_l3",
6114 sizeof(info->driver));
6115 strlcpy(info->version, "1.0", sizeof(info->version));
6116 strlcpy(info->fw_version, card->info.mcl_level,
6117 sizeof(info->fw_version));
6118 snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
6119 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
6121 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
6123 /* Helper function to fill 'advertising' and 'supported' which are the same. */
6124 /* Autoneg and full-duplex are supported and advertised unconditionally. */
6125 /* Always advertise and support all speeds up to specified, and only one */
6126 /* specified port type. */
6127 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd,
6128 int maxspeed, int porttype)
6130 ethtool_link_ksettings_zero_link_mode(cmd, supported);
6131 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
6132 ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
6134 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
6135 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
6137 switch (porttype) {
6138 case PORT_TP:
6139 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6140 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6141 break;
6142 case PORT_FIBRE:
6143 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
6144 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
6145 break;
6146 default:
6147 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6148 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6149 WARN_ON_ONCE(1);
6152 /* fallthrough from high to low, to select all legal speeds: */
6153 switch (maxspeed) {
6154 case SPEED_10000:
6155 ethtool_link_ksettings_add_link_mode(cmd, supported,
6156 10000baseT_Full);
6157 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6158 10000baseT_Full);
6159 case SPEED_1000:
6160 ethtool_link_ksettings_add_link_mode(cmd, supported,
6161 1000baseT_Full);
6162 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6163 1000baseT_Full);
6164 ethtool_link_ksettings_add_link_mode(cmd, supported,
6165 1000baseT_Half);
6166 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6167 1000baseT_Half);
6168 case SPEED_100:
6169 ethtool_link_ksettings_add_link_mode(cmd, supported,
6170 100baseT_Full);
6171 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6172 100baseT_Full);
6173 ethtool_link_ksettings_add_link_mode(cmd, supported,
6174 100baseT_Half);
6175 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6176 100baseT_Half);
6177 case SPEED_10:
6178 ethtool_link_ksettings_add_link_mode(cmd, supported,
6179 10baseT_Full);
6180 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6181 10baseT_Full);
6182 ethtool_link_ksettings_add_link_mode(cmd, supported,
6183 10baseT_Half);
6184 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6185 10baseT_Half);
6186 /* end fallthrough */
6187 break;
6188 default:
6189 ethtool_link_ksettings_add_link_mode(cmd, supported,
6190 10baseT_Full);
6191 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6192 10baseT_Full);
6193 ethtool_link_ksettings_add_link_mode(cmd, supported,
6194 10baseT_Half);
6195 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6196 10baseT_Half);
6197 WARN_ON_ONCE(1);
6201 int qeth_core_ethtool_get_link_ksettings(struct net_device *netdev,
6202 struct ethtool_link_ksettings *cmd)
6204 struct qeth_card *card = netdev->ml_priv;
6205 enum qeth_link_types link_type;
6206 struct carrier_info carrier_info;
6207 int rc;
6209 if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
6210 link_type = QETH_LINK_TYPE_10GBIT_ETH;
6211 else
6212 link_type = card->info.link_type;
6214 cmd->base.duplex = DUPLEX_FULL;
6215 cmd->base.autoneg = AUTONEG_ENABLE;
6216 cmd->base.phy_address = 0;
6217 cmd->base.mdio_support = 0;
6218 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
6219 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
6221 switch (link_type) {
6222 case QETH_LINK_TYPE_FAST_ETH:
6223 case QETH_LINK_TYPE_LANE_ETH100:
6224 cmd->base.speed = SPEED_100;
6225 cmd->base.port = PORT_TP;
6226 break;
6227 case QETH_LINK_TYPE_GBIT_ETH:
6228 case QETH_LINK_TYPE_LANE_ETH1000:
6229 cmd->base.speed = SPEED_1000;
6230 cmd->base.port = PORT_FIBRE;
6231 break;
6232 case QETH_LINK_TYPE_10GBIT_ETH:
6233 cmd->base.speed = SPEED_10000;
6234 cmd->base.port = PORT_FIBRE;
6235 break;
6236 default:
6237 cmd->base.speed = SPEED_10;
6238 cmd->base.port = PORT_TP;
6240 qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port);
6242 /* Check if we can obtain more accurate information. */
6243 /* If QUERY_CARD_INFO command is not supported or fails, */
6244 /* just return the heuristics that was filled above. */
6245 if (!qeth_card_hw_is_reachable(card))
6246 return -ENODEV;
6247 rc = qeth_query_card_info(card, &carrier_info);
6248 if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
6249 return 0;
6250 if (rc) /* report error from the hardware operation */
6251 return rc;
6252 /* on success, fill in the information got from the hardware */
6254 netdev_dbg(netdev,
6255 "card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
6256 carrier_info.card_type,
6257 carrier_info.port_mode,
6258 carrier_info.port_speed);
6260 /* Update attributes for which we've obtained more authoritative */
6261 /* information, leave the rest the way they where filled above. */
6262 switch (carrier_info.card_type) {
6263 case CARD_INFO_TYPE_1G_COPPER_A:
6264 case CARD_INFO_TYPE_1G_COPPER_B:
6265 cmd->base.port = PORT_TP;
6266 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6267 break;
6268 case CARD_INFO_TYPE_1G_FIBRE_A:
6269 case CARD_INFO_TYPE_1G_FIBRE_B:
6270 cmd->base.port = PORT_FIBRE;
6271 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6272 break;
6273 case CARD_INFO_TYPE_10G_FIBRE_A:
6274 case CARD_INFO_TYPE_10G_FIBRE_B:
6275 cmd->base.port = PORT_FIBRE;
6276 qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port);
6277 break;
6280 switch (carrier_info.port_mode) {
6281 case CARD_INFO_PORTM_FULLDUPLEX:
6282 cmd->base.duplex = DUPLEX_FULL;
6283 break;
6284 case CARD_INFO_PORTM_HALFDUPLEX:
6285 cmd->base.duplex = DUPLEX_HALF;
6286 break;
6289 switch (carrier_info.port_speed) {
6290 case CARD_INFO_PORTS_10M:
6291 cmd->base.speed = SPEED_10;
6292 break;
6293 case CARD_INFO_PORTS_100M:
6294 cmd->base.speed = SPEED_100;
6295 break;
6296 case CARD_INFO_PORTS_1G:
6297 cmd->base.speed = SPEED_1000;
6298 break;
6299 case CARD_INFO_PORTS_10G:
6300 cmd->base.speed = SPEED_10000;
6301 break;
6304 return 0;
6306 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_link_ksettings);
6308 /* Callback to handle checksum offload command reply from OSA card.
6309 * Verify that required features have been enabled on the card.
6310 * Return error in hdr->return_code as this value is checked by caller.
6312 * Always returns zero to indicate no further messages from the OSA card.
6314 static int qeth_ipa_checksum_run_cmd_cb(struct qeth_card *card,
6315 struct qeth_reply *reply,
6316 unsigned long data)
6318 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
6319 struct qeth_checksum_cmd *chksum_cb =
6320 (struct qeth_checksum_cmd *)reply->param;
6322 QETH_CARD_TEXT(card, 4, "chkdoccb");
6323 if (qeth_setassparms_inspect_rc(cmd))
6324 return 0;
6326 memset(chksum_cb, 0, sizeof(*chksum_cb));
6327 if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
6328 chksum_cb->supported =
6329 cmd->data.setassparms.data.chksum.supported;
6330 QETH_CARD_TEXT_(card, 3, "strt:%x", chksum_cb->supported);
6332 if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_ENABLE) {
6333 chksum_cb->supported =
6334 cmd->data.setassparms.data.chksum.supported;
6335 chksum_cb->enabled =
6336 cmd->data.setassparms.data.chksum.enabled;
6337 QETH_CARD_TEXT_(card, 3, "supp:%x", chksum_cb->supported);
6338 QETH_CARD_TEXT_(card, 3, "enab:%x", chksum_cb->enabled);
6340 return 0;
6343 /* Send command to OSA card and check results. */
6344 static int qeth_ipa_checksum_run_cmd(struct qeth_card *card,
6345 enum qeth_ipa_funcs ipa_func,
6346 __u16 cmd_code, long data,
6347 struct qeth_checksum_cmd *chksum_cb,
6348 enum qeth_prot_versions prot)
6350 struct qeth_cmd_buffer *iob;
6351 int rc = -ENOMEM;
6353 QETH_CARD_TEXT(card, 4, "chkdocmd");
6354 iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
6355 sizeof(__u32), prot);
6356 if (iob)
6357 rc = qeth_send_setassparms(card, iob, sizeof(__u32), data,
6358 qeth_ipa_checksum_run_cmd_cb,
6359 chksum_cb);
6360 return rc;
6363 static int qeth_send_checksum_on(struct qeth_card *card, int cstype,
6364 enum qeth_prot_versions prot)
6366 u32 required_features = QETH_IPA_CHECKSUM_UDP | QETH_IPA_CHECKSUM_TCP;
6367 struct qeth_checksum_cmd chksum_cb;
6368 int rc;
6370 if (prot == QETH_PROT_IPV4)
6371 required_features |= QETH_IPA_CHECKSUM_IP_HDR;
6372 rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_START, 0,
6373 &chksum_cb, prot);
6374 if (!rc) {
6375 if ((required_features & chksum_cb.supported) !=
6376 required_features)
6377 rc = -EIO;
6378 else if (!(QETH_IPA_CHECKSUM_LP2LP & chksum_cb.supported) &&
6379 cstype == IPA_INBOUND_CHECKSUM)
6380 dev_warn(&card->gdev->dev,
6381 "Hardware checksumming is performed only if %s and its peer use different OSA Express 3 ports\n",
6382 QETH_CARD_IFNAME(card));
6384 if (rc) {
6385 qeth_send_simple_setassparms_prot(card, cstype,
6386 IPA_CMD_ASS_STOP, 0, prot);
6387 dev_warn(&card->gdev->dev,
6388 "Starting HW IPv%d checksumming for %s failed, using SW checksumming\n",
6389 prot, QETH_CARD_IFNAME(card));
6390 return rc;
6392 rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_ENABLE,
6393 chksum_cb.supported, &chksum_cb,
6394 prot);
6395 if (!rc) {
6396 if ((required_features & chksum_cb.enabled) !=
6397 required_features)
6398 rc = -EIO;
6400 if (rc) {
6401 qeth_send_simple_setassparms_prot(card, cstype,
6402 IPA_CMD_ASS_STOP, 0, prot);
6403 dev_warn(&card->gdev->dev,
6404 "Enabling HW IPv%d checksumming for %s failed, using SW checksumming\n",
6405 prot, QETH_CARD_IFNAME(card));
6406 return rc;
6409 dev_info(&card->gdev->dev, "HW Checksumming (%sbound IPv%d) enabled\n",
6410 cstype == IPA_INBOUND_CHECKSUM ? "in" : "out", prot);
6411 return 0;
6414 static int qeth_set_ipa_csum(struct qeth_card *card, bool on, int cstype,
6415 enum qeth_prot_versions prot)
6417 int rc = (on) ? qeth_send_checksum_on(card, cstype, prot)
6418 : qeth_send_simple_setassparms_prot(card, cstype,
6419 IPA_CMD_ASS_STOP, 0,
6420 prot);
6421 return rc ? -EIO : 0;
6424 static int qeth_set_ipa_tso(struct qeth_card *card, int on)
6426 int rc;
6428 QETH_CARD_TEXT(card, 3, "sttso");
6430 if (on) {
6431 rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6432 IPA_CMD_ASS_START, 0);
6433 if (rc) {
6434 dev_warn(&card->gdev->dev,
6435 "Starting outbound TCP segmentation offload for %s failed\n",
6436 QETH_CARD_IFNAME(card));
6437 return -EIO;
6439 dev_info(&card->gdev->dev, "Outbound TSO enabled\n");
6440 } else {
6441 rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6442 IPA_CMD_ASS_STOP, 0);
6444 return rc;
6447 static int qeth_set_ipa_rx_csum(struct qeth_card *card, bool on)
6449 int rc_ipv4 = (on) ? -EOPNOTSUPP : 0;
6450 int rc_ipv6;
6452 if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM))
6453 rc_ipv4 = qeth_set_ipa_csum(card, on, IPA_INBOUND_CHECKSUM,
6454 QETH_PROT_IPV4);
6455 if (!qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6))
6456 /* no/one Offload Assist available, so the rc is trivial */
6457 return rc_ipv4;
6459 rc_ipv6 = qeth_set_ipa_csum(card, on, IPA_INBOUND_CHECKSUM,
6460 QETH_PROT_IPV6);
6462 if (on)
6463 /* enable: success if any Assist is active */
6464 return (rc_ipv6) ? rc_ipv4 : 0;
6466 /* disable: failure if any Assist is still active */
6467 return (rc_ipv6) ? rc_ipv6 : rc_ipv4;
6470 #define QETH_HW_FEATURES (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_TSO | \
6471 NETIF_F_IPV6_CSUM)
6473 * qeth_enable_hw_features() - (Re-)Enable HW functions for device features
6474 * @dev: a net_device
6476 void qeth_enable_hw_features(struct net_device *dev)
6478 struct qeth_card *card = dev->ml_priv;
6479 netdev_features_t features;
6481 rtnl_lock();
6482 features = dev->features;
6483 /* force-off any feature that needs an IPA sequence.
6484 * netdev_update_features() will restart them.
6486 dev->features &= ~QETH_HW_FEATURES;
6487 netdev_update_features(dev);
6488 if (features != dev->features)
6489 dev_warn(&card->gdev->dev,
6490 "Device recovery failed to restore all offload features\n");
6491 rtnl_unlock();
6493 EXPORT_SYMBOL_GPL(qeth_enable_hw_features);
6495 int qeth_set_features(struct net_device *dev, netdev_features_t features)
6497 struct qeth_card *card = dev->ml_priv;
6498 netdev_features_t changed = dev->features ^ features;
6499 int rc = 0;
6501 QETH_DBF_TEXT(SETUP, 2, "setfeat");
6502 QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6504 if ((changed & NETIF_F_IP_CSUM)) {
6505 rc = qeth_set_ipa_csum(card, features & NETIF_F_IP_CSUM,
6506 IPA_OUTBOUND_CHECKSUM, QETH_PROT_IPV4);
6507 if (rc)
6508 changed ^= NETIF_F_IP_CSUM;
6510 if (changed & NETIF_F_IPV6_CSUM) {
6511 rc = qeth_set_ipa_csum(card, features & NETIF_F_IPV6_CSUM,
6512 IPA_OUTBOUND_CHECKSUM, QETH_PROT_IPV6);
6513 if (rc)
6514 changed ^= NETIF_F_IPV6_CSUM;
6516 if (changed & NETIF_F_RXCSUM) {
6517 rc = qeth_set_ipa_rx_csum(card, features & NETIF_F_RXCSUM);
6518 if (rc)
6519 changed ^= NETIF_F_RXCSUM;
6521 if ((changed & NETIF_F_TSO)) {
6522 rc = qeth_set_ipa_tso(card, features & NETIF_F_TSO ? 1 : 0);
6523 if (rc)
6524 changed ^= NETIF_F_TSO;
6527 /* everything changed successfully? */
6528 if ((dev->features ^ features) == changed)
6529 return 0;
6530 /* something went wrong. save changed features and return error */
6531 dev->features ^= changed;
6532 return -EIO;
6534 EXPORT_SYMBOL_GPL(qeth_set_features);
6536 netdev_features_t qeth_fix_features(struct net_device *dev,
6537 netdev_features_t features)
6539 struct qeth_card *card = dev->ml_priv;
6541 QETH_DBF_TEXT(SETUP, 2, "fixfeat");
6542 if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
6543 features &= ~NETIF_F_IP_CSUM;
6544 if (!qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6))
6545 features &= ~NETIF_F_IPV6_CSUM;
6546 if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM) &&
6547 !qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6))
6548 features &= ~NETIF_F_RXCSUM;
6549 if (!qeth_is_supported(card, IPA_OUTBOUND_TSO))
6550 features &= ~NETIF_F_TSO;
6551 /* if the card isn't up, remove features that require hw changes */
6552 if (card->state == CARD_STATE_DOWN ||
6553 card->state == CARD_STATE_RECOVER)
6554 features &= ~QETH_HW_FEATURES;
6555 QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6556 return features;
6558 EXPORT_SYMBOL_GPL(qeth_fix_features);
6560 netdev_features_t qeth_features_check(struct sk_buff *skb,
6561 struct net_device *dev,
6562 netdev_features_t features)
6564 /* GSO segmentation builds skbs with
6565 * a (small) linear part for the headers, and
6566 * page frags for the data.
6567 * Compared to a linear skb, the header-only part consumes an
6568 * additional buffer element. This reduces buffer utilization, and
6569 * hurts throughput. So compress small segments into one element.
6571 if (netif_needs_gso(skb, features)) {
6572 /* match skb_segment(): */
6573 unsigned int doffset = skb->data - skb_mac_header(skb);
6574 unsigned int hsize = skb_shinfo(skb)->gso_size;
6575 unsigned int hroom = skb_headroom(skb);
6577 /* linearize only if resulting skb allocations are order-0: */
6578 if (SKB_DATA_ALIGN(hroom + doffset + hsize) <= SKB_MAX_HEAD(0))
6579 features &= ~NETIF_F_SG;
6582 return vlan_features_check(skb, features);
6584 EXPORT_SYMBOL_GPL(qeth_features_check);
6586 static int __init qeth_core_init(void)
6588 int rc;
6590 pr_info("loading core functions\n");
6591 INIT_LIST_HEAD(&qeth_core_card_list.list);
6592 INIT_LIST_HEAD(&qeth_dbf_list);
6593 rwlock_init(&qeth_core_card_list.rwlock);
6594 mutex_init(&qeth_mod_mutex);
6596 qeth_wq = create_singlethread_workqueue("qeth_wq");
6597 if (!qeth_wq) {
6598 rc = -ENOMEM;
6599 goto out_err;
6602 rc = qeth_register_dbf_views();
6603 if (rc)
6604 goto dbf_err;
6605 qeth_core_root_dev = root_device_register("qeth");
6606 rc = PTR_ERR_OR_ZERO(qeth_core_root_dev);
6607 if (rc)
6608 goto register_err;
6609 qeth_core_header_cache = kmem_cache_create("qeth_hdr",
6610 sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
6611 if (!qeth_core_header_cache) {
6612 rc = -ENOMEM;
6613 goto slab_err;
6615 qeth_qdio_outbuf_cache = kmem_cache_create("qeth_buf",
6616 sizeof(struct qeth_qdio_out_buffer), 0, 0, NULL);
6617 if (!qeth_qdio_outbuf_cache) {
6618 rc = -ENOMEM;
6619 goto cqslab_err;
6621 rc = ccw_driver_register(&qeth_ccw_driver);
6622 if (rc)
6623 goto ccw_err;
6624 qeth_core_ccwgroup_driver.driver.groups = qeth_drv_attr_groups;
6625 rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
6626 if (rc)
6627 goto ccwgroup_err;
6629 return 0;
6631 ccwgroup_err:
6632 ccw_driver_unregister(&qeth_ccw_driver);
6633 ccw_err:
6634 kmem_cache_destroy(qeth_qdio_outbuf_cache);
6635 cqslab_err:
6636 kmem_cache_destroy(qeth_core_header_cache);
6637 slab_err:
6638 root_device_unregister(qeth_core_root_dev);
6639 register_err:
6640 qeth_unregister_dbf_views();
6641 dbf_err:
6642 destroy_workqueue(qeth_wq);
6643 out_err:
6644 pr_err("Initializing the qeth device driver failed\n");
6645 return rc;
6648 static void __exit qeth_core_exit(void)
6650 qeth_clear_dbf_list();
6651 destroy_workqueue(qeth_wq);
6652 ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
6653 ccw_driver_unregister(&qeth_ccw_driver);
6654 kmem_cache_destroy(qeth_qdio_outbuf_cache);
6655 kmem_cache_destroy(qeth_core_header_cache);
6656 root_device_unregister(qeth_core_root_dev);
6657 qeth_unregister_dbf_views();
6658 pr_info("core functions removed\n");
6661 module_init(qeth_core_init);
6662 module_exit(qeth_core_exit);
6663 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
6664 MODULE_DESCRIPTION("qeth core functions");
6665 MODULE_LICENSE("GPL");