zfcp_qdio.c 12.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
S
Swen Schillig 已提交
2
 * zfcp device driver
L
Linus Torvalds 已提交
3
 *
S
Swen Schillig 已提交
4
 * Setup and helper functions to access QDIO.
L
Linus Torvalds 已提交
5
 *
6
 * Copyright IBM Corporation 2002, 2010
L
Linus Torvalds 已提交
7 8
 */

9 10 11
#define KMSG_COMPONENT "zfcp"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

12
#include <linux/slab.h>
L
Linus Torvalds 已提交
13
#include "zfcp_ext.h"
14
#include "zfcp_qdio.h"
L
Linus Torvalds 已提交
15

16
#define QBUFF_PER_PAGE		(PAGE_SIZE / sizeof(struct qdio_buffer))
L
Linus Torvalds 已提交
17

S
Swen Schillig 已提交
18
static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
L
Linus Torvalds 已提交
19
{
S
Swen Schillig 已提交
20
	int pos;
L
Linus Torvalds 已提交
21

S
Swen Schillig 已提交
22
	for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
S
Swen Schillig 已提交
23 24
		sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
		if (!sbal[pos])
S
Swen Schillig 已提交
25 26 27 28
			return -ENOMEM;
	}
	for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
		if (pos % QBUFF_PER_PAGE)
S
Swen Schillig 已提交
29
			sbal[pos] = sbal[pos - 1] + 1;
S
Swen Schillig 已提交
30
	return 0;
L
Linus Torvalds 已提交
31 32
}

33
static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
L
Linus Torvalds 已提交
34
{
35 36
	struct zfcp_adapter *adapter = qdio->adapter;

37
	dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
L
Linus Torvalds 已提交
38

S
Swen Schillig 已提交
39 40 41
	zfcp_erp_adapter_reopen(adapter,
				ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
				ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
L
Linus Torvalds 已提交
42 43
}

44 45 46 47 48 49 50 51 52 53
static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
{
	int i, sbal_idx;

	for (i = first; i < first + cnt; i++) {
		sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
		memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
	}
}

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 59
	int free, used;

60
	spin_lock(&qdio->stat_lock);
61 62 63
	now = get_clock_monotonic();
	span = (now - qdio->req_q_time) >> 12;
	free = atomic_read(&qdio->req_q.count);
64
	used = QDIO_MAX_BUFFERS_PER_Q - free;
65 66 67
	qdio->req_q_util += used * span;
	qdio->req_q_time = now;
	spin_unlock(&qdio->stat_lock);
68 69
}

J
Jan Glauber 已提交
70 71
static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
			      int queue_no, int first, int count,
S
Swen Schillig 已提交
72
			      unsigned long parm)
L
Linus Torvalds 已提交
73
{
74 75
	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
	struct zfcp_qdio_queue *queue = &qdio->req_q;
L
Linus Torvalds 已提交
76

J
Jan Glauber 已提交
77
	if (unlikely(qdio_err)) {
S
Swen Schillig 已提交
78 79
		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
					count);
80
		zfcp_qdio_handler_error(qdio, "qdireq1");
S
Swen Schillig 已提交
81 82
		return;
	}
L
Linus Torvalds 已提交
83 84

	/* cleanup all SBALs being program-owned now */
S
Swen Schillig 已提交
85
	zfcp_qdio_zero_sbals(queue->sbal, first, count);
L
Linus Torvalds 已提交
86

87
	zfcp_qdio_account(qdio);
S
Swen Schillig 已提交
88
	atomic_add(count, &queue->count);
89
	wake_up(&qdio->req_q_wq);
L
Linus Torvalds 已提交
90 91
}

92
static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
L
Linus Torvalds 已提交
93
{
94 95
	struct zfcp_qdio_queue *queue = &qdio->resp_q;
	struct ccw_device *cdev = qdio->adapter->ccw_device;
S
Swen Schillig 已提交
96 97
	u8 count, start = queue->first;
	unsigned int retval;
L
Linus Torvalds 已提交
98

S
Swen Schillig 已提交
99 100
	count = atomic_read(&queue->count) + processed;

J
Jan Glauber 已提交
101
	retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
S
Swen Schillig 已提交
102 103 104

	if (unlikely(retval)) {
		atomic_set(&queue->count, count);
105
		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdrpb_1", NULL);
S
Swen Schillig 已提交
106 107 108 109 110 111 112
	} else {
		queue->first += count;
		queue->first %= QDIO_MAX_BUFFERS_PER_Q;
		atomic_set(&queue->count, 0);
	}
}

J
Jan Glauber 已提交
113 114
static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
			       int queue_no, int first, int count,
S
Swen Schillig 已提交
115 116
			       unsigned long parm)
{
117
	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
118
	int sbal_idx, sbal_no;
S
Swen Schillig 已提交
119

J
Jan Glauber 已提交
120
	if (unlikely(qdio_err)) {
S
Swen Schillig 已提交
121 122
		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
					count);
123
		zfcp_qdio_handler_error(qdio, "qdires1");
S
Swen Schillig 已提交
124 125
		return;
	}
L
Linus Torvalds 已提交
126 127 128 129 130

	/*
	 * go through all SBALs from input queue currently
	 * returned by QDIO layer
	 */
S
Swen Schillig 已提交
131 132
	for (sbal_no = 0; sbal_no < count; sbal_no++) {
		sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
L
Linus Torvalds 已提交
133
		/* go through all SBALEs of SBAL */
134
		zfcp_fsf_reqid_check(qdio, sbal_idx);
L
Linus Torvalds 已提交
135 136 137 138 139 140
	}

	/*
	 * put range of SBALs back to response queue
	 * (including SBALs which have already been free before)
	 */
141
	zfcp_qdio_resp_put_back(qdio, count);
L
Linus Torvalds 已提交
142 143
}

144
static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
145
				 struct zfcp_qdio_req *q_req, int max_sbals)
L
Linus Torvalds 已提交
146
{
147
	int count = atomic_read(&qdio->req_q.count);
L
Linus Torvalds 已提交
148
	count = min(count, max_sbals);
149
	q_req->sbal_limit = (q_req->sbal_first + count - 1)
S
Swen Schillig 已提交
150
					% QDIO_MAX_BUFFERS_PER_Q;
L
Linus Torvalds 已提交
151 152
}

153
static struct qdio_buffer_element *
154
zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
155
{
156
	struct qdio_buffer_element *sbale;
L
Linus Torvalds 已提交
157 158

	/* set last entry flag in current SBALE of current SBAL */
159
	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
L
Linus Torvalds 已提交
160 161 162
	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;

	/* don't exceed last allowed SBAL */
163
	if (q_req->sbal_last == q_req->sbal_limit)
L
Linus Torvalds 已提交
164 165 166
		return NULL;

	/* set chaining flag in first SBALE of current SBAL */
167
	sbale = zfcp_qdio_sbale_req(qdio, q_req);
L
Linus Torvalds 已提交
168 169 170
	sbale->flags |= SBAL_FLAGS0_MORE_SBALS;

	/* calculate index of next SBAL */
171 172
	q_req->sbal_last++;
	q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
L
Linus Torvalds 已提交
173 174

	/* keep this requests number of SBALs up-to-date */
175
	q_req->sbal_number++;
L
Linus Torvalds 已提交
176 177

	/* start at first SBALE of new SBAL */
178
	q_req->sbale_curr = 0;
L
Linus Torvalds 已提交
179 180

	/* set storage-block type for new SBAL */
181
	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
182
	sbale->flags |= q_req->sbtype;
L
Linus Torvalds 已提交
183 184 185 186

	return sbale;
}

187
static struct qdio_buffer_element *
188
zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
189
{
190 191
	if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
		return zfcp_qdio_sbal_chain(qdio, q_req);
192
	q_req->sbale_curr++;
193
	return zfcp_qdio_sbale_curr(qdio, q_req);
L
Linus Torvalds 已提交
194 195
}

196
static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
197
				 struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
198
{
199
	struct qdio_buffer **sbal = qdio->req_q.sbal;
200 201
	int first = q_req->sbal_first;
	int last = q_req->sbal_last;
S
Swen Schillig 已提交
202 203 204
	int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
		QDIO_MAX_BUFFERS_PER_Q + 1;
	zfcp_qdio_zero_sbals(sbal, first, count);
L
Linus Torvalds 已提交
205 206 207 208
}

/**
 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
209 210
 * @qdio: pointer to struct zfcp_qdio
 * @q_req: pointer to struct zfcp_qdio_req
L
Linus Torvalds 已提交
211 212
 * @sg: scatter-gather list
 * @max_sbals: upper bound for number of SBALs to be used
S
Swen Schillig 已提交
213
 * Returns: number of bytes, or error (negativ)
L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
217
{
218
	struct qdio_buffer_element *sbale;
219
	int bytes = 0;
L
Linus Torvalds 已提交
220 221

	/* figure out last allowed SBAL */
222
	zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
L
Linus Torvalds 已提交
223

S
Swen Schillig 已提交
224
	/* set storage-block type for this request */
225
	sbale = zfcp_qdio_sbale_req(qdio, q_req);
226
	sbale->flags |= q_req->sbtype;
L
Linus Torvalds 已提交
227

S
Swen Schillig 已提交
228
	for (; sg; sg = sg_next(sg)) {
229
		sbale = zfcp_qdio_sbale_next(qdio, q_req);
230 231 232 233 234 235 236 237 238
		if (!sbale) {
			atomic_inc(&qdio->req_q_full);
			zfcp_qdio_undo_sbals(qdio, q_req);
			return -EINVAL;
		}

		sbale->addr = sg_virt(sg);
		sbale->length = sg->length;

S
Swen Schillig 已提交
239
		bytes += sg->length;
L
Linus Torvalds 已提交
240
	}
S
Swen Schillig 已提交
241

L
Linus Torvalds 已提交
242
	/* assume that no other SBALEs are to follow in the same SBAL */
243
	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
L
Linus Torvalds 已提交
244
	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
S
Swen Schillig 已提交
245

L
Linus Torvalds 已提交
246 247 248
	return bytes;
}

249 250 251 252 253
static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
{
	struct zfcp_qdio_queue *req_q = &qdio->req_q;

	spin_lock_bh(&qdio->req_q_lock);
254 255
	if (atomic_read(&req_q->count) ||
	    !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		return 1;
	spin_unlock_bh(&qdio->req_q_lock);
	return 0;
}

/**
 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
 * @qdio: pointer to struct zfcp_qdio
 *
 * The req_q_lock must be held by the caller of this function, and
 * this function may only be called from process context; it will
 * sleep when waiting for a free sbal.
 *
 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
 */
int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
{
	long ret;

	spin_unlock_bh(&qdio->req_q_lock);
	ret = wait_event_interruptible_timeout(qdio->req_q_wq,
			       zfcp_qdio_sbal_check(qdio), 5 * HZ);
278 279 280 281

	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
		return -EIO;

282 283
	if (ret > 0)
		return 0;
284

285 286 287 288 289 290 291 292 293 294
	if (!ret) {
		atomic_inc(&qdio->req_q_full);
		/* assume hanging outbound queue, try queue recovery */
		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1", NULL);
	}

	spin_lock_bh(&qdio->req_q_lock);
	return -EIO;
}

L
Linus Torvalds 已提交
295
/**
S
Swen Schillig 已提交
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
S
Swen Schillig 已提交
299
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
300
 */
301
int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
302
{
303
	struct zfcp_qdio_queue *req_q = &qdio->req_q;
304 305
	int first = q_req->sbal_first;
	int count = q_req->sbal_number;
C
Christof Schmitt 已提交
306 307
	int retval;
	unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
S
Swen Schillig 已提交
308

309
	zfcp_qdio_account(qdio);
310

311 312
	retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
			 count);
S
Swen Schillig 已提交
313 314 315 316 317 318 319 320 321 322
	if (unlikely(retval)) {
		zfcp_qdio_zero_sbals(req_q->sbal, first, count);
		return retval;
	}

	/* account for transferred buffers */
	atomic_sub(count, &req_q->count);
	req_q->first += count;
	req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
	return 0;
L
Linus Torvalds 已提交
323 324
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
				      struct zfcp_qdio *qdio)
{

	id->cdev = qdio->adapter->ccw_device;
	id->q_format = QDIO_ZFCP_QFMT;
	memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
	ASCEBC(id->adapter_name, 8);
	id->qib_param_field_format = 0;
	id->qib_param_field = NULL;
	id->input_slib_elements = NULL;
	id->output_slib_elements = NULL;
	id->no_input_qs = 1;
	id->no_output_qs = 1;
	id->input_handler = zfcp_qdio_int_resp;
	id->output_handler = zfcp_qdio_int_req;
	id->int_parm = (unsigned long) qdio;
	id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
	id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);

}
S
Swen Schillig 已提交
347 348 349 350 351 352
/**
 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
 * @adapter: pointer to struct zfcp_adapter
 * Returns: -ENOMEM on memory allocation error or return value from
 *          qdio_allocate
 */
353
static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
S
Swen Schillig 已提交
354
{
355
	struct qdio_initialize init_data;
S
Swen Schillig 已提交
356

357 358
	if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
	    zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
S
Swen Schillig 已提交
359 360
		return -ENOMEM;

361 362 363
	zfcp_qdio_setup_init_data(&init_data, qdio);

	return qdio_allocate(&init_data);
S
Swen Schillig 已提交
364 365 366 367
}

/**
 * zfcp_close_qdio - close qdio queues for an adapter
368
 * @qdio: pointer to structure zfcp_qdio
L
Linus Torvalds 已提交
369
 */
370
void zfcp_qdio_close(struct zfcp_qdio *qdio)
L
Linus Torvalds 已提交
371
{
S
Swen Schillig 已提交
372 373 374
	struct zfcp_qdio_queue *req_q;
	int first, count;

375
	if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
S
Swen Schillig 已提交
376 377 378
		return;

	/* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
379 380 381 382
	req_q = &qdio->req_q;
	spin_lock_bh(&qdio->req_q_lock);
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
383

384 385
	wake_up(&qdio->req_q_wq);

386 387
	qdio_shutdown(qdio->adapter->ccw_device,
		      QDIO_FLAG_CLEANUP_USING_CLEAR);
S
Swen Schillig 已提交
388 389 390 391 392 393 394

	/* cleanup used outbound sbals */
	count = atomic_read(&req_q->count);
	if (count < QDIO_MAX_BUFFERS_PER_Q) {
		first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
		count = QDIO_MAX_BUFFERS_PER_Q - count;
		zfcp_qdio_zero_sbals(req_q->sbal, first, count);
L
Linus Torvalds 已提交
395
	}
S
Swen Schillig 已提交
396 397
	req_q->first = 0;
	atomic_set(&req_q->count, 0);
398 399
	qdio->resp_q.first = 0;
	atomic_set(&qdio->resp_q.count, 0);
L
Linus Torvalds 已提交
400 401
}

S
Swen Schillig 已提交
402 403
/**
 * zfcp_qdio_open - prepare and initialize response queue
404
 * @qdio: pointer to struct zfcp_qdio
S
Swen Schillig 已提交
405 406
 * Returns: 0 on success, otherwise -EIO
 */
407
int zfcp_qdio_open(struct zfcp_qdio *qdio)
S
Swen Schillig 已提交
408
{
409
	struct qdio_buffer_element *sbale;
410 411
	struct qdio_initialize init_data;
	struct ccw_device *cdev = qdio->adapter->ccw_device;
S
Swen Schillig 已提交
412 413
	int cc;

414
	if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
S
Swen Schillig 已提交
415 416
		return -EIO;

417 418 419
	zfcp_qdio_setup_init_data(&init_data, qdio);

	if (qdio_establish(&init_data))
420
		goto failed_establish;
S
Swen Schillig 已提交
421

422
	if (qdio_activate(cdev))
S
Swen Schillig 已提交
423 424 425
		goto failed_qdio;

	for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
426
		sbale = &(qdio->resp_q.sbal[cc]->element[0]);
S
Swen Schillig 已提交
427 428 429 430 431
		sbale->length = 0;
		sbale->flags = SBAL_FLAGS_LAST_ENTRY;
		sbale->addr = NULL;
	}

432
	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
433
		     QDIO_MAX_BUFFERS_PER_Q))
S
Swen Schillig 已提交
434 435 436
		goto failed_qdio;

	/* set index of first avalable SBALS / number of available SBALS */
437 438
	qdio->req_q.first = 0;
	atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
S
Swen Schillig 已提交
439 440 441 442

	return 0;

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");
S
Swen Schillig 已提交
447 448
	return -EIO;
}
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
{
	struct qdio_buffer **sbal_req, **sbal_resp;
	int p;

	if (!qdio)
		return;

	if (qdio->adapter->ccw_device)
		qdio_free(qdio->adapter->ccw_device);

	sbal_req = qdio->req_q.sbal;
	sbal_resp = qdio->resp_q.sbal;

	for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
		free_page((unsigned long) sbal_req[p]);
		free_page((unsigned long) sbal_resp[p]);
	}

	kfree(qdio);
}

int zfcp_qdio_setup(struct zfcp_adapter *adapter)
{
	struct zfcp_qdio *qdio;

	qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
	if (!qdio)
		return -ENOMEM;

	qdio->adapter = adapter;

	if (zfcp_qdio_allocate(qdio)) {
		zfcp_qdio_destroy(qdio);
		return -ENOMEM;
	}

	spin_lock_init(&qdio->req_q_lock);
	spin_lock_init(&qdio->stat_lock);

	adapter->qdio = qdio;
	return 0;
}