Patrick Venture | 605687d | 2019-03-08 20:23:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * I2C Bus driver proxy for BMC I2C bus access. |
| 3 | * |
| 4 | * For hosts incorporating a BMC, I2C resources are typically connected to |
| 5 | * the BMC, not the host. This module creates host proxies for the BMC's |
| 6 | * virtual I2C adapters. |
| 7 | * |
| 8 | * At heart, each proxy is a virtual i2c-adapter, and so supports: |
| 9 | * . raw I2C I/O - as used by i2c-tools, for example |
| 10 | * . dynamic i2c device creation |
| 11 | * |
| 12 | * These proxies and the IPMI extensions underpinning them directly support |
| 13 | * the I2C_RDWR interface; Linux' SMBUS emulation layer supports SMBUS calls |
| 14 | * on top of that. |
| 15 | * |
| 16 | * Each proxy device connects to exactly one i2c adapter at the BMC, |
| 17 | * but you can create many proxies if you have many adapters to reach. |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License version 2 as |
| 21 | * published by the Free Software Foundation. |
| 22 | */ |
| 23 | |
| 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
| 26 | #include <linux/completion.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/ipmi.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/of_platform.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/printk.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/stddef.h> |
| 36 | #include <linux/types.h> |
| 37 | #include <stdarg.h> |
| 38 | |
| 39 | /* TODO(peterh): move to include/uapi/linux/ipmi_msgdefs.h */ |
| 40 | #define IPMI_NETFN_OEM_GROUP_REQUEST 0x2e |
| 41 | #define IPMI_NETFN_OEM_GROUP_RESPONSE 0x2f |
| 42 | |
| 43 | /* Specific IANA OEM Numbers for this functionality. */ |
| 44 | #define IPMI_OEM_GOOG 11129 |
| 45 | #define IPMI_OEM_OPENBMC 49871 |
| 46 | |
| 47 | /* Specific OEM Message for this functionality. */ |
| 48 | #define OPENBMC_I2C_OEM_IPMI_CMD 2 |
| 49 | |
| 50 | /* OEM IPMI Request header consists of 3-byte IANA OEN */ |
| 51 | #define OEM_IPMI_REQ_HDR_OEN 0 |
| 52 | #define OEM_IPMI_REQ_HDR_LEN 3 |
| 53 | |
| 54 | /* OEM IPMI Reply header consists of CC and 3 byte IANA OEN */ |
| 55 | #define OEM_IPMI_REPLY_HDR_CC 0 |
| 56 | #define OEM_IPMI_REPLY_HDR_OEN 1 |
| 57 | #define OEM_IPMI_REPLY_HDR_LEN 4 |
| 58 | |
| 59 | /* I2C OEM IPMI Request header */ |
| 60 | #define I2C_OEM_IPMI_REQ_BUS 0 |
| 61 | #define I2C_OEM_IPMI_REQ_FLAGS 1 |
| 62 | #define I2C_OEM_IPMI_REQ_FLAG_PEC (1 << 7) |
| 63 | #define I2C_OEM_IPMI_REQ_HDR_LEN 2 |
| 64 | /* Followed by one or more steps. */ |
| 65 | |
| 66 | /* I2C OEM IPMI Request step - optional repeated element. */ |
| 67 | #define I2C_OEM_IPMI_STEP_DEV_AND_DIR 0 |
| 68 | #define I2C_OEM_IPMI_STEP_IS_READ (1 << 0) |
| 69 | #define I2C_OEM_IPMI_STEP_DEV(dandd) ((dandd) >> 1) |
| 70 | #define I2C_OEM_IPMI_STEP_FLAGS 1 |
| 71 | #define I2C_OEM_IPMI_STEP_FLAG_RECV_LEN (1 << 7) |
| 72 | #define I2C_OEM_IPMI_STEP_PARM 2 |
| 73 | #define I2C_OEM_IPMI_STEP_HDR_LEN 3 |
| 74 | /* Followed by parm bytes of wr data if ! IS_READ */ |
| 75 | |
| 76 | /* |
| 77 | * I2C OEM IPMI Reply consists of OEM IPMI reply header, |
| 78 | * followed by every byte read, in requested order. |
| 79 | */ |
| 80 | |
| 81 | #define IPMI_MAX_REQ_LEN 60 |
| 82 | #define IPMI_MAX_STEP_LEN 34 |
| 83 | |
| 84 | /* via-ipmi as little-endian hex-ascii */ |
| 85 | #define I2C_VIA_IPMI_BUS_MAGIC 0x696d70692d616976 |
| 86 | |
| 87 | /** |
| 88 | * struct i2c_via_ipmi_bus - per proxy state. |
| 89 | |
| 90 | * @magic: Pattern usable to verify void pointer conversion validity. |
| 91 | * @node: Embedded list pointers for this structure. |
| 92 | * @pdev: Parent i2c-via-ipmi platform device. |
| 93 | * @remote_bus_id: Which BMC i2c adapter number to proxy. |
| 94 | * @adap: Embedded i2c adapter. |
| 95 | * @lock: Spinlock protecting proxy from ioctl vs. reply races. |
| 96 | * @ipmi_seq: Nonce to use as msgId for IPMI request/reply; message |
| 97 | * handler rejects mismatches as stale replies. |
| 98 | * @cmd_complete: Completion struct for this proxy. |
| 99 | * @cmd_rc: Completion code from IPMI reply. |
| 100 | * @msgs: Pointer to array of messages as given to i2c xfer handler. |
| 101 | * @msgs_count:`Number of msgs[] to process as given to i2c xfer handler. |
| 102 | * @if_num: IPMI interface number to use for BMC messaging. |
| 103 | * @user: IPMI user for routing requests and replies. |
| 104 | * @req_addr: IPMI address to which request is addressed. |
| 105 | * (Always local BMC in this version.) |
| 106 | * @req: IPMI request structure. |
| 107 | * @req_data: Reserved for IPMI request data bytes. |
| 108 | * |
| 109 | * This structure holds the state of each BMC i2c proxy adapter during its |
| 110 | * lifetime. |
| 111 | * |
| 112 | * The adap property allows this proxy to act as an i2c adapter, while |
| 113 | * IPMI user and related addressing information tie the proxy to the |
| 114 | * IPMI messaging service, and thence the BMC. |
| 115 | * |
| 116 | * The msgs and req related properties convery i2c xfer arguments to the |
| 117 | * reply handler, while the completion properties flow back from the reply |
| 118 | * handler to the i2c xfer operation that launched the request. |
| 119 | */ |
| 120 | struct i2c_via_ipmi_bus { |
| 121 | /* private: internal use only */ |
| 122 | u64 magic; |
| 123 | struct list_head node; |
| 124 | struct platform_device *pdev; |
| 125 | unsigned remote_bus_id; |
| 126 | struct i2c_adapter adap; |
| 127 | spinlock_t lock; |
| 128 | long ipmi_seq; |
| 129 | struct completion cmd_complete; |
| 130 | int cmd_rc; |
| 131 | struct i2c_msg *msgs; |
| 132 | size_t msgs_count; |
| 133 | unsigned if_num; |
| 134 | ipmi_user_t user; |
| 135 | struct ipmi_addr req_addr; |
| 136 | struct kernel_ipmi_msg req; |
| 137 | unsigned char req_data[256]; |
| 138 | }; |
| 139 | |
| 140 | #define ADAP_NAME_MAX sizeof(((struct i2c_via_ipmi_bus *)0)->adap.name) |
| 141 | |
| 142 | static LIST_HEAD(proxies); |
| 143 | |
| 144 | static char *proxy_invalid_reason(struct i2c_via_ipmi_bus *bus) |
| 145 | { |
| 146 | if (!bus) |
| 147 | return kasprintf(GFP_KERNEL, "is nullptr"); |
| 148 | if (bus->magic != I2C_VIA_IPMI_BUS_MAGIC) |
| 149 | return kasprintf(GFP_KERNEL, "has bad magic %#llx", bus->magic); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | static void i2c_via_ipmi_reply_handler(struct ipmi_recv_msg *reply, |
| 154 | void *user_msg_data) |
| 155 | { |
| 156 | const unsigned char *buf, *oen, *next_byte, *buf_end; |
| 157 | struct i2c_via_ipmi_bus *bus; |
| 158 | unsigned char recv_len; |
| 159 | struct i2c_msg *msg; |
| 160 | struct device *dev; |
| 161 | char *reason; |
| 162 | u32 oem_code; |
| 163 | int cmd_rc; |
| 164 | size_t i; |
| 165 | /* |
| 166 | * If message isn't a reply to our ipmi_request_settime, |
| 167 | * then user_msg_data is unlikely to point to our i2c_via_ipmi_bus, |
| 168 | * so confirm everything we can. |
| 169 | */ |
| 170 | bus = user_msg_data; |
| 171 | reason = proxy_invalid_reason(bus); |
| 172 | if (reason) { |
| 173 | pr_err("%s: user_msg_data %s", __func__, reason); |
| 174 | kfree(reason); |
| 175 | goto not_our_reply; |
| 176 | } |
| 177 | dev = &bus->adap.dev; |
| 178 | if (!(reply->user && |
| 179 | reply->user_msg_data == user_msg_data && |
| 180 | reply->msg.netfn == IPMI_NETFN_OEM_GROUP_RESPONSE && |
| 181 | reply->msg.cmd == OPENBMC_I2C_OEM_IPMI_CMD && |
| 182 | /* Check response length for error state or valid OEM packet */ |
| 183 | ((reply->msg.data_len > 0 && |
| 184 | (reply->msg.data[OEM_IPMI_REPLY_HDR_CC] != IPMI_CC_NO_ERROR)) || |
| 185 | (reply->msg.data_len >= OEM_IPMI_REPLY_HDR_LEN)))) { |
| 186 | dev_info(dev, "%s: apparent non-reply?", __func__); |
| 187 | goto not_our_reply; |
| 188 | } |
| 189 | if (!spin_trylock(&bus->lock)) { |
| 190 | dev_info(dev, "%s: drop seq=%ld, mutex busy.", |
| 191 | __func__, reply->msgid); |
| 192 | goto not_our_reply; |
| 193 | } |
| 194 | if (reply->msgid != bus->ipmi_seq) { |
| 195 | spin_unlock(&bus->lock); |
| 196 | dev_info(dev, "%s: drop seq=%ld, want seq=%ld.", |
| 197 | __func__, reply->msgid, bus->ipmi_seq); |
| 198 | goto not_our_reply; |
| 199 | } |
| 200 | buf = reply->msg.data; |
| 201 | buf_end = buf + reply->msg.data_len; |
| 202 | |
| 203 | /* |
| 204 | * Extract CC and IANA OEN from OEM IPMI Reply header. |
| 205 | */ |
| 206 | cmd_rc = buf[OEM_IPMI_REPLY_HDR_CC]; |
| 207 | /* |
| 208 | * If we get an error return, accept the reply and return the error |
| 209 | * up the I2C stack. |
| 210 | */ |
| 211 | if (cmd_rc != IPMI_CC_NO_ERROR) |
| 212 | goto accept_reply; |
| 213 | |
| 214 | oen = &buf[OEM_IPMI_REPLY_HDR_OEN]; |
| 215 | oem_code = ((((u32)oen[2] << 8) | oen[1]) << 8) | oen[0]; |
| 216 | next_byte = buf + OEM_IPMI_REPLY_HDR_LEN; |
| 217 | if (oem_code != IPMI_OEM_OPENBMC) { |
| 218 | spin_unlock(&bus->lock); |
| 219 | dev_info(dev, "%s: wrong OEM Enterprise Number %u", |
| 220 | __func__, oem_code); |
| 221 | goto not_our_reply; |
| 222 | } |
| 223 | /* |
| 224 | * Note: having confirmed it's a reply to our request, |
| 225 | * any return past here should complete that request. |
| 226 | */ |
| 227 | /* |
| 228 | * BMC I/O seems to have worked - that's all for writes; if there |
| 229 | * are read steps, the rest of the reply caries the bytes read. |
| 230 | * Check the whole reply before trusting any of it - if it is |
| 231 | * the wrong size for this request, call it an I/O error. |
| 232 | */ |
| 233 | for (i = 0; i < bus->msgs_count; ++i) { |
| 234 | msg = &bus->msgs[i]; |
| 235 | if (!(msg->flags & I2C_M_RD)) |
| 236 | continue; |
| 237 | if (msg->flags & I2C_M_RECV_LEN) { |
| 238 | if (next_byte >= buf_end) { |
| 239 | bus->cmd_rc = -EPROTO; |
| 240 | spin_unlock(&bus->lock); |
| 241 | dev_err(dev, "%s[%zu]: response omits RECV_LEN", |
| 242 | __func__, i); |
| 243 | goto reject_reply; |
| 244 | } |
| 245 | /* |
| 246 | * Limit net payload to SMBUS maximum. |
| 247 | * To avoid overrun, buffer should be able to hold |
| 248 | * union i2c_smbus_data for this kind of step. |
| 249 | */ |
| 250 | recv_len = *next_byte; |
| 251 | if (recv_len > I2C_SMBUS_BLOCK_MAX) { |
| 252 | bus->cmd_rc = -EPROTO; |
| 253 | spin_unlock(&bus->lock); |
| 254 | dev_err(dev, "%s[%zu]: recv_len=%u, max is %d", |
| 255 | __func__, i, recv_len, |
| 256 | I2C_SMBUS_BLOCK_MAX); |
| 257 | goto reject_reply; |
| 258 | } |
| 259 | msg->len = recv_len + |
| 260 | (msg->flags & I2C_CLIENT_PEC ? 2 : 1); |
| 261 | } |
| 262 | next_byte += msg->len; |
| 263 | if (next_byte > buf_end) { |
| 264 | bus->cmd_rc = -EPROTO; |
| 265 | spin_unlock(&bus->lock); |
| 266 | dev_err(dev, "%s[%zu]: response too small", |
| 267 | __func__, i); |
| 268 | goto reject_reply; |
| 269 | } |
| 270 | } |
| 271 | if (next_byte < buf_end) { |
| 272 | bus->cmd_rc = -EPROTO; |
| 273 | spin_unlock(&bus->lock); |
| 274 | dev_err(dev, "%s: response len=%d, expected=%zd", |
| 275 | __func__, reply->msg.data_len, buf_end - next_byte); |
| 276 | goto reject_reply; |
| 277 | } |
| 278 | /* |
| 279 | * Everything checks out, copy results back to caller. |
| 280 | */ |
| 281 | next_byte = buf + 4; |
| 282 | for (i = 0; i < bus->msgs_count; ++i) { |
| 283 | msg = &bus->msgs[i]; |
| 284 | if (msg->flags & I2C_M_RD) { |
| 285 | memcpy(msg->buf, next_byte, msg->len); |
| 286 | next_byte += msg->len; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | accept_reply: |
| 291 | bus->cmd_rc = cmd_rc; |
| 292 | spin_unlock(&bus->lock); |
| 293 | |
| 294 | reject_reply: |
| 295 | complete(&bus->cmd_complete); |
| 296 | |
| 297 | not_our_reply: |
| 298 | reply->done(reply); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | static struct ipmi_user_hndl i2c_via_ipmi_ipmi_handlers = { |
| 303 | .ipmi_recv_hndl = i2c_via_ipmi_reply_handler, |
| 304 | }; |
| 305 | |
| 306 | static u32 i2c_via_ipmi_functionality(struct i2c_adapter *adap) |
| 307 | { |
| 308 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA; |
| 309 | } |
| 310 | |
| 311 | static int i2c_via_ipmi_xfer(struct i2c_adapter *adap, |
| 312 | struct i2c_msg *msgs, int num) |
| 313 | { |
| 314 | unsigned char *next_byte, *oen, *msg_hdr, *step_hdr; |
| 315 | struct i2c_via_ipmi_bus *bus = adap->algo_data; |
| 316 | struct device *dev = &adap->dev; |
| 317 | struct kernel_ipmi_msg *req; |
| 318 | const struct i2c_msg *msg; |
| 319 | char *reason; |
| 320 | int cmd_rc; |
| 321 | size_t i; |
| 322 | int rc; |
| 323 | |
| 324 | reason = proxy_invalid_reason(bus); |
| 325 | if (reason) { |
| 326 | dev_err(dev, "%s: adap.algo_data %s", __func__, reason); |
| 327 | kfree(reason); |
| 328 | return -EFAULT; |
| 329 | } |
| 330 | /* |
| 331 | * Setup kernel message structure |
| 332 | */ |
| 333 | req = &bus->req; |
| 334 | next_byte = bus->req_data; |
| 335 | req->data = next_byte; |
| 336 | /* |
| 337 | * Route message to specific OEM command handler. |
| 338 | */ |
| 339 | req->netfn = IPMI_NETFN_OEM_GROUP_REQUEST; |
| 340 | req->cmd = OPENBMC_I2C_OEM_IPMI_CMD; |
| 341 | /* |
| 342 | * IANA OEN is the only content of OEM IPMI Request header. |
| 343 | */ |
| 344 | oen = &next_byte[OEM_IPMI_REQ_HDR_OEN]; |
| 345 | oen[0] = (unsigned char)(IPMI_OEM_OPENBMC & 255); |
| 346 | oen[1] = (unsigned char)((IPMI_OEM_OPENBMC >> 8) & 255); |
| 347 | oen[2] = (unsigned char)((IPMI_OEM_OPENBMC >> 16) & 255); |
| 348 | next_byte += OEM_IPMI_REQ_HDR_LEN; |
| 349 | /* |
| 350 | * I2C OEM Message header: bus id & overall flag byte. |
| 351 | */ |
| 352 | msg_hdr = next_byte; |
| 353 | msg_hdr[I2C_OEM_IPMI_REQ_BUS] = bus->remote_bus_id; |
| 354 | msg_hdr[I2C_OEM_IPMI_REQ_FLAGS] = 0; |
| 355 | next_byte += I2C_OEM_IPMI_REQ_HDR_LEN; |
| 356 | /* |
| 357 | * Foreach i2c message, append a step. |
| 358 | */ |
| 359 | for (i = 0; i < num; ++i) { |
| 360 | msg = &msgs[i]; |
| 361 | if (msg->len > IPMI_MAX_STEP_LEN) { |
| 362 | dev_err(dev, "%s[%zu](%#x): len=%d, max=%d", |
| 363 | __func__, i, msg->addr, |
| 364 | msg->len, IPMI_MAX_STEP_LEN); |
| 365 | return -EMSGSIZE; |
| 366 | } |
| 367 | /* |
| 368 | * Step header - if write step, payload will follow. |
| 369 | */ |
| 370 | step_hdr = next_byte; |
| 371 | next_byte += I2C_OEM_IPMI_STEP_HDR_LEN; |
| 372 | if (next_byte - msg_hdr > IPMI_MAX_REQ_LEN) { |
| 373 | dev_err(dev, "%s: request too big.", __func__); |
| 374 | return -EMSGSIZE; |
| 375 | } |
| 376 | step_hdr[I2C_OEM_IPMI_STEP_DEV_AND_DIR] = msg->addr << 1; |
| 377 | step_hdr[I2C_OEM_IPMI_STEP_FLAGS] = 0; |
| 378 | step_hdr[I2C_OEM_IPMI_STEP_PARM] = msg->len; |
| 379 | if (!(msg->flags & I2C_M_RD)) { |
| 380 | if (msg->flags & I2C_M_RECV_LEN) { |
| 381 | dev_err(dev, "%s[%zu](%#x): RECV_LEN for WR?", |
| 382 | __func__, i, msg->addr); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | if (next_byte + msg->len - msg_hdr > IPMI_MAX_REQ_LEN) { |
| 386 | dev_err(dev, "%s[%zu](%#x): request too big.", |
| 387 | __func__, i, msg->addr); |
| 388 | return -EMSGSIZE; |
| 389 | } |
| 390 | if (msg->len) { |
| 391 | memcpy(next_byte, msg->buf, msg->len); |
| 392 | next_byte += msg->len; |
| 393 | } |
| 394 | dev_dbg(dev, "%s[%zu](%#x): write %d bytes.", |
| 395 | __func__, i, msg->addr, msg->len); |
| 396 | continue; |
| 397 | } |
| 398 | step_hdr[0] |= 1; /* Set rd bit */ |
| 399 | if (msg->flags & I2C_M_RECV_LEN) { |
| 400 | step_hdr[I2C_OEM_IPMI_STEP_FLAGS] |= |
| 401 | I2C_OEM_IPMI_STEP_FLAG_RECV_LEN; |
| 402 | if (msg->len == 2) |
| 403 | msg_hdr[I2C_OEM_IPMI_REQ_FLAGS] |= |
| 404 | I2C_OEM_IPMI_REQ_FLAG_PEC; |
| 405 | dev_dbg(dev, "%s[%zu](%#x): read count+%d bytes.", |
| 406 | __func__, i, msg->addr, msg->len); |
| 407 | continue; |
| 408 | } |
| 409 | dev_dbg(dev, "%s[%zu](%#x): read %d bytes.", |
| 410 | __func__, i, msg->addr, msg->len); |
| 411 | } |
| 412 | req->data_len = next_byte - bus->req_data; |
| 413 | dev_dbg(dev, "%s: Sending %d step, %d byte request.", |
| 414 | __func__, num, req->data_len); |
| 415 | /* |
| 416 | * Completion routines need key xfer parameters. |
| 417 | */ |
| 418 | bus->msgs = msgs; |
| 419 | bus->msgs_count = num; |
| 420 | |
| 421 | /* |
| 422 | * IPMI handler modifies the same proxy object to return results. |
| 423 | * Concurrent proxy access is protected by two properties: |
| 424 | * bus->ipmi_seq, a unique sequence number, and |
| 425 | * bus->lock, a regular spinlock. |
| 426 | * The ipmi_request_settime() call carries ipmi_seq as its msgId; |
| 427 | * the IPMI handler checks for exact match to ensure it is a reply |
| 428 | * to THIS request. Upon match, it copies answers back & completes |
| 429 | * the request. Upon mismatch, proxy state is not modified in any |
| 430 | * way, and only lock, impi_seq, and dev are referenced. |
| 431 | * This side, the ioctl handler, advances ipmi_seq immediately after |
| 432 | * completion or timeout, so any later response will be dropped. |
| 433 | * Upon timeout, the cmd_cc state seeded here will remain. |
| 434 | * The spinlock is held here while advancing ipmi_seq, and in the |
| 435 | * IPMI resonse handler while relying upon the match. |
| 436 | */ |
| 437 | bus->cmd_rc = -ETIMEDOUT; |
| 438 | reinit_completion(&bus->cmd_complete); |
| 439 | rc = ipmi_request_settime(bus->user, &bus->req_addr, bus->ipmi_seq, |
| 440 | req, bus, 0, -1, 0); |
| 441 | if (rc < 0) { |
| 442 | dev_err(dev, "%s: ipmi_request_settime returned %d.", |
| 443 | __func__, rc); |
| 444 | return rc; |
| 445 | } |
| 446 | wait_for_completion_killable_timeout(&bus->cmd_complete, |
| 447 | bus->adap.timeout); |
| 448 | spin_lock(&bus->lock); |
| 449 | bus->ipmi_seq += 1; |
| 450 | cmd_rc = bus->cmd_rc; |
| 451 | spin_unlock(&bus->lock); |
| 452 | if (!cmd_rc) { |
| 453 | dev_dbg(dev, "%s: returning num=%d.", __func__, num); |
| 454 | return num; |
| 455 | } |
| 456 | if (cmd_rc == -ETIMEDOUT) { |
| 457 | dev_err(dev, "%s: returning -ETIMEDOUT.", __func__); |
| 458 | return cmd_rc; |
| 459 | } |
| 460 | if (cmd_rc == -EPROTO) { |
| 461 | dev_err(dev, "%s: returning -EPROTO.", __func__); |
| 462 | return -EPROTO; |
| 463 | } |
| 464 | if (cmd_rc > 0) { |
| 465 | dev_dbg(dev, "%s: cmd_rc=%d, so returning -EPROTO.", |
| 466 | __func__, cmd_rc); |
| 467 | return -EPROTO; |
| 468 | } |
| 469 | dev_dbg(dev, "%s: returning cmd_rc=%d.", __func__, cmd_rc); |
| 470 | return cmd_rc; |
| 471 | } |
| 472 | |
| 473 | static const struct i2c_algorithm i2c_via_ipmi_algo = { |
| 474 | .functionality = i2c_via_ipmi_functionality, |
| 475 | .master_xfer = i2c_via_ipmi_xfer, |
| 476 | }; |
| 477 | |
| 478 | static int i2c_via_ipmi_create(struct platform_device *pdev, |
| 479 | const char *name, |
| 480 | unsigned bmc_bus_id, |
| 481 | struct i2c_via_ipmi_bus **new_bus) |
| 482 | { |
| 483 | struct device *parent = &pdev->dev; |
| 484 | struct i2c_via_ipmi_bus *bus = NULL; |
| 485 | int rc; |
| 486 | |
| 487 | bus = devm_kzalloc(parent, sizeof(*bus), GFP_KERNEL); |
| 488 | if (!bus) |
| 489 | return -ENOMEM; |
| 490 | bus->magic = I2C_VIA_IPMI_BUS_MAGIC; |
| 491 | INIT_LIST_HEAD(&bus->node); |
| 492 | bus->pdev = pdev; |
| 493 | bus->remote_bus_id = bmc_bus_id; |
| 494 | spin_lock_init(&bus->lock); |
| 495 | bus->ipmi_seq = 0x40000000L; |
| 496 | init_completion(&bus->cmd_complete); |
| 497 | bus->req_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 498 | bus->req_addr.channel = IPMI_BMC_CHANNEL; |
| 499 | dev_dbg(parent, "%s: name=%s, bmc_bus_id=%u", |
| 500 | __func__, name, bmc_bus_id); |
| 501 | rc = ipmi_create_user(bus->if_num, &i2c_via_ipmi_ipmi_handlers, |
| 502 | bus, &bus->user); |
| 503 | if (rc) { |
| 504 | dev_err(parent, "%s: ipmi_create_user(%d, ...) returned %d.", |
| 505 | __func__, bus->if_num, rc); |
| 506 | goto free_instance; |
| 507 | } |
| 508 | |
| 509 | bus->adap.owner = THIS_MODULE; |
| 510 | strncpy(bus->adap.name, name, sizeof(bus->adap.name)); |
| 511 | bus->adap.retries = 0; |
| 512 | /* The BMC's i2c timeout is (and likely will remain) 5s, therefore |
| 513 | * this host timeout should be set longer to avoid it timing out |
| 514 | * ahead of the BMC's chance. |
| 515 | */ |
| 516 | bus->adap.timeout = 10 * HZ; |
| 517 | bus->adap.algo = &i2c_via_ipmi_algo; |
| 518 | bus->adap.algo_data = bus; |
| 519 | if (pdev) |
| 520 | bus->adap.dev.parent = &pdev->dev; |
| 521 | rc = i2c_add_adapter(&bus->adap); |
| 522 | if (rc < 0) { |
| 523 | dev_err(parent, "%s: i2c_add_adapter(%d, ...) returned %d.", |
| 524 | __func__, bmc_bus_id, rc); |
| 525 | goto free_instance; |
| 526 | } |
| 527 | |
| 528 | list_add_tail(&bus->node, &proxies); |
| 529 | if (new_bus) |
| 530 | *new_bus = bus; |
| 531 | return 0; |
| 532 | |
| 533 | free_instance: |
| 534 | devm_kfree(parent, bus); |
| 535 | return rc; |
| 536 | } |
| 537 | |
| 538 | static int i2c_via_ipmi_destroy(struct i2c_via_ipmi_bus *bus) |
| 539 | { |
| 540 | struct device *parent = &bus->pdev->dev; |
| 541 | |
| 542 | dev_dbg(parent, "%s: name=%s, bmc_bus_id=%u", |
| 543 | __func__, bus->adap.name, bus->remote_bus_id); |
| 544 | i2c_del_adapter(&bus->adap); |
| 545 | list_del(&bus->node); |
| 546 | devm_kfree(parent, bus); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int i2c_via_ipmi_probe(struct platform_device *pdev) |
| 551 | { |
| 552 | char name[ADAP_NAME_MAX]; |
| 553 | unsigned bus_id; |
| 554 | int rc; |
| 555 | |
| 556 | /* |
| 557 | * Create requested population of proxies. |
| 558 | */ |
| 559 | for (bus_id = 0; bus_id < CONFIG_I2C_VIA_IPMI_AUTOPROXIES; ++bus_id) { |
| 560 | snprintf(name, sizeof(name), "bmc%u", bus_id); |
| 561 | rc = i2c_via_ipmi_create(pdev, name, bus_id, NULL); |
| 562 | if (rc) |
| 563 | return rc; |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int i2c_via_ipmi_remove(struct platform_device *pdev) |
| 569 | { |
| 570 | struct i2c_via_ipmi_bus *bus, *safe; |
| 571 | int pass = 0; |
| 572 | |
| 573 | /* |
| 574 | * Destroy all proxies in reverse order. |
| 575 | */ |
| 576 | list_for_each_entry_safe_reverse(bus, safe, &proxies, node) { |
| 577 | i2c_via_ipmi_destroy(bus); |
| 578 | ++pass; |
| 579 | } |
| 580 | dev_dbg(&pdev->dev, "%s: removed %d proxies.", __func__, pass); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static void i2c_via_ipmi_release(struct device *__unused) |
| 585 | { |
| 586 | /* |
| 587 | * This function releases the resources associated with a device. Since |
| 588 | * our device struct is statically allocated, we don't need to do |
| 589 | * anything here. |
| 590 | */ |
| 591 | } |
| 592 | |
| 593 | static struct platform_driver i2c_via_ipmi_driver = { |
| 594 | .driver = { |
| 595 | .name = "i2c-via-ipmi", |
| 596 | }, |
| 597 | .probe = i2c_via_ipmi_probe, |
| 598 | .remove = i2c_via_ipmi_remove, |
| 599 | }; |
| 600 | |
| 601 | static struct platform_device i2c_via_ipmi_pdev = { |
| 602 | .name = "i2c-via-ipmi", |
| 603 | .id = PLATFORM_DEVID_NONE, |
| 604 | .dev.release = i2c_via_ipmi_release, |
| 605 | }; |
| 606 | |
| 607 | int i2c_via_ipmi_init(void) |
| 608 | { |
| 609 | int rc; |
| 610 | |
| 611 | pr_debug("%s: entering.", __func__); |
| 612 | rc = platform_driver_register(&i2c_via_ipmi_driver); |
| 613 | if (rc) { |
| 614 | pr_err("couldn't register i2c-via-ipmi platform driver\n"); |
| 615 | return rc; |
| 616 | } |
| 617 | rc = platform_device_register(&i2c_via_ipmi_pdev); |
| 618 | if (rc) { |
| 619 | pr_err("couldn't register i2c-via-ipmi platform device\n"); |
| 620 | goto unregister_pdrv; |
| 621 | } |
| 622 | pr_debug("%s: returning 0.", __func__); |
| 623 | return 0; |
| 624 | |
| 625 | unregister_pdrv: |
| 626 | platform_driver_unregister(&i2c_via_ipmi_driver); |
| 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | void i2c_via_ipmi_exit(void) |
| 631 | { |
| 632 | pr_debug("%s: entering.", __func__); |
| 633 | platform_device_unregister(&i2c_via_ipmi_pdev); |
| 634 | platform_driver_unregister(&i2c_via_ipmi_driver); |
| 635 | pr_debug("%s: returning.", __func__); |
| 636 | } |
| 637 | |
| 638 | subsys_initcall(i2c_via_ipmi_init); |
| 639 | module_exit(i2c_via_ipmi_exit); |
| 640 | |
| 641 | MODULE_AUTHOR("Peter Hanson <peterh@google.com>"); |
| 642 | MODULE_DESCRIPTION("I2C Bus driver proxy for BMC I2C bus access."); |
| 643 | MODULE_LICENSE("GPL v2"); |