xref: /freebsd/sys/dev/liquidio/base/lio_ctrl.h (revision 61e21613)
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
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
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*   \file lio_ctrl.h
35  *   \brief Host NIC Driver: Routine to send network data &
36  *   control packet to Octeon.
37  */
38 
39 #ifndef __LIO_CTRL_H__
40 #define __LIO_CTRL_H__
41 
42 /* Maximum number of 8-byte words can be sent in a NIC control message. */
43 #define LIO_MAX_NCTRL_UDD	32
44 
45 typedef void	(*lio_ctrl_pkt_cb_fn_t)(void *);
46 
47 /*
48  * Structure of control information passed by the NIC module to the OSI
49  * layer when sending control commands to Octeon device software.
50  */
51 struct lio_ctrl_pkt {
52 	/* Command to be passed to the Octeon device software. */
53 	union octeon_cmd	ncmd;
54 
55 	/* Send buffer  */
56 	void			*data;
57 	uint64_t		dmadata;
58 
59 	/* Response buffer */
60 	void			*rdata;
61 	uint64_t		dmardata;
62 
63 	/* Additional data that may be needed by some commands. */
64 	uint64_t		udd[LIO_MAX_NCTRL_UDD];
65 
66 	/* Input queue to use to send this command. */
67 	uint64_t		iq_no;
68 
69 	/*
70 	 *  Time to wait for Octeon software to respond to this control command.
71 	 *  If wait_time is 0, OSI assumes no response is expected.
72 	 */
73 	size_t			wait_time;
74 
75 	/* The network device that issued the control command. */
76 	struct lio		*lio;
77 
78 	/* Callback function called when the command has been fetched */
79 	lio_ctrl_pkt_cb_fn_t	cb_fn;
80 };
81 
82 /*
83  * Structure of data information passed by the NIC module to the OSI
84  * layer when forwarding data to Octeon device software.
85  */
86 struct lio_data_pkt {
87 	/*
88 	 *  Pointer to information maintained by NIC module for this packet. The
89 	 *  OSI layer passes this as-is to the driver.
90 	 */
91 	void			*buf;
92 
93 	/* Type of buffer passed in "buf" above. */
94 	uint32_t		reqtype;
95 
96 	/* Total data bytes to be transferred in this command. */
97 	uint32_t		datasize;
98 
99 	/* Command to be passed to the Octeon device software. */
100 	union lio_instr_64B	cmd;
101 
102 	/* Input queue to use to send this command. */
103 	uint32_t		q_no;
104 
105 };
106 
107 /*
108  * Structure passed by NIC module to OSI layer to prepare a command to send
109  * network data to Octeon.
110  */
111 union lio_cmd_setup {
112 	struct {
113 		uint32_t	iq_no:8;
114 		uint32_t	gather:1;
115 		uint32_t	timestamp:1;
116 		uint32_t	ip_csum:1;
117 		uint32_t	transport_csum:1;
118 		uint32_t	tnl_csum:1;
119 		uint32_t	rsvd:19;
120 
121 		union {
122 			uint32_t	datasize;
123 			uint32_t	gatherptrs;
124 		}	u;
125 	}	s;
126 
127 	uint64_t	cmd_setup64;
128 
129 };
130 
131 static inline int
132 lio_iq_is_full(struct octeon_device *oct, uint32_t q_no)
133 {
134 
135 	return (atomic_load_acq_int(&oct->instr_queue[q_no]->instr_pending) >=
136 		(oct->instr_queue[q_no]->max_count - 2));
137 }
138 
139 static inline void
140 lio_prepare_pci_cmd_o3(struct octeon_device *oct, union lio_instr_64B *cmd,
141 		       union lio_cmd_setup *setup, uint32_t tag)
142 {
143 	union octeon_packet_params	packet_params;
144 	struct octeon_instr_irh		*irh;
145 	struct octeon_instr_ih3		*ih3;
146 	struct octeon_instr_pki_ih3	*pki_ih3;
147 	int	port;
148 
149 	bzero(cmd, sizeof(union lio_instr_64B));
150 
151 	ih3 = (struct octeon_instr_ih3 *)&cmd->cmd3.ih3;
152 	pki_ih3 = (struct octeon_instr_pki_ih3 *)&cmd->cmd3.pki_ih3;
153 
154 	/*
155 	 * assume that rflag is cleared so therefore front data will only have
156 	 * irh and ossp[1] and ossp[2] for a total of 24 bytes
157 	 */
158 	ih3->pkind = oct->instr_queue[setup->s.iq_no]->txpciq.s.pkind;
159 	/* PKI IH */
160 	ih3->fsz = LIO_PCICMD_O3;
161 
162 	if (!setup->s.gather) {
163 		ih3->dlengsz = setup->s.u.datasize;
164 	} else {
165 		ih3->gather = 1;
166 		ih3->dlengsz = setup->s.u.gatherptrs;
167 	}
168 
169 	pki_ih3->w = 1;
170 	pki_ih3->raw = 0;
171 	pki_ih3->utag = 0;
172 	pki_ih3->utt = 1;
173 	pki_ih3->uqpg = oct->instr_queue[setup->s.iq_no]->txpciq.s.use_qpg;
174 
175 	port = (int)oct->instr_queue[setup->s.iq_no]->txpciq.s.port;
176 
177 	if (tag)
178 		pki_ih3->tag = tag;
179 	else
180 		pki_ih3->tag = LIO_DATA(port);
181 
182 	pki_ih3->tagtype = LIO_ORDERED_TAG;
183 	pki_ih3->qpg = oct->instr_queue[setup->s.iq_no]->txpciq.s.qpg;
184 	pki_ih3->pm = 0x0;		/* parse from L2 */
185 	/* sl will be sizeof(pki_ih3) + irh + ossp0 + ossp1 */
186 	pki_ih3->sl = 32;
187 
188 	irh = (struct octeon_instr_irh *)&cmd->cmd3.irh;
189 
190 	irh->opcode = LIO_OPCODE_NIC;
191 	irh->subcode = LIO_OPCODE_NIC_NW_DATA;
192 
193 	packet_params.pkt_params32 = 0;
194 
195 	packet_params.s.ip_csum = setup->s.ip_csum;
196 	packet_params.s.transport_csum = setup->s.transport_csum;
197 	packet_params.s.tnl_csum = setup->s.tnl_csum;
198 	packet_params.s.tsflag = setup->s.timestamp;
199 
200 	irh->ossp = packet_params.pkt_params32;
201 }
202 
203 /*
204  * Utility function to prepare a 64B NIC instruction based on a setup command
205  * @param oct - Pointer to current octeon device
206  * @param cmd - pointer to instruction to be filled in.
207  * @param setup - pointer to the setup structure
208  * @param q_no - which queue for back pressure
209  *
210  * Assumes the cmd instruction is pre-allocated, but no fields are filled in.
211  */
212 static inline void
213 lio_prepare_pci_cmd(struct octeon_device *oct, union lio_instr_64B *cmd,
214 		    union lio_cmd_setup *setup, uint32_t tag)
215 {
216 
217 	lio_prepare_pci_cmd_o3(oct, cmd, setup, tag);
218 }
219 
220 /*
221  * Send a NIC data packet to the device
222  * @param oct - octeon device pointer
223  * @param ndata - control structure with queueing, and buffer information
224  *
225  * @returns LIO_IQ_FAILED if it failed to add to the input queue.
226  * LIO_IQ_STOP if it the queue should be stopped,
227  * and LIO_IQ_SEND_OK if it sent okay.
228  */
229 int	lio_send_data_pkt(struct octeon_device *oct,
230 			  struct lio_data_pkt *ndata);
231 
232 /*
233  * Send a NIC control packet to the device
234  * @param oct - octeon device pointer
235  * @param nctrl - control structure with command, timeout, and callback info
236  * @returns IQ_FAILED if it failed to add to the input queue. IQ_STOP if it the
237  * queue should be stopped, and LIO_IQ_SEND_OK if it sent okay.
238  */
239 int	lio_send_ctrl_pkt(struct octeon_device *oct,
240 			  struct lio_ctrl_pkt *nctrl);
241 
242 #endif	/* __LIO_CTRL_H__ */
243