0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/acpi.h>
0012 #include <linux/errno.h>
0013 #include <linux/i2c.h>
0014 #include <linux/miscdevice.h>
0015 #include <linux/module.h>
0016 #include <linux/mutex.h>
0017 #include <linux/poll.h>
0018 #include <linux/slab.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/wait.h>
0021
0022 #define MAX_MSG_LEN 240
0023 #define IPMB_REQUEST_LEN_MIN 7
0024 #define NETFN_RSP_BIT_MASK 0x4
0025 #define REQUEST_QUEUE_MAX_LEN 256
0026
0027 #define IPMB_MSG_LEN_IDX 0
0028 #define RQ_SA_8BIT_IDX 1
0029 #define NETFN_LUN_IDX 2
0030
0031 #define GET_7BIT_ADDR(addr_8bit) (addr_8bit >> 1)
0032 #define GET_8BIT_ADDR(addr_7bit) ((addr_7bit << 1) & 0xff)
0033
0034 #define IPMB_MSG_PAYLOAD_LEN_MAX (MAX_MSG_LEN - IPMB_REQUEST_LEN_MIN - 1)
0035
0036 #define SMBUS_MSG_HEADER_LENGTH 2
0037 #define SMBUS_MSG_IDX_OFFSET (SMBUS_MSG_HEADER_LENGTH + 1)
0038
0039 struct ipmb_msg {
0040 u8 len;
0041 u8 rs_sa;
0042 u8 netfn_rs_lun;
0043 u8 checksum1;
0044 u8 rq_sa;
0045 u8 rq_seq_rq_lun;
0046 u8 cmd;
0047 u8 payload[IPMB_MSG_PAYLOAD_LEN_MAX];
0048
0049 } __packed;
0050
0051 struct ipmb_request_elem {
0052 struct list_head list;
0053 struct ipmb_msg request;
0054 };
0055
0056 struct ipmb_dev {
0057 struct i2c_client *client;
0058 struct miscdevice miscdev;
0059 struct ipmb_msg request;
0060 struct list_head request_queue;
0061 atomic_t request_queue_len;
0062 size_t msg_idx;
0063 spinlock_t lock;
0064 wait_queue_head_t wait_queue;
0065 struct mutex file_mutex;
0066 bool is_i2c_protocol;
0067 };
0068
0069 static inline struct ipmb_dev *to_ipmb_dev(struct file *file)
0070 {
0071 return container_of(file->private_data, struct ipmb_dev, miscdev);
0072 }
0073
0074 static ssize_t ipmb_read(struct file *file, char __user *buf, size_t count,
0075 loff_t *ppos)
0076 {
0077 struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
0078 struct ipmb_request_elem *queue_elem;
0079 struct ipmb_msg msg;
0080 ssize_t ret = 0;
0081
0082 memset(&msg, 0, sizeof(msg));
0083
0084 spin_lock_irq(&ipmb_dev->lock);
0085
0086 while (list_empty(&ipmb_dev->request_queue)) {
0087 spin_unlock_irq(&ipmb_dev->lock);
0088
0089 if (file->f_flags & O_NONBLOCK)
0090 return -EAGAIN;
0091
0092 ret = wait_event_interruptible(ipmb_dev->wait_queue,
0093 !list_empty(&ipmb_dev->request_queue));
0094 if (ret)
0095 return ret;
0096
0097 spin_lock_irq(&ipmb_dev->lock);
0098 }
0099
0100 queue_elem = list_first_entry(&ipmb_dev->request_queue,
0101 struct ipmb_request_elem, list);
0102 memcpy(&msg, &queue_elem->request, sizeof(msg));
0103 list_del(&queue_elem->list);
0104 kfree(queue_elem);
0105 atomic_dec(&ipmb_dev->request_queue_len);
0106
0107 spin_unlock_irq(&ipmb_dev->lock);
0108
0109 count = min_t(size_t, count, msg.len + 1);
0110 if (copy_to_user(buf, &msg, count))
0111 ret = -EFAULT;
0112
0113 return ret < 0 ? ret : count;
0114 }
0115
0116 static int ipmb_i2c_write(struct i2c_client *client, u8 *msg, u8 addr)
0117 {
0118 struct i2c_msg i2c_msg;
0119
0120
0121
0122
0123
0124 i2c_msg.len = msg[IPMB_MSG_LEN_IDX] - 1;
0125
0126
0127 i2c_msg.buf = msg + 2;
0128
0129 i2c_msg.addr = addr;
0130 i2c_msg.flags = client->flags & I2C_CLIENT_PEC;
0131
0132 return i2c_transfer(client->adapter, &i2c_msg, 1);
0133 }
0134
0135 static ssize_t ipmb_write(struct file *file, const char __user *buf,
0136 size_t count, loff_t *ppos)
0137 {
0138 struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
0139 u8 rq_sa, netf_rq_lun, msg_len;
0140 struct i2c_client *temp_client;
0141 u8 msg[MAX_MSG_LEN];
0142 ssize_t ret;
0143
0144 if (count > sizeof(msg))
0145 return -EINVAL;
0146
0147 if (copy_from_user(&msg, buf, count))
0148 return -EFAULT;
0149
0150 if (count < msg[0])
0151 return -EINVAL;
0152
0153 rq_sa = GET_7BIT_ADDR(msg[RQ_SA_8BIT_IDX]);
0154 netf_rq_lun = msg[NETFN_LUN_IDX];
0155
0156
0157 if (ipmb_dev->is_i2c_protocol) {
0158 ret = ipmb_i2c_write(ipmb_dev->client, msg, rq_sa);
0159 return (ret == 1) ? count : ret;
0160 }
0161
0162
0163
0164
0165
0166 msg_len = msg[IPMB_MSG_LEN_IDX] - SMBUS_MSG_HEADER_LENGTH;
0167 temp_client = kmemdup(ipmb_dev->client, sizeof(*temp_client), GFP_KERNEL);
0168 if (!temp_client)
0169 return -ENOMEM;
0170
0171 temp_client->addr = rq_sa;
0172
0173 ret = i2c_smbus_write_block_data(temp_client, netf_rq_lun, msg_len,
0174 msg + SMBUS_MSG_IDX_OFFSET);
0175 kfree(temp_client);
0176
0177 return ret < 0 ? ret : count;
0178 }
0179
0180 static __poll_t ipmb_poll(struct file *file, poll_table *wait)
0181 {
0182 struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
0183 __poll_t mask = EPOLLOUT;
0184
0185 mutex_lock(&ipmb_dev->file_mutex);
0186 poll_wait(file, &ipmb_dev->wait_queue, wait);
0187
0188 if (atomic_read(&ipmb_dev->request_queue_len))
0189 mask |= EPOLLIN;
0190 mutex_unlock(&ipmb_dev->file_mutex);
0191
0192 return mask;
0193 }
0194
0195 static const struct file_operations ipmb_fops = {
0196 .owner = THIS_MODULE,
0197 .read = ipmb_read,
0198 .write = ipmb_write,
0199 .poll = ipmb_poll,
0200 };
0201
0202
0203 static void ipmb_handle_request(struct ipmb_dev *ipmb_dev)
0204 {
0205 struct ipmb_request_elem *queue_elem;
0206
0207 if (atomic_read(&ipmb_dev->request_queue_len) >=
0208 REQUEST_QUEUE_MAX_LEN)
0209 return;
0210
0211 queue_elem = kmalloc(sizeof(*queue_elem), GFP_ATOMIC);
0212 if (!queue_elem)
0213 return;
0214
0215 memcpy(&queue_elem->request, &ipmb_dev->request,
0216 sizeof(struct ipmb_msg));
0217 list_add(&queue_elem->list, &ipmb_dev->request_queue);
0218 atomic_inc(&ipmb_dev->request_queue_len);
0219 wake_up_all(&ipmb_dev->wait_queue);
0220 }
0221
0222 static u8 ipmb_verify_checksum1(struct ipmb_dev *ipmb_dev, u8 rs_sa)
0223 {
0224
0225 return (rs_sa + ipmb_dev->request.netfn_rs_lun +
0226 ipmb_dev->request.checksum1);
0227 }
0228
0229
0230
0231
0232
0233 static bool is_ipmb_msg(struct ipmb_dev *ipmb_dev, u8 rs_sa)
0234 {
0235 if ((ipmb_dev->msg_idx >= IPMB_REQUEST_LEN_MIN) &&
0236 (!ipmb_verify_checksum1(ipmb_dev, rs_sa)))
0237 return true;
0238
0239 return false;
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249 static int ipmb_slave_cb(struct i2c_client *client,
0250 enum i2c_slave_event event, u8 *val)
0251 {
0252 struct ipmb_dev *ipmb_dev = i2c_get_clientdata(client);
0253 u8 *buf = (u8 *)&ipmb_dev->request;
0254 unsigned long flags;
0255
0256 spin_lock_irqsave(&ipmb_dev->lock, flags);
0257 switch (event) {
0258 case I2C_SLAVE_WRITE_REQUESTED:
0259 memset(&ipmb_dev->request, 0, sizeof(ipmb_dev->request));
0260 ipmb_dev->msg_idx = 0;
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278 buf[++ipmb_dev->msg_idx] = GET_8BIT_ADDR(client->addr);
0279 break;
0280
0281 case I2C_SLAVE_WRITE_RECEIVED:
0282 if (ipmb_dev->msg_idx >= sizeof(struct ipmb_msg) - 1)
0283 break;
0284
0285 buf[++ipmb_dev->msg_idx] = *val;
0286 break;
0287
0288 case I2C_SLAVE_STOP:
0289 ipmb_dev->request.len = ipmb_dev->msg_idx;
0290 if (is_ipmb_msg(ipmb_dev, GET_8BIT_ADDR(client->addr)))
0291 ipmb_handle_request(ipmb_dev);
0292 break;
0293
0294 default:
0295 break;
0296 }
0297 spin_unlock_irqrestore(&ipmb_dev->lock, flags);
0298
0299 return 0;
0300 }
0301
0302 static int ipmb_probe(struct i2c_client *client)
0303 {
0304 struct ipmb_dev *ipmb_dev;
0305 int ret;
0306
0307 ipmb_dev = devm_kzalloc(&client->dev, sizeof(*ipmb_dev),
0308 GFP_KERNEL);
0309 if (!ipmb_dev)
0310 return -ENOMEM;
0311
0312 spin_lock_init(&ipmb_dev->lock);
0313 init_waitqueue_head(&ipmb_dev->wait_queue);
0314 atomic_set(&ipmb_dev->request_queue_len, 0);
0315 INIT_LIST_HEAD(&ipmb_dev->request_queue);
0316
0317 mutex_init(&ipmb_dev->file_mutex);
0318
0319 ipmb_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
0320
0321 ipmb_dev->miscdev.name = devm_kasprintf(&client->dev, GFP_KERNEL,
0322 "%s%d", "ipmb-",
0323 client->adapter->nr);
0324 ipmb_dev->miscdev.fops = &ipmb_fops;
0325 ipmb_dev->miscdev.parent = &client->dev;
0326 ret = misc_register(&ipmb_dev->miscdev);
0327 if (ret)
0328 return ret;
0329
0330 ipmb_dev->is_i2c_protocol
0331 = device_property_read_bool(&client->dev, "i2c-protocol");
0332
0333 ipmb_dev->client = client;
0334 i2c_set_clientdata(client, ipmb_dev);
0335 ret = i2c_slave_register(client, ipmb_slave_cb);
0336 if (ret) {
0337 misc_deregister(&ipmb_dev->miscdev);
0338 return ret;
0339 }
0340
0341 return 0;
0342 }
0343
0344 static int ipmb_remove(struct i2c_client *client)
0345 {
0346 struct ipmb_dev *ipmb_dev = i2c_get_clientdata(client);
0347
0348 i2c_slave_unregister(client);
0349 misc_deregister(&ipmb_dev->miscdev);
0350
0351 return 0;
0352 }
0353
0354 static const struct i2c_device_id ipmb_id[] = {
0355 { "ipmb-dev", 0 },
0356 {},
0357 };
0358 MODULE_DEVICE_TABLE(i2c, ipmb_id);
0359
0360 static const struct acpi_device_id acpi_ipmb_id[] = {
0361 { "IPMB0001", 0 },
0362 {},
0363 };
0364 MODULE_DEVICE_TABLE(acpi, acpi_ipmb_id);
0365
0366 static struct i2c_driver ipmb_driver = {
0367 .driver = {
0368 .name = "ipmb-dev",
0369 .acpi_match_table = ACPI_PTR(acpi_ipmb_id),
0370 },
0371 .probe_new = ipmb_probe,
0372 .remove = ipmb_remove,
0373 .id_table = ipmb_id,
0374 };
0375 module_i2c_driver(ipmb_driver);
0376
0377 MODULE_AUTHOR("Mellanox Technologies");
0378 MODULE_DESCRIPTION("IPMB driver");
0379 MODULE_LICENSE("GPL v2");