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