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 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
19 static bool enable_multibuffer
;
20 module_param_named(datarouter
, enable_multibuffer
, bool, 0400);
21 MODULE_PARM_DESC(datarouter
, "Enable hardware data router support");
23 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer
**sbal
)
27 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
+= QBUFF_PER_PAGE
) {
28 sbal
[pos
] = (struct qdio_buffer
*) get_zeroed_page(GFP_KERNEL
);
32 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
++)
33 if (pos
% QBUFF_PER_PAGE
)
34 sbal
[pos
] = sbal
[pos
- 1] + 1;
38 static void zfcp_qdio_handler_error(struct zfcp_qdio
*qdio
, char *id
,
39 unsigned int qdio_err
)
41 struct zfcp_adapter
*adapter
= qdio
->adapter
;
43 dev_warn(&adapter
->ccw_device
->dev
, "A QDIO problem occurred\n");
45 if (qdio_err
& QDIO_ERROR_SLSB_STATE
) {
46 zfcp_qdio_siosl(adapter
);
47 zfcp_erp_adapter_shutdown(adapter
, 0, id
);
50 zfcp_erp_adapter_reopen(adapter
,
51 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
|
52 ZFCP_STATUS_COMMON_ERP_FAILED
, id
);
55 static void zfcp_qdio_zero_sbals(struct qdio_buffer
*sbal
[], int first
, int cnt
)
59 for (i
= first
; i
< first
+ cnt
; i
++) {
60 sbal_idx
= i
% QDIO_MAX_BUFFERS_PER_Q
;
61 memset(sbal
[sbal_idx
], 0, sizeof(struct qdio_buffer
));
65 /* this needs to be called prior to updating the queue fill level */
66 static inline void zfcp_qdio_account(struct zfcp_qdio
*qdio
)
68 unsigned long long now
, span
;
71 now
= get_tod_clock_monotonic();
72 span
= (now
- qdio
->req_q_time
) >> 12;
73 used
= QDIO_MAX_BUFFERS_PER_Q
- atomic_read(&qdio
->req_q_free
);
74 qdio
->req_q_util
+= used
* span
;
75 qdio
->req_q_time
= now
;
78 static void zfcp_qdio_int_req(struct ccw_device
*cdev
, unsigned int qdio_err
,
79 int queue_no
, int idx
, int count
,
82 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
84 if (unlikely(qdio_err
)) {
85 zfcp_qdio_handler_error(qdio
, "qdireq1", qdio_err
);
89 /* cleanup all SBALs being program-owned now */
90 zfcp_qdio_zero_sbals(qdio
->req_q
, idx
, count
);
92 spin_lock_irq(&qdio
->stat_lock
);
93 zfcp_qdio_account(qdio
);
94 spin_unlock_irq(&qdio
->stat_lock
);
95 atomic_add(count
, &qdio
->req_q_free
);
96 wake_up(&qdio
->req_q_wq
);
99 static void zfcp_qdio_int_resp(struct ccw_device
*cdev
, unsigned int qdio_err
,
100 int queue_no
, int idx
, int count
,
103 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
104 struct zfcp_adapter
*adapter
= qdio
->adapter
;
105 int sbal_no
, sbal_idx
;
107 if (unlikely(qdio_err
)) {
108 if (zfcp_adapter_multi_buffer_active(adapter
)) {
109 void *pl
[ZFCP_QDIO_MAX_SBALS_PER_REQ
+ 1];
110 struct qdio_buffer_element
*sbale
;
115 ZFCP_QDIO_MAX_SBALS_PER_REQ
* sizeof(void *));
116 sbale
= qdio
->res_q
[idx
]->element
;
117 req_id
= (u64
) sbale
->addr
;
118 scount
= min(sbale
->scount
+ 1,
119 ZFCP_QDIO_MAX_SBALS_PER_REQ
+ 1);
120 /* incl. signaling SBAL */
122 for (sbal_no
= 0; sbal_no
< scount
; sbal_no
++) {
123 sbal_idx
= (idx
+ sbal_no
) %
124 QDIO_MAX_BUFFERS_PER_Q
;
125 pl
[sbal_no
] = qdio
->res_q
[sbal_idx
];
127 zfcp_dbf_hba_def_err(adapter
, req_id
, scount
, pl
);
129 zfcp_qdio_handler_error(qdio
, "qdires1", qdio_err
);
134 * go through all SBALs from input queue currently
135 * returned by QDIO layer
137 for (sbal_no
= 0; sbal_no
< count
; sbal_no
++) {
138 sbal_idx
= (idx
+ sbal_no
) % QDIO_MAX_BUFFERS_PER_Q
;
139 /* go through all SBALEs of SBAL */
140 zfcp_fsf_reqid_check(qdio
, sbal_idx
);
144 * put SBALs back to response queue
146 if (do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, idx
, count
))
147 zfcp_erp_adapter_reopen(qdio
->adapter
, 0, "qdires2");
150 static struct qdio_buffer_element
*
151 zfcp_qdio_sbal_chain(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
153 struct qdio_buffer_element
*sbale
;
155 /* set last entry flag in current SBALE of current SBAL */
156 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
157 sbale
->eflags
|= SBAL_EFLAGS_LAST_ENTRY
;
159 /* don't exceed last allowed SBAL */
160 if (q_req
->sbal_last
== q_req
->sbal_limit
)
163 /* set chaining flag in first SBALE of current SBAL */
164 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
165 sbale
->sflags
|= SBAL_SFLAGS0_MORE_SBALS
;
167 /* calculate index of next SBAL */
169 q_req
->sbal_last
%= QDIO_MAX_BUFFERS_PER_Q
;
171 /* keep this requests number of SBALs up-to-date */
172 q_req
->sbal_number
++;
173 BUG_ON(q_req
->sbal_number
> ZFCP_QDIO_MAX_SBALS_PER_REQ
);
175 /* start at first SBALE of new SBAL */
176 q_req
->sbale_curr
= 0;
178 /* set storage-block type for new SBAL */
179 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
180 sbale
->sflags
|= q_req
->sbtype
;
185 static struct qdio_buffer_element
*
186 zfcp_qdio_sbale_next(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
188 if (q_req
->sbale_curr
== qdio
->max_sbale_per_sbal
- 1)
189 return zfcp_qdio_sbal_chain(qdio
, q_req
);
191 return zfcp_qdio_sbale_curr(qdio
, q_req
);
195 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
196 * @qdio: pointer to struct zfcp_qdio
197 * @q_req: pointer to struct zfcp_qdio_req
198 * @sg: scatter-gather list
199 * @max_sbals: upper bound for number of SBALs to be used
200 * Returns: zero or -EINVAL on error
202 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
,
203 struct scatterlist
*sg
)
205 struct qdio_buffer_element
*sbale
;
207 /* set storage-block type for this request */
208 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
209 sbale
->sflags
|= q_req
->sbtype
;
211 for (; sg
; sg
= sg_next(sg
)) {
212 sbale
= zfcp_qdio_sbale_next(qdio
, q_req
);
214 atomic_inc(&qdio
->req_q_full
);
215 zfcp_qdio_zero_sbals(qdio
->req_q
, q_req
->sbal_first
,
219 sbale
->addr
= sg_virt(sg
);
220 sbale
->length
= sg
->length
;
225 static int zfcp_qdio_sbal_check(struct zfcp_qdio
*qdio
)
227 spin_lock_irq(&qdio
->req_q_lock
);
228 if (atomic_read(&qdio
->req_q_free
) ||
229 !(atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
231 spin_unlock_irq(&qdio
->req_q_lock
);
236 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
237 * @qdio: pointer to struct zfcp_qdio
239 * The req_q_lock must be held by the caller of this function, and
240 * this function may only be called from process context; it will
241 * sleep when waiting for a free sbal.
243 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
245 int zfcp_qdio_sbal_get(struct zfcp_qdio
*qdio
)
249 spin_unlock_irq(&qdio
->req_q_lock
);
250 ret
= wait_event_interruptible_timeout(qdio
->req_q_wq
,
251 zfcp_qdio_sbal_check(qdio
), 5 * HZ
);
253 if (!(atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
260 atomic_inc(&qdio
->req_q_full
);
261 /* assume hanging outbound queue, try queue recovery */
262 zfcp_erp_adapter_reopen(qdio
->adapter
, 0, "qdsbg_1");
265 spin_lock_irq(&qdio
->req_q_lock
);
270 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
271 * @qdio: pointer to struct zfcp_qdio
272 * @q_req: pointer to struct zfcp_qdio_req
273 * Returns: 0 on success, error otherwise
275 int zfcp_qdio_send(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
278 u8 sbal_number
= q_req
->sbal_number
;
280 spin_lock(&qdio
->stat_lock
);
281 zfcp_qdio_account(qdio
);
282 spin_unlock(&qdio
->stat_lock
);
284 retval
= do_QDIO(qdio
->adapter
->ccw_device
, QDIO_FLAG_SYNC_OUTPUT
, 0,
285 q_req
->sbal_first
, sbal_number
);
287 if (unlikely(retval
)) {
288 zfcp_qdio_zero_sbals(qdio
->req_q
, q_req
->sbal_first
,
293 /* account for transferred buffers */
294 atomic_sub(sbal_number
, &qdio
->req_q_free
);
295 qdio
->req_q_idx
+= sbal_number
;
296 qdio
->req_q_idx
%= QDIO_MAX_BUFFERS_PER_Q
;
302 static void zfcp_qdio_setup_init_data(struct qdio_initialize
*id
,
303 struct zfcp_qdio
*qdio
)
305 memset(id
, 0, sizeof(*id
));
306 id
->cdev
= qdio
->adapter
->ccw_device
;
307 id
->q_format
= QDIO_ZFCP_QFMT
;
308 memcpy(id
->adapter_name
, dev_name(&id
->cdev
->dev
), 8);
309 ASCEBC(id
->adapter_name
, 8);
310 id
->qib_rflags
= QIB_RFLAGS_ENABLE_DATA_DIV
;
311 if (enable_multibuffer
)
312 id
->qdr_ac
|= QDR_AC_MULTI_BUFFER_ENABLE
;
314 id
->no_output_qs
= 1;
315 id
->input_handler
= zfcp_qdio_int_resp
;
316 id
->output_handler
= zfcp_qdio_int_req
;
317 id
->int_parm
= (unsigned long) qdio
;
318 id
->input_sbal_addr_array
= (void **) (qdio
->res_q
);
319 id
->output_sbal_addr_array
= (void **) (qdio
->req_q
);
321 QDIO_MAX_BUFFERS_PER_Q
- ZFCP_QDIO_MAX_SBALS_PER_REQ
* 2;
325 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
326 * @adapter: pointer to struct zfcp_adapter
327 * Returns: -ENOMEM on memory allocation error or return value from
330 static int zfcp_qdio_allocate(struct zfcp_qdio
*qdio
)
332 struct qdio_initialize init_data
;
334 if (zfcp_qdio_buffers_enqueue(qdio
->req_q
) ||
335 zfcp_qdio_buffers_enqueue(qdio
->res_q
))
338 zfcp_qdio_setup_init_data(&init_data
, qdio
);
339 init_waitqueue_head(&qdio
->req_q_wq
);
341 return qdio_allocate(&init_data
);
345 * zfcp_close_qdio - close qdio queues for an adapter
346 * @qdio: pointer to structure zfcp_qdio
348 void zfcp_qdio_close(struct zfcp_qdio
*qdio
)
350 struct zfcp_adapter
*adapter
= qdio
->adapter
;
353 if (!(atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
356 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
357 spin_lock_irq(&qdio
->req_q_lock
);
358 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP
, &adapter
->status
);
359 spin_unlock_irq(&qdio
->req_q_lock
);
361 wake_up(&qdio
->req_q_wq
);
363 qdio_shutdown(adapter
->ccw_device
, QDIO_FLAG_CLEANUP_USING_CLEAR
);
365 /* cleanup used outbound sbals */
366 count
= atomic_read(&qdio
->req_q_free
);
367 if (count
< QDIO_MAX_BUFFERS_PER_Q
) {
368 idx
= (qdio
->req_q_idx
+ count
) % QDIO_MAX_BUFFERS_PER_Q
;
369 count
= QDIO_MAX_BUFFERS_PER_Q
- count
;
370 zfcp_qdio_zero_sbals(qdio
->req_q
, idx
, count
);
373 atomic_set(&qdio
->req_q_free
, 0);
377 * zfcp_qdio_open - prepare and initialize response queue
378 * @qdio: pointer to struct zfcp_qdio
379 * Returns: 0 on success, otherwise -EIO
381 int zfcp_qdio_open(struct zfcp_qdio
*qdio
)
383 struct qdio_buffer_element
*sbale
;
384 struct qdio_initialize init_data
;
385 struct zfcp_adapter
*adapter
= qdio
->adapter
;
386 struct ccw_device
*cdev
= adapter
->ccw_device
;
387 struct qdio_ssqd_desc ssqd
;
390 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
)
393 atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
,
394 &qdio
->adapter
->status
);
396 zfcp_qdio_setup_init_data(&init_data
, qdio
);
398 if (qdio_establish(&init_data
))
399 goto failed_establish
;
401 if (qdio_get_ssqd_desc(init_data
.cdev
, &ssqd
))
404 if (ssqd
.qdioac2
& CHSC_AC2_DATA_DIV_ENABLED
)
405 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED
,
406 &qdio
->adapter
->status
);
408 if (ssqd
.qdioac2
& CHSC_AC2_MULTI_BUFFER_ENABLED
) {
409 atomic_set_mask(ZFCP_STATUS_ADAPTER_MB_ACT
, &adapter
->status
);
410 qdio
->max_sbale_per_sbal
= QDIO_MAX_ELEMENTS_PER_BUFFER
;
412 atomic_clear_mask(ZFCP_STATUS_ADAPTER_MB_ACT
, &adapter
->status
);
413 qdio
->max_sbale_per_sbal
= QDIO_MAX_ELEMENTS_PER_BUFFER
- 1;
416 qdio
->max_sbale_per_req
=
417 ZFCP_QDIO_MAX_SBALS_PER_REQ
* qdio
->max_sbale_per_sbal
419 if (qdio_activate(cdev
))
422 for (cc
= 0; cc
< QDIO_MAX_BUFFERS_PER_Q
; cc
++) {
423 sbale
= &(qdio
->res_q
[cc
]->element
[0]);
425 sbale
->eflags
= SBAL_EFLAGS_LAST_ENTRY
;
430 if (do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, 0, QDIO_MAX_BUFFERS_PER_Q
))
433 /* set index of first available SBALS / number of available SBALS */
435 atomic_set(&qdio
->req_q_free
, QDIO_MAX_BUFFERS_PER_Q
);
436 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP
, &qdio
->adapter
->status
);
438 if (adapter
->scsi_host
) {
439 adapter
->scsi_host
->sg_tablesize
= qdio
->max_sbale_per_req
;
440 adapter
->scsi_host
->max_sectors
= qdio
->max_sbale_per_req
* 8;
446 qdio_shutdown(cdev
, QDIO_FLAG_CLEANUP_USING_CLEAR
);
449 "Setting up the QDIO connection to the FCP adapter failed\n");
453 void zfcp_qdio_destroy(struct zfcp_qdio
*qdio
)
460 if (qdio
->adapter
->ccw_device
)
461 qdio_free(qdio
->adapter
->ccw_device
);
463 for (p
= 0; p
< QDIO_MAX_BUFFERS_PER_Q
; p
+= QBUFF_PER_PAGE
) {
464 free_page((unsigned long) qdio
->req_q
[p
]);
465 free_page((unsigned long) qdio
->res_q
[p
]);
471 int zfcp_qdio_setup(struct zfcp_adapter
*adapter
)
473 struct zfcp_qdio
*qdio
;
475 qdio
= kzalloc(sizeof(struct zfcp_qdio
), GFP_KERNEL
);
479 qdio
->adapter
= adapter
;
481 if (zfcp_qdio_allocate(qdio
)) {
482 zfcp_qdio_destroy(qdio
);
486 spin_lock_init(&qdio
->req_q_lock
);
487 spin_lock_init(&qdio
->stat_lock
);
489 adapter
->qdio
= qdio
;
494 * zfcp_qdio_siosl - Trigger logging in FCP channel
495 * @adapter: The zfcp_adapter where to trigger logging
497 * Call the cio siosl function to trigger hardware logging. This
498 * wrapper function sets a flag to ensure hardware logging is only
499 * triggered once before going through qdio shutdown.
501 * The triggers are always run from qdio tasklet context, so no
502 * additional synchronization is necessary.
504 void zfcp_qdio_siosl(struct zfcp_adapter
*adapter
)
508 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
)
511 rc
= ccw_device_siosl(adapter
->ccw_device
);
513 atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
,