Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / s390 / net / qeth_core_main.c
blobca72f3311004a3d020ed07266596ff63ce740b26
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/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/ip.h>
19 #include <linux/tcp.h>
20 #include <linux/mii.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
23 #include <linux/if_vlan.h>
24 #include <linux/netdevice.h>
25 #include <linux/netdev_features.h>
26 #include <linux/skbuff.h>
28 #include <net/iucv/af_iucv.h>
29 #include <net/dsfield.h>
31 #include <asm/ebcdic.h>
32 #include <asm/chpid.h>
33 #include <asm/io.h>
34 #include <asm/sysinfo.h>
35 #include <asm/compat.h>
36 #include <asm/diag.h>
37 #include <asm/cio.h>
38 #include <asm/ccwdev.h>
39 #include <asm/cpcmd.h>
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 void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
77 struct qeth_qdio_out_buffer *buf,
78 enum qeth_qdio_buffer_states newbufstate);
79 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *, int);
81 struct workqueue_struct *qeth_wq;
82 EXPORT_SYMBOL_GPL(qeth_wq);
84 int qeth_card_hw_is_reachable(struct qeth_card *card)
86 return (card->state == CARD_STATE_SOFTSETUP) ||
87 (card->state == CARD_STATE_UP);
89 EXPORT_SYMBOL_GPL(qeth_card_hw_is_reachable);
91 static void qeth_close_dev_handler(struct work_struct *work)
93 struct qeth_card *card;
95 card = container_of(work, struct qeth_card, close_dev_work);
96 QETH_CARD_TEXT(card, 2, "cldevhdl");
97 rtnl_lock();
98 dev_close(card->dev);
99 rtnl_unlock();
100 ccwgroup_set_offline(card->gdev);
103 void qeth_close_dev(struct qeth_card *card)
105 QETH_CARD_TEXT(card, 2, "cldevsubm");
106 queue_work(qeth_wq, &card->close_dev_work);
108 EXPORT_SYMBOL_GPL(qeth_close_dev);
110 static const char *qeth_get_cardname(struct qeth_card *card)
112 if (card->info.guestlan) {
113 switch (card->info.type) {
114 case QETH_CARD_TYPE_OSD:
115 return " Virtual NIC QDIO";
116 case QETH_CARD_TYPE_IQD:
117 return " Virtual NIC Hiper";
118 case QETH_CARD_TYPE_OSM:
119 return " Virtual NIC QDIO - OSM";
120 case QETH_CARD_TYPE_OSX:
121 return " Virtual NIC QDIO - OSX";
122 default:
123 return " unknown";
125 } else {
126 switch (card->info.type) {
127 case QETH_CARD_TYPE_OSD:
128 return " OSD Express";
129 case QETH_CARD_TYPE_IQD:
130 return " HiperSockets";
131 case QETH_CARD_TYPE_OSN:
132 return " OSN QDIO";
133 case QETH_CARD_TYPE_OSM:
134 return " OSM QDIO";
135 case QETH_CARD_TYPE_OSX:
136 return " OSX QDIO";
137 default:
138 return " unknown";
141 return " n/a";
144 /* max length to be returned: 14 */
145 const char *qeth_get_cardname_short(struct qeth_card *card)
147 if (card->info.guestlan) {
148 switch (card->info.type) {
149 case QETH_CARD_TYPE_OSD:
150 return "Virt.NIC QDIO";
151 case QETH_CARD_TYPE_IQD:
152 return "Virt.NIC Hiper";
153 case QETH_CARD_TYPE_OSM:
154 return "Virt.NIC OSM";
155 case QETH_CARD_TYPE_OSX:
156 return "Virt.NIC OSX";
157 default:
158 return "unknown";
160 } else {
161 switch (card->info.type) {
162 case QETH_CARD_TYPE_OSD:
163 switch (card->info.link_type) {
164 case QETH_LINK_TYPE_FAST_ETH:
165 return "OSD_100";
166 case QETH_LINK_TYPE_HSTR:
167 return "HSTR";
168 case QETH_LINK_TYPE_GBIT_ETH:
169 return "OSD_1000";
170 case QETH_LINK_TYPE_10GBIT_ETH:
171 return "OSD_10GIG";
172 case QETH_LINK_TYPE_LANE_ETH100:
173 return "OSD_FE_LANE";
174 case QETH_LINK_TYPE_LANE_TR:
175 return "OSD_TR_LANE";
176 case QETH_LINK_TYPE_LANE_ETH1000:
177 return "OSD_GbE_LANE";
178 case QETH_LINK_TYPE_LANE:
179 return "OSD_ATM_LANE";
180 default:
181 return "OSD_Express";
183 case QETH_CARD_TYPE_IQD:
184 return "HiperSockets";
185 case QETH_CARD_TYPE_OSN:
186 return "OSN";
187 case QETH_CARD_TYPE_OSM:
188 return "OSM_1000";
189 case QETH_CARD_TYPE_OSX:
190 return "OSX_10GIG";
191 default:
192 return "unknown";
195 return "n/a";
198 void qeth_set_recovery_task(struct qeth_card *card)
200 card->recovery_task = current;
202 EXPORT_SYMBOL_GPL(qeth_set_recovery_task);
204 void qeth_clear_recovery_task(struct qeth_card *card)
206 card->recovery_task = NULL;
208 EXPORT_SYMBOL_GPL(qeth_clear_recovery_task);
210 static bool qeth_is_recovery_task(const struct qeth_card *card)
212 return card->recovery_task == current;
215 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
216 int clear_start_mask)
218 unsigned long flags;
220 spin_lock_irqsave(&card->thread_mask_lock, flags);
221 card->thread_allowed_mask = threads;
222 if (clear_start_mask)
223 card->thread_start_mask &= threads;
224 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
225 wake_up(&card->wait_q);
227 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
229 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
231 unsigned long flags;
232 int rc = 0;
234 spin_lock_irqsave(&card->thread_mask_lock, flags);
235 rc = (card->thread_running_mask & threads);
236 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
237 return rc;
239 EXPORT_SYMBOL_GPL(qeth_threads_running);
241 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
243 if (qeth_is_recovery_task(card))
244 return 0;
245 return wait_event_interruptible(card->wait_q,
246 qeth_threads_running(card, threads) == 0);
248 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
250 void qeth_clear_working_pool_list(struct qeth_card *card)
252 struct qeth_buffer_pool_entry *pool_entry, *tmp;
254 QETH_CARD_TEXT(card, 5, "clwrklst");
255 list_for_each_entry_safe(pool_entry, tmp,
256 &card->qdio.in_buf_pool.entry_list, list){
257 list_del(&pool_entry->list);
260 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
262 static int qeth_alloc_buffer_pool(struct qeth_card *card)
264 struct qeth_buffer_pool_entry *pool_entry;
265 void *ptr;
266 int i, j;
268 QETH_CARD_TEXT(card, 5, "alocpool");
269 for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
270 pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
271 if (!pool_entry) {
272 qeth_free_buffer_pool(card);
273 return -ENOMEM;
275 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
276 ptr = (void *) __get_free_page(GFP_KERNEL);
277 if (!ptr) {
278 while (j > 0)
279 free_page((unsigned long)
280 pool_entry->elements[--j]);
281 kfree(pool_entry);
282 qeth_free_buffer_pool(card);
283 return -ENOMEM;
285 pool_entry->elements[j] = ptr;
287 list_add(&pool_entry->init_list,
288 &card->qdio.init_pool.entry_list);
290 return 0;
293 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
295 QETH_CARD_TEXT(card, 2, "realcbp");
297 if ((card->state != CARD_STATE_DOWN) &&
298 (card->state != CARD_STATE_RECOVER))
299 return -EPERM;
301 /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
302 qeth_clear_working_pool_list(card);
303 qeth_free_buffer_pool(card);
304 card->qdio.in_buf_pool.buf_count = bufcnt;
305 card->qdio.init_pool.buf_count = bufcnt;
306 return qeth_alloc_buffer_pool(card);
308 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
310 static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
312 if (!q)
313 return;
315 qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
316 kfree(q);
319 static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
321 struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
322 int i;
324 if (!q)
325 return NULL;
327 if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
328 kfree(q);
329 return NULL;
332 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
333 q->bufs[i].buffer = q->qdio_bufs[i];
335 QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
336 return q;
339 static int qeth_cq_init(struct qeth_card *card)
341 int rc;
343 if (card->options.cq == QETH_CQ_ENABLED) {
344 QETH_DBF_TEXT(SETUP, 2, "cqinit");
345 qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
346 QDIO_MAX_BUFFERS_PER_Q);
347 card->qdio.c_q->next_buf_to_init = 127;
348 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
349 card->qdio.no_in_queues - 1, 0,
350 127);
351 if (rc) {
352 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
353 goto out;
356 rc = 0;
357 out:
358 return rc;
361 static int qeth_alloc_cq(struct qeth_card *card)
363 int rc;
365 if (card->options.cq == QETH_CQ_ENABLED) {
366 int i;
367 struct qdio_outbuf_state *outbuf_states;
369 QETH_DBF_TEXT(SETUP, 2, "cqon");
370 card->qdio.c_q = qeth_alloc_qdio_queue();
371 if (!card->qdio.c_q) {
372 rc = -1;
373 goto kmsg_out;
375 card->qdio.no_in_queues = 2;
376 card->qdio.out_bufstates =
377 kzalloc(card->qdio.no_out_queues *
378 QDIO_MAX_BUFFERS_PER_Q *
379 sizeof(struct qdio_outbuf_state), GFP_KERNEL);
380 outbuf_states = card->qdio.out_bufstates;
381 if (outbuf_states == NULL) {
382 rc = -1;
383 goto free_cq_out;
385 for (i = 0; i < card->qdio.no_out_queues; ++i) {
386 card->qdio.out_qs[i]->bufstates = outbuf_states;
387 outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
389 } else {
390 QETH_DBF_TEXT(SETUP, 2, "nocq");
391 card->qdio.c_q = NULL;
392 card->qdio.no_in_queues = 1;
394 QETH_DBF_TEXT_(SETUP, 2, "iqc%d", card->qdio.no_in_queues);
395 rc = 0;
396 out:
397 return rc;
398 free_cq_out:
399 qeth_free_qdio_queue(card->qdio.c_q);
400 card->qdio.c_q = NULL;
401 kmsg_out:
402 dev_err(&card->gdev->dev, "Failed to create completion queue\n");
403 goto out;
406 static void qeth_free_cq(struct qeth_card *card)
408 if (card->qdio.c_q) {
409 --card->qdio.no_in_queues;
410 qeth_free_qdio_queue(card->qdio.c_q);
411 card->qdio.c_q = NULL;
413 kfree(card->qdio.out_bufstates);
414 card->qdio.out_bufstates = NULL;
417 static enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
418 int delayed)
420 enum iucv_tx_notify n;
422 switch (sbalf15) {
423 case 0:
424 n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
425 break;
426 case 4:
427 case 16:
428 case 17:
429 case 18:
430 n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
431 TX_NOTIFY_UNREACHABLE;
432 break;
433 default:
434 n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
435 TX_NOTIFY_GENERALERROR;
436 break;
439 return n;
442 static void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q, int bidx,
443 int forced_cleanup)
445 if (q->card->options.cq != QETH_CQ_ENABLED)
446 return;
448 if (q->bufs[bidx]->next_pending != NULL) {
449 struct qeth_qdio_out_buffer *head = q->bufs[bidx];
450 struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
452 while (c) {
453 if (forced_cleanup ||
454 atomic_read(&c->state) ==
455 QETH_QDIO_BUF_HANDLED_DELAYED) {
456 struct qeth_qdio_out_buffer *f = c;
457 QETH_CARD_TEXT(f->q->card, 5, "fp");
458 QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
459 /* release here to avoid interleaving between
460 outbound tasklet and inbound tasklet
461 regarding notifications and lifecycle */
462 qeth_release_skbs(c);
464 c = f->next_pending;
465 WARN_ON_ONCE(head->next_pending != f);
466 head->next_pending = c;
467 kmem_cache_free(qeth_qdio_outbuf_cache, f);
468 } else {
469 head = c;
470 c = c->next_pending;
475 if (forced_cleanup && (atomic_read(&(q->bufs[bidx]->state)) ==
476 QETH_QDIO_BUF_HANDLED_DELAYED)) {
477 /* for recovery situations */
478 q->bufs[bidx]->aob = q->bufstates[bidx].aob;
479 qeth_init_qdio_out_buf(q, bidx);
480 QETH_CARD_TEXT(q->card, 2, "clprecov");
485 static void qeth_qdio_handle_aob(struct qeth_card *card,
486 unsigned long phys_aob_addr)
488 struct qaob *aob;
489 struct qeth_qdio_out_buffer *buffer;
490 enum iucv_tx_notify notification;
492 aob = (struct qaob *) phys_to_virt(phys_aob_addr);
493 QETH_CARD_TEXT(card, 5, "haob");
494 QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
495 buffer = (struct qeth_qdio_out_buffer *) aob->user1;
496 QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
498 if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
499 QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
500 notification = TX_NOTIFY_OK;
501 } else {
502 WARN_ON_ONCE(atomic_read(&buffer->state) !=
503 QETH_QDIO_BUF_PENDING);
504 atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
505 notification = TX_NOTIFY_DELAYED_OK;
508 if (aob->aorc != 0) {
509 QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
510 notification = qeth_compute_cq_notification(aob->aorc, 1);
512 qeth_notify_skbs(buffer->q, buffer, notification);
514 buffer->aob = NULL;
515 qeth_clear_output_buffer(buffer->q, buffer,
516 QETH_QDIO_BUF_HANDLED_DELAYED);
518 /* from here on: do not touch buffer anymore */
519 qdio_release_aob(aob);
522 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
524 return card->options.cq == QETH_CQ_ENABLED &&
525 card->qdio.c_q != NULL &&
526 queue != 0 &&
527 queue == card->qdio.no_in_queues - 1;
531 static int qeth_issue_next_read(struct qeth_card *card)
533 int rc;
534 struct qeth_cmd_buffer *iob;
536 QETH_CARD_TEXT(card, 5, "issnxrd");
537 if (card->read.state != CH_STATE_UP)
538 return -EIO;
539 iob = qeth_get_buffer(&card->read);
540 if (!iob) {
541 dev_warn(&card->gdev->dev, "The qeth device driver "
542 "failed to recover an error on the device\n");
543 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
544 "available\n", dev_name(&card->gdev->dev));
545 return -ENOMEM;
547 qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
548 QETH_CARD_TEXT(card, 6, "noirqpnd");
549 rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
550 (addr_t) iob, 0, 0);
551 if (rc) {
552 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
553 "rc=%i\n", dev_name(&card->gdev->dev), rc);
554 atomic_set(&card->read.irq_pending, 0);
555 card->read_or_write_problem = 1;
556 qeth_schedule_recovery(card);
557 wake_up(&card->wait_q);
559 return rc;
562 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
564 struct qeth_reply *reply;
566 reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
567 if (reply) {
568 refcount_set(&reply->refcnt, 1);
569 atomic_set(&reply->received, 0);
570 reply->card = card;
572 return reply;
575 static void qeth_get_reply(struct qeth_reply *reply)
577 refcount_inc(&reply->refcnt);
580 static void qeth_put_reply(struct qeth_reply *reply)
582 if (refcount_dec_and_test(&reply->refcnt))
583 kfree(reply);
586 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
587 struct qeth_card *card)
589 char *ipa_name;
590 int com = cmd->hdr.command;
591 ipa_name = qeth_get_ipa_cmd_name(com);
592 if (rc)
593 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s/%s returned "
594 "x%X \"%s\"\n",
595 ipa_name, com, dev_name(&card->gdev->dev),
596 QETH_CARD_IFNAME(card), rc,
597 qeth_get_ipa_msg(rc));
598 else
599 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s/%s succeeded\n",
600 ipa_name, com, dev_name(&card->gdev->dev),
601 QETH_CARD_IFNAME(card));
604 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
605 struct qeth_cmd_buffer *iob)
607 struct qeth_ipa_cmd *cmd = NULL;
609 QETH_CARD_TEXT(card, 5, "chkipad");
610 if (IS_IPA(iob->data)) {
611 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
612 if (IS_IPA_REPLY(cmd)) {
613 if (cmd->hdr.command != IPA_CMD_SETCCID &&
614 cmd->hdr.command != IPA_CMD_DELCCID &&
615 cmd->hdr.command != IPA_CMD_MODCCID &&
616 cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
617 qeth_issue_ipa_msg(cmd,
618 cmd->hdr.return_code, card);
619 return cmd;
620 } else {
621 switch (cmd->hdr.command) {
622 case IPA_CMD_STOPLAN:
623 if (cmd->hdr.return_code ==
624 IPA_RC_VEPA_TO_VEB_TRANSITION) {
625 dev_err(&card->gdev->dev,
626 "Interface %s is down because the "
627 "adjacent port is no longer in "
628 "reflective relay mode\n",
629 QETH_CARD_IFNAME(card));
630 qeth_close_dev(card);
631 } else {
632 dev_warn(&card->gdev->dev,
633 "The link for interface %s on CHPID"
634 " 0x%X failed\n",
635 QETH_CARD_IFNAME(card),
636 card->info.chpid);
637 qeth_issue_ipa_msg(cmd,
638 cmd->hdr.return_code, card);
640 card->lan_online = 0;
641 if (card->dev && netif_carrier_ok(card->dev))
642 netif_carrier_off(card->dev);
643 return NULL;
644 case IPA_CMD_STARTLAN:
645 dev_info(&card->gdev->dev,
646 "The link for %s on CHPID 0x%X has"
647 " been restored\n",
648 QETH_CARD_IFNAME(card),
649 card->info.chpid);
650 netif_carrier_on(card->dev);
651 card->lan_online = 1;
652 if (card->info.hwtrap)
653 card->info.hwtrap = 2;
654 qeth_schedule_recovery(card);
655 return NULL;
656 case IPA_CMD_SETBRIDGEPORT_IQD:
657 case IPA_CMD_SETBRIDGEPORT_OSA:
658 case IPA_CMD_ADDRESS_CHANGE_NOTIF:
659 if (card->discipline->control_event_handler
660 (card, cmd))
661 return cmd;
662 else
663 return NULL;
664 case IPA_CMD_MODCCID:
665 return cmd;
666 case IPA_CMD_REGISTER_LOCAL_ADDR:
667 QETH_CARD_TEXT(card, 3, "irla");
668 break;
669 case IPA_CMD_UNREGISTER_LOCAL_ADDR:
670 QETH_CARD_TEXT(card, 3, "urla");
671 break;
672 default:
673 QETH_DBF_MESSAGE(2, "Received data is IPA "
674 "but not a reply!\n");
675 break;
679 return cmd;
682 void qeth_clear_ipacmd_list(struct qeth_card *card)
684 struct qeth_reply *reply, *r;
685 unsigned long flags;
687 QETH_CARD_TEXT(card, 4, "clipalst");
689 spin_lock_irqsave(&card->lock, flags);
690 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
691 qeth_get_reply(reply);
692 reply->rc = -EIO;
693 atomic_inc(&reply->received);
694 list_del_init(&reply->list);
695 wake_up(&reply->wait_q);
696 qeth_put_reply(reply);
698 spin_unlock_irqrestore(&card->lock, flags);
699 atomic_set(&card->write.irq_pending, 0);
701 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
703 static int qeth_check_idx_response(struct qeth_card *card,
704 unsigned char *buffer)
706 if (!buffer)
707 return 0;
709 QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
710 if ((buffer[2] & 0xc0) == 0xc0) {
711 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
712 "with cause code 0x%02x%s\n",
713 buffer[4],
714 ((buffer[4] == 0x22) ?
715 " -- try another portname" : ""));
716 QETH_CARD_TEXT(card, 2, "ckidxres");
717 QETH_CARD_TEXT(card, 2, " idxterm");
718 QETH_CARD_TEXT_(card, 2, " rc%d", -EIO);
719 if (buffer[4] == 0xf6) {
720 dev_err(&card->gdev->dev,
721 "The qeth device is not configured "
722 "for the OSI layer required by z/VM\n");
723 return -EPERM;
725 return -EIO;
727 return 0;
730 static struct qeth_card *CARD_FROM_CDEV(struct ccw_device *cdev)
732 struct qeth_card *card = dev_get_drvdata(&((struct ccwgroup_device *)
733 dev_get_drvdata(&cdev->dev))->dev);
734 return card;
737 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
738 __u32 len)
740 struct qeth_card *card;
742 card = CARD_FROM_CDEV(channel->ccwdev);
743 QETH_CARD_TEXT(card, 4, "setupccw");
744 if (channel == &card->read)
745 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
746 else
747 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
748 channel->ccw.count = len;
749 channel->ccw.cda = (__u32) __pa(iob);
752 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
754 __u8 index;
756 QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "getbuff");
757 index = channel->io_buf_no;
758 do {
759 if (channel->iob[index].state == BUF_STATE_FREE) {
760 channel->iob[index].state = BUF_STATE_LOCKED;
761 channel->io_buf_no = (channel->io_buf_no + 1) %
762 QETH_CMD_BUFFER_NO;
763 memset(channel->iob[index].data, 0, QETH_BUFSIZE);
764 return channel->iob + index;
766 index = (index + 1) % QETH_CMD_BUFFER_NO;
767 } while (index != channel->io_buf_no);
769 return NULL;
772 void qeth_release_buffer(struct qeth_channel *channel,
773 struct qeth_cmd_buffer *iob)
775 unsigned long flags;
777 QETH_CARD_TEXT(CARD_FROM_CDEV(channel->ccwdev), 6, "relbuff");
778 spin_lock_irqsave(&channel->iob_lock, flags);
779 memset(iob->data, 0, QETH_BUFSIZE);
780 iob->state = BUF_STATE_FREE;
781 iob->callback = qeth_send_control_data_cb;
782 iob->rc = 0;
783 spin_unlock_irqrestore(&channel->iob_lock, flags);
784 wake_up(&channel->wait_q);
786 EXPORT_SYMBOL_GPL(qeth_release_buffer);
788 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
790 struct qeth_cmd_buffer *buffer = NULL;
791 unsigned long flags;
793 spin_lock_irqsave(&channel->iob_lock, flags);
794 buffer = __qeth_get_buffer(channel);
795 spin_unlock_irqrestore(&channel->iob_lock, flags);
796 return buffer;
799 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
801 struct qeth_cmd_buffer *buffer;
802 wait_event(channel->wait_q,
803 ((buffer = qeth_get_buffer(channel)) != NULL));
804 return buffer;
806 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
808 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
810 int cnt;
812 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
813 qeth_release_buffer(channel, &channel->iob[cnt]);
814 channel->buf_no = 0;
815 channel->io_buf_no = 0;
817 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
819 static void qeth_send_control_data_cb(struct qeth_channel *channel,
820 struct qeth_cmd_buffer *iob)
822 struct qeth_card *card;
823 struct qeth_reply *reply, *r;
824 struct qeth_ipa_cmd *cmd;
825 unsigned long flags;
826 int keep_reply;
827 int rc = 0;
829 card = CARD_FROM_CDEV(channel->ccwdev);
830 QETH_CARD_TEXT(card, 4, "sndctlcb");
831 rc = qeth_check_idx_response(card, iob->data);
832 switch (rc) {
833 case 0:
834 break;
835 case -EIO:
836 qeth_clear_ipacmd_list(card);
837 qeth_schedule_recovery(card);
838 /* fall through */
839 default:
840 goto out;
843 cmd = qeth_check_ipa_data(card, iob);
844 if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
845 goto out;
846 /*in case of OSN : check if cmd is set */
847 if (card->info.type == QETH_CARD_TYPE_OSN &&
848 cmd &&
849 cmd->hdr.command != IPA_CMD_STARTLAN &&
850 card->osn_info.assist_cb != NULL) {
851 card->osn_info.assist_cb(card->dev, cmd);
852 goto out;
855 spin_lock_irqsave(&card->lock, flags);
856 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
857 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
858 ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
859 qeth_get_reply(reply);
860 list_del_init(&reply->list);
861 spin_unlock_irqrestore(&card->lock, flags);
862 keep_reply = 0;
863 if (reply->callback != NULL) {
864 if (cmd) {
865 reply->offset = (__u16)((char *)cmd -
866 (char *)iob->data);
867 keep_reply = reply->callback(card,
868 reply,
869 (unsigned long)cmd);
870 } else
871 keep_reply = reply->callback(card,
872 reply,
873 (unsigned long)iob);
875 if (cmd)
876 reply->rc = (u16) cmd->hdr.return_code;
877 else if (iob->rc)
878 reply->rc = iob->rc;
879 if (keep_reply) {
880 spin_lock_irqsave(&card->lock, flags);
881 list_add_tail(&reply->list,
882 &card->cmd_waiter_list);
883 spin_unlock_irqrestore(&card->lock, flags);
884 } else {
885 atomic_inc(&reply->received);
886 wake_up(&reply->wait_q);
888 qeth_put_reply(reply);
889 goto out;
892 spin_unlock_irqrestore(&card->lock, flags);
893 out:
894 memcpy(&card->seqno.pdu_hdr_ack,
895 QETH_PDU_HEADER_SEQ_NO(iob->data),
896 QETH_SEQ_NO_LENGTH);
897 qeth_release_buffer(channel, iob);
900 static int qeth_setup_channel(struct qeth_channel *channel)
902 int cnt;
904 QETH_DBF_TEXT(SETUP, 2, "setupch");
905 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
906 channel->iob[cnt].data =
907 kzalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
908 if (channel->iob[cnt].data == NULL)
909 break;
910 channel->iob[cnt].state = BUF_STATE_FREE;
911 channel->iob[cnt].channel = channel;
912 channel->iob[cnt].callback = qeth_send_control_data_cb;
913 channel->iob[cnt].rc = 0;
915 if (cnt < QETH_CMD_BUFFER_NO) {
916 while (cnt-- > 0)
917 kfree(channel->iob[cnt].data);
918 return -ENOMEM;
920 channel->buf_no = 0;
921 channel->io_buf_no = 0;
922 atomic_set(&channel->irq_pending, 0);
923 spin_lock_init(&channel->iob_lock);
925 init_waitqueue_head(&channel->wait_q);
926 return 0;
929 static int qeth_set_thread_start_bit(struct qeth_card *card,
930 unsigned long thread)
932 unsigned long flags;
934 spin_lock_irqsave(&card->thread_mask_lock, flags);
935 if (!(card->thread_allowed_mask & thread) ||
936 (card->thread_start_mask & thread)) {
937 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
938 return -EPERM;
940 card->thread_start_mask |= thread;
941 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
942 return 0;
945 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
947 unsigned long flags;
949 spin_lock_irqsave(&card->thread_mask_lock, flags);
950 card->thread_start_mask &= ~thread;
951 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
952 wake_up(&card->wait_q);
954 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
956 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
958 unsigned long flags;
960 spin_lock_irqsave(&card->thread_mask_lock, flags);
961 card->thread_running_mask &= ~thread;
962 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
963 wake_up(&card->wait_q);
965 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
967 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
969 unsigned long flags;
970 int rc = 0;
972 spin_lock_irqsave(&card->thread_mask_lock, flags);
973 if (card->thread_start_mask & thread) {
974 if ((card->thread_allowed_mask & thread) &&
975 !(card->thread_running_mask & thread)) {
976 rc = 1;
977 card->thread_start_mask &= ~thread;
978 card->thread_running_mask |= thread;
979 } else
980 rc = -EPERM;
982 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
983 return rc;
986 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
988 int rc = 0;
990 wait_event(card->wait_q,
991 (rc = __qeth_do_run_thread(card, thread)) >= 0);
992 return rc;
994 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
996 void qeth_schedule_recovery(struct qeth_card *card)
998 QETH_CARD_TEXT(card, 2, "startrec");
999 if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
1000 schedule_work(&card->kernel_thread_starter);
1002 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
1004 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
1006 int dstat, cstat;
1007 char *sense;
1008 struct qeth_card *card;
1010 sense = (char *) irb->ecw;
1011 cstat = irb->scsw.cmd.cstat;
1012 dstat = irb->scsw.cmd.dstat;
1013 card = CARD_FROM_CDEV(cdev);
1015 if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
1016 SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
1017 SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
1018 QETH_CARD_TEXT(card, 2, "CGENCHK");
1019 dev_warn(&cdev->dev, "The qeth device driver "
1020 "failed to recover an error on the device\n");
1021 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x\n",
1022 dev_name(&cdev->dev), dstat, cstat);
1023 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
1024 16, 1, irb, 64, 1);
1025 return 1;
1028 if (dstat & DEV_STAT_UNIT_CHECK) {
1029 if (sense[SENSE_RESETTING_EVENT_BYTE] &
1030 SENSE_RESETTING_EVENT_FLAG) {
1031 QETH_CARD_TEXT(card, 2, "REVIND");
1032 return 1;
1034 if (sense[SENSE_COMMAND_REJECT_BYTE] &
1035 SENSE_COMMAND_REJECT_FLAG) {
1036 QETH_CARD_TEXT(card, 2, "CMDREJi");
1037 return 1;
1039 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
1040 QETH_CARD_TEXT(card, 2, "AFFE");
1041 return 1;
1043 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
1044 QETH_CARD_TEXT(card, 2, "ZEROSEN");
1045 return 0;
1047 QETH_CARD_TEXT(card, 2, "DGENCHK");
1048 return 1;
1050 return 0;
1053 static long __qeth_check_irb_error(struct ccw_device *cdev,
1054 unsigned long intparm, struct irb *irb)
1056 struct qeth_card *card;
1058 card = CARD_FROM_CDEV(cdev);
1060 if (!card || !IS_ERR(irb))
1061 return 0;
1063 switch (PTR_ERR(irb)) {
1064 case -EIO:
1065 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
1066 dev_name(&cdev->dev));
1067 QETH_CARD_TEXT(card, 2, "ckirberr");
1068 QETH_CARD_TEXT_(card, 2, " rc%d", -EIO);
1069 break;
1070 case -ETIMEDOUT:
1071 dev_warn(&cdev->dev, "A hardware operation timed out"
1072 " on the device\n");
1073 QETH_CARD_TEXT(card, 2, "ckirberr");
1074 QETH_CARD_TEXT_(card, 2, " rc%d", -ETIMEDOUT);
1075 if (intparm == QETH_RCD_PARM) {
1076 if (card->data.ccwdev == cdev) {
1077 card->data.state = CH_STATE_DOWN;
1078 wake_up(&card->wait_q);
1081 break;
1082 default:
1083 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
1084 dev_name(&cdev->dev), PTR_ERR(irb));
1085 QETH_CARD_TEXT(card, 2, "ckirberr");
1086 QETH_CARD_TEXT(card, 2, " rc???");
1088 return PTR_ERR(irb);
1091 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
1092 struct irb *irb)
1094 int rc;
1095 int cstat, dstat;
1096 struct qeth_cmd_buffer *buffer;
1097 struct qeth_channel *channel;
1098 struct qeth_card *card;
1099 struct qeth_cmd_buffer *iob;
1100 __u8 index;
1102 if (__qeth_check_irb_error(cdev, intparm, irb))
1103 return;
1104 cstat = irb->scsw.cmd.cstat;
1105 dstat = irb->scsw.cmd.dstat;
1107 card = CARD_FROM_CDEV(cdev);
1108 if (!card)
1109 return;
1111 QETH_CARD_TEXT(card, 5, "irq");
1113 if (card->read.ccwdev == cdev) {
1114 channel = &card->read;
1115 QETH_CARD_TEXT(card, 5, "read");
1116 } else if (card->write.ccwdev == cdev) {
1117 channel = &card->write;
1118 QETH_CARD_TEXT(card, 5, "write");
1119 } else {
1120 channel = &card->data;
1121 QETH_CARD_TEXT(card, 5, "data");
1123 atomic_set(&channel->irq_pending, 0);
1125 if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1126 channel->state = CH_STATE_STOPPED;
1128 if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1129 channel->state = CH_STATE_HALTED;
1131 /*let's wake up immediately on data channel*/
1132 if ((channel == &card->data) && (intparm != 0) &&
1133 (intparm != QETH_RCD_PARM))
1134 goto out;
1136 if (intparm == QETH_CLEAR_CHANNEL_PARM) {
1137 QETH_CARD_TEXT(card, 6, "clrchpar");
1138 /* we don't have to handle this further */
1139 intparm = 0;
1141 if (intparm == QETH_HALT_CHANNEL_PARM) {
1142 QETH_CARD_TEXT(card, 6, "hltchpar");
1143 /* we don't have to handle this further */
1144 intparm = 0;
1146 if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1147 (dstat & DEV_STAT_UNIT_CHECK) ||
1148 (cstat)) {
1149 if (irb->esw.esw0.erw.cons) {
1150 dev_warn(&channel->ccwdev->dev,
1151 "The qeth device driver failed to recover "
1152 "an error on the device\n");
1153 QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
1154 "0x%X dstat 0x%X\n",
1155 dev_name(&channel->ccwdev->dev), cstat, dstat);
1156 print_hex_dump(KERN_WARNING, "qeth: irb ",
1157 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1158 print_hex_dump(KERN_WARNING, "qeth: sense data ",
1159 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1161 if (intparm == QETH_RCD_PARM) {
1162 channel->state = CH_STATE_DOWN;
1163 goto out;
1165 rc = qeth_get_problem(cdev, irb);
1166 if (rc) {
1167 qeth_clear_ipacmd_list(card);
1168 qeth_schedule_recovery(card);
1169 goto out;
1173 if (intparm == QETH_RCD_PARM) {
1174 channel->state = CH_STATE_RCD_DONE;
1175 goto out;
1177 if (intparm) {
1178 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
1179 buffer->state = BUF_STATE_PROCESSED;
1181 if (channel == &card->data)
1182 return;
1183 if (channel == &card->read &&
1184 channel->state == CH_STATE_UP)
1185 qeth_issue_next_read(card);
1187 iob = channel->iob;
1188 index = channel->buf_no;
1189 while (iob[index].state == BUF_STATE_PROCESSED) {
1190 if (iob[index].callback != NULL)
1191 iob[index].callback(channel, iob + index);
1193 index = (index + 1) % QETH_CMD_BUFFER_NO;
1195 channel->buf_no = index;
1196 out:
1197 wake_up(&card->wait_q);
1198 return;
1201 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1202 struct qeth_qdio_out_buffer *buf,
1203 enum iucv_tx_notify notification)
1205 struct sk_buff *skb;
1207 if (skb_queue_empty(&buf->skb_list))
1208 goto out;
1209 skb = skb_peek(&buf->skb_list);
1210 while (skb) {
1211 QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1212 QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1213 if (be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1214 if (skb->sk) {
1215 struct iucv_sock *iucv = iucv_sk(skb->sk);
1216 iucv->sk_txnotify(skb, notification);
1219 if (skb_queue_is_last(&buf->skb_list, skb))
1220 skb = NULL;
1221 else
1222 skb = skb_queue_next(&buf->skb_list, skb);
1224 out:
1225 return;
1228 static void qeth_release_skbs(struct qeth_qdio_out_buffer *buf)
1230 struct sk_buff *skb;
1231 struct iucv_sock *iucv;
1232 int notify_general_error = 0;
1234 if (atomic_read(&buf->state) == QETH_QDIO_BUF_PENDING)
1235 notify_general_error = 1;
1237 /* release may never happen from within CQ tasklet scope */
1238 WARN_ON_ONCE(atomic_read(&buf->state) == QETH_QDIO_BUF_IN_CQ);
1240 skb = skb_dequeue(&buf->skb_list);
1241 while (skb) {
1242 QETH_CARD_TEXT(buf->q->card, 5, "skbr");
1243 QETH_CARD_TEXT_(buf->q->card, 5, "%lx", (long) skb);
1244 if (notify_general_error &&
1245 be16_to_cpu(skb->protocol) == ETH_P_AF_IUCV) {
1246 if (skb->sk) {
1247 iucv = iucv_sk(skb->sk);
1248 iucv->sk_txnotify(skb, TX_NOTIFY_GENERALERROR);
1251 refcount_dec(&skb->users);
1252 dev_kfree_skb_any(skb);
1253 skb = skb_dequeue(&buf->skb_list);
1257 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1258 struct qeth_qdio_out_buffer *buf,
1259 enum qeth_qdio_buffer_states newbufstate)
1261 int i;
1263 /* is PCI flag set on buffer? */
1264 if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1265 atomic_dec(&queue->set_pci_flags_count);
1267 if (newbufstate == QETH_QDIO_BUF_EMPTY) {
1268 qeth_release_skbs(buf);
1270 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
1271 if (buf->buffer->element[i].addr && buf->is_header[i])
1272 kmem_cache_free(qeth_core_header_cache,
1273 buf->buffer->element[i].addr);
1274 buf->is_header[i] = 0;
1275 buf->buffer->element[i].length = 0;
1276 buf->buffer->element[i].addr = NULL;
1277 buf->buffer->element[i].eflags = 0;
1278 buf->buffer->element[i].sflags = 0;
1280 buf->buffer->element[15].eflags = 0;
1281 buf->buffer->element[15].sflags = 0;
1282 buf->next_element_to_fill = 0;
1283 atomic_set(&buf->state, newbufstate);
1286 static void qeth_clear_outq_buffers(struct qeth_qdio_out_q *q, int free)
1288 int j;
1290 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1291 if (!q->bufs[j])
1292 continue;
1293 qeth_cleanup_handled_pending(q, j, 1);
1294 qeth_clear_output_buffer(q, q->bufs[j], QETH_QDIO_BUF_EMPTY);
1295 if (free) {
1296 kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1297 q->bufs[j] = NULL;
1302 void qeth_clear_qdio_buffers(struct qeth_card *card)
1304 int i;
1306 QETH_CARD_TEXT(card, 2, "clearqdbf");
1307 /* clear outbound buffers to free skbs */
1308 for (i = 0; i < card->qdio.no_out_queues; ++i) {
1309 if (card->qdio.out_qs[i]) {
1310 qeth_clear_outq_buffers(card->qdio.out_qs[i], 0);
1314 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
1316 static void qeth_free_buffer_pool(struct qeth_card *card)
1318 struct qeth_buffer_pool_entry *pool_entry, *tmp;
1319 int i = 0;
1320 list_for_each_entry_safe(pool_entry, tmp,
1321 &card->qdio.init_pool.entry_list, init_list){
1322 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1323 free_page((unsigned long)pool_entry->elements[i]);
1324 list_del(&pool_entry->init_list);
1325 kfree(pool_entry);
1329 static void qeth_clean_channel(struct qeth_channel *channel)
1331 int cnt;
1333 QETH_DBF_TEXT(SETUP, 2, "freech");
1334 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
1335 kfree(channel->iob[cnt].data);
1338 static void qeth_set_single_write_queues(struct qeth_card *card)
1340 if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1341 (card->qdio.no_out_queues == 4))
1342 qeth_free_qdio_buffers(card);
1344 card->qdio.no_out_queues = 1;
1345 if (card->qdio.default_out_queue != 0)
1346 dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1348 card->qdio.default_out_queue = 0;
1351 static void qeth_set_multiple_write_queues(struct qeth_card *card)
1353 if ((atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED) &&
1354 (card->qdio.no_out_queues == 1)) {
1355 qeth_free_qdio_buffers(card);
1356 card->qdio.default_out_queue = 2;
1358 card->qdio.no_out_queues = 4;
1361 static void qeth_update_from_chp_desc(struct qeth_card *card)
1363 struct ccw_device *ccwdev;
1364 struct channel_path_desc *chp_dsc;
1366 QETH_DBF_TEXT(SETUP, 2, "chp_desc");
1368 ccwdev = card->data.ccwdev;
1369 chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1370 if (!chp_dsc)
1371 goto out;
1373 card->info.func_level = 0x4100 + chp_dsc->desc;
1374 if (card->info.type == QETH_CARD_TYPE_IQD)
1375 goto out;
1377 /* CHPP field bit 6 == 1 -> single queue */
1378 if ((chp_dsc->chpp & 0x02) == 0x02)
1379 qeth_set_single_write_queues(card);
1380 else
1381 qeth_set_multiple_write_queues(card);
1382 out:
1383 kfree(chp_dsc);
1384 QETH_DBF_TEXT_(SETUP, 2, "nr:%x", card->qdio.no_out_queues);
1385 QETH_DBF_TEXT_(SETUP, 2, "lvl:%02x", card->info.func_level);
1388 static void qeth_init_qdio_info(struct qeth_card *card)
1390 QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1391 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1392 /* inbound */
1393 card->qdio.no_in_queues = 1;
1394 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1395 if (card->info.type == QETH_CARD_TYPE_IQD)
1396 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1397 else
1398 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1399 card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1400 INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1401 INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1404 static void qeth_set_intial_options(struct qeth_card *card)
1406 card->options.route4.type = NO_ROUTER;
1407 card->options.route6.type = NO_ROUTER;
1408 card->options.fake_broadcast = 0;
1409 card->options.performance_stats = 0;
1410 card->options.rx_sg_cb = QETH_RX_SG_CB;
1411 card->options.isolation = ISOLATION_MODE_NONE;
1412 card->options.cq = QETH_CQ_DISABLED;
1415 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1417 unsigned long flags;
1418 int rc = 0;
1420 spin_lock_irqsave(&card->thread_mask_lock, flags);
1421 QETH_CARD_TEXT_(card, 4, " %02x%02x%02x",
1422 (u8) card->thread_start_mask,
1423 (u8) card->thread_allowed_mask,
1424 (u8) card->thread_running_mask);
1425 rc = (card->thread_start_mask & thread);
1426 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1427 return rc;
1430 static void qeth_start_kernel_thread(struct work_struct *work)
1432 struct task_struct *ts;
1433 struct qeth_card *card = container_of(work, struct qeth_card,
1434 kernel_thread_starter);
1435 QETH_CARD_TEXT(card , 2, "strthrd");
1437 if (card->read.state != CH_STATE_UP &&
1438 card->write.state != CH_STATE_UP)
1439 return;
1440 if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1441 ts = kthread_run(card->discipline->recover, (void *)card,
1442 "qeth_recover");
1443 if (IS_ERR(ts)) {
1444 qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1445 qeth_clear_thread_running_bit(card,
1446 QETH_RECOVER_THREAD);
1451 static void qeth_buffer_reclaim_work(struct work_struct *);
1452 static int qeth_setup_card(struct qeth_card *card)
1455 QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1456 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1458 card->read.state = CH_STATE_DOWN;
1459 card->write.state = CH_STATE_DOWN;
1460 card->data.state = CH_STATE_DOWN;
1461 card->state = CARD_STATE_DOWN;
1462 card->lan_online = 0;
1463 card->read_or_write_problem = 0;
1464 card->dev = NULL;
1465 spin_lock_init(&card->vlanlock);
1466 spin_lock_init(&card->mclock);
1467 spin_lock_init(&card->lock);
1468 spin_lock_init(&card->ip_lock);
1469 spin_lock_init(&card->thread_mask_lock);
1470 mutex_init(&card->conf_mutex);
1471 mutex_init(&card->discipline_mutex);
1472 card->thread_start_mask = 0;
1473 card->thread_allowed_mask = 0;
1474 card->thread_running_mask = 0;
1475 INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1476 INIT_LIST_HEAD(&card->cmd_waiter_list);
1477 init_waitqueue_head(&card->wait_q);
1478 /* initial options */
1479 qeth_set_intial_options(card);
1480 /* IP address takeover */
1481 INIT_LIST_HEAD(&card->ipato.entries);
1482 card->ipato.enabled = false;
1483 card->ipato.invert4 = false;
1484 card->ipato.invert6 = false;
1485 /* init QDIO stuff */
1486 qeth_init_qdio_info(card);
1487 INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1488 INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1489 return 0;
1492 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1494 struct qeth_card *card = container_of(slr, struct qeth_card,
1495 qeth_service_level);
1496 if (card->info.mcl_level[0])
1497 seq_printf(m, "qeth: %s firmware level %s\n",
1498 CARD_BUS_ID(card), card->info.mcl_level);
1501 static struct qeth_card *qeth_alloc_card(void)
1503 struct qeth_card *card;
1505 QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1506 card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1507 if (!card)
1508 goto out;
1509 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1510 if (qeth_setup_channel(&card->read))
1511 goto out_ip;
1512 if (qeth_setup_channel(&card->write))
1513 goto out_channel;
1514 card->options.layer2 = -1;
1515 card->qeth_service_level.seq_print = qeth_core_sl_print;
1516 register_service_level(&card->qeth_service_level);
1517 return card;
1519 out_channel:
1520 qeth_clean_channel(&card->read);
1521 out_ip:
1522 kfree(card);
1523 out:
1524 return NULL;
1527 static void qeth_determine_card_type(struct qeth_card *card)
1529 QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1531 card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1532 card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1533 card->info.type = CARD_RDEV(card)->id.driver_info;
1534 card->qdio.no_out_queues = QETH_MAX_QUEUES;
1535 if (card->info.type == QETH_CARD_TYPE_IQD)
1536 card->info.is_multicast_different = 0x0103;
1537 qeth_update_from_chp_desc(card);
1540 static int qeth_clear_channel(struct qeth_channel *channel)
1542 unsigned long flags;
1543 struct qeth_card *card;
1544 int rc;
1546 card = CARD_FROM_CDEV(channel->ccwdev);
1547 QETH_CARD_TEXT(card, 3, "clearch");
1548 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1549 rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1550 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1552 if (rc)
1553 return rc;
1554 rc = wait_event_interruptible_timeout(card->wait_q,
1555 channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1556 if (rc == -ERESTARTSYS)
1557 return rc;
1558 if (channel->state != CH_STATE_STOPPED)
1559 return -ETIME;
1560 channel->state = CH_STATE_DOWN;
1561 return 0;
1564 static int qeth_halt_channel(struct qeth_channel *channel)
1566 unsigned long flags;
1567 struct qeth_card *card;
1568 int rc;
1570 card = CARD_FROM_CDEV(channel->ccwdev);
1571 QETH_CARD_TEXT(card, 3, "haltch");
1572 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1573 rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1574 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1576 if (rc)
1577 return rc;
1578 rc = wait_event_interruptible_timeout(card->wait_q,
1579 channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1580 if (rc == -ERESTARTSYS)
1581 return rc;
1582 if (channel->state != CH_STATE_HALTED)
1583 return -ETIME;
1584 return 0;
1587 static int qeth_halt_channels(struct qeth_card *card)
1589 int rc1 = 0, rc2 = 0, rc3 = 0;
1591 QETH_CARD_TEXT(card, 3, "haltchs");
1592 rc1 = qeth_halt_channel(&card->read);
1593 rc2 = qeth_halt_channel(&card->write);
1594 rc3 = qeth_halt_channel(&card->data);
1595 if (rc1)
1596 return rc1;
1597 if (rc2)
1598 return rc2;
1599 return rc3;
1602 static int qeth_clear_channels(struct qeth_card *card)
1604 int rc1 = 0, rc2 = 0, rc3 = 0;
1606 QETH_CARD_TEXT(card, 3, "clearchs");
1607 rc1 = qeth_clear_channel(&card->read);
1608 rc2 = qeth_clear_channel(&card->write);
1609 rc3 = qeth_clear_channel(&card->data);
1610 if (rc1)
1611 return rc1;
1612 if (rc2)
1613 return rc2;
1614 return rc3;
1617 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1619 int rc = 0;
1621 QETH_CARD_TEXT(card, 3, "clhacrd");
1623 if (halt)
1624 rc = qeth_halt_channels(card);
1625 if (rc)
1626 return rc;
1627 return qeth_clear_channels(card);
1630 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1632 int rc = 0;
1634 QETH_CARD_TEXT(card, 3, "qdioclr");
1635 switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1636 QETH_QDIO_CLEANING)) {
1637 case QETH_QDIO_ESTABLISHED:
1638 if (card->info.type == QETH_CARD_TYPE_IQD)
1639 rc = qdio_shutdown(CARD_DDEV(card),
1640 QDIO_FLAG_CLEANUP_USING_HALT);
1641 else
1642 rc = qdio_shutdown(CARD_DDEV(card),
1643 QDIO_FLAG_CLEANUP_USING_CLEAR);
1644 if (rc)
1645 QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1646 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1647 break;
1648 case QETH_QDIO_CLEANING:
1649 return rc;
1650 default:
1651 break;
1653 rc = qeth_clear_halt_card(card, use_halt);
1654 if (rc)
1655 QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1656 card->state = CARD_STATE_DOWN;
1657 return rc;
1659 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1661 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1662 int *length)
1664 struct ciw *ciw;
1665 char *rcd_buf;
1666 int ret;
1667 struct qeth_channel *channel = &card->data;
1668 unsigned long flags;
1671 * scan for RCD command in extended SenseID data
1673 ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1674 if (!ciw || ciw->cmd == 0)
1675 return -EOPNOTSUPP;
1676 rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1677 if (!rcd_buf)
1678 return -ENOMEM;
1680 channel->ccw.cmd_code = ciw->cmd;
1681 channel->ccw.cda = (__u32) __pa(rcd_buf);
1682 channel->ccw.count = ciw->count;
1683 channel->ccw.flags = CCW_FLAG_SLI;
1684 channel->state = CH_STATE_RCD;
1685 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1686 ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1687 QETH_RCD_PARM, LPM_ANYPATH, 0,
1688 QETH_RCD_TIMEOUT);
1689 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1690 if (!ret)
1691 wait_event(card->wait_q,
1692 (channel->state == CH_STATE_RCD_DONE ||
1693 channel->state == CH_STATE_DOWN));
1694 if (channel->state == CH_STATE_DOWN)
1695 ret = -EIO;
1696 else
1697 channel->state = CH_STATE_DOWN;
1698 if (ret) {
1699 kfree(rcd_buf);
1700 *buffer = NULL;
1701 *length = 0;
1702 } else {
1703 *length = ciw->count;
1704 *buffer = rcd_buf;
1706 return ret;
1709 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1711 QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1712 card->info.chpid = prcd[30];
1713 card->info.unit_addr2 = prcd[31];
1714 card->info.cula = prcd[63];
1715 card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1716 (prcd[0x11] == _ascebc['M']));
1719 static enum qeth_discipline_id qeth_vm_detect_layer(struct qeth_card *card)
1721 enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1722 struct diag26c_vnic_resp *response = NULL;
1723 struct diag26c_vnic_req *request = NULL;
1724 struct ccw_dev_id id;
1725 char userid[80];
1726 int rc = 0;
1728 QETH_DBF_TEXT(SETUP, 2, "vmlayer");
1730 cpcmd("QUERY USERID", userid, sizeof(userid), &rc);
1731 if (rc)
1732 goto out;
1734 request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
1735 response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
1736 if (!request || !response) {
1737 rc = -ENOMEM;
1738 goto out;
1741 ccw_device_get_id(CARD_RDEV(card), &id);
1742 request->resp_buf_len = sizeof(*response);
1743 request->resp_version = DIAG26C_VERSION6_VM65918;
1744 request->req_format = DIAG26C_VNIC_INFO;
1745 ASCEBC(userid, 8);
1746 memcpy(&request->sys_name, userid, 8);
1747 request->devno = id.devno;
1749 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1750 rc = diag26c(request, response, DIAG26C_PORT_VNIC);
1751 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1752 if (rc)
1753 goto out;
1754 QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
1756 if (request->resp_buf_len < sizeof(*response) ||
1757 response->version != request->resp_version) {
1758 rc = -EIO;
1759 goto out;
1762 if (response->protocol == VNIC_INFO_PROT_L2)
1763 disc = QETH_DISCIPLINE_LAYER2;
1764 else if (response->protocol == VNIC_INFO_PROT_L3)
1765 disc = QETH_DISCIPLINE_LAYER3;
1767 out:
1768 kfree(response);
1769 kfree(request);
1770 if (rc)
1771 QETH_DBF_TEXT_(SETUP, 2, "err%x", rc);
1772 return disc;
1775 /* Determine whether the device requires a specific layer discipline */
1776 static enum qeth_discipline_id qeth_enforce_discipline(struct qeth_card *card)
1778 enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1780 if (card->info.type == QETH_CARD_TYPE_OSM ||
1781 card->info.type == QETH_CARD_TYPE_OSN)
1782 disc = QETH_DISCIPLINE_LAYER2;
1783 else if (card->info.guestlan)
1784 disc = (card->info.type == QETH_CARD_TYPE_IQD) ?
1785 QETH_DISCIPLINE_LAYER3 :
1786 qeth_vm_detect_layer(card);
1788 switch (disc) {
1789 case QETH_DISCIPLINE_LAYER2:
1790 QETH_DBF_TEXT(SETUP, 3, "force l2");
1791 break;
1792 case QETH_DISCIPLINE_LAYER3:
1793 QETH_DBF_TEXT(SETUP, 3, "force l3");
1794 break;
1795 default:
1796 QETH_DBF_TEXT(SETUP, 3, "force no");
1799 return disc;
1802 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1804 QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1806 if (prcd[74] == 0xF0 && prcd[75] == 0xF0 &&
1807 prcd[76] >= 0xF1 && prcd[76] <= 0xF4) {
1808 card->info.blkt.time_total = 0;
1809 card->info.blkt.inter_packet = 0;
1810 card->info.blkt.inter_packet_jumbo = 0;
1811 } else {
1812 card->info.blkt.time_total = 250;
1813 card->info.blkt.inter_packet = 5;
1814 card->info.blkt.inter_packet_jumbo = 15;
1818 static void qeth_init_tokens(struct qeth_card *card)
1820 card->token.issuer_rm_w = 0x00010103UL;
1821 card->token.cm_filter_w = 0x00010108UL;
1822 card->token.cm_connection_w = 0x0001010aUL;
1823 card->token.ulp_filter_w = 0x0001010bUL;
1824 card->token.ulp_connection_w = 0x0001010dUL;
1827 static void qeth_init_func_level(struct qeth_card *card)
1829 switch (card->info.type) {
1830 case QETH_CARD_TYPE_IQD:
1831 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1832 break;
1833 case QETH_CARD_TYPE_OSD:
1834 case QETH_CARD_TYPE_OSN:
1835 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1836 break;
1837 default:
1838 break;
1842 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1843 void (*idx_reply_cb)(struct qeth_channel *,
1844 struct qeth_cmd_buffer *))
1846 struct qeth_cmd_buffer *iob;
1847 unsigned long flags;
1848 int rc;
1849 struct qeth_card *card;
1851 QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1852 card = CARD_FROM_CDEV(channel->ccwdev);
1853 iob = qeth_get_buffer(channel);
1854 if (!iob)
1855 return -ENOMEM;
1856 iob->callback = idx_reply_cb;
1857 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1858 channel->ccw.count = QETH_BUFSIZE;
1859 channel->ccw.cda = (__u32) __pa(iob->data);
1861 wait_event(card->wait_q,
1862 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1863 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1864 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1865 rc = ccw_device_start(channel->ccwdev,
1866 &channel->ccw, (addr_t) iob, 0, 0);
1867 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1869 if (rc) {
1870 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1871 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1872 atomic_set(&channel->irq_pending, 0);
1873 wake_up(&card->wait_q);
1874 return rc;
1876 rc = wait_event_interruptible_timeout(card->wait_q,
1877 channel->state == CH_STATE_UP, QETH_TIMEOUT);
1878 if (rc == -ERESTARTSYS)
1879 return rc;
1880 if (channel->state != CH_STATE_UP) {
1881 rc = -ETIME;
1882 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1883 qeth_clear_cmd_buffers(channel);
1884 } else
1885 rc = 0;
1886 return rc;
1889 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1890 void (*idx_reply_cb)(struct qeth_channel *,
1891 struct qeth_cmd_buffer *))
1893 struct qeth_card *card;
1894 struct qeth_cmd_buffer *iob;
1895 unsigned long flags;
1896 __u16 temp;
1897 __u8 tmp;
1898 int rc;
1899 struct ccw_dev_id temp_devid;
1901 card = CARD_FROM_CDEV(channel->ccwdev);
1903 QETH_DBF_TEXT(SETUP, 2, "idxactch");
1905 iob = qeth_get_buffer(channel);
1906 if (!iob)
1907 return -ENOMEM;
1908 iob->callback = idx_reply_cb;
1909 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1910 channel->ccw.count = IDX_ACTIVATE_SIZE;
1911 channel->ccw.cda = (__u32) __pa(iob->data);
1912 if (channel == &card->write) {
1913 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1914 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1915 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1916 card->seqno.trans_hdr++;
1917 } else {
1918 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1919 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1920 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1922 tmp = ((__u8)card->info.portno) | 0x80;
1923 memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1924 memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1925 &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1926 memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1927 &card->info.func_level, sizeof(__u16));
1928 ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1929 memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1930 temp = (card->info.cula << 8) + card->info.unit_addr2;
1931 memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1933 wait_event(card->wait_q,
1934 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1935 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1936 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1937 rc = ccw_device_start(channel->ccwdev,
1938 &channel->ccw, (addr_t) iob, 0, 0);
1939 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1941 if (rc) {
1942 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1943 rc);
1944 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1945 atomic_set(&channel->irq_pending, 0);
1946 wake_up(&card->wait_q);
1947 return rc;
1949 rc = wait_event_interruptible_timeout(card->wait_q,
1950 channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1951 if (rc == -ERESTARTSYS)
1952 return rc;
1953 if (channel->state != CH_STATE_ACTIVATING) {
1954 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1955 " failed to recover an error on the device\n");
1956 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1957 dev_name(&channel->ccwdev->dev));
1958 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1959 qeth_clear_cmd_buffers(channel);
1960 return -ETIME;
1962 return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1965 static int qeth_peer_func_level(int level)
1967 if ((level & 0xff) == 8)
1968 return (level & 0xff) + 0x400;
1969 if (((level >> 8) & 3) == 1)
1970 return (level & 0xff) + 0x200;
1971 return level;
1974 static void qeth_idx_write_cb(struct qeth_channel *channel,
1975 struct qeth_cmd_buffer *iob)
1977 struct qeth_card *card;
1978 __u16 temp;
1980 QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1982 if (channel->state == CH_STATE_DOWN) {
1983 channel->state = CH_STATE_ACTIVATING;
1984 goto out;
1986 card = CARD_FROM_CDEV(channel->ccwdev);
1988 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1989 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == QETH_IDX_ACT_ERR_EXCL)
1990 dev_err(&card->write.ccwdev->dev,
1991 "The adapter is used exclusively by another "
1992 "host\n");
1993 else
1994 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1995 " negative reply\n",
1996 dev_name(&card->write.ccwdev->dev));
1997 goto out;
1999 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2000 if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
2001 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
2002 "function level mismatch (sent: 0x%x, received: "
2003 "0x%x)\n", dev_name(&card->write.ccwdev->dev),
2004 card->info.func_level, temp);
2005 goto out;
2007 channel->state = CH_STATE_UP;
2008 out:
2009 qeth_release_buffer(channel, iob);
2012 static void qeth_idx_read_cb(struct qeth_channel *channel,
2013 struct qeth_cmd_buffer *iob)
2015 struct qeth_card *card;
2016 __u16 temp;
2018 QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
2019 if (channel->state == CH_STATE_DOWN) {
2020 channel->state = CH_STATE_ACTIVATING;
2021 goto out;
2024 card = CARD_FROM_CDEV(channel->ccwdev);
2025 if (qeth_check_idx_response(card, iob->data))
2026 goto out;
2028 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
2029 switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
2030 case QETH_IDX_ACT_ERR_EXCL:
2031 dev_err(&card->write.ccwdev->dev,
2032 "The adapter is used exclusively by another "
2033 "host\n");
2034 break;
2035 case QETH_IDX_ACT_ERR_AUTH:
2036 case QETH_IDX_ACT_ERR_AUTH_USER:
2037 dev_err(&card->read.ccwdev->dev,
2038 "Setting the device online failed because of "
2039 "insufficient authorization\n");
2040 break;
2041 default:
2042 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
2043 " negative reply\n",
2044 dev_name(&card->read.ccwdev->dev));
2046 QETH_CARD_TEXT_(card, 2, "idxread%c",
2047 QETH_IDX_ACT_CAUSE_CODE(iob->data));
2048 goto out;
2051 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
2052 if (temp != qeth_peer_func_level(card->info.func_level)) {
2053 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
2054 "level mismatch (sent: 0x%x, received: 0x%x)\n",
2055 dev_name(&card->read.ccwdev->dev),
2056 card->info.func_level, temp);
2057 goto out;
2059 memcpy(&card->token.issuer_rm_r,
2060 QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
2061 QETH_MPC_TOKEN_LENGTH);
2062 memcpy(&card->info.mcl_level[0],
2063 QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
2064 channel->state = CH_STATE_UP;
2065 out:
2066 qeth_release_buffer(channel, iob);
2069 void qeth_prepare_control_data(struct qeth_card *card, int len,
2070 struct qeth_cmd_buffer *iob)
2072 qeth_setup_ccw(&card->write, iob->data, len);
2073 iob->callback = qeth_release_buffer;
2075 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
2076 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
2077 card->seqno.trans_hdr++;
2078 memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
2079 &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
2080 card->seqno.pdu_hdr++;
2081 memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
2082 &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
2083 QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
2085 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
2088 * qeth_send_control_data() - send control command to the card
2089 * @card: qeth_card structure pointer
2090 * @len: size of the command buffer
2091 * @iob: qeth_cmd_buffer pointer
2092 * @reply_cb: callback function pointer
2093 * @cb_card: pointer to the qeth_card structure
2094 * @cb_reply: pointer to the qeth_reply structure
2095 * @cb_cmd: pointer to the original iob for non-IPA
2096 * commands, or to the qeth_ipa_cmd structure
2097 * for the IPA commands.
2098 * @reply_param: private pointer passed to the callback
2100 * Returns the value of the `return_code' field of the response
2101 * block returned from the hardware, or other error indication.
2102 * Value of zero indicates successful execution of the command.
2104 * Callback function gets called one or more times, with cb_cmd
2105 * pointing to the response returned by the hardware. Callback
2106 * function must return non-zero if more reply blocks are expected,
2107 * and zero if the last or only reply block is received. Callback
2108 * function can get the value of the reply_param pointer from the
2109 * field 'param' of the structure qeth_reply.
2112 int qeth_send_control_data(struct qeth_card *card, int len,
2113 struct qeth_cmd_buffer *iob,
2114 int (*reply_cb)(struct qeth_card *cb_card,
2115 struct qeth_reply *cb_reply,
2116 unsigned long cb_cmd),
2117 void *reply_param)
2119 int rc;
2120 unsigned long flags;
2121 struct qeth_reply *reply = NULL;
2122 unsigned long timeout, event_timeout;
2123 struct qeth_ipa_cmd *cmd = NULL;
2125 QETH_CARD_TEXT(card, 2, "sendctl");
2127 if (card->read_or_write_problem) {
2128 qeth_release_buffer(iob->channel, iob);
2129 return -EIO;
2131 reply = qeth_alloc_reply(card);
2132 if (!reply) {
2133 return -ENOMEM;
2135 reply->callback = reply_cb;
2136 reply->param = reply_param;
2137 if (card->state == CARD_STATE_DOWN)
2138 reply->seqno = QETH_IDX_COMMAND_SEQNO;
2139 else
2140 reply->seqno = card->seqno.ipa++;
2141 init_waitqueue_head(&reply->wait_q);
2142 spin_lock_irqsave(&card->lock, flags);
2143 list_add_tail(&reply->list, &card->cmd_waiter_list);
2144 spin_unlock_irqrestore(&card->lock, flags);
2146 while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
2147 qeth_prepare_control_data(card, len, iob);
2149 if (IS_IPA(iob->data)) {
2150 cmd = __ipa_cmd(iob);
2151 event_timeout = QETH_IPA_TIMEOUT;
2152 } else {
2153 event_timeout = QETH_TIMEOUT;
2156 timeout = jiffies + event_timeout;
2158 QETH_CARD_TEXT(card, 6, "noirqpnd");
2159 spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
2160 rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
2161 (addr_t) iob, 0, 0);
2162 spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
2163 if (rc) {
2164 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
2165 "ccw_device_start rc = %i\n",
2166 dev_name(&card->write.ccwdev->dev), rc);
2167 QETH_CARD_TEXT_(card, 2, " err%d", rc);
2168 spin_lock_irqsave(&card->lock, flags);
2169 list_del_init(&reply->list);
2170 qeth_put_reply(reply);
2171 spin_unlock_irqrestore(&card->lock, flags);
2172 qeth_release_buffer(iob->channel, iob);
2173 atomic_set(&card->write.irq_pending, 0);
2174 wake_up(&card->wait_q);
2175 return rc;
2178 /* we have only one long running ipassist, since we can ensure
2179 process context of this command we can sleep */
2180 if (cmd && cmd->hdr.command == IPA_CMD_SETIP &&
2181 cmd->hdr.prot_version == QETH_PROT_IPV4) {
2182 if (!wait_event_timeout(reply->wait_q,
2183 atomic_read(&reply->received), event_timeout))
2184 goto time_err;
2185 } else {
2186 while (!atomic_read(&reply->received)) {
2187 if (time_after(jiffies, timeout))
2188 goto time_err;
2189 cpu_relax();
2193 if (reply->rc == -EIO)
2194 goto error;
2195 rc = reply->rc;
2196 qeth_put_reply(reply);
2197 return rc;
2199 time_err:
2200 reply->rc = -ETIME;
2201 spin_lock_irqsave(&reply->card->lock, flags);
2202 list_del_init(&reply->list);
2203 spin_unlock_irqrestore(&reply->card->lock, flags);
2204 atomic_inc(&reply->received);
2205 error:
2206 atomic_set(&card->write.irq_pending, 0);
2207 qeth_release_buffer(iob->channel, iob);
2208 card->write.buf_no = (card->write.buf_no + 1) % QETH_CMD_BUFFER_NO;
2209 rc = reply->rc;
2210 qeth_put_reply(reply);
2211 return rc;
2213 EXPORT_SYMBOL_GPL(qeth_send_control_data);
2215 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2216 unsigned long data)
2218 struct qeth_cmd_buffer *iob;
2220 QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
2222 iob = (struct qeth_cmd_buffer *) data;
2223 memcpy(&card->token.cm_filter_r,
2224 QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2225 QETH_MPC_TOKEN_LENGTH);
2226 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2227 return 0;
2230 static int qeth_cm_enable(struct qeth_card *card)
2232 int rc;
2233 struct qeth_cmd_buffer *iob;
2235 QETH_DBF_TEXT(SETUP, 2, "cmenable");
2237 iob = qeth_wait_for_buffer(&card->write);
2238 memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
2239 memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2240 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2241 memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2242 &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2244 rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
2245 qeth_cm_enable_cb, NULL);
2246 return rc;
2249 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2250 unsigned long data)
2253 struct qeth_cmd_buffer *iob;
2255 QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
2257 iob = (struct qeth_cmd_buffer *) data;
2258 memcpy(&card->token.cm_connection_r,
2259 QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2260 QETH_MPC_TOKEN_LENGTH);
2261 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2262 return 0;
2265 static int qeth_cm_setup(struct qeth_card *card)
2267 int rc;
2268 struct qeth_cmd_buffer *iob;
2270 QETH_DBF_TEXT(SETUP, 2, "cmsetup");
2272 iob = qeth_wait_for_buffer(&card->write);
2273 memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
2274 memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2275 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2276 memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2277 &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2278 memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2279 &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2280 rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
2281 qeth_cm_setup_cb, NULL);
2282 return rc;
2286 static int qeth_get_initial_mtu_for_card(struct qeth_card *card)
2288 switch (card->info.type) {
2289 case QETH_CARD_TYPE_IQD:
2290 return card->info.max_mtu;
2291 case QETH_CARD_TYPE_OSD:
2292 case QETH_CARD_TYPE_OSX:
2293 if (!card->options.layer2)
2294 return ETH_DATA_LEN - 8; /* L3: allow for LLC + SNAP */
2295 /* fall through */
2296 default:
2297 return ETH_DATA_LEN;
2301 static int qeth_get_mtu_outof_framesize(int framesize)
2303 switch (framesize) {
2304 case 0x4000:
2305 return 8192;
2306 case 0x6000:
2307 return 16384;
2308 case 0xa000:
2309 return 32768;
2310 case 0xffff:
2311 return 57344;
2312 default:
2313 return 0;
2317 static int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
2319 switch (card->info.type) {
2320 case QETH_CARD_TYPE_OSD:
2321 case QETH_CARD_TYPE_OSM:
2322 case QETH_CARD_TYPE_OSX:
2323 case QETH_CARD_TYPE_IQD:
2324 return ((mtu >= 576) &&
2325 (mtu <= card->info.max_mtu));
2326 case QETH_CARD_TYPE_OSN:
2327 default:
2328 return 1;
2332 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2333 unsigned long data)
2336 __u16 mtu, framesize;
2337 __u16 len;
2338 __u8 link_type;
2339 struct qeth_cmd_buffer *iob;
2341 QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
2343 iob = (struct qeth_cmd_buffer *) data;
2344 memcpy(&card->token.ulp_filter_r,
2345 QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2346 QETH_MPC_TOKEN_LENGTH);
2347 if (card->info.type == QETH_CARD_TYPE_IQD) {
2348 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2349 mtu = qeth_get_mtu_outof_framesize(framesize);
2350 if (!mtu) {
2351 iob->rc = -EINVAL;
2352 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2353 return 0;
2355 if (card->info.initial_mtu && (card->info.initial_mtu != mtu)) {
2356 /* frame size has changed */
2357 if (card->dev &&
2358 ((card->dev->mtu == card->info.initial_mtu) ||
2359 (card->dev->mtu > mtu)))
2360 card->dev->mtu = mtu;
2361 qeth_free_qdio_buffers(card);
2363 card->info.initial_mtu = mtu;
2364 card->info.max_mtu = mtu;
2365 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
2366 } else {
2367 card->info.max_mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(
2368 iob->data);
2369 card->info.initial_mtu = min(card->info.max_mtu,
2370 qeth_get_initial_mtu_for_card(card));
2371 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
2374 memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2375 if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2376 memcpy(&link_type,
2377 QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2378 card->info.link_type = link_type;
2379 } else
2380 card->info.link_type = 0;
2381 QETH_DBF_TEXT_(SETUP, 2, "link%d", card->info.link_type);
2382 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2383 return 0;
2386 static int qeth_ulp_enable(struct qeth_card *card)
2388 int rc;
2389 char prot_type;
2390 struct qeth_cmd_buffer *iob;
2392 /*FIXME: trace view callbacks*/
2393 QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
2395 iob = qeth_wait_for_buffer(&card->write);
2396 memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
2398 *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
2399 (__u8) card->info.portno;
2400 if (card->options.layer2)
2401 if (card->info.type == QETH_CARD_TYPE_OSN)
2402 prot_type = QETH_PROT_OSN2;
2403 else
2404 prot_type = QETH_PROT_LAYER2;
2405 else
2406 prot_type = QETH_PROT_TCPIP;
2408 memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2409 memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2410 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2411 memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2412 &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2413 rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
2414 qeth_ulp_enable_cb, NULL);
2415 return rc;
2419 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2420 unsigned long data)
2422 struct qeth_cmd_buffer *iob;
2424 QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
2426 iob = (struct qeth_cmd_buffer *) data;
2427 memcpy(&card->token.ulp_connection_r,
2428 QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2429 QETH_MPC_TOKEN_LENGTH);
2430 if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2431 3)) {
2432 QETH_DBF_TEXT(SETUP, 2, "olmlimit");
2433 dev_err(&card->gdev->dev, "A connection could not be "
2434 "established because of an OLM limit\n");
2435 iob->rc = -EMLINK;
2437 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
2438 return 0;
2441 static int qeth_ulp_setup(struct qeth_card *card)
2443 int rc;
2444 __u16 temp;
2445 struct qeth_cmd_buffer *iob;
2446 struct ccw_dev_id dev_id;
2448 QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
2450 iob = qeth_wait_for_buffer(&card->write);
2451 memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2453 memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2454 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2455 memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2456 &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2457 memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2458 &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2460 ccw_device_get_id(CARD_DDEV(card), &dev_id);
2461 memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2462 temp = (card->info.cula << 8) + card->info.unit_addr2;
2463 memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2464 rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2465 qeth_ulp_setup_cb, NULL);
2466 return rc;
2469 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2471 int rc;
2472 struct qeth_qdio_out_buffer *newbuf;
2474 rc = 0;
2475 newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2476 if (!newbuf) {
2477 rc = -ENOMEM;
2478 goto out;
2480 newbuf->buffer = q->qdio_bufs[bidx];
2481 skb_queue_head_init(&newbuf->skb_list);
2482 lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2483 newbuf->q = q;
2484 newbuf->aob = NULL;
2485 newbuf->next_pending = q->bufs[bidx];
2486 atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2487 q->bufs[bidx] = newbuf;
2488 if (q->bufstates) {
2489 q->bufstates[bidx].user = newbuf;
2490 QETH_CARD_TEXT_(q->card, 2, "nbs%d", bidx);
2491 QETH_CARD_TEXT_(q->card, 2, "%lx", (long) newbuf);
2492 QETH_CARD_TEXT_(q->card, 2, "%lx",
2493 (long) newbuf->next_pending);
2495 out:
2496 return rc;
2499 static void qeth_free_qdio_out_buf(struct qeth_qdio_out_q *q)
2501 if (!q)
2502 return;
2504 qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2505 kfree(q);
2508 static struct qeth_qdio_out_q *qeth_alloc_qdio_out_buf(void)
2510 struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2512 if (!q)
2513 return NULL;
2515 if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
2516 kfree(q);
2517 return NULL;
2519 return q;
2522 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2524 int i, j;
2526 QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2528 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2529 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2530 return 0;
2532 QETH_DBF_TEXT(SETUP, 2, "inq");
2533 card->qdio.in_q = qeth_alloc_qdio_queue();
2534 if (!card->qdio.in_q)
2535 goto out_nomem;
2537 /* inbound buffer pool */
2538 if (qeth_alloc_buffer_pool(card))
2539 goto out_freeinq;
2541 /* outbound */
2542 card->qdio.out_qs =
2543 kzalloc(card->qdio.no_out_queues *
2544 sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2545 if (!card->qdio.out_qs)
2546 goto out_freepool;
2547 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2548 card->qdio.out_qs[i] = qeth_alloc_qdio_out_buf();
2549 if (!card->qdio.out_qs[i])
2550 goto out_freeoutq;
2551 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2552 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2553 card->qdio.out_qs[i]->queue_no = i;
2554 /* give outbound qeth_qdio_buffers their qdio_buffers */
2555 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2556 WARN_ON(card->qdio.out_qs[i]->bufs[j] != NULL);
2557 if (qeth_init_qdio_out_buf(card->qdio.out_qs[i], j))
2558 goto out_freeoutqbufs;
2562 /* completion */
2563 if (qeth_alloc_cq(card))
2564 goto out_freeoutq;
2566 return 0;
2568 out_freeoutqbufs:
2569 while (j > 0) {
2570 --j;
2571 kmem_cache_free(qeth_qdio_outbuf_cache,
2572 card->qdio.out_qs[i]->bufs[j]);
2573 card->qdio.out_qs[i]->bufs[j] = NULL;
2575 out_freeoutq:
2576 while (i > 0) {
2577 qeth_free_qdio_out_buf(card->qdio.out_qs[--i]);
2578 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2580 kfree(card->qdio.out_qs);
2581 card->qdio.out_qs = NULL;
2582 out_freepool:
2583 qeth_free_buffer_pool(card);
2584 out_freeinq:
2585 qeth_free_qdio_queue(card->qdio.in_q);
2586 card->qdio.in_q = NULL;
2587 out_nomem:
2588 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2589 return -ENOMEM;
2592 static void qeth_free_qdio_buffers(struct qeth_card *card)
2594 int i, j;
2596 if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2597 QETH_QDIO_UNINITIALIZED)
2598 return;
2600 qeth_free_cq(card);
2601 cancel_delayed_work_sync(&card->buffer_reclaim_work);
2602 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2603 if (card->qdio.in_q->bufs[j].rx_skb)
2604 dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2606 qeth_free_qdio_queue(card->qdio.in_q);
2607 card->qdio.in_q = NULL;
2608 /* inbound buffer pool */
2609 qeth_free_buffer_pool(card);
2610 /* free outbound qdio_qs */
2611 if (card->qdio.out_qs) {
2612 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2613 qeth_clear_outq_buffers(card->qdio.out_qs[i], 1);
2614 qeth_free_qdio_out_buf(card->qdio.out_qs[i]);
2616 kfree(card->qdio.out_qs);
2617 card->qdio.out_qs = NULL;
2621 static void qeth_create_qib_param_field(struct qeth_card *card,
2622 char *param_field)
2625 param_field[0] = _ascebc['P'];
2626 param_field[1] = _ascebc['C'];
2627 param_field[2] = _ascebc['I'];
2628 param_field[3] = _ascebc['T'];
2629 *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2630 *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2631 *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2634 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2635 char *param_field)
2637 param_field[16] = _ascebc['B'];
2638 param_field[17] = _ascebc['L'];
2639 param_field[18] = _ascebc['K'];
2640 param_field[19] = _ascebc['T'];
2641 *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2642 *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2643 *((unsigned int *) (&param_field[28])) =
2644 card->info.blkt.inter_packet_jumbo;
2647 static int qeth_qdio_activate(struct qeth_card *card)
2649 QETH_DBF_TEXT(SETUP, 3, "qdioact");
2650 return qdio_activate(CARD_DDEV(card));
2653 static int qeth_dm_act(struct qeth_card *card)
2655 int rc;
2656 struct qeth_cmd_buffer *iob;
2658 QETH_DBF_TEXT(SETUP, 2, "dmact");
2660 iob = qeth_wait_for_buffer(&card->write);
2661 memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2663 memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2664 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2665 memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2666 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2667 rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2668 return rc;
2671 static int qeth_mpc_initialize(struct qeth_card *card)
2673 int rc;
2675 QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2677 rc = qeth_issue_next_read(card);
2678 if (rc) {
2679 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2680 return rc;
2682 rc = qeth_cm_enable(card);
2683 if (rc) {
2684 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2685 goto out_qdio;
2687 rc = qeth_cm_setup(card);
2688 if (rc) {
2689 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2690 goto out_qdio;
2692 rc = qeth_ulp_enable(card);
2693 if (rc) {
2694 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2695 goto out_qdio;
2697 rc = qeth_ulp_setup(card);
2698 if (rc) {
2699 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2700 goto out_qdio;
2702 rc = qeth_alloc_qdio_buffers(card);
2703 if (rc) {
2704 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2705 goto out_qdio;
2707 rc = qeth_qdio_establish(card);
2708 if (rc) {
2709 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2710 qeth_free_qdio_buffers(card);
2711 goto out_qdio;
2713 rc = qeth_qdio_activate(card);
2714 if (rc) {
2715 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2716 goto out_qdio;
2718 rc = qeth_dm_act(card);
2719 if (rc) {
2720 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2721 goto out_qdio;
2724 return 0;
2725 out_qdio:
2726 qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2727 qdio_free(CARD_DDEV(card));
2728 return rc;
2731 void qeth_print_status_message(struct qeth_card *card)
2733 switch (card->info.type) {
2734 case QETH_CARD_TYPE_OSD:
2735 case QETH_CARD_TYPE_OSM:
2736 case QETH_CARD_TYPE_OSX:
2737 /* VM will use a non-zero first character
2738 * to indicate a HiperSockets like reporting
2739 * of the level OSA sets the first character to zero
2740 * */
2741 if (!card->info.mcl_level[0]) {
2742 sprintf(card->info.mcl_level, "%02x%02x",
2743 card->info.mcl_level[2],
2744 card->info.mcl_level[3]);
2745 break;
2747 /* fallthrough */
2748 case QETH_CARD_TYPE_IQD:
2749 if ((card->info.guestlan) ||
2750 (card->info.mcl_level[0] & 0x80)) {
2751 card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2752 card->info.mcl_level[0]];
2753 card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2754 card->info.mcl_level[1]];
2755 card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2756 card->info.mcl_level[2]];
2757 card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2758 card->info.mcl_level[3]];
2759 card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2761 break;
2762 default:
2763 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2765 dev_info(&card->gdev->dev,
2766 "Device is a%s card%s%s%s\nwith link type %s.\n",
2767 qeth_get_cardname(card),
2768 (card->info.mcl_level[0]) ? " (level: " : "",
2769 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2770 (card->info.mcl_level[0]) ? ")" : "",
2771 qeth_get_cardname_short(card));
2773 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2775 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2777 struct qeth_buffer_pool_entry *entry;
2779 QETH_CARD_TEXT(card, 5, "inwrklst");
2781 list_for_each_entry(entry,
2782 &card->qdio.init_pool.entry_list, init_list) {
2783 qeth_put_buffer_pool_entry(card, entry);
2787 static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2788 struct qeth_card *card)
2790 struct list_head *plh;
2791 struct qeth_buffer_pool_entry *entry;
2792 int i, free;
2793 struct page *page;
2795 if (list_empty(&card->qdio.in_buf_pool.entry_list))
2796 return NULL;
2798 list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2799 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2800 free = 1;
2801 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2802 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2803 free = 0;
2804 break;
2807 if (free) {
2808 list_del_init(&entry->list);
2809 return entry;
2813 /* no free buffer in pool so take first one and swap pages */
2814 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2815 struct qeth_buffer_pool_entry, list);
2816 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2817 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2818 page = alloc_page(GFP_ATOMIC);
2819 if (!page) {
2820 return NULL;
2821 } else {
2822 free_page((unsigned long)entry->elements[i]);
2823 entry->elements[i] = page_address(page);
2824 if (card->options.performance_stats)
2825 card->perf_stats.sg_alloc_page_rx++;
2829 list_del_init(&entry->list);
2830 return entry;
2833 static int qeth_init_input_buffer(struct qeth_card *card,
2834 struct qeth_qdio_buffer *buf)
2836 struct qeth_buffer_pool_entry *pool_entry;
2837 int i;
2839 if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2840 buf->rx_skb = dev_alloc_skb(QETH_RX_PULL_LEN + ETH_HLEN);
2841 if (!buf->rx_skb)
2842 return 1;
2845 pool_entry = qeth_find_free_buffer_pool_entry(card);
2846 if (!pool_entry)
2847 return 1;
2850 * since the buffer is accessed only from the input_tasklet
2851 * there shouldn't be a need to synchronize; also, since we use
2852 * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run out off
2853 * buffers
2856 buf->pool_entry = pool_entry;
2857 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2858 buf->buffer->element[i].length = PAGE_SIZE;
2859 buf->buffer->element[i].addr = pool_entry->elements[i];
2860 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2861 buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2862 else
2863 buf->buffer->element[i].eflags = 0;
2864 buf->buffer->element[i].sflags = 0;
2866 return 0;
2869 int qeth_init_qdio_queues(struct qeth_card *card)
2871 int i, j;
2872 int rc;
2874 QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2876 /* inbound queue */
2877 qdio_reset_buffers(card->qdio.in_q->qdio_bufs,
2878 QDIO_MAX_BUFFERS_PER_Q);
2879 qeth_initialize_working_pool_list(card);
2880 /*give only as many buffers to hardware as we have buffer pool entries*/
2881 for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2882 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2883 card->qdio.in_q->next_buf_to_init =
2884 card->qdio.in_buf_pool.buf_count - 1;
2885 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2886 card->qdio.in_buf_pool.buf_count - 1);
2887 if (rc) {
2888 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2889 return rc;
2892 /* completion */
2893 rc = qeth_cq_init(card);
2894 if (rc) {
2895 return rc;
2898 /* outbound queue */
2899 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2900 qdio_reset_buffers(card->qdio.out_qs[i]->qdio_bufs,
2901 QDIO_MAX_BUFFERS_PER_Q);
2902 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2903 qeth_clear_output_buffer(card->qdio.out_qs[i],
2904 card->qdio.out_qs[i]->bufs[j],
2905 QETH_QDIO_BUF_EMPTY);
2907 card->qdio.out_qs[i]->card = card;
2908 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2909 card->qdio.out_qs[i]->do_pack = 0;
2910 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2911 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2912 atomic_set(&card->qdio.out_qs[i]->state,
2913 QETH_OUT_Q_UNLOCKED);
2915 return 0;
2917 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2919 static __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2921 switch (link_type) {
2922 case QETH_LINK_TYPE_HSTR:
2923 return 2;
2924 default:
2925 return 1;
2929 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2930 struct qeth_ipa_cmd *cmd, __u8 command,
2931 enum qeth_prot_versions prot)
2933 memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2934 cmd->hdr.command = command;
2935 cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2936 cmd->hdr.seqno = card->seqno.ipa;
2937 cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2938 cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2939 if (card->options.layer2)
2940 cmd->hdr.prim_version_no = 2;
2941 else
2942 cmd->hdr.prim_version_no = 1;
2943 cmd->hdr.param_count = 1;
2944 cmd->hdr.prot_version = prot;
2945 cmd->hdr.ipa_supported = 0;
2946 cmd->hdr.ipa_enabled = 0;
2949 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2950 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2952 struct qeth_cmd_buffer *iob;
2953 struct qeth_ipa_cmd *cmd;
2955 iob = qeth_get_buffer(&card->write);
2956 if (iob) {
2957 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2958 qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2959 } else {
2960 dev_warn(&card->gdev->dev,
2961 "The qeth driver ran out of channel command buffers\n");
2962 QETH_DBF_MESSAGE(1, "%s The qeth driver ran out of channel command buffers",
2963 dev_name(&card->gdev->dev));
2966 return iob;
2968 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2970 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2971 char prot_type)
2973 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2974 memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2975 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2976 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2978 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2981 * qeth_send_ipa_cmd() - send an IPA command
2983 * See qeth_send_control_data() for explanation of the arguments.
2986 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2987 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2988 unsigned long),
2989 void *reply_param)
2991 int rc;
2992 char prot_type;
2994 QETH_CARD_TEXT(card, 4, "sendipa");
2996 if (card->options.layer2)
2997 if (card->info.type == QETH_CARD_TYPE_OSN)
2998 prot_type = QETH_PROT_OSN2;
2999 else
3000 prot_type = QETH_PROT_LAYER2;
3001 else
3002 prot_type = QETH_PROT_TCPIP;
3003 qeth_prepare_ipa_cmd(card, iob, prot_type);
3004 rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
3005 iob, reply_cb, reply_param);
3006 if (rc == -ETIME) {
3007 qeth_clear_ipacmd_list(card);
3008 qeth_schedule_recovery(card);
3010 return rc;
3012 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
3014 static int qeth_send_startlan(struct qeth_card *card)
3016 int rc;
3017 struct qeth_cmd_buffer *iob;
3019 QETH_DBF_TEXT(SETUP, 2, "strtlan");
3021 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_STARTLAN, 0);
3022 if (!iob)
3023 return -ENOMEM;
3024 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
3025 return rc;
3028 static int qeth_default_setadapterparms_cb(struct qeth_card *card,
3029 struct qeth_reply *reply, unsigned long data)
3031 struct qeth_ipa_cmd *cmd;
3033 QETH_CARD_TEXT(card, 4, "defadpcb");
3035 cmd = (struct qeth_ipa_cmd *) data;
3036 if (cmd->hdr.return_code == 0)
3037 cmd->hdr.return_code =
3038 cmd->data.setadapterparms.hdr.return_code;
3039 return 0;
3042 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
3043 struct qeth_reply *reply, unsigned long data)
3045 struct qeth_ipa_cmd *cmd;
3047 QETH_CARD_TEXT(card, 3, "quyadpcb");
3049 cmd = (struct qeth_ipa_cmd *) data;
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 qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
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 = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
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;
3150 struct qeth_switch_info *sw_info;
3151 struct qeth_query_switch_attributes *attrs;
3153 QETH_CARD_TEXT(card, 2, "qswiatcb");
3154 cmd = (struct qeth_ipa_cmd *) data;
3155 sw_info = (struct qeth_switch_info *)reply->param;
3156 if (cmd->data.setadapterparms.hdr.return_code == 0) {
3157 attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
3158 sw_info->capabilities = attrs->capabilities;
3159 sw_info->settings = attrs->settings;
3160 QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
3161 sw_info->settings);
3163 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3165 return 0;
3168 int qeth_query_switch_attributes(struct qeth_card *card,
3169 struct qeth_switch_info *sw_info)
3171 struct qeth_cmd_buffer *iob;
3173 QETH_CARD_TEXT(card, 2, "qswiattr");
3174 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
3175 return -EOPNOTSUPP;
3176 if (!netif_carrier_ok(card->dev))
3177 return -ENOMEDIUM;
3178 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES,
3179 sizeof(struct qeth_ipacmd_setadpparms_hdr));
3180 if (!iob)
3181 return -ENOMEM;
3182 return qeth_send_ipa_cmd(card, iob,
3183 qeth_query_switch_attributes_cb, sw_info);
3185 EXPORT_SYMBOL_GPL(qeth_query_switch_attributes);
3187 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3188 struct qeth_reply *reply, unsigned long data)
3190 struct qeth_ipa_cmd *cmd;
3191 __u16 rc;
3193 cmd = (struct qeth_ipa_cmd *)data;
3194 rc = cmd->hdr.return_code;
3195 if (rc)
3196 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3197 else
3198 card->info.diagass_support = cmd->data.diagass.ext;
3199 return 0;
3202 static int qeth_query_setdiagass(struct qeth_card *card)
3204 struct qeth_cmd_buffer *iob;
3205 struct qeth_ipa_cmd *cmd;
3207 QETH_DBF_TEXT(SETUP, 2, "qdiagass");
3208 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3209 if (!iob)
3210 return -ENOMEM;
3211 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3212 cmd->data.diagass.subcmd_len = 16;
3213 cmd->data.diagass.subcmd = QETH_DIAGS_CMD_QUERY;
3214 return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3217 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3219 unsigned long info = get_zeroed_page(GFP_KERNEL);
3220 struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3221 struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3222 struct ccw_dev_id ccwid;
3223 int level;
3225 tid->chpid = card->info.chpid;
3226 ccw_device_get_id(CARD_RDEV(card), &ccwid);
3227 tid->ssid = ccwid.ssid;
3228 tid->devno = ccwid.devno;
3229 if (!info)
3230 return;
3231 level = stsi(NULL, 0, 0, 0);
3232 if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3233 tid->lparnr = info222->lpar_number;
3234 if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3235 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3236 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3238 free_page(info);
3239 return;
3242 static int qeth_hw_trap_cb(struct qeth_card *card,
3243 struct qeth_reply *reply, unsigned long data)
3245 struct qeth_ipa_cmd *cmd;
3246 __u16 rc;
3248 cmd = (struct qeth_ipa_cmd *)data;
3249 rc = cmd->hdr.return_code;
3250 if (rc)
3251 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3252 return 0;
3255 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3257 struct qeth_cmd_buffer *iob;
3258 struct qeth_ipa_cmd *cmd;
3260 QETH_DBF_TEXT(SETUP, 2, "diagtrap");
3261 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
3262 if (!iob)
3263 return -ENOMEM;
3264 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3265 cmd->data.diagass.subcmd_len = 80;
3266 cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRAP;
3267 cmd->data.diagass.type = 1;
3268 cmd->data.diagass.action = action;
3269 switch (action) {
3270 case QETH_DIAGS_TRAP_ARM:
3271 cmd->data.diagass.options = 0x0003;
3272 cmd->data.diagass.ext = 0x00010000 +
3273 sizeof(struct qeth_trap_id);
3274 qeth_get_trap_id(card,
3275 (struct qeth_trap_id *)cmd->data.diagass.cdata);
3276 break;
3277 case QETH_DIAGS_TRAP_DISARM:
3278 cmd->data.diagass.options = 0x0001;
3279 break;
3280 case QETH_DIAGS_TRAP_CAPTURE:
3281 break;
3283 return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3285 EXPORT_SYMBOL_GPL(qeth_hw_trap);
3287 static int qeth_check_qdio_errors(struct qeth_card *card,
3288 struct qdio_buffer *buf,
3289 unsigned int qdio_error,
3290 const char *dbftext)
3292 if (qdio_error) {
3293 QETH_CARD_TEXT(card, 2, dbftext);
3294 QETH_CARD_TEXT_(card, 2, " F15=%02X",
3295 buf->element[15].sflags);
3296 QETH_CARD_TEXT_(card, 2, " F14=%02X",
3297 buf->element[14].sflags);
3298 QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3299 if ((buf->element[15].sflags) == 0x12) {
3300 card->stats.rx_dropped++;
3301 return 0;
3302 } else
3303 return 1;
3305 return 0;
3308 static void qeth_queue_input_buffer(struct qeth_card *card, int index)
3310 struct qeth_qdio_q *queue = card->qdio.in_q;
3311 struct list_head *lh;
3312 int count;
3313 int i;
3314 int rc;
3315 int newcount = 0;
3317 count = (index < queue->next_buf_to_init)?
3318 card->qdio.in_buf_pool.buf_count -
3319 (queue->next_buf_to_init - index) :
3320 card->qdio.in_buf_pool.buf_count -
3321 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
3322 /* only requeue at a certain threshold to avoid SIGAs */
3323 if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3324 for (i = queue->next_buf_to_init;
3325 i < queue->next_buf_to_init + count; ++i) {
3326 if (qeth_init_input_buffer(card,
3327 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
3328 break;
3329 } else {
3330 newcount++;
3334 if (newcount < count) {
3335 /* we are in memory shortage so we switch back to
3336 traditional skb allocation and drop packages */
3337 atomic_set(&card->force_alloc_skb, 3);
3338 count = newcount;
3339 } else {
3340 atomic_add_unless(&card->force_alloc_skb, -1, 0);
3343 if (!count) {
3344 i = 0;
3345 list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3346 i++;
3347 if (i == card->qdio.in_buf_pool.buf_count) {
3348 QETH_CARD_TEXT(card, 2, "qsarbw");
3349 card->reclaim_index = index;
3350 schedule_delayed_work(
3351 &card->buffer_reclaim_work,
3352 QETH_RECLAIM_WORK_TIME);
3354 return;
3358 * according to old code it should be avoided to requeue all
3359 * 128 buffers in order to benefit from PCI avoidance.
3360 * this function keeps at least one buffer (the buffer at
3361 * 'index') un-requeued -> this buffer is the first buffer that
3362 * will be requeued the next time
3364 if (card->options.performance_stats) {
3365 card->perf_stats.inbound_do_qdio_cnt++;
3366 card->perf_stats.inbound_do_qdio_start_time =
3367 qeth_get_micros();
3369 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3370 queue->next_buf_to_init, count);
3371 if (card->options.performance_stats)
3372 card->perf_stats.inbound_do_qdio_time +=
3373 qeth_get_micros() -
3374 card->perf_stats.inbound_do_qdio_start_time;
3375 if (rc) {
3376 QETH_CARD_TEXT(card, 2, "qinberr");
3378 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
3379 QDIO_MAX_BUFFERS_PER_Q;
3383 static void qeth_buffer_reclaim_work(struct work_struct *work)
3385 struct qeth_card *card = container_of(work, struct qeth_card,
3386 buffer_reclaim_work.work);
3388 QETH_CARD_TEXT_(card, 2, "brw:%x", card->reclaim_index);
3389 qeth_queue_input_buffer(card, card->reclaim_index);
3392 static void qeth_handle_send_error(struct qeth_card *card,
3393 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3395 int sbalf15 = buffer->buffer->element[15].sflags;
3397 QETH_CARD_TEXT(card, 6, "hdsnderr");
3398 if (card->info.type == QETH_CARD_TYPE_IQD) {
3399 if (sbalf15 == 0) {
3400 qdio_err = 0;
3401 } else {
3402 qdio_err = 1;
3405 qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3407 if (!qdio_err)
3408 return;
3410 if ((sbalf15 >= 15) && (sbalf15 <= 31))
3411 return;
3413 QETH_CARD_TEXT(card, 1, "lnkfail");
3414 QETH_CARD_TEXT_(card, 1, "%04x %02x",
3415 (u16)qdio_err, (u8)sbalf15);
3419 * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3420 * @queue: queue to check for packing buffer
3422 * Returns number of buffers that were prepared for flush.
3424 static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3426 struct qeth_qdio_out_buffer *buffer;
3428 buffer = queue->bufs[queue->next_buf_to_fill];
3429 if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3430 (buffer->next_element_to_fill > 0)) {
3431 /* it's a packing buffer */
3432 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3433 queue->next_buf_to_fill =
3434 (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
3435 return 1;
3437 return 0;
3441 * Switched to packing state if the number of used buffers on a queue
3442 * reaches a certain limit.
3444 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3446 if (!queue->do_pack) {
3447 if (atomic_read(&queue->used_buffers)
3448 >= QETH_HIGH_WATERMARK_PACK){
3449 /* switch non-PACKING -> PACKING */
3450 QETH_CARD_TEXT(queue->card, 6, "np->pack");
3451 if (queue->card->options.performance_stats)
3452 queue->card->perf_stats.sc_dp_p++;
3453 queue->do_pack = 1;
3459 * Switches from packing to non-packing mode. If there is a packing
3460 * buffer on the queue this buffer will be prepared to be flushed.
3461 * In that case 1 is returned to inform the caller. If no buffer
3462 * has to be flushed, zero is returned.
3464 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3466 if (queue->do_pack) {
3467 if (atomic_read(&queue->used_buffers)
3468 <= QETH_LOW_WATERMARK_PACK) {
3469 /* switch PACKING -> non-PACKING */
3470 QETH_CARD_TEXT(queue->card, 6, "pack->np");
3471 if (queue->card->options.performance_stats)
3472 queue->card->perf_stats.sc_p_dp++;
3473 queue->do_pack = 0;
3474 return qeth_prep_flush_pack_buffer(queue);
3477 return 0;
3480 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3481 int count)
3483 struct qeth_qdio_out_buffer *buf;
3484 int rc;
3485 int i;
3486 unsigned int qdio_flags;
3488 for (i = index; i < index + count; ++i) {
3489 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3490 buf = queue->bufs[bidx];
3491 buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3492 SBAL_EFLAGS_LAST_ENTRY;
3494 if (queue->bufstates)
3495 queue->bufstates[bidx].user = buf;
3497 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
3498 continue;
3500 if (!queue->do_pack) {
3501 if ((atomic_read(&queue->used_buffers) >=
3502 (QETH_HIGH_WATERMARK_PACK -
3503 QETH_WATERMARK_PACK_FUZZ)) &&
3504 !atomic_read(&queue->set_pci_flags_count)) {
3505 /* it's likely that we'll go to packing
3506 * mode soon */
3507 atomic_inc(&queue->set_pci_flags_count);
3508 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3510 } else {
3511 if (!atomic_read(&queue->set_pci_flags_count)) {
3513 * there's no outstanding PCI any more, so we
3514 * have to request a PCI to be sure the the PCI
3515 * will wake at some time in the future then we
3516 * can flush packed buffers that might still be
3517 * hanging around, which can happen if no
3518 * further send was requested by the stack
3520 atomic_inc(&queue->set_pci_flags_count);
3521 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3526 netif_trans_update(queue->card->dev);
3527 if (queue->card->options.performance_stats) {
3528 queue->card->perf_stats.outbound_do_qdio_cnt++;
3529 queue->card->perf_stats.outbound_do_qdio_start_time =
3530 qeth_get_micros();
3532 qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3533 if (atomic_read(&queue->set_pci_flags_count))
3534 qdio_flags |= QDIO_FLAG_PCI_OUT;
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 atomic_add(count, &queue->used_buffers);
3542 if (rc) {
3543 queue->card->stats.tx_errors += count;
3544 /* ignore temporary SIGA errors without busy condition */
3545 if (rc == -ENOBUFS)
3546 return;
3547 QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3548 QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3549 QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3550 QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3551 QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3553 /* this must not happen under normal circumstances. if it
3554 * happens something is really wrong -> recover */
3555 qeth_schedule_recovery(queue->card);
3556 return;
3558 if (queue->card->options.performance_stats)
3559 queue->card->perf_stats.bufs_sent += count;
3562 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3564 int index;
3565 int flush_cnt = 0;
3566 int q_was_packing = 0;
3569 * check if weed have to switch to non-packing mode or if
3570 * we have to get a pci flag out on the queue
3572 if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3573 !atomic_read(&queue->set_pci_flags_count)) {
3574 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
3575 QETH_OUT_Q_UNLOCKED) {
3577 * If we get in here, there was no action in
3578 * do_send_packet. So, we check if there is a
3579 * packing buffer to be flushed here.
3581 netif_stop_queue(queue->card->dev);
3582 index = queue->next_buf_to_fill;
3583 q_was_packing = queue->do_pack;
3584 /* queue->do_pack may change */
3585 barrier();
3586 flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3587 if (!flush_cnt &&
3588 !atomic_read(&queue->set_pci_flags_count))
3589 flush_cnt += qeth_prep_flush_pack_buffer(queue);
3590 if (queue->card->options.performance_stats &&
3591 q_was_packing)
3592 queue->card->perf_stats.bufs_sent_pack +=
3593 flush_cnt;
3594 if (flush_cnt)
3595 qeth_flush_buffers(queue, index, flush_cnt);
3596 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3601 void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3602 unsigned long card_ptr)
3604 struct qeth_card *card = (struct qeth_card *)card_ptr;
3606 if (card->dev && (card->dev->flags & IFF_UP))
3607 napi_schedule(&card->napi);
3609 EXPORT_SYMBOL_GPL(qeth_qdio_start_poll);
3611 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3613 int rc;
3615 if (card->options.cq == QETH_CQ_NOTAVAILABLE) {
3616 rc = -1;
3617 goto out;
3618 } else {
3619 if (card->options.cq == cq) {
3620 rc = 0;
3621 goto out;
3624 if (card->state != CARD_STATE_DOWN &&
3625 card->state != CARD_STATE_RECOVER) {
3626 rc = -1;
3627 goto out;
3630 qeth_free_qdio_buffers(card);
3631 card->options.cq = cq;
3632 rc = 0;
3634 out:
3635 return rc;
3638 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3641 static void qeth_qdio_cq_handler(struct qeth_card *card,
3642 unsigned int qdio_err,
3643 unsigned int queue, int first_element, int count) {
3644 struct qeth_qdio_q *cq = card->qdio.c_q;
3645 int i;
3646 int rc;
3648 if (!qeth_is_cq(card, queue))
3649 goto out;
3651 QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3652 QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3653 QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3655 if (qdio_err) {
3656 netif_stop_queue(card->dev);
3657 qeth_schedule_recovery(card);
3658 goto out;
3661 if (card->options.performance_stats) {
3662 card->perf_stats.cq_cnt++;
3663 card->perf_stats.cq_start_time = qeth_get_micros();
3666 for (i = first_element; i < first_element + count; ++i) {
3667 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3668 struct qdio_buffer *buffer = cq->qdio_bufs[bidx];
3669 int e;
3671 e = 0;
3672 while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3673 buffer->element[e].addr) {
3674 unsigned long phys_aob_addr;
3676 phys_aob_addr = (unsigned long) buffer->element[e].addr;
3677 qeth_qdio_handle_aob(card, phys_aob_addr);
3678 buffer->element[e].addr = NULL;
3679 buffer->element[e].eflags = 0;
3680 buffer->element[e].sflags = 0;
3681 buffer->element[e].length = 0;
3683 ++e;
3686 buffer->element[15].eflags = 0;
3687 buffer->element[15].sflags = 0;
3689 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3690 card->qdio.c_q->next_buf_to_init,
3691 count);
3692 if (rc) {
3693 dev_warn(&card->gdev->dev,
3694 "QDIO reported an error, rc=%i\n", rc);
3695 QETH_CARD_TEXT(card, 2, "qcqherr");
3697 card->qdio.c_q->next_buf_to_init = (card->qdio.c_q->next_buf_to_init
3698 + count) % QDIO_MAX_BUFFERS_PER_Q;
3700 netif_wake_queue(card->dev);
3702 if (card->options.performance_stats) {
3703 int delta_t = qeth_get_micros();
3704 delta_t -= card->perf_stats.cq_start_time;
3705 card->perf_stats.cq_time += delta_t;
3707 out:
3708 return;
3711 void qeth_qdio_input_handler(struct ccw_device *ccwdev, unsigned int qdio_err,
3712 unsigned int queue, int first_elem, int count,
3713 unsigned long card_ptr)
3715 struct qeth_card *card = (struct qeth_card *)card_ptr;
3717 QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3718 QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3720 if (qeth_is_cq(card, queue))
3721 qeth_qdio_cq_handler(card, qdio_err, queue, first_elem, count);
3722 else if (qdio_err)
3723 qeth_schedule_recovery(card);
3727 EXPORT_SYMBOL_GPL(qeth_qdio_input_handler);
3729 void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3730 unsigned int qdio_error, int __queue, int first_element,
3731 int count, unsigned long card_ptr)
3733 struct qeth_card *card = (struct qeth_card *) card_ptr;
3734 struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3735 struct qeth_qdio_out_buffer *buffer;
3736 int i;
3738 QETH_CARD_TEXT(card, 6, "qdouhdl");
3739 if (qdio_error & QDIO_ERROR_FATAL) {
3740 QETH_CARD_TEXT(card, 2, "achkcond");
3741 netif_stop_queue(card->dev);
3742 qeth_schedule_recovery(card);
3743 return;
3745 if (card->options.performance_stats) {
3746 card->perf_stats.outbound_handler_cnt++;
3747 card->perf_stats.outbound_handler_start_time =
3748 qeth_get_micros();
3750 for (i = first_element; i < (first_element + count); ++i) {
3751 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3752 buffer = queue->bufs[bidx];
3753 qeth_handle_send_error(card, buffer, qdio_error);
3755 if (queue->bufstates &&
3756 (queue->bufstates[bidx].flags &
3757 QDIO_OUTBUF_STATE_FLAG_PENDING) != 0) {
3758 WARN_ON_ONCE(card->options.cq != QETH_CQ_ENABLED);
3760 if (atomic_cmpxchg(&buffer->state,
3761 QETH_QDIO_BUF_PRIMED,
3762 QETH_QDIO_BUF_PENDING) ==
3763 QETH_QDIO_BUF_PRIMED) {
3764 qeth_notify_skbs(queue, buffer,
3765 TX_NOTIFY_PENDING);
3767 buffer->aob = queue->bufstates[bidx].aob;
3768 QETH_CARD_TEXT_(queue->card, 5, "pel%d", bidx);
3769 QETH_CARD_TEXT(queue->card, 5, "aob");
3770 QETH_CARD_TEXT_(queue->card, 5, "%lx",
3771 virt_to_phys(buffer->aob));
3772 if (qeth_init_qdio_out_buf(queue, bidx)) {
3773 QETH_CARD_TEXT(card, 2, "outofbuf");
3774 qeth_schedule_recovery(card);
3776 } else {
3777 if (card->options.cq == QETH_CQ_ENABLED) {
3778 enum iucv_tx_notify n;
3780 n = qeth_compute_cq_notification(
3781 buffer->buffer->element[15].sflags, 0);
3782 qeth_notify_skbs(queue, buffer, n);
3785 qeth_clear_output_buffer(queue, buffer,
3786 QETH_QDIO_BUF_EMPTY);
3788 qeth_cleanup_handled_pending(queue, bidx, 0);
3790 atomic_sub(count, &queue->used_buffers);
3791 /* check if we need to do something on this outbound queue */
3792 if (card->info.type != QETH_CARD_TYPE_IQD)
3793 qeth_check_outbound_queue(queue);
3795 netif_wake_queue(queue->card->dev);
3796 if (card->options.performance_stats)
3797 card->perf_stats.outbound_handler_time += qeth_get_micros() -
3798 card->perf_stats.outbound_handler_start_time;
3800 EXPORT_SYMBOL_GPL(qeth_qdio_output_handler);
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 int elements = qeth_get_elements_for_range(
3902 (addr_t)skb->data + data_offset,
3903 (addr_t)skb->data + skb_headlen(skb)) +
3904 qeth_get_elements_for_frags(skb);
3906 if ((elements + extra_elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
3907 QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
3908 "(Number=%d / Length=%d). Discarded.\n",
3909 elements + extra_elems, skb->len);
3910 return 0;
3912 return elements;
3914 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
3916 int qeth_hdr_chk_and_bounce(struct sk_buff *skb, struct qeth_hdr **hdr, int len)
3918 int hroom, inpage, rest;
3920 if (((unsigned long)skb->data & PAGE_MASK) !=
3921 (((unsigned long)skb->data + len - 1) & PAGE_MASK)) {
3922 hroom = skb_headroom(skb);
3923 inpage = PAGE_SIZE - ((unsigned long) skb->data % PAGE_SIZE);
3924 rest = len - inpage;
3925 if (rest > hroom)
3926 return 1;
3927 memmove(skb->data - rest, skb->data, skb_headlen(skb));
3928 skb->data -= rest;
3929 skb->tail -= rest;
3930 *hdr = (struct qeth_hdr *)skb->data;
3931 QETH_DBF_MESSAGE(2, "skb bounce len: %d rest: %d\n", len, rest);
3933 return 0;
3935 EXPORT_SYMBOL_GPL(qeth_hdr_chk_and_bounce);
3938 * qeth_push_hdr() - push a qeth_hdr onto an skb.
3939 * @skb: skb that the qeth_hdr should be pushed onto.
3940 * @hdr: double pointer to a qeth_hdr. When returning with >= 0,
3941 * it contains a valid pointer to a qeth_hdr.
3942 * @len: length of the hdr that needs to be pushed on.
3944 * Returns the pushed length. If the header can't be pushed on
3945 * (eg. because it would cross a page boundary), it is allocated from
3946 * the cache instead and 0 is returned.
3947 * Error to create the hdr is indicated by returning with < 0.
3949 int qeth_push_hdr(struct sk_buff *skb, struct qeth_hdr **hdr, unsigned int len)
3951 if (skb_headroom(skb) >= len &&
3952 qeth_get_elements_for_range((addr_t)skb->data - len,
3953 (addr_t)skb->data) == 1) {
3954 *hdr = skb_push(skb, len);
3955 return len;
3957 /* fall back */
3958 *hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
3959 if (!*hdr)
3960 return -ENOMEM;
3961 return 0;
3963 EXPORT_SYMBOL_GPL(qeth_push_hdr);
3965 static void __qeth_fill_buffer(struct sk_buff *skb,
3966 struct qeth_qdio_out_buffer *buf,
3967 bool is_first_elem, unsigned int offset)
3969 struct qdio_buffer *buffer = buf->buffer;
3970 int element = buf->next_element_to_fill;
3971 int length = skb_headlen(skb) - offset;
3972 char *data = skb->data + offset;
3973 int length_here, cnt;
3975 /* map linear part into buffer element(s) */
3976 while (length > 0) {
3977 /* length_here is the remaining amount of data in this page */
3978 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3979 if (length < length_here)
3980 length_here = length;
3982 buffer->element[element].addr = data;
3983 buffer->element[element].length = length_here;
3984 length -= length_here;
3985 if (is_first_elem) {
3986 is_first_elem = false;
3987 if (length || skb_is_nonlinear(skb))
3988 /* skb needs additional elements */
3989 buffer->element[element].eflags =
3990 SBAL_EFLAGS_FIRST_FRAG;
3991 else
3992 buffer->element[element].eflags = 0;
3993 } else {
3994 buffer->element[element].eflags =
3995 SBAL_EFLAGS_MIDDLE_FRAG;
3997 data += length_here;
3998 element++;
4001 /* map page frags into buffer element(s) */
4002 for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
4003 skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
4005 data = skb_frag_address(frag);
4006 length = skb_frag_size(frag);
4007 while (length > 0) {
4008 length_here = PAGE_SIZE -
4009 ((unsigned long) data % PAGE_SIZE);
4010 if (length < length_here)
4011 length_here = length;
4013 buffer->element[element].addr = data;
4014 buffer->element[element].length = length_here;
4015 buffer->element[element].eflags =
4016 SBAL_EFLAGS_MIDDLE_FRAG;
4017 length -= length_here;
4018 data += length_here;
4019 element++;
4023 if (buffer->element[element - 1].eflags)
4024 buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
4025 buf->next_element_to_fill = element;
4029 * qeth_fill_buffer() - map skb into an output buffer
4030 * @queue: QDIO queue to submit the buffer on
4031 * @buf: buffer to transport the skb
4032 * @skb: skb to map into the buffer
4033 * @hdr: qeth_hdr for this skb. Either at skb->data, or allocated
4034 * from qeth_core_header_cache.
4035 * @offset: when mapping the skb, start at skb->data + offset
4036 * @hd_len: if > 0, build a dedicated header element of this size
4038 static int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
4039 struct qeth_qdio_out_buffer *buf,
4040 struct sk_buff *skb, struct qeth_hdr *hdr,
4041 unsigned int offset, unsigned int hd_len)
4043 struct qdio_buffer *buffer = buf->buffer;
4044 bool is_first_elem = true;
4045 int flush_cnt = 0;
4047 refcount_inc(&skb->users);
4048 skb_queue_tail(&buf->skb_list, skb);
4050 /* build dedicated header element */
4051 if (hd_len) {
4052 int element = buf->next_element_to_fill;
4053 is_first_elem = false;
4055 buffer->element[element].addr = hdr;
4056 buffer->element[element].length = hd_len;
4057 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
4058 /* remember to free cache-allocated qeth_hdr: */
4059 buf->is_header[element] = ((void *)hdr != skb->data);
4060 buf->next_element_to_fill++;
4063 __qeth_fill_buffer(skb, buf, is_first_elem, offset);
4065 if (!queue->do_pack) {
4066 QETH_CARD_TEXT(queue->card, 6, "fillbfnp");
4067 /* set state to PRIMED -> will be flushed */
4068 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4069 flush_cnt = 1;
4070 } else {
4071 QETH_CARD_TEXT(queue->card, 6, "fillbfpa");
4072 if (queue->card->options.performance_stats)
4073 queue->card->perf_stats.skbs_sent_pack++;
4074 if (buf->next_element_to_fill >=
4075 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
4077 * packed buffer if full -> set state PRIMED
4078 * -> will be flushed
4080 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
4081 flush_cnt = 1;
4084 return flush_cnt;
4087 int qeth_do_send_packet_fast(struct qeth_qdio_out_q *queue, struct sk_buff *skb,
4088 struct qeth_hdr *hdr, unsigned int offset,
4089 unsigned int hd_len)
4091 int index = queue->next_buf_to_fill;
4092 struct qeth_qdio_out_buffer *buffer = queue->bufs[index];
4095 * check if buffer is empty to make sure that we do not 'overtake'
4096 * ourselves and try to fill a buffer that is already primed
4098 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
4099 return -EBUSY;
4100 queue->next_buf_to_fill = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
4101 qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4102 qeth_flush_buffers(queue, index, 1);
4103 return 0;
4105 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
4107 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
4108 struct sk_buff *skb, struct qeth_hdr *hdr,
4109 unsigned int offset, unsigned int hd_len,
4110 int elements_needed)
4112 struct qeth_qdio_out_buffer *buffer;
4113 int start_index;
4114 int flush_count = 0;
4115 int do_pack = 0;
4116 int tmp;
4117 int rc = 0;
4119 /* spin until we get the queue ... */
4120 while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
4121 QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
4122 start_index = queue->next_buf_to_fill;
4123 buffer = queue->bufs[queue->next_buf_to_fill];
4125 * check if buffer is empty to make sure that we do not 'overtake'
4126 * ourselves and try to fill a buffer that is already primed
4128 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
4129 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
4130 return -EBUSY;
4132 /* check if we need to switch packing state of this queue */
4133 qeth_switch_to_packing_if_needed(queue);
4134 if (queue->do_pack) {
4135 do_pack = 1;
4136 /* does packet fit in current buffer? */
4137 if ((QETH_MAX_BUFFER_ELEMENTS(card) -
4138 buffer->next_element_to_fill) < elements_needed) {
4139 /* ... no -> set state PRIMED */
4140 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
4141 flush_count++;
4142 queue->next_buf_to_fill =
4143 (queue->next_buf_to_fill + 1) %
4144 QDIO_MAX_BUFFERS_PER_Q;
4145 buffer = queue->bufs[queue->next_buf_to_fill];
4146 /* we did a step forward, so check buffer state
4147 * again */
4148 if (atomic_read(&buffer->state) !=
4149 QETH_QDIO_BUF_EMPTY) {
4150 qeth_flush_buffers(queue, start_index,
4151 flush_count);
4152 atomic_set(&queue->state,
4153 QETH_OUT_Q_UNLOCKED);
4154 rc = -EBUSY;
4155 goto out;
4159 tmp = qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
4160 queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
4161 QDIO_MAX_BUFFERS_PER_Q;
4162 flush_count += tmp;
4163 if (flush_count)
4164 qeth_flush_buffers(queue, start_index, flush_count);
4165 else if (!atomic_read(&queue->set_pci_flags_count))
4166 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
4168 * queue->state will go from LOCKED -> UNLOCKED or from
4169 * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
4170 * (switch packing state or flush buffer to get another pci flag out).
4171 * In that case we will enter this loop
4173 while (atomic_dec_return(&queue->state)) {
4174 start_index = queue->next_buf_to_fill;
4175 /* check if we can go back to non-packing state */
4176 tmp = qeth_switch_to_nonpacking_if_needed(queue);
4178 * check if we need to flush a packing buffer to get a pci
4179 * flag out on the queue
4181 if (!tmp && !atomic_read(&queue->set_pci_flags_count))
4182 tmp = qeth_prep_flush_pack_buffer(queue);
4183 if (tmp) {
4184 qeth_flush_buffers(queue, start_index, tmp);
4185 flush_count += tmp;
4188 out:
4189 /* at this point the queue is UNLOCKED again */
4190 if (queue->card->options.performance_stats && do_pack)
4191 queue->card->perf_stats.bufs_sent_pack += flush_count;
4193 return rc;
4195 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
4197 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4198 struct qeth_reply *reply, unsigned long data)
4200 struct qeth_ipa_cmd *cmd;
4201 struct qeth_ipacmd_setadpparms *setparms;
4203 QETH_CARD_TEXT(card, 4, "prmadpcb");
4205 cmd = (struct qeth_ipa_cmd *) data;
4206 setparms = &(cmd->data.setadapterparms);
4208 qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
4209 if (cmd->hdr.return_code) {
4210 QETH_CARD_TEXT_(card, 4, "prmrc%x", cmd->hdr.return_code);
4211 setparms->data.mode = SET_PROMISC_MODE_OFF;
4213 card->info.promisc_mode = setparms->data.mode;
4214 return 0;
4217 void qeth_setadp_promisc_mode(struct qeth_card *card)
4219 enum qeth_ipa_promisc_modes mode;
4220 struct net_device *dev = card->dev;
4221 struct qeth_cmd_buffer *iob;
4222 struct qeth_ipa_cmd *cmd;
4224 QETH_CARD_TEXT(card, 4, "setprom");
4226 if (((dev->flags & IFF_PROMISC) &&
4227 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
4228 (!(dev->flags & IFF_PROMISC) &&
4229 (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
4230 return;
4231 mode = SET_PROMISC_MODE_OFF;
4232 if (dev->flags & IFF_PROMISC)
4233 mode = SET_PROMISC_MODE_ON;
4234 QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4236 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4237 sizeof(struct qeth_ipacmd_setadpparms_hdr) + 8);
4238 if (!iob)
4239 return;
4240 cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
4241 cmd->data.setadapterparms.data.mode = mode;
4242 qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4244 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4246 int qeth_change_mtu(struct net_device *dev, int new_mtu)
4248 struct qeth_card *card;
4249 char dbf_text[15];
4251 card = dev->ml_priv;
4253 QETH_CARD_TEXT(card, 4, "chgmtu");
4254 sprintf(dbf_text, "%8x", new_mtu);
4255 QETH_CARD_TEXT(card, 4, dbf_text);
4257 if (!qeth_mtu_is_valid(card, new_mtu))
4258 return -EINVAL;
4259 dev->mtu = new_mtu;
4260 return 0;
4262 EXPORT_SYMBOL_GPL(qeth_change_mtu);
4264 struct net_device_stats *qeth_get_stats(struct net_device *dev)
4266 struct qeth_card *card;
4268 card = dev->ml_priv;
4270 QETH_CARD_TEXT(card, 5, "getstat");
4272 return &card->stats;
4274 EXPORT_SYMBOL_GPL(qeth_get_stats);
4276 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4277 struct qeth_reply *reply, unsigned long data)
4279 struct qeth_ipa_cmd *cmd;
4281 QETH_CARD_TEXT(card, 4, "chgmaccb");
4283 cmd = (struct qeth_ipa_cmd *) data;
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 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4291 return 0;
4294 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4296 int rc;
4297 struct qeth_cmd_buffer *iob;
4298 struct qeth_ipa_cmd *cmd;
4300 QETH_CARD_TEXT(card, 4, "chgmac");
4302 iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4303 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4304 sizeof(struct qeth_change_addr));
4305 if (!iob)
4306 return -ENOMEM;
4307 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4308 cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4309 cmd->data.setadapterparms.data.change_addr.addr_size = ETH_ALEN;
4310 ether_addr_copy(cmd->data.setadapterparms.data.change_addr.addr,
4311 card->dev->dev_addr);
4312 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4313 NULL);
4314 return rc;
4316 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4318 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4319 struct qeth_reply *reply, unsigned long data)
4321 struct qeth_ipa_cmd *cmd;
4322 struct qeth_set_access_ctrl *access_ctrl_req;
4323 int fallback = *(int *)reply->param;
4325 QETH_CARD_TEXT(card, 4, "setaccb");
4327 cmd = (struct qeth_ipa_cmd *) data;
4328 access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4329 QETH_DBF_TEXT_(SETUP, 2, "setaccb");
4330 QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
4331 QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
4332 cmd->data.setadapterparms.hdr.return_code);
4333 if (cmd->data.setadapterparms.hdr.return_code !=
4334 SET_ACCESS_CTRL_RC_SUCCESS)
4335 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
4336 card->gdev->dev.kobj.name,
4337 access_ctrl_req->subcmd_code,
4338 cmd->data.setadapterparms.hdr.return_code);
4339 switch (cmd->data.setadapterparms.hdr.return_code) {
4340 case SET_ACCESS_CTRL_RC_SUCCESS:
4341 if (card->options.isolation == ISOLATION_MODE_NONE) {
4342 dev_info(&card->gdev->dev,
4343 "QDIO data connection isolation is deactivated\n");
4344 } else {
4345 dev_info(&card->gdev->dev,
4346 "QDIO data connection isolation is activated\n");
4348 break;
4349 case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4350 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already "
4351 "deactivated\n", dev_name(&card->gdev->dev));
4352 if (fallback)
4353 card->options.isolation = card->options.prev_isolation;
4354 break;
4355 case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4356 QETH_DBF_MESSAGE(2, "%s QDIO data connection isolation already"
4357 " activated\n", dev_name(&card->gdev->dev));
4358 if (fallback)
4359 card->options.isolation = card->options.prev_isolation;
4360 break;
4361 case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4362 dev_err(&card->gdev->dev, "Adapter does not "
4363 "support QDIO data connection isolation\n");
4364 break;
4365 case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4366 dev_err(&card->gdev->dev,
4367 "Adapter is dedicated. "
4368 "QDIO data connection isolation not supported\n");
4369 if (fallback)
4370 card->options.isolation = card->options.prev_isolation;
4371 break;
4372 case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4373 dev_err(&card->gdev->dev,
4374 "TSO does not permit QDIO data connection isolation\n");
4375 if (fallback)
4376 card->options.isolation = card->options.prev_isolation;
4377 break;
4378 case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4379 dev_err(&card->gdev->dev, "The adjacent switch port does not "
4380 "support reflective relay mode\n");
4381 if (fallback)
4382 card->options.isolation = card->options.prev_isolation;
4383 break;
4384 case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4385 dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4386 "enabled at the adjacent switch port");
4387 if (fallback)
4388 card->options.isolation = card->options.prev_isolation;
4389 break;
4390 case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4391 dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4392 "at the adjacent switch failed\n");
4393 break;
4394 default:
4395 /* this should never happen */
4396 if (fallback)
4397 card->options.isolation = card->options.prev_isolation;
4398 break;
4400 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
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 = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
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 = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
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;
4689 struct qeth_qoat_priv *priv;
4690 char *resdata;
4691 int resdatalen;
4693 QETH_CARD_TEXT(card, 3, "qoatcb");
4695 cmd = (struct qeth_ipa_cmd *)data;
4696 priv = (struct qeth_qoat_priv *)reply->param;
4697 resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4698 resdata = (char *)data + 28;
4700 if (resdatalen > (priv->buffer_len - priv->response_len)) {
4701 cmd->hdr.return_code = IPA_RC_FFFF;
4702 return 0;
4705 memcpy((priv->buffer + priv->response_len), resdata,
4706 resdatalen);
4707 priv->response_len += resdatalen;
4709 if (cmd->data.setadapterparms.hdr.seq_no <
4710 cmd->data.setadapterparms.hdr.used_total)
4711 return 1;
4712 return 0;
4715 static int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4717 int rc = 0;
4718 struct qeth_cmd_buffer *iob;
4719 struct qeth_ipa_cmd *cmd;
4720 struct qeth_query_oat *oat_req;
4721 struct qeth_query_oat_data oat_data;
4722 struct qeth_qoat_priv priv;
4723 void __user *tmp;
4725 QETH_CARD_TEXT(card, 3, "qoatcmd");
4727 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4728 rc = -EOPNOTSUPP;
4729 goto out;
4732 if (copy_from_user(&oat_data, udata,
4733 sizeof(struct qeth_query_oat_data))) {
4734 rc = -EFAULT;
4735 goto out;
4738 priv.buffer_len = oat_data.buffer_len;
4739 priv.response_len = 0;
4740 priv.buffer = kzalloc(oat_data.buffer_len, GFP_KERNEL);
4741 if (!priv.buffer) {
4742 rc = -ENOMEM;
4743 goto out;
4746 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4747 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
4748 sizeof(struct qeth_query_oat));
4749 if (!iob) {
4750 rc = -ENOMEM;
4751 goto out_free;
4753 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
4754 oat_req = &cmd->data.setadapterparms.data.query_oat;
4755 oat_req->subcmd_code = oat_data.command;
4757 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb,
4758 &priv);
4759 if (!rc) {
4760 if (is_compat_task())
4761 tmp = compat_ptr(oat_data.ptr);
4762 else
4763 tmp = (void __user *)(unsigned long)oat_data.ptr;
4765 if (copy_to_user(tmp, priv.buffer,
4766 priv.response_len)) {
4767 rc = -EFAULT;
4768 goto out_free;
4771 oat_data.response_len = priv.response_len;
4773 if (copy_to_user(udata, &oat_data,
4774 sizeof(struct qeth_query_oat_data)))
4775 rc = -EFAULT;
4776 } else
4777 if (rc == IPA_RC_FFFF)
4778 rc = -EFAULT;
4780 out_free:
4781 kfree(priv.buffer);
4782 out:
4783 return rc;
4786 static int qeth_query_card_info_cb(struct qeth_card *card,
4787 struct qeth_reply *reply, unsigned long data)
4789 struct qeth_ipa_cmd *cmd;
4790 struct qeth_query_card_info *card_info;
4791 struct carrier_info *carrier_info;
4793 QETH_CARD_TEXT(card, 2, "qcrdincb");
4794 carrier_info = (struct carrier_info *)reply->param;
4795 cmd = (struct qeth_ipa_cmd *)data;
4796 card_info = &cmd->data.setadapterparms.data.card_info;
4797 if (cmd->data.setadapterparms.hdr.return_code == 0) {
4798 carrier_info->card_type = card_info->card_type;
4799 carrier_info->port_mode = card_info->port_mode;
4800 carrier_info->port_speed = card_info->port_speed;
4803 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
4804 return 0;
4807 static int qeth_query_card_info(struct qeth_card *card,
4808 struct carrier_info *carrier_info)
4810 struct qeth_cmd_buffer *iob;
4812 QETH_CARD_TEXT(card, 2, "qcrdinfo");
4813 if (!qeth_adp_supported(card, IPA_SETADP_QUERY_CARD_INFO))
4814 return -EOPNOTSUPP;
4815 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_CARD_INFO,
4816 sizeof(struct qeth_ipacmd_setadpparms_hdr));
4817 if (!iob)
4818 return -ENOMEM;
4819 return qeth_send_ipa_cmd(card, iob, qeth_query_card_info_cb,
4820 (void *)carrier_info);
4824 * qeth_vm_request_mac() - Request a hypervisor-managed MAC address
4825 * @card: pointer to a qeth_card
4827 * Returns
4828 * 0, if a MAC address has been set for the card's netdevice
4829 * a return code, for various error conditions
4831 int qeth_vm_request_mac(struct qeth_card *card)
4833 struct diag26c_mac_resp *response;
4834 struct diag26c_mac_req *request;
4835 struct ccw_dev_id id;
4836 int rc;
4838 QETH_DBF_TEXT(SETUP, 2, "vmreqmac");
4840 if (!card->dev)
4841 return -ENODEV;
4843 request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
4844 response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
4845 if (!request || !response) {
4846 rc = -ENOMEM;
4847 goto out;
4850 ccw_device_get_id(CARD_DDEV(card), &id);
4851 request->resp_buf_len = sizeof(*response);
4852 request->resp_version = DIAG26C_VERSION2;
4853 request->op_code = DIAG26C_GET_MAC;
4854 request->devno = id.devno;
4856 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4857 rc = diag26c(request, response, DIAG26C_MAC_SERVICES);
4858 QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4859 if (rc)
4860 goto out;
4861 QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
4863 if (request->resp_buf_len < sizeof(*response) ||
4864 response->version != request->resp_version) {
4865 rc = -EIO;
4866 QETH_DBF_TEXT(SETUP, 2, "badresp");
4867 QETH_DBF_HEX(SETUP, 2, &request->resp_buf_len,
4868 sizeof(request->resp_buf_len));
4869 } else if (!is_valid_ether_addr(response->mac)) {
4870 rc = -EINVAL;
4871 QETH_DBF_TEXT(SETUP, 2, "badmac");
4872 QETH_DBF_HEX(SETUP, 2, response->mac, ETH_ALEN);
4873 } else {
4874 ether_addr_copy(card->dev->dev_addr, response->mac);
4877 out:
4878 kfree(response);
4879 kfree(request);
4880 return rc;
4882 EXPORT_SYMBOL_GPL(qeth_vm_request_mac);
4884 static int qeth_get_qdio_q_format(struct qeth_card *card)
4886 if (card->info.type == QETH_CARD_TYPE_IQD)
4887 return QDIO_IQDIO_QFMT;
4888 else
4889 return QDIO_QETH_QFMT;
4892 static void qeth_determine_capabilities(struct qeth_card *card)
4894 int rc;
4895 int length;
4896 char *prcd;
4897 struct ccw_device *ddev;
4898 int ddev_offline = 0;
4900 QETH_DBF_TEXT(SETUP, 2, "detcapab");
4901 ddev = CARD_DDEV(card);
4902 if (!ddev->online) {
4903 ddev_offline = 1;
4904 rc = ccw_device_set_online(ddev);
4905 if (rc) {
4906 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4907 goto out;
4911 rc = qeth_read_conf_data(card, (void **) &prcd, &length);
4912 if (rc) {
4913 QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
4914 dev_name(&card->gdev->dev), rc);
4915 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4916 goto out_offline;
4918 qeth_configure_unitaddr(card, prcd);
4919 if (ddev_offline)
4920 qeth_configure_blkt_default(card, prcd);
4921 kfree(prcd);
4923 rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4924 if (rc)
4925 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
4927 QETH_DBF_TEXT_(SETUP, 2, "qfmt%d", card->ssqd.qfmt);
4928 QETH_DBF_TEXT_(SETUP, 2, "ac1:%02x", card->ssqd.qdioac1);
4929 QETH_DBF_TEXT_(SETUP, 2, "ac2:%04x", card->ssqd.qdioac2);
4930 QETH_DBF_TEXT_(SETUP, 2, "ac3:%04x", card->ssqd.qdioac3);
4931 QETH_DBF_TEXT_(SETUP, 2, "icnt%d", card->ssqd.icnt);
4932 if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
4933 ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
4934 ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
4935 dev_info(&card->gdev->dev,
4936 "Completion Queueing supported\n");
4937 } else {
4938 card->options.cq = QETH_CQ_NOTAVAILABLE;
4942 out_offline:
4943 if (ddev_offline == 1)
4944 ccw_device_set_offline(ddev);
4945 out:
4946 return;
4949 static void qeth_qdio_establish_cq(struct qeth_card *card,
4950 struct qdio_buffer **in_sbal_ptrs,
4951 void (**queue_start_poll)
4952 (struct ccw_device *, int,
4953 unsigned long))
4955 int i;
4957 if (card->options.cq == QETH_CQ_ENABLED) {
4958 int offset = QDIO_MAX_BUFFERS_PER_Q *
4959 (card->qdio.no_in_queues - 1);
4960 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4961 in_sbal_ptrs[offset + i] = (struct qdio_buffer *)
4962 virt_to_phys(card->qdio.c_q->bufs[i].buffer);
4965 queue_start_poll[card->qdio.no_in_queues - 1] = NULL;
4969 static int qeth_qdio_establish(struct qeth_card *card)
4971 struct qdio_initialize init_data;
4972 char *qib_param_field;
4973 struct qdio_buffer **in_sbal_ptrs;
4974 void (**queue_start_poll) (struct ccw_device *, int, unsigned long);
4975 struct qdio_buffer **out_sbal_ptrs;
4976 int i, j, k;
4977 int rc = 0;
4979 QETH_DBF_TEXT(SETUP, 2, "qdioest");
4981 qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
4982 GFP_KERNEL);
4983 if (!qib_param_field) {
4984 rc = -ENOMEM;
4985 goto out_free_nothing;
4988 qeth_create_qib_param_field(card, qib_param_field);
4989 qeth_create_qib_param_field_blkt(card, qib_param_field);
4991 in_sbal_ptrs = kzalloc(card->qdio.no_in_queues *
4992 QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
4993 GFP_KERNEL);
4994 if (!in_sbal_ptrs) {
4995 rc = -ENOMEM;
4996 goto out_free_qib_param;
4998 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i) {
4999 in_sbal_ptrs[i] = (struct qdio_buffer *)
5000 virt_to_phys(card->qdio.in_q->bufs[i].buffer);
5003 queue_start_poll = kzalloc(sizeof(void *) * card->qdio.no_in_queues,
5004 GFP_KERNEL);
5005 if (!queue_start_poll) {
5006 rc = -ENOMEM;
5007 goto out_free_in_sbals;
5009 for (i = 0; i < card->qdio.no_in_queues; ++i)
5010 queue_start_poll[i] = card->discipline->start_poll;
5012 qeth_qdio_establish_cq(card, in_sbal_ptrs, queue_start_poll);
5014 out_sbal_ptrs =
5015 kzalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
5016 sizeof(void *), GFP_KERNEL);
5017 if (!out_sbal_ptrs) {
5018 rc = -ENOMEM;
5019 goto out_free_queue_start_poll;
5021 for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
5022 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
5023 out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
5024 card->qdio.out_qs[i]->bufs[j]->buffer);
5027 memset(&init_data, 0, sizeof(struct qdio_initialize));
5028 init_data.cdev = CARD_DDEV(card);
5029 init_data.q_format = qeth_get_qdio_q_format(card);
5030 init_data.qib_param_field_format = 0;
5031 init_data.qib_param_field = qib_param_field;
5032 init_data.no_input_qs = card->qdio.no_in_queues;
5033 init_data.no_output_qs = card->qdio.no_out_queues;
5034 init_data.input_handler = card->discipline->input_handler;
5035 init_data.output_handler = card->discipline->output_handler;
5036 init_data.queue_start_poll_array = queue_start_poll;
5037 init_data.int_parm = (unsigned long) card;
5038 init_data.input_sbal_addr_array = (void **) in_sbal_ptrs;
5039 init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
5040 init_data.output_sbal_state_array = card->qdio.out_bufstates;
5041 init_data.scan_threshold =
5042 (card->info.type == QETH_CARD_TYPE_IQD) ? 1 : 32;
5044 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
5045 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
5046 rc = qdio_allocate(&init_data);
5047 if (rc) {
5048 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5049 goto out;
5051 rc = qdio_establish(&init_data);
5052 if (rc) {
5053 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
5054 qdio_free(CARD_DDEV(card));
5058 switch (card->options.cq) {
5059 case QETH_CQ_ENABLED:
5060 dev_info(&card->gdev->dev, "Completion Queue support enabled");
5061 break;
5062 case QETH_CQ_DISABLED:
5063 dev_info(&card->gdev->dev, "Completion Queue support disabled");
5064 break;
5065 default:
5066 break;
5068 out:
5069 kfree(out_sbal_ptrs);
5070 out_free_queue_start_poll:
5071 kfree(queue_start_poll);
5072 out_free_in_sbals:
5073 kfree(in_sbal_ptrs);
5074 out_free_qib_param:
5075 kfree(qib_param_field);
5076 out_free_nothing:
5077 return rc;
5080 static void qeth_core_free_card(struct qeth_card *card)
5083 QETH_DBF_TEXT(SETUP, 2, "freecrd");
5084 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
5085 qeth_clean_channel(&card->read);
5086 qeth_clean_channel(&card->write);
5087 if (card->dev)
5088 free_netdev(card->dev);
5089 qeth_free_qdio_buffers(card);
5090 unregister_service_level(&card->qeth_service_level);
5091 kfree(card);
5094 void qeth_trace_features(struct qeth_card *card)
5096 QETH_CARD_TEXT(card, 2, "features");
5097 QETH_CARD_HEX(card, 2, &card->options.ipa4, sizeof(card->options.ipa4));
5098 QETH_CARD_HEX(card, 2, &card->options.ipa6, sizeof(card->options.ipa6));
5099 QETH_CARD_HEX(card, 2, &card->options.adp, sizeof(card->options.adp));
5100 QETH_CARD_HEX(card, 2, &card->info.diagass_support,
5101 sizeof(card->info.diagass_support));
5103 EXPORT_SYMBOL_GPL(qeth_trace_features);
5105 static struct ccw_device_id qeth_ids[] = {
5106 {CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
5107 .driver_info = QETH_CARD_TYPE_OSD},
5108 {CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
5109 .driver_info = QETH_CARD_TYPE_IQD},
5110 {CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
5111 .driver_info = QETH_CARD_TYPE_OSN},
5112 {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
5113 .driver_info = QETH_CARD_TYPE_OSM},
5114 {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
5115 .driver_info = QETH_CARD_TYPE_OSX},
5118 MODULE_DEVICE_TABLE(ccw, qeth_ids);
5120 static struct ccw_driver qeth_ccw_driver = {
5121 .driver = {
5122 .owner = THIS_MODULE,
5123 .name = "qeth",
5125 .ids = qeth_ids,
5126 .probe = ccwgroup_probe_ccwdev,
5127 .remove = ccwgroup_remove_ccwdev,
5130 int qeth_core_hardsetup_card(struct qeth_card *card)
5132 int retries = 3;
5133 int rc;
5135 QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
5136 atomic_set(&card->force_alloc_skb, 0);
5137 qeth_update_from_chp_desc(card);
5138 retry:
5139 if (retries < 3)
5140 QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
5141 dev_name(&card->gdev->dev));
5142 rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
5143 ccw_device_set_offline(CARD_DDEV(card));
5144 ccw_device_set_offline(CARD_WDEV(card));
5145 ccw_device_set_offline(CARD_RDEV(card));
5146 qdio_free(CARD_DDEV(card));
5147 rc = ccw_device_set_online(CARD_RDEV(card));
5148 if (rc)
5149 goto retriable;
5150 rc = ccw_device_set_online(CARD_WDEV(card));
5151 if (rc)
5152 goto retriable;
5153 rc = ccw_device_set_online(CARD_DDEV(card));
5154 if (rc)
5155 goto retriable;
5156 retriable:
5157 if (rc == -ERESTARTSYS) {
5158 QETH_DBF_TEXT(SETUP, 2, "break1");
5159 return rc;
5160 } else if (rc) {
5161 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
5162 if (--retries < 0)
5163 goto out;
5164 else
5165 goto retry;
5167 qeth_determine_capabilities(card);
5168 qeth_init_tokens(card);
5169 qeth_init_func_level(card);
5170 rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
5171 if (rc == -ERESTARTSYS) {
5172 QETH_DBF_TEXT(SETUP, 2, "break2");
5173 return rc;
5174 } else if (rc) {
5175 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
5176 if (--retries < 0)
5177 goto out;
5178 else
5179 goto retry;
5181 rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
5182 if (rc == -ERESTARTSYS) {
5183 QETH_DBF_TEXT(SETUP, 2, "break3");
5184 return rc;
5185 } else if (rc) {
5186 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
5187 if (--retries < 0)
5188 goto out;
5189 else
5190 goto retry;
5192 card->read_or_write_problem = 0;
5193 rc = qeth_mpc_initialize(card);
5194 if (rc) {
5195 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
5196 goto out;
5199 rc = qeth_send_startlan(card);
5200 if (rc) {
5201 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
5202 if (rc == IPA_RC_LAN_OFFLINE) {
5203 dev_warn(&card->gdev->dev,
5204 "The LAN is offline\n");
5205 card->lan_online = 0;
5206 } else {
5207 rc = -ENODEV;
5208 goto out;
5210 } else
5211 card->lan_online = 1;
5213 card->options.ipa4.supported_funcs = 0;
5214 card->options.ipa6.supported_funcs = 0;
5215 card->options.adp.supported_funcs = 0;
5216 card->options.sbp.supported_funcs = 0;
5217 card->info.diagass_support = 0;
5218 rc = qeth_query_ipassists(card, QETH_PROT_IPV4);
5219 if (rc == -ENOMEM)
5220 goto out;
5221 if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
5222 rc = qeth_query_setadapterparms(card);
5223 if (rc < 0) {
5224 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
5225 goto out;
5228 if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
5229 rc = qeth_query_setdiagass(card);
5230 if (rc < 0) {
5231 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
5232 goto out;
5235 return 0;
5236 out:
5237 dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5238 "an error on the device\n");
5239 QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
5240 dev_name(&card->gdev->dev), rc);
5241 return rc;
5243 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
5245 static void qeth_create_skb_frag(struct qdio_buffer_element *element,
5246 struct sk_buff *skb, int offset, int data_len)
5248 struct page *page = virt_to_page(element->addr);
5249 unsigned int next_frag;
5251 /* first fill the linear space */
5252 if (!skb->len) {
5253 unsigned int linear = min(data_len, skb_tailroom(skb));
5255 skb_put_data(skb, element->addr + offset, linear);
5256 data_len -= linear;
5257 if (!data_len)
5258 return;
5259 offset += linear;
5260 /* fall through to add page frag for remaining data */
5263 next_frag = skb_shinfo(skb)->nr_frags;
5264 get_page(page);
5265 skb_add_rx_frag(skb, next_frag, page, offset, data_len, data_len);
5268 static inline int qeth_is_last_sbale(struct qdio_buffer_element *sbale)
5270 return (sbale->eflags & SBAL_EFLAGS_LAST_ENTRY);
5273 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
5274 struct qeth_qdio_buffer *qethbuffer,
5275 struct qdio_buffer_element **__element, int *__offset,
5276 struct qeth_hdr **hdr)
5278 struct qdio_buffer_element *element = *__element;
5279 struct qdio_buffer *buffer = qethbuffer->buffer;
5280 int offset = *__offset;
5281 struct sk_buff *skb;
5282 int skb_len = 0;
5283 void *data_ptr;
5284 int data_len;
5285 int headroom = 0;
5286 int use_rx_sg = 0;
5288 /* qeth_hdr must not cross element boundaries */
5289 while (element->length < offset + sizeof(struct qeth_hdr)) {
5290 if (qeth_is_last_sbale(element))
5291 return NULL;
5292 element++;
5293 offset = 0;
5295 *hdr = element->addr + offset;
5297 offset += sizeof(struct qeth_hdr);
5298 switch ((*hdr)->hdr.l2.id) {
5299 case QETH_HEADER_TYPE_LAYER2:
5300 skb_len = (*hdr)->hdr.l2.pkt_length;
5301 break;
5302 case QETH_HEADER_TYPE_LAYER3:
5303 skb_len = (*hdr)->hdr.l3.length;
5304 headroom = ETH_HLEN;
5305 break;
5306 case QETH_HEADER_TYPE_OSN:
5307 skb_len = (*hdr)->hdr.osn.pdu_length;
5308 headroom = sizeof(struct qeth_hdr);
5309 break;
5310 default:
5311 break;
5314 if (!skb_len)
5315 return NULL;
5317 if (((skb_len >= card->options.rx_sg_cb) &&
5318 (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
5319 (!atomic_read(&card->force_alloc_skb))) ||
5320 (card->options.cq == QETH_CQ_ENABLED))
5321 use_rx_sg = 1;
5323 if (use_rx_sg && qethbuffer->rx_skb) {
5324 /* QETH_CQ_ENABLED only: */
5325 skb = qethbuffer->rx_skb;
5326 qethbuffer->rx_skb = NULL;
5327 } else {
5328 unsigned int linear = (use_rx_sg) ? QETH_RX_PULL_LEN : skb_len;
5330 skb = dev_alloc_skb(linear + headroom);
5332 if (!skb)
5333 goto no_mem;
5334 if (headroom)
5335 skb_reserve(skb, headroom);
5337 data_ptr = element->addr + offset;
5338 while (skb_len) {
5339 data_len = min(skb_len, (int)(element->length - offset));
5340 if (data_len) {
5341 if (use_rx_sg)
5342 qeth_create_skb_frag(element, skb, offset,
5343 data_len);
5344 else
5345 skb_put_data(skb, data_ptr, data_len);
5347 skb_len -= data_len;
5348 if (skb_len) {
5349 if (qeth_is_last_sbale(element)) {
5350 QETH_CARD_TEXT(card, 4, "unexeob");
5351 QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
5352 dev_kfree_skb_any(skb);
5353 card->stats.rx_errors++;
5354 return NULL;
5356 element++;
5357 offset = 0;
5358 data_ptr = element->addr;
5359 } else {
5360 offset += data_len;
5363 *__element = element;
5364 *__offset = offset;
5365 if (use_rx_sg && card->options.performance_stats) {
5366 card->perf_stats.sg_skbs_rx++;
5367 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
5369 return skb;
5370 no_mem:
5371 if (net_ratelimit()) {
5372 QETH_CARD_TEXT(card, 2, "noskbmem");
5374 card->stats.rx_dropped++;
5375 return NULL;
5377 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
5379 int qeth_poll(struct napi_struct *napi, int budget)
5381 struct qeth_card *card = container_of(napi, struct qeth_card, napi);
5382 int work_done = 0;
5383 struct qeth_qdio_buffer *buffer;
5384 int done;
5385 int new_budget = budget;
5387 if (card->options.performance_stats) {
5388 card->perf_stats.inbound_cnt++;
5389 card->perf_stats.inbound_start_time = qeth_get_micros();
5392 while (1) {
5393 if (!card->rx.b_count) {
5394 card->rx.qdio_err = 0;
5395 card->rx.b_count = qdio_get_next_buffers(
5396 card->data.ccwdev, 0, &card->rx.b_index,
5397 &card->rx.qdio_err);
5398 if (card->rx.b_count <= 0) {
5399 card->rx.b_count = 0;
5400 break;
5402 card->rx.b_element =
5403 &card->qdio.in_q->bufs[card->rx.b_index]
5404 .buffer->element[0];
5405 card->rx.e_offset = 0;
5408 while (card->rx.b_count) {
5409 buffer = &card->qdio.in_q->bufs[card->rx.b_index];
5410 if (!(card->rx.qdio_err &&
5411 qeth_check_qdio_errors(card, buffer->buffer,
5412 card->rx.qdio_err, "qinerr")))
5413 work_done +=
5414 card->discipline->process_rx_buffer(
5415 card, new_budget, &done);
5416 else
5417 done = 1;
5419 if (done) {
5420 if (card->options.performance_stats)
5421 card->perf_stats.bufs_rec++;
5422 qeth_put_buffer_pool_entry(card,
5423 buffer->pool_entry);
5424 qeth_queue_input_buffer(card, card->rx.b_index);
5425 card->rx.b_count--;
5426 if (card->rx.b_count) {
5427 card->rx.b_index =
5428 (card->rx.b_index + 1) %
5429 QDIO_MAX_BUFFERS_PER_Q;
5430 card->rx.b_element =
5431 &card->qdio.in_q
5432 ->bufs[card->rx.b_index]
5433 .buffer->element[0];
5434 card->rx.e_offset = 0;
5438 if (work_done >= budget)
5439 goto out;
5440 else
5441 new_budget = budget - work_done;
5445 napi_complete_done(napi, work_done);
5446 if (qdio_start_irq(card->data.ccwdev, 0))
5447 napi_schedule(&card->napi);
5448 out:
5449 if (card->options.performance_stats)
5450 card->perf_stats.inbound_time += qeth_get_micros() -
5451 card->perf_stats.inbound_start_time;
5452 return work_done;
5454 EXPORT_SYMBOL_GPL(qeth_poll);
5456 static int qeth_setassparms_inspect_rc(struct qeth_ipa_cmd *cmd)
5458 if (!cmd->hdr.return_code)
5459 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5460 return cmd->hdr.return_code;
5463 int qeth_setassparms_cb(struct qeth_card *card,
5464 struct qeth_reply *reply, unsigned long data)
5466 struct qeth_ipa_cmd *cmd;
5468 QETH_CARD_TEXT(card, 4, "defadpcb");
5470 cmd = (struct qeth_ipa_cmd *) data;
5471 if (cmd->hdr.return_code == 0) {
5472 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5473 if (cmd->hdr.prot_version == QETH_PROT_IPV4)
5474 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
5475 if (cmd->hdr.prot_version == QETH_PROT_IPV6)
5476 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
5478 return 0;
5480 EXPORT_SYMBOL_GPL(qeth_setassparms_cb);
5482 struct qeth_cmd_buffer *qeth_get_setassparms_cmd(struct qeth_card *card,
5483 enum qeth_ipa_funcs ipa_func,
5484 __u16 cmd_code, __u16 len,
5485 enum qeth_prot_versions prot)
5487 struct qeth_cmd_buffer *iob;
5488 struct qeth_ipa_cmd *cmd;
5490 QETH_CARD_TEXT(card, 4, "getasscm");
5491 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETASSPARMS, prot);
5493 if (iob) {
5494 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5495 cmd->data.setassparms.hdr.assist_no = ipa_func;
5496 cmd->data.setassparms.hdr.length = 8 + len;
5497 cmd->data.setassparms.hdr.command_code = cmd_code;
5498 cmd->data.setassparms.hdr.return_code = 0;
5499 cmd->data.setassparms.hdr.seq_no = 0;
5502 return iob;
5504 EXPORT_SYMBOL_GPL(qeth_get_setassparms_cmd);
5506 int qeth_send_setassparms(struct qeth_card *card,
5507 struct qeth_cmd_buffer *iob, __u16 len, long data,
5508 int (*reply_cb)(struct qeth_card *,
5509 struct qeth_reply *, unsigned long),
5510 void *reply_param)
5512 int rc;
5513 struct qeth_ipa_cmd *cmd;
5515 QETH_CARD_TEXT(card, 4, "sendassp");
5517 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
5518 if (len <= sizeof(__u32))
5519 cmd->data.setassparms.data.flags_32bit = (__u32) data;
5520 else /* (len > sizeof(__u32)) */
5521 memcpy(&cmd->data.setassparms.data, (void *) data, len);
5523 rc = qeth_send_ipa_cmd(card, iob, reply_cb, reply_param);
5524 return rc;
5526 EXPORT_SYMBOL_GPL(qeth_send_setassparms);
5528 int qeth_send_simple_setassparms(struct qeth_card *card,
5529 enum qeth_ipa_funcs ipa_func,
5530 __u16 cmd_code, long data)
5532 int rc;
5533 int length = 0;
5534 struct qeth_cmd_buffer *iob;
5536 QETH_CARD_TEXT(card, 4, "simassp4");
5537 if (data)
5538 length = sizeof(__u32);
5539 iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
5540 length, QETH_PROT_IPV4);
5541 if (!iob)
5542 return -ENOMEM;
5543 rc = qeth_send_setassparms(card, iob, length, data,
5544 qeth_setassparms_cb, NULL);
5545 return rc;
5547 EXPORT_SYMBOL_GPL(qeth_send_simple_setassparms);
5549 static void qeth_unregister_dbf_views(void)
5551 int x;
5552 for (x = 0; x < QETH_DBF_INFOS; x++) {
5553 debug_unregister(qeth_dbf[x].id);
5554 qeth_dbf[x].id = NULL;
5558 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
5560 char dbf_txt_buf[32];
5561 va_list args;
5563 if (!debug_level_enabled(id, level))
5564 return;
5565 va_start(args, fmt);
5566 vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
5567 va_end(args);
5568 debug_text_event(id, level, dbf_txt_buf);
5570 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
5572 static int qeth_register_dbf_views(void)
5574 int ret;
5575 int x;
5577 for (x = 0; x < QETH_DBF_INFOS; x++) {
5578 /* register the areas */
5579 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
5580 qeth_dbf[x].pages,
5581 qeth_dbf[x].areas,
5582 qeth_dbf[x].len);
5583 if (qeth_dbf[x].id == NULL) {
5584 qeth_unregister_dbf_views();
5585 return -ENOMEM;
5588 /* register a view */
5589 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
5590 if (ret) {
5591 qeth_unregister_dbf_views();
5592 return ret;
5595 /* set a passing level */
5596 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
5599 return 0;
5602 int qeth_core_load_discipline(struct qeth_card *card,
5603 enum qeth_discipline_id discipline)
5605 int rc = 0;
5607 mutex_lock(&qeth_mod_mutex);
5608 switch (discipline) {
5609 case QETH_DISCIPLINE_LAYER3:
5610 card->discipline = try_then_request_module(
5611 symbol_get(qeth_l3_discipline), "qeth_l3");
5612 break;
5613 case QETH_DISCIPLINE_LAYER2:
5614 card->discipline = try_then_request_module(
5615 symbol_get(qeth_l2_discipline), "qeth_l2");
5616 break;
5617 default:
5618 break;
5621 if (!card->discipline) {
5622 dev_err(&card->gdev->dev, "There is no kernel module to "
5623 "support discipline %d\n", discipline);
5624 rc = -EINVAL;
5626 mutex_unlock(&qeth_mod_mutex);
5627 return rc;
5630 void qeth_core_free_discipline(struct qeth_card *card)
5632 if (card->options.layer2)
5633 symbol_put(qeth_l2_discipline);
5634 else
5635 symbol_put(qeth_l3_discipline);
5636 card->discipline = NULL;
5639 const struct device_type qeth_generic_devtype = {
5640 .name = "qeth_generic",
5641 .groups = qeth_generic_attr_groups,
5643 EXPORT_SYMBOL_GPL(qeth_generic_devtype);
5645 static const struct device_type qeth_osn_devtype = {
5646 .name = "qeth_osn",
5647 .groups = qeth_osn_attr_groups,
5650 #define DBF_NAME_LEN 20
5652 struct qeth_dbf_entry {
5653 char dbf_name[DBF_NAME_LEN];
5654 debug_info_t *dbf_info;
5655 struct list_head dbf_list;
5658 static LIST_HEAD(qeth_dbf_list);
5659 static DEFINE_MUTEX(qeth_dbf_list_mutex);
5661 static debug_info_t *qeth_get_dbf_entry(char *name)
5663 struct qeth_dbf_entry *entry;
5664 debug_info_t *rc = NULL;
5666 mutex_lock(&qeth_dbf_list_mutex);
5667 list_for_each_entry(entry, &qeth_dbf_list, dbf_list) {
5668 if (strcmp(entry->dbf_name, name) == 0) {
5669 rc = entry->dbf_info;
5670 break;
5673 mutex_unlock(&qeth_dbf_list_mutex);
5674 return rc;
5677 static int qeth_add_dbf_entry(struct qeth_card *card, char *name)
5679 struct qeth_dbf_entry *new_entry;
5681 card->debug = debug_register(name, 2, 1, 8);
5682 if (!card->debug) {
5683 QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
5684 goto err;
5686 if (debug_register_view(card->debug, &debug_hex_ascii_view))
5687 goto err_dbg;
5688 new_entry = kzalloc(sizeof(struct qeth_dbf_entry), GFP_KERNEL);
5689 if (!new_entry)
5690 goto err_dbg;
5691 strncpy(new_entry->dbf_name, name, DBF_NAME_LEN);
5692 new_entry->dbf_info = card->debug;
5693 mutex_lock(&qeth_dbf_list_mutex);
5694 list_add(&new_entry->dbf_list, &qeth_dbf_list);
5695 mutex_unlock(&qeth_dbf_list_mutex);
5697 return 0;
5699 err_dbg:
5700 debug_unregister(card->debug);
5701 err:
5702 return -ENOMEM;
5705 static void qeth_clear_dbf_list(void)
5707 struct qeth_dbf_entry *entry, *tmp;
5709 mutex_lock(&qeth_dbf_list_mutex);
5710 list_for_each_entry_safe(entry, tmp, &qeth_dbf_list, dbf_list) {
5711 list_del(&entry->dbf_list);
5712 debug_unregister(entry->dbf_info);
5713 kfree(entry);
5715 mutex_unlock(&qeth_dbf_list_mutex);
5718 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
5720 struct qeth_card *card;
5721 struct device *dev;
5722 int rc;
5723 enum qeth_discipline_id enforced_disc;
5724 unsigned long flags;
5725 char dbf_name[DBF_NAME_LEN];
5727 QETH_DBF_TEXT(SETUP, 2, "probedev");
5729 dev = &gdev->dev;
5730 if (!get_device(dev))
5731 return -ENODEV;
5733 QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
5735 card = qeth_alloc_card();
5736 if (!card) {
5737 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
5738 rc = -ENOMEM;
5739 goto err_dev;
5742 snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
5743 dev_name(&gdev->dev));
5744 card->debug = qeth_get_dbf_entry(dbf_name);
5745 if (!card->debug) {
5746 rc = qeth_add_dbf_entry(card, dbf_name);
5747 if (rc)
5748 goto err_card;
5751 card->read.ccwdev = gdev->cdev[0];
5752 card->write.ccwdev = gdev->cdev[1];
5753 card->data.ccwdev = gdev->cdev[2];
5754 dev_set_drvdata(&gdev->dev, card);
5755 card->gdev = gdev;
5756 gdev->cdev[0]->handler = qeth_irq;
5757 gdev->cdev[1]->handler = qeth_irq;
5758 gdev->cdev[2]->handler = qeth_irq;
5760 qeth_determine_card_type(card);
5761 rc = qeth_setup_card(card);
5762 if (rc) {
5763 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
5764 goto err_card;
5767 qeth_determine_capabilities(card);
5768 enforced_disc = qeth_enforce_discipline(card);
5769 switch (enforced_disc) {
5770 case QETH_DISCIPLINE_UNDETERMINED:
5771 gdev->dev.type = &qeth_generic_devtype;
5772 break;
5773 default:
5774 card->info.layer_enforced = true;
5775 rc = qeth_core_load_discipline(card, enforced_disc);
5776 if (rc)
5777 goto err_card;
5779 gdev->dev.type = (card->info.type != QETH_CARD_TYPE_OSN)
5780 ? card->discipline->devtype
5781 : &qeth_osn_devtype;
5782 rc = card->discipline->setup(card->gdev);
5783 if (rc)
5784 goto err_disc;
5785 break;
5788 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5789 list_add_tail(&card->list, &qeth_core_card_list.list);
5790 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5791 return 0;
5793 err_disc:
5794 qeth_core_free_discipline(card);
5795 err_card:
5796 qeth_core_free_card(card);
5797 err_dev:
5798 put_device(dev);
5799 return rc;
5802 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
5804 unsigned long flags;
5805 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5807 QETH_DBF_TEXT(SETUP, 2, "removedv");
5809 if (card->discipline) {
5810 card->discipline->remove(gdev);
5811 qeth_core_free_discipline(card);
5814 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
5815 list_del(&card->list);
5816 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
5817 qeth_core_free_card(card);
5818 dev_set_drvdata(&gdev->dev, NULL);
5819 put_device(&gdev->dev);
5820 return;
5823 static int qeth_core_set_online(struct ccwgroup_device *gdev)
5825 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5826 int rc = 0;
5827 enum qeth_discipline_id def_discipline;
5829 if (!card->discipline) {
5830 if (card->info.type == QETH_CARD_TYPE_IQD)
5831 def_discipline = QETH_DISCIPLINE_LAYER3;
5832 else
5833 def_discipline = QETH_DISCIPLINE_LAYER2;
5834 rc = qeth_core_load_discipline(card, def_discipline);
5835 if (rc)
5836 goto err;
5837 rc = card->discipline->setup(card->gdev);
5838 if (rc) {
5839 qeth_core_free_discipline(card);
5840 goto err;
5843 rc = card->discipline->set_online(gdev);
5844 err:
5845 return rc;
5848 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
5850 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5851 return card->discipline->set_offline(gdev);
5854 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
5856 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5857 qeth_set_allowed_threads(card, 0, 1);
5858 if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
5859 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
5860 qeth_qdio_clear_card(card, 0);
5861 qeth_clear_qdio_buffers(card);
5862 qdio_free(CARD_DDEV(card));
5865 static int qeth_core_freeze(struct ccwgroup_device *gdev)
5867 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5868 if (card->discipline && card->discipline->freeze)
5869 return card->discipline->freeze(gdev);
5870 return 0;
5873 static int qeth_core_thaw(struct ccwgroup_device *gdev)
5875 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5876 if (card->discipline && card->discipline->thaw)
5877 return card->discipline->thaw(gdev);
5878 return 0;
5881 static int qeth_core_restore(struct ccwgroup_device *gdev)
5883 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5884 if (card->discipline && card->discipline->restore)
5885 return card->discipline->restore(gdev);
5886 return 0;
5889 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
5890 .driver = {
5891 .owner = THIS_MODULE,
5892 .name = "qeth",
5894 .ccw_driver = &qeth_ccw_driver,
5895 .setup = qeth_core_probe_device,
5896 .remove = qeth_core_remove_device,
5897 .set_online = qeth_core_set_online,
5898 .set_offline = qeth_core_set_offline,
5899 .shutdown = qeth_core_shutdown,
5900 .prepare = NULL,
5901 .complete = NULL,
5902 .freeze = qeth_core_freeze,
5903 .thaw = qeth_core_thaw,
5904 .restore = qeth_core_restore,
5907 static ssize_t group_store(struct device_driver *ddrv, const char *buf,
5908 size_t count)
5910 int err;
5912 err = ccwgroup_create_dev(qeth_core_root_dev,
5913 &qeth_core_ccwgroup_driver, 3, buf);
5915 return err ? err : count;
5917 static DRIVER_ATTR_WO(group);
5919 static struct attribute *qeth_drv_attrs[] = {
5920 &driver_attr_group.attr,
5921 NULL,
5923 static struct attribute_group qeth_drv_attr_group = {
5924 .attrs = qeth_drv_attrs,
5926 static const struct attribute_group *qeth_drv_attr_groups[] = {
5927 &qeth_drv_attr_group,
5928 NULL,
5931 int qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5933 struct qeth_card *card = dev->ml_priv;
5934 struct mii_ioctl_data *mii_data;
5935 int rc = 0;
5937 if (!card)
5938 return -ENODEV;
5940 if (!qeth_card_hw_is_reachable(card))
5941 return -ENODEV;
5943 if (card->info.type == QETH_CARD_TYPE_OSN)
5944 return -EPERM;
5946 switch (cmd) {
5947 case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5948 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5949 break;
5950 case SIOC_QETH_GET_CARD_TYPE:
5951 if ((card->info.type == QETH_CARD_TYPE_OSD ||
5952 card->info.type == QETH_CARD_TYPE_OSM ||
5953 card->info.type == QETH_CARD_TYPE_OSX) &&
5954 !card->info.guestlan)
5955 return 1;
5956 else
5957 return 0;
5958 case SIOCGMIIPHY:
5959 mii_data = if_mii(rq);
5960 mii_data->phy_id = 0;
5961 break;
5962 case SIOCGMIIREG:
5963 mii_data = if_mii(rq);
5964 if (mii_data->phy_id != 0)
5965 rc = -EINVAL;
5966 else
5967 mii_data->val_out = qeth_mdio_read(dev,
5968 mii_data->phy_id, mii_data->reg_num);
5969 break;
5970 case SIOC_QETH_QUERY_OAT:
5971 rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
5972 break;
5973 default:
5974 if (card->discipline->do_ioctl)
5975 rc = card->discipline->do_ioctl(dev, rq, cmd);
5976 else
5977 rc = -EOPNOTSUPP;
5979 if (rc)
5980 QETH_CARD_TEXT_(card, 2, "ioce%x", rc);
5981 return rc;
5983 EXPORT_SYMBOL_GPL(qeth_do_ioctl);
5985 static struct {
5986 const char str[ETH_GSTRING_LEN];
5987 } qeth_ethtool_stats_keys[] = {
5988 /* 0 */{"rx skbs"},
5989 {"rx buffers"},
5990 {"tx skbs"},
5991 {"tx buffers"},
5992 {"tx skbs no packing"},
5993 {"tx buffers no packing"},
5994 {"tx skbs packing"},
5995 {"tx buffers packing"},
5996 {"tx sg skbs"},
5997 {"tx sg frags"},
5998 /* 10 */{"rx sg skbs"},
5999 {"rx sg frags"},
6000 {"rx sg page allocs"},
6001 {"tx large kbytes"},
6002 {"tx large count"},
6003 {"tx pk state ch n->p"},
6004 {"tx pk state ch p->n"},
6005 {"tx pk watermark low"},
6006 {"tx pk watermark high"},
6007 {"queue 0 buffer usage"},
6008 /* 20 */{"queue 1 buffer usage"},
6009 {"queue 2 buffer usage"},
6010 {"queue 3 buffer usage"},
6011 {"rx poll time"},
6012 {"rx poll count"},
6013 {"rx do_QDIO time"},
6014 {"rx do_QDIO count"},
6015 {"tx handler time"},
6016 {"tx handler count"},
6017 {"tx time"},
6018 /* 30 */{"tx count"},
6019 {"tx do_QDIO time"},
6020 {"tx do_QDIO count"},
6021 {"tx csum"},
6022 {"tx lin"},
6023 {"tx linfail"},
6024 {"cq handler count"},
6025 {"cq handler time"}
6028 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
6030 switch (stringset) {
6031 case ETH_SS_STATS:
6032 return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
6033 default:
6034 return -EINVAL;
6037 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
6039 void qeth_core_get_ethtool_stats(struct net_device *dev,
6040 struct ethtool_stats *stats, u64 *data)
6042 struct qeth_card *card = dev->ml_priv;
6043 data[0] = card->stats.rx_packets -
6044 card->perf_stats.initial_rx_packets;
6045 data[1] = card->perf_stats.bufs_rec;
6046 data[2] = card->stats.tx_packets -
6047 card->perf_stats.initial_tx_packets;
6048 data[3] = card->perf_stats.bufs_sent;
6049 data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
6050 - card->perf_stats.skbs_sent_pack;
6051 data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
6052 data[6] = card->perf_stats.skbs_sent_pack;
6053 data[7] = card->perf_stats.bufs_sent_pack;
6054 data[8] = card->perf_stats.sg_skbs_sent;
6055 data[9] = card->perf_stats.sg_frags_sent;
6056 data[10] = card->perf_stats.sg_skbs_rx;
6057 data[11] = card->perf_stats.sg_frags_rx;
6058 data[12] = card->perf_stats.sg_alloc_page_rx;
6059 data[13] = (card->perf_stats.large_send_bytes >> 10);
6060 data[14] = card->perf_stats.large_send_cnt;
6061 data[15] = card->perf_stats.sc_dp_p;
6062 data[16] = card->perf_stats.sc_p_dp;
6063 data[17] = QETH_LOW_WATERMARK_PACK;
6064 data[18] = QETH_HIGH_WATERMARK_PACK;
6065 data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
6066 data[20] = (card->qdio.no_out_queues > 1) ?
6067 atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
6068 data[21] = (card->qdio.no_out_queues > 2) ?
6069 atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
6070 data[22] = (card->qdio.no_out_queues > 3) ?
6071 atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
6072 data[23] = card->perf_stats.inbound_time;
6073 data[24] = card->perf_stats.inbound_cnt;
6074 data[25] = card->perf_stats.inbound_do_qdio_time;
6075 data[26] = card->perf_stats.inbound_do_qdio_cnt;
6076 data[27] = card->perf_stats.outbound_handler_time;
6077 data[28] = card->perf_stats.outbound_handler_cnt;
6078 data[29] = card->perf_stats.outbound_time;
6079 data[30] = card->perf_stats.outbound_cnt;
6080 data[31] = card->perf_stats.outbound_do_qdio_time;
6081 data[32] = card->perf_stats.outbound_do_qdio_cnt;
6082 data[33] = card->perf_stats.tx_csum;
6083 data[34] = card->perf_stats.tx_lin;
6084 data[35] = card->perf_stats.tx_linfail;
6085 data[36] = card->perf_stats.cq_cnt;
6086 data[37] = card->perf_stats.cq_time;
6088 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
6090 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6092 switch (stringset) {
6093 case ETH_SS_STATS:
6094 memcpy(data, &qeth_ethtool_stats_keys,
6095 sizeof(qeth_ethtool_stats_keys));
6096 break;
6097 default:
6098 WARN_ON(1);
6099 break;
6102 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
6104 void qeth_core_get_drvinfo(struct net_device *dev,
6105 struct ethtool_drvinfo *info)
6107 struct qeth_card *card = dev->ml_priv;
6109 strlcpy(info->driver, card->options.layer2 ? "qeth_l2" : "qeth_l3",
6110 sizeof(info->driver));
6111 strlcpy(info->version, "1.0", sizeof(info->version));
6112 strlcpy(info->fw_version, card->info.mcl_level,
6113 sizeof(info->fw_version));
6114 snprintf(info->bus_info, sizeof(info->bus_info), "%s/%s/%s",
6115 CARD_RDEV_ID(card), CARD_WDEV_ID(card), CARD_DDEV_ID(card));
6117 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
6119 /* Helper function to fill 'advertising' and 'supported' which are the same. */
6120 /* Autoneg and full-duplex are supported and advertised unconditionally. */
6121 /* Always advertise and support all speeds up to specified, and only one */
6122 /* specified port type. */
6123 static void qeth_set_cmd_adv_sup(struct ethtool_link_ksettings *cmd,
6124 int maxspeed, int porttype)
6126 ethtool_link_ksettings_zero_link_mode(cmd, supported);
6127 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
6128 ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
6130 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
6131 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
6133 switch (porttype) {
6134 case PORT_TP:
6135 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6136 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6137 break;
6138 case PORT_FIBRE:
6139 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
6140 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
6141 break;
6142 default:
6143 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
6144 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
6145 WARN_ON_ONCE(1);
6148 /* fallthrough from high to low, to select all legal speeds: */
6149 switch (maxspeed) {
6150 case SPEED_10000:
6151 ethtool_link_ksettings_add_link_mode(cmd, supported,
6152 10000baseT_Full);
6153 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6154 10000baseT_Full);
6155 case SPEED_1000:
6156 ethtool_link_ksettings_add_link_mode(cmd, supported,
6157 1000baseT_Full);
6158 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6159 1000baseT_Full);
6160 ethtool_link_ksettings_add_link_mode(cmd, supported,
6161 1000baseT_Half);
6162 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6163 1000baseT_Half);
6164 case SPEED_100:
6165 ethtool_link_ksettings_add_link_mode(cmd, supported,
6166 100baseT_Full);
6167 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6168 100baseT_Full);
6169 ethtool_link_ksettings_add_link_mode(cmd, supported,
6170 100baseT_Half);
6171 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6172 100baseT_Half);
6173 case SPEED_10:
6174 ethtool_link_ksettings_add_link_mode(cmd, supported,
6175 10baseT_Full);
6176 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6177 10baseT_Full);
6178 ethtool_link_ksettings_add_link_mode(cmd, supported,
6179 10baseT_Half);
6180 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6181 10baseT_Half);
6182 /* end fallthrough */
6183 break;
6184 default:
6185 ethtool_link_ksettings_add_link_mode(cmd, supported,
6186 10baseT_Full);
6187 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6188 10baseT_Full);
6189 ethtool_link_ksettings_add_link_mode(cmd, supported,
6190 10baseT_Half);
6191 ethtool_link_ksettings_add_link_mode(cmd, advertising,
6192 10baseT_Half);
6193 WARN_ON_ONCE(1);
6197 int qeth_core_ethtool_get_link_ksettings(struct net_device *netdev,
6198 struct ethtool_link_ksettings *cmd)
6200 struct qeth_card *card = netdev->ml_priv;
6201 enum qeth_link_types link_type;
6202 struct carrier_info carrier_info;
6203 int rc;
6205 if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
6206 link_type = QETH_LINK_TYPE_10GBIT_ETH;
6207 else
6208 link_type = card->info.link_type;
6210 cmd->base.duplex = DUPLEX_FULL;
6211 cmd->base.autoneg = AUTONEG_ENABLE;
6212 cmd->base.phy_address = 0;
6213 cmd->base.mdio_support = 0;
6214 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
6215 cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
6217 switch (link_type) {
6218 case QETH_LINK_TYPE_FAST_ETH:
6219 case QETH_LINK_TYPE_LANE_ETH100:
6220 cmd->base.speed = SPEED_100;
6221 cmd->base.port = PORT_TP;
6222 break;
6223 case QETH_LINK_TYPE_GBIT_ETH:
6224 case QETH_LINK_TYPE_LANE_ETH1000:
6225 cmd->base.speed = SPEED_1000;
6226 cmd->base.port = PORT_FIBRE;
6227 break;
6228 case QETH_LINK_TYPE_10GBIT_ETH:
6229 cmd->base.speed = SPEED_10000;
6230 cmd->base.port = PORT_FIBRE;
6231 break;
6232 default:
6233 cmd->base.speed = SPEED_10;
6234 cmd->base.port = PORT_TP;
6236 qeth_set_cmd_adv_sup(cmd, cmd->base.speed, cmd->base.port);
6238 /* Check if we can obtain more accurate information. */
6239 /* If QUERY_CARD_INFO command is not supported or fails, */
6240 /* just return the heuristics that was filled above. */
6241 if (!qeth_card_hw_is_reachable(card))
6242 return -ENODEV;
6243 rc = qeth_query_card_info(card, &carrier_info);
6244 if (rc == -EOPNOTSUPP) /* for old hardware, return heuristic */
6245 return 0;
6246 if (rc) /* report error from the hardware operation */
6247 return rc;
6248 /* on success, fill in the information got from the hardware */
6250 netdev_dbg(netdev,
6251 "card info: card_type=0x%02x, port_mode=0x%04x, port_speed=0x%08x\n",
6252 carrier_info.card_type,
6253 carrier_info.port_mode,
6254 carrier_info.port_speed);
6256 /* Update attributes for which we've obtained more authoritative */
6257 /* information, leave the rest the way they where filled above. */
6258 switch (carrier_info.card_type) {
6259 case CARD_INFO_TYPE_1G_COPPER_A:
6260 case CARD_INFO_TYPE_1G_COPPER_B:
6261 cmd->base.port = PORT_TP;
6262 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6263 break;
6264 case CARD_INFO_TYPE_1G_FIBRE_A:
6265 case CARD_INFO_TYPE_1G_FIBRE_B:
6266 cmd->base.port = PORT_FIBRE;
6267 qeth_set_cmd_adv_sup(cmd, SPEED_1000, cmd->base.port);
6268 break;
6269 case CARD_INFO_TYPE_10G_FIBRE_A:
6270 case CARD_INFO_TYPE_10G_FIBRE_B:
6271 cmd->base.port = PORT_FIBRE;
6272 qeth_set_cmd_adv_sup(cmd, SPEED_10000, cmd->base.port);
6273 break;
6276 switch (carrier_info.port_mode) {
6277 case CARD_INFO_PORTM_FULLDUPLEX:
6278 cmd->base.duplex = DUPLEX_FULL;
6279 break;
6280 case CARD_INFO_PORTM_HALFDUPLEX:
6281 cmd->base.duplex = DUPLEX_HALF;
6282 break;
6285 switch (carrier_info.port_speed) {
6286 case CARD_INFO_PORTS_10M:
6287 cmd->base.speed = SPEED_10;
6288 break;
6289 case CARD_INFO_PORTS_100M:
6290 cmd->base.speed = SPEED_100;
6291 break;
6292 case CARD_INFO_PORTS_1G:
6293 cmd->base.speed = SPEED_1000;
6294 break;
6295 case CARD_INFO_PORTS_10G:
6296 cmd->base.speed = SPEED_10000;
6297 break;
6300 return 0;
6302 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_link_ksettings);
6304 /* Callback to handle checksum offload command reply from OSA card.
6305 * Verify that required features have been enabled on the card.
6306 * Return error in hdr->return_code as this value is checked by caller.
6308 * Always returns zero to indicate no further messages from the OSA card.
6310 static int qeth_ipa_checksum_run_cmd_cb(struct qeth_card *card,
6311 struct qeth_reply *reply,
6312 unsigned long data)
6314 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
6315 struct qeth_checksum_cmd *chksum_cb =
6316 (struct qeth_checksum_cmd *)reply->param;
6318 QETH_CARD_TEXT(card, 4, "chkdoccb");
6319 if (qeth_setassparms_inspect_rc(cmd))
6320 return 0;
6322 memset(chksum_cb, 0, sizeof(*chksum_cb));
6323 if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_START) {
6324 chksum_cb->supported =
6325 cmd->data.setassparms.data.chksum.supported;
6326 QETH_CARD_TEXT_(card, 3, "strt:%x", chksum_cb->supported);
6328 if (cmd->data.setassparms.hdr.command_code == IPA_CMD_ASS_ENABLE) {
6329 chksum_cb->supported =
6330 cmd->data.setassparms.data.chksum.supported;
6331 chksum_cb->enabled =
6332 cmd->data.setassparms.data.chksum.enabled;
6333 QETH_CARD_TEXT_(card, 3, "supp:%x", chksum_cb->supported);
6334 QETH_CARD_TEXT_(card, 3, "enab:%x", chksum_cb->enabled);
6336 return 0;
6339 /* Send command to OSA card and check results. */
6340 static int qeth_ipa_checksum_run_cmd(struct qeth_card *card,
6341 enum qeth_ipa_funcs ipa_func,
6342 __u16 cmd_code, long data,
6343 struct qeth_checksum_cmd *chksum_cb)
6345 struct qeth_cmd_buffer *iob;
6346 int rc = -ENOMEM;
6348 QETH_CARD_TEXT(card, 4, "chkdocmd");
6349 iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
6350 sizeof(__u32), QETH_PROT_IPV4);
6351 if (iob)
6352 rc = qeth_send_setassparms(card, iob, sizeof(__u32), data,
6353 qeth_ipa_checksum_run_cmd_cb,
6354 chksum_cb);
6355 return rc;
6358 static int qeth_send_checksum_on(struct qeth_card *card, int cstype)
6360 const __u32 required_features = QETH_IPA_CHECKSUM_IP_HDR |
6361 QETH_IPA_CHECKSUM_UDP |
6362 QETH_IPA_CHECKSUM_TCP;
6363 struct qeth_checksum_cmd chksum_cb;
6364 int rc;
6366 rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_START, 0,
6367 &chksum_cb);
6368 if (!rc) {
6369 if ((required_features & chksum_cb.supported) !=
6370 required_features)
6371 rc = -EIO;
6372 else if (!(QETH_IPA_CHECKSUM_LP2LP & chksum_cb.supported) &&
6373 cstype == IPA_INBOUND_CHECKSUM)
6374 dev_warn(&card->gdev->dev,
6375 "Hardware checksumming is performed only if %s and its peer use different OSA Express 3 ports\n",
6376 QETH_CARD_IFNAME(card));
6378 if (rc) {
6379 qeth_send_simple_setassparms(card, cstype, IPA_CMD_ASS_STOP, 0);
6380 dev_warn(&card->gdev->dev,
6381 "Starting HW checksumming for %s failed, using SW checksumming\n",
6382 QETH_CARD_IFNAME(card));
6383 return rc;
6385 rc = qeth_ipa_checksum_run_cmd(card, cstype, IPA_CMD_ASS_ENABLE,
6386 chksum_cb.supported, &chksum_cb);
6387 if (!rc) {
6388 if ((required_features & chksum_cb.enabled) !=
6389 required_features)
6390 rc = -EIO;
6392 if (rc) {
6393 qeth_send_simple_setassparms(card, cstype, IPA_CMD_ASS_STOP, 0);
6394 dev_warn(&card->gdev->dev,
6395 "Enabling HW checksumming for %s failed, using SW checksumming\n",
6396 QETH_CARD_IFNAME(card));
6397 return rc;
6400 dev_info(&card->gdev->dev, "HW Checksumming (%sbound) enabled\n",
6401 cstype == IPA_INBOUND_CHECKSUM ? "in" : "out");
6402 return 0;
6405 static int qeth_set_ipa_csum(struct qeth_card *card, int on, int cstype)
6407 int rc = (on) ? qeth_send_checksum_on(card, cstype)
6408 : qeth_send_simple_setassparms(card, cstype,
6409 IPA_CMD_ASS_STOP, 0);
6410 return rc ? -EIO : 0;
6413 static int qeth_set_ipa_tso(struct qeth_card *card, int on)
6415 int rc;
6417 QETH_CARD_TEXT(card, 3, "sttso");
6419 if (on) {
6420 rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6421 IPA_CMD_ASS_START, 0);
6422 if (rc) {
6423 dev_warn(&card->gdev->dev,
6424 "Starting outbound TCP segmentation offload for %s failed\n",
6425 QETH_CARD_IFNAME(card));
6426 return -EIO;
6428 dev_info(&card->gdev->dev, "Outbound TSO enabled\n");
6429 } else {
6430 rc = qeth_send_simple_setassparms(card, IPA_OUTBOUND_TSO,
6431 IPA_CMD_ASS_STOP, 0);
6433 return rc;
6436 #define QETH_HW_FEATURES (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_TSO)
6439 * qeth_recover_features() - Restore device features after recovery
6440 * @dev: the recovering net_device
6442 * Caller must hold rtnl lock.
6444 void qeth_recover_features(struct net_device *dev)
6446 netdev_features_t features = dev->features;
6447 struct qeth_card *card = dev->ml_priv;
6449 /* force-off any feature that needs an IPA sequence.
6450 * netdev_update_features() will restart them.
6452 dev->features &= ~QETH_HW_FEATURES;
6453 netdev_update_features(dev);
6455 if (features == dev->features)
6456 return;
6457 dev_warn(&card->gdev->dev,
6458 "Device recovery failed to restore all offload features\n");
6460 EXPORT_SYMBOL_GPL(qeth_recover_features);
6462 int qeth_set_features(struct net_device *dev, netdev_features_t features)
6464 struct qeth_card *card = dev->ml_priv;
6465 netdev_features_t changed = dev->features ^ features;
6466 int rc = 0;
6468 QETH_DBF_TEXT(SETUP, 2, "setfeat");
6469 QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6471 if ((changed & NETIF_F_IP_CSUM)) {
6472 rc = qeth_set_ipa_csum(card,
6473 features & NETIF_F_IP_CSUM ? 1 : 0,
6474 IPA_OUTBOUND_CHECKSUM);
6475 if (rc)
6476 changed ^= NETIF_F_IP_CSUM;
6478 if ((changed & NETIF_F_RXCSUM)) {
6479 rc = qeth_set_ipa_csum(card,
6480 features & NETIF_F_RXCSUM ? 1 : 0,
6481 IPA_INBOUND_CHECKSUM);
6482 if (rc)
6483 changed ^= NETIF_F_RXCSUM;
6485 if ((changed & NETIF_F_TSO)) {
6486 rc = qeth_set_ipa_tso(card, features & NETIF_F_TSO ? 1 : 0);
6487 if (rc)
6488 changed ^= NETIF_F_TSO;
6491 /* everything changed successfully? */
6492 if ((dev->features ^ features) == changed)
6493 return 0;
6494 /* something went wrong. save changed features and return error */
6495 dev->features ^= changed;
6496 return -EIO;
6498 EXPORT_SYMBOL_GPL(qeth_set_features);
6500 netdev_features_t qeth_fix_features(struct net_device *dev,
6501 netdev_features_t features)
6503 struct qeth_card *card = dev->ml_priv;
6505 QETH_DBF_TEXT(SETUP, 2, "fixfeat");
6506 if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
6507 features &= ~NETIF_F_IP_CSUM;
6508 if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM))
6509 features &= ~NETIF_F_RXCSUM;
6510 if (!qeth_is_supported(card, IPA_OUTBOUND_TSO))
6511 features &= ~NETIF_F_TSO;
6512 /* if the card isn't up, remove features that require hw changes */
6513 if (card->state == CARD_STATE_DOWN ||
6514 card->state == CARD_STATE_RECOVER)
6515 features &= ~QETH_HW_FEATURES;
6516 QETH_DBF_HEX(SETUP, 2, &features, sizeof(features));
6517 return features;
6519 EXPORT_SYMBOL_GPL(qeth_fix_features);
6521 netdev_features_t qeth_features_check(struct sk_buff *skb,
6522 struct net_device *dev,
6523 netdev_features_t features)
6525 /* GSO segmentation builds skbs with
6526 * a (small) linear part for the headers, and
6527 * page frags for the data.
6528 * Compared to a linear skb, the header-only part consumes an
6529 * additional buffer element. This reduces buffer utilization, and
6530 * hurts throughput. So compress small segments into one element.
6532 if (netif_needs_gso(skb, features)) {
6533 /* match skb_segment(): */
6534 unsigned int doffset = skb->data - skb_mac_header(skb);
6535 unsigned int hsize = skb_shinfo(skb)->gso_size;
6536 unsigned int hroom = skb_headroom(skb);
6538 /* linearize only if resulting skb allocations are order-0: */
6539 if (SKB_DATA_ALIGN(hroom + doffset + hsize) <= SKB_MAX_HEAD(0))
6540 features &= ~NETIF_F_SG;
6543 return vlan_features_check(skb, features);
6545 EXPORT_SYMBOL_GPL(qeth_features_check);
6547 static int __init qeth_core_init(void)
6549 int rc;
6551 pr_info("loading core functions\n");
6552 INIT_LIST_HEAD(&qeth_core_card_list.list);
6553 INIT_LIST_HEAD(&qeth_dbf_list);
6554 rwlock_init(&qeth_core_card_list.rwlock);
6555 mutex_init(&qeth_mod_mutex);
6557 qeth_wq = create_singlethread_workqueue("qeth_wq");
6559 rc = qeth_register_dbf_views();
6560 if (rc)
6561 goto out_err;
6562 qeth_core_root_dev = root_device_register("qeth");
6563 rc = PTR_ERR_OR_ZERO(qeth_core_root_dev);
6564 if (rc)
6565 goto register_err;
6566 qeth_core_header_cache = kmem_cache_create("qeth_hdr",
6567 sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
6568 if (!qeth_core_header_cache) {
6569 rc = -ENOMEM;
6570 goto slab_err;
6572 qeth_qdio_outbuf_cache = kmem_cache_create("qeth_buf",
6573 sizeof(struct qeth_qdio_out_buffer), 0, 0, NULL);
6574 if (!qeth_qdio_outbuf_cache) {
6575 rc = -ENOMEM;
6576 goto cqslab_err;
6578 rc = ccw_driver_register(&qeth_ccw_driver);
6579 if (rc)
6580 goto ccw_err;
6581 qeth_core_ccwgroup_driver.driver.groups = qeth_drv_attr_groups;
6582 rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
6583 if (rc)
6584 goto ccwgroup_err;
6586 return 0;
6588 ccwgroup_err:
6589 ccw_driver_unregister(&qeth_ccw_driver);
6590 ccw_err:
6591 kmem_cache_destroy(qeth_qdio_outbuf_cache);
6592 cqslab_err:
6593 kmem_cache_destroy(qeth_core_header_cache);
6594 slab_err:
6595 root_device_unregister(qeth_core_root_dev);
6596 register_err:
6597 qeth_unregister_dbf_views();
6598 out_err:
6599 pr_err("Initializing the qeth device driver failed\n");
6600 return rc;
6603 static void __exit qeth_core_exit(void)
6605 qeth_clear_dbf_list();
6606 destroy_workqueue(qeth_wq);
6607 ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
6608 ccw_driver_unregister(&qeth_ccw_driver);
6609 kmem_cache_destroy(qeth_qdio_outbuf_cache);
6610 kmem_cache_destroy(qeth_core_header_cache);
6611 root_device_unregister(qeth_core_root_dev);
6612 qeth_unregister_dbf_views();
6613 pr_info("core functions removed\n");
6616 module_init(qeth_core_init);
6617 module_exit(qeth_core_exit);
6618 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
6619 MODULE_DESCRIPTION("qeth core functions");
6620 MODULE_LICENSE("GPL");