fsi: occ: Use a large buffer for responses
[linux-2.6-block.git] / drivers / fsi / fsi-occ.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/device.h>
4 #include <linux/err.h>
5 #include <linux/errno.h>
6 #include <linux/fs.h>
7 #include <linux/fsi-sbefifo.h>
8 #include <linux/gfp.h>
9 #include <linux/idr.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/miscdevice.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/fsi-occ.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <asm/unaligned.h>
24
25 #define OCC_SRAM_BYTES          4096
26 #define OCC_CMD_DATA_BYTES      4090
27 #define OCC_RESP_DATA_BYTES     4089
28
29 #define OCC_P9_SRAM_CMD_ADDR    0xFFFBE000
30 #define OCC_P9_SRAM_RSP_ADDR    0xFFFBF000
31
32 #define OCC_P10_SRAM_CMD_ADDR   0xFFFFD000
33 #define OCC_P10_SRAM_RSP_ADDR   0xFFFFE000
34
35 #define OCC_P10_SRAM_MODE       0x58    /* Normal mode, OCB channel 2 */
36
37 #define OCC_TIMEOUT_MS          1000
38 #define OCC_CMD_IN_PRG_WAIT_MS  50
39
40 enum versions { occ_p9, occ_p10 };
41
42 struct occ {
43         struct device *dev;
44         struct device *sbefifo;
45         char name[32];
46         int idx;
47         u8 sequence_number;
48         void *buffer;
49         enum versions version;
50         struct miscdevice mdev;
51         struct mutex occ_lock;
52 };
53
54 #define to_occ(x)       container_of((x), struct occ, mdev)
55
56 struct occ_response {
57         u8 seq_no;
58         u8 cmd_type;
59         u8 return_status;
60         __be16 data_length;
61         u8 data[OCC_RESP_DATA_BYTES + 2];       /* two bytes checksum */
62 } __packed;
63
64 struct occ_client {
65         struct occ *occ;
66         struct mutex lock;
67         size_t data_size;
68         size_t read_offset;
69         u8 *buffer;
70 };
71
72 #define to_client(x)    container_of((x), struct occ_client, xfr)
73
74 static DEFINE_IDA(occ_ida);
75
76 static int occ_open(struct inode *inode, struct file *file)
77 {
78         struct occ_client *client = kzalloc(sizeof(*client), GFP_KERNEL);
79         struct miscdevice *mdev = file->private_data;
80         struct occ *occ = to_occ(mdev);
81
82         if (!client)
83                 return -ENOMEM;
84
85         client->buffer = (u8 *)__get_free_page(GFP_KERNEL);
86         if (!client->buffer) {
87                 kfree(client);
88                 return -ENOMEM;
89         }
90
91         client->occ = occ;
92         mutex_init(&client->lock);
93         file->private_data = client;
94
95         /* We allocate a 1-page buffer, make sure it all fits */
96         BUILD_BUG_ON((OCC_CMD_DATA_BYTES + 3) > PAGE_SIZE);
97         BUILD_BUG_ON((OCC_RESP_DATA_BYTES + 7) > PAGE_SIZE);
98
99         return 0;
100 }
101
102 static ssize_t occ_read(struct file *file, char __user *buf, size_t len,
103                         loff_t *offset)
104 {
105         struct occ_client *client = file->private_data;
106         ssize_t rc = 0;
107
108         if (!client)
109                 return -ENODEV;
110
111         if (len > OCC_SRAM_BYTES)
112                 return -EINVAL;
113
114         mutex_lock(&client->lock);
115
116         /* This should not be possible ... */
117         if (WARN_ON_ONCE(client->read_offset > client->data_size)) {
118                 rc = -EIO;
119                 goto done;
120         }
121
122         /* Grab how much data we have to read */
123         rc = min(len, client->data_size - client->read_offset);
124         if (copy_to_user(buf, client->buffer + client->read_offset, rc))
125                 rc = -EFAULT;
126         else
127                 client->read_offset += rc;
128
129  done:
130         mutex_unlock(&client->lock);
131
132         return rc;
133 }
134
135 static ssize_t occ_write(struct file *file, const char __user *buf,
136                          size_t len, loff_t *offset)
137 {
138         struct occ_client *client = file->private_data;
139         size_t rlen, data_length;
140         ssize_t rc;
141         u8 *cmd;
142
143         if (!client)
144                 return -ENODEV;
145
146         if (len > (OCC_CMD_DATA_BYTES + 3) || len < 3)
147                 return -EINVAL;
148
149         mutex_lock(&client->lock);
150
151         /* Construct the command */
152         cmd = client->buffer;
153
154         /*
155          * Copy the user command (assume user data follows the occ command
156          * format)
157          * byte 0: command type
158          * bytes 1-2: data length (msb first)
159          * bytes 3-n: data
160          */
161         if (copy_from_user(&cmd[1], buf, len)) {
162                 rc = -EFAULT;
163                 goto done;
164         }
165
166         /* Extract data length */
167         data_length = (cmd[2] << 8) + cmd[3];
168         if (data_length > OCC_CMD_DATA_BYTES) {
169                 rc = -EINVAL;
170                 goto done;
171         }
172
173         /* Submit command; 4 bytes before the data and 2 bytes after */
174         rlen = PAGE_SIZE;
175         rc = fsi_occ_submit(client->occ->dev, cmd, data_length + 6, cmd,
176                             &rlen);
177         if (rc)
178                 goto done;
179
180         /* Set read tracking data */
181         client->data_size = rlen;
182         client->read_offset = 0;
183
184         /* Done */
185         rc = len;
186
187  done:
188         mutex_unlock(&client->lock);
189
190         return rc;
191 }
192
193 static int occ_release(struct inode *inode, struct file *file)
194 {
195         struct occ_client *client = file->private_data;
196
197         free_page((unsigned long)client->buffer);
198         kfree(client);
199
200         return 0;
201 }
202
203 static const struct file_operations occ_fops = {
204         .owner = THIS_MODULE,
205         .open = occ_open,
206         .read = occ_read,
207         .write = occ_write,
208         .release = occ_release,
209 };
210
211 static int occ_verify_checksum(struct occ *occ, struct occ_response *resp,
212                                u16 data_length)
213 {
214         /* Fetch the two bytes after the data for the checksum. */
215         u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);
216         u16 checksum;
217         u16 i;
218
219         checksum = resp->seq_no;
220         checksum += resp->cmd_type;
221         checksum += resp->return_status;
222         checksum += (data_length >> 8) + (data_length & 0xFF);
223
224         for (i = 0; i < data_length; ++i)
225                 checksum += resp->data[i];
226
227         if (checksum != checksum_resp) {
228                 dev_err(occ->dev, "Bad checksum: %04x!=%04x\n", checksum,
229                         checksum_resp);
230                 return -EBADMSG;
231         }
232
233         return 0;
234 }
235
236 static int occ_getsram(struct occ *occ, u32 offset, void *data, ssize_t len)
237 {
238         u32 data_len = ((len + 7) / 8) * 8;     /* must be multiples of 8 B */
239         size_t cmd_len, resp_data_len;
240         size_t resp_len = OCC_MAX_RESP_WORDS;
241         __be32 *resp = occ->buffer;
242         __be32 cmd[6];
243         int idx = 0, rc;
244
245         /*
246          * Magic sequence to do SBE getsram command. SBE will fetch data from
247          * specified SRAM address.
248          */
249         switch (occ->version) {
250         default:
251         case occ_p9:
252                 cmd_len = 5;
253                 cmd[2] = cpu_to_be32(1);        /* Normal mode */
254                 cmd[3] = cpu_to_be32(OCC_P9_SRAM_RSP_ADDR + offset);
255                 break;
256         case occ_p10:
257                 idx = 1;
258                 cmd_len = 6;
259                 cmd[2] = cpu_to_be32(OCC_P10_SRAM_MODE);
260                 cmd[3] = 0;
261                 cmd[4] = cpu_to_be32(OCC_P10_SRAM_RSP_ADDR + offset);
262                 break;
263         }
264
265         cmd[0] = cpu_to_be32(cmd_len);
266         cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_OCC_SRAM);
267         cmd[4 + idx] = cpu_to_be32(data_len);
268
269         rc = sbefifo_submit(occ->sbefifo, cmd, cmd_len, resp, &resp_len);
270         if (rc)
271                 return rc;
272
273         rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_GET_OCC_SRAM,
274                                   resp, resp_len, &resp_len);
275         if (rc > 0) {
276                 dev_err(occ->dev, "SRAM read returned failure status: %08x\n",
277                         rc);
278                 return -EBADMSG;
279         } else if (rc) {
280                 return rc;
281         }
282
283         resp_data_len = be32_to_cpu(resp[resp_len - 1]);
284         if (resp_data_len != data_len) {
285                 dev_err(occ->dev, "SRAM read expected %d bytes got %zd\n",
286                         data_len, resp_data_len);
287                 rc = -EBADMSG;
288         } else {
289                 memcpy(data, resp, len);
290         }
291
292         return rc;
293 }
294
295 static int occ_putsram(struct occ *occ, const void *data, ssize_t len,
296                        u8 seq_no, u16 checksum)
297 {
298         u32 data_len = ((len + 7) / 8) * 8;     /* must be multiples of 8 B */
299         size_t cmd_len, resp_data_len;
300         size_t resp_len = OCC_MAX_RESP_WORDS;
301         __be32 *buf = occ->buffer;
302         u8 *byte_buf;
303         int idx = 0, rc;
304
305         cmd_len = (occ->version == occ_p10) ? 6 : 5;
306         cmd_len += data_len >> 2;
307
308         /*
309          * Magic sequence to do SBE putsram command. SBE will transfer
310          * data to specified SRAM address.
311          */
312         buf[0] = cpu_to_be32(cmd_len);
313         buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
314
315         switch (occ->version) {
316         default:
317         case occ_p9:
318                 buf[2] = cpu_to_be32(1);        /* Normal mode */
319                 buf[3] = cpu_to_be32(OCC_P9_SRAM_CMD_ADDR);
320                 break;
321         case occ_p10:
322                 idx = 1;
323                 buf[2] = cpu_to_be32(OCC_P10_SRAM_MODE);
324                 buf[3] = 0;
325                 buf[4] = cpu_to_be32(OCC_P10_SRAM_CMD_ADDR);
326                 break;
327         }
328
329         buf[4 + idx] = cpu_to_be32(data_len);
330         memcpy(&buf[5 + idx], data, len);
331
332         byte_buf = (u8 *)&buf[5 + idx];
333         /*
334          * Overwrite the first byte with our sequence number and the last two
335          * bytes with the checksum.
336          */
337         byte_buf[0] = seq_no;
338         byte_buf[len - 2] = checksum >> 8;
339         byte_buf[len - 1] = checksum & 0xff;
340
341         rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
342         if (rc)
343                 return rc;
344
345         rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
346                                   buf, resp_len, &resp_len);
347         if (rc > 0) {
348                 dev_err(occ->dev, "SRAM write returned failure status: %08x\n",
349                         rc);
350                 return -EBADMSG;
351         } else if (rc) {
352                 return rc;
353         }
354
355         if (resp_len != 1) {
356                 dev_err(occ->dev, "SRAM write response length invalid: %zd\n",
357                         resp_len);
358                 rc = -EBADMSG;
359         } else {
360                 resp_data_len = be32_to_cpu(buf[0]);
361                 if (resp_data_len != data_len) {
362                         dev_err(occ->dev,
363                                 "SRAM write expected %d bytes got %zd\n",
364                                 data_len, resp_data_len);
365                         rc = -EBADMSG;
366                 }
367         }
368
369         return rc;
370 }
371
372 static int occ_trigger_attn(struct occ *occ)
373 {
374         __be32 *buf = occ->buffer;
375         size_t cmd_len, resp_data_len;
376         size_t resp_len = OCC_MAX_RESP_WORDS;
377         int idx = 0, rc;
378
379         switch (occ->version) {
380         default:
381         case occ_p9:
382                 cmd_len = 7;
383                 buf[2] = cpu_to_be32(3); /* Circular mode */
384                 buf[3] = 0;
385                 break;
386         case occ_p10:
387                 idx = 1;
388                 cmd_len = 8;
389                 buf[2] = cpu_to_be32(0xd0); /* Circular mode, OCB Channel 1 */
390                 buf[3] = 0;
391                 buf[4] = 0;
392                 break;
393         }
394
395         buf[0] = cpu_to_be32(cmd_len);          /* Chip-op length in words */
396         buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
397         buf[4 + idx] = cpu_to_be32(8);          /* Data length in bytes */
398         buf[5 + idx] = cpu_to_be32(0x20010000); /* Trigger OCC attention */
399         buf[6 + idx] = 0;
400
401         rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
402         if (rc)
403                 return rc;
404
405         rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
406                                   buf, resp_len, &resp_len);
407         if (rc > 0) {
408                 dev_err(occ->dev, "SRAM attn returned failure status: %08x\n",
409                         rc);
410                 return -EBADMSG;
411         } else if (rc) {
412                 return rc;
413         }
414
415         if (resp_len != 1) {
416                 dev_err(occ->dev, "SRAM attn response length invalid: %zd\n",
417                         resp_len);
418                 rc = -EBADMSG;
419         } else {
420                 resp_data_len = be32_to_cpu(buf[0]);
421                 if (resp_data_len != 8) {
422                         dev_err(occ->dev,
423                                 "SRAM attn expected 8 bytes got %zd\n",
424                                 resp_data_len);
425                         rc = -EBADMSG;
426                 }
427         }
428
429         return rc;
430 }
431
432 int fsi_occ_submit(struct device *dev, const void *request, size_t req_len,
433                    void *response, size_t *resp_len)
434 {
435         const unsigned long timeout = msecs_to_jiffies(OCC_TIMEOUT_MS);
436         const unsigned long wait_time =
437                 msecs_to_jiffies(OCC_CMD_IN_PRG_WAIT_MS);
438         struct occ *occ = dev_get_drvdata(dev);
439         struct occ_response *resp = response;
440         u8 seq_no;
441         u16 checksum = 0;
442         u16 resp_data_length;
443         const u8 *byte_request = (const u8 *)request;
444         unsigned long start;
445         int rc;
446         size_t i;
447
448         if (!occ)
449                 return -ENODEV;
450
451         if (*resp_len < 7) {
452                 dev_dbg(dev, "Bad resplen %zd\n", *resp_len);
453                 return -EINVAL;
454         }
455
456         /* Checksum the request, ignoring first byte (sequence number). */
457         for (i = 1; i < req_len - 2; ++i)
458                 checksum += byte_request[i];
459
460         mutex_lock(&occ->occ_lock);
461
462         /*
463          * Get a sequence number and update the counter. Avoid a sequence
464          * number of 0 which would pass the response check below even if the
465          * OCC response is uninitialized. Any sequence number the user is
466          * trying to send is overwritten since this function is the only common
467          * interface to the OCC and therefore the only place we can guarantee
468          * unique sequence numbers.
469          */
470         seq_no = occ->sequence_number++;
471         if (!occ->sequence_number)
472                 occ->sequence_number = 1;
473         checksum += seq_no;
474
475         rc = occ_putsram(occ, request, req_len, seq_no, checksum);
476         if (rc)
477                 goto done;
478
479         rc = occ_trigger_attn(occ);
480         if (rc)
481                 goto done;
482
483         /* Read occ response header */
484         start = jiffies;
485         do {
486                 rc = occ_getsram(occ, 0, resp, 8);
487                 if (rc)
488                         goto done;
489
490                 if (resp->return_status == OCC_RESP_CMD_IN_PRG ||
491                     resp->return_status == OCC_RESP_CRIT_INIT ||
492                     resp->seq_no != seq_no) {
493                         rc = -ETIMEDOUT;
494
495                         if (time_after(jiffies, start + timeout)) {
496                                 dev_err(occ->dev, "resp timeout status=%02x "
497                                         "resp seq_no=%d our seq_no=%d\n",
498                                         resp->return_status, resp->seq_no,
499                                         seq_no);
500                                 goto done;
501                         }
502
503                         set_current_state(TASK_UNINTERRUPTIBLE);
504                         schedule_timeout(wait_time);
505                 }
506         } while (rc);
507
508         /* Extract size of response data */
509         resp_data_length = get_unaligned_be16(&resp->data_length);
510
511         /* Message size is data length + 5 bytes header + 2 bytes checksum */
512         if ((resp_data_length + 7) > *resp_len) {
513                 rc = -EMSGSIZE;
514                 goto done;
515         }
516
517         dev_dbg(dev, "resp_status=%02x resp_data_len=%d\n",
518                 resp->return_status, resp_data_length);
519
520         /* Grab the rest */
521         if (resp_data_length > 1) {
522                 /* already got 3 bytes resp, also need 2 bytes checksum */
523                 rc = occ_getsram(occ, 8, &resp->data[3], resp_data_length - 1);
524                 if (rc)
525                         goto done;
526         }
527
528         *resp_len = resp_data_length + 7;
529         rc = occ_verify_checksum(occ, resp, resp_data_length);
530
531  done:
532         mutex_unlock(&occ->occ_lock);
533
534         return rc;
535 }
536 EXPORT_SYMBOL_GPL(fsi_occ_submit);
537
538 static int occ_unregister_child(struct device *dev, void *data)
539 {
540         struct platform_device *hwmon_dev = to_platform_device(dev);
541
542         platform_device_unregister(hwmon_dev);
543
544         return 0;
545 }
546
547 static int occ_probe(struct platform_device *pdev)
548 {
549         int rc;
550         u32 reg;
551         struct occ *occ;
552         struct platform_device *hwmon_dev;
553         struct device *dev = &pdev->dev;
554         struct platform_device_info hwmon_dev_info = {
555                 .parent = dev,
556                 .name = "occ-hwmon",
557         };
558
559         occ = devm_kzalloc(dev, sizeof(*occ), GFP_KERNEL);
560         if (!occ)
561                 return -ENOMEM;
562
563         /* SBE words are always four bytes */
564         occ->buffer = kvmalloc(OCC_MAX_RESP_WORDS * 4, GFP_KERNEL);
565         if (!occ->buffer)
566                 return -ENOMEM;
567
568         occ->version = (uintptr_t)of_device_get_match_data(dev);
569         occ->dev = dev;
570         occ->sbefifo = dev->parent;
571         occ->sequence_number = 1;
572         mutex_init(&occ->occ_lock);
573
574         if (dev->of_node) {
575                 rc = of_property_read_u32(dev->of_node, "reg", &reg);
576                 if (!rc) {
577                         /* make sure we don't have a duplicate from dts */
578                         occ->idx = ida_simple_get(&occ_ida, reg, reg + 1,
579                                                   GFP_KERNEL);
580                         if (occ->idx < 0)
581                                 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
582                                                           GFP_KERNEL);
583                 } else {
584                         occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
585                                                   GFP_KERNEL);
586                 }
587         } else {
588                 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX, GFP_KERNEL);
589         }
590
591         platform_set_drvdata(pdev, occ);
592
593         snprintf(occ->name, sizeof(occ->name), "occ%d", occ->idx);
594         occ->mdev.fops = &occ_fops;
595         occ->mdev.minor = MISC_DYNAMIC_MINOR;
596         occ->mdev.name = occ->name;
597         occ->mdev.parent = dev;
598
599         rc = misc_register(&occ->mdev);
600         if (rc) {
601                 dev_err(dev, "failed to register miscdevice: %d\n", rc);
602                 ida_simple_remove(&occ_ida, occ->idx);
603                 kvfree(occ->buffer);
604                 return rc;
605         }
606
607         hwmon_dev_info.id = occ->idx;
608         hwmon_dev = platform_device_register_full(&hwmon_dev_info);
609         if (IS_ERR(hwmon_dev))
610                 dev_warn(dev, "failed to create hwmon device\n");
611
612         return 0;
613 }
614
615 static int occ_remove(struct platform_device *pdev)
616 {
617         struct occ *occ = platform_get_drvdata(pdev);
618
619         kvfree(occ->buffer);
620
621         misc_deregister(&occ->mdev);
622
623         device_for_each_child(&pdev->dev, NULL, occ_unregister_child);
624
625         ida_simple_remove(&occ_ida, occ->idx);
626
627         return 0;
628 }
629
630 static const struct of_device_id occ_match[] = {
631         {
632                 .compatible = "ibm,p9-occ",
633                 .data = (void *)occ_p9
634         },
635         {
636                 .compatible = "ibm,p10-occ",
637                 .data = (void *)occ_p10
638         },
639         { },
640 };
641 MODULE_DEVICE_TABLE(of, occ_match);
642
643 static struct platform_driver occ_driver = {
644         .driver = {
645                 .name = "occ",
646                 .of_match_table = occ_match,
647         },
648         .probe  = occ_probe,
649         .remove = occ_remove,
650 };
651
652 static int occ_init(void)
653 {
654         return platform_driver_register(&occ_driver);
655 }
656
657 static void occ_exit(void)
658 {
659         platform_driver_unregister(&occ_driver);
660
661         ida_destroy(&occ_ida);
662 }
663
664 module_init(occ_init);
665 module_exit(occ_exit);
666
667 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
668 MODULE_DESCRIPTION("BMC P9 OCC driver");
669 MODULE_LICENSE("GPL");