4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2010
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/slab.h>
14 #include "zfcp_qdio.h"
16 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
18 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer
**sbal
)
22 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
+= QBUFF_PER_PAGE
) {
23 sbal
[pos
] = (struct qdio_buffer
*) get_zeroed_page(GFP_KERNEL
);
27 for (pos
= 0; pos
< QDIO_MAX_BUFFERS_PER_Q
; pos
++)
28 if (pos
% QBUFF_PER_PAGE
)
29 sbal
[pos
] = sbal
[pos
- 1] + 1;
33 static void zfcp_qdio_handler_error(struct zfcp_qdio
*qdio
, char *id
,
34 unsigned int qdio_err
)
36 struct zfcp_adapter
*adapter
= qdio
->adapter
;
38 dev_warn(&adapter
->ccw_device
->dev
, "A QDIO problem occurred\n");
40 if (qdio_err
& QDIO_ERROR_SLSB_STATE
)
41 zfcp_qdio_siosl(adapter
);
42 zfcp_erp_adapter_reopen(adapter
,
43 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
|
44 ZFCP_STATUS_COMMON_ERP_FAILED
, id
, NULL
);
47 static void zfcp_qdio_zero_sbals(struct qdio_buffer
*sbal
[], int first
, int cnt
)
51 for (i
= first
; i
< first
+ cnt
; i
++) {
52 sbal_idx
= i
% QDIO_MAX_BUFFERS_PER_Q
;
53 memset(sbal
[sbal_idx
], 0, sizeof(struct qdio_buffer
));
57 /* this needs to be called prior to updating the queue fill level */
58 static inline void zfcp_qdio_account(struct zfcp_qdio
*qdio
)
60 unsigned long long now
, span
;
63 spin_lock(&qdio
->stat_lock
);
64 now
= get_clock_monotonic();
65 span
= (now
- qdio
->req_q_time
) >> 12;
66 used
= QDIO_MAX_BUFFERS_PER_Q
- atomic_read(&qdio
->req_q_free
);
67 qdio
->req_q_util
+= used
* span
;
68 qdio
->req_q_time
= now
;
69 spin_unlock(&qdio
->stat_lock
);
72 static void zfcp_qdio_int_req(struct ccw_device
*cdev
, unsigned int qdio_err
,
73 int queue_no
, int idx
, int count
,
76 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
78 if (unlikely(qdio_err
)) {
79 zfcp_dbf_hba_qdio(qdio
->adapter
->dbf
, qdio_err
, idx
, count
);
80 zfcp_qdio_handler_error(qdio
, "qdireq1", qdio_err
);
84 /* cleanup all SBALs being program-owned now */
85 zfcp_qdio_zero_sbals(qdio
->req_q
, idx
, count
);
87 zfcp_qdio_account(qdio
);
88 atomic_add(count
, &qdio
->req_q_free
);
89 wake_up(&qdio
->req_q_wq
);
92 static void zfcp_qdio_int_resp(struct ccw_device
*cdev
, unsigned int qdio_err
,
93 int queue_no
, int idx
, int count
,
96 struct zfcp_qdio
*qdio
= (struct zfcp_qdio
*) parm
;
97 int sbal_idx
, sbal_no
;
99 if (unlikely(qdio_err
)) {
100 zfcp_dbf_hba_qdio(qdio
->adapter
->dbf
, qdio_err
, idx
, count
);
101 zfcp_qdio_handler_error(qdio
, "qdires1", qdio_err
);
106 * go through all SBALs from input queue currently
107 * returned by QDIO layer
109 for (sbal_no
= 0; sbal_no
< count
; sbal_no
++) {
110 sbal_idx
= (idx
+ sbal_no
) % QDIO_MAX_BUFFERS_PER_Q
;
111 /* go through all SBALEs of SBAL */
112 zfcp_fsf_reqid_check(qdio
, sbal_idx
);
116 * put SBALs back to response queue
118 if (do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, idx
, count
))
119 zfcp_erp_adapter_reopen(qdio
->adapter
, 0, "qdires2", NULL
);
122 static struct qdio_buffer_element
*
123 zfcp_qdio_sbal_chain(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
125 struct qdio_buffer_element
*sbale
;
127 /* set last entry flag in current SBALE of current SBAL */
128 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
129 sbale
->flags
|= SBAL_FLAGS_LAST_ENTRY
;
131 /* don't exceed last allowed SBAL */
132 if (q_req
->sbal_last
== q_req
->sbal_limit
)
135 /* set chaining flag in first SBALE of current SBAL */
136 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
137 sbale
->flags
|= SBAL_FLAGS0_MORE_SBALS
;
139 /* calculate index of next SBAL */
141 q_req
->sbal_last
%= QDIO_MAX_BUFFERS_PER_Q
;
143 /* keep this requests number of SBALs up-to-date */
144 q_req
->sbal_number
++;
145 BUG_ON(q_req
->sbal_number
> ZFCP_QDIO_MAX_SBALS_PER_REQ
);
147 /* start at first SBALE of new SBAL */
148 q_req
->sbale_curr
= 0;
150 /* set storage-block type for new SBAL */
151 sbale
= zfcp_qdio_sbale_curr(qdio
, q_req
);
152 sbale
->flags
|= q_req
->sbtype
;
157 static struct qdio_buffer_element
*
158 zfcp_qdio_sbale_next(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
160 if (q_req
->sbale_curr
== ZFCP_QDIO_LAST_SBALE_PER_SBAL
)
161 return zfcp_qdio_sbal_chain(qdio
, q_req
);
163 return zfcp_qdio_sbale_curr(qdio
, q_req
);
167 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
168 * @qdio: pointer to struct zfcp_qdio
169 * @q_req: pointer to struct zfcp_qdio_req
170 * @sg: scatter-gather list
171 * @max_sbals: upper bound for number of SBALs to be used
172 * Returns: number of bytes, or error (negativ)
174 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
,
175 struct scatterlist
*sg
)
177 struct qdio_buffer_element
*sbale
;
180 /* set storage-block type for this request */
181 sbale
= zfcp_qdio_sbale_req(qdio
, q_req
);
182 sbale
->flags
|= q_req
->sbtype
;
184 for (; sg
; sg
= sg_next(sg
)) {
185 sbale
= zfcp_qdio_sbale_next(qdio
, q_req
);
187 atomic_inc(&qdio
->req_q_full
);
188 zfcp_qdio_zero_sbals(qdio
->req_q
, q_req
->sbal_first
,
193 sbale
->addr
= sg_virt(sg
);
194 sbale
->length
= sg
->length
;
202 static int zfcp_qdio_sbal_check(struct zfcp_qdio
*qdio
)
204 spin_lock_bh(&qdio
->req_q_lock
);
205 if (atomic_read(&qdio
->req_q_free
) ||
206 !(atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
208 spin_unlock_bh(&qdio
->req_q_lock
);
213 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
214 * @qdio: pointer to struct zfcp_qdio
216 * The req_q_lock must be held by the caller of this function, and
217 * this function may only be called from process context; it will
218 * sleep when waiting for a free sbal.
220 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
222 int zfcp_qdio_sbal_get(struct zfcp_qdio
*qdio
)
226 spin_unlock_bh(&qdio
->req_q_lock
);
227 ret
= wait_event_interruptible_timeout(qdio
->req_q_wq
,
228 zfcp_qdio_sbal_check(qdio
), 5 * HZ
);
230 if (!(atomic_read(&qdio
->adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
237 atomic_inc(&qdio
->req_q_full
);
238 /* assume hanging outbound queue, try queue recovery */
239 zfcp_erp_adapter_reopen(qdio
->adapter
, 0, "qdsbg_1", NULL
);
242 spin_lock_bh(&qdio
->req_q_lock
);
247 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
248 * @qdio: pointer to struct zfcp_qdio
249 * @q_req: pointer to struct zfcp_qdio_req
250 * Returns: 0 on success, error otherwise
252 int zfcp_qdio_send(struct zfcp_qdio
*qdio
, struct zfcp_qdio_req
*q_req
)
255 u8 sbal_number
= q_req
->sbal_number
;
257 zfcp_qdio_account(qdio
);
259 retval
= do_QDIO(qdio
->adapter
->ccw_device
, QDIO_FLAG_SYNC_OUTPUT
, 0,
260 q_req
->sbal_first
, sbal_number
);
262 if (unlikely(retval
)) {
263 zfcp_qdio_zero_sbals(qdio
->req_q
, q_req
->sbal_first
,
268 /* account for transferred buffers */
269 atomic_sub(sbal_number
, &qdio
->req_q_free
);
270 qdio
->req_q_idx
+= sbal_number
;
271 qdio
->req_q_idx
%= QDIO_MAX_BUFFERS_PER_Q
;
277 static void zfcp_qdio_setup_init_data(struct qdio_initialize
*id
,
278 struct zfcp_qdio
*qdio
)
281 id
->cdev
= qdio
->adapter
->ccw_device
;
282 id
->q_format
= QDIO_ZFCP_QFMT
;
283 memcpy(id
->adapter_name
, dev_name(&id
->cdev
->dev
), 8);
284 ASCEBC(id
->adapter_name
, 8);
285 id
->qib_rflags
= QIB_RFLAGS_ENABLE_DATA_DIV
;
286 id
->qib_param_field_format
= 0;
287 id
->qib_param_field
= NULL
;
288 id
->input_slib_elements
= NULL
;
289 id
->output_slib_elements
= NULL
;
291 id
->no_output_qs
= 1;
292 id
->input_handler
= zfcp_qdio_int_resp
;
293 id
->output_handler
= zfcp_qdio_int_req
;
294 id
->int_parm
= (unsigned long) qdio
;
295 id
->input_sbal_addr_array
= (void **) (qdio
->res_q
);
296 id
->output_sbal_addr_array
= (void **) (qdio
->req_q
);
300 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
301 * @adapter: pointer to struct zfcp_adapter
302 * Returns: -ENOMEM on memory allocation error or return value from
305 static int zfcp_qdio_allocate(struct zfcp_qdio
*qdio
)
307 struct qdio_initialize init_data
;
309 if (zfcp_qdio_buffers_enqueue(qdio
->req_q
) ||
310 zfcp_qdio_buffers_enqueue(qdio
->res_q
))
313 zfcp_qdio_setup_init_data(&init_data
, qdio
);
315 return qdio_allocate(&init_data
);
319 * zfcp_close_qdio - close qdio queues for an adapter
320 * @qdio: pointer to structure zfcp_qdio
322 void zfcp_qdio_close(struct zfcp_qdio
*qdio
)
324 struct zfcp_adapter
*adapter
= qdio
->adapter
;
327 if (!(atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
))
330 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
331 spin_lock_bh(&qdio
->req_q_lock
);
332 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP
, &adapter
->status
);
333 spin_unlock_bh(&qdio
->req_q_lock
);
335 wake_up(&qdio
->req_q_wq
);
337 qdio_shutdown(adapter
->ccw_device
, QDIO_FLAG_CLEANUP_USING_CLEAR
);
339 /* cleanup used outbound sbals */
340 count
= atomic_read(&qdio
->req_q_free
);
341 if (count
< QDIO_MAX_BUFFERS_PER_Q
) {
342 idx
= (qdio
->req_q_idx
+ count
) % QDIO_MAX_BUFFERS_PER_Q
;
343 count
= QDIO_MAX_BUFFERS_PER_Q
- count
;
344 zfcp_qdio_zero_sbals(qdio
->req_q
, idx
, count
);
347 atomic_set(&qdio
->req_q_free
, 0);
351 * zfcp_qdio_open - prepare and initialize response queue
352 * @qdio: pointer to struct zfcp_qdio
353 * Returns: 0 on success, otherwise -EIO
355 int zfcp_qdio_open(struct zfcp_qdio
*qdio
)
357 struct qdio_buffer_element
*sbale
;
358 struct qdio_initialize init_data
;
359 struct zfcp_adapter
*adapter
= qdio
->adapter
;
360 struct ccw_device
*cdev
= adapter
->ccw_device
;
361 struct qdio_ssqd_desc ssqd
;
364 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_QDIOUP
)
367 atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
,
368 &qdio
->adapter
->status
);
370 zfcp_qdio_setup_init_data(&init_data
, qdio
);
372 if (qdio_establish(&init_data
))
373 goto failed_establish
;
375 if (qdio_get_ssqd_desc(init_data
.cdev
, &ssqd
))
378 if (ssqd
.qdioac2
& CHSC_AC2_DATA_DIV_ENABLED
)
379 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED
,
380 &qdio
->adapter
->status
);
382 if (qdio_activate(cdev
))
385 for (cc
= 0; cc
< QDIO_MAX_BUFFERS_PER_Q
; cc
++) {
386 sbale
= &(qdio
->res_q
[cc
]->element
[0]);
388 sbale
->flags
= SBAL_FLAGS_LAST_ENTRY
;
392 if (do_QDIO(cdev
, QDIO_FLAG_SYNC_INPUT
, 0, 0, QDIO_MAX_BUFFERS_PER_Q
))
395 /* set index of first avalable SBALS / number of available SBALS */
397 atomic_set(&qdio
->req_q_free
, QDIO_MAX_BUFFERS_PER_Q
);
402 qdio_shutdown(cdev
, QDIO_FLAG_CLEANUP_USING_CLEAR
);
405 "Setting up the QDIO connection to the FCP adapter failed\n");
409 void zfcp_qdio_destroy(struct zfcp_qdio
*qdio
)
416 if (qdio
->adapter
->ccw_device
)
417 qdio_free(qdio
->adapter
->ccw_device
);
419 for (p
= 0; p
< QDIO_MAX_BUFFERS_PER_Q
; p
+= QBUFF_PER_PAGE
) {
420 free_page((unsigned long) qdio
->req_q
[p
]);
421 free_page((unsigned long) qdio
->res_q
[p
]);
427 int zfcp_qdio_setup(struct zfcp_adapter
*adapter
)
429 struct zfcp_qdio
*qdio
;
431 qdio
= kzalloc(sizeof(struct zfcp_qdio
), GFP_KERNEL
);
435 qdio
->adapter
= adapter
;
437 if (zfcp_qdio_allocate(qdio
)) {
438 zfcp_qdio_destroy(qdio
);
442 spin_lock_init(&qdio
->req_q_lock
);
443 spin_lock_init(&qdio
->stat_lock
);
445 adapter
->qdio
= qdio
;
450 * zfcp_qdio_siosl - Trigger logging in FCP channel
451 * @adapter: The zfcp_adapter where to trigger logging
453 * Call the cio siosl function to trigger hardware logging. This
454 * wrapper function sets a flag to ensure hardware logging is only
455 * triggered once before going through qdio shutdown.
457 * The triggers are always run from qdio tasklet context, so no
458 * additional synchronization is necessary.
460 void zfcp_qdio_siosl(struct zfcp_adapter
*adapter
)
464 if (atomic_read(&adapter
->status
) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
)
467 rc
= ccw_device_siosl(adapter
->ccw_device
);
469 atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED
,