blob: 54e22ace012b601f5a3cc8dd3cc33505458b8cfd [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 {
Jan Glauber3ec90872011-06-06 14:14:40 +020070 u8 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,
Jan Glauber3ec90872011-06-06 14:14:40 +0200119 unsigned long req_id, u8 sbtype, void *data, u32 len)
Christof Schmitt1674b402010-04-30 18:09:34 +0200120{
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;
Jan Glauber3ec90872011-06-06 14:14:40 +0200134 sbale->eflags = 0;
135 sbale->sflags = SBAL_SFLAGS0_COMMAND | sbtype;
Christof Schmitt1674b402010-04-30 18:09:34 +0200136
Swen Schillig706eca42010-07-16 15:37:38 +0200137 if (unlikely(!data))
138 return;
Christof Schmitt1674b402010-04-30 18:09:34 +0200139 sbale++;
140 sbale->addr = data;
Swen Schillig706eca42010-07-16 15:37:38 +0200141 sbale->length = len;
Christof Schmitt1674b402010-04-30 18:09:34 +0200142}
143
144/**
145 * zfcp_qdio_fill_next - Fill next sbale, only for single sbal requests
146 * @qdio: pointer to struct zfcp_qdio
147 * @q_req: pointer to struct zfcp_queue_req
148 *
149 * This is only required for single sbal requests, calling it when
150 * wrapping around to the next sbal is a bug.
151 */
152static inline
153void zfcp_qdio_fill_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
154 void *data, u32 len)
155{
156 struct qdio_buffer_element *sbale;
157
158 BUG_ON(q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL);
159 q_req->sbale_curr++;
160 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
161 sbale->addr = data;
162 sbale->length = len;
163}
164
165/**
166 * zfcp_qdio_set_sbale_last - set last entry flag in current sbale
167 * @qdio: pointer to struct zfcp_qdio
168 * @q_req: pointer to struct zfcp_queue_req
169 */
170static inline
171void zfcp_qdio_set_sbale_last(struct zfcp_qdio *qdio,
172 struct zfcp_qdio_req *q_req)
173{
174 struct qdio_buffer_element *sbale;
175
176 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec90872011-06-06 14:14:40 +0200177 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
Christof Schmitt1674b402010-04-30 18:09:34 +0200178}
179
180/**
181 * zfcp_qdio_sg_one_sbal - check if one sbale is enough for sg data
182 * @sg: The scatterlist where to check the data size
183 *
184 * Returns: 1 when one sbale is enough for the data in the scatterlist,
185 * 0 if not.
186 */
187static inline
188int zfcp_qdio_sg_one_sbale(struct scatterlist *sg)
189{
190 return sg_is_last(sg) && sg->length <= ZFCP_QDIO_SBALE_LEN;
191}
192
193/**
194 * zfcp_qdio_skip_to_last_sbale - skip to last sbale in sbal
195 * @q_req: The current zfcp_qdio_req
196 */
197static inline
198void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio_req *q_req)
199{
200 q_req->sbale_curr = ZFCP_QDIO_LAST_SBALE_PER_SBAL;
201}
202
Swen Schillig01b04752010-07-16 15:37:37 +0200203/**
204 * zfcp_qdio_sbal_limit - set the sbal limit for a request in q_req
205 * @qdio: pointer to struct zfcp_qdio
206 * @q_req: The current zfcp_qdio_req
207 * @max_sbals: maximum number of SBALs allowed
208 */
209static inline
210void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
211 struct zfcp_qdio_req *q_req, int max_sbals)
212{
Swen Schillig706eca42010-07-16 15:37:38 +0200213 int count = min(atomic_read(&qdio->req_q_free), max_sbals);
Swen Schillig01b04752010-07-16 15:37:37 +0200214
215 q_req->sbal_limit = (q_req->sbal_first + count - 1) %
216 QDIO_MAX_BUFFERS_PER_Q;
217}
218
Felix Beckef3eb712010-07-16 15:37:42 +0200219/**
220 * zfcp_qdio_set_data_div - set data division count
221 * @qdio: pointer to struct zfcp_qdio
222 * @q_req: The current zfcp_qdio_req
223 * @count: The data division count
224 */
225static inline
226void zfcp_qdio_set_data_div(struct zfcp_qdio *qdio,
227 struct zfcp_qdio_req *q_req, u32 count)
228{
229 struct qdio_buffer_element *sbale;
230
231 sbale = &qdio->req_q[q_req->sbal_first]->element[0];
232 sbale->length = count;
233}
234
Christof Schmitt34c2b712010-02-17 11:18:59 +0100235#endif /* ZFCP_QDIO_H */