4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corp. 2002, 2010
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/slab.h>
13 #include <linux/module.h>
15 #include "zfcp_qdio.h"
17 static bool enable_multibuffer = 1;
18 module_param_named(datarouter, enable_multibuffer, bool, 0400);
19 MODULE_PARM_DESC(datarouter, "Enable hardware data router support (default on)");
21 static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
22 unsigned int qdio_err)
24 struct zfcp_adapter *adapter = qdio->adapter;
26 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
28 if (qdio_err & QDIO_ERROR_SLSB_STATE) {
29 zfcp_qdio_siosl(adapter);
30 zfcp_erp_adapter_shutdown(adapter, 0, id);
33 zfcp_erp_adapter_reopen(adapter,
34 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
35 ZFCP_STATUS_COMMON_ERP_FAILED, id);
38 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
42 for (i = first; i < first + cnt; i++) {
43 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
44 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
48 /* this needs to be called prior to updating the queue fill level */
49 static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
51 unsigned long long now, span;
54 now = get_tod_clock_monotonic();
55 span = (now - qdio->req_q_time) >> 12;
56 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
57 qdio->req_q_util += used * span;
58 qdio->req_q_time = now;
61 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
62 int queue_no, int idx, int count,
65 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
67 if (unlikely(qdio_err)) {
68 zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
72 /* cleanup all SBALs being program-owned now */
73 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
75 spin_lock_irq(&qdio->stat_lock);
76 zfcp_qdio_account(qdio);
77 spin_unlock_irq(&qdio->stat_lock);
78 atomic_add(count, &qdio->req_q_free);
79 wake_up(&qdio->req_q_wq);
82 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
83 int queue_no, int idx, int count,
86 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
87 struct zfcp_adapter *adapter = qdio->adapter;
88 int sbal_no, sbal_idx;
90 if (unlikely(qdio_err)) {
91 if (zfcp_adapter_multi_buffer_active(adapter)) {
92 void *pl[ZFCP_QDIO_MAX_SBALS_PER_REQ + 1];
93 struct qdio_buffer_element *sbale;
98 ZFCP_QDIO_MAX_SBALS_PER_REQ * sizeof(void *));
99 sbale = qdio->res_q[idx]->element;
100 req_id = (u64) sbale->addr;
101 scount = min(sbale->scount + 1,
102 ZFCP_QDIO_MAX_SBALS_PER_REQ + 1);
103 /* incl. signaling SBAL */
105 for (sbal_no = 0; sbal_no < scount; sbal_no++) {
106 sbal_idx = (idx + sbal_no) %
107 QDIO_MAX_BUFFERS_PER_Q;
108 pl[sbal_no] = qdio->res_q[sbal_idx];
110 zfcp_dbf_hba_def_err(adapter, req_id, scount, pl);
112 zfcp_qdio_handler_error(qdio, "qdires1", qdio_err);
117 * go through all SBALs from input queue currently
118 * returned by QDIO layer
120 for (sbal_no = 0; sbal_no < count; sbal_no++) {
121 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
122 /* go through all SBALEs of SBAL */
123 zfcp_fsf_reqid_check(qdio, sbal_idx);
127 * put SBALs back to response queue
129 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
130 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2");
133 static struct qdio_buffer_element *
134 zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
136 struct qdio_buffer_element *sbale;
138 /* set last entry flag in current SBALE of current SBAL */
139 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
140 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
142 /* don't exceed last allowed SBAL */
143 if (q_req->sbal_last == q_req->sbal_limit)
146 /* set chaining flag in first SBALE of current SBAL */
147 sbale = zfcp_qdio_sbale_req(qdio, q_req);
148 sbale->sflags |= SBAL_SFLAGS0_MORE_SBALS;
150 /* calculate index of next SBAL */
152 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
154 /* keep this requests number of SBALs up-to-date */
155 q_req->sbal_number++;
156 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
158 /* start at first SBALE of new SBAL */
159 q_req->sbale_curr = 0;
161 /* set storage-block type for new SBAL */
162 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
163 sbale->sflags |= q_req->sbtype;
168 static struct qdio_buffer_element *
169 zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
171 if (q_req->sbale_curr == qdio->max_sbale_per_sbal - 1)
172 return zfcp_qdio_sbal_chain(qdio, q_req);
174 return zfcp_qdio_sbale_curr(qdio, q_req);
178 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
179 * @qdio: pointer to struct zfcp_qdio
180 * @q_req: pointer to struct zfcp_qdio_req
181 * @sg: scatter-gather list
182 * @max_sbals: upper bound for number of SBALs to be used
183 * Returns: zero or -EINVAL on error
185 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
186 struct scatterlist *sg)
188 struct qdio_buffer_element *sbale;
190 /* set storage-block type for this request */
191 sbale = zfcp_qdio_sbale_req(qdio, q_req);
192 sbale->sflags |= q_req->sbtype;
194 for (; sg; sg = sg_next(sg)) {
195 sbale = zfcp_qdio_sbale_next(qdio, q_req);
197 atomic_inc(&qdio->req_q_full);
198 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
202 sbale->addr = sg_virt(sg);
203 sbale->length = sg->length;
208 static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
210 if (atomic_read(&qdio->req_q_free) ||
211 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
217 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
218 * @qdio: pointer to struct zfcp_qdio
220 * The req_q_lock must be held by the caller of this function, and
221 * this function may only be called from process context; it will
222 * sleep when waiting for a free sbal.
224 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
226 int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
230 ret = wait_event_interruptible_lock_irq_timeout(qdio->req_q_wq,
231 zfcp_qdio_sbal_check(qdio), qdio->req_q_lock, 5 * HZ);
233 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
240 atomic_inc(&qdio->req_q_full);
241 /* assume hanging outbound queue, try queue recovery */
242 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
249 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
250 * @qdio: pointer to struct zfcp_qdio
251 * @q_req: pointer to struct zfcp_qdio_req
252 * Returns: 0 on success, error otherwise
254 int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
257 u8 sbal_number = q_req->sbal_number;
259 spin_lock(&qdio->stat_lock);
260 zfcp_qdio_account(qdio);
261 spin_unlock(&qdio->stat_lock);
263 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
264 q_req->sbal_first, sbal_number);
266 if (unlikely(retval)) {
267 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
272 /* account for transferred buffers */
273 atomic_sub(sbal_number, &qdio->req_q_free);
274 qdio->req_q_idx += sbal_number;
275 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
281 static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
282 struct zfcp_qdio *qdio)
284 memset(id, 0, sizeof(*id));
285 id->cdev = qdio->adapter->ccw_device;
286 id->q_format = QDIO_ZFCP_QFMT;
287 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
288 ASCEBC(id->adapter_name, 8);
289 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
290 if (enable_multibuffer)
291 id->qdr_ac |= QDR_AC_MULTI_BUFFER_ENABLE;
293 id->no_output_qs = 1;
294 id->input_handler = zfcp_qdio_int_resp;
295 id->output_handler = zfcp_qdio_int_req;
296 id->int_parm = (unsigned long) qdio;
297 id->input_sbal_addr_array = (void **) (qdio->res_q);
298 id->output_sbal_addr_array = (void **) (qdio->req_q);
300 QDIO_MAX_BUFFERS_PER_Q - ZFCP_QDIO_MAX_SBALS_PER_REQ * 2;
304 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
305 * @adapter: pointer to struct zfcp_adapter
306 * Returns: -ENOMEM on memory allocation error or return value from
309 static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
311 struct qdio_initialize init_data;
314 ret = qdio_alloc_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
318 ret = qdio_alloc_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
322 zfcp_qdio_setup_init_data(&init_data, qdio);
323 init_waitqueue_head(&qdio->req_q_wq);
325 ret = qdio_allocate(&init_data);
332 qdio_free_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
334 qdio_free_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
339 * zfcp_close_qdio - close qdio queues for an adapter
340 * @qdio: pointer to structure zfcp_qdio
342 void zfcp_qdio_close(struct zfcp_qdio *qdio)
344 struct zfcp_adapter *adapter = qdio->adapter;
347 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
350 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
351 spin_lock_irq(&qdio->req_q_lock);
352 atomic_andnot(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
353 spin_unlock_irq(&qdio->req_q_lock);
355 wake_up(&qdio->req_q_wq);
357 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
359 /* cleanup used outbound sbals */
360 count = atomic_read(&qdio->req_q_free);
361 if (count < QDIO_MAX_BUFFERS_PER_Q) {
362 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
363 count = QDIO_MAX_BUFFERS_PER_Q - count;
364 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
367 atomic_set(&qdio->req_q_free, 0);
371 * zfcp_qdio_open - prepare and initialize response queue
372 * @qdio: pointer to struct zfcp_qdio
373 * Returns: 0 on success, otherwise -EIO
375 int zfcp_qdio_open(struct zfcp_qdio *qdio)
377 struct qdio_buffer_element *sbale;
378 struct qdio_initialize init_data;
379 struct zfcp_adapter *adapter = qdio->adapter;
380 struct ccw_device *cdev = adapter->ccw_device;
381 struct qdio_ssqd_desc ssqd;
384 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
387 atomic_andnot(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
388 &qdio->adapter->status);
390 zfcp_qdio_setup_init_data(&init_data, qdio);
392 if (qdio_establish(&init_data))
393 goto failed_establish;
395 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
398 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
399 atomic_or(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
400 &qdio->adapter->status);
402 if (ssqd.qdioac2 & CHSC_AC2_MULTI_BUFFER_ENABLED) {
403 atomic_or(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
404 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER;
406 atomic_andnot(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
407 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER - 1;
410 qdio->max_sbale_per_req =
411 ZFCP_QDIO_MAX_SBALS_PER_REQ * qdio->max_sbale_per_sbal
413 if (qdio_activate(cdev))
416 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
417 sbale = &(qdio->res_q[cc]->element[0]);
419 sbale->eflags = SBAL_EFLAGS_LAST_ENTRY;
424 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
427 /* set index of first available SBALS / number of available SBALS */
429 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
430 atomic_or(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
432 if (adapter->scsi_host) {
433 adapter->scsi_host->sg_tablesize = qdio->max_sbale_per_req;
434 adapter->scsi_host->max_sectors = qdio->max_sbale_per_req * 8;
440 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
443 "Setting up the QDIO connection to the FCP adapter failed\n");
447 void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
452 if (qdio->adapter->ccw_device)
453 qdio_free(qdio->adapter->ccw_device);
455 qdio_free_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
456 qdio_free_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
460 int zfcp_qdio_setup(struct zfcp_adapter *adapter)
462 struct zfcp_qdio *qdio;
464 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
468 qdio->adapter = adapter;
470 if (zfcp_qdio_allocate(qdio)) {
475 spin_lock_init(&qdio->req_q_lock);
476 spin_lock_init(&qdio->stat_lock);
478 adapter->qdio = qdio;
483 * zfcp_qdio_siosl - Trigger logging in FCP channel
484 * @adapter: The zfcp_adapter where to trigger logging
486 * Call the cio siosl function to trigger hardware logging. This
487 * wrapper function sets a flag to ensure hardware logging is only
488 * triggered once before going through qdio shutdown.
490 * The triggers are always run from qdio tasklet context, so no
491 * additional synchronization is necessary.
493 void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
497 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
500 rc = ccw_device_siosl(adapter->ccw_device);
502 atomic_or(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,