Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / s390 / net / qeth_core_main.c
blob3ba738b2e27117eb4c932143fffedce377919df1
1 /*
2 * drivers/s390/net/qeth_core_main.c
4 * Copyright IBM Corp. 2007, 2009
5 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
6 * Frank Pavlic <fpavlic@de.ibm.com>,
7 * Thomas Spatzier <tspat@de.ibm.com>,
8 * Frank Blaschka <frank.blaschka@de.ibm.com>
9 */
11 #define KMSG_COMPONENT "qeth"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/ip.h>
20 #include <linux/tcp.h>
21 #include <linux/mii.h>
22 #include <linux/kthread.h>
23 #include <linux/slab.h>
25 #include <asm/ebcdic.h>
26 #include <asm/io.h>
28 #include "qeth_core.h"
30 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
31 /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
32 /* N P A M L V H */
33 [QETH_DBF_SETUP] = {"qeth_setup",
34 8, 1, 8, 5, &debug_hex_ascii_view, NULL},
35 [QETH_DBF_QERR] = {"qeth_qerr",
36 2, 1, 8, 2, &debug_hex_ascii_view, NULL},
37 [QETH_DBF_TRACE] = {"qeth_trace",
38 4, 1, 8, 3, &debug_hex_ascii_view, NULL},
39 [QETH_DBF_MSG] = {"qeth_msg",
40 8, 1, 128, 3, &debug_sprintf_view, NULL},
41 [QETH_DBF_SENSE] = {"qeth_sense",
42 2, 1, 64, 2, &debug_hex_ascii_view, NULL},
43 [QETH_DBF_MISC] = {"qeth_misc",
44 2, 1, 256, 2, &debug_hex_ascii_view, NULL},
45 [QETH_DBF_CTRL] = {"qeth_control",
46 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
48 EXPORT_SYMBOL_GPL(qeth_dbf);
50 struct qeth_card_list_struct qeth_core_card_list;
51 EXPORT_SYMBOL_GPL(qeth_core_card_list);
52 struct kmem_cache *qeth_core_header_cache;
53 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
55 static struct device *qeth_core_root_dev;
56 static unsigned int known_devices[][10] = QETH_MODELLIST_ARRAY;
57 static struct lock_class_key qdio_out_skb_queue_key;
59 static void qeth_send_control_data_cb(struct qeth_channel *,
60 struct qeth_cmd_buffer *);
61 static int qeth_issue_next_read(struct qeth_card *);
62 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *);
63 static void qeth_setup_ccw(struct qeth_channel *, unsigned char *, __u32);
64 static void qeth_free_buffer_pool(struct qeth_card *);
65 static int qeth_qdio_establish(struct qeth_card *);
68 static inline void __qeth_fill_buffer_frag(struct sk_buff *skb,
69 struct qdio_buffer *buffer, int is_tso,
70 int *next_element_to_fill)
72 struct skb_frag_struct *frag;
73 int fragno;
74 unsigned long addr;
75 int element, cnt, dlen;
77 fragno = skb_shinfo(skb)->nr_frags;
78 element = *next_element_to_fill;
79 dlen = 0;
81 if (is_tso)
82 buffer->element[element].flags =
83 SBAL_FLAGS_MIDDLE_FRAG;
84 else
85 buffer->element[element].flags =
86 SBAL_FLAGS_FIRST_FRAG;
87 dlen = skb->len - skb->data_len;
88 if (dlen) {
89 buffer->element[element].addr = skb->data;
90 buffer->element[element].length = dlen;
91 element++;
93 for (cnt = 0; cnt < fragno; cnt++) {
94 frag = &skb_shinfo(skb)->frags[cnt];
95 addr = (page_to_pfn(frag->page) << PAGE_SHIFT) +
96 frag->page_offset;
97 buffer->element[element].addr = (char *)addr;
98 buffer->element[element].length = frag->size;
99 if (cnt < (fragno - 1))
100 buffer->element[element].flags =
101 SBAL_FLAGS_MIDDLE_FRAG;
102 else
103 buffer->element[element].flags =
104 SBAL_FLAGS_LAST_FRAG;
105 element++;
107 *next_element_to_fill = element;
110 static inline const char *qeth_get_cardname(struct qeth_card *card)
112 if (card->info.guestlan) {
113 switch (card->info.type) {
114 case QETH_CARD_TYPE_OSAE:
115 return " Guest LAN QDIO";
116 case QETH_CARD_TYPE_IQD:
117 return " Guest LAN Hiper";
118 default:
119 return " unknown";
121 } else {
122 switch (card->info.type) {
123 case QETH_CARD_TYPE_OSAE:
124 return " OSD Express";
125 case QETH_CARD_TYPE_IQD:
126 return " HiperSockets";
127 case QETH_CARD_TYPE_OSN:
128 return " OSN QDIO";
129 default:
130 return " unknown";
133 return " n/a";
136 /* max length to be returned: 14 */
137 const char *qeth_get_cardname_short(struct qeth_card *card)
139 if (card->info.guestlan) {
140 switch (card->info.type) {
141 case QETH_CARD_TYPE_OSAE:
142 return "GuestLAN QDIO";
143 case QETH_CARD_TYPE_IQD:
144 return "GuestLAN Hiper";
145 default:
146 return "unknown";
148 } else {
149 switch (card->info.type) {
150 case QETH_CARD_TYPE_OSAE:
151 switch (card->info.link_type) {
152 case QETH_LINK_TYPE_FAST_ETH:
153 return "OSD_100";
154 case QETH_LINK_TYPE_HSTR:
155 return "HSTR";
156 case QETH_LINK_TYPE_GBIT_ETH:
157 return "OSD_1000";
158 case QETH_LINK_TYPE_10GBIT_ETH:
159 return "OSD_10GIG";
160 case QETH_LINK_TYPE_LANE_ETH100:
161 return "OSD_FE_LANE";
162 case QETH_LINK_TYPE_LANE_TR:
163 return "OSD_TR_LANE";
164 case QETH_LINK_TYPE_LANE_ETH1000:
165 return "OSD_GbE_LANE";
166 case QETH_LINK_TYPE_LANE:
167 return "OSD_ATM_LANE";
168 default:
169 return "OSD_Express";
171 case QETH_CARD_TYPE_IQD:
172 return "HiperSockets";
173 case QETH_CARD_TYPE_OSN:
174 return "OSN";
175 default:
176 return "unknown";
179 return "n/a";
182 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
183 int clear_start_mask)
185 unsigned long flags;
187 spin_lock_irqsave(&card->thread_mask_lock, flags);
188 card->thread_allowed_mask = threads;
189 if (clear_start_mask)
190 card->thread_start_mask &= threads;
191 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
192 wake_up(&card->wait_q);
194 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
196 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
198 unsigned long flags;
199 int rc = 0;
201 spin_lock_irqsave(&card->thread_mask_lock, flags);
202 rc = (card->thread_running_mask & threads);
203 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
204 return rc;
206 EXPORT_SYMBOL_GPL(qeth_threads_running);
208 int qeth_wait_for_threads(struct qeth_card *card, unsigned long threads)
210 return wait_event_interruptible(card->wait_q,
211 qeth_threads_running(card, threads) == 0);
213 EXPORT_SYMBOL_GPL(qeth_wait_for_threads);
215 void qeth_clear_working_pool_list(struct qeth_card *card)
217 struct qeth_buffer_pool_entry *pool_entry, *tmp;
219 QETH_DBF_TEXT(TRACE, 5, "clwrklst");
220 list_for_each_entry_safe(pool_entry, tmp,
221 &card->qdio.in_buf_pool.entry_list, list){
222 list_del(&pool_entry->list);
225 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
227 static int qeth_alloc_buffer_pool(struct qeth_card *card)
229 struct qeth_buffer_pool_entry *pool_entry;
230 void *ptr;
231 int i, j;
233 QETH_DBF_TEXT(TRACE, 5, "alocpool");
234 for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
235 pool_entry = kmalloc(sizeof(*pool_entry), GFP_KERNEL);
236 if (!pool_entry) {
237 qeth_free_buffer_pool(card);
238 return -ENOMEM;
240 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
241 ptr = (void *) __get_free_page(GFP_KERNEL);
242 if (!ptr) {
243 while (j > 0)
244 free_page((unsigned long)
245 pool_entry->elements[--j]);
246 kfree(pool_entry);
247 qeth_free_buffer_pool(card);
248 return -ENOMEM;
250 pool_entry->elements[j] = ptr;
252 list_add(&pool_entry->init_list,
253 &card->qdio.init_pool.entry_list);
255 return 0;
258 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
260 QETH_DBF_TEXT(TRACE, 2, "realcbp");
262 if ((card->state != CARD_STATE_DOWN) &&
263 (card->state != CARD_STATE_RECOVER))
264 return -EPERM;
266 /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
267 qeth_clear_working_pool_list(card);
268 qeth_free_buffer_pool(card);
269 card->qdio.in_buf_pool.buf_count = bufcnt;
270 card->qdio.init_pool.buf_count = bufcnt;
271 return qeth_alloc_buffer_pool(card);
273 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
275 static int qeth_issue_next_read(struct qeth_card *card)
277 int rc;
278 struct qeth_cmd_buffer *iob;
280 QETH_DBF_TEXT(TRACE, 5, "issnxrd");
281 if (card->read.state != CH_STATE_UP)
282 return -EIO;
283 iob = qeth_get_buffer(&card->read);
284 if (!iob) {
285 dev_warn(&card->gdev->dev, "The qeth device driver "
286 "failed to recover an error on the device\n");
287 QETH_DBF_MESSAGE(2, "%s issue_next_read failed: no iob "
288 "available\n", dev_name(&card->gdev->dev));
289 return -ENOMEM;
291 qeth_setup_ccw(&card->read, iob->data, QETH_BUFSIZE);
292 QETH_DBF_TEXT(TRACE, 6, "noirqpnd");
293 rc = ccw_device_start(card->read.ccwdev, &card->read.ccw,
294 (addr_t) iob, 0, 0);
295 if (rc) {
296 QETH_DBF_MESSAGE(2, "%s error in starting next read ccw! "
297 "rc=%i\n", dev_name(&card->gdev->dev), rc);
298 atomic_set(&card->read.irq_pending, 0);
299 qeth_schedule_recovery(card);
300 wake_up(&card->wait_q);
302 return rc;
305 static struct qeth_reply *qeth_alloc_reply(struct qeth_card *card)
307 struct qeth_reply *reply;
309 reply = kzalloc(sizeof(struct qeth_reply), GFP_ATOMIC);
310 if (reply) {
311 atomic_set(&reply->refcnt, 1);
312 atomic_set(&reply->received, 0);
313 reply->card = card;
315 return reply;
318 static void qeth_get_reply(struct qeth_reply *reply)
320 WARN_ON(atomic_read(&reply->refcnt) <= 0);
321 atomic_inc(&reply->refcnt);
324 static void qeth_put_reply(struct qeth_reply *reply)
326 WARN_ON(atomic_read(&reply->refcnt) <= 0);
327 if (atomic_dec_and_test(&reply->refcnt))
328 kfree(reply);
331 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
332 struct qeth_card *card)
334 char *ipa_name;
335 int com = cmd->hdr.command;
336 ipa_name = qeth_get_ipa_cmd_name(com);
337 if (rc)
338 QETH_DBF_MESSAGE(2, "IPA: %s(x%X) for %s returned x%X \"%s\"\n",
339 ipa_name, com, QETH_CARD_IFNAME(card),
340 rc, qeth_get_ipa_msg(rc));
341 else
342 QETH_DBF_MESSAGE(5, "IPA: %s(x%X) for %s succeeded\n",
343 ipa_name, com, QETH_CARD_IFNAME(card));
346 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
347 struct qeth_cmd_buffer *iob)
349 struct qeth_ipa_cmd *cmd = NULL;
351 QETH_DBF_TEXT(TRACE, 5, "chkipad");
352 if (IS_IPA(iob->data)) {
353 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
354 if (IS_IPA_REPLY(cmd)) {
355 if (cmd->hdr.command != IPA_CMD_SETCCID &&
356 cmd->hdr.command != IPA_CMD_DELCCID &&
357 cmd->hdr.command != IPA_CMD_MODCCID &&
358 cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
359 qeth_issue_ipa_msg(cmd,
360 cmd->hdr.return_code, card);
361 return cmd;
362 } else {
363 switch (cmd->hdr.command) {
364 case IPA_CMD_STOPLAN:
365 dev_warn(&card->gdev->dev,
366 "The link for interface %s on CHPID"
367 " 0x%X failed\n",
368 QETH_CARD_IFNAME(card),
369 card->info.chpid);
370 card->lan_online = 0;
371 if (card->dev && netif_carrier_ok(card->dev))
372 netif_carrier_off(card->dev);
373 return NULL;
374 case IPA_CMD_STARTLAN:
375 dev_info(&card->gdev->dev,
376 "The link for %s on CHPID 0x%X has"
377 " been restored\n",
378 QETH_CARD_IFNAME(card),
379 card->info.chpid);
380 netif_carrier_on(card->dev);
381 card->lan_online = 1;
382 qeth_schedule_recovery(card);
383 return NULL;
384 case IPA_CMD_MODCCID:
385 return cmd;
386 case IPA_CMD_REGISTER_LOCAL_ADDR:
387 QETH_DBF_TEXT(TRACE, 3, "irla");
388 break;
389 case IPA_CMD_UNREGISTER_LOCAL_ADDR:
390 QETH_DBF_TEXT(TRACE, 3, "urla");
391 break;
392 default:
393 QETH_DBF_MESSAGE(2, "Received data is IPA "
394 "but not a reply!\n");
395 break;
399 return cmd;
402 void qeth_clear_ipacmd_list(struct qeth_card *card)
404 struct qeth_reply *reply, *r;
405 unsigned long flags;
407 QETH_DBF_TEXT(TRACE, 4, "clipalst");
409 spin_lock_irqsave(&card->lock, flags);
410 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
411 qeth_get_reply(reply);
412 reply->rc = -EIO;
413 atomic_inc(&reply->received);
414 list_del_init(&reply->list);
415 wake_up(&reply->wait_q);
416 qeth_put_reply(reply);
418 spin_unlock_irqrestore(&card->lock, flags);
420 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
422 static int qeth_check_idx_response(unsigned char *buffer)
424 if (!buffer)
425 return 0;
427 QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
428 if ((buffer[2] & 0xc0) == 0xc0) {
429 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE "
430 "with cause code 0x%02x%s\n",
431 buffer[4],
432 ((buffer[4] == 0x22) ?
433 " -- try another portname" : ""));
434 QETH_DBF_TEXT(TRACE, 2, "ckidxres");
435 QETH_DBF_TEXT(TRACE, 2, " idxterm");
436 QETH_DBF_TEXT_(TRACE, 2, " rc%d", -EIO);
437 return -EIO;
439 return 0;
442 static void qeth_setup_ccw(struct qeth_channel *channel, unsigned char *iob,
443 __u32 len)
445 struct qeth_card *card;
447 QETH_DBF_TEXT(TRACE, 4, "setupccw");
448 card = CARD_FROM_CDEV(channel->ccwdev);
449 if (channel == &card->read)
450 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
451 else
452 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
453 channel->ccw.count = len;
454 channel->ccw.cda = (__u32) __pa(iob);
457 static struct qeth_cmd_buffer *__qeth_get_buffer(struct qeth_channel *channel)
459 __u8 index;
461 QETH_DBF_TEXT(TRACE, 6, "getbuff");
462 index = channel->io_buf_no;
463 do {
464 if (channel->iob[index].state == BUF_STATE_FREE) {
465 channel->iob[index].state = BUF_STATE_LOCKED;
466 channel->io_buf_no = (channel->io_buf_no + 1) %
467 QETH_CMD_BUFFER_NO;
468 memset(channel->iob[index].data, 0, QETH_BUFSIZE);
469 return channel->iob + index;
471 index = (index + 1) % QETH_CMD_BUFFER_NO;
472 } while (index != channel->io_buf_no);
474 return NULL;
477 void qeth_release_buffer(struct qeth_channel *channel,
478 struct qeth_cmd_buffer *iob)
480 unsigned long flags;
482 QETH_DBF_TEXT(TRACE, 6, "relbuff");
483 spin_lock_irqsave(&channel->iob_lock, flags);
484 memset(iob->data, 0, QETH_BUFSIZE);
485 iob->state = BUF_STATE_FREE;
486 iob->callback = qeth_send_control_data_cb;
487 iob->rc = 0;
488 spin_unlock_irqrestore(&channel->iob_lock, flags);
490 EXPORT_SYMBOL_GPL(qeth_release_buffer);
492 static struct qeth_cmd_buffer *qeth_get_buffer(struct qeth_channel *channel)
494 struct qeth_cmd_buffer *buffer = NULL;
495 unsigned long flags;
497 spin_lock_irqsave(&channel->iob_lock, flags);
498 buffer = __qeth_get_buffer(channel);
499 spin_unlock_irqrestore(&channel->iob_lock, flags);
500 return buffer;
503 struct qeth_cmd_buffer *qeth_wait_for_buffer(struct qeth_channel *channel)
505 struct qeth_cmd_buffer *buffer;
506 wait_event(channel->wait_q,
507 ((buffer = qeth_get_buffer(channel)) != NULL));
508 return buffer;
510 EXPORT_SYMBOL_GPL(qeth_wait_for_buffer);
512 void qeth_clear_cmd_buffers(struct qeth_channel *channel)
514 int cnt;
516 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
517 qeth_release_buffer(channel, &channel->iob[cnt]);
518 channel->buf_no = 0;
519 channel->io_buf_no = 0;
521 EXPORT_SYMBOL_GPL(qeth_clear_cmd_buffers);
523 static void qeth_send_control_data_cb(struct qeth_channel *channel,
524 struct qeth_cmd_buffer *iob)
526 struct qeth_card *card;
527 struct qeth_reply *reply, *r;
528 struct qeth_ipa_cmd *cmd;
529 unsigned long flags;
530 int keep_reply;
532 QETH_DBF_TEXT(TRACE, 4, "sndctlcb");
534 card = CARD_FROM_CDEV(channel->ccwdev);
535 if (qeth_check_idx_response(iob->data)) {
536 qeth_clear_ipacmd_list(card);
537 if (((iob->data[2] & 0xc0) == 0xc0) && iob->data[4] == 0xf6)
538 dev_err(&card->gdev->dev,
539 "The qeth device is not configured "
540 "for the OSI layer required by z/VM\n");
541 else
542 qeth_schedule_recovery(card);
543 goto out;
546 cmd = qeth_check_ipa_data(card, iob);
547 if ((cmd == NULL) && (card->state != CARD_STATE_DOWN))
548 goto out;
549 /*in case of OSN : check if cmd is set */
550 if (card->info.type == QETH_CARD_TYPE_OSN &&
551 cmd &&
552 cmd->hdr.command != IPA_CMD_STARTLAN &&
553 card->osn_info.assist_cb != NULL) {
554 card->osn_info.assist_cb(card->dev, cmd);
555 goto out;
558 spin_lock_irqsave(&card->lock, flags);
559 list_for_each_entry_safe(reply, r, &card->cmd_waiter_list, list) {
560 if ((reply->seqno == QETH_IDX_COMMAND_SEQNO) ||
561 ((cmd) && (reply->seqno == cmd->hdr.seqno))) {
562 qeth_get_reply(reply);
563 list_del_init(&reply->list);
564 spin_unlock_irqrestore(&card->lock, flags);
565 keep_reply = 0;
566 if (reply->callback != NULL) {
567 if (cmd) {
568 reply->offset = (__u16)((char *)cmd -
569 (char *)iob->data);
570 keep_reply = reply->callback(card,
571 reply,
572 (unsigned long)cmd);
573 } else
574 keep_reply = reply->callback(card,
575 reply,
576 (unsigned long)iob);
578 if (cmd)
579 reply->rc = (u16) cmd->hdr.return_code;
580 else if (iob->rc)
581 reply->rc = iob->rc;
582 if (keep_reply) {
583 spin_lock_irqsave(&card->lock, flags);
584 list_add_tail(&reply->list,
585 &card->cmd_waiter_list);
586 spin_unlock_irqrestore(&card->lock, flags);
587 } else {
588 atomic_inc(&reply->received);
589 wake_up(&reply->wait_q);
591 qeth_put_reply(reply);
592 goto out;
595 spin_unlock_irqrestore(&card->lock, flags);
596 out:
597 memcpy(&card->seqno.pdu_hdr_ack,
598 QETH_PDU_HEADER_SEQ_NO(iob->data),
599 QETH_SEQ_NO_LENGTH);
600 qeth_release_buffer(channel, iob);
603 static int qeth_setup_channel(struct qeth_channel *channel)
605 int cnt;
607 QETH_DBF_TEXT(SETUP, 2, "setupch");
608 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++) {
609 channel->iob[cnt].data = (char *)
610 kmalloc(QETH_BUFSIZE, GFP_DMA|GFP_KERNEL);
611 if (channel->iob[cnt].data == NULL)
612 break;
613 channel->iob[cnt].state = BUF_STATE_FREE;
614 channel->iob[cnt].channel = channel;
615 channel->iob[cnt].callback = qeth_send_control_data_cb;
616 channel->iob[cnt].rc = 0;
618 if (cnt < QETH_CMD_BUFFER_NO) {
619 while (cnt-- > 0)
620 kfree(channel->iob[cnt].data);
621 return -ENOMEM;
623 channel->buf_no = 0;
624 channel->io_buf_no = 0;
625 atomic_set(&channel->irq_pending, 0);
626 spin_lock_init(&channel->iob_lock);
628 init_waitqueue_head(&channel->wait_q);
629 return 0;
632 static int qeth_set_thread_start_bit(struct qeth_card *card,
633 unsigned long thread)
635 unsigned long flags;
637 spin_lock_irqsave(&card->thread_mask_lock, flags);
638 if (!(card->thread_allowed_mask & thread) ||
639 (card->thread_start_mask & thread)) {
640 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
641 return -EPERM;
643 card->thread_start_mask |= thread;
644 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
645 return 0;
648 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
650 unsigned long flags;
652 spin_lock_irqsave(&card->thread_mask_lock, flags);
653 card->thread_start_mask &= ~thread;
654 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
655 wake_up(&card->wait_q);
657 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
659 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
661 unsigned long flags;
663 spin_lock_irqsave(&card->thread_mask_lock, flags);
664 card->thread_running_mask &= ~thread;
665 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
666 wake_up(&card->wait_q);
668 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
670 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
672 unsigned long flags;
673 int rc = 0;
675 spin_lock_irqsave(&card->thread_mask_lock, flags);
676 if (card->thread_start_mask & thread) {
677 if ((card->thread_allowed_mask & thread) &&
678 !(card->thread_running_mask & thread)) {
679 rc = 1;
680 card->thread_start_mask &= ~thread;
681 card->thread_running_mask |= thread;
682 } else
683 rc = -EPERM;
685 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
686 return rc;
689 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
691 int rc = 0;
693 wait_event(card->wait_q,
694 (rc = __qeth_do_run_thread(card, thread)) >= 0);
695 return rc;
697 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
699 void qeth_schedule_recovery(struct qeth_card *card)
701 QETH_DBF_TEXT(TRACE, 2, "startrec");
702 if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
703 schedule_work(&card->kernel_thread_starter);
705 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
707 static int qeth_get_problem(struct ccw_device *cdev, struct irb *irb)
709 int dstat, cstat;
710 char *sense;
712 sense = (char *) irb->ecw;
713 cstat = irb->scsw.cmd.cstat;
714 dstat = irb->scsw.cmd.dstat;
716 if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
717 SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
718 SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
719 QETH_DBF_TEXT(TRACE, 2, "CGENCHK");
720 dev_warn(&cdev->dev, "The qeth device driver "
721 "failed to recover an error on the device\n");
722 QETH_DBF_MESSAGE(2, "%s check on device dstat=x%x, cstat=x%x ",
723 dev_name(&cdev->dev), dstat, cstat);
724 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
725 16, 1, irb, 64, 1);
726 return 1;
729 if (dstat & DEV_STAT_UNIT_CHECK) {
730 if (sense[SENSE_RESETTING_EVENT_BYTE] &
731 SENSE_RESETTING_EVENT_FLAG) {
732 QETH_DBF_TEXT(TRACE, 2, "REVIND");
733 return 1;
735 if (sense[SENSE_COMMAND_REJECT_BYTE] &
736 SENSE_COMMAND_REJECT_FLAG) {
737 QETH_DBF_TEXT(TRACE, 2, "CMDREJi");
738 return 1;
740 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
741 QETH_DBF_TEXT(TRACE, 2, "AFFE");
742 return 1;
744 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
745 QETH_DBF_TEXT(TRACE, 2, "ZEROSEN");
746 return 0;
748 QETH_DBF_TEXT(TRACE, 2, "DGENCHK");
749 return 1;
751 return 0;
754 static long __qeth_check_irb_error(struct ccw_device *cdev,
755 unsigned long intparm, struct irb *irb)
757 if (!IS_ERR(irb))
758 return 0;
760 switch (PTR_ERR(irb)) {
761 case -EIO:
762 QETH_DBF_MESSAGE(2, "%s i/o-error on device\n",
763 dev_name(&cdev->dev));
764 QETH_DBF_TEXT(TRACE, 2, "ckirberr");
765 QETH_DBF_TEXT_(TRACE, 2, " rc%d", -EIO);
766 break;
767 case -ETIMEDOUT:
768 dev_warn(&cdev->dev, "A hardware operation timed out"
769 " on the device\n");
770 QETH_DBF_TEXT(TRACE, 2, "ckirberr");
771 QETH_DBF_TEXT_(TRACE, 2, " rc%d", -ETIMEDOUT);
772 if (intparm == QETH_RCD_PARM) {
773 struct qeth_card *card = CARD_FROM_CDEV(cdev);
775 if (card && (card->data.ccwdev == cdev)) {
776 card->data.state = CH_STATE_DOWN;
777 wake_up(&card->wait_q);
780 break;
781 default:
782 QETH_DBF_MESSAGE(2, "%s unknown error %ld on device\n",
783 dev_name(&cdev->dev), PTR_ERR(irb));
784 QETH_DBF_TEXT(TRACE, 2, "ckirberr");
785 QETH_DBF_TEXT(TRACE, 2, " rc???");
787 return PTR_ERR(irb);
790 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
791 struct irb *irb)
793 int rc;
794 int cstat, dstat;
795 struct qeth_cmd_buffer *buffer;
796 struct qeth_channel *channel;
797 struct qeth_card *card;
798 struct qeth_cmd_buffer *iob;
799 __u8 index;
801 QETH_DBF_TEXT(TRACE, 5, "irq");
803 if (__qeth_check_irb_error(cdev, intparm, irb))
804 return;
805 cstat = irb->scsw.cmd.cstat;
806 dstat = irb->scsw.cmd.dstat;
808 card = CARD_FROM_CDEV(cdev);
809 if (!card)
810 return;
812 if (card->read.ccwdev == cdev) {
813 channel = &card->read;
814 QETH_DBF_TEXT(TRACE, 5, "read");
815 } else if (card->write.ccwdev == cdev) {
816 channel = &card->write;
817 QETH_DBF_TEXT(TRACE, 5, "write");
818 } else {
819 channel = &card->data;
820 QETH_DBF_TEXT(TRACE, 5, "data");
822 atomic_set(&channel->irq_pending, 0);
824 if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
825 channel->state = CH_STATE_STOPPED;
827 if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
828 channel->state = CH_STATE_HALTED;
830 /*let's wake up immediately on data channel*/
831 if ((channel == &card->data) && (intparm != 0) &&
832 (intparm != QETH_RCD_PARM))
833 goto out;
835 if (intparm == QETH_CLEAR_CHANNEL_PARM) {
836 QETH_DBF_TEXT(TRACE, 6, "clrchpar");
837 /* we don't have to handle this further */
838 intparm = 0;
840 if (intparm == QETH_HALT_CHANNEL_PARM) {
841 QETH_DBF_TEXT(TRACE, 6, "hltchpar");
842 /* we don't have to handle this further */
843 intparm = 0;
845 if ((dstat & DEV_STAT_UNIT_EXCEP) ||
846 (dstat & DEV_STAT_UNIT_CHECK) ||
847 (cstat)) {
848 if (irb->esw.esw0.erw.cons) {
849 dev_warn(&channel->ccwdev->dev,
850 "The qeth device driver failed to recover "
851 "an error on the device\n");
852 QETH_DBF_MESSAGE(2, "%s sense data available. cstat "
853 "0x%X dstat 0x%X\n",
854 dev_name(&channel->ccwdev->dev), cstat, dstat);
855 print_hex_dump(KERN_WARNING, "qeth: irb ",
856 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
857 print_hex_dump(KERN_WARNING, "qeth: sense data ",
858 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
860 if (intparm == QETH_RCD_PARM) {
861 channel->state = CH_STATE_DOWN;
862 goto out;
864 rc = qeth_get_problem(cdev, irb);
865 if (rc) {
866 qeth_clear_ipacmd_list(card);
867 qeth_schedule_recovery(card);
868 goto out;
872 if (intparm == QETH_RCD_PARM) {
873 channel->state = CH_STATE_RCD_DONE;
874 goto out;
876 if (intparm) {
877 buffer = (struct qeth_cmd_buffer *) __va((addr_t)intparm);
878 buffer->state = BUF_STATE_PROCESSED;
880 if (channel == &card->data)
881 return;
882 if (channel == &card->read &&
883 channel->state == CH_STATE_UP)
884 qeth_issue_next_read(card);
886 iob = channel->iob;
887 index = channel->buf_no;
888 while (iob[index].state == BUF_STATE_PROCESSED) {
889 if (iob[index].callback != NULL)
890 iob[index].callback(channel, iob + index);
892 index = (index + 1) % QETH_CMD_BUFFER_NO;
894 channel->buf_no = index;
895 out:
896 wake_up(&card->wait_q);
897 return;
900 static void __qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
901 struct qeth_qdio_out_buffer *buf, unsigned int qeth_skip_skb)
903 int i;
904 struct sk_buff *skb;
906 /* is PCI flag set on buffer? */
907 if (buf->buffer->element[0].flags & 0x40)
908 atomic_dec(&queue->set_pci_flags_count);
910 if (!qeth_skip_skb) {
911 skb = skb_dequeue(&buf->skb_list);
912 while (skb) {
913 atomic_dec(&skb->users);
914 dev_kfree_skb_any(skb);
915 skb = skb_dequeue(&buf->skb_list);
918 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(queue->card); ++i) {
919 if (buf->buffer->element[i].addr && buf->is_header[i])
920 kmem_cache_free(qeth_core_header_cache,
921 buf->buffer->element[i].addr);
922 buf->is_header[i] = 0;
923 buf->buffer->element[i].length = 0;
924 buf->buffer->element[i].addr = NULL;
925 buf->buffer->element[i].flags = 0;
927 buf->buffer->element[15].flags = 0;
928 buf->next_element_to_fill = 0;
929 atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
932 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
933 struct qeth_qdio_out_buffer *buf)
935 __qeth_clear_output_buffer(queue, buf, 0);
938 void qeth_clear_qdio_buffers(struct qeth_card *card)
940 int i, j;
942 QETH_DBF_TEXT(TRACE, 2, "clearqdbf");
943 /* clear outbound buffers to free skbs */
944 for (i = 0; i < card->qdio.no_out_queues; ++i)
945 if (card->qdio.out_qs[i]) {
946 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
947 qeth_clear_output_buffer(card->qdio.out_qs[i],
948 &card->qdio.out_qs[i]->bufs[j]);
951 EXPORT_SYMBOL_GPL(qeth_clear_qdio_buffers);
953 static void qeth_free_buffer_pool(struct qeth_card *card)
955 struct qeth_buffer_pool_entry *pool_entry, *tmp;
956 int i = 0;
957 QETH_DBF_TEXT(TRACE, 5, "freepool");
958 list_for_each_entry_safe(pool_entry, tmp,
959 &card->qdio.init_pool.entry_list, init_list){
960 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
961 free_page((unsigned long)pool_entry->elements[i]);
962 list_del(&pool_entry->init_list);
963 kfree(pool_entry);
967 static void qeth_free_qdio_buffers(struct qeth_card *card)
969 int i, j;
971 QETH_DBF_TEXT(TRACE, 2, "freeqdbf");
972 if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
973 QETH_QDIO_UNINITIALIZED)
974 return;
975 kfree(card->qdio.in_q);
976 card->qdio.in_q = NULL;
977 /* inbound buffer pool */
978 qeth_free_buffer_pool(card);
979 /* free outbound qdio_qs */
980 if (card->qdio.out_qs) {
981 for (i = 0; i < card->qdio.no_out_queues; ++i) {
982 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j)
983 qeth_clear_output_buffer(card->qdio.out_qs[i],
984 &card->qdio.out_qs[i]->bufs[j]);
985 kfree(card->qdio.out_qs[i]);
987 kfree(card->qdio.out_qs);
988 card->qdio.out_qs = NULL;
992 static void qeth_clean_channel(struct qeth_channel *channel)
994 int cnt;
996 QETH_DBF_TEXT(SETUP, 2, "freech");
997 for (cnt = 0; cnt < QETH_CMD_BUFFER_NO; cnt++)
998 kfree(channel->iob[cnt].data);
1001 static int qeth_is_1920_device(struct qeth_card *card)
1003 int single_queue = 0;
1004 struct ccw_device *ccwdev;
1005 struct channelPath_dsc {
1006 u8 flags;
1007 u8 lsn;
1008 u8 desc;
1009 u8 chpid;
1010 u8 swla;
1011 u8 zeroes;
1012 u8 chla;
1013 u8 chpp;
1014 } *chp_dsc;
1016 QETH_DBF_TEXT(SETUP, 2, "chk_1920");
1018 ccwdev = card->data.ccwdev;
1019 chp_dsc = (struct channelPath_dsc *)ccw_device_get_chp_desc(ccwdev, 0);
1020 if (chp_dsc != NULL) {
1021 /* CHPP field bit 6 == 1 -> single queue */
1022 single_queue = ((chp_dsc->chpp & 0x02) == 0x02);
1023 kfree(chp_dsc);
1025 QETH_DBF_TEXT_(SETUP, 2, "rc:%x", single_queue);
1026 return single_queue;
1029 static void qeth_init_qdio_info(struct qeth_card *card)
1031 QETH_DBF_TEXT(SETUP, 4, "intqdinf");
1032 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1033 /* inbound */
1034 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1035 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1036 card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1037 INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1038 INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1041 static void qeth_set_intial_options(struct qeth_card *card)
1043 card->options.route4.type = NO_ROUTER;
1044 card->options.route6.type = NO_ROUTER;
1045 card->options.checksum_type = QETH_CHECKSUM_DEFAULT;
1046 card->options.broadcast_mode = QETH_TR_BROADCAST_ALLRINGS;
1047 card->options.macaddr_mode = QETH_TR_MACADDR_NONCANONICAL;
1048 card->options.fake_broadcast = 0;
1049 card->options.add_hhlen = DEFAULT_ADD_HHLEN;
1050 card->options.performance_stats = 0;
1051 card->options.rx_sg_cb = QETH_RX_SG_CB;
1052 card->options.isolation = ISOLATION_MODE_NONE;
1055 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1057 unsigned long flags;
1058 int rc = 0;
1060 spin_lock_irqsave(&card->thread_mask_lock, flags);
1061 QETH_DBF_TEXT_(TRACE, 4, " %02x%02x%02x",
1062 (u8) card->thread_start_mask,
1063 (u8) card->thread_allowed_mask,
1064 (u8) card->thread_running_mask);
1065 rc = (card->thread_start_mask & thread);
1066 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1067 return rc;
1070 static void qeth_start_kernel_thread(struct work_struct *work)
1072 struct qeth_card *card = container_of(work, struct qeth_card,
1073 kernel_thread_starter);
1074 QETH_DBF_TEXT(TRACE , 2, "strthrd");
1076 if (card->read.state != CH_STATE_UP &&
1077 card->write.state != CH_STATE_UP)
1078 return;
1079 if (qeth_do_start_thread(card, QETH_RECOVER_THREAD))
1080 kthread_run(card->discipline.recover, (void *) card,
1081 "qeth_recover");
1084 static int qeth_setup_card(struct qeth_card *card)
1087 QETH_DBF_TEXT(SETUP, 2, "setupcrd");
1088 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1090 card->read.state = CH_STATE_DOWN;
1091 card->write.state = CH_STATE_DOWN;
1092 card->data.state = CH_STATE_DOWN;
1093 card->state = CARD_STATE_DOWN;
1094 card->lan_online = 0;
1095 card->use_hard_stop = 0;
1096 card->dev = NULL;
1097 spin_lock_init(&card->vlanlock);
1098 spin_lock_init(&card->mclock);
1099 card->vlangrp = NULL;
1100 spin_lock_init(&card->lock);
1101 spin_lock_init(&card->ip_lock);
1102 spin_lock_init(&card->thread_mask_lock);
1103 card->thread_start_mask = 0;
1104 card->thread_allowed_mask = 0;
1105 card->thread_running_mask = 0;
1106 INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1107 INIT_LIST_HEAD(&card->ip_list);
1108 INIT_LIST_HEAD(card->ip_tbd_list);
1109 INIT_LIST_HEAD(&card->cmd_waiter_list);
1110 init_waitqueue_head(&card->wait_q);
1111 /* intial options */
1112 qeth_set_intial_options(card);
1113 /* IP address takeover */
1114 INIT_LIST_HEAD(&card->ipato.entries);
1115 card->ipato.enabled = 0;
1116 card->ipato.invert4 = 0;
1117 card->ipato.invert6 = 0;
1118 /* init QDIO stuff */
1119 qeth_init_qdio_info(card);
1120 return 0;
1123 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1125 struct qeth_card *card = container_of(slr, struct qeth_card,
1126 qeth_service_level);
1127 if (card->info.mcl_level[0])
1128 seq_printf(m, "qeth: %s firmware level %s\n",
1129 CARD_BUS_ID(card), card->info.mcl_level);
1132 static struct qeth_card *qeth_alloc_card(void)
1134 struct qeth_card *card;
1136 QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1137 card = kzalloc(sizeof(struct qeth_card), GFP_DMA|GFP_KERNEL);
1138 if (!card)
1139 goto out;
1140 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1141 card->ip_tbd_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1142 if (!card->ip_tbd_list) {
1143 QETH_DBF_TEXT(SETUP, 0, "iptbdnom");
1144 goto out_card;
1146 if (qeth_setup_channel(&card->read))
1147 goto out_ip;
1148 if (qeth_setup_channel(&card->write))
1149 goto out_channel;
1150 card->options.layer2 = -1;
1151 card->qeth_service_level.seq_print = qeth_core_sl_print;
1152 register_service_level(&card->qeth_service_level);
1153 return card;
1155 out_channel:
1156 qeth_clean_channel(&card->read);
1157 out_ip:
1158 kfree(card->ip_tbd_list);
1159 out_card:
1160 kfree(card);
1161 out:
1162 return NULL;
1165 static int qeth_determine_card_type(struct qeth_card *card)
1167 int i = 0;
1169 QETH_DBF_TEXT(SETUP, 2, "detcdtyp");
1171 card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1172 card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1173 while (known_devices[i][4]) {
1174 if ((CARD_RDEV(card)->id.dev_type == known_devices[i][2]) &&
1175 (CARD_RDEV(card)->id.dev_model == known_devices[i][3])) {
1176 card->info.type = known_devices[i][4];
1177 card->qdio.no_out_queues = known_devices[i][8];
1178 card->info.is_multicast_different = known_devices[i][9];
1179 if (qeth_is_1920_device(card)) {
1180 dev_info(&card->gdev->dev,
1181 "Priority Queueing not supported\n");
1182 card->qdio.no_out_queues = 1;
1183 card->qdio.default_out_queue = 0;
1185 return 0;
1187 i++;
1189 card->info.type = QETH_CARD_TYPE_UNKNOWN;
1190 dev_err(&card->gdev->dev, "The adapter hardware is of an "
1191 "unknown type\n");
1192 return -ENOENT;
1195 static int qeth_clear_channel(struct qeth_channel *channel)
1197 unsigned long flags;
1198 struct qeth_card *card;
1199 int rc;
1201 QETH_DBF_TEXT(TRACE, 3, "clearch");
1202 card = CARD_FROM_CDEV(channel->ccwdev);
1203 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1204 rc = ccw_device_clear(channel->ccwdev, QETH_CLEAR_CHANNEL_PARM);
1205 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1207 if (rc)
1208 return rc;
1209 rc = wait_event_interruptible_timeout(card->wait_q,
1210 channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1211 if (rc == -ERESTARTSYS)
1212 return rc;
1213 if (channel->state != CH_STATE_STOPPED)
1214 return -ETIME;
1215 channel->state = CH_STATE_DOWN;
1216 return 0;
1219 static int qeth_halt_channel(struct qeth_channel *channel)
1221 unsigned long flags;
1222 struct qeth_card *card;
1223 int rc;
1225 QETH_DBF_TEXT(TRACE, 3, "haltch");
1226 card = CARD_FROM_CDEV(channel->ccwdev);
1227 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1228 rc = ccw_device_halt(channel->ccwdev, QETH_HALT_CHANNEL_PARM);
1229 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1231 if (rc)
1232 return rc;
1233 rc = wait_event_interruptible_timeout(card->wait_q,
1234 channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1235 if (rc == -ERESTARTSYS)
1236 return rc;
1237 if (channel->state != CH_STATE_HALTED)
1238 return -ETIME;
1239 return 0;
1242 static int qeth_halt_channels(struct qeth_card *card)
1244 int rc1 = 0, rc2 = 0, rc3 = 0;
1246 QETH_DBF_TEXT(TRACE, 3, "haltchs");
1247 rc1 = qeth_halt_channel(&card->read);
1248 rc2 = qeth_halt_channel(&card->write);
1249 rc3 = qeth_halt_channel(&card->data);
1250 if (rc1)
1251 return rc1;
1252 if (rc2)
1253 return rc2;
1254 return rc3;
1257 static int qeth_clear_channels(struct qeth_card *card)
1259 int rc1 = 0, rc2 = 0, rc3 = 0;
1261 QETH_DBF_TEXT(TRACE, 3, "clearchs");
1262 rc1 = qeth_clear_channel(&card->read);
1263 rc2 = qeth_clear_channel(&card->write);
1264 rc3 = qeth_clear_channel(&card->data);
1265 if (rc1)
1266 return rc1;
1267 if (rc2)
1268 return rc2;
1269 return rc3;
1272 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1274 int rc = 0;
1276 QETH_DBF_TEXT(TRACE, 3, "clhacrd");
1277 QETH_DBF_HEX(TRACE, 3, &card, sizeof(void *));
1279 if (halt)
1280 rc = qeth_halt_channels(card);
1281 if (rc)
1282 return rc;
1283 return qeth_clear_channels(card);
1286 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1288 int rc = 0;
1290 QETH_DBF_TEXT(TRACE, 3, "qdioclr");
1291 switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1292 QETH_QDIO_CLEANING)) {
1293 case QETH_QDIO_ESTABLISHED:
1294 if (card->info.type == QETH_CARD_TYPE_IQD)
1295 rc = qdio_cleanup(CARD_DDEV(card),
1296 QDIO_FLAG_CLEANUP_USING_HALT);
1297 else
1298 rc = qdio_cleanup(CARD_DDEV(card),
1299 QDIO_FLAG_CLEANUP_USING_CLEAR);
1300 if (rc)
1301 QETH_DBF_TEXT_(TRACE, 3, "1err%d", rc);
1302 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1303 break;
1304 case QETH_QDIO_CLEANING:
1305 return rc;
1306 default:
1307 break;
1309 rc = qeth_clear_halt_card(card, use_halt);
1310 if (rc)
1311 QETH_DBF_TEXT_(TRACE, 3, "2err%d", rc);
1312 card->state = CARD_STATE_DOWN;
1313 return rc;
1315 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1317 static int qeth_read_conf_data(struct qeth_card *card, void **buffer,
1318 int *length)
1320 struct ciw *ciw;
1321 char *rcd_buf;
1322 int ret;
1323 struct qeth_channel *channel = &card->data;
1324 unsigned long flags;
1327 * scan for RCD command in extended SenseID data
1329 ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1330 if (!ciw || ciw->cmd == 0)
1331 return -EOPNOTSUPP;
1332 rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
1333 if (!rcd_buf)
1334 return -ENOMEM;
1336 channel->ccw.cmd_code = ciw->cmd;
1337 channel->ccw.cda = (__u32) __pa(rcd_buf);
1338 channel->ccw.count = ciw->count;
1339 channel->ccw.flags = CCW_FLAG_SLI;
1340 channel->state = CH_STATE_RCD;
1341 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1342 ret = ccw_device_start_timeout(channel->ccwdev, &channel->ccw,
1343 QETH_RCD_PARM, LPM_ANYPATH, 0,
1344 QETH_RCD_TIMEOUT);
1345 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1346 if (!ret)
1347 wait_event(card->wait_q,
1348 (channel->state == CH_STATE_RCD_DONE ||
1349 channel->state == CH_STATE_DOWN));
1350 if (channel->state == CH_STATE_DOWN)
1351 ret = -EIO;
1352 else
1353 channel->state = CH_STATE_DOWN;
1354 if (ret) {
1355 kfree(rcd_buf);
1356 *buffer = NULL;
1357 *length = 0;
1358 } else {
1359 *length = ciw->count;
1360 *buffer = rcd_buf;
1362 return ret;
1365 static void qeth_configure_unitaddr(struct qeth_card *card, char *prcd)
1367 QETH_DBF_TEXT(SETUP, 2, "cfgunit");
1368 card->info.chpid = prcd[30];
1369 card->info.unit_addr2 = prcd[31];
1370 card->info.cula = prcd[63];
1371 card->info.guestlan = ((prcd[0x10] == _ascebc['V']) &&
1372 (prcd[0x11] == _ascebc['M']));
1375 static void qeth_configure_blkt_default(struct qeth_card *card, char *prcd)
1377 QETH_DBF_TEXT(SETUP, 2, "cfgblkt");
1379 if (prcd[74] == 0xF0 && prcd[75] == 0xF0 && prcd[76] == 0xF5) {
1380 card->info.blkt.time_total = 250;
1381 card->info.blkt.inter_packet = 5;
1382 card->info.blkt.inter_packet_jumbo = 15;
1383 } else {
1384 card->info.blkt.time_total = 0;
1385 card->info.blkt.inter_packet = 0;
1386 card->info.blkt.inter_packet_jumbo = 0;
1390 static void qeth_init_tokens(struct qeth_card *card)
1392 card->token.issuer_rm_w = 0x00010103UL;
1393 card->token.cm_filter_w = 0x00010108UL;
1394 card->token.cm_connection_w = 0x0001010aUL;
1395 card->token.ulp_filter_w = 0x0001010bUL;
1396 card->token.ulp_connection_w = 0x0001010dUL;
1399 static void qeth_init_func_level(struct qeth_card *card)
1401 if (card->ipato.enabled) {
1402 if (card->info.type == QETH_CARD_TYPE_IQD)
1403 card->info.func_level =
1404 QETH_IDX_FUNC_LEVEL_IQD_ENA_IPAT;
1405 else
1406 card->info.func_level =
1407 QETH_IDX_FUNC_LEVEL_OSAE_ENA_IPAT;
1408 } else {
1409 if (card->info.type == QETH_CARD_TYPE_IQD)
1410 /*FIXME:why do we have same values for dis and ena for
1411 osae??? */
1412 card->info.func_level =
1413 QETH_IDX_FUNC_LEVEL_IQD_DIS_IPAT;
1414 else
1415 card->info.func_level =
1416 QETH_IDX_FUNC_LEVEL_OSAE_DIS_IPAT;
1420 static int qeth_idx_activate_get_answer(struct qeth_channel *channel,
1421 void (*idx_reply_cb)(struct qeth_channel *,
1422 struct qeth_cmd_buffer *))
1424 struct qeth_cmd_buffer *iob;
1425 unsigned long flags;
1426 int rc;
1427 struct qeth_card *card;
1429 QETH_DBF_TEXT(SETUP, 2, "idxanswr");
1430 card = CARD_FROM_CDEV(channel->ccwdev);
1431 iob = qeth_get_buffer(channel);
1432 iob->callback = idx_reply_cb;
1433 memcpy(&channel->ccw, READ_CCW, sizeof(struct ccw1));
1434 channel->ccw.count = QETH_BUFSIZE;
1435 channel->ccw.cda = (__u32) __pa(iob->data);
1437 wait_event(card->wait_q,
1438 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1439 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1440 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1441 rc = ccw_device_start(channel->ccwdev,
1442 &channel->ccw, (addr_t) iob, 0, 0);
1443 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1445 if (rc) {
1446 QETH_DBF_MESSAGE(2, "Error2 in activating channel rc=%d\n", rc);
1447 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
1448 atomic_set(&channel->irq_pending, 0);
1449 wake_up(&card->wait_q);
1450 return rc;
1452 rc = wait_event_interruptible_timeout(card->wait_q,
1453 channel->state == CH_STATE_UP, QETH_TIMEOUT);
1454 if (rc == -ERESTARTSYS)
1455 return rc;
1456 if (channel->state != CH_STATE_UP) {
1457 rc = -ETIME;
1458 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
1459 qeth_clear_cmd_buffers(channel);
1460 } else
1461 rc = 0;
1462 return rc;
1465 static int qeth_idx_activate_channel(struct qeth_channel *channel,
1466 void (*idx_reply_cb)(struct qeth_channel *,
1467 struct qeth_cmd_buffer *))
1469 struct qeth_card *card;
1470 struct qeth_cmd_buffer *iob;
1471 unsigned long flags;
1472 __u16 temp;
1473 __u8 tmp;
1474 int rc;
1475 struct ccw_dev_id temp_devid;
1477 card = CARD_FROM_CDEV(channel->ccwdev);
1479 QETH_DBF_TEXT(SETUP, 2, "idxactch");
1481 iob = qeth_get_buffer(channel);
1482 iob->callback = idx_reply_cb;
1483 memcpy(&channel->ccw, WRITE_CCW, sizeof(struct ccw1));
1484 channel->ccw.count = IDX_ACTIVATE_SIZE;
1485 channel->ccw.cda = (__u32) __pa(iob->data);
1486 if (channel == &card->write) {
1487 memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
1488 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1489 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1490 card->seqno.trans_hdr++;
1491 } else {
1492 memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
1493 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1494 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1496 tmp = ((__u8)card->info.portno) | 0x80;
1497 memcpy(QETH_IDX_ACT_PNO(iob->data), &tmp, 1);
1498 memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1499 &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1500 memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1501 &card->info.func_level, sizeof(__u16));
1502 ccw_device_get_id(CARD_DDEV(card), &temp_devid);
1503 memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &temp_devid.devno, 2);
1504 temp = (card->info.cula << 8) + card->info.unit_addr2;
1505 memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &temp, 2);
1507 wait_event(card->wait_q,
1508 atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0);
1509 QETH_DBF_TEXT(SETUP, 6, "noirqpnd");
1510 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1511 rc = ccw_device_start(channel->ccwdev,
1512 &channel->ccw, (addr_t) iob, 0, 0);
1513 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1515 if (rc) {
1516 QETH_DBF_MESSAGE(2, "Error1 in activating channel. rc=%d\n",
1517 rc);
1518 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
1519 atomic_set(&channel->irq_pending, 0);
1520 wake_up(&card->wait_q);
1521 return rc;
1523 rc = wait_event_interruptible_timeout(card->wait_q,
1524 channel->state == CH_STATE_ACTIVATING, QETH_TIMEOUT);
1525 if (rc == -ERESTARTSYS)
1526 return rc;
1527 if (channel->state != CH_STATE_ACTIVATING) {
1528 dev_warn(&channel->ccwdev->dev, "The qeth device driver"
1529 " failed to recover an error on the device\n");
1530 QETH_DBF_MESSAGE(2, "%s IDX activate timed out\n",
1531 dev_name(&channel->ccwdev->dev));
1532 QETH_DBF_TEXT_(SETUP, 2, "2err%d", -ETIME);
1533 qeth_clear_cmd_buffers(channel);
1534 return -ETIME;
1536 return qeth_idx_activate_get_answer(channel, idx_reply_cb);
1539 static int qeth_peer_func_level(int level)
1541 if ((level & 0xff) == 8)
1542 return (level & 0xff) + 0x400;
1543 if (((level >> 8) & 3) == 1)
1544 return (level & 0xff) + 0x200;
1545 return level;
1548 static void qeth_idx_write_cb(struct qeth_channel *channel,
1549 struct qeth_cmd_buffer *iob)
1551 struct qeth_card *card;
1552 __u16 temp;
1554 QETH_DBF_TEXT(SETUP , 2, "idxwrcb");
1556 if (channel->state == CH_STATE_DOWN) {
1557 channel->state = CH_STATE_ACTIVATING;
1558 goto out;
1560 card = CARD_FROM_CDEV(channel->ccwdev);
1562 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1563 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == 0x19)
1564 dev_err(&card->write.ccwdev->dev,
1565 "The adapter is used exclusively by another "
1566 "host\n");
1567 else
1568 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel:"
1569 " negative reply\n",
1570 dev_name(&card->write.ccwdev->dev));
1571 goto out;
1573 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1574 if ((temp & ~0x0100) != qeth_peer_func_level(card->info.func_level)) {
1575 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on write channel: "
1576 "function level mismatch (sent: 0x%x, received: "
1577 "0x%x)\n", dev_name(&card->write.ccwdev->dev),
1578 card->info.func_level, temp);
1579 goto out;
1581 channel->state = CH_STATE_UP;
1582 out:
1583 qeth_release_buffer(channel, iob);
1586 static void qeth_idx_read_cb(struct qeth_channel *channel,
1587 struct qeth_cmd_buffer *iob)
1589 struct qeth_card *card;
1590 __u16 temp;
1592 QETH_DBF_TEXT(SETUP , 2, "idxrdcb");
1593 if (channel->state == CH_STATE_DOWN) {
1594 channel->state = CH_STATE_ACTIVATING;
1595 goto out;
1598 card = CARD_FROM_CDEV(channel->ccwdev);
1599 if (qeth_check_idx_response(iob->data))
1600 goto out;
1602 if (!(QETH_IS_IDX_ACT_POS_REPLY(iob->data))) {
1603 if (QETH_IDX_ACT_CAUSE_CODE(iob->data) == 0x19)
1604 dev_err(&card->write.ccwdev->dev,
1605 "The adapter is used exclusively by another "
1606 "host\n");
1607 else
1608 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel:"
1609 " negative reply\n",
1610 dev_name(&card->read.ccwdev->dev));
1611 goto out;
1615 * temporary fix for microcode bug
1616 * to revert it,replace OR by AND
1618 if ((!QETH_IDX_NO_PORTNAME_REQUIRED(iob->data)) ||
1619 (card->info.type == QETH_CARD_TYPE_OSAE))
1620 card->info.portname_required = 1;
1622 memcpy(&temp, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1623 if (temp != qeth_peer_func_level(card->info.func_level)) {
1624 QETH_DBF_MESSAGE(2, "%s IDX_ACTIVATE on read channel: function "
1625 "level mismatch (sent: 0x%x, received: 0x%x)\n",
1626 dev_name(&card->read.ccwdev->dev),
1627 card->info.func_level, temp);
1628 goto out;
1630 memcpy(&card->token.issuer_rm_r,
1631 QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1632 QETH_MPC_TOKEN_LENGTH);
1633 memcpy(&card->info.mcl_level[0],
1634 QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1635 channel->state = CH_STATE_UP;
1636 out:
1637 qeth_release_buffer(channel, iob);
1640 void qeth_prepare_control_data(struct qeth_card *card, int len,
1641 struct qeth_cmd_buffer *iob)
1643 qeth_setup_ccw(&card->write, iob->data, len);
1644 iob->callback = qeth_release_buffer;
1646 memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data),
1647 &card->seqno.trans_hdr, QETH_SEQ_NO_LENGTH);
1648 card->seqno.trans_hdr++;
1649 memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1650 &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1651 card->seqno.pdu_hdr++;
1652 memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1653 &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1654 QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1656 EXPORT_SYMBOL_GPL(qeth_prepare_control_data);
1658 int qeth_send_control_data(struct qeth_card *card, int len,
1659 struct qeth_cmd_buffer *iob,
1660 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1661 unsigned long),
1662 void *reply_param)
1664 int rc;
1665 unsigned long flags;
1666 struct qeth_reply *reply = NULL;
1667 unsigned long timeout, event_timeout;
1668 struct qeth_ipa_cmd *cmd;
1670 QETH_DBF_TEXT(TRACE, 2, "sendctl");
1672 reply = qeth_alloc_reply(card);
1673 if (!reply) {
1674 return -ENOMEM;
1676 reply->callback = reply_cb;
1677 reply->param = reply_param;
1678 if (card->state == CARD_STATE_DOWN)
1679 reply->seqno = QETH_IDX_COMMAND_SEQNO;
1680 else
1681 reply->seqno = card->seqno.ipa++;
1682 init_waitqueue_head(&reply->wait_q);
1683 spin_lock_irqsave(&card->lock, flags);
1684 list_add_tail(&reply->list, &card->cmd_waiter_list);
1685 spin_unlock_irqrestore(&card->lock, flags);
1686 QETH_DBF_HEX(CTRL, 2, iob->data, QETH_DBF_CTRL_LEN);
1688 while (atomic_cmpxchg(&card->write.irq_pending, 0, 1)) ;
1689 qeth_prepare_control_data(card, len, iob);
1691 if (IS_IPA(iob->data))
1692 event_timeout = QETH_IPA_TIMEOUT;
1693 else
1694 event_timeout = QETH_TIMEOUT;
1695 timeout = jiffies + event_timeout;
1697 QETH_DBF_TEXT(TRACE, 6, "noirqpnd");
1698 spin_lock_irqsave(get_ccwdev_lock(card->write.ccwdev), flags);
1699 rc = ccw_device_start(card->write.ccwdev, &card->write.ccw,
1700 (addr_t) iob, 0, 0);
1701 spin_unlock_irqrestore(get_ccwdev_lock(card->write.ccwdev), flags);
1702 if (rc) {
1703 QETH_DBF_MESSAGE(2, "%s qeth_send_control_data: "
1704 "ccw_device_start rc = %i\n",
1705 dev_name(&card->write.ccwdev->dev), rc);
1706 QETH_DBF_TEXT_(TRACE, 2, " err%d", rc);
1707 spin_lock_irqsave(&card->lock, flags);
1708 list_del_init(&reply->list);
1709 qeth_put_reply(reply);
1710 spin_unlock_irqrestore(&card->lock, flags);
1711 qeth_release_buffer(iob->channel, iob);
1712 atomic_set(&card->write.irq_pending, 0);
1713 wake_up(&card->wait_q);
1714 return rc;
1717 /* we have only one long running ipassist, since we can ensure
1718 process context of this command we can sleep */
1719 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1720 if ((cmd->hdr.command == IPA_CMD_SETIP) &&
1721 (cmd->hdr.prot_version == QETH_PROT_IPV4)) {
1722 if (!wait_event_timeout(reply->wait_q,
1723 atomic_read(&reply->received), event_timeout))
1724 goto time_err;
1725 } else {
1726 while (!atomic_read(&reply->received)) {
1727 if (time_after(jiffies, timeout))
1728 goto time_err;
1729 cpu_relax();
1733 rc = reply->rc;
1734 qeth_put_reply(reply);
1735 return rc;
1737 time_err:
1738 spin_lock_irqsave(&reply->card->lock, flags);
1739 list_del_init(&reply->list);
1740 spin_unlock_irqrestore(&reply->card->lock, flags);
1741 reply->rc = -ETIME;
1742 atomic_inc(&reply->received);
1743 wake_up(&reply->wait_q);
1744 rc = reply->rc;
1745 qeth_put_reply(reply);
1746 return rc;
1748 EXPORT_SYMBOL_GPL(qeth_send_control_data);
1750 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1751 unsigned long data)
1753 struct qeth_cmd_buffer *iob;
1755 QETH_DBF_TEXT(SETUP, 2, "cmenblcb");
1757 iob = (struct qeth_cmd_buffer *) data;
1758 memcpy(&card->token.cm_filter_r,
1759 QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
1760 QETH_MPC_TOKEN_LENGTH);
1761 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
1762 return 0;
1765 static int qeth_cm_enable(struct qeth_card *card)
1767 int rc;
1768 struct qeth_cmd_buffer *iob;
1770 QETH_DBF_TEXT(SETUP, 2, "cmenable");
1772 iob = qeth_wait_for_buffer(&card->write);
1773 memcpy(iob->data, CM_ENABLE, CM_ENABLE_SIZE);
1774 memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
1775 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1776 memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
1777 &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
1779 rc = qeth_send_control_data(card, CM_ENABLE_SIZE, iob,
1780 qeth_cm_enable_cb, NULL);
1781 return rc;
1784 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1785 unsigned long data)
1788 struct qeth_cmd_buffer *iob;
1790 QETH_DBF_TEXT(SETUP, 2, "cmsetpcb");
1792 iob = (struct qeth_cmd_buffer *) data;
1793 memcpy(&card->token.cm_connection_r,
1794 QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
1795 QETH_MPC_TOKEN_LENGTH);
1796 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
1797 return 0;
1800 static int qeth_cm_setup(struct qeth_card *card)
1802 int rc;
1803 struct qeth_cmd_buffer *iob;
1805 QETH_DBF_TEXT(SETUP, 2, "cmsetup");
1807 iob = qeth_wait_for_buffer(&card->write);
1808 memcpy(iob->data, CM_SETUP, CM_SETUP_SIZE);
1809 memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
1810 &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
1811 memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
1812 &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
1813 memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
1814 &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
1815 rc = qeth_send_control_data(card, CM_SETUP_SIZE, iob,
1816 qeth_cm_setup_cb, NULL);
1817 return rc;
1821 static inline int qeth_get_initial_mtu_for_card(struct qeth_card *card)
1823 switch (card->info.type) {
1824 case QETH_CARD_TYPE_UNKNOWN:
1825 return 1500;
1826 case QETH_CARD_TYPE_IQD:
1827 return card->info.max_mtu;
1828 case QETH_CARD_TYPE_OSAE:
1829 switch (card->info.link_type) {
1830 case QETH_LINK_TYPE_HSTR:
1831 case QETH_LINK_TYPE_LANE_TR:
1832 return 2000;
1833 default:
1834 return 1492;
1836 default:
1837 return 1500;
1841 static inline int qeth_get_max_mtu_for_card(int cardtype)
1843 switch (cardtype) {
1845 case QETH_CARD_TYPE_UNKNOWN:
1846 case QETH_CARD_TYPE_OSAE:
1847 case QETH_CARD_TYPE_OSN:
1848 return 61440;
1849 case QETH_CARD_TYPE_IQD:
1850 return 57344;
1851 default:
1852 return 1500;
1856 static inline int qeth_get_mtu_out_of_mpc(int cardtype)
1858 switch (cardtype) {
1859 case QETH_CARD_TYPE_IQD:
1860 return 1;
1861 default:
1862 return 0;
1866 static inline int qeth_get_mtu_outof_framesize(int framesize)
1868 switch (framesize) {
1869 case 0x4000:
1870 return 8192;
1871 case 0x6000:
1872 return 16384;
1873 case 0xa000:
1874 return 32768;
1875 case 0xffff:
1876 return 57344;
1877 default:
1878 return 0;
1882 static inline int qeth_mtu_is_valid(struct qeth_card *card, int mtu)
1884 switch (card->info.type) {
1885 case QETH_CARD_TYPE_OSAE:
1886 return ((mtu >= 576) && (mtu <= 61440));
1887 case QETH_CARD_TYPE_IQD:
1888 return ((mtu >= 576) &&
1889 (mtu <= card->info.max_mtu + 4096 - 32));
1890 case QETH_CARD_TYPE_OSN:
1891 case QETH_CARD_TYPE_UNKNOWN:
1892 default:
1893 return 1;
1897 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
1898 unsigned long data)
1901 __u16 mtu, framesize;
1902 __u16 len;
1903 __u8 link_type;
1904 struct qeth_cmd_buffer *iob;
1906 QETH_DBF_TEXT(SETUP, 2, "ulpenacb");
1908 iob = (struct qeth_cmd_buffer *) data;
1909 memcpy(&card->token.ulp_filter_r,
1910 QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
1911 QETH_MPC_TOKEN_LENGTH);
1912 if (qeth_get_mtu_out_of_mpc(card->info.type)) {
1913 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
1914 mtu = qeth_get_mtu_outof_framesize(framesize);
1915 if (!mtu) {
1916 iob->rc = -EINVAL;
1917 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
1918 return 0;
1920 card->info.max_mtu = mtu;
1921 card->info.initial_mtu = mtu;
1922 card->qdio.in_buf_size = mtu + 2 * PAGE_SIZE;
1923 } else {
1924 card->info.initial_mtu = qeth_get_initial_mtu_for_card(card);
1925 card->info.max_mtu = qeth_get_max_mtu_for_card(card->info.type);
1926 card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1929 memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
1930 if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
1931 memcpy(&link_type,
1932 QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
1933 card->info.link_type = link_type;
1934 } else
1935 card->info.link_type = 0;
1936 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
1937 return 0;
1940 static int qeth_ulp_enable(struct qeth_card *card)
1942 int rc;
1943 char prot_type;
1944 struct qeth_cmd_buffer *iob;
1946 /*FIXME: trace view callbacks*/
1947 QETH_DBF_TEXT(SETUP, 2, "ulpenabl");
1949 iob = qeth_wait_for_buffer(&card->write);
1950 memcpy(iob->data, ULP_ENABLE, ULP_ENABLE_SIZE);
1952 *(QETH_ULP_ENABLE_LINKNUM(iob->data)) =
1953 (__u8) card->info.portno;
1954 if (card->options.layer2)
1955 if (card->info.type == QETH_CARD_TYPE_OSN)
1956 prot_type = QETH_PROT_OSN2;
1957 else
1958 prot_type = QETH_PROT_LAYER2;
1959 else
1960 prot_type = QETH_PROT_TCPIP;
1962 memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
1963 memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
1964 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
1965 memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
1966 &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
1967 memcpy(QETH_ULP_ENABLE_PORTNAME_AND_LL(iob->data),
1968 card->info.portname, 9);
1969 rc = qeth_send_control_data(card, ULP_ENABLE_SIZE, iob,
1970 qeth_ulp_enable_cb, NULL);
1971 return rc;
1975 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
1976 unsigned long data)
1978 struct qeth_cmd_buffer *iob;
1980 QETH_DBF_TEXT(SETUP, 2, "ulpstpcb");
1982 iob = (struct qeth_cmd_buffer *) data;
1983 memcpy(&card->token.ulp_connection_r,
1984 QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
1985 QETH_MPC_TOKEN_LENGTH);
1986 QETH_DBF_TEXT_(SETUP, 2, " rc%d", iob->rc);
1987 return 0;
1990 static int qeth_ulp_setup(struct qeth_card *card)
1992 int rc;
1993 __u16 temp;
1994 struct qeth_cmd_buffer *iob;
1995 struct ccw_dev_id dev_id;
1997 QETH_DBF_TEXT(SETUP, 2, "ulpsetup");
1999 iob = qeth_wait_for_buffer(&card->write);
2000 memcpy(iob->data, ULP_SETUP, ULP_SETUP_SIZE);
2002 memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2003 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2004 memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2005 &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2006 memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2007 &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2009 ccw_device_get_id(CARD_DDEV(card), &dev_id);
2010 memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2011 temp = (card->info.cula << 8) + card->info.unit_addr2;
2012 memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2013 rc = qeth_send_control_data(card, ULP_SETUP_SIZE, iob,
2014 qeth_ulp_setup_cb, NULL);
2015 return rc;
2018 static int qeth_alloc_qdio_buffers(struct qeth_card *card)
2020 int i, j;
2022 QETH_DBF_TEXT(SETUP, 2, "allcqdbf");
2024 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2025 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2026 return 0;
2028 card->qdio.in_q = kmalloc(sizeof(struct qeth_qdio_q),
2029 GFP_KERNEL);
2030 if (!card->qdio.in_q)
2031 goto out_nomem;
2032 QETH_DBF_TEXT(SETUP, 2, "inq");
2033 QETH_DBF_HEX(SETUP, 2, &card->qdio.in_q, sizeof(void *));
2034 memset(card->qdio.in_q, 0, sizeof(struct qeth_qdio_q));
2035 /* give inbound qeth_qdio_buffers their qdio_buffers */
2036 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
2037 card->qdio.in_q->bufs[i].buffer =
2038 &card->qdio.in_q->qdio_bufs[i];
2039 /* inbound buffer pool */
2040 if (qeth_alloc_buffer_pool(card))
2041 goto out_freeinq;
2042 /* outbound */
2043 card->qdio.out_qs =
2044 kmalloc(card->qdio.no_out_queues *
2045 sizeof(struct qeth_qdio_out_q *), GFP_KERNEL);
2046 if (!card->qdio.out_qs)
2047 goto out_freepool;
2048 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2049 card->qdio.out_qs[i] = kmalloc(sizeof(struct qeth_qdio_out_q),
2050 GFP_KERNEL);
2051 if (!card->qdio.out_qs[i])
2052 goto out_freeoutq;
2053 QETH_DBF_TEXT_(SETUP, 2, "outq %i", i);
2054 QETH_DBF_HEX(SETUP, 2, &card->qdio.out_qs[i], sizeof(void *));
2055 memset(card->qdio.out_qs[i], 0, sizeof(struct qeth_qdio_out_q));
2056 card->qdio.out_qs[i]->queue_no = i;
2057 /* give outbound qeth_qdio_buffers their qdio_buffers */
2058 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2059 card->qdio.out_qs[i]->bufs[j].buffer =
2060 &card->qdio.out_qs[i]->qdio_bufs[j];
2061 skb_queue_head_init(&card->qdio.out_qs[i]->bufs[j].
2062 skb_list);
2063 lockdep_set_class(
2064 &card->qdio.out_qs[i]->bufs[j].skb_list.lock,
2065 &qdio_out_skb_queue_key);
2066 INIT_LIST_HEAD(&card->qdio.out_qs[i]->bufs[j].ctx_list);
2069 return 0;
2071 out_freeoutq:
2072 while (i > 0)
2073 kfree(card->qdio.out_qs[--i]);
2074 kfree(card->qdio.out_qs);
2075 card->qdio.out_qs = NULL;
2076 out_freepool:
2077 qeth_free_buffer_pool(card);
2078 out_freeinq:
2079 kfree(card->qdio.in_q);
2080 card->qdio.in_q = NULL;
2081 out_nomem:
2082 atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2083 return -ENOMEM;
2086 static void qeth_create_qib_param_field(struct qeth_card *card,
2087 char *param_field)
2090 param_field[0] = _ascebc['P'];
2091 param_field[1] = _ascebc['C'];
2092 param_field[2] = _ascebc['I'];
2093 param_field[3] = _ascebc['T'];
2094 *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2095 *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2096 *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2099 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2100 char *param_field)
2102 param_field[16] = _ascebc['B'];
2103 param_field[17] = _ascebc['L'];
2104 param_field[18] = _ascebc['K'];
2105 param_field[19] = _ascebc['T'];
2106 *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2107 *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2108 *((unsigned int *) (&param_field[28])) =
2109 card->info.blkt.inter_packet_jumbo;
2112 static int qeth_qdio_activate(struct qeth_card *card)
2114 QETH_DBF_TEXT(SETUP, 3, "qdioact");
2115 return qdio_activate(CARD_DDEV(card));
2118 static int qeth_dm_act(struct qeth_card *card)
2120 int rc;
2121 struct qeth_cmd_buffer *iob;
2123 QETH_DBF_TEXT(SETUP, 2, "dmact");
2125 iob = qeth_wait_for_buffer(&card->write);
2126 memcpy(iob->data, DM_ACT, DM_ACT_SIZE);
2128 memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2129 &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2130 memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2131 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2132 rc = qeth_send_control_data(card, DM_ACT_SIZE, iob, NULL, NULL);
2133 return rc;
2136 static int qeth_mpc_initialize(struct qeth_card *card)
2138 int rc;
2140 QETH_DBF_TEXT(SETUP, 2, "mpcinit");
2142 rc = qeth_issue_next_read(card);
2143 if (rc) {
2144 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2145 return rc;
2147 rc = qeth_cm_enable(card);
2148 if (rc) {
2149 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2150 goto out_qdio;
2152 rc = qeth_cm_setup(card);
2153 if (rc) {
2154 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2155 goto out_qdio;
2157 rc = qeth_ulp_enable(card);
2158 if (rc) {
2159 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
2160 goto out_qdio;
2162 rc = qeth_ulp_setup(card);
2163 if (rc) {
2164 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2165 goto out_qdio;
2167 rc = qeth_alloc_qdio_buffers(card);
2168 if (rc) {
2169 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
2170 goto out_qdio;
2172 rc = qeth_qdio_establish(card);
2173 if (rc) {
2174 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2175 qeth_free_qdio_buffers(card);
2176 goto out_qdio;
2178 rc = qeth_qdio_activate(card);
2179 if (rc) {
2180 QETH_DBF_TEXT_(SETUP, 2, "7err%d", rc);
2181 goto out_qdio;
2183 rc = qeth_dm_act(card);
2184 if (rc) {
2185 QETH_DBF_TEXT_(SETUP, 2, "8err%d", rc);
2186 goto out_qdio;
2189 return 0;
2190 out_qdio:
2191 qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
2192 return rc;
2195 static void qeth_print_status_with_portname(struct qeth_card *card)
2197 char dbf_text[15];
2198 int i;
2200 sprintf(dbf_text, "%s", card->info.portname + 1);
2201 for (i = 0; i < 8; i++)
2202 dbf_text[i] =
2203 (char) _ebcasc[(__u8) dbf_text[i]];
2204 dbf_text[8] = 0;
2205 dev_info(&card->gdev->dev, "Device is a%s card%s%s%s\n"
2206 "with link type %s (portname: %s)\n",
2207 qeth_get_cardname(card),
2208 (card->info.mcl_level[0]) ? " (level: " : "",
2209 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2210 (card->info.mcl_level[0]) ? ")" : "",
2211 qeth_get_cardname_short(card),
2212 dbf_text);
2216 static void qeth_print_status_no_portname(struct qeth_card *card)
2218 if (card->info.portname[0])
2219 dev_info(&card->gdev->dev, "Device is a%s "
2220 "card%s%s%s\nwith link type %s "
2221 "(no portname needed by interface).\n",
2222 qeth_get_cardname(card),
2223 (card->info.mcl_level[0]) ? " (level: " : "",
2224 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2225 (card->info.mcl_level[0]) ? ")" : "",
2226 qeth_get_cardname_short(card));
2227 else
2228 dev_info(&card->gdev->dev, "Device is a%s "
2229 "card%s%s%s\nwith link type %s.\n",
2230 qeth_get_cardname(card),
2231 (card->info.mcl_level[0]) ? " (level: " : "",
2232 (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2233 (card->info.mcl_level[0]) ? ")" : "",
2234 qeth_get_cardname_short(card));
2237 void qeth_print_status_message(struct qeth_card *card)
2239 switch (card->info.type) {
2240 case QETH_CARD_TYPE_OSAE:
2241 /* VM will use a non-zero first character
2242 * to indicate a HiperSockets like reporting
2243 * of the level OSA sets the first character to zero
2244 * */
2245 if (!card->info.mcl_level[0]) {
2246 sprintf(card->info.mcl_level, "%02x%02x",
2247 card->info.mcl_level[2],
2248 card->info.mcl_level[3]);
2250 card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2251 break;
2253 /* fallthrough */
2254 case QETH_CARD_TYPE_IQD:
2255 if ((card->info.guestlan) ||
2256 (card->info.mcl_level[0] & 0x80)) {
2257 card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2258 card->info.mcl_level[0]];
2259 card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2260 card->info.mcl_level[1]];
2261 card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2262 card->info.mcl_level[2]];
2263 card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2264 card->info.mcl_level[3]];
2265 card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2267 break;
2268 default:
2269 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2271 if (card->info.portname_required)
2272 qeth_print_status_with_portname(card);
2273 else
2274 qeth_print_status_no_portname(card);
2276 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2278 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2280 struct qeth_buffer_pool_entry *entry;
2282 QETH_DBF_TEXT(TRACE, 5, "inwrklst");
2284 list_for_each_entry(entry,
2285 &card->qdio.init_pool.entry_list, init_list) {
2286 qeth_put_buffer_pool_entry(card, entry);
2290 static inline struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2291 struct qeth_card *card)
2293 struct list_head *plh;
2294 struct qeth_buffer_pool_entry *entry;
2295 int i, free;
2296 struct page *page;
2298 if (list_empty(&card->qdio.in_buf_pool.entry_list))
2299 return NULL;
2301 list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2302 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2303 free = 1;
2304 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2305 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2306 free = 0;
2307 break;
2310 if (free) {
2311 list_del_init(&entry->list);
2312 return entry;
2316 /* no free buffer in pool so take first one and swap pages */
2317 entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2318 struct qeth_buffer_pool_entry, list);
2319 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2320 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2321 page = alloc_page(GFP_ATOMIC);
2322 if (!page) {
2323 return NULL;
2324 } else {
2325 free_page((unsigned long)entry->elements[i]);
2326 entry->elements[i] = page_address(page);
2327 if (card->options.performance_stats)
2328 card->perf_stats.sg_alloc_page_rx++;
2332 list_del_init(&entry->list);
2333 return entry;
2336 static int qeth_init_input_buffer(struct qeth_card *card,
2337 struct qeth_qdio_buffer *buf)
2339 struct qeth_buffer_pool_entry *pool_entry;
2340 int i;
2342 pool_entry = qeth_find_free_buffer_pool_entry(card);
2343 if (!pool_entry)
2344 return 1;
2347 * since the buffer is accessed only from the input_tasklet
2348 * there shouldn't be a need to synchronize; also, since we use
2349 * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run out off
2350 * buffers
2353 buf->pool_entry = pool_entry;
2354 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2355 buf->buffer->element[i].length = PAGE_SIZE;
2356 buf->buffer->element[i].addr = pool_entry->elements[i];
2357 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2358 buf->buffer->element[i].flags = SBAL_FLAGS_LAST_ENTRY;
2359 else
2360 buf->buffer->element[i].flags = 0;
2362 return 0;
2365 int qeth_init_qdio_queues(struct qeth_card *card)
2367 int i, j;
2368 int rc;
2370 QETH_DBF_TEXT(SETUP, 2, "initqdqs");
2372 /* inbound queue */
2373 memset(card->qdio.in_q->qdio_bufs, 0,
2374 QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2375 qeth_initialize_working_pool_list(card);
2376 /*give only as many buffers to hardware as we have buffer pool entries*/
2377 for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; ++i)
2378 qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2379 card->qdio.in_q->next_buf_to_init =
2380 card->qdio.in_buf_pool.buf_count - 1;
2381 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2382 card->qdio.in_buf_pool.buf_count - 1);
2383 if (rc) {
2384 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2385 return rc;
2387 /* outbound queue */
2388 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2389 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
2390 QDIO_MAX_BUFFERS_PER_Q * sizeof(struct qdio_buffer));
2391 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2392 qeth_clear_output_buffer(card->qdio.out_qs[i],
2393 &card->qdio.out_qs[i]->bufs[j]);
2395 card->qdio.out_qs[i]->card = card;
2396 card->qdio.out_qs[i]->next_buf_to_fill = 0;
2397 card->qdio.out_qs[i]->do_pack = 0;
2398 atomic_set(&card->qdio.out_qs[i]->used_buffers, 0);
2399 atomic_set(&card->qdio.out_qs[i]->set_pci_flags_count, 0);
2400 atomic_set(&card->qdio.out_qs[i]->state,
2401 QETH_OUT_Q_UNLOCKED);
2403 return 0;
2405 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2407 static inline __u8 qeth_get_ipa_adp_type(enum qeth_link_types link_type)
2409 switch (link_type) {
2410 case QETH_LINK_TYPE_HSTR:
2411 return 2;
2412 default:
2413 return 1;
2417 static void qeth_fill_ipacmd_header(struct qeth_card *card,
2418 struct qeth_ipa_cmd *cmd, __u8 command,
2419 enum qeth_prot_versions prot)
2421 memset(cmd, 0, sizeof(struct qeth_ipa_cmd));
2422 cmd->hdr.command = command;
2423 cmd->hdr.initiator = IPA_CMD_INITIATOR_HOST;
2424 cmd->hdr.seqno = card->seqno.ipa;
2425 cmd->hdr.adapter_type = qeth_get_ipa_adp_type(card->info.link_type);
2426 cmd->hdr.rel_adapter_no = (__u8) card->info.portno;
2427 if (card->options.layer2)
2428 cmd->hdr.prim_version_no = 2;
2429 else
2430 cmd->hdr.prim_version_no = 1;
2431 cmd->hdr.param_count = 1;
2432 cmd->hdr.prot_version = prot;
2433 cmd->hdr.ipa_supported = 0;
2434 cmd->hdr.ipa_enabled = 0;
2437 struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *card,
2438 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2440 struct qeth_cmd_buffer *iob;
2441 struct qeth_ipa_cmd *cmd;
2443 iob = qeth_wait_for_buffer(&card->write);
2444 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2445 qeth_fill_ipacmd_header(card, cmd, ipacmd, prot);
2447 return iob;
2449 EXPORT_SYMBOL_GPL(qeth_get_ipacmd_buffer);
2451 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2452 char prot_type)
2454 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2455 memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2456 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2457 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2459 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2461 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2462 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2463 unsigned long),
2464 void *reply_param)
2466 int rc;
2467 char prot_type;
2469 QETH_DBF_TEXT(TRACE, 4, "sendipa");
2471 if (card->options.layer2)
2472 if (card->info.type == QETH_CARD_TYPE_OSN)
2473 prot_type = QETH_PROT_OSN2;
2474 else
2475 prot_type = QETH_PROT_LAYER2;
2476 else
2477 prot_type = QETH_PROT_TCPIP;
2478 qeth_prepare_ipa_cmd(card, iob, prot_type);
2479 rc = qeth_send_control_data(card, IPA_CMD_LENGTH,
2480 iob, reply_cb, reply_param);
2481 return rc;
2483 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
2485 static int qeth_send_startstoplan(struct qeth_card *card,
2486 enum qeth_ipa_cmds ipacmd, enum qeth_prot_versions prot)
2488 int rc;
2489 struct qeth_cmd_buffer *iob;
2491 iob = qeth_get_ipacmd_buffer(card, ipacmd, prot);
2492 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
2494 return rc;
2497 int qeth_send_startlan(struct qeth_card *card)
2499 int rc;
2501 QETH_DBF_TEXT(SETUP, 2, "strtlan");
2503 rc = qeth_send_startstoplan(card, IPA_CMD_STARTLAN, 0);
2504 return rc;
2506 EXPORT_SYMBOL_GPL(qeth_send_startlan);
2508 int qeth_send_stoplan(struct qeth_card *card)
2510 int rc = 0;
2513 * TODO: according to the IPA format document page 14,
2514 * TCP/IP (we!) never issue a STOPLAN
2515 * is this right ?!?
2517 QETH_DBF_TEXT(SETUP, 2, "stoplan");
2519 rc = qeth_send_startstoplan(card, IPA_CMD_STOPLAN, 0);
2520 return rc;
2522 EXPORT_SYMBOL_GPL(qeth_send_stoplan);
2524 int qeth_default_setadapterparms_cb(struct qeth_card *card,
2525 struct qeth_reply *reply, unsigned long data)
2527 struct qeth_ipa_cmd *cmd;
2529 QETH_DBF_TEXT(TRACE, 4, "defadpcb");
2531 cmd = (struct qeth_ipa_cmd *) data;
2532 if (cmd->hdr.return_code == 0)
2533 cmd->hdr.return_code =
2534 cmd->data.setadapterparms.hdr.return_code;
2535 return 0;
2537 EXPORT_SYMBOL_GPL(qeth_default_setadapterparms_cb);
2539 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
2540 struct qeth_reply *reply, unsigned long data)
2542 struct qeth_ipa_cmd *cmd;
2544 QETH_DBF_TEXT(TRACE, 3, "quyadpcb");
2546 cmd = (struct qeth_ipa_cmd *) data;
2547 if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f)
2548 card->info.link_type =
2549 cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
2550 card->options.adp.supported_funcs =
2551 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
2552 return qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
2555 struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
2556 __u32 command, __u32 cmdlen)
2558 struct qeth_cmd_buffer *iob;
2559 struct qeth_ipa_cmd *cmd;
2561 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETADAPTERPARMS,
2562 QETH_PROT_IPV4);
2563 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2564 cmd->data.setadapterparms.hdr.cmdlength = cmdlen;
2565 cmd->data.setadapterparms.hdr.command_code = command;
2566 cmd->data.setadapterparms.hdr.used_total = 1;
2567 cmd->data.setadapterparms.hdr.seq_no = 1;
2569 return iob;
2571 EXPORT_SYMBOL_GPL(qeth_get_adapter_cmd);
2573 int qeth_query_setadapterparms(struct qeth_card *card)
2575 int rc;
2576 struct qeth_cmd_buffer *iob;
2578 QETH_DBF_TEXT(TRACE, 3, "queryadp");
2579 iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
2580 sizeof(struct qeth_ipacmd_setadpparms));
2581 rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
2582 return rc;
2584 EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
2586 int qeth_check_qdio_errors(struct qeth_card *card, struct qdio_buffer *buf,
2587 unsigned int qdio_error, const char *dbftext)
2589 if (qdio_error) {
2590 QETH_DBF_TEXT(TRACE, 2, dbftext);
2591 QETH_DBF_TEXT(QERR, 2, dbftext);
2592 QETH_DBF_TEXT_(QERR, 2, " F15=%02X",
2593 buf->element[15].flags & 0xff);
2594 QETH_DBF_TEXT_(QERR, 2, " F14=%02X",
2595 buf->element[14].flags & 0xff);
2596 QETH_DBF_TEXT_(QERR, 2, " qerr=%X", qdio_error);
2597 if ((buf->element[15].flags & 0xff) == 0x12) {
2598 card->stats.rx_dropped++;
2599 return 0;
2600 } else
2601 return 1;
2603 return 0;
2605 EXPORT_SYMBOL_GPL(qeth_check_qdio_errors);
2607 void qeth_queue_input_buffer(struct qeth_card *card, int index)
2609 struct qeth_qdio_q *queue = card->qdio.in_q;
2610 int count;
2611 int i;
2612 int rc;
2613 int newcount = 0;
2615 count = (index < queue->next_buf_to_init)?
2616 card->qdio.in_buf_pool.buf_count -
2617 (queue->next_buf_to_init - index) :
2618 card->qdio.in_buf_pool.buf_count -
2619 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
2620 /* only requeue at a certain threshold to avoid SIGAs */
2621 if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
2622 for (i = queue->next_buf_to_init;
2623 i < queue->next_buf_to_init + count; ++i) {
2624 if (qeth_init_input_buffer(card,
2625 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
2626 break;
2627 } else {
2628 newcount++;
2632 if (newcount < count) {
2633 /* we are in memory shortage so we switch back to
2634 traditional skb allocation and drop packages */
2635 atomic_set(&card->force_alloc_skb, 3);
2636 count = newcount;
2637 } else {
2638 atomic_add_unless(&card->force_alloc_skb, -1, 0);
2642 * according to old code it should be avoided to requeue all
2643 * 128 buffers in order to benefit from PCI avoidance.
2644 * this function keeps at least one buffer (the buffer at
2645 * 'index') un-requeued -> this buffer is the first buffer that
2646 * will be requeued the next time
2648 if (card->options.performance_stats) {
2649 card->perf_stats.inbound_do_qdio_cnt++;
2650 card->perf_stats.inbound_do_qdio_start_time =
2651 qeth_get_micros();
2653 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
2654 queue->next_buf_to_init, count);
2655 if (card->options.performance_stats)
2656 card->perf_stats.inbound_do_qdio_time +=
2657 qeth_get_micros() -
2658 card->perf_stats.inbound_do_qdio_start_time;
2659 if (rc) {
2660 dev_warn(&card->gdev->dev,
2661 "QDIO reported an error, rc=%i\n", rc);
2662 QETH_DBF_TEXT(TRACE, 2, "qinberr");
2663 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_BUS_ID(card));
2665 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
2666 QDIO_MAX_BUFFERS_PER_Q;
2669 EXPORT_SYMBOL_GPL(qeth_queue_input_buffer);
2671 static int qeth_handle_send_error(struct qeth_card *card,
2672 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
2674 int sbalf15 = buffer->buffer->element[15].flags & 0xff;
2676 QETH_DBF_TEXT(TRACE, 6, "hdsnderr");
2677 if (card->info.type == QETH_CARD_TYPE_IQD) {
2678 if (sbalf15 == 0) {
2679 qdio_err = 0;
2680 } else {
2681 qdio_err = 1;
2684 qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
2686 if (!qdio_err)
2687 return QETH_SEND_ERROR_NONE;
2689 if ((sbalf15 >= 15) && (sbalf15 <= 31))
2690 return QETH_SEND_ERROR_RETRY;
2692 QETH_DBF_TEXT(TRACE, 1, "lnkfail");
2693 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
2694 QETH_DBF_TEXT_(TRACE, 1, "%04x %02x",
2695 (u16)qdio_err, (u8)sbalf15);
2696 return QETH_SEND_ERROR_LINK_FAILURE;
2700 * Switched to packing state if the number of used buffers on a queue
2701 * reaches a certain limit.
2703 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
2705 if (!queue->do_pack) {
2706 if (atomic_read(&queue->used_buffers)
2707 >= QETH_HIGH_WATERMARK_PACK){
2708 /* switch non-PACKING -> PACKING */
2709 QETH_DBF_TEXT(TRACE, 6, "np->pack");
2710 if (queue->card->options.performance_stats)
2711 queue->card->perf_stats.sc_dp_p++;
2712 queue->do_pack = 1;
2718 * Switches from packing to non-packing mode. If there is a packing
2719 * buffer on the queue this buffer will be prepared to be flushed.
2720 * In that case 1 is returned to inform the caller. If no buffer
2721 * has to be flushed, zero is returned.
2723 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
2725 struct qeth_qdio_out_buffer *buffer;
2726 int flush_count = 0;
2728 if (queue->do_pack) {
2729 if (atomic_read(&queue->used_buffers)
2730 <= QETH_LOW_WATERMARK_PACK) {
2731 /* switch PACKING -> non-PACKING */
2732 QETH_DBF_TEXT(TRACE, 6, "pack->np");
2733 if (queue->card->options.performance_stats)
2734 queue->card->perf_stats.sc_p_dp++;
2735 queue->do_pack = 0;
2736 /* flush packing buffers */
2737 buffer = &queue->bufs[queue->next_buf_to_fill];
2738 if ((atomic_read(&buffer->state) ==
2739 QETH_QDIO_BUF_EMPTY) &&
2740 (buffer->next_element_to_fill > 0)) {
2741 atomic_set(&buffer->state,
2742 QETH_QDIO_BUF_PRIMED);
2743 flush_count++;
2744 queue->next_buf_to_fill =
2745 (queue->next_buf_to_fill + 1) %
2746 QDIO_MAX_BUFFERS_PER_Q;
2750 return flush_count;
2754 * Called to flush a packing buffer if no more pci flags are on the queue.
2755 * Checks if there is a packing buffer and prepares it to be flushed.
2756 * In that case returns 1, otherwise zero.
2758 static int qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2760 struct qeth_qdio_out_buffer *buffer;
2762 buffer = &queue->bufs[queue->next_buf_to_fill];
2763 if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
2764 (buffer->next_element_to_fill > 0)) {
2765 /* it's a packing buffer */
2766 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
2767 queue->next_buf_to_fill =
2768 (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
2769 return 1;
2771 return 0;
2774 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
2775 int count)
2777 struct qeth_qdio_out_buffer *buf;
2778 int rc;
2779 int i;
2780 unsigned int qdio_flags;
2782 for (i = index; i < index + count; ++i) {
2783 buf = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2784 buf->buffer->element[buf->next_element_to_fill - 1].flags |=
2785 SBAL_FLAGS_LAST_ENTRY;
2787 if (queue->card->info.type == QETH_CARD_TYPE_IQD)
2788 continue;
2790 if (!queue->do_pack) {
2791 if ((atomic_read(&queue->used_buffers) >=
2792 (QETH_HIGH_WATERMARK_PACK -
2793 QETH_WATERMARK_PACK_FUZZ)) &&
2794 !atomic_read(&queue->set_pci_flags_count)) {
2795 /* it's likely that we'll go to packing
2796 * mode soon */
2797 atomic_inc(&queue->set_pci_flags_count);
2798 buf->buffer->element[0].flags |= 0x40;
2800 } else {
2801 if (!atomic_read(&queue->set_pci_flags_count)) {
2803 * there's no outstanding PCI any more, so we
2804 * have to request a PCI to be sure the the PCI
2805 * will wake at some time in the future then we
2806 * can flush packed buffers that might still be
2807 * hanging around, which can happen if no
2808 * further send was requested by the stack
2810 atomic_inc(&queue->set_pci_flags_count);
2811 buf->buffer->element[0].flags |= 0x40;
2816 queue->sync_iqdio_error = 0;
2817 queue->card->dev->trans_start = jiffies;
2818 if (queue->card->options.performance_stats) {
2819 queue->card->perf_stats.outbound_do_qdio_cnt++;
2820 queue->card->perf_stats.outbound_do_qdio_start_time =
2821 qeth_get_micros();
2823 qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
2824 if (atomic_read(&queue->set_pci_flags_count))
2825 qdio_flags |= QDIO_FLAG_PCI_OUT;
2826 rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
2827 queue->queue_no, index, count);
2828 if (queue->card->options.performance_stats)
2829 queue->card->perf_stats.outbound_do_qdio_time +=
2830 qeth_get_micros() -
2831 queue->card->perf_stats.outbound_do_qdio_start_time;
2832 if (rc > 0) {
2833 if (!(rc & QDIO_ERROR_SIGA_BUSY))
2834 queue->sync_iqdio_error = rc & 3;
2836 if (rc) {
2837 queue->card->stats.tx_errors += count;
2838 /* ignore temporary SIGA errors without busy condition */
2839 if (rc == QDIO_ERROR_SIGA_TARGET)
2840 return;
2841 QETH_DBF_TEXT(TRACE, 2, "flushbuf");
2842 QETH_DBF_TEXT_(TRACE, 2, " err%d", rc);
2843 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_DDEV_ID(queue->card));
2845 /* this must not happen under normal circumstances. if it
2846 * happens something is really wrong -> recover */
2847 qeth_schedule_recovery(queue->card);
2848 return;
2850 atomic_add(count, &queue->used_buffers);
2851 if (queue->card->options.performance_stats)
2852 queue->card->perf_stats.bufs_sent += count;
2855 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2857 int index;
2858 int flush_cnt = 0;
2859 int q_was_packing = 0;
2862 * check if weed have to switch to non-packing mode or if
2863 * we have to get a pci flag out on the queue
2865 if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
2866 !atomic_read(&queue->set_pci_flags_count)) {
2867 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
2868 QETH_OUT_Q_UNLOCKED) {
2870 * If we get in here, there was no action in
2871 * do_send_packet. So, we check if there is a
2872 * packing buffer to be flushed here.
2874 netif_stop_queue(queue->card->dev);
2875 index = queue->next_buf_to_fill;
2876 q_was_packing = queue->do_pack;
2877 /* queue->do_pack may change */
2878 barrier();
2879 flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
2880 if (!flush_cnt &&
2881 !atomic_read(&queue->set_pci_flags_count))
2882 flush_cnt +=
2883 qeth_flush_buffers_on_no_pci(queue);
2884 if (queue->card->options.performance_stats &&
2885 q_was_packing)
2886 queue->card->perf_stats.bufs_sent_pack +=
2887 flush_cnt;
2888 if (flush_cnt)
2889 qeth_flush_buffers(queue, index, flush_cnt);
2890 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
2895 void qeth_qdio_output_handler(struct ccw_device *ccwdev,
2896 unsigned int qdio_error, int __queue, int first_element,
2897 int count, unsigned long card_ptr)
2899 struct qeth_card *card = (struct qeth_card *) card_ptr;
2900 struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
2901 struct qeth_qdio_out_buffer *buffer;
2902 int i;
2903 unsigned qeth_send_err;
2905 QETH_DBF_TEXT(TRACE, 6, "qdouhdl");
2906 if (qdio_error & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
2907 QETH_DBF_TEXT(TRACE, 2, "achkcond");
2908 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_BUS_ID(card));
2909 netif_stop_queue(card->dev);
2910 qeth_schedule_recovery(card);
2911 return;
2913 if (card->options.performance_stats) {
2914 card->perf_stats.outbound_handler_cnt++;
2915 card->perf_stats.outbound_handler_start_time =
2916 qeth_get_micros();
2918 for (i = first_element; i < (first_element + count); ++i) {
2919 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2920 qeth_send_err = qeth_handle_send_error(card, buffer, qdio_error);
2921 __qeth_clear_output_buffer(queue, buffer,
2922 (qeth_send_err == QETH_SEND_ERROR_RETRY) ? 1 : 0);
2924 atomic_sub(count, &queue->used_buffers);
2925 /* check if we need to do something on this outbound queue */
2926 if (card->info.type != QETH_CARD_TYPE_IQD)
2927 qeth_check_outbound_queue(queue);
2929 netif_wake_queue(queue->card->dev);
2930 if (card->options.performance_stats)
2931 card->perf_stats.outbound_handler_time += qeth_get_micros() -
2932 card->perf_stats.outbound_handler_start_time;
2934 EXPORT_SYMBOL_GPL(qeth_qdio_output_handler);
2936 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb,
2937 int ipv, int cast_type)
2939 if (!ipv && (card->info.type == QETH_CARD_TYPE_OSAE))
2940 return card->qdio.default_out_queue;
2941 switch (card->qdio.no_out_queues) {
2942 case 4:
2943 if (cast_type && card->info.is_multicast_different)
2944 return card->info.is_multicast_different &
2945 (card->qdio.no_out_queues - 1);
2946 if (card->qdio.do_prio_queueing && (ipv == 4)) {
2947 const u8 tos = ip_hdr(skb)->tos;
2949 if (card->qdio.do_prio_queueing ==
2950 QETH_PRIO_Q_ING_TOS) {
2951 if (tos & IP_TOS_NOTIMPORTANT)
2952 return 3;
2953 if (tos & IP_TOS_HIGHRELIABILITY)
2954 return 2;
2955 if (tos & IP_TOS_HIGHTHROUGHPUT)
2956 return 1;
2957 if (tos & IP_TOS_LOWDELAY)
2958 return 0;
2960 if (card->qdio.do_prio_queueing ==
2961 QETH_PRIO_Q_ING_PREC)
2962 return 3 - (tos >> 6);
2963 } else if (card->qdio.do_prio_queueing && (ipv == 6)) {
2964 /* TODO: IPv6!!! */
2966 return card->qdio.default_out_queue;
2967 case 1: /* fallthrough for single-out-queue 1920-device */
2968 default:
2969 return card->qdio.default_out_queue;
2972 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
2974 int qeth_get_elements_no(struct qeth_card *card, void *hdr,
2975 struct sk_buff *skb, int elems)
2977 int elements_needed = 0;
2979 if (skb_shinfo(skb)->nr_frags > 0)
2980 elements_needed = (skb_shinfo(skb)->nr_frags + 1);
2981 if (elements_needed == 0)
2982 elements_needed = 1 + (((((unsigned long) skb->data) %
2983 PAGE_SIZE) + skb->len) >> PAGE_SHIFT);
2984 if ((elements_needed + elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
2985 QETH_DBF_MESSAGE(2, "Invalid size of IP packet "
2986 "(Number=%d / Length=%d). Discarded.\n",
2987 (elements_needed+elems), skb->len);
2988 return 0;
2990 return elements_needed;
2992 EXPORT_SYMBOL_GPL(qeth_get_elements_no);
2994 static inline void __qeth_fill_buffer(struct sk_buff *skb,
2995 struct qdio_buffer *buffer, int is_tso, int *next_element_to_fill,
2996 int offset)
2998 int length = skb->len;
2999 int length_here;
3000 int element;
3001 char *data;
3002 int first_lap ;
3004 element = *next_element_to_fill;
3005 data = skb->data;
3006 first_lap = (is_tso == 0 ? 1 : 0);
3008 if (offset >= 0) {
3009 data = skb->data + offset;
3010 length -= offset;
3011 first_lap = 0;
3014 while (length > 0) {
3015 /* length_here is the remaining amount of data in this page */
3016 length_here = PAGE_SIZE - ((unsigned long) data % PAGE_SIZE);
3017 if (length < length_here)
3018 length_here = length;
3020 buffer->element[element].addr = data;
3021 buffer->element[element].length = length_here;
3022 length -= length_here;
3023 if (!length) {
3024 if (first_lap)
3025 buffer->element[element].flags = 0;
3026 else
3027 buffer->element[element].flags =
3028 SBAL_FLAGS_LAST_FRAG;
3029 } else {
3030 if (first_lap)
3031 buffer->element[element].flags =
3032 SBAL_FLAGS_FIRST_FRAG;
3033 else
3034 buffer->element[element].flags =
3035 SBAL_FLAGS_MIDDLE_FRAG;
3037 data += length_here;
3038 element++;
3039 first_lap = 0;
3041 *next_element_to_fill = element;
3044 static inline int qeth_fill_buffer(struct qeth_qdio_out_q *queue,
3045 struct qeth_qdio_out_buffer *buf, struct sk_buff *skb,
3046 struct qeth_hdr *hdr, int offset, int hd_len)
3048 struct qdio_buffer *buffer;
3049 int flush_cnt = 0, hdr_len, large_send = 0;
3051 buffer = buf->buffer;
3052 atomic_inc(&skb->users);
3053 skb_queue_tail(&buf->skb_list, skb);
3055 /*check first on TSO ....*/
3056 if (hdr->hdr.l3.id == QETH_HEADER_TYPE_TSO) {
3057 int element = buf->next_element_to_fill;
3059 hdr_len = sizeof(struct qeth_hdr_tso) +
3060 ((struct qeth_hdr_tso *)hdr)->ext.dg_hdr_len;
3061 /*fill first buffer entry only with header information */
3062 buffer->element[element].addr = skb->data;
3063 buffer->element[element].length = hdr_len;
3064 buffer->element[element].flags = SBAL_FLAGS_FIRST_FRAG;
3065 buf->next_element_to_fill++;
3066 skb->data += hdr_len;
3067 skb->len -= hdr_len;
3068 large_send = 1;
3071 if (offset >= 0) {
3072 int element = buf->next_element_to_fill;
3073 buffer->element[element].addr = hdr;
3074 buffer->element[element].length = sizeof(struct qeth_hdr) +
3075 hd_len;
3076 buffer->element[element].flags = SBAL_FLAGS_FIRST_FRAG;
3077 buf->is_header[element] = 1;
3078 buf->next_element_to_fill++;
3081 if (skb_shinfo(skb)->nr_frags == 0)
3082 __qeth_fill_buffer(skb, buffer, large_send,
3083 (int *)&buf->next_element_to_fill, offset);
3084 else
3085 __qeth_fill_buffer_frag(skb, buffer, large_send,
3086 (int *)&buf->next_element_to_fill);
3088 if (!queue->do_pack) {
3089 QETH_DBF_TEXT(TRACE, 6, "fillbfnp");
3090 /* set state to PRIMED -> will be flushed */
3091 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3092 flush_cnt = 1;
3093 } else {
3094 QETH_DBF_TEXT(TRACE, 6, "fillbfpa");
3095 if (queue->card->options.performance_stats)
3096 queue->card->perf_stats.skbs_sent_pack++;
3097 if (buf->next_element_to_fill >=
3098 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
3100 * packed buffer if full -> set state PRIMED
3101 * -> will be flushed
3103 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
3104 flush_cnt = 1;
3107 return flush_cnt;
3110 int qeth_do_send_packet_fast(struct qeth_card *card,
3111 struct qeth_qdio_out_q *queue, struct sk_buff *skb,
3112 struct qeth_hdr *hdr, int elements_needed,
3113 int offset, int hd_len)
3115 struct qeth_qdio_out_buffer *buffer;
3116 struct sk_buff *skb1;
3117 struct qeth_skb_data *retry_ctrl;
3118 int index;
3119 int rc;
3121 /* spin until we get the queue ... */
3122 while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
3123 QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
3124 /* ... now we've got the queue */
3125 index = queue->next_buf_to_fill;
3126 buffer = &queue->bufs[queue->next_buf_to_fill];
3128 * check if buffer is empty to make sure that we do not 'overtake'
3129 * ourselves and try to fill a buffer that is already primed
3131 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
3132 goto out;
3133 queue->next_buf_to_fill = (queue->next_buf_to_fill + 1) %
3134 QDIO_MAX_BUFFERS_PER_Q;
3135 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3136 qeth_fill_buffer(queue, buffer, skb, hdr, offset, hd_len);
3137 qeth_flush_buffers(queue, index, 1);
3138 if (queue->sync_iqdio_error == 2) {
3139 skb1 = skb_dequeue(&buffer->skb_list);
3140 while (skb1) {
3141 atomic_dec(&skb1->users);
3142 skb1 = skb_dequeue(&buffer->skb_list);
3144 retry_ctrl = (struct qeth_skb_data *) &skb->cb[16];
3145 if (retry_ctrl->magic != QETH_SKB_MAGIC) {
3146 retry_ctrl->magic = QETH_SKB_MAGIC;
3147 retry_ctrl->count = 0;
3149 if (retry_ctrl->count < QETH_SIGA_CC2_RETRIES) {
3150 retry_ctrl->count++;
3151 rc = dev_queue_xmit(skb);
3152 } else {
3153 dev_kfree_skb_any(skb);
3154 QETH_DBF_TEXT(QERR, 2, "qrdrop");
3157 return 0;
3158 out:
3159 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3160 return -EBUSY;
3162 EXPORT_SYMBOL_GPL(qeth_do_send_packet_fast);
3164 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3165 struct sk_buff *skb, struct qeth_hdr *hdr,
3166 int elements_needed)
3168 struct qeth_qdio_out_buffer *buffer;
3169 int start_index;
3170 int flush_count = 0;
3171 int do_pack = 0;
3172 int tmp;
3173 int rc = 0;
3175 /* spin until we get the queue ... */
3176 while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
3177 QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
3178 start_index = queue->next_buf_to_fill;
3179 buffer = &queue->bufs[queue->next_buf_to_fill];
3181 * check if buffer is empty to make sure that we do not 'overtake'
3182 * ourselves and try to fill a buffer that is already primed
3184 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
3185 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3186 return -EBUSY;
3188 /* check if we need to switch packing state of this queue */
3189 qeth_switch_to_packing_if_needed(queue);
3190 if (queue->do_pack) {
3191 do_pack = 1;
3192 /* does packet fit in current buffer? */
3193 if ((QETH_MAX_BUFFER_ELEMENTS(card) -
3194 buffer->next_element_to_fill) < elements_needed) {
3195 /* ... no -> set state PRIMED */
3196 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3197 flush_count++;
3198 queue->next_buf_to_fill =
3199 (queue->next_buf_to_fill + 1) %
3200 QDIO_MAX_BUFFERS_PER_Q;
3201 buffer = &queue->bufs[queue->next_buf_to_fill];
3202 /* we did a step forward, so check buffer state
3203 * again */
3204 if (atomic_read(&buffer->state) !=
3205 QETH_QDIO_BUF_EMPTY) {
3206 qeth_flush_buffers(queue, start_index,
3207 flush_count);
3208 atomic_set(&queue->state,
3209 QETH_OUT_Q_UNLOCKED);
3210 return -EBUSY;
3214 tmp = qeth_fill_buffer(queue, buffer, skb, hdr, -1, 0);
3215 queue->next_buf_to_fill = (queue->next_buf_to_fill + tmp) %
3216 QDIO_MAX_BUFFERS_PER_Q;
3217 flush_count += tmp;
3218 if (flush_count)
3219 qeth_flush_buffers(queue, start_index, flush_count);
3220 else if (!atomic_read(&queue->set_pci_flags_count))
3221 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
3223 * queue->state will go from LOCKED -> UNLOCKED or from
3224 * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
3225 * (switch packing state or flush buffer to get another pci flag out).
3226 * In that case we will enter this loop
3228 while (atomic_dec_return(&queue->state)) {
3229 flush_count = 0;
3230 start_index = queue->next_buf_to_fill;
3231 /* check if we can go back to non-packing state */
3232 flush_count += qeth_switch_to_nonpacking_if_needed(queue);
3234 * check if we need to flush a packing buffer to get a pci
3235 * flag out on the queue
3237 if (!flush_count && !atomic_read(&queue->set_pci_flags_count))
3238 flush_count += qeth_flush_buffers_on_no_pci(queue);
3239 if (flush_count)
3240 qeth_flush_buffers(queue, start_index, flush_count);
3242 /* at this point the queue is UNLOCKED again */
3243 if (queue->card->options.performance_stats && do_pack)
3244 queue->card->perf_stats.bufs_sent_pack += flush_count;
3246 return rc;
3248 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
3250 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
3251 struct qeth_reply *reply, unsigned long data)
3253 struct qeth_ipa_cmd *cmd;
3254 struct qeth_ipacmd_setadpparms *setparms;
3256 QETH_DBF_TEXT(TRACE, 4, "prmadpcb");
3258 cmd = (struct qeth_ipa_cmd *) data;
3259 setparms = &(cmd->data.setadapterparms);
3261 qeth_default_setadapterparms_cb(card, reply, (unsigned long)cmd);
3262 if (cmd->hdr.return_code) {
3263 QETH_DBF_TEXT_(TRACE, 4, "prmrc%2.2x", cmd->hdr.return_code);
3264 setparms->data.mode = SET_PROMISC_MODE_OFF;
3266 card->info.promisc_mode = setparms->data.mode;
3267 return 0;
3270 void qeth_setadp_promisc_mode(struct qeth_card *card)
3272 enum qeth_ipa_promisc_modes mode;
3273 struct net_device *dev = card->dev;
3274 struct qeth_cmd_buffer *iob;
3275 struct qeth_ipa_cmd *cmd;
3277 QETH_DBF_TEXT(TRACE, 4, "setprom");
3279 if (((dev->flags & IFF_PROMISC) &&
3280 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
3281 (!(dev->flags & IFF_PROMISC) &&
3282 (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
3283 return;
3284 mode = SET_PROMISC_MODE_OFF;
3285 if (dev->flags & IFF_PROMISC)
3286 mode = SET_PROMISC_MODE_ON;
3287 QETH_DBF_TEXT_(TRACE, 4, "mode:%x", mode);
3289 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
3290 sizeof(struct qeth_ipacmd_setadpparms));
3291 cmd = (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
3292 cmd->data.setadapterparms.data.mode = mode;
3293 qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
3295 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
3297 int qeth_change_mtu(struct net_device *dev, int new_mtu)
3299 struct qeth_card *card;
3300 char dbf_text[15];
3302 card = dev->ml_priv;
3304 QETH_DBF_TEXT(TRACE, 4, "chgmtu");
3305 sprintf(dbf_text, "%8x", new_mtu);
3306 QETH_DBF_TEXT(TRACE, 4, dbf_text);
3308 if (new_mtu < 64)
3309 return -EINVAL;
3310 if (new_mtu > 65535)
3311 return -EINVAL;
3312 if ((!qeth_is_supported(card, IPA_IP_FRAGMENTATION)) &&
3313 (!qeth_mtu_is_valid(card, new_mtu)))
3314 return -EINVAL;
3315 dev->mtu = new_mtu;
3316 return 0;
3318 EXPORT_SYMBOL_GPL(qeth_change_mtu);
3320 struct net_device_stats *qeth_get_stats(struct net_device *dev)
3322 struct qeth_card *card;
3324 card = dev->ml_priv;
3326 QETH_DBF_TEXT(TRACE, 5, "getstat");
3328 return &card->stats;
3330 EXPORT_SYMBOL_GPL(qeth_get_stats);
3332 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
3333 struct qeth_reply *reply, unsigned long data)
3335 struct qeth_ipa_cmd *cmd;
3337 QETH_DBF_TEXT(TRACE, 4, "chgmaccb");
3339 cmd = (struct qeth_ipa_cmd *) data;
3340 if (!card->options.layer2 ||
3341 !(card->info.mac_bits & QETH_LAYER2_MAC_READ)) {
3342 memcpy(card->dev->dev_addr,
3343 &cmd->data.setadapterparms.data.change_addr.addr,
3344 OSA_ADDR_LEN);
3345 card->info.mac_bits |= QETH_LAYER2_MAC_READ;
3347 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3348 return 0;
3351 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
3353 int rc;
3354 struct qeth_cmd_buffer *iob;
3355 struct qeth_ipa_cmd *cmd;
3357 QETH_DBF_TEXT(TRACE, 4, "chgmac");
3359 iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
3360 sizeof(struct qeth_ipacmd_setadpparms));
3361 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3362 cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
3363 cmd->data.setadapterparms.data.change_addr.addr_size = OSA_ADDR_LEN;
3364 memcpy(&cmd->data.setadapterparms.data.change_addr.addr,
3365 card->dev->dev_addr, OSA_ADDR_LEN);
3366 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
3367 NULL);
3368 return rc;
3370 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
3372 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
3373 struct qeth_reply *reply, unsigned long data)
3375 struct qeth_ipa_cmd *cmd;
3376 struct qeth_set_access_ctrl *access_ctrl_req;
3377 int rc;
3379 QETH_DBF_TEXT(TRACE, 4, "setaccb");
3381 cmd = (struct qeth_ipa_cmd *) data;
3382 access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
3383 QETH_DBF_TEXT_(SETUP, 2, "setaccb");
3384 QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
3385 QETH_DBF_TEXT_(SETUP, 2, "rc=%d",
3386 cmd->data.setadapterparms.hdr.return_code);
3387 switch (cmd->data.setadapterparms.hdr.return_code) {
3388 case SET_ACCESS_CTRL_RC_SUCCESS:
3389 case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
3390 case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
3392 card->options.isolation = access_ctrl_req->subcmd_code;
3393 if (card->options.isolation == ISOLATION_MODE_NONE) {
3394 dev_info(&card->gdev->dev,
3395 "QDIO data connection isolation is deactivated\n");
3396 } else {
3397 dev_info(&card->gdev->dev,
3398 "QDIO data connection isolation is activated\n");
3400 QETH_DBF_MESSAGE(3, "OK:SET_ACCESS_CTRL(%s, %d)==%d\n",
3401 card->gdev->dev.kobj.name,
3402 access_ctrl_req->subcmd_code,
3403 cmd->data.setadapterparms.hdr.return_code);
3404 rc = 0;
3405 break;
3407 case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
3409 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%s,%d)==%d\n",
3410 card->gdev->dev.kobj.name,
3411 access_ctrl_req->subcmd_code,
3412 cmd->data.setadapterparms.hdr.return_code);
3413 dev_err(&card->gdev->dev, "Adapter does not "
3414 "support QDIO data connection isolation\n");
3416 /* ensure isolation mode is "none" */
3417 card->options.isolation = ISOLATION_MODE_NONE;
3418 rc = -EOPNOTSUPP;
3419 break;
3421 case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
3423 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d\n",
3424 card->gdev->dev.kobj.name,
3425 access_ctrl_req->subcmd_code,
3426 cmd->data.setadapterparms.hdr.return_code);
3427 dev_err(&card->gdev->dev,
3428 "Adapter is dedicated. "
3429 "QDIO data connection isolation not supported\n");
3431 /* ensure isolation mode is "none" */
3432 card->options.isolation = ISOLATION_MODE_NONE;
3433 rc = -EOPNOTSUPP;
3434 break;
3436 case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
3438 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d\n",
3439 card->gdev->dev.kobj.name,
3440 access_ctrl_req->subcmd_code,
3441 cmd->data.setadapterparms.hdr.return_code);
3442 dev_err(&card->gdev->dev,
3443 "TSO does not permit QDIO data connection isolation\n");
3445 /* ensure isolation mode is "none" */
3446 card->options.isolation = ISOLATION_MODE_NONE;
3447 rc = -EPERM;
3448 break;
3450 default:
3452 /* this should never happen */
3453 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_MODE(%s,%d)==%d"
3454 "==UNKNOWN\n",
3455 card->gdev->dev.kobj.name,
3456 access_ctrl_req->subcmd_code,
3457 cmd->data.setadapterparms.hdr.return_code);
3459 /* ensure isolation mode is "none" */
3460 card->options.isolation = ISOLATION_MODE_NONE;
3461 rc = 0;
3462 break;
3465 qeth_default_setadapterparms_cb(card, reply, (unsigned long) cmd);
3466 return rc;
3469 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
3470 enum qeth_ipa_isolation_modes isolation)
3472 int rc;
3473 struct qeth_cmd_buffer *iob;
3474 struct qeth_ipa_cmd *cmd;
3475 struct qeth_set_access_ctrl *access_ctrl_req;
3477 QETH_DBF_TEXT(TRACE, 4, "setacctl");
3479 QETH_DBF_TEXT_(SETUP, 2, "setacctl");
3480 QETH_DBF_TEXT_(SETUP, 2, "%s", card->gdev->dev.kobj.name);
3482 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
3483 sizeof(struct qeth_ipacmd_setadpparms_hdr) +
3484 sizeof(struct qeth_set_access_ctrl));
3485 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3486 access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
3487 access_ctrl_req->subcmd_code = isolation;
3489 rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
3490 NULL);
3491 QETH_DBF_TEXT_(SETUP, 2, "rc=%d", rc);
3492 return rc;
3495 int qeth_set_access_ctrl_online(struct qeth_card *card)
3497 int rc = 0;
3499 QETH_DBF_TEXT(TRACE, 4, "setactlo");
3501 if (card->info.type == QETH_CARD_TYPE_OSAE &&
3502 qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
3503 rc = qeth_setadpparms_set_access_ctrl(card,
3504 card->options.isolation);
3505 if (rc) {
3506 QETH_DBF_MESSAGE(3,
3507 "IPA(SET_ACCESS_CTRL,%s,%d) sent failed",
3508 card->gdev->dev.kobj.name,
3509 rc);
3511 } else if (card->options.isolation != ISOLATION_MODE_NONE) {
3512 card->options.isolation = ISOLATION_MODE_NONE;
3514 dev_err(&card->gdev->dev, "Adapter does not "
3515 "support QDIO data connection isolation\n");
3516 rc = -EOPNOTSUPP;
3518 return rc;
3520 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
3522 void qeth_tx_timeout(struct net_device *dev)
3524 struct qeth_card *card;
3526 QETH_DBF_TEXT(TRACE, 4, "txtimeo");
3527 card = dev->ml_priv;
3528 card->stats.tx_errors++;
3529 qeth_schedule_recovery(card);
3531 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
3533 int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
3535 struct qeth_card *card = dev->ml_priv;
3536 int rc = 0;
3538 switch (regnum) {
3539 case MII_BMCR: /* Basic mode control register */
3540 rc = BMCR_FULLDPLX;
3541 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
3542 (card->info.link_type != QETH_LINK_TYPE_OSN) &&
3543 (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH))
3544 rc |= BMCR_SPEED100;
3545 break;
3546 case MII_BMSR: /* Basic mode status register */
3547 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
3548 BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
3549 BMSR_100BASE4;
3550 break;
3551 case MII_PHYSID1: /* PHYS ID 1 */
3552 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
3553 dev->dev_addr[2];
3554 rc = (rc >> 5) & 0xFFFF;
3555 break;
3556 case MII_PHYSID2: /* PHYS ID 2 */
3557 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
3558 break;
3559 case MII_ADVERTISE: /* Advertisement control reg */
3560 rc = ADVERTISE_ALL;
3561 break;
3562 case MII_LPA: /* Link partner ability reg */
3563 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
3564 LPA_100BASE4 | LPA_LPACK;
3565 break;
3566 case MII_EXPANSION: /* Expansion register */
3567 break;
3568 case MII_DCOUNTER: /* disconnect counter */
3569 break;
3570 case MII_FCSCOUNTER: /* false carrier counter */
3571 break;
3572 case MII_NWAYTEST: /* N-way auto-neg test register */
3573 break;
3574 case MII_RERRCOUNTER: /* rx error counter */
3575 rc = card->stats.rx_errors;
3576 break;
3577 case MII_SREVISION: /* silicon revision */
3578 break;
3579 case MII_RESV1: /* reserved 1 */
3580 break;
3581 case MII_LBRERROR: /* loopback, rx, bypass error */
3582 break;
3583 case MII_PHYADDR: /* physical address */
3584 break;
3585 case MII_RESV2: /* reserved 2 */
3586 break;
3587 case MII_TPISTATUS: /* TPI status for 10mbps */
3588 break;
3589 case MII_NCONFIG: /* network interface config */
3590 break;
3591 default:
3592 break;
3594 return rc;
3596 EXPORT_SYMBOL_GPL(qeth_mdio_read);
3598 static int qeth_send_ipa_snmp_cmd(struct qeth_card *card,
3599 struct qeth_cmd_buffer *iob, int len,
3600 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
3601 unsigned long),
3602 void *reply_param)
3604 u16 s1, s2;
3606 QETH_DBF_TEXT(TRACE, 4, "sendsnmp");
3608 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
3609 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
3610 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
3611 /* adjust PDU length fields in IPA_PDU_HEADER */
3612 s1 = (u32) IPA_PDU_HEADER_SIZE + len;
3613 s2 = (u32) len;
3614 memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &s1, 2);
3615 memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &s2, 2);
3616 memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &s2, 2);
3617 memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &s2, 2);
3618 return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
3619 reply_cb, reply_param);
3622 static int qeth_snmp_command_cb(struct qeth_card *card,
3623 struct qeth_reply *reply, unsigned long sdata)
3625 struct qeth_ipa_cmd *cmd;
3626 struct qeth_arp_query_info *qinfo;
3627 struct qeth_snmp_cmd *snmp;
3628 unsigned char *data;
3629 __u16 data_len;
3631 QETH_DBF_TEXT(TRACE, 3, "snpcmdcb");
3633 cmd = (struct qeth_ipa_cmd *) sdata;
3634 data = (unsigned char *)((char *)cmd - reply->offset);
3635 qinfo = (struct qeth_arp_query_info *) reply->param;
3636 snmp = &cmd->data.setadapterparms.data.snmp;
3638 if (cmd->hdr.return_code) {
3639 QETH_DBF_TEXT_(TRACE, 4, "scer1%i", cmd->hdr.return_code);
3640 return 0;
3642 if (cmd->data.setadapterparms.hdr.return_code) {
3643 cmd->hdr.return_code =
3644 cmd->data.setadapterparms.hdr.return_code;
3645 QETH_DBF_TEXT_(TRACE, 4, "scer2%i", cmd->hdr.return_code);
3646 return 0;
3648 data_len = *((__u16 *)QETH_IPA_PDU_LEN_PDU1(data));
3649 if (cmd->data.setadapterparms.hdr.seq_no == 1)
3650 data_len -= (__u16)((char *)&snmp->data - (char *)cmd);
3651 else
3652 data_len -= (__u16)((char *)&snmp->request - (char *)cmd);
3654 /* check if there is enough room in userspace */
3655 if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
3656 QETH_DBF_TEXT_(TRACE, 4, "scer3%i", -ENOMEM);
3657 cmd->hdr.return_code = -ENOMEM;
3658 return 0;
3660 QETH_DBF_TEXT_(TRACE, 4, "snore%i",
3661 cmd->data.setadapterparms.hdr.used_total);
3662 QETH_DBF_TEXT_(TRACE, 4, "sseqn%i",
3663 cmd->data.setadapterparms.hdr.seq_no);
3664 /*copy entries to user buffer*/
3665 if (cmd->data.setadapterparms.hdr.seq_no == 1) {
3666 memcpy(qinfo->udata + qinfo->udata_offset,
3667 (char *)snmp,
3668 data_len + offsetof(struct qeth_snmp_cmd, data));
3669 qinfo->udata_offset += offsetof(struct qeth_snmp_cmd, data);
3670 } else {
3671 memcpy(qinfo->udata + qinfo->udata_offset,
3672 (char *)&snmp->request, data_len);
3674 qinfo->udata_offset += data_len;
3675 /* check if all replies received ... */
3676 QETH_DBF_TEXT_(TRACE, 4, "srtot%i",
3677 cmd->data.setadapterparms.hdr.used_total);
3678 QETH_DBF_TEXT_(TRACE, 4, "srseq%i",
3679 cmd->data.setadapterparms.hdr.seq_no);
3680 if (cmd->data.setadapterparms.hdr.seq_no <
3681 cmd->data.setadapterparms.hdr.used_total)
3682 return 1;
3683 return 0;
3686 int qeth_snmp_command(struct qeth_card *card, char __user *udata)
3688 struct qeth_cmd_buffer *iob;
3689 struct qeth_ipa_cmd *cmd;
3690 struct qeth_snmp_ureq *ureq;
3691 int req_len;
3692 struct qeth_arp_query_info qinfo = {0, };
3693 int rc = 0;
3695 QETH_DBF_TEXT(TRACE, 3, "snmpcmd");
3697 if (card->info.guestlan)
3698 return -EOPNOTSUPP;
3700 if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
3701 (!card->options.layer2)) {
3702 return -EOPNOTSUPP;
3704 /* skip 4 bytes (data_len struct member) to get req_len */
3705 if (copy_from_user(&req_len, udata + sizeof(int), sizeof(int)))
3706 return -EFAULT;
3707 ureq = kmalloc(req_len+sizeof(struct qeth_snmp_ureq_hdr), GFP_KERNEL);
3708 if (!ureq) {
3709 QETH_DBF_TEXT(TRACE, 2, "snmpnome");
3710 return -ENOMEM;
3712 if (copy_from_user(ureq, udata,
3713 req_len + sizeof(struct qeth_snmp_ureq_hdr))) {
3714 kfree(ureq);
3715 return -EFAULT;
3717 qinfo.udata_len = ureq->hdr.data_len;
3718 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
3719 if (!qinfo.udata) {
3720 kfree(ureq);
3721 return -ENOMEM;
3723 qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
3725 iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL,
3726 QETH_SNMP_SETADP_CMDLENGTH + req_len);
3727 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
3728 memcpy(&cmd->data.setadapterparms.data.snmp, &ureq->cmd, req_len);
3729 rc = qeth_send_ipa_snmp_cmd(card, iob, QETH_SETADP_BASE_LEN + req_len,
3730 qeth_snmp_command_cb, (void *)&qinfo);
3731 if (rc)
3732 QETH_DBF_MESSAGE(2, "SNMP command failed on %s: (0x%x)\n",
3733 QETH_CARD_IFNAME(card), rc);
3734 else {
3735 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
3736 rc = -EFAULT;
3739 kfree(ureq);
3740 kfree(qinfo.udata);
3741 return rc;
3743 EXPORT_SYMBOL_GPL(qeth_snmp_command);
3745 static inline int qeth_get_qdio_q_format(struct qeth_card *card)
3747 switch (card->info.type) {
3748 case QETH_CARD_TYPE_IQD:
3749 return 2;
3750 default:
3751 return 0;
3755 static int qeth_qdio_establish(struct qeth_card *card)
3757 struct qdio_initialize init_data;
3758 char *qib_param_field;
3759 struct qdio_buffer **in_sbal_ptrs;
3760 struct qdio_buffer **out_sbal_ptrs;
3761 int i, j, k;
3762 int rc = 0;
3764 QETH_DBF_TEXT(SETUP, 2, "qdioest");
3766 qib_param_field = kzalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(char),
3767 GFP_KERNEL);
3768 if (!qib_param_field)
3769 return -ENOMEM;
3771 qeth_create_qib_param_field(card, qib_param_field);
3772 qeth_create_qib_param_field_blkt(card, qib_param_field);
3774 in_sbal_ptrs = kmalloc(QDIO_MAX_BUFFERS_PER_Q * sizeof(void *),
3775 GFP_KERNEL);
3776 if (!in_sbal_ptrs) {
3777 kfree(qib_param_field);
3778 return -ENOMEM;
3780 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
3781 in_sbal_ptrs[i] = (struct qdio_buffer *)
3782 virt_to_phys(card->qdio.in_q->bufs[i].buffer);
3784 out_sbal_ptrs =
3785 kmalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q *
3786 sizeof(void *), GFP_KERNEL);
3787 if (!out_sbal_ptrs) {
3788 kfree(in_sbal_ptrs);
3789 kfree(qib_param_field);
3790 return -ENOMEM;
3792 for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
3793 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j, ++k) {
3794 out_sbal_ptrs[k] = (struct qdio_buffer *)virt_to_phys(
3795 card->qdio.out_qs[i]->bufs[j].buffer);
3798 memset(&init_data, 0, sizeof(struct qdio_initialize));
3799 init_data.cdev = CARD_DDEV(card);
3800 init_data.q_format = qeth_get_qdio_q_format(card);
3801 init_data.qib_param_field_format = 0;
3802 init_data.qib_param_field = qib_param_field;
3803 init_data.no_input_qs = 1;
3804 init_data.no_output_qs = card->qdio.no_out_queues;
3805 init_data.input_handler = card->discipline.input_handler;
3806 init_data.output_handler = card->discipline.output_handler;
3807 init_data.int_parm = (unsigned long) card;
3808 init_data.input_sbal_addr_array = (void **) in_sbal_ptrs;
3809 init_data.output_sbal_addr_array = (void **) out_sbal_ptrs;
3811 if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
3812 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
3813 rc = qdio_initialize(&init_data);
3814 if (rc)
3815 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
3817 kfree(out_sbal_ptrs);
3818 kfree(in_sbal_ptrs);
3819 kfree(qib_param_field);
3820 return rc;
3823 static void qeth_core_free_card(struct qeth_card *card)
3826 QETH_DBF_TEXT(SETUP, 2, "freecrd");
3827 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
3828 qeth_clean_channel(&card->read);
3829 qeth_clean_channel(&card->write);
3830 if (card->dev)
3831 free_netdev(card->dev);
3832 kfree(card->ip_tbd_list);
3833 qeth_free_qdio_buffers(card);
3834 unregister_service_level(&card->qeth_service_level);
3835 kfree(card);
3838 static struct ccw_device_id qeth_ids[] = {
3839 {CCW_DEVICE(0x1731, 0x01), .driver_info = QETH_CARD_TYPE_OSAE},
3840 {CCW_DEVICE(0x1731, 0x05), .driver_info = QETH_CARD_TYPE_IQD},
3841 {CCW_DEVICE(0x1731, 0x06), .driver_info = QETH_CARD_TYPE_OSN},
3844 MODULE_DEVICE_TABLE(ccw, qeth_ids);
3846 static struct ccw_driver qeth_ccw_driver = {
3847 .name = "qeth",
3848 .ids = qeth_ids,
3849 .probe = ccwgroup_probe_ccwdev,
3850 .remove = ccwgroup_remove_ccwdev,
3853 static int qeth_core_driver_group(const char *buf, struct device *root_dev,
3854 unsigned long driver_id)
3856 return ccwgroup_create_from_string(root_dev, driver_id,
3857 &qeth_ccw_driver, 3, buf);
3860 int qeth_core_hardsetup_card(struct qeth_card *card)
3862 int retries = 0;
3863 int rc;
3865 QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
3866 atomic_set(&card->force_alloc_skb, 0);
3867 retry:
3868 if (retries)
3869 QETH_DBF_MESSAGE(2, "%s Retrying to do IDX activates.\n",
3870 dev_name(&card->gdev->dev));
3871 ccw_device_set_offline(CARD_DDEV(card));
3872 ccw_device_set_offline(CARD_WDEV(card));
3873 ccw_device_set_offline(CARD_RDEV(card));
3874 rc = ccw_device_set_online(CARD_RDEV(card));
3875 if (rc)
3876 goto retriable;
3877 rc = ccw_device_set_online(CARD_WDEV(card));
3878 if (rc)
3879 goto retriable;
3880 rc = ccw_device_set_online(CARD_DDEV(card));
3881 if (rc)
3882 goto retriable;
3883 rc = qeth_qdio_clear_card(card, card->info.type != QETH_CARD_TYPE_IQD);
3884 retriable:
3885 if (rc == -ERESTARTSYS) {
3886 QETH_DBF_TEXT(SETUP, 2, "break1");
3887 return rc;
3888 } else if (rc) {
3889 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
3890 if (++retries > 3)
3891 goto out;
3892 else
3893 goto retry;
3895 qeth_init_tokens(card);
3896 qeth_init_func_level(card);
3897 rc = qeth_idx_activate_channel(&card->read, qeth_idx_read_cb);
3898 if (rc == -ERESTARTSYS) {
3899 QETH_DBF_TEXT(SETUP, 2, "break2");
3900 return rc;
3901 } else if (rc) {
3902 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
3903 if (--retries < 0)
3904 goto out;
3905 else
3906 goto retry;
3908 rc = qeth_idx_activate_channel(&card->write, qeth_idx_write_cb);
3909 if (rc == -ERESTARTSYS) {
3910 QETH_DBF_TEXT(SETUP, 2, "break3");
3911 return rc;
3912 } else if (rc) {
3913 QETH_DBF_TEXT_(SETUP, 2, "4err%d", rc);
3914 if (--retries < 0)
3915 goto out;
3916 else
3917 goto retry;
3919 rc = qeth_mpc_initialize(card);
3920 if (rc) {
3921 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
3922 goto out;
3924 return 0;
3925 out:
3926 dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
3927 "an error on the device\n");
3928 QETH_DBF_MESSAGE(2, "%s Initialization in hardsetup failed! rc=%d\n",
3929 dev_name(&card->gdev->dev), rc);
3930 return rc;
3932 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
3934 static inline int qeth_create_skb_frag(struct qdio_buffer_element *element,
3935 struct sk_buff **pskb, int offset, int *pfrag, int data_len)
3937 struct page *page = virt_to_page(element->addr);
3938 if (*pskb == NULL) {
3939 /* the upper protocol layers assume that there is data in the
3940 * skb itself. Copy a small amount (64 bytes) to make them
3941 * happy. */
3942 *pskb = dev_alloc_skb(64 + ETH_HLEN);
3943 if (!(*pskb))
3944 return -ENOMEM;
3945 skb_reserve(*pskb, ETH_HLEN);
3946 if (data_len <= 64) {
3947 memcpy(skb_put(*pskb, data_len), element->addr + offset,
3948 data_len);
3949 } else {
3950 get_page(page);
3951 memcpy(skb_put(*pskb, 64), element->addr + offset, 64);
3952 skb_fill_page_desc(*pskb, *pfrag, page, offset + 64,
3953 data_len - 64);
3954 (*pskb)->data_len += data_len - 64;
3955 (*pskb)->len += data_len - 64;
3956 (*pskb)->truesize += data_len - 64;
3957 (*pfrag)++;
3959 } else {
3960 get_page(page);
3961 skb_fill_page_desc(*pskb, *pfrag, page, offset, data_len);
3962 (*pskb)->data_len += data_len;
3963 (*pskb)->len += data_len;
3964 (*pskb)->truesize += data_len;
3965 (*pfrag)++;
3967 return 0;
3970 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
3971 struct qdio_buffer *buffer,
3972 struct qdio_buffer_element **__element, int *__offset,
3973 struct qeth_hdr **hdr)
3975 struct qdio_buffer_element *element = *__element;
3976 int offset = *__offset;
3977 struct sk_buff *skb = NULL;
3978 int skb_len = 0;
3979 void *data_ptr;
3980 int data_len;
3981 int headroom = 0;
3982 int use_rx_sg = 0;
3983 int frag = 0;
3985 /* qeth_hdr must not cross element boundaries */
3986 if (element->length < offset + sizeof(struct qeth_hdr)) {
3987 if (qeth_is_last_sbale(element))
3988 return NULL;
3989 element++;
3990 offset = 0;
3991 if (element->length < sizeof(struct qeth_hdr))
3992 return NULL;
3994 *hdr = element->addr + offset;
3996 offset += sizeof(struct qeth_hdr);
3997 switch ((*hdr)->hdr.l2.id) {
3998 case QETH_HEADER_TYPE_LAYER2:
3999 skb_len = (*hdr)->hdr.l2.pkt_length;
4000 break;
4001 case QETH_HEADER_TYPE_LAYER3:
4002 skb_len = (*hdr)->hdr.l3.length;
4003 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
4004 (card->info.link_type == QETH_LINK_TYPE_HSTR))
4005 headroom = TR_HLEN;
4006 else
4007 headroom = ETH_HLEN;
4008 break;
4009 case QETH_HEADER_TYPE_OSN:
4010 skb_len = (*hdr)->hdr.osn.pdu_length;
4011 headroom = sizeof(struct qeth_hdr);
4012 break;
4013 default:
4014 break;
4017 if (!skb_len)
4018 return NULL;
4020 if ((skb_len >= card->options.rx_sg_cb) &&
4021 (!(card->info.type == QETH_CARD_TYPE_OSN)) &&
4022 (!atomic_read(&card->force_alloc_skb))) {
4023 use_rx_sg = 1;
4024 } else {
4025 skb = dev_alloc_skb(skb_len + headroom);
4026 if (!skb)
4027 goto no_mem;
4028 if (headroom)
4029 skb_reserve(skb, headroom);
4032 data_ptr = element->addr + offset;
4033 while (skb_len) {
4034 data_len = min(skb_len, (int)(element->length - offset));
4035 if (data_len) {
4036 if (use_rx_sg) {
4037 if (qeth_create_skb_frag(element, &skb, offset,
4038 &frag, data_len))
4039 goto no_mem;
4040 } else {
4041 memcpy(skb_put(skb, data_len), data_ptr,
4042 data_len);
4045 skb_len -= data_len;
4046 if (skb_len) {
4047 if (qeth_is_last_sbale(element)) {
4048 QETH_DBF_TEXT(TRACE, 4, "unexeob");
4049 QETH_DBF_TEXT_(TRACE, 4, "%s",
4050 CARD_BUS_ID(card));
4051 QETH_DBF_TEXT(QERR, 2, "unexeob");
4052 QETH_DBF_TEXT_(QERR, 2, "%s",
4053 CARD_BUS_ID(card));
4054 QETH_DBF_HEX(MISC, 4, buffer, sizeof(*buffer));
4055 dev_kfree_skb_any(skb);
4056 card->stats.rx_errors++;
4057 return NULL;
4059 element++;
4060 offset = 0;
4061 data_ptr = element->addr;
4062 } else {
4063 offset += data_len;
4066 *__element = element;
4067 *__offset = offset;
4068 if (use_rx_sg && card->options.performance_stats) {
4069 card->perf_stats.sg_skbs_rx++;
4070 card->perf_stats.sg_frags_rx += skb_shinfo(skb)->nr_frags;
4072 return skb;
4073 no_mem:
4074 if (net_ratelimit()) {
4075 QETH_DBF_TEXT(TRACE, 2, "noskbmem");
4076 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_BUS_ID(card));
4078 card->stats.rx_dropped++;
4079 return NULL;
4081 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
4083 static void qeth_unregister_dbf_views(void)
4085 int x;
4086 for (x = 0; x < QETH_DBF_INFOS; x++) {
4087 debug_unregister(qeth_dbf[x].id);
4088 qeth_dbf[x].id = NULL;
4092 void qeth_dbf_longtext(enum qeth_dbf_names dbf_nix, int level, char *fmt, ...)
4094 char dbf_txt_buf[32];
4095 va_list args;
4097 if (level > (qeth_dbf[dbf_nix].id)->level)
4098 return;
4099 va_start(args, fmt);
4100 vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
4101 va_end(args);
4102 debug_text_event(qeth_dbf[dbf_nix].id, level, dbf_txt_buf);
4104 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
4106 static int qeth_register_dbf_views(void)
4108 int ret;
4109 int x;
4111 for (x = 0; x < QETH_DBF_INFOS; x++) {
4112 /* register the areas */
4113 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
4114 qeth_dbf[x].pages,
4115 qeth_dbf[x].areas,
4116 qeth_dbf[x].len);
4117 if (qeth_dbf[x].id == NULL) {
4118 qeth_unregister_dbf_views();
4119 return -ENOMEM;
4122 /* register a view */
4123 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
4124 if (ret) {
4125 qeth_unregister_dbf_views();
4126 return ret;
4129 /* set a passing level */
4130 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
4133 return 0;
4136 int qeth_core_load_discipline(struct qeth_card *card,
4137 enum qeth_discipline_id discipline)
4139 int rc = 0;
4140 switch (discipline) {
4141 case QETH_DISCIPLINE_LAYER3:
4142 card->discipline.ccwgdriver = try_then_request_module(
4143 symbol_get(qeth_l3_ccwgroup_driver),
4144 "qeth_l3");
4145 break;
4146 case QETH_DISCIPLINE_LAYER2:
4147 card->discipline.ccwgdriver = try_then_request_module(
4148 symbol_get(qeth_l2_ccwgroup_driver),
4149 "qeth_l2");
4150 break;
4152 if (!card->discipline.ccwgdriver) {
4153 dev_err(&card->gdev->dev, "There is no kernel module to "
4154 "support discipline %d\n", discipline);
4155 rc = -EINVAL;
4157 return rc;
4160 void qeth_core_free_discipline(struct qeth_card *card)
4162 if (card->options.layer2)
4163 symbol_put(qeth_l2_ccwgroup_driver);
4164 else
4165 symbol_put(qeth_l3_ccwgroup_driver);
4166 card->discipline.ccwgdriver = NULL;
4169 static void qeth_determine_capabilities(struct qeth_card *card)
4171 int rc;
4172 int length;
4173 char *prcd;
4175 QETH_DBF_TEXT(SETUP, 2, "detcapab");
4176 rc = ccw_device_set_online(CARD_DDEV(card));
4177 if (rc) {
4178 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4179 goto out;
4183 rc = qeth_read_conf_data(card, (void **) &prcd, &length);
4184 if (rc) {
4185 QETH_DBF_MESSAGE(2, "%s qeth_read_conf_data returned %i\n",
4186 dev_name(&card->gdev->dev), rc);
4187 QETH_DBF_TEXT_(SETUP, 2, "5err%d", rc);
4188 goto out_offline;
4190 qeth_configure_unitaddr(card, prcd);
4191 qeth_configure_blkt_default(card, prcd);
4192 kfree(prcd);
4194 rc = qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
4195 if (rc)
4196 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
4198 out_offline:
4199 ccw_device_set_offline(CARD_DDEV(card));
4200 out:
4201 return;
4204 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
4206 struct qeth_card *card;
4207 struct device *dev;
4208 int rc;
4209 unsigned long flags;
4211 QETH_DBF_TEXT(SETUP, 2, "probedev");
4213 dev = &gdev->dev;
4214 if (!get_device(dev))
4215 return -ENODEV;
4217 QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
4219 card = qeth_alloc_card();
4220 if (!card) {
4221 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
4222 rc = -ENOMEM;
4223 goto err_dev;
4225 card->read.ccwdev = gdev->cdev[0];
4226 card->write.ccwdev = gdev->cdev[1];
4227 card->data.ccwdev = gdev->cdev[2];
4228 dev_set_drvdata(&gdev->dev, card);
4229 card->gdev = gdev;
4230 gdev->cdev[0]->handler = qeth_irq;
4231 gdev->cdev[1]->handler = qeth_irq;
4232 gdev->cdev[2]->handler = qeth_irq;
4234 rc = qeth_determine_card_type(card);
4235 if (rc) {
4236 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
4237 goto err_card;
4239 rc = qeth_setup_card(card);
4240 if (rc) {
4241 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
4242 goto err_card;
4245 if (card->info.type == QETH_CARD_TYPE_OSN) {
4246 rc = qeth_core_create_osn_attributes(dev);
4247 if (rc)
4248 goto err_card;
4249 rc = qeth_core_load_discipline(card, QETH_DISCIPLINE_LAYER2);
4250 if (rc) {
4251 qeth_core_remove_osn_attributes(dev);
4252 goto err_card;
4254 rc = card->discipline.ccwgdriver->probe(card->gdev);
4255 if (rc) {
4256 qeth_core_free_discipline(card);
4257 qeth_core_remove_osn_attributes(dev);
4258 goto err_card;
4260 } else {
4261 rc = qeth_core_create_device_attributes(dev);
4262 if (rc)
4263 goto err_card;
4266 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
4267 list_add_tail(&card->list, &qeth_core_card_list.list);
4268 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
4270 qeth_determine_capabilities(card);
4271 return 0;
4273 err_card:
4274 qeth_core_free_card(card);
4275 err_dev:
4276 put_device(dev);
4277 return rc;
4280 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
4282 unsigned long flags;
4283 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4285 QETH_DBF_TEXT(SETUP, 2, "removedv");
4286 if (card->discipline.ccwgdriver) {
4287 card->discipline.ccwgdriver->remove(gdev);
4288 qeth_core_free_discipline(card);
4291 if (card->info.type == QETH_CARD_TYPE_OSN) {
4292 qeth_core_remove_osn_attributes(&gdev->dev);
4293 } else {
4294 qeth_core_remove_device_attributes(&gdev->dev);
4296 write_lock_irqsave(&qeth_core_card_list.rwlock, flags);
4297 list_del(&card->list);
4298 write_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
4299 qeth_core_free_card(card);
4300 dev_set_drvdata(&gdev->dev, NULL);
4301 put_device(&gdev->dev);
4302 return;
4305 static int qeth_core_set_online(struct ccwgroup_device *gdev)
4307 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4308 int rc = 0;
4309 int def_discipline;
4311 if (!card->discipline.ccwgdriver) {
4312 if (card->info.type == QETH_CARD_TYPE_IQD)
4313 def_discipline = QETH_DISCIPLINE_LAYER3;
4314 else
4315 def_discipline = QETH_DISCIPLINE_LAYER2;
4316 rc = qeth_core_load_discipline(card, def_discipline);
4317 if (rc)
4318 goto err;
4319 rc = card->discipline.ccwgdriver->probe(card->gdev);
4320 if (rc)
4321 goto err;
4323 rc = card->discipline.ccwgdriver->set_online(gdev);
4324 err:
4325 return rc;
4328 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
4330 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4331 return card->discipline.ccwgdriver->set_offline(gdev);
4334 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
4336 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4337 if (card->discipline.ccwgdriver &&
4338 card->discipline.ccwgdriver->shutdown)
4339 card->discipline.ccwgdriver->shutdown(gdev);
4342 static int qeth_core_prepare(struct ccwgroup_device *gdev)
4344 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4345 if (card->discipline.ccwgdriver &&
4346 card->discipline.ccwgdriver->prepare)
4347 return card->discipline.ccwgdriver->prepare(gdev);
4348 return 0;
4351 static void qeth_core_complete(struct ccwgroup_device *gdev)
4353 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4354 if (card->discipline.ccwgdriver &&
4355 card->discipline.ccwgdriver->complete)
4356 card->discipline.ccwgdriver->complete(gdev);
4359 static int qeth_core_freeze(struct ccwgroup_device *gdev)
4361 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4362 if (card->discipline.ccwgdriver &&
4363 card->discipline.ccwgdriver->freeze)
4364 return card->discipline.ccwgdriver->freeze(gdev);
4365 return 0;
4368 static int qeth_core_thaw(struct ccwgroup_device *gdev)
4370 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4371 if (card->discipline.ccwgdriver &&
4372 card->discipline.ccwgdriver->thaw)
4373 return card->discipline.ccwgdriver->thaw(gdev);
4374 return 0;
4377 static int qeth_core_restore(struct ccwgroup_device *gdev)
4379 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
4380 if (card->discipline.ccwgdriver &&
4381 card->discipline.ccwgdriver->restore)
4382 return card->discipline.ccwgdriver->restore(gdev);
4383 return 0;
4386 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
4387 .owner = THIS_MODULE,
4388 .name = "qeth",
4389 .driver_id = 0xD8C5E3C8,
4390 .probe = qeth_core_probe_device,
4391 .remove = qeth_core_remove_device,
4392 .set_online = qeth_core_set_online,
4393 .set_offline = qeth_core_set_offline,
4394 .shutdown = qeth_core_shutdown,
4395 .prepare = qeth_core_prepare,
4396 .complete = qeth_core_complete,
4397 .freeze = qeth_core_freeze,
4398 .thaw = qeth_core_thaw,
4399 .restore = qeth_core_restore,
4402 static ssize_t
4403 qeth_core_driver_group_store(struct device_driver *ddrv, const char *buf,
4404 size_t count)
4406 int err;
4407 err = qeth_core_driver_group(buf, qeth_core_root_dev,
4408 qeth_core_ccwgroup_driver.driver_id);
4409 if (err)
4410 return err;
4411 else
4412 return count;
4415 static DRIVER_ATTR(group, 0200, NULL, qeth_core_driver_group_store);
4417 static struct {
4418 const char str[ETH_GSTRING_LEN];
4419 } qeth_ethtool_stats_keys[] = {
4420 /* 0 */{"rx skbs"},
4421 {"rx buffers"},
4422 {"tx skbs"},
4423 {"tx buffers"},
4424 {"tx skbs no packing"},
4425 {"tx buffers no packing"},
4426 {"tx skbs packing"},
4427 {"tx buffers packing"},
4428 {"tx sg skbs"},
4429 {"tx sg frags"},
4430 /* 10 */{"rx sg skbs"},
4431 {"rx sg frags"},
4432 {"rx sg page allocs"},
4433 {"tx large kbytes"},
4434 {"tx large count"},
4435 {"tx pk state ch n->p"},
4436 {"tx pk state ch p->n"},
4437 {"tx pk watermark low"},
4438 {"tx pk watermark high"},
4439 {"queue 0 buffer usage"},
4440 /* 20 */{"queue 1 buffer usage"},
4441 {"queue 2 buffer usage"},
4442 {"queue 3 buffer usage"},
4443 {"rx handler time"},
4444 {"rx handler count"},
4445 {"rx do_QDIO time"},
4446 {"rx do_QDIO count"},
4447 {"tx handler time"},
4448 {"tx handler count"},
4449 {"tx time"},
4450 /* 30 */{"tx count"},
4451 {"tx do_QDIO time"},
4452 {"tx do_QDIO count"},
4453 {"tx csum"},
4454 {"tx lin"},
4457 int qeth_core_get_sset_count(struct net_device *dev, int stringset)
4459 switch (stringset) {
4460 case ETH_SS_STATS:
4461 return (sizeof(qeth_ethtool_stats_keys) / ETH_GSTRING_LEN);
4462 default:
4463 return -EINVAL;
4466 EXPORT_SYMBOL_GPL(qeth_core_get_sset_count);
4468 void qeth_core_get_ethtool_stats(struct net_device *dev,
4469 struct ethtool_stats *stats, u64 *data)
4471 struct qeth_card *card = dev->ml_priv;
4472 data[0] = card->stats.rx_packets -
4473 card->perf_stats.initial_rx_packets;
4474 data[1] = card->perf_stats.bufs_rec;
4475 data[2] = card->stats.tx_packets -
4476 card->perf_stats.initial_tx_packets;
4477 data[3] = card->perf_stats.bufs_sent;
4478 data[4] = card->stats.tx_packets - card->perf_stats.initial_tx_packets
4479 - card->perf_stats.skbs_sent_pack;
4480 data[5] = card->perf_stats.bufs_sent - card->perf_stats.bufs_sent_pack;
4481 data[6] = card->perf_stats.skbs_sent_pack;
4482 data[7] = card->perf_stats.bufs_sent_pack;
4483 data[8] = card->perf_stats.sg_skbs_sent;
4484 data[9] = card->perf_stats.sg_frags_sent;
4485 data[10] = card->perf_stats.sg_skbs_rx;
4486 data[11] = card->perf_stats.sg_frags_rx;
4487 data[12] = card->perf_stats.sg_alloc_page_rx;
4488 data[13] = (card->perf_stats.large_send_bytes >> 10);
4489 data[14] = card->perf_stats.large_send_cnt;
4490 data[15] = card->perf_stats.sc_dp_p;
4491 data[16] = card->perf_stats.sc_p_dp;
4492 data[17] = QETH_LOW_WATERMARK_PACK;
4493 data[18] = QETH_HIGH_WATERMARK_PACK;
4494 data[19] = atomic_read(&card->qdio.out_qs[0]->used_buffers);
4495 data[20] = (card->qdio.no_out_queues > 1) ?
4496 atomic_read(&card->qdio.out_qs[1]->used_buffers) : 0;
4497 data[21] = (card->qdio.no_out_queues > 2) ?
4498 atomic_read(&card->qdio.out_qs[2]->used_buffers) : 0;
4499 data[22] = (card->qdio.no_out_queues > 3) ?
4500 atomic_read(&card->qdio.out_qs[3]->used_buffers) : 0;
4501 data[23] = card->perf_stats.inbound_time;
4502 data[24] = card->perf_stats.inbound_cnt;
4503 data[25] = card->perf_stats.inbound_do_qdio_time;
4504 data[26] = card->perf_stats.inbound_do_qdio_cnt;
4505 data[27] = card->perf_stats.outbound_handler_time;
4506 data[28] = card->perf_stats.outbound_handler_cnt;
4507 data[29] = card->perf_stats.outbound_time;
4508 data[30] = card->perf_stats.outbound_cnt;
4509 data[31] = card->perf_stats.outbound_do_qdio_time;
4510 data[32] = card->perf_stats.outbound_do_qdio_cnt;
4511 data[33] = card->perf_stats.tx_csum;
4512 data[34] = card->perf_stats.tx_lin;
4514 EXPORT_SYMBOL_GPL(qeth_core_get_ethtool_stats);
4516 void qeth_core_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4518 switch (stringset) {
4519 case ETH_SS_STATS:
4520 memcpy(data, &qeth_ethtool_stats_keys,
4521 sizeof(qeth_ethtool_stats_keys));
4522 break;
4523 default:
4524 WARN_ON(1);
4525 break;
4528 EXPORT_SYMBOL_GPL(qeth_core_get_strings);
4530 void qeth_core_get_drvinfo(struct net_device *dev,
4531 struct ethtool_drvinfo *info)
4533 struct qeth_card *card = dev->ml_priv;
4534 if (card->options.layer2)
4535 strcpy(info->driver, "qeth_l2");
4536 else
4537 strcpy(info->driver, "qeth_l3");
4539 strcpy(info->version, "1.0");
4540 strcpy(info->fw_version, card->info.mcl_level);
4541 sprintf(info->bus_info, "%s/%s/%s",
4542 CARD_RDEV_ID(card),
4543 CARD_WDEV_ID(card),
4544 CARD_DDEV_ID(card));
4546 EXPORT_SYMBOL_GPL(qeth_core_get_drvinfo);
4548 int qeth_core_ethtool_get_settings(struct net_device *netdev,
4549 struct ethtool_cmd *ecmd)
4551 struct qeth_card *card = netdev->ml_priv;
4552 enum qeth_link_types link_type;
4554 if ((card->info.type == QETH_CARD_TYPE_IQD) || (card->info.guestlan))
4555 link_type = QETH_LINK_TYPE_10GBIT_ETH;
4556 else
4557 link_type = card->info.link_type;
4559 ecmd->transceiver = XCVR_INTERNAL;
4560 ecmd->supported = SUPPORTED_Autoneg;
4561 ecmd->advertising = ADVERTISED_Autoneg;
4562 ecmd->duplex = DUPLEX_FULL;
4563 ecmd->autoneg = AUTONEG_ENABLE;
4565 switch (link_type) {
4566 case QETH_LINK_TYPE_FAST_ETH:
4567 case QETH_LINK_TYPE_LANE_ETH100:
4568 ecmd->supported |= SUPPORTED_10baseT_Half |
4569 SUPPORTED_10baseT_Full |
4570 SUPPORTED_100baseT_Half |
4571 SUPPORTED_100baseT_Full |
4572 SUPPORTED_TP;
4573 ecmd->advertising |= ADVERTISED_10baseT_Half |
4574 ADVERTISED_10baseT_Full |
4575 ADVERTISED_100baseT_Half |
4576 ADVERTISED_100baseT_Full |
4577 ADVERTISED_TP;
4578 ecmd->speed = SPEED_100;
4579 ecmd->port = PORT_TP;
4580 break;
4582 case QETH_LINK_TYPE_GBIT_ETH:
4583 case QETH_LINK_TYPE_LANE_ETH1000:
4584 ecmd->supported |= SUPPORTED_10baseT_Half |
4585 SUPPORTED_10baseT_Full |
4586 SUPPORTED_100baseT_Half |
4587 SUPPORTED_100baseT_Full |
4588 SUPPORTED_1000baseT_Half |
4589 SUPPORTED_1000baseT_Full |
4590 SUPPORTED_FIBRE;
4591 ecmd->advertising |= ADVERTISED_10baseT_Half |
4592 ADVERTISED_10baseT_Full |
4593 ADVERTISED_100baseT_Half |
4594 ADVERTISED_100baseT_Full |
4595 ADVERTISED_1000baseT_Half |
4596 ADVERTISED_1000baseT_Full |
4597 ADVERTISED_FIBRE;
4598 ecmd->speed = SPEED_1000;
4599 ecmd->port = PORT_FIBRE;
4600 break;
4602 case QETH_LINK_TYPE_10GBIT_ETH:
4603 ecmd->supported |= SUPPORTED_10baseT_Half |
4604 SUPPORTED_10baseT_Full |
4605 SUPPORTED_100baseT_Half |
4606 SUPPORTED_100baseT_Full |
4607 SUPPORTED_1000baseT_Half |
4608 SUPPORTED_1000baseT_Full |
4609 SUPPORTED_10000baseT_Full |
4610 SUPPORTED_FIBRE;
4611 ecmd->advertising |= ADVERTISED_10baseT_Half |
4612 ADVERTISED_10baseT_Full |
4613 ADVERTISED_100baseT_Half |
4614 ADVERTISED_100baseT_Full |
4615 ADVERTISED_1000baseT_Half |
4616 ADVERTISED_1000baseT_Full |
4617 ADVERTISED_10000baseT_Full |
4618 ADVERTISED_FIBRE;
4619 ecmd->speed = SPEED_10000;
4620 ecmd->port = PORT_FIBRE;
4621 break;
4623 default:
4624 ecmd->supported |= SUPPORTED_10baseT_Half |
4625 SUPPORTED_10baseT_Full |
4626 SUPPORTED_TP;
4627 ecmd->advertising |= ADVERTISED_10baseT_Half |
4628 ADVERTISED_10baseT_Full |
4629 ADVERTISED_TP;
4630 ecmd->speed = SPEED_10;
4631 ecmd->port = PORT_TP;
4634 return 0;
4636 EXPORT_SYMBOL_GPL(qeth_core_ethtool_get_settings);
4638 static int __init qeth_core_init(void)
4640 int rc;
4642 pr_info("loading core functions\n");
4643 INIT_LIST_HEAD(&qeth_core_card_list.list);
4644 rwlock_init(&qeth_core_card_list.rwlock);
4646 rc = qeth_register_dbf_views();
4647 if (rc)
4648 goto out_err;
4649 rc = ccw_driver_register(&qeth_ccw_driver);
4650 if (rc)
4651 goto ccw_err;
4652 rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
4653 if (rc)
4654 goto ccwgroup_err;
4655 rc = driver_create_file(&qeth_core_ccwgroup_driver.driver,
4656 &driver_attr_group);
4657 if (rc)
4658 goto driver_err;
4659 qeth_core_root_dev = root_device_register("qeth");
4660 rc = IS_ERR(qeth_core_root_dev) ? PTR_ERR(qeth_core_root_dev) : 0;
4661 if (rc)
4662 goto register_err;
4664 qeth_core_header_cache = kmem_cache_create("qeth_hdr",
4665 sizeof(struct qeth_hdr) + ETH_HLEN, 64, 0, NULL);
4666 if (!qeth_core_header_cache) {
4667 rc = -ENOMEM;
4668 goto slab_err;
4671 return 0;
4672 slab_err:
4673 root_device_unregister(qeth_core_root_dev);
4674 register_err:
4675 driver_remove_file(&qeth_core_ccwgroup_driver.driver,
4676 &driver_attr_group);
4677 driver_err:
4678 ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
4679 ccwgroup_err:
4680 ccw_driver_unregister(&qeth_ccw_driver);
4681 ccw_err:
4682 QETH_DBF_MESSAGE(2, "Initialization failed with code %d\n", rc);
4683 qeth_unregister_dbf_views();
4684 out_err:
4685 pr_err("Initializing the qeth device driver failed\n");
4686 return rc;
4689 static void __exit qeth_core_exit(void)
4691 root_device_unregister(qeth_core_root_dev);
4692 driver_remove_file(&qeth_core_ccwgroup_driver.driver,
4693 &driver_attr_group);
4694 ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
4695 ccw_driver_unregister(&qeth_ccw_driver);
4696 kmem_cache_destroy(qeth_core_header_cache);
4697 qeth_unregister_dbf_views();
4698 pr_info("core functions removed\n");
4701 module_init(qeth_core_init);
4702 module_exit(qeth_core_exit);
4703 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
4704 MODULE_DESCRIPTION("qeth core functions");
4705 MODULE_LICENSE("GPL");