blob: dfb6644d17b836b830e2ce49ccc31cb7490b608d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "zfcp_ext.h"
23
24static inline void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *, int);
25static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_get
26 (struct zfcp_qdio_queue *, int, int);
27static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_resp
28 (struct zfcp_fsf_req *, int, int);
29static inline volatile struct qdio_buffer_element *zfcp_qdio_sbal_chain
30 (struct zfcp_fsf_req *, unsigned long);
31static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_next
32 (struct zfcp_fsf_req *, unsigned long);
33static inline int zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *, int, int);
34static inline int zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *);
35static inline void zfcp_qdio_sbale_fill
36 (struct zfcp_fsf_req *, unsigned long, void *, int);
37static inline int zfcp_qdio_sbals_from_segment
38 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long);
39static inline int zfcp_qdio_sbals_from_buffer
40 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long, int);
41
42static qdio_handler_t zfcp_qdio_request_handler;
43static qdio_handler_t zfcp_qdio_response_handler;
44static int zfcp_qdio_handler_error_check(struct zfcp_adapter *,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020045 unsigned int, unsigned int, unsigned int, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define ZFCP_LOG_AREA ZFCP_LOG_AREA_QDIO
48
49/*
50 * Allocates BUFFER memory to each of the pointers of the qdio_buffer_t
51 * array in the adapter struct.
52 * Cur_buf is the pointer array and count can be any number of required
53 * buffers, the page-fitting arithmetic is done entirely within this funciton.
54 *
55 * returns: number of buffers allocated
56 * locks: must only be called with zfcp_data.config_sema taken
57 */
58static int
59zfcp_qdio_buffers_enqueue(struct qdio_buffer **cur_buf, int count)
60{
61 int buf_pos;
62 int qdio_buffers_per_page;
63 int page_pos = 0;
64 struct qdio_buffer *first_in_page = NULL;
65
66 qdio_buffers_per_page = PAGE_SIZE / sizeof (struct qdio_buffer);
67 ZFCP_LOG_TRACE("buffers_per_page=%d\n", qdio_buffers_per_page);
68
69 for (buf_pos = 0; buf_pos < count; buf_pos++) {
70 if (page_pos == 0) {
71 cur_buf[buf_pos] = (struct qdio_buffer *)
72 get_zeroed_page(GFP_KERNEL);
73 if (cur_buf[buf_pos] == NULL) {
74 ZFCP_LOG_INFO("error: allocation of "
75 "QDIO buffer failed \n");
76 goto out;
77 }
78 first_in_page = cur_buf[buf_pos];
79 } else {
80 cur_buf[buf_pos] = first_in_page + page_pos;
81
82 }
83 /* was initialised to zero */
84 page_pos++;
85 page_pos %= qdio_buffers_per_page;
86 }
87 out:
88 return buf_pos;
89}
90
91/*
92 * Frees BUFFER memory for each of the pointers of the struct qdio_buffer array
93 * in the adapter struct cur_buf is the pointer array and count can be any
94 * number of buffers in the array that should be freed starting from buffer 0
95 *
96 * locks: must only be called with zfcp_data.config_sema taken
97 */
98static void
99zfcp_qdio_buffers_dequeue(struct qdio_buffer **cur_buf, int count)
100{
101 int buf_pos;
102 int qdio_buffers_per_page;
103
104 qdio_buffers_per_page = PAGE_SIZE / sizeof (struct qdio_buffer);
105 ZFCP_LOG_TRACE("buffers_per_page=%d\n", qdio_buffers_per_page);
106
107 for (buf_pos = 0; buf_pos < count; buf_pos += qdio_buffers_per_page)
108 free_page((unsigned long) cur_buf[buf_pos]);
109 return;
110}
111
112/* locks: must only be called with zfcp_data.config_sema taken */
113int
114zfcp_qdio_allocate_queues(struct zfcp_adapter *adapter)
115{
116 int buffer_count;
117 int retval = 0;
118
119 buffer_count =
120 zfcp_qdio_buffers_enqueue(&(adapter->request_queue.buffer[0]),
121 QDIO_MAX_BUFFERS_PER_Q);
122 if (buffer_count < QDIO_MAX_BUFFERS_PER_Q) {
123 ZFCP_LOG_DEBUG("only %d QDIO buffers allocated for request "
124 "queue\n", buffer_count);
125 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
126 buffer_count);
127 retval = -ENOMEM;
128 goto out;
129 }
130
131 buffer_count =
132 zfcp_qdio_buffers_enqueue(&(adapter->response_queue.buffer[0]),
133 QDIO_MAX_BUFFERS_PER_Q);
134 if (buffer_count < QDIO_MAX_BUFFERS_PER_Q) {
135 ZFCP_LOG_DEBUG("only %d QDIO buffers allocated for response "
136 "queue", buffer_count);
137 zfcp_qdio_buffers_dequeue(&(adapter->response_queue.buffer[0]),
138 buffer_count);
139 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
140 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
141 QDIO_MAX_BUFFERS_PER_Q);
142 retval = -ENOMEM;
143 goto out;
144 }
145 out:
146 return retval;
147}
148
149/* locks: must only be called with zfcp_data.config_sema taken */
150void
151zfcp_qdio_free_queues(struct zfcp_adapter *adapter)
152{
153 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
154 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
155 QDIO_MAX_BUFFERS_PER_Q);
156
157 ZFCP_LOG_TRACE("freeing response_queue buffers\n");
158 zfcp_qdio_buffers_dequeue(&(adapter->response_queue.buffer[0]),
159 QDIO_MAX_BUFFERS_PER_Q);
160}
161
162int
163zfcp_qdio_allocate(struct zfcp_adapter *adapter)
164{
165 struct qdio_initialize *init_data;
166
167 init_data = &adapter->qdio_init_data;
168
169 init_data->cdev = adapter->ccw_device;
170 init_data->q_format = QDIO_SCSI_QFMT;
171 memcpy(init_data->adapter_name, &adapter->name, 8);
172 init_data->qib_param_field_format = 0;
173 init_data->qib_param_field = NULL;
174 init_data->input_slib_elements = NULL;
175 init_data->output_slib_elements = NULL;
176 init_data->min_input_threshold = ZFCP_MIN_INPUT_THRESHOLD;
177 init_data->max_input_threshold = ZFCP_MAX_INPUT_THRESHOLD;
178 init_data->min_output_threshold = ZFCP_MIN_OUTPUT_THRESHOLD;
179 init_data->max_output_threshold = ZFCP_MAX_OUTPUT_THRESHOLD;
180 init_data->no_input_qs = 1;
181 init_data->no_output_qs = 1;
182 init_data->input_handler = zfcp_qdio_response_handler;
183 init_data->output_handler = zfcp_qdio_request_handler;
184 init_data->int_parm = (unsigned long) adapter;
185 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
186 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
187 init_data->input_sbal_addr_array =
188 (void **) (adapter->response_queue.buffer);
189 init_data->output_sbal_addr_array =
190 (void **) (adapter->request_queue.buffer);
191
192 return qdio_allocate(init_data);
193}
194
195/*
196 * function: zfcp_qdio_handler_error_check
197 *
198 * purpose: called by the response handler to determine error condition
199 *
200 * returns: error flag
201 *
202 */
203static inline int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200204zfcp_qdio_handler_error_check(struct zfcp_adapter *adapter, unsigned int status,
205 unsigned int qdio_error, unsigned int siga_error,
206 int first_element, int elements_processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int retval = 0;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
211 retval = -EIO;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
214 "qdio_error=0x%x, siga_error=0x%x)\n",
215 status, qdio_error, siga_error);
216
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200217 zfcp_hba_dbf_event_qdio(adapter, status, qdio_error, siga_error,
218 first_element, elements_processed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /*
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200220 * Restarting IO on the failed adapter from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Since we have been using this adapter, it is save to assume
222 * that it is not failed but recoverable. The card seems to
223 * report link-up events by self-initiated queue shutdown.
224 * That is why we need to clear the the link-down flag
225 * which is set again in case we have missed by a mile.
226 */
227 zfcp_erp_adapter_reopen(
228 adapter,
229 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
230 ZFCP_STATUS_COMMON_ERP_FAILED);
231 }
232 return retval;
233}
234
235/*
236 * function: zfcp_qdio_request_handler
237 *
238 * purpose: is called by QDIO layer for completed SBALs in request queue
239 *
240 * returns: (void)
241 */
242static void
243zfcp_qdio_request_handler(struct ccw_device *ccw_device,
244 unsigned int status,
245 unsigned int qdio_error,
246 unsigned int siga_error,
247 unsigned int queue_number,
248 int first_element,
249 int elements_processed,
250 unsigned long int_parm)
251{
252 struct zfcp_adapter *adapter;
253 struct zfcp_qdio_queue *queue;
254
255 adapter = (struct zfcp_adapter *) int_parm;
256 queue = &adapter->request_queue;
257
258 ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
259 zfcp_get_busid_by_adapter(adapter),
260 first_element, elements_processed);
261
262 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200263 siga_error, first_element,
264 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto out;
266 /*
267 * we stored address of struct zfcp_adapter data structure
268 * associated with irq in int_parm
269 */
270
271 /* cleanup all SBALs being program-owned now */
272 zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
273
274 /* increase free space in outbound queue */
275 atomic_add(elements_processed, &queue->free_count);
276 ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
277 wake_up(&adapter->request_wq);
278 ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
279 elements_processed, atomic_read(&queue->free_count));
280 out:
281 return;
282}
283
284/*
285 * function: zfcp_qdio_response_handler
286 *
287 * purpose: is called by QDIO layer for completed SBALs in response queue
288 *
289 * returns: (void)
290 */
291static void
292zfcp_qdio_response_handler(struct ccw_device *ccw_device,
293 unsigned int status,
294 unsigned int qdio_error,
295 unsigned int siga_error,
296 unsigned int queue_number,
297 int first_element,
298 int elements_processed,
299 unsigned long int_parm)
300{
301 struct zfcp_adapter *adapter;
302 struct zfcp_qdio_queue *queue;
303 int buffer_index;
304 int i;
305 struct qdio_buffer *buffer;
306 int retval = 0;
307 u8 count;
308 u8 start;
309 volatile struct qdio_buffer_element *buffere = NULL;
310 int buffere_index;
311
312 adapter = (struct zfcp_adapter *) int_parm;
313 queue = &adapter->response_queue;
314
315 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200316 siga_error, first_element,
317 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out;
319
320 /*
321 * we stored address of struct zfcp_adapter data structure
322 * associated with irq in int_parm
323 */
324
325 buffere = &(queue->buffer[first_element]->element[0]);
326 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
327 /*
328 * go through all SBALs from input queue currently
329 * returned by QDIO layer
330 */
331
332 for (i = 0; i < elements_processed; i++) {
333
334 buffer_index = first_element + i;
335 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
336 buffer = queue->buffer[buffer_index];
337
338 /* go through all SBALEs of SBAL */
339 for (buffere_index = 0;
340 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
341 buffere_index++) {
342
343 /* look for QDIO request identifiers in SB */
344 buffere = &buffer->element[buffere_index];
345 retval = zfcp_qdio_reqid_check(adapter,
346 (void *) buffere->addr);
347
348 if (retval) {
349 ZFCP_LOG_NORMAL("bug: unexpected inbound "
350 "packet on adapter %s "
351 "(reqid=0x%lx, "
352 "first_element=%d, "
353 "elements_processed=%d)\n",
354 zfcp_get_busid_by_adapter(adapter),
355 (unsigned long) buffere->addr,
356 first_element,
357 elements_processed);
358 ZFCP_LOG_NORMAL("hex dump of inbound buffer "
359 "at address %p "
360 "(buffer_index=%d, "
361 "buffere_index=%d)\n", buffer,
362 buffer_index, buffere_index);
363 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
364 (char *) buffer, SBAL_SIZE);
365 }
366 /*
367 * A single used SBALE per inbound SBALE has been
368 * implemented by QDIO so far. Hope they will
369 * do some optimisation. Will need to change to
370 * unlikely() then.
371 */
372 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
373 break;
374 };
375
376 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
377 ZFCP_LOG_NORMAL("bug: End of inbound data "
378 "not marked!\n");
379 }
380 }
381
382 /*
383 * put range of SBALs back to response queue
384 * (including SBALs which have already been free before)
385 */
386 count = atomic_read(&queue->free_count) + elements_processed;
387 start = queue->free_index;
388
389 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
390 "queue_no=%i, index_in_queue=%i, count=%i, "
391 "buffers=0x%lx\n",
392 zfcp_get_busid_by_adapter(adapter),
393 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
394 0, start, count, (unsigned long) &queue->buffer[start]);
395
396 retval = do_QDIO(ccw_device,
397 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
398 0, start, count, NULL);
399
400 if (unlikely(retval)) {
401 atomic_set(&queue->free_count, count);
402 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
403 "queues may be down "
404 "(count=%d, start=%d, retval=%d)\n",
405 count, start, retval);
406 } else {
407 queue->free_index += count;
408 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
409 atomic_set(&queue->free_count, 0);
410 ZFCP_LOG_TRACE("%i buffers enqueued to response "
411 "queue at position %i\n", count, start);
412 }
413 out:
414 return;
415}
416
417/*
418 * function: zfcp_qdio_reqid_check
419 *
420 * purpose: checks for valid reqids or unsolicited status
421 *
422 * returns: 0 - valid request id or unsolicited status
423 * !0 - otherwise
424 */
425int
426zfcp_qdio_reqid_check(struct zfcp_adapter *adapter, void *sbale_addr)
427{
428 struct zfcp_fsf_req *fsf_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* invalid (per convention used in this driver) */
431 if (unlikely(!sbale_addr)) {
432 ZFCP_LOG_NORMAL("bug: invalid reqid\n");
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 /* valid request id and thus (hopefully :) valid fsf_req address */
437 fsf_req = (struct zfcp_fsf_req *) sbale_addr;
438
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200439 /* serialize with zfcp_fsf_req_dismiss_all */
440 spin_lock(&adapter->fsf_req_list_lock);
441 if (list_empty(&adapter->fsf_req_list_head)) {
442 spin_unlock(&adapter->fsf_req_list_lock);
443 return 0;
444 }
445 list_del(&fsf_req->list);
446 atomic_dec(&adapter->fsf_reqs_active);
447 spin_unlock(&adapter->fsf_req_list_lock);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (unlikely(adapter != fsf_req->adapter)) {
450 ZFCP_LOG_NORMAL("bug: invalid reqid (fsf_req=%p, "
451 "fsf_req->adapter=%p, adapter=%p)\n",
452 fsf_req, fsf_req->adapter, adapter);
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 /* finish the FSF request */
457 zfcp_fsf_req_complete(fsf_req);
Andreas Herrmann1db2c9c2005-06-13 13:20:35 +0200458
459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/**
463 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
464 * @queue: queue from which SBALE should be returned
465 * @sbal: specifies number of SBAL in queue
466 * @sbale: specifes number of SBALE in SBAL
467 */
468static inline volatile struct qdio_buffer_element *
469zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
470{
471 return &queue->buffer[sbal]->element[sbale];
472}
473
474/**
475 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
476 * a struct zfcp_fsf_req
477 */
478inline volatile struct qdio_buffer_element *
479zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
480{
481 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
482 sbal, sbale);
483}
484
485/**
486 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
487 * a struct zfcp_fsf_req
488 */
489static inline volatile struct qdio_buffer_element *
490zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
491{
492 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
493 sbal, sbale);
494}
495
496/**
497 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
498 * a struct zfcp_fsf_req
499 */
500inline volatile struct qdio_buffer_element *
501zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
502{
503 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
504 fsf_req->sbale_curr);
505}
506
507/**
508 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
509 * on the request_queue for a struct zfcp_fsf_req
510 * @fsf_req: the number of the last SBAL that can be used is stored herein
511 * @max_sbals: used to pass an upper limit for the number of SBALs
512 *
513 * Note: We can assume at least one free SBAL in the request_queue when called.
514 */
515static inline void
516zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
517{
518 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
519 count = min(count, max_sbals);
520 fsf_req->sbal_last = fsf_req->sbal_first;
521 fsf_req->sbal_last += (count - 1);
522 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
523}
524
525/**
526 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
527 * request
528 * @fsf_req: zfcp_fsf_req to be processed
529 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
530 *
531 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
532 */
533static inline volatile struct qdio_buffer_element *
534zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
535{
536 volatile struct qdio_buffer_element *sbale;
537
538 /* set last entry flag in current SBALE of current SBAL */
539 sbale = zfcp_qdio_sbale_curr(fsf_req);
540 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
541
542 /* don't exceed last allowed SBAL */
543 if (fsf_req->sbal_curr == fsf_req->sbal_last)
544 return NULL;
545
546 /* set chaining flag in first SBALE of current SBAL */
547 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
548 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
549
550 /* calculate index of next SBAL */
551 fsf_req->sbal_curr++;
552 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
553
554 /* keep this requests number of SBALs up-to-date */
555 fsf_req->sbal_number++;
556
557 /* start at first SBALE of new SBAL */
558 fsf_req->sbale_curr = 0;
559
560 /* set storage-block type for new SBAL */
561 sbale = zfcp_qdio_sbale_curr(fsf_req);
562 sbale->flags |= sbtype;
563
564 return sbale;
565}
566
567/**
568 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
569 */
570static inline volatile struct qdio_buffer_element *
571zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
572{
573 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
574 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
575
576 fsf_req->sbale_curr++;
577
578 return zfcp_qdio_sbale_curr(fsf_req);
579}
580
581/**
582 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
583 * with zero from
584 */
585static inline int
586zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
587{
588 struct qdio_buffer **buf = queue->buffer;
589 int curr = first;
590 int count = 0;
591
592 for(;;) {
593 curr %= QDIO_MAX_BUFFERS_PER_Q;
594 count++;
595 memset(buf[curr], 0, sizeof(struct qdio_buffer));
596 if (curr == last)
597 break;
598 curr++;
599 }
600 return count;
601}
602
603
604/**
605 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
606 */
607static inline int
608zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
609{
610 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
611 fsf_req->sbal_first, fsf_req->sbal_curr);
612}
613
614
615/**
616 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
617 * on request_queue
618 */
619static inline void
620zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
621 void *addr, int length)
622{
623 volatile struct qdio_buffer_element *sbale;
624
625 sbale = zfcp_qdio_sbale_curr(fsf_req);
626 sbale->addr = addr;
627 sbale->length = length;
628}
629
630/**
631 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
632 * @fsf_req: request to be processed
633 * @sbtype: SBALE flags
634 * @start_addr: address of memory segment
635 * @total_length: length of memory segment
636 *
637 * Alignment and length of the segment determine how many SBALEs are needed
638 * for the memory segment.
639 */
640static inline int
641zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
642 void *start_addr, unsigned long total_length)
643{
644 unsigned long remaining, length;
645 void *addr;
646
647 /* split segment up heeding page boundaries */
648 for (addr = start_addr, remaining = total_length; remaining > 0;
649 addr += length, remaining -= length) {
650 /* get next free SBALE for new piece */
651 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
652 /* no SBALE left, clean up and leave */
653 zfcp_qdio_sbals_wipe(fsf_req);
654 return -EINVAL;
655 }
656 /* calculate length of new piece */
657 length = min(remaining,
658 (PAGE_SIZE - ((unsigned long) addr &
659 (PAGE_SIZE - 1))));
660 /* fill current SBALE with calculated piece */
661 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
662 }
663 return total_length;
664}
665
666
667/**
668 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
669 * @fsf_req: request to be processed
670 * @sbtype: SBALE flags
671 * @sg: scatter-gather list
672 * @sg_count: number of elements in scatter-gather list
673 * @max_sbals: upper bound for number of SBALs to be used
674 */
675inline int
676zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
677 struct scatterlist *sg, int sg_count, int max_sbals)
678{
679 int sg_index;
680 struct scatterlist *sg_segment;
681 int retval;
682 volatile struct qdio_buffer_element *sbale;
683 int bytes = 0;
684
685 /* figure out last allowed SBAL */
686 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
687
688 /* set storage-block type for current SBAL */
689 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
690 sbale->flags |= sbtype;
691
692 /* process all segements of scatter-gather list */
693 for (sg_index = 0, sg_segment = sg, bytes = 0;
694 sg_index < sg_count;
695 sg_index++, sg_segment++) {
696 retval = zfcp_qdio_sbals_from_segment(
697 fsf_req,
698 sbtype,
699 zfcp_sg_to_address(sg_segment),
700 sg_segment->length);
701 if (retval < 0) {
702 bytes = retval;
703 goto out;
704 } else
705 bytes += retval;
706 }
707 /* assume that no other SBALEs are to follow in the same SBAL */
708 sbale = zfcp_qdio_sbale_curr(fsf_req);
709 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
710out:
711 return bytes;
712}
713
714
715/**
716 * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
717 * @fsf_req: request to be processed
718 * @sbtype: SBALE flags
719 * @buffer: data buffer
720 * @length: length of buffer
721 * @max_sbals: upper bound for number of SBALs to be used
722 */
723static inline int
724zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
725 void *buffer, unsigned long length, int max_sbals)
726{
727 struct scatterlist sg_segment;
728
729 zfcp_address_to_sg(buffer, &sg_segment);
730 sg_segment.length = length;
731
732 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
733 max_sbals);
734}
735
736
737/**
738 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
739 * @fsf_req: request to be processed
740 * @sbtype: SBALE flags
741 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
742 * to fill SBALs
743 */
744inline int
745zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
746 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
747{
748 if (scsi_cmnd->use_sg) {
749 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
750 (struct scatterlist *)
751 scsi_cmnd->request_buffer,
752 scsi_cmnd->use_sg,
753 ZFCP_MAX_SBALS_PER_REQ);
754 } else {
755 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
756 scsi_cmnd->request_buffer,
757 scsi_cmnd->request_bufflen,
758 ZFCP_MAX_SBALS_PER_REQ);
759 }
760}
761
762/**
763 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
764 */
765int
766zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
767 struct zfcp_fsf_req *fsf_req)
768{
769 int new_distance_from_int;
770 int pci_pos;
771 volatile struct qdio_buffer_element *sbale;
772
773 new_distance_from_int = req_queue->distance_from_int +
774 fsf_req->sbal_number;
775
776 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
777 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
778 pci_pos = fsf_req->sbal_first;
779 pci_pos += fsf_req->sbal_number;
780 pci_pos -= new_distance_from_int;
781 pci_pos -= 1;
782 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
783 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
784 sbale->flags |= SBAL_FLAGS0_PCI;
785 }
786 return new_distance_from_int;
787}
788
789/*
790 * function: zfcp_zero_sbals
791 *
792 * purpose: zeros specified range of SBALs
793 *
794 * returns:
795 */
796void
797zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
798{
799 int cur_pos;
800 int index;
801
802 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
803 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
804 memset(buf[index], 0, sizeof (struct qdio_buffer));
805 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
806 index, buf[index]);
807 }
808}
809
810#undef ZFCP_LOG_AREA