blob: 81daa8204bfeeda745cedd8838093b52a40d850e [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static qdio_handler_t zfcp_qdio_request_handler;
41static qdio_handler_t zfcp_qdio_response_handler;
42static int zfcp_qdio_handler_error_check(struct zfcp_adapter *,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020043 unsigned int, unsigned int, unsigned int, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define ZFCP_LOG_AREA ZFCP_LOG_AREA_QDIO
46
47/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * Frees BUFFER memory for each of the pointers of the struct qdio_buffer array
Swen Schilligb4e44592007-07-18 10:55:13 +020049 * in the adapter struct sbuf is the pointer array.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 *
51 * locks: must only be called with zfcp_data.config_sema taken
52 */
53static void
Swen Schilligb4e44592007-07-18 10:55:13 +020054zfcp_qdio_buffers_dequeue(struct qdio_buffer **sbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Swen Schilligb4e44592007-07-18 10:55:13 +020056 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Swen Schilligb4e44592007-07-18 10:55:13 +020058 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE)
59 free_page((unsigned long) sbuf[pos]);
60}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Swen Schilligb4e44592007-07-18 10:55:13 +020062/*
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
66 *
67 * returns: zero on success else -ENOMEM
68 * locks: must only be called with zfcp_data.config_sema taken
69 */
70static int
71zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbuf)
72{
73 int pos;
74
75 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
76 sbuf[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
77 if (!sbuf[pos]) {
78 zfcp_qdio_buffers_dequeue(sbuf);
79 return -ENOMEM;
80 }
81 }
82 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
83 if (pos % QBUFF_PER_PAGE)
84 sbuf[pos] = sbuf[pos - 1] + 1;
85 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/* locks: must only be called with zfcp_data.config_sema taken */
89int
90zfcp_qdio_allocate_queues(struct zfcp_adapter *adapter)
91{
Swen Schilligb4e44592007-07-18 10:55:13 +020092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Swen Schilligb4e44592007-07-18 10:55:13 +020094 ret = zfcp_qdio_buffers_enqueue(adapter->request_queue.buffer);
95 if (ret)
96 return ret;
97 return zfcp_qdio_buffers_enqueue(adapter->response_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100/* locks: must only be called with zfcp_data.config_sema taken */
101void
102zfcp_qdio_free_queues(struct zfcp_adapter *adapter)
103{
104 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
Swen Schilligb4e44592007-07-18 10:55:13 +0200105 zfcp_qdio_buffers_dequeue(adapter->request_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 ZFCP_LOG_TRACE("freeing response_queue buffers\n");
Swen Schilligb4e44592007-07-18 10:55:13 +0200108 zfcp_qdio_buffers_dequeue(adapter->response_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111int
112zfcp_qdio_allocate(struct zfcp_adapter *adapter)
113{
114 struct qdio_initialize *init_data;
115
116 init_data = &adapter->qdio_init_data;
117
118 init_data->cdev = adapter->ccw_device;
119 init_data->q_format = QDIO_SCSI_QFMT;
Andreas Herrmann06506d02006-05-22 18:18:19 +0200120 memcpy(init_data->adapter_name, zfcp_get_busid_by_adapter(adapter), 8);
121 ASCEBC(init_data->adapter_name, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 init_data->qib_param_field_format = 0;
123 init_data->qib_param_field = NULL;
124 init_data->input_slib_elements = NULL;
125 init_data->output_slib_elements = NULL;
126 init_data->min_input_threshold = ZFCP_MIN_INPUT_THRESHOLD;
127 init_data->max_input_threshold = ZFCP_MAX_INPUT_THRESHOLD;
128 init_data->min_output_threshold = ZFCP_MIN_OUTPUT_THRESHOLD;
129 init_data->max_output_threshold = ZFCP_MAX_OUTPUT_THRESHOLD;
130 init_data->no_input_qs = 1;
131 init_data->no_output_qs = 1;
132 init_data->input_handler = zfcp_qdio_response_handler;
133 init_data->output_handler = zfcp_qdio_request_handler;
134 init_data->int_parm = (unsigned long) adapter;
135 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
136 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
137 init_data->input_sbal_addr_array =
138 (void **) (adapter->response_queue.buffer);
139 init_data->output_sbal_addr_array =
140 (void **) (adapter->request_queue.buffer);
141
142 return qdio_allocate(init_data);
143}
144
145/*
146 * function: zfcp_qdio_handler_error_check
147 *
148 * purpose: called by the response handler to determine error condition
149 *
150 * returns: error flag
151 *
152 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100153static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200154zfcp_qdio_handler_error_check(struct zfcp_adapter *adapter, unsigned int status,
155 unsigned int qdio_error, unsigned int siga_error,
156 int first_element, int elements_processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int retval = 0;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
161 retval = -EIO;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
164 "qdio_error=0x%x, siga_error=0x%x)\n",
165 status, qdio_error, siga_error);
166
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200167 zfcp_hba_dbf_event_qdio(adapter, status, qdio_error, siga_error,
168 first_element, elements_processed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200170 * Restarting IO on the failed adapter from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Since we have been using this adapter, it is save to assume
172 * that it is not failed but recoverable. The card seems to
173 * report link-up events by self-initiated queue shutdown.
Michael Opdenacker59c51592007-05-09 08:57:56 +0200174 * That is why we need to clear the link-down flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * which is set again in case we have missed by a mile.
176 */
177 zfcp_erp_adapter_reopen(
178 adapter,
179 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
180 ZFCP_STATUS_COMMON_ERP_FAILED);
181 }
182 return retval;
183}
184
185/*
186 * function: zfcp_qdio_request_handler
187 *
188 * purpose: is called by QDIO layer for completed SBALs in request queue
189 *
190 * returns: (void)
191 */
192static void
193zfcp_qdio_request_handler(struct ccw_device *ccw_device,
194 unsigned int status,
195 unsigned int qdio_error,
196 unsigned int siga_error,
197 unsigned int queue_number,
198 int first_element,
199 int elements_processed,
200 unsigned long int_parm)
201{
202 struct zfcp_adapter *adapter;
203 struct zfcp_qdio_queue *queue;
204
205 adapter = (struct zfcp_adapter *) int_parm;
206 queue = &adapter->request_queue;
207
208 ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
209 zfcp_get_busid_by_adapter(adapter),
210 first_element, elements_processed);
211
212 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200213 siga_error, first_element,
214 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto out;
216 /*
217 * we stored address of struct zfcp_adapter data structure
218 * associated with irq in int_parm
219 */
220
221 /* cleanup all SBALs being program-owned now */
222 zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
223
224 /* increase free space in outbound queue */
225 atomic_add(elements_processed, &queue->free_count);
226 ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
227 wake_up(&adapter->request_wq);
228 ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
229 elements_processed, atomic_read(&queue->free_count));
230 out:
231 return;
232}
233
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200234/**
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200235 * zfcp_qdio_reqid_check - checks for valid reqids.
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200236 */
Christof Schmittb03670e2007-05-07 16:35:04 +0200237static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
238 unsigned long req_id)
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200239{
240 struct zfcp_fsf_req *fsf_req;
241 unsigned long flags;
242
243 debug_long_event(adapter->erp_dbf, 4, req_id);
244
245 spin_lock_irqsave(&adapter->req_list_lock, flags);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200246 fsf_req = zfcp_reqlist_find(adapter, req_id);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200247
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200248 if (!fsf_req)
249 /*
250 * Unknown request means that we have potentially memory
251 * corruption and must stop the machine immediatly.
252 */
253 panic("error: unknown request id (%ld) on adapter %s.\n",
254 req_id, zfcp_get_busid_by_adapter(adapter));
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200255
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200256 zfcp_reqlist_remove(adapter, fsf_req);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200257 atomic_dec(&adapter->reqs_active);
258 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
259
260 /* finish the FSF request */
261 zfcp_fsf_req_complete(fsf_req);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * function: zfcp_qdio_response_handler
266 *
267 * purpose: is called by QDIO layer for completed SBALs in response queue
268 *
269 * returns: (void)
270 */
271static void
272zfcp_qdio_response_handler(struct ccw_device *ccw_device,
273 unsigned int status,
274 unsigned int qdio_error,
275 unsigned int siga_error,
276 unsigned int queue_number,
277 int first_element,
278 int elements_processed,
279 unsigned long int_parm)
280{
281 struct zfcp_adapter *adapter;
282 struct zfcp_qdio_queue *queue;
283 int buffer_index;
284 int i;
285 struct qdio_buffer *buffer;
286 int retval = 0;
287 u8 count;
288 u8 start;
289 volatile struct qdio_buffer_element *buffere = NULL;
290 int buffere_index;
291
292 adapter = (struct zfcp_adapter *) int_parm;
293 queue = &adapter->response_queue;
294
295 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200296 siga_error, first_element,
297 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out;
299
300 /*
301 * we stored address of struct zfcp_adapter data structure
302 * associated with irq in int_parm
303 */
304
305 buffere = &(queue->buffer[first_element]->element[0]);
306 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
307 /*
308 * go through all SBALs from input queue currently
309 * returned by QDIO layer
310 */
311
312 for (i = 0; i < elements_processed; i++) {
313
314 buffer_index = first_element + i;
315 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
316 buffer = queue->buffer[buffer_index];
317
318 /* go through all SBALEs of SBAL */
319 for (buffere_index = 0;
320 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
321 buffere_index++) {
322
323 /* look for QDIO request identifiers in SB */
324 buffere = &buffer->element[buffere_index];
Christof Schmittb03670e2007-05-07 16:35:04 +0200325 zfcp_qdio_reqid_check(adapter,
326 (unsigned long) buffere->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /*
329 * A single used SBALE per inbound SBALE has been
330 * implemented by QDIO so far. Hope they will
331 * do some optimisation. Will need to change to
332 * unlikely() then.
333 */
334 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
335 break;
336 };
337
338 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
339 ZFCP_LOG_NORMAL("bug: End of inbound data "
340 "not marked!\n");
341 }
342 }
343
344 /*
345 * put range of SBALs back to response queue
346 * (including SBALs which have already been free before)
347 */
348 count = atomic_read(&queue->free_count) + elements_processed;
349 start = queue->free_index;
350
351 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
352 "queue_no=%i, index_in_queue=%i, count=%i, "
353 "buffers=0x%lx\n",
354 zfcp_get_busid_by_adapter(adapter),
355 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
356 0, start, count, (unsigned long) &queue->buffer[start]);
357
358 retval = do_QDIO(ccw_device,
359 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
360 0, start, count, NULL);
361
362 if (unlikely(retval)) {
363 atomic_set(&queue->free_count, count);
364 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
365 "queues may be down "
366 "(count=%d, start=%d, retval=%d)\n",
367 count, start, retval);
368 } else {
369 queue->free_index += count;
370 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
371 atomic_set(&queue->free_count, 0);
372 ZFCP_LOG_TRACE("%i buffers enqueued to response "
373 "queue at position %i\n", count, start);
374 }
375 out:
376 return;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
380 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
381 * @queue: queue from which SBALE should be returned
382 * @sbal: specifies number of SBAL in queue
383 * @sbale: specifes number of SBALE in SBAL
384 */
385static inline volatile struct qdio_buffer_element *
386zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
387{
388 return &queue->buffer[sbal]->element[sbale];
389}
390
391/**
392 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
393 * a struct zfcp_fsf_req
394 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100395volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
397{
398 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
399 sbal, sbale);
400}
401
402/**
403 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
404 * a struct zfcp_fsf_req
405 */
406static inline volatile struct qdio_buffer_element *
407zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
408{
409 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
410 sbal, sbale);
411}
412
413/**
414 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
415 * a struct zfcp_fsf_req
416 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100417volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
419{
420 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
421 fsf_req->sbale_curr);
422}
423
424/**
425 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
426 * on the request_queue for a struct zfcp_fsf_req
427 * @fsf_req: the number of the last SBAL that can be used is stored herein
428 * @max_sbals: used to pass an upper limit for the number of SBALs
429 *
430 * Note: We can assume at least one free SBAL in the request_queue when called.
431 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100432static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
434{
435 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
436 count = min(count, max_sbals);
437 fsf_req->sbal_last = fsf_req->sbal_first;
438 fsf_req->sbal_last += (count - 1);
439 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
440}
441
442/**
443 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
444 * request
445 * @fsf_req: zfcp_fsf_req to be processed
446 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
447 *
448 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
449 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100450static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
452{
453 volatile struct qdio_buffer_element *sbale;
454
455 /* set last entry flag in current SBALE of current SBAL */
456 sbale = zfcp_qdio_sbale_curr(fsf_req);
457 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
458
459 /* don't exceed last allowed SBAL */
460 if (fsf_req->sbal_curr == fsf_req->sbal_last)
461 return NULL;
462
463 /* set chaining flag in first SBALE of current SBAL */
464 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
465 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
466
467 /* calculate index of next SBAL */
468 fsf_req->sbal_curr++;
469 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
470
471 /* keep this requests number of SBALs up-to-date */
472 fsf_req->sbal_number++;
473
474 /* start at first SBALE of new SBAL */
475 fsf_req->sbale_curr = 0;
476
477 /* set storage-block type for new SBAL */
478 sbale = zfcp_qdio_sbale_curr(fsf_req);
479 sbale->flags |= sbtype;
480
481 return sbale;
482}
483
484/**
485 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
486 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100487static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
489{
490 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
491 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
492
493 fsf_req->sbale_curr++;
494
495 return zfcp_qdio_sbale_curr(fsf_req);
496}
497
498/**
499 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
500 * with zero from
501 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100502static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
504{
505 struct qdio_buffer **buf = queue->buffer;
506 int curr = first;
507 int count = 0;
508
509 for(;;) {
510 curr %= QDIO_MAX_BUFFERS_PER_Q;
511 count++;
512 memset(buf[curr], 0, sizeof(struct qdio_buffer));
513 if (curr == last)
514 break;
515 curr++;
516 }
517 return count;
518}
519
520
521/**
522 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
523 */
524static inline int
525zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
526{
527 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
528 fsf_req->sbal_first, fsf_req->sbal_curr);
529}
530
531
532/**
533 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
534 * on request_queue
535 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100536static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
538 void *addr, int length)
539{
540 volatile struct qdio_buffer_element *sbale;
541
542 sbale = zfcp_qdio_sbale_curr(fsf_req);
543 sbale->addr = addr;
544 sbale->length = length;
545}
546
547/**
548 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
549 * @fsf_req: request to be processed
550 * @sbtype: SBALE flags
551 * @start_addr: address of memory segment
552 * @total_length: length of memory segment
553 *
554 * Alignment and length of the segment determine how many SBALEs are needed
555 * for the memory segment.
556 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100557static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
559 void *start_addr, unsigned long total_length)
560{
561 unsigned long remaining, length;
562 void *addr;
563
564 /* split segment up heeding page boundaries */
565 for (addr = start_addr, remaining = total_length; remaining > 0;
566 addr += length, remaining -= length) {
567 /* get next free SBALE for new piece */
568 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
569 /* no SBALE left, clean up and leave */
570 zfcp_qdio_sbals_wipe(fsf_req);
571 return -EINVAL;
572 }
573 /* calculate length of new piece */
574 length = min(remaining,
575 (PAGE_SIZE - ((unsigned long) addr &
576 (PAGE_SIZE - 1))));
577 /* fill current SBALE with calculated piece */
578 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
579 }
580 return total_length;
581}
582
583
584/**
585 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
586 * @fsf_req: request to be processed
587 * @sbtype: SBALE flags
588 * @sg: scatter-gather list
589 * @sg_count: number of elements in scatter-gather list
590 * @max_sbals: upper bound for number of SBALs to be used
591 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100592int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
594 struct scatterlist *sg, int sg_count, int max_sbals)
595{
596 int sg_index;
597 struct scatterlist *sg_segment;
598 int retval;
599 volatile struct qdio_buffer_element *sbale;
600 int bytes = 0;
601
602 /* figure out last allowed SBAL */
603 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
604
605 /* set storage-block type for current SBAL */
606 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
607 sbale->flags |= sbtype;
608
609 /* process all segements of scatter-gather list */
610 for (sg_index = 0, sg_segment = sg, bytes = 0;
611 sg_index < sg_count;
612 sg_index++, sg_segment++) {
613 retval = zfcp_qdio_sbals_from_segment(
614 fsf_req,
615 sbtype,
616 zfcp_sg_to_address(sg_segment),
617 sg_segment->length);
618 if (retval < 0) {
619 bytes = retval;
620 goto out;
621 } else
622 bytes += retval;
623 }
624 /* assume that no other SBALEs are to follow in the same SBAL */
625 sbale = zfcp_qdio_sbale_curr(fsf_req);
626 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
627out:
628 return bytes;
629}
630
631
632/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
634 * @fsf_req: request to be processed
635 * @sbtype: SBALE flags
636 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
637 * to fill SBALs
638 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100639int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
641 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
642{
FUJITA Tomonori7936a892007-07-29 16:46:28 +0900643 if (scsi_sg_count(scsi_cmnd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
FUJITA Tomonori7936a892007-07-29 16:46:28 +0900645 scsi_sglist(scsi_cmnd),
646 scsi_sg_count(scsi_cmnd),
647 ZFCP_MAX_SBALS_PER_REQ);
648 else
649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/**
653 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
654 */
655int
656zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
657 struct zfcp_fsf_req *fsf_req)
658{
659 int new_distance_from_int;
660 int pci_pos;
661 volatile struct qdio_buffer_element *sbale;
662
663 new_distance_from_int = req_queue->distance_from_int +
664 fsf_req->sbal_number;
665
666 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
667 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
668 pci_pos = fsf_req->sbal_first;
669 pci_pos += fsf_req->sbal_number;
670 pci_pos -= new_distance_from_int;
671 pci_pos -= 1;
672 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
673 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
674 sbale->flags |= SBAL_FLAGS0_PCI;
675 }
676 return new_distance_from_int;
677}
678
679/*
680 * function: zfcp_zero_sbals
681 *
682 * purpose: zeros specified range of SBALs
683 *
684 * returns:
685 */
686void
687zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
688{
689 int cur_pos;
690 int index;
691
692 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
693 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
694 memset(buf[index], 0, sizeof (struct qdio_buffer));
695 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
696 index, buf[index]);
697 }
698}
699
700#undef ZFCP_LOG_AREA