xref: /openbmc/linux/drivers/scsi/be2iscsi/be.h (revision bfead3b2)
1 /**
2  * Copyright (C) 2005 - 2009 ServerEngines
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Contact Information:
11  * linux-drivers@serverengines.com
12  *
13  * ServerEngines
14  * 209 N. Fair Oaks Ave
15  * Sunnyvale, CA 94085
16  */
17 
18 #ifndef BEISCSI_H
19 #define BEISCSI_H
20 
21 #include <linux/pci.h>
22 #include <linux/if_vlan.h>
23 #include <linux/blk-iopoll.h>
24 #define FW_VER_LEN	32
25 #define MCC_Q_LEN	128
26 #define MCC_CQ_LEN	256
27 
28 struct be_dma_mem {
29 	void *va;
30 	dma_addr_t dma;
31 	u32 size;
32 };
33 
34 struct be_queue_info {
35 	struct be_dma_mem dma_mem;
36 	u16 len;
37 	u16 entry_size;		/* Size of an element in the queue */
38 	u16 id;
39 	u16 tail, head;
40 	bool created;
41 	atomic_t used;		/* Number of valid elements in the queue */
42 };
43 
44 static inline u32 MODULO(u16 val, u16 limit)
45 {
46 	WARN_ON(limit & (limit - 1));
47 	return val & (limit - 1);
48 }
49 
50 static inline void index_inc(u16 *index, u16 limit)
51 {
52 	*index = MODULO((*index + 1), limit);
53 }
54 
55 static inline void *queue_head_node(struct be_queue_info *q)
56 {
57 	return q->dma_mem.va + q->head * q->entry_size;
58 }
59 
60 static inline void *queue_tail_node(struct be_queue_info *q)
61 {
62 	return q->dma_mem.va + q->tail * q->entry_size;
63 }
64 
65 static inline void queue_head_inc(struct be_queue_info *q)
66 {
67 	index_inc(&q->head, q->len);
68 }
69 
70 static inline void queue_tail_inc(struct be_queue_info *q)
71 {
72 	index_inc(&q->tail, q->len);
73 }
74 
75 /*ISCSI */
76 
77 struct be_eq_obj {
78 	struct be_queue_info q;
79 	struct beiscsi_hba *phba;
80 	struct be_queue_info *cq;
81 	struct blk_iopoll	iopoll;
82 };
83 
84 struct be_mcc_obj {
85 	struct be_queue_info q;
86 	struct be_queue_info cq;
87 };
88 
89 struct be_ctrl_info {
90 	u8 __iomem *csr;
91 	u8 __iomem *db;		/* Door Bell */
92 	u8 __iomem *pcicfg;	/* PCI config space */
93 	struct pci_dev *pdev;
94 
95 	/* Mbox used for cmd request/response */
96 	spinlock_t mbox_lock;	/* For serializing mbox cmds to BE card */
97 	struct be_dma_mem mbox_mem;
98 	/* Mbox mem is adjusted to align to 16 bytes. The allocated addr
99 	 * is stored for freeing purpose */
100 	struct be_dma_mem mbox_mem_alloced;
101 
102 	/* MCC Rings */
103 	struct be_mcc_obj mcc_obj;
104 	spinlock_t mcc_lock;	/* For serializing mcc cmds to BE card */
105 	spinlock_t mcc_cq_lock;
106 
107 	/* MCC Async callback */
108 	void (*async_cb) (void *adapter, bool link_up);
109 	void *adapter_ctxt;
110 };
111 
112 #include "be_cmds.h"
113 
114 #define PAGE_SHIFT_4K 12
115 #define PAGE_SIZE_4K (1 << PAGE_SHIFT_4K)
116 
117 /* Returns number of pages spanned by the data starting at the given addr */
118 #define PAGES_4K_SPANNED(_address, size) 				\
119 		((u32)((((size_t)(_address) & (PAGE_SIZE_4K - 1)) + 	\
120 			(size) + (PAGE_SIZE_4K - 1)) >> PAGE_SHIFT_4K))
121 
122 /* Byte offset into the page corresponding to given address */
123 #define OFFSET_IN_PAGE(addr)						\
124 		((size_t)(addr) & (PAGE_SIZE_4K-1))
125 
126 /* Returns bit offset within a DWORD of a bitfield */
127 #define AMAP_BIT_OFFSET(_struct, field)  				\
128 		(((size_t)&(((_struct *)0)->field))%32)
129 
130 /* Returns the bit mask of the field that is NOT shifted into location. */
131 static inline u32 amap_mask(u32 bitsize)
132 {
133 	return (bitsize == 32 ? 0xFFFFFFFF : (1 << bitsize) - 1);
134 }
135 
136 static inline void amap_set(void *ptr, u32 dw_offset, u32 mask,
137 					u32 offset, u32 value)
138 {
139 	u32 *dw = (u32 *) ptr + dw_offset;
140 	*dw &= ~(mask << offset);
141 	*dw |= (mask & value) << offset;
142 }
143 
144 #define AMAP_SET_BITS(_struct, field, ptr, val)				\
145 		amap_set(ptr,						\
146 			offsetof(_struct, field)/32,			\
147 			amap_mask(sizeof(((_struct *)0)->field)),	\
148 			AMAP_BIT_OFFSET(_struct, field),		\
149 			val)
150 
151 static inline u32 amap_get(void *ptr, u32 dw_offset, u32 mask, u32 offset)
152 {
153 	u32 *dw = ptr;
154 	return mask & (*(dw + dw_offset) >> offset);
155 }
156 
157 #define AMAP_GET_BITS(_struct, field, ptr)				\
158 		amap_get(ptr,						\
159 			offsetof(_struct, field)/32,			\
160 			amap_mask(sizeof(((_struct *)0)->field)),	\
161 			AMAP_BIT_OFFSET(_struct, field))
162 
163 #define be_dws_cpu_to_le(wrb, len) swap_dws(wrb, len)
164 #define be_dws_le_to_cpu(wrb, len) swap_dws(wrb, len)
165 static inline void swap_dws(void *wrb, int len)
166 {
167 #ifdef __BIG_ENDIAN
168 	u32 *dw = wrb;
169 	WARN_ON(len % 4);
170 	do {
171 		*dw = cpu_to_le32(*dw);
172 		dw++;
173 		len -= 4;
174 	} while (len);
175 #endif /* __BIG_ENDIAN */
176 }
177 #endif /* BEISCSI_H */
178