4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2008
11 /* FIXME(tune): free space should be one max. SBAL chain plus what? */
12 #define ZFCP_QDIO_PCI_INTERVAL (QDIO_MAX_BUFFERS_PER_Q \
13 - (FSF_MAX_SBALS_PER_REQ + 4))
14 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
16 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
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);
25 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
26 if (pos % QBUFF_PER_PAGE)
27 sbal[pos] = sbal[pos - 1] + 1;
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];
38 * zfcp_qdio_free - free memory used by request- and resposne queue
39 * @adapter: pointer to the zfcp_adapter structure
41 void zfcp_qdio_free(struct zfcp_adapter *adapter)
43 struct qdio_buffer **sbal_req, **sbal_resp;
46 if (adapter->ccw_device)
47 qdio_free(adapter->ccw_device);
49 sbal_req = adapter->req_q.sbal;
50 sbal_resp = adapter->resp_q.sbal;
52 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
53 free_page((unsigned long) sbal_req[p]);
54 free_page((unsigned long) sbal_resp[p]);
58 static void zfcp_qdio_handler_error(struct zfcp_adapter *adapter, u8 id)
60 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
62 zfcp_erp_adapter_reopen(adapter,
63 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
64 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
67 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
71 for (i = first; i < first + cnt; i++) {
72 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
73 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
77 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
78 int queue_no, int first, int count,
81 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
82 struct zfcp_qdio_queue *queue = &adapter->req_q;
84 if (unlikely(qdio_err)) {
85 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
86 zfcp_qdio_handler_error(adapter, 140);
90 /* cleanup all SBALs being program-owned now */
91 zfcp_qdio_zero_sbals(queue->sbal, first, count);
93 atomic_add(count, &queue->count);
94 wake_up(&adapter->request_wq);
97 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
98 unsigned long req_id, int sbal_idx)
100 struct zfcp_fsf_req *fsf_req;
103 spin_lock_irqsave(&adapter->req_list_lock, flags);
104 fsf_req = zfcp_reqlist_find(adapter, req_id);
108 * Unknown request means that we have potentially memory
109 * corruption and must stop the machine immediatly.
111 panic("error: unknown request id (%lx) on adapter %s.\n",
112 req_id, zfcp_get_busid_by_adapter(adapter));
114 zfcp_reqlist_remove(adapter, fsf_req);
115 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
117 fsf_req->sbal_response = sbal_idx;
118 zfcp_fsf_req_complete(fsf_req);
121 static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
123 struct zfcp_qdio_queue *queue = &adapter->resp_q;
124 struct ccw_device *cdev = adapter->ccw_device;
125 u8 count, start = queue->first;
128 count = atomic_read(&queue->count) + processed;
130 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
132 if (unlikely(retval)) {
133 atomic_set(&queue->count, count);
134 /* FIXME: Recover this with an adapter reopen? */
136 queue->first += count;
137 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
138 atomic_set(&queue->count, 0);
142 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
143 int queue_no, int first, int count,
146 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
147 struct zfcp_qdio_queue *queue = &adapter->resp_q;
148 struct qdio_buffer_element *sbale;
149 int sbal_idx, sbale_idx, sbal_no;
151 if (unlikely(qdio_err)) {
152 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
153 zfcp_qdio_handler_error(adapter, 147);
158 * go through all SBALs from input queue currently
159 * returned by QDIO layer
161 for (sbal_no = 0; sbal_no < count; sbal_no++) {
162 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
164 /* go through all SBALEs of SBAL */
165 for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER;
167 sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx);
168 zfcp_qdio_reqid_check(adapter,
169 (unsigned long) sbale->addr,
171 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
175 if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY)))
176 dev_warn(&adapter->ccw_device->dev,
177 "A QDIO protocol error occurred, "
178 "operations continue\n");
182 * put range of SBALs back to response queue
183 * (including SBALs which have already been free before)
185 zfcp_qdio_resp_put_back(adapter, count);
189 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
190 * @fsf_req: pointer to struct fsf_req
191 * Returns: pointer to qdio_buffer_element (SBALE) structure
193 struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_fsf_req *req)
195 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0);
199 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
200 * @fsf_req: pointer to struct fsf_req
201 * Returns: pointer to qdio_buffer_element (SBALE) structure
203 struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req)
205 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last,
209 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
211 int count = atomic_read(&fsf_req->adapter->req_q.count);
212 count = min(count, max_sbals);
213 fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1)
214 % QDIO_MAX_BUFFERS_PER_Q;
217 static struct qdio_buffer_element *
218 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
220 struct qdio_buffer_element *sbale;
222 /* set last entry flag in current SBALE of current SBAL */
223 sbale = zfcp_qdio_sbale_curr(fsf_req);
224 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
226 /* don't exceed last allowed SBAL */
227 if (fsf_req->sbal_last == fsf_req->sbal_limit)
230 /* set chaining flag in first SBALE of current SBAL */
231 sbale = zfcp_qdio_sbale_req(fsf_req);
232 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
234 /* calculate index of next SBAL */
235 fsf_req->sbal_last++;
236 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
238 /* keep this requests number of SBALs up-to-date */
239 fsf_req->sbal_number++;
241 /* start at first SBALE of new SBAL */
242 fsf_req->sbale_curr = 0;
244 /* set storage-block type for new SBAL */
245 sbale = zfcp_qdio_sbale_curr(fsf_req);
246 sbale->flags |= sbtype;
251 static struct qdio_buffer_element *
252 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
254 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
255 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
256 fsf_req->sbale_curr++;
257 return zfcp_qdio_sbale_curr(fsf_req);
260 static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req)
262 struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal;
263 int first = fsf_req->sbal_first;
264 int last = fsf_req->sbal_last;
265 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
266 QDIO_MAX_BUFFERS_PER_Q + 1;
267 zfcp_qdio_zero_sbals(sbal, first, count);
270 static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req,
271 unsigned int sbtype, void *start_addr,
272 unsigned int total_length)
274 struct qdio_buffer_element *sbale;
275 unsigned long remaining, length;
278 /* split segment up */
279 for (addr = start_addr, remaining = total_length; remaining > 0;
280 addr += length, remaining -= length) {
281 sbale = zfcp_qdio_sbale_next(fsf_req, sbtype);
283 atomic_inc(&fsf_req->adapter->qdio_outb_full);
284 zfcp_qdio_undo_sbals(fsf_req);
288 /* new piece must not exceed next page boundary */
289 length = min(remaining,
290 (PAGE_SIZE - ((unsigned long)addr &
293 sbale->length = length;
299 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
300 * @fsf_req: request to be processed
301 * @sbtype: SBALE flags
302 * @sg: scatter-gather list
303 * @max_sbals: upper bound for number of SBALs to be used
304 * Returns: number of bytes, or error (negativ)
306 int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
307 struct scatterlist *sg, int max_sbals)
309 struct qdio_buffer_element *sbale;
310 int retval, bytes = 0;
312 /* figure out last allowed SBAL */
313 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
315 /* set storage-block type for this request */
316 sbale = zfcp_qdio_sbale_req(fsf_req);
317 sbale->flags |= sbtype;
319 for (; sg; sg = sg_next(sg)) {
320 retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg),
327 /* assume that no other SBALEs are to follow in the same SBAL */
328 sbale = zfcp_qdio_sbale_curr(fsf_req);
329 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
335 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
336 * @fsf_req: pointer to struct zfcp_fsf_req
337 * Returns: 0 on success, error otherwise
339 int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
341 struct zfcp_adapter *adapter = fsf_req->adapter;
342 struct zfcp_qdio_queue *req_q = &adapter->req_q;
343 int first = fsf_req->sbal_first;
344 int count = fsf_req->sbal_number;
345 int retval, pci, pci_batch;
346 struct qdio_buffer_element *sbale;
348 /* acknowledgements for transferred buffers */
349 pci_batch = adapter->req_q_pci_batch + count;
350 if (unlikely(pci_batch >= ZFCP_QDIO_PCI_INTERVAL)) {
351 pci_batch %= ZFCP_QDIO_PCI_INTERVAL;
352 pci = first + count - (pci_batch + 1);
353 pci %= QDIO_MAX_BUFFERS_PER_Q;
354 sbale = zfcp_qdio_sbale(req_q, pci, 0);
355 sbale->flags |= SBAL_FLAGS0_PCI;
358 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
360 if (unlikely(retval)) {
361 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
365 /* account for transferred buffers */
366 atomic_sub(count, &req_q->count);
367 req_q->first += count;
368 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
369 adapter->req_q_pci_batch = pci_batch;
374 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
375 * @adapter: pointer to struct zfcp_adapter
376 * Returns: -ENOMEM on memory allocation error or return value from
379 int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
381 struct qdio_initialize *init_data;
383 if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
384 zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
387 init_data = &adapter->qdio_init_data;
389 init_data->cdev = adapter->ccw_device;
390 init_data->q_format = QDIO_ZFCP_QFMT;
391 memcpy(init_data->adapter_name, zfcp_get_busid_by_adapter(adapter), 8);
392 ASCEBC(init_data->adapter_name, 8);
393 init_data->qib_param_field_format = 0;
394 init_data->qib_param_field = NULL;
395 init_data->input_slib_elements = NULL;
396 init_data->output_slib_elements = NULL;
397 init_data->no_input_qs = 1;
398 init_data->no_output_qs = 1;
399 init_data->input_handler = zfcp_qdio_int_resp;
400 init_data->output_handler = zfcp_qdio_int_req;
401 init_data->int_parm = (unsigned long) adapter;
402 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
403 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
404 init_data->input_sbal_addr_array =
405 (void **) (adapter->resp_q.sbal);
406 init_data->output_sbal_addr_array =
407 (void **) (adapter->req_q.sbal);
409 return qdio_allocate(init_data);
413 * zfcp_close_qdio - close qdio queues for an adapter
415 void zfcp_qdio_close(struct zfcp_adapter *adapter)
417 struct zfcp_qdio_queue *req_q;
420 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
423 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
424 req_q = &adapter->req_q;
425 spin_lock_bh(&adapter->req_q_lock);
426 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
427 spin_unlock_bh(&adapter->req_q_lock);
429 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
431 /* cleanup used outbound sbals */
432 count = atomic_read(&req_q->count);
433 if (count < QDIO_MAX_BUFFERS_PER_Q) {
434 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
435 count = QDIO_MAX_BUFFERS_PER_Q - count;
436 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
439 atomic_set(&req_q->count, 0);
440 adapter->req_q_pci_batch = 0;
441 adapter->resp_q.first = 0;
442 atomic_set(&adapter->resp_q.count, 0);
446 * zfcp_qdio_open - prepare and initialize response queue
447 * @adapter: pointer to struct zfcp_adapter
448 * Returns: 0 on success, otherwise -EIO
450 int zfcp_qdio_open(struct zfcp_adapter *adapter)
452 struct qdio_buffer_element *sbale;
455 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
458 if (qdio_establish(&adapter->qdio_init_data))
459 goto failed_establish;
461 if (qdio_activate(adapter->ccw_device))
464 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
465 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
467 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
471 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
472 QDIO_MAX_BUFFERS_PER_Q))
475 /* set index of first avalable SBALS / number of available SBALS */
476 adapter->req_q.first = 0;
477 atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
478 adapter->req_q_pci_batch = 0;
483 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
485 dev_err(&adapter->ccw_device->dev,
486 "Setting up the QDIO connection to the FCP adapter failed\n");