[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / s390 / scsi / zfcp_qdio.c
blob24e16ec331d9e5760a00b59972e6fa5ab3009175
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");
233 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
234 retval = -EIO;
236 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
237 "qdio_error=0x%x, siga_error=0x%x)\n",
238 status, qdio_error, siga_error);
240 /* Restarting IO on the failed adapter from scratch */
241 debug_text_event(adapter->erp_dbf, 1, "qdio_err");
243 * Since we have been using this adapter, it is save to assume
244 * that it is not failed but recoverable. The card seems to
245 * report link-up events by self-initiated queue shutdown.
246 * That is why we need to clear the the link-down flag
247 * which is set again in case we have missed by a mile.
249 zfcp_erp_adapter_reopen(
250 adapter,
251 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
252 ZFCP_STATUS_COMMON_ERP_FAILED);
254 return retval;
258 * function: zfcp_qdio_request_handler
260 * purpose: is called by QDIO layer for completed SBALs in request queue
262 * returns: (void)
264 static void
265 zfcp_qdio_request_handler(struct ccw_device *ccw_device,
266 unsigned int status,
267 unsigned int qdio_error,
268 unsigned int siga_error,
269 unsigned int queue_number,
270 int first_element,
271 int elements_processed,
272 unsigned long int_parm)
274 struct zfcp_adapter *adapter;
275 struct zfcp_qdio_queue *queue;
277 adapter = (struct zfcp_adapter *) int_parm;
278 queue = &adapter->request_queue;
280 ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
281 zfcp_get_busid_by_adapter(adapter),
282 first_element, elements_processed);
284 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
285 siga_error)))
286 goto out;
288 * we stored address of struct zfcp_adapter data structure
289 * associated with irq in int_parm
292 /* cleanup all SBALs being program-owned now */
293 zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
295 /* increase free space in outbound queue */
296 atomic_add(elements_processed, &queue->free_count);
297 ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
298 wake_up(&adapter->request_wq);
299 ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
300 elements_processed, atomic_read(&queue->free_count));
301 out:
302 return;
306 * function: zfcp_qdio_response_handler
308 * purpose: is called by QDIO layer for completed SBALs in response queue
310 * returns: (void)
312 static void
313 zfcp_qdio_response_handler(struct ccw_device *ccw_device,
314 unsigned int status,
315 unsigned int qdio_error,
316 unsigned int siga_error,
317 unsigned int queue_number,
318 int first_element,
319 int elements_processed,
320 unsigned long int_parm)
322 struct zfcp_adapter *adapter;
323 struct zfcp_qdio_queue *queue;
324 int buffer_index;
325 int i;
326 struct qdio_buffer *buffer;
327 int retval = 0;
328 u8 count;
329 u8 start;
330 volatile struct qdio_buffer_element *buffere = NULL;
331 int buffere_index;
333 adapter = (struct zfcp_adapter *) int_parm;
334 queue = &adapter->response_queue;
336 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
337 siga_error)))
338 goto out;
341 * we stored address of struct zfcp_adapter data structure
342 * associated with irq in int_parm
345 buffere = &(queue->buffer[first_element]->element[0]);
346 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
348 * go through all SBALs from input queue currently
349 * returned by QDIO layer
352 for (i = 0; i < elements_processed; i++) {
354 buffer_index = first_element + i;
355 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
356 buffer = queue->buffer[buffer_index];
358 /* go through all SBALEs of SBAL */
359 for (buffere_index = 0;
360 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
361 buffere_index++) {
363 /* look for QDIO request identifiers in SB */
364 buffere = &buffer->element[buffere_index];
365 retval = zfcp_qdio_reqid_check(adapter,
366 (void *) buffere->addr);
368 if (retval) {
369 ZFCP_LOG_NORMAL("bug: unexpected inbound "
370 "packet on adapter %s "
371 "(reqid=0x%lx, "
372 "first_element=%d, "
373 "elements_processed=%d)\n",
374 zfcp_get_busid_by_adapter(adapter),
375 (unsigned long) buffere->addr,
376 first_element,
377 elements_processed);
378 ZFCP_LOG_NORMAL("hex dump of inbound buffer "
379 "at address %p "
380 "(buffer_index=%d, "
381 "buffere_index=%d)\n", buffer,
382 buffer_index, buffere_index);
383 ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
384 (char *) buffer, SBAL_SIZE);
387 * A single used SBALE per inbound SBALE has been
388 * implemented by QDIO so far. Hope they will
389 * do some optimisation. Will need to change to
390 * unlikely() then.
392 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
393 break;
396 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
397 ZFCP_LOG_NORMAL("bug: End of inbound data "
398 "not marked!\n");
403 * put range of SBALs back to response queue
404 * (including SBALs which have already been free before)
406 count = atomic_read(&queue->free_count) + elements_processed;
407 start = queue->free_index;
409 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
410 "queue_no=%i, index_in_queue=%i, count=%i, "
411 "buffers=0x%lx\n",
412 zfcp_get_busid_by_adapter(adapter),
413 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
414 0, start, count, (unsigned long) &queue->buffer[start]);
416 retval = do_QDIO(ccw_device,
417 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
418 0, start, count, NULL);
420 if (unlikely(retval)) {
421 atomic_set(&queue->free_count, count);
422 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
423 "queues may be down "
424 "(count=%d, start=%d, retval=%d)\n",
425 count, start, retval);
426 } else {
427 queue->free_index += count;
428 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
429 atomic_set(&queue->free_count, 0);
430 ZFCP_LOG_TRACE("%i buffers enqueued to response "
431 "queue at position %i\n", count, start);
433 out:
434 return;
438 * function: zfcp_qdio_reqid_check
440 * purpose: checks for valid reqids or unsolicited status
442 * returns: 0 - valid request id or unsolicited status
443 * !0 - otherwise
446 zfcp_qdio_reqid_check(struct zfcp_adapter *adapter, void *sbale_addr)
448 struct zfcp_fsf_req *fsf_req;
450 /* invalid (per convention used in this driver) */
451 if (unlikely(!sbale_addr)) {
452 ZFCP_LOG_NORMAL("bug: invalid reqid\n");
453 return -EINVAL;
456 /* valid request id and thus (hopefully :) valid fsf_req address */
457 fsf_req = (struct zfcp_fsf_req *) sbale_addr;
459 /* serialize with zfcp_fsf_req_dismiss_all */
460 spin_lock(&adapter->fsf_req_list_lock);
461 if (list_empty(&adapter->fsf_req_list_head)) {
462 spin_unlock(&adapter->fsf_req_list_lock);
463 return 0;
465 list_del(&fsf_req->list);
466 atomic_dec(&adapter->fsf_reqs_active);
467 spin_unlock(&adapter->fsf_req_list_lock);
469 if (unlikely(adapter != fsf_req->adapter)) {
470 ZFCP_LOG_NORMAL("bug: invalid reqid (fsf_req=%p, "
471 "fsf_req->adapter=%p, adapter=%p)\n",
472 fsf_req, fsf_req->adapter, adapter);
473 return -EINVAL;
476 /* finish the FSF request */
477 zfcp_fsf_req_complete(fsf_req);
479 return 0;
483 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
484 * @queue: queue from which SBALE should be returned
485 * @sbal: specifies number of SBAL in queue
486 * @sbale: specifes number of SBALE in SBAL
488 static inline volatile struct qdio_buffer_element *
489 zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
491 return &queue->buffer[sbal]->element[sbale];
495 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
496 * a struct zfcp_fsf_req
498 inline volatile struct qdio_buffer_element *
499 zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
501 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
502 sbal, sbale);
506 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
507 * a struct zfcp_fsf_req
509 static inline volatile struct qdio_buffer_element *
510 zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
512 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
513 sbal, sbale);
517 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
518 * a struct zfcp_fsf_req
520 inline volatile struct qdio_buffer_element *
521 zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
523 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
524 fsf_req->sbale_curr);
528 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
529 * on the request_queue for a struct zfcp_fsf_req
530 * @fsf_req: the number of the last SBAL that can be used is stored herein
531 * @max_sbals: used to pass an upper limit for the number of SBALs
533 * Note: We can assume at least one free SBAL in the request_queue when called.
535 static inline void
536 zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
538 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
539 count = min(count, max_sbals);
540 fsf_req->sbal_last = fsf_req->sbal_first;
541 fsf_req->sbal_last += (count - 1);
542 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
546 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
547 * request
548 * @fsf_req: zfcp_fsf_req to be processed
549 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
551 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
553 static inline volatile struct qdio_buffer_element *
554 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
556 volatile struct qdio_buffer_element *sbale;
558 /* set last entry flag in current SBALE of current SBAL */
559 sbale = zfcp_qdio_sbale_curr(fsf_req);
560 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
562 /* don't exceed last allowed SBAL */
563 if (fsf_req->sbal_curr == fsf_req->sbal_last)
564 return NULL;
566 /* set chaining flag in first SBALE of current SBAL */
567 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
568 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
570 /* calculate index of next SBAL */
571 fsf_req->sbal_curr++;
572 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
574 /* keep this requests number of SBALs up-to-date */
575 fsf_req->sbal_number++;
577 /* start at first SBALE of new SBAL */
578 fsf_req->sbale_curr = 0;
580 /* set storage-block type for new SBAL */
581 sbale = zfcp_qdio_sbale_curr(fsf_req);
582 sbale->flags |= sbtype;
584 return sbale;
588 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
590 static inline volatile struct qdio_buffer_element *
591 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
593 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
594 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
596 fsf_req->sbale_curr++;
598 return zfcp_qdio_sbale_curr(fsf_req);
602 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
603 * with zero from
605 static inline int
606 zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
608 struct qdio_buffer **buf = queue->buffer;
609 int curr = first;
610 int count = 0;
612 for(;;) {
613 curr %= QDIO_MAX_BUFFERS_PER_Q;
614 count++;
615 memset(buf[curr], 0, sizeof(struct qdio_buffer));
616 if (curr == last)
617 break;
618 curr++;
620 return count;
625 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
627 static inline int
628 zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
630 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
631 fsf_req->sbal_first, fsf_req->sbal_curr);
636 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
637 * on request_queue
639 static inline void
640 zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
641 void *addr, int length)
643 volatile struct qdio_buffer_element *sbale;
645 sbale = zfcp_qdio_sbale_curr(fsf_req);
646 sbale->addr = addr;
647 sbale->length = length;
651 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
652 * @fsf_req: request to be processed
653 * @sbtype: SBALE flags
654 * @start_addr: address of memory segment
655 * @total_length: length of memory segment
657 * Alignment and length of the segment determine how many SBALEs are needed
658 * for the memory segment.
660 static inline int
661 zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
662 void *start_addr, unsigned long total_length)
664 unsigned long remaining, length;
665 void *addr;
667 /* split segment up heeding page boundaries */
668 for (addr = start_addr, remaining = total_length; remaining > 0;
669 addr += length, remaining -= length) {
670 /* get next free SBALE for new piece */
671 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
672 /* no SBALE left, clean up and leave */
673 zfcp_qdio_sbals_wipe(fsf_req);
674 return -EINVAL;
676 /* calculate length of new piece */
677 length = min(remaining,
678 (PAGE_SIZE - ((unsigned long) addr &
679 (PAGE_SIZE - 1))));
680 /* fill current SBALE with calculated piece */
681 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
683 return total_length;
688 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
689 * @fsf_req: request to be processed
690 * @sbtype: SBALE flags
691 * @sg: scatter-gather list
692 * @sg_count: number of elements in scatter-gather list
693 * @max_sbals: upper bound for number of SBALs to be used
695 inline int
696 zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
697 struct scatterlist *sg, int sg_count, int max_sbals)
699 int sg_index;
700 struct scatterlist *sg_segment;
701 int retval;
702 volatile struct qdio_buffer_element *sbale;
703 int bytes = 0;
705 /* figure out last allowed SBAL */
706 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
708 /* set storage-block type for current SBAL */
709 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
710 sbale->flags |= sbtype;
712 /* process all segements of scatter-gather list */
713 for (sg_index = 0, sg_segment = sg, bytes = 0;
714 sg_index < sg_count;
715 sg_index++, sg_segment++) {
716 retval = zfcp_qdio_sbals_from_segment(
717 fsf_req,
718 sbtype,
719 zfcp_sg_to_address(sg_segment),
720 sg_segment->length);
721 if (retval < 0) {
722 bytes = retval;
723 goto out;
724 } else
725 bytes += retval;
727 /* assume that no other SBALEs are to follow in the same SBAL */
728 sbale = zfcp_qdio_sbale_curr(fsf_req);
729 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
730 out:
731 return bytes;
736 * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
737 * @fsf_req: request to be processed
738 * @sbtype: SBALE flags
739 * @buffer: data buffer
740 * @length: length of buffer
741 * @max_sbals: upper bound for number of SBALs to be used
743 static inline int
744 zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
745 void *buffer, unsigned long length, int max_sbals)
747 struct scatterlist sg_segment;
749 zfcp_address_to_sg(buffer, &sg_segment);
750 sg_segment.length = length;
752 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
753 max_sbals);
758 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
759 * @fsf_req: request to be processed
760 * @sbtype: SBALE flags
761 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
762 * to fill SBALs
764 inline int
765 zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
766 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
768 if (scsi_cmnd->use_sg) {
769 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
770 (struct scatterlist *)
771 scsi_cmnd->request_buffer,
772 scsi_cmnd->use_sg,
773 ZFCP_MAX_SBALS_PER_REQ);
774 } else {
775 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
776 scsi_cmnd->request_buffer,
777 scsi_cmnd->request_bufflen,
778 ZFCP_MAX_SBALS_PER_REQ);
783 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
786 zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
787 struct zfcp_fsf_req *fsf_req)
789 int new_distance_from_int;
790 int pci_pos;
791 volatile struct qdio_buffer_element *sbale;
793 new_distance_from_int = req_queue->distance_from_int +
794 fsf_req->sbal_number;
796 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
797 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
798 pci_pos = fsf_req->sbal_first;
799 pci_pos += fsf_req->sbal_number;
800 pci_pos -= new_distance_from_int;
801 pci_pos -= 1;
802 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
803 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
804 sbale->flags |= SBAL_FLAGS0_PCI;
806 return new_distance_from_int;
810 * function: zfcp_zero_sbals
812 * purpose: zeros specified range of SBALs
814 * returns:
816 void
817 zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
819 int cur_pos;
820 int index;
822 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
823 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
824 memset(buf[index], 0, sizeof (struct qdio_buffer));
825 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
826 index, buf[index]);
830 #undef ZFCP_LOG_AREA