4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2008
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 /* FIXME(tune): free space should be one max. SBAL chain plus what? */
15 #define ZFCP_QDIO_PCI_INTERVAL (QDIO_MAX_BUFFERS_PER_Q \
16 - (FSF_MAX_SBALS_PER_REQ + 4))
17 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
19 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
23 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
24 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
28 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
29 if (pos % QBUFF_PER_PAGE)
30 sbal[pos] = sbal[pos - 1] + 1;
34 static struct qdio_buffer_element *
35 zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
37 return &q->sbal[sbal_idx]->element[sbale_idx];
41 * zfcp_qdio_free - free memory used by request- and resposne queue
42 * @adapter: pointer to the zfcp_adapter structure
44 void zfcp_qdio_free(struct zfcp_adapter *adapter)
46 struct qdio_buffer **sbal_req, **sbal_resp;
49 if (adapter->ccw_device)
50 qdio_free(adapter->ccw_device);
52 sbal_req = adapter->req_q.sbal;
53 sbal_resp = adapter->resp_q.sbal;
55 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
56 free_page((unsigned long) sbal_req[p]);
57 free_page((unsigned long) sbal_resp[p]);
61 static void zfcp_qdio_handler_error(struct zfcp_adapter *adapter, char *id)
63 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
65 zfcp_erp_adapter_reopen(adapter,
66 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
67 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
70 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
74 for (i = first; i < first + cnt; i++) {
75 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
76 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
80 /* this needs to be called prior to updating the queue fill level */
81 static void zfcp_qdio_account(struct zfcp_adapter *adapter)
87 spin_lock(&adapter->qdio_stat_lock);
89 span = ktime_us_delta(now, adapter->req_q_time);
90 free = max(0, atomic_read(&adapter->req_q.count));
91 used = QDIO_MAX_BUFFERS_PER_Q - free;
92 adapter->req_q_util += used * span;
93 adapter->req_q_time = now;
94 spin_unlock(&adapter->qdio_stat_lock);
97 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
98 int queue_no, int first, int count,
101 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
102 struct zfcp_qdio_queue *queue = &adapter->req_q;
104 if (unlikely(qdio_err)) {
105 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
106 zfcp_qdio_handler_error(adapter, "qdireq1");
110 /* cleanup all SBALs being program-owned now */
111 zfcp_qdio_zero_sbals(queue->sbal, first, count);
113 zfcp_qdio_account(adapter);
114 atomic_add(count, &queue->count);
115 wake_up(&adapter->request_wq);
118 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
119 unsigned long req_id, int sbal_idx)
121 struct zfcp_fsf_req *fsf_req;
124 spin_lock_irqsave(&adapter->req_list_lock, flags);
125 fsf_req = zfcp_reqlist_find(adapter, req_id);
129 * Unknown request means that we have potentially memory
130 * corruption and must stop the machine immediatly.
132 panic("error: unknown request id (%lx) on adapter %s.\n",
133 req_id, dev_name(&adapter->ccw_device->dev));
135 zfcp_reqlist_remove(adapter, fsf_req);
136 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
138 fsf_req->sbal_response = sbal_idx;
139 fsf_req->qdio_inb_usage = atomic_read(&adapter->resp_q.count);
140 zfcp_fsf_req_complete(fsf_req);
143 static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
145 struct zfcp_qdio_queue *queue = &adapter->resp_q;
146 struct ccw_device *cdev = adapter->ccw_device;
147 u8 count, start = queue->first;
150 count = atomic_read(&queue->count) + processed;
152 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
154 if (unlikely(retval)) {
155 atomic_set(&queue->count, count);
156 /* FIXME: Recover this with an adapter reopen? */
158 queue->first += count;
159 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
160 atomic_set(&queue->count, 0);
164 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
165 int queue_no, int first, int count,
168 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
169 struct zfcp_qdio_queue *queue = &adapter->resp_q;
170 struct qdio_buffer_element *sbale;
171 int sbal_idx, sbale_idx, sbal_no;
173 if (unlikely(qdio_err)) {
174 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
175 zfcp_qdio_handler_error(adapter, "qdires1");
180 * go through all SBALs from input queue currently
181 * returned by QDIO layer
183 for (sbal_no = 0; sbal_no < count; sbal_no++) {
184 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
186 /* go through all SBALEs of SBAL */
187 for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER;
189 sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx);
190 zfcp_qdio_reqid_check(adapter,
191 (unsigned long) sbale->addr,
193 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
197 if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY)))
198 dev_warn(&adapter->ccw_device->dev,
199 "A QDIO protocol error occurred, "
200 "operations continue\n");
204 * put range of SBALs back to response queue
205 * (including SBALs which have already been free before)
207 zfcp_qdio_resp_put_back(adapter, count);
211 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
212 * @fsf_req: pointer to struct fsf_req
213 * Returns: pointer to qdio_buffer_element (SBALE) structure
215 struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_fsf_req *req)
217 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0);
221 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
222 * @fsf_req: pointer to struct fsf_req
223 * Returns: pointer to qdio_buffer_element (SBALE) structure
225 struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req)
227 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last,
231 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
233 int count = atomic_read(&fsf_req->adapter->req_q.count);
234 count = min(count, max_sbals);
235 fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1)
236 % QDIO_MAX_BUFFERS_PER_Q;
239 static struct qdio_buffer_element *
240 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
242 struct qdio_buffer_element *sbale;
244 /* set last entry flag in current SBALE of current SBAL */
245 sbale = zfcp_qdio_sbale_curr(fsf_req);
246 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
248 /* don't exceed last allowed SBAL */
249 if (fsf_req->sbal_last == fsf_req->sbal_limit)
252 /* set chaining flag in first SBALE of current SBAL */
253 sbale = zfcp_qdio_sbale_req(fsf_req);
254 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
256 /* calculate index of next SBAL */
257 fsf_req->sbal_last++;
258 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
260 /* keep this requests number of SBALs up-to-date */
261 fsf_req->sbal_number++;
263 /* start at first SBALE of new SBAL */
264 fsf_req->sbale_curr = 0;
266 /* set storage-block type for new SBAL */
267 sbale = zfcp_qdio_sbale_curr(fsf_req);
268 sbale->flags |= sbtype;
273 static struct qdio_buffer_element *
274 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
276 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
277 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
278 fsf_req->sbale_curr++;
279 return zfcp_qdio_sbale_curr(fsf_req);
282 static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req)
284 struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal;
285 int first = fsf_req->sbal_first;
286 int last = fsf_req->sbal_last;
287 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
288 QDIO_MAX_BUFFERS_PER_Q + 1;
289 zfcp_qdio_zero_sbals(sbal, first, count);
292 static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req,
293 unsigned int sbtype, void *start_addr,
294 unsigned int total_length)
296 struct qdio_buffer_element *sbale;
297 unsigned long remaining, length;
300 /* split segment up */
301 for (addr = start_addr, remaining = total_length; remaining > 0;
302 addr += length, remaining -= length) {
303 sbale = zfcp_qdio_sbale_next(fsf_req, sbtype);
305 atomic_inc(&fsf_req->adapter->qdio_outb_full);
306 zfcp_qdio_undo_sbals(fsf_req);
310 /* new piece must not exceed next page boundary */
311 length = min(remaining,
312 (PAGE_SIZE - ((unsigned long)addr &
315 sbale->length = length;
321 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
322 * @fsf_req: request to be processed
323 * @sbtype: SBALE flags
324 * @sg: scatter-gather list
325 * @max_sbals: upper bound for number of SBALs to be used
326 * Returns: number of bytes, or error (negativ)
328 int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
329 struct scatterlist *sg, int max_sbals)
331 struct qdio_buffer_element *sbale;
332 int retval, bytes = 0;
334 /* figure out last allowed SBAL */
335 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
337 /* set storage-block type for this request */
338 sbale = zfcp_qdio_sbale_req(fsf_req);
339 sbale->flags |= sbtype;
341 for (; sg; sg = sg_next(sg)) {
342 retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg),
349 /* assume that no other SBALEs are to follow in the same SBAL */
350 sbale = zfcp_qdio_sbale_curr(fsf_req);
351 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
357 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
358 * @fsf_req: pointer to struct zfcp_fsf_req
359 * Returns: 0 on success, error otherwise
361 int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
363 struct zfcp_adapter *adapter = fsf_req->adapter;
364 struct zfcp_qdio_queue *req_q = &adapter->req_q;
365 int first = fsf_req->sbal_first;
366 int count = fsf_req->sbal_number;
367 int retval, pci, pci_batch;
368 struct qdio_buffer_element *sbale;
370 /* acknowledgements for transferred buffers */
371 pci_batch = adapter->req_q_pci_batch + count;
372 if (unlikely(pci_batch >= ZFCP_QDIO_PCI_INTERVAL)) {
373 pci_batch %= ZFCP_QDIO_PCI_INTERVAL;
374 pci = first + count - (pci_batch + 1);
375 pci %= QDIO_MAX_BUFFERS_PER_Q;
376 sbale = zfcp_qdio_sbale(req_q, pci, 0);
377 sbale->flags |= SBAL_FLAGS0_PCI;
380 zfcp_qdio_account(adapter);
382 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
384 if (unlikely(retval)) {
385 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
389 /* account for transferred buffers */
390 atomic_sub(count, &req_q->count);
391 req_q->first += count;
392 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
393 adapter->req_q_pci_batch = pci_batch;
398 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
399 * @adapter: pointer to struct zfcp_adapter
400 * Returns: -ENOMEM on memory allocation error or return value from
403 int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
405 struct qdio_initialize *init_data;
407 if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
408 zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
411 init_data = &adapter->qdio_init_data;
413 init_data->cdev = adapter->ccw_device;
414 init_data->q_format = QDIO_ZFCP_QFMT;
415 memcpy(init_data->adapter_name, dev_name(&adapter->ccw_device->dev), 8);
416 ASCEBC(init_data->adapter_name, 8);
417 init_data->qib_param_field_format = 0;
418 init_data->qib_param_field = NULL;
419 init_data->input_slib_elements = NULL;
420 init_data->output_slib_elements = NULL;
421 init_data->no_input_qs = 1;
422 init_data->no_output_qs = 1;
423 init_data->input_handler = zfcp_qdio_int_resp;
424 init_data->output_handler = zfcp_qdio_int_req;
425 init_data->int_parm = (unsigned long) adapter;
426 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
427 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
428 init_data->input_sbal_addr_array =
429 (void **) (adapter->resp_q.sbal);
430 init_data->output_sbal_addr_array =
431 (void **) (adapter->req_q.sbal);
433 return qdio_allocate(init_data);
437 * zfcp_close_qdio - close qdio queues for an adapter
439 void zfcp_qdio_close(struct zfcp_adapter *adapter)
441 struct zfcp_qdio_queue *req_q;
444 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
447 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
448 req_q = &adapter->req_q;
449 spin_lock_bh(&adapter->req_q_lock);
450 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
451 spin_unlock_bh(&adapter->req_q_lock);
453 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
455 /* cleanup used outbound sbals */
456 count = atomic_read(&req_q->count);
457 if (count < QDIO_MAX_BUFFERS_PER_Q) {
458 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
459 count = QDIO_MAX_BUFFERS_PER_Q - count;
460 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
463 atomic_set(&req_q->count, 0);
464 adapter->req_q_pci_batch = 0;
465 adapter->resp_q.first = 0;
466 atomic_set(&adapter->resp_q.count, 0);
470 * zfcp_qdio_open - prepare and initialize response queue
471 * @adapter: pointer to struct zfcp_adapter
472 * Returns: 0 on success, otherwise -EIO
474 int zfcp_qdio_open(struct zfcp_adapter *adapter)
476 struct qdio_buffer_element *sbale;
479 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
482 if (qdio_establish(&adapter->qdio_init_data))
483 goto failed_establish;
485 if (qdio_activate(adapter->ccw_device))
488 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
489 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
491 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
495 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
496 QDIO_MAX_BUFFERS_PER_Q))
499 /* set index of first avalable SBALS / number of available SBALS */
500 adapter->req_q.first = 0;
501 atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
502 adapter->req_q_pci_batch = 0;
507 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
509 dev_err(&adapter->ccw_device->dev,
510 "Setting up the QDIO connection to the FCP adapter failed\n");