Blame view

drivers/fsi/fsi-scom.c 16.1 KB
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * SCOM FSI Client device driver
   *
   * Copyright (C) IBM Corporation 2016
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
  
  #include <linux/fsi.h>
  #include <linux/module.h>
  #include <linux/cdev.h>
  #include <linux/delay.h>
  #include <linux/fs.h>
  #include <linux/uaccess.h>
  #include <linux/slab.h>
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
23
  #include <linux/cdev.h>
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
24
  #include <linux/list.h>
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
25

6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
26
  #include <uapi/linux/fsi.h>
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
27
  #define FSI_ENGID_SCOM		0x5
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
28
29
30
31
  /* SCOM engine register set */
  #define SCOM_DATA0_REG		0x00
  #define SCOM_DATA1_REG		0x04
  #define SCOM_CMD_REG		0x08
f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
32
33
34
  #define SCOM_FSI2PIB_RESET_REG	0x18
  #define SCOM_STATUS_REG		0x1C /* Read */
  #define SCOM_PIB_RESET_REG	0x1C /* Write */
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
35

f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
36
  /* Command register */
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
37
  #define SCOM_WRITE_CMD		0x80000000
f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
38
39
40
41
42
  #define SCOM_READ_CMD		0x00000000
  
  /* Status register bits */
  #define SCOM_STATUS_ERR_SUMMARY		0x80000000
  #define SCOM_STATUS_PROTECTION		0x01000000
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
43
  #define SCOM_STATUS_PARITY		0x04000000
f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
44
45
46
47
48
49
  #define SCOM_STATUS_PIB_ABORT		0x00100000
  #define SCOM_STATUS_PIB_RESP_MASK	0x00007000
  #define SCOM_STATUS_PIB_RESP_SHIFT	12
  
  #define SCOM_STATUS_ANY_ERR		(SCOM_STATUS_ERR_SUMMARY | \
  					 SCOM_STATUS_PROTECTION | \
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
50
  					 SCOM_STATUS_PARITY |	  \
f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
51
52
  					 SCOM_STATUS_PIB_ABORT | \
  					 SCOM_STATUS_PIB_RESP_MASK)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /* SCOM address encodings */
  #define XSCOM_ADDR_IND_FLAG		BIT_ULL(63)
  #define XSCOM_ADDR_INF_FORM1		BIT_ULL(60)
  
  /* SCOM indirect stuff */
  #define XSCOM_ADDR_DIRECT_PART		0x7fffffffull
  #define XSCOM_ADDR_INDIRECT_PART	0x000fffff00000000ull
  #define XSCOM_DATA_IND_READ		BIT_ULL(63)
  #define XSCOM_DATA_IND_COMPLETE		BIT_ULL(31)
  #define XSCOM_DATA_IND_ERR_MASK		0x70000000ull
  #define XSCOM_DATA_IND_ERR_SHIFT	28
  #define XSCOM_DATA_IND_DATA		0x0000ffffull
  #define XSCOM_DATA_IND_FORM1_DATA	0x000fffffffffffffull
  #define XSCOM_ADDR_FORM1_LOW		0x000ffffffffull
  #define XSCOM_ADDR_FORM1_HI		0xfff00000000ull
  #define XSCOM_ADDR_FORM1_HI_SHIFT	20
  
  /* Retries */
  #define SCOM_MAX_RETRIES		100	/* Retries on busy */
  #define SCOM_MAX_IND_RETRIES		10	/* Retries indirect not ready */
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
73
74
75
76
  
  struct scom_device {
  	struct list_head link;
  	struct fsi_device *fsi_dev;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
77
78
  	struct device dev;
  	struct cdev cdev;
162c39467   Benjamin Herrenschmidt   fsi: scom: Add mu...
79
  	struct mutex lock;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
80
  	bool dead;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
81
  };
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
82
83
  static int __put_scom(struct scom_device *scom_dev, uint64_t value,
  		      uint32_t addr, uint32_t *status)
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
84
  {
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
85
  	__be32 data, raw_status;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
86
  	int rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
87

680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
88
89
90
91
  	data = cpu_to_be32((value >> 32) & 0xffffffff);
  	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
  				sizeof(uint32_t));
  	if (rc)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
92
  		return rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
93
94
95
96
97
  
  	data = cpu_to_be32(value & 0xffffffff);
  	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
  				sizeof(uint32_t));
  	if (rc)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
98
  		return rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
99
100
  
  	data = cpu_to_be32(SCOM_WRITE_CMD | addr);
162c39467   Benjamin Herrenschmidt   fsi: scom: Add mu...
101
  	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
102
  				sizeof(uint32_t));
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
103
104
105
106
107
108
109
110
111
  	if (rc)
  		return rc;
  	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
  			     sizeof(uint32_t));
  	if (rc)
  		return rc;
  	*status = be32_to_cpu(raw_status);
  
  	return 0;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
112
  }
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
113
114
  static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
  		      uint32_t addr, uint32_t *status)
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
115
  {
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
116
  	__be32 data, raw_status;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
117
  	int rc;
162c39467   Benjamin Herrenschmidt   fsi: scom: Add mu...
118

680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
119
  	*value = 0ULL;
f14330444   Benjamin Herrenschmidt   fsi: scom: Add re...
120
  	data = cpu_to_be32(SCOM_READ_CMD | addr);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
121
122
123
  	rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
  				sizeof(uint32_t));
  	if (rc)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
124
125
126
127
128
  		return rc;
  	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
  			     sizeof(uint32_t));
  	if (rc)
  		return rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
129

6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
130
131
132
133
134
  	/*
  	 * Read the data registers even on error, so we don't have
  	 * to interpret the status register here.
  	 */
  	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
135
136
  				sizeof(uint32_t));
  	if (rc)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
137
138
139
  		return rc;
  	*value |= (uint64_t)be32_to_cpu(data) << 32;
  	rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
140
141
  				sizeof(uint32_t));
  	if (rc)
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  		return rc;
  	*value |= be32_to_cpu(data);
  	*status = be32_to_cpu(raw_status);
  
  	return rc;
  }
  
  static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
  				   uint64_t addr, uint32_t *status)
  {
  	uint64_t ind_data, ind_addr;
  	int rc, retries, err = 0;
  
  	if (value & ~XSCOM_DATA_IND_DATA)
  		return -EINVAL;
  
  	ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
  	ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
  	rc = __put_scom(scom, ind_data, ind_addr, status);
  	if (rc || (*status & SCOM_STATUS_ANY_ERR))
  		return rc;
  
  	for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
  		rc = __get_scom(scom, &ind_data, addr, status);
  		if (rc || (*status & SCOM_STATUS_ANY_ERR))
  			return rc;
  
  		err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
  		*status = err << SCOM_STATUS_PIB_RESP_SHIFT;
  		if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
  			return 0;
  
  		msleep(1);
  	}
  	return rc;
  }
  
  static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
  				   uint64_t addr, uint32_t *status)
  {
  	uint64_t ind_data, ind_addr;
  
  	if (value & ~XSCOM_DATA_IND_FORM1_DATA)
  		return -EINVAL;
  
  	ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
  	ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
  	return __put_scom(scom, ind_data, ind_addr, status);
  }
  
  static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
  				   uint64_t addr, uint32_t *status)
  {
  	uint64_t ind_data, ind_addr;
  	int rc, retries, err = 0;
  
  	ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
  	ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
  	rc = __put_scom(scom, ind_data, ind_addr, status);
  	if (rc || (*status & SCOM_STATUS_ANY_ERR))
  		return rc;
  
  	for (retries = 0; retries < SCOM_MAX_IND_RETRIES; retries++) {
  		rc = __get_scom(scom, &ind_data, addr, status);
  		if (rc || (*status & SCOM_STATUS_ANY_ERR))
  			return rc;
  
  		err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
  		*status = err << SCOM_STATUS_PIB_RESP_SHIFT;
  		*value = ind_data & XSCOM_DATA_IND_DATA;
  
  		if ((ind_data & XSCOM_DATA_IND_COMPLETE) || (err != SCOM_PIB_BLOCKED))
  			return 0;
  
  		msleep(1);
  	}
  	return rc;
  }
  
  static int raw_put_scom(struct scom_device *scom, uint64_t value,
  			uint64_t addr, uint32_t *status)
  {
  	if (addr & XSCOM_ADDR_IND_FLAG) {
  		if (addr & XSCOM_ADDR_INF_FORM1)
  			return put_indirect_scom_form1(scom, value, addr, status);
  		else
  			return put_indirect_scom_form0(scom, value, addr, status);
  	} else
  		return __put_scom(scom, value, addr, status);
  }
  
  static int raw_get_scom(struct scom_device *scom, uint64_t *value,
  			uint64_t addr, uint32_t *status)
  {
  	if (addr & XSCOM_ADDR_IND_FLAG) {
  		if (addr & XSCOM_ADDR_INF_FORM1)
  			return -ENXIO;
  		return get_indirect_scom_form0(scom, value, addr, status);
  	} else
  		return __get_scom(scom, value, addr, status);
  }
  
  static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
  {
  	uint32_t dummy = -1;
  
  	if (status & SCOM_STATUS_PROTECTION)
  		return -EPERM;
  	if (status & SCOM_STATUS_PARITY) {
  		fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  				 sizeof(uint32_t));
  		return -EIO;
  	}
  	/* Return -EBUSY on PIB abort to force a retry */
  	if (status & SCOM_STATUS_PIB_ABORT)
  		return -EBUSY;
  	if (status & SCOM_STATUS_ERR_SUMMARY) {
  		fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  				 sizeof(uint32_t));
  		return -EIO;
  	}
  	return 0;
  }
  
  static int handle_pib_status(struct scom_device *scom, uint8_t status)
  {
  	uint32_t dummy = -1;
  
  	if (status == SCOM_PIB_SUCCESS)
  		return 0;
  	if (status == SCOM_PIB_BLOCKED)
  		return -EBUSY;
  
  	/* Reset the bridge */
  	fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  			 sizeof(uint32_t));
  
  	switch(status) {
  	case SCOM_PIB_OFFLINE:
  		return -ENODEV;
  	case SCOM_PIB_BAD_ADDR:
  		return -ENXIO;
  	case SCOM_PIB_TIMEOUT:
  		return -ETIMEDOUT;
  	case SCOM_PIB_PARTIAL:
  	case SCOM_PIB_CLK_ERR:
  	case SCOM_PIB_PARITY_ERR:
  	default:
  		return -EIO;
  	}
  }
  
  static int put_scom(struct scom_device *scom, uint64_t value,
  		    uint64_t addr)
  {
  	uint32_t status, dummy = -1;
  	int rc, retries;
  
  	for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
  		rc = raw_put_scom(scom, value, addr, &status);
  		if (rc) {
  			/* Try resetting the bridge if FSI fails */
  			if (rc != -ENODEV && retries == 0) {
  				fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
  						 &dummy, sizeof(uint32_t));
  				rc = -EBUSY;
  			} else
  				return rc;
  		} else
  			rc = handle_fsi2pib_status(scom, status);
  		if (rc && rc != -EBUSY)
  			break;
  		if (rc == 0) {
  			rc = handle_pib_status(scom,
  					       (status & SCOM_STATUS_PIB_RESP_MASK)
  					       >> SCOM_STATUS_PIB_RESP_SHIFT);
  			if (rc && rc != -EBUSY)
  				break;
  		}
  		if (rc == 0)
  			break;
  		msleep(1);
  	}
  	return rc;
  }
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
327

6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  static int get_scom(struct scom_device *scom, uint64_t *value,
  		    uint64_t addr)
  {
  	uint32_t status, dummy = -1;
  	int rc, retries;
  
  	for (retries = 0; retries < SCOM_MAX_RETRIES; retries++) {
  		rc = raw_get_scom(scom, value, addr, &status);
  		if (rc) {
  			/* Try resetting the bridge if FSI fails */
  			if (rc != -ENODEV && retries == 0) {
  				fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG,
  						 &dummy, sizeof(uint32_t));
  				rc = -EBUSY;
  			} else
  				return rc;
  		} else
  			rc = handle_fsi2pib_status(scom, status);
  		if (rc && rc != -EBUSY)
  			break;
  		if (rc == 0) {
  			rc = handle_pib_status(scom,
  					       (status & SCOM_STATUS_PIB_RESP_MASK)
  					       >> SCOM_STATUS_PIB_RESP_SHIFT);
  			if (rc && rc != -EBUSY)
  				break;
  		}
  		if (rc == 0)
  			break;
  		msleep(1);
  	}
162c39467   Benjamin Herrenschmidt   fsi: scom: Add mu...
359
  	return rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
360
361
362
  }
  
  static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
bd2133645   Benjamin Herrenschmidt   fsi: scom: Whites...
363
  			 loff_t *offset)
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
364
  {
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
365
  	struct scom_device *scom = filep->private_data;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
366
367
  	struct device *dev = &scom->fsi_dev->dev;
  	uint64_t val;
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
368
  	int rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
369
370
371
  
  	if (len != sizeof(uint64_t))
  		return -EINVAL;
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
372
  	mutex_lock(&scom->lock);
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
373
374
375
376
  	if (scom->dead)
  		rc = -ENODEV;
  	else
  		rc = get_scom(scom, &val, *offset);
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
377
  	mutex_unlock(&scom->lock);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
  	if (rc) {
  		dev_dbg(dev, "get_scom fail:%d
  ", rc);
  		return rc;
  	}
  
  	rc = copy_to_user(buf, &val, len);
  	if (rc)
  		dev_dbg(dev, "copy to user failed:%d
  ", rc);
  
  	return rc ? rc : len;
  }
  
  static ssize_t scom_write(struct file *filep, const char __user *buf,
bd2133645   Benjamin Herrenschmidt   fsi: scom: Whites...
393
  			  size_t len, loff_t *offset)
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
394
395
  {
  	int rc;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
396
  	struct scom_device *scom = filep->private_data;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
397
398
399
400
401
402
403
404
405
406
407
408
  	struct device *dev = &scom->fsi_dev->dev;
  	uint64_t val;
  
  	if (len != sizeof(uint64_t))
  		return -EINVAL;
  
  	rc = copy_from_user(&val, buf, len);
  	if (rc) {
  		dev_dbg(dev, "copy from user failed:%d
  ", rc);
  		return -EINVAL;
  	}
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
409
  	mutex_lock(&scom->lock);
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
410
411
412
413
  	if (scom->dead)
  		rc = -ENODEV;
  	else
  		rc = put_scom(scom, val, *offset);
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
414
  	mutex_unlock(&scom->lock);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  	if (rc) {
  		dev_dbg(dev, "put_scom failed with:%d
  ", rc);
  		return rc;
  	}
  
  	return len;
  }
  
  static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
  {
  	switch (whence) {
  	case SEEK_CUR:
  		break;
  	case SEEK_SET:
  		file->f_pos = offset;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return offset;
  }
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
438
439
440
441
442
443
444
445
446
447
448
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  static void raw_convert_status(struct scom_access *acc, uint32_t status)
  {
  	acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
  		SCOM_STATUS_PIB_RESP_SHIFT;
  	acc->intf_errors = 0;
  
  	if (status & SCOM_STATUS_PROTECTION)
  		acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
  	else if (status & SCOM_STATUS_PARITY)
  		acc->intf_errors |= SCOM_INTF_ERR_PARITY;
  	else if (status & SCOM_STATUS_PIB_ABORT)
  		acc->intf_errors |= SCOM_INTF_ERR_ABORT;
  	else if (status & SCOM_STATUS_ERR_SUMMARY)
  		acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
  }
  
  static int scom_raw_read(struct scom_device *scom, void __user *argp)
  {
  	struct scom_access acc;
  	uint32_t status;
  	int rc;
  
  	if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
  		return -EFAULT;
  
  	rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
  	if (rc)
  		return rc;
  	raw_convert_status(&acc, status);
  	if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
  		return -EFAULT;
  	return 0;
  }
  
  static int scom_raw_write(struct scom_device *scom, void __user *argp)
  {
  	u64 prev_data, mask, data;
  	struct scom_access acc;
  	uint32_t status;
  	int rc;
  
  	if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
  		return -EFAULT;
  
  	if (acc.mask) {
  		rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
  		if (rc)
  			return rc;
  		if (status & SCOM_STATUS_ANY_ERR)
  			goto fail;
  		mask = acc.mask;
  	} else {
  		prev_data = mask = -1ull;
  	}
  	data = (prev_data & ~mask) | (acc.data & mask);
  	rc = raw_put_scom(scom, data, acc.addr, &status);
  	if (rc)
  		return rc;
   fail:
  	raw_convert_status(&acc, status);
  	if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
  		return -EFAULT;
  	return 0;
  }
  
  static int scom_reset(struct scom_device *scom, void __user *argp)
  {
  	uint32_t flags, dummy = -1;
  	int rc = 0;
  
  	if (get_user(flags, (__u32 __user *)argp))
  		return -EFAULT;
  	if (flags & SCOM_RESET_PIB)
  		rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
  				      sizeof(uint32_t));
  	if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
  		rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  				      sizeof(uint32_t));
  	return rc;
  }
  
  static int scom_check(struct scom_device *scom, void __user *argp)
  {
  	/* Still need to find out how to get "protected" */
  	return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
  }
  
  static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  {
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
527
  	struct scom_device *scom = file->private_data;
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
528
529
530
531
  	void __user *argp = (void __user *)arg;
  	int rc = -ENOTTY;
  
  	mutex_lock(&scom->lock);
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
532
533
534
535
  	if (scom->dead) {
  		mutex_unlock(&scom->lock);
  		return -ENODEV;
  	}
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
  	switch(cmd) {
  	case FSI_SCOM_CHECK:
  		rc = scom_check(scom, argp);
  		break;
  	case FSI_SCOM_READ:
  		rc = scom_raw_read(scom, argp);
  		break;
  	case FSI_SCOM_WRITE:
  		rc = scom_raw_write(scom, argp);
  		break;
  	case FSI_SCOM_RESET:
  		rc = scom_reset(scom, argp);
  		break;
  	}
  	mutex_unlock(&scom->lock);
  	return rc;
  }
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
553
554
555
556
557
558
559
560
  static int scom_open(struct inode *inode, struct file *file)
  {
  	struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
  
  	file->private_data = scom;
  
  	return 0;
  }
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
561
  static const struct file_operations scom_fops = {
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
562
  	.owner		= THIS_MODULE,
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
563
  	.open		= scom_open,
6b293258c   Benjamin Herrenschmidt   fsi: scom: Major ...
564
565
566
567
  	.llseek		= scom_llseek,
  	.read		= scom_read,
  	.write		= scom_write,
  	.unlocked_ioctl	= scom_ioctl,
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
568
  };
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
569
570
571
572
573
574
575
  static void scom_free(struct device *dev)
  {
  	struct scom_device *scom = container_of(dev, struct scom_device, dev);
  
  	put_device(&scom->fsi_dev->dev);
  	kfree(scom);
  }
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
576
577
578
579
  static int scom_probe(struct device *dev)
  {
  	struct fsi_device *fsi_dev = to_fsi_dev(dev);
  	struct scom_device *scom;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
580
  	int rc, didx;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
581

d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
582
  	scom = kzalloc(sizeof(*scom), GFP_KERNEL);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
583
584
  	if (!scom)
  		return -ENOMEM;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
585
  	dev_set_drvdata(dev, scom);
162c39467   Benjamin Herrenschmidt   fsi: scom: Add mu...
586
  	mutex_init(&scom->lock);
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
587
588
589
590
591
592
  
  	/* Grab a reference to the device (parent of our cdev), we'll drop it later */
  	if (!get_device(dev)) {
  		kfree(scom);
  		return -ENODEV;
  	}
aa1221b25   Benjamin Herrenschmidt   fsi: scom: Fix NU...
593
  	scom->fsi_dev = fsi_dev;
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  
  	/* Create chardev for userspace access */
  	scom->dev.type = &fsi_cdev_type;
  	scom->dev.parent = dev;
  	scom->dev.release = scom_free;
  	device_initialize(&scom->dev);
  
  	/* Allocate a minor in the FSI space */
  	rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
  	if (rc)
  		goto err;
  
  	dev_set_name(&scom->dev, "scom%d", didx);
  	cdev_init(&scom->cdev, &scom_fops);
  	rc = cdev_device_add(&scom->cdev, &scom->dev);
  	if (rc) {
  		dev_err(dev, "Error %d creating char device %s
  ",
  			rc, dev_name(&scom->dev));
  		goto err_free_minor;
  	}
  
  	return 0;
   err_free_minor:
  	fsi_free_minor(scom->dev.devt);
   err:
  	put_device(&scom->dev);
  	return rc;
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
622
623
624
625
  }
  
  static int scom_remove(struct device *dev)
  {
d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
626
  	struct scom_device *scom = dev_get_drvdata(dev);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
627

d8f458765   Benjamin Herrenschmidt   fsi: scom: Conver...
628
629
630
631
632
633
  	mutex_lock(&scom->lock);
  	scom->dead = true;
  	mutex_unlock(&scom->lock);
  	cdev_device_del(&scom->cdev, &scom->dev);
  	fsi_free_minor(scom->dev.devt);
  	put_device(&scom->dev);
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
  
  	return 0;
  }
  
  static struct fsi_device_id scom_ids[] = {
  	{
  		.engine_type = FSI_ENGID_SCOM,
  		.version = FSI_VERSION_ANY,
  	},
  	{ 0 }
  };
  
  static struct fsi_driver scom_drv = {
  	.id_table = scom_ids,
  	.drv = {
  		.name = "scom",
  		.bus = &fsi_bus_type,
  		.probe = scom_probe,
  		.remove = scom_remove,
  	}
  };
  
  static int scom_init(void)
  {
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
658
659
660
661
662
  	return fsi_driver_register(&scom_drv);
  }
  
  static void scom_exit(void)
  {
680ca6dcf   Christopher Bostic   drivers/fsi: Add ...
663
664
665
666
667
668
  	fsi_driver_unregister(&scom_drv);
  }
  
  module_init(scom_init);
  module_exit(scom_exit);
  MODULE_LICENSE("GPL");