xref: /freebsd/sys/dev/qlxgb/qla_inline.h (revision 088fc97186dc243619d00071b352f3f516df747d)
10bc7cf6fSBjoern A. Zeeb /*
2*088fc971SDavid C Somayajulu  * Copyright (c) 2011-2013 Qlogic Corporation
30bc7cf6fSBjoern A. Zeeb  * All rights reserved.
40bc7cf6fSBjoern A. Zeeb  *
50bc7cf6fSBjoern A. Zeeb  *  Redistribution and use in source and binary forms, with or without
60bc7cf6fSBjoern A. Zeeb  *  modification, are permitted provided that the following conditions
70bc7cf6fSBjoern A. Zeeb  *  are met:
80bc7cf6fSBjoern A. Zeeb  *
90bc7cf6fSBjoern A. Zeeb  *  1. Redistributions of source code must retain the above copyright
100bc7cf6fSBjoern A. Zeeb  *     notice, this list of conditions and the following disclaimer.
110bc7cf6fSBjoern A. Zeeb  *  2. Redistributions in binary form must reproduce the above copyright
120bc7cf6fSBjoern A. Zeeb  *     notice, this list of conditions and the following disclaimer in the
130bc7cf6fSBjoern A. Zeeb  *     documentation and/or other materials provided with the distribution.
140bc7cf6fSBjoern A. Zeeb  *
150bc7cf6fSBjoern A. Zeeb  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
160bc7cf6fSBjoern A. Zeeb  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170bc7cf6fSBjoern A. Zeeb  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180bc7cf6fSBjoern A. Zeeb  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
190bc7cf6fSBjoern A. Zeeb  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
200bc7cf6fSBjoern A. Zeeb  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
210bc7cf6fSBjoern A. Zeeb  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
220bc7cf6fSBjoern A. Zeeb  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
230bc7cf6fSBjoern A. Zeeb  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
240bc7cf6fSBjoern A. Zeeb  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
250bc7cf6fSBjoern A. Zeeb  *  POSSIBILITY OF SUCH DAMAGE.
260bc7cf6fSBjoern A. Zeeb  *
270bc7cf6fSBjoern A. Zeeb  * $FreeBSD$
280bc7cf6fSBjoern A. Zeeb  */
290bc7cf6fSBjoern A. Zeeb /*
300bc7cf6fSBjoern A. Zeeb  * File: qla_inline.h
310bc7cf6fSBjoern A. Zeeb  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
320bc7cf6fSBjoern A. Zeeb  */
330bc7cf6fSBjoern A. Zeeb #ifndef _QLA_INLINE_H_
340bc7cf6fSBjoern A. Zeeb #define _QLA_INLINE_H_
350bc7cf6fSBjoern A. Zeeb 
360bc7cf6fSBjoern A. Zeeb /*
370bc7cf6fSBjoern A. Zeeb  * Function: qla_hw_reset
380bc7cf6fSBjoern A. Zeeb  */
390bc7cf6fSBjoern A. Zeeb static __inline void qla_hw_reset(qla_host_t *ha)
400bc7cf6fSBjoern A. Zeeb {
410bc7cf6fSBjoern A. Zeeb         WRITE_OFFSET32(ha, Q8_ASIC_RESET, 0xFFFFFFFF);
420bc7cf6fSBjoern A. Zeeb }
430bc7cf6fSBjoern A. Zeeb 
440bc7cf6fSBjoern A. Zeeb #define QL8_SEMLOCK_TIMEOUT	1000/* QLA8020 Semaphore Lock Timeout 10ms */
450bc7cf6fSBjoern A. Zeeb 
460bc7cf6fSBjoern A. Zeeb 
470bc7cf6fSBjoern A. Zeeb /*
480bc7cf6fSBjoern A. Zeeb  * Inline functions for hardware semaphores
490bc7cf6fSBjoern A. Zeeb  */
500bc7cf6fSBjoern A. Zeeb 
510bc7cf6fSBjoern A. Zeeb /*
520bc7cf6fSBjoern A. Zeeb  * Name:	qla_sem_lock
530bc7cf6fSBjoern A. Zeeb  * Function:	Locks one of the semaphore registers (semaphore 2,3,5 & 7)
540bc7cf6fSBjoern A. Zeeb  *		If the id_reg is valid, then id_val is written into it.
550bc7cf6fSBjoern A. Zeeb  *		This is for debugging purpose
560bc7cf6fSBjoern A. Zeeb  * Returns:	0 on success; otherwise its failed.
570bc7cf6fSBjoern A. Zeeb  */
580bc7cf6fSBjoern A. Zeeb static __inline int
590bc7cf6fSBjoern A. Zeeb qla_sem_lock(qla_host_t *ha, uint32_t sem_reg, uint32_t id_reg, uint32_t id_val)
600bc7cf6fSBjoern A. Zeeb {
610bc7cf6fSBjoern A. Zeeb 	int count = QL8_SEMLOCK_TIMEOUT;
620bc7cf6fSBjoern A. Zeeb 
630bc7cf6fSBjoern A. Zeeb 	while (count) {
640bc7cf6fSBjoern A. Zeeb 		if ((READ_REG32(ha, sem_reg) & SEM_LOCK_BIT))
650bc7cf6fSBjoern A. Zeeb 			break;
660bc7cf6fSBjoern A. Zeeb 		count--;
670bc7cf6fSBjoern A. Zeeb 
680bc7cf6fSBjoern A. Zeeb 		if (!count)
690bc7cf6fSBjoern A. Zeeb 			return(-1);
700bc7cf6fSBjoern A. Zeeb 		qla_mdelay(__func__, 10);
710bc7cf6fSBjoern A. Zeeb 	}
720bc7cf6fSBjoern A. Zeeb 	if (id_reg)
730bc7cf6fSBjoern A. Zeeb 		WRITE_OFFSET32(ha, id_reg, id_val);
740bc7cf6fSBjoern A. Zeeb 
750bc7cf6fSBjoern A. Zeeb 	return(0);
760bc7cf6fSBjoern A. Zeeb }
770bc7cf6fSBjoern A. Zeeb 
780bc7cf6fSBjoern A. Zeeb /*
790bc7cf6fSBjoern A. Zeeb  * Name:	qla_sem_unlock
800bc7cf6fSBjoern A. Zeeb  * Function:	Unlocks the semaphore registers (semaphore 2,3,5 & 7)
810bc7cf6fSBjoern A. Zeeb  *		previously locked by qla_sem_lock()
820bc7cf6fSBjoern A. Zeeb  */
830bc7cf6fSBjoern A. Zeeb static __inline void
840bc7cf6fSBjoern A. Zeeb qla_sem_unlock(qla_host_t *ha, uint32_t sem_reg)
850bc7cf6fSBjoern A. Zeeb {
860bc7cf6fSBjoern A. Zeeb 	READ_REG32(ha, sem_reg);
870bc7cf6fSBjoern A. Zeeb }
880bc7cf6fSBjoern A. Zeeb 
890bc7cf6fSBjoern A. Zeeb static __inline int
900bc7cf6fSBjoern A. Zeeb qla_get_ifq_snd_maxlen(qla_host_t *ha)
910bc7cf6fSBjoern A. Zeeb {
920bc7cf6fSBjoern A. Zeeb 	return((NUM_TX_DESCRIPTORS - 1));
930bc7cf6fSBjoern A. Zeeb }
940bc7cf6fSBjoern A. Zeeb 
950bc7cf6fSBjoern A. Zeeb static __inline uint32_t
960bc7cf6fSBjoern A. Zeeb qla_get_optics(qla_host_t *ha)
970bc7cf6fSBjoern A. Zeeb {
980bc7cf6fSBjoern A. Zeeb 	uint32_t link_speed;
990bc7cf6fSBjoern A. Zeeb 
1000bc7cf6fSBjoern A. Zeeb 	link_speed = READ_REG32(ha, Q8_LINK_SPEED_0);
1010bc7cf6fSBjoern A. Zeeb 	if (ha->pci_func == 0)
1020bc7cf6fSBjoern A. Zeeb 		link_speed = link_speed & 0xFF;
1030bc7cf6fSBjoern A. Zeeb 	else
1040bc7cf6fSBjoern A. Zeeb 		link_speed = (link_speed >> 8) & 0xFF;
1050bc7cf6fSBjoern A. Zeeb 
1060bc7cf6fSBjoern A. Zeeb 	switch (link_speed) {
1070bc7cf6fSBjoern A. Zeeb 	case 0x1:
1080bc7cf6fSBjoern A. Zeeb 		link_speed = IFM_100_FX;
1090bc7cf6fSBjoern A. Zeeb 		break;
1100bc7cf6fSBjoern A. Zeeb 
1110bc7cf6fSBjoern A. Zeeb 	case 0x10:
1120bc7cf6fSBjoern A. Zeeb 		link_speed = IFM_1000_SX;
1130bc7cf6fSBjoern A. Zeeb 		break;
1140bc7cf6fSBjoern A. Zeeb 
1150bc7cf6fSBjoern A. Zeeb 	default:
1160bc7cf6fSBjoern A. Zeeb 		link_speed = (IFM_10G_LR | IFM_10G_SR);
1170bc7cf6fSBjoern A. Zeeb 		break;
1180bc7cf6fSBjoern A. Zeeb 	}
1190bc7cf6fSBjoern A. Zeeb 
1200bc7cf6fSBjoern A. Zeeb 	return(link_speed);
1210bc7cf6fSBjoern A. Zeeb }
1220bc7cf6fSBjoern A. Zeeb 
1230bc7cf6fSBjoern A. Zeeb static __inline uint8_t *
1240bc7cf6fSBjoern A. Zeeb qla_get_mac_addr(qla_host_t *ha)
1250bc7cf6fSBjoern A. Zeeb {
1260bc7cf6fSBjoern A. Zeeb 	return (ha->hw.mac_addr);
1270bc7cf6fSBjoern A. Zeeb }
1280bc7cf6fSBjoern A. Zeeb 
1290bc7cf6fSBjoern A. Zeeb static __inline void
1300bc7cf6fSBjoern A. Zeeb qla_read_mac_addr(qla_host_t *ha)
1310bc7cf6fSBjoern A. Zeeb {
1320bc7cf6fSBjoern A. Zeeb 	uint32_t mac_crb_addr;
1330bc7cf6fSBjoern A. Zeeb 	uint32_t mac_lo;
1340bc7cf6fSBjoern A. Zeeb 	uint32_t mac_hi;
1350bc7cf6fSBjoern A. Zeeb 	uint8_t	*macp;
1360bc7cf6fSBjoern A. Zeeb 
1370bc7cf6fSBjoern A. Zeeb 	mac_crb_addr = Q8_CRB_MAC_BLOCK_START +
1380bc7cf6fSBjoern A. Zeeb 		(((ha->pci_func >> 1) * 3) << 2) + ((ha->pci_func & 0x01) << 2);
1390bc7cf6fSBjoern A. Zeeb 
1400bc7cf6fSBjoern A. Zeeb 	mac_lo = READ_REG32(ha, mac_crb_addr);
1410bc7cf6fSBjoern A. Zeeb 	mac_hi = READ_REG32(ha, (mac_crb_addr + 0x4));
1420bc7cf6fSBjoern A. Zeeb 
1430bc7cf6fSBjoern A. Zeeb 	if (ha->pci_func & 0x01) {
1440bc7cf6fSBjoern A. Zeeb 		mac_lo = mac_lo >> 16;
1450bc7cf6fSBjoern A. Zeeb 
1460bc7cf6fSBjoern A. Zeeb 		macp = (uint8_t *)&mac_lo;
1470bc7cf6fSBjoern A. Zeeb 
1480bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[5] = macp[0];
1490bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[4] = macp[1];
1500bc7cf6fSBjoern A. Zeeb 
1510bc7cf6fSBjoern A. Zeeb 		macp = (uint8_t *)&mac_hi;
1520bc7cf6fSBjoern A. Zeeb 
1530bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[3] = macp[0];
1540bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[2] = macp[1];
1550bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[1] = macp[2];
1560bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[0] = macp[3];
1570bc7cf6fSBjoern A. Zeeb 	} else {
1580bc7cf6fSBjoern A. Zeeb 		macp = (uint8_t *)&mac_lo;
1590bc7cf6fSBjoern A. Zeeb 
1600bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[5] = macp[0];
1610bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[4] = macp[1];
1620bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[3] = macp[2];
1630bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[2] = macp[3];
1640bc7cf6fSBjoern A. Zeeb 
1650bc7cf6fSBjoern A. Zeeb 		macp = (uint8_t *)&mac_hi;
1660bc7cf6fSBjoern A. Zeeb 
1670bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[1] = macp[0];
1680bc7cf6fSBjoern A. Zeeb 		ha->hw.mac_addr[0] = macp[1];
1690bc7cf6fSBjoern A. Zeeb 	}
1700bc7cf6fSBjoern A. Zeeb 	return;
1710bc7cf6fSBjoern A. Zeeb }
1720bc7cf6fSBjoern A. Zeeb 
1730bc7cf6fSBjoern A. Zeeb static __inline void
1740bc7cf6fSBjoern A. Zeeb qla_set_hw_rcv_desc(qla_host_t *ha, uint32_t ridx, uint32_t index,
1750bc7cf6fSBjoern A. Zeeb 	uint32_t handle, bus_addr_t paddr, uint32_t buf_size)
1760bc7cf6fSBjoern A. Zeeb {
1770bc7cf6fSBjoern A. Zeeb 	q80_recv_desc_t *rcv_desc;
1780bc7cf6fSBjoern A. Zeeb 
1790bc7cf6fSBjoern A. Zeeb 	rcv_desc = (q80_recv_desc_t *)ha->hw.dma_buf.rds_ring[ridx].dma_b;
1800bc7cf6fSBjoern A. Zeeb 
1810bc7cf6fSBjoern A. Zeeb 	rcv_desc += index;
1820bc7cf6fSBjoern A. Zeeb 
1830bc7cf6fSBjoern A. Zeeb 	rcv_desc->handle = (uint16_t)handle;
1840bc7cf6fSBjoern A. Zeeb 	rcv_desc->buf_size = buf_size;
1850bc7cf6fSBjoern A. Zeeb 	rcv_desc->buf_addr = paddr;
1860bc7cf6fSBjoern A. Zeeb 
1870bc7cf6fSBjoern A. Zeeb 	return;
1880bc7cf6fSBjoern A. Zeeb }
1890bc7cf6fSBjoern A. Zeeb 
1900bc7cf6fSBjoern A. Zeeb static __inline void
1910bc7cf6fSBjoern A. Zeeb qla_init_hw_rcv_descriptors(qla_host_t *ha, uint32_t ridx)
1920bc7cf6fSBjoern A. Zeeb {
1930bc7cf6fSBjoern A. Zeeb 	if (ridx == RDS_RING_INDEX_NORMAL)
1940bc7cf6fSBjoern A. Zeeb 		bzero((void *)ha->hw.dma_buf.rds_ring[ridx].dma_b,
1950bc7cf6fSBjoern A. Zeeb 			(sizeof(q80_recv_desc_t) * NUM_RX_DESCRIPTORS));
1960bc7cf6fSBjoern A. Zeeb 	else if (ridx == RDS_RING_INDEX_JUMBO)
1970bc7cf6fSBjoern A. Zeeb 		bzero((void *)ha->hw.dma_buf.rds_ring[ridx].dma_b,
1980bc7cf6fSBjoern A. Zeeb 			(sizeof(q80_recv_desc_t) * NUM_RX_JUMBO_DESCRIPTORS));
1990bc7cf6fSBjoern A. Zeeb 	else
2000bc7cf6fSBjoern A. Zeeb 		QL_ASSERT(0, ("%s: invalid rds index [%d]\n", __func__, ridx));
2010bc7cf6fSBjoern A. Zeeb }
2020bc7cf6fSBjoern A. Zeeb 
2030bc7cf6fSBjoern A. Zeeb static __inline void
2040bc7cf6fSBjoern A. Zeeb qla_lock(qla_host_t *ha, const char *str)
2050bc7cf6fSBjoern A. Zeeb {
2060bc7cf6fSBjoern A. Zeeb 	while (1) {
2070bc7cf6fSBjoern A. Zeeb 		mtx_lock(&ha->hw_lock);
2080bc7cf6fSBjoern A. Zeeb 		if (!ha->hw_lock_held) {
2090bc7cf6fSBjoern A. Zeeb 			ha->hw_lock_held = 1;
2100bc7cf6fSBjoern A. Zeeb 			ha->qla_lock = str;
2110bc7cf6fSBjoern A. Zeeb 			mtx_unlock(&ha->hw_lock);
2120bc7cf6fSBjoern A. Zeeb 			break;
2130bc7cf6fSBjoern A. Zeeb 		}
2140bc7cf6fSBjoern A. Zeeb 		mtx_unlock(&ha->hw_lock);
2150bc7cf6fSBjoern A. Zeeb 		qla_mdelay(__func__, 1);
2160bc7cf6fSBjoern A. Zeeb 	}
2170bc7cf6fSBjoern A. Zeeb 	return;
2180bc7cf6fSBjoern A. Zeeb }
2190bc7cf6fSBjoern A. Zeeb 
2200bc7cf6fSBjoern A. Zeeb static __inline void
2210bc7cf6fSBjoern A. Zeeb qla_unlock(qla_host_t *ha, const char *str)
2220bc7cf6fSBjoern A. Zeeb {
2230bc7cf6fSBjoern A. Zeeb 	mtx_lock(&ha->hw_lock);
2240bc7cf6fSBjoern A. Zeeb 	ha->hw_lock_held = 0;
2250bc7cf6fSBjoern A. Zeeb 	ha->qla_unlock = str;
2260bc7cf6fSBjoern A. Zeeb 	mtx_unlock(&ha->hw_lock);
2270bc7cf6fSBjoern A. Zeeb }
2280bc7cf6fSBjoern A. Zeeb 
2290bc7cf6fSBjoern A. Zeeb #endif /* #ifndef _QLA_INLINE_H_ */
230