1*f173c2b7SSean Bruno /*
2*f173c2b7SSean Bruno * BSD LICENSE
3*f173c2b7SSean Bruno *
4*f173c2b7SSean Bruno * Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5*f173c2b7SSean Bruno * All rights reserved.
6*f173c2b7SSean Bruno *
7*f173c2b7SSean Bruno * Redistribution and use in source and binary forms, with or without
8*f173c2b7SSean Bruno * modification, are permitted provided that the following conditions
9*f173c2b7SSean Bruno * are met:
10*f173c2b7SSean Bruno *
11*f173c2b7SSean Bruno * * Redistributions of source code must retain the above copyright
12*f173c2b7SSean Bruno * notice, this list of conditions and the following disclaimer.
13*f173c2b7SSean Bruno * * Redistributions in binary form must reproduce the above copyright
14*f173c2b7SSean Bruno * notice, this list of conditions and the following disclaimer in
15*f173c2b7SSean Bruno * the documentation and/or other materials provided with the
16*f173c2b7SSean Bruno * distribution.
17*f173c2b7SSean Bruno * * Neither the name of Cavium, Inc. nor the names of its
18*f173c2b7SSean Bruno * contributors may be used to endorse or promote products derived
19*f173c2b7SSean Bruno * from this software without specific prior written permission.
20*f173c2b7SSean Bruno *
21*f173c2b7SSean Bruno * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*f173c2b7SSean Bruno * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*f173c2b7SSean Bruno * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24*f173c2b7SSean Bruno * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25*f173c2b7SSean Bruno * OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26*f173c2b7SSean Bruno * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27*f173c2b7SSean Bruno * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*f173c2b7SSean Bruno * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*f173c2b7SSean Bruno * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*f173c2b7SSean Bruno * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*f173c2b7SSean Bruno * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*f173c2b7SSean Bruno */
33*f173c2b7SSean Bruno
34*f173c2b7SSean Bruno #include "lio_bsd.h"
35*f173c2b7SSean Bruno #include "lio_common.h"
36*f173c2b7SSean Bruno #include "lio_droq.h"
37*f173c2b7SSean Bruno #include "lio_iq.h"
38*f173c2b7SSean Bruno #include "lio_response_manager.h"
39*f173c2b7SSean Bruno #include "lio_device.h"
40*f173c2b7SSean Bruno #include "lio_ctrl.h"
41*f173c2b7SSean Bruno #include "lio_main.h"
42*f173c2b7SSean Bruno
43*f173c2b7SSean Bruno int
lio_send_data_pkt(struct octeon_device * oct,struct lio_data_pkt * ndata)44*f173c2b7SSean Bruno lio_send_data_pkt(struct octeon_device *oct, struct lio_data_pkt *ndata)
45*f173c2b7SSean Bruno {
46*f173c2b7SSean Bruno int ring_doorbell = 1;
47*f173c2b7SSean Bruno
48*f173c2b7SSean Bruno return (lio_send_command(oct, ndata->q_no, ring_doorbell, &ndata->cmd,
49*f173c2b7SSean Bruno ndata->buf, ndata->datasize, ndata->reqtype));
50*f173c2b7SSean Bruno }
51*f173c2b7SSean Bruno
52*f173c2b7SSean Bruno static void
lio_ctrl_callback(struct octeon_device * oct,uint32_t status,void * sc_ptr)53*f173c2b7SSean Bruno lio_ctrl_callback(struct octeon_device *oct, uint32_t status, void *sc_ptr)
54*f173c2b7SSean Bruno {
55*f173c2b7SSean Bruno struct lio_soft_command *sc = (struct lio_soft_command *)sc_ptr;
56*f173c2b7SSean Bruno struct lio_ctrl_pkt *nctrl;
57*f173c2b7SSean Bruno
58*f173c2b7SSean Bruno nctrl = (struct lio_ctrl_pkt *)sc->ctxptr;
59*f173c2b7SSean Bruno
60*f173c2b7SSean Bruno /*
61*f173c2b7SSean Bruno * Call the callback function if status is OK.
62*f173c2b7SSean Bruno * Status is OK only if a response was expected and core returned
63*f173c2b7SSean Bruno * success.
64*f173c2b7SSean Bruno * If no response was expected, status is OK if the command was posted
65*f173c2b7SSean Bruno * successfully.
66*f173c2b7SSean Bruno */
67*f173c2b7SSean Bruno if (!status && nctrl->cb_fn)
68*f173c2b7SSean Bruno nctrl->cb_fn(nctrl);
69*f173c2b7SSean Bruno
70*f173c2b7SSean Bruno lio_free_soft_command(oct, sc);
71*f173c2b7SSean Bruno }
72*f173c2b7SSean Bruno
73*f173c2b7SSean Bruno static inline struct lio_soft_command *
lio_alloc_ctrl_pkt_sc(struct octeon_device * oct,struct lio_ctrl_pkt * nctrl)74*f173c2b7SSean Bruno lio_alloc_ctrl_pkt_sc(struct octeon_device *oct, struct lio_ctrl_pkt *nctrl)
75*f173c2b7SSean Bruno {
76*f173c2b7SSean Bruno struct lio_soft_command *sc = NULL;
77*f173c2b7SSean Bruno uint32_t datasize = 0, rdatasize, uddsize = 0;
78*f173c2b7SSean Bruno uint8_t *data;
79*f173c2b7SSean Bruno
80*f173c2b7SSean Bruno uddsize = (uint32_t)(nctrl->ncmd.s.more * 8);
81*f173c2b7SSean Bruno
82*f173c2b7SSean Bruno datasize = OCTEON_CMD_SIZE + uddsize;
83*f173c2b7SSean Bruno rdatasize = (nctrl->wait_time) ? 16 : 0;
84*f173c2b7SSean Bruno
85*f173c2b7SSean Bruno sc = lio_alloc_soft_command(oct, datasize, rdatasize,
86*f173c2b7SSean Bruno sizeof(struct lio_ctrl_pkt));
87*f173c2b7SSean Bruno
88*f173c2b7SSean Bruno if (sc == NULL)
89*f173c2b7SSean Bruno return (NULL);
90*f173c2b7SSean Bruno
91*f173c2b7SSean Bruno memcpy(sc->ctxptr, nctrl, sizeof(struct lio_ctrl_pkt));
92*f173c2b7SSean Bruno
93*f173c2b7SSean Bruno data = (uint8_t *)sc->virtdptr;
94*f173c2b7SSean Bruno
95*f173c2b7SSean Bruno memcpy(data, &nctrl->ncmd, OCTEON_CMD_SIZE);
96*f173c2b7SSean Bruno
97*f173c2b7SSean Bruno lio_swap_8B_data((uint64_t *)data, (OCTEON_CMD_SIZE >> 3));
98*f173c2b7SSean Bruno
99*f173c2b7SSean Bruno if (uddsize) {
100*f173c2b7SSean Bruno /* Endian-Swap for UDD should have been done by caller. */
101*f173c2b7SSean Bruno memcpy(data + OCTEON_CMD_SIZE, nctrl->udd, uddsize);
102*f173c2b7SSean Bruno }
103*f173c2b7SSean Bruno sc->iq_no = (uint32_t)nctrl->iq_no;
104*f173c2b7SSean Bruno
105*f173c2b7SSean Bruno lio_prepare_soft_command(oct, sc, LIO_OPCODE_NIC, LIO_OPCODE_NIC_CMD, 0,
106*f173c2b7SSean Bruno 0, 0);
107*f173c2b7SSean Bruno
108*f173c2b7SSean Bruno sc->callback = lio_ctrl_callback;
109*f173c2b7SSean Bruno sc->callback_arg = sc;
110*f173c2b7SSean Bruno sc->wait_time = nctrl->wait_time;
111*f173c2b7SSean Bruno
112*f173c2b7SSean Bruno return (sc);
113*f173c2b7SSean Bruno }
114*f173c2b7SSean Bruno
115*f173c2b7SSean Bruno int
lio_send_ctrl_pkt(struct octeon_device * oct,struct lio_ctrl_pkt * nctrl)116*f173c2b7SSean Bruno lio_send_ctrl_pkt(struct octeon_device *oct, struct lio_ctrl_pkt *nctrl)
117*f173c2b7SSean Bruno {
118*f173c2b7SSean Bruno struct lio_soft_command *sc = NULL;
119*f173c2b7SSean Bruno int retval;
120*f173c2b7SSean Bruno
121*f173c2b7SSean Bruno mtx_lock(&oct->cmd_resp_wqlock);
122*f173c2b7SSean Bruno /*
123*f173c2b7SSean Bruno * Allow only rx ctrl command to stop traffic on the chip
124*f173c2b7SSean Bruno * during offline operations
125*f173c2b7SSean Bruno */
126*f173c2b7SSean Bruno if ((oct->cmd_resp_state == LIO_DRV_OFFLINE) &&
127*f173c2b7SSean Bruno (nctrl->ncmd.s.cmd != LIO_CMD_RX_CTL)) {
128*f173c2b7SSean Bruno mtx_unlock(&oct->cmd_resp_wqlock);
129*f173c2b7SSean Bruno lio_dev_err(oct, "%s cmd:%d not processed since driver offline\n",
130*f173c2b7SSean Bruno __func__, nctrl->ncmd.s.cmd);
131*f173c2b7SSean Bruno return (-1);
132*f173c2b7SSean Bruno }
133*f173c2b7SSean Bruno
134*f173c2b7SSean Bruno sc = lio_alloc_ctrl_pkt_sc(oct, nctrl);
135*f173c2b7SSean Bruno if (sc == NULL) {
136*f173c2b7SSean Bruno lio_dev_err(oct, "%s soft command alloc failed\n", __func__);
137*f173c2b7SSean Bruno mtx_unlock(&oct->cmd_resp_wqlock);
138*f173c2b7SSean Bruno return (-1);
139*f173c2b7SSean Bruno }
140*f173c2b7SSean Bruno
141*f173c2b7SSean Bruno retval = lio_send_soft_command(oct, sc);
142*f173c2b7SSean Bruno if (retval == LIO_IQ_SEND_FAILED) {
143*f173c2b7SSean Bruno lio_free_soft_command(oct, sc);
144*f173c2b7SSean Bruno lio_dev_err(oct, "%s pf_num:%d soft command:%d send failed status: %x\n",
145*f173c2b7SSean Bruno __func__, oct->pf_num, nctrl->ncmd.s.cmd, retval);
146*f173c2b7SSean Bruno mtx_unlock(&oct->cmd_resp_wqlock);
147*f173c2b7SSean Bruno return (-1);
148*f173c2b7SSean Bruno }
149*f173c2b7SSean Bruno
150*f173c2b7SSean Bruno mtx_unlock(&oct->cmd_resp_wqlock);
151*f173c2b7SSean Bruno return (retval);
152*f173c2b7SSean Bruno }
153