Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
| 4 | * Copyright (c) 2003-2012, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/mei.h> |
| 21 | |
| 22 | #include "mei_dev.h" |
| 23 | #include "interface.h" |
| 24 | |
| 25 | /** |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 26 | * mei_hbm_cl_hdr - construct client hbm header |
| 27 | * @cl: - client |
| 28 | * @hbm_cmd: host bus message command |
| 29 | * @buf: buffer for cl header |
| 30 | * @len: buffer length |
| 31 | */ |
| 32 | static inline |
| 33 | void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len) |
| 34 | { |
| 35 | struct mei_hbm_cl_cmd *cmd = buf; |
| 36 | |
| 37 | memset(cmd, 0, len); |
| 38 | |
| 39 | cmd->hbm_cmd = hbm_cmd; |
| 40 | cmd->host_addr = cl->host_client_id; |
| 41 | cmd->me_addr = cl->me_client_id; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * same_disconn_addr - tells if they have the same address |
| 46 | * |
| 47 | * @file: private data of the file object. |
| 48 | * @disconn: disconnection request. |
| 49 | * |
| 50 | * returns true if addres are same |
| 51 | */ |
| 52 | static inline |
| 53 | bool mei_hbm_cl_addr_equal(struct mei_cl *cl, void *buf) |
| 54 | { |
| 55 | struct mei_hbm_cl_cmd *cmd = buf; |
| 56 | return cl->host_client_id == cmd->host_addr && |
| 57 | cl->me_client_id == cmd->me_addr; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 62 | * host_start_message - mei host sends start message. |
| 63 | * |
| 64 | * @dev: the device structure |
| 65 | * |
| 66 | * returns none. |
| 67 | */ |
| 68 | void mei_host_start_message(struct mei_device *dev) |
| 69 | { |
| 70 | struct mei_msg_hdr *mei_hdr; |
| 71 | struct hbm_host_version_request *start_req; |
| 72 | const size_t len = sizeof(struct hbm_host_version_request); |
| 73 | |
| 74 | mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
| 75 | |
| 76 | /* host start message */ |
| 77 | start_req = (struct hbm_host_version_request *)&dev->wr_msg_buf[1]; |
| 78 | memset(start_req, 0, len); |
| 79 | start_req->hbm_cmd = HOST_START_REQ_CMD; |
| 80 | start_req->host_version.major_version = HBM_MAJOR_VERSION; |
| 81 | start_req->host_version.minor_version = HBM_MINOR_VERSION; |
| 82 | |
| 83 | dev->recvd_msg = false; |
| 84 | if (mei_write_message(dev, mei_hdr, (unsigned char *)start_req)) { |
| 85 | dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n"); |
| 86 | dev->dev_state = MEI_DEV_RESETING; |
| 87 | mei_reset(dev, 1); |
| 88 | } |
| 89 | dev->init_clients_state = MEI_START_MESSAGE; |
| 90 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; |
| 91 | return ; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * host_enum_clients_message - host sends enumeration client request message. |
| 96 | * |
| 97 | * @dev: the device structure |
| 98 | * |
| 99 | * returns none. |
| 100 | */ |
| 101 | void mei_host_enum_clients_message(struct mei_device *dev) |
| 102 | { |
| 103 | struct mei_msg_hdr *mei_hdr; |
| 104 | struct hbm_host_enum_request *enum_req; |
| 105 | const size_t len = sizeof(struct hbm_host_enum_request); |
| 106 | /* enumerate clients */ |
| 107 | mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
| 108 | |
| 109 | enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1]; |
| 110 | memset(enum_req, 0, sizeof(struct hbm_host_enum_request)); |
| 111 | enum_req->hbm_cmd = HOST_ENUM_REQ_CMD; |
| 112 | |
| 113 | if (mei_write_message(dev, mei_hdr, (unsigned char *)enum_req)) { |
| 114 | dev->dev_state = MEI_DEV_RESETING; |
| 115 | dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n"); |
| 116 | mei_reset(dev, 1); |
| 117 | } |
| 118 | dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE; |
| 119 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | int mei_host_client_enumerate(struct mei_device *dev) |
| 125 | { |
| 126 | |
| 127 | struct mei_msg_hdr *mei_hdr; |
| 128 | struct hbm_props_request *prop_req; |
| 129 | const size_t len = sizeof(struct hbm_props_request); |
| 130 | unsigned long next_client_index; |
| 131 | u8 client_num; |
| 132 | |
| 133 | |
| 134 | client_num = dev->me_client_presentation_num; |
| 135 | |
| 136 | next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, |
| 137 | dev->me_client_index); |
| 138 | |
| 139 | /* We got all client properties */ |
| 140 | if (next_client_index == MEI_CLIENTS_MAX) { |
| 141 | schedule_work(&dev->init_work); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | dev->me_clients[client_num].client_id = next_client_index; |
| 147 | dev->me_clients[client_num].mei_flow_ctrl_creds = 0; |
| 148 | |
| 149 | mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
| 150 | prop_req = (struct hbm_props_request *)&dev->wr_msg_buf[1]; |
| 151 | |
| 152 | memset(prop_req, 0, sizeof(struct hbm_props_request)); |
| 153 | |
| 154 | |
| 155 | prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD; |
| 156 | prop_req->address = next_client_index; |
| 157 | |
| 158 | if (mei_write_message(dev, mei_hdr, (unsigned char *) prop_req)) { |
| 159 | dev->dev_state = MEI_DEV_RESETING; |
| 160 | dev_err(&dev->pdev->dev, "Properties request command failed\n"); |
| 161 | mei_reset(dev, 1); |
| 162 | |
| 163 | return -EIO; |
| 164 | } |
| 165 | |
| 166 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; |
| 167 | dev->me_client_index = next_client_index; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * mei_send_flow_control - sends flow control to fw. |
| 174 | * |
| 175 | * @dev: the device structure |
| 176 | * @cl: private data of the file object |
| 177 | * |
| 178 | * This function returns -EIO on write failure |
| 179 | */ |
| 180 | int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl) |
| 181 | { |
| 182 | struct mei_msg_hdr *mei_hdr; |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 183 | unsigned char *buf = (unsigned char *)&dev->wr_msg_buf[1]; |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 184 | const size_t len = sizeof(struct hbm_flow_control); |
| 185 | |
| 186 | mei_hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 187 | mei_hbm_cl_hdr(cl, MEI_FLOW_CONTROL_CMD, buf, len); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 188 | |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 189 | dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n", |
| 190 | cl->host_client_id, cl->me_client_id); |
| 191 | |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 192 | return mei_write_message(dev, mei_hdr, buf); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * mei_disconnect - sends disconnect message to fw. |
| 197 | * |
| 198 | * @dev: the device structure |
| 199 | * @cl: private data of the file object |
| 200 | * |
| 201 | * This function returns -EIO on write failure |
| 202 | */ |
| 203 | int mei_disconnect(struct mei_device *dev, struct mei_cl *cl) |
| 204 | { |
| 205 | struct mei_msg_hdr *hdr; |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 206 | unsigned char *buf = (unsigned char *)&dev->wr_msg_buf[1]; |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 207 | const size_t len = sizeof(struct hbm_client_connect_request); |
| 208 | |
| 209 | hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 210 | mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, buf, len); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 211 | |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 212 | return mei_write_message(dev, hdr, buf); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * mei_connect - sends connect message to fw. |
| 217 | * |
| 218 | * @dev: the device structure |
| 219 | * @cl: private data of the file object |
| 220 | * |
| 221 | * This function returns -EIO on write failure |
| 222 | */ |
| 223 | int mei_connect(struct mei_device *dev, struct mei_cl *cl) |
| 224 | { |
| 225 | struct mei_msg_hdr *hdr; |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 226 | unsigned char *buf = (unsigned char *)&dev->wr_msg_buf[1]; |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 227 | const size_t len = sizeof(struct hbm_client_connect_request); |
| 228 | |
| 229 | hdr = mei_hbm_hdr(&dev->wr_msg_buf[0], len); |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 230 | mei_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, buf, len); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 231 | |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 232 | return mei_write_message(dev, hdr, buf); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * mei_client_disconnect_request - disconnects from request irq routine |
| 237 | * |
| 238 | * @dev: the device structure. |
| 239 | * @disconnect_req: disconnect request bus message. |
| 240 | */ |
| 241 | static void mei_client_disconnect_request(struct mei_device *dev, |
| 242 | struct hbm_client_connect_request *disconnect_req) |
| 243 | { |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 244 | struct mei_cl *cl, *next; |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 245 | const size_t len = sizeof(struct hbm_client_connect_response); |
| 246 | |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 247 | list_for_each_entry_safe(cl, next, &dev->file_list, link) { |
| 248 | if (mei_hbm_cl_addr_equal(cl, disconnect_req)) { |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 249 | dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n", |
| 250 | disconnect_req->host_addr, |
| 251 | disconnect_req->me_addr); |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 252 | cl->state = MEI_FILE_DISCONNECTED; |
| 253 | cl->timer_count = 0; |
| 254 | if (cl == &dev->wd_cl) |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 255 | dev->wd_pending = false; |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 256 | else if (cl == &dev->iamthif_cl) |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 257 | dev->iamthif_timer = 0; |
| 258 | |
| 259 | /* prepare disconnect response */ |
| 260 | (void)mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len); |
Tomas Winkler | cd51ed6 | 2012-12-25 19:06:09 +0200 | [diff] [blame^] | 261 | mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, |
| 262 | &dev->wr_ext_msg.data, len); |
Tomas Winkler | bb1b013 | 2012-12-25 19:06:07 +0200 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * mei_hbm_dispatch - bottom half read routine after ISR to |
| 271 | * handle the read bus message cmd processing. |
| 272 | * |
| 273 | * @dev: the device structure |
| 274 | * @mei_hdr: header of bus message |
| 275 | */ |
| 276 | void mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr) |
| 277 | { |
| 278 | struct mei_bus_message *mei_msg; |
| 279 | struct mei_me_client *me_client; |
| 280 | struct hbm_host_version_response *version_res; |
| 281 | struct hbm_client_connect_response *connect_res; |
| 282 | struct hbm_client_connect_response *disconnect_res; |
| 283 | struct hbm_client_connect_request *disconnect_req; |
| 284 | struct hbm_flow_control *flow_control; |
| 285 | struct hbm_props_response *props_res; |
| 286 | struct hbm_host_enum_response *enum_res; |
| 287 | struct hbm_host_stop_request *stop_req; |
| 288 | |
| 289 | /* read the message to our buffer */ |
| 290 | BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf)); |
| 291 | mei_read_slots(dev, dev->rd_msg_buf, hdr->length); |
| 292 | mei_msg = (struct mei_bus_message *)dev->rd_msg_buf; |
| 293 | |
| 294 | switch (mei_msg->hbm_cmd) { |
| 295 | case HOST_START_RES_CMD: |
| 296 | version_res = (struct hbm_host_version_response *)mei_msg; |
| 297 | if (version_res->host_version_supported) { |
| 298 | dev->version.major_version = HBM_MAJOR_VERSION; |
| 299 | dev->version.minor_version = HBM_MINOR_VERSION; |
| 300 | if (dev->dev_state == MEI_DEV_INIT_CLIENTS && |
| 301 | dev->init_clients_state == MEI_START_MESSAGE) { |
| 302 | dev->init_clients_timer = 0; |
| 303 | mei_host_enum_clients_message(dev); |
| 304 | } else { |
| 305 | dev->recvd_msg = false; |
| 306 | dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n"); |
| 307 | mei_reset(dev, 1); |
| 308 | return; |
| 309 | } |
| 310 | } else { |
| 311 | u32 *buf = dev->wr_msg_buf; |
| 312 | const size_t len = sizeof(struct hbm_host_stop_request); |
| 313 | |
| 314 | dev->version = version_res->me_max_version; |
| 315 | |
| 316 | /* send stop message */ |
| 317 | hdr = mei_hbm_hdr(&buf[0], len); |
| 318 | stop_req = (struct hbm_host_stop_request *)&buf[1]; |
| 319 | memset(stop_req, 0, len); |
| 320 | stop_req->hbm_cmd = HOST_STOP_REQ_CMD; |
| 321 | stop_req->reason = DRIVER_STOP_REQUEST; |
| 322 | |
| 323 | mei_write_message(dev, hdr, (unsigned char *)stop_req); |
| 324 | dev_dbg(&dev->pdev->dev, "version mismatch.\n"); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | dev->recvd_msg = true; |
| 329 | dev_dbg(&dev->pdev->dev, "host start response message received.\n"); |
| 330 | break; |
| 331 | |
| 332 | case CLIENT_CONNECT_RES_CMD: |
| 333 | connect_res = (struct hbm_client_connect_response *) mei_msg; |
| 334 | mei_client_connect_response(dev, connect_res); |
| 335 | dev_dbg(&dev->pdev->dev, "client connect response message received.\n"); |
| 336 | wake_up(&dev->wait_recvd_msg); |
| 337 | break; |
| 338 | |
| 339 | case CLIENT_DISCONNECT_RES_CMD: |
| 340 | disconnect_res = (struct hbm_client_connect_response *) mei_msg; |
| 341 | mei_client_disconnect_response(dev, disconnect_res); |
| 342 | dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n"); |
| 343 | wake_up(&dev->wait_recvd_msg); |
| 344 | break; |
| 345 | |
| 346 | case MEI_FLOW_CONTROL_CMD: |
| 347 | flow_control = (struct hbm_flow_control *) mei_msg; |
| 348 | mei_client_flow_control_response(dev, flow_control); |
| 349 | dev_dbg(&dev->pdev->dev, "client flow control response message received.\n"); |
| 350 | break; |
| 351 | |
| 352 | case HOST_CLIENT_PROPERTIES_RES_CMD: |
| 353 | props_res = (struct hbm_props_response *)mei_msg; |
| 354 | me_client = &dev->me_clients[dev->me_client_presentation_num]; |
| 355 | |
| 356 | if (props_res->status || !dev->me_clients) { |
| 357 | dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n"); |
| 358 | mei_reset(dev, 1); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | if (me_client->client_id != props_res->address) { |
| 363 | dev_err(&dev->pdev->dev, |
| 364 | "Host client properties reply mismatch\n"); |
| 365 | mei_reset(dev, 1); |
| 366 | |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | if (dev->dev_state != MEI_DEV_INIT_CLIENTS || |
| 371 | dev->init_clients_state != MEI_CLIENT_PROPERTIES_MESSAGE) { |
| 372 | dev_err(&dev->pdev->dev, |
| 373 | "Unexpected client properties reply\n"); |
| 374 | mei_reset(dev, 1); |
| 375 | |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | me_client->props = props_res->client_properties; |
| 380 | dev->me_client_index++; |
| 381 | dev->me_client_presentation_num++; |
| 382 | |
| 383 | mei_host_client_enumerate(dev); |
| 384 | |
| 385 | break; |
| 386 | |
| 387 | case HOST_ENUM_RES_CMD: |
| 388 | enum_res = (struct hbm_host_enum_response *) mei_msg; |
| 389 | memcpy(dev->me_clients_map, enum_res->valid_addresses, 32); |
| 390 | if (dev->dev_state == MEI_DEV_INIT_CLIENTS && |
| 391 | dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) { |
| 392 | dev->init_clients_timer = 0; |
| 393 | dev->me_client_presentation_num = 0; |
| 394 | dev->me_client_index = 0; |
| 395 | mei_allocate_me_clients_storage(dev); |
| 396 | dev->init_clients_state = |
| 397 | MEI_CLIENT_PROPERTIES_MESSAGE; |
| 398 | |
| 399 | mei_host_client_enumerate(dev); |
| 400 | } else { |
| 401 | dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n"); |
| 402 | mei_reset(dev, 1); |
| 403 | return; |
| 404 | } |
| 405 | break; |
| 406 | |
| 407 | case HOST_STOP_RES_CMD: |
| 408 | dev->dev_state = MEI_DEV_DISABLED; |
| 409 | dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n"); |
| 410 | mei_reset(dev, 1); |
| 411 | break; |
| 412 | |
| 413 | case CLIENT_DISCONNECT_REQ_CMD: |
| 414 | /* search for client */ |
| 415 | disconnect_req = (struct hbm_client_connect_request *)mei_msg; |
| 416 | mei_client_disconnect_request(dev, disconnect_req); |
| 417 | break; |
| 418 | |
| 419 | case ME_STOP_REQ_CMD: |
| 420 | { |
| 421 | /* prepare stop request: sent in next interrupt event */ |
| 422 | |
| 423 | const size_t len = sizeof(struct hbm_host_stop_request); |
| 424 | |
| 425 | hdr = mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len); |
| 426 | stop_req = (struct hbm_host_stop_request *)&dev->wr_ext_msg.data; |
| 427 | memset(stop_req, 0, len); |
| 428 | stop_req->hbm_cmd = HOST_STOP_REQ_CMD; |
| 429 | stop_req->reason = DRIVER_STOP_REQUEST; |
| 430 | break; |
| 431 | } |
| 432 | default: |
| 433 | BUG(); |
| 434 | break; |
| 435 | |
| 436 | } |
| 437 | } |
| 438 | |