xref: /openbmc/linux/drivers/s390/scsi/zfcp_qdio.c (revision 5d4a2e29)
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 {
35 	struct zfcp_adapter *adapter = qdio->adapter;
36 
37 	dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
38 
39 	zfcp_erp_adapter_reopen(adapter,
40 				ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
41 				ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
42 }
43 
44 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
45 {
46 	int i, sbal_idx;
47 
48 	for (i = first; i < first + cnt; i++) {
49 		sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
50 		memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
51 	}
52 }
53 
54 /* this needs to be called prior to updating the queue fill level */
55 static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
56 {
57 	unsigned long long now, span;
58 	int free, used;
59 
60 	spin_lock(&qdio->stat_lock);
61 	now = get_clock_monotonic();
62 	span = (now - qdio->req_q_time) >> 12;
63 	free = atomic_read(&qdio->req_q.count);
64 	used = QDIO_MAX_BUFFERS_PER_Q - free;
65 	qdio->req_q_util += used * span;
66 	qdio->req_q_time = now;
67 	spin_unlock(&qdio->stat_lock);
68 }
69 
70 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
71 			      int queue_no, int first, int count,
72 			      unsigned long parm)
73 {
74 	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
75 	struct zfcp_qdio_queue *queue = &qdio->req_q;
76 
77 	if (unlikely(qdio_err)) {
78 		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
79 					count);
80 		zfcp_qdio_handler_error(qdio, "qdireq1");
81 		return;
82 	}
83 
84 	/* cleanup all SBALs being program-owned now */
85 	zfcp_qdio_zero_sbals(queue->sbal, first, count);
86 
87 	zfcp_qdio_account(qdio);
88 	atomic_add(count, &queue->count);
89 	wake_up(&qdio->req_q_wq);
90 }
91 
92 static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
93 {
94 	struct zfcp_qdio_queue *queue = &qdio->resp_q;
95 	struct ccw_device *cdev = qdio->adapter->ccw_device;
96 	u8 count, start = queue->first;
97 	unsigned int retval;
98 
99 	count = atomic_read(&queue->count) + processed;
100 
101 	retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
102 
103 	if (unlikely(retval)) {
104 		atomic_set(&queue->count, count);
105 		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdrpb_1", NULL);
106 	} else {
107 		queue->first += count;
108 		queue->first %= QDIO_MAX_BUFFERS_PER_Q;
109 		atomic_set(&queue->count, 0);
110 	}
111 }
112 
113 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
114 			       int queue_no, int first, int count,
115 			       unsigned long parm)
116 {
117 	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
118 	int sbal_idx, sbal_no;
119 
120 	if (unlikely(qdio_err)) {
121 		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
122 					count);
123 		zfcp_qdio_handler_error(qdio, "qdires1");
124 		return;
125 	}
126 
127 	/*
128 	 * go through all SBALs from input queue currently
129 	 * returned by QDIO layer
130 	 */
131 	for (sbal_no = 0; sbal_no < count; sbal_no++) {
132 		sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
133 		/* go through all SBALEs of SBAL */
134 		zfcp_fsf_reqid_check(qdio, sbal_idx);
135 	}
136 
137 	/*
138 	 * put range of SBALs back to response queue
139 	 * (including SBALs which have already been free before)
140 	 */
141 	zfcp_qdio_resp_put_back(qdio, count);
142 }
143 
144 static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
145 				 struct zfcp_qdio_req *q_req, int max_sbals)
146 {
147 	int count = atomic_read(&qdio->req_q.count);
148 	count = min(count, max_sbals);
149 	q_req->sbal_limit = (q_req->sbal_first + count - 1)
150 					% QDIO_MAX_BUFFERS_PER_Q;
151 }
152 
153 static struct qdio_buffer_element *
154 zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
155 {
156 	struct qdio_buffer_element *sbale;
157 
158 	/* set last entry flag in current SBALE of current SBAL */
159 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
160 	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
161 
162 	/* don't exceed last allowed SBAL */
163 	if (q_req->sbal_last == q_req->sbal_limit)
164 		return NULL;
165 
166 	/* set chaining flag in first SBALE of current SBAL */
167 	sbale = zfcp_qdio_sbale_req(qdio, q_req);
168 	sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
169 
170 	/* calculate index of next SBAL */
171 	q_req->sbal_last++;
172 	q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
173 
174 	/* keep this requests number of SBALs up-to-date */
175 	q_req->sbal_number++;
176 
177 	/* start at first SBALE of new SBAL */
178 	q_req->sbale_curr = 0;
179 
180 	/* set storage-block type for new SBAL */
181 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
182 	sbale->flags |= q_req->sbtype;
183 
184 	return sbale;
185 }
186 
187 static struct qdio_buffer_element *
188 zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
189 {
190 	if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
191 		return zfcp_qdio_sbal_chain(qdio, q_req);
192 	q_req->sbale_curr++;
193 	return zfcp_qdio_sbale_curr(qdio, q_req);
194 }
195 
196 static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
197 				 struct zfcp_qdio_req *q_req)
198 {
199 	struct qdio_buffer **sbal = qdio->req_q.sbal;
200 	int first = q_req->sbal_first;
201 	int last = q_req->sbal_last;
202 	int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
203 		QDIO_MAX_BUFFERS_PER_Q + 1;
204 	zfcp_qdio_zero_sbals(sbal, first, count);
205 }
206 
207 /**
208  * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
209  * @qdio: pointer to struct zfcp_qdio
210  * @q_req: pointer to struct zfcp_qdio_req
211  * @sg: scatter-gather list
212  * @max_sbals: upper bound for number of SBALs to be used
213  * Returns: number of bytes, or error (negativ)
214  */
215 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
216 			    struct scatterlist *sg, int max_sbals)
217 {
218 	struct qdio_buffer_element *sbale;
219 	int bytes = 0;
220 
221 	/* figure out last allowed SBAL */
222 	zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
223 
224 	/* set storage-block type for this request */
225 	sbale = zfcp_qdio_sbale_req(qdio, q_req);
226 	sbale->flags |= q_req->sbtype;
227 
228 	for (; sg; sg = sg_next(sg)) {
229 		sbale = zfcp_qdio_sbale_next(qdio, q_req);
230 		if (!sbale) {
231 			atomic_inc(&qdio->req_q_full);
232 			zfcp_qdio_undo_sbals(qdio, q_req);
233 			return -EINVAL;
234 		}
235 
236 		sbale->addr = sg_virt(sg);
237 		sbale->length = sg->length;
238 
239 		bytes += sg->length;
240 	}
241 
242 	/* assume that no other SBALEs are to follow in the same SBAL */
243 	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
244 	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
245 
246 	return bytes;
247 }
248 
249 static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
250 {
251 	struct zfcp_qdio_queue *req_q = &qdio->req_q;
252 
253 	spin_lock_bh(&qdio->req_q_lock);
254 	if (atomic_read(&req_q->count) ||
255 	    !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
256 		return 1;
257 	spin_unlock_bh(&qdio->req_q_lock);
258 	return 0;
259 }
260 
261 /**
262  * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
263  * @qdio: pointer to struct zfcp_qdio
264  *
265  * The req_q_lock must be held by the caller of this function, and
266  * this function may only be called from process context; it will
267  * sleep when waiting for a free sbal.
268  *
269  * Returns: 0 on success, -EIO if there is no free sbal after waiting.
270  */
271 int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
272 {
273 	long ret;
274 
275 	spin_unlock_bh(&qdio->req_q_lock);
276 	ret = wait_event_interruptible_timeout(qdio->req_q_wq,
277 			       zfcp_qdio_sbal_check(qdio), 5 * HZ);
278 
279 	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
280 		return -EIO;
281 
282 	if (ret > 0)
283 		return 0;
284 
285 	if (!ret) {
286 		atomic_inc(&qdio->req_q_full);
287 		/* assume hanging outbound queue, try queue recovery */
288 		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1", NULL);
289 	}
290 
291 	spin_lock_bh(&qdio->req_q_lock);
292 	return -EIO;
293 }
294 
295 /**
296  * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
297  * @qdio: pointer to struct zfcp_qdio
298  * @q_req: pointer to struct zfcp_qdio_req
299  * Returns: 0 on success, error otherwise
300  */
301 int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
302 {
303 	struct zfcp_qdio_queue *req_q = &qdio->req_q;
304 	int first = q_req->sbal_first;
305 	int count = q_req->sbal_number;
306 	int retval;
307 	unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
308 
309 	zfcp_qdio_account(qdio);
310 
311 	retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
312 			 count);
313 	if (unlikely(retval)) {
314 		zfcp_qdio_zero_sbals(req_q->sbal, first, count);
315 		return retval;
316 	}
317 
318 	/* account for transferred buffers */
319 	atomic_sub(count, &req_q->count);
320 	req_q->first += count;
321 	req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
322 	return 0;
323 }
324 
325 
326 static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
327 				      struct zfcp_qdio *qdio)
328 {
329 
330 	id->cdev = qdio->adapter->ccw_device;
331 	id->q_format = QDIO_ZFCP_QFMT;
332 	memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
333 	ASCEBC(id->adapter_name, 8);
334 	id->qib_param_field_format = 0;
335 	id->qib_param_field = NULL;
336 	id->input_slib_elements = NULL;
337 	id->output_slib_elements = NULL;
338 	id->no_input_qs = 1;
339 	id->no_output_qs = 1;
340 	id->input_handler = zfcp_qdio_int_resp;
341 	id->output_handler = zfcp_qdio_int_req;
342 	id->int_parm = (unsigned long) qdio;
343 	id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
344 	id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
345 
346 }
347 /**
348  * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
349  * @adapter: pointer to struct zfcp_adapter
350  * Returns: -ENOMEM on memory allocation error or return value from
351  *          qdio_allocate
352  */
353 static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
354 {
355 	struct qdio_initialize init_data;
356 
357 	if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
358 	    zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
359 		return -ENOMEM;
360 
361 	zfcp_qdio_setup_init_data(&init_data, qdio);
362 
363 	return qdio_allocate(&init_data);
364 }
365 
366 /**
367  * zfcp_close_qdio - close qdio queues for an adapter
368  * @qdio: pointer to structure zfcp_qdio
369  */
370 void zfcp_qdio_close(struct zfcp_qdio *qdio)
371 {
372 	struct zfcp_qdio_queue *req_q;
373 	int first, count;
374 
375 	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
376 		return;
377 
378 	/* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
379 	req_q = &qdio->req_q;
380 	spin_lock_bh(&qdio->req_q_lock);
381 	atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
382 	spin_unlock_bh(&qdio->req_q_lock);
383 
384 	wake_up(&qdio->req_q_wq);
385 
386 	qdio_shutdown(qdio->adapter->ccw_device,
387 		      QDIO_FLAG_CLEANUP_USING_CLEAR);
388 
389 	/* cleanup used outbound sbals */
390 	count = atomic_read(&req_q->count);
391 	if (count < QDIO_MAX_BUFFERS_PER_Q) {
392 		first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
393 		count = QDIO_MAX_BUFFERS_PER_Q - count;
394 		zfcp_qdio_zero_sbals(req_q->sbal, first, count);
395 	}
396 	req_q->first = 0;
397 	atomic_set(&req_q->count, 0);
398 	qdio->resp_q.first = 0;
399 	atomic_set(&qdio->resp_q.count, 0);
400 }
401 
402 /**
403  * zfcp_qdio_open - prepare and initialize response queue
404  * @qdio: pointer to struct zfcp_qdio
405  * Returns: 0 on success, otherwise -EIO
406  */
407 int zfcp_qdio_open(struct zfcp_qdio *qdio)
408 {
409 	struct qdio_buffer_element *sbale;
410 	struct qdio_initialize init_data;
411 	struct ccw_device *cdev = qdio->adapter->ccw_device;
412 	int cc;
413 
414 	if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
415 		return -EIO;
416 
417 	zfcp_qdio_setup_init_data(&init_data, qdio);
418 
419 	if (qdio_establish(&init_data))
420 		goto failed_establish;
421 
422 	if (qdio_activate(cdev))
423 		goto failed_qdio;
424 
425 	for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
426 		sbale = &(qdio->resp_q.sbal[cc]->element[0]);
427 		sbale->length = 0;
428 		sbale->flags = SBAL_FLAGS_LAST_ENTRY;
429 		sbale->addr = NULL;
430 	}
431 
432 	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
433 		     QDIO_MAX_BUFFERS_PER_Q))
434 		goto failed_qdio;
435 
436 	/* set index of first avalable SBALS / number of available SBALS */
437 	qdio->req_q.first = 0;
438 	atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
439 
440 	return 0;
441 
442 failed_qdio:
443 	qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
444 failed_establish:
445 	dev_err(&cdev->dev,
446 		"Setting up the QDIO connection to the FCP adapter failed\n");
447 	return -EIO;
448 }
449 
450 void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
451 {
452 	struct qdio_buffer **sbal_req, **sbal_resp;
453 	int p;
454 
455 	if (!qdio)
456 		return;
457 
458 	if (qdio->adapter->ccw_device)
459 		qdio_free(qdio->adapter->ccw_device);
460 
461 	sbal_req = qdio->req_q.sbal;
462 	sbal_resp = qdio->resp_q.sbal;
463 
464 	for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
465 		free_page((unsigned long) sbal_req[p]);
466 		free_page((unsigned long) sbal_resp[p]);
467 	}
468 
469 	kfree(qdio);
470 }
471 
472 int zfcp_qdio_setup(struct zfcp_adapter *adapter)
473 {
474 	struct zfcp_qdio *qdio;
475 
476 	qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
477 	if (!qdio)
478 		return -ENOMEM;
479 
480 	qdio->adapter = adapter;
481 
482 	if (zfcp_qdio_allocate(qdio)) {
483 		zfcp_qdio_destroy(qdio);
484 		return -ENOMEM;
485 	}
486 
487 	spin_lock_init(&qdio->req_q_lock);
488 	spin_lock_init(&qdio->stat_lock);
489 
490 	adapter->qdio = qdio;
491 	return 0;
492 }
493 
494