Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / drivers / s390 / scsi / zfcp_qdio.c
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
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
22 #include "zfcp_ext.h"
23
24 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *, int);
25 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_get
26         (struct zfcp_qdio_queue *, int, int);
27 static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_resp
28         (struct zfcp_fsf_req *, int, int);
29 static volatile struct qdio_buffer_element *zfcp_qdio_sbal_chain
30         (struct zfcp_fsf_req *, unsigned long);
31 static volatile struct qdio_buffer_element *zfcp_qdio_sbale_next
32         (struct zfcp_fsf_req *, unsigned long);
33 static int zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *, int, int);
34 static inline int zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *);
35 static void zfcp_qdio_sbale_fill
36         (struct zfcp_fsf_req *, unsigned long, void *, int);
37 static int zfcp_qdio_sbals_from_segment
38         (struct zfcp_fsf_req *, unsigned long, void *, unsigned long);
39 static int zfcp_qdio_sbals_from_buffer
40         (struct zfcp_fsf_req *, unsigned long, void *, unsigned long, int);
41
42 static qdio_handler_t zfcp_qdio_request_handler;
43 static qdio_handler_t zfcp_qdio_response_handler;
44 static int zfcp_qdio_handler_error_check(struct zfcp_adapter *,
45         unsigned int, unsigned int, unsigned int, int, int);
46
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  */
58 static int
59 zfcp_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  */
98 static void
99 zfcp_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 */
113 int
114 zfcp_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 */
150 void
151 zfcp_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
162 int
163 zfcp_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, zfcp_get_busid_by_adapter(adapter), 8);
172         ASCEBC(init_data->adapter_name, 8);
173         init_data->qib_param_field_format = 0;
174         init_data->qib_param_field = NULL;
175         init_data->input_slib_elements = NULL;
176         init_data->output_slib_elements = NULL;
177         init_data->min_input_threshold = ZFCP_MIN_INPUT_THRESHOLD;
178         init_data->max_input_threshold = ZFCP_MAX_INPUT_THRESHOLD;
179         init_data->min_output_threshold = ZFCP_MIN_OUTPUT_THRESHOLD;
180         init_data->max_output_threshold = ZFCP_MAX_OUTPUT_THRESHOLD;
181         init_data->no_input_qs = 1;
182         init_data->no_output_qs = 1;
183         init_data->input_handler = zfcp_qdio_response_handler;
184         init_data->output_handler = zfcp_qdio_request_handler;
185         init_data->int_parm = (unsigned long) adapter;
186         init_data->flags = QDIO_INBOUND_0COPY_SBALS |
187             QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
188         init_data->input_sbal_addr_array =
189             (void **) (adapter->response_queue.buffer);
190         init_data->output_sbal_addr_array =
191             (void **) (adapter->request_queue.buffer);
192
193         return qdio_allocate(init_data);
194 }
195
196 /*
197  * function:    zfcp_qdio_handler_error_check
198  *
199  * purpose:     called by the response handler to determine error condition
200  *
201  * returns:     error flag
202  *
203  */
204 static int
205 zfcp_qdio_handler_error_check(struct zfcp_adapter *adapter, unsigned int status,
206                               unsigned int qdio_error, unsigned int siga_error,
207                               int first_element, int elements_processed)
208 {
209         int retval = 0;
210
211         if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
212                 retval = -EIO;
213
214                 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
215                               "qdio_error=0x%x, siga_error=0x%x)\n",
216                               status, qdio_error, siga_error);
217
218                 zfcp_hba_dbf_event_qdio(adapter, status, qdio_error, siga_error,
219                                 first_element, elements_processed);
220                /*
221                 * Restarting IO on the failed adapter from scratch.
222                 * Since we have been using this adapter, it is save to assume
223                 * that it is not failed but recoverable. The card seems to
224                 * report link-up events by self-initiated queue shutdown.
225                 * That is why we need to clear the link-down flag
226                 * which is set again in case we have missed by a mile.
227                 */
228                zfcp_erp_adapter_reopen(
229                        adapter, 
230                        ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
231                        ZFCP_STATUS_COMMON_ERP_FAILED);
232         }
233         return retval;
234 }
235
236 /*
237  * function:    zfcp_qdio_request_handler
238  *
239  * purpose:     is called by QDIO layer for completed SBALs in request queue
240  *
241  * returns:     (void)
242  */
243 static void
244 zfcp_qdio_request_handler(struct ccw_device *ccw_device,
245                           unsigned int status,
246                           unsigned int qdio_error,
247                           unsigned int siga_error,
248                           unsigned int queue_number,
249                           int first_element,
250                           int elements_processed,
251                           unsigned long int_parm)
252 {
253         struct zfcp_adapter *adapter;
254         struct zfcp_qdio_queue *queue;
255
256         adapter = (struct zfcp_adapter *) int_parm;
257         queue = &adapter->request_queue;
258
259         ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
260                        zfcp_get_busid_by_adapter(adapter),
261                        first_element, elements_processed);
262
263         if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
264                                                    siga_error, first_element,
265                                                    elements_processed)))
266                 goto out;
267         /*
268          * we stored address of struct zfcp_adapter  data structure
269          * associated with irq in int_parm
270          */
271
272         /* cleanup all SBALs being program-owned now */
273         zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
274
275         /* increase free space in outbound queue */
276         atomic_add(elements_processed, &queue->free_count);
277         ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
278         wake_up(&adapter->request_wq);
279         ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
280                        elements_processed, atomic_read(&queue->free_count));
281  out:
282         return;
283 }
284
285 /**
286  * zfcp_qdio_reqid_check - checks for valid reqids.
287  */
288 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
289                                   unsigned long req_id)
290 {
291         struct zfcp_fsf_req *fsf_req;
292         unsigned long flags;
293
294         debug_long_event(adapter->erp_dbf, 4, req_id);
295
296         spin_lock_irqsave(&adapter->req_list_lock, flags);
297         fsf_req = zfcp_reqlist_find(adapter, req_id);
298
299         if (!fsf_req)
300                 /*
301                  * Unknown request means that we have potentially memory
302                  * corruption and must stop the machine immediatly.
303                  */
304                 panic("error: unknown request id (%ld) on adapter %s.\n",
305                       req_id, zfcp_get_busid_by_adapter(adapter));
306
307         zfcp_reqlist_remove(adapter, fsf_req);
308         atomic_dec(&adapter->reqs_active);
309         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
310
311         /* finish the FSF request */
312         zfcp_fsf_req_complete(fsf_req);
313 }
314
315 /*
316  * function:    zfcp_qdio_response_handler
317  *
318  * purpose:     is called by QDIO layer for completed SBALs in response queue
319  *
320  * returns:     (void)
321  */
322 static void
323 zfcp_qdio_response_handler(struct ccw_device *ccw_device,
324                            unsigned int status,
325                            unsigned int qdio_error,
326                            unsigned int siga_error,
327                            unsigned int queue_number,
328                            int first_element,
329                            int elements_processed,
330                            unsigned long int_parm)
331 {
332         struct zfcp_adapter *adapter;
333         struct zfcp_qdio_queue *queue;
334         int buffer_index;
335         int i;
336         struct qdio_buffer *buffer;
337         int retval = 0;
338         u8 count;
339         u8 start;
340         volatile struct qdio_buffer_element *buffere = NULL;
341         int buffere_index;
342
343         adapter = (struct zfcp_adapter *) int_parm;
344         queue = &adapter->response_queue;
345
346         if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
347                                                    siga_error, first_element,
348                                                    elements_processed)))
349                 goto out;
350
351         /*
352          * we stored address of struct zfcp_adapter  data structure
353          * associated with irq in int_parm
354          */
355
356         buffere = &(queue->buffer[first_element]->element[0]);
357         ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
358         /*
359          * go through all SBALs from input queue currently
360          * returned by QDIO layer
361          */
362
363         for (i = 0; i < elements_processed; i++) {
364
365                 buffer_index = first_element + i;
366                 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
367                 buffer = queue->buffer[buffer_index];
368
369                 /* go through all SBALEs of SBAL */
370                 for (buffere_index = 0;
371                      buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
372                      buffere_index++) {
373
374                         /* look for QDIO request identifiers in SB */
375                         buffere = &buffer->element[buffere_index];
376                         zfcp_qdio_reqid_check(adapter,
377                                               (unsigned long) buffere->addr);
378
379                         /*
380                          * A single used SBALE per inbound SBALE has been
381                          * implemented by QDIO so far. Hope they will
382                          * do some optimisation. Will need to change to
383                          * unlikely() then.
384                          */
385                         if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
386                                 break;
387                 };
388
389                 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
390                         ZFCP_LOG_NORMAL("bug: End of inbound data "
391                                         "not marked!\n");
392                 }
393         }
394
395         /*
396          * put range of SBALs back to response queue
397          * (including SBALs which have already been free before)
398          */
399         count = atomic_read(&queue->free_count) + elements_processed;
400         start = queue->free_index;
401
402         ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
403                        "queue_no=%i, index_in_queue=%i, count=%i, "
404                        "buffers=0x%lx\n",
405                        zfcp_get_busid_by_adapter(adapter),
406                        QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
407                        0, start, count, (unsigned long) &queue->buffer[start]);
408
409         retval = do_QDIO(ccw_device,
410                          QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
411                          0, start, count, NULL);
412
413         if (unlikely(retval)) {
414                 atomic_set(&queue->free_count, count);
415                 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
416                                "queues may be down "
417                                "(count=%d, start=%d, retval=%d)\n",
418                                count, start, retval);
419         } else {
420                 queue->free_index += count;
421                 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
422                 atomic_set(&queue->free_count, 0);
423                 ZFCP_LOG_TRACE("%i buffers enqueued to response "
424                                "queue at position %i\n", count, start);
425         }
426  out:
427         return;
428 }
429
430 /**
431  * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
432  * @queue: queue from which SBALE should be returned
433  * @sbal: specifies number of SBAL in queue
434  * @sbale: specifes number of SBALE in SBAL
435  */
436 static inline volatile struct qdio_buffer_element *
437 zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
438 {
439         return &queue->buffer[sbal]->element[sbale];
440 }
441
442 /**
443  * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
444  *      a struct zfcp_fsf_req
445  */
446 volatile struct qdio_buffer_element *
447 zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
448 {
449         return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
450                                    sbal, sbale);
451 }
452
453 /**
454  * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
455  *      a struct zfcp_fsf_req
456  */
457 static inline volatile struct qdio_buffer_element *
458 zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
459 {
460         return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
461                                    sbal, sbale);
462 }
463
464 /**
465  * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
466  *      a struct zfcp_fsf_req
467  */
468 volatile struct qdio_buffer_element *
469 zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
470 {
471         return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
472                                    fsf_req->sbale_curr);
473 }
474
475 /**
476  * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
477  *      on the request_queue for a struct zfcp_fsf_req
478  * @fsf_req: the number of the last SBAL that can be used is stored herein
479  * @max_sbals: used to pass an upper limit for the number of SBALs
480  *
481  * Note: We can assume at least one free SBAL in the request_queue when called.
482  */
483 static void
484 zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
485 {
486         int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
487         count = min(count, max_sbals);
488         fsf_req->sbal_last  = fsf_req->sbal_first;
489         fsf_req->sbal_last += (count - 1);
490         fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
491 }
492
493 /**
494  * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
495  *      request
496  * @fsf_req: zfcp_fsf_req to be processed
497  * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
498  *
499  * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
500  */
501 static volatile struct qdio_buffer_element *
502 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
503 {
504         volatile struct qdio_buffer_element *sbale;
505
506         /* set last entry flag in current SBALE of current SBAL */
507         sbale = zfcp_qdio_sbale_curr(fsf_req);
508         sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
509
510         /* don't exceed last allowed SBAL */
511         if (fsf_req->sbal_curr == fsf_req->sbal_last)
512                 return NULL;
513
514         /* set chaining flag in first SBALE of current SBAL */
515         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
516         sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
517
518         /* calculate index of next SBAL */
519         fsf_req->sbal_curr++;
520         fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
521
522         /* keep this requests number of SBALs up-to-date */
523         fsf_req->sbal_number++;
524
525         /* start at first SBALE of new SBAL */
526         fsf_req->sbale_curr = 0;
527
528         /* set storage-block type for new SBAL */
529         sbale = zfcp_qdio_sbale_curr(fsf_req);
530         sbale->flags |= sbtype;
531
532         return sbale;
533 }
534
535 /**
536  * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
537  */
538 static volatile struct qdio_buffer_element *
539 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
540 {
541         if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
542                 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
543
544         fsf_req->sbale_curr++;
545
546         return zfcp_qdio_sbale_curr(fsf_req);
547 }
548
549 /**
550  * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
551  *      with zero from
552  */
553 static int
554 zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
555 {
556         struct qdio_buffer **buf = queue->buffer;
557         int curr = first;
558         int count = 0;
559
560         for(;;) {
561                 curr %= QDIO_MAX_BUFFERS_PER_Q;
562                 count++;
563                 memset(buf[curr], 0, sizeof(struct qdio_buffer));
564                 if (curr == last)
565                         break;
566                 curr++;
567         }
568         return count;
569 }
570
571
572 /**
573  * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
574  */
575 static inline int
576 zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
577 {
578         return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
579                                     fsf_req->sbal_first, fsf_req->sbal_curr);
580 }
581
582
583 /**
584  * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
585  *      on request_queue
586  */
587 static void
588 zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
589                      void *addr, int length)
590 {
591         volatile struct qdio_buffer_element *sbale;
592
593         sbale = zfcp_qdio_sbale_curr(fsf_req);
594         sbale->addr = addr;
595         sbale->length = length;
596 }
597
598 /**
599  * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
600  * @fsf_req: request to be processed
601  * @sbtype: SBALE flags
602  * @start_addr: address of memory segment
603  * @total_length: length of memory segment
604  *
605  * Alignment and length of the segment determine how many SBALEs are needed
606  * for the memory segment.
607  */
608 static int
609 zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
610                              void *start_addr, unsigned long total_length)
611 {
612         unsigned long remaining, length;
613         void *addr;
614
615         /* split segment up heeding page boundaries */
616         for (addr = start_addr, remaining = total_length; remaining > 0;
617              addr += length, remaining -= length) {
618                 /* get next free SBALE for new piece */
619                 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
620                         /* no SBALE left, clean up and leave */
621                         zfcp_qdio_sbals_wipe(fsf_req);
622                         return -EINVAL;
623                 }
624                 /* calculate length of new piece */
625                 length = min(remaining,
626                              (PAGE_SIZE - ((unsigned long) addr &
627                                            (PAGE_SIZE - 1))));
628                 /* fill current SBALE with calculated piece */
629                 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
630         }
631         return total_length;
632 }
633
634
635 /**
636  * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
637  * @fsf_req: request to be processed
638  * @sbtype: SBALE flags
639  * @sg: scatter-gather list
640  * @sg_count: number of elements in scatter-gather list
641  * @max_sbals: upper bound for number of SBALs to be used
642  */
643 int
644 zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
645                         struct scatterlist *sg, int sg_count, int max_sbals)
646 {
647         int sg_index;
648         struct scatterlist *sg_segment;
649         int retval;
650         volatile struct qdio_buffer_element *sbale;
651         int bytes = 0;
652
653         /* figure out last allowed SBAL */
654         zfcp_qdio_sbal_limit(fsf_req, max_sbals);
655
656         /* set storage-block type for current SBAL */
657         sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
658         sbale->flags |= sbtype;
659
660         /* process all segements of scatter-gather list */
661         for (sg_index = 0, sg_segment = sg, bytes = 0;
662              sg_index < sg_count;
663              sg_index++, sg_segment++) {
664                 retval = zfcp_qdio_sbals_from_segment(
665                                 fsf_req,
666                                 sbtype,
667                                 zfcp_sg_to_address(sg_segment),
668                                 sg_segment->length);
669                 if (retval < 0) {
670                         bytes = retval;
671                         goto out;
672                 } else
673                         bytes += retval;
674         }
675         /* assume that no other SBALEs are to follow in the same SBAL */
676         sbale = zfcp_qdio_sbale_curr(fsf_req);
677         sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
678 out:
679         return bytes;
680 }
681
682
683 /**
684  * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
685  * @fsf_req: request to be processed
686  * @sbtype: SBALE flags
687  * @buffer: data buffer
688  * @length: length of buffer
689  * @max_sbals: upper bound for number of SBALs to be used
690  */
691 static int
692 zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
693                             void *buffer, unsigned long length, int max_sbals)
694 {
695         struct scatterlist sg_segment;
696
697         zfcp_address_to_sg(buffer, &sg_segment);
698         sg_segment.length = length;
699
700         return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
701                                        max_sbals);
702 }
703
704
705 /**
706  * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
707  * @fsf_req: request to be processed
708  * @sbtype: SBALE flags
709  * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
710  *      to fill SBALs
711  */
712 int
713 zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
714                               unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
715 {
716         if (scsi_cmnd->use_sg) {
717                 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
718                                                (struct scatterlist *)
719                                                scsi_cmnd->request_buffer,
720                                                scsi_cmnd->use_sg,
721                                                ZFCP_MAX_SBALS_PER_REQ);
722         } else {
723                 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
724                                                    scsi_cmnd->request_buffer,
725                                                    scsi_cmnd->request_bufflen,
726                                                    ZFCP_MAX_SBALS_PER_REQ);
727         }
728 }
729
730 /**
731  * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
732  */
733 int
734 zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
735                         struct zfcp_fsf_req *fsf_req)
736 {
737         int new_distance_from_int;
738         int pci_pos;
739         volatile struct qdio_buffer_element *sbale;
740
741         new_distance_from_int = req_queue->distance_from_int +
742                 fsf_req->sbal_number;
743
744         if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
745                 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
746                 pci_pos  = fsf_req->sbal_first;
747                 pci_pos += fsf_req->sbal_number;
748                 pci_pos -= new_distance_from_int;
749                 pci_pos -= 1;
750                 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
751                 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
752                 sbale->flags |= SBAL_FLAGS0_PCI;
753         }
754         return new_distance_from_int;
755 }
756
757 /*
758  * function:    zfcp_zero_sbals
759  *
760  * purpose:     zeros specified range of SBALs
761  *
762  * returns:
763  */
764 void
765 zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
766 {
767         int cur_pos;
768         int index;
769
770         for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
771                 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
772                 memset(buf[index], 0, sizeof (struct qdio_buffer));
773                 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
774                                index, buf[index]);
775         }
776 }
777
778 #undef ZFCP_LOG_AREA