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