Linux-libre 4.14.82-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / cavium / liquidio / octeon_nic.c
1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2016 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 #include <linux/pci.h>
20 #include <linux/netdevice.h>
21 #include "liquidio_common.h"
22 #include "octeon_droq.h"
23 #include "octeon_iq.h"
24 #include "response_manager.h"
25 #include "octeon_device.h"
26 #include "octeon_nic.h"
27 #include "octeon_main.h"
28
29 void *
30 octeon_alloc_soft_command_resp(struct octeon_device    *oct,
31                                union octeon_instr_64B *cmd,
32                                u32                     rdatasize)
33 {
34         struct octeon_soft_command *sc;
35         struct octeon_instr_ih3  *ih3;
36         struct octeon_instr_ih2  *ih2;
37         struct octeon_instr_irh *irh;
38         struct octeon_instr_rdp *rdp;
39
40         sc = (struct octeon_soft_command *)
41                 octeon_alloc_soft_command(oct, 0, rdatasize, 0);
42
43         if (!sc)
44                 return NULL;
45
46         /* Copy existing command structure into the soft command */
47         memcpy(&sc->cmd, cmd, sizeof(union octeon_instr_64B));
48
49         /* Add in the response related fields. Opcode and Param are already
50          * there.
51          */
52         if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
53                 ih3      = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
54                 rdp     = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
55                 irh     = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
56                 /*pkiih3 + irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
57                 ih3->fsz = LIO_SOFTCMDRESP_IH3;
58         } else {
59                 ih2      = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
60                 rdp     = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
61                 irh     = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
62                 /* irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
63                 ih2->fsz = LIO_SOFTCMDRESP_IH2;
64         }
65
66         irh->rflag = 1; /* a response is required */
67
68         rdp->pcie_port = oct->pcie_port;
69         rdp->rlen      = rdatasize;
70
71         *sc->status_word = COMPLETION_WORD_INIT;
72
73         if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct))
74                 sc->cmd.cmd3.rptr =  sc->dmarptr;
75         else
76                 sc->cmd.cmd2.rptr =  sc->dmarptr;
77
78         sc->wait_time = 1000;
79         sc->timeout = jiffies + sc->wait_time;
80
81         return sc;
82 }
83
84 int octnet_send_nic_data_pkt(struct octeon_device *oct,
85                              struct octnic_data_pkt *ndata)
86 {
87         int ring_doorbell = 1;
88
89         return octeon_send_command(oct, ndata->q_no, ring_doorbell, &ndata->cmd,
90                                    ndata->buf, ndata->datasize,
91                                    ndata->reqtype);
92 }
93
94 static void octnet_link_ctrl_callback(struct octeon_device *oct,
95                                       u32 status,
96                                       void *sc_ptr)
97 {
98         struct octeon_soft_command *sc = (struct octeon_soft_command *)sc_ptr;
99         struct octnic_ctrl_pkt *nctrl;
100
101         nctrl = (struct octnic_ctrl_pkt *)sc->ctxptr;
102
103         /* Call the callback function if status is zero (meaning OK) or status
104          * contains a firmware status code bigger than zero (meaning the
105          * firmware is reporting an error).
106          * If no response was expected, status is OK if the command was posted
107          * successfully.
108          */
109         if ((!status || status > FIRMWARE_STATUS_CODE(0)) && nctrl->cb_fn) {
110                 nctrl->status = status;
111                 nctrl->cb_fn(nctrl);
112         }
113
114         octeon_free_soft_command(oct, sc);
115 }
116
117 static inline struct octeon_soft_command
118 *octnic_alloc_ctrl_pkt_sc(struct octeon_device *oct,
119                           struct octnic_ctrl_pkt *nctrl)
120 {
121         struct octeon_soft_command *sc = NULL;
122         u8 *data;
123         u32 rdatasize;
124         u32 uddsize = 0, datasize = 0;
125
126         uddsize = (u32)(nctrl->ncmd.s.more * 8);
127
128         datasize = OCTNET_CMD_SIZE + uddsize;
129         rdatasize = (nctrl->wait_time) ? 16 : 0;
130
131         sc = (struct octeon_soft_command *)
132                 octeon_alloc_soft_command(oct, datasize, rdatasize,
133                                           sizeof(struct octnic_ctrl_pkt));
134
135         if (!sc)
136                 return NULL;
137
138         memcpy(sc->ctxptr, nctrl, sizeof(struct octnic_ctrl_pkt));
139
140         data = (u8 *)sc->virtdptr;
141
142         memcpy(data, &nctrl->ncmd, OCTNET_CMD_SIZE);
143
144         octeon_swap_8B_data((u64 *)data, (OCTNET_CMD_SIZE >> 3));
145
146         if (uddsize) {
147                 /* Endian-Swap for UDD should have been done by caller. */
148                 memcpy(data + OCTNET_CMD_SIZE, nctrl->udd, uddsize);
149         }
150
151         sc->iq_no = (u32)nctrl->iq_no;
152
153         octeon_prepare_soft_command(oct, sc, OPCODE_NIC, OPCODE_NIC_CMD,
154                                     0, 0, 0);
155
156         sc->callback = octnet_link_ctrl_callback;
157         sc->callback_arg = sc;
158         sc->wait_time = nctrl->wait_time;
159
160         return sc;
161 }
162
163 int
164 octnet_send_nic_ctrl_pkt(struct octeon_device *oct,
165                          struct octnic_ctrl_pkt *nctrl)
166 {
167         int retval;
168         struct octeon_soft_command *sc = NULL;
169
170         spin_lock_bh(&oct->cmd_resp_wqlock);
171         /* Allow only rx ctrl command to stop traffic on the chip
172          * during offline operations
173          */
174         if ((oct->cmd_resp_state == OCT_DRV_OFFLINE) &&
175             (nctrl->ncmd.s.cmd != OCTNET_CMD_RX_CTL)) {
176                 spin_unlock_bh(&oct->cmd_resp_wqlock);
177                 dev_err(&oct->pci_dev->dev,
178                         "%s cmd:%d not processed since driver offline\n",
179                         __func__, nctrl->ncmd.s.cmd);
180                 return -1;
181         }
182
183         sc = octnic_alloc_ctrl_pkt_sc(oct, nctrl);
184         if (!sc) {
185                 dev_err(&oct->pci_dev->dev, "%s soft command alloc failed\n",
186                         __func__);
187                 spin_unlock_bh(&oct->cmd_resp_wqlock);
188                 return -1;
189         }
190
191         retval = octeon_send_soft_command(oct, sc);
192         if (retval == IQ_SEND_FAILED) {
193                 octeon_free_soft_command(oct, sc);
194                 dev_err(&oct->pci_dev->dev, "%s pf_num:%d soft command:%d send failed status: %x\n",
195                         __func__, oct->pf_num, nctrl->ncmd.s.cmd, retval);
196                 spin_unlock_bh(&oct->cmd_resp_wqlock);
197                 return -1;
198         }
199
200         spin_unlock_bh(&oct->cmd_resp_wqlock);
201         return retval;
202 }