blob: c408badd2ae966c20917bf286951c3b347efb056 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * Frees BUFFER memory for each of the pointers of the struct qdio_buffer array
Swen Schilligb4e44592007-07-18 10:55:13 +020051 * in the adapter struct sbuf is the pointer array.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * locks: must only be called with zfcp_data.config_sema taken
54 */
55static void
Swen Schilligb4e44592007-07-18 10:55:13 +020056zfcp_qdio_buffers_dequeue(struct qdio_buffer **sbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Swen Schilligb4e44592007-07-18 10:55:13 +020058 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Swen Schilligb4e44592007-07-18 10:55:13 +020060 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE)
61 free_page((unsigned long) sbuf[pos]);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Swen Schilligb4e44592007-07-18 10:55:13 +020064/*
65 * Allocates BUFFER memory to each of the pointers of the qdio_buffer_t
66 * array in the adapter struct.
67 * Cur_buf is the pointer array
68 *
69 * returns: zero on success else -ENOMEM
70 * locks: must only be called with zfcp_data.config_sema taken
71 */
72static int
73zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbuf)
74{
75 int pos;
76
77 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
78 sbuf[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
79 if (!sbuf[pos]) {
80 zfcp_qdio_buffers_dequeue(sbuf);
81 return -ENOMEM;
82 }
83 }
84 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
85 if (pos % QBUFF_PER_PAGE)
86 sbuf[pos] = sbuf[pos - 1] + 1;
87 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/* locks: must only be called with zfcp_data.config_sema taken */
91int
92zfcp_qdio_allocate_queues(struct zfcp_adapter *adapter)
93{
Swen Schilligb4e44592007-07-18 10:55:13 +020094 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Swen Schilligb4e44592007-07-18 10:55:13 +020096 ret = zfcp_qdio_buffers_enqueue(adapter->request_queue.buffer);
97 if (ret)
98 return ret;
99 return zfcp_qdio_buffers_enqueue(adapter->response_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/* locks: must only be called with zfcp_data.config_sema taken */
103void
104zfcp_qdio_free_queues(struct zfcp_adapter *adapter)
105{
106 ZFCP_LOG_TRACE("freeing request_queue buffers\n");
Swen Schilligb4e44592007-07-18 10:55:13 +0200107 zfcp_qdio_buffers_dequeue(adapter->request_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 ZFCP_LOG_TRACE("freeing response_queue buffers\n");
Swen Schilligb4e44592007-07-18 10:55:13 +0200110 zfcp_qdio_buffers_dequeue(adapter->response_queue.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113int
114zfcp_qdio_allocate(struct zfcp_adapter *adapter)
115{
116 struct qdio_initialize *init_data;
117
118 init_data = &adapter->qdio_init_data;
119
120 init_data->cdev = adapter->ccw_device;
121 init_data->q_format = QDIO_SCSI_QFMT;
Andreas Herrmann06506d02006-05-22 18:18:19 +0200122 memcpy(init_data->adapter_name, zfcp_get_busid_by_adapter(adapter), 8);
123 ASCEBC(init_data->adapter_name, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 init_data->qib_param_field_format = 0;
125 init_data->qib_param_field = NULL;
126 init_data->input_slib_elements = NULL;
127 init_data->output_slib_elements = NULL;
128 init_data->min_input_threshold = ZFCP_MIN_INPUT_THRESHOLD;
129 init_data->max_input_threshold = ZFCP_MAX_INPUT_THRESHOLD;
130 init_data->min_output_threshold = ZFCP_MIN_OUTPUT_THRESHOLD;
131 init_data->max_output_threshold = ZFCP_MAX_OUTPUT_THRESHOLD;
132 init_data->no_input_qs = 1;
133 init_data->no_output_qs = 1;
134 init_data->input_handler = zfcp_qdio_response_handler;
135 init_data->output_handler = zfcp_qdio_request_handler;
136 init_data->int_parm = (unsigned long) adapter;
137 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
138 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
139 init_data->input_sbal_addr_array =
140 (void **) (adapter->response_queue.buffer);
141 init_data->output_sbal_addr_array =
142 (void **) (adapter->request_queue.buffer);
143
144 return qdio_allocate(init_data);
145}
146
147/*
148 * function: zfcp_qdio_handler_error_check
149 *
150 * purpose: called by the response handler to determine error condition
151 *
152 * returns: error flag
153 *
154 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100155static int
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200156zfcp_qdio_handler_error_check(struct zfcp_adapter *adapter, unsigned int status,
157 unsigned int qdio_error, unsigned int siga_error,
158 int first_element, int elements_processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 int retval = 0;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
163 retval = -EIO;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ZFCP_LOG_INFO("QDIO problem occurred (status=0x%x, "
166 "qdio_error=0x%x, siga_error=0x%x)\n",
167 status, qdio_error, siga_error);
168
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200169 zfcp_hba_dbf_event_qdio(adapter, status, qdio_error, siga_error,
170 first_element, elements_processed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /*
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200172 * Restarting IO on the failed adapter from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Since we have been using this adapter, it is save to assume
174 * that it is not failed but recoverable. The card seems to
175 * report link-up events by self-initiated queue shutdown.
Michael Opdenacker59c51592007-05-09 08:57:56 +0200176 * That is why we need to clear the link-down flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * which is set again in case we have missed by a mile.
178 */
179 zfcp_erp_adapter_reopen(
180 adapter,
181 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
182 ZFCP_STATUS_COMMON_ERP_FAILED);
183 }
184 return retval;
185}
186
187/*
188 * function: zfcp_qdio_request_handler
189 *
190 * purpose: is called by QDIO layer for completed SBALs in request queue
191 *
192 * returns: (void)
193 */
194static void
195zfcp_qdio_request_handler(struct ccw_device *ccw_device,
196 unsigned int status,
197 unsigned int qdio_error,
198 unsigned int siga_error,
199 unsigned int queue_number,
200 int first_element,
201 int elements_processed,
202 unsigned long int_parm)
203{
204 struct zfcp_adapter *adapter;
205 struct zfcp_qdio_queue *queue;
206
207 adapter = (struct zfcp_adapter *) int_parm;
208 queue = &adapter->request_queue;
209
210 ZFCP_LOG_DEBUG("adapter %s, first=%d, elements_processed=%d\n",
211 zfcp_get_busid_by_adapter(adapter),
212 first_element, elements_processed);
213
214 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200215 siga_error, first_element,
216 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out;
218 /*
219 * we stored address of struct zfcp_adapter data structure
220 * associated with irq in int_parm
221 */
222
223 /* cleanup all SBALs being program-owned now */
224 zfcp_qdio_zero_sbals(queue->buffer, first_element, elements_processed);
225
226 /* increase free space in outbound queue */
227 atomic_add(elements_processed, &queue->free_count);
228 ZFCP_LOG_DEBUG("free_count=%d\n", atomic_read(&queue->free_count));
229 wake_up(&adapter->request_wq);
230 ZFCP_LOG_DEBUG("elements_processed=%d, free count=%d\n",
231 elements_processed, atomic_read(&queue->free_count));
232 out:
233 return;
234}
235
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200236/**
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200237 * zfcp_qdio_reqid_check - checks for valid reqids.
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200238 */
Christof Schmittb03670e2007-05-07 16:35:04 +0200239static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
240 unsigned long req_id)
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200241{
242 struct zfcp_fsf_req *fsf_req;
243 unsigned long flags;
244
245 debug_long_event(adapter->erp_dbf, 4, req_id);
246
247 spin_lock_irqsave(&adapter->req_list_lock, flags);
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200248 fsf_req = zfcp_reqlist_find(adapter, req_id);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200249
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200250 if (!fsf_req)
251 /*
252 * Unknown request means that we have potentially memory
253 * corruption and must stop the machine immediatly.
254 */
255 panic("error: unknown request id (%ld) on adapter %s.\n",
256 req_id, zfcp_get_busid_by_adapter(adapter));
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200257
Heiko Carstensca2d02c2007-05-08 11:17:54 +0200258 zfcp_reqlist_remove(adapter, fsf_req);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200259 atomic_dec(&adapter->reqs_active);
260 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
261
262 /* finish the FSF request */
263 zfcp_fsf_req_complete(fsf_req);
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * function: zfcp_qdio_response_handler
268 *
269 * purpose: is called by QDIO layer for completed SBALs in response queue
270 *
271 * returns: (void)
272 */
273static void
274zfcp_qdio_response_handler(struct ccw_device *ccw_device,
275 unsigned int status,
276 unsigned int qdio_error,
277 unsigned int siga_error,
278 unsigned int queue_number,
279 int first_element,
280 int elements_processed,
281 unsigned long int_parm)
282{
283 struct zfcp_adapter *adapter;
284 struct zfcp_qdio_queue *queue;
285 int buffer_index;
286 int i;
287 struct qdio_buffer *buffer;
288 int retval = 0;
289 u8 count;
290 u8 start;
291 volatile struct qdio_buffer_element *buffere = NULL;
292 int buffere_index;
293
294 adapter = (struct zfcp_adapter *) int_parm;
295 queue = &adapter->response_queue;
296
297 if (unlikely(zfcp_qdio_handler_error_check(adapter, status, qdio_error,
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200298 siga_error, first_element,
299 elements_processed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto out;
301
302 /*
303 * we stored address of struct zfcp_adapter data structure
304 * associated with irq in int_parm
305 */
306
307 buffere = &(queue->buffer[first_element]->element[0]);
308 ZFCP_LOG_DEBUG("first BUFFERE flags=0x%x\n", buffere->flags);
309 /*
310 * go through all SBALs from input queue currently
311 * returned by QDIO layer
312 */
313
314 for (i = 0; i < elements_processed; i++) {
315
316 buffer_index = first_element + i;
317 buffer_index %= QDIO_MAX_BUFFERS_PER_Q;
318 buffer = queue->buffer[buffer_index];
319
320 /* go through all SBALEs of SBAL */
321 for (buffere_index = 0;
322 buffere_index < QDIO_MAX_ELEMENTS_PER_BUFFER;
323 buffere_index++) {
324
325 /* look for QDIO request identifiers in SB */
326 buffere = &buffer->element[buffere_index];
Christof Schmittb03670e2007-05-07 16:35:04 +0200327 zfcp_qdio_reqid_check(adapter,
328 (unsigned long) buffere->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * A single used SBALE per inbound SBALE has been
332 * implemented by QDIO so far. Hope they will
333 * do some optimisation. Will need to change to
334 * unlikely() then.
335 */
336 if (likely(buffere->flags & SBAL_FLAGS_LAST_ENTRY))
337 break;
338 };
339
340 if (unlikely(!(buffere->flags & SBAL_FLAGS_LAST_ENTRY))) {
341 ZFCP_LOG_NORMAL("bug: End of inbound data "
342 "not marked!\n");
343 }
344 }
345
346 /*
347 * put range of SBALs back to response queue
348 * (including SBALs which have already been free before)
349 */
350 count = atomic_read(&queue->free_count) + elements_processed;
351 start = queue->free_index;
352
353 ZFCP_LOG_TRACE("calling do_QDIO on adapter %s (flags=0x%x, "
354 "queue_no=%i, index_in_queue=%i, count=%i, "
355 "buffers=0x%lx\n",
356 zfcp_get_busid_by_adapter(adapter),
357 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
358 0, start, count, (unsigned long) &queue->buffer[start]);
359
360 retval = do_QDIO(ccw_device,
361 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
362 0, start, count, NULL);
363
364 if (unlikely(retval)) {
365 atomic_set(&queue->free_count, count);
366 ZFCP_LOG_DEBUG("clearing of inbound data regions failed, "
367 "queues may be down "
368 "(count=%d, start=%d, retval=%d)\n",
369 count, start, retval);
370 } else {
371 queue->free_index += count;
372 queue->free_index %= QDIO_MAX_BUFFERS_PER_Q;
373 atomic_set(&queue->free_count, 0);
374 ZFCP_LOG_TRACE("%i buffers enqueued to response "
375 "queue at position %i\n", count, start);
376 }
377 out:
378 return;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
382 * zfcp_qdio_sbale_get - return pointer to SBALE of qdio_queue
383 * @queue: queue from which SBALE should be returned
384 * @sbal: specifies number of SBAL in queue
385 * @sbale: specifes number of SBALE in SBAL
386 */
387static inline volatile struct qdio_buffer_element *
388zfcp_qdio_sbale_get(struct zfcp_qdio_queue *queue, int sbal, int sbale)
389{
390 return &queue->buffer[sbal]->element[sbale];
391}
392
393/**
394 * zfcp_qdio_sbale_req - return pointer to SBALE of request_queue for
395 * a struct zfcp_fsf_req
396 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100397volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398zfcp_qdio_sbale_req(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
399{
400 return zfcp_qdio_sbale_get(&fsf_req->adapter->request_queue,
401 sbal, sbale);
402}
403
404/**
405 * zfcp_qdio_sbale_resp - return pointer to SBALE of response_queue for
406 * a struct zfcp_fsf_req
407 */
408static inline volatile struct qdio_buffer_element *
409zfcp_qdio_sbale_resp(struct zfcp_fsf_req *fsf_req, int sbal, int sbale)
410{
411 return zfcp_qdio_sbale_get(&fsf_req->adapter->response_queue,
412 sbal, sbale);
413}
414
415/**
416 * zfcp_qdio_sbale_curr - return current SBALE on request_queue for
417 * a struct zfcp_fsf_req
418 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100419volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420zfcp_qdio_sbale_curr(struct zfcp_fsf_req *fsf_req)
421{
422 return zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr,
423 fsf_req->sbale_curr);
424}
425
426/**
427 * zfcp_qdio_sbal_limit - determine maximum number of SBALs that can be used
428 * on the request_queue for a struct zfcp_fsf_req
429 * @fsf_req: the number of the last SBAL that can be used is stored herein
430 * @max_sbals: used to pass an upper limit for the number of SBALs
431 *
432 * Note: We can assume at least one free SBAL in the request_queue when called.
433 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100434static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
436{
437 int count = atomic_read(&fsf_req->adapter->request_queue.free_count);
438 count = min(count, max_sbals);
439 fsf_req->sbal_last = fsf_req->sbal_first;
440 fsf_req->sbal_last += (count - 1);
441 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
442}
443
444/**
445 * zfcp_qdio_sbal_chain - chain SBALs if more than one SBAL is needed for a
446 * request
447 * @fsf_req: zfcp_fsf_req to be processed
448 * @sbtype: SBAL flags which have to be set in first SBALE of new SBAL
449 *
450 * This function changes sbal_curr, sbale_curr, sbal_number of fsf_req.
451 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100452static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
454{
455 volatile struct qdio_buffer_element *sbale;
456
457 /* set last entry flag in current SBALE of current SBAL */
458 sbale = zfcp_qdio_sbale_curr(fsf_req);
459 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
460
461 /* don't exceed last allowed SBAL */
462 if (fsf_req->sbal_curr == fsf_req->sbal_last)
463 return NULL;
464
465 /* set chaining flag in first SBALE of current SBAL */
466 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
467 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
468
469 /* calculate index of next SBAL */
470 fsf_req->sbal_curr++;
471 fsf_req->sbal_curr %= QDIO_MAX_BUFFERS_PER_Q;
472
473 /* keep this requests number of SBALs up-to-date */
474 fsf_req->sbal_number++;
475
476 /* start at first SBALE of new SBAL */
477 fsf_req->sbale_curr = 0;
478
479 /* set storage-block type for new SBAL */
480 sbale = zfcp_qdio_sbale_curr(fsf_req);
481 sbale->flags |= sbtype;
482
483 return sbale;
484}
485
486/**
487 * zfcp_qdio_sbale_next - switch to next SBALE, chain SBALs if needed
488 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100489static volatile struct qdio_buffer_element *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
491{
492 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
493 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
494
495 fsf_req->sbale_curr++;
496
497 return zfcp_qdio_sbale_curr(fsf_req);
498}
499
500/**
501 * zfcp_qdio_sbals_zero - initialize SBALs between first and last in queue
502 * with zero from
503 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100504static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505zfcp_qdio_sbals_zero(struct zfcp_qdio_queue *queue, int first, int last)
506{
507 struct qdio_buffer **buf = queue->buffer;
508 int curr = first;
509 int count = 0;
510
511 for(;;) {
512 curr %= QDIO_MAX_BUFFERS_PER_Q;
513 count++;
514 memset(buf[curr], 0, sizeof(struct qdio_buffer));
515 if (curr == last)
516 break;
517 curr++;
518 }
519 return count;
520}
521
522
523/**
524 * zfcp_qdio_sbals_wipe - reset all changes in SBALs for an fsf_req
525 */
526static inline int
527zfcp_qdio_sbals_wipe(struct zfcp_fsf_req *fsf_req)
528{
529 return zfcp_qdio_sbals_zero(&fsf_req->adapter->request_queue,
530 fsf_req->sbal_first, fsf_req->sbal_curr);
531}
532
533
534/**
535 * zfcp_qdio_sbale_fill - set address and lenght in current SBALE
536 * on request_queue
537 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100538static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539zfcp_qdio_sbale_fill(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
540 void *addr, int length)
541{
542 volatile struct qdio_buffer_element *sbale;
543
544 sbale = zfcp_qdio_sbale_curr(fsf_req);
545 sbale->addr = addr;
546 sbale->length = length;
547}
548
549/**
550 * zfcp_qdio_sbals_from_segment - map memory segment to SBALE(s)
551 * @fsf_req: request to be processed
552 * @sbtype: SBALE flags
553 * @start_addr: address of memory segment
554 * @total_length: length of memory segment
555 *
556 * Alignment and length of the segment determine how many SBALEs are needed
557 * for the memory segment.
558 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100559static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
561 void *start_addr, unsigned long total_length)
562{
563 unsigned long remaining, length;
564 void *addr;
565
566 /* split segment up heeding page boundaries */
567 for (addr = start_addr, remaining = total_length; remaining > 0;
568 addr += length, remaining -= length) {
569 /* get next free SBALE for new piece */
570 if (NULL == zfcp_qdio_sbale_next(fsf_req, sbtype)) {
571 /* no SBALE left, clean up and leave */
572 zfcp_qdio_sbals_wipe(fsf_req);
573 return -EINVAL;
574 }
575 /* calculate length of new piece */
576 length = min(remaining,
577 (PAGE_SIZE - ((unsigned long) addr &
578 (PAGE_SIZE - 1))));
579 /* fill current SBALE with calculated piece */
580 zfcp_qdio_sbale_fill(fsf_req, sbtype, addr, length);
581 }
582 return total_length;
583}
584
585
586/**
587 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
588 * @fsf_req: request to be processed
589 * @sbtype: SBALE flags
590 * @sg: scatter-gather list
591 * @sg_count: number of elements in scatter-gather list
592 * @max_sbals: upper bound for number of SBALs to be used
593 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100594int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
596 struct scatterlist *sg, int sg_count, int max_sbals)
597{
598 int sg_index;
599 struct scatterlist *sg_segment;
600 int retval;
601 volatile struct qdio_buffer_element *sbale;
602 int bytes = 0;
603
604 /* figure out last allowed SBAL */
605 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
606
607 /* set storage-block type for current SBAL */
608 sbale = zfcp_qdio_sbale_req(fsf_req, fsf_req->sbal_curr, 0);
609 sbale->flags |= sbtype;
610
611 /* process all segements of scatter-gather list */
612 for (sg_index = 0, sg_segment = sg, bytes = 0;
613 sg_index < sg_count;
614 sg_index++, sg_segment++) {
615 retval = zfcp_qdio_sbals_from_segment(
616 fsf_req,
617 sbtype,
618 zfcp_sg_to_address(sg_segment),
619 sg_segment->length);
620 if (retval < 0) {
621 bytes = retval;
622 goto out;
623 } else
624 bytes += retval;
625 }
626 /* assume that no other SBALEs are to follow in the same SBAL */
627 sbale = zfcp_qdio_sbale_curr(fsf_req);
628 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
629out:
630 return bytes;
631}
632
633
634/**
635 * zfcp_qdio_sbals_from_buffer - fill SBALs from buffer
636 * @fsf_req: request to be processed
637 * @sbtype: SBALE flags
638 * @buffer: data buffer
639 * @length: length of buffer
640 * @max_sbals: upper bound for number of SBALs to be used
641 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100642static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643zfcp_qdio_sbals_from_buffer(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
644 void *buffer, unsigned long length, int max_sbals)
645{
646 struct scatterlist sg_segment;
647
648 zfcp_address_to_sg(buffer, &sg_segment);
649 sg_segment.length = length;
650
651 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype, &sg_segment, 1,
652 max_sbals);
653}
654
655
656/**
657 * zfcp_qdio_sbals_from_scsicmnd - fill SBALs from scsi command
658 * @fsf_req: request to be processed
659 * @sbtype: SBALE flags
660 * @scsi_cmnd: either scatter-gather list or buffer contained herein is used
661 * to fill SBALs
662 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100663int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664zfcp_qdio_sbals_from_scsicmnd(struct zfcp_fsf_req *fsf_req,
665 unsigned long sbtype, struct scsi_cmnd *scsi_cmnd)
666{
667 if (scsi_cmnd->use_sg) {
668 return zfcp_qdio_sbals_from_sg(fsf_req, sbtype,
669 (struct scatterlist *)
670 scsi_cmnd->request_buffer,
671 scsi_cmnd->use_sg,
672 ZFCP_MAX_SBALS_PER_REQ);
673 } else {
674 return zfcp_qdio_sbals_from_buffer(fsf_req, sbtype,
675 scsi_cmnd->request_buffer,
676 scsi_cmnd->request_bufflen,
677 ZFCP_MAX_SBALS_PER_REQ);
678 }
679}
680
681/**
682 * zfcp_qdio_determine_pci - set PCI flag in first SBALE on qdio queue if needed
683 */
684int
685zfcp_qdio_determine_pci(struct zfcp_qdio_queue *req_queue,
686 struct zfcp_fsf_req *fsf_req)
687{
688 int new_distance_from_int;
689 int pci_pos;
690 volatile struct qdio_buffer_element *sbale;
691
692 new_distance_from_int = req_queue->distance_from_int +
693 fsf_req->sbal_number;
694
695 if (unlikely(new_distance_from_int >= ZFCP_QDIO_PCI_INTERVAL)) {
696 new_distance_from_int %= ZFCP_QDIO_PCI_INTERVAL;
697 pci_pos = fsf_req->sbal_first;
698 pci_pos += fsf_req->sbal_number;
699 pci_pos -= new_distance_from_int;
700 pci_pos -= 1;
701 pci_pos %= QDIO_MAX_BUFFERS_PER_Q;
702 sbale = zfcp_qdio_sbale_req(fsf_req, pci_pos, 0);
703 sbale->flags |= SBAL_FLAGS0_PCI;
704 }
705 return new_distance_from_int;
706}
707
708/*
709 * function: zfcp_zero_sbals
710 *
711 * purpose: zeros specified range of SBALs
712 *
713 * returns:
714 */
715void
716zfcp_qdio_zero_sbals(struct qdio_buffer *buf[], int first, int clean_count)
717{
718 int cur_pos;
719 int index;
720
721 for (cur_pos = first; cur_pos < (first + clean_count); cur_pos++) {
722 index = cur_pos % QDIO_MAX_BUFFERS_PER_Q;
723 memset(buf[index], 0, sizeof (struct qdio_buffer));
724 ZFCP_LOG_TRACE("zeroing BUFFER %d at address %p\n",
725 index, buf[index]);
726 }
727}
728
729#undef ZFCP_LOG_AREA