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 volatile 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, "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 status,
78 unsigned int qdio_err, unsigned int siga_err,
79 unsigned int queue_no, int first, int count,
82 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
83 struct zfcp_qdio_queue *queue = &adapter->req_q;
85 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
86 zfcp_hba_dbf_event_qdio(adapter, status, qdio_err, siga_err,
88 zfcp_qdio_handler_error(adapter, 140);
92 /* cleanup all SBALs being program-owned now */
93 zfcp_qdio_zero_sbals(queue->sbal, first, count);
95 atomic_add(count, &queue->count);
96 wake_up(&adapter->request_wq);
99 static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter,
100 unsigned long req_id, int sbal_idx)
102 struct zfcp_fsf_req *fsf_req;
105 spin_lock_irqsave(&adapter->req_list_lock, flags);
106 fsf_req = zfcp_reqlist_find(adapter, req_id);
110 * Unknown request means that we have potentially memory
111 * corruption and must stop the machine immediatly.
113 panic("error: unknown request id (%lx) on adapter %s.\n",
114 req_id, zfcp_get_busid_by_adapter(adapter));
116 zfcp_reqlist_remove(adapter, fsf_req);
117 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
119 fsf_req->sbal_response = sbal_idx;
120 zfcp_fsf_req_complete(fsf_req);
123 static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
125 struct zfcp_qdio_queue *queue = &adapter->resp_q;
126 struct ccw_device *cdev = adapter->ccw_device;
127 u8 count, start = queue->first;
130 count = atomic_read(&queue->count) + processed;
132 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT,
133 0, start, count, NULL);
135 if (unlikely(retval)) {
136 atomic_set(&queue->count, count);
137 /* FIXME: Recover this with an adapter reopen? */
139 queue->first += count;
140 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
141 atomic_set(&queue->count, 0);
145 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int status,
146 unsigned int qdio_err, unsigned int siga_err,
147 unsigned 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 volatile struct qdio_buffer_element *sbale;
153 int sbal_idx, sbale_idx, sbal_no;
155 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) {
156 zfcp_hba_dbf_event_qdio(adapter, status, qdio_err, siga_err,
158 zfcp_qdio_handler_error(adapter, 147);
163 * go through all SBALs from input queue currently
164 * returned by QDIO layer
166 for (sbal_no = 0; sbal_no < count; sbal_no++) {
167 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
169 /* go through all SBALEs of SBAL */
170 for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER;
172 sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx);
173 zfcp_qdio_reqid_check(adapter,
174 (unsigned long) sbale->addr,
176 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
180 if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY)))
181 dev_warn(&adapter->ccw_device->dev,
182 "Protocol violation by adapter. "
183 "Continuing operations.\n");
187 * put range of SBALs back to response queue
188 * (including SBALs which have already been free before)
190 zfcp_qdio_resp_put_back(adapter, count);
194 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
195 * @fsf_req: pointer to struct fsf_req
196 * Returns: pointer to qdio_buffer_element (SBALE) structure
198 volatile struct qdio_buffer_element *
199 zfcp_qdio_sbale_req(struct zfcp_fsf_req *req)
201 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0);
205 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
206 * @fsf_req: pointer to struct fsf_req
207 * Returns: pointer to qdio_buffer_element (SBALE) structure
209 volatile struct qdio_buffer_element *
210 zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req)
212 return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last,
216 static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals)
218 int count = atomic_read(&fsf_req->adapter->req_q.count);
219 count = min(count, max_sbals);
220 fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1)
221 % QDIO_MAX_BUFFERS_PER_Q;
224 static volatile struct qdio_buffer_element *
225 zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
227 volatile struct qdio_buffer_element *sbale;
229 /* set last entry flag in current SBALE of current SBAL */
230 sbale = zfcp_qdio_sbale_curr(fsf_req);
231 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
233 /* don't exceed last allowed SBAL */
234 if (fsf_req->sbal_last == fsf_req->sbal_limit)
237 /* set chaining flag in first SBALE of current SBAL */
238 sbale = zfcp_qdio_sbale_req(fsf_req);
239 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
241 /* calculate index of next SBAL */
242 fsf_req->sbal_last++;
243 fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
245 /* keep this requests number of SBALs up-to-date */
246 fsf_req->sbal_number++;
248 /* start at first SBALE of new SBAL */
249 fsf_req->sbale_curr = 0;
251 /* set storage-block type for new SBAL */
252 sbale = zfcp_qdio_sbale_curr(fsf_req);
253 sbale->flags |= sbtype;
258 static volatile struct qdio_buffer_element *
259 zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype)
261 if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
262 return zfcp_qdio_sbal_chain(fsf_req, sbtype);
263 fsf_req->sbale_curr++;
264 return zfcp_qdio_sbale_curr(fsf_req);
267 static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req)
269 struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal;
270 int first = fsf_req->sbal_first;
271 int last = fsf_req->sbal_last;
272 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
273 QDIO_MAX_BUFFERS_PER_Q + 1;
274 zfcp_qdio_zero_sbals(sbal, first, count);
277 static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req,
278 unsigned int sbtype, void *start_addr,
279 unsigned int total_length)
281 volatile struct qdio_buffer_element *sbale;
282 unsigned long remaining, length;
285 /* split segment up */
286 for (addr = start_addr, remaining = total_length; remaining > 0;
287 addr += length, remaining -= length) {
288 sbale = zfcp_qdio_sbale_next(fsf_req, sbtype);
290 zfcp_qdio_undo_sbals(fsf_req);
294 /* new piece must not exceed next page boundary */
295 length = min(remaining,
296 (PAGE_SIZE - ((unsigned long)addr &
299 sbale->length = length;
305 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
306 * @fsf_req: request to be processed
307 * @sbtype: SBALE flags
308 * @sg: scatter-gather list
309 * @max_sbals: upper bound for number of SBALs to be used
310 * Returns: number of bytes, or error (negativ)
312 int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
313 struct scatterlist *sg, int max_sbals)
315 volatile struct qdio_buffer_element *sbale;
316 int retval, bytes = 0;
318 /* figure out last allowed SBAL */
319 zfcp_qdio_sbal_limit(fsf_req, max_sbals);
321 /* set storage-block type for this request */
322 sbale = zfcp_qdio_sbale_req(fsf_req);
323 sbale->flags |= sbtype;
325 for (; sg; sg = sg_next(sg)) {
326 retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg),
333 /* assume that no other SBALEs are to follow in the same SBAL */
334 sbale = zfcp_qdio_sbale_curr(fsf_req);
335 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
341 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
342 * @fsf_req: pointer to struct zfcp_fsf_req
343 * Returns: 0 on success, error otherwise
345 int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
347 struct zfcp_adapter *adapter = fsf_req->adapter;
348 struct zfcp_qdio_queue *req_q = &adapter->req_q;
349 int first = fsf_req->sbal_first;
350 int count = fsf_req->sbal_number;
351 int retval, pci, pci_batch;
352 volatile struct qdio_buffer_element *sbale;
354 /* acknowledgements for transferred buffers */
355 pci_batch = req_q->pci_batch + count;
356 if (unlikely(pci_batch >= ZFCP_QDIO_PCI_INTERVAL)) {
357 pci_batch %= ZFCP_QDIO_PCI_INTERVAL;
358 pci = first + count - (pci_batch + 1);
359 pci %= QDIO_MAX_BUFFERS_PER_Q;
360 sbale = zfcp_qdio_sbale(req_q, pci, 0);
361 sbale->flags |= SBAL_FLAGS0_PCI;
364 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
366 if (unlikely(retval)) {
367 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
371 /* account for transferred buffers */
372 atomic_sub(count, &req_q->count);
373 req_q->first += count;
374 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
375 req_q->pci_batch = pci_batch;
380 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
381 * @adapter: pointer to struct zfcp_adapter
382 * Returns: -ENOMEM on memory allocation error or return value from
385 int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
387 struct qdio_initialize *init_data;
389 if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) ||
390 zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal))
393 init_data = &adapter->qdio_init_data;
395 init_data->cdev = adapter->ccw_device;
396 init_data->q_format = QDIO_ZFCP_QFMT;
397 memcpy(init_data->adapter_name, zfcp_get_busid_by_adapter(adapter), 8);
398 ASCEBC(init_data->adapter_name, 8);
399 init_data->qib_param_field_format = 0;
400 init_data->qib_param_field = NULL;
401 init_data->input_slib_elements = NULL;
402 init_data->output_slib_elements = NULL;
403 init_data->min_input_threshold = 1;
404 init_data->max_input_threshold = 5000;
405 init_data->min_output_threshold = 1;
406 init_data->max_output_threshold = 1000;
407 init_data->no_input_qs = 1;
408 init_data->no_output_qs = 1;
409 init_data->input_handler = zfcp_qdio_int_resp;
410 init_data->output_handler = zfcp_qdio_int_req;
411 init_data->int_parm = (unsigned long) adapter;
412 init_data->flags = QDIO_INBOUND_0COPY_SBALS |
413 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
414 init_data->input_sbal_addr_array =
415 (void **) (adapter->resp_q.sbal);
416 init_data->output_sbal_addr_array =
417 (void **) (adapter->req_q.sbal);
419 return qdio_allocate(init_data);
423 * zfcp_close_qdio - close qdio queues for an adapter
425 void zfcp_qdio_close(struct zfcp_adapter *adapter)
427 struct zfcp_qdio_queue *req_q;
430 if (!atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status))
433 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
434 req_q = &adapter->req_q;
435 spin_lock(&req_q->lock);
436 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
437 spin_unlock(&req_q->lock);
439 while (qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR)
443 /* cleanup used outbound sbals */
444 count = atomic_read(&req_q->count);
445 if (count < QDIO_MAX_BUFFERS_PER_Q) {
446 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
447 count = QDIO_MAX_BUFFERS_PER_Q - count;
448 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
451 atomic_set(&req_q->count, 0);
452 req_q->pci_batch = 0;
453 adapter->resp_q.first = 0;
454 atomic_set(&adapter->resp_q.count, 0);
458 * zfcp_qdio_open - prepare and initialize response queue
459 * @adapter: pointer to struct zfcp_adapter
460 * Returns: 0 on success, otherwise -EIO
462 int zfcp_qdio_open(struct zfcp_adapter *adapter)
464 volatile struct qdio_buffer_element *sbale;
467 if (atomic_test_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status))
470 if (qdio_establish(&adapter->qdio_init_data)) {
471 dev_err(&adapter->ccw_device->dev,
472 "Establish of QDIO queues failed.\n");
476 if (qdio_activate(adapter->ccw_device, 0)) {
477 dev_err(&adapter->ccw_device->dev,
478 "Activate of QDIO queues failed.\n");
482 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
483 sbale = &(adapter->resp_q.sbal[cc]->element[0]);
485 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
489 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
490 QDIO_MAX_BUFFERS_PER_Q, NULL)) {
491 dev_err(&adapter->ccw_device->dev,
492 "Init of QDIO response queue failed.\n");
496 /* set index of first avalable SBALS / number of available SBALS */
497 adapter->req_q.first = 0;
498 atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
499 adapter->req_q.pci_batch = 0;
504 while (qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR)