Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved. |
| 3 | * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved. |
| 4 | * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved. |
| 5 | * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved. |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 6 | * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 7 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This software is available to you under a choice of one of two |
| 10 | * licenses. You may choose to be licensed under the terms of the GNU |
| 11 | * General Public License (GPL) Version 2, available from the file |
| 12 | * COPYING in the main directory of this source tree, or the |
| 13 | * OpenIB.org BSD license below: |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or |
| 16 | * without modification, are permitted provided that the following |
| 17 | * conditions are met: |
| 18 | * |
| 19 | * - Redistributions of source code must retain the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer. |
| 22 | * |
| 23 | * - Redistributions in binary form must reproduce the above |
| 24 | * copyright notice, this list of conditions and the following |
| 25 | * disclaimer in the documentation and/or other materials |
| 26 | * provided with the distribution. |
| 27 | * |
| 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 31 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 32 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 33 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 34 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | * SOFTWARE. |
| 36 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 39 | #include <linux/slab.h> |
| 40 | #include <linux/string.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "agent.h" |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 43 | #include "smi.h" |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 44 | #include "mad_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 46 | #define SPFX "ib_agent: " |
| 47 | |
| 48 | struct ib_agent_port_private { |
| 49 | struct list_head port_list; |
| 50 | struct ib_mad_agent *agent[2]; |
| 51 | }; |
| 52 | |
| 53 | static DEFINE_SPINLOCK(ib_agent_port_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static LIST_HEAD(ib_agent_port_list); |
| 55 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 56 | static struct ib_agent_port_private * |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 57 | __ib_get_agent_port(const struct ib_device *device, int port_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | struct ib_agent_port_private *entry; |
| 60 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 61 | list_for_each_entry(entry, &ib_agent_port_list, port_list) { |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 62 | if (entry->agent[1]->device == device && |
| 63 | entry->agent[1]->port_num == port_num) |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 64 | return entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 69 | static struct ib_agent_port_private * |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 70 | ib_get_agent_port(const struct ib_device *device, int port_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | struct ib_agent_port_private *entry; |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&ib_agent_port_list_lock, flags); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 76 | entry = __ib_get_agent_port(device, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return entry; |
| 79 | } |
| 80 | |
Ira Weiny | c9082e5 | 2015-06-06 14:38:30 -0400 | [diff] [blame] | 81 | void agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh, |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 82 | const struct ib_wc *wc, const struct ib_device *device, |
Ira Weiny | 8e4349d | 2015-06-10 16:16:48 -0400 | [diff] [blame] | 83 | int port_num, int qpn, size_t resp_mad_len, bool opa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct ib_agent_port_private *port_priv; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 86 | struct ib_mad_agent *agent; |
| 87 | struct ib_mad_send_buf *send_buf; |
| 88 | struct ib_ah *ah; |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 89 | struct ib_mad_send_wr_private *mad_send_wr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 91 | if (device->node_type == RDMA_NODE_IB_SWITCH) |
| 92 | port_priv = ib_get_agent_port(device, 0); |
| 93 | else |
| 94 | port_priv = ib_get_agent_port(device, port_num); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!port_priv) { |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 97 | dev_err(&device->dev, "Unable to find port agent\n"); |
Hal Rosenstock | 8fc394b | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 98 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 101 | agent = port_priv->agent[qpn]; |
| 102 | ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num); |
| 103 | if (IS_ERR(ah)) { |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 104 | dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n", |
Michael Heinz | 1eba843 | 2011-02-11 21:44:30 +0000 | [diff] [blame] | 105 | PTR_ERR(ah)); |
Hal Rosenstock | 8fc394b | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 106 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ira Weiny | 8e4349d | 2015-06-10 16:16:48 -0400 | [diff] [blame] | 109 | if (opa && mad_hdr->base_version != OPA_MGMT_BASE_VERSION) |
| 110 | resp_mad_len = IB_MGMT_MAD_SIZE; |
| 111 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 112 | send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0, |
Ira Weiny | c9082e5 | 2015-06-06 14:38:30 -0400 | [diff] [blame] | 113 | IB_MGMT_MAD_HDR, |
| 114 | resp_mad_len - IB_MGMT_MAD_HDR, |
Ira Weiny | da2dfaa | 2015-06-06 14:38:28 -0400 | [diff] [blame] | 115 | GFP_KERNEL, |
Ira Weiny | 8e4349d | 2015-06-10 16:16:48 -0400 | [diff] [blame] | 116 | mad_hdr->base_version); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 117 | if (IS_ERR(send_buf)) { |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 118 | dev_err(&device->dev, "ib_create_send_mad error\n"); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 119 | goto err1; |
| 120 | } |
| 121 | |
Ira Weiny | c9082e5 | 2015-06-06 14:38:30 -0400 | [diff] [blame] | 122 | memcpy(send_buf->mad, mad_hdr, resp_mad_len); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 123 | send_buf->ah = ah; |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 124 | |
| 125 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
| 126 | mad_send_wr = container_of(send_buf, |
| 127 | struct ib_mad_send_wr_private, |
| 128 | send_buf); |
| 129 | mad_send_wr->send_wr.wr.ud.port_num = port_num; |
| 130 | } |
| 131 | |
Hal Rosenstock | 8fc394b | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 132 | if (ib_post_send_mad(send_buf, NULL)) { |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 133 | dev_err(&device->dev, "ib_post_send_mad error\n"); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 134 | goto err2; |
| 135 | } |
Hal Rosenstock | 8fc394b | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 136 | return; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 137 | err2: |
| 138 | ib_free_send_mad(send_buf); |
| 139 | err1: |
| 140 | ib_destroy_ah(ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void agent_send_handler(struct ib_mad_agent *mad_agent, |
| 144 | struct ib_mad_send_wc *mad_send_wc) |
| 145 | { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 146 | ib_destroy_ah(mad_send_wc->send_buf->ah); |
| 147 | ib_free_send_mad(mad_send_wc->send_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int ib_agent_port_open(struct ib_device *device, int port_num) |
| 151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | struct ib_agent_port_private *port_priv; |
| 153 | unsigned long flags; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 154 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* Create new device info */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 157 | port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (!port_priv) { |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 159 | dev_err(&device->dev, "No memory for ib_agent_port_private\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | ret = -ENOMEM; |
| 161 | goto error1; |
| 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Michael Wang | 29541e3 | 2015-05-05 14:50:33 +0200 | [diff] [blame] | 164 | if (rdma_cap_ib_smi(device, port_num)) { |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 165 | /* Obtain send only MAD agent for SMI QP */ |
| 166 | port_priv->agent[0] = ib_register_mad_agent(device, port_num, |
| 167 | IB_QPT_SMI, NULL, 0, |
| 168 | &agent_send_handler, |
Ira Weiny | 0f29b46 | 2014-08-08 19:00:55 -0400 | [diff] [blame] | 169 | NULL, NULL, 0); |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 170 | if (IS_ERR(port_priv->agent[0])) { |
| 171 | ret = PTR_ERR(port_priv->agent[0]); |
| 172 | goto error2; |
| 173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 176 | /* Obtain send only MAD agent for GSI QP */ |
| 177 | port_priv->agent[1] = ib_register_mad_agent(device, port_num, |
| 178 | IB_QPT_GSI, NULL, 0, |
| 179 | &agent_send_handler, |
Ira Weiny | 0f29b46 | 2014-08-08 19:00:55 -0400 | [diff] [blame] | 180 | NULL, NULL, 0); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 181 | if (IS_ERR(port_priv->agent[1])) { |
| 182 | ret = PTR_ERR(port_priv->agent[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | goto error3; |
| 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | spin_lock_irqsave(&ib_agent_port_list_lock, flags); |
| 187 | list_add_tail(&port_priv->port_list, &ib_agent_port_list); |
| 188 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
| 189 | |
| 190 | return 0; |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | error3: |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 193 | if (port_priv->agent[0]) |
| 194 | ib_unregister_mad_agent(port_priv->agent[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | error2: |
| 196 | kfree(port_priv); |
| 197 | error1: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | int ib_agent_port_close(struct ib_device *device, int port_num) |
| 202 | { |
| 203 | struct ib_agent_port_private *port_priv; |
| 204 | unsigned long flags; |
| 205 | |
| 206 | spin_lock_irqsave(&ib_agent_port_list_lock, flags); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 207 | port_priv = __ib_get_agent_port(device, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (port_priv == NULL) { |
| 209 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
Ira Weiny | 7ef5d4b | 2014-08-08 19:00:53 -0400 | [diff] [blame] | 210 | dev_err(&device->dev, "Port %d not found\n", port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return -ENODEV; |
| 212 | } |
| 213 | list_del(&port_priv->port_list); |
| 214 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
| 215 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 216 | ib_unregister_mad_agent(port_priv->agent[1]); |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 217 | if (port_priv->agent[0]) |
| 218 | ib_unregister_mad_agent(port_priv->agent[0]); |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | kfree(port_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |