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