pvrdma_cmd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of EITHER the GNU General Public License
  6. * version 2 as published by the Free Software Foundation or the BSD
  7. * 2-Clause License. This program is distributed in the hope that it
  8. * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  9. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License version 2 for more details at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program available in the file COPYING in the main
  15. * directory of this source tree.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials
  30. * provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. * OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #include <linux/list.h>
  46. #include "pvrdma.h"
  47. #define PVRDMA_CMD_TIMEOUT 10000 /* ms */
  48. static inline int pvrdma_cmd_recv(struct pvrdma_dev *dev,
  49. union pvrdma_cmd_resp *resp,
  50. unsigned resp_code)
  51. {
  52. int err;
  53. dev_dbg(&dev->pdev->dev, "receive response from device\n");
  54. err = wait_for_completion_interruptible_timeout(&dev->cmd_done,
  55. msecs_to_jiffies(PVRDMA_CMD_TIMEOUT));
  56. if (err == 0 || err == -ERESTARTSYS) {
  57. dev_warn(&dev->pdev->dev,
  58. "completion timeout or interrupted\n");
  59. return -ETIMEDOUT;
  60. }
  61. spin_lock(&dev->cmd_lock);
  62. memcpy(resp, dev->resp_slot, sizeof(*resp));
  63. spin_unlock(&dev->cmd_lock);
  64. if (resp->hdr.ack != resp_code) {
  65. dev_warn(&dev->pdev->dev,
  66. "unknown response %#x expected %#x\n",
  67. resp->hdr.ack, resp_code);
  68. return -EFAULT;
  69. }
  70. return 0;
  71. }
  72. int
  73. pvrdma_cmd_post(struct pvrdma_dev *dev, union pvrdma_cmd_req *req,
  74. union pvrdma_cmd_resp *resp, unsigned resp_code)
  75. {
  76. int err;
  77. dev_dbg(&dev->pdev->dev, "post request to device\n");
  78. /* Serializiation */
  79. down(&dev->cmd_sema);
  80. BUILD_BUG_ON(sizeof(union pvrdma_cmd_req) !=
  81. sizeof(struct pvrdma_cmd_modify_qp));
  82. spin_lock(&dev->cmd_lock);
  83. memcpy(dev->cmd_slot, req, sizeof(*req));
  84. spin_unlock(&dev->cmd_lock);
  85. init_completion(&dev->cmd_done);
  86. pvrdma_write_reg(dev, PVRDMA_REG_REQUEST, 0);
  87. /* Make sure the request is written before reading status. */
  88. mb();
  89. err = pvrdma_read_reg(dev, PVRDMA_REG_ERR);
  90. if (err == 0) {
  91. if (resp != NULL)
  92. err = pvrdma_cmd_recv(dev, resp, resp_code);
  93. } else {
  94. dev_warn(&dev->pdev->dev,
  95. "failed to write request error reg: %d\n", err);
  96. err = -EFAULT;
  97. }
  98. up(&dev->cmd_sema);
  99. return err;
  100. }