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, u8 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 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
81 int queue_no, int first, int count,
84 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
85 struct zfcp_qdio_queue *queue = &adapter->req_q;
87 if (unlikely(qdio_err)) {
88 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
89 zfcp_qdio_handler_error(adapter, 140);
93 /* cleanup all SBALs being program-owned now */
94 zfcp_qdio_zero_sbals(queue->sbal, first, count);
96 atomic_add(count, &queue->count);
97 wake_up(&adapter->request_wq);
100 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
101 unsigned long req_id, int sbal_idx)
103 struct zfcp_fsf_req *fsf_req;
106 spin_lock_irqsave(&adapter->req_list_lock, flags);
107 fsf_req = zfcp_reqlist_find(adapter, req_id);
111 * Unknown request means that we have potentially memory
112 * corruption and must stop the machine immediatly.
114 panic("error: unknown request id (%lx) on adapter %s.\n",
115 req_id, dev_name(&adapter->ccw_device->dev));
117 zfcp_reqlist_remove(adapter, fsf_req);
118 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
120 fsf_req->sbal_response = sbal_idx;
121 fsf_req->qdio_inb_usage = atomic_read(&adapter->resp_q.count);
122 zfcp_fsf_req_complete(fsf_req);
125 static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
127 struct zfcp_qdio_queue *queue = &adapter->resp_q;
128 struct ccw_device *cdev = adapter->ccw_device;
129 u8 count, start = queue->first;
132 count = atomic_read(&queue->count) + processed;
134 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
136 if (unlikely(retval)) {
137 atomic_set(&queue->count, count);
138 /* FIXME: Recover this with an adapter reopen? */
140 queue->first += count;
141 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
142 atomic_set(&queue->count, 0);
146 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
147 int queue_no, int first, int count,
150 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
151 struct zfcp_qdio_queue *queue = &adapter->resp_q;
152 struct qdio_buffer_element *sbale;
153 int sbal_idx, sbale_idx, sbal_no;
155 if (unlikely(qdio_err)) {
156 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
157 zfcp_qdio_handler_error(adapter, 147);
162 * go through all SBALs from input queue currently
163 * returned by QDIO layer
165 for (sbal_no = 0; sbal_no < count; sbal_no++) {
166 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
168 /* go through all SBALEs of SBAL */
169 for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER;
171 sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx);
172 zfcp_qdio_reqid_check(adapter,
173 (unsigned long) sbale->addr,
175 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
179 if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY)))
180 dev_warn(&adapter->ccw_device->dev,
181 "A QDIO protocol error occurred, "
182 "operations continue\n");
186 * put range of SBALs back to response queue
187 * (including SBALs which have already been free before)
189 zfcp_qdio_resp_put_back(adapter, count);
193 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
194 * @fsf_req: pointer to struct fsf_req
195 * Returns: pointer to qdio_buffer_element (SBALE) structure
197 struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_fsf_req *req)
199 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0);
203 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
204 * @fsf_req: pointer to struct fsf_req
205 * Returns: pointer to qdio_buffer_element (SBALE) structure
207 struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req)
209 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last,
213 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
215 int count = atomic_read(&fsf_req->adapter->req_q.count);
216 count = min(count, max_sbals);
217 fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1)
218 % QDIO_MAX_BUFFERS_PER_Q;
221 static struct qdio_buffer_element *
222 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
224 struct qdio_buffer_element *sbale;
226 /* set last entry flag in current SBALE of current SBAL */
227 sbale = zfcp_qdio_sbale_curr(fsf_req);
228 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
230 /* don't exceed last allowed SBAL */
231 if (fsf_req->sbal_last == fsf_req->sbal_limit)
234 /* set chaining flag in first SBALE of current SBAL */
235 sbale = zfcp_qdio_sbale_req(fsf_req);
236 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
238 /* calculate index of next SBAL */
239 fsf_req->sbal_last++;
240 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
242 /* keep this requests number of SBALs up-to-date */
243 fsf_req->sbal_number++;
245 /* start at first SBALE of new SBAL */
246 fsf_req->sbale_curr = 0;
248 /* set storage-block type for new SBAL */
249 sbale = zfcp_qdio_sbale_curr(fsf_req);
250 sbale->flags |= sbtype;
255 static struct qdio_buffer_element *
256 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
258 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
259 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
260 fsf_req->sbale_curr++;
261 return zfcp_qdio_sbale_curr(fsf_req);
264 static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req)
266 struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal;
267 int first = fsf_req->sbal_first;
268 int last = fsf_req->sbal_last;
269 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
270 QDIO_MAX_BUFFERS_PER_Q + 1;
271 zfcp_qdio_zero_sbals(sbal, first, count);
274 static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req,
275 unsigned int sbtype, void *start_addr,
276 unsigned int total_length)
278 struct qdio_buffer_element *sbale;
279 unsigned long remaining, length;
282 /* split segment up */
283 for (addr = start_addr, remaining = total_length; remaining > 0;
284 addr += length, remaining -= length) {
285 sbale = zfcp_qdio_sbale_next(fsf_req, sbtype);
287 atomic_inc(&fsf_req->adapter->qdio_outb_full);
288 zfcp_qdio_undo_sbals(fsf_req);
292 /* new piece must not exceed next page boundary */
293 length = min(remaining,
294 (PAGE_SIZE - ((unsigned long)addr &
297 sbale->length = length;
303 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
304 * @fsf_req: request to be processed
305 * @sbtype: SBALE flags
306 * @sg: scatter-gather list
307 * @max_sbals: upper bound for number of SBALs to be used
308 * Returns: number of bytes, or error (negativ)
310 int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
311 struct scatterlist *sg, int max_sbals)
313 struct qdio_buffer_element *sbale;
314 int retval, bytes = 0;
316 /* figure out last allowed SBAL */
317 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
319 /* set storage-block type for this request */
320 sbale = zfcp_qdio_sbale_req(fsf_req);
321 sbale->flags |= sbtype;
323 for (; sg; sg = sg_next(sg)) {
324 retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg),
331 /* assume that no other SBALEs are to follow in the same SBAL */
332 sbale = zfcp_qdio_sbale_curr(fsf_req);
333 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
339 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
340 * @fsf_req: pointer to struct zfcp_fsf_req
341 * Returns: 0 on success, error otherwise
343 int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
345 struct zfcp_adapter *adapter = fsf_req->adapter;
346 struct zfcp_qdio_queue *req_q = &adapter->req_q;
347 int first = fsf_req->sbal_first;
348 int count = fsf_req->sbal_number;
349 int retval, pci, pci_batch;
350 struct qdio_buffer_element *sbale;
352 /* acknowledgements for transferred buffers */
353 pci_batch = adapter->req_q_pci_batch + count;
354 if (unlikely(pci_batch >= ZFCP_QDIO_PCI_INTERVAL)) {
355 pci_batch %= ZFCP_QDIO_PCI_INTERVAL;
356 pci = first + count - (pci_batch + 1);
357 pci %= QDIO_MAX_BUFFERS_PER_Q;
358 sbale = zfcp_qdio_sbale(req_q, pci, 0);
359 sbale->flags |= SBAL_FLAGS0_PCI;
362 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
364 if (unlikely(retval)) {
365 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
369 /* account for transferred buffers */
370 atomic_sub(count, &req_q->count);
371 req_q->first += count;
372 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
373 adapter->req_q_pci_batch = pci_batch;
378 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
379 * @adapter: pointer to struct zfcp_adapter
380 * Returns: -ENOMEM on memory allocation error or return value from
383 int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
385 struct qdio_initialize *init_data;
387 if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
388 zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
391 init_data = &adapter->qdio_init_data;
393 init_data->cdev = adapter->ccw_device;
394 init_data->q_format = QDIO_ZFCP_QFMT;
395 memcpy(init_data->adapter_name, dev_name(&adapter->ccw_device->dev), 8);
396 ASCEBC(init_data->adapter_name, 8);
397 init_data->qib_param_field_format = 0;
398 init_data->qib_param_field = NULL;
399 init_data->input_slib_elements = NULL;
400 init_data->output_slib_elements = NULL;
401 init_data->no_input_qs = 1;
402 init_data->no_output_qs = 1;
403 init_data->input_handler = zfcp_qdio_int_resp;
404 init_data->output_handler = zfcp_qdio_int_req;
405 init_data->int_parm = (unsigned long) adapter;
406 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
407 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
408 init_data->input_sbal_addr_array =
409 (void **) (adapter->resp_q.sbal);
410 init_data->output_sbal_addr_array =
411 (void **) (adapter->req_q.sbal);
413 return qdio_allocate(init_data);
417 * zfcp_close_qdio - close qdio queues for an adapter
419 void zfcp_qdio_close(struct zfcp_adapter *adapter)
421 struct zfcp_qdio_queue *req_q;
424 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
427 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
428 req_q = &adapter->req_q;
429 spin_lock_bh(&adapter->req_q_lock);
430 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
431 spin_unlock_bh(&adapter->req_q_lock);
433 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
435 /* cleanup used outbound sbals */
436 count = atomic_read(&req_q->count);
437 if (count < QDIO_MAX_BUFFERS_PER_Q) {
438 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
439 count = QDIO_MAX_BUFFERS_PER_Q - count;
440 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
443 atomic_set(&req_q->count, 0);
444 adapter->req_q_pci_batch = 0;
445 adapter->resp_q.first = 0;
446 atomic_set(&adapter->resp_q.count, 0);
450 * zfcp_qdio_open - prepare and initialize response queue
451 * @adapter: pointer to struct zfcp_adapter
452 * Returns: 0 on success, otherwise -EIO
454 int zfcp_qdio_open(struct zfcp_adapter *adapter)
456 struct qdio_buffer_element *sbale;
459 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
462 if (qdio_establish(&adapter->qdio_init_data))
463 goto failed_establish;
465 if (qdio_activate(adapter->ccw_device))
468 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
469 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
471 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
475 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
476 QDIO_MAX_BUFFERS_PER_Q))
479 /* set index of first avalable SBALS / number of available SBALS */
480 adapter->req_q.first = 0;
481 atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
482 adapter->req_q_pci_batch = 0;
487 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
489 dev_err(&adapter->ccw_device->dev,
490 "Setting up the QDIO connection to the FCP adapter failed\n");