1 /*
2  * zfcp device driver
3  *
4  * Header file for zfcp qdio interface
5  *
6  * Copyright IBM Corporation 2010
7  */
8 
9 #ifndef ZFCP_QDIO_H
10 #define ZFCP_QDIO_H
11 
12 #include <asm/qdio.h>
13 
14 #define ZFCP_QDIO_SBALE_LEN	PAGE_SIZE
15 
16 /* DMQ bug workaround: don't use last SBALE */
17 #define ZFCP_QDIO_MAX_SBALES_PER_SBAL	(QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
18 
19 /* index of last SBALE (with respect to DMQ bug workaround) */
20 #define ZFCP_QDIO_LAST_SBALE_PER_SBAL	(ZFCP_QDIO_MAX_SBALES_PER_SBAL - 1)
21 
22 /* Max SBALS for chaining */
23 #define ZFCP_QDIO_MAX_SBALS_PER_REQ	36
24 
25 /* max. number of (data buffer) SBALEs in largest SBAL chain
26  * request ID + QTCB in SBALE 0 + 1 of first SBAL in chain   */
27 #define ZFCP_QDIO_MAX_SBALES_PER_REQ     \
28 	(ZFCP_QDIO_MAX_SBALS_PER_REQ * ZFCP_QDIO_MAX_SBALES_PER_SBAL - 2)
29 
30 /**
31  * struct zfcp_qdio - basic qdio data structure
32  * @res_q: response queue
33  * @req_q: request queue
34  * @req_q_idx: index of next free buffer
35  * @req_q_free: number of free buffers in queue
36  * @stat_lock: lock to protect req_q_util and req_q_time
37  * @req_q_lock: lock to serialize access to request queue
38  * @req_q_time: time of last fill level change
39  * @req_q_util: used for accounting
40  * @req_q_full: queue full incidents
41  * @req_q_wq: used to wait for SBAL availability
42  * @adapter: adapter used in conjunction with this qdio structure
43  */
44 struct zfcp_qdio {
45 	struct qdio_buffer	*res_q[QDIO_MAX_BUFFERS_PER_Q];
46 	struct qdio_buffer	*req_q[QDIO_MAX_BUFFERS_PER_Q];
47 	u8			req_q_idx;
48 	atomic_t		req_q_free;
49 	spinlock_t		stat_lock;
50 	spinlock_t		req_q_lock;
51 	unsigned long long	req_q_time;
52 	u64			req_q_util;
53 	atomic_t		req_q_full;
54 	wait_queue_head_t	req_q_wq;
55 	struct zfcp_adapter	*adapter;
56 };
57 
58 /**
59  * struct zfcp_qdio_req - qdio queue related values for a request
60  * @sbtype: sbal type flags for sbale 0
61  * @sbal_number: number of free sbals
62  * @sbal_first: first sbal for this request
63  * @sbal_last: last sbal for this request
64  * @sbal_limit: last possible sbal for this request
65  * @sbale_curr: current sbale at creation of this request
66  * @sbal_response: sbal used in interrupt
67  * @qdio_outb_usage: usage of outbound queue
68  */
69 struct zfcp_qdio_req {
70 	u32	sbtype;
71 	u8	sbal_number;
72 	u8	sbal_first;
73 	u8	sbal_last;
74 	u8	sbal_limit;
75 	u8	sbale_curr;
76 	u8	sbal_response;
77 	u16	qdio_outb_usage;
78 };
79 
80 /**
81  * zfcp_qdio_sbale_req - return pointer to sbale on req_q for a request
82  * @qdio: pointer to struct zfcp_qdio
83  * @q_rec: pointer to struct zfcp_qdio_req
84  * Returns: pointer to qdio_buffer_element (sbale) structure
85  */
86 static inline struct qdio_buffer_element *
zfcp_qdio_sbale_req(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req)87 zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
88 {
89 	return &qdio->req_q[q_req->sbal_last]->element[0];
90 }
91 
92 /**
93  * zfcp_qdio_sbale_curr - return current sbale on req_q for a request
94  * @qdio: pointer to struct zfcp_qdio
95  * @fsf_req: pointer to struct zfcp_fsf_req
96  * Returns: pointer to qdio_buffer_element (sbale) structure
97  */
98 static inline struct qdio_buffer_element *
zfcp_qdio_sbale_curr(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req)99 zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
100 {
101 	return &qdio->req_q[q_req->sbal_last]->element[q_req->sbale_curr];
102 }
103 
104 /**
105  * zfcp_qdio_req_init - initialize qdio request
106  * @qdio: request queue where to start putting the request
107  * @q_req: the qdio request to start
108  * @req_id: The request id
109  * @sbtype: type flags to set for all sbals
110  * @data: First data block
111  * @len: Length of first data block
112  *
113  * This is the start of putting the request into the queue, the last
114  * step is passing the request to zfcp_qdio_send. The request queue
115  * lock must be held during the whole process from init to send.
116  */
117 static inline
zfcp_qdio_req_init(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req,unsigned long req_id,u32 sbtype,void * data,u32 len)118 void zfcp_qdio_req_init(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
119 			unsigned long req_id, u32 sbtype, void *data, u32 len)
120 {
121 	struct qdio_buffer_element *sbale;
122 	int count = min(atomic_read(&qdio->req_q_free),
123 			ZFCP_QDIO_MAX_SBALS_PER_REQ);
124 
125 	q_req->sbal_first = q_req->sbal_last = qdio->req_q_idx;
126 	q_req->sbal_number = 1;
127 	q_req->sbtype = sbtype;
128 	q_req->sbale_curr = 1;
129 	q_req->sbal_limit = (q_req->sbal_first + count - 1)
130 					% QDIO_MAX_BUFFERS_PER_Q;
131 
132 	sbale = zfcp_qdio_sbale_req(qdio, q_req);
133 	sbale->addr = (void *) req_id;
134 	sbale->flags = SBAL_FLAGS0_COMMAND | sbtype;
135 
136 	if (unlikely(!data))
137 		return;
138 	sbale++;
139 	sbale->addr = data;
140 	sbale->length = len;
141 }
142 
143 /**
144  * zfcp_qdio_fill_next - Fill next sbale, only for single sbal requests
145  * @qdio: pointer to struct zfcp_qdio
146  * @q_req: pointer to struct zfcp_queue_req
147  *
148  * This is only required for single sbal requests, calling it when
149  * wrapping around to the next sbal is a bug.
150  */
151 static inline
zfcp_qdio_fill_next(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req,void * data,u32 len)152 void zfcp_qdio_fill_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
153 			 void *data, u32 len)
154 {
155 	struct qdio_buffer_element *sbale;
156 
157 	BUG_ON(q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL);
158 	q_req->sbale_curr++;
159 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
160 	sbale->addr = data;
161 	sbale->length = len;
162 }
163 
164 /**
165  * zfcp_qdio_set_sbale_last - set last entry flag in current sbale
166  * @qdio: pointer to struct zfcp_qdio
167  * @q_req: pointer to struct zfcp_queue_req
168  */
169 static inline
zfcp_qdio_set_sbale_last(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req)170 void zfcp_qdio_set_sbale_last(struct zfcp_qdio *qdio,
171 			      struct zfcp_qdio_req *q_req)
172 {
173 	struct qdio_buffer_element *sbale;
174 
175 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
176 	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
177 }
178 
179 /**
180  * zfcp_qdio_sg_one_sbal - check if one sbale is enough for sg data
181  * @sg: The scatterlist where to check the data size
182  *
183  * Returns: 1 when one sbale is enough for the data in the scatterlist,
184  *	    0 if not.
185  */
186 static inline
zfcp_qdio_sg_one_sbale(struct scatterlist * sg)187 int zfcp_qdio_sg_one_sbale(struct scatterlist *sg)
188 {
189 	return sg_is_last(sg) && sg->length <= ZFCP_QDIO_SBALE_LEN;
190 }
191 
192 /**
193  * zfcp_qdio_skip_to_last_sbale - skip to last sbale in sbal
194  * @q_req: The current zfcp_qdio_req
195  */
196 static inline
zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio_req * q_req)197 void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio_req *q_req)
198 {
199 	q_req->sbale_curr = ZFCP_QDIO_LAST_SBALE_PER_SBAL;
200 }
201 
202 /**
203  * zfcp_qdio_sbal_limit - set the sbal limit for a request in q_req
204  * @qdio: pointer to struct zfcp_qdio
205  * @q_req: The current zfcp_qdio_req
206  * @max_sbals: maximum number of SBALs allowed
207  */
208 static inline
zfcp_qdio_sbal_limit(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req,int max_sbals)209 void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
210 			  struct zfcp_qdio_req *q_req, int max_sbals)
211 {
212 	int count = min(atomic_read(&qdio->req_q_free), max_sbals);
213 
214 	q_req->sbal_limit = (q_req->sbal_first + count - 1) %
215 				QDIO_MAX_BUFFERS_PER_Q;
216 }
217 
218 /**
219  * zfcp_qdio_set_data_div - set data division count
220  * @qdio: pointer to struct zfcp_qdio
221  * @q_req: The current zfcp_qdio_req
222  * @count: The data division count
223  */
224 static inline
zfcp_qdio_set_data_div(struct zfcp_qdio * qdio,struct zfcp_qdio_req * q_req,u32 count)225 void zfcp_qdio_set_data_div(struct zfcp_qdio *qdio,
226 			    struct zfcp_qdio_req *q_req, u32 count)
227 {
228 	struct qdio_buffer_element *sbale;
229 
230 	sbale = &qdio->req_q[q_req->sbal_first]->element[0];
231 	sbale->length = count;
232 }
233 
234 #endif /* ZFCP_QDIO_H */
235