1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/drivers/net/wireless/quantenna/qtnfmac/bus.h
Gustavo A. R. Silva 55bb8a2b01 qtnfmac: Replace zero-length array with flexible-array
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773293 ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Sergey Matyukevich <sergey.matyukevich.os@quantenna.com>
Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
Link: https://lore.kernel.org/r/20200507191926.GA15970@embeddedor
2020-05-12 11:56:28 +03:00

158 lines
3.6 KiB
C

/* SPDX-License-Identifier: GPL-2.0+ */
/* Copyright (c) 2015 Quantenna Communications. All rights reserved. */
#ifndef QTNFMAC_BUS_H
#define QTNFMAC_BUS_H
#include <linux/netdevice.h>
#include <linux/workqueue.h>
#include "trans.h"
#include "core.h"
#define QTNF_MAX_MAC 3
#define HBM_FRAME_META_MAGIC_PATTERN_S 0xAB
#define HBM_FRAME_META_MAGIC_PATTERN_E 0xBA
struct qtnf_frame_meta_info {
u8 magic_s;
u8 ifidx;
u8 macid;
u8 magic_e;
} __packed;
enum qtnf_fw_state {
QTNF_FW_STATE_DETACHED,
QTNF_FW_STATE_BOOT_DONE,
QTNF_FW_STATE_ACTIVE,
QTNF_FW_STATE_RUNNING,
QTNF_FW_STATE_DEAD,
};
struct qtnf_bus;
struct qtnf_bus_ops {
/* mgmt methods */
int (*preinit)(struct qtnf_bus *);
void (*stop)(struct qtnf_bus *);
/* control path methods */
int (*control_tx)(struct qtnf_bus *, struct sk_buff *);
/* data xfer methods */
int (*data_tx)(struct qtnf_bus *bus, struct sk_buff *skb,
unsigned int macid, unsigned int vifid);
void (*data_tx_timeout)(struct qtnf_bus *, struct net_device *);
void (*data_tx_use_meta_set)(struct qtnf_bus *bus, bool use_meta);
void (*data_rx_start)(struct qtnf_bus *);
void (*data_rx_stop)(struct qtnf_bus *);
};
struct qtnf_bus {
struct device *dev;
enum qtnf_fw_state fw_state;
u32 chip;
u32 chiprev;
struct qtnf_bus_ops *bus_ops;
struct qtnf_wmac *mac[QTNF_MAX_MAC];
struct qtnf_qlink_transport trans;
struct qtnf_hw_info hw_info;
struct napi_struct mux_napi;
struct net_device mux_dev;
struct workqueue_struct *workqueue;
struct workqueue_struct *hprio_workqueue;
struct work_struct fw_work;
struct work_struct event_work;
struct mutex bus_lock; /* lock during command/event processing */
struct dentry *dbg_dir;
struct notifier_block netdev_nb;
u8 hw_id[ETH_ALEN];
/* bus private data */
char bus_priv[] __aligned(sizeof(void *));
};
static inline bool qtnf_fw_is_up(struct qtnf_bus *bus)
{
enum qtnf_fw_state state = bus->fw_state;
return ((state == QTNF_FW_STATE_ACTIVE) ||
(state == QTNF_FW_STATE_RUNNING));
}
static inline bool qtnf_fw_is_attached(struct qtnf_bus *bus)
{
enum qtnf_fw_state state = bus->fw_state;
return ((state == QTNF_FW_STATE_ACTIVE) ||
(state == QTNF_FW_STATE_RUNNING) ||
(state == QTNF_FW_STATE_DEAD));
}
static inline void *get_bus_priv(struct qtnf_bus *bus)
{
if (WARN(!bus, "qtnfmac: invalid bus pointer"))
return NULL;
return &bus->bus_priv;
}
/* callback wrappers */
static inline int qtnf_bus_preinit(struct qtnf_bus *bus)
{
if (!bus->bus_ops->preinit)
return 0;
return bus->bus_ops->preinit(bus);
}
static inline void qtnf_bus_stop(struct qtnf_bus *bus)
{
if (!bus->bus_ops->stop)
return;
bus->bus_ops->stop(bus);
}
static inline int qtnf_bus_data_tx(struct qtnf_bus *bus, struct sk_buff *skb,
unsigned int macid, unsigned int vifid)
{
return bus->bus_ops->data_tx(bus, skb, macid, vifid);
}
static inline void
qtnf_bus_data_tx_timeout(struct qtnf_bus *bus, struct net_device *ndev)
{
return bus->bus_ops->data_tx_timeout(bus, ndev);
}
static inline int qtnf_bus_control_tx(struct qtnf_bus *bus, struct sk_buff *skb)
{
return bus->bus_ops->control_tx(bus, skb);
}
static inline void qtnf_bus_data_rx_start(struct qtnf_bus *bus)
{
return bus->bus_ops->data_rx_start(bus);
}
static inline void qtnf_bus_data_rx_stop(struct qtnf_bus *bus)
{
return bus->bus_ops->data_rx_stop(bus);
}
static __always_inline void qtnf_bus_lock(struct qtnf_bus *bus)
{
mutex_lock(&bus->bus_lock);
}
static __always_inline void qtnf_bus_unlock(struct qtnf_bus *bus)
{
mutex_unlock(&bus->bus_lock);
}
/* interface functions from common layer */
int qtnf_core_attach(struct qtnf_bus *bus);
void qtnf_core_detach(struct qtnf_bus *bus);
#endif /* QTNFMAC_BUS_H */