[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / s390 / scsi / zfcp_qdio.c
blob06e862d7bc90075ff320fb34a463767be68ae0c2
1 /*
2 * linux/drivers/s390/scsi/zfcp_qdio.c
4 * FCP adapter driver for IBM eServer zSeries
6 * QDIO related routines
8 * (C) Copyright IBM Corp. 2002, 2004
10 * Authors:
11 * Martin Peschke <mpeschke@de.ibm.com>
12 * Raimund Schroeder <raimund.schroeder@de.ibm.com>
13 * Wolfgang Taphorn
14 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 * Andreas Herrmann <aherrman@de.ibm.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
20 * any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #define ZFCP_QDIO_C_REVISION "$Revision: 1.20 $"
34 #include "zfcp_ext.h"
36 static inline void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *, int);
37 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_get
38 (struct zfcp_qdio_queue *, int, int);
39 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_resp
40 (struct zfcp_fsf_req *, int, int);
41 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbal_chain
42 (struct zfcp_fsf_req *, unsigned long);
43 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_next
44 (struct zfcp_fsf_req *, unsigned long);
45 static inline int zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *, int, int);
46 static inline int zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *);
47 static inline void zfcp_qdio_sbale_fill
48 (struct zfcp_fsf_req *, unsigned long, void *, int);
49 static inline int zfcp_qdio_sbals_from_segment
50 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long);
51 static inline int zfcp_qdio_sbals_from_buffer
52 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long, int);
54 static qdio_handler_t zfcp_qdio_request_handler;
55 static qdio_handler_t zfcp_qdio_response_handler;
56 static int zfcp_qdio_handler_error_check(struct zfcp_adapter *,
57 unsigned int,
58 unsigned int, unsigned int);
60 #define ZFCP_LOG_AREA ZFCP_LOG_AREA_QDIO
63 * Allocates BUFFER memory to each of the pointers of the qdio_buffer_t
64 * array in the adapter struct.
65 * Cur_buf is the pointer array and count can be any number of required
66 * buffers, the page-fitting arithmetic is done entirely within this funciton.
68 * returns: number of buffers allocated
69 * locks: must only be called with zfcp_data.config_sema taken
71 static int
72 zfcp_qdio_buffers_enqueue(struct qdio_buffer **cur_buf, int count)
74 int buf_pos;
75 int qdio_buffers_per_page;
76 int page_pos = 0;
77 struct qdio_buffer *first_in_page = NULL;
79 qdio_buffers_per_page = PAGE_SIZE / sizeof (struct qdio_buffer);
80 ZFCP_LOG_TRACE("buffers_per_page=%d\n", qdio_buffers_per_page);
82 for (buf_pos = 0; buf_pos < count; buf_pos++) {
83 if (page_pos == 0) {
84 cur_buf[buf_pos] = (struct qdio_buffer *)
85 get_zeroed_page(GFP_KERNEL);
86 if (cur_buf[buf_pos] == NULL) {
87 ZFCP_LOG_INFO("error: allocation of "
88 "QDIO buffer failed \n");
89 goto out;
91 first_in_page = cur_buf[buf_pos];
92 } else {
93 cur_buf[buf_pos] = first_in_page + page_pos;
96 /* was initialised to zero */
97 page_pos++;
98 page_pos %= qdio_buffers_per_page;
100 out:
101 return buf_pos;
105 * Frees BUFFER memory for each of the pointers of the struct qdio_buffer array
106 * in the adapter struct cur_buf is the pointer array and count can be any
107 * number of buffers in the array that should be freed starting from buffer 0
109 * locks: must only be called with zfcp_data.config_sema taken
111 static void
112 zfcp_qdio_buffers_dequeue(struct qdio_buffer **cur_buf, int count)
114 int buf_pos;
115 int qdio_buffers_per_page;
117 qdio_buffers_per_page = PAGE_SIZE / sizeof (struct qdio_buffer);
118 ZFCP_LOG_TRACE("buffers_per_page=%d\n", qdio_buffers_per_page);
120 for (buf_pos = 0; buf_pos < count; buf_pos += qdio_buffers_per_page)
121 free_page((unsigned long) cur_buf[buf_pos]);
122 return;
125 /* locks: must only be called with zfcp_data.config_sema taken */
127 zfcp_qdio_allocate_queues(struct zfcp_adapter *adapter)
129 int buffer_count;
130 int retval = 0;
132 buffer_count =
133 zfcp_qdio_buffers_enqueue(&(adapter->request_queue.buffer[0]),
134 QDIO_MAX_BUFFERS_PER_Q);
135 if (buffer_count < QDIO_MAX_BUFFERS_PER_Q) {
136 ZFCP_LOG_DEBUG("only %d QDIO buffers allocated for request "
137 "queue\n", buffer_count);
138 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
139 buffer_count);
140 retval = -ENOMEM;
141 goto out;
144 buffer_count =
145 zfcp_qdio_buffers_enqueue(&(adapter->response_queue.buffer[0]),
146 QDIO_MAX_BUFFERS_PER_Q);
147 if (buffer_count < QDIO_MAX_BUFFERS_PER_Q) {
148 ZFCP_LOG_DEBUG("only %d QDIO buffers allocated for response "
149 "queue", buffer_count);
150 zfcp_qdio_buffers_dequeue(&(adapter->response_queue.buffer[0]),
151 buffer_count);
152 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
153 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
154 QDIO_MAX_BUFFERS_PER_Q);
155 retval = -ENOMEM;
156 goto out;
158 out:
159 return retval;
162 /* locks: must only be called with zfcp_data.config_sema taken */
163 void
164 zfcp_qdio_free_queues(struct zfcp_adapter *adapter)
166 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
167 zfcp_qdio_buffers_dequeue(&(adapter->request_queue.buffer[0]),
168 QDIO_MAX_BUFFERS_PER_Q);
170 ZFCP_LOG_TRACE("freeing response_queue buffers\n");
171 zfcp_qdio_buffers_dequeue(&(adapter->response_queue.buffer[0]),
172 QDIO_MAX_BUFFERS_PER_Q);
176 zfcp_qdio_allocate(struct zfcp_adapter *adapter)
178 struct qdio_initialize *init_data;
180 init_data = &adapter->qdio_init_data;
182 init_data->cdev = adapter->ccw_device;
183 init_data->q_format = QDIO_SCSI_QFMT;
184 memcpy(init_data->adapter_name, &adapter->name, 8);
185 init_data->qib_param_field_format = 0;
186 init_data->qib_param_field = NULL;
187 init_data->input_slib_elements = NULL;
188 init_data->output_slib_elements = NULL;
189 init_data->min_input_threshold = ZFCP_MIN_INPUT_THRESHOLD;
190 init_data->max_input_threshold = ZFCP_MAX_INPUT_THRESHOLD;
191 init_data->min_output_threshold = ZFCP_MIN_OUTPUT_THRESHOLD;
192 init_data->max_output_threshold = ZFCP_MAX_OUTPUT_THRESHOLD;
193 init_data->no_input_qs = 1;
194 init_data->no_output_qs = 1;
195 init_data->input_handler = zfcp_qdio_response_handler;
196 init_data->output_handler = zfcp_qdio_request_handler;
197 init_data->int_parm = (unsigned long) adapter;
198 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
199 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
200 init_data->input_sbal_addr_array =
201 (void **) (adapter->response_queue.buffer);
202 init_data->output_sbal_addr_array =
203 (void **) (adapter->request_queue.buffer);
205 return qdio_allocate(init_data);
209 * function: zfcp_qdio_handler_error_check
211 * purpose: called by the response handler to determine error condition
213 * returns: error flag
216 static inline int
217 zfcp_qdio_handler_error_check(struct zfcp_adapter *adapter,
218 unsigned int status,
219 unsigned int qdio_error, unsigned int siga_error)
221 int retval = 0;
223 if (ZFCP_LOG_CHECK(ZFCP_LOG_LEVEL_TRACE)) {
224 if (status & QDIO_STATUS_INBOUND_INT) {
225 ZFCP_LOG_TRACE("status is"
226 " QDIO_STATUS_INBOUND_INT \n");
228 if (status & QDIO_STATUS_OUTBOUND_INT) {
229 ZFCP_LOG_TRACE("status is"
230 " QDIO_STATUS_OUTBOUND_INT \n");
232 } // if (ZFCP_LOG_CHECK(ZFCP_LOG_LEVEL_TRACE))
233 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
234 retval = -EIO;
236 ZFCP_LOG_FLAGS(1, "QDIO_STATUS_LOOK_FOR_ERROR \n");
238 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
239 "qdio_error=0x%x, siga_error=0x%x)\n",
240 status, qdio_error, siga_error);
242 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION) {
243 ZFCP_LOG_FLAGS(2,
244 "QDIO_STATUS_ACTIVATE_CHECK_CONDITION\n");
246 if (status & QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR) {
247 ZFCP_LOG_FLAGS(2,
248 "QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR\n");
250 if (status & QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR) {
251 ZFCP_LOG_FLAGS(2,
252 "QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR\n");
255 if (siga_error & QDIO_SIGA_ERROR_ACCESS_EXCEPTION) {
256 ZFCP_LOG_FLAGS(2, "QDIO_SIGA_ERROR_ACCESS_EXCEPTION\n");
259 if (siga_error & QDIO_SIGA_ERROR_B_BIT_SET) {
260 ZFCP_LOG_FLAGS(2, "QDIO_SIGA_ERROR_B_BIT_SET\n");
263 switch (qdio_error) {
264 case 0:
265 ZFCP_LOG_FLAGS(3, "QDIO_OK");
266 break;
267 case SLSB_P_INPUT_ERROR:
268 ZFCP_LOG_FLAGS(1, "SLSB_P_INPUT_ERROR\n");
269 break;
270 case SLSB_P_OUTPUT_ERROR:
271 ZFCP_LOG_FLAGS(1, "SLSB_P_OUTPUT_ERROR\n");
272 break;
273 default:
274 ZFCP_LOG_NORMAL("bug: unknown QDIO error 0x%x\n",
275 qdio_error);
276 break;
278 /* Restarting IO on the failed adapter from scratch */
279 debug_text_event(adapter->erp_dbf, 1, "qdio_err");
281 * Since we have been using this adapter, it is save to assume
282 * that it is not failed but recoverable. The card seems to
283 * report link-up events by self-initiated queue shutdown.
284 * That is why we need to clear the the link-down flag
285 * which is set again in case we have missed by a mile.
287 zfcp_erp_adapter_reopen(
288 adapter,
289 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
290 ZFCP_STATUS_COMMON_ERP_FAILED);
292 return retval;
296 * function: zfcp_qdio_request_handler
298 * purpose: is called by QDIO layer for completed SBALs in request queue
300 * returns: (void)
302 static void
303 zfcp_qdio_request_handler(struct ccw_device *ccw_device,
304 unsigned int status,
305 unsigned int qdio_error,
306 unsigned int siga_error,
307 unsigned int queue_number,
308 int first_element,
309 int elements_processed,
310 unsigned long int_parm)
312 struct zfcp_adapter *adapter;
313 struct zfcp_qdio_queue *queue;
315 adapter = (struct zfcp_adapter *) int_parm;
316 queue = &adapter->request_queue;
318 ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
319 zfcp_get_busid_by_adapter(adapter),
320 first_element, elements_processed);
322 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
323 siga_error)))
324 goto out;
326 * we stored address of struct zfcp_adapter data structure
327 * associated with irq in int_parm
330 /* cleanup all SBALs being program-owned now */
331 zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
333 /* increase free space in outbound queue */
334 atomic_add(elements_processed, &queue->free_count);
335 ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
336 wake_up(&adapter->request_wq);
337 ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
338 elements_processed, atomic_read(&queue->free_count));
339 out:
340 return;
344 * function: zfcp_qdio_response_handler
346 * purpose: is called by QDIO layer for completed SBALs in response queue
348 * returns: (void)
350 static void
351 zfcp_qdio_response_handler(struct ccw_device *ccw_device,
352 unsigned int status,
353 unsigned int qdio_error,
354 unsigned int siga_error,
355 unsigned int queue_number,
356 int first_element,
357 int elements_processed,
358 unsigned long int_parm)
360 struct zfcp_adapter *adapter;
361 struct zfcp_qdio_queue *queue;
362 int buffer_index;
363 int i;
364 struct qdio_buffer *buffer;
365 int retval = 0;
366 u8 count;
367 u8 start;
368 volatile struct qdio_buffer_element *buffere = NULL;
369 int buffere_index;
371 adapter = (struct zfcp_adapter *) int_parm;
372 queue = &adapter->response_queue;
374 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
375 siga_error)))
376 goto out;
379 * we stored address of struct zfcp_adapter data structure
380 * associated with irq in int_parm
383 buffere = &(queue->buffer[first_element]->element[0]);
384 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
386 * go through all SBALs from input queue currently
387 * returned by QDIO layer
390 for (i = 0; i < elements_processed; i++) {
392 buffer_index = first_element + i;
393 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
394 buffer = queue->buffer[buffer_index];
396 /* go through all SBALEs of SBAL */
397 for (buffere_index = 0;
398 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
399 buffere_index++) {
401 /* look for QDIO request identifiers in SB */
402 buffere = &buffer->element[buffere_index];
403 retval = zfcp_qdio_reqid_check(adapter,
404 (void *) buffere->addr);
406 if (retval) {
407 ZFCP_LOG_NORMAL("bug: unexpected inbound "
408 "packet on adapter %s "
409 "(reqid=0x%lx, "
410 "first_element=%d, "
411 "elements_processed=%d)\n",
412 zfcp_get_busid_by_adapter(adapter),
413 (unsigned long) buffere->addr,
414 first_element,
415 elements_processed);
416 ZFCP_LOG_NORMAL("hex dump of inbound buffer "
417 "at address %p "
418 "(buffer_index=%d, "
419 "buffere_index=%d)\n", buffer,
420 buffer_index, buffere_index);
421 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
422 (char *) buffer, SBAL_SIZE);
425 * A single used SBALE per inbound SBALE has been
426 * implemented by QDIO so far. Hope they will
427 * do some optimisation. Will need to change to
428 * unlikely() then.
430 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
431 break;
434 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
435 ZFCP_LOG_NORMAL("bug: End of inbound data "
436 "not marked!\n");
441 * put range of SBALs back to response queue
442 * (including SBALs which have already been free before)
444 count = atomic_read(&queue->free_count) + elements_processed;
445 start = queue->free_index;
447 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
448 "queue_no=%i, index_in_queue=%i, count=%i, "
449 "buffers=0x%lx\n",
450 zfcp_get_busid_by_adapter(adapter),
451 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
452 0, start, count, (unsigned long) &queue->buffer[start]);
454 retval = do_QDIO(ccw_device,
455 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
456 0, start, count, NULL);
458 if (unlikely(retval)) {
459 atomic_set(&queue->free_count, count);
460 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
461 "queues may be down "
462 "(count=%d, start=%d, retval=%d)\n",
463 count, start, retval);
464 } else {
465 queue->free_index += count;
466 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
467 atomic_set(&queue->free_count, 0);
468 ZFCP_LOG_TRACE("%i buffers enqueued to response "
469 "queue at position %i\n", count, start);
471 out:
472 return;
476 * function: zfcp_qdio_reqid_check
478 * purpose: checks for valid reqids or unsolicited status
480 * returns: 0 - valid request id or unsolicited status
481 * !0 - otherwise
484 zfcp_qdio_reqid_check(struct zfcp_adapter *adapter, void *sbale_addr)
486 struct zfcp_fsf_req *fsf_req;
487 int retval = 0;
489 /* invalid (per convention used in this driver) */
490 if (unlikely(!sbale_addr)) {
491 ZFCP_LOG_NORMAL("bug: invalid reqid\n");
492 retval = -EINVAL;
493 goto out;
496 /* valid request id and thus (hopefully :) valid fsf_req address */
497 fsf_req = (struct zfcp_fsf_req *) sbale_addr;
499 if (unlikely(adapter != fsf_req->adapter)) {
500 ZFCP_LOG_NORMAL("bug: invalid reqid (fsf_req=%p, "
501 "fsf_req->adapter=%p, adapter=%p)\n",
502 fsf_req, fsf_req->adapter, adapter);
503 retval = -EINVAL;
504 goto out;
507 ZFCP_LOG_TRACE("fsf_req at %p, QTCB at %p\n", fsf_req, fsf_req->qtcb);
508 if (likely(fsf_req->qtcb)) {
509 ZFCP_LOG_TRACE("hex dump of QTCB:\n");
510 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_TRACE, (char *) fsf_req->qtcb,
511 sizeof(struct fsf_qtcb));
514 /* finish the FSF request */
515 zfcp_fsf_req_complete(fsf_req);
516 out:
517 return retval;
521 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
522 * @queue: queue from which SBALE should be returned
523 * @sbal: specifies number of SBAL in queue
524 * @sbale: specifes number of SBALE in SBAL
526 static inline volatile struct qdio_buffer_element *
527 zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
529 return &queue->buffer[sbal]->element[sbale];
533 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
534 * a struct zfcp_fsf_req
536 inline volatile struct qdio_buffer_element *
537 zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
539 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
540 sbal, sbale);
544 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
545 * a struct zfcp_fsf_req
547 static inline volatile struct qdio_buffer_element *
548 zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
550 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
551 sbal, sbale);
555 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
556 * a struct zfcp_fsf_req
558 inline volatile struct qdio_buffer_element *
559 zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
561 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
562 fsf_req->sbale_curr);
566 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
567 * on the request_queue for a struct zfcp_fsf_req
568 * @fsf_req: the number of the last SBAL that can be used is stored herein
569 * @max_sbals: used to pass an upper limit for the number of SBALs
571 * Note: We can assume at least one free SBAL in the request_queue when called.
573 static inline void
574 zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
576 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
577 count = min(count, max_sbals);
578 fsf_req->sbal_last = fsf_req->sbal_first;
579 fsf_req->sbal_last += (count - 1);
580 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
584 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
585 * request
586 * @fsf_req: zfcp_fsf_req to be processed
587 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
589 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
591 static inline volatile struct qdio_buffer_element *
592 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
594 volatile struct qdio_buffer_element *sbale;
596 /* set last entry flag in current SBALE of current SBAL */
597 sbale = zfcp_qdio_sbale_curr(fsf_req);
598 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
600 /* don't exceed last allowed SBAL */
601 if (fsf_req->sbal_curr == fsf_req->sbal_last)
602 return NULL;
604 /* set chaining flag in first SBALE of current SBAL */
605 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
606 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
608 /* calculate index of next SBAL */
609 fsf_req->sbal_curr++;
610 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
612 /* keep this requests number of SBALs up-to-date */
613 fsf_req->sbal_number++;
615 /* start at first SBALE of new SBAL */
616 fsf_req->sbale_curr = 0;
618 /* set storage-block type for new SBAL */
619 sbale = zfcp_qdio_sbale_curr(fsf_req);
620 sbale->flags |= sbtype;
622 return sbale;
626 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
628 static inline volatile struct qdio_buffer_element *
629 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
631 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
632 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
634 fsf_req->sbale_curr++;
636 return zfcp_qdio_sbale_curr(fsf_req);
640 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
641 * with zero from
643 static inline int
644 zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
646 struct qdio_buffer **buf = queue->buffer;
647 int curr = first;
648 int count = 0;
650 for(;;) {
651 curr %= QDIO_MAX_BUFFERS_PER_Q;
652 count++;
653 memset(buf[curr], 0, sizeof(struct qdio_buffer));
654 if (curr == last)
655 break;
656 curr++;
658 return count;
663 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
665 static inline int
666 zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
668 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
669 fsf_req->sbal_first, fsf_req->sbal_curr);
674 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
675 * on request_queue
677 static inline void
678 zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
679 void *addr, int length)
681 volatile struct qdio_buffer_element *sbale;
683 sbale = zfcp_qdio_sbale_curr(fsf_req);
684 sbale->addr = addr;
685 sbale->length = length;
689 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
690 * @fsf_req: request to be processed
691 * @sbtype: SBALE flags
692 * @start_addr: address of memory segment
693 * @total_length: length of memory segment
695 * Alignment and length of the segment determine how many SBALEs are needed
696 * for the memory segment.
698 static inline int
699 zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
700 void *start_addr, unsigned long total_length)
702 unsigned long remaining, length;
703 void *addr;
705 /* split segment up heeding page boundaries */
706 for (addr = start_addr, remaining = total_length; remaining > 0;
707 addr += length, remaining -= length) {
708 /* get next free SBALE for new piece */
709 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
710 /* no SBALE left, clean up and leave */
711 zfcp_qdio_sbals_wipe(fsf_req);
712 return -EINVAL;
714 /* calculate length of new piece */
715 length = min(remaining,
716 (PAGE_SIZE - ((unsigned long) addr &
717 (PAGE_SIZE - 1))));
718 /* fill current SBALE with calculated piece */
719 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
721 return total_length;
726 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
727 * @fsf_req: request to be processed
728 * @sbtype: SBALE flags
729 * @sg: scatter-gather list
730 * @sg_count: number of elements in scatter-gather list
731 * @max_sbals: upper bound for number of SBALs to be used
733 inline int
734 zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
735 struct scatterlist *sg, int sg_count, int max_sbals)
737 int sg_index;
738 struct scatterlist *sg_segment;
739 int retval;
740 volatile struct qdio_buffer_element *sbale;
741 int bytes = 0;
743 /* figure out last allowed SBAL */
744 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
746 /* set storage-block type for current SBAL */
747 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
748 sbale->flags |= sbtype;
750 /* process all segements of scatter-gather list */
751 for (sg_index = 0, sg_segment = sg, bytes = 0;
752 sg_index < sg_count;
753 sg_index++, sg_segment++) {
754 retval = zfcp_qdio_sbals_from_segment(
755 fsf_req,
756 sbtype,
757 zfcp_sg_to_address(sg_segment),
758 sg_segment->length);
759 if (retval < 0) {
760 bytes = retval;
761 goto out;
762 } else
763 bytes += retval;
765 /* assume that no other SBALEs are to follow in the same SBAL */
766 sbale = zfcp_qdio_sbale_curr(fsf_req);
767 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
768 out:
769 return bytes;
774 * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
775 * @fsf_req: request to be processed
776 * @sbtype: SBALE flags
777 * @buffer: data buffer
778 * @length: length of buffer
779 * @max_sbals: upper bound for number of SBALs to be used
781 static inline int
782 zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
783 void *buffer, unsigned long length, int max_sbals)
785 struct scatterlist sg_segment;
787 zfcp_address_to_sg(buffer, &sg_segment);
788 sg_segment.length = length;
790 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
791 max_sbals);
796 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
797 * @fsf_req: request to be processed
798 * @sbtype: SBALE flags
799 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
800 * to fill SBALs
802 inline int
803 zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
804 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
806 if (scsi_cmnd->use_sg) {
807 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
808 (struct scatterlist *)
809 scsi_cmnd->request_buffer,
810 scsi_cmnd->use_sg,
811 ZFCP_MAX_SBALS_PER_REQ);
812 } else {
813 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
814 scsi_cmnd->request_buffer,
815 scsi_cmnd->request_bufflen,
816 ZFCP_MAX_SBALS_PER_REQ);
821 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
824 zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
825 struct zfcp_fsf_req *fsf_req)
827 int new_distance_from_int;
828 int pci_pos;
829 volatile struct qdio_buffer_element *sbale;
831 new_distance_from_int = req_queue->distance_from_int +
832 fsf_req->sbal_number;
834 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
835 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
836 pci_pos = fsf_req->sbal_first;
837 pci_pos += fsf_req->sbal_number;
838 pci_pos -= new_distance_from_int;
839 pci_pos -= 1;
840 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
841 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
842 sbale->flags |= SBAL_FLAGS0_PCI;
844 return new_distance_from_int;
848 * function: zfcp_zero_sbals
850 * purpose: zeros specified range of SBALs
852 * returns:
854 void
855 zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
857 int cur_pos;
858 int index;
860 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
861 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
862 memset(buf[index], 0, sizeof (struct qdio_buffer));
863 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
864 index, buf[index]);
868 #undef ZFCP_LOG_AREA