4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2009
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/slab.h>
14 #include "zfcp_qdio.h"
16 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
18 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer
**sbal
)
22 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
+= QBUFF_PER_PAGE
) {
23 sbal
[pos
] = (struct qdio_buffer
*) get_zeroed_page(GFP_KERNEL
);
27 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
++)
28 if (pos
% QBUFF_PER_PAGE
)
29 sbal
[pos
] = sbal
[pos
- 1] + 1;
33 static void zfcp_qdio_handler_error(struct zfcp_qdio
*qdio
, char *id
)
35 struct zfcp_adapter
*adapter
= qdio
->adapter
;
37 dev_warn(&adapter
->ccw_device
->dev
, "A QDIO problem occurred\n");
39 zfcp_erp_adapter_reopen(adapter
,
40 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
|
41 ZFCP_STATUS_COMMON_ERP_FAILED
, id
, NULL
);
44 static void zfcp_qdio_zero_sbals(struct qdio_buffer
*sbal
[], int first
, int cnt
)
48 for (i
= first
; i
< first
+ cnt
; i
++) {
49 sbal_idx
= i
% QDIO_MAX_BUFFERS_PER_Q
;
50 memset(sbal
[sbal_idx
], 0, sizeof(struct qdio_buffer
));
54 /* this needs to be called prior to updating the queue fill level */
55 static inline void zfcp_qdio_account(struct zfcp_qdio
*qdio
)
57 unsigned long long now
, span
;
60 spin_lock(&qdio
->stat_lock
);
61 now
= get_clock_monotonic();
62 span
= (now
- qdio
->req_q_time
) >> 12;
63 free
= atomic_read(&qdio
->req_q
.count
);
64 used
= QDIO_MAX_BUFFERS_PER_Q
- free
;
65 qdio
->req_q_util
+= used
* span
;
66 qdio
->req_q_time
= now
;
67 spin_unlock(&qdio
->stat_lock
);
70 static void zfcp_qdio_int_req(struct ccw_device
*cdev
, unsigned int qdio_err
,
71 int queue_no
, int first
, int count
,
74 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
75 struct zfcp_qdio_queue
*queue
= &qdio
->req_q
;
77 if (unlikely(qdio_err
)) {
78 zfcp_dbf_hba_qdio(qdio
->adapter
->dbf
, qdio_err
, first
,
80 zfcp_qdio_handler_error(qdio
, "qdireq1");
84 /* cleanup all SBALs being program-owned now */
85 zfcp_qdio_zero_sbals(queue
->sbal
, first
, count
);
87 zfcp_qdio_account(qdio
);
88 atomic_add(count
, &queue
->count
);
89 wake_up(&qdio
->req_q_wq
);
92 static void zfcp_qdio_resp_put_back(struct zfcp_qdio
*qdio
, int processed
)
94 struct zfcp_qdio_queue
*queue
= &qdio
->resp_q
;
95 struct ccw_device
*cdev
= qdio
->adapter
->ccw_device
;
96 u8 count
, start
= queue
->first
;
99 count
= atomic_read(&queue
->count
) + processed
;
101 retval
= do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, start
, count
);
103 if (unlikely(retval
)) {
104 atomic_set(&queue
->count
, count
);
105 zfcp_erp_adapter_reopen(qdio
->adapter
, 0, "qdrpb_1", NULL
);
107 queue
->first
+= count
;
108 queue
->first
%= QDIO_MAX_BUFFERS_PER_Q
;
109 atomic_set(&queue
->count
, 0);
113 static void zfcp_qdio_int_resp(struct ccw_device
*cdev
, unsigned int qdio_err
,
114 int queue_no
, int first
, int count
,
117 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
118 int sbal_idx
, sbal_no
;
120 if (unlikely(qdio_err
)) {
121 zfcp_dbf_hba_qdio(qdio
->adapter
->dbf
, qdio_err
, first
,
123 zfcp_qdio_handler_error(qdio
, "qdires1");
128 * go through all SBALs from input queue currently
129 * returned by QDIO layer
131 for (sbal_no
= 0; sbal_no
< count
; sbal_no
++) {
132 sbal_idx
= (first
+ sbal_no
) % QDIO_MAX_BUFFERS_PER_Q
;
133 /* go through all SBALEs of SBAL */
134 zfcp_fsf_reqid_check(qdio
, sbal_idx
);
138 * put range of SBALs back to response queue
139 * (including SBALs which have already been free before)
141 zfcp_qdio_resp_put_back(qdio
, count
);
144 static void zfcp_qdio_sbal_limit(struct zfcp_qdio
*qdio
,
145 struct zfcp_qdio_req
*q_req
, int max_sbals
)
147 int count
= atomic_read(&qdio
->req_q
.count
);
148 count
= min(count
, max_sbals
);
149 q_req
->sbal_limit
= (q_req
->sbal_first
+ count
- 1)
150 % QDIO_MAX_BUFFERS_PER_Q
;
153 static struct qdio_buffer_element
*
154 zfcp_qdio_sbal_chain(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
,
155 unsigned long sbtype
)
157 struct qdio_buffer_element
*sbale
;
159 /* set last entry flag in current SBALE of current SBAL */
160 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
161 sbale
->flags
|= SBAL_FLAGS_LAST_ENTRY
;
163 /* don't exceed last allowed SBAL */
164 if (q_req
->sbal_last
== q_req
->sbal_limit
)
167 /* set chaining flag in first SBALE of current SBAL */
168 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
169 sbale
->flags
|= SBAL_FLAGS0_MORE_SBALS
;
171 /* calculate index of next SBAL */
173 q_req
->sbal_last
%= QDIO_MAX_BUFFERS_PER_Q
;
175 /* keep this requests number of SBALs up-to-date */
176 q_req
->sbal_number
++;
178 /* start at first SBALE of new SBAL */
179 q_req
->sbale_curr
= 0;
181 /* set storage-block type for new SBAL */
182 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
183 sbale
->flags
|= sbtype
;
188 static struct qdio_buffer_element
*
189 zfcp_qdio_sbale_next(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
,
192 if (q_req
->sbale_curr
== ZFCP_LAST_SBALE_PER_SBAL
)
193 return zfcp_qdio_sbal_chain(qdio
, q_req
, sbtype
);
195 return zfcp_qdio_sbale_curr(qdio
, q_req
);
198 static void zfcp_qdio_undo_sbals(struct zfcp_qdio
*qdio
,
199 struct zfcp_qdio_req
*q_req
)
201 struct qdio_buffer
**sbal
= qdio
->req_q
.sbal
;
202 int first
= q_req
->sbal_first
;
203 int last
= q_req
->sbal_last
;
204 int count
= (last
- first
+ QDIO_MAX_BUFFERS_PER_Q
) %
205 QDIO_MAX_BUFFERS_PER_Q
+ 1;
206 zfcp_qdio_zero_sbals(sbal
, first
, count
);
209 static int zfcp_qdio_fill_sbals(struct zfcp_qdio
*qdio
,
210 struct zfcp_qdio_req
*q_req
,
211 unsigned int sbtype
, void *start_addr
,
212 unsigned int total_length
)
214 struct qdio_buffer_element
*sbale
;
215 unsigned long remaining
, length
;
218 /* split segment up */
219 for (addr
= start_addr
, remaining
= total_length
; remaining
> 0;
220 addr
+= length
, remaining
-= length
) {
221 sbale
= zfcp_qdio_sbale_next(qdio
, q_req
, sbtype
);
223 atomic_inc(&qdio
->req_q_full
);
224 zfcp_qdio_undo_sbals(qdio
, q_req
);
228 /* new piece must not exceed next page boundary */
229 length
= min(remaining
,
230 (PAGE_SIZE
- ((unsigned long)addr
&
233 sbale
->length
= length
;
239 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
240 * @fsf_req: request to be processed
241 * @sbtype: SBALE flags
242 * @sg: scatter-gather list
243 * @max_sbals: upper bound for number of SBALs to be used
244 * Returns: number of bytes, or error (negativ)
246 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
,
247 unsigned long sbtype
, struct scatterlist
*sg
,
250 struct qdio_buffer_element
*sbale
;
251 int retval
, bytes
= 0;
253 /* figure out last allowed SBAL */
254 zfcp_qdio_sbal_limit(qdio
, q_req
, max_sbals
);
256 /* set storage-block type for this request */
257 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
258 sbale
->flags
|= sbtype
;
260 for (; sg
; sg
= sg_next(sg
)) {
261 retval
= zfcp_qdio_fill_sbals(qdio
, q_req
, sbtype
,
262 sg_virt(sg
), sg
->length
);
268 /* assume that no other SBALEs are to follow in the same SBAL */
269 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
270 sbale
->flags
|= SBAL_FLAGS_LAST_ENTRY
;
276 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
277 * @qdio: pointer to struct zfcp_qdio
278 * @q_req: pointer to struct zfcp_qdio_req
279 * Returns: 0 on success, error otherwise
281 int zfcp_qdio_send(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
283 struct zfcp_qdio_queue
*req_q
= &qdio
->req_q
;
284 int first
= q_req
->sbal_first
;
285 int count
= q_req
->sbal_number
;
287 unsigned int qdio_flags
= QDIO_FLAG_SYNC_OUTPUT
;
289 zfcp_qdio_account(qdio
);
291 retval
= do_QDIO(qdio
->adapter
->ccw_device
, qdio_flags
, 0, first
,
293 if (unlikely(retval
)) {
294 zfcp_qdio_zero_sbals(req_q
->sbal
, first
, count
);
298 /* account for transferred buffers */
299 atomic_sub(count
, &req_q
->count
);
300 req_q
->first
+= count
;
301 req_q
->first
%= QDIO_MAX_BUFFERS_PER_Q
;
306 static void zfcp_qdio_setup_init_data(struct qdio_initialize
*id
,
307 struct zfcp_qdio
*qdio
)
310 id
->cdev
= qdio
->adapter
->ccw_device
;
311 id
->q_format
= QDIO_ZFCP_QFMT
;
312 memcpy(id
->adapter_name
, dev_name(&id
->cdev
->dev
), 8);
313 ASCEBC(id
->adapter_name
, 8);
314 id
->qib_param_field_format
= 0;
315 id
->qib_param_field
= NULL
;
316 id
->input_slib_elements
= NULL
;
317 id
->output_slib_elements
= NULL
;
319 id
->no_output_qs
= 1;
320 id
->input_handler
= zfcp_qdio_int_resp
;
321 id
->output_handler
= zfcp_qdio_int_req
;
322 id
->int_parm
= (unsigned long) qdio
;
323 id
->input_sbal_addr_array
= (void **) (qdio
->resp_q
.sbal
);
324 id
->output_sbal_addr_array
= (void **) (qdio
->req_q
.sbal
);
328 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
329 * @adapter: pointer to struct zfcp_adapter
330 * Returns: -ENOMEM on memory allocation error or return value from
333 static int zfcp_qdio_allocate(struct zfcp_qdio
*qdio
)
335 struct qdio_initialize init_data
;
337 if (zfcp_qdio_buffers_enqueue(qdio
->req_q
.sbal
) ||
338 zfcp_qdio_buffers_enqueue(qdio
->resp_q
.sbal
))
341 zfcp_qdio_setup_init_data(&init_data
, qdio
);
343 return qdio_allocate(&init_data
);
347 * zfcp_close_qdio - close qdio queues for an adapter
348 * @qdio: pointer to structure zfcp_qdio
350 void zfcp_qdio_close(struct zfcp_qdio
*qdio
)
352 struct zfcp_qdio_queue
*req_q
;
355 if (!(atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
358 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
359 req_q
= &qdio
->req_q
;
360 spin_lock_bh(&qdio
->req_q_lock
);
361 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP
, &qdio
->adapter
->status
);
362 spin_unlock_bh(&qdio
->req_q_lock
);
364 qdio_shutdown(qdio
->adapter
->ccw_device
,
365 QDIO_FLAG_CLEANUP_USING_CLEAR
);
367 /* cleanup used outbound sbals */
368 count
= atomic_read(&req_q
->count
);
369 if (count
< QDIO_MAX_BUFFERS_PER_Q
) {
370 first
= (req_q
->first
+ count
) % QDIO_MAX_BUFFERS_PER_Q
;
371 count
= QDIO_MAX_BUFFERS_PER_Q
- count
;
372 zfcp_qdio_zero_sbals(req_q
->sbal
, first
, count
);
375 atomic_set(&req_q
->count
, 0);
376 qdio
->resp_q
.first
= 0;
377 atomic_set(&qdio
->resp_q
.count
, 0);
381 * zfcp_qdio_open - prepare and initialize response queue
382 * @qdio: pointer to struct zfcp_qdio
383 * Returns: 0 on success, otherwise -EIO
385 int zfcp_qdio_open(struct zfcp_qdio
*qdio
)
387 struct qdio_buffer_element
*sbale
;
388 struct qdio_initialize init_data
;
389 struct ccw_device
*cdev
= qdio
->adapter
->ccw_device
;
392 if (atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
)
395 zfcp_qdio_setup_init_data(&init_data
, qdio
);
397 if (qdio_establish(&init_data
))
398 goto failed_establish
;
400 if (qdio_activate(cdev
))
403 for (cc
= 0; cc
< QDIO_MAX_BUFFERS_PER_Q
; cc
++) {
404 sbale
= &(qdio
->resp_q
.sbal
[cc
]->element
[0]);
406 sbale
->flags
= SBAL_FLAGS_LAST_ENTRY
;
410 if (do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, 0,
411 QDIO_MAX_BUFFERS_PER_Q
))
414 /* set index of first avalable SBALS / number of available SBALS */
415 qdio
->req_q
.first
= 0;
416 atomic_set(&qdio
->req_q
.count
, QDIO_MAX_BUFFERS_PER_Q
);
421 qdio_shutdown(cdev
, QDIO_FLAG_CLEANUP_USING_CLEAR
);
424 "Setting up the QDIO connection to the FCP adapter failed\n");
428 void zfcp_qdio_destroy(struct zfcp_qdio
*qdio
)
430 struct qdio_buffer
**sbal_req
, **sbal_resp
;
436 if (qdio
->adapter
->ccw_device
)
437 qdio_free(qdio
->adapter
->ccw_device
);
439 sbal_req
= qdio
->req_q
.sbal
;
440 sbal_resp
= qdio
->resp_q
.sbal
;
442 for (p
= 0; p
< QDIO_MAX_BUFFERS_PER_Q
; p
+= QBUFF_PER_PAGE
) {
443 free_page((unsigned long) sbal_req
[p
]);
444 free_page((unsigned long) sbal_resp
[p
]);
450 int zfcp_qdio_setup(struct zfcp_adapter
*adapter
)
452 struct zfcp_qdio
*qdio
;
454 qdio
= kzalloc(sizeof(struct zfcp_qdio
), GFP_KERNEL
);
458 qdio
->adapter
= adapter
;
460 if (zfcp_qdio_allocate(qdio
)) {
461 zfcp_qdio_destroy(qdio
);
465 spin_lock_init(&qdio
->req_q_lock
);
466 spin_lock_init(&qdio
->stat_lock
);
468 adapter
->qdio
= qdio
;