cregit-Linux how code gets into the kernel

Release 4.8 net/ncsi/ncsi-manage.c

Directory: net/ncsi
/*
 * Copyright Gavin Shan, IBM Corporation 2016.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>

#include <net/ncsi.h>
#include <net/net_namespace.h>
#include <net/sock.h>
#include <net/addrconf.h>
#include <net/ipv6.h>
#include <net/if_inet6.h>

#include "internal.h"
#include "ncsi-pkt.h"


LIST_HEAD(ncsi_dev_list);

DEFINE_SPINLOCK(ncsi_dev_lock);


static inline int ncsi_filter_size(int table) { int sizes[] = { 2, 6, 6, 6 }; BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX); if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX) return -EINVAL; return sizes[table]; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan54100.00%1100.00%
Total54100.00%1100.00%


int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data) { struct ncsi_channel_filter *ncf; void *bitmap; int index, size; unsigned long flags; ncf = nc->filters[table]; if (!ncf) return -ENXIO; size = ncsi_filter_size(table); if (size < 0) return size; spin_lock_irqsave(&nc->lock, flags); bitmap = (void *)&ncf->bitmap; index = -1; while ((index = find_next_bit(bitmap, ncf->total, index + 1)) < ncf->total) { if (!memcmp(ncf->data + size * index, data, size)) { spin_unlock_irqrestore(&nc->lock, flags); return index; } } spin_unlock_irqrestore(&nc->lock, flags); return -ENOENT; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan167100.00%1100.00%
Total167100.00%1100.00%


int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data) { struct ncsi_channel_filter *ncf; int index, size; void *bitmap; unsigned long flags; size = ncsi_filter_size(table); if (size < 0) return size; index = ncsi_find_filter(nc, table, data); if (index >= 0) return index; ncf = nc->filters[table]; if (!ncf) return -ENODEV; spin_lock_irqsave(&nc->lock, flags); bitmap = (void *)&ncf->bitmap; do { index = find_next_zero_bit(bitmap, ncf->total, 0); if (index >= ncf->total) { spin_unlock_irqrestore(&nc->lock, flags); return -ENOSPC; } } while (test_and_set_bit(index, bitmap)); memcpy(ncf->data + size * index, data, size); spin_unlock_irqrestore(&nc->lock, flags); return index; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan188100.00%1100.00%
Total188100.00%1100.00%


int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index) { struct ncsi_channel_filter *ncf; int size; void *bitmap; unsigned long flags; size = ncsi_filter_size(table); if (size < 0) return size; ncf = nc->filters[table]; if (!ncf || index >= ncf->total) return -ENODEV; spin_lock_irqsave(&nc->lock, flags); bitmap = (void *)&ncf->bitmap; if (test_and_clear_bit(index, bitmap)) memset(ncf->data + size * index, 0, size); spin_unlock_irqrestore(&nc->lock, flags); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan130100.00%1100.00%
Total130100.00%1100.00%


static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down) { struct ncsi_dev *nd = &ndp->ndev; struct ncsi_package *np; struct ncsi_channel *nc; nd->state = ncsi_dev_state_functional; if (force_down) { nd->link_up = 0; goto report; } nd->link_up = 0; NCSI_FOR_EACH_PACKAGE(ndp, np) { NCSI_FOR_EACH_CHANNEL(np, nc) { if (!list_empty(&nc->link) || nc->state != NCSI_CHANNEL_ACTIVE) continue; if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) { nd->link_up = 1; goto report; } } } report: nd->handler(nd); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan131100.00%2100.00%
Total131100.00%2100.00%


static void ncsi_channel_monitor(unsigned long data) { struct ncsi_channel *nc = (struct ncsi_channel *)data; struct ncsi_package *np = nc->package; struct ncsi_dev_priv *ndp = np->ndp; struct ncsi_cmd_arg nca; bool enabled; unsigned int timeout; unsigned long flags; int ret; spin_lock_irqsave(&nc->lock, flags); timeout = nc->timeout; enabled = nc->enabled; spin_unlock_irqrestore(&nc->lock, flags); if (!enabled || !list_empty(&nc->link)) return; if (nc->state != NCSI_CHANNEL_INACTIVE && nc->state != NCSI_CHANNEL_ACTIVE) return; if (!(timeout % 2)) { nca.ndp = ndp; nca.package = np->id; nca.channel = nc->id; nca.type = NCSI_PKT_CMD_GLS; nca.driven = false; ret = ncsi_xmit_cmd(&nca); if (ret) { netdev_err(ndp->ndev.dev, "Error %d sending GLS\n", ret); return; } } if (timeout + 1 >= 3) { if (!(ndp->flags & NCSI_DEV_HWA) && nc->state == NCSI_CHANNEL_ACTIVE) ncsi_report_link(ndp, true); spin_lock_irqsave(&ndp->lock, flags); xchg(&nc->state, NCSI_CHANNEL_INACTIVE); list_add_tail_rcu(&nc->link, &ndp->channel_queue); spin_unlock_irqrestore(&ndp->lock, flags); ncsi_process_next_channel(ndp); return; } spin_lock_irqsave(&nc->lock, flags); nc->timeout = timeout + 1; nc->enabled = true; spin_unlock_irqrestore(&nc->lock, flags); mod_timer(&nc->timer, jiffies + HZ * (1 << (nc->timeout / 2))); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan334100.00%2100.00%
Total334100.00%2100.00%


void ncsi_start_channel_monitor(struct ncsi_channel *nc) { unsigned long flags; spin_lock_irqsave(&nc->lock, flags); WARN_ON_ONCE(nc->enabled); nc->timeout = 0; nc->enabled = true; spin_unlock_irqrestore(&nc->lock, flags); mod_timer(&nc->timer, jiffies + HZ * (1 << (nc->timeout / 2))); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan77100.00%2100.00%
Total77100.00%2100.00%


void ncsi_stop_channel_monitor(struct ncsi_channel *nc) { unsigned long flags; spin_lock_irqsave(&nc->lock, flags); if (!nc->enabled) { spin_unlock_irqrestore(&nc->lock, flags); return; } nc->enabled = false; spin_unlock_irqrestore(&nc->lock, flags); del_timer_sync(&nc->timer); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan68100.00%2100.00%
Total68100.00%2100.00%


struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np, unsigned char id) { struct ncsi_channel *nc; NCSI_FOR_EACH_CHANNEL(np, nc) { if (nc->id == id) return nc; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan43100.00%2100.00%
Total43100.00%2100.00%


struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id) { struct ncsi_channel *nc, *tmp; int index; unsigned long flags; nc = kzalloc(sizeof(*nc), GFP_ATOMIC); if (!nc) return NULL; nc->id = id; nc->package = np; nc->state = NCSI_CHANNEL_INACTIVE; nc->enabled = false; setup_timer(&nc->timer, ncsi_channel_monitor, (unsigned long)nc); spin_lock_init(&nc->lock); INIT_LIST_HEAD(&nc->link); for (index = 0; index < NCSI_CAP_MAX; index++) nc->caps[index].index = index; for (index = 0; index < NCSI_MODE_MAX; index++) nc->modes[index].index = index; spin_lock_irqsave(&np->lock, flags); tmp = ncsi_find_channel(np, id); if (tmp) { spin_unlock_irqrestore(&np->lock, flags); kfree(nc); return tmp; } list_add_tail_rcu(&nc->node, &np->channels); np->channel_num++; spin_unlock_irqrestore(&np->lock, flags); return nc; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan230100.00%2100.00%
Total230100.00%2100.00%


static void ncsi_remove_channel(struct ncsi_channel *nc) { struct ncsi_package *np = nc->package; struct ncsi_channel_filter *ncf; unsigned long flags; int i; /* Release filters */ spin_lock_irqsave(&nc->lock, flags); for (i = 0; i < NCSI_FILTER_MAX; i++) { ncf = nc->filters[i]; if (!ncf) continue; nc->filters[i] = NULL; kfree(ncf); } nc->state = NCSI_CHANNEL_INACTIVE; spin_unlock_irqrestore(&nc->lock, flags); ncsi_stop_channel_monitor(nc); /* Remove and free channel */ spin_lock_irqsave(&np->lock, flags); list_del_rcu(&nc->node); np->channel_num--; spin_unlock_irqrestore(&np->lock, flags); kfree(nc); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan147100.00%2100.00%
Total147100.00%2100.00%


struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp, unsigned char id) { struct ncsi_package *np; NCSI_FOR_EACH_PACKAGE(ndp, np) { if (np->id == id) return np; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan43100.00%2100.00%
Total43100.00%2100.00%


struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp, unsigned char id) { struct ncsi_package *np, *tmp; unsigned long flags; np = kzalloc(sizeof(*np), GFP_ATOMIC); if (!np) return NULL; np->id = id; np->ndp = ndp; spin_lock_init(&np->lock); INIT_LIST_HEAD(&np->channels); spin_lock_irqsave(&ndp->lock, flags); tmp = ncsi_find_package(ndp, id); if (tmp) { spin_unlock_irqrestore(&ndp->lock, flags); kfree(np); return tmp; } list_add_tail_rcu(&np->node, &ndp->packages); ndp->package_num++; spin_unlock_irqrestore(&ndp->lock, flags); return np; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan151100.00%2100.00%
Total151100.00%2100.00%


void ncsi_remove_package(struct ncsi_package *np) { struct ncsi_dev_priv *ndp = np->ndp; struct ncsi_channel *nc, *tmp; unsigned long flags; /* Release all child channels */ list_for_each_entry_safe(nc, tmp, &np->channels, node) ncsi_remove_channel(nc); /* Remove and free package */ spin_lock_irqsave(&ndp->lock, flags); list_del_rcu(&np->node); ndp->package_num--; spin_unlock_irqrestore(&ndp->lock, flags); kfree(np); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan86100.00%2100.00%
Total86100.00%2100.00%


void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp, unsigned char id, struct ncsi_package **np, struct ncsi_channel **nc) { struct ncsi_package *p; struct ncsi_channel *c; p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id)); c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL; if (np) *np = p; if (nc) *nc = c; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan82100.00%2100.00%
Total82100.00%2100.00%

/* For two consecutive NCSI commands, the packet IDs shouldn't * be same. Otherwise, the bogus response might be replied. So * the available IDs are allocated in round-robin fashion. */
struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp, bool driven) { struct ncsi_request *nr = NULL; int i, limit = ARRAY_SIZE(ndp->requests); unsigned long flags; /* Check if there is one available request until the ceiling */ spin_lock_irqsave(&ndp->lock, flags); for (i = ndp->request_id; !nr && i < limit; i++) { if (ndp->requests[i].used) continue; nr = &ndp->requests[i]; nr->used = true; nr->driven = driven; if (++ndp->request_id >= limit) ndp->request_id = 0; } /* Fail back to check from the starting cursor */ for (i = 0; !nr && i < ndp->request_id; i++) { if (ndp->requests[i].used) continue; nr = &ndp->requests[i]; nr->used = true; nr->driven = driven; if (++ndp->request_id >= limit) ndp->request_id = 0; } spin_unlock_irqrestore(&ndp->lock, flags); return nr; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan201100.00%2100.00%
Total201100.00%2100.00%


void ncsi_free_request(struct ncsi_request *nr) { struct ncsi_dev_priv *ndp = nr->ndp; struct sk_buff *cmd, *rsp; unsigned long flags; bool driven; if (nr->enabled) { nr->enabled = false; del_timer_sync(&nr->timer); } spin_lock_irqsave(&ndp->lock, flags); cmd = nr->cmd; rsp = nr->rsp; nr->cmd = NULL; nr->rsp = NULL; nr->used = false; driven = nr->driven; spin_unlock_irqrestore(&ndp->lock, flags); if (driven && cmd && --ndp->pending_req_num == 0) schedule_work(&ndp->work); /* Release command and response */ consume_skb(cmd); consume_skb(rsp); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan144100.00%1100.00%
Total144100.00%1100.00%


struct ncsi_dev *ncsi_find_dev(struct net_device *dev) { struct ncsi_dev_priv *ndp; NCSI_FOR_EACH_DEV(ndp) { if (ndp->ndev.dev == dev) return &ndp->ndev; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan42100.00%1100.00%
Total42100.00%1100.00%


static void ncsi_request_timeout(unsigned long data) { struct ncsi_request *nr = (struct ncsi_request *)data; struct ncsi_dev_priv *ndp = nr->ndp; unsigned long flags; /* If the request already had associated response, * let the response handler to release it. */ spin_lock_irqsave(&ndp->lock, flags); nr->enabled = false; if (nr->rsp || !nr->cmd) { spin_unlock_irqrestore(&ndp->lock, flags); return; } spin_unlock_irqrestore(&ndp->lock, flags); /* Release the request */ ncsi_free_request(nr); }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan92100.00%1100.00%
Total92100.00%1100.00%


static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp) { struct ncsi_dev *nd = &ndp->ndev; struct ncsi_package *np = ndp->active_package; struct ncsi_channel *nc = ndp->active_channel; struct ncsi_cmd_arg nca; int ret; nca.ndp = ndp; nca.driven = true; switch (nd->state) { case ncsi_dev_state_suspend: nd->state = ncsi_dev_state_suspend_select; /* Fall through */ case ncsi_dev_state_suspend_select: case ncsi_dev_state_suspend_dcnt: case ncsi_dev_state_suspend_dc: case ncsi_dev_state_suspend_deselect: ndp->pending_req_num = 1; np = ndp->active_package; nc = ndp->active_channel; nca.package = np->id; if (nd->state == ncsi_dev_state_suspend_select) { nca.type = NCSI_PKT_CMD_SP; nca.channel = 0x1f; if (ndp->flags & NCSI_DEV_HWA) nca.bytes[0] = 0; else nca.bytes[0] = 1; nd->state = ncsi_dev_state_suspend_dcnt; } else if (nd->state == ncsi_dev_state_suspend_dcnt) { nca.type = NCSI_PKT_CMD_DCNT; nca.channel = nc->id; nd->state = ncsi_dev_state_suspend_dc; } else if (nd->state == ncsi_dev_state_suspend_dc) { nca.type = NCSI_PKT_CMD_DC; nca.channel = nc->id; nca.bytes[0] = 1; nd->state = ncsi_dev_state_suspend_deselect; } else if (nd->state == ncsi_dev_state_suspend_deselect) { nca.type = NCSI_PKT_CMD_DP; nca.channel = 0x1f; nd->state = ncsi_dev_state_suspend_done; } ret = ncsi_xmit_cmd(&nca); if (ret) { nd->state = ncsi_dev_state_functional; return; } break; case ncsi_dev_state_suspend_done: xchg(&nc->state, NCSI_CHANNEL_INACTIVE); ncsi_process_next_channel(ndp); break; default: netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n", nd->state); } }

Contributors

PersonTokensPropCommitsCommitProp
gavin shangavin shan324100.00%1100.00%
Total324100.00%1100.00%


static void ncsi_configure_channel(struct ncsi_dev_priv *ndp) { struct ncsi_dev *nd = &ndp->ndev; struct net_device *dev = nd->dev; struct ncsi_package *np = ndp->active_package; struct ncsi_channel *nc = ndp->active_channel; struct ncsi_cmd_arg nca; unsigned char index; int ret; nca.ndp = ndp; nca.driven = true; switch (nd->state) { case ncsi_dev_state_config: case ncsi_dev_state_config_sp: ndp->pending_req_num = 1; /* Select the specific package */ nca.type = NCSI_PKT_CMD_SP; if (ndp->flags & NCSI_DEV_HWA) nca.bytes[0] = 0; else nca.bytes[0] = 1; nca.package = np->id; nca.channel = 0x1f; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; nd->state = ncsi_dev_state_config_cis; break; case ncsi_dev_state_config_cis: ndp->pending_req_num = 1; /* Clear initial state */ nca.type = NCSI_PKT_CMD_CIS; nca.package = np->id; nca.channel = nc->id; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; nd->state = ncsi_dev_state_config_sma; break; case ncsi_dev_state_config_sma: case ncsi_dev_state_config_ebf: #if IS_ENABLED(CONFIG_IPV6) case ncsi_dev_state_config_egmf: #endif case ncsi_dev_state_config_ecnt: case ncsi_dev_state_config_ec: case ncsi_dev_state_config_ae: case ncsi_dev_state_config_gls: ndp->pending_req_num = 1; nca.package = np->id; nca.channel = nc->id; /* Use first entry in unicast filter table. Note that * the MAC filter table starts from entry 1 instead of * 0. */ if (nd->state == ncsi_dev_state_config_sma) { nca.type = NCSI_PKT_CMD_SMA; for (index =