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. |
| 6 | * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved. |
| 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 | * |
| 37 | * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $ |
| 38 | */ |
| 39 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 40 | #include <linux/slab.h> |
| 41 | #include <linux/string.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "agent.h" |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 44 | #include "smi.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 * |
| 57 | __ib_get_agent_port(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) { |
| 62 | if (entry->agent[0]->device == device && |
| 63 | entry->agent[0]->port_num == port_num) |
| 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 * |
| 70 | ib_get_agent_port(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 | |
| 81 | int smi_check_local_dr_smp(struct ib_smp *smp, |
| 82 | struct ib_device *device, |
| 83 | int port_num) |
| 84 | { |
| 85 | struct ib_agent_port_private *port_priv; |
| 86 | |
| 87 | if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) |
| 88 | return 1; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 89 | |
| 90 | port_priv = ib_get_agent_port(device, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | if (!port_priv) { |
| 92 | printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d " |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 93 | "not open\n", device->name, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 97 | return smi_check_local_smp(port_priv->agent[0], smp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 100 | int agent_send_response(struct ib_mad *mad, struct ib_grh *grh, |
| 101 | struct ib_wc *wc, struct ib_device *device, |
| 102 | int port_num, int qpn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | struct ib_agent_port_private *port_priv; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 105 | struct ib_mad_agent *agent; |
| 106 | struct ib_mad_send_buf *send_buf; |
| 107 | struct ib_ah *ah; |
| 108 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 110 | port_priv = ib_get_agent_port(device, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (!port_priv) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 112 | printk(KERN_ERR SPFX "Unable to find port agent\n"); |
| 113 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 116 | agent = port_priv->agent[qpn]; |
| 117 | ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num); |
| 118 | if (IS_ERR(ah)) { |
| 119 | ret = PTR_ERR(ah); |
| 120 | printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret); |
| 121 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 124 | send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0, |
| 125 | IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, |
| 126 | GFP_KERNEL); |
| 127 | if (IS_ERR(send_buf)) { |
| 128 | ret = PTR_ERR(send_buf); |
| 129 | printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret); |
| 130 | goto err1; |
| 131 | } |
| 132 | |
| 133 | memcpy(send_buf->mad, mad, sizeof *mad); |
| 134 | send_buf->ah = ah; |
| 135 | if ((ret = ib_post_send_mad(send_buf, NULL))) { |
| 136 | printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret); |
| 137 | goto err2; |
| 138 | } |
| 139 | return 0; |
| 140 | err2: |
| 141 | ib_free_send_mad(send_buf); |
| 142 | err1: |
| 143 | ib_destroy_ah(ah); |
| 144 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void agent_send_handler(struct ib_mad_agent *mad_agent, |
| 148 | struct ib_mad_send_wc *mad_send_wc) |
| 149 | { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 150 | ib_destroy_ah(mad_send_wc->send_buf->ah); |
| 151 | ib_free_send_mad(mad_send_wc->send_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int ib_agent_port_open(struct ib_device *device, int port_num) |
| 155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct ib_agent_port_private *port_priv; |
| 157 | unsigned long flags; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 158 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /* Create new device info */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 161 | port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (!port_priv) { |
| 163 | printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n"); |
| 164 | ret = -ENOMEM; |
| 165 | goto error1; |
| 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 168 | /* Obtain send only MAD agent for SMI QP */ |
| 169 | port_priv->agent[0] = ib_register_mad_agent(device, port_num, |
| 170 | IB_QPT_SMI, NULL, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | &agent_send_handler, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 172 | NULL, NULL); |
| 173 | if (IS_ERR(port_priv->agent[0])) { |
| 174 | ret = PTR_ERR(port_priv->agent[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | goto error2; |
| 176 | } |
| 177 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 178 | /* Obtain send only MAD agent for GSI QP */ |
| 179 | port_priv->agent[1] = ib_register_mad_agent(device, port_num, |
| 180 | IB_QPT_GSI, NULL, 0, |
| 181 | &agent_send_handler, |
| 182 | NULL, NULL); |
| 183 | if (IS_ERR(port_priv->agent[1])) { |
| 184 | ret = PTR_ERR(port_priv->agent[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | goto error3; |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | spin_lock_irqsave(&ib_agent_port_list_lock, flags); |
| 189 | list_add_tail(&port_priv->port_list, &ib_agent_port_list); |
| 190 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
| 191 | |
| 192 | return 0; |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | error3: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 195 | ib_unregister_mad_agent(port_priv->agent[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | error2: |
| 197 | kfree(port_priv); |
| 198 | error1: |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | int ib_agent_port_close(struct ib_device *device, int port_num) |
| 203 | { |
| 204 | struct ib_agent_port_private *port_priv; |
| 205 | unsigned long flags; |
| 206 | |
| 207 | spin_lock_irqsave(&ib_agent_port_list_lock, flags); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 208 | port_priv = __ib_get_agent_port(device, port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (port_priv == NULL) { |
| 210 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
| 211 | printk(KERN_ERR SPFX "Port %d not found\n", port_num); |
| 212 | return -ENODEV; |
| 213 | } |
| 214 | list_del(&port_priv->port_list); |
| 215 | spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); |
| 216 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 217 | ib_unregister_mad_agent(port_priv->agent[1]); |
| 218 | ib_unregister_mad_agent(port_priv->agent[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | kfree(port_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |