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