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