cregit-Linux how code gets into the kernel

Release 4.11 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 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 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 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 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; unsigned long flags; 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) { spin_lock_irqsave(&nc->lock, flags); if (!list_empty(&nc->link) || nc->state != NCSI_CHANNEL_ACTIVE) { spin_unlock_irqrestore(&nc->lock, flags); continue; } if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) { spin_unlock_irqrestore(&nc->lock, flags); nd->link_up = 1; goto report; } spin_unlock_irqrestore(&nc->lock, flags); } } report: nd->handler(nd); }

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan177100.00%3100.00%
Total177100.00%3100.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, chained; unsigned int monitor_state; unsigned long flags; int state, ret; spin_lock_irqsave(&nc->lock, flags); state = nc->state; chained = !list_empty(&nc->link); enabled = nc->monitor.enabled; monitor_state = nc->monitor.state; spin_unlock_irqrestore(&nc->lock, flags); if (!enabled || chained) return; if (state != NCSI_CHANNEL_INACTIVE && state != NCSI_CHANNEL_ACTIVE) return; switch (monitor_state) { case NCSI_CHANNEL_MONITOR_START: case NCSI_CHANNEL_MONITOR_RETRY: nca.ndp = ndp; nca.package = np->id; nca.channel = nc->id; nca.type = NCSI_PKT_CMD_GLS; nca.req_flags = 0; ret = ncsi_xmit_cmd(&nca); if (ret) { netdev_err(ndp->ndev.dev, "Error %d sending GLS\n", ret); return; } break; case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX: break; default: if (!(ndp->flags & NCSI_DEV_HWA) && state == NCSI_CHANNEL_ACTIVE) { ncsi_report_link(ndp, true); ndp->flags |= NCSI_DEV_RESHUFFLE; } spin_lock_irqsave(&nc->lock, flags); nc->state = NCSI_CHANNEL_INVISIBLE; spin_unlock_irqrestore(&nc->lock, flags); spin_lock_irqsave(&ndp->lock, flags); 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->monitor.state++; spin_unlock_irqrestore(&nc->lock, flags); mod_timer(&nc->monitor.timer, jiffies + HZ); }

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan358100.00%5100.00%
Total358100.00%5100.00%


void ncsi_start_channel_monitor(struct ncsi_channel *nc) { unsigned long flags; spin_lock_irqsave(&nc->lock, flags); WARN_ON_ONCE(nc->monitor.enabled); nc->monitor.enabled = true; nc->monitor.state = NCSI_CHANNEL_MONITOR_START; spin_unlock_irqrestore(&nc->lock, flags); mod_timer(&nc->monitor.timer, jiffies + HZ); }

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan73100.00%3100.00%
Total73100.00%3100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan74100.00%3100.00%
Total74100.00%3100.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 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->monitor.enabled = false; setup_timer(&nc->monitor.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 Shan234100.00%3100.00%
Total234100.00%3100.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 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 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 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 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 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, unsigned int req_flags) { 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; i < limit; i++) { if (ndp->requests[i].used) continue; nr = &ndp->requests[i]; nr->used = true; nr->flags = req_flags; ndp->request_id = i + 1; goto found; } /* Fail back to check from the starting cursor */ for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) { if (ndp->requests[i].used) continue; nr = &ndp->requests[i]; nr->used = true; nr->flags = req_flags; ndp->request_id = i + 1; goto found; } found: spin_unlock_irqrestore(&ndp->lock, flags); return nr; }

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan190100.00%4100.00%
Total190100.00%4100.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->flags & NCSI_REQ_FLAG_EVENT_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 Shan150100.00%2100.00%
Total150100.00%2100.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 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 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; unsigned long flags; int ret; nca.ndp = ndp; nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN; switch (nd->state) { case ncsi_dev_state_suspend: nd->state = ncsi_dev_state_suspend_select; /* Fall through */ case ncsi_dev_state_suspend_select: ndp->pending_req_num = 1; nca.type = NCSI_PKT_CMD_SP; nca.package = np->id; nca.channel = NCSI_RESERVED_CHANNEL; if (ndp->flags & NCSI_DEV_HWA) nca.bytes[0] = 0; else nca.bytes[0] = 1; /* To retrieve the last link states of channels in current * package when current active channel needs fail over to * another one. It means we will possibly select another * channel as next active one. The link states of channels * are most important factor of the selection. So we need * accurate link states. Unfortunately, the link states on * inactive channels can't be updated with LSC AEN in time. */ if (ndp->flags & NCSI_DEV_RESHUFFLE) nd->state = ncsi_dev_state_suspend_gls; else nd->state = ncsi_dev_state_suspend_dcnt; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; break; case ncsi_dev_state_suspend_gls: ndp->pending_req_num = np->channel_num; nca.type = NCSI_PKT_CMD_GLS; nca.package = np->id; nd->state = ncsi_dev_state_suspend_dcnt; NCSI_FOR_EACH_CHANNEL(np, nc) { nca.channel = nc->id; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; } break; case ncsi_dev_state_suspend_dcnt: ndp->pending_req_num = 1; nca.type = NCSI_PKT_CMD_DCNT; nca.package = np->id; nca.channel = nc->id; nd->state = ncsi_dev_state_suspend_dc; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; break; case ncsi_dev_state_suspend_dc: ndp->pending_req_num = 1; nca.type = NCSI_PKT_CMD_DC; nca.package = np->id; nca.channel = nc->id; nca.bytes[0] = 1; nd->state = ncsi_dev_state_suspend_deselect; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; break; case ncsi_dev_state_suspend_deselect: ndp->pending_req_num = 1; nca.type = NCSI_PKT_CMD_DP; nca.package = np->id; nca.channel = NCSI_RESERVED_CHANNEL; nd->state = ncsi_dev_state_suspend_done; ret = ncsi_xmit_cmd(&nca); if (ret) goto error; break; case ncsi_dev_state_suspend_done: spin_lock_irqsave(&nc->lock, flags); nc->state = NCSI_CHANNEL_INACTIVE; spin_unlock_irqrestore(&nc->lock, flags); ncsi_process_next_channel(ndp); break; default: netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n", nd->state); } return; error: nd->state = ncsi_dev_state_functional; }

Contributors

PersonTokensPropCommitsCommitProp
Gavin Shan461100.00%6100.00%
Total461100.00%6100.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_channel *hot_nc = NULL; struct ncsi_cmd_arg nca; unsigned char index; unsigned long flags; int ret; nca.ndp = ndp; nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN; 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 = NCSI_RESERVED_CHANNEL; ret = ncsi_xmit_cmd(&nca); if (ret)