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 /*
30f10a77bbSDavid C Somayajulu * File: ql_inline.h
31f10a77bbSDavid C Somayajulu * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
32f10a77bbSDavid C Somayajulu */
33f10a77bbSDavid C Somayajulu #ifndef _QL_INLINE_H_
34f10a77bbSDavid C Somayajulu #define _QL_INLINE_H_
35f10a77bbSDavid C Somayajulu
36f10a77bbSDavid C Somayajulu #define QL8_SEMLOCK_TIMEOUT 1000/* QLA8020 Semaphore Lock Timeout 10ms */
37f10a77bbSDavid C Somayajulu
38f10a77bbSDavid C Somayajulu /*
39f10a77bbSDavid C Somayajulu * Inline functions for hardware semaphores
40f10a77bbSDavid C Somayajulu */
41f10a77bbSDavid C Somayajulu
42f10a77bbSDavid C Somayajulu /*
43f10a77bbSDavid C Somayajulu * Name: qla_sem_lock
44f10a77bbSDavid C Somayajulu * Function: Locks one of the semaphore registers (semaphore 2,3,5 & 7)
45f10a77bbSDavid C Somayajulu * If the id_reg is valid, then id_val is written into it.
46f10a77bbSDavid C Somayajulu * This is for debugging purpose
47f10a77bbSDavid C Somayajulu * Returns: 0 on success; otherwise its failed.
48f10a77bbSDavid C Somayajulu */
49f10a77bbSDavid C Somayajulu static __inline int
qla_sem_lock(qla_host_t * ha,uint32_t sem_reg,uint32_t id_reg,uint32_t id_val)50f10a77bbSDavid C Somayajulu qla_sem_lock(qla_host_t *ha, uint32_t sem_reg, uint32_t id_reg, uint32_t id_val)
51f10a77bbSDavid C Somayajulu {
52f10a77bbSDavid C Somayajulu int count = QL8_SEMLOCK_TIMEOUT;
53f10a77bbSDavid C Somayajulu
54f10a77bbSDavid C Somayajulu while (count) {
55f10a77bbSDavid C Somayajulu if ((READ_REG32(ha, sem_reg) & BIT_0))
56f10a77bbSDavid C Somayajulu break;
57f10a77bbSDavid C Somayajulu count--;
58f10a77bbSDavid C Somayajulu
59f10a77bbSDavid C Somayajulu if (!count)
60f10a77bbSDavid C Somayajulu return(-1);
61f10a77bbSDavid C Somayajulu qla_mdelay(__func__, 10);
62f10a77bbSDavid C Somayajulu }
63f10a77bbSDavid C Somayajulu if (id_reg)
64f10a77bbSDavid C Somayajulu WRITE_REG32(ha, id_reg, id_val);
65f10a77bbSDavid C Somayajulu
66f10a77bbSDavid C Somayajulu return(0);
67f10a77bbSDavid C Somayajulu }
68f10a77bbSDavid C Somayajulu
69f10a77bbSDavid C Somayajulu /*
70f10a77bbSDavid C Somayajulu * Name: qla_sem_unlock
71f10a77bbSDavid C Somayajulu * Function: Unlocks the semaphore registers (semaphore 2,3,5 & 7)
72f10a77bbSDavid C Somayajulu * previously locked by qla_sem_lock()
73f10a77bbSDavid C Somayajulu */
74f10a77bbSDavid C Somayajulu static __inline void
qla_sem_unlock(qla_host_t * ha,uint32_t sem_reg)75f10a77bbSDavid C Somayajulu qla_sem_unlock(qla_host_t *ha, uint32_t sem_reg)
76f10a77bbSDavid C Somayajulu {
77f10a77bbSDavid C Somayajulu READ_REG32(ha, sem_reg);
78f10a77bbSDavid C Somayajulu }
79f10a77bbSDavid C Somayajulu
80f10a77bbSDavid C Somayajulu static __inline int
qla_get_ifq_snd_maxlen(qla_host_t * ha)81f10a77bbSDavid C Somayajulu qla_get_ifq_snd_maxlen(qla_host_t *ha)
82f10a77bbSDavid C Somayajulu {
83f10a77bbSDavid C Somayajulu return(((NUM_TX_DESCRIPTORS * 4) - 1));
84f10a77bbSDavid C Somayajulu }
85f10a77bbSDavid C Somayajulu
86f10a77bbSDavid C Somayajulu static __inline uint32_t
qla_get_optics(qla_host_t * ha)87f10a77bbSDavid C Somayajulu qla_get_optics(qla_host_t *ha)
88f10a77bbSDavid C Somayajulu {
89f10a77bbSDavid C Somayajulu uint32_t link_speed;
90f10a77bbSDavid C Somayajulu
91f10a77bbSDavid C Somayajulu link_speed = READ_REG32(ha, Q8_LINK_SPEED_0);
92f10a77bbSDavid C Somayajulu if (ha->pci_func == 0)
93f10a77bbSDavid C Somayajulu link_speed = link_speed & 0xFF;
94f10a77bbSDavid C Somayajulu else
95f10a77bbSDavid C Somayajulu link_speed = (link_speed >> 8) & 0xFF;
96f10a77bbSDavid C Somayajulu
97f10a77bbSDavid C Somayajulu switch (link_speed) {
98f10a77bbSDavid C Somayajulu case 0x1:
99f10a77bbSDavid C Somayajulu link_speed = IFM_100_FX;
100f10a77bbSDavid C Somayajulu break;
101f10a77bbSDavid C Somayajulu
102f10a77bbSDavid C Somayajulu case 0x10:
103f10a77bbSDavid C Somayajulu link_speed = IFM_1000_SX;
104f10a77bbSDavid C Somayajulu break;
105f10a77bbSDavid C Somayajulu
106f10a77bbSDavid C Somayajulu default:
107f10a77bbSDavid C Somayajulu if ((ha->hw.module_type == 0x4) ||
108f10a77bbSDavid C Somayajulu (ha->hw.module_type == 0x5) ||
109f10a77bbSDavid C Somayajulu (ha->hw.module_type == 0x6))
110f10a77bbSDavid C Somayajulu link_speed = (IFM_10G_TWINAX);
111f10a77bbSDavid C Somayajulu else
112f10a77bbSDavid C Somayajulu link_speed = (IFM_10G_LR | IFM_10G_SR);
113f10a77bbSDavid C Somayajulu break;
114f10a77bbSDavid C Somayajulu }
115f10a77bbSDavid C Somayajulu
116f10a77bbSDavid C Somayajulu return(link_speed);
117f10a77bbSDavid C Somayajulu }
118f10a77bbSDavid C Somayajulu
119f10a77bbSDavid C Somayajulu static __inline uint8_t *
qla_get_mac_addr(qla_host_t * ha)120f10a77bbSDavid C Somayajulu qla_get_mac_addr(qla_host_t *ha)
121f10a77bbSDavid C Somayajulu {
122f10a77bbSDavid C Somayajulu return (ha->hw.mac_addr);
123f10a77bbSDavid C Somayajulu }
124f10a77bbSDavid C Somayajulu
125f10a77bbSDavid C Somayajulu static __inline void
qla_set_hw_rcv_desc(qla_host_t * ha,uint32_t r_idx,uint32_t index,uint32_t handle,bus_addr_t paddr,uint32_t buf_size)126f10a77bbSDavid C Somayajulu qla_set_hw_rcv_desc(qla_host_t *ha, uint32_t r_idx, uint32_t index,
127f10a77bbSDavid C Somayajulu uint32_t handle, bus_addr_t paddr, uint32_t buf_size)
128f10a77bbSDavid C Somayajulu {
129f10a77bbSDavid C Somayajulu volatile q80_recv_desc_t *rcv_desc;
130f10a77bbSDavid C Somayajulu
131f10a77bbSDavid C Somayajulu rcv_desc = (q80_recv_desc_t *)ha->hw.dma_buf.rds_ring[r_idx].dma_b;
132f10a77bbSDavid C Somayajulu
133f10a77bbSDavid C Somayajulu rcv_desc += index;
134f10a77bbSDavid C Somayajulu
135f10a77bbSDavid C Somayajulu rcv_desc->handle = (uint16_t)handle;
136f10a77bbSDavid C Somayajulu rcv_desc->buf_size = buf_size;
137f10a77bbSDavid C Somayajulu rcv_desc->buf_addr = paddr;
138f10a77bbSDavid C Somayajulu
139f10a77bbSDavid C Somayajulu return;
140f10a77bbSDavid C Somayajulu }
141f10a77bbSDavid C Somayajulu
142f10a77bbSDavid C Somayajulu static __inline void
qla_init_hw_rcv_descriptors(qla_host_t * ha)143f10a77bbSDavid C Somayajulu qla_init_hw_rcv_descriptors(qla_host_t *ha)
144f10a77bbSDavid C Somayajulu {
145f10a77bbSDavid C Somayajulu int i;
146f10a77bbSDavid C Somayajulu
147f10a77bbSDavid C Somayajulu for (i = 0; i < ha->hw.num_rds_rings; i++)
148f10a77bbSDavid C Somayajulu bzero((void *)ha->hw.dma_buf.rds_ring[i].dma_b,
149f10a77bbSDavid C Somayajulu (sizeof(q80_recv_desc_t) * NUM_RX_DESCRIPTORS));
150f10a77bbSDavid C Somayajulu }
151f10a77bbSDavid C Somayajulu
1527fb51846SDavid C Somayajulu #define QLA_LOCK_DEFAULT_MS_TIMEOUT 3000
1537fb51846SDavid C Somayajulu
1547fb51846SDavid C Somayajulu #ifndef QLA_LOCK_NO_SLEEP
1557fb51846SDavid C Somayajulu #define QLA_LOCK_NO_SLEEP 0
1567fb51846SDavid C Somayajulu #endif
1577fb51846SDavid C Somayajulu
158f10a77bbSDavid C Somayajulu static __inline int
qla_lock(qla_host_t * ha,const char * str,uint32_t timeout_ms,uint32_t no_sleep)1597fb51846SDavid C Somayajulu qla_lock(qla_host_t *ha, const char *str, uint32_t timeout_ms,
1607fb51846SDavid C Somayajulu uint32_t no_sleep)
161f10a77bbSDavid C Somayajulu {
162f10a77bbSDavid C Somayajulu int ret = -1;
163f10a77bbSDavid C Somayajulu
164f10a77bbSDavid C Somayajulu while (1) {
165f10a77bbSDavid C Somayajulu mtx_lock(&ha->hw_lock);
1667fb51846SDavid C Somayajulu
167b65c0c07SDavid C Somayajulu if (ha->qla_detach_active || ha->offline) {
1687fb51846SDavid C Somayajulu mtx_unlock(&ha->hw_lock);
1697fb51846SDavid C Somayajulu break;
1707fb51846SDavid C Somayajulu }
1717fb51846SDavid C Somayajulu
172f10a77bbSDavid C Somayajulu if (!ha->hw_lock_held) {
173f10a77bbSDavid C Somayajulu ha->hw_lock_held = 1;
174f10a77bbSDavid C Somayajulu ha->qla_lock = str;
175f10a77bbSDavid C Somayajulu ret = 0;
176f10a77bbSDavid C Somayajulu mtx_unlock(&ha->hw_lock);
177f10a77bbSDavid C Somayajulu break;
178f10a77bbSDavid C Somayajulu }
179f10a77bbSDavid C Somayajulu mtx_unlock(&ha->hw_lock);
180f10a77bbSDavid C Somayajulu
1817fb51846SDavid C Somayajulu if (--timeout_ms == 0) {
1827fb51846SDavid C Somayajulu ha->hw_lock_failed++;
183f10a77bbSDavid C Somayajulu break;
1847fb51846SDavid C Somayajulu } else {
1857fb51846SDavid C Somayajulu if (no_sleep)
1867fb51846SDavid C Somayajulu DELAY(1000);
187f10a77bbSDavid C Somayajulu else
188f10a77bbSDavid C Somayajulu qla_mdelay(__func__, 1);
189f10a77bbSDavid C Somayajulu }
1907fb51846SDavid C Somayajulu }
1917fb51846SDavid C Somayajulu
192b65c0c07SDavid C Somayajulu // if (!ha->enable_error_recovery)
193b65c0c07SDavid C Somayajulu // device_printf(ha->pci_dev, "%s: %s ret = %d\n", __func__,
194b65c0c07SDavid C Somayajulu // str,ret);
195b65c0c07SDavid C Somayajulu
196f10a77bbSDavid C Somayajulu return (ret);
197f10a77bbSDavid C Somayajulu }
198f10a77bbSDavid C Somayajulu
199f10a77bbSDavid C Somayajulu static __inline void
qla_unlock(qla_host_t * ha,const char * str)200f10a77bbSDavid C Somayajulu qla_unlock(qla_host_t *ha, const char *str)
201f10a77bbSDavid C Somayajulu {
202f10a77bbSDavid C Somayajulu mtx_lock(&ha->hw_lock);
203f10a77bbSDavid C Somayajulu ha->hw_lock_held = 0;
204f10a77bbSDavid C Somayajulu ha->qla_unlock = str;
205f10a77bbSDavid C Somayajulu mtx_unlock(&ha->hw_lock);
206b65c0c07SDavid C Somayajulu
207b65c0c07SDavid C Somayajulu // if (!ha->enable_error_recovery)
2087fb51846SDavid C Somayajulu // device_printf(ha->pci_dev, "%s: %s\n", __func__, str);
2097fb51846SDavid C Somayajulu
2107fb51846SDavid C Somayajulu return;
211f10a77bbSDavid C Somayajulu }
212f10a77bbSDavid C Somayajulu
213f10a77bbSDavid C Somayajulu #endif /* #ifndef _QL_INLINE_H_ */
214