xref: /openbmc/linux/drivers/s390/scsi/zfcp_qdio.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  * zfcp device driver
3  *
4  * Setup and helper functions to access QDIO.
5  *
6  * Copyright IBM Corporation 2002, 2010
7  */
8 
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/slab.h>
13 #include "zfcp_ext.h"
14 #include "zfcp_qdio.h"
15 
16 #define QBUFF_PER_PAGE		(PAGE_SIZE / sizeof(struct qdio_buffer))
17 
18 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
19 {
20 	int pos;
21 
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);
24 		if (!sbal[pos])
25 			return -ENOMEM;
26 	}
27 	for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
28 		if (pos % QBUFF_PER_PAGE)
29 			sbal[pos] = sbal[pos - 1] + 1;
30 	return 0;
31 }
32 
33 static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
34 				    unsigned int qdio_err)
35 {
36 	struct zfcp_adapter *adapter = qdio->adapter;
37 
38 	dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
39 
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);
45 }
46 
47 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
48 {
49 	int i, sbal_idx;
50 
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));
54 	}
55 }
56 
57 /* this needs to be called prior to updating the queue fill level */
58 static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
59 {
60 	unsigned long long now, span;
61 	int used;
62 
63 	now = get_clock_monotonic();
64 	span = (now - qdio->req_q_time) >> 12;
65 	used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
66 	qdio->req_q_util += used * span;
67 	qdio->req_q_time = now;
68 }
69 
70 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
71 			      int queue_no, int idx, int count,
72 			      unsigned long parm)
73 {
74 	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
75 
76 	if (unlikely(qdio_err)) {
77 		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, idx, count);
78 		zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
79 		return;
80 	}
81 
82 	/* cleanup all SBALs being program-owned now */
83 	zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
84 
85 	spin_lock_irq(&qdio->stat_lock);
86 	zfcp_qdio_account(qdio);
87 	spin_unlock_irq(&qdio->stat_lock);
88 	atomic_add(count, &qdio->req_q_free);
89 	wake_up(&qdio->req_q_wq);
90 }
91 
92 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
93 			       int queue_no, int idx, int count,
94 			       unsigned long parm)
95 {
96 	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
97 	int sbal_idx, sbal_no;
98 
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);
102 		return;
103 	}
104 
105 	/*
106 	 * go through all SBALs from input queue currently
107 	 * returned by QDIO layer
108 	 */
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);
113 	}
114 
115 	/*
116 	 * put SBALs back to response queue
117 	 */
118 	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
119 		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2", NULL);
120 }
121 
122 static struct qdio_buffer_element *
123 zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
124 {
125 	struct qdio_buffer_element *sbale;
126 
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;
130 
131 	/* don't exceed last allowed SBAL */
132 	if (q_req->sbal_last == q_req->sbal_limit)
133 		return NULL;
134 
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;
138 
139 	/* calculate index of next SBAL */
140 	q_req->sbal_last++;
141 	q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
142 
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);
146 
147 	/* start at first SBALE of new SBAL */
148 	q_req->sbale_curr = 0;
149 
150 	/* set storage-block type for new SBAL */
151 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
152 	sbale->flags |= q_req->sbtype;
153 
154 	return sbale;
155 }
156 
157 static struct qdio_buffer_element *
158 zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
159 {
160 	if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
161 		return zfcp_qdio_sbal_chain(qdio, q_req);
162 	q_req->sbale_curr++;
163 	return zfcp_qdio_sbale_curr(qdio, q_req);
164 }
165 
166 /**
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)
173  */
174 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
175 			    struct scatterlist *sg)
176 {
177 	struct qdio_buffer_element *sbale;
178 	int bytes = 0;
179 
180 	/* set storage-block type for this request */
181 	sbale = zfcp_qdio_sbale_req(qdio, q_req);
182 	sbale->flags |= q_req->sbtype;
183 
184 	for (; sg; sg = sg_next(sg)) {
185 		sbale = zfcp_qdio_sbale_next(qdio, q_req);
186 		if (!sbale) {
187 			atomic_inc(&qdio->req_q_full);
188 			zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
189 					     q_req->sbal_number);
190 			return -EINVAL;
191 		}
192 
193 		sbale->addr = sg_virt(sg);
194 		sbale->length = sg->length;
195 
196 		bytes += sg->length;
197 	}
198 
199 	return bytes;
200 }
201 
202 static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
203 {
204 	spin_lock_irq(&qdio->req_q_lock);
205 	if (atomic_read(&qdio->req_q_free) ||
206 	    !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
207 		return 1;
208 	spin_unlock_irq(&qdio->req_q_lock);
209 	return 0;
210 }
211 
212 /**
213  * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
214  * @qdio: pointer to struct zfcp_qdio
215  *
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.
219  *
220  * Returns: 0 on success, -EIO if there is no free sbal after waiting.
221  */
222 int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
223 {
224 	long ret;
225 
226 	spin_unlock_irq(&qdio->req_q_lock);
227 	ret = wait_event_interruptible_timeout(qdio->req_q_wq,
228 			       zfcp_qdio_sbal_check(qdio), 5 * HZ);
229 
230 	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
231 		return -EIO;
232 
233 	if (ret > 0)
234 		return 0;
235 
236 	if (!ret) {
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);
240 	}
241 
242 	spin_lock_irq(&qdio->req_q_lock);
243 	return -EIO;
244 }
245 
246 /**
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
251  */
252 int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
253 {
254 	int retval;
255 	u8 sbal_number = q_req->sbal_number;
256 
257 	spin_lock(&qdio->stat_lock);
258 	zfcp_qdio_account(qdio);
259 	spin_unlock(&qdio->stat_lock);
260 
261 	retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
262 			 q_req->sbal_first, sbal_number);
263 
264 	if (unlikely(retval)) {
265 		zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
266 				     sbal_number);
267 		return retval;
268 	}
269 
270 	/* account for transferred buffers */
271 	atomic_sub(sbal_number, &qdio->req_q_free);
272 	qdio->req_q_idx += sbal_number;
273 	qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
274 
275 	return 0;
276 }
277 
278 
279 static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
280 				      struct zfcp_qdio *qdio)
281 {
282 	memset(id, 0, sizeof(*id));
283 	id->cdev = qdio->adapter->ccw_device;
284 	id->q_format = QDIO_ZFCP_QFMT;
285 	memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
286 	ASCEBC(id->adapter_name, 8);
287 	id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
288 	id->no_input_qs = 1;
289 	id->no_output_qs = 1;
290 	id->input_handler = zfcp_qdio_int_resp;
291 	id->output_handler = zfcp_qdio_int_req;
292 	id->int_parm = (unsigned long) qdio;
293 	id->input_sbal_addr_array = (void **) (qdio->res_q);
294 	id->output_sbal_addr_array = (void **) (qdio->req_q);
295 }
296 
297 /**
298  * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
299  * @adapter: pointer to struct zfcp_adapter
300  * Returns: -ENOMEM on memory allocation error or return value from
301  *          qdio_allocate
302  */
303 static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
304 {
305 	struct qdio_initialize init_data;
306 
307 	if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
308 	    zfcp_qdio_buffers_enqueue(qdio->res_q))
309 		return -ENOMEM;
310 
311 	zfcp_qdio_setup_init_data(&init_data, qdio);
312 
313 	return qdio_allocate(&init_data);
314 }
315 
316 /**
317  * zfcp_close_qdio - close qdio queues for an adapter
318  * @qdio: pointer to structure zfcp_qdio
319  */
320 void zfcp_qdio_close(struct zfcp_qdio *qdio)
321 {
322 	struct zfcp_adapter *adapter = qdio->adapter;
323 	int idx, count;
324 
325 	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
326 		return;
327 
328 	/* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
329 	spin_lock_irq(&qdio->req_q_lock);
330 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
331 	spin_unlock_irq(&qdio->req_q_lock);
332 
333 	wake_up(&qdio->req_q_wq);
334 
335 	qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
336 
337 	/* cleanup used outbound sbals */
338 	count = atomic_read(&qdio->req_q_free);
339 	if (count < QDIO_MAX_BUFFERS_PER_Q) {
340 		idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
341 		count = QDIO_MAX_BUFFERS_PER_Q - count;
342 		zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
343 	}
344 	qdio->req_q_idx = 0;
345 	atomic_set(&qdio->req_q_free, 0);
346 }
347 
348 /**
349  * zfcp_qdio_open - prepare and initialize response queue
350  * @qdio: pointer to struct zfcp_qdio
351  * Returns: 0 on success, otherwise -EIO
352  */
353 int zfcp_qdio_open(struct zfcp_qdio *qdio)
354 {
355 	struct qdio_buffer_element *sbale;
356 	struct qdio_initialize init_data;
357 	struct zfcp_adapter *adapter = qdio->adapter;
358 	struct ccw_device *cdev = adapter->ccw_device;
359 	struct qdio_ssqd_desc ssqd;
360 	int cc;
361 
362 	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
363 		return -EIO;
364 
365 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
366 			  &qdio->adapter->status);
367 
368 	zfcp_qdio_setup_init_data(&init_data, qdio);
369 
370 	if (qdio_establish(&init_data))
371 		goto failed_establish;
372 
373 	if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
374 		goto failed_qdio;
375 
376 	if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
377 		atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
378 				&qdio->adapter->status);
379 
380 	if (qdio_activate(cdev))
381 		goto failed_qdio;
382 
383 	for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
384 		sbale = &(qdio->res_q[cc]->element[0]);
385 		sbale->length = 0;
386 		sbale->flags = SBAL_FLAGS_LAST_ENTRY;
387 		sbale->addr = NULL;
388 	}
389 
390 	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
391 		goto failed_qdio;
392 
393 	/* set index of first avalable SBALS / number of available SBALS */
394 	qdio->req_q_idx = 0;
395 	atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
396 
397 	return 0;
398 
399 failed_qdio:
400 	qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
401 failed_establish:
402 	dev_err(&cdev->dev,
403 		"Setting up the QDIO connection to the FCP adapter failed\n");
404 	return -EIO;
405 }
406 
407 void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
408 {
409 	int p;
410 
411 	if (!qdio)
412 		return;
413 
414 	if (qdio->adapter->ccw_device)
415 		qdio_free(qdio->adapter->ccw_device);
416 
417 	for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
418 		free_page((unsigned long) qdio->req_q[p]);
419 		free_page((unsigned long) qdio->res_q[p]);
420 	}
421 
422 	kfree(qdio);
423 }
424 
425 int zfcp_qdio_setup(struct zfcp_adapter *adapter)
426 {
427 	struct zfcp_qdio *qdio;
428 
429 	qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
430 	if (!qdio)
431 		return -ENOMEM;
432 
433 	qdio->adapter = adapter;
434 
435 	if (zfcp_qdio_allocate(qdio)) {
436 		zfcp_qdio_destroy(qdio);
437 		return -ENOMEM;
438 	}
439 
440 	spin_lock_init(&qdio->req_q_lock);
441 	spin_lock_init(&qdio->stat_lock);
442 
443 	adapter->qdio = qdio;
444 	return 0;
445 }
446 
447 /**
448  * zfcp_qdio_siosl - Trigger logging in FCP channel
449  * @adapter: The zfcp_adapter where to trigger logging
450  *
451  * Call the cio siosl function to trigger hardware logging.  This
452  * wrapper function sets a flag to ensure hardware logging is only
453  * triggered once before going through qdio shutdown.
454  *
455  * The triggers are always run from qdio tasklet context, so no
456  * additional synchronization is necessary.
457  */
458 void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
459 {
460 	int rc;
461 
462 	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
463 		return;
464 
465 	rc = ccw_device_siosl(adapter->ccw_device);
466 	if (!rc)
467 		atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
468 				&adapter->status);
469 }
470