Edward A. James | 1c37e09 | 2017-05-15 12:28:44 -0500 | [diff] [blame] | 1 | From patchwork Thu May 11 02:52:53 2017 |
| 2 | Content-Type: text/plain; charset="utf-8" |
| 3 | MIME-Version: 1.0 |
| 4 | Content-Transfer-Encoding: 7bit |
| 5 | Subject: [linux,dev-4.10] drivers: fsi: Add FSI SBEFIFO driver |
| 6 | From: eajames@linux.vnet.ibm.com |
| 7 | X-Patchwork-Id: 760920 |
| 8 | Message-Id: <1494471173-6077-1-git-send-email-eajames@linux.vnet.ibm.com> |
| 9 | To: openbmc@lists.ozlabs.org |
| 10 | Cc: "Edward A. James" <eajames@us.ibm.com>, bradleyb@fuzziesquirrel.com |
| 11 | Date: Wed, 10 May 2017 21:52:53 -0500 |
| 12 | |
| 13 | From: "Edward A. James" <eajames@us.ibm.com> |
| 14 | |
| 15 | IBM POWER9 processors contain some embedded hardware and software bits |
| 16 | collectively referred to as the self boot engine (SBE). One role of |
| 17 | the SBE is to act as a proxy that provides access to the registers of |
| 18 | the POWER chip from other (embedded) systems. |
| 19 | |
| 20 | The POWER9 chip contains a hardware frontend for communicating with |
| 21 | the SBE from remote systems called the SBEFIFO. The SBEFIFO logic |
| 22 | is contained within an FSI CFAM (see Documentation/fsi) and as such |
| 23 | the driver implements an FSI bus device. |
| 24 | |
| 25 | The SBE expects to communicate using a defined wire protocol; however, |
| 26 | the driver knows nothing of the protocol and only provides raw access |
| 27 | to the fifo device to userspace applications wishing to communicate with |
| 28 | the SBE using the wire protocol. |
| 29 | |
| 30 | The SBEFIFO consists of two hardware fifos. The upstream fifo is used |
| 31 | by the driver to transfer data to the SBE on the POWER chip, from the |
| 32 | system hosting the driver. The downstream fifo is used by the driver to |
| 33 | transfer data from the SBE on the power chip to the system hosting the |
| 34 | driver. |
| 35 | |
| 36 | Signed-off-by: Edward A. James <eajames@us.ibm.com> |
| 37 | Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com> |
| 38 | --- |
| 39 | drivers/fsi/Kconfig | 5 + |
| 40 | drivers/fsi/Makefile | 1 + |
| 41 | drivers/fsi/fsi-sbefifo.c | 824 ++++++++++++++++++++++++++++++++++++++++++++++ |
| 42 | 3 files changed, 830 insertions(+) |
| 43 | create mode 100644 drivers/fsi/fsi-sbefifo.c |
| 44 | |
| 45 | diff --git a/drivers/fsi/Kconfig b/drivers/fsi/Kconfig |
| 46 | index fc031ac..39527fa 100644 |
| 47 | --- a/drivers/fsi/Kconfig |
| 48 | +++ b/drivers/fsi/Kconfig |
| 49 | @@ -31,6 +31,11 @@ config FSI_SCOM |
| 50 | ---help--- |
| 51 | This option enables an FSI based SCOM device driver. |
| 52 | |
| 53 | +config FSI_SBEFIFO |
| 54 | + tristate "SBEFIFO FSI client device driver" |
| 55 | + ---help--- |
| 56 | + This option enables an FSI based SBEFIFO device driver. |
| 57 | + |
| 58 | endif |
| 59 | |
| 60 | endmenu |
| 61 | diff --git a/drivers/fsi/Makefile b/drivers/fsi/Makefile |
| 62 | index 65eb99d..851182e 100644 |
| 63 | --- a/drivers/fsi/Makefile |
| 64 | +++ b/drivers/fsi/Makefile |
| 65 | @@ -3,3 +3,4 @@ obj-$(CONFIG_FSI) += fsi-core.o |
| 66 | obj-$(CONFIG_FSI_MASTER_HUB) += fsi-master-hub.o |
| 67 | obj-$(CONFIG_FSI_MASTER_GPIO) += fsi-master-gpio.o |
| 68 | obj-$(CONFIG_FSI_SCOM) += fsi-scom.o |
| 69 | +obj-$(CONFIG_FSI_SBEFIFO) += fsi-sbefifo.o |
| 70 | diff --git a/drivers/fsi/fsi-sbefifo.c b/drivers/fsi/fsi-sbefifo.c |
| 71 | new file mode 100644 |
| 72 | index 0000000..b49aec2 |
| 73 | --- /dev/null |
| 74 | +++ b/drivers/fsi/fsi-sbefifo.c |
| 75 | @@ -0,0 +1,824 @@ |
| 76 | +/* |
| 77 | + * Copyright (C) IBM Corporation 2017 |
| 78 | + * |
| 79 | + * This program is free software; you can redistribute it and/or modify |
| 80 | + * it under the terms of the GNU General Public License version 2 as |
| 81 | + * published by the Free Software Foundation. |
| 82 | + * |
| 83 | + * This program is distributed in the hope that it will be useful, |
| 84 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 85 | + * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 86 | + * GNU General Public License for more details. |
| 87 | + */ |
| 88 | + |
| 89 | +#include <linux/delay.h> |
| 90 | +#include <linux/errno.h> |
| 91 | +#include <linux/idr.h> |
| 92 | +#include <linux/fsi.h> |
| 93 | +#include <linux/list.h> |
| 94 | +#include <linux/miscdevice.h> |
| 95 | +#include <linux/module.h> |
| 96 | +#include <linux/poll.h> |
| 97 | +#include <linux/sched.h> |
| 98 | +#include <linux/slab.h> |
| 99 | +#include <linux/timer.h> |
| 100 | +#include <linux/uaccess.h> |
| 101 | + |
| 102 | +/* |
| 103 | + * The SBEFIFO is a pipe-like FSI device for communicating with |
| 104 | + * the self boot engine on POWER processors. |
| 105 | + */ |
| 106 | + |
| 107 | +#define DEVICE_NAME "sbefifo" |
| 108 | +#define FSI_ENGID_SBE 0x22 |
| 109 | +#define SBEFIFO_BUF_CNT 32 |
| 110 | + |
| 111 | +#define SBEFIFO_UP 0x00 /* Up register offset */ |
| 112 | +#define SBEFIFO_DWN 0x40 /* Down register offset */ |
| 113 | + |
| 114 | +#define SBEFIFO_STS 0x04 |
| 115 | +#define SBEFIFO_EMPTY BIT(20) |
| 116 | +#define SBEFIFO_EOT_RAISE 0x08 |
| 117 | +#define SBEFIFO_EOT_MAGIC 0xffffffff |
| 118 | +#define SBEFIFO_EOT_ACK 0x14 |
| 119 | + |
| 120 | +struct sbefifo { |
| 121 | + struct timer_list poll_timer; |
| 122 | + struct fsi_device *fsi_dev; |
| 123 | + struct miscdevice mdev; |
| 124 | + wait_queue_head_t wait; |
| 125 | + struct list_head link; |
| 126 | + struct list_head xfrs; |
| 127 | + struct kref kref; |
| 128 | + spinlock_t lock; |
| 129 | + char name[32]; |
| 130 | + int idx; |
| 131 | + int rc; |
| 132 | +}; |
| 133 | + |
| 134 | +struct sbefifo_buf { |
| 135 | + u32 buf[SBEFIFO_BUF_CNT]; |
| 136 | + unsigned long flags; |
| 137 | +#define SBEFIFO_BUF_FULL 1 |
| 138 | + u32 *rpos; |
| 139 | + u32 *wpos; |
| 140 | +}; |
| 141 | + |
| 142 | +struct sbefifo_xfr { |
| 143 | + struct sbefifo_buf *rbuf; |
| 144 | + struct sbefifo_buf *wbuf; |
| 145 | + struct list_head client; |
| 146 | + struct list_head xfrs; |
| 147 | + unsigned long flags; |
| 148 | +#define SBEFIFO_XFR_WRITE_DONE 1 |
| 149 | +#define SBEFIFO_XFR_RESP_PENDING 2 |
| 150 | +#define SBEFIFO_XFR_COMPLETE 3 |
| 151 | +#define SBEFIFO_XFR_CANCEL 4 |
| 152 | +}; |
| 153 | + |
| 154 | +struct sbefifo_client { |
| 155 | + struct sbefifo_buf rbuf; |
| 156 | + struct sbefifo_buf wbuf; |
| 157 | + struct list_head xfrs; |
| 158 | + struct sbefifo *dev; |
| 159 | + struct kref kref; |
| 160 | +}; |
| 161 | + |
| 162 | +static struct list_head sbefifo_fifos; |
| 163 | + |
| 164 | +static DEFINE_IDA(sbefifo_ida); |
| 165 | + |
| 166 | +static int sbefifo_inw(struct sbefifo *sbefifo, int reg, u32 *word) |
| 167 | +{ |
| 168 | + int rc; |
| 169 | + u32 raw_word; |
| 170 | + |
| 171 | + rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word, |
| 172 | + sizeof(raw_word)); |
| 173 | + if (rc) |
| 174 | + return rc; |
| 175 | + |
| 176 | + *word = be32_to_cpu(raw_word); |
| 177 | + return 0; |
| 178 | +} |
| 179 | + |
| 180 | +static int sbefifo_outw(struct sbefifo *sbefifo, int reg, u32 word) |
| 181 | +{ |
| 182 | + u32 raw_word = cpu_to_be32(word); |
| 183 | + |
| 184 | + return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word, |
| 185 | + sizeof(raw_word)); |
| 186 | +} |
| 187 | + |
| 188 | +static int sbefifo_readw(struct sbefifo *sbefifo, u32 *word) |
| 189 | +{ |
| 190 | + return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DWN, word, |
| 191 | + sizeof(*word)); |
| 192 | +} |
| 193 | + |
| 194 | +static int sbefifo_writew(struct sbefifo *sbefifo, u32 word) |
| 195 | +{ |
| 196 | + return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word, |
| 197 | + sizeof(word)); |
| 198 | +} |
| 199 | + |
| 200 | +static int sbefifo_ack_eot(struct sbefifo *sbefifo) |
| 201 | +{ |
| 202 | + u32 discard; |
| 203 | + int ret; |
| 204 | + |
| 205 | + /* Discard the EOT word. */ |
| 206 | + ret = sbefifo_readw(sbefifo, &discard); |
| 207 | + if (ret) |
| 208 | + return ret; |
| 209 | + |
| 210 | + return sbefifo_outw(sbefifo, SBEFIFO_DWN | SBEFIFO_EOT_ACK, |
| 211 | + SBEFIFO_EOT_MAGIC); |
| 212 | +} |
| 213 | + |
| 214 | +static size_t sbefifo_dev_nwreadable(u32 sts) |
| 215 | +{ |
| 216 | + static const u32 FIFO_NTRY_CNT_MSK = 0x000f0000; |
| 217 | + static const unsigned int FIFO_NTRY_CNT_SHIFT = 16; |
| 218 | + |
| 219 | + return (sts & FIFO_NTRY_CNT_MSK) >> FIFO_NTRY_CNT_SHIFT; |
| 220 | +} |
| 221 | + |
| 222 | +static size_t sbefifo_dev_nwwriteable(u32 sts) |
| 223 | +{ |
| 224 | + static const size_t FIFO_DEPTH = 8; |
| 225 | + |
| 226 | + return FIFO_DEPTH - sbefifo_dev_nwreadable(sts); |
| 227 | +} |
| 228 | + |
| 229 | +static void sbefifo_buf_init(struct sbefifo_buf *buf) |
| 230 | +{ |
| 231 | + WRITE_ONCE(buf->rpos, buf->buf); |
| 232 | + WRITE_ONCE(buf->wpos, buf->buf); |
| 233 | +} |
| 234 | + |
| 235 | +static size_t sbefifo_buf_nbreadable(struct sbefifo_buf *buf) |
| 236 | +{ |
| 237 | + size_t n; |
| 238 | + u32 *rpos = READ_ONCE(buf->rpos); |
| 239 | + u32 *wpos = READ_ONCE(buf->wpos); |
| 240 | + |
| 241 | + if (test_bit(SBEFIFO_BUF_FULL, &buf->flags)) |
| 242 | + n = SBEFIFO_BUF_CNT; |
| 243 | + else if (rpos <= wpos) |
| 244 | + n = wpos - rpos; |
| 245 | + else |
| 246 | + n = (buf->buf + SBEFIFO_BUF_CNT) - rpos; |
| 247 | + |
| 248 | + return n << 2; |
| 249 | +} |
| 250 | + |
| 251 | +static size_t sbefifo_buf_nbwriteable(struct sbefifo_buf *buf) |
| 252 | +{ |
| 253 | + size_t n; |
| 254 | + u32 *rpos = READ_ONCE(buf->rpos); |
| 255 | + u32 *wpos = READ_ONCE(buf->wpos); |
| 256 | + |
| 257 | + if (test_bit(SBEFIFO_BUF_FULL, &buf->flags)) |
| 258 | + n = 0; |
| 259 | + else if (wpos < rpos) |
| 260 | + n = rpos - wpos; |
| 261 | + else |
| 262 | + n = (buf->buf + SBEFIFO_BUF_CNT) - wpos; |
| 263 | + |
| 264 | + return n << 2; |
| 265 | +} |
| 266 | + |
| 267 | +/* |
| 268 | + * Update pointers and flags after doing a buffer read. Return true if the |
| 269 | + * buffer is now empty; |
| 270 | + */ |
| 271 | +static bool sbefifo_buf_readnb(struct sbefifo_buf *buf, size_t n) |
| 272 | +{ |
| 273 | + u32 *rpos = READ_ONCE(buf->rpos); |
| 274 | + u32 *wpos = READ_ONCE(buf->wpos); |
| 275 | + |
| 276 | + if (n) |
| 277 | + clear_bit(SBEFIFO_BUF_FULL, &buf->flags); |
| 278 | + |
| 279 | + rpos += (n >> 2); |
| 280 | + if (rpos == buf->buf + SBEFIFO_BUF_CNT) |
| 281 | + rpos = buf->buf; |
| 282 | + |
| 283 | + WRITE_ONCE(buf->rpos, rpos); |
| 284 | + return rpos == wpos; |
| 285 | +} |
| 286 | + |
| 287 | +/* |
| 288 | + * Update pointers and flags after doing a buffer write. Return true if the |
| 289 | + * buffer is now full. |
| 290 | + */ |
| 291 | +static bool sbefifo_buf_wrotenb(struct sbefifo_buf *buf, size_t n) |
| 292 | +{ |
| 293 | + u32 *rpos = READ_ONCE(buf->rpos); |
| 294 | + u32 *wpos = READ_ONCE(buf->wpos); |
| 295 | + |
| 296 | + wpos += (n >> 2); |
| 297 | + if (wpos == buf->buf + SBEFIFO_BUF_CNT) |
| 298 | + wpos = buf->buf; |
| 299 | + if (wpos == rpos) |
| 300 | + set_bit(SBEFIFO_BUF_FULL, &buf->flags); |
| 301 | + |
| 302 | + WRITE_ONCE(buf->wpos, wpos); |
| 303 | + return rpos == wpos; |
| 304 | +} |
| 305 | + |
| 306 | +static void sbefifo_free(struct kref *kref) |
| 307 | +{ |
| 308 | + struct sbefifo *sbefifo; |
| 309 | + |
| 310 | + sbefifo = container_of(kref, struct sbefifo, kref); |
| 311 | + kfree(sbefifo); |
| 312 | +} |
| 313 | + |
| 314 | +static void sbefifo_get(struct sbefifo *sbefifo) |
| 315 | +{ |
| 316 | + kref_get(&sbefifo->kref); |
| 317 | +} |
| 318 | + |
| 319 | +static void sbefifo_put(struct sbefifo *sbefifo) |
| 320 | +{ |
| 321 | + kref_put(&sbefifo->kref, sbefifo_free); |
| 322 | +} |
| 323 | + |
| 324 | +static struct sbefifo_xfr *sbefifo_enq_xfr(struct sbefifo_client *client) |
| 325 | +{ |
| 326 | + struct sbefifo *sbefifo = client->dev; |
| 327 | + struct sbefifo_xfr *xfr; |
| 328 | + |
| 329 | + xfr = kzalloc(sizeof(*xfr), GFP_KERNEL); |
| 330 | + if (!xfr) |
| 331 | + return NULL; |
| 332 | + |
| 333 | + xfr->rbuf = &client->rbuf; |
| 334 | + xfr->wbuf = &client->wbuf; |
| 335 | + list_add_tail(&xfr->xfrs, &sbefifo->xfrs); |
| 336 | + list_add_tail(&xfr->client, &client->xfrs); |
| 337 | + |
| 338 | + return xfr; |
| 339 | +} |
| 340 | + |
| 341 | +static struct sbefifo_xfr *sbefifo_client_next_xfr( |
| 342 | + struct sbefifo_client *client) |
| 343 | +{ |
| 344 | + if (list_empty(&client->xfrs)) |
| 345 | + return NULL; |
| 346 | + |
| 347 | + return container_of(client->xfrs.next, struct sbefifo_xfr, |
| 348 | + client); |
| 349 | +} |
| 350 | + |
| 351 | +static bool sbefifo_xfr_rsp_pending(struct sbefifo_client *client) |
| 352 | +{ |
| 353 | + struct sbefifo_xfr *xfr; |
| 354 | + |
| 355 | + xfr = sbefifo_client_next_xfr(client); |
| 356 | + if (xfr && test_bit(SBEFIFO_XFR_RESP_PENDING, &xfr->flags)) |
| 357 | + return true; |
| 358 | + |
| 359 | + return false; |
| 360 | +} |
| 361 | + |
| 362 | +static struct sbefifo_client *sbefifo_new_client(struct sbefifo *sbefifo) |
| 363 | +{ |
| 364 | + struct sbefifo_client *client; |
| 365 | + |
| 366 | + client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 367 | + if (!client) |
| 368 | + return NULL; |
| 369 | + |
| 370 | + kref_init(&client->kref); |
| 371 | + client->dev = sbefifo; |
| 372 | + sbefifo_buf_init(&client->rbuf); |
| 373 | + sbefifo_buf_init(&client->wbuf); |
| 374 | + INIT_LIST_HEAD(&client->xfrs); |
| 375 | + |
| 376 | + sbefifo_get(sbefifo); |
| 377 | + |
| 378 | + return client; |
| 379 | +} |
| 380 | + |
| 381 | +static void sbefifo_client_release(struct kref *kref) |
| 382 | +{ |
| 383 | + struct sbefifo_client *client; |
| 384 | + struct sbefifo_xfr *xfr; |
| 385 | + |
| 386 | + client = container_of(kref, struct sbefifo_client, kref); |
| 387 | + list_for_each_entry(xfr, &client->xfrs, client) { |
| 388 | + /* |
| 389 | + * The client left with pending or running xfrs. |
| 390 | + * Cancel them. |
| 391 | + */ |
| 392 | + set_bit(SBEFIFO_XFR_CANCEL, &xfr->flags); |
| 393 | + sbefifo_get(client->dev); |
| 394 | + if (mod_timer(&client->dev->poll_timer, jiffies)) |
| 395 | + sbefifo_put(client->dev); |
| 396 | + } |
| 397 | + |
| 398 | + sbefifo_put(client->dev); |
| 399 | + kfree(client); |
| 400 | +} |
| 401 | + |
| 402 | +static void sbefifo_get_client(struct sbefifo_client *client) |
| 403 | +{ |
| 404 | + kref_get(&client->kref); |
| 405 | +} |
| 406 | + |
| 407 | +static void sbefifo_put_client(struct sbefifo_client *client) |
| 408 | +{ |
| 409 | + kref_put(&client->kref, sbefifo_client_release); |
| 410 | +} |
| 411 | + |
| 412 | +static struct sbefifo_xfr *sbefifo_next_xfr(struct sbefifo *sbefifo) |
| 413 | +{ |
| 414 | + struct sbefifo_xfr *xfr, *tmp; |
| 415 | + |
| 416 | + list_for_each_entry_safe(xfr, tmp, &sbefifo->xfrs, xfrs) { |
| 417 | + if (unlikely(test_bit(SBEFIFO_XFR_CANCEL, &xfr->flags))) { |
| 418 | + /* Discard cancelled transfers. */ |
| 419 | + list_del(&xfr->xfrs); |
| 420 | + kfree(xfr); |
| 421 | + continue; |
| 422 | + } |
| 423 | + return xfr; |
| 424 | + } |
| 425 | + |
| 426 | + return NULL; |
| 427 | +} |
| 428 | + |
| 429 | +static void sbefifo_poll_timer(unsigned long data) |
| 430 | +{ |
| 431 | + static const unsigned long EOT_MASK = 0x000000ff; |
| 432 | + struct sbefifo *sbefifo = (void *)data; |
| 433 | + struct sbefifo_buf *rbuf, *wbuf; |
| 434 | + struct sbefifo_xfr *xfr = NULL; |
| 435 | + struct sbefifo_buf drain; |
| 436 | + size_t devn, bufn; |
| 437 | + int eot = 0; |
| 438 | + int ret = 0; |
| 439 | + u32 sts; |
| 440 | + int i; |
| 441 | + |
| 442 | + spin_lock(&sbefifo->lock); |
| 443 | + xfr = list_first_entry_or_null(&sbefifo->xfrs, struct sbefifo_xfr, |
| 444 | + xfrs); |
| 445 | + if (!xfr) |
| 446 | + goto out_unlock; |
| 447 | + |
| 448 | + rbuf = xfr->rbuf; |
| 449 | + wbuf = xfr->wbuf; |
| 450 | + |
| 451 | + if (unlikely(test_bit(SBEFIFO_XFR_CANCEL, &xfr->flags))) { |
| 452 | + /* The client left. */ |
| 453 | + rbuf = &drain; |
| 454 | + wbuf = &drain; |
| 455 | + sbefifo_buf_init(&drain); |
| 456 | + if (!test_bit(SBEFIFO_XFR_RESP_PENDING, &xfr->flags)) |
| 457 | + set_bit(SBEFIFO_XFR_WRITE_DONE, &xfr->flags); |
| 458 | + } |
| 459 | + |
| 460 | + /* Drain the write buffer. */ |
| 461 | + while ((bufn = sbefifo_buf_nbreadable(wbuf))) { |
| 462 | + ret = sbefifo_inw(sbefifo, SBEFIFO_UP | SBEFIFO_STS, |
| 463 | + &sts); |
| 464 | + if (ret) |
| 465 | + goto out; |
| 466 | + |
| 467 | + devn = sbefifo_dev_nwwriteable(sts); |
| 468 | + if (devn == 0) { |
| 469 | + /* No open slot for write. Reschedule. */ |
| 470 | + sbefifo->poll_timer.expires = jiffies + |
| 471 | + msecs_to_jiffies(500); |
| 472 | + add_timer(&sbefifo->poll_timer); |
| 473 | + goto out_unlock; |
| 474 | + } |
| 475 | + |
| 476 | + devn = min_t(size_t, devn, bufn >> 2); |
| 477 | + for (i = 0; i < devn; i++) { |
| 478 | + ret = sbefifo_writew(sbefifo, *wbuf->rpos); |
| 479 | + if (ret) |
| 480 | + goto out; |
| 481 | + |
| 482 | + sbefifo_buf_readnb(wbuf, 1 << 2); |
| 483 | + } |
| 484 | + } |
| 485 | + |
| 486 | + /* Send EOT if the writer is finished. */ |
| 487 | + if (test_and_clear_bit(SBEFIFO_XFR_WRITE_DONE, &xfr->flags)) { |
| 488 | + ret = sbefifo_outw(sbefifo, |
| 489 | + SBEFIFO_UP | SBEFIFO_EOT_RAISE, |
| 490 | + SBEFIFO_EOT_MAGIC); |
| 491 | + if (ret) |
| 492 | + goto out; |
| 493 | + |
| 494 | + /* Inform reschedules that the writer is finished. */ |
| 495 | + set_bit(SBEFIFO_XFR_RESP_PENDING, &xfr->flags); |
| 496 | + } |
| 497 | + |
| 498 | + /* Nothing left to do if the writer is not finished. */ |
| 499 | + if (!test_bit(SBEFIFO_XFR_RESP_PENDING, &xfr->flags)) |
| 500 | + goto out; |
| 501 | + |
| 502 | + /* Fill the read buffer. */ |
| 503 | + while ((bufn = sbefifo_buf_nbwriteable(rbuf))) { |
| 504 | + ret = sbefifo_inw(sbefifo, SBEFIFO_DWN | SBEFIFO_STS, &sts); |
| 505 | + if (ret) |
| 506 | + goto out; |
| 507 | + |
| 508 | + devn = sbefifo_dev_nwreadable(sts); |
| 509 | + if (devn == 0) { |
| 510 | + /* No data yet. Reschedule. */ |
| 511 | + sbefifo->poll_timer.expires = jiffies + |
| 512 | + msecs_to_jiffies(500); |
| 513 | + add_timer(&sbefifo->poll_timer); |
| 514 | + goto out_unlock; |
| 515 | + } |
| 516 | + |
| 517 | + /* Fill. The EOT word is discarded. */ |
| 518 | + devn = min_t(size_t, devn, bufn >> 2); |
| 519 | + eot = (sts & EOT_MASK) != 0; |
| 520 | + if (eot) |
| 521 | + devn--; |
| 522 | + |
| 523 | + for (i = 0; i < devn; i++) { |
| 524 | + ret = sbefifo_readw(sbefifo, rbuf->wpos); |
| 525 | + if (ret) |
| 526 | + goto out; |
| 527 | + |
| 528 | + if (likely(!test_bit(SBEFIFO_XFR_CANCEL, &xfr->flags))) |
| 529 | + sbefifo_buf_wrotenb(rbuf, 1 << 2); |
| 530 | + } |
| 531 | + |
| 532 | + if (eot) { |
| 533 | + ret = sbefifo_ack_eot(sbefifo); |
| 534 | + if (ret) |
| 535 | + goto out; |
| 536 | + |
| 537 | + set_bit(SBEFIFO_XFR_COMPLETE, &xfr->flags); |
| 538 | + list_del(&xfr->xfrs); |
| 539 | + if (unlikely(test_bit(SBEFIFO_XFR_CANCEL, |
| 540 | + &xfr->flags))) |
| 541 | + kfree(xfr); |
| 542 | + break; |
| 543 | + } |
| 544 | + } |
| 545 | + |
| 546 | +out: |
| 547 | + if (unlikely(ret)) { |
| 548 | + sbefifo->rc = ret; |
| 549 | + dev_err(&sbefifo->fsi_dev->dev, |
| 550 | + "Fatal bus access failure: %d\n", ret); |
| 551 | + list_for_each_entry(xfr, &sbefifo->xfrs, xfrs) |
| 552 | + kfree(xfr); |
| 553 | + INIT_LIST_HEAD(&sbefifo->xfrs); |
| 554 | + |
| 555 | + } else if (eot && sbefifo_next_xfr(sbefifo)) { |
| 556 | + sbefifo_get(sbefifo); |
| 557 | + sbefifo->poll_timer.expires = jiffies; |
| 558 | + add_timer(&sbefifo->poll_timer); |
| 559 | + } |
| 560 | + |
| 561 | + sbefifo_put(sbefifo); |
| 562 | + wake_up(&sbefifo->wait); |
| 563 | + |
| 564 | +out_unlock: |
| 565 | + spin_unlock(&sbefifo->lock); |
| 566 | +} |
| 567 | + |
| 568 | +static int sbefifo_open(struct inode *inode, struct file *file) |
| 569 | +{ |
| 570 | + struct sbefifo *sbefifo = container_of(file->private_data, |
| 571 | + struct sbefifo, mdev); |
| 572 | + struct sbefifo_client *client; |
| 573 | + int ret; |
| 574 | + |
| 575 | + ret = READ_ONCE(sbefifo->rc); |
| 576 | + if (ret) |
| 577 | + return ret; |
| 578 | + |
| 579 | + client = sbefifo_new_client(sbefifo); |
| 580 | + if (!client) |
| 581 | + return -ENOMEM; |
| 582 | + |
| 583 | + file->private_data = client; |
| 584 | + |
| 585 | + return 0; |
| 586 | +} |
| 587 | + |
| 588 | +static unsigned int sbefifo_poll(struct file *file, poll_table *wait) |
| 589 | +{ |
| 590 | + struct sbefifo_client *client = file->private_data; |
| 591 | + struct sbefifo *sbefifo = client->dev; |
| 592 | + unsigned int mask = 0; |
| 593 | + |
| 594 | + poll_wait(file, &sbefifo->wait, wait); |
| 595 | + |
| 596 | + if (READ_ONCE(sbefifo->rc)) |
| 597 | + mask |= POLLERR; |
| 598 | + |
| 599 | + if (sbefifo_buf_nbreadable(&client->rbuf)) |
| 600 | + mask |= POLLIN; |
| 601 | + |
| 602 | + if (sbefifo_buf_nbwriteable(&client->wbuf)) |
| 603 | + mask |= POLLOUT; |
| 604 | + |
| 605 | + return mask; |
| 606 | +} |
| 607 | + |
| 608 | +static ssize_t sbefifo_read(struct file *file, char __user *buf, |
| 609 | + size_t len, loff_t *offset) |
| 610 | +{ |
| 611 | + struct sbefifo_client *client = file->private_data; |
| 612 | + struct sbefifo *sbefifo = client->dev; |
| 613 | + struct sbefifo_xfr *xfr; |
| 614 | + ssize_t ret = 0; |
| 615 | + size_t n; |
| 616 | + |
| 617 | + WARN_ON(*offset); |
| 618 | + |
| 619 | + if (!access_ok(VERIFY_WRITE, buf, len)) |
| 620 | + return -EFAULT; |
| 621 | + |
| 622 | + if ((len >> 2) << 2 != len) |
| 623 | + return -EINVAL; |
| 624 | + |
| 625 | + if ((file->f_flags & O_NONBLOCK) && !sbefifo_xfr_rsp_pending(client)) |
| 626 | + return -EAGAIN; |
| 627 | + |
| 628 | + sbefifo_get_client(client); |
| 629 | + if (wait_event_interruptible(sbefifo->wait, |
| 630 | + (ret = READ_ONCE(sbefifo->rc)) || |
| 631 | + (n = sbefifo_buf_nbreadable( |
| 632 | + &client->rbuf)))) { |
| 633 | + sbefifo_put_client(client); |
| 634 | + return -ERESTARTSYS; |
| 635 | + } |
| 636 | + if (ret) { |
| 637 | + INIT_LIST_HEAD(&client->xfrs); |
| 638 | + sbefifo_put_client(client); |
| 639 | + return ret; |
| 640 | + } |
| 641 | + |
| 642 | + n = min_t(size_t, n, len); |
| 643 | + |
| 644 | + if (copy_to_user(buf, READ_ONCE(client->rbuf.rpos), n)) { |
| 645 | + sbefifo_put_client(client); |
| 646 | + return -EFAULT; |
| 647 | + } |
| 648 | + |
| 649 | + if (sbefifo_buf_readnb(&client->rbuf, n)) { |
| 650 | + xfr = sbefifo_client_next_xfr(client); |
| 651 | + if (!test_bit(SBEFIFO_XFR_COMPLETE, &xfr->flags)) { |
| 652 | + /* |
| 653 | + * Fill the read buffer back up. |
| 654 | + */ |
| 655 | + sbefifo_get(sbefifo); |
| 656 | + if (mod_timer(&client->dev->poll_timer, jiffies)) |
| 657 | + sbefifo_put(sbefifo); |
| 658 | + } else { |
| 659 | + kfree(xfr); |
| 660 | + list_del(&xfr->client); |
| 661 | + wake_up(&sbefifo->wait); |
| 662 | + } |
| 663 | + } |
| 664 | + |
| 665 | + sbefifo_put_client(client); |
| 666 | + |
| 667 | + return n; |
| 668 | +} |
| 669 | + |
| 670 | +static ssize_t sbefifo_write(struct file *file, const char __user *buf, |
| 671 | + size_t len, loff_t *offset) |
| 672 | +{ |
| 673 | + struct sbefifo_client *client = file->private_data; |
| 674 | + struct sbefifo *sbefifo = client->dev; |
| 675 | + struct sbefifo_xfr *xfr; |
| 676 | + ssize_t ret = 0; |
| 677 | + size_t n; |
| 678 | + |
| 679 | + WARN_ON(*offset); |
| 680 | + |
| 681 | + if (!access_ok(VERIFY_READ, buf, len)) |
| 682 | + return -EFAULT; |
| 683 | + |
| 684 | + if ((len >> 2) << 2 != len) |
| 685 | + return -EINVAL; |
| 686 | + |
| 687 | + if (!len) |
| 688 | + return 0; |
| 689 | + |
| 690 | + n = sbefifo_buf_nbwriteable(&client->wbuf); |
| 691 | + |
| 692 | + spin_lock_irq(&sbefifo->lock); |
| 693 | + xfr = sbefifo_next_xfr(sbefifo); |
| 694 | + |
| 695 | + if ((file->f_flags & O_NONBLOCK) && xfr && n < len) { |
| 696 | + spin_unlock_irq(&sbefifo->lock); |
| 697 | + return -EAGAIN; |
| 698 | + } |
| 699 | + |
| 700 | + xfr = sbefifo_enq_xfr(client); |
| 701 | + if (!xfr) { |
| 702 | + spin_unlock_irq(&sbefifo->lock); |
| 703 | + return -ENOMEM; |
| 704 | + } |
| 705 | + spin_unlock_irq(&sbefifo->lock); |
| 706 | + |
| 707 | + sbefifo_get_client(client); |
| 708 | + |
| 709 | + /* |
| 710 | + * Partial writes are not really allowed in that EOT is sent exactly |
| 711 | + * once per write. |
| 712 | + */ |
| 713 | + while (len) { |
| 714 | + if (wait_event_interruptible(sbefifo->wait, |
| 715 | + READ_ONCE(sbefifo->rc) || |
| 716 | + (sbefifo_client_next_xfr(client) == xfr && |
| 717 | + (n = sbefifo_buf_nbwriteable( |
| 718 | + &client->wbuf))))) { |
| 719 | + set_bit(SBEFIFO_XFR_CANCEL, &xfr->flags); |
| 720 | + sbefifo_get(sbefifo); |
| 721 | + if (mod_timer(&sbefifo->poll_timer, jiffies)) |
| 722 | + sbefifo_put(sbefifo); |
| 723 | + |
| 724 | + sbefifo_put_client(client); |
| 725 | + return -ERESTARTSYS; |
| 726 | + } |
| 727 | + if (sbefifo->rc) { |
| 728 | + INIT_LIST_HEAD(&client->xfrs); |
| 729 | + sbefifo_put_client(client); |
| 730 | + return sbefifo->rc; |
| 731 | + } |
| 732 | + |
| 733 | + n = min_t(size_t, n, len); |
| 734 | + |
| 735 | + if (copy_from_user(READ_ONCE(client->wbuf.wpos), buf, n)) { |
| 736 | + set_bit(SBEFIFO_XFR_CANCEL, &xfr->flags); |
| 737 | + sbefifo_get(sbefifo); |
| 738 | + if (mod_timer(&sbefifo->poll_timer, jiffies)) |
| 739 | + sbefifo_put(sbefifo); |
| 740 | + sbefifo_put_client(client); |
| 741 | + return -EFAULT; |
| 742 | + } |
| 743 | + |
| 744 | + sbefifo_buf_wrotenb(&client->wbuf, n); |
| 745 | + len -= n; |
| 746 | + buf += n; |
| 747 | + ret += n; |
| 748 | + |
| 749 | + /* |
| 750 | + * Drain the write buffer. |
| 751 | + */ |
| 752 | + sbefifo_get(sbefifo); |
| 753 | + if (mod_timer(&client->dev->poll_timer, jiffies)) |
| 754 | + sbefifo_put(sbefifo); |
| 755 | + } |
| 756 | + |
| 757 | + set_bit(SBEFIFO_XFR_WRITE_DONE, &xfr->flags); |
| 758 | + sbefifo_put_client(client); |
| 759 | + |
| 760 | + return ret; |
| 761 | +} |
| 762 | + |
| 763 | +static int sbefifo_release(struct inode *inode, struct file *file) |
| 764 | +{ |
| 765 | + struct sbefifo_client *client = file->private_data; |
| 766 | + struct sbefifo *sbefifo = client->dev; |
| 767 | + |
| 768 | + sbefifo_put_client(client); |
| 769 | + |
| 770 | + return READ_ONCE(sbefifo->rc); |
| 771 | +} |
| 772 | + |
| 773 | +static const struct file_operations sbefifo_fops = { |
| 774 | + .owner = THIS_MODULE, |
| 775 | + .open = sbefifo_open, |
| 776 | + .read = sbefifo_read, |
| 777 | + .write = sbefifo_write, |
| 778 | + .poll = sbefifo_poll, |
| 779 | + .release = sbefifo_release, |
| 780 | +}; |
| 781 | + |
| 782 | +static int sbefifo_probe(struct device *dev) |
| 783 | +{ |
| 784 | + struct fsi_device *fsi_dev = to_fsi_dev(dev); |
| 785 | + struct sbefifo *sbefifo; |
| 786 | + u32 sts; |
| 787 | + int ret; |
| 788 | + |
| 789 | + dev_info(dev, "Found sbefifo device\n"); |
| 790 | + sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL); |
| 791 | + if (!sbefifo) |
| 792 | + return -ENOMEM; |
| 793 | + |
| 794 | + sbefifo->fsi_dev = fsi_dev; |
| 795 | + |
| 796 | + ret = sbefifo_inw(sbefifo, |
| 797 | + SBEFIFO_UP | SBEFIFO_STS, &sts); |
| 798 | + if (ret) |
| 799 | + return ret; |
| 800 | + if (!(sts & SBEFIFO_EMPTY)) { |
| 801 | + dev_err(&sbefifo->fsi_dev->dev, |
| 802 | + "Found data in upstream fifo\n"); |
| 803 | + return -EIO; |
| 804 | + } |
| 805 | + |
| 806 | + ret = sbefifo_inw(sbefifo, SBEFIFO_DWN | SBEFIFO_STS, &sts); |
| 807 | + if (ret) |
| 808 | + return ret; |
| 809 | + if (!(sts & SBEFIFO_EMPTY)) { |
| 810 | + dev_err(&sbefifo->fsi_dev->dev, |
| 811 | + "Found data in downstream fifo\n"); |
| 812 | + return -EIO; |
| 813 | + } |
| 814 | + |
| 815 | + sbefifo->mdev.minor = MISC_DYNAMIC_MINOR; |
| 816 | + sbefifo->mdev.fops = &sbefifo_fops; |
| 817 | + sbefifo->mdev.name = sbefifo->name; |
| 818 | + sbefifo->mdev.parent = dev; |
| 819 | + spin_lock_init(&sbefifo->lock); |
| 820 | + kref_init(&sbefifo->kref); |
| 821 | + |
| 822 | + sbefifo->idx = ida_simple_get(&sbefifo_ida, 1, INT_MAX, GFP_KERNEL); |
| 823 | + snprintf(sbefifo->name, sizeof(sbefifo->name), "sbefifo%d", |
| 824 | + sbefifo->idx); |
| 825 | + init_waitqueue_head(&sbefifo->wait); |
| 826 | + INIT_LIST_HEAD(&sbefifo->xfrs); |
| 827 | + |
| 828 | + /* This bit of silicon doesn't offer any interrupts... */ |
| 829 | + setup_timer(&sbefifo->poll_timer, sbefifo_poll_timer, |
| 830 | + (unsigned long)sbefifo); |
| 831 | + |
| 832 | + list_add(&sbefifo->link, &sbefifo_fifos); |
| 833 | + return misc_register(&sbefifo->mdev); |
| 834 | +} |
| 835 | + |
| 836 | +static int sbefifo_remove(struct device *dev) |
| 837 | +{ |
| 838 | + struct fsi_device *fsi_dev = to_fsi_dev(dev); |
| 839 | + struct sbefifo *sbefifo, *sbefifo_tmp; |
| 840 | + struct sbefifo_xfr *xfr; |
| 841 | + |
| 842 | + list_for_each_entry_safe(sbefifo, sbefifo_tmp, &sbefifo_fifos, link) { |
| 843 | + if (sbefifo->fsi_dev != fsi_dev) |
| 844 | + continue; |
| 845 | + misc_deregister(&sbefifo->mdev); |
| 846 | + list_del(&sbefifo->link); |
| 847 | + ida_simple_remove(&sbefifo_ida, sbefifo->idx); |
| 848 | + |
| 849 | + if (del_timer_sync(&sbefifo->poll_timer)) |
| 850 | + sbefifo_put(sbefifo); |
| 851 | + |
| 852 | + spin_lock(&sbefifo->lock); |
| 853 | + list_for_each_entry(xfr, &sbefifo->xfrs, xfrs) |
| 854 | + kfree(xfr); |
| 855 | + spin_unlock(&sbefifo->lock); |
| 856 | + |
| 857 | + WRITE_ONCE(sbefifo->rc, -ENODEV); |
| 858 | + |
| 859 | + wake_up(&sbefifo->wait); |
| 860 | + sbefifo_put(sbefifo); |
| 861 | + } |
| 862 | + |
| 863 | + return 0; |
| 864 | +} |
| 865 | + |
| 866 | +static struct fsi_device_id sbefifo_ids[] = { |
| 867 | + { |
| 868 | + .engine_type = FSI_ENGID_SBE, |
| 869 | + .version = FSI_VERSION_ANY, |
| 870 | + }, |
| 871 | + { 0 } |
| 872 | +}; |
| 873 | + |
| 874 | +static struct fsi_driver sbefifo_drv = { |
| 875 | + .id_table = sbefifo_ids, |
| 876 | + .drv = { |
| 877 | + .name = DEVICE_NAME, |
| 878 | + .bus = &fsi_bus_type, |
| 879 | + .probe = sbefifo_probe, |
| 880 | + .remove = sbefifo_remove, |
| 881 | + } |
| 882 | +}; |
| 883 | + |
| 884 | +static int sbefifo_init(void) |
| 885 | +{ |
| 886 | + INIT_LIST_HEAD(&sbefifo_fifos); |
| 887 | + return fsi_driver_register(&sbefifo_drv); |
| 888 | +} |
| 889 | + |
| 890 | +static void sbefifo_exit(void) |
| 891 | +{ |
| 892 | + fsi_driver_unregister(&sbefifo_drv); |
| 893 | +} |
| 894 | + |
| 895 | +module_init(sbefifo_init); |
| 896 | +module_exit(sbefifo_exit); |
| 897 | +MODULE_LICENSE("GPL"); |
| 898 | +MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>"); |
| 899 | +MODULE_DESCRIPTION("Linux device interface to the POWER self boot engine"); |