blob: 2297d8d3e947b0aa16ba1d4c0d68251c6eade33d [file] [log] [blame]
Christof Schmitt34c2b712010-02-17 11:18:59 +01001/*
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
Christof Schmitt68322982010-04-30 18:09:33 +020014#define ZFCP_QDIO_SBALE_LEN PAGE_SIZE
15
Christof Schmitt1674b402010-04-30 18:09:34 +020016/* 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
Swen Schillig01b04752010-07-16 15:37:37 +020022/* 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
Christof Schmitt34c2b712010-02-17 11:18:59 +010030/**
Christof Schmitt34c2b712010-02-17 11:18:59 +010031 * struct zfcp_qdio - basic qdio data structure
Swen Schillig706eca42010-07-16 15:37:38 +020032 * @res_q: response queue
Christof Schmitt34c2b712010-02-17 11:18:59 +010033 * @req_q: request queue
Swen Schillig706eca42010-07-16 15:37:38 +020034 * @req_q_idx: index of next free buffer
35 * @req_q_free: number of free buffers in queue
Christof Schmitt34c2b712010-02-17 11:18:59 +010036 * @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 */
44struct zfcp_qdio {
Swen Schillig706eca42010-07-16 15:37:38 +020045 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;
Christof Schmitt34c2b712010-02-17 11:18:59 +010049 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
Christof Schmitt1674b402010-04-30 18:09:34 +020060 * @sbtype: sbal type flags for sbale 0
Christof Schmitt34c2b712010-02-17 11:18:59 +010061 * @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
Christof Schmitt34c2b712010-02-17 11:18:59 +010068 */
69struct zfcp_qdio_req {
Christof Schmitt1674b402010-04-30 18:09:34 +020070 u32 sbtype;
Christof Schmitt34c2b712010-02-17 11:18:59 +010071 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;
Christof Schmitt34c2b712010-02-17 11:18:59 +010078};
79
80/**
Christof Schmitt34c2b712010-02-17 11:18:59 +010081 * 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 */
86static inline struct qdio_buffer_element *
87zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
88{
Swen Schillig706eca42010-07-16 15:37:38 +020089 return &qdio->req_q[q_req->sbal_last]->element[0];
Christof Schmitt34c2b712010-02-17 11:18:59 +010090}
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 */
98static inline struct qdio_buffer_element *
99zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
100{
Swen Schillig706eca42010-07-16 15:37:38 +0200101 return &qdio->req_q[q_req->sbal_last]->element[q_req->sbale_curr];
Christof Schmitt34c2b712010-02-17 11:18:59 +0100102}
103
Christof Schmitt1674b402010-04-30 18:09:34 +0200104/**
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 */
117static inline
118void 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;
Swen Schillig706eca42010-07-16 15:37:38 +0200122 int count = min(atomic_read(&qdio->req_q_free),
Swen Schillig01b04752010-07-16 15:37:37 +0200123 ZFCP_QDIO_MAX_SBALS_PER_REQ);
Christof Schmitt1674b402010-04-30 18:09:34 +0200124
Swen Schillig706eca42010-07-16 15:37:38 +0200125 q_req->sbal_first = q_req->sbal_last = qdio->req_q_idx;
Christof Schmitt1674b402010-04-30 18:09:34 +0200126 q_req->sbal_number = 1;
127 q_req->sbtype = sbtype;
Swen Schillig706eca42010-07-16 15:37:38 +0200128 q_req->sbale_curr = 1;
Swen Schillig01b04752010-07-16 15:37:37 +0200129 q_req->sbal_limit = (q_req->sbal_first + count - 1)
130 % QDIO_MAX_BUFFERS_PER_Q;
Christof Schmitt1674b402010-04-30 18:09:34 +0200131
132 sbale = zfcp_qdio_sbale_req(qdio, q_req);
133 sbale->addr = (void *) req_id;
Swen Schillig706eca42010-07-16 15:37:38 +0200134 sbale->flags = SBAL_FLAGS0_COMMAND | sbtype;
Christof Schmitt1674b402010-04-30 18:09:34 +0200135
Swen Schillig706eca42010-07-16 15:37:38 +0200136 if (unlikely(!data))
137 return;
Christof Schmitt1674b402010-04-30 18:09:34 +0200138 sbale++;
139 sbale->addr = data;
Swen Schillig706eca42010-07-16 15:37:38 +0200140 sbale->length = len;
Christof Schmitt1674b402010-04-30 18:09:34 +0200141}
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 */
151static inline
152void 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 */
169static inline
170void 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 */
186static inline
187int 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 */
196static inline
197void 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
Swen Schillig01b04752010-07-16 15:37:37 +0200202/**
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 */
208static inline
209void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
210 struct zfcp_qdio_req *q_req, int max_sbals)
211{
Swen Schillig706eca42010-07-16 15:37:38 +0200212 int count = min(atomic_read(&qdio->req_q_free), max_sbals);
Swen Schillig01b04752010-07-16 15:37:37 +0200213
214 q_req->sbal_limit = (q_req->sbal_first + count - 1) %
215 QDIO_MAX_BUFFERS_PER_Q;
216}
217
Felix Beckef3eb712010-07-16 15:37:42 +0200218/**
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 */
224static inline
225void 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
Christof Schmitt34c2b712010-02-17 11:18:59 +0100234#endif /* ZFCP_QDIO_H */