xref: /freebsd/sys/dev/liquidio/base/lio_ctrl.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 #include "lio_bsd.h"
35 #include "lio_common.h"
36 #include "lio_droq.h"
37 #include "lio_iq.h"
38 #include "lio_response_manager.h"
39 #include "lio_device.h"
40 #include "lio_ctrl.h"
41 #include "lio_main.h"
42 
43 int
44 lio_send_data_pkt(struct octeon_device *oct, struct lio_data_pkt *ndata)
45 {
46 	int	ring_doorbell = 1;
47 
48 	return (lio_send_command(oct, ndata->q_no, ring_doorbell, &ndata->cmd,
49 				 ndata->buf, ndata->datasize, ndata->reqtype));
50 }
51 
52 static void
53 lio_ctrl_callback(struct octeon_device *oct, uint32_t status, void *sc_ptr)
54 {
55 	struct lio_soft_command	*sc = (struct lio_soft_command *)sc_ptr;
56 	struct lio_ctrl_pkt	*nctrl;
57 
58 	nctrl = (struct lio_ctrl_pkt *)sc->ctxptr;
59 
60 	/*
61 	 * Call the callback function if status is OK.
62 	 * Status is OK only if a response was expected and core returned
63 	 * success.
64 	 * If no response was expected, status is OK if the command was posted
65 	 * successfully.
66 	 */
67 	if (!status && nctrl->cb_fn)
68 		nctrl->cb_fn(nctrl);
69 
70 	lio_free_soft_command(oct, sc);
71 }
72 
73 static inline struct lio_soft_command *
74 lio_alloc_ctrl_pkt_sc(struct octeon_device *oct, struct lio_ctrl_pkt *nctrl)
75 {
76 	struct lio_soft_command	*sc = NULL;
77 	uint32_t		datasize = 0, rdatasize, uddsize = 0;
78 	uint8_t			*data;
79 
80 	uddsize = (uint32_t)(nctrl->ncmd.s.more * 8);
81 
82 	datasize = OCTEON_CMD_SIZE + uddsize;
83 	rdatasize = (nctrl->wait_time) ? 16 : 0;
84 
85 	sc = lio_alloc_soft_command(oct, datasize, rdatasize,
86 				    sizeof(struct lio_ctrl_pkt));
87 
88 	if (sc == NULL)
89 		return (NULL);
90 
91 	memcpy(sc->ctxptr, nctrl, sizeof(struct lio_ctrl_pkt));
92 
93 	data = (uint8_t *)sc->virtdptr;
94 
95 	memcpy(data, &nctrl->ncmd, OCTEON_CMD_SIZE);
96 
97 	lio_swap_8B_data((uint64_t *)data, (OCTEON_CMD_SIZE >> 3));
98 
99 	if (uddsize) {
100 		/* Endian-Swap for UDD should have been done by caller. */
101 		memcpy(data + OCTEON_CMD_SIZE, nctrl->udd, uddsize);
102 	}
103 	sc->iq_no = (uint32_t)nctrl->iq_no;
104 
105 	lio_prepare_soft_command(oct, sc, LIO_OPCODE_NIC, LIO_OPCODE_NIC_CMD, 0,
106 				 0, 0);
107 
108 	sc->callback = lio_ctrl_callback;
109 	sc->callback_arg = sc;
110 	sc->wait_time = nctrl->wait_time;
111 
112 	return (sc);
113 }
114 
115 int
116 lio_send_ctrl_pkt(struct octeon_device *oct, struct lio_ctrl_pkt *nctrl)
117 {
118 	struct lio_soft_command	*sc = NULL;
119 	int	retval;
120 
121 	mtx_lock(&oct->cmd_resp_wqlock);
122 	/*
123 	 * Allow only rx ctrl command to stop traffic on the chip
124 	 * during offline operations
125 	 */
126 	if ((oct->cmd_resp_state == LIO_DRV_OFFLINE) &&
127 	    (nctrl->ncmd.s.cmd != LIO_CMD_RX_CTL)) {
128 		mtx_unlock(&oct->cmd_resp_wqlock);
129 		lio_dev_err(oct, "%s cmd:%d not processed since driver offline\n",
130 			    __func__, nctrl->ncmd.s.cmd);
131 		return (-1);
132 	}
133 
134 	sc = lio_alloc_ctrl_pkt_sc(oct, nctrl);
135 	if (sc == NULL) {
136 		lio_dev_err(oct, "%s soft command alloc failed\n", __func__);
137 		mtx_unlock(&oct->cmd_resp_wqlock);
138 		return (-1);
139 	}
140 
141 	retval = lio_send_soft_command(oct, sc);
142 	if (retval == LIO_IQ_SEND_FAILED) {
143 		lio_free_soft_command(oct, sc);
144 		lio_dev_err(oct, "%s pf_num:%d soft command:%d send failed status: %x\n",
145 			    __func__, oct->pf_num, nctrl->ncmd.s.cmd, retval);
146 		mtx_unlock(&oct->cmd_resp_wqlock);
147 		return (-1);
148 	}
149 
150 	mtx_unlock(&oct->cmd_resp_wqlock);
151 	return (retval);
152 }
153