blob: e50e6ad4e6cb79b61a710edac9a9e7c5f5f24534 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "zfcp_ext.h"
23
Heiko Carstens4d284ca2007-02-05 21:18:53 +010024static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_get
26 (struct zfcp_qdio_queue *, int, int);
27static inline volatile struct qdio_buffer_element *zfcp_qdio_sbale_resp
28 (struct zfcp_fsf_req *, int, int);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010029static volatile struct qdio_buffer_element *zfcp_qdio_sbal_chain
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 (struct zfcp_fsf_req *, unsigned long);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010031static volatile struct qdio_buffer_element *zfcp_qdio_sbale_next
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 (struct zfcp_fsf_req *, unsigned long);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010033static int zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static inline int zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010035static void zfcp_qdio_sbale_fill
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 (struct zfcp_fsf_req *, unsigned long, void *, int);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010037static int zfcp_qdio_sbals_from_segment
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long);
Heiko Carstens4d284ca2007-02-05 21:18:53 +010039static int zfcp_qdio_sbals_from_buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 (struct zfcp_fsf_req *, unsigned long, void *, unsigned long, int);
41
42static qdio_handler_t zfcp_qdio_request_handler;
43static qdio_handler_t zfcp_qdio_response_handler;
44static int zfcp_qdio_handler_error_check(struct zfcp_adapter *,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020045 unsigned int, unsigned int, unsigned int, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
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 */
58static int
59zfcp_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 */
98static void
99zfcp_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 */
113int
114zfcp_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 */
150void
151zfcp_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
162int
163zfcp_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;
Andreas Herrmann06506d02006-05-22 18:18:19 +0200171 memcpy(init_data->adapter_name, zfcp_get_busid_by_adapter(adapter), 8);
172 ASCEBC(init_data->adapter_name, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100204static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200205zfcp_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int retval = 0;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
212 retval = -EIO;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200218 zfcp_hba_dbf_event_qdio(adapter, status, qdio_error, siga_error,
219 first_element, elements_processed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200221 * Restarting IO on the failed adapter from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * 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 */
243static void
244zfcp_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,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200264 siga_error, first_element,
265 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 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
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200285/**
286 * zfcp_qdio_reqid_check - checks for valid reqids or unsolicited status
287 */
Christof Schmittb03670e2007-05-07 16:35:04 +0200288static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
289 unsigned long req_id)
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200290{
291 struct zfcp_fsf_req *fsf_req;
292 unsigned long flags;
293
294 debug_long_event(adapter->erp_dbf, 4, req_id);
295
296 spin_lock_irqsave(&adapter->req_list_lock, flags);
297 fsf_req = zfcp_reqlist_ismember(adapter, req_id);
298
299 if (!fsf_req) {
300 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
Christof Schmittb03670e2007-05-07 16:35:04 +0200301 panic("error: unknown request id (%ld).\n", req_id);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200302 }
303
304 zfcp_reqlist_remove(adapter, req_id);
305 atomic_dec(&adapter->reqs_active);
306 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
307
308 /* finish the FSF request */
309 zfcp_fsf_req_complete(fsf_req);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/*
313 * function: zfcp_qdio_response_handler
314 *
315 * purpose: is called by QDIO layer for completed SBALs in response queue
316 *
317 * returns: (void)
318 */
319static void
320zfcp_qdio_response_handler(struct ccw_device *ccw_device,
321 unsigned int status,
322 unsigned int qdio_error,
323 unsigned int siga_error,
324 unsigned int queue_number,
325 int first_element,
326 int elements_processed,
327 unsigned long int_parm)
328{
329 struct zfcp_adapter *adapter;
330 struct zfcp_qdio_queue *queue;
331 int buffer_index;
332 int i;
333 struct qdio_buffer *buffer;
334 int retval = 0;
335 u8 count;
336 u8 start;
337 volatile struct qdio_buffer_element *buffere = NULL;
338 int buffere_index;
339
340 adapter = (struct zfcp_adapter *) int_parm;
341 queue = &adapter->response_queue;
342
343 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200344 siga_error, first_element,
345 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto out;
347
348 /*
349 * we stored address of struct zfcp_adapter data structure
350 * associated with irq in int_parm
351 */
352
353 buffere = &(queue->buffer[first_element]->element[0]);
354 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
355 /*
356 * go through all SBALs from input queue currently
357 * returned by QDIO layer
358 */
359
360 for (i = 0; i < elements_processed; i++) {
361
362 buffer_index = first_element + i;
363 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
364 buffer = queue->buffer[buffer_index];
365
366 /* go through all SBALEs of SBAL */
367 for (buffere_index = 0;
368 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
369 buffere_index++) {
370
371 /* look for QDIO request identifiers in SB */
372 buffere = &buffer->element[buffere_index];
Christof Schmittb03670e2007-05-07 16:35:04 +0200373 zfcp_qdio_reqid_check(adapter,
374 (unsigned long) buffere->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
377 * A single used SBALE per inbound SBALE has been
378 * implemented by QDIO so far. Hope they will
379 * do some optimisation. Will need to change to
380 * unlikely() then.
381 */
382 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
383 break;
384 };
385
386 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
387 ZFCP_LOG_NORMAL("bug: End of inbound data "
388 "not marked!\n");
389 }
390 }
391
392 /*
393 * put range of SBALs back to response queue
394 * (including SBALs which have already been free before)
395 */
396 count = atomic_read(&queue->free_count) + elements_processed;
397 start = queue->free_index;
398
399 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
400 "queue_no=%i, index_in_queue=%i, count=%i, "
401 "buffers=0x%lx\n",
402 zfcp_get_busid_by_adapter(adapter),
403 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
404 0, start, count, (unsigned long) &queue->buffer[start]);
405
406 retval = do_QDIO(ccw_device,
407 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
408 0, start, count, NULL);
409
410 if (unlikely(retval)) {
411 atomic_set(&queue->free_count, count);
412 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
413 "queues may be down "
414 "(count=%d, start=%d, retval=%d)\n",
415 count, start, retval);
416 } else {
417 queue->free_index += count;
418 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
419 atomic_set(&queue->free_count, 0);
420 ZFCP_LOG_TRACE("%i buffers enqueued to response "
421 "queue at position %i\n", count, start);
422 }
423 out:
424 return;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
429 * @queue: queue from which SBALE should be returned
430 * @sbal: specifies number of SBAL in queue
431 * @sbale: specifes number of SBALE in SBAL
432 */
433static inline volatile struct qdio_buffer_element *
434zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
435{
436 return &queue->buffer[sbal]->element[sbale];
437}
438
439/**
440 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
441 * a struct zfcp_fsf_req
442 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100443volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
445{
446 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
447 sbal, sbale);
448}
449
450/**
451 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
452 * a struct zfcp_fsf_req
453 */
454static inline volatile struct qdio_buffer_element *
455zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
456{
457 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
458 sbal, sbale);
459}
460
461/**
462 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
463 * a struct zfcp_fsf_req
464 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100465volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
467{
468 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
469 fsf_req->sbale_curr);
470}
471
472/**
473 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
474 * on the request_queue for a struct zfcp_fsf_req
475 * @fsf_req: the number of the last SBAL that can be used is stored herein
476 * @max_sbals: used to pass an upper limit for the number of SBALs
477 *
478 * Note: We can assume at least one free SBAL in the request_queue when called.
479 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100480static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
482{
483 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
484 count = min(count, max_sbals);
485 fsf_req->sbal_last = fsf_req->sbal_first;
486 fsf_req->sbal_last += (count - 1);
487 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
488}
489
490/**
491 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
492 * request
493 * @fsf_req: zfcp_fsf_req to be processed
494 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
495 *
496 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
497 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100498static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
500{
501 volatile struct qdio_buffer_element *sbale;
502
503 /* set last entry flag in current SBALE of current SBAL */
504 sbale = zfcp_qdio_sbale_curr(fsf_req);
505 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
506
507 /* don't exceed last allowed SBAL */
508 if (fsf_req->sbal_curr == fsf_req->sbal_last)
509 return NULL;
510
511 /* set chaining flag in first SBALE of current SBAL */
512 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
513 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
514
515 /* calculate index of next SBAL */
516 fsf_req->sbal_curr++;
517 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
518
519 /* keep this requests number of SBALs up-to-date */
520 fsf_req->sbal_number++;
521
522 /* start at first SBALE of new SBAL */
523 fsf_req->sbale_curr = 0;
524
525 /* set storage-block type for new SBAL */
526 sbale = zfcp_qdio_sbale_curr(fsf_req);
527 sbale->flags |= sbtype;
528
529 return sbale;
530}
531
532/**
533 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
534 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100535static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
537{
538 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
539 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
540
541 fsf_req->sbale_curr++;
542
543 return zfcp_qdio_sbale_curr(fsf_req);
544}
545
546/**
547 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
548 * with zero from
549 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100550static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
552{
553 struct qdio_buffer **buf = queue->buffer;
554 int curr = first;
555 int count = 0;
556
557 for(;;) {
558 curr %= QDIO_MAX_BUFFERS_PER_Q;
559 count++;
560 memset(buf[curr], 0, sizeof(struct qdio_buffer));
561 if (curr == last)
562 break;
563 curr++;
564 }
565 return count;
566}
567
568
569/**
570 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
571 */
572static inline int
573zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
574{
575 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
576 fsf_req->sbal_first, fsf_req->sbal_curr);
577}
578
579
580/**
581 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
582 * on request_queue
583 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100584static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
586 void *addr, int length)
587{
588 volatile struct qdio_buffer_element *sbale;
589
590 sbale = zfcp_qdio_sbale_curr(fsf_req);
591 sbale->addr = addr;
592 sbale->length = length;
593}
594
595/**
596 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
597 * @fsf_req: request to be processed
598 * @sbtype: SBALE flags
599 * @start_addr: address of memory segment
600 * @total_length: length of memory segment
601 *
602 * Alignment and length of the segment determine how many SBALEs are needed
603 * for the memory segment.
604 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100605static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
607 void *start_addr, unsigned long total_length)
608{
609 unsigned long remaining, length;
610 void *addr;
611
612 /* split segment up heeding page boundaries */
613 for (addr = start_addr, remaining = total_length; remaining > 0;
614 addr += length, remaining -= length) {
615 /* get next free SBALE for new piece */
616 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
617 /* no SBALE left, clean up and leave */
618 zfcp_qdio_sbals_wipe(fsf_req);
619 return -EINVAL;
620 }
621 /* calculate length of new piece */
622 length = min(remaining,
623 (PAGE_SIZE - ((unsigned long) addr &
624 (PAGE_SIZE - 1))));
625 /* fill current SBALE with calculated piece */
626 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
627 }
628 return total_length;
629}
630
631
632/**
633 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
634 * @fsf_req: request to be processed
635 * @sbtype: SBALE flags
636 * @sg: scatter-gather list
637 * @sg_count: number of elements in scatter-gather list
638 * @max_sbals: upper bound for number of SBALs to be used
639 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100640int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
642 struct scatterlist *sg, int sg_count, int max_sbals)
643{
644 int sg_index;
645 struct scatterlist *sg_segment;
646 int retval;
647 volatile struct qdio_buffer_element *sbale;
648 int bytes = 0;
649
650 /* figure out last allowed SBAL */
651 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
652
653 /* set storage-block type for current SBAL */
654 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
655 sbale->flags |= sbtype;
656
657 /* process all segements of scatter-gather list */
658 for (sg_index = 0, sg_segment = sg, bytes = 0;
659 sg_index < sg_count;
660 sg_index++, sg_segment++) {
661 retval = zfcp_qdio_sbals_from_segment(
662 fsf_req,
663 sbtype,
664 zfcp_sg_to_address(sg_segment),
665 sg_segment->length);
666 if (retval < 0) {
667 bytes = retval;
668 goto out;
669 } else
670 bytes += retval;
671 }
672 /* assume that no other SBALEs are to follow in the same SBAL */
673 sbale = zfcp_qdio_sbale_curr(fsf_req);
674 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
675out:
676 return bytes;
677}
678
679
680/**
681 * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
682 * @fsf_req: request to be processed
683 * @sbtype: SBALE flags
684 * @buffer: data buffer
685 * @length: length of buffer
686 * @max_sbals: upper bound for number of SBALs to be used
687 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100688static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
690 void *buffer, unsigned long length, int max_sbals)
691{
692 struct scatterlist sg_segment;
693
694 zfcp_address_to_sg(buffer, &sg_segment);
695 sg_segment.length = length;
696
697 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
698 max_sbals);
699}
700
701
702/**
703 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
704 * @fsf_req: request to be processed
705 * @sbtype: SBALE flags
706 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
707 * to fill SBALs
708 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100709int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
711 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
712{
713 if (scsi_cmnd->use_sg) {
714 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
715 (struct scatterlist *)
716 scsi_cmnd->request_buffer,
717 scsi_cmnd->use_sg,
718 ZFCP_MAX_SBALS_PER_REQ);
719 } else {
720 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
721 scsi_cmnd->request_buffer,
722 scsi_cmnd->request_bufflen,
723 ZFCP_MAX_SBALS_PER_REQ);
724 }
725}
726
727/**
728 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
729 */
730int
731zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
732 struct zfcp_fsf_req *fsf_req)
733{
734 int new_distance_from_int;
735 int pci_pos;
736 volatile struct qdio_buffer_element *sbale;
737
738 new_distance_from_int = req_queue->distance_from_int +
739 fsf_req->sbal_number;
740
741 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
742 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
743 pci_pos = fsf_req->sbal_first;
744 pci_pos += fsf_req->sbal_number;
745 pci_pos -= new_distance_from_int;
746 pci_pos -= 1;
747 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
748 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
749 sbale->flags |= SBAL_FLAGS0_PCI;
750 }
751 return new_distance_from_int;
752}
753
754/*
755 * function: zfcp_zero_sbals
756 *
757 * purpose: zeros specified range of SBALs
758 *
759 * returns:
760 */
761void
762zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
763{
764 int cur_pos;
765 int index;
766
767 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
768 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
769 memset(buf[index], 0, sizeof (struct qdio_buffer));
770 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
771 index, buf[index]);
772 }
773}
774
775#undef ZFCP_LOG_AREA