zfcp_qdio.c 11.4 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
	int used;
59

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

J
Jan Glauber 已提交
69
static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
70
			      int queue_no, int idx, int count,
S
Swen Schillig 已提交
71
			      unsigned long parm)
L
Linus Torvalds 已提交
72
{
73
	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
L
Linus Torvalds 已提交
74

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

	/* cleanup all SBALs being program-owned now */
82
	zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
L
Linus Torvalds 已提交
83

84
	zfcp_qdio_account(qdio);
85
	atomic_add(count, &qdio->req_q_free);
86
	wake_up(&qdio->req_q_wq);
L
Linus Torvalds 已提交
87 88
}

J
Jan Glauber 已提交
89
static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
90
			       int queue_no, int idx, int count,
S
Swen Schillig 已提交
91 92
			       unsigned long parm)
{
93
	struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
94
	int sbal_idx, sbal_no;
S
Swen Schillig 已提交
95

J
Jan Glauber 已提交
96
	if (unlikely(qdio_err)) {
97
		zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, idx, count);
98
		zfcp_qdio_handler_error(qdio, "qdires1");
S
Swen Schillig 已提交
99 100
		return;
	}
L
Linus Torvalds 已提交
101 102 103 104 105

	/*
	 * go through all SBALs from input queue currently
	 * returned by QDIO layer
	 */
S
Swen Schillig 已提交
106
	for (sbal_no = 0; sbal_no < count; sbal_no++) {
107
		sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
L
Linus Torvalds 已提交
108
		/* go through all SBALEs of SBAL */
109
		zfcp_fsf_reqid_check(qdio, sbal_idx);
L
Linus Torvalds 已提交
110 111 112
	}

	/*
113
	 * put SBALs back to response queue
L
Linus Torvalds 已提交
114
	 */
115 116
	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
		zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2", NULL);
L
Linus Torvalds 已提交
117 118
}

119
static struct qdio_buffer_element *
120
zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
121
{
122
	struct qdio_buffer_element *sbale;
L
Linus Torvalds 已提交
123 124

	/* set last entry flag in current SBALE of current SBAL */
125
	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
L
Linus Torvalds 已提交
126 127 128
	sbale->flags |= SBAL_FLAGS_LAST_ENTRY;

	/* don't exceed last allowed SBAL */
129
	if (q_req->sbal_last == q_req->sbal_limit)
L
Linus Torvalds 已提交
130 131 132
		return NULL;

	/* set chaining flag in first SBALE of current SBAL */
133
	sbale = zfcp_qdio_sbale_req(qdio, q_req);
L
Linus Torvalds 已提交
134 135 136
	sbale->flags |= SBAL_FLAGS0_MORE_SBALS;

	/* calculate index of next SBAL */
137 138
	q_req->sbal_last++;
	q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
L
Linus Torvalds 已提交
139 140

	/* keep this requests number of SBALs up-to-date */
141
	q_req->sbal_number++;
142
	BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
L
Linus Torvalds 已提交
143 144

	/* start at first SBALE of new SBAL */
145
	q_req->sbale_curr = 0;
L
Linus Torvalds 已提交
146 147

	/* set storage-block type for new SBAL */
148
	sbale = zfcp_qdio_sbale_curr(qdio, q_req);
149
	sbale->flags |= q_req->sbtype;
L
Linus Torvalds 已提交
150 151 152 153

	return sbale;
}

154
static struct qdio_buffer_element *
155
zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
156
{
157 158
	if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
		return zfcp_qdio_sbal_chain(qdio, q_req);
159
	q_req->sbale_curr++;
160
	return zfcp_qdio_sbale_curr(qdio, q_req);
L
Linus Torvalds 已提交
161 162 163 164
}

/**
 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
165 166
 * @qdio: pointer to struct zfcp_qdio
 * @q_req: pointer to struct zfcp_qdio_req
L
Linus Torvalds 已提交
167 168
 * @sg: scatter-gather list
 * @max_sbals: upper bound for number of SBALs to be used
S
Swen Schillig 已提交
169
 * Returns: number of bytes, or error (negativ)
L
Linus Torvalds 已提交
170
 */
171
int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
172
			    struct scatterlist *sg)
L
Linus Torvalds 已提交
173
{
174
	struct qdio_buffer_element *sbale;
175
	int bytes = 0;
L
Linus Torvalds 已提交
176

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

S
Swen Schillig 已提交
181
	for (; sg; sg = sg_next(sg)) {
182
		sbale = zfcp_qdio_sbale_next(qdio, q_req);
183 184
		if (!sbale) {
			atomic_inc(&qdio->req_q_full);
185 186
			zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
					     q_req->sbal_number);
187 188 189 190 191 192
			return -EINVAL;
		}

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

S
Swen Schillig 已提交
193
		bytes += sg->length;
L
Linus Torvalds 已提交
194
	}
S
Swen Schillig 已提交
195

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

L
Linus Torvalds 已提交
200 201 202
	return bytes;
}

203 204 205
static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
{
	spin_lock_bh(&qdio->req_q_lock);
206
	if (atomic_read(&qdio->req_q_free) ||
207
	    !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		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);
230 231 232 233

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

234 235
	if (ret > 0)
		return 0;
236

237 238 239 240 241 242 243 244 245 246
	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 已提交
247
/**
S
Swen Schillig 已提交
248
 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
249
 * @qdio: pointer to struct zfcp_qdio
250
 * @q_req: pointer to struct zfcp_qdio_req
S
Swen Schillig 已提交
251
 * Returns: 0 on success, error otherwise
L
Linus Torvalds 已提交
252
 */
253
int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
L
Linus Torvalds 已提交
254
{
C
Christof Schmitt 已提交
255
	int retval;
256
	u8 sbal_number = q_req->sbal_number;
S
Swen Schillig 已提交
257

258
	zfcp_qdio_account(qdio);
259

260 261 262
	retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
			 q_req->sbal_first, sbal_number);

S
Swen Schillig 已提交
263
	if (unlikely(retval)) {
264 265
		zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
				     sbal_number);
S
Swen Schillig 已提交
266 267 268 269
		return retval;
	}

	/* account for transferred buffers */
270 271 272 273
	atomic_sub(sbal_number, &qdio->req_q_free);
	qdio->req_q_idx += sbal_number;
	qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;

S
Swen Schillig 已提交
274
	return 0;
L
Linus Torvalds 已提交
275 276
}

277 278 279 280 281 282 283 284 285

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);
286
	id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
287 288 289 290 291 292 293 294 295
	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;
296 297
	id->input_sbal_addr_array = (void **) (qdio->res_q);
	id->output_sbal_addr_array = (void **) (qdio->req_q);
298
}
299

S
Swen Schillig 已提交
300 301 302 303 304 305
/**
 * 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
 */
306
static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
S
Swen Schillig 已提交
307
{
308
	struct qdio_initialize init_data;
S
Swen Schillig 已提交
309

310 311
	if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
	    zfcp_qdio_buffers_enqueue(qdio->res_q))
S
Swen Schillig 已提交
312 313
		return -ENOMEM;

314 315 316
	zfcp_qdio_setup_init_data(&init_data, qdio);

	return qdio_allocate(&init_data);
S
Swen Schillig 已提交
317 318 319 320
}

/**
 * zfcp_close_qdio - close qdio queues for an adapter
321
 * @qdio: pointer to structure zfcp_qdio
L
Linus Torvalds 已提交
322
 */
323
void zfcp_qdio_close(struct zfcp_qdio *qdio)
L
Linus Torvalds 已提交
324
{
325 326
	struct zfcp_adapter *adapter = qdio->adapter;
	int idx, count;
S
Swen Schillig 已提交
327

328
	if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
S
Swen Schillig 已提交
329 330 331
		return;

	/* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
332
	spin_lock_bh(&qdio->req_q_lock);
333
	atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
334
	spin_unlock_bh(&qdio->req_q_lock);
S
Swen Schillig 已提交
335

336 337
	wake_up(&qdio->req_q_wq);

338
	qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
S
Swen Schillig 已提交
339 340

	/* cleanup used outbound sbals */
341
	count = atomic_read(&qdio->req_q_free);
S
Swen Schillig 已提交
342
	if (count < QDIO_MAX_BUFFERS_PER_Q) {
343
		idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
S
Swen Schillig 已提交
344
		count = QDIO_MAX_BUFFERS_PER_Q - count;
345
		zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
L
Linus Torvalds 已提交
346
	}
347 348
	qdio->req_q_idx = 0;
	atomic_set(&qdio->req_q_free, 0);
L
Linus Torvalds 已提交
349 350
}

S
Swen Schillig 已提交
351 352
/**
 * zfcp_qdio_open - prepare and initialize response queue
353
 * @qdio: pointer to struct zfcp_qdio
S
Swen Schillig 已提交
354 355
 * Returns: 0 on success, otherwise -EIO
 */
356
int zfcp_qdio_open(struct zfcp_qdio *qdio)
S
Swen Schillig 已提交
357
{
358
	struct qdio_buffer_element *sbale;
359
	struct qdio_initialize init_data;
360 361
	struct zfcp_adapter *adapter = qdio->adapter;
	struct ccw_device *cdev = adapter->ccw_device;
362
	struct qdio_ssqd_desc ssqd;
S
Swen Schillig 已提交
363 364
	int cc;

365
	if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
S
Swen Schillig 已提交
366 367
		return -EIO;

368 369 370
	zfcp_qdio_setup_init_data(&init_data, qdio);

	if (qdio_establish(&init_data))
371
		goto failed_establish;
S
Swen Schillig 已提交
372

373 374 375 376 377 378 379
	if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
		goto failed_qdio;

	if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
		atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
				&qdio->adapter->status);

380
	if (qdio_activate(cdev))
S
Swen Schillig 已提交
381 382 383
		goto failed_qdio;

	for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
384
		sbale = &(qdio->res_q[cc]->element[0]);
S
Swen Schillig 已提交
385 386 387 388 389
		sbale->length = 0;
		sbale->flags = SBAL_FLAGS_LAST_ENTRY;
		sbale->addr = NULL;
	}

390
	if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
S
Swen Schillig 已提交
391 392 393
		goto failed_qdio;

	/* set index of first avalable SBALS / number of available SBALS */
394 395
	qdio->req_q_idx = 0;
	atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
S
Swen Schillig 已提交
396 397 398 399

	return 0;

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");
S
Swen Schillig 已提交
404 405
	return -EIO;
}
406 407 408 409 410 411 412 413 414 415 416 417

void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
{
	int p;

	if (!qdio)
		return;

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

	for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
418 419
		free_page((unsigned long) qdio->req_q[p]);
		free_page((unsigned long) qdio->res_q[p]);
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	}

	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;
}