OMAP3: PM: Prevented DVFS state switches when enabling off-mode
[linux-ginger.git] / drivers / s390 / scsi / zfcp_qdio.c
blob6c5228b627fc540bac927019bf0fb829bb9d4bf8
1 /*
2 * zfcp device driver
4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2009
7 */
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include "zfcp_ext.h"
14 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
16 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
18 int pos;
20 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
21 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
22 if (!sbal[pos])
23 return -ENOMEM;
25 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
26 if (pos % QBUFF_PER_PAGE)
27 sbal[pos] = sbal[pos - 1] + 1;
28 return 0;
31 static struct qdio_buffer_element *
32 zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
34 return &q->sbal[sbal_idx]->element[sbale_idx];
37 static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
39 struct zfcp_adapter *adapter = qdio->adapter;
41 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
43 zfcp_erp_adapter_reopen(adapter,
44 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
45 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
48 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
50 int i, sbal_idx;
52 for (i = first; i < first + cnt; i++) {
53 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
54 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
58 /* this needs to be called prior to updating the queue fill level */
59 static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
61 unsigned long long now, span;
62 int free, used;
64 spin_lock(&qdio->stat_lock);
65 now = get_clock_monotonic();
66 span = (now - qdio->req_q_time) >> 12;
67 free = atomic_read(&qdio->req_q.count);
68 used = QDIO_MAX_BUFFERS_PER_Q - free;
69 qdio->req_q_util += used * span;
70 qdio->req_q_time = now;
71 spin_unlock(&qdio->stat_lock);
74 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
75 int queue_no, int first, int count,
76 unsigned long parm)
78 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
79 struct zfcp_qdio_queue *queue = &qdio->req_q;
81 if (unlikely(qdio_err)) {
82 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
83 count);
84 zfcp_qdio_handler_error(qdio, "qdireq1");
85 return;
88 /* cleanup all SBALs being program-owned now */
89 zfcp_qdio_zero_sbals(queue->sbal, first, count);
91 zfcp_qdio_account(qdio);
92 atomic_add(count, &queue->count);
93 wake_up(&qdio->req_q_wq);
96 static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
98 struct zfcp_qdio_queue *queue = &qdio->resp_q;
99 struct ccw_device *cdev = qdio->adapter->ccw_device;
100 u8 count, start = queue->first;
101 unsigned int retval;
103 count = atomic_read(&queue->count) + processed;
105 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
107 if (unlikely(retval)) {
108 atomic_set(&queue->count, count);
109 /* FIXME: Recover this with an adapter reopen? */
110 } else {
111 queue->first += count;
112 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
113 atomic_set(&queue->count, 0);
117 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
118 int queue_no, int first, int count,
119 unsigned long parm)
121 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
122 int sbal_idx, sbal_no;
124 if (unlikely(qdio_err)) {
125 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
126 count);
127 zfcp_qdio_handler_error(qdio, "qdires1");
128 return;
132 * go through all SBALs from input queue currently
133 * returned by QDIO layer
135 for (sbal_no = 0; sbal_no < count; sbal_no++) {
136 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
137 /* go through all SBALEs of SBAL */
138 zfcp_fsf_reqid_check(qdio, sbal_idx);
142 * put range of SBALs back to response queue
143 * (including SBALs which have already been free before)
145 zfcp_qdio_resp_put_back(qdio, count);
149 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
150 * @qdio: pointer to struct zfcp_qdio
151 * @q_rec: pointer to struct zfcp_queue_rec
152 * Returns: pointer to qdio_buffer_element (SBALE) structure
154 struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_qdio *qdio,
155 struct zfcp_queue_req *q_req)
157 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last, 0);
161 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
162 * @fsf_req: pointer to struct fsf_req
163 * Returns: pointer to qdio_buffer_element (SBALE) structure
165 struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio,
166 struct zfcp_queue_req *q_req)
168 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last,
169 q_req->sbale_curr);
172 static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
173 struct zfcp_queue_req *q_req, int max_sbals)
175 int count = atomic_read(&qdio->req_q.count);
176 count = min(count, max_sbals);
177 q_req->sbal_limit = (q_req->sbal_first + count - 1)
178 % QDIO_MAX_BUFFERS_PER_Q;
181 static struct qdio_buffer_element *
182 zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
183 unsigned long sbtype)
185 struct qdio_buffer_element *sbale;
187 /* set last entry flag in current SBALE of current SBAL */
188 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
189 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
191 /* don't exceed last allowed SBAL */
192 if (q_req->sbal_last == q_req->sbal_limit)
193 return NULL;
195 /* set chaining flag in first SBALE of current SBAL */
196 sbale = zfcp_qdio_sbale_req(qdio, q_req);
197 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
199 /* calculate index of next SBAL */
200 q_req->sbal_last++;
201 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
203 /* keep this requests number of SBALs up-to-date */
204 q_req->sbal_number++;
206 /* start at first SBALE of new SBAL */
207 q_req->sbale_curr = 0;
209 /* set storage-block type for new SBAL */
210 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
211 sbale->flags |= sbtype;
213 return sbale;
216 static struct qdio_buffer_element *
217 zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
218 unsigned int sbtype)
220 if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
221 return zfcp_qdio_sbal_chain(qdio, q_req, sbtype);
222 q_req->sbale_curr++;
223 return zfcp_qdio_sbale_curr(qdio, q_req);
226 static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
227 struct zfcp_queue_req *q_req)
229 struct qdio_buffer **sbal = qdio->req_q.sbal;
230 int first = q_req->sbal_first;
231 int last = q_req->sbal_last;
232 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
233 QDIO_MAX_BUFFERS_PER_Q + 1;
234 zfcp_qdio_zero_sbals(sbal, first, count);
237 static int zfcp_qdio_fill_sbals(struct zfcp_qdio *qdio,
238 struct zfcp_queue_req *q_req,
239 unsigned int sbtype, void *start_addr,
240 unsigned int total_length)
242 struct qdio_buffer_element *sbale;
243 unsigned long remaining, length;
244 void *addr;
246 /* split segment up */
247 for (addr = start_addr, remaining = total_length; remaining > 0;
248 addr += length, remaining -= length) {
249 sbale = zfcp_qdio_sbale_next(qdio, q_req, sbtype);
250 if (!sbale) {
251 atomic_inc(&qdio->req_q_full);
252 zfcp_qdio_undo_sbals(qdio, q_req);
253 return -EINVAL;
256 /* new piece must not exceed next page boundary */
257 length = min(remaining,
258 (PAGE_SIZE - ((unsigned long)addr &
259 (PAGE_SIZE - 1))));
260 sbale->addr = addr;
261 sbale->length = length;
263 return 0;
267 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
268 * @fsf_req: request to be processed
269 * @sbtype: SBALE flags
270 * @sg: scatter-gather list
271 * @max_sbals: upper bound for number of SBALs to be used
272 * Returns: number of bytes, or error (negativ)
274 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio,
275 struct zfcp_queue_req *q_req,
276 unsigned long sbtype, struct scatterlist *sg,
277 int max_sbals)
279 struct qdio_buffer_element *sbale;
280 int retval, bytes = 0;
282 /* figure out last allowed SBAL */
283 zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
285 /* set storage-block type for this request */
286 sbale = zfcp_qdio_sbale_req(qdio, q_req);
287 sbale->flags |= sbtype;
289 for (; sg; sg = sg_next(sg)) {
290 retval = zfcp_qdio_fill_sbals(qdio, q_req, sbtype,
291 sg_virt(sg), sg->length);
292 if (retval < 0)
293 return retval;
294 bytes += sg->length;
297 /* assume that no other SBALEs are to follow in the same SBAL */
298 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
299 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
301 return bytes;
305 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
306 * @qdio: pointer to struct zfcp_qdio
307 * @q_req: pointer to struct zfcp_queue_req
308 * Returns: 0 on success, error otherwise
310 int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req)
312 struct zfcp_qdio_queue *req_q = &qdio->req_q;
313 int first = q_req->sbal_first;
314 int count = q_req->sbal_number;
315 int retval;
316 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
318 zfcp_qdio_account(qdio);
320 retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
321 count);
322 if (unlikely(retval)) {
323 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
324 return retval;
327 /* account for transferred buffers */
328 atomic_sub(count, &req_q->count);
329 req_q->first += count;
330 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
331 return 0;
335 static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
336 struct zfcp_qdio *qdio)
339 id->cdev = qdio->adapter->ccw_device;
340 id->q_format = QDIO_ZFCP_QFMT;
341 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
342 ASCEBC(id->adapter_name, 8);
343 id->qib_param_field_format = 0;
344 id->qib_param_field = NULL;
345 id->input_slib_elements = NULL;
346 id->output_slib_elements = NULL;
347 id->no_input_qs = 1;
348 id->no_output_qs = 1;
349 id->input_handler = zfcp_qdio_int_resp;
350 id->output_handler = zfcp_qdio_int_req;
351 id->int_parm = (unsigned long) qdio;
352 id->flags = QDIO_INBOUND_0COPY_SBALS |
353 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
354 id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
355 id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
359 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
360 * @adapter: pointer to struct zfcp_adapter
361 * Returns: -ENOMEM on memory allocation error or return value from
362 * qdio_allocate
364 static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
366 struct qdio_initialize init_data;
368 if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
369 zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
370 return -ENOMEM;
372 zfcp_qdio_setup_init_data(&init_data, qdio);
374 return qdio_allocate(&init_data);
378 * zfcp_close_qdio - close qdio queues for an adapter
379 * @qdio: pointer to structure zfcp_qdio
381 void zfcp_qdio_close(struct zfcp_qdio *qdio)
383 struct zfcp_qdio_queue *req_q;
384 int first, count;
386 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
387 return;
389 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
390 req_q = &qdio->req_q;
391 spin_lock_bh(&qdio->req_q_lock);
392 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
393 spin_unlock_bh(&qdio->req_q_lock);
395 qdio_shutdown(qdio->adapter->ccw_device,
396 QDIO_FLAG_CLEANUP_USING_CLEAR);
398 /* cleanup used outbound sbals */
399 count = atomic_read(&req_q->count);
400 if (count < QDIO_MAX_BUFFERS_PER_Q) {
401 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
402 count = QDIO_MAX_BUFFERS_PER_Q - count;
403 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
405 req_q->first = 0;
406 atomic_set(&req_q->count, 0);
407 qdio->resp_q.first = 0;
408 atomic_set(&qdio->resp_q.count, 0);
412 * zfcp_qdio_open - prepare and initialize response queue
413 * @qdio: pointer to struct zfcp_qdio
414 * Returns: 0 on success, otherwise -EIO
416 int zfcp_qdio_open(struct zfcp_qdio *qdio)
418 struct qdio_buffer_element *sbale;
419 struct qdio_initialize init_data;
420 struct ccw_device *cdev = qdio->adapter->ccw_device;
421 int cc;
423 if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
424 return -EIO;
426 zfcp_qdio_setup_init_data(&init_data, qdio);
428 if (qdio_establish(&init_data))
429 goto failed_establish;
431 if (qdio_activate(cdev))
432 goto failed_qdio;
434 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
435 sbale = &(qdio->resp_q.sbal[cc]->element[0]);
436 sbale->length = 0;
437 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
438 sbale->addr = NULL;
441 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
442 QDIO_MAX_BUFFERS_PER_Q))
443 goto failed_qdio;
445 /* set index of first avalable SBALS / number of available SBALS */
446 qdio->req_q.first = 0;
447 atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
449 return 0;
451 failed_qdio:
452 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
453 failed_establish:
454 dev_err(&cdev->dev,
455 "Setting up the QDIO connection to the FCP adapter failed\n");
456 return -EIO;
459 void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
461 struct qdio_buffer **sbal_req, **sbal_resp;
462 int p;
464 if (!qdio)
465 return;
467 if (qdio->adapter->ccw_device)
468 qdio_free(qdio->adapter->ccw_device);
470 sbal_req = qdio->req_q.sbal;
471 sbal_resp = qdio->resp_q.sbal;
473 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
474 free_page((unsigned long) sbal_req[p]);
475 free_page((unsigned long) sbal_resp[p]);
478 kfree(qdio);
481 int zfcp_qdio_setup(struct zfcp_adapter *adapter)
483 struct zfcp_qdio *qdio;
485 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
486 if (!qdio)
487 return -ENOMEM;
489 qdio->adapter = adapter;
491 if (zfcp_qdio_allocate(qdio)) {
492 zfcp_qdio_destroy(qdio);
493 return -ENOMEM;
496 spin_lock_init(&qdio->req_q_lock);
497 spin_lock_init(&qdio->stat_lock);
499 adapter->qdio = qdio;
500 return 0;