xref: /freebsd/sys/dev/cxgb/cxgb_offload.h (revision ef72318f)
1 
2 /**************************************************************************
3 
4 Copyright (c) 2007, Chelsio Inc.
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice,
11     this list of conditions and the following disclaimer.
12 
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16 
17  3. Neither the name of the Chelsio Corporation nor the names of its
18     contributors may be used to endorse or promote products derived from
19     this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32 
33 $FreeBSD$
34 
35 ***************************************************************************/
36 
37 #ifndef _CXGB_OFFLOAD_H
38 #define _CXGB_OFFLOAD_H
39 
40 
41 #include <dev/cxgb/common/cxgb_tcb.h>
42 #include <dev/cxgb/cxgb_l2t.h>
43 
44 #include <dev/cxgb/ulp/toecore/toedev.h>
45 #include <dev/cxgb/common/cxgb_t3_cpl.h>
46 
47 struct adapter;
48 struct cxgb_client;
49 
50 void cxgb_offload_init(void);
51 void cxgb_offload_exit(void);
52 
53 void cxgb_adapter_ofld(struct adapter *adapter);
54 void cxgb_adapter_unofld(struct adapter *adapter);
55 int cxgb_offload_activate(struct adapter *adapter);
56 void cxgb_offload_deactivate(struct adapter *adapter);
57 int cxgb_ofld_recv(struct toedev *dev, struct mbuf **m, int n);
58 
59 void cxgb_set_dummy_ops(struct toedev *dev);
60 
61 
62 /*
63  * Client registration.  Users of T3 driver must register themselves.
64  * The T3 driver will call the add function of every client for each T3
65  * adapter activated, passing up the toedev ptr.  Each client fills out an
66  * array of callback functions to process CPL messages.
67  */
68 
69 void cxgb_register_client(struct cxgb_client *client);
70 void cxgb_unregister_client(struct cxgb_client *client);
71 void cxgb_add_clients(struct toedev *tdev);
72 void cxgb_remove_clients(struct toedev *tdev);
73 
74 typedef int (*cxgb_cpl_handler_func)(struct toedev *dev,
75 				      struct mbuf *m, void *ctx);
76 
77 struct cxgb_client {
78 	char 			*name;
79 	void 			(*add) (struct toedev *);
80 	void 			(*remove) (struct toedev *);
81 	cxgb_cpl_handler_func 	*handlers;
82 	int			(*redirect)(void *ctx, struct rtentry *old,
83 					    struct rtentry *new,
84 					    struct l2t_entry *l2t);
85 	TAILQ_ENTRY(cxgb_client)         client_entry;
86 };
87 
88 /*
89  * TID allocation services.
90  */
91 int cxgb_alloc_atid(struct toedev *dev, struct cxgb_client *client,
92 		     void *ctx);
93 int cxgb_alloc_stid(struct toedev *dev, struct cxgb_client *client,
94 		     void *ctx);
95 void *cxgb_free_atid(struct toedev *dev, int atid);
96 void cxgb_free_stid(struct toedev *dev, int stid);
97 void cxgb_insert_tid(struct toedev *dev, struct cxgb_client *client,
98 		      void *ctx,
99 	unsigned int tid);
100 void cxgb_queue_tid_release(struct toedev *dev, unsigned int tid);
101 void cxgb_remove_tid(struct toedev *dev, void *ctx, unsigned int tid);
102 
103 struct toe_tid_entry {
104 	struct cxgb_client 	*client;
105 	void 			*ctx;
106 };
107 
108 /* CPL message priority levels */
109 enum {
110 	CPL_PRIORITY_DATA = 0,     /* data messages */
111 	CPL_PRIORITY_SETUP = 1,	   /* connection setup messages */
112 	CPL_PRIORITY_TEARDOWN = 0, /* connection teardown messages */
113 	CPL_PRIORITY_LISTEN = 1,   /* listen start/stop messages */
114 	CPL_PRIORITY_ACK = 1,      /* RX ACK messages */
115 	CPL_PRIORITY_CONTROL = 1   /* offload control messages */
116 };
117 
118 /* Flags for return value of CPL message handlers */
119 enum {
120 	CPL_RET_BUF_DONE = 1,   // buffer processing done, buffer may be freed
121 	CPL_RET_BAD_MSG = 2,    // bad CPL message (e.g., unknown opcode)
122 	CPL_RET_UNKNOWN_TID = 4	// unexpected unknown TID
123 };
124 
125 typedef int (*cpl_handler_func)(struct toedev *dev, struct mbuf *m);
126 
127 /*
128  * Returns a pointer to the first byte of the CPL header in an sk_buff that
129  * contains a CPL message.
130  */
131 static inline void *cplhdr(struct mbuf *m)
132 {
133 	return mtod(m, uint8_t *);
134 }
135 
136 void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h);
137 
138 union listen_entry {
139 	struct toe_tid_entry toe_tid;
140 	union listen_entry *next;
141 };
142 
143 union active_open_entry {
144 	struct toe_tid_entry toe_tid;
145 	union active_open_entry *next;
146 };
147 
148 /*
149  * Holds the size, base address, free list start, etc of the TID, server TID,
150  * and active-open TID tables for a offload device.
151  * The tables themselves are allocated dynamically.
152  */
153 struct tid_info {
154 	struct toe_tid_entry *tid_tab;
155 	unsigned int ntids;
156 	volatile int tids_in_use;
157 
158 	union listen_entry *stid_tab;
159 	unsigned int nstids;
160 	unsigned int stid_base;
161 
162 	union active_open_entry *atid_tab;
163 	unsigned int natids;
164 	unsigned int atid_base;
165 
166 	/*
167 	 * The following members are accessed R/W so we put them in their own
168 	 * cache lines.
169 	 *
170 	 * XXX We could combine the atid fields above with the lock here since
171 	 * atids are use once (unlike other tids).  OTOH the above fields are
172 	 * usually in cache due to tid_tab.
173 	 */
174 	struct mtx atid_lock /* ____cacheline_aligned_in_smp */;
175 	union active_open_entry *afree;
176 	unsigned int atids_in_use;
177 
178 	struct mtx stid_lock /*____cacheline_aligned */;
179 	union listen_entry *sfree;
180 	unsigned int stids_in_use;
181 };
182 
183 struct toe_data {
184 #ifdef notyet
185 	struct list_head list_node;
186 #endif
187 	struct toedev *dev;
188 	unsigned int tx_max_chunk;  /* max payload for TX_DATA */
189 	unsigned int max_wrs;       /* max in-flight WRs per connection */
190 	unsigned int nmtus;
191 	const unsigned short *mtus;
192 	struct tid_info tid_maps;
193 
194 	struct toe_tid_entry *tid_release_list;
195 	struct mtx tid_release_lock;
196 	struct task tid_release_task;
197 };
198 
199 /*
200  * toedev -> toe_data accessor
201  */
202 #define TOE_DATA(dev) (*(struct toe_data **)&(dev)->l4opt)
203 
204 /*
205  * Map an ATID or STID to their entries in the corresponding TID tables.
206  */
207 static inline union active_open_entry *atid2entry(const struct tid_info *t,
208                                                   unsigned int atid)
209 {
210         return &t->atid_tab[atid - t->atid_base];
211 }
212 
213 
214 static inline union listen_entry *stid2entry(const struct tid_info *t,
215                                              unsigned int stid)
216 {
217         return &t->stid_tab[stid - t->stid_base];
218 }
219 
220 /*
221  * Find the connection corresponding to a TID.
222  */
223 static inline struct toe_tid_entry *lookup_tid(const struct tid_info *t,
224                                                unsigned int tid)
225 {
226         return tid < t->ntids ? &(t->tid_tab[tid]) : NULL;
227 }
228 
229 /*
230  * Find the connection corresponding to a server TID.
231  */
232 static inline struct toe_tid_entry *lookup_stid(const struct tid_info *t,
233                                                 unsigned int tid)
234 {
235         if (tid < t->stid_base || tid >= t->stid_base + t->nstids)
236                 return NULL;
237         return &(stid2entry(t, tid)->toe_tid);
238 }
239 
240 /*
241  * Find the connection corresponding to an active-open TID.
242  */
243 static inline struct toe_tid_entry *lookup_atid(const struct tid_info *t,
244                                                 unsigned int tid)
245 {
246         if (tid < t->atid_base || tid >= t->atid_base + t->natids)
247                 return NULL;
248         return &(atid2entry(t, tid)->toe_tid);
249 }
250 
251 void *cxgb_alloc_mem(unsigned long size);
252 void cxgb_free_mem(void *addr);
253 void cxgb_neigh_update(struct rtentry *rt);
254 void cxgb_redirect(struct rtentry *old, struct rtentry *new);
255 int process_rx(struct toedev *dev, struct mbuf **m, int n);
256 int attach_toedev(struct toedev *dev);
257 void detach_toedev(struct toedev *dev);
258 
259 
260 #endif
261