blob: 8bb00545f19cc7582277c4503d4e786e0d0bec9c [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/**
31 * struct zfcp_qdio_queue - qdio queue buffer, zfcp index and free count
32 * @sbal: qdio buffers
33 * @first: index of next free buffer in queue
34 * @count: number of free buffers in queue
35 */
36struct zfcp_qdio_queue {
37 struct qdio_buffer *sbal[QDIO_MAX_BUFFERS_PER_Q];
38 u8 first;
39 atomic_t count;
40};
41
42/**
43 * struct zfcp_qdio - basic qdio data structure
44 * @resp_q: response queue
45 * @req_q: request queue
46 * @stat_lock: lock to protect req_q_util and req_q_time
47 * @req_q_lock: lock to serialize access to request queue
48 * @req_q_time: time of last fill level change
49 * @req_q_util: used for accounting
50 * @req_q_full: queue full incidents
51 * @req_q_wq: used to wait for SBAL availability
52 * @adapter: adapter used in conjunction with this qdio structure
53 */
54struct zfcp_qdio {
55 struct zfcp_qdio_queue resp_q;
56 struct zfcp_qdio_queue req_q;
57 spinlock_t stat_lock;
58 spinlock_t req_q_lock;
59 unsigned long long req_q_time;
60 u64 req_q_util;
61 atomic_t req_q_full;
62 wait_queue_head_t req_q_wq;
63 struct zfcp_adapter *adapter;
64};
65
66/**
67 * struct zfcp_qdio_req - qdio queue related values for a request
Christof Schmitt1674b402010-04-30 18:09:34 +020068 * @sbtype: sbal type flags for sbale 0
Christof Schmitt34c2b712010-02-17 11:18:59 +010069 * @sbal_number: number of free sbals
70 * @sbal_first: first sbal for this request
71 * @sbal_last: last sbal for this request
72 * @sbal_limit: last possible sbal for this request
73 * @sbale_curr: current sbale at creation of this request
74 * @sbal_response: sbal used in interrupt
75 * @qdio_outb_usage: usage of outbound queue
76 * @qdio_inb_usage: usage of inbound queue
77 */
78struct zfcp_qdio_req {
Christof Schmitt1674b402010-04-30 18:09:34 +020079 u32 sbtype;
Christof Schmitt34c2b712010-02-17 11:18:59 +010080 u8 sbal_number;
81 u8 sbal_first;
82 u8 sbal_last;
83 u8 sbal_limit;
84 u8 sbale_curr;
85 u8 sbal_response;
86 u16 qdio_outb_usage;
87 u16 qdio_inb_usage;
88};
89
90/**
91 * zfcp_qdio_sbale - return pointer to sbale in qdio queue
92 * @q: queue where to find sbal
93 * @sbal_idx: sbal index in queue
94 * @sbale_idx: sbale index in sbal
95 */
96static inline struct qdio_buffer_element *
97zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
98{
99 return &q->sbal[sbal_idx]->element[sbale_idx];
100}
101
102/**
103 * zfcp_qdio_sbale_req - return pointer to sbale on req_q for a request
104 * @qdio: pointer to struct zfcp_qdio
105 * @q_rec: pointer to struct zfcp_qdio_req
106 * Returns: pointer to qdio_buffer_element (sbale) structure
107 */
108static inline struct qdio_buffer_element *
109zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
110{
111 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last, 0);
112}
113
114/**
115 * zfcp_qdio_sbale_curr - return current sbale on req_q for a request
116 * @qdio: pointer to struct zfcp_qdio
117 * @fsf_req: pointer to struct zfcp_fsf_req
118 * Returns: pointer to qdio_buffer_element (sbale) structure
119 */
120static inline struct qdio_buffer_element *
121zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
122{
123 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last,
124 q_req->sbale_curr);
125}
126
Christof Schmitt1674b402010-04-30 18:09:34 +0200127/**
128 * zfcp_qdio_req_init - initialize qdio request
129 * @qdio: request queue where to start putting the request
130 * @q_req: the qdio request to start
131 * @req_id: The request id
132 * @sbtype: type flags to set for all sbals
133 * @data: First data block
134 * @len: Length of first data block
135 *
136 * This is the start of putting the request into the queue, the last
137 * step is passing the request to zfcp_qdio_send. The request queue
138 * lock must be held during the whole process from init to send.
139 */
140static inline
141void zfcp_qdio_req_init(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
142 unsigned long req_id, u32 sbtype, void *data, u32 len)
143{
144 struct qdio_buffer_element *sbale;
Swen Schillig01b04752010-07-16 15:37:37 +0200145 int count = min(atomic_read(&qdio->req_q.count),
146 ZFCP_QDIO_MAX_SBALS_PER_REQ);
Christof Schmitt1674b402010-04-30 18:09:34 +0200147
148 q_req->sbal_first = q_req->sbal_last = qdio->req_q.first;
149 q_req->sbal_number = 1;
150 q_req->sbtype = sbtype;
Swen Schillig01b04752010-07-16 15:37:37 +0200151 q_req->sbal_limit = (q_req->sbal_first + count - 1)
152 % QDIO_MAX_BUFFERS_PER_Q;
Christof Schmitt1674b402010-04-30 18:09:34 +0200153
154 sbale = zfcp_qdio_sbale_req(qdio, q_req);
155 sbale->addr = (void *) req_id;
156 sbale->flags |= SBAL_FLAGS0_COMMAND;
157 sbale->flags |= sbtype;
158
159 q_req->sbale_curr = 1;
160 sbale++;
161 sbale->addr = data;
162 if (likely(data))
163 sbale->length = len;
164}
165
166/**
167 * zfcp_qdio_fill_next - Fill next sbale, only for single sbal requests
168 * @qdio: pointer to struct zfcp_qdio
169 * @q_req: pointer to struct zfcp_queue_req
170 *
171 * This is only required for single sbal requests, calling it when
172 * wrapping around to the next sbal is a bug.
173 */
174static inline
175void zfcp_qdio_fill_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
176 void *data, u32 len)
177{
178 struct qdio_buffer_element *sbale;
179
180 BUG_ON(q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL);
181 q_req->sbale_curr++;
182 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
183 sbale->addr = data;
184 sbale->length = len;
185}
186
187/**
188 * zfcp_qdio_set_sbale_last - set last entry flag in current sbale
189 * @qdio: pointer to struct zfcp_qdio
190 * @q_req: pointer to struct zfcp_queue_req
191 */
192static inline
193void zfcp_qdio_set_sbale_last(struct zfcp_qdio *qdio,
194 struct zfcp_qdio_req *q_req)
195{
196 struct qdio_buffer_element *sbale;
197
198 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
199 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
200}
201
202/**
203 * zfcp_qdio_sg_one_sbal - check if one sbale is enough for sg data
204 * @sg: The scatterlist where to check the data size
205 *
206 * Returns: 1 when one sbale is enough for the data in the scatterlist,
207 * 0 if not.
208 */
209static inline
210int zfcp_qdio_sg_one_sbale(struct scatterlist *sg)
211{
212 return sg_is_last(sg) && sg->length <= ZFCP_QDIO_SBALE_LEN;
213}
214
215/**
216 * zfcp_qdio_skip_to_last_sbale - skip to last sbale in sbal
217 * @q_req: The current zfcp_qdio_req
218 */
219static inline
220void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio_req *q_req)
221{
222 q_req->sbale_curr = ZFCP_QDIO_LAST_SBALE_PER_SBAL;
223}
224
Swen Schillig01b04752010-07-16 15:37:37 +0200225/**
226 * zfcp_qdio_sbal_limit - set the sbal limit for a request in q_req
227 * @qdio: pointer to struct zfcp_qdio
228 * @q_req: The current zfcp_qdio_req
229 * @max_sbals: maximum number of SBALs allowed
230 */
231static inline
232void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
233 struct zfcp_qdio_req *q_req, int max_sbals)
234{
235 int count = min(atomic_read(&qdio->req_q.count), max_sbals);
236
237 q_req->sbal_limit = (q_req->sbal_first + count - 1) %
238 QDIO_MAX_BUFFERS_PER_Q;
239}
240
Christof Schmitt34c2b712010-02-17 11:18:59 +0100241#endif /* ZFCP_QDIO_H */