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