xref: /freebsd/sys/dev/qlxgbe/ql_inline.h (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1718cf2ccSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
435291c22SDavid C Somayajulu  * Copyright (c) 2013-2016 Qlogic Corporation
5f10a77bbSDavid C Somayajulu  * All rights reserved.
6f10a77bbSDavid C Somayajulu  *
7f10a77bbSDavid C Somayajulu  *  Redistribution and use in source and binary forms, with or without
8f10a77bbSDavid C Somayajulu  *  modification, are permitted provided that the following conditions
9f10a77bbSDavid C Somayajulu  *  are met:
10f10a77bbSDavid C Somayajulu  *
11f10a77bbSDavid C Somayajulu  *  1. Redistributions of source code must retain the above copyright
12f10a77bbSDavid C Somayajulu  *     notice, this list of conditions and the following disclaimer.
13f10a77bbSDavid C Somayajulu  *  2. Redistributions in binary form must reproduce the above copyright
14f10a77bbSDavid C Somayajulu  *     notice, this list of conditions and the following disclaimer in the
15f10a77bbSDavid C Somayajulu  *     documentation and/or other materials provided with the distribution.
16f10a77bbSDavid C Somayajulu  *
17f10a77bbSDavid C Somayajulu  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18f10a77bbSDavid C Somayajulu  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f10a77bbSDavid C Somayajulu  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f10a77bbSDavid C Somayajulu  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21f10a77bbSDavid C Somayajulu  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22f10a77bbSDavid C Somayajulu  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23f10a77bbSDavid C Somayajulu  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24f10a77bbSDavid C Somayajulu  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25f10a77bbSDavid C Somayajulu  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26f10a77bbSDavid C Somayajulu  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27f10a77bbSDavid C Somayajulu  *  POSSIBILITY OF SUCH DAMAGE.
28f10a77bbSDavid C Somayajulu  *
29f10a77bbSDavid C Somayajulu  * $FreeBSD$
30f10a77bbSDavid C Somayajulu  */
31f10a77bbSDavid C Somayajulu /*
32f10a77bbSDavid C Somayajulu  * File: ql_inline.h
33f10a77bbSDavid C Somayajulu  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
34f10a77bbSDavid C Somayajulu  */
35f10a77bbSDavid C Somayajulu #ifndef _QL_INLINE_H_
36f10a77bbSDavid C Somayajulu #define _QL_INLINE_H_
37f10a77bbSDavid C Somayajulu 
38f10a77bbSDavid C Somayajulu #define QL8_SEMLOCK_TIMEOUT	1000/* QLA8020 Semaphore Lock Timeout 10ms */
39f10a77bbSDavid C Somayajulu 
40f10a77bbSDavid C Somayajulu /*
41f10a77bbSDavid C Somayajulu  * Inline functions for hardware semaphores
42f10a77bbSDavid C Somayajulu  */
43f10a77bbSDavid C Somayajulu 
44f10a77bbSDavid C Somayajulu /*
45f10a77bbSDavid C Somayajulu  * Name:	qla_sem_lock
46f10a77bbSDavid C Somayajulu  * Function:	Locks one of the semaphore registers (semaphore 2,3,5 & 7)
47f10a77bbSDavid C Somayajulu  *		If the id_reg is valid, then id_val is written into it.
48f10a77bbSDavid C Somayajulu  *		This is for debugging purpose
49f10a77bbSDavid C Somayajulu  * Returns:	0 on success; otherwise its failed.
50f10a77bbSDavid C Somayajulu  */
51f10a77bbSDavid C Somayajulu static __inline int
52f10a77bbSDavid C Somayajulu qla_sem_lock(qla_host_t *ha, uint32_t sem_reg, uint32_t id_reg, uint32_t id_val)
53f10a77bbSDavid C Somayajulu {
54f10a77bbSDavid C Somayajulu 	int count = QL8_SEMLOCK_TIMEOUT;
55f10a77bbSDavid C Somayajulu 
56f10a77bbSDavid C Somayajulu 	while (count) {
57f10a77bbSDavid C Somayajulu 		if ((READ_REG32(ha, sem_reg) & BIT_0))
58f10a77bbSDavid C Somayajulu 			break;
59f10a77bbSDavid C Somayajulu 		count--;
60f10a77bbSDavid C Somayajulu 
61f10a77bbSDavid C Somayajulu 		if (!count)
62f10a77bbSDavid C Somayajulu 			return(-1);
63f10a77bbSDavid C Somayajulu 		qla_mdelay(__func__, 10);
64f10a77bbSDavid C Somayajulu 	}
65f10a77bbSDavid C Somayajulu 	if (id_reg)
66f10a77bbSDavid C Somayajulu 		WRITE_REG32(ha, id_reg, id_val);
67f10a77bbSDavid C Somayajulu 
68f10a77bbSDavid C Somayajulu 	return(0);
69f10a77bbSDavid C Somayajulu }
70f10a77bbSDavid C Somayajulu 
71f10a77bbSDavid C Somayajulu /*
72f10a77bbSDavid C Somayajulu  * Name:	qla_sem_unlock
73f10a77bbSDavid C Somayajulu  * Function:	Unlocks the semaphore registers (semaphore 2,3,5 & 7)
74f10a77bbSDavid C Somayajulu  *		previously locked by qla_sem_lock()
75f10a77bbSDavid C Somayajulu  */
76f10a77bbSDavid C Somayajulu static __inline void
77f10a77bbSDavid C Somayajulu qla_sem_unlock(qla_host_t *ha, uint32_t sem_reg)
78f10a77bbSDavid C Somayajulu {
79f10a77bbSDavid C Somayajulu 	READ_REG32(ha, sem_reg);
80f10a77bbSDavid C Somayajulu }
81f10a77bbSDavid C Somayajulu 
82f10a77bbSDavid C Somayajulu static __inline int
83f10a77bbSDavid C Somayajulu qla_get_ifq_snd_maxlen(qla_host_t *ha)
84f10a77bbSDavid C Somayajulu {
85f10a77bbSDavid C Somayajulu 	return(((NUM_TX_DESCRIPTORS * 4) - 1));
86f10a77bbSDavid C Somayajulu }
87f10a77bbSDavid C Somayajulu 
88f10a77bbSDavid C Somayajulu static __inline uint32_t
89f10a77bbSDavid C Somayajulu qla_get_optics(qla_host_t *ha)
90f10a77bbSDavid C Somayajulu {
91f10a77bbSDavid C Somayajulu 	uint32_t link_speed;
92f10a77bbSDavid C Somayajulu 
93f10a77bbSDavid C Somayajulu 	link_speed = READ_REG32(ha, Q8_LINK_SPEED_0);
94f10a77bbSDavid C Somayajulu 	if (ha->pci_func == 0)
95f10a77bbSDavid C Somayajulu 		link_speed = link_speed & 0xFF;
96f10a77bbSDavid C Somayajulu 	else
97f10a77bbSDavid C Somayajulu 		link_speed = (link_speed >> 8) & 0xFF;
98f10a77bbSDavid C Somayajulu 
99f10a77bbSDavid C Somayajulu 	switch (link_speed) {
100f10a77bbSDavid C Somayajulu 	case 0x1:
101f10a77bbSDavid C Somayajulu 		link_speed = IFM_100_FX;
102f10a77bbSDavid C Somayajulu 		break;
103f10a77bbSDavid C Somayajulu 
104f10a77bbSDavid C Somayajulu 	case 0x10:
105f10a77bbSDavid C Somayajulu 		link_speed = IFM_1000_SX;
106f10a77bbSDavid C Somayajulu 		break;
107f10a77bbSDavid C Somayajulu 
108f10a77bbSDavid C Somayajulu 	default:
109f10a77bbSDavid C Somayajulu 		if ((ha->hw.module_type == 0x4) ||
110f10a77bbSDavid C Somayajulu 			(ha->hw.module_type == 0x5) ||
111f10a77bbSDavid C Somayajulu 			(ha->hw.module_type == 0x6))
112f10a77bbSDavid C Somayajulu 			link_speed = (IFM_10G_TWINAX);
113f10a77bbSDavid C Somayajulu 		else
114f10a77bbSDavid C Somayajulu 			link_speed = (IFM_10G_LR | IFM_10G_SR);
115f10a77bbSDavid C Somayajulu 		break;
116f10a77bbSDavid C Somayajulu 	}
117f10a77bbSDavid C Somayajulu 
118f10a77bbSDavid C Somayajulu 	return(link_speed);
119f10a77bbSDavid C Somayajulu }
120f10a77bbSDavid C Somayajulu 
121f10a77bbSDavid C Somayajulu static __inline uint8_t *
122f10a77bbSDavid C Somayajulu qla_get_mac_addr(qla_host_t *ha)
123f10a77bbSDavid C Somayajulu {
124f10a77bbSDavid C Somayajulu 	return (ha->hw.mac_addr);
125f10a77bbSDavid C Somayajulu }
126f10a77bbSDavid C Somayajulu 
127f10a77bbSDavid C Somayajulu static __inline void
128f10a77bbSDavid C Somayajulu qla_set_hw_rcv_desc(qla_host_t *ha, uint32_t r_idx, uint32_t index,
129f10a77bbSDavid C Somayajulu 	uint32_t handle, bus_addr_t paddr, uint32_t buf_size)
130f10a77bbSDavid C Somayajulu {
131f10a77bbSDavid C Somayajulu 	volatile q80_recv_desc_t *rcv_desc;
132f10a77bbSDavid C Somayajulu 
133f10a77bbSDavid C Somayajulu 	rcv_desc = (q80_recv_desc_t *)ha->hw.dma_buf.rds_ring[r_idx].dma_b;
134f10a77bbSDavid C Somayajulu 
135f10a77bbSDavid C Somayajulu 	rcv_desc += index;
136f10a77bbSDavid C Somayajulu 
137f10a77bbSDavid C Somayajulu 	rcv_desc->handle = (uint16_t)handle;
138f10a77bbSDavid C Somayajulu 	rcv_desc->buf_size = buf_size;
139f10a77bbSDavid C Somayajulu 	rcv_desc->buf_addr = paddr;
140f10a77bbSDavid C Somayajulu 
141f10a77bbSDavid C Somayajulu 	return;
142f10a77bbSDavid C Somayajulu }
143f10a77bbSDavid C Somayajulu 
144f10a77bbSDavid C Somayajulu static __inline void
145f10a77bbSDavid C Somayajulu qla_init_hw_rcv_descriptors(qla_host_t *ha)
146f10a77bbSDavid C Somayajulu {
147f10a77bbSDavid C Somayajulu 	int i;
148f10a77bbSDavid C Somayajulu 
149f10a77bbSDavid C Somayajulu 	for (i = 0; i < ha->hw.num_rds_rings; i++)
150f10a77bbSDavid C Somayajulu 		bzero((void *)ha->hw.dma_buf.rds_ring[i].dma_b,
151f10a77bbSDavid C Somayajulu 			(sizeof(q80_recv_desc_t) * NUM_RX_DESCRIPTORS));
152f10a77bbSDavid C Somayajulu }
153f10a77bbSDavid C Somayajulu 
1547fb51846SDavid C Somayajulu #define QLA_LOCK_DEFAULT_MS_TIMEOUT	3000
1557fb51846SDavid C Somayajulu 
1567fb51846SDavid C Somayajulu #ifndef QLA_LOCK_NO_SLEEP
1577fb51846SDavid C Somayajulu #define QLA_LOCK_NO_SLEEP		0
1587fb51846SDavid C Somayajulu #endif
1597fb51846SDavid C Somayajulu 
160f10a77bbSDavid C Somayajulu static __inline int
1617fb51846SDavid C Somayajulu qla_lock(qla_host_t *ha, const char *str, uint32_t timeout_ms,
1627fb51846SDavid C Somayajulu 	uint32_t no_sleep)
163f10a77bbSDavid C Somayajulu {
164f10a77bbSDavid C Somayajulu 	int ret = -1;
165f10a77bbSDavid C Somayajulu 
166f10a77bbSDavid C Somayajulu 	while (1) {
167f10a77bbSDavid C Somayajulu 		mtx_lock(&ha->hw_lock);
1687fb51846SDavid C Somayajulu 
169b65c0c07SDavid C Somayajulu 		if (ha->qla_detach_active || ha->offline) {
1707fb51846SDavid C Somayajulu 			mtx_unlock(&ha->hw_lock);
1717fb51846SDavid C Somayajulu 			break;
1727fb51846SDavid C Somayajulu 		}
1737fb51846SDavid C Somayajulu 
174f10a77bbSDavid C Somayajulu 		if (!ha->hw_lock_held) {
175f10a77bbSDavid C Somayajulu 			ha->hw_lock_held = 1;
176f10a77bbSDavid C Somayajulu 			ha->qla_lock = str;
177f10a77bbSDavid C Somayajulu 			ret = 0;
178f10a77bbSDavid C Somayajulu 			mtx_unlock(&ha->hw_lock);
179f10a77bbSDavid C Somayajulu 			break;
180f10a77bbSDavid C Somayajulu 		}
181f10a77bbSDavid C Somayajulu 		mtx_unlock(&ha->hw_lock);
182f10a77bbSDavid C Somayajulu 
1837fb51846SDavid C Somayajulu 		if (--timeout_ms == 0) {
1847fb51846SDavid C Somayajulu 			ha->hw_lock_failed++;
185f10a77bbSDavid C Somayajulu 			break;
1867fb51846SDavid C Somayajulu 		} else {
1877fb51846SDavid C Somayajulu 			if (no_sleep)
1887fb51846SDavid C Somayajulu 				DELAY(1000);
189f10a77bbSDavid C Somayajulu 			else
190f10a77bbSDavid C Somayajulu 				qla_mdelay(__func__, 1);
191f10a77bbSDavid C Somayajulu 		}
1927fb51846SDavid C Somayajulu 	}
1937fb51846SDavid C Somayajulu 
194b65c0c07SDavid C Somayajulu //	if (!ha->enable_error_recovery)
195b65c0c07SDavid C Somayajulu //		device_printf(ha->pci_dev, "%s: %s ret = %d\n", __func__,
196b65c0c07SDavid C Somayajulu //			str,ret);
197b65c0c07SDavid C Somayajulu 
198f10a77bbSDavid C Somayajulu 	return (ret);
199f10a77bbSDavid C Somayajulu }
200f10a77bbSDavid C Somayajulu 
201f10a77bbSDavid C Somayajulu static __inline void
202f10a77bbSDavid C Somayajulu qla_unlock(qla_host_t *ha, const char *str)
203f10a77bbSDavid C Somayajulu {
204f10a77bbSDavid C Somayajulu 	mtx_lock(&ha->hw_lock);
205f10a77bbSDavid C Somayajulu 	ha->hw_lock_held = 0;
206f10a77bbSDavid C Somayajulu 	ha->qla_unlock = str;
207f10a77bbSDavid C Somayajulu 	mtx_unlock(&ha->hw_lock);
208b65c0c07SDavid C Somayajulu 
209b65c0c07SDavid C Somayajulu //	if (!ha->enable_error_recovery)
2107fb51846SDavid C Somayajulu //		device_printf(ha->pci_dev, "%s: %s\n", __func__, str);
2117fb51846SDavid C Somayajulu 
2127fb51846SDavid C Somayajulu 	return;
213f10a77bbSDavid C Somayajulu }
214f10a77bbSDavid C Somayajulu 
215f10a77bbSDavid C Somayajulu #endif /* #ifndef _QL_INLINE_H_ */
216