xref: /linux/drivers/net/ethernet/brocade/bna/bfa_cee.c (revision 5a02bf4f)
152fa7bf9SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f844a0eaSJeff Kirsher /*
32732ba56SRasesh Mody  * Linux network driver for QLogic BR-series Converged Network Adapter.
4f844a0eaSJeff Kirsher  */
5f844a0eaSJeff Kirsher /*
62732ba56SRasesh Mody  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
72732ba56SRasesh Mody  * Copyright (c) 2014-2015 QLogic Corporation
8f844a0eaSJeff Kirsher  * All rights reserved
92732ba56SRasesh Mody  * www.qlogic.com
10f844a0eaSJeff Kirsher  */
11f844a0eaSJeff Kirsher 
12f844a0eaSJeff Kirsher #include "bfa_cee.h"
13f844a0eaSJeff Kirsher #include "bfi_cna.h"
14f844a0eaSJeff Kirsher #include "bfa_ioc.h"
15f844a0eaSJeff Kirsher 
16f844a0eaSJeff Kirsher static void bfa_cee_format_lldp_cfg(struct bfa_cee_lldp_cfg *lldp_cfg);
17f844a0eaSJeff Kirsher static void bfa_cee_format_cee_cfg(void *buffer);
18f844a0eaSJeff Kirsher 
19f844a0eaSJeff Kirsher static void
bfa_cee_format_cee_cfg(void * buffer)20f844a0eaSJeff Kirsher bfa_cee_format_cee_cfg(void *buffer)
21f844a0eaSJeff Kirsher {
22f844a0eaSJeff Kirsher 	struct bfa_cee_attr *cee_cfg = buffer;
23f844a0eaSJeff Kirsher 	bfa_cee_format_lldp_cfg(&cee_cfg->lldp_remote);
24f844a0eaSJeff Kirsher }
25f844a0eaSJeff Kirsher 
26f844a0eaSJeff Kirsher static void
bfa_cee_stats_swap(struct bfa_cee_stats * stats)27f844a0eaSJeff Kirsher bfa_cee_stats_swap(struct bfa_cee_stats *stats)
28f844a0eaSJeff Kirsher {
29f844a0eaSJeff Kirsher 	u32 *buffer = (u32 *)stats;
30f844a0eaSJeff Kirsher 	int i;
31f844a0eaSJeff Kirsher 
32f844a0eaSJeff Kirsher 	for (i = 0; i < (sizeof(struct bfa_cee_stats) / sizeof(u32));
33f844a0eaSJeff Kirsher 		i++) {
34f844a0eaSJeff Kirsher 		buffer[i] = ntohl(buffer[i]);
35f844a0eaSJeff Kirsher 	}
36f844a0eaSJeff Kirsher }
37f844a0eaSJeff Kirsher 
38f844a0eaSJeff Kirsher static void
bfa_cee_format_lldp_cfg(struct bfa_cee_lldp_cfg * lldp_cfg)39f844a0eaSJeff Kirsher bfa_cee_format_lldp_cfg(struct bfa_cee_lldp_cfg *lldp_cfg)
40f844a0eaSJeff Kirsher {
41f844a0eaSJeff Kirsher 	lldp_cfg->time_to_live =
42f844a0eaSJeff Kirsher 			ntohs(lldp_cfg->time_to_live);
43f844a0eaSJeff Kirsher 	lldp_cfg->enabled_system_cap =
44f844a0eaSJeff Kirsher 			ntohs(lldp_cfg->enabled_system_cap);
45f844a0eaSJeff Kirsher }
46f844a0eaSJeff Kirsher 
47f844a0eaSJeff Kirsher /**
4849ce9c2cSBen Hutchings  * bfa_cee_attr_meminfo - Returns the size of the DMA memory needed by CEE attributes
49f844a0eaSJeff Kirsher  */
50f844a0eaSJeff Kirsher static u32
bfa_cee_attr_meminfo(void)51f844a0eaSJeff Kirsher bfa_cee_attr_meminfo(void)
52f844a0eaSJeff Kirsher {
53f844a0eaSJeff Kirsher 	return roundup(sizeof(struct bfa_cee_attr), BFA_DMA_ALIGN_SZ);
54f844a0eaSJeff Kirsher }
55f844a0eaSJeff Kirsher /**
5649ce9c2cSBen Hutchings  * bfa_cee_stats_meminfo - Returns the size of the DMA memory needed by CEE stats
57f844a0eaSJeff Kirsher  */
58f844a0eaSJeff Kirsher static u32
bfa_cee_stats_meminfo(void)59f844a0eaSJeff Kirsher bfa_cee_stats_meminfo(void)
60f844a0eaSJeff Kirsher {
61f844a0eaSJeff Kirsher 	return roundup(sizeof(struct bfa_cee_stats), BFA_DMA_ALIGN_SZ);
62f844a0eaSJeff Kirsher }
63f844a0eaSJeff Kirsher 
64f844a0eaSJeff Kirsher /**
6549ce9c2cSBen Hutchings  * bfa_cee_get_attr_isr - CEE ISR for get-attributes responses from f/w
66f844a0eaSJeff Kirsher  *
6749ce9c2cSBen Hutchings  * @cee: Pointer to the CEE module
6849ce9c2cSBen Hutchings  * @status: Return status from the f/w
69f844a0eaSJeff Kirsher  */
70f844a0eaSJeff Kirsher static void
bfa_cee_get_attr_isr(struct bfa_cee * cee,enum bfa_status status)71f844a0eaSJeff Kirsher bfa_cee_get_attr_isr(struct bfa_cee *cee, enum bfa_status status)
72f844a0eaSJeff Kirsher {
73f844a0eaSJeff Kirsher 	cee->get_attr_status = status;
74f844a0eaSJeff Kirsher 	if (status == BFA_STATUS_OK) {
75f844a0eaSJeff Kirsher 		memcpy(cee->attr, cee->attr_dma.kva,
76f844a0eaSJeff Kirsher 		    sizeof(struct bfa_cee_attr));
77f844a0eaSJeff Kirsher 		bfa_cee_format_cee_cfg(cee->attr);
78f844a0eaSJeff Kirsher 	}
79f844a0eaSJeff Kirsher 	cee->get_attr_pending = false;
80f844a0eaSJeff Kirsher 	if (cee->cbfn.get_attr_cbfn)
81f844a0eaSJeff Kirsher 		cee->cbfn.get_attr_cbfn(cee->cbfn.get_attr_cbarg, status);
82f844a0eaSJeff Kirsher }
83f844a0eaSJeff Kirsher 
84f844a0eaSJeff Kirsher /**
85*5a02bf4fSYang Shen  * bfa_cee_get_stats_isr - CEE ISR for get-stats responses from f/w
86f844a0eaSJeff Kirsher  *
8749ce9c2cSBen Hutchings  * @cee: Pointer to the CEE module
8849ce9c2cSBen Hutchings  * @status: Return status from the f/w
89f844a0eaSJeff Kirsher  */
90f844a0eaSJeff Kirsher static void
bfa_cee_get_stats_isr(struct bfa_cee * cee,enum bfa_status status)91f844a0eaSJeff Kirsher bfa_cee_get_stats_isr(struct bfa_cee *cee, enum bfa_status status)
92f844a0eaSJeff Kirsher {
93f844a0eaSJeff Kirsher 	cee->get_stats_status = status;
94f844a0eaSJeff Kirsher 	if (status == BFA_STATUS_OK) {
95f844a0eaSJeff Kirsher 		memcpy(cee->stats, cee->stats_dma.kva,
96f844a0eaSJeff Kirsher 			sizeof(struct bfa_cee_stats));
97f844a0eaSJeff Kirsher 		bfa_cee_stats_swap(cee->stats);
98f844a0eaSJeff Kirsher 	}
99f844a0eaSJeff Kirsher 	cee->get_stats_pending = false;
100f844a0eaSJeff Kirsher 	if (cee->cbfn.get_stats_cbfn)
101f844a0eaSJeff Kirsher 		cee->cbfn.get_stats_cbfn(cee->cbfn.get_stats_cbarg, status);
102f844a0eaSJeff Kirsher }
103f844a0eaSJeff Kirsher 
104f844a0eaSJeff Kirsher /**
105d0ea5cbdSJesse Brandeburg  * bfa_cee_reset_stats_isr - CEE ISR for reset-stats responses from f/w
106f844a0eaSJeff Kirsher  *
107d0ea5cbdSJesse Brandeburg  * @cee: Input Pointer to the CEE module
108d0ea5cbdSJesse Brandeburg  * @status: Return status from the f/w
109f844a0eaSJeff Kirsher  */
110f844a0eaSJeff Kirsher static void
bfa_cee_reset_stats_isr(struct bfa_cee * cee,enum bfa_status status)111f844a0eaSJeff Kirsher bfa_cee_reset_stats_isr(struct bfa_cee *cee, enum bfa_status status)
112f844a0eaSJeff Kirsher {
113f844a0eaSJeff Kirsher 	cee->reset_stats_status = status;
114f844a0eaSJeff Kirsher 	cee->reset_stats_pending = false;
115f844a0eaSJeff Kirsher 	if (cee->cbfn.reset_stats_cbfn)
116f844a0eaSJeff Kirsher 		cee->cbfn.reset_stats_cbfn(cee->cbfn.reset_stats_cbarg, status);
117f844a0eaSJeff Kirsher }
118f844a0eaSJeff Kirsher /**
1191aa8b471SBen Hutchings  * bfa_nw_cee_meminfo - Returns the size of the DMA memory needed by CEE module
120f844a0eaSJeff Kirsher  */
121f844a0eaSJeff Kirsher u32
bfa_nw_cee_meminfo(void)122f844a0eaSJeff Kirsher bfa_nw_cee_meminfo(void)
123f844a0eaSJeff Kirsher {
124f844a0eaSJeff Kirsher 	return bfa_cee_attr_meminfo() + bfa_cee_stats_meminfo();
125f844a0eaSJeff Kirsher }
126f844a0eaSJeff Kirsher 
127f844a0eaSJeff Kirsher /**
1281aa8b471SBen Hutchings  * bfa_nw_cee_mem_claim - Initialized CEE DMA Memory
129f844a0eaSJeff Kirsher  *
1301aa8b471SBen Hutchings  * @cee: CEE module pointer
1311aa8b471SBen Hutchings  * @dma_kva: Kernel Virtual Address of CEE DMA Memory
1321aa8b471SBen Hutchings  * @dma_pa:  Physical Address of CEE DMA Memory
133f844a0eaSJeff Kirsher  */
134f844a0eaSJeff Kirsher void
bfa_nw_cee_mem_claim(struct bfa_cee * cee,u8 * dma_kva,u64 dma_pa)135f844a0eaSJeff Kirsher bfa_nw_cee_mem_claim(struct bfa_cee *cee, u8 *dma_kva, u64 dma_pa)
136f844a0eaSJeff Kirsher {
137f844a0eaSJeff Kirsher 	cee->attr_dma.kva = dma_kva;
138f844a0eaSJeff Kirsher 	cee->attr_dma.pa = dma_pa;
139f844a0eaSJeff Kirsher 	cee->stats_dma.kva = dma_kva + bfa_cee_attr_meminfo();
140f844a0eaSJeff Kirsher 	cee->stats_dma.pa = dma_pa + bfa_cee_attr_meminfo();
141f844a0eaSJeff Kirsher 	cee->attr = (struct bfa_cee_attr *) dma_kva;
142f844a0eaSJeff Kirsher 	cee->stats = (struct bfa_cee_stats *)
143f844a0eaSJeff Kirsher 		(dma_kva + bfa_cee_attr_meminfo());
144f844a0eaSJeff Kirsher }
145f844a0eaSJeff Kirsher 
146f844a0eaSJeff Kirsher /**
147d0ea5cbdSJesse Brandeburg  * bfa_nw_cee_get_attr - Send the request to the f/w to fetch CEE attributes.
1487afc5dbdSKrishna Gudipati  *
1491aa8b471SBen Hutchings  * @cee: Pointer to the CEE module data structure.
150d0ea5cbdSJesse Brandeburg  * @attr: attribute requested
151d0ea5cbdSJesse Brandeburg  * @cbfn: function pointer
152d0ea5cbdSJesse Brandeburg  * @cbarg: function pointer arguments
1537afc5dbdSKrishna Gudipati  *
1541aa8b471SBen Hutchings  * Return: status
1557afc5dbdSKrishna Gudipati  */
1567afc5dbdSKrishna Gudipati enum bfa_status
bfa_nw_cee_get_attr(struct bfa_cee * cee,struct bfa_cee_attr * attr,bfa_cee_get_attr_cbfn_t cbfn,void * cbarg)1577afc5dbdSKrishna Gudipati bfa_nw_cee_get_attr(struct bfa_cee *cee, struct bfa_cee_attr *attr,
1587afc5dbdSKrishna Gudipati 		    bfa_cee_get_attr_cbfn_t cbfn, void *cbarg)
1597afc5dbdSKrishna Gudipati {
1607afc5dbdSKrishna Gudipati 	struct bfi_cee_get_req *cmd;
1617afc5dbdSKrishna Gudipati 
1627afc5dbdSKrishna Gudipati 	BUG_ON(!((cee != NULL) && (cee->ioc != NULL)));
1637afc5dbdSKrishna Gudipati 	if (!bfa_nw_ioc_is_operational(cee->ioc))
1647afc5dbdSKrishna Gudipati 		return BFA_STATUS_IOC_FAILURE;
1657afc5dbdSKrishna Gudipati 
16623677ce3SJoe Perches 	if (cee->get_attr_pending)
1677afc5dbdSKrishna Gudipati 		return  BFA_STATUS_DEVBUSY;
1687afc5dbdSKrishna Gudipati 
1697afc5dbdSKrishna Gudipati 	cee->get_attr_pending = true;
1707afc5dbdSKrishna Gudipati 	cmd = (struct bfi_cee_get_req *) cee->get_cfg_mb.msg;
1717afc5dbdSKrishna Gudipati 	cee->attr = attr;
1727afc5dbdSKrishna Gudipati 	cee->cbfn.get_attr_cbfn = cbfn;
1737afc5dbdSKrishna Gudipati 	cee->cbfn.get_attr_cbarg = cbarg;
1747afc5dbdSKrishna Gudipati 	bfi_h2i_set(cmd->mh, BFI_MC_CEE, BFI_CEE_H2I_GET_CFG_REQ,
1757afc5dbdSKrishna Gudipati 		    bfa_ioc_portid(cee->ioc));
1767afc5dbdSKrishna Gudipati 	bfa_dma_be_addr_set(cmd->dma_addr, cee->attr_dma.pa);
1777afc5dbdSKrishna Gudipati 	bfa_nw_ioc_mbox_queue(cee->ioc, &cee->get_cfg_mb, NULL, NULL);
1787afc5dbdSKrishna Gudipati 
1797afc5dbdSKrishna Gudipati 	return BFA_STATUS_OK;
1807afc5dbdSKrishna Gudipati }
1817afc5dbdSKrishna Gudipati 
1827afc5dbdSKrishna Gudipati /**
183d0ea5cbdSJesse Brandeburg  * bfa_cee_isr - Handles Mail-box interrupts for CEE module.
184d0ea5cbdSJesse Brandeburg  * @cbarg: argument passed containing pointer to the CEE module data structure.
185d0ea5cbdSJesse Brandeburg  * @m: message pointer
186f844a0eaSJeff Kirsher  */
187f844a0eaSJeff Kirsher 
188f844a0eaSJeff Kirsher static void
bfa_cee_isr(void * cbarg,struct bfi_mbmsg * m)189f844a0eaSJeff Kirsher bfa_cee_isr(void *cbarg, struct bfi_mbmsg *m)
190f844a0eaSJeff Kirsher {
191f844a0eaSJeff Kirsher 	union bfi_cee_i2h_msg_u *msg;
192f844a0eaSJeff Kirsher 	struct bfi_cee_get_rsp *get_rsp;
193f844a0eaSJeff Kirsher 	struct bfa_cee *cee = (struct bfa_cee *) cbarg;
194f844a0eaSJeff Kirsher 	msg = (union bfi_cee_i2h_msg_u *) m;
195f844a0eaSJeff Kirsher 	get_rsp = (struct bfi_cee_get_rsp *) m;
196f844a0eaSJeff Kirsher 	switch (msg->mh.msg_id) {
197f844a0eaSJeff Kirsher 	case BFI_CEE_I2H_GET_CFG_RSP:
198f844a0eaSJeff Kirsher 		bfa_cee_get_attr_isr(cee, get_rsp->cmd_status);
199f844a0eaSJeff Kirsher 		break;
200f844a0eaSJeff Kirsher 	case BFI_CEE_I2H_GET_STATS_RSP:
201f844a0eaSJeff Kirsher 		bfa_cee_get_stats_isr(cee, get_rsp->cmd_status);
202f844a0eaSJeff Kirsher 		break;
203f844a0eaSJeff Kirsher 	case BFI_CEE_I2H_RESET_STATS_RSP:
204f844a0eaSJeff Kirsher 		bfa_cee_reset_stats_isr(cee, get_rsp->cmd_status);
205f844a0eaSJeff Kirsher 		break;
206f844a0eaSJeff Kirsher 	default:
207f844a0eaSJeff Kirsher 		BUG_ON(1);
208f844a0eaSJeff Kirsher 	}
209f844a0eaSJeff Kirsher }
210f844a0eaSJeff Kirsher 
211f844a0eaSJeff Kirsher /**
2121aa8b471SBen Hutchings  * bfa_cee_notify - CEE module heart-beat failure handler.
213f844a0eaSJeff Kirsher  *
214d0ea5cbdSJesse Brandeburg  * @arg: argument passed containing pointer to the CEE module data structure.
2151aa8b471SBen Hutchings  * @event: IOC event type
216f844a0eaSJeff Kirsher  */
217f844a0eaSJeff Kirsher 
218f844a0eaSJeff Kirsher static void
bfa_cee_notify(void * arg,enum bfa_ioc_event event)219f844a0eaSJeff Kirsher bfa_cee_notify(void *arg, enum bfa_ioc_event event)
220f844a0eaSJeff Kirsher {
221f844a0eaSJeff Kirsher 	struct bfa_cee *cee;
222f844a0eaSJeff Kirsher 	cee = (struct bfa_cee *) arg;
223f844a0eaSJeff Kirsher 
224f844a0eaSJeff Kirsher 	switch (event) {
225f844a0eaSJeff Kirsher 	case BFA_IOC_E_DISABLED:
226f844a0eaSJeff Kirsher 	case BFA_IOC_E_FAILED:
22723677ce3SJoe Perches 		if (cee->get_attr_pending) {
228f844a0eaSJeff Kirsher 			cee->get_attr_status = BFA_STATUS_FAILED;
229f844a0eaSJeff Kirsher 			cee->get_attr_pending  = false;
230f844a0eaSJeff Kirsher 			if (cee->cbfn.get_attr_cbfn) {
231f844a0eaSJeff Kirsher 				cee->cbfn.get_attr_cbfn(
232f844a0eaSJeff Kirsher 					cee->cbfn.get_attr_cbarg,
233f844a0eaSJeff Kirsher 					BFA_STATUS_FAILED);
234f844a0eaSJeff Kirsher 			}
235f844a0eaSJeff Kirsher 		}
23623677ce3SJoe Perches 		if (cee->get_stats_pending) {
237f844a0eaSJeff Kirsher 			cee->get_stats_status = BFA_STATUS_FAILED;
238f844a0eaSJeff Kirsher 			cee->get_stats_pending  = false;
239f844a0eaSJeff Kirsher 			if (cee->cbfn.get_stats_cbfn) {
240f844a0eaSJeff Kirsher 				cee->cbfn.get_stats_cbfn(
241f844a0eaSJeff Kirsher 					cee->cbfn.get_stats_cbarg,
242f844a0eaSJeff Kirsher 					BFA_STATUS_FAILED);
243f844a0eaSJeff Kirsher 			}
244f844a0eaSJeff Kirsher 		}
24523677ce3SJoe Perches 		if (cee->reset_stats_pending) {
246f844a0eaSJeff Kirsher 			cee->reset_stats_status = BFA_STATUS_FAILED;
247f844a0eaSJeff Kirsher 			cee->reset_stats_pending  = false;
248f844a0eaSJeff Kirsher 			if (cee->cbfn.reset_stats_cbfn) {
249f844a0eaSJeff Kirsher 				cee->cbfn.reset_stats_cbfn(
250f844a0eaSJeff Kirsher 					cee->cbfn.reset_stats_cbarg,
251f844a0eaSJeff Kirsher 					BFA_STATUS_FAILED);
252f844a0eaSJeff Kirsher 			}
253f844a0eaSJeff Kirsher 		}
254f844a0eaSJeff Kirsher 		break;
255f844a0eaSJeff Kirsher 
256f844a0eaSJeff Kirsher 	default:
257f844a0eaSJeff Kirsher 		break;
258f844a0eaSJeff Kirsher 	}
259f844a0eaSJeff Kirsher }
260f844a0eaSJeff Kirsher 
261f844a0eaSJeff Kirsher /**
2621aa8b471SBen Hutchings  * bfa_nw_cee_attach - CEE module-attach API
263f844a0eaSJeff Kirsher  *
2641aa8b471SBen Hutchings  * @cee: Pointer to the CEE module data structure
2651aa8b471SBen Hutchings  * @ioc: Pointer to the ioc module data structure
2661aa8b471SBen Hutchings  * @dev: Pointer to the device driver module data structure.
267f844a0eaSJeff Kirsher  *       The device driver specific mbox ISR functions have
268f844a0eaSJeff Kirsher  *       this pointer as one of the parameters.
269f844a0eaSJeff Kirsher  */
270f844a0eaSJeff Kirsher void
bfa_nw_cee_attach(struct bfa_cee * cee,struct bfa_ioc * ioc,void * dev)271f844a0eaSJeff Kirsher bfa_nw_cee_attach(struct bfa_cee *cee, struct bfa_ioc *ioc,
272f844a0eaSJeff Kirsher 		void *dev)
273f844a0eaSJeff Kirsher {
274f844a0eaSJeff Kirsher 	BUG_ON(!(cee != NULL));
275f844a0eaSJeff Kirsher 	cee->dev = dev;
276f844a0eaSJeff Kirsher 	cee->ioc = ioc;
277f844a0eaSJeff Kirsher 
278f844a0eaSJeff Kirsher 	bfa_nw_ioc_mbox_regisr(cee->ioc, BFI_MC_CEE, bfa_cee_isr, cee);
279f844a0eaSJeff Kirsher 	bfa_ioc_notify_init(&cee->ioc_notify, bfa_cee_notify, cee);
280f844a0eaSJeff Kirsher 	bfa_nw_ioc_notify_register(cee->ioc, &cee->ioc_notify);
281f844a0eaSJeff Kirsher }
282