1267 lines · 31.4 KB · C
RawDownload
/* * Copyright (c) 2004 Topspin Communications.  All rights reserved. * Copyright (c) 2005 Intel Corporation. All rights reserved. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2005 Voltaire, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses.  You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * *     Redistribution and use in source and binary forms, with or *     without modification, are permitted provided that the following *     conditions are met: * *      - Redistributions of source code must retain the above *        copyright notice, this list of conditions and the following *        disclaimer. * *      - Redistributions in binary form must reproduce the above *        copyright notice, this list of conditions and the following *        disclaimer in the documentation and/or other materials *        provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include <linux/module.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/workqueue.h>#include <linux/netdevice.h>#include <net/addrconf.h> #include <rdma/ib_cache.h> #include "core_priv.h" struct ib_pkey_cache {	int             table_len;	u16             table[0];}; struct ib_update_work {	struct work_struct work;	struct ib_device  *device;	u8                 port_num;	bool		   enforce_security;}; union ib_gid zgid;EXPORT_SYMBOL(zgid); static const struct ib_gid_attr zattr; enum gid_attr_find_mask {	GID_ATTR_FIND_MASK_GID          = 1UL << 0,	GID_ATTR_FIND_MASK_NETDEV	= 1UL << 1,	GID_ATTR_FIND_MASK_DEFAULT	= 1UL << 2,	GID_ATTR_FIND_MASK_GID_TYPE	= 1UL << 3,}; enum gid_table_entry_props {	GID_TABLE_ENTRY_INVALID		= 1UL << 0,	GID_TABLE_ENTRY_DEFAULT		= 1UL << 1,}; enum gid_table_write_action {	GID_TABLE_WRITE_ACTION_ADD,	GID_TABLE_WRITE_ACTION_DEL,	/* MODIFY only updates the GID table. Currently only used by	 * ib_cache_update.	 */	GID_TABLE_WRITE_ACTION_MODIFY}; struct ib_gid_table_entry {	unsigned long	    props;	union ib_gid        gid;	struct ib_gid_attr  attr;	void		   *context;}; struct ib_gid_table {	int                  sz;	/* In RoCE, adding a GID to the table requires:	 * (a) Find if this GID is already exists.	 * (b) Find a free space.	 * (c) Write the new GID	 *	 * Delete requires different set of operations:	 * (a) Find the GID	 * (b) Delete it.	 *	 * Add/delete should be carried out atomically.	 * This is done by locking this mutex from multiple	 * writers. We don't need this lock for IB, as the MAD	 * layer replaces all entries. All data_vec entries	 * are locked by this lock.	 **/	struct mutex         lock;	/* This lock protects the table entries from being	 * read and written simultaneously.	 */	rwlock_t	     rwlock;	struct ib_gid_table_entry *data_vec;}; static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port){	if (rdma_cap_roce_gid_table(ib_dev, port)) {		struct ib_event event; 		event.device		= ib_dev;		event.element.port_num	= port;		event.event		= IB_EVENT_GID_CHANGE; 		ib_dispatch_event(&event);	}} static const char * const gid_type_str[] = {	[IB_GID_TYPE_IB]	= "IB/RoCE v1",	[IB_GID_TYPE_ROCE_UDP_ENCAP]	= "RoCE v2",}; const char *ib_cache_gid_type_str(enum ib_gid_type gid_type){	if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])		return gid_type_str[gid_type]; 	return "Invalid GID type";}EXPORT_SYMBOL(ib_cache_gid_type_str); int ib_cache_gid_parse_type_str(const char *buf){	unsigned int i;	size_t len;	int err = -EINVAL; 	len = strlen(buf);	if (len == 0)		return -EINVAL; 	if (buf[len - 1] == '\n')		len--; 	for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)		if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&		    len == strlen(gid_type_str[i])) {			err = i;			break;		} 	return err;}EXPORT_SYMBOL(ib_cache_gid_parse_type_str); /* This function expects that rwlock will be write locked in all * scenarios and that lock will be locked in sleep-able (RoCE) * scenarios. */static int write_gid(struct ib_device *ib_dev, u8 port,		     struct ib_gid_table *table, int ix,		     const union ib_gid *gid,		     const struct ib_gid_attr *attr,		     enum gid_table_write_action action,		     bool  default_gid)	__releases(&table->rwlock) __acquires(&table->rwlock){	int ret = 0;	struct net_device *old_net_dev;	enum ib_gid_type old_gid_type; 	/* in rdma_cap_roce_gid_table, this funciton should be protected by a	 * sleep-able lock.	 */ 	if (rdma_cap_roce_gid_table(ib_dev, port)) {		table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;		write_unlock_irq(&table->rwlock);		/* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by		 * RoCE providers and thus only updates the cache.		 */		if (action == GID_TABLE_WRITE_ACTION_ADD)			ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,					      &table->data_vec[ix].context);		else if (action == GID_TABLE_WRITE_ACTION_DEL)			ret = ib_dev->del_gid(ib_dev, port, ix,					      &table->data_vec[ix].context);		write_lock_irq(&table->rwlock);	} 	old_net_dev = table->data_vec[ix].attr.ndev;	old_gid_type = table->data_vec[ix].attr.gid_type;	if (old_net_dev && old_net_dev != attr->ndev)		dev_put(old_net_dev);	/* if modify_gid failed, just delete the old gid */	if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {		gid = &zgid;		attr = &zattr;		table->data_vec[ix].context = NULL;	} 	memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));	memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));	if (default_gid) {		table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;		if (action == GID_TABLE_WRITE_ACTION_DEL)			table->data_vec[ix].attr.gid_type = old_gid_type;	}	if (table->data_vec[ix].attr.ndev &&	    table->data_vec[ix].attr.ndev != old_net_dev)		dev_hold(table->data_vec[ix].attr.ndev); 	table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID; 	return ret;} static int add_gid(struct ib_device *ib_dev, u8 port,		   struct ib_gid_table *table, int ix,		   const union ib_gid *gid,		   const struct ib_gid_attr *attr,		   bool  default_gid) {	return write_gid(ib_dev, port, table, ix, gid, attr,			 GID_TABLE_WRITE_ACTION_ADD, default_gid);} static int modify_gid(struct ib_device *ib_dev, u8 port,		      struct ib_gid_table *table, int ix,		      const union ib_gid *gid,		      const struct ib_gid_attr *attr,		      bool  default_gid) {	return write_gid(ib_dev, port, table, ix, gid, attr,			 GID_TABLE_WRITE_ACTION_MODIFY, default_gid);} static int del_gid(struct ib_device *ib_dev, u8 port,		   struct ib_gid_table *table, int ix,		   bool  default_gid) {	return write_gid(ib_dev, port, table, ix, &zgid, &zattr,			 GID_TABLE_WRITE_ACTION_DEL, default_gid);} /* rwlock should be read locked */static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,		    const struct ib_gid_attr *val, bool default_gid,		    unsigned long mask, int *pempty){	int i = 0;	int found = -1;	int empty = pempty ? -1 : 0; 	while (i < table->sz && (found < 0 || empty < 0)) {		struct ib_gid_table_entry *data = &table->data_vec[i];		struct ib_gid_attr *attr = &data->attr;		int curr_index = i; 		i++; 		if (data->props & GID_TABLE_ENTRY_INVALID)			continue; 		if (empty < 0)			if (!memcmp(&data->gid, &zgid, sizeof(*gid)) &&			    !memcmp(attr, &zattr, sizeof(*attr)) &&			    !data->props)				empty = curr_index; 		if (found >= 0)			continue; 		if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&		    attr->gid_type != val->gid_type)			continue; 		if (mask & GID_ATTR_FIND_MASK_GID &&		    memcmp(gid, &data->gid, sizeof(*gid)))			continue; 		if (mask & GID_ATTR_FIND_MASK_NETDEV &&		    attr->ndev != val->ndev)			continue; 		if (mask & GID_ATTR_FIND_MASK_DEFAULT &&		    !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=		    default_gid)			continue; 		found = curr_index;	} 	if (pempty)		*pempty = empty; 	return found;} static void make_default_gid(struct  net_device *dev, union ib_gid *gid){	gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);	addrconf_ifid_eui48(&gid->raw[8], dev);} int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,		     union ib_gid *gid, struct ib_gid_attr *attr){	struct ib_gid_table *table;	int ix;	int ret = 0;	struct net_device *idev;	int empty; 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 	if (!memcmp(gid, &zgid, sizeof(*gid)))		return -EINVAL; 	if (ib_dev->get_netdev) {		idev = ib_dev->get_netdev(ib_dev, port);		if (idev && attr->ndev != idev) {			union ib_gid default_gid; 			/* Adding default GIDs in not permitted */			make_default_gid(idev, &default_gid);			if (!memcmp(gid, &default_gid, sizeof(*gid))) {				dev_put(idev);				return -EPERM;			}		}		if (idev)			dev_put(idev);	} 	mutex_lock(&table->lock);	write_lock_irq(&table->rwlock); 	ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |		      GID_ATTR_FIND_MASK_GID_TYPE |		      GID_ATTR_FIND_MASK_NETDEV, &empty);	if (ix >= 0)		goto out_unlock; 	if (empty < 0) {		ret = -ENOSPC;		goto out_unlock;	} 	ret = add_gid(ib_dev, port, table, empty, gid, attr, false);	if (!ret)		dispatch_gid_change_event(ib_dev, port); out_unlock:	write_unlock_irq(&table->rwlock);	mutex_unlock(&table->lock);	return ret;} int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,		     union ib_gid *gid, struct ib_gid_attr *attr){	struct ib_gid_table *table;	int ix; 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 	mutex_lock(&table->lock);	write_lock_irq(&table->rwlock); 	ix = find_gid(table, gid, attr, false,		      GID_ATTR_FIND_MASK_GID	  |		      GID_ATTR_FIND_MASK_GID_TYPE |		      GID_ATTR_FIND_MASK_NETDEV	  |		      GID_ATTR_FIND_MASK_DEFAULT,		      NULL);	if (ix < 0)		goto out_unlock; 	if (!del_gid(ib_dev, port, table, ix, false))		dispatch_gid_change_event(ib_dev, port); out_unlock:	write_unlock_irq(&table->rwlock);	mutex_unlock(&table->lock);	return 0;} int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,				     struct net_device *ndev){	struct ib_gid_table *table;	int ix;	bool deleted = false; 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 	mutex_lock(&table->lock);	write_lock_irq(&table->rwlock); 	for (ix = 0; ix < table->sz; ix++)		if (table->data_vec[ix].attr.ndev == ndev)			if (!del_gid(ib_dev, port, table, ix,				     !!(table->data_vec[ix].props &					GID_TABLE_ENTRY_DEFAULT)))				deleted = true; 	write_unlock_irq(&table->rwlock);	mutex_unlock(&table->lock); 	if (deleted)		dispatch_gid_change_event(ib_dev, port); 	return 0;} static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,			      union ib_gid *gid, struct ib_gid_attr *attr){	struct ib_gid_table *table; 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 	if (index < 0 || index >= table->sz)		return -EINVAL; 	if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)		return -EAGAIN; 	memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));	if (attr) {		memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));		if (attr->ndev)			dev_hold(attr->ndev);	} 	return 0;} static int _ib_cache_gid_table_find(struct ib_device *ib_dev,				    const union ib_gid *gid,				    const struct ib_gid_attr *val,				    unsigned long mask,				    u8 *port, u16 *index){	struct ib_gid_table *table;	u8 p;	int local_index;	unsigned long flags; 	for (p = 0; p < ib_dev->phys_port_cnt; p++) {		table = ib_dev->cache.ports[p].gid;		read_lock_irqsave(&table->rwlock, flags);		local_index = find_gid(table, gid, val, false, mask, NULL);		if (local_index >= 0) {			if (index)				*index = local_index;			if (port)				*port = p + rdma_start_port(ib_dev);			read_unlock_irqrestore(&table->rwlock, flags);			return 0;		}		read_unlock_irqrestore(&table->rwlock, flags);	} 	return -ENOENT;} static int ib_cache_gid_find(struct ib_device *ib_dev,			     const union ib_gid *gid,			     enum ib_gid_type gid_type,			     struct net_device *ndev, u8 *port,			     u16 *index){	unsigned long mask = GID_ATTR_FIND_MASK_GID |			     GID_ATTR_FIND_MASK_GID_TYPE;	struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type}; 	if (ndev)		mask |= GID_ATTR_FIND_MASK_NETDEV; 	return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,					mask, port, index);} int ib_find_cached_gid_by_port(struct ib_device *ib_dev,			       const union ib_gid *gid,			       enum ib_gid_type gid_type,			       u8 port, struct net_device *ndev,			       u16 *index){

Lines 1–500 of 1267.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t