1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21a2255df3SDaniel Beauregard 
22a2255df3SDaniel Beauregard /*
23a2255df3SDaniel Beauregard  * Copyright 2009 QLogic Corporation.  All rights reserved.
24a2255df3SDaniel Beauregard  * Use is subject to license terms.
25a2255df3SDaniel Beauregard  */
26a2255df3SDaniel Beauregard 
27fcf3ce44SJohn Forte /*
283fb517f7SJames Moore  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
29fcf3ce44SJohn Forte  */
30a2255df3SDaniel Beauregard 
31fcf3ce44SJohn Forte #ifndef	_QLT_DMA_H
32fcf3ce44SJohn Forte #define	_QLT_DMA_H
33fcf3ce44SJohn Forte 
34*4558d122SViswanathan Kannappan #include <sys/stmf.h>
35fcf3ce44SJohn Forte 
36fcf3ce44SJohn Forte #ifdef	__cplusplus
37fcf3ce44SJohn Forte extern "C" {
38fcf3ce44SJohn Forte #endif
39fcf3ce44SJohn Forte 
403fb517f7SJames Moore /*
413fb517f7SJames Moore  * DMA memory object.
423fb517f7SJames Moore  */
433fb517f7SJames Moore #define	QLT_DMA_SG_LIST_LENGTH	1270
443fb517f7SJames Moore #define	CMD7_2400_DATA_SEGMENTS	1
453fb517f7SJames Moore #define	CONT_A64_DATA_SEGMENTS	5
463fb517f7SJames Moore 
473fb517f7SJames Moore 
483fb517f7SJames Moore /*
493fb517f7SJames Moore  * Container for ddi_dma_handle
503fb517f7SJames Moore  *
513fb517f7SJames Moore  * These elements are either linked to an active dbuf or in the free list.
523fb517f7SJames Moore  */
533fb517f7SJames Moore struct qlt_dma_handle {
543fb517f7SJames Moore 	struct qlt_dma_handle	*next;
553fb517f7SJames Moore 	ddi_dma_handle_t	dma_handle;
563fb517f7SJames Moore 	ddi_dma_cookie_t	first_cookie;
573fb517f7SJames Moore 	uint_t			num_cookies;
583fb517f7SJames Moore 	uint_t			num_cookies_fetched;
593fb517f7SJames Moore };
603fb517f7SJames Moore 
613fb517f7SJames Moore typedef struct qlt_dma_handle qlt_dma_handle_t;
623fb517f7SJames Moore 
633fb517f7SJames Moore /*
643fb517f7SJames Moore  * The dbuf private data when using a scatter/gather list.
653fb517f7SJames Moore  */
663fb517f7SJames Moore struct qlt_dma_sgl {
673fb517f7SJames Moore 	uint16_t		handle_count;
683fb517f7SJames Moore 	uint16_t		cookie_count;
693fb517f7SJames Moore 	uint16_t		cookie_next_fetch;
703fb517f7SJames Moore 	uint16_t		cookie_prefetched;
713fb517f7SJames Moore 	qlt_dma_handle_t	*handle_list;
723fb517f7SJames Moore 	qlt_dma_handle_t	*handle_next_fetch;
733fb517f7SJames Moore 	size_t			qsize;
743fb517f7SJames Moore 	ddi_dma_cookie_t	cookie[1];
753fb517f7SJames Moore };
763fb517f7SJames Moore 
773fb517f7SJames Moore typedef struct qlt_dma_sgl qlt_dma_sgl_t;
783fb517f7SJames Moore 
793fb517f7SJames Moore /*
803fb517f7SJames Moore  * Structure to maintain ddi_dma_handle free pool.
813fb517f7SJames Moore  */
823fb517f7SJames Moore struct qlt_dma_handle_pool {
833fb517f7SJames Moore 	kmutex_t		pool_lock;	/* protects all fields */
843fb517f7SJames Moore 	qlt_dma_handle_t	*free_list;
853fb517f7SJames Moore 	int			num_free;
863fb517f7SJames Moore 	int			num_total;
873fb517f7SJames Moore };
883fb517f7SJames Moore 
893fb517f7SJames Moore typedef struct qlt_dma_handle_pool qlt_dma_handle_pool_t;
903fb517f7SJames Moore 
91fcf3ce44SJohn Forte struct qlt_dmem_bucket;
92fcf3ce44SJohn Forte 
93fcf3ce44SJohn Forte typedef struct qlt_dmem_bctl {
94fcf3ce44SJohn Forte 	struct qlt_dmem_bucket	*bctl_bucket;
95fcf3ce44SJohn Forte 	struct qlt_dmem_bctl	*bctl_next;
96fcf3ce44SJohn Forte 	uint64_t		bctl_dev_addr;
973fb517f7SJames Moore 	uint8_t			bctl_task_ndx;	/* notused */
98fcf3ce44SJohn Forte 	stmf_data_buf_t		*bctl_buf;
99fcf3ce44SJohn Forte } qlt_dmem_bctl_t;
100fcf3ce44SJohn Forte 
101fcf3ce44SJohn Forte typedef struct qlt_dmem_bucket {
102fcf3ce44SJohn Forte 	uint32_t		dmem_buf_size;
103fcf3ce44SJohn Forte 	uint32_t		dmem_nbufs;
104fcf3ce44SJohn Forte 	uint32_t		dmem_nbufs_free;
105fcf3ce44SJohn Forte 	uint8_t			*dmem_host_addr;
106fcf3ce44SJohn Forte 	uint64_t		dmem_dev_addr;
107fcf3ce44SJohn Forte 	ddi_dma_handle_t	dmem_dma_handle;
108fcf3ce44SJohn Forte 	ddi_acc_handle_t	dmem_acc_handle;
109fcf3ce44SJohn Forte 	kmutex_t		dmem_lock;
110fcf3ce44SJohn Forte 	qlt_dmem_bctl_t		*dmem_bctl_free_list;
111fcf3ce44SJohn Forte 	void			*dmem_bctls_mem;
112fcf3ce44SJohn Forte } qlt_dmem_bucket_t;
113fcf3ce44SJohn Forte 
114fcf3ce44SJohn Forte fct_status_t qlt_dmem_init(qlt_state_t *qlt);
115fcf3ce44SJohn Forte void qlt_dmem_fini(qlt_state_t *qlt);
1163fb517f7SJames Moore void qlt_dma_handle_pool_init(qlt_state_t *qlt);
1173fb517f7SJames Moore void qlt_dma_handle_pool_fini(qlt_state_t *qlt);
118fcf3ce44SJohn Forte stmf_data_buf_t *qlt_dmem_alloc(fct_local_port_t *port, uint32_t size,
119fcf3ce44SJohn Forte     uint32_t *pminsize, uint32_t flags);
120fcf3ce44SJohn Forte stmf_data_buf_t *qlt_i_dmem_alloc(qlt_state_t *qlt, uint32_t size,
121a2255df3SDaniel Beauregard     uint32_t *pminsize, uint32_t flags);
122fcf3ce44SJohn Forte void qlt_dmem_free(fct_dbuf_store_t *fds, stmf_data_buf_t *dbuf);
123fcf3ce44SJohn Forte void qlt_i_dmem_free(qlt_state_t *qlt, stmf_data_buf_t *dbuf);
1243fb517f7SJames Moore stmf_status_t qlt_dma_setup_dbuf(fct_local_port_t *port,
1253fb517f7SJames Moore     stmf_data_buf_t *dbuf, uint32_t flags);
1263fb517f7SJames Moore void qlt_dma_teardown_dbuf(fct_dbuf_store_t *fds, stmf_data_buf_t *dbuf);
127fcf3ce44SJohn Forte void qlt_dmem_dma_sync(stmf_data_buf_t *dbuf, uint_t sync_type);
1283fb517f7SJames Moore uint8_t qlt_get_iocb_count(uint32_t cookie_cnt);
1293fb517f7SJames Moore uint64_t qlt_ddi_vtop(caddr_t vaddr);
1303fb517f7SJames Moore /*
1313fb517f7SJames Moore  * XXX move the following into the fct layer
1323fb517f7SJames Moore  */
1333fb517f7SJames Moore uint16_t qlt_get_cookie_count(stmf_data_buf_t *dbuf);
1343fb517f7SJames Moore void qlt_ddi_dma_nextcookie(stmf_data_buf_t *dbuf, ddi_dma_cookie_t *cookie_p);
1353fb517f7SJames Moore ddi_dma_cookie_t *qlt_get_cookie_array(stmf_data_buf_t *dbuf);
1363fb517f7SJames Moore 
137fcf3ce44SJohn Forte 
138fcf3ce44SJohn Forte #ifdef	__cplusplus
139fcf3ce44SJohn Forte }
140fcf3ce44SJohn Forte #endif
141fcf3ce44SJohn Forte 
142fcf3ce44SJohn Forte #endif /* _QLT_DMA_H */
143