1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Header file for zfcp qdio interface
7 * Copyright IBM Corp. 2010
13 #include <linux/interrupt.h>
16 #define ZFCP_QDIO_SBALE_LEN PAGE_SIZE
18 /* Max SBALS for chaining */
19 #define ZFCP_QDIO_MAX_SBALS_PER_REQ 36
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
41 struct qdio_buffer *res_q[QDIO_MAX_BUFFERS_PER_Q];
42 struct qdio_buffer *req_q[QDIO_MAX_BUFFERS_PER_Q];
46 spinlock_t req_q_lock;
47 unsigned long long req_q_time;
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;
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
69 struct zfcp_qdio_req {
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
85 static inline struct qdio_buffer_element *
86 zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
88 return &qdio->req_q[q_req->sbal_last]->element[0];
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
97 static inline struct qdio_buffer_element *
98 zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
100 return &qdio->req_q[q_req->sbal_last]->element[q_req->sbale_curr];
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
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.
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)
120 struct qdio_buffer_element *sbale;
121 int count = min(atomic_read(&qdio->req_q_free),
122 ZFCP_QDIO_MAX_SBALS_PER_REQ);
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;
131 sbale = zfcp_qdio_sbale_req(qdio, q_req);
132 sbale->addr = req_id;
134 sbale->sflags = SBAL_SFLAGS0_COMMAND | sbtype;
139 sbale->addr = virt_to_phys(data);
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
150 * This is only required for single sbal requests, calling it when
151 * wrapping around to the next sbal is a bug.
154 void zfcp_qdio_fill_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
157 struct qdio_buffer_element *sbale;
159 BUG_ON(q_req->sbale_curr == qdio->max_sbale_per_sbal - 1);
161 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
162 sbale->addr = virt_to_phys(data);
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
172 void zfcp_qdio_set_sbale_last(struct zfcp_qdio *qdio,
173 struct zfcp_qdio_req *q_req)
175 struct qdio_buffer_element *sbale;
177 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
178 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
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
185 * Returns: 1 when one sbale is enough for the data in the scatterlist,
189 int zfcp_qdio_sg_one_sbale(struct scatterlist *sg)
191 return sg_is_last(sg) && sg->length <= ZFCP_QDIO_SBALE_LEN;
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
200 void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio *qdio,
201 struct zfcp_qdio_req *q_req)
203 q_req->sbale_curr = qdio->max_sbale_per_sbal - 1;
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
213 void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
214 struct zfcp_qdio_req *q_req, int max_sbals)
216 int count = min(atomic_read(&qdio->req_q_free), max_sbals);
218 q_req->sbal_limit = (q_req->sbal_first + count - 1) %
219 QDIO_MAX_BUFFERS_PER_Q;
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
229 void zfcp_qdio_set_data_div(struct zfcp_qdio *qdio,
230 struct zfcp_qdio_req *q_req, u32 count)
232 struct qdio_buffer_element *sbale;
234 sbale = qdio->req_q[q_req->sbal_first]->element;
235 sbale->length = count;
239 * zfcp_qdio_real_bytes - count bytes used
240 * @sg: pointer to struct scatterlist
243 unsigned int zfcp_qdio_real_bytes(struct scatterlist *sg)
245 unsigned int real_bytes = 0;
247 for (; sg; sg = sg_next(sg))
248 real_bytes += sg->length;
254 * zfcp_qdio_set_scount - set SBAL count value
255 * @qdio: pointer to struct zfcp_qdio
256 * @q_req: The current zfcp_qdio_req
259 void zfcp_qdio_set_scount(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
261 struct qdio_buffer_element *sbale;
263 sbale = qdio->req_q[q_req->sbal_first]->element;
264 sbale->scount = q_req->sbal_number - 1;
267 #endif /* ZFCP_QDIO_H */