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