711ab5097bd14b1e99c9c89d144fa28b5d524f41
[linux-2.6-microblaze.git] / drivers / s390 / net / qeth_core.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *    Copyright IBM Corp. 2007
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *               Frank Pavlic <fpavlic@de.ibm.com>,
6  *               Thomas Spatzier <tspat@de.ibm.com>,
7  *               Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9
10 #ifndef __QETH_CORE_H__
11 #define __QETH_CORE_H__
12
13 #include <linux/completion.h>
14 #include <linux/debugfs.h>
15 #include <linux/if.h>
16 #include <linux/if_arp.h>
17 #include <linux/etherdevice.h>
18 #include <linux/if_vlan.h>
19 #include <linux/ctype.h>
20 #include <linux/in6.h>
21 #include <linux/bitops.h>
22 #include <linux/seq_file.h>
23 #include <linux/hashtable.h>
24 #include <linux/ip.h>
25 #include <linux/rcupdate.h>
26 #include <linux/refcount.h>
27 #include <linux/timer.h>
28 #include <linux/types.h>
29 #include <linux/wait.h>
30 #include <linux/workqueue.h>
31
32 #include <net/dst.h>
33 #include <net/ip6_fib.h>
34 #include <net/ipv6.h>
35 #include <net/if_inet6.h>
36 #include <net/addrconf.h>
37 #include <net/route.h>
38 #include <net/sch_generic.h>
39 #include <net/tcp.h>
40
41 #include <asm/debug.h>
42 #include <asm/qdio.h>
43 #include <asm/ccwdev.h>
44 #include <asm/ccwgroup.h>
45 #include <asm/sysinfo.h>
46
47 #include <uapi/linux/if_link.h>
48
49 #include "qeth_core_mpc.h"
50
51 /**
52  * Debug Facility stuff
53  */
54 enum qeth_dbf_names {
55         QETH_DBF_SETUP,
56         QETH_DBF_MSG,
57         QETH_DBF_CTRL,
58         QETH_DBF_INFOS  /* must be last element */
59 };
60
61 struct qeth_dbf_info {
62         char name[DEBUG_MAX_NAME_LEN];
63         int pages;
64         int areas;
65         int len;
66         int level;
67         struct debug_view *view;
68         debug_info_t *id;
69 };
70
71 #define QETH_DBF_CTRL_LEN 256U
72
73 #define QETH_DBF_TEXT(name, level, text) \
74         debug_text_event(qeth_dbf[QETH_DBF_##name].id, level, text)
75
76 #define QETH_DBF_HEX(name, level, addr, len) \
77         debug_event(qeth_dbf[QETH_DBF_##name].id, level, (void *)(addr), len)
78
79 #define QETH_DBF_MESSAGE(level, text...) \
80         debug_sprintf_event(qeth_dbf[QETH_DBF_MSG].id, level, text)
81
82 #define QETH_DBF_TEXT_(name, level, text...) \
83         qeth_dbf_longtext(qeth_dbf[QETH_DBF_##name].id, level, text)
84
85 #define QETH_CARD_TEXT(card, level, text) \
86         debug_text_event(card->debug, level, text)
87
88 #define QETH_CARD_HEX(card, level, addr, len) \
89         debug_event(card->debug, level, (void *)(addr), len)
90
91 #define QETH_CARD_MESSAGE(card, text...) \
92         debug_sprintf_event(card->debug, level, text)
93
94 #define QETH_CARD_TEXT_(card, level, text...) \
95         qeth_dbf_longtext(card->debug, level, text)
96
97 #define SENSE_COMMAND_REJECT_BYTE 0
98 #define SENSE_COMMAND_REJECT_FLAG 0x80
99 #define SENSE_RESETTING_EVENT_BYTE 1
100 #define SENSE_RESETTING_EVENT_FLAG 0x80
101
102 static inline u32 qeth_get_device_id(struct ccw_device *cdev)
103 {
104         struct ccw_dev_id dev_id;
105         u32 id;
106
107         ccw_device_get_id(cdev, &dev_id);
108         id = dev_id.devno;
109         id |= (u32) (dev_id.ssid << 16);
110
111         return id;
112 }
113
114 /*
115  * Common IO related definitions
116  */
117 #define CARD_RDEV(card) card->read.ccwdev
118 #define CARD_WDEV(card) card->write.ccwdev
119 #define CARD_DDEV(card) card->data.ccwdev
120 #define CARD_BUS_ID(card) dev_name(&card->gdev->dev)
121 #define CARD_RDEV_ID(card) dev_name(&card->read.ccwdev->dev)
122 #define CARD_WDEV_ID(card) dev_name(&card->write.ccwdev->dev)
123 #define CARD_DDEV_ID(card) dev_name(&card->data.ccwdev->dev)
124 #define CCW_DEVID(cdev)         (qeth_get_device_id(cdev))
125 #define CARD_DEVID(card)        (CCW_DEVID(CARD_RDEV(card)))
126
127 /* Routing stuff */
128 struct qeth_routing_info {
129         enum qeth_routing_types type;
130 };
131
132 /* SETBRIDGEPORT stuff */
133 enum qeth_sbp_roles {
134         QETH_SBP_ROLE_NONE      = 0,
135         QETH_SBP_ROLE_PRIMARY   = 1,
136         QETH_SBP_ROLE_SECONDARY = 2,
137 };
138
139 enum qeth_sbp_states {
140         QETH_SBP_STATE_INACTIVE = 0,
141         QETH_SBP_STATE_STANDBY  = 1,
142         QETH_SBP_STATE_ACTIVE   = 2,
143 };
144
145 #define QETH_SBP_HOST_NOTIFICATION 1
146
147 struct qeth_sbp_info {
148         __u32 supported_funcs;
149         enum qeth_sbp_roles role;
150         __u32 hostnotification:1;
151         __u32 reflect_promisc:1;
152         __u32 reflect_promisc_primary:1;
153 };
154
155 struct qeth_vnicc_info {
156         /* supported/currently configured VNICCs; updated in IPA exchanges */
157         u32 sup_chars;
158         u32 cur_chars;
159         /* supported commands: bitmasks which VNICCs support respective cmd */
160         u32 set_char_sup;
161         u32 getset_timeout_sup;
162         /* timeout value for the learning characteristic */
163         u32 learning_timeout;
164         /* characteristics wanted/configured by user */
165         u32 wanted_chars;
166         /* has user explicitly enabled rx_bcast while online? */
167         bool rx_bcast_enabled;
168 };
169
170 #define QETH_IDX_FUNC_LEVEL_OSD          0x0101
171 #define QETH_IDX_FUNC_LEVEL_IQD          0x4108
172
173 #define QETH_BUFSIZE            4096
174 #define CCW_CMD_WRITE           0x01
175 #define CCW_CMD_READ            0x02
176
177 /**
178  * some more defs
179  */
180 #define QETH_TX_TIMEOUT         100 * HZ
181 #define QETH_RCD_TIMEOUT        60 * HZ
182 #define QETH_RECLAIM_WORK_TIME  HZ
183 #define QETH_MAX_PORTNO         15
184
185 /*****************************************************************************/
186 /* QDIO queue and buffer handling                                            */
187 /*****************************************************************************/
188 #define QETH_MAX_OUT_QUEUES     4
189 #define QETH_IQD_MIN_TXQ        2       /* One for ucast, one for mcast. */
190 #define QETH_IQD_MCAST_TXQ      0
191 #define QETH_IQD_MIN_UCAST_TXQ  1
192
193 #define QETH_MAX_IN_QUEUES      2
194 #define QETH_RX_COPYBREAK      (PAGE_SIZE >> 1)
195 #define QETH_IN_BUF_SIZE_DEFAULT 65536
196 #define QETH_IN_BUF_COUNT_DEFAULT 64
197 #define QETH_IN_BUF_COUNT_HSDEFAULT 128
198 #define QETH_IN_BUF_COUNT_MIN   8U
199 #define QETH_IN_BUF_COUNT_MAX   128U
200 #define QETH_MAX_BUFFER_ELEMENTS(card) ((card)->qdio.in_buf_size >> 12)
201 #define QETH_IN_BUF_REQUEUE_THRESHOLD(card) \
202                  ((card)->qdio.in_buf_pool.buf_count / 2)
203
204 /* buffers we have to be behind before we get a PCI */
205 #define QETH_PCI_THRESHOLD_A(card) ((card)->qdio.in_buf_pool.buf_count+1)
206 /*enqueued free buffers left before we get a PCI*/
207 #define QETH_PCI_THRESHOLD_B(card) 0
208 /*not used unless the microcode gets patched*/
209 #define QETH_PCI_TIMER_VALUE(card) 3
210
211 /* priority queing */
212 #define QETH_PRIOQ_DEFAULT QETH_NO_PRIO_QUEUEING
213 #define QETH_DEFAULT_QUEUE    2
214 #define QETH_NO_PRIO_QUEUEING 0
215 #define QETH_PRIO_Q_ING_PREC  1
216 #define QETH_PRIO_Q_ING_TOS   2
217 #define QETH_PRIO_Q_ING_SKB   3
218 #define QETH_PRIO_Q_ING_VLAN  4
219 #define QETH_PRIO_Q_ING_FIXED 5
220
221 /* Packing */
222 #define QETH_LOW_WATERMARK_PACK  2
223 #define QETH_HIGH_WATERMARK_PACK 5
224 #define QETH_WATERMARK_PACK_FUZZ 1
225
226 struct qeth_hdr_layer3 {
227         __u8  id;
228         __u8  flags;
229         __u16 inbound_checksum; /*TSO:__u16 seqno */
230         __u32 token;            /*TSO: __u32 reserved */
231         __u16 length;
232         __u8  vlan_prio;
233         __u8  ext_flags;
234         __u16 vlan_id;
235         __u16 frame_offset;
236         union {
237                 /* TX: */
238                 struct in6_addr addr;
239                 /* RX: */
240                 struct rx {
241                         u8 res1[2];
242                         u8 src_mac[6];
243                         u8 res2[4];
244                         u16 vlan_id;
245                         u8 res3[2];
246                 } rx;
247         } next_hop;
248 };
249
250 struct qeth_hdr_layer2 {
251         __u8 id;
252         __u8 flags[3];
253         __u8 port_no;
254         __u8 hdr_length;
255         __u16 pkt_length;
256         __u16 seq_no;
257         __u16 vlan_id;
258         __u32 reserved;
259         __u8 reserved2[16];
260 } __attribute__ ((packed));
261
262 struct qeth_hdr_osn {
263         __u8 id;
264         __u8 reserved;
265         __u16 seq_no;
266         __u16 reserved2;
267         __u16 control_flags;
268         __u16 pdu_length;
269         __u8 reserved3[18];
270         __u32 ccid;
271 } __attribute__ ((packed));
272
273 struct qeth_hdr {
274         union {
275                 struct qeth_hdr_layer2 l2;
276                 struct qeth_hdr_layer3 l3;
277                 struct qeth_hdr_osn    osn;
278         } hdr;
279 } __attribute__ ((packed));
280
281 /*TCP Segmentation Offload header*/
282 struct qeth_hdr_ext_tso {
283         __u16 hdr_tot_len;
284         __u8  imb_hdr_no;
285         __u8  reserved;
286         __u8  hdr_type;
287         __u8  hdr_version;
288         __u16 hdr_len;
289         __u32 payload_len;
290         __u16 mss;
291         __u16 dg_hdr_len;
292         __u8  padding[16];
293 } __attribute__ ((packed));
294
295 struct qeth_hdr_tso {
296         struct qeth_hdr hdr;    /*hdr->hdr.l3.xxx*/
297         struct qeth_hdr_ext_tso ext;
298 } __attribute__ ((packed));
299
300
301 /* flags for qeth_hdr.flags */
302 #define QETH_HDR_PASSTHRU 0x10
303 #define QETH_HDR_IPV6     0x80
304 #define QETH_HDR_CAST_MASK 0x07
305 enum qeth_cast_flags {
306         QETH_CAST_UNICAST   = 0x06,
307         QETH_CAST_MULTICAST = 0x04,
308         QETH_CAST_BROADCAST = 0x05,
309         QETH_CAST_ANYCAST   = 0x07,
310         QETH_CAST_NOCAST    = 0x00,
311 };
312
313 enum qeth_layer2_frame_flags {
314         QETH_LAYER2_FLAG_MULTICAST = 0x01,
315         QETH_LAYER2_FLAG_BROADCAST = 0x02,
316         QETH_LAYER2_FLAG_UNICAST   = 0x04,
317         QETH_LAYER2_FLAG_VLAN      = 0x10,
318 };
319
320 enum qeth_header_ids {
321         QETH_HEADER_TYPE_LAYER3 = 0x01,
322         QETH_HEADER_TYPE_LAYER2 = 0x02,
323         QETH_HEADER_TYPE_L3_TSO = 0x03,
324         QETH_HEADER_TYPE_OSN    = 0x04,
325         QETH_HEADER_TYPE_L2_TSO = 0x06,
326         QETH_HEADER_MASK_INVAL  = 0x80,
327 };
328 /* flags for qeth_hdr.ext_flags */
329 #define QETH_HDR_EXT_VLAN_FRAME       0x01
330 #define QETH_HDR_EXT_TOKEN_ID         0x02
331 #define QETH_HDR_EXT_INCLUDE_VLAN_TAG 0x04
332 #define QETH_HDR_EXT_SRC_MAC_ADDR     0x08
333 #define QETH_HDR_EXT_CSUM_HDR_REQ     0x10
334 #define QETH_HDR_EXT_CSUM_TRANSP_REQ  0x20
335 #define QETH_HDR_EXT_UDP              0x40 /*bit off for TCP*/
336
337 static inline bool qeth_l2_same_vlan(struct qeth_hdr_layer2 *h1,
338                                      struct qeth_hdr_layer2 *h2)
339 {
340         return !((h1->flags[2] ^ h2->flags[2]) & QETH_LAYER2_FLAG_VLAN) &&
341                h1->vlan_id == h2->vlan_id;
342 }
343
344 static inline bool qeth_l3_iqd_same_vlan(struct qeth_hdr_layer3 *h1,
345                                          struct qeth_hdr_layer3 *h2)
346 {
347         return !((h1->ext_flags ^ h2->ext_flags) & QETH_HDR_EXT_VLAN_FRAME) &&
348                h1->vlan_id == h2->vlan_id;
349 }
350
351 static inline bool qeth_l3_same_next_hop(struct qeth_hdr_layer3 *h1,
352                                          struct qeth_hdr_layer3 *h2)
353 {
354         return !((h1->flags ^ h2->flags) & QETH_HDR_IPV6) &&
355                ipv6_addr_equal(&h1->next_hop.addr, &h2->next_hop.addr);
356 }
357
358 struct qeth_local_addr {
359         struct hlist_node hnode;
360         struct rcu_head rcu;
361         struct in6_addr addr;
362 };
363
364 enum qeth_qdio_info_states {
365         QETH_QDIO_UNINITIALIZED,
366         QETH_QDIO_ALLOCATED,
367         QETH_QDIO_ESTABLISHED,
368         QETH_QDIO_CLEANING
369 };
370
371 struct qeth_buffer_pool_entry {
372         struct list_head list;
373         struct list_head init_list;
374         struct page *elements[QDIO_MAX_ELEMENTS_PER_BUFFER];
375 };
376
377 struct qeth_qdio_buffer_pool {
378         struct list_head entry_list;
379         int buf_count;
380 };
381
382 struct qeth_qdio_buffer {
383         struct qdio_buffer *buffer;
384         /* the buffer pool entry currently associated to this buffer */
385         struct qeth_buffer_pool_entry *pool_entry;
386         struct sk_buff *rx_skb;
387 };
388
389 struct qeth_qdio_q {
390         struct qdio_buffer *qdio_bufs[QDIO_MAX_BUFFERS_PER_Q];
391         struct qeth_qdio_buffer bufs[QDIO_MAX_BUFFERS_PER_Q];
392         int next_buf_to_init;
393 };
394
395 enum qeth_qdio_out_buffer_state {
396         /* Owned by driver, in order to be filled. */
397         QETH_QDIO_BUF_EMPTY,
398         /* Filled by driver; owned by hardware in order to be sent. */
399         QETH_QDIO_BUF_PRIMED,
400         /* Identified to be pending in TPQ. */
401         QETH_QDIO_BUF_PENDING,
402         /* Found in completion queue. */
403         QETH_QDIO_BUF_IN_CQ,
404         /* Handled via transfer pending / completion queue. */
405         QETH_QDIO_BUF_HANDLED_DELAYED,
406 };
407
408 struct qeth_qdio_out_buffer {
409         struct qdio_buffer *buffer;
410         atomic_t state;
411         int next_element_to_fill;
412         unsigned int frames;
413         unsigned int bytes;
414         struct sk_buff_head skb_list;
415         int is_header[QDIO_MAX_ELEMENTS_PER_BUFFER];
416
417         struct qeth_qdio_out_q *q;
418         struct qeth_qdio_out_buffer *next_pending;
419 };
420
421 struct qeth_card;
422
423 #define QETH_CARD_STAT_ADD(_c, _stat, _val)     ((_c)->stats._stat += (_val))
424 #define QETH_CARD_STAT_INC(_c, _stat)           QETH_CARD_STAT_ADD(_c, _stat, 1)
425
426 #define QETH_TXQ_STAT_ADD(_q, _stat, _val)      ((_q)->stats._stat += (_val))
427 #define QETH_TXQ_STAT_INC(_q, _stat)            QETH_TXQ_STAT_ADD(_q, _stat, 1)
428
429 struct qeth_card_stats {
430         u64 rx_bufs;
431         u64 rx_skb_csum;
432         u64 rx_sg_skbs;
433         u64 rx_sg_frags;
434         u64 rx_sg_alloc_page;
435
436         u64 rx_dropped_nomem;
437         u64 rx_dropped_notsupp;
438         u64 rx_dropped_runt;
439
440         /* rtnl_link_stats64 */
441         u64 rx_packets;
442         u64 rx_bytes;
443         u64 rx_multicast;
444         u64 rx_length_errors;
445         u64 rx_frame_errors;
446         u64 rx_fifo_errors;
447 };
448
449 struct qeth_out_q_stats {
450         u64 bufs;
451         u64 bufs_pack;
452         u64 buf_elements;
453         u64 skbs_pack;
454         u64 skbs_sg;
455         u64 skbs_csum;
456         u64 skbs_tso;
457         u64 skbs_linearized;
458         u64 skbs_linearized_fail;
459         u64 tso_bytes;
460         u64 packing_mode_switch;
461         u64 stopped;
462         u64 doorbell;
463         u64 coal_frames;
464         u64 completion_yield;
465         u64 completion_timer;
466
467         /* rtnl_link_stats64 */
468         u64 tx_packets;
469         u64 tx_bytes;
470         u64 tx_errors;
471         u64 tx_dropped;
472 };
473
474 #define QETH_TX_MAX_COALESCED_FRAMES    1
475 #define QETH_TX_COALESCE_USECS          25
476 #define QETH_TX_TIMER_USECS             500
477
478 struct qeth_qdio_out_q {
479         struct qdio_buffer *qdio_bufs[QDIO_MAX_BUFFERS_PER_Q];
480         struct qeth_qdio_out_buffer *bufs[QDIO_MAX_BUFFERS_PER_Q];
481         struct qdio_outbuf_state *bufstates; /* convenience pointer */
482         struct qeth_out_q_stats stats;
483         spinlock_t lock;
484         u8 next_buf_to_fill;
485         u8 max_elements;
486         u8 queue_no;
487         u8 do_pack;
488         struct qeth_card *card;
489         /*
490          * number of buffers that are currently filled (PRIMED)
491          * -> these buffers are hardware-owned
492          */
493         atomic_t used_buffers;
494         /* indicates whether PCI flag must be set (or if one is outstanding) */
495         atomic_t set_pci_flags_count;
496         struct napi_struct napi;
497         struct timer_list timer;
498         struct qeth_hdr *prev_hdr;
499         unsigned int coalesced_frames;
500         u8 bulk_start;
501         u8 bulk_count;
502         u8 bulk_max;
503
504         unsigned int coalesce_usecs;
505         unsigned int max_coalesced_frames;
506 };
507
508 #define qeth_for_each_output_queue(card, q, i)          \
509         for (i = 0; i < card->qdio.no_out_queues &&     \
510                     (q = card->qdio.out_qs[i]); i++)
511
512 #define qeth_napi_to_out_queue(n) container_of(n, struct qeth_qdio_out_q, napi)
513
514 static inline void qeth_tx_arm_timer(struct qeth_qdio_out_q *queue,
515                                      unsigned long usecs)
516 {
517         timer_reduce(&queue->timer, usecs_to_jiffies(usecs) + jiffies);
518 }
519
520 static inline bool qeth_out_queue_is_full(struct qeth_qdio_out_q *queue)
521 {
522         return atomic_read(&queue->used_buffers) >= QDIO_MAX_BUFFERS_PER_Q;
523 }
524
525 static inline bool qeth_out_queue_is_empty(struct qeth_qdio_out_q *queue)
526 {
527         return atomic_read(&queue->used_buffers) == 0;
528 }
529
530 struct qeth_qdio_info {
531         atomic_t state;
532         /* input */
533         int no_in_queues;
534         struct qeth_qdio_q *in_q;
535         struct qeth_qdio_q *c_q;
536         struct qeth_qdio_buffer_pool in_buf_pool;
537         struct qeth_qdio_buffer_pool init_pool;
538         int in_buf_size;
539
540         /* output */
541         int no_out_queues;
542         struct qeth_qdio_out_q *out_qs[QETH_MAX_OUT_QUEUES];
543         struct qdio_outbuf_state *out_bufstates;
544
545         /* priority queueing */
546         int do_prio_queueing;
547         int default_out_queue;
548 };
549
550 /**
551  *  channel state machine
552  */
553 enum qeth_channel_states {
554         CH_STATE_UP,
555         CH_STATE_DOWN,
556         CH_STATE_HALTED,
557         CH_STATE_STOPPED,
558 };
559 /**
560  * card state machine
561  */
562 enum qeth_card_states {
563         CARD_STATE_DOWN,
564         CARD_STATE_SOFTSETUP,
565 };
566
567 /**
568  * Protocol versions
569  */
570 enum qeth_prot_versions {
571         QETH_PROT_NONE = 0x0000,
572         QETH_PROT_IPV4 = 0x0004,
573         QETH_PROT_IPV6 = 0x0006,
574 };
575
576 enum qeth_cq {
577         QETH_CQ_DISABLED = 0,
578         QETH_CQ_ENABLED = 1,
579         QETH_CQ_NOTAVAILABLE = 2,
580 };
581
582 struct qeth_ipato {
583         bool enabled;
584         bool invert4;
585         bool invert6;
586         struct list_head entries;
587 };
588
589 struct qeth_channel {
590         struct ccw_device *ccwdev;
591         struct qeth_cmd_buffer *active_cmd;
592         enum qeth_channel_states state;
593         atomic_t irq_pending;
594 };
595
596 struct qeth_reply {
597         int (*callback)(struct qeth_card *card, struct qeth_reply *reply,
598                         unsigned long data);
599         void *param;
600 };
601
602 struct qeth_cmd_buffer {
603         struct list_head list;
604         struct completion done;
605         spinlock_t lock;
606         unsigned int length;
607         refcount_t ref_count;
608         struct qeth_channel *channel;
609         struct qeth_reply reply;
610         long timeout;
611         unsigned char *data;
612         void (*finalize)(struct qeth_card *card, struct qeth_cmd_buffer *iob);
613         bool (*match)(struct qeth_cmd_buffer *iob,
614                       struct qeth_cmd_buffer *reply);
615         void (*callback)(struct qeth_card *card, struct qeth_cmd_buffer *iob,
616                          unsigned int data_length);
617         int rc;
618 };
619
620 static inline void qeth_get_cmd(struct qeth_cmd_buffer *iob)
621 {
622         refcount_inc(&iob->ref_count);
623 }
624
625 static inline struct qeth_ipa_cmd *__ipa_reply(struct qeth_cmd_buffer *iob)
626 {
627         if (!IS_IPA(iob->data))
628                 return NULL;
629
630         return (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
631 }
632
633 static inline struct qeth_ipa_cmd *__ipa_cmd(struct qeth_cmd_buffer *iob)
634 {
635         return (struct qeth_ipa_cmd *)(iob->data + IPA_PDU_HEADER_SIZE);
636 }
637
638 static inline struct ccw1 *__ccw_from_cmd(struct qeth_cmd_buffer *iob)
639 {
640         return (struct ccw1 *)(iob->data + ALIGN(iob->length, 8));
641 }
642
643 static inline bool qeth_trylock_channel(struct qeth_channel *channel)
644 {
645         return atomic_cmpxchg(&channel->irq_pending, 0, 1) == 0;
646 }
647
648 /**
649  *  OSA card related definitions
650  */
651 struct qeth_token {
652         __u32 issuer_rm_w;
653         __u32 issuer_rm_r;
654         __u32 cm_filter_w;
655         __u32 cm_filter_r;
656         __u32 cm_connection_w;
657         __u32 cm_connection_r;
658         __u32 ulp_filter_w;
659         __u32 ulp_filter_r;
660         __u32 ulp_connection_w;
661         __u32 ulp_connection_r;
662 };
663
664 struct qeth_seqno {
665         __u32 trans_hdr;
666         __u32 pdu_hdr;
667         __u32 pdu_hdr_ack;
668         __u16 ipa;
669 };
670
671 struct qeth_card_blkt {
672         int time_total;
673         int inter_packet;
674         int inter_packet_jumbo;
675 };
676
677 enum qeth_pnso_mode {
678         QETH_PNSO_NONE,
679         QETH_PNSO_BRIDGEPORT,
680         QETH_PNSO_ADDR_INFO,
681 };
682
683 #define QETH_BROADCAST_WITH_ECHO    0x01
684 #define QETH_BROADCAST_WITHOUT_ECHO 0x02
685 struct qeth_card_info {
686         unsigned short unit_addr2;
687         unsigned short cula;
688         __u16 func_level;
689         char mcl_level[QETH_MCL_LENGTH + 1];
690         /* doubleword below corresponds to net_if_token */
691         u16 ddev_devno;
692         u8 cssid;
693         u8 iid;
694         u8 ssid;
695         u8 chpid;
696         u16 chid;
697         u8 ids_valid:1; /* cssid,iid,chid */
698         u8 dev_addr_is_registered:1;
699         u8 open_when_online:1;
700         u8 promisc_mode:1;
701         u8 use_v1_blkt:1;
702         u8 is_vm_nic:1;
703         /* no bitfield, we take a pointer on these two: */
704         u8 has_lp2lp_cso_v6;
705         u8 has_lp2lp_cso_v4;
706         enum qeth_pnso_mode pnso_mode;
707         enum qeth_card_types type;
708         enum qeth_link_types link_type;
709         int broadcast_capable;
710         bool layer_enforced;
711         struct qeth_card_blkt blkt;
712         __u32 diagass_support;
713         __u32 hwtrap;
714 };
715
716 enum qeth_discipline_id {
717         QETH_DISCIPLINE_UNDETERMINED = -1,
718         QETH_DISCIPLINE_LAYER3 = 0,
719         QETH_DISCIPLINE_LAYER2 = 1,
720 };
721
722 struct qeth_card_options {
723         struct qeth_ipa_caps ipa4;
724         struct qeth_ipa_caps ipa6;
725         struct qeth_routing_info route4;
726         struct qeth_routing_info route6;
727         struct qeth_ipa_caps adp; /* Adapter parameters */
728         struct qeth_sbp_info sbp; /* SETBRIDGEPORT options */
729         struct qeth_vnicc_info vnicc; /* VNICC options */
730         enum qeth_discipline_id layer;
731         enum qeth_ipa_isolation_modes isolation;
732         int sniffer;
733         enum qeth_cq cq;
734         char hsuid[9];
735 };
736
737 #define IS_LAYER2(card) ((card)->options.layer == QETH_DISCIPLINE_LAYER2)
738 #define IS_LAYER3(card) ((card)->options.layer == QETH_DISCIPLINE_LAYER3)
739
740 /*
741  * thread bits for qeth_card thread masks
742  */
743 enum qeth_threads {
744         QETH_RECOVER_THREAD = 1,
745 };
746
747 struct qeth_osn_info {
748         int (*assist_cb)(struct net_device *dev, void *data);
749         int (*data_cb)(struct sk_buff *skb);
750 };
751
752 struct qeth_discipline {
753         const struct device_type *devtype;
754         int (*setup) (struct ccwgroup_device *);
755         void (*remove) (struct ccwgroup_device *);
756         int (*set_online)(struct qeth_card *card);
757         void (*set_offline)(struct qeth_card *card);
758         int (*do_ioctl)(struct net_device *dev, struct ifreq *rq, int cmd);
759         int (*control_event_handler)(struct qeth_card *card,
760                                         struct qeth_ipa_cmd *cmd);
761 };
762
763 enum qeth_addr_disposition {
764         QETH_DISP_ADDR_DELETE = 0,
765         QETH_DISP_ADDR_DO_NOTHING = 1,
766         QETH_DISP_ADDR_ADD = 2,
767 };
768
769 struct qeth_rx {
770         int b_count;
771         int b_index;
772         u8 buf_element;
773         int e_offset;
774         int qdio_err;
775         u8 bufs_refill;
776 };
777
778 struct carrier_info {
779         __u8  card_type;
780         __u16 port_mode;
781         __u32 port_speed;
782 };
783
784 struct qeth_switch_info {
785         __u32 capabilities;
786         __u32 settings;
787 };
788
789 struct qeth_priv {
790         unsigned int rx_copybreak;
791         u32 brport_hw_features;
792         u32 brport_features;
793 };
794
795 #define QETH_NAPI_WEIGHT NAPI_POLL_WEIGHT
796
797 struct qeth_card {
798         enum qeth_card_states state;
799         spinlock_t lock;
800         struct ccwgroup_device *gdev;
801         struct qeth_cmd_buffer *read_cmd;
802         struct qeth_channel read;
803         struct qeth_channel write;
804         struct qeth_channel data;
805
806         struct net_device *dev;
807         struct dentry *debugfs;
808         struct qeth_card_stats stats;
809         struct qeth_card_info info;
810         struct qeth_token token;
811         struct qeth_seqno seqno;
812         struct qeth_card_options options;
813
814         struct workqueue_struct *event_wq;
815         struct workqueue_struct *cmd_wq;
816         wait_queue_head_t wait_q;
817
818         struct mutex ip_lock;
819         /* protected by ip_lock: */
820         DECLARE_HASHTABLE(ip_htable, 4);
821         struct qeth_ipato ipato;
822
823         DECLARE_HASHTABLE(local_addrs4, 4);
824         DECLARE_HASHTABLE(local_addrs6, 4);
825         spinlock_t local_addrs4_lock;
826         spinlock_t local_addrs6_lock;
827         DECLARE_HASHTABLE(rx_mode_addrs, 4);
828         struct work_struct rx_mode_work;
829         struct work_struct kernel_thread_starter;
830         spinlock_t thread_mask_lock;
831         unsigned long thread_start_mask;
832         unsigned long thread_allowed_mask;
833         unsigned long thread_running_mask;
834         struct list_head cmd_waiter_list;
835         /* QDIO buffer handling */
836         struct qeth_qdio_info qdio;
837         int read_or_write_problem;
838         struct qeth_osn_info osn_info;
839         struct qeth_discipline *discipline;
840         atomic_t force_alloc_skb;
841         struct service_level qeth_service_level;
842         struct qdio_ssqd_desc ssqd;
843         debug_info_t *debug;
844         struct mutex sbp_lock;
845         struct mutex conf_mutex;
846         struct mutex discipline_mutex;
847         struct napi_struct napi;
848         struct qeth_rx rx;
849         struct delayed_work buffer_reclaim_work;
850         struct work_struct close_dev_work;
851 };
852
853 static inline bool qeth_card_hw_is_reachable(struct qeth_card *card)
854 {
855         return card->state == CARD_STATE_SOFTSETUP;
856 }
857
858 static inline void qeth_unlock_channel(struct qeth_card *card,
859                                        struct qeth_channel *channel)
860 {
861         atomic_set(&channel->irq_pending, 0);
862         wake_up(&card->wait_q);
863 }
864
865 struct qeth_trap_id {
866         __u16 lparnr;
867         char vmname[8];
868         __u8 chpid;
869         __u8 ssid;
870         __u16 devno;
871 } __packed;
872
873 /*some helper functions*/
874 #define QETH_CARD_IFNAME(card) (((card)->dev)? (card)->dev->name : "")
875
876 static inline u16 qeth_iqd_translate_txq(struct net_device *dev, u16 txq)
877 {
878         if (txq == QETH_IQD_MCAST_TXQ)
879                 return dev->num_tx_queues - 1;
880         if (txq == dev->num_tx_queues - 1)
881                 return QETH_IQD_MCAST_TXQ;
882         return txq;
883 }
884
885 static inline bool qeth_iqd_is_mcast_queue(struct qeth_card *card,
886                                            struct qeth_qdio_out_q *queue)
887 {
888         return qeth_iqd_translate_txq(card->dev, queue->queue_no) ==
889                QETH_IQD_MCAST_TXQ;
890 }
891
892 static inline void qeth_scrub_qdio_buffer(struct qdio_buffer *buf,
893                                           unsigned int elements)
894 {
895         unsigned int i;
896
897         for (i = 0; i < elements; i++)
898                 memset(&buf->element[i], 0, sizeof(struct qdio_buffer_element));
899         buf->element[14].sflags = 0;
900         buf->element[15].sflags = 0;
901 }
902
903 /**
904  * qeth_get_elements_for_range() -      find number of SBALEs to cover range.
905  * @start:                              Start of the address range.
906  * @end:                                Address after the end of the range.
907  *
908  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
909  * the specified address range.
910  */
911 static inline int qeth_get_elements_for_range(addr_t start, addr_t end)
912 {
913         return PFN_UP(end) - PFN_DOWN(start);
914 }
915
916 static inline int qeth_get_ip_version(struct sk_buff *skb)
917 {
918         struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
919         __be16 prot = veth->h_vlan_proto;
920
921         if (prot == htons(ETH_P_8021Q))
922                 prot = veth->h_vlan_encapsulated_proto;
923
924         switch (prot) {
925         case htons(ETH_P_IPV6):
926                 return 6;
927         case htons(ETH_P_IP):
928                 return 4;
929         default:
930                 return 0;
931         }
932 }
933
934 static inline int qeth_get_ether_cast_type(struct sk_buff *skb)
935 {
936         u8 *addr = eth_hdr(skb)->h_dest;
937
938         if (is_multicast_ether_addr(addr))
939                 return is_broadcast_ether_addr(addr) ? RTN_BROADCAST :
940                                                        RTN_MULTICAST;
941         return RTN_UNICAST;
942 }
943
944 static inline struct dst_entry *qeth_dst_check_rcu(struct sk_buff *skb, int ipv)
945 {
946         struct dst_entry *dst = skb_dst(skb);
947         struct rt6_info *rt;
948
949         rt = (struct rt6_info *) dst;
950         if (dst)
951                 dst = dst_check(dst, (ipv == 6) ? rt6_get_cookie(rt) : 0);
952         return dst;
953 }
954
955 static inline __be32 qeth_next_hop_v4_rcu(struct sk_buff *skb,
956                                           struct dst_entry *dst)
957 {
958         struct rtable *rt = (struct rtable *) dst;
959
960         return (rt) ? rt_nexthop(rt, ip_hdr(skb)->daddr) : ip_hdr(skb)->daddr;
961 }
962
963 static inline struct in6_addr *qeth_next_hop_v6_rcu(struct sk_buff *skb,
964                                                     struct dst_entry *dst)
965 {
966         struct rt6_info *rt = (struct rt6_info *) dst;
967
968         if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
969                 return &rt->rt6i_gateway;
970         else
971                 return &ipv6_hdr(skb)->daddr;
972 }
973
974 static inline void qeth_tx_csum(struct sk_buff *skb, u8 *flags, int ipv)
975 {
976         *flags |= QETH_HDR_EXT_CSUM_TRANSP_REQ;
977         if ((ipv == 4 && ip_hdr(skb)->protocol == IPPROTO_UDP) ||
978             (ipv == 6 && ipv6_hdr(skb)->nexthdr == IPPROTO_UDP))
979                 *flags |= QETH_HDR_EXT_UDP;
980 }
981
982 static inline void qeth_put_buffer_pool_entry(struct qeth_card *card,
983                 struct qeth_buffer_pool_entry *entry)
984 {
985         list_add_tail(&entry->list, &card->qdio.in_buf_pool.entry_list);
986 }
987
988 static inline int qeth_is_diagass_supported(struct qeth_card *card,
989                 enum qeth_diags_cmds cmd)
990 {
991         return card->info.diagass_support & (__u32)cmd;
992 }
993
994 int qeth_send_simple_setassparms_prot(struct qeth_card *card,
995                                       enum qeth_ipa_funcs ipa_func,
996                                       u16 cmd_code, u32 *data,
997                                       enum qeth_prot_versions prot);
998 /* IPv4 variant */
999 static inline int qeth_send_simple_setassparms(struct qeth_card *card,
1000                                                enum qeth_ipa_funcs ipa_func,
1001                                                u16 cmd_code, u32 *data)
1002 {
1003         return qeth_send_simple_setassparms_prot(card, ipa_func, cmd_code,
1004                                                  data, QETH_PROT_IPV4);
1005 }
1006
1007 static inline int qeth_send_simple_setassparms_v6(struct qeth_card *card,
1008                                                   enum qeth_ipa_funcs ipa_func,
1009                                                   u16 cmd_code, u32 *data)
1010 {
1011         return qeth_send_simple_setassparms_prot(card, ipa_func, cmd_code,
1012                                                  data, QETH_PROT_IPV6);
1013 }
1014
1015 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb);
1016
1017 extern struct qeth_discipline qeth_l2_discipline;
1018 extern struct qeth_discipline qeth_l3_discipline;
1019 extern const struct ethtool_ops qeth_ethtool_ops;
1020 extern const struct ethtool_ops qeth_osn_ethtool_ops;
1021 extern const struct attribute_group *qeth_generic_attr_groups[];
1022 extern const struct attribute_group *qeth_osn_attr_groups[];
1023 extern const struct attribute_group qeth_device_attr_group;
1024 extern const struct attribute_group qeth_device_blkt_group;
1025 extern const struct device_type qeth_generic_devtype;
1026
1027 const char *qeth_get_cardname_short(struct qeth_card *);
1028 int qeth_resize_buffer_pool(struct qeth_card *card, unsigned int count);
1029 int qeth_core_load_discipline(struct qeth_card *, enum qeth_discipline_id);
1030 void qeth_core_free_discipline(struct qeth_card *);
1031
1032 /* exports for qeth discipline device drivers */
1033 extern struct kmem_cache *qeth_core_header_cache;
1034 extern struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS];
1035
1036 struct net_device *qeth_clone_netdev(struct net_device *orig);
1037 struct qeth_card *qeth_get_card_by_busid(char *bus_id);
1038 void qeth_set_allowed_threads(struct qeth_card *, unsigned long , int);
1039 int qeth_threads_running(struct qeth_card *, unsigned long);
1040 int qeth_core_hardsetup_card(struct qeth_card *card, bool *carrier_ok);
1041 int qeth_stop_channel(struct qeth_channel *channel);
1042 int qeth_set_offline(struct qeth_card *card, bool resetting);
1043
1044 void qeth_print_status_message(struct qeth_card *);
1045 int qeth_send_ipa_cmd(struct qeth_card *, struct qeth_cmd_buffer *,
1046                   int (*reply_cb)
1047                   (struct qeth_card *, struct qeth_reply *, unsigned long),
1048                   void *);
1049 struct qeth_cmd_buffer *qeth_ipa_alloc_cmd(struct qeth_card *card,
1050                                            enum qeth_ipa_cmds cmd_code,
1051                                            enum qeth_prot_versions prot,
1052                                            unsigned int data_length);
1053 struct qeth_cmd_buffer *qeth_alloc_cmd(struct qeth_channel *channel,
1054                                        unsigned int length, unsigned int ccws,
1055                                        long timeout);
1056 struct qeth_cmd_buffer *qeth_get_setassparms_cmd(struct qeth_card *card,
1057                                                  enum qeth_ipa_funcs ipa_func,
1058                                                  u16 cmd_code,
1059                                                  unsigned int data_length,
1060                                                  enum qeth_prot_versions prot);
1061 struct qeth_cmd_buffer *qeth_get_diag_cmd(struct qeth_card *card,
1062                                           enum qeth_diags_cmds sub_cmd,
1063                                           unsigned int data_length);
1064 void qeth_notify_cmd(struct qeth_cmd_buffer *iob, int reason);
1065 void qeth_put_cmd(struct qeth_cmd_buffer *iob);
1066
1067 int qeth_schedule_recovery(struct qeth_card *card);
1068 void qeth_flush_local_addrs(struct qeth_card *card);
1069 int qeth_poll(struct napi_struct *napi, int budget);
1070 int qeth_qdio_clear_card(struct qeth_card *, int);
1071 void qeth_clear_working_pool_list(struct qeth_card *);
1072 void qeth_drain_output_queues(struct qeth_card *card);
1073 void qeth_setadp_promisc_mode(struct qeth_card *card, bool enable);
1074 int qeth_setadpparms_change_macaddr(struct qeth_card *);
1075 void qeth_tx_timeout(struct net_device *, unsigned int txqueue);
1076 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
1077                           u16 cmd_length,
1078                           bool (*match)(struct qeth_cmd_buffer *iob,
1079                                         struct qeth_cmd_buffer *reply));
1080 int qeth_query_switch_attributes(struct qeth_card *card,
1081                                   struct qeth_switch_info *sw_info);
1082 int qeth_query_card_info(struct qeth_card *card,
1083                          struct carrier_info *carrier_info);
1084 int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
1085                                      enum qeth_ipa_isolation_modes mode);
1086
1087 unsigned int qeth_count_elements(struct sk_buff *skb, unsigned int data_offset);
1088 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
1089                         struct sk_buff *skb, struct qeth_hdr *hdr,
1090                         unsigned int offset, unsigned int hd_len,
1091                         int elements_needed);
1092 int qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
1093 void qeth_dbf_longtext(debug_info_t *id, int level, char *text, ...);
1094 int qeth_configure_cq(struct qeth_card *, enum qeth_cq);
1095 int qeth_hw_trap(struct qeth_card *, enum qeth_diags_trap_action);
1096 void qeth_trace_features(struct qeth_card *);
1097 int qeth_setassparms_cb(struct qeth_card *, struct qeth_reply *, unsigned long);
1098 int qeth_setup_netdev(struct qeth_card *card);
1099 int qeth_set_features(struct net_device *, netdev_features_t);
1100 void qeth_enable_hw_features(struct net_device *dev);
1101 netdev_features_t qeth_fix_features(struct net_device *, netdev_features_t);
1102 netdev_features_t qeth_features_check(struct sk_buff *skb,
1103                                       struct net_device *dev,
1104                                       netdev_features_t features);
1105 void qeth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats);
1106 int qeth_set_real_num_tx_queues(struct qeth_card *card, unsigned int count);
1107 u16 qeth_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
1108                           u8 cast_type, struct net_device *sb_dev);
1109 int qeth_open(struct net_device *dev);
1110 int qeth_stop(struct net_device *dev);
1111
1112 int qeth_vm_request_mac(struct qeth_card *card);
1113 int qeth_xmit(struct qeth_card *card, struct sk_buff *skb,
1114               struct qeth_qdio_out_q *queue, int ipv,
1115               void (*fill_header)(struct qeth_qdio_out_q *queue,
1116                                   struct qeth_hdr *hdr, struct sk_buff *skb,
1117                                   int ipv, unsigned int data_len));
1118
1119 /* exports for OSN */
1120 int qeth_osn_assist(struct net_device *, void *, int);
1121 int qeth_osn_register(unsigned char *read_dev_no, struct net_device **,
1122                 int (*assist_cb)(struct net_device *, void *),
1123                 int (*data_cb)(struct sk_buff *));
1124 void qeth_osn_deregister(struct net_device *);
1125
1126 #endif /* __QETH_CORE_H__ */