1 /* 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. 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 40 #include "agent.h" 41 #include "smi.h" 42 43 #define SPFX "ib_agent: " 44 45 struct ib_agent_port_private { 46 struct list_head port_list; 47 struct ib_mad_agent *agent[2]; 48 }; 49 50 static DEFINE_SPINLOCK(ib_agent_port_list_lock); 51 static LIST_HEAD(ib_agent_port_list); 52 53 static struct ib_agent_port_private * 54 __ib_get_agent_port(struct ib_device *device, int port_num) 55 { 56 struct ib_agent_port_private *entry; 57 58 list_for_each_entry(entry, &ib_agent_port_list, port_list) { 59 if (entry->agent[0]->device == device && 60 entry->agent[0]->port_num == port_num) 61 return entry; 62 } 63 return NULL; 64 } 65 66 static struct ib_agent_port_private * 67 ib_get_agent_port(struct ib_device *device, int port_num) 68 { 69 struct ib_agent_port_private *entry; 70 unsigned long flags; 71 72 spin_lock_irqsave(&ib_agent_port_list_lock, flags); 73 entry = __ib_get_agent_port(device, port_num); 74 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 75 return entry; 76 } 77 78 int smi_check_local_dr_smp(struct ib_smp *smp, 79 struct ib_device *device, 80 int port_num) 81 { 82 struct ib_agent_port_private *port_priv; 83 84 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) 85 return 1; 86 87 port_priv = ib_get_agent_port(device, port_num); 88 if (!port_priv) { 89 printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d " 90 "not open\n", device->name, port_num); 91 return 1; 92 } 93 94 return smi_check_local_smp(port_priv->agent[0], smp); 95 } 96 97 int agent_send_response(struct ib_mad *mad, struct ib_grh *grh, 98 struct ib_wc *wc, struct ib_device *device, 99 int port_num, int qpn) 100 { 101 struct ib_agent_port_private *port_priv; 102 struct ib_mad_agent *agent; 103 struct ib_mad_send_buf *send_buf; 104 struct ib_ah *ah; 105 int ret; 106 107 port_priv = ib_get_agent_port(device, port_num); 108 if (!port_priv) { 109 printk(KERN_ERR SPFX "Unable to find port agent\n"); 110 return -ENODEV; 111 } 112 113 agent = port_priv->agent[qpn]; 114 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num); 115 if (IS_ERR(ah)) { 116 ret = PTR_ERR(ah); 117 printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret); 118 return ret; 119 } 120 121 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0, 122 IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, 123 GFP_KERNEL); 124 if (IS_ERR(send_buf)) { 125 ret = PTR_ERR(send_buf); 126 printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret); 127 goto err1; 128 } 129 130 memcpy(send_buf->mad, mad, sizeof *mad); 131 send_buf->ah = ah; 132 if ((ret = ib_post_send_mad(send_buf, NULL))) { 133 printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret); 134 goto err2; 135 } 136 return 0; 137 err2: 138 ib_free_send_mad(send_buf); 139 err1: 140 ib_destroy_ah(ah); 141 return ret; 142 } 143 144 static void agent_send_handler(struct ib_mad_agent *mad_agent, 145 struct ib_mad_send_wc *mad_send_wc) 146 { 147 ib_destroy_ah(mad_send_wc->send_buf->ah); 148 ib_free_send_mad(mad_send_wc->send_buf); 149 } 150 151 int ib_agent_port_open(struct ib_device *device, int port_num) 152 { 153 struct ib_agent_port_private *port_priv; 154 unsigned long flags; 155 int ret; 156 157 /* Create new device info */ 158 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); 159 if (!port_priv) { 160 printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n"); 161 ret = -ENOMEM; 162 goto error1; 163 } 164 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, 169 NULL, NULL); 170 if (IS_ERR(port_priv->agent[0])) { 171 ret = PTR_ERR(port_priv->agent[0]); 172 goto error2; 173 } 174 175 /* Obtain send only MAD agent for GSI QP */ 176 port_priv->agent[1] = ib_register_mad_agent(device, port_num, 177 IB_QPT_GSI, NULL, 0, 178 &agent_send_handler, 179 NULL, NULL); 180 if (IS_ERR(port_priv->agent[1])) { 181 ret = PTR_ERR(port_priv->agent[1]); 182 goto error3; 183 } 184 185 spin_lock_irqsave(&ib_agent_port_list_lock, flags); 186 list_add_tail(&port_priv->port_list, &ib_agent_port_list); 187 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 188 189 return 0; 190 191 error3: 192 ib_unregister_mad_agent(port_priv->agent[0]); 193 error2: 194 kfree(port_priv); 195 error1: 196 return ret; 197 } 198 199 int ib_agent_port_close(struct ib_device *device, int port_num) 200 { 201 struct ib_agent_port_private *port_priv; 202 unsigned long flags; 203 204 spin_lock_irqsave(&ib_agent_port_list_lock, flags); 205 port_priv = __ib_get_agent_port(device, port_num); 206 if (port_priv == NULL) { 207 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 208 printk(KERN_ERR SPFX "Port %d not found\n", port_num); 209 return -ENODEV; 210 } 211 list_del(&port_priv->port_list); 212 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags); 213 214 ib_unregister_mad_agent(port_priv->agent[1]); 215 ib_unregister_mad_agent(port_priv->agent[0]); 216 kfree(port_priv); 217 return 0; 218 } 219