linux/drivers/char/ipmi/bt-bmc.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2015-2016, IBM Corporation.
   3 *
   4 * This program is free software; you can redistribute it and/or
   5 * modify it under the terms of the GNU General Public License
   6 * as published by the Free Software Foundation; either version
   7 * 2 of the License, or (at your option) any later version.
   8 */
   9
  10#include <linux/atomic.h>
  11#include <linux/bt-bmc.h>
  12#include <linux/errno.h>
  13#include <linux/interrupt.h>
  14#include <linux/io.h>
  15#include <linux/mfd/syscon.h>
  16#include <linux/miscdevice.h>
  17#include <linux/module.h>
  18#include <linux/of.h>
  19#include <linux/platform_device.h>
  20#include <linux/poll.h>
  21#include <linux/regmap.h>
  22#include <linux/sched.h>
  23#include <linux/timer.h>
  24
  25/*
  26 * This is a BMC device used to communicate to the host
  27 */
  28#define DEVICE_NAME     "ipmi-bt-host"
  29
  30#define BT_IO_BASE      0xe4
  31#define BT_IRQ          10
  32
  33#define BT_CR0          0x0
  34#define   BT_CR0_IO_BASE                16
  35#define   BT_CR0_IRQ                    12
  36#define   BT_CR0_EN_CLR_SLV_RDP         0x8
  37#define   BT_CR0_EN_CLR_SLV_WRP         0x4
  38#define   BT_CR0_ENABLE_IBT             0x1
  39#define BT_CR1          0x4
  40#define   BT_CR1_IRQ_H2B        0x01
  41#define   BT_CR1_IRQ_HBUSY      0x40
  42#define BT_CR2          0x8
  43#define   BT_CR2_IRQ_H2B        0x01
  44#define   BT_CR2_IRQ_HBUSY      0x40
  45#define BT_CR3          0xc
  46#define BT_CTRL         0x10
  47#define   BT_CTRL_B_BUSY                0x80
  48#define   BT_CTRL_H_BUSY                0x40
  49#define   BT_CTRL_OEM0                  0x20
  50#define   BT_CTRL_SMS_ATN               0x10
  51#define   BT_CTRL_B2H_ATN               0x08
  52#define   BT_CTRL_H2B_ATN               0x04
  53#define   BT_CTRL_CLR_RD_PTR            0x02
  54#define   BT_CTRL_CLR_WR_PTR            0x01
  55#define BT_BMC2HOST     0x14
  56#define BT_INTMASK      0x18
  57#define   BT_INTMASK_B2H_IRQEN          0x01
  58#define   BT_INTMASK_B2H_IRQ            0x02
  59#define   BT_INTMASK_BMC_HWRST          0x80
  60
  61#define BT_BMC_BUFFER_SIZE 256
  62
  63struct bt_bmc {
  64        struct device           dev;
  65        struct miscdevice       miscdev;
  66        struct regmap           *map;
  67        int                     offset;
  68        int                     irq;
  69        wait_queue_head_t       queue;
  70        struct timer_list       poll_timer;
  71        struct mutex            mutex;
  72};
  73
  74static atomic_t open_count = ATOMIC_INIT(0);
  75
  76static const struct regmap_config bt_regmap_cfg = {
  77        .reg_bits = 32,
  78        .val_bits = 32,
  79        .reg_stride = 4,
  80};
  81
  82static u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
  83{
  84        uint32_t val = 0;
  85        int rc;
  86
  87        rc = regmap_read(bt_bmc->map, bt_bmc->offset + reg, &val);
  88        WARN(rc != 0, "regmap_read() failed: %d\n", rc);
  89
  90        return rc == 0 ? (u8) val : 0;
  91}
  92
  93static void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
  94{
  95        int rc;
  96
  97        rc = regmap_write(bt_bmc->map, bt_bmc->offset + reg, data);
  98        WARN(rc != 0, "regmap_write() failed: %d\n", rc);
  99}
 100
 101static void clr_rd_ptr(struct bt_bmc *bt_bmc)
 102{
 103        bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
 104}
 105
 106static void clr_wr_ptr(struct bt_bmc *bt_bmc)
 107{
 108        bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
 109}
 110
 111static void clr_h2b_atn(struct bt_bmc *bt_bmc)
 112{
 113        bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
 114}
 115
 116static void set_b_busy(struct bt_bmc *bt_bmc)
 117{
 118        if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
 119                bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
 120}
 121
 122static void clr_b_busy(struct bt_bmc *bt_bmc)
 123{
 124        if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
 125                bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
 126}
 127
 128static void set_b2h_atn(struct bt_bmc *bt_bmc)
 129{
 130        bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
 131}
 132
 133static u8 bt_read(struct bt_bmc *bt_bmc)
 134{
 135        return bt_inb(bt_bmc, BT_BMC2HOST);
 136}
 137
 138static ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
 139{
 140        int i;
 141
 142        for (i = 0; i < n; i++)
 143                buf[i] = bt_read(bt_bmc);
 144        return n;
 145}
 146
 147static void bt_write(struct bt_bmc *bt_bmc, u8 c)
 148{
 149        bt_outb(bt_bmc, c, BT_BMC2HOST);
 150}
 151
 152static ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
 153{
 154        int i;
 155
 156        for (i = 0; i < n; i++)
 157                bt_write(bt_bmc, buf[i]);
 158        return n;
 159}
 160
 161static void set_sms_atn(struct bt_bmc *bt_bmc)
 162{
 163        bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
 164}
 165
 166static struct bt_bmc *file_bt_bmc(struct file *file)
 167{
 168        return container_of(file->private_data, struct bt_bmc, miscdev);
 169}
 170
 171static int bt_bmc_open(struct inode *inode, struct file *file)
 172{
 173        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 174
 175        if (atomic_inc_return(&open_count) == 1) {
 176                clr_b_busy(bt_bmc);
 177                return 0;
 178        }
 179
 180        atomic_dec(&open_count);
 181        return -EBUSY;
 182}
 183
 184/*
 185 * The BT (Block Transfer) interface means that entire messages are
 186 * buffered by the host before a notification is sent to the BMC that
 187 * there is data to be read. The first byte is the length and the
 188 * message data follows. The read operation just tries to capture the
 189 * whole before returning it to userspace.
 190 *
 191 * BT Message format :
 192 *
 193 *    Byte 1  Byte 2     Byte 3  Byte 4  Byte 5:N
 194 *    Length  NetFn/LUN  Seq     Cmd     Data
 195 *
 196 */
 197static ssize_t bt_bmc_read(struct file *file, char __user *buf,
 198                           size_t count, loff_t *ppos)
 199{
 200        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 201        u8 len;
 202        int len_byte = 1;
 203        u8 kbuffer[BT_BMC_BUFFER_SIZE];
 204        ssize_t ret = 0;
 205        ssize_t nread;
 206
 207        if (!access_ok(VERIFY_WRITE, buf, count))
 208                return -EFAULT;
 209
 210        WARN_ON(*ppos);
 211
 212        if (wait_event_interruptible(bt_bmc->queue,
 213                                     bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
 214                return -ERESTARTSYS;
 215
 216        mutex_lock(&bt_bmc->mutex);
 217
 218        if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
 219                ret = -EIO;
 220                goto out_unlock;
 221        }
 222
 223        set_b_busy(bt_bmc);
 224        clr_h2b_atn(bt_bmc);
 225        clr_rd_ptr(bt_bmc);
 226
 227        /*
 228         * The BT frames start with the message length, which does not
 229         * include the length byte.
 230         */
 231        kbuffer[0] = bt_read(bt_bmc);
 232        len = kbuffer[0];
 233
 234        /* We pass the length back to userspace as well */
 235        if (len + 1 > count)
 236                len = count - 1;
 237
 238        while (len) {
 239                nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
 240
 241                bt_readn(bt_bmc, kbuffer + len_byte, nread);
 242
 243                if (copy_to_user(buf, kbuffer, nread + len_byte)) {
 244                        ret = -EFAULT;
 245                        break;
 246                }
 247                len -= nread;
 248                buf += nread + len_byte;
 249                ret += nread + len_byte;
 250                len_byte = 0;
 251        }
 252
 253        clr_b_busy(bt_bmc);
 254
 255out_unlock:
 256        mutex_unlock(&bt_bmc->mutex);
 257        return ret;
 258}
 259
 260/*
 261 * BT Message response format :
 262 *
 263 *    Byte 1  Byte 2     Byte 3  Byte 4  Byte 5  Byte 6:N
 264 *    Length  NetFn/LUN  Seq     Cmd     Code    Data
 265 */
 266static ssize_t bt_bmc_write(struct file *file, const char __user *buf,
 267                            size_t count, loff_t *ppos)
 268{
 269        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 270        u8 kbuffer[BT_BMC_BUFFER_SIZE];
 271        ssize_t ret = 0;
 272        ssize_t nwritten;
 273
 274        /*
 275         * send a minimum response size
 276         */
 277        if (count < 5)
 278                return -EINVAL;
 279
 280        if (!access_ok(VERIFY_READ, buf, count))
 281                return -EFAULT;
 282
 283        WARN_ON(*ppos);
 284
 285        /*
 286         * There's no interrupt for clearing bmc busy so we have to
 287         * poll
 288         */
 289        if (wait_event_interruptible(bt_bmc->queue,
 290                                     !(bt_inb(bt_bmc, BT_CTRL) &
 291                                       (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
 292                return -ERESTARTSYS;
 293
 294        mutex_lock(&bt_bmc->mutex);
 295
 296        if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
 297                     (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
 298                ret = -EIO;
 299                goto out_unlock;
 300        }
 301
 302        clr_wr_ptr(bt_bmc);
 303
 304        while (count) {
 305                nwritten = min_t(ssize_t, count, sizeof(kbuffer));
 306                if (copy_from_user(&kbuffer, buf, nwritten)) {
 307                        ret = -EFAULT;
 308                        break;
 309                }
 310
 311                bt_writen(bt_bmc, kbuffer, nwritten);
 312
 313                count -= nwritten;
 314                buf += nwritten;
 315                ret += nwritten;
 316        }
 317
 318        set_b2h_atn(bt_bmc);
 319
 320out_unlock:
 321        mutex_unlock(&bt_bmc->mutex);
 322        return ret;
 323}
 324
 325static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
 326                         unsigned long param)
 327{
 328        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 329
 330        switch (cmd) {
 331        case BT_BMC_IOCTL_SMS_ATN:
 332                set_sms_atn(bt_bmc);
 333                return 0;
 334        }
 335        return -EINVAL;
 336}
 337
 338static int bt_bmc_release(struct inode *inode, struct file *file)
 339{
 340        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 341
 342        atomic_dec(&open_count);
 343        set_b_busy(bt_bmc);
 344        return 0;
 345}
 346
 347static unsigned int bt_bmc_poll(struct file *file, poll_table *wait)
 348{
 349        struct bt_bmc *bt_bmc = file_bt_bmc(file);
 350        unsigned int mask = 0;
 351        u8 ctrl;
 352
 353        poll_wait(file, &bt_bmc->queue, wait);
 354
 355        ctrl = bt_inb(bt_bmc, BT_CTRL);
 356
 357        if (ctrl & BT_CTRL_H2B_ATN)
 358                mask |= POLLIN;
 359
 360        if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
 361                mask |= POLLOUT;
 362
 363        return mask;
 364}
 365
 366static const struct file_operations bt_bmc_fops = {
 367        .owner          = THIS_MODULE,
 368        .open           = bt_bmc_open,
 369        .read           = bt_bmc_read,
 370        .write          = bt_bmc_write,
 371        .release        = bt_bmc_release,
 372        .poll           = bt_bmc_poll,
 373        .unlocked_ioctl = bt_bmc_ioctl,
 374};
 375
 376static void poll_timer(unsigned long data)
 377{
 378        struct bt_bmc *bt_bmc = (void *)data;
 379
 380        bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
 381        wake_up(&bt_bmc->queue);
 382        add_timer(&bt_bmc->poll_timer);
 383}
 384
 385static irqreturn_t bt_bmc_irq(int irq, void *arg)
 386{
 387        struct bt_bmc *bt_bmc = arg;
 388        u32 reg;
 389        int rc;
 390
 391        rc = regmap_read(bt_bmc->map, bt_bmc->offset + BT_CR2, &reg);
 392        if (rc)
 393                return IRQ_NONE;
 394
 395        reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
 396        if (!reg)
 397                return IRQ_NONE;
 398
 399        /* ack pending IRQs */
 400        regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR2, reg);
 401
 402        wake_up(&bt_bmc->queue);
 403        return IRQ_HANDLED;
 404}
 405
 406static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
 407                             struct platform_device *pdev)
 408{
 409        struct device *dev = &pdev->dev;
 410        int rc;
 411
 412        bt_bmc->irq = platform_get_irq(pdev, 0);
 413        if (!bt_bmc->irq)
 414                return -ENODEV;
 415
 416        rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
 417                              DEVICE_NAME, bt_bmc);
 418        if (rc < 0) {
 419                dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
 420                bt_bmc->irq = 0;
 421                return rc;
 422        }
 423
 424        /*
 425         * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
 426         * H2B will be asserted when the bmc has data for us; HBUSY
 427         * will be cleared (along with B2H) when we can write the next
 428         * message to the BT buffer
 429         */
 430        rc = regmap_update_bits(bt_bmc->map, bt_bmc->offset + BT_CR1,
 431                                (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY),
 432                                (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY));
 433
 434        return rc;
 435}
 436
 437static int bt_bmc_probe(struct platform_device *pdev)
 438{
 439        struct bt_bmc *bt_bmc;
 440        struct device *dev;
 441        int rc;
 442
 443        if (!pdev || !pdev->dev.of_node)
 444                return -ENODEV;
 445
 446        dev = &pdev->dev;
 447        dev_info(dev, "Found bt bmc device\n");
 448
 449        bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
 450        if (!bt_bmc)
 451                return -ENOMEM;
 452
 453        dev_set_drvdata(&pdev->dev, bt_bmc);
 454
 455        bt_bmc->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
 456        if (IS_ERR(bt_bmc->map)) {
 457                struct resource *res;
 458                void __iomem *base;
 459
 460                /*
 461                 * Assume it's not the MFD-based devicetree description, in
 462                 * which case generate a regmap ourselves
 463                 */
 464                res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 465                base = devm_ioremap_resource(&pdev->dev, res);
 466                if (IS_ERR(base))
 467                        return PTR_ERR(base);
 468
 469                bt_bmc->map = devm_regmap_init_mmio(dev, base, &bt_regmap_cfg);
 470                bt_bmc->offset = 0;
 471        } else {
 472                rc = of_property_read_u32(dev->of_node, "reg", &bt_bmc->offset);
 473                if (rc)
 474                        return rc;
 475        }
 476
 477        mutex_init(&bt_bmc->mutex);
 478        init_waitqueue_head(&bt_bmc->queue);
 479
 480        bt_bmc->miscdev.minor   = MISC_DYNAMIC_MINOR,
 481                bt_bmc->miscdev.name    = DEVICE_NAME,
 482                bt_bmc->miscdev.fops    = &bt_bmc_fops,
 483                bt_bmc->miscdev.parent = dev;
 484        rc = misc_register(&bt_bmc->miscdev);
 485        if (rc) {
 486                dev_err(dev, "Unable to register misc device\n");
 487                return rc;
 488        }
 489
 490        bt_bmc_config_irq(bt_bmc, pdev);
 491
 492        if (bt_bmc->irq) {
 493                dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
 494        } else {
 495                dev_info(dev, "No IRQ; using timer\n");
 496                setup_timer(&bt_bmc->poll_timer, poll_timer,
 497                            (unsigned long)bt_bmc);
 498                bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
 499                add_timer(&bt_bmc->poll_timer);
 500        }
 501
 502        regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR0,
 503                     (BT_IO_BASE << BT_CR0_IO_BASE) |
 504                     (BT_IRQ << BT_CR0_IRQ) |
 505                     BT_CR0_EN_CLR_SLV_RDP |
 506                     BT_CR0_EN_CLR_SLV_WRP |
 507                     BT_CR0_ENABLE_IBT);
 508
 509        clr_b_busy(bt_bmc);
 510
 511        return 0;
 512}
 513
 514static int bt_bmc_remove(struct platform_device *pdev)
 515{
 516        struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
 517
 518        misc_deregister(&bt_bmc->miscdev);
 519        if (!bt_bmc->irq)
 520                del_timer_sync(&bt_bmc->poll_timer);
 521        return 0;
 522}
 523
 524static const struct of_device_id bt_bmc_match[] = {
 525        { .compatible = "aspeed,ast2400-ibt-bmc" },
 526        { .compatible = "aspeed,ast2500-ibt-bmc" },
 527        { },
 528};
 529
 530static struct platform_driver bt_bmc_driver = {
 531        .driver = {
 532                .name           = DEVICE_NAME,
 533                .of_match_table = bt_bmc_match,
 534        },
 535        .probe = bt_bmc_probe,
 536        .remove = bt_bmc_remove,
 537};
 538
 539module_platform_driver(bt_bmc_driver);
 540
 541MODULE_DEVICE_TABLE(of, bt_bmc_match);
 542MODULE_LICENSE("GPL");
 543MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
 544MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");
 545