blob: 40ac1fe889569de3f33660656a2d4e7187865364 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2015 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * This file may also be available under a different license from Cavium.
20 * Contact Cavium, Inc. for more information
21 **********************************************************************/
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070022#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include <linux/netdevice.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include "liquidio_common.h"
25#include "octeon_droq.h"
26#include "octeon_iq.h"
27#include "response_manager.h"
28#include "octeon_device.h"
29#include "octeon_nic.h"
30#include "octeon_main.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070031
32void *
33octeon_alloc_soft_command_resp(struct octeon_device *oct,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -070034 union octeon_instr_64B *cmd,
35 u32 rdatasize)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070036{
37 struct octeon_soft_command *sc;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -070038 struct octeon_instr_ih3 *ih3;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -070039 struct octeon_instr_ih2 *ih2;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070040 struct octeon_instr_irh *irh;
41 struct octeon_instr_rdp *rdp;
42
43 sc = (struct octeon_soft_command *)
44 octeon_alloc_soft_command(oct, 0, rdatasize, 0);
45
46 if (!sc)
47 return NULL;
48
49 /* Copy existing command structure into the soft command */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -070050 memcpy(&sc->cmd, cmd, sizeof(union octeon_instr_64B));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070051
52 /* Add in the response related fields. Opcode and Param are already
53 * there.
54 */
Raghu Vatsavayi5b823512016-09-01 11:16:07 -070055 if (OCTEON_CN23XX_PF(oct)) {
56 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
57 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
58 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
59 /*pkiih3 + irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
60 ih3->fsz = LIO_SOFTCMDRESP_IH3;
61 } else {
62 ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
63 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
64 irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
65 /* irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
66 ih2->fsz = LIO_SOFTCMDRESP_IH2;
67 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070068
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070069 irh->rflag = 1; /* a response is required */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070070
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070071 rdp->pcie_port = oct->pcie_port;
72 rdp->rlen = rdatasize;
73
74 *sc->status_word = COMPLETION_WORD_INIT;
75
Raghu Vatsavayi5b823512016-09-01 11:16:07 -070076 if (OCTEON_CN23XX_PF(oct))
77 sc->cmd.cmd3.rptr = sc->dmarptr;
78 else
79 sc->cmd.cmd2.rptr = sc->dmarptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -070080
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070081 sc->wait_time = 1000;
82 sc->timeout = jiffies + sc->wait_time;
83
84 return sc;
85}
86
87int octnet_send_nic_data_pkt(struct octeon_device *oct,
Raghu Vatsavayi32581242016-08-31 11:03:20 -070088 struct octnic_data_pkt *ndata)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070089{
Raghu Vatsavayi32581242016-08-31 11:03:20 -070090 int ring_doorbell = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070091
92 return octeon_send_command(oct, ndata->q_no, ring_doorbell, &ndata->cmd,
93 ndata->buf, ndata->datasize,
94 ndata->reqtype);
95}
96
97static void octnet_link_ctrl_callback(struct octeon_device *oct,
98 u32 status,
99 void *sc_ptr)
100{
101 struct octeon_soft_command *sc = (struct octeon_soft_command *)sc_ptr;
102 struct octnic_ctrl_pkt *nctrl;
103
104 nctrl = (struct octnic_ctrl_pkt *)sc->ctxptr;
105
106 /* Call the callback function if status is OK.
107 * Status is OK only if a response was expected and core returned
108 * success.
109 * If no response was expected, status is OK if the command was posted
110 * successfully.
111 */
112 if (!status && nctrl->cb_fn)
113 nctrl->cb_fn(nctrl);
114
115 octeon_free_soft_command(oct, sc);
116}
117
118static inline struct octeon_soft_command
119*octnic_alloc_ctrl_pkt_sc(struct octeon_device *oct,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700120 struct octnic_ctrl_pkt *nctrl)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700121{
122 struct octeon_soft_command *sc = NULL;
123 u8 *data;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -0700124 u32 rdatasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700125 u32 uddsize = 0, datasize = 0;
126
127 uddsize = (u32)(nctrl->ncmd.s.more * 8);
128
129 datasize = OCTNET_CMD_SIZE + uddsize;
130 rdatasize = (nctrl->wait_time) ? 16 : 0;
131
132 sc = (struct octeon_soft_command *)
133 octeon_alloc_soft_command(oct, datasize, rdatasize,
134 sizeof(struct octnic_ctrl_pkt));
135
136 if (!sc)
137 return NULL;
138
139 memcpy(sc->ctxptr, nctrl, sizeof(struct octnic_ctrl_pkt));
140
141 data = (u8 *)sc->virtdptr;
142
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700143 memcpy(data, &nctrl->ncmd, OCTNET_CMD_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700144
145 octeon_swap_8B_data((u64 *)data, (OCTNET_CMD_SIZE >> 3));
146
147 if (uddsize) {
148 /* Endian-Swap for UDD should have been done by caller. */
149 memcpy(data + OCTNET_CMD_SIZE, nctrl->udd, uddsize);
150 }
151
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700152 sc->iq_no = (u32)nctrl->iq_no;
153
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700154 octeon_prepare_soft_command(oct, sc, OPCODE_NIC, OPCODE_NIC_CMD,
155 0, 0, 0);
156
157 sc->callback = octnet_link_ctrl_callback;
158 sc->callback_arg = sc;
159 sc->wait_time = nctrl->wait_time;
160
161 return sc;
162}
163
164int
165octnet_send_nic_ctrl_pkt(struct octeon_device *oct,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700166 struct octnic_ctrl_pkt *nctrl)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700167{
168 int retval;
169 struct octeon_soft_command *sc = NULL;
170
Raghu Vatsavayi60441882016-06-21 22:53:08 -0700171 spin_lock_bh(&oct->cmd_resp_wqlock);
172 /* Allow only rx ctrl command to stop traffic on the chip
173 * during offline operations
174 */
175 if ((oct->cmd_resp_state == OCT_DRV_OFFLINE) &&
176 (nctrl->ncmd.s.cmd != OCTNET_CMD_RX_CTL)) {
177 spin_unlock_bh(&oct->cmd_resp_wqlock);
178 dev_err(&oct->pci_dev->dev,
179 "%s cmd:%d not processed since driver offline\n",
180 __func__, nctrl->ncmd.s.cmd);
181 return -1;
182 }
183
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700184 sc = octnic_alloc_ctrl_pkt_sc(oct, nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700185 if (!sc) {
186 dev_err(&oct->pci_dev->dev, "%s soft command alloc failed\n",
187 __func__);
Raghu Vatsavayi60441882016-06-21 22:53:08 -0700188 spin_unlock_bh(&oct->cmd_resp_wqlock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700189 return -1;
190 }
191
192 retval = octeon_send_soft_command(oct, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -0700193 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700194 octeon_free_soft_command(oct, sc);
Raghu Vatsavayi5b823512016-09-01 11:16:07 -0700195 dev_err(&oct->pci_dev->dev, "%s pf_num:%d soft command:%d send failed status: %x\n",
196 __func__, oct->pf_num, nctrl->ncmd.s.cmd, retval);
Raghu Vatsavayi60441882016-06-21 22:53:08 -0700197 spin_unlock_bh(&oct->cmd_resp_wqlock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700198 return -1;
199 }
200
Raghu Vatsavayi60441882016-06-21 22:53:08 -0700201 spin_unlock_bh(&oct->cmd_resp_wqlock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700202 return retval;
203}