1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/fs.h>
18#include <linux/fsi.h>
19#include <linux/fsi-sbefifo.h>
20#include <linux/kernel.h>
21#include <linux/cdev.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/of_platform.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/uaccess.h>
30#include <linux/delay.h>
31#include <linux/uio.h>
32#include <linux/vmalloc.h>
33#include <linux/mm.h>
34
35
36
37
38
39
40#define DEVICE_NAME "sbefifo"
41#define FSI_ENGID_SBE 0x22
42
43
44
45
46
47
48#define SBEFIFO_UP 0x00
49#define SBEFIFO_DOWN 0x40
50
51
52#define SBEFIFO_FIFO 0x00
53#define SBEFIFO_STS 0x04
54#define SBEFIFO_STS_PARITY_ERR 0x20000000
55#define SBEFIFO_STS_RESET_REQ 0x02000000
56#define SBEFIFO_STS_GOT_EOT 0x00800000
57#define SBEFIFO_STS_MAX_XFER_LIMIT 0x00400000
58#define SBEFIFO_STS_FULL 0x00200000
59#define SBEFIFO_STS_EMPTY 0x00100000
60#define SBEFIFO_STS_ECNT_MASK 0x000f0000
61#define SBEFIFO_STS_ECNT_SHIFT 16
62#define SBEFIFO_STS_VALID_MASK 0x0000ff00
63#define SBEFIFO_STS_VALID_SHIFT 8
64#define SBEFIFO_STS_EOT_MASK 0x000000ff
65#define SBEFIFO_STS_EOT_SHIFT 0
66#define SBEFIFO_EOT_RAISE 0x08
67#define SBEFIFO_REQ_RESET 0x0C
68#define SBEFIFO_PERFORM_RESET 0x10
69#define SBEFIFO_EOT_ACK 0x14
70#define SBEFIFO_DOWN_MAX 0x18
71
72
73#define CFAM_GP_MBOX_SBM_ADDR 0x2824
74
75#define CFAM_SBM_SBE_BOOTED 0x80000000
76#define CFAM_SBM_SBE_ASYNC_FFDC 0x40000000
77#define CFAM_SBM_SBE_STATE_MASK 0x00f00000
78#define CFAM_SBM_SBE_STATE_SHIFT 20
79
80enum sbe_state
81{
82 SBE_STATE_UNKNOWN = 0x0,
83 SBE_STATE_IPLING = 0x1,
84 SBE_STATE_ISTEP = 0x2,
85 SBE_STATE_MPIPL = 0x3,
86 SBE_STATE_RUNTIME = 0x4,
87 SBE_STATE_DMT = 0x5,
88 SBE_STATE_DUMP = 0x6,
89 SBE_STATE_FAILURE = 0x7,
90 SBE_STATE_QUIESCE = 0x8,
91};
92
93
94#define SBEFIFO_FIFO_DEPTH 8
95
96
97#define sbefifo_empty(sts) ((sts) & SBEFIFO_STS_EMPTY)
98#define sbefifo_full(sts) ((sts) & SBEFIFO_STS_FULL)
99#define sbefifo_parity_err(sts) ((sts) & SBEFIFO_STS_PARITY_ERR)
100#define sbefifo_populated(sts) (((sts) & SBEFIFO_STS_ECNT_MASK) >> SBEFIFO_STS_ECNT_SHIFT)
101#define sbefifo_vacant(sts) (SBEFIFO_FIFO_DEPTH - sbefifo_populated(sts))
102#define sbefifo_eot_set(sts) (((sts) & SBEFIFO_STS_EOT_MASK) >> SBEFIFO_STS_EOT_SHIFT)
103
104
105#define SBEFIFO_RESET_TIMEOUT 10000
106
107
108#define SBEFIFO_TIMEOUT_START_CMD 10000
109#define SBEFIFO_TIMEOUT_IN_CMD 1000
110#define SBEFIFO_TIMEOUT_START_RSP 10000
111#define SBEFIFO_TIMEOUT_IN_RSP 1000
112
113
114#define SBEFIFO_MAX_USER_CMD_LEN (0x100000 + PAGE_SIZE)
115#define SBEFIFO_RESET_MAGIC 0x52534554
116
117struct sbefifo {
118 uint32_t magic;
119#define SBEFIFO_MAGIC 0x53424546
120 struct fsi_device *fsi_dev;
121 struct device dev;
122 struct cdev cdev;
123 struct mutex lock;
124 bool broken;
125 bool dead;
126 bool async_ffdc;
127};
128
129struct sbefifo_user {
130 struct sbefifo *sbefifo;
131 struct mutex file_lock;
132 void *cmd_page;
133 void *pending_cmd;
134 size_t pending_len;
135};
136
137static DEFINE_MUTEX(sbefifo_ffdc_mutex);
138
139
140static void __sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
141 size_t ffdc_sz, bool internal)
142{
143 int pack = 0;
144#define FFDC_LSIZE 60
145 static char ffdc_line[FFDC_LSIZE];
146 char *p = ffdc_line;
147
148 while (ffdc_sz) {
149 u32 w0, w1, w2, i;
150 if (ffdc_sz < 3) {
151 dev_err(dev, "SBE invalid FFDC package size %zd\n", ffdc_sz);
152 return;
153 }
154 w0 = be32_to_cpu(*(ffdc++));
155 w1 = be32_to_cpu(*(ffdc++));
156 w2 = be32_to_cpu(*(ffdc++));
157 ffdc_sz -= 3;
158 if ((w0 >> 16) != 0xFFDC) {
159 dev_err(dev, "SBE invalid FFDC package signature %08x %08x %08x\n",
160 w0, w1, w2);
161 break;
162 }
163 w0 &= 0xffff;
164 if (w0 > ffdc_sz) {
165 dev_err(dev, "SBE FFDC package len %d words but only %zd remaining\n",
166 w0, ffdc_sz);
167 w0 = ffdc_sz;
168 break;
169 }
170 if (internal) {
171 dev_warn(dev, "+---- SBE FFDC package %d for async err -----+\n",
172 pack++);
173 } else {
174 dev_warn(dev, "+---- SBE FFDC package %d for cmd %02x:%02x -----+\n",
175 pack++, (w1 >> 8) & 0xff, w1 & 0xff);
176 }
177 dev_warn(dev, "| Response code: %08x |\n", w2);
178 dev_warn(dev, "|-------------------------------------------|\n");
179 for (i = 0; i < w0; i++) {
180 if ((i & 3) == 0) {
181 p = ffdc_line;
182 p += sprintf(p, "| %04x:", i << 4);
183 }
184 p += sprintf(p, " %08x", be32_to_cpu(*(ffdc++)));
185 ffdc_sz--;
186 if ((i & 3) == 3 || i == (w0 - 1)) {
187 while ((i & 3) < 3) {
188 p += sprintf(p, " ");
189 i++;
190 }
191 dev_warn(dev, "%s |\n", ffdc_line);
192 }
193 }
194 dev_warn(dev, "+-------------------------------------------+\n");
195 }
196}
197
198static void sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
199 size_t ffdc_sz, bool internal)
200{
201 mutex_lock(&sbefifo_ffdc_mutex);
202 __sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, internal);
203 mutex_unlock(&sbefifo_ffdc_mutex);
204}
205
206int sbefifo_parse_status(struct device *dev, u16 cmd, __be32 *response,
207 size_t resp_len, size_t *data_len)
208{
209 u32 dh, s0, s1;
210 size_t ffdc_sz;
211
212 if (resp_len < 3) {
213 pr_debug("sbefifo: cmd %04x, response too small: %zd\n",
214 cmd, resp_len);
215 return -ENXIO;
216 }
217 dh = be32_to_cpu(response[resp_len - 1]);
218 if (dh > resp_len || dh < 3) {
219 dev_err(dev, "SBE cmd %02x:%02x status offset out of range: %d/%zd\n",
220 cmd >> 8, cmd & 0xff, dh, resp_len);
221 return -ENXIO;
222 }
223 s0 = be32_to_cpu(response[resp_len - dh]);
224 s1 = be32_to_cpu(response[resp_len - dh + 1]);
225 if (((s0 >> 16) != 0xC0DE) || ((s0 & 0xffff) != cmd)) {
226 dev_err(dev, "SBE cmd %02x:%02x, status signature invalid: 0x%08x 0x%08x\n",
227 cmd >> 8, cmd & 0xff, s0, s1);
228 return -ENXIO;
229 }
230 if (s1 != 0) {
231 ffdc_sz = dh - 3;
232 dev_warn(dev, "SBE error cmd %02x:%02x status=%04x:%04x\n",
233 cmd >> 8, cmd & 0xff, s1 >> 16, s1 & 0xffff);
234 if (ffdc_sz)
235 sbefifo_dump_ffdc(dev, &response[resp_len - dh + 2],
236 ffdc_sz, false);
237 }
238 if (data_len)
239 *data_len = resp_len - dh;
240
241
242
243
244
245 return s1;
246}
247EXPORT_SYMBOL_GPL(sbefifo_parse_status);
248
249static int sbefifo_regr(struct sbefifo *sbefifo, int reg, u32 *word)
250{
251 __be32 raw_word;
252 int rc;
253
254 rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word,
255 sizeof(raw_word));
256 if (rc)
257 return rc;
258
259 *word = be32_to_cpu(raw_word);
260
261 return 0;
262}
263
264static int sbefifo_regw(struct sbefifo *sbefifo, int reg, u32 word)
265{
266 __be32 raw_word = cpu_to_be32(word);
267
268 return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word,
269 sizeof(raw_word));
270}
271
272static int sbefifo_check_sbe_state(struct sbefifo *sbefifo)
273{
274 __be32 raw_word;
275 u32 sbm;
276 int rc;
277
278 rc = fsi_slave_read(sbefifo->fsi_dev->slave, CFAM_GP_MBOX_SBM_ADDR,
279 &raw_word, sizeof(raw_word));
280 if (rc)
281 return rc;
282 sbm = be32_to_cpu(raw_word);
283
284
285 if (!(sbm & CFAM_SBM_SBE_BOOTED))
286 return -ESHUTDOWN;
287
288
289 switch ((sbm & CFAM_SBM_SBE_STATE_MASK) >> CFAM_SBM_SBE_STATE_SHIFT) {
290 case SBE_STATE_UNKNOWN:
291 return -ESHUTDOWN;
292 case SBE_STATE_DMT:
293 return -EBUSY;
294 case SBE_STATE_IPLING:
295 case SBE_STATE_ISTEP:
296 case SBE_STATE_MPIPL:
297 case SBE_STATE_RUNTIME:
298 case SBE_STATE_DUMP:
299 break;
300 case SBE_STATE_FAILURE:
301 case SBE_STATE_QUIESCE:
302 return -ESHUTDOWN;
303 }
304
305
306 if (sbm & CFAM_SBM_SBE_ASYNC_FFDC)
307 sbefifo->async_ffdc = true;
308
309 return 0;
310}
311
312
313static int sbefifo_down_read(struct sbefifo *sbefifo, __be32 *word)
314{
315 return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DOWN, word,
316 sizeof(*word));
317}
318
319static int sbefifo_up_write(struct sbefifo *sbefifo, __be32 word)
320{
321 return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word,
322 sizeof(word));
323}
324
325static int sbefifo_request_reset(struct sbefifo *sbefifo)
326{
327 struct device *dev = &sbefifo->fsi_dev->dev;
328 u32 status, timeout;
329 int rc;
330
331 dev_dbg(dev, "Requesting FIFO reset\n");
332
333
334 sbefifo->broken = true;
335
336
337 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1);
338 if (rc) {
339 dev_err(dev, "Sending reset request failed, rc=%d\n", rc);
340 return rc;
341 }
342
343
344 for (timeout = 0; timeout < SBEFIFO_RESET_TIMEOUT; timeout++) {
345 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status);
346 if (rc) {
347 dev_err(dev, "Failed to read UP fifo status during reset"
348 " , rc=%d\n", rc);
349 return rc;
350 }
351
352 if (!(status & SBEFIFO_STS_RESET_REQ)) {
353 dev_dbg(dev, "FIFO reset done\n");
354 sbefifo->broken = false;
355 return 0;
356 }
357
358 msleep(1);
359 }
360 dev_err(dev, "FIFO reset timed out\n");
361
362 return -ETIMEDOUT;
363}
364
365static int sbefifo_cleanup_hw(struct sbefifo *sbefifo)
366{
367 struct device *dev = &sbefifo->fsi_dev->dev;
368 u32 up_status, down_status;
369 bool need_reset = false;
370 int rc;
371
372 rc = sbefifo_check_sbe_state(sbefifo);
373 if (rc) {
374 dev_dbg(dev, "SBE state=%d\n", rc);
375 return rc;
376 }
377
378
379 if (sbefifo->broken)
380 goto do_reset;
381
382 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status);
383 if (rc) {
384 dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc);
385
386
387 sbefifo->broken = true;
388 return rc;
389 }
390
391 rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status);
392 if (rc) {
393 dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc);
394
395
396 sbefifo->broken = true;
397 return rc;
398 }
399
400
401 if (down_status & SBEFIFO_STS_RESET_REQ) {
402 dev_info(dev, "Cleanup: FIFO reset request set, resetting\n");
403 rc = sbefifo_regw(sbefifo, SBEFIFO_UP, SBEFIFO_PERFORM_RESET);
404 if (rc) {
405 sbefifo->broken = true;
406 dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc);
407 return rc;
408 }
409 sbefifo->broken = false;
410 return 0;
411 }
412
413
414 if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR)
415 need_reset = true;
416
417
418 if (!((up_status & down_status) & SBEFIFO_STS_EMPTY))
419 need_reset = true;
420
421 if (!need_reset)
422 return 0;
423
424 dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
425 up_status, down_status);
426
427 do_reset:
428
429
430 return sbefifo_request_reset(sbefifo);
431}
432
433static int sbefifo_wait(struct sbefifo *sbefifo, bool up,
434 u32 *status, unsigned long timeout)
435{
436 struct device *dev = &sbefifo->fsi_dev->dev;
437 unsigned long end_time;
438 bool ready = false;
439 u32 addr, sts = 0;
440 int rc;
441
442 dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down");
443
444 addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS;
445
446 end_time = jiffies + timeout;
447 while (!time_after(jiffies, end_time)) {
448 cond_resched();
449 rc = sbefifo_regr(sbefifo, addr, &sts);
450 if (rc < 0) {
451 dev_err(dev, "FSI error %d reading status register\n", rc);
452 return rc;
453 }
454 if (!up && sbefifo_parity_err(sts)) {
455 dev_err(dev, "Parity error in DOWN FIFO\n");
456 return -ENXIO;
457 }
458 ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts));
459 if (ready)
460 break;
461 }
462 if (!ready) {
463 dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts);
464 return -ETIMEDOUT;
465 }
466 dev_vdbg(dev, "End of wait status: %08x\n", sts);
467
468 *status = sts;
469
470 return 0;
471}
472
473static int sbefifo_send_command(struct sbefifo *sbefifo,
474 const __be32 *command, size_t cmd_len)
475{
476 struct device *dev = &sbefifo->fsi_dev->dev;
477 size_t len, chunk, vacant = 0, remaining = cmd_len;
478 unsigned long timeout;
479 u32 status;
480 int rc;
481
482 dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n",
483 cmd_len, be32_to_cpu(command[1]));
484
485
486 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD);
487 while (remaining) {
488
489 rc = sbefifo_wait(sbefifo, true, &status, timeout);
490 if (rc < 0)
491 return rc;
492 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD);
493
494 vacant = sbefifo_vacant(status);
495 len = chunk = min(vacant, remaining);
496
497 dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n",
498 status, vacant, chunk);
499
500
501 while (len--) {
502 rc = sbefifo_up_write(sbefifo, *(command++));
503 if (rc) {
504 dev_err(dev, "FSI error %d writing UP FIFO\n", rc);
505 return rc;
506 }
507 }
508 remaining -= chunk;
509 vacant -= chunk;
510 }
511
512
513 if (!vacant) {
514 rc = sbefifo_wait(sbefifo, true, &status, timeout);
515 if (rc)
516 return rc;
517 }
518
519
520 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0);
521 if (rc)
522 dev_err(dev, "FSI error %d writing EOT\n", rc);
523 return rc;
524}
525
526static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response)
527{
528 struct device *dev = &sbefifo->fsi_dev->dev;
529 u32 status, eot_set;
530 unsigned long timeout;
531 bool overflow = false;
532 __be32 data;
533 size_t len;
534 int rc;
535
536 dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response));
537
538 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP);
539 for (;;) {
540
541 rc = sbefifo_wait(sbefifo, false, &status, timeout);
542 if (rc < 0)
543 return rc;
544 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP);
545
546
547 len = sbefifo_populated(status);
548 eot_set = sbefifo_eot_set(status);
549
550 dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set);
551
552
553 while(len--) {
554
555 rc = sbefifo_down_read(sbefifo, &data);
556 if (rc < 0)
557 return rc;
558
559
560 if (eot_set & 0x80) {
561
562
563
564
565
566
567 if (len) {
568 dev_warn(dev, "FIFO read hit"
569 " EOT with still %zd data\n",
570 len);
571 sbefifo->broken = true;
572 }
573
574
575 rc = sbefifo_regw(sbefifo,
576 SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0);
577
578
579
580
581
582
583 if (rc) {
584 dev_err(dev, "FSI error %d ack'ing EOT\n", rc);
585 sbefifo->broken = true;
586 }
587
588
589 return overflow ? -EOVERFLOW : 0;
590 }
591
592
593 if (iov_iter_count(response) >= sizeof(__be32)) {
594 if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32))
595 return -EFAULT;
596 } else {
597 dev_vdbg(dev, "Response overflowed !\n");
598
599 overflow = true;
600 }
601
602
603 eot_set <<= 1;
604 }
605 }
606
607 return -EIO;
608}
609
610static int sbefifo_do_command(struct sbefifo *sbefifo,
611 const __be32 *command, size_t cmd_len,
612 struct iov_iter *response)
613{
614
615 int rc = sbefifo_send_command(sbefifo, command, cmd_len);
616 if (rc)
617 return rc;
618
619
620 return sbefifo_read_response(sbefifo, response);
621}
622
623static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo)
624{
625 struct device *dev = &sbefifo->fsi_dev->dev;
626 struct iov_iter ffdc_iter;
627 struct kvec ffdc_iov;
628 __be32 *ffdc;
629 size_t ffdc_sz;
630 __be32 cmd[2];
631 int rc;
632
633 sbefifo->async_ffdc = false;
634 ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE);
635 if (!ffdc) {
636 dev_err(dev, "Failed to allocate SBE FFDC buffer\n");
637 return;
638 }
639 ffdc_iov.iov_base = ffdc;
640 ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE;
641 iov_iter_kvec(&ffdc_iter, WRITE, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE);
642 cmd[0] = cpu_to_be32(2);
643 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC);
644 rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter);
645 if (rc != 0) {
646 dev_err(dev, "Error %d retrieving SBE FFDC\n", rc);
647 goto bail;
648 }
649 ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter);
650 ffdc_sz /= sizeof(__be32);
651 rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc,
652 ffdc_sz, &ffdc_sz);
653 if (rc != 0) {
654 dev_err(dev, "Error %d decoding SBE FFDC\n", rc);
655 goto bail;
656 }
657 if (ffdc_sz > 0)
658 sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true);
659 bail:
660 vfree(ffdc);
661
662}
663
664static int __sbefifo_submit(struct sbefifo *sbefifo,
665 const __be32 *command, size_t cmd_len,
666 struct iov_iter *response)
667{
668 struct device *dev = &sbefifo->fsi_dev->dev;
669 int rc;
670
671 if (sbefifo->dead)
672 return -ENODEV;
673
674 if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) {
675 dev_vdbg(dev, "Invalid command len %zd (header: %d)\n",
676 cmd_len, be32_to_cpu(command[0]));
677 return -EINVAL;
678 }
679
680
681 rc = sbefifo_cleanup_hw(sbefifo);
682 if (rc)
683 return rc;
684
685
686 if (sbefifo->async_ffdc)
687 sbefifo_collect_async_ffdc(sbefifo);
688
689 rc = sbefifo_do_command(sbefifo, command, cmd_len, response);
690 if (rc != 0 && rc != -EOVERFLOW)
691 goto fail;
692 return rc;
693 fail:
694
695
696
697
698 sbefifo_request_reset(sbefifo);
699
700
701 return rc;
702}
703
704
705
706
707
708
709
710
711
712
713
714
715int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len,
716 __be32 *response, size_t *resp_len)
717{
718 struct sbefifo *sbefifo;
719 struct iov_iter resp_iter;
720 struct kvec resp_iov;
721 size_t rbytes;
722 int rc;
723
724 if (!dev)
725 return -ENODEV;
726 sbefifo = dev_get_drvdata(dev);
727 if (!sbefifo)
728 return -ENODEV;
729 if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC))
730 return -ENODEV;
731 if (!resp_len || !command || !response)
732 return -EINVAL;
733
734
735 rbytes = (*resp_len) * sizeof(__be32);
736 resp_iov.iov_base = response;
737 resp_iov.iov_len = rbytes;
738 iov_iter_kvec(&resp_iter, WRITE, &resp_iov, 1, rbytes);
739
740
741 mutex_lock(&sbefifo->lock);
742 rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter);
743 mutex_unlock(&sbefifo->lock);
744
745
746 rbytes -= iov_iter_count(&resp_iter);
747 *resp_len = rbytes / sizeof(__be32);
748
749 return rc;
750}
751EXPORT_SYMBOL_GPL(sbefifo_submit);
752
753
754
755
756
757static void sbefifo_release_command(struct sbefifo_user *user)
758{
759 if (is_vmalloc_addr(user->pending_cmd))
760 vfree(user->pending_cmd);
761 user->pending_cmd = NULL;
762 user->pending_len = 0;
763}
764
765static int sbefifo_user_open(struct inode *inode, struct file *file)
766{
767 struct sbefifo *sbefifo = container_of(inode->i_cdev, struct sbefifo, cdev);
768 struct sbefifo_user *user;
769
770 user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL);
771 if (!user)
772 return -ENOMEM;
773
774 file->private_data = user;
775 user->sbefifo = sbefifo;
776 user->cmd_page = (void *)__get_free_page(GFP_KERNEL);
777 if (!user->cmd_page) {
778 kfree(user);
779 return -ENOMEM;
780 }
781 mutex_init(&user->file_lock);
782
783 return 0;
784}
785
786static ssize_t sbefifo_user_read(struct file *file, char __user *buf,
787 size_t len, loff_t *offset)
788{
789 struct sbefifo_user *user = file->private_data;
790 struct sbefifo *sbefifo;
791 struct iov_iter resp_iter;
792 struct iovec resp_iov;
793 size_t cmd_len;
794 int rc;
795
796 if (!user)
797 return -EINVAL;
798 sbefifo = user->sbefifo;
799 if (len & 3)
800 return -EINVAL;
801
802 mutex_lock(&user->file_lock);
803
804
805 if (user->pending_len == 0) {
806 rc = -EAGAIN;
807 goto bail;
808 }
809 if (user->pending_len < 8) {
810 rc = -EINVAL;
811 goto bail;
812 }
813 cmd_len = user->pending_len >> 2;
814
815
816 resp_iov.iov_base = buf;
817 resp_iov.iov_len = len;
818 iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len);
819
820
821 mutex_lock(&sbefifo->lock);
822 rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter);
823 mutex_unlock(&sbefifo->lock);
824 if (rc < 0)
825 goto bail;
826
827
828 rc = len - iov_iter_count(&resp_iter);
829 bail:
830 sbefifo_release_command(user);
831 mutex_unlock(&user->file_lock);
832 return rc;
833}
834
835static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
836 size_t len, loff_t *offset)
837{
838 struct sbefifo_user *user = file->private_data;
839 struct sbefifo *sbefifo;
840 int rc = len;
841
842 if (!user)
843 return -EINVAL;
844 sbefifo = user->sbefifo;
845 if (len > SBEFIFO_MAX_USER_CMD_LEN)
846 return -EINVAL;
847 if (len & 3)
848 return -EINVAL;
849
850 mutex_lock(&user->file_lock);
851
852
853 if (len <= PAGE_SIZE)
854 user->pending_cmd = user->cmd_page;
855 else
856 user->pending_cmd = vmalloc(len);
857 if (!user->pending_cmd) {
858 rc = -ENOMEM;
859 goto bail;
860 }
861
862
863 if (copy_from_user(user->pending_cmd, buf, len)) {
864 rc = -EFAULT;
865 goto bail;
866 }
867
868
869 if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) ==
870 SBEFIFO_RESET_MAGIC) {
871
872
873 user->pending_len = 0;
874
875
876 mutex_lock(&sbefifo->lock);
877 rc = sbefifo_request_reset(user->sbefifo);
878 mutex_unlock(&sbefifo->lock);
879 if (rc == 0)
880 rc = 4;
881 goto bail;
882 }
883
884
885 user->pending_len = len;
886 bail:
887 if (!user->pending_len)
888 sbefifo_release_command(user);
889
890 mutex_unlock(&user->file_lock);
891
892
893 return rc;
894}
895
896static int sbefifo_user_release(struct inode *inode, struct file *file)
897{
898 struct sbefifo_user *user = file->private_data;
899
900 if (!user)
901 return -EINVAL;
902
903 sbefifo_release_command(user);
904 free_page((unsigned long)user->cmd_page);
905 kfree(user);
906
907 return 0;
908}
909
910static const struct file_operations sbefifo_fops = {
911 .owner = THIS_MODULE,
912 .open = sbefifo_user_open,
913 .read = sbefifo_user_read,
914 .write = sbefifo_user_write,
915 .release = sbefifo_user_release,
916};
917
918static void sbefifo_free(struct device *dev)
919{
920 struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
921
922 put_device(&sbefifo->fsi_dev->dev);
923 kfree(sbefifo);
924}
925
926
927
928
929
930static int sbefifo_probe(struct device *dev)
931{
932 struct fsi_device *fsi_dev = to_fsi_dev(dev);
933 struct sbefifo *sbefifo;
934 struct device_node *np;
935 struct platform_device *child;
936 char child_name[32];
937 int rc, didx, child_idx = 0;
938
939 dev_dbg(dev, "Found sbefifo device\n");
940
941 sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL);
942 if (!sbefifo)
943 return -ENOMEM;
944
945
946 if (!get_device(dev)) {
947 kfree(sbefifo);
948 return -ENODEV;
949 }
950
951 sbefifo->magic = SBEFIFO_MAGIC;
952 sbefifo->fsi_dev = fsi_dev;
953 dev_set_drvdata(dev, sbefifo);
954 mutex_init(&sbefifo->lock);
955
956
957
958
959
960 rc = sbefifo_cleanup_hw(sbefifo);
961 if (rc && rc != -ESHUTDOWN)
962 dev_err(dev, "Initial HW cleanup failed, will retry later\n");
963
964
965 sbefifo->dev.type = &fsi_cdev_type;
966 sbefifo->dev.parent = dev;
967 sbefifo->dev.release = sbefifo_free;
968 device_initialize(&sbefifo->dev);
969
970
971 rc = fsi_get_new_minor(fsi_dev, fsi_dev_sbefifo, &sbefifo->dev.devt, &didx);
972 if (rc)
973 goto err;
974
975 dev_set_name(&sbefifo->dev, "sbefifo%d", didx);
976 cdev_init(&sbefifo->cdev, &sbefifo_fops);
977 rc = cdev_device_add(&sbefifo->cdev, &sbefifo->dev);
978 if (rc) {
979 dev_err(dev, "Error %d creating char device %s\n",
980 rc, dev_name(&sbefifo->dev));
981 goto err_free_minor;
982 }
983
984
985 for_each_available_child_of_node(dev->of_node, np) {
986 snprintf(child_name, sizeof(child_name), "%s-dev%d",
987 dev_name(&sbefifo->dev), child_idx++);
988 child = of_platform_device_create(np, child_name, dev);
989 if (!child)
990 dev_warn(dev, "failed to create child %s dev\n",
991 child_name);
992 }
993
994 return 0;
995 err_free_minor:
996 fsi_free_minor(sbefifo->dev.devt);
997 err:
998 put_device(&sbefifo->dev);
999 return rc;
1000}
1001
1002static int sbefifo_unregister_child(struct device *dev, void *data)
1003{
1004 struct platform_device *child = to_platform_device(dev);
1005
1006 of_device_unregister(child);
1007 if (dev->of_node)
1008 of_node_clear_flag(dev->of_node, OF_POPULATED);
1009
1010 return 0;
1011}
1012
1013static int sbefifo_remove(struct device *dev)
1014{
1015 struct sbefifo *sbefifo = dev_get_drvdata(dev);
1016
1017 dev_dbg(dev, "Removing sbefifo device...\n");
1018
1019 mutex_lock(&sbefifo->lock);
1020 sbefifo->dead = true;
1021 mutex_unlock(&sbefifo->lock);
1022
1023 cdev_device_del(&sbefifo->cdev, &sbefifo->dev);
1024 fsi_free_minor(sbefifo->dev.devt);
1025 device_for_each_child(dev, NULL, sbefifo_unregister_child);
1026 put_device(&sbefifo->dev);
1027
1028 return 0;
1029}
1030
1031static struct fsi_device_id sbefifo_ids[] = {
1032 {
1033 .engine_type = FSI_ENGID_SBE,
1034 .version = FSI_VERSION_ANY,
1035 },
1036 { 0 }
1037};
1038
1039static struct fsi_driver sbefifo_drv = {
1040 .id_table = sbefifo_ids,
1041 .drv = {
1042 .name = DEVICE_NAME,
1043 .bus = &fsi_bus_type,
1044 .probe = sbefifo_probe,
1045 .remove = sbefifo_remove,
1046 }
1047};
1048
1049static int sbefifo_init(void)
1050{
1051 return fsi_driver_register(&sbefifo_drv);
1052}
1053
1054static void sbefifo_exit(void)
1055{
1056 fsi_driver_unregister(&sbefifo_drv);
1057}
1058
1059module_init(sbefifo_init);
1060module_exit(sbefifo_exit);
1061MODULE_LICENSE("GPL");
1062MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>");
1063MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>");
1064MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
1065MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
1066MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine");
1067