| 1 | |
| 2 | |
| 3 | * IBM Power Systems Virtual Management Channel Support.
|
| 4 | *
|
| 5 | * Copyright (c) 2004, 2018 IBM Corp.
|
| 6 | * Dave Engebretsen [email protected]
|
| 7 | * Steven Royer [email protected]
|
| 8 | * Adam Reznechek [email protected]
|
| 9 | * Bryant G. Ly <[email protected]>
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/kernel.h>
|
| 14 | #include <linux/kthread.h>
|
| 15 | #include <linux/major.h>
|
| 16 | #include <linux/string.h>
|
| 17 | #include <linux/fcntl.h>
|
| 18 | #include <linux/slab.h>
|
| 19 | #include <linux/poll.h>
|
| 20 | #include <linux/init.h>
|
| 21 | #include <linux/fs.h>
|
| 22 | #include <linux/interrupt.h>
|
| 23 | #include <linux/spinlock.h>
|
| 24 | #include <linux/percpu.h>
|
| 25 | #include <linux/delay.h>
|
| 26 | #include <linux/uaccess.h>
|
| 27 | #include <linux/io.h>
|
| 28 | #include <linux/miscdevice.h>
|
| 29 | #include <linux/sched/signal.h>
|
| 30 |
|
| 31 | #include <asm/byteorder.h>
|
| 32 | #include <asm/irq.h>
|
| 33 | #include <asm/vio.h>
|
| 34 |
|
| 35 | #include "ibmvmc.h"
|
| 36 |
|
| 37 | #define IBMVMC_DRIVER_VERSION "1.0"
|
| 38 |
|
| 39 | |
| 40 | * Static global variables
|
| 41 |
|
| 42 | static DECLARE_WAIT_QUEUE_HEAD(ibmvmc_read_wait);
|
| 43 |
|
| 44 | static const char ibmvmc_driver_name[] = "ibmvmc";
|
| 45 |
|
| 46 | static struct ibmvmc_struct ibmvmc;
|
| 47 | static struct ibmvmc_hmc hmcs[MAX_HMCS];
|
| 48 | static struct crq_server_adapter ibmvmc_adapter;
|
| 49 |
|
| 50 | static int ibmvmc_max_buf_pool_size = DEFAULT_BUF_POOL_SIZE;
|
| 51 | static int ibmvmc_max_hmcs = DEFAULT_HMCS;
|
| 52 | static int ibmvmc_max_mtu = DEFAULT_MTU;
|
| 53 |
|
| 54 | static inline long h_copy_rdma(s64 length, u64 sliobn, u64 slioba,
|
| 55 | u64 dliobn, u64 dlioba)
|
| 56 | {
|
| 57 | long rc = 0;
|
| 58 |
|
| 59 |
|
| 60 | dma_wmb();
|
| 61 | pr_debug("ibmvmc: h_copy_rdma(0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
|
| 62 | length, sliobn, slioba, dliobn, dlioba);
|
| 63 | rc = plpar_hcall_norets(H_COPY_RDMA, length, sliobn, slioba,
|
| 64 | dliobn, dlioba);
|
| 65 | pr_debug("ibmvmc: h_copy_rdma rc = 0x%lx\n", rc);
|
| 66 |
|
| 67 | return rc;
|
| 68 | }
|
| 69 |
|
| 70 | static inline void h_free_crq(uint32_t unit_address)
|
| 71 | {
|
| 72 | long rc = 0;
|
| 73 |
|
| 74 | do {
|
| 75 | if (H_IS_LONG_BUSY(rc))
|
| 76 | msleep(get_longbusy_msecs(rc));
|
| 77 |
|
| 78 | rc = plpar_hcall_norets(H_FREE_CRQ, unit_address);
|
| 79 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
|
| 80 | }
|
| 81 |
|
| 82 | |
| 83 | * h_request_vmc: - request a hypervisor virtual management channel device
|
| 84 | * @vmc_index: drc index of the vmc device created
|
| 85 | *
|
| 86 | * Requests the hypervisor create a new virtual management channel device,
|
| 87 | * allowing this partition to send hypervisor virtualization control
|
| 88 | * commands.
|
| 89 | *
|
| 90 | * Return:
|
| 91 | * 0 - Success
|
| 92 | * Non-zero - Failure
|
| 93 |
|
| 94 | static inline long h_request_vmc(u32 *vmc_index)
|
| 95 | {
|
| 96 | long rc = 0;
|
| 97 | unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
|
| 98 |
|
| 99 | do {
|
| 100 | if (H_IS_LONG_BUSY(rc))
|
| 101 | msleep(get_longbusy_msecs(rc));
|
| 102 |
|
| 103 |
|
| 104 | rc = plpar_hcall(H_REQUEST_VMC, retbuf);
|
| 105 | pr_debug("ibmvmc: %s rc = 0x%lx\n", __func__, rc);
|
| 106 | *vmc_index = retbuf[0];
|
| 107 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
|
| 108 |
|
| 109 | return rc;
|
| 110 | }
|
| 111 |
|
| 112 |
|
| 113 | |
| 114 | * ibmvmc_handle_event: - Interrupt handler for crq events
|
| 115 | * @irq: number of irq to handle, not used
|
| 116 | * @dev_instance: crq_server_adapter that received interrupt
|
| 117 | *
|
| 118 | * Disables interrupts and schedules ibmvmc_task
|
| 119 | *
|
| 120 | * Always returns IRQ_HANDLED
|
| 121 |
|
| 122 | static irqreturn_t ibmvmc_handle_event(int irq, void *dev_instance)
|
| 123 | {
|
| 124 | struct crq_server_adapter *adapter =
|
| 125 | (struct crq_server_adapter *)dev_instance;
|
| 126 |
|
| 127 | vio_disable_interrupts(to_vio_dev(adapter->dev));
|
| 128 | tasklet_schedule(&adapter->work_task);
|
| 129 |
|
| 130 | return IRQ_HANDLED;
|
| 131 | }
|
| 132 |
|
| 133 | |
| 134 | * ibmvmc_release_crq_queue - Release CRQ Queue
|
| 135 | *
|
| 136 | * @adapter: crq_server_adapter struct
|
| 137 | *
|
| 138 | * Return:
|
| 139 | * 0 - Success
|
| 140 | * Non-Zero - Failure
|
| 141 |
|
| 142 | static void ibmvmc_release_crq_queue(struct crq_server_adapter *adapter)
|
| 143 | {
|
| 144 | struct vio_dev *vdev = to_vio_dev(adapter->dev);
|
| 145 | struct crq_queue *queue = &adapter->queue;
|
| 146 |
|
| 147 | free_irq(vdev->irq, (void *)adapter);
|
| 148 | tasklet_kill(&adapter->work_task);
|
| 149 |
|
| 150 | if (adapter->reset_task)
|
| 151 | kthread_stop(adapter->reset_task);
|
| 152 |
|
| 153 | h_free_crq(vdev->unit_address);
|
| 154 | dma_unmap_single(adapter->dev,
|
| 155 | queue->msg_token,
|
| 156 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
|
| 157 | free_page((unsigned long)queue->msgs);
|
| 158 | }
|
| 159 |
|
| 160 | |
| 161 | * ibmvmc_reset_crq_queue - Reset CRQ Queue
|
| 162 | *
|
| 163 | * @adapter: crq_server_adapter struct
|
| 164 | *
|
| 165 | * This function calls h_free_crq and then calls H_REG_CRQ and does all the
|
| 166 | * bookkeeping to get us back to where we can communicate.
|
| 167 | *
|
| 168 | * Return:
|
| 169 | * 0 - Success
|
| 170 | * Non-Zero - Failure
|
| 171 |
|
| 172 | static int ibmvmc_reset_crq_queue(struct crq_server_adapter *adapter)
|
| 173 | {
|
| 174 | struct vio_dev *vdev = to_vio_dev(adapter->dev);
|
| 175 | struct crq_queue *queue = &adapter->queue;
|
| 176 | int rc = 0;
|
| 177 |
|
| 178 |
|
| 179 | h_free_crq(vdev->unit_address);
|
| 180 |
|
| 181 |
|
| 182 | memset(queue->msgs, 0x00, PAGE_SIZE);
|
| 183 | queue->cur = 0;
|
| 184 |
|
| 185 |
|
| 186 | rc = plpar_hcall_norets(H_REG_CRQ,
|
| 187 | vdev->unit_address,
|
| 188 | queue->msg_token, PAGE_SIZE);
|
| 189 | if (rc == 2)
|
| 190 |
|
| 191 | dev_warn(adapter->dev, "Partner adapter not ready\n");
|
| 192 | else if (rc != 0)
|
| 193 | dev_err(adapter->dev, "couldn't register crq--rc 0x%x\n", rc);
|
| 194 |
|
| 195 | return rc;
|
| 196 | }
|
| 197 |
|
| 198 | |
| 199 | * crq_queue_next_crq: - Returns the next entry in message queue
|
| 200 | * @queue: crq_queue to use
|
| 201 | *
|
| 202 | * Returns pointer to next entry in queue, or NULL if there are no new
|
| 203 | * entried in the CRQ.
|
| 204 |
|
| 205 | static struct ibmvmc_crq_msg *crq_queue_next_crq(struct crq_queue *queue)
|
| 206 | {
|
| 207 | struct ibmvmc_crq_msg *crq;
|
| 208 | unsigned long flags;
|
| 209 |
|
| 210 | spin_lock_irqsave(&queue->lock, flags);
|
| 211 | crq = &queue->msgs[queue->cur];
|
| 212 | if (crq->valid & 0x80) {
|
| 213 | if (++queue->cur == queue->size)
|
| 214 | queue->cur = 0;
|
| 215 |
|
| 216 | |
| 217 | * other bits of the CRQ entry
|
| 218 |
|
| 219 | dma_rmb();
|
| 220 | } else {
|
| 221 | crq = NULL;
|
| 222 | }
|
| 223 |
|
| 224 | spin_unlock_irqrestore(&queue->lock, flags);
|
| 225 |
|
| 226 | return crq;
|
| 227 | }
|
| 228 |
|
| 229 | |
| 230 | * ibmvmc_send_crq - Send CRQ
|
| 231 | *
|
| 232 | * @adapter: crq_server_adapter struct
|
| 233 | * @word1: Word1 Data field
|
| 234 | * @word2: Word2 Data field
|
| 235 | *
|
| 236 | * Return:
|
| 237 | * 0 - Success
|
| 238 | * Non-Zero - Failure
|
| 239 |
|
| 240 | static long ibmvmc_send_crq(struct crq_server_adapter *adapter,
|
| 241 | u64 word1, u64 word2)
|
| 242 | {
|
| 243 | struct vio_dev *vdev = to_vio_dev(adapter->dev);
|
| 244 | long rc = 0;
|
| 245 |
|
| 246 | dev_dbg(adapter->dev, "(0x%x, 0x%016llx, 0x%016llx)\n",
|
| 247 | vdev->unit_address, word1, word2);
|
| 248 |
|
| 249 | |
| 250 | * Ensure the command buffer is flushed to memory before handing it
|
| 251 | * over to the other side to prevent it from fetching any stale data.
|
| 252 |
|
| 253 | dma_wmb();
|
| 254 | rc = plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
|
| 255 | dev_dbg(adapter->dev, "rc = 0x%lx\n", rc);
|
| 256 |
|
| 257 | return rc;
|
| 258 | }
|
| 259 |
|
| 260 | |
| 261 | * alloc_dma_buffer - Create DMA Buffer
|
| 262 | *
|
| 263 | * @vdev: vio_dev struct
|
| 264 | * @size: Size field
|
| 265 | * @dma_handle: DMA address field
|
| 266 | *
|
| 267 | * Allocates memory for the command queue and maps remote memory into an
|
| 268 | * ioba.
|
| 269 | *
|
| 270 | * Returns a pointer to the buffer
|
| 271 |
|
| 272 | static void *alloc_dma_buffer(struct vio_dev *vdev, size_t size,
|
| 273 | dma_addr_t *dma_handle)
|
| 274 | {
|
| 275 |
|
| 276 | void *buffer = kzalloc(size, GFP_ATOMIC);
|
| 277 |
|
| 278 | if (!buffer) {
|
| 279 | *dma_handle = 0;
|
| 280 | return NULL;
|
| 281 | }
|
| 282 |
|
| 283 |
|
| 284 | *dma_handle = dma_map_single(&vdev->dev, buffer, size,
|
| 285 | DMA_BIDIRECTIONAL);
|
| 286 |
|
| 287 | if (dma_mapping_error(&vdev->dev, *dma_handle)) {
|
| 288 | *dma_handle = 0;
|
| 289 | kfree_sensitive(buffer);
|
| 290 | return NULL;
|
| 291 | }
|
| 292 |
|
| 293 | return buffer;
|
| 294 | }
|
| 295 |
|
| 296 | |
| 297 | * free_dma_buffer - Free DMA Buffer
|
| 298 | *
|
| 299 | * @vdev: vio_dev struct
|
| 300 | * @size: Size field
|
| 301 | * @vaddr: Address field
|
| 302 | * @dma_handle: DMA address field
|
| 303 | *
|
| 304 | * Releases memory for a command queue and unmaps mapped remote memory.
|
| 305 |
|
| 306 | static void free_dma_buffer(struct vio_dev *vdev, size_t size, void *vaddr,
|
| 307 | dma_addr_t dma_handle)
|
| 308 | {
|
| 309 |
|
| 310 | dma_unmap_single(&vdev->dev, dma_handle, size, DMA_BIDIRECTIONAL);
|
| 311 |
|
| 312 |
|
| 313 | kfree_sensitive(vaddr);
|
| 314 | }
|
| 315 |
|
| 316 | |
| 317 | * ibmvmc_get_valid_hmc_buffer - Retrieve Valid HMC Buffer
|
| 318 | *
|
| 319 | * @hmc_index: HMC Index Field
|
| 320 | *
|
| 321 | * Return:
|
| 322 | * Pointer to ibmvmc_buffer
|
| 323 |
|
| 324 | static struct ibmvmc_buffer *ibmvmc_get_valid_hmc_buffer(u8 hmc_index)
|
| 325 | {
|
| 326 | struct ibmvmc_buffer *buffer;
|
| 327 | struct ibmvmc_buffer *ret_buf = NULL;
|
| 328 | unsigned long i;
|
| 329 |
|
| 330 | if (hmc_index > ibmvmc.max_hmc_index)
|
| 331 | return NULL;
|
| 332 |
|
| 333 | buffer = hmcs[hmc_index].buffer;
|
| 334 |
|
| 335 | for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
|
| 336 | if (buffer[i].valid && buffer[i].free &&
|
| 337 | buffer[i].owner == VMC_BUF_OWNER_ALPHA) {
|
| 338 | buffer[i].free = 0;
|
| 339 | ret_buf = &buffer[i];
|
| 340 | break;
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | return ret_buf;
|
| 345 | }
|
| 346 |
|
| 347 | |
| 348 | * ibmvmc_get_free_hmc_buffer - Get Free HMC Buffer
|
| 349 | *
|
| 350 | * @adapter: crq_server_adapter struct
|
| 351 | * @hmc_index: Hmc Index field
|
| 352 | *
|
| 353 | * Return:
|
| 354 | * Pointer to ibmvmc_buffer
|
| 355 |
|
| 356 | static struct ibmvmc_buffer *ibmvmc_get_free_hmc_buffer(struct crq_server_adapter *adapter,
|
| 357 | u8 hmc_index)
|
| 358 | {
|
| 359 | struct ibmvmc_buffer *buffer;
|
| 360 | struct ibmvmc_buffer *ret_buf = NULL;
|
| 361 | unsigned long i;
|
| 362 |
|
| 363 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 364 | dev_info(adapter->dev, "get_free_hmc_buffer: invalid hmc_index=0x%x\n",
|
| 365 | hmc_index);
|
| 366 | return NULL;
|
| 367 | }
|
| 368 |
|
| 369 | buffer = hmcs[hmc_index].buffer;
|
| 370 |
|
| 371 | for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
|
| 372 | if (buffer[i].free &&
|
| 373 | buffer[i].owner == VMC_BUF_OWNER_ALPHA) {
|
| 374 | buffer[i].free = 0;
|
| 375 | ret_buf = &buffer[i];
|
| 376 | break;
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | return ret_buf;
|
| 381 | }
|
| 382 |
|
| 383 | |
| 384 | * ibmvmc_free_hmc_buffer - Free an HMC Buffer
|
| 385 | *
|
| 386 | * @hmc: ibmvmc_hmc struct
|
| 387 | * @buffer: ibmvmc_buffer struct
|
| 388 | *
|
| 389 |
|
| 390 | static void ibmvmc_free_hmc_buffer(struct ibmvmc_hmc *hmc,
|
| 391 | struct ibmvmc_buffer *buffer)
|
| 392 | {
|
| 393 | unsigned long flags;
|
| 394 |
|
| 395 | spin_lock_irqsave(&hmc->lock, flags);
|
| 396 | buffer->free = 1;
|
| 397 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 398 | }
|
| 399 |
|
| 400 | |
| 401 | * ibmvmc_count_hmc_buffers - Count HMC Buffers
|
| 402 | *
|
| 403 | * @hmc_index: HMC Index field
|
| 404 | * @valid: Valid number of buffers field
|
| 405 | * @free: Free number of buffers field
|
| 406 | *
|
| 407 |
|
| 408 | static void ibmvmc_count_hmc_buffers(u8 hmc_index, unsigned int *valid,
|
| 409 | unsigned int *free)
|
| 410 | {
|
| 411 | struct ibmvmc_buffer *buffer;
|
| 412 | unsigned long i;
|
| 413 | unsigned long flags;
|
| 414 |
|
| 415 | if (hmc_index > ibmvmc.max_hmc_index)
|
| 416 | return;
|
| 417 |
|
| 418 | if (!valid || !free)
|
| 419 | return;
|
| 420 |
|
| 421 | *valid = 0; *free = 0;
|
| 422 |
|
| 423 | buffer = hmcs[hmc_index].buffer;
|
| 424 | spin_lock_irqsave(&hmcs[hmc_index].lock, flags);
|
| 425 |
|
| 426 | for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
|
| 427 | if (buffer[i].valid) {
|
| 428 | *valid = *valid + 1;
|
| 429 | if (buffer[i].free)
|
| 430 | *free = *free + 1;
|
| 431 | }
|
| 432 | }
|
| 433 |
|
| 434 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 435 | }
|
| 436 |
|
| 437 | |
| 438 | * ibmvmc_get_free_hmc - Get Free HMC
|
| 439 | *
|
| 440 | * Return:
|
| 441 | * Pointer to an available HMC Connection
|
| 442 | * Null otherwise
|
| 443 |
|
| 444 | static struct ibmvmc_hmc *ibmvmc_get_free_hmc(void)
|
| 445 | {
|
| 446 | unsigned long i;
|
| 447 | unsigned long flags;
|
| 448 |
|
| 449 | |
| 450 | * Find an available HMC connection.
|
| 451 |
|
| 452 | for (i = 0; i <= ibmvmc.max_hmc_index; i++) {
|
| 453 | spin_lock_irqsave(&hmcs[i].lock, flags);
|
| 454 | if (hmcs[i].state == ibmhmc_state_free) {
|
| 455 | hmcs[i].index = i;
|
| 456 | hmcs[i].state = ibmhmc_state_initial;
|
| 457 | spin_unlock_irqrestore(&hmcs[i].lock, flags);
|
| 458 | return &hmcs[i];
|
| 459 | }
|
| 460 | spin_unlock_irqrestore(&hmcs[i].lock, flags);
|
| 461 | }
|
| 462 |
|
| 463 | return NULL;
|
| 464 | }
|
| 465 |
|
| 466 | |
| 467 | * ibmvmc_return_hmc - Return an HMC Connection
|
| 468 | *
|
| 469 | * @hmc: ibmvmc_hmc struct
|
| 470 | * @release_readers: Number of readers connected to session
|
| 471 | *
|
| 472 | * This function releases the HMC connections back into the pool.
|
| 473 | *
|
| 474 | * Return:
|
| 475 | * 0 - Success
|
| 476 | * Non-zero - Failure
|
| 477 |
|
| 478 | static int ibmvmc_return_hmc(struct ibmvmc_hmc *hmc, bool release_readers)
|
| 479 | {
|
| 480 | struct ibmvmc_buffer *buffer;
|
| 481 | struct crq_server_adapter *adapter;
|
| 482 | struct vio_dev *vdev;
|
| 483 | unsigned long i;
|
| 484 | unsigned long flags;
|
| 485 |
|
| 486 | if (!hmc || !hmc->adapter)
|
| 487 | return -EIO;
|
| 488 |
|
| 489 | if (release_readers) {
|
| 490 | if (hmc->file_session) {
|
| 491 | struct ibmvmc_file_session *session = hmc->file_session;
|
| 492 |
|
| 493 | session->valid = 0;
|
| 494 | wake_up_interruptible(&ibmvmc_read_wait);
|
| 495 | }
|
| 496 | }
|
| 497 |
|
| 498 | adapter = hmc->adapter;
|
| 499 | vdev = to_vio_dev(adapter->dev);
|
| 500 |
|
| 501 | spin_lock_irqsave(&hmc->lock, flags);
|
| 502 | hmc->index = 0;
|
| 503 | hmc->state = ibmhmc_state_free;
|
| 504 | hmc->queue_head = 0;
|
| 505 | hmc->queue_tail = 0;
|
| 506 | buffer = hmc->buffer;
|
| 507 | for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
|
| 508 | if (buffer[i].valid) {
|
| 509 | free_dma_buffer(vdev,
|
| 510 | ibmvmc.max_mtu,
|
| 511 | buffer[i].real_addr_local,
|
| 512 | buffer[i].dma_addr_local);
|
| 513 | dev_dbg(adapter->dev, "Forgot buffer id 0x%lx\n", i);
|
| 514 | }
|
| 515 | memset(&buffer[i], 0, sizeof(struct ibmvmc_buffer));
|
| 516 |
|
| 517 | hmc->queue_outbound_msgs[i] = VMC_INVALID_BUFFER_ID;
|
| 518 | }
|
| 519 |
|
| 520 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 521 |
|
| 522 | return 0;
|
| 523 | }
|
| 524 |
|
| 525 | |
| 526 | * ibmvmc_send_open - Interface Open
|
| 527 | * @buffer: Pointer to ibmvmc_buffer struct
|
| 528 | * @hmc: Pointer to ibmvmc_hmc struct
|
| 529 | *
|
| 530 | * This command is sent by the management partition as the result of a
|
| 531 | * management partition device request. It causes the hypervisor to
|
| 532 | * prepare a set of data buffers for the management application connection
|
| 533 | * indicated HMC idx. A unique HMC Idx would be used if multiple management
|
| 534 | * applications running concurrently were desired. Before responding to this
|
| 535 | * command, the hypervisor must provide the management partition with at
|
| 536 | * least one of these new buffers via the Add Buffer. This indicates whether
|
| 537 | * the messages are inbound or outbound from the hypervisor.
|
| 538 | *
|
| 539 | * Return:
|
| 540 | * 0 - Success
|
| 541 | * Non-zero - Failure
|
| 542 |
|
| 543 | static int ibmvmc_send_open(struct ibmvmc_buffer *buffer,
|
| 544 | struct ibmvmc_hmc *hmc)
|
| 545 | {
|
| 546 | struct ibmvmc_crq_msg crq_msg;
|
| 547 | struct crq_server_adapter *adapter;
|
| 548 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 549 | int rc = 0;
|
| 550 |
|
| 551 | if (!hmc || !hmc->adapter)
|
| 552 | return -EIO;
|
| 553 |
|
| 554 | adapter = hmc->adapter;
|
| 555 |
|
| 556 | dev_dbg(adapter->dev, "send_open: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
|
| 557 | (unsigned long)buffer->size, (unsigned long)adapter->liobn,
|
| 558 | (unsigned long)buffer->dma_addr_local,
|
| 559 | (unsigned long)adapter->riobn,
|
| 560 | (unsigned long)buffer->dma_addr_remote);
|
| 561 |
|
| 562 | rc = h_copy_rdma(buffer->size,
|
| 563 | adapter->liobn,
|
| 564 | buffer->dma_addr_local,
|
| 565 | adapter->riobn,
|
| 566 | buffer->dma_addr_remote);
|
| 567 | if (rc) {
|
| 568 | dev_err(adapter->dev, "Error: In send_open, h_copy_rdma rc 0x%x\n",
|
| 569 | rc);
|
| 570 | return -EIO;
|
| 571 | }
|
| 572 |
|
| 573 | hmc->state = ibmhmc_state_opening;
|
| 574 |
|
| 575 | crq_msg.valid = 0x80;
|
| 576 | crq_msg.type = VMC_MSG_OPEN;
|
| 577 | crq_msg.status = 0;
|
| 578 | crq_msg.var1.rsvd = 0;
|
| 579 | crq_msg.hmc_session = hmc->session;
|
| 580 | crq_msg.hmc_index = hmc->index;
|
| 581 | crq_msg.var2.buffer_id = cpu_to_be16(buffer->id);
|
| 582 | crq_msg.rsvd = 0;
|
| 583 | crq_msg.var3.rsvd = 0;
|
| 584 |
|
| 585 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 586 | be64_to_cpu(crq_as_u64[1]));
|
| 587 |
|
| 588 | return rc;
|
| 589 | }
|
| 590 |
|
| 591 | |
| 592 | * ibmvmc_send_close - Interface Close
|
| 593 | * @hmc: Pointer to ibmvmc_hmc struct
|
| 594 | *
|
| 595 | * This command is sent by the management partition to terminate a
|
| 596 | * management application to hypervisor connection. When this command is
|
| 597 | * sent, the management partition has quiesced all I/O operations to all
|
| 598 | * buffers associated with this management application connection, and
|
| 599 | * has freed any storage for these buffers.
|
| 600 | *
|
| 601 | * Return:
|
| 602 | * 0 - Success
|
| 603 | * Non-zero - Failure
|
| 604 |
|
| 605 | static int ibmvmc_send_close(struct ibmvmc_hmc *hmc)
|
| 606 | {
|
| 607 | struct ibmvmc_crq_msg crq_msg;
|
| 608 | struct crq_server_adapter *adapter;
|
| 609 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 610 | int rc = 0;
|
| 611 |
|
| 612 | if (!hmc || !hmc->adapter)
|
| 613 | return -EIO;
|
| 614 |
|
| 615 | adapter = hmc->adapter;
|
| 616 |
|
| 617 | dev_info(adapter->dev, "CRQ send: close\n");
|
| 618 |
|
| 619 | crq_msg.valid = 0x80;
|
| 620 | crq_msg.type = VMC_MSG_CLOSE;
|
| 621 | crq_msg.status = 0;
|
| 622 | crq_msg.var1.rsvd = 0;
|
| 623 | crq_msg.hmc_session = hmc->session;
|
| 624 | crq_msg.hmc_index = hmc->index;
|
| 625 | crq_msg.var2.rsvd = 0;
|
| 626 | crq_msg.rsvd = 0;
|
| 627 | crq_msg.var3.rsvd = 0;
|
| 628 |
|
| 629 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 630 | be64_to_cpu(crq_as_u64[1]));
|
| 631 |
|
| 632 | return rc;
|
| 633 | }
|
| 634 |
|
| 635 | |
| 636 | * ibmvmc_send_capabilities - Send VMC Capabilities
|
| 637 | *
|
| 638 | * @adapter: crq_server_adapter struct
|
| 639 | *
|
| 640 | * The capabilities message is an administrative message sent after the CRQ
|
| 641 | * initialization sequence of messages and is used to exchange VMC capabilities
|
| 642 | * between the management partition and the hypervisor. The management
|
| 643 | * partition must send this message and the hypervisor must respond with VMC
|
| 644 | * capabilities Response message before HMC interface message can begin. Any
|
| 645 | * HMC interface messages received before the exchange of capabilities has
|
| 646 | * complete are dropped.
|
| 647 | *
|
| 648 | * Return:
|
| 649 | * 0 - Success
|
| 650 |
|
| 651 | static int ibmvmc_send_capabilities(struct crq_server_adapter *adapter)
|
| 652 | {
|
| 653 | struct ibmvmc_admin_crq_msg crq_msg;
|
| 654 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 655 |
|
| 656 | dev_dbg(adapter->dev, "ibmvmc: CRQ send: capabilities\n");
|
| 657 | crq_msg.valid = 0x80;
|
| 658 | crq_msg.type = VMC_MSG_CAP;
|
| 659 | crq_msg.status = 0;
|
| 660 | crq_msg.rsvd[0] = 0;
|
| 661 | crq_msg.rsvd[1] = 0;
|
| 662 | crq_msg.max_hmc = ibmvmc_max_hmcs;
|
| 663 | crq_msg.max_mtu = cpu_to_be32(ibmvmc_max_mtu);
|
| 664 | crq_msg.pool_size = cpu_to_be16(ibmvmc_max_buf_pool_size);
|
| 665 | crq_msg.crq_size = cpu_to_be16(adapter->queue.size);
|
| 666 | crq_msg.version = cpu_to_be16(IBMVMC_PROTOCOL_VERSION);
|
| 667 |
|
| 668 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 669 | be64_to_cpu(crq_as_u64[1]));
|
| 670 |
|
| 671 | ibmvmc.state = ibmvmc_state_capabilities;
|
| 672 |
|
| 673 | return 0;
|
| 674 | }
|
| 675 |
|
| 676 | |
| 677 | * ibmvmc_send_add_buffer_resp - Add Buffer Response
|
| 678 | *
|
| 679 | * @adapter: crq_server_adapter struct
|
| 680 | * @status: Status field
|
| 681 | * @hmc_session: HMC Session field
|
| 682 | * @hmc_index: HMC Index field
|
| 683 | * @buffer_id: Buffer Id field
|
| 684 | *
|
| 685 | * This command is sent by the management partition to the hypervisor in
|
| 686 | * response to the Add Buffer message. The Status field indicates the result of
|
| 687 | * the command.
|
| 688 | *
|
| 689 | * Return:
|
| 690 | * 0 - Success
|
| 691 |
|
| 692 | static int ibmvmc_send_add_buffer_resp(struct crq_server_adapter *adapter,
|
| 693 | u8 status, u8 hmc_session,
|
| 694 | u8 hmc_index, u16 buffer_id)
|
| 695 | {
|
| 696 | struct ibmvmc_crq_msg crq_msg;
|
| 697 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 698 |
|
| 699 | dev_dbg(adapter->dev, "CRQ send: add_buffer_resp\n");
|
| 700 | crq_msg.valid = 0x80;
|
| 701 | crq_msg.type = VMC_MSG_ADD_BUF_RESP;
|
| 702 | crq_msg.status = status;
|
| 703 | crq_msg.var1.rsvd = 0;
|
| 704 | crq_msg.hmc_session = hmc_session;
|
| 705 | crq_msg.hmc_index = hmc_index;
|
| 706 | crq_msg.var2.buffer_id = cpu_to_be16(buffer_id);
|
| 707 | crq_msg.rsvd = 0;
|
| 708 | crq_msg.var3.rsvd = 0;
|
| 709 |
|
| 710 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 711 | be64_to_cpu(crq_as_u64[1]));
|
| 712 |
|
| 713 | return 0;
|
| 714 | }
|
| 715 |
|
| 716 | |
| 717 | * ibmvmc_send_rem_buffer_resp - Remove Buffer Response
|
| 718 | *
|
| 719 | * @adapter: crq_server_adapter struct
|
| 720 | * @status: Status field
|
| 721 | * @hmc_session: HMC Session field
|
| 722 | * @hmc_index: HMC Index field
|
| 723 | * @buffer_id: Buffer Id field
|
| 724 | *
|
| 725 | * This command is sent by the management partition to the hypervisor in
|
| 726 | * response to the Remove Buffer message. The Buffer ID field indicates
|
| 727 | * which buffer the management partition selected to remove. The Status
|
| 728 | * field indicates the result of the command.
|
| 729 | *
|
| 730 | * Return:
|
| 731 | * 0 - Success
|
| 732 |
|
| 733 | static int ibmvmc_send_rem_buffer_resp(struct crq_server_adapter *adapter,
|
| 734 | u8 status, u8 hmc_session,
|
| 735 | u8 hmc_index, u16 buffer_id)
|
| 736 | {
|
| 737 | struct ibmvmc_crq_msg crq_msg;
|
| 738 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 739 |
|
| 740 | dev_dbg(adapter->dev, "CRQ send: rem_buffer_resp\n");
|
| 741 | crq_msg.valid = 0x80;
|
| 742 | crq_msg.type = VMC_MSG_REM_BUF_RESP;
|
| 743 | crq_msg.status = status;
|
| 744 | crq_msg.var1.rsvd = 0;
|
| 745 | crq_msg.hmc_session = hmc_session;
|
| 746 | crq_msg.hmc_index = hmc_index;
|
| 747 | crq_msg.var2.buffer_id = cpu_to_be16(buffer_id);
|
| 748 | crq_msg.rsvd = 0;
|
| 749 | crq_msg.var3.rsvd = 0;
|
| 750 |
|
| 751 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 752 | be64_to_cpu(crq_as_u64[1]));
|
| 753 |
|
| 754 | return 0;
|
| 755 | }
|
| 756 |
|
| 757 | |
| 758 | * ibmvmc_send_msg - Signal Message
|
| 759 | *
|
| 760 | * @adapter: crq_server_adapter struct
|
| 761 | * @buffer: ibmvmc_buffer struct
|
| 762 | * @hmc: ibmvmc_hmc struct
|
| 763 | * @msg_len: message length field
|
| 764 | *
|
| 765 | * This command is sent between the management partition and the hypervisor
|
| 766 | * in order to signal the arrival of an HMC protocol message. The command
|
| 767 | * can be sent by both the management partition and the hypervisor. It is
|
| 768 | * used for all traffic between the management application and the hypervisor,
|
| 769 | * regardless of who initiated the communication.
|
| 770 | *
|
| 771 | * There is no response to this message.
|
| 772 | *
|
| 773 | * Return:
|
| 774 | * 0 - Success
|
| 775 | * Non-zero - Failure
|
| 776 |
|
| 777 | static int ibmvmc_send_msg(struct crq_server_adapter *adapter,
|
| 778 | struct ibmvmc_buffer *buffer,
|
| 779 | struct ibmvmc_hmc *hmc, int msg_len)
|
| 780 | {
|
| 781 | struct ibmvmc_crq_msg crq_msg;
|
| 782 | __be64 *crq_as_u64 = (__be64 *)&crq_msg;
|
| 783 | int rc = 0;
|
| 784 |
|
| 785 | dev_dbg(adapter->dev, "CRQ send: rdma to HV\n");
|
| 786 | rc = h_copy_rdma(msg_len,
|
| 787 | adapter->liobn,
|
| 788 | buffer->dma_addr_local,
|
| 789 | adapter->riobn,
|
| 790 | buffer->dma_addr_remote);
|
| 791 | if (rc) {
|
| 792 | dev_err(adapter->dev, "Error in send_msg, h_copy_rdma rc 0x%x\n",
|
| 793 | rc);
|
| 794 | return rc;
|
| 795 | }
|
| 796 |
|
| 797 | crq_msg.valid = 0x80;
|
| 798 | crq_msg.type = VMC_MSG_SIGNAL;
|
| 799 | crq_msg.status = 0;
|
| 800 | crq_msg.var1.rsvd = 0;
|
| 801 | crq_msg.hmc_session = hmc->session;
|
| 802 | crq_msg.hmc_index = hmc->index;
|
| 803 | crq_msg.var2.buffer_id = cpu_to_be16(buffer->id);
|
| 804 | crq_msg.var3.msg_len = cpu_to_be32(msg_len);
|
| 805 | dev_dbg(adapter->dev, "CRQ send: msg to HV 0x%llx 0x%llx\n",
|
| 806 | be64_to_cpu(crq_as_u64[0]), be64_to_cpu(crq_as_u64[1]));
|
| 807 |
|
| 808 | buffer->owner = VMC_BUF_OWNER_HV;
|
| 809 | ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
|
| 810 | be64_to_cpu(crq_as_u64[1]));
|
| 811 |
|
| 812 | return rc;
|
| 813 | }
|
| 814 |
|
| 815 | |
| 816 | * ibmvmc_open - Open Session
|
| 817 | *
|
| 818 | * @inode: inode struct
|
| 819 | * @file: file struct
|
| 820 | *
|
| 821 | * Return:
|
| 822 | * 0 - Success
|
| 823 | * Non-zero - Failure
|
| 824 |
|
| 825 | static int ibmvmc_open(struct inode *inode, struct file *file)
|
| 826 | {
|
| 827 | struct ibmvmc_file_session *session;
|
| 828 |
|
| 829 | pr_debug("%s: inode = 0x%lx, file = 0x%lx, state = 0x%x\n", __func__,
|
| 830 | (unsigned long)inode, (unsigned long)file,
|
| 831 | ibmvmc.state);
|
| 832 |
|
| 833 | session = kzalloc_obj(*session);
|
| 834 | if (!session)
|
| 835 | return -ENOMEM;
|
| 836 |
|
| 837 | session->file = file;
|
| 838 | file->private_data = session;
|
| 839 |
|
| 840 | return 0;
|
| 841 | }
|
| 842 |
|
| 843 | |
| 844 | * ibmvmc_close - Close Session
|
| 845 | *
|
| 846 | * @inode: inode struct
|
| 847 | * @file: file struct
|
| 848 | *
|
| 849 | * Return:
|
| 850 | * 0 - Success
|
| 851 | * Non-zero - Failure
|
| 852 |
|
| 853 | static int ibmvmc_close(struct inode *inode, struct file *file)
|
| 854 | {
|
| 855 | struct ibmvmc_file_session *session;
|
| 856 | struct ibmvmc_hmc *hmc;
|
| 857 | int rc = 0;
|
| 858 | unsigned long flags;
|
| 859 |
|
| 860 | pr_debug("%s: file = 0x%lx, state = 0x%x\n", __func__,
|
| 861 | (unsigned long)file, ibmvmc.state);
|
| 862 |
|
| 863 | session = file->private_data;
|
| 864 | if (!session)
|
| 865 | return -EIO;
|
| 866 |
|
| 867 | hmc = session->hmc;
|
| 868 | if (hmc) {
|
| 869 | if (!hmc->adapter)
|
| 870 | return -EIO;
|
| 871 |
|
| 872 | if (ibmvmc.state == ibmvmc_state_failed) {
|
| 873 | dev_warn(hmc->adapter->dev, "close: state_failed\n");
|
| 874 | return -EIO;
|
| 875 | }
|
| 876 |
|
| 877 | spin_lock_irqsave(&hmc->lock, flags);
|
| 878 | if (hmc->state >= ibmhmc_state_opening) {
|
| 879 | rc = ibmvmc_send_close(hmc);
|
| 880 | if (rc)
|
| 881 | dev_warn(hmc->adapter->dev, "close: send_close failed.\n");
|
| 882 | }
|
| 883 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 884 | }
|
| 885 |
|
| 886 | kfree_sensitive(session);
|
| 887 |
|
| 888 | return rc;
|
| 889 | }
|
| 890 |
|
| 891 | |
| 892 | * ibmvmc_read - Read
|
| 893 | *
|
| 894 | * @file: file struct
|
| 895 | * @buf: Character buffer
|
| 896 | * @nbytes: Size in bytes
|
| 897 | * @ppos: Offset
|
| 898 | *
|
| 899 | * Return:
|
| 900 | * 0 - Success
|
| 901 | * Non-zero - Failure
|
| 902 |
|
| 903 | static ssize_t ibmvmc_read(struct file *file, char *buf, size_t nbytes,
|
| 904 | loff_t *ppos)
|
| 905 | {
|
| 906 | struct ibmvmc_file_session *session;
|
| 907 | struct ibmvmc_hmc *hmc;
|
| 908 | struct crq_server_adapter *adapter;
|
| 909 | struct ibmvmc_buffer *buffer;
|
| 910 | ssize_t n;
|
| 911 | ssize_t retval = 0;
|
| 912 | unsigned long flags;
|
| 913 | DEFINE_WAIT(wait);
|
| 914 |
|
| 915 | pr_debug("ibmvmc: read: file = 0x%lx, buf = 0x%lx, nbytes = 0x%lx\n",
|
| 916 | (unsigned long)file, (unsigned long)buf,
|
| 917 | (unsigned long)nbytes);
|
| 918 |
|
| 919 | if (nbytes == 0)
|
| 920 | return 0;
|
| 921 |
|
| 922 | if (nbytes > ibmvmc.max_mtu) {
|
| 923 | pr_warn("ibmvmc: read: nbytes invalid 0x%x\n",
|
| 924 | (unsigned int)nbytes);
|
| 925 | return -EINVAL;
|
| 926 | }
|
| 927 |
|
| 928 | session = file->private_data;
|
| 929 | if (!session) {
|
| 930 | pr_warn("ibmvmc: read: no session\n");
|
| 931 | return -EIO;
|
| 932 | }
|
| 933 |
|
| 934 | hmc = session->hmc;
|
| 935 | if (!hmc) {
|
| 936 | pr_warn("ibmvmc: read: no hmc\n");
|
| 937 | return -EIO;
|
| 938 | }
|
| 939 |
|
| 940 | adapter = hmc->adapter;
|
| 941 | if (!adapter) {
|
| 942 | pr_warn("ibmvmc: read: no adapter\n");
|
| 943 | return -EIO;
|
| 944 | }
|
| 945 |
|
| 946 | do {
|
| 947 | prepare_to_wait(&ibmvmc_read_wait, &wait, TASK_INTERRUPTIBLE);
|
| 948 |
|
| 949 | spin_lock_irqsave(&hmc->lock, flags);
|
| 950 | if (hmc->queue_tail != hmc->queue_head)
|
| 951 |
|
| 952 | break;
|
| 953 |
|
| 954 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 955 |
|
| 956 | if (!session->valid) {
|
| 957 | retval = -EBADFD;
|
| 958 | goto out;
|
| 959 | }
|
| 960 | if (file->f_flags & O_NONBLOCK) {
|
| 961 | retval = -EAGAIN;
|
| 962 | goto out;
|
| 963 | }
|
| 964 |
|
| 965 | schedule();
|
| 966 |
|
| 967 | if (signal_pending(current)) {
|
| 968 | retval = -ERESTARTSYS;
|
| 969 | goto out;
|
| 970 | }
|
| 971 | } while (1);
|
| 972 |
|
| 973 | buffer = &(hmc->buffer[hmc->queue_outbound_msgs[hmc->queue_tail]]);
|
| 974 | hmc->queue_tail++;
|
| 975 | if (hmc->queue_tail == ibmvmc_max_buf_pool_size)
|
| 976 | hmc->queue_tail = 0;
|
| 977 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 978 |
|
| 979 | nbytes = min_t(size_t, nbytes, buffer->msg_len);
|
| 980 | n = copy_to_user((void *)buf, buffer->real_addr_local, nbytes);
|
| 981 | dev_dbg(adapter->dev, "read: copy to user nbytes = 0x%lx.\n", nbytes);
|
| 982 | ibmvmc_free_hmc_buffer(hmc, buffer);
|
| 983 | retval = nbytes;
|
| 984 |
|
| 985 | if (n) {
|
| 986 | dev_warn(adapter->dev, "read: copy to user failed.\n");
|
| 987 | retval = -EFAULT;
|
| 988 | }
|
| 989 |
|
| 990 | out:
|
| 991 | finish_wait(&ibmvmc_read_wait, &wait);
|
| 992 | dev_dbg(adapter->dev, "read: out %ld\n", retval);
|
| 993 | return retval;
|
| 994 | }
|
| 995 |
|
| 996 | |
| 997 | * ibmvmc_poll - Poll
|
| 998 | *
|
| 999 | * @file: file struct
|
| 1000 | * @wait: Poll Table
|
| 1001 | *
|
| 1002 | * Return:
|
| 1003 | * poll.h return values
|
| 1004 |
|
| 1005 | static unsigned int ibmvmc_poll(struct file *file, poll_table *wait)
|
| 1006 | {
|
| 1007 | struct ibmvmc_file_session *session;
|
| 1008 | struct ibmvmc_hmc *hmc;
|
| 1009 | unsigned int mask = 0;
|
| 1010 |
|
| 1011 | session = file->private_data;
|
| 1012 | if (!session)
|
| 1013 | return 0;
|
| 1014 |
|
| 1015 | hmc = session->hmc;
|
| 1016 | if (!hmc)
|
| 1017 | return 0;
|
| 1018 |
|
| 1019 | poll_wait(file, &ibmvmc_read_wait, wait);
|
| 1020 |
|
| 1021 | if (hmc->queue_head != hmc->queue_tail)
|
| 1022 | mask |= POLLIN | POLLRDNORM;
|
| 1023 |
|
| 1024 | return mask;
|
| 1025 | }
|
| 1026 |
|
| 1027 | |
| 1028 | * ibmvmc_write - Write
|
| 1029 | *
|
| 1030 | * @file: file struct
|
| 1031 | * @buffer: Character buffer
|
| 1032 | * @count: Count field
|
| 1033 | * @ppos: Offset
|
| 1034 | *
|
| 1035 | * Return:
|
| 1036 | * 0 - Success
|
| 1037 | * Non-zero - Failure
|
| 1038 |
|
| 1039 | static ssize_t ibmvmc_write(struct file *file, const char *buffer,
|
| 1040 | size_t count, loff_t *ppos)
|
| 1041 | {
|
| 1042 | struct inode *inode;
|
| 1043 | struct ibmvmc_buffer *vmc_buffer = NULL;
|
| 1044 | struct ibmvmc_file_session *session;
|
| 1045 | struct crq_server_adapter *adapter;
|
| 1046 | struct ibmvmc_hmc *hmc;
|
| 1047 | unsigned char *buf;
|
| 1048 | unsigned long flags;
|
| 1049 | size_t bytes;
|
| 1050 | const char *p = buffer;
|
| 1051 | size_t c = count;
|
| 1052 | int ret = 0;
|
| 1053 |
|
| 1054 | session = file->private_data;
|
| 1055 | if (!session)
|
| 1056 | return -EIO;
|
| 1057 |
|
| 1058 | hmc = session->hmc;
|
| 1059 | if (!hmc)
|
| 1060 | return -EIO;
|
| 1061 |
|
| 1062 | spin_lock_irqsave(&hmc->lock, flags);
|
| 1063 | if (hmc->state == ibmhmc_state_free) {
|
| 1064 |
|
| 1065 | ret = -EIO;
|
| 1066 | goto out;
|
| 1067 | }
|
| 1068 |
|
| 1069 | adapter = hmc->adapter;
|
| 1070 | if (!adapter) {
|
| 1071 | ret = -EIO;
|
| 1072 | goto out;
|
| 1073 | }
|
| 1074 |
|
| 1075 | if (count > ibmvmc.max_mtu) {
|
| 1076 | dev_warn(adapter->dev, "invalid buffer size 0x%lx\n",
|
| 1077 | (unsigned long)count);
|
| 1078 | ret = -EIO;
|
| 1079 | goto out;
|
| 1080 | }
|
| 1081 |
|
| 1082 |
|
| 1083 | if (hmc->state == ibmhmc_state_opening) {
|
| 1084 | ret = -EBUSY;
|
| 1085 | goto out;
|
| 1086 | }
|
| 1087 |
|
| 1088 | |
| 1089 | * the HMC connection has not failed.
|
| 1090 |
|
| 1091 | if (hmc->state != ibmhmc_state_ready) {
|
| 1092 | ret = -EIO;
|
| 1093 | goto out;
|
| 1094 | }
|
| 1095 |
|
| 1096 | vmc_buffer = ibmvmc_get_valid_hmc_buffer(hmc->index);
|
| 1097 | if (!vmc_buffer) {
|
| 1098 | |
| 1099 | * completed the open/open_resp sequence. Retry until this is
|
| 1100 | * complete.
|
| 1101 |
|
| 1102 | ret = -EBUSY;
|
| 1103 | goto out;
|
| 1104 | }
|
| 1105 | if (!vmc_buffer->real_addr_local) {
|
| 1106 | dev_err(adapter->dev, "no buffer storage assigned\n");
|
| 1107 | ret = -EIO;
|
| 1108 | goto out;
|
| 1109 | }
|
| 1110 | buf = vmc_buffer->real_addr_local;
|
| 1111 |
|
| 1112 | while (c > 0) {
|
| 1113 | bytes = min_t(size_t, c, vmc_buffer->size);
|
| 1114 |
|
| 1115 | bytes -= copy_from_user(buf, p, bytes);
|
| 1116 | if (!bytes) {
|
| 1117 | ret = -EFAULT;
|
| 1118 | goto out;
|
| 1119 | }
|
| 1120 | c -= bytes;
|
| 1121 | p += bytes;
|
| 1122 | }
|
| 1123 | if (p == buffer)
|
| 1124 | goto out;
|
| 1125 |
|
| 1126 | inode = file_inode(file);
|
| 1127 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
|
| 1128 | mark_inode_dirty(inode);
|
| 1129 |
|
| 1130 | dev_dbg(adapter->dev, "write: file = 0x%lx, count = 0x%lx\n",
|
| 1131 | (unsigned long)file, (unsigned long)count);
|
| 1132 |
|
| 1133 | if (ibmvmc_send_msg(adapter, vmc_buffer, hmc, count)) {
|
| 1134 | ret = -EIO;
|
| 1135 | goto out;
|
| 1136 | }
|
| 1137 | vmc_buffer = NULL;
|
| 1138 | ret = p - buffer;
|
| 1139 | out:
|
| 1140 | if (vmc_buffer)
|
| 1141 | vmc_buffer->free = 1;
|
| 1142 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1143 | return (ssize_t)(ret);
|
| 1144 | }
|
| 1145 |
|
| 1146 | |
| 1147 | * ibmvmc_setup_hmc - Setup the HMC
|
| 1148 | *
|
| 1149 | * @session: ibmvmc_file_session struct
|
| 1150 | *
|
| 1151 | * Return:
|
| 1152 | * 0 - Success
|
| 1153 | * Non-zero - Failure
|
| 1154 |
|
| 1155 | static long ibmvmc_setup_hmc(struct ibmvmc_file_session *session)
|
| 1156 | {
|
| 1157 | struct ibmvmc_hmc *hmc;
|
| 1158 | unsigned int valid, free, index;
|
| 1159 |
|
| 1160 | if (ibmvmc.state == ibmvmc_state_failed) {
|
| 1161 | pr_warn("ibmvmc: Reserve HMC: state_failed\n");
|
| 1162 | return -EIO;
|
| 1163 | }
|
| 1164 |
|
| 1165 | if (ibmvmc.state < ibmvmc_state_ready) {
|
| 1166 | pr_warn("ibmvmc: Reserve HMC: not state_ready\n");
|
| 1167 | return -EAGAIN;
|
| 1168 | }
|
| 1169 |
|
| 1170 | |
| 1171 | * have a generic buffer for each possible HMC connection.
|
| 1172 |
|
| 1173 | for (index = 0; index <= ibmvmc.max_hmc_index; index++) {
|
| 1174 | valid = 0;
|
| 1175 | ibmvmc_count_hmc_buffers(index, &valid, &free);
|
| 1176 | if (valid == 0) {
|
| 1177 | pr_warn("ibmvmc: buffers not ready for index %d\n",
|
| 1178 | index);
|
| 1179 | return -ENOBUFS;
|
| 1180 | }
|
| 1181 | }
|
| 1182 |
|
| 1183 |
|
| 1184 | hmc = ibmvmc_get_free_hmc();
|
| 1185 | if (!hmc) {
|
| 1186 | pr_warn("%s: free hmc not found\n", __func__);
|
| 1187 | return -EBUSY;
|
| 1188 | }
|
| 1189 |
|
| 1190 | hmc->session = hmc->session + 1;
|
| 1191 | if (hmc->session == 0xff)
|
| 1192 | hmc->session = 1;
|
| 1193 |
|
| 1194 | session->hmc = hmc;
|
| 1195 | hmc->adapter = &ibmvmc_adapter;
|
| 1196 | hmc->file_session = session;
|
| 1197 | session->valid = 1;
|
| 1198 |
|
| 1199 | return 0;
|
| 1200 | }
|
| 1201 |
|
| 1202 | |
| 1203 | * ibmvmc_ioctl_sethmcid - IOCTL Set HMC ID
|
| 1204 | *
|
| 1205 | * @session: ibmvmc_file_session struct
|
| 1206 | * @new_hmc_id: HMC id field
|
| 1207 | *
|
| 1208 | * IOCTL command to setup the hmc id
|
| 1209 | *
|
| 1210 | * Return:
|
| 1211 | * 0 - Success
|
| 1212 | * Non-zero - Failure
|
| 1213 |
|
| 1214 | static long ibmvmc_ioctl_sethmcid(struct ibmvmc_file_session *session,
|
| 1215 | unsigned char __user *new_hmc_id)
|
| 1216 | {
|
| 1217 | struct ibmvmc_hmc *hmc;
|
| 1218 | struct ibmvmc_buffer *buffer;
|
| 1219 | size_t bytes;
|
| 1220 | char print_buffer[HMC_ID_LEN + 1];
|
| 1221 | unsigned long flags;
|
| 1222 | long rc = 0;
|
| 1223 |
|
| 1224 |
|
| 1225 | hmc = session->hmc;
|
| 1226 | if (!hmc) {
|
| 1227 | rc = ibmvmc_setup_hmc(session);
|
| 1228 | if (rc)
|
| 1229 | return rc;
|
| 1230 |
|
| 1231 | hmc = session->hmc;
|
| 1232 | if (!hmc) {
|
| 1233 | pr_err("ibmvmc: setup_hmc success but no hmc\n");
|
| 1234 | return -EIO;
|
| 1235 | }
|
| 1236 | }
|
| 1237 |
|
| 1238 | if (hmc->state != ibmhmc_state_initial) {
|
| 1239 | pr_warn("ibmvmc: sethmcid: invalid state to send open 0x%x\n",
|
| 1240 | hmc->state);
|
| 1241 | return -EIO;
|
| 1242 | }
|
| 1243 |
|
| 1244 | bytes = copy_from_user(hmc->hmc_id, new_hmc_id, HMC_ID_LEN);
|
| 1245 | if (bytes)
|
| 1246 | return -EFAULT;
|
| 1247 |
|
| 1248 |
|
| 1249 | spin_lock_irqsave(&hmc->lock, flags);
|
| 1250 | buffer = ibmvmc_get_valid_hmc_buffer(hmc->index);
|
| 1251 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1252 |
|
| 1253 | if (!buffer || !buffer->real_addr_local) {
|
| 1254 | pr_warn("ibmvmc: sethmcid: no buffer available\n");
|
| 1255 | return -EIO;
|
| 1256 | }
|
| 1257 |
|
| 1258 | strscpy(print_buffer, hmc->hmc_id, sizeof(print_buffer));
|
| 1259 | pr_info("ibmvmc: sethmcid: Set HMC ID: \"%s\"\n", print_buffer);
|
| 1260 |
|
| 1261 | memcpy(buffer->real_addr_local, hmc->hmc_id, HMC_ID_LEN);
|
| 1262 |
|
| 1263 | rc = ibmvmc_send_open(buffer, hmc);
|
| 1264 |
|
| 1265 | return rc;
|
| 1266 | }
|
| 1267 |
|
| 1268 | |
| 1269 | * ibmvmc_ioctl_query - IOCTL Query
|
| 1270 | *
|
| 1271 | * @session: ibmvmc_file_session struct
|
| 1272 | * @ret_struct: ibmvmc_query_struct
|
| 1273 | *
|
| 1274 | * Return:
|
| 1275 | * 0 - Success
|
| 1276 | * Non-zero - Failure
|
| 1277 |
|
| 1278 | static long ibmvmc_ioctl_query(struct ibmvmc_file_session *session,
|
| 1279 | struct ibmvmc_query_struct __user *ret_struct)
|
| 1280 | {
|
| 1281 | struct ibmvmc_query_struct query_struct;
|
| 1282 | size_t bytes;
|
| 1283 |
|
| 1284 | memset(&query_struct, 0, sizeof(query_struct));
|
| 1285 | query_struct.have_vmc = (ibmvmc.state > ibmvmc_state_initial);
|
| 1286 | query_struct.state = ibmvmc.state;
|
| 1287 | query_struct.vmc_drc_index = ibmvmc.vmc_drc_index;
|
| 1288 |
|
| 1289 | bytes = copy_to_user(ret_struct, &query_struct,
|
| 1290 | sizeof(query_struct));
|
| 1291 | if (bytes)
|
| 1292 | return -EFAULT;
|
| 1293 |
|
| 1294 | return 0;
|
| 1295 | }
|
| 1296 |
|
| 1297 | |
| 1298 | * ibmvmc_ioctl_requestvmc - IOCTL Request VMC
|
| 1299 | *
|
| 1300 | * @session: ibmvmc_file_session struct
|
| 1301 | * @ret_vmc_index: VMC Index
|
| 1302 | *
|
| 1303 | * Return:
|
| 1304 | * 0 - Success
|
| 1305 | * Non-zero - Failure
|
| 1306 |
|
| 1307 | static long ibmvmc_ioctl_requestvmc(struct ibmvmc_file_session *session,
|
| 1308 | u32 __user *ret_vmc_index)
|
| 1309 | {
|
| 1310 |
|
| 1311 | size_t bytes;
|
| 1312 | long rc;
|
| 1313 | u32 vmc_drc_index;
|
| 1314 |
|
| 1315 |
|
| 1316 | rc = h_request_vmc(&vmc_drc_index);
|
| 1317 | pr_debug("ibmvmc: requestvmc: H_REQUEST_VMC rc = 0x%lx\n", rc);
|
| 1318 |
|
| 1319 | if (rc == H_SUCCESS) {
|
| 1320 | rc = 0;
|
| 1321 | } else if (rc == H_FUNCTION) {
|
| 1322 | pr_err("ibmvmc: requestvmc: h_request_vmc not supported\n");
|
| 1323 | return -EPERM;
|
| 1324 | } else if (rc == H_AUTHORITY) {
|
| 1325 | pr_err("ibmvmc: requestvmc: hypervisor denied vmc request\n");
|
| 1326 | return -EPERM;
|
| 1327 | } else if (rc == H_HARDWARE) {
|
| 1328 | pr_err("ibmvmc: requestvmc: hypervisor hardware fault\n");
|
| 1329 | return -EIO;
|
| 1330 | } else if (rc == H_RESOURCE) {
|
| 1331 | pr_err("ibmvmc: requestvmc: vmc resource unavailable\n");
|
| 1332 | return -ENODEV;
|
| 1333 | } else if (rc == H_NOT_AVAILABLE) {
|
| 1334 | pr_err("ibmvmc: requestvmc: system cannot be vmc managed\n");
|
| 1335 | return -EPERM;
|
| 1336 | } else if (rc == H_PARAMETER) {
|
| 1337 | pr_err("ibmvmc: requestvmc: invalid parameter\n");
|
| 1338 | return -EINVAL;
|
| 1339 | }
|
| 1340 |
|
| 1341 |
|
| 1342 | ibmvmc.vmc_drc_index = vmc_drc_index;
|
| 1343 |
|
| 1344 | bytes = copy_to_user(ret_vmc_index, &vmc_drc_index,
|
| 1345 | sizeof(*ret_vmc_index));
|
| 1346 | if (bytes) {
|
| 1347 | pr_warn("ibmvmc: requestvmc: copy to user failed.\n");
|
| 1348 | return -EFAULT;
|
| 1349 | }
|
| 1350 | return rc;
|
| 1351 | }
|
| 1352 |
|
| 1353 | |
| 1354 | * ibmvmc_ioctl - IOCTL
|
| 1355 | *
|
| 1356 | * @file: file information
|
| 1357 | * @cmd: cmd field
|
| 1358 | * @arg: Argument field
|
| 1359 | *
|
| 1360 | * Return:
|
| 1361 | * 0 - Success
|
| 1362 | * Non-zero - Failure
|
| 1363 |
|
| 1364 | static long ibmvmc_ioctl(struct file *file,
|
| 1365 | unsigned int cmd, unsigned long arg)
|
| 1366 | {
|
| 1367 | struct ibmvmc_file_session *session = file->private_data;
|
| 1368 |
|
| 1369 | pr_debug("ibmvmc: ioctl file=0x%lx, cmd=0x%x, arg=0x%lx, ses=0x%lx\n",
|
| 1370 | (unsigned long)file, cmd, arg,
|
| 1371 | (unsigned long)session);
|
| 1372 |
|
| 1373 | if (!session) {
|
| 1374 | pr_warn("ibmvmc: ioctl: no session\n");
|
| 1375 | return -EIO;
|
| 1376 | }
|
| 1377 |
|
| 1378 | switch (cmd) {
|
| 1379 | case VMC_IOCTL_SETHMCID:
|
| 1380 | return ibmvmc_ioctl_sethmcid(session,
|
| 1381 | (unsigned char __user *)arg);
|
| 1382 | case VMC_IOCTL_QUERY:
|
| 1383 | return ibmvmc_ioctl_query(session,
|
| 1384 | (struct ibmvmc_query_struct __user *)arg);
|
| 1385 | case VMC_IOCTL_REQUESTVMC:
|
| 1386 | return ibmvmc_ioctl_requestvmc(session,
|
| 1387 | (unsigned int __user *)arg);
|
| 1388 | default:
|
| 1389 | pr_warn("ibmvmc: unknown ioctl 0x%x\n", cmd);
|
| 1390 | return -EINVAL;
|
| 1391 | }
|
| 1392 | }
|
| 1393 |
|
| 1394 | static const struct file_operations ibmvmc_fops = {
|
| 1395 | .owner = THIS_MODULE,
|
| 1396 | .read = ibmvmc_read,
|
| 1397 | .write = ibmvmc_write,
|
| 1398 | .poll = ibmvmc_poll,
|
| 1399 | .unlocked_ioctl = ibmvmc_ioctl,
|
| 1400 | .open = ibmvmc_open,
|
| 1401 | .release = ibmvmc_close,
|
| 1402 | };
|
| 1403 |
|
| 1404 | |
| 1405 | * ibmvmc_add_buffer - Add Buffer
|
| 1406 | *
|
| 1407 | * @adapter: crq_server_adapter struct
|
| 1408 | * @crq: ibmvmc_crq_msg struct
|
| 1409 | *
|
| 1410 | * This message transfers a buffer from hypervisor ownership to management
|
| 1411 | * partition ownership. The LIOBA is obtained from the virtual TCE table
|
| 1412 | * associated with the hypervisor side of the VMC device, and points to a
|
| 1413 | * buffer of size MTU (as established in the capabilities exchange).
|
| 1414 | *
|
| 1415 | * Typical flow for ading buffers:
|
| 1416 | * 1. A new management application connection is opened by the management
|
| 1417 | * partition.
|
| 1418 | * 2. The hypervisor assigns new buffers for the traffic associated with
|
| 1419 | * that connection.
|
| 1420 | * 3. The hypervisor sends VMC Add Buffer messages to the management
|
| 1421 | * partition, informing it of the new buffers.
|
| 1422 | * 4. The hypervisor sends an HMC protocol message (to the management
|
| 1423 | * application) notifying it of the new buffers. This informs the
|
| 1424 | * application that it has buffers available for sending HMC
|
| 1425 | * commands.
|
| 1426 | *
|
| 1427 | * Return:
|
| 1428 | * 0 - Success
|
| 1429 | * Non-zero - Failure
|
| 1430 |
|
| 1431 | static int ibmvmc_add_buffer(struct crq_server_adapter *adapter,
|
| 1432 | struct ibmvmc_crq_msg *crq)
|
| 1433 | {
|
| 1434 | struct ibmvmc_buffer *buffer;
|
| 1435 | u8 hmc_index;
|
| 1436 | u8 hmc_session;
|
| 1437 | u16 buffer_id;
|
| 1438 | unsigned long flags;
|
| 1439 | int rc = 0;
|
| 1440 |
|
| 1441 | if (!crq)
|
| 1442 | return -1;
|
| 1443 |
|
| 1444 | hmc_session = crq->hmc_session;
|
| 1445 | hmc_index = crq->hmc_index;
|
| 1446 | buffer_id = be16_to_cpu(crq->var2.buffer_id);
|
| 1447 |
|
| 1448 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 1449 | dev_err(adapter->dev, "add_buffer: invalid hmc_index = 0x%x\n",
|
| 1450 | hmc_index);
|
| 1451 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INVALID_HMC_INDEX,
|
| 1452 | hmc_session, hmc_index, buffer_id);
|
| 1453 | return -1;
|
| 1454 | }
|
| 1455 |
|
| 1456 | if (buffer_id >= ibmvmc.max_buffer_pool_size) {
|
| 1457 | dev_err(adapter->dev, "add_buffer: invalid buffer_id = 0x%x\n",
|
| 1458 | buffer_id);
|
| 1459 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INVALID_BUFFER_ID,
|
| 1460 | hmc_session, hmc_index, buffer_id);
|
| 1461 | return -1;
|
| 1462 | }
|
| 1463 |
|
| 1464 | spin_lock_irqsave(&hmcs[hmc_index].lock, flags);
|
| 1465 | buffer = &hmcs[hmc_index].buffer[buffer_id];
|
| 1466 |
|
| 1467 | if (buffer->real_addr_local || buffer->dma_addr_local) {
|
| 1468 | dev_warn(adapter->dev, "add_buffer: already allocated id = 0x%lx\n",
|
| 1469 | (unsigned long)buffer_id);
|
| 1470 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 1471 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INVALID_BUFFER_ID,
|
| 1472 | hmc_session, hmc_index, buffer_id);
|
| 1473 | return -1;
|
| 1474 | }
|
| 1475 |
|
| 1476 | buffer->real_addr_local = alloc_dma_buffer(to_vio_dev(adapter->dev),
|
| 1477 | ibmvmc.max_mtu,
|
| 1478 | &buffer->dma_addr_local);
|
| 1479 |
|
| 1480 | if (!buffer->real_addr_local) {
|
| 1481 | dev_err(adapter->dev, "add_buffer: alloc_dma_buffer failed.\n");
|
| 1482 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 1483 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INTERFACE_FAILURE,
|
| 1484 | hmc_session, hmc_index, buffer_id);
|
| 1485 | return -1;
|
| 1486 | }
|
| 1487 |
|
| 1488 | buffer->dma_addr_remote = be32_to_cpu(crq->var3.lioba);
|
| 1489 | buffer->size = ibmvmc.max_mtu;
|
| 1490 | buffer->owner = crq->var1.owner;
|
| 1491 | buffer->free = 1;
|
| 1492 |
|
| 1493 | dma_wmb();
|
| 1494 | buffer->valid = 1;
|
| 1495 | buffer->id = buffer_id;
|
| 1496 |
|
| 1497 | dev_dbg(adapter->dev, "add_buffer: successfully added a buffer:\n");
|
| 1498 | dev_dbg(adapter->dev, " index: %d, session: %d, buffer: 0x%x, owner: %d\n",
|
| 1499 | hmc_index, hmc_session, buffer_id, buffer->owner);
|
| 1500 | dev_dbg(adapter->dev, " local: 0x%x, remote: 0x%x\n",
|
| 1501 | (u32)buffer->dma_addr_local,
|
| 1502 | (u32)buffer->dma_addr_remote);
|
| 1503 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 1504 |
|
| 1505 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_SUCCESS, hmc_session,
|
| 1506 | hmc_index, buffer_id);
|
| 1507 |
|
| 1508 | return rc;
|
| 1509 | }
|
| 1510 |
|
| 1511 | |
| 1512 | * ibmvmc_rem_buffer - Remove Buffer
|
| 1513 | *
|
| 1514 | * @adapter: crq_server_adapter struct
|
| 1515 | * @crq: ibmvmc_crq_msg struct
|
| 1516 | *
|
| 1517 | * This message requests an HMC buffer to be transferred from management
|
| 1518 | * partition ownership to hypervisor ownership. The management partition may
|
| 1519 | * not be able to satisfy the request at a particular point in time if all its
|
| 1520 | * buffers are in use. The management partition requires a depth of at least
|
| 1521 | * one inbound buffer to allow management application commands to flow to the
|
| 1522 | * hypervisor. It is, therefore, an interface error for the hypervisor to
|
| 1523 | * attempt to remove the management partition's last buffer.
|
| 1524 | *
|
| 1525 | * The hypervisor is expected to manage buffer usage with the management
|
| 1526 | * application directly and inform the management partition when buffers may be
|
| 1527 | * removed. The typical flow for removing buffers:
|
| 1528 | *
|
| 1529 | * 1. The management application no longer needs a communication path to a
|
| 1530 | * particular hypervisor function. That function is closed.
|
| 1531 | * 2. The hypervisor and the management application quiesce all traffic to that
|
| 1532 | * function. The hypervisor requests a reduction in buffer pool size.
|
| 1533 | * 3. The management application acknowledges the reduction in buffer pool size.
|
| 1534 | * 4. The hypervisor sends a Remove Buffer message to the management partition,
|
| 1535 | * informing it of the reduction in buffers.
|
| 1536 | * 5. The management partition verifies it can remove the buffer. This is
|
| 1537 | * possible if buffers have been quiesced.
|
| 1538 | *
|
| 1539 | * Return:
|
| 1540 | * 0 - Success
|
| 1541 | * Non-zero - Failure
|
| 1542 |
|
| 1543 | |
| 1544 | * The hypervisor requested that we pick an unused buffer, and return it.
|
| 1545 | * Before sending the buffer back, we free any storage associated with the
|
| 1546 | * buffer.
|
| 1547 |
|
| 1548 | static int ibmvmc_rem_buffer(struct crq_server_adapter *adapter,
|
| 1549 | struct ibmvmc_crq_msg *crq)
|
| 1550 | {
|
| 1551 | struct ibmvmc_buffer *buffer;
|
| 1552 | u8 hmc_index;
|
| 1553 | u8 hmc_session;
|
| 1554 | u16 buffer_id = 0;
|
| 1555 | unsigned long flags;
|
| 1556 | int rc = 0;
|
| 1557 |
|
| 1558 | if (!crq)
|
| 1559 | return -1;
|
| 1560 |
|
| 1561 | hmc_session = crq->hmc_session;
|
| 1562 | hmc_index = crq->hmc_index;
|
| 1563 |
|
| 1564 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 1565 | dev_warn(adapter->dev, "rem_buffer: invalid hmc_index = 0x%x\n",
|
| 1566 | hmc_index);
|
| 1567 | ibmvmc_send_rem_buffer_resp(adapter, VMC_MSG_INVALID_HMC_INDEX,
|
| 1568 | hmc_session, hmc_index, buffer_id);
|
| 1569 | return -1;
|
| 1570 | }
|
| 1571 |
|
| 1572 | spin_lock_irqsave(&hmcs[hmc_index].lock, flags);
|
| 1573 | buffer = ibmvmc_get_free_hmc_buffer(adapter, hmc_index);
|
| 1574 | if (!buffer) {
|
| 1575 | dev_info(adapter->dev, "rem_buffer: no buffer to remove\n");
|
| 1576 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 1577 | ibmvmc_send_rem_buffer_resp(adapter, VMC_MSG_NO_BUFFER,
|
| 1578 | hmc_session, hmc_index,
|
| 1579 | VMC_INVALID_BUFFER_ID);
|
| 1580 | return -1;
|
| 1581 | }
|
| 1582 |
|
| 1583 | buffer_id = buffer->id;
|
| 1584 |
|
| 1585 | if (buffer->valid)
|
| 1586 | free_dma_buffer(to_vio_dev(adapter->dev),
|
| 1587 | ibmvmc.max_mtu,
|
| 1588 | buffer->real_addr_local,
|
| 1589 | buffer->dma_addr_local);
|
| 1590 |
|
| 1591 | memset(buffer, 0, sizeof(struct ibmvmc_buffer));
|
| 1592 | spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
|
| 1593 |
|
| 1594 | dev_dbg(adapter->dev, "rem_buffer: removed buffer 0x%x.\n", buffer_id);
|
| 1595 | ibmvmc_send_rem_buffer_resp(adapter, VMC_MSG_SUCCESS, hmc_session,
|
| 1596 | hmc_index, buffer_id);
|
| 1597 |
|
| 1598 | return rc;
|
| 1599 | }
|
| 1600 |
|
| 1601 | static int ibmvmc_recv_msg(struct crq_server_adapter *adapter,
|
| 1602 | struct ibmvmc_crq_msg *crq)
|
| 1603 | {
|
| 1604 | struct ibmvmc_buffer *buffer;
|
| 1605 | struct ibmvmc_hmc *hmc;
|
| 1606 | unsigned long msg_len;
|
| 1607 | u8 hmc_index;
|
| 1608 | u8 hmc_session;
|
| 1609 | u16 buffer_id;
|
| 1610 | unsigned long flags;
|
| 1611 | int rc = 0;
|
| 1612 |
|
| 1613 | if (!crq)
|
| 1614 | return -1;
|
| 1615 |
|
| 1616 |
|
| 1617 | dev_dbg(adapter->dev, "Recv_msg: msg from HV 0x%016llx 0x%016llx\n",
|
| 1618 | be64_to_cpu(*((unsigned long *)crq)),
|
| 1619 | be64_to_cpu(*(((unsigned long *)crq) + 1)));
|
| 1620 |
|
| 1621 | hmc_session = crq->hmc_session;
|
| 1622 | hmc_index = crq->hmc_index;
|
| 1623 | buffer_id = be16_to_cpu(crq->var2.buffer_id);
|
| 1624 | msg_len = be32_to_cpu(crq->var3.msg_len);
|
| 1625 |
|
| 1626 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 1627 | dev_err(adapter->dev, "Recv_msg: invalid hmc_index = 0x%x\n",
|
| 1628 | hmc_index);
|
| 1629 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INVALID_HMC_INDEX,
|
| 1630 | hmc_session, hmc_index, buffer_id);
|
| 1631 | return -1;
|
| 1632 | }
|
| 1633 |
|
| 1634 | if (buffer_id >= ibmvmc.max_buffer_pool_size) {
|
| 1635 | dev_err(adapter->dev, "Recv_msg: invalid buffer_id = 0x%x\n",
|
| 1636 | buffer_id);
|
| 1637 | ibmvmc_send_add_buffer_resp(adapter, VMC_MSG_INVALID_BUFFER_ID,
|
| 1638 | hmc_session, hmc_index, buffer_id);
|
| 1639 | return -1;
|
| 1640 | }
|
| 1641 |
|
| 1642 | hmc = &hmcs[hmc_index];
|
| 1643 | spin_lock_irqsave(&hmc->lock, flags);
|
| 1644 |
|
| 1645 | if (hmc->state == ibmhmc_state_free) {
|
| 1646 | dev_err(adapter->dev, "Recv_msg: invalid hmc state = 0x%x\n",
|
| 1647 | hmc->state);
|
| 1648 |
|
| 1649 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1650 | return -1;
|
| 1651 | }
|
| 1652 |
|
| 1653 | buffer = &hmc->buffer[buffer_id];
|
| 1654 |
|
| 1655 | if (buffer->valid == 0 || buffer->owner == VMC_BUF_OWNER_ALPHA) {
|
| 1656 | dev_err(adapter->dev, "Recv_msg: not valid, or not HV. 0x%x 0x%x\n",
|
| 1657 | buffer->valid, buffer->owner);
|
| 1658 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1659 | return -1;
|
| 1660 | }
|
| 1661 |
|
| 1662 | if (msg_len > buffer->size) {
|
| 1663 | dev_err(adapter->dev, "Recv_msg: msg_len 0x%lx exceeds buffer size 0x%x\n",
|
| 1664 | msg_len, buffer->size);
|
| 1665 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1666 | return -1;
|
| 1667 | }
|
| 1668 |
|
| 1669 |
|
| 1670 | rc = h_copy_rdma(msg_len,
|
| 1671 | adapter->riobn,
|
| 1672 | buffer->dma_addr_remote,
|
| 1673 | adapter->liobn,
|
| 1674 | buffer->dma_addr_local);
|
| 1675 |
|
| 1676 | dev_dbg(adapter->dev, "Recv_msg: msg_len = 0x%x, buffer_id = 0x%x, queue_head = 0x%x, hmc_idx = 0x%x\n",
|
| 1677 | (unsigned int)msg_len, (unsigned int)buffer_id,
|
| 1678 | (unsigned int)hmc->queue_head, (unsigned int)hmc_index);
|
| 1679 | buffer->msg_len = msg_len;
|
| 1680 | buffer->free = 0;
|
| 1681 | buffer->owner = VMC_BUF_OWNER_ALPHA;
|
| 1682 |
|
| 1683 | if (rc) {
|
| 1684 | dev_err(adapter->dev, "Failure in recv_msg: h_copy_rdma = 0x%x\n",
|
| 1685 | rc);
|
| 1686 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1687 | return -1;
|
| 1688 | }
|
| 1689 |
|
| 1690 |
|
| 1691 | hmc->queue_outbound_msgs[hmc->queue_head] = buffer_id;
|
| 1692 | hmc->queue_head++;
|
| 1693 | if (hmc->queue_head == ibmvmc_max_buf_pool_size)
|
| 1694 | hmc->queue_head = 0;
|
| 1695 |
|
| 1696 | if (hmc->queue_head == hmc->queue_tail)
|
| 1697 | dev_err(adapter->dev, "outbound buffer queue wrapped.\n");
|
| 1698 |
|
| 1699 | spin_unlock_irqrestore(&hmc->lock, flags);
|
| 1700 |
|
| 1701 | wake_up_interruptible(&ibmvmc_read_wait);
|
| 1702 |
|
| 1703 | return 0;
|
| 1704 | }
|
| 1705 |
|
| 1706 | |
| 1707 | * ibmvmc_process_capabilities - Process Capabilities
|
| 1708 | *
|
| 1709 | * @adapter: crq_server_adapter struct
|
| 1710 | * @crqp: ibmvmc_crq_msg struct
|
| 1711 | *
|
| 1712 |
|
| 1713 | static void ibmvmc_process_capabilities(struct crq_server_adapter *adapter,
|
| 1714 | struct ibmvmc_crq_msg *crqp)
|
| 1715 | {
|
| 1716 | struct ibmvmc_admin_crq_msg *crq = (struct ibmvmc_admin_crq_msg *)crqp;
|
| 1717 |
|
| 1718 | if ((be16_to_cpu(crq->version) >> 8) !=
|
| 1719 | (IBMVMC_PROTOCOL_VERSION >> 8)) {
|
| 1720 | dev_err(adapter->dev, "init failed, incompatible versions 0x%x 0x%x\n",
|
| 1721 | be16_to_cpu(crq->version),
|
| 1722 | IBMVMC_PROTOCOL_VERSION);
|
| 1723 | ibmvmc.state = ibmvmc_state_failed;
|
| 1724 | return;
|
| 1725 | }
|
| 1726 |
|
| 1727 | ibmvmc.max_mtu = min_t(u32, ibmvmc_max_mtu, be32_to_cpu(crq->max_mtu));
|
| 1728 | ibmvmc.max_buffer_pool_size = min_t(u16, ibmvmc_max_buf_pool_size,
|
| 1729 | be16_to_cpu(crq->pool_size));
|
| 1730 | ibmvmc.max_hmc_index = min_t(u8, ibmvmc_max_hmcs, crq->max_hmc) - 1;
|
| 1731 | ibmvmc.state = ibmvmc_state_ready;
|
| 1732 |
|
| 1733 | dev_info(adapter->dev, "Capabilities: mtu=0x%x, pool_size=0x%x, max_hmc=0x%x\n",
|
| 1734 | ibmvmc.max_mtu, ibmvmc.max_buffer_pool_size,
|
| 1735 | ibmvmc.max_hmc_index);
|
| 1736 | }
|
| 1737 |
|
| 1738 | |
| 1739 | * ibmvmc_validate_hmc_session - Validate HMC Session
|
| 1740 | *
|
| 1741 | * @adapter: crq_server_adapter struct
|
| 1742 | * @crq: ibmvmc_crq_msg struct
|
| 1743 | *
|
| 1744 | * Return:
|
| 1745 | * 0 - Success
|
| 1746 | * Non-zero - Failure
|
| 1747 |
|
| 1748 | static int ibmvmc_validate_hmc_session(struct crq_server_adapter *adapter,
|
| 1749 | struct ibmvmc_crq_msg *crq)
|
| 1750 | {
|
| 1751 | unsigned char hmc_index;
|
| 1752 |
|
| 1753 | hmc_index = crq->hmc_index;
|
| 1754 |
|
| 1755 | if (crq->hmc_session == 0)
|
| 1756 | return 0;
|
| 1757 |
|
| 1758 | if (hmc_index > ibmvmc.max_hmc_index)
|
| 1759 | return -1;
|
| 1760 |
|
| 1761 | if (hmcs[hmc_index].session != crq->hmc_session) {
|
| 1762 | dev_warn(adapter->dev, "Drop, bad session: expected 0x%x, recv 0x%x\n",
|
| 1763 | hmcs[hmc_index].session, crq->hmc_session);
|
| 1764 | return -1;
|
| 1765 | }
|
| 1766 |
|
| 1767 | return 0;
|
| 1768 | }
|
| 1769 |
|
| 1770 | |
| 1771 | * ibmvmc_reset - Reset
|
| 1772 | *
|
| 1773 | * @adapter: crq_server_adapter struct
|
| 1774 | * @xport_event: export_event field
|
| 1775 | *
|
| 1776 | * Closes all HMC sessions and conditionally schedules a CRQ reset.
|
| 1777 | * @xport_event: If true, the partner closed their CRQ; we don't need to reset.
|
| 1778 | * If false, we need to schedule a CRQ reset.
|
| 1779 |
|
| 1780 | static void ibmvmc_reset(struct crq_server_adapter *adapter, bool xport_event)
|
| 1781 | {
|
| 1782 | int i;
|
| 1783 |
|
| 1784 | if (ibmvmc.state != ibmvmc_state_sched_reset) {
|
| 1785 | dev_info(adapter->dev, "*** Reset to initial state.\n");
|
| 1786 | for (i = 0; i < ibmvmc_max_hmcs; i++)
|
| 1787 | ibmvmc_return_hmc(&hmcs[i], xport_event);
|
| 1788 |
|
| 1789 | if (xport_event) {
|
| 1790 | |
| 1791 | * anything except set ourself to the correct state to
|
| 1792 | * handle init msgs.
|
| 1793 |
|
| 1794 | ibmvmc.state = ibmvmc_state_crqinit;
|
| 1795 | } else {
|
| 1796 | |
| 1797 | * closing the CRQ on our end. Need to schedule this
|
| 1798 | * for process context, because CRQ reset may require a
|
| 1799 | * sleep.
|
| 1800 | *
|
| 1801 | * Setting ibmvmc.state here immediately prevents
|
| 1802 | * ibmvmc_open from completing until the reset
|
| 1803 | * completes in process context.
|
| 1804 |
|
| 1805 | ibmvmc.state = ibmvmc_state_sched_reset;
|
| 1806 | dev_dbg(adapter->dev, "Device reset scheduled");
|
| 1807 | wake_up_interruptible(&adapter->reset_wait_queue);
|
| 1808 | }
|
| 1809 | }
|
| 1810 | }
|
| 1811 |
|
| 1812 | |
| 1813 | * ibmvmc_reset_task - Reset Task
|
| 1814 | *
|
| 1815 | * @data: Data field
|
| 1816 | *
|
| 1817 | * Performs a CRQ reset of the VMC device in process context.
|
| 1818 | * NOTE: This function should not be called directly, use ibmvmc_reset.
|
| 1819 |
|
| 1820 | static int ibmvmc_reset_task(void *data)
|
| 1821 | {
|
| 1822 | struct crq_server_adapter *adapter = data;
|
| 1823 | int rc;
|
| 1824 |
|
| 1825 | set_user_nice(current, -20);
|
| 1826 |
|
| 1827 | while (!kthread_should_stop()) {
|
| 1828 | wait_event_interruptible(adapter->reset_wait_queue,
|
| 1829 | (ibmvmc.state == ibmvmc_state_sched_reset) ||
|
| 1830 | kthread_should_stop());
|
| 1831 |
|
| 1832 | if (kthread_should_stop())
|
| 1833 | break;
|
| 1834 |
|
| 1835 | dev_dbg(adapter->dev, "CRQ resetting in process context");
|
| 1836 | tasklet_disable(&adapter->work_task);
|
| 1837 |
|
| 1838 | rc = ibmvmc_reset_crq_queue(adapter);
|
| 1839 |
|
| 1840 | if (rc != H_SUCCESS && rc != H_RESOURCE) {
|
| 1841 | dev_err(adapter->dev, "Error initializing CRQ. rc = 0x%x\n",
|
| 1842 | rc);
|
| 1843 | ibmvmc.state = ibmvmc_state_failed;
|
| 1844 | } else {
|
| 1845 | ibmvmc.state = ibmvmc_state_crqinit;
|
| 1846 |
|
| 1847 | if (ibmvmc_send_crq(adapter, 0xC001000000000000LL, 0)
|
| 1848 | != 0 && rc != H_RESOURCE)
|
| 1849 | dev_warn(adapter->dev, "Failed to send initialize CRQ message\n");
|
| 1850 | }
|
| 1851 |
|
| 1852 | vio_enable_interrupts(to_vio_dev(adapter->dev));
|
| 1853 | tasklet_enable(&adapter->work_task);
|
| 1854 | }
|
| 1855 |
|
| 1856 | return 0;
|
| 1857 | }
|
| 1858 |
|
| 1859 | |
| 1860 | * ibmvmc_process_open_resp - Process Open Response
|
| 1861 | *
|
| 1862 | * @crq: ibmvmc_crq_msg struct
|
| 1863 | * @adapter: crq_server_adapter struct
|
| 1864 | *
|
| 1865 | * This command is sent by the hypervisor in response to the Interface
|
| 1866 | * Open message. When this message is received, the indicated buffer is
|
| 1867 | * again available for management partition use.
|
| 1868 |
|
| 1869 | static void ibmvmc_process_open_resp(struct ibmvmc_crq_msg *crq,
|
| 1870 | struct crq_server_adapter *adapter)
|
| 1871 | {
|
| 1872 | unsigned char hmc_index;
|
| 1873 | unsigned short buffer_id;
|
| 1874 |
|
| 1875 | hmc_index = crq->hmc_index;
|
| 1876 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 1877 |
|
| 1878 | ibmvmc_reset(adapter, false);
|
| 1879 | return;
|
| 1880 | }
|
| 1881 |
|
| 1882 | if (crq->status) {
|
| 1883 | dev_warn(adapter->dev, "open_resp: failed - status 0x%x\n",
|
| 1884 | crq->status);
|
| 1885 | ibmvmc_return_hmc(&hmcs[hmc_index], false);
|
| 1886 | return;
|
| 1887 | }
|
| 1888 |
|
| 1889 | if (hmcs[hmc_index].state == ibmhmc_state_opening) {
|
| 1890 | buffer_id = be16_to_cpu(crq->var2.buffer_id);
|
| 1891 | if (buffer_id >= ibmvmc.max_buffer_pool_size) {
|
| 1892 | dev_err(adapter->dev, "open_resp: invalid buffer_id = 0x%x\n",
|
| 1893 | buffer_id);
|
| 1894 | hmcs[hmc_index].state = ibmhmc_state_failed;
|
| 1895 | } else {
|
| 1896 | ibmvmc_free_hmc_buffer(&hmcs[hmc_index],
|
| 1897 | &hmcs[hmc_index].buffer[buffer_id]);
|
| 1898 | hmcs[hmc_index].state = ibmhmc_state_ready;
|
| 1899 | dev_dbg(adapter->dev, "open_resp: set hmc state = ready\n");
|
| 1900 | }
|
| 1901 | } else {
|
| 1902 | dev_warn(adapter->dev, "open_resp: invalid hmc state (0x%x)\n",
|
| 1903 | hmcs[hmc_index].state);
|
| 1904 | }
|
| 1905 | }
|
| 1906 |
|
| 1907 | |
| 1908 | * ibmvmc_process_close_resp - Process Close Response
|
| 1909 | *
|
| 1910 | * @crq: ibmvmc_crq_msg struct
|
| 1911 | * @adapter: crq_server_adapter struct
|
| 1912 | *
|
| 1913 | * This command is sent by the hypervisor in response to the managemant
|
| 1914 | * application Interface Close message.
|
| 1915 | *
|
| 1916 | * If the close fails, simply reset the entire driver as the state of the VMC
|
| 1917 | * must be in tough shape.
|
| 1918 |
|
| 1919 | static void ibmvmc_process_close_resp(struct ibmvmc_crq_msg *crq,
|
| 1920 | struct crq_server_adapter *adapter)
|
| 1921 | {
|
| 1922 | unsigned char hmc_index;
|
| 1923 |
|
| 1924 | hmc_index = crq->hmc_index;
|
| 1925 | if (hmc_index > ibmvmc.max_hmc_index) {
|
| 1926 | ibmvmc_reset(adapter, false);
|
| 1927 | return;
|
| 1928 | }
|
| 1929 |
|
| 1930 | if (crq->status) {
|
| 1931 | dev_warn(adapter->dev, "close_resp: failed - status 0x%x\n",
|
| 1932 | crq->status);
|
| 1933 | ibmvmc_reset(adapter, false);
|
| 1934 | return;
|
| 1935 | }
|
| 1936 |
|
| 1937 | ibmvmc_return_hmc(&hmcs[hmc_index], false);
|
| 1938 | }
|
| 1939 |
|
| 1940 | |
| 1941 | * ibmvmc_crq_process - Process CRQ
|
| 1942 | *
|
| 1943 | * @adapter: crq_server_adapter struct
|
| 1944 | * @crq: ibmvmc_crq_msg struct
|
| 1945 | *
|
| 1946 | * Process the CRQ message based upon the type of message received.
|
| 1947 | *
|
| 1948 |
|
| 1949 | static void ibmvmc_crq_process(struct crq_server_adapter *adapter,
|
| 1950 | struct ibmvmc_crq_msg *crq)
|
| 1951 | {
|
| 1952 | switch (crq->type) {
|
| 1953 | case VMC_MSG_CAP_RESP:
|
| 1954 | dev_dbg(adapter->dev, "CRQ recv: capabilities resp (0x%x)\n",
|
| 1955 | crq->type);
|
| 1956 | if (ibmvmc.state == ibmvmc_state_capabilities)
|
| 1957 | ibmvmc_process_capabilities(adapter, crq);
|
| 1958 | else
|
| 1959 | dev_warn(adapter->dev, "caps msg invalid in state 0x%x\n",
|
| 1960 | ibmvmc.state);
|
| 1961 | break;
|
| 1962 | case VMC_MSG_OPEN_RESP:
|
| 1963 | dev_dbg(adapter->dev, "CRQ recv: open resp (0x%x)\n",
|
| 1964 | crq->type);
|
| 1965 | if (ibmvmc_validate_hmc_session(adapter, crq) == 0)
|
| 1966 | ibmvmc_process_open_resp(crq, adapter);
|
| 1967 | break;
|
| 1968 | case VMC_MSG_ADD_BUF:
|
| 1969 | dev_dbg(adapter->dev, "CRQ recv: add buf (0x%x)\n",
|
| 1970 | crq->type);
|
| 1971 | if (ibmvmc_validate_hmc_session(adapter, crq) == 0)
|
| 1972 | ibmvmc_add_buffer(adapter, crq);
|
| 1973 | break;
|
| 1974 | case VMC_MSG_REM_BUF:
|
| 1975 | dev_dbg(adapter->dev, "CRQ recv: rem buf (0x%x)\n",
|
| 1976 | crq->type);
|
| 1977 | if (ibmvmc_validate_hmc_session(adapter, crq) == 0)
|
| 1978 | ibmvmc_rem_buffer(adapter, crq);
|
| 1979 | break;
|
| 1980 | case VMC_MSG_SIGNAL:
|
| 1981 | dev_dbg(adapter->dev, "CRQ recv: signal msg (0x%x)\n",
|
| 1982 | crq->type);
|
| 1983 | if (ibmvmc_validate_hmc_session(adapter, crq) == 0)
|
| 1984 | ibmvmc_recv_msg(adapter, crq);
|
| 1985 | break;
|
| 1986 | case VMC_MSG_CLOSE_RESP:
|
| 1987 | dev_dbg(adapter->dev, "CRQ recv: close resp (0x%x)\n",
|
| 1988 | crq->type);
|
| 1989 | if (ibmvmc_validate_hmc_session(adapter, crq) == 0)
|
| 1990 | ibmvmc_process_close_resp(crq, adapter);
|
| 1991 | break;
|
| 1992 | case VMC_MSG_CAP:
|
| 1993 | case VMC_MSG_OPEN:
|
| 1994 | case VMC_MSG_CLOSE:
|
| 1995 | case VMC_MSG_ADD_BUF_RESP:
|
| 1996 | case VMC_MSG_REM_BUF_RESP:
|
| 1997 | dev_warn(adapter->dev, "CRQ recv: unexpected msg (0x%x)\n",
|
| 1998 | crq->type);
|
| 1999 | break;
|
| 2000 | default:
|
| 2001 | dev_warn(adapter->dev, "CRQ recv: unknown msg (0x%x)\n",
|
| 2002 | crq->type);
|
| 2003 | break;
|
| 2004 | }
|
| 2005 | }
|
| 2006 |
|
| 2007 | |
| 2008 | * ibmvmc_handle_crq_init - Handle CRQ Init
|
| 2009 | *
|
| 2010 | * @crq: ibmvmc_crq_msg struct
|
| 2011 | * @adapter: crq_server_adapter struct
|
| 2012 | *
|
| 2013 | * Handle the type of crq initialization based on whether
|
| 2014 | * it is a message or a response.
|
| 2015 | *
|
| 2016 |
|
| 2017 | static void ibmvmc_handle_crq_init(struct ibmvmc_crq_msg *crq,
|
| 2018 | struct crq_server_adapter *adapter)
|
| 2019 | {
|
| 2020 | switch (crq->type) {
|
| 2021 | case 0x01:
|
| 2022 | dev_dbg(adapter->dev, "CRQ recv: CRQ init msg - state 0x%x\n",
|
| 2023 | ibmvmc.state);
|
| 2024 | if (ibmvmc.state == ibmvmc_state_crqinit) {
|
| 2025 |
|
| 2026 | if (ibmvmc_send_crq(adapter, 0xC002000000000000,
|
| 2027 | 0) == 0)
|
| 2028 | ibmvmc_send_capabilities(adapter);
|
| 2029 | else
|
| 2030 | dev_err(adapter->dev, " Unable to send init rsp\n");
|
| 2031 | } else {
|
| 2032 | dev_err(adapter->dev, "Invalid state 0x%x mtu = 0x%x\n",
|
| 2033 | ibmvmc.state, ibmvmc.max_mtu);
|
| 2034 | }
|
| 2035 |
|
| 2036 | break;
|
| 2037 | case 0x02:
|
| 2038 | dev_dbg(adapter->dev, "CRQ recv: initialization resp msg - state 0x%x\n",
|
| 2039 | ibmvmc.state);
|
| 2040 | if (ibmvmc.state == ibmvmc_state_crqinit)
|
| 2041 | ibmvmc_send_capabilities(adapter);
|
| 2042 | break;
|
| 2043 | default:
|
| 2044 | dev_warn(adapter->dev, "Unknown crq message type 0x%lx\n",
|
| 2045 | (unsigned long)crq->type);
|
| 2046 | }
|
| 2047 | }
|
| 2048 |
|
| 2049 | |
| 2050 | * ibmvmc_handle_crq - Handle CRQ
|
| 2051 | *
|
| 2052 | * @crq: ibmvmc_crq_msg struct
|
| 2053 | * @adapter: crq_server_adapter struct
|
| 2054 | *
|
| 2055 | * Read the command elements from the command queue and execute the
|
| 2056 | * requests based upon the type of crq message.
|
| 2057 | *
|
| 2058 |
|
| 2059 | static void ibmvmc_handle_crq(struct ibmvmc_crq_msg *crq,
|
| 2060 | struct crq_server_adapter *adapter)
|
| 2061 | {
|
| 2062 | switch (crq->valid) {
|
| 2063 | case 0xC0:
|
| 2064 | ibmvmc_handle_crq_init(crq, adapter);
|
| 2065 | break;
|
| 2066 | case 0xFF:
|
| 2067 | dev_warn(adapter->dev, "CRQ recv: virtual adapter failed - resetting.\n");
|
| 2068 | ibmvmc_reset(adapter, true);
|
| 2069 | break;
|
| 2070 | case 0x80:
|
| 2071 | ibmvmc_crq_process(adapter, crq);
|
| 2072 | break;
|
| 2073 | default:
|
| 2074 | dev_warn(adapter->dev, "CRQ recv: unknown msg 0x%02x.\n",
|
| 2075 | crq->valid);
|
| 2076 | break;
|
| 2077 | }
|
| 2078 | }
|
| 2079 |
|
| 2080 | static void ibmvmc_task(unsigned long data)
|
| 2081 | {
|
| 2082 | struct crq_server_adapter *adapter =
|
| 2083 | (struct crq_server_adapter *)data;
|
| 2084 | struct vio_dev *vdev = to_vio_dev(adapter->dev);
|
| 2085 | struct ibmvmc_crq_msg *crq;
|
| 2086 | int done = 0;
|
| 2087 |
|
| 2088 | while (!done) {
|
| 2089 |
|
| 2090 | while ((crq = crq_queue_next_crq(&adapter->queue)) != NULL) {
|
| 2091 | ibmvmc_handle_crq(crq, adapter);
|
| 2092 | crq->valid = 0x00;
|
| 2093 | |
| 2094 | * Interrupts will be re-enabled by the reset task.
|
| 2095 |
|
| 2096 | if (ibmvmc.state == ibmvmc_state_sched_reset)
|
| 2097 | return;
|
| 2098 | }
|
| 2099 |
|
| 2100 | vio_enable_interrupts(vdev);
|
| 2101 | crq = crq_queue_next_crq(&adapter->queue);
|
| 2102 | if (crq) {
|
| 2103 | vio_disable_interrupts(vdev);
|
| 2104 | ibmvmc_handle_crq(crq, adapter);
|
| 2105 | crq->valid = 0x00;
|
| 2106 | |
| 2107 | * Interrupts will be re-enabled by the reset task.
|
| 2108 |
|
| 2109 | if (ibmvmc.state == ibmvmc_state_sched_reset)
|
| 2110 | return;
|
| 2111 | } else {
|
| 2112 | done = 1;
|
| 2113 | }
|
| 2114 | }
|
| 2115 | }
|
| 2116 |
|
| 2117 | |
| 2118 | * ibmvmc_init_crq_queue - Init CRQ Queue
|
| 2119 | *
|
| 2120 | * @adapter: crq_server_adapter struct
|
| 2121 | *
|
| 2122 | * Return:
|
| 2123 | * 0 - Success
|
| 2124 | * Non-zero - Failure
|
| 2125 |
|
| 2126 | static int ibmvmc_init_crq_queue(struct crq_server_adapter *adapter)
|
| 2127 | {
|
| 2128 | struct vio_dev *vdev = to_vio_dev(adapter->dev);
|
| 2129 | struct crq_queue *queue = &adapter->queue;
|
| 2130 | int rc = 0;
|
| 2131 | int retrc = 0;
|
| 2132 |
|
| 2133 | queue->msgs = (struct ibmvmc_crq_msg *)get_zeroed_page(GFP_KERNEL);
|
| 2134 |
|
| 2135 | if (!queue->msgs)
|
| 2136 | goto malloc_failed;
|
| 2137 |
|
| 2138 | queue->size = PAGE_SIZE / sizeof(*queue->msgs);
|
| 2139 |
|
| 2140 | queue->msg_token = dma_map_single(adapter->dev, queue->msgs,
|
| 2141 | queue->size * sizeof(*queue->msgs),
|
| 2142 | DMA_BIDIRECTIONAL);
|
| 2143 |
|
| 2144 | if (dma_mapping_error(adapter->dev, queue->msg_token))
|
| 2145 | goto map_failed;
|
| 2146 |
|
| 2147 | retrc = plpar_hcall_norets(H_REG_CRQ,
|
| 2148 | vdev->unit_address,
|
| 2149 | queue->msg_token, PAGE_SIZE);
|
| 2150 | rc = retrc;
|
| 2151 |
|
| 2152 | if (rc == H_RESOURCE)
|
| 2153 | rc = ibmvmc_reset_crq_queue(adapter);
|
| 2154 |
|
| 2155 | if (rc == 2) {
|
| 2156 | dev_warn(adapter->dev, "Partner adapter not ready\n");
|
| 2157 | retrc = 0;
|
| 2158 | } else if (rc != 0) {
|
| 2159 | dev_err(adapter->dev, "Error %d opening adapter\n", rc);
|
| 2160 | goto reg_crq_failed;
|
| 2161 | }
|
| 2162 |
|
| 2163 | queue->cur = 0;
|
| 2164 | spin_lock_init(&queue->lock);
|
| 2165 |
|
| 2166 | tasklet_init(&adapter->work_task, ibmvmc_task, (unsigned long)adapter);
|
| 2167 |
|
| 2168 | if (request_irq(vdev->irq,
|
| 2169 | ibmvmc_handle_event,
|
| 2170 | 0, "ibmvmc", (void *)adapter) != 0) {
|
| 2171 | dev_err(adapter->dev, "couldn't register irq 0x%x\n",
|
| 2172 | vdev->irq);
|
| 2173 | goto req_irq_failed;
|
| 2174 | }
|
| 2175 |
|
| 2176 | rc = vio_enable_interrupts(vdev);
|
| 2177 | if (rc != 0) {
|
| 2178 | dev_err(adapter->dev, "Error %d enabling interrupts!!!\n", rc);
|
| 2179 | goto req_irq_failed;
|
| 2180 | }
|
| 2181 |
|
| 2182 | return retrc;
|
| 2183 |
|
| 2184 | req_irq_failed:
|
| 2185 | |
| 2186 | * or never got interrupts enabled
|
| 2187 |
|
| 2188 | tasklet_kill(&adapter->work_task);
|
| 2189 | h_free_crq(vdev->unit_address);
|
| 2190 | reg_crq_failed:
|
| 2191 | dma_unmap_single(adapter->dev,
|
| 2192 | queue->msg_token,
|
| 2193 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
|
| 2194 | map_failed:
|
| 2195 | free_page((unsigned long)queue->msgs);
|
| 2196 | malloc_failed:
|
| 2197 | return -ENOMEM;
|
| 2198 | }
|
| 2199 |
|
| 2200 |
|
| 2201 | static int read_dma_window(struct vio_dev *vdev,
|
| 2202 | struct crq_server_adapter *adapter)
|
| 2203 | {
|
| 2204 | const __be32 *dma_window;
|
| 2205 | const __be32 *prop;
|
| 2206 |
|
| 2207 | |
| 2208 | * a way to read multiple windows without already knowing the size of
|
| 2209 | * a window or the number of windows
|
| 2210 |
|
| 2211 | dma_window =
|
| 2212 | (const __be32 *)vio_get_attribute(vdev, "ibm,my-dma-window",
|
| 2213 | NULL);
|
| 2214 | if (!dma_window) {
|
| 2215 | dev_warn(adapter->dev, "Couldn't find ibm,my-dma-window property\n");
|
| 2216 | return -1;
|
| 2217 | }
|
| 2218 |
|
| 2219 | adapter->liobn = be32_to_cpu(*dma_window);
|
| 2220 | dma_window++;
|
| 2221 |
|
| 2222 | prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
|
| 2223 | NULL);
|
| 2224 | if (!prop) {
|
| 2225 | dev_warn(adapter->dev, "Couldn't find ibm,#dma-address-cells property\n");
|
| 2226 | dma_window++;
|
| 2227 | } else {
|
| 2228 | dma_window += be32_to_cpu(*prop);
|
| 2229 | }
|
| 2230 |
|
| 2231 | prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
|
| 2232 | NULL);
|
| 2233 | if (!prop) {
|
| 2234 | dev_warn(adapter->dev, "Couldn't find ibm,#dma-size-cells property\n");
|
| 2235 | dma_window++;
|
| 2236 | } else {
|
| 2237 | dma_window += be32_to_cpu(*prop);
|
| 2238 | }
|
| 2239 |
|
| 2240 |
|
| 2241 | adapter->riobn = be32_to_cpu(*dma_window);
|
| 2242 |
|
| 2243 | return 0;
|
| 2244 | }
|
| 2245 |
|
| 2246 | static int ibmvmc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
|
| 2247 | {
|
| 2248 | struct crq_server_adapter *adapter = &ibmvmc_adapter;
|
| 2249 | int rc;
|
| 2250 |
|
| 2251 | dev_set_drvdata(&vdev->dev, NULL);
|
| 2252 | memset(adapter, 0, sizeof(*adapter));
|
| 2253 | adapter->dev = &vdev->dev;
|
| 2254 |
|
| 2255 | dev_info(adapter->dev, "Probe for UA 0x%x\n", vdev->unit_address);
|
| 2256 |
|
| 2257 | rc = read_dma_window(vdev, adapter);
|
| 2258 | if (rc != 0) {
|
| 2259 | ibmvmc.state = ibmvmc_state_failed;
|
| 2260 | return -1;
|
| 2261 | }
|
| 2262 |
|
| 2263 | dev_dbg(adapter->dev, "Probe: liobn 0x%x, riobn 0x%x\n",
|
| 2264 | adapter->liobn, adapter->riobn);
|
| 2265 |
|
| 2266 | init_waitqueue_head(&adapter->reset_wait_queue);
|
| 2267 | adapter->reset_task = kthread_run(ibmvmc_reset_task, adapter, "ibmvmc");
|
| 2268 | if (IS_ERR(adapter->reset_task)) {
|
| 2269 | dev_err(adapter->dev, "Failed to start reset thread\n");
|
| 2270 | ibmvmc.state = ibmvmc_state_failed;
|
| 2271 | rc = PTR_ERR(adapter->reset_task);
|
| 2272 | adapter->reset_task = NULL;
|
| 2273 | return rc;
|
| 2274 | }
|
| 2275 |
|
| 2276 | rc = ibmvmc_init_crq_queue(adapter);
|
| 2277 | if (rc != 0 && rc != H_RESOURCE) {
|
| 2278 | dev_err(adapter->dev, "Error initializing CRQ. rc = 0x%x\n",
|
| 2279 | rc);
|
| 2280 | ibmvmc.state = ibmvmc_state_failed;
|
| 2281 | goto crq_failed;
|
| 2282 | }
|
| 2283 |
|
| 2284 | ibmvmc.state = ibmvmc_state_crqinit;
|
| 2285 |
|
| 2286 | |
| 2287 | * to fail if the other end is not acive. In that case we just wait
|
| 2288 | * for the other side to initialize.
|
| 2289 |
|
| 2290 | if (ibmvmc_send_crq(adapter, 0xC001000000000000LL, 0) != 0 &&
|
| 2291 | rc != H_RESOURCE)
|
| 2292 | dev_warn(adapter->dev, "Failed to send initialize CRQ message\n");
|
| 2293 |
|
| 2294 | dev_set_drvdata(&vdev->dev, adapter);
|
| 2295 |
|
| 2296 | return 0;
|
| 2297 |
|
| 2298 | crq_failed:
|
| 2299 | kthread_stop(adapter->reset_task);
|
| 2300 | adapter->reset_task = NULL;
|
| 2301 | return -EPERM;
|
| 2302 | }
|
| 2303 |
|
| 2304 | static void ibmvmc_remove(struct vio_dev *vdev)
|
| 2305 | {
|
| 2306 | struct crq_server_adapter *adapter = dev_get_drvdata(&vdev->dev);
|
| 2307 |
|
| 2308 | dev_info(adapter->dev, "Entering remove for UA 0x%x\n",
|
| 2309 | vdev->unit_address);
|
| 2310 | ibmvmc_release_crq_queue(adapter);
|
| 2311 | }
|
| 2312 |
|
| 2313 | static struct vio_device_id ibmvmc_device_table[] = {
|
| 2314 | { .type = "ibm,vmc", .compat = "IBM,vmc" },
|
| 2315 | { }
|
| 2316 | };
|
| 2317 | MODULE_DEVICE_TABLE(vio, ibmvmc_device_table);
|
| 2318 |
|
| 2319 | static struct vio_driver ibmvmc_driver = {
|
| 2320 | .name = ibmvmc_driver_name,
|
| 2321 | .id_table = ibmvmc_device_table,
|
| 2322 | .probe = ibmvmc_probe,
|
| 2323 | .remove = ibmvmc_remove,
|
| 2324 | };
|
| 2325 |
|
| 2326 | static void __init ibmvmc_scrub_module_parms(void)
|
| 2327 | {
|
| 2328 | if (ibmvmc_max_mtu > MAX_MTU) {
|
| 2329 | pr_warn("ibmvmc: Max MTU reduced to %d\n", MAX_MTU);
|
| 2330 | ibmvmc_max_mtu = MAX_MTU;
|
| 2331 | } else if (ibmvmc_max_mtu < MIN_MTU) {
|
| 2332 | pr_warn("ibmvmc: Max MTU increased to %d\n", MIN_MTU);
|
| 2333 | ibmvmc_max_mtu = MIN_MTU;
|
| 2334 | }
|
| 2335 |
|
| 2336 | if (ibmvmc_max_buf_pool_size > MAX_BUF_POOL_SIZE) {
|
| 2337 | pr_warn("ibmvmc: Max buffer pool size reduced to %d\n",
|
| 2338 | MAX_BUF_POOL_SIZE);
|
| 2339 | ibmvmc_max_buf_pool_size = MAX_BUF_POOL_SIZE;
|
| 2340 | } else if (ibmvmc_max_buf_pool_size < MIN_BUF_POOL_SIZE) {
|
| 2341 | pr_warn("ibmvmc: Max buffer pool size increased to %d\n",
|
| 2342 | MIN_BUF_POOL_SIZE);
|
| 2343 | ibmvmc_max_buf_pool_size = MIN_BUF_POOL_SIZE;
|
| 2344 | }
|
| 2345 |
|
| 2346 | if (ibmvmc_max_hmcs > MAX_HMCS) {
|
| 2347 | pr_warn("ibmvmc: Max HMCs reduced to %d\n", MAX_HMCS);
|
| 2348 | ibmvmc_max_hmcs = MAX_HMCS;
|
| 2349 | } else if (ibmvmc_max_hmcs < MIN_HMCS) {
|
| 2350 | pr_warn("ibmvmc: Max HMCs increased to %d\n", MIN_HMCS);
|
| 2351 | ibmvmc_max_hmcs = MIN_HMCS;
|
| 2352 | }
|
| 2353 | }
|
| 2354 |
|
| 2355 | static struct miscdevice ibmvmc_miscdev = {
|
| 2356 | .name = ibmvmc_driver_name,
|
| 2357 | .minor = MISC_DYNAMIC_MINOR,
|
| 2358 | .fops = &ibmvmc_fops,
|
| 2359 | };
|
| 2360 |
|
| 2361 | static int __init ibmvmc_module_init(void)
|
| 2362 | {
|
| 2363 | int rc, i, j;
|
| 2364 |
|
| 2365 | ibmvmc.state = ibmvmc_state_initial;
|
| 2366 | pr_info("ibmvmc: version %s\n", IBMVMC_DRIVER_VERSION);
|
| 2367 |
|
| 2368 | rc = misc_register(&ibmvmc_miscdev);
|
| 2369 | if (rc) {
|
| 2370 | pr_err("ibmvmc: misc registration failed\n");
|
| 2371 | goto misc_register_failed;
|
| 2372 | }
|
| 2373 | pr_info("ibmvmc: node %d:%d\n", MISC_MAJOR,
|
| 2374 | ibmvmc_miscdev.minor);
|
| 2375 |
|
| 2376 |
|
| 2377 | memset(hmcs, 0, sizeof(struct ibmvmc_hmc) * MAX_HMCS);
|
| 2378 | for (i = 0; i < MAX_HMCS; i++) {
|
| 2379 | spin_lock_init(&hmcs[i].lock);
|
| 2380 | hmcs[i].state = ibmhmc_state_free;
|
| 2381 | for (j = 0; j < MAX_BUF_POOL_SIZE; j++)
|
| 2382 | hmcs[i].queue_outbound_msgs[j] = VMC_INVALID_BUFFER_ID;
|
| 2383 | }
|
| 2384 |
|
| 2385 |
|
| 2386 | ibmvmc_scrub_module_parms();
|
| 2387 |
|
| 2388 | |
| 2389 | * Initialize some reasonable values. Might be negotiated smaller
|
| 2390 | * values during the capabilities exchange.
|
| 2391 |
|
| 2392 | ibmvmc.max_mtu = ibmvmc_max_mtu;
|
| 2393 | ibmvmc.max_buffer_pool_size = ibmvmc_max_buf_pool_size;
|
| 2394 | ibmvmc.max_hmc_index = ibmvmc_max_hmcs - 1;
|
| 2395 |
|
| 2396 | rc = vio_register_driver(&ibmvmc_driver);
|
| 2397 |
|
| 2398 | if (rc) {
|
| 2399 | pr_err("ibmvmc: rc %d from vio_register_driver\n", rc);
|
| 2400 | goto vio_reg_failed;
|
| 2401 | }
|
| 2402 |
|
| 2403 | return 0;
|
| 2404 |
|
| 2405 | vio_reg_failed:
|
| 2406 | misc_deregister(&ibmvmc_miscdev);
|
| 2407 | misc_register_failed:
|
| 2408 | return rc;
|
| 2409 | }
|
| 2410 |
|
| 2411 | static void __exit ibmvmc_module_exit(void)
|
| 2412 | {
|
| 2413 | pr_info("ibmvmc: module exit\n");
|
| 2414 | vio_unregister_driver(&ibmvmc_driver);
|
| 2415 | misc_deregister(&ibmvmc_miscdev);
|
| 2416 | }
|
| 2417 |
|
| 2418 | module_init(ibmvmc_module_init);
|
| 2419 | module_exit(ibmvmc_module_exit);
|
| 2420 |
|
| 2421 | module_param_named(buf_pool_size, ibmvmc_max_buf_pool_size,
|
| 2422 | int, 0644);
|
| 2423 | MODULE_PARM_DESC(buf_pool_size, "Buffer pool size");
|
| 2424 | module_param_named(max_hmcs, ibmvmc_max_hmcs, int, 0644);
|
| 2425 | MODULE_PARM_DESC(max_hmcs, "Max HMCs");
|
| 2426 | module_param_named(max_mtu, ibmvmc_max_mtu, int, 0644);
|
| 2427 | MODULE_PARM_DESC(max_mtu, "Max MTU");
|
| 2428 |
|
| 2429 | MODULE_AUTHOR("Steven Royer <[email protected]>");
|
| 2430 | MODULE_DESCRIPTION("IBM VMC");
|
| 2431 | MODULE_VERSION(IBMVMC_DRIVER_VERSION);
|
| 2432 | MODULE_LICENSE("GPL v2");
|