License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / drivers / s390 / scsi / zfcp_qdio.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
34c2b712
CS
2/*
3 * zfcp device driver
4 *
5 * Header file for zfcp qdio interface
6 *
a53c8fab 7 * Copyright IBM Corp. 2010
34c2b712
CS
8 */
9
10#ifndef ZFCP_QDIO_H
11#define ZFCP_QDIO_H
12
13#include <asm/qdio.h>
14
68322984
CS
15#define ZFCP_QDIO_SBALE_LEN PAGE_SIZE
16
01b04759
SS
17/* Max SBALS for chaining */
18#define ZFCP_QDIO_MAX_SBALS_PER_REQ 36
19
34c2b712
CS
20/**
21 * struct zfcp_qdio - basic qdio data structure
706eca49 22 * @res_q: response queue
34c2b712 23 * @req_q: request queue
706eca49
SS
24 * @req_q_idx: index of next free buffer
25 * @req_q_free: number of free buffers in queue
34c2b712
CS
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 */
34struct zfcp_qdio {
706eca49
SS
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;
34c2b712
CS
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;
86a9668a
SS
46 u16 max_sbale_per_sbal;
47 u16 max_sbale_per_req;
34c2b712
CS
48};
49
50/**
51 * struct zfcp_qdio_req - qdio queue related values for a request
1674b405 52 * @sbtype: sbal type flags for sbale 0
34c2b712
CS
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
34c2b712 58 * @qdio_outb_usage: usage of outbound queue
34c2b712
CS
59 */
60struct zfcp_qdio_req {
3ec90878 61 u8 sbtype;
34c2b712
CS
62 u8 sbal_number;
63 u8 sbal_first;
64 u8 sbal_last;
65 u8 sbal_limit;
66 u8 sbale_curr;
34c2b712 67 u16 qdio_outb_usage;
34c2b712
CS
68};
69
34c2b712
CS
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 */
76static inline struct qdio_buffer_element *
77zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
78{
706eca49 79 return &qdio->req_q[q_req->sbal_last]->element[0];
34c2b712
CS
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 */
88static inline struct qdio_buffer_element *
89zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
90{
706eca49 91 return &qdio->req_q[q_req->sbal_last]->element[q_req->sbale_curr];
34c2b712
CS
92}
93
1674b405
CS
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 */
107static inline
108void zfcp_qdio_req_init(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
3ec90878 109 unsigned long req_id, u8 sbtype, void *data, u32 len)
1674b405
CS
110{
111 struct qdio_buffer_element *sbale;
706eca49 112 int count = min(atomic_read(&qdio->req_q_free),
01b04759 113 ZFCP_QDIO_MAX_SBALS_PER_REQ);
1674b405 114
706eca49 115 q_req->sbal_first = q_req->sbal_last = qdio->req_q_idx;
1674b405
CS
116 q_req->sbal_number = 1;
117 q_req->sbtype = sbtype;
706eca49 118 q_req->sbale_curr = 1;
01b04759
SS
119 q_req->sbal_limit = (q_req->sbal_first + count - 1)
120 % QDIO_MAX_BUFFERS_PER_Q;
1674b405
CS
121
122 sbale = zfcp_qdio_sbale_req(qdio, q_req);
123 sbale->addr = (void *) req_id;
3ec90878
JG
124 sbale->eflags = 0;
125 sbale->sflags = SBAL_SFLAGS0_COMMAND | sbtype;
1674b405 126
706eca49
SS
127 if (unlikely(!data))
128 return;
1674b405
CS
129 sbale++;
130 sbale->addr = data;
706eca49 131 sbale->length = len;
1674b405
CS
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 */
142static inline
143void 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
86a9668a 148 BUG_ON(q_req->sbale_curr == qdio->max_sbale_per_sbal - 1);
1674b405
CS
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 */
160static inline
161void 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);
3ec90878 167 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
1674b405
CS
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 */
177static inline
178int 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 */
187static inline
86a9668a
SS
188void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio *qdio,
189 struct zfcp_qdio_req *q_req)
1674b405 190{
86a9668a 191 q_req->sbale_curr = qdio->max_sbale_per_sbal - 1;
1674b405
CS
192}
193
01b04759
SS
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 */
200static inline
201void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
202 struct zfcp_qdio_req *q_req, int max_sbals)
203{
706eca49 204 int count = min(atomic_read(&qdio->req_q_free), max_sbals);
01b04759
SS
205
206 q_req->sbal_limit = (q_req->sbal_first + count - 1) %
207 QDIO_MAX_BUFFERS_PER_Q;
208}
209
ef3eb71d
FB
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 */
216static inline
217void 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
86a9668a 222 sbale = qdio->req_q[q_req->sbal_first]->element;
ef3eb71d
FB
223 sbale->length = count;
224}
225
86a9668a
SS
226/**
227 * zfcp_qdio_real_bytes - count bytes used
228 * @sg: pointer to struct scatterlist
229 */
230static inline
231unsigned 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 */
246static inline
247void 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
34c2b712 255#endif /* ZFCP_QDIO_H */