1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Linux network driver for QLogic BR-series Converged Network Adapter.
4  */
5 /*
6  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
7  * Copyright (c) 2014-2015 QLogic Corporation
8  * All rights reserved
9  * www.qlogic.com
10  */
11 
12 #include "bfa_cee.h"
13 #include "bfi_cna.h"
14 #include "bfa_ioc.h"
15 
16 static void bfa_cee_format_lldp_cfg(struct bfa_cee_lldp_cfg *lldp_cfg);
17 static void bfa_cee_format_cee_cfg(void *buffer);
18 
19 static void
20 bfa_cee_format_cee_cfg(void *buffer)
21 {
22 	struct bfa_cee_attr *cee_cfg = buffer;
23 	bfa_cee_format_lldp_cfg(&cee_cfg->lldp_remote);
24 }
25 
26 static void
27 bfa_cee_stats_swap(struct bfa_cee_stats *stats)
28 {
29 	u32 *buffer = (u32 *)stats;
30 	int i;
31 
32 	for (i = 0; i < (sizeof(struct bfa_cee_stats) / sizeof(u32));
33 		i++) {
34 		buffer[i] = ntohl(buffer[i]);
35 	}
36 }
37 
38 static void
39 bfa_cee_format_lldp_cfg(struct bfa_cee_lldp_cfg *lldp_cfg)
40 {
41 	lldp_cfg->time_to_live =
42 			ntohs(lldp_cfg->time_to_live);
43 	lldp_cfg->enabled_system_cap =
44 			ntohs(lldp_cfg->enabled_system_cap);
45 }
46 
47 /**
48  * bfa_cee_attr_meminfo - Returns the size of the DMA memory needed by CEE attributes
49  */
50 static u32
51 bfa_cee_attr_meminfo(void)
52 {
53 	return roundup(sizeof(struct bfa_cee_attr), BFA_DMA_ALIGN_SZ);
54 }
55 /**
56  * bfa_cee_stats_meminfo - Returns the size of the DMA memory needed by CEE stats
57  */
58 static u32
59 bfa_cee_stats_meminfo(void)
60 {
61 	return roundup(sizeof(struct bfa_cee_stats), BFA_DMA_ALIGN_SZ);
62 }
63 
64 /**
65  * bfa_cee_get_attr_isr - CEE ISR for get-attributes responses from f/w
66  *
67  * @cee: Pointer to the CEE module
68  * @status: Return status from the f/w
69  */
70 static void
71 bfa_cee_get_attr_isr(struct bfa_cee *cee, enum bfa_status status)
72 {
73 	cee->get_attr_status = status;
74 	if (status == BFA_STATUS_OK) {
75 		memcpy(cee->attr, cee->attr_dma.kva,
76 		    sizeof(struct bfa_cee_attr));
77 		bfa_cee_format_cee_cfg(cee->attr);
78 	}
79 	cee->get_attr_pending = false;
80 	if (cee->cbfn.get_attr_cbfn)
81 		cee->cbfn.get_attr_cbfn(cee->cbfn.get_attr_cbarg, status);
82 }
83 
84 /**
85  * bfa_cee_get_attr_isr - CEE ISR for get-stats responses from f/w
86  *
87  * @cee: Pointer to the CEE module
88  * @status: Return status from the f/w
89  */
90 static void
91 bfa_cee_get_stats_isr(struct bfa_cee *cee, enum bfa_status status)
92 {
93 	cee->get_stats_status = status;
94 	if (status == BFA_STATUS_OK) {
95 		memcpy(cee->stats, cee->stats_dma.kva,
96 			sizeof(struct bfa_cee_stats));
97 		bfa_cee_stats_swap(cee->stats);
98 	}
99 	cee->get_stats_pending = false;
100 	if (cee->cbfn.get_stats_cbfn)
101 		cee->cbfn.get_stats_cbfn(cee->cbfn.get_stats_cbarg, status);
102 }
103 
104 /**
105  * bfa_cee_get_attr_isr()
106  *
107  * @brief CEE ISR for reset-stats responses from f/w
108  *
109  * @param[in] cee - Pointer to the CEE module
110  *            status - Return status from the f/w
111  *
112  * @return void
113  */
114 static void
115 bfa_cee_reset_stats_isr(struct bfa_cee *cee, enum bfa_status status)
116 {
117 	cee->reset_stats_status = status;
118 	cee->reset_stats_pending = false;
119 	if (cee->cbfn.reset_stats_cbfn)
120 		cee->cbfn.reset_stats_cbfn(cee->cbfn.reset_stats_cbarg, status);
121 }
122 /**
123  * bfa_nw_cee_meminfo - Returns the size of the DMA memory needed by CEE module
124  */
125 u32
126 bfa_nw_cee_meminfo(void)
127 {
128 	return bfa_cee_attr_meminfo() + bfa_cee_stats_meminfo();
129 }
130 
131 /**
132  * bfa_nw_cee_mem_claim - Initialized CEE DMA Memory
133  *
134  * @cee: CEE module pointer
135  * @dma_kva: Kernel Virtual Address of CEE DMA Memory
136  * @dma_pa:  Physical Address of CEE DMA Memory
137  */
138 void
139 bfa_nw_cee_mem_claim(struct bfa_cee *cee, u8 *dma_kva, u64 dma_pa)
140 {
141 	cee->attr_dma.kva = dma_kva;
142 	cee->attr_dma.pa = dma_pa;
143 	cee->stats_dma.kva = dma_kva + bfa_cee_attr_meminfo();
144 	cee->stats_dma.pa = dma_pa + bfa_cee_attr_meminfo();
145 	cee->attr = (struct bfa_cee_attr *) dma_kva;
146 	cee->stats = (struct bfa_cee_stats *)
147 		(dma_kva + bfa_cee_attr_meminfo());
148 }
149 
150 /**
151  * bfa_cee_get_attr - Send the request to the f/w to fetch CEE attributes.
152  *
153  * @cee: Pointer to the CEE module data structure.
154  *
155  * Return: status
156  */
157 enum bfa_status
158 bfa_nw_cee_get_attr(struct bfa_cee *cee, struct bfa_cee_attr *attr,
159 		    bfa_cee_get_attr_cbfn_t cbfn, void *cbarg)
160 {
161 	struct bfi_cee_get_req *cmd;
162 
163 	BUG_ON(!((cee != NULL) && (cee->ioc != NULL)));
164 	if (!bfa_nw_ioc_is_operational(cee->ioc))
165 		return BFA_STATUS_IOC_FAILURE;
166 
167 	if (cee->get_attr_pending)
168 		return  BFA_STATUS_DEVBUSY;
169 
170 	cee->get_attr_pending = true;
171 	cmd = (struct bfi_cee_get_req *) cee->get_cfg_mb.msg;
172 	cee->attr = attr;
173 	cee->cbfn.get_attr_cbfn = cbfn;
174 	cee->cbfn.get_attr_cbarg = cbarg;
175 	bfi_h2i_set(cmd->mh, BFI_MC_CEE, BFI_CEE_H2I_GET_CFG_REQ,
176 		    bfa_ioc_portid(cee->ioc));
177 	bfa_dma_be_addr_set(cmd->dma_addr, cee->attr_dma.pa);
178 	bfa_nw_ioc_mbox_queue(cee->ioc, &cee->get_cfg_mb, NULL, NULL);
179 
180 	return BFA_STATUS_OK;
181 }
182 
183 /**
184  * bfa_cee_isrs - Handles Mail-box interrupts for CEE module.
185  */
186 
187 static void
188 bfa_cee_isr(void *cbarg, struct bfi_mbmsg *m)
189 {
190 	union bfi_cee_i2h_msg_u *msg;
191 	struct bfi_cee_get_rsp *get_rsp;
192 	struct bfa_cee *cee = (struct bfa_cee *) cbarg;
193 	msg = (union bfi_cee_i2h_msg_u *) m;
194 	get_rsp = (struct bfi_cee_get_rsp *) m;
195 	switch (msg->mh.msg_id) {
196 	case BFI_CEE_I2H_GET_CFG_RSP:
197 		bfa_cee_get_attr_isr(cee, get_rsp->cmd_status);
198 		break;
199 	case BFI_CEE_I2H_GET_STATS_RSP:
200 		bfa_cee_get_stats_isr(cee, get_rsp->cmd_status);
201 		break;
202 	case BFI_CEE_I2H_RESET_STATS_RSP:
203 		bfa_cee_reset_stats_isr(cee, get_rsp->cmd_status);
204 		break;
205 	default:
206 		BUG_ON(1);
207 	}
208 }
209 
210 /**
211  * bfa_cee_notify - CEE module heart-beat failure handler.
212  *
213  * @event: IOC event type
214  */
215 
216 static void
217 bfa_cee_notify(void *arg, enum bfa_ioc_event event)
218 {
219 	struct bfa_cee *cee;
220 	cee = (struct bfa_cee *) arg;
221 
222 	switch (event) {
223 	case BFA_IOC_E_DISABLED:
224 	case BFA_IOC_E_FAILED:
225 		if (cee->get_attr_pending) {
226 			cee->get_attr_status = BFA_STATUS_FAILED;
227 			cee->get_attr_pending  = false;
228 			if (cee->cbfn.get_attr_cbfn) {
229 				cee->cbfn.get_attr_cbfn(
230 					cee->cbfn.get_attr_cbarg,
231 					BFA_STATUS_FAILED);
232 			}
233 		}
234 		if (cee->get_stats_pending) {
235 			cee->get_stats_status = BFA_STATUS_FAILED;
236 			cee->get_stats_pending  = false;
237 			if (cee->cbfn.get_stats_cbfn) {
238 				cee->cbfn.get_stats_cbfn(
239 					cee->cbfn.get_stats_cbarg,
240 					BFA_STATUS_FAILED);
241 			}
242 		}
243 		if (cee->reset_stats_pending) {
244 			cee->reset_stats_status = BFA_STATUS_FAILED;
245 			cee->reset_stats_pending  = false;
246 			if (cee->cbfn.reset_stats_cbfn) {
247 				cee->cbfn.reset_stats_cbfn(
248 					cee->cbfn.reset_stats_cbarg,
249 					BFA_STATUS_FAILED);
250 			}
251 		}
252 		break;
253 
254 	default:
255 		break;
256 	}
257 }
258 
259 /**
260  * bfa_nw_cee_attach - CEE module-attach API
261  *
262  * @cee: Pointer to the CEE module data structure
263  * @ioc: Pointer to the ioc module data structure
264  * @dev: Pointer to the device driver module data structure.
265  *       The device driver specific mbox ISR functions have
266  *       this pointer as one of the parameters.
267  */
268 void
269 bfa_nw_cee_attach(struct bfa_cee *cee, struct bfa_ioc *ioc,
270 		void *dev)
271 {
272 	BUG_ON(!(cee != NULL));
273 	cee->dev = dev;
274 	cee->ioc = ioc;
275 
276 	bfa_nw_ioc_mbox_regisr(cee->ioc, BFI_MC_CEE, bfa_cee_isr, cee);
277 	bfa_ioc_notify_init(&cee->ioc_notify, bfa_cee_notify, cee);
278 	bfa_nw_ioc_notify_register(cee->ioc, &cee->ioc_notify);
279 }
280