|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2017 Hisilicon Limited. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/list.h> |
| 11 | +#include <linux/slab.h> |
| 12 | +#include <linux/spinlock.h> |
| 13 | + |
| 14 | +#include "hnae3.h" |
| 15 | + |
| 16 | +static LIST_HEAD(hnae3_ae_algo_list); |
| 17 | +static LIST_HEAD(hnae3_client_list); |
| 18 | +static LIST_HEAD(hnae3_ae_dev_list); |
| 19 | + |
| 20 | +/* we are keeping things simple and using single lock for all the |
| 21 | + * list. This is a non-critical code so other updations, if happen |
| 22 | + * in parallel, can wait. |
| 23 | + */ |
| 24 | +static DEFINE_MUTEX(hnae3_common_lock); |
| 25 | + |
| 26 | +static bool hnae3_client_match(enum hnae3_client_type client_type, |
| 27 | + enum hnae3_dev_type dev_type) |
| 28 | +{ |
| 29 | + if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC || |
| 30 | + client_type == HNAE3_CLIENT_ROCE)) |
| 31 | + return true; |
| 32 | + |
| 33 | + if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC) |
| 34 | + return true; |
| 35 | + |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +static int hnae3_match_n_instantiate(struct hnae3_client *client, |
| 40 | + struct hnae3_ae_dev *ae_dev, |
| 41 | + bool is_reg, bool *matched) |
| 42 | +{ |
| 43 | + int ret; |
| 44 | + |
| 45 | + *matched = false; |
| 46 | + |
| 47 | + /* check if this client matches the type of ae_dev */ |
| 48 | + if (!(hnae3_client_match(client->type, ae_dev->dev_type) && |
| 49 | + hnae_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + /* there is a match of client and dev */ |
| 53 | + *matched = true; |
| 54 | + |
| 55 | + /* now, (un-)instantiate client by calling lower layer */ |
| 56 | + if (is_reg) { |
| 57 | + ret = ae_dev->ops->init_client_instance(client, ae_dev); |
| 58 | + if (ret) |
| 59 | + dev_err(&ae_dev->pdev->dev, |
| 60 | + "fail to instantiate client\n"); |
| 61 | + return ret; |
| 62 | + } |
| 63 | + |
| 64 | + ae_dev->ops->uninit_client_instance(client, ae_dev); |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +int hnae3_register_client(struct hnae3_client *client) |
| 69 | +{ |
| 70 | + struct hnae3_client *client_tmp; |
| 71 | + struct hnae3_ae_dev *ae_dev; |
| 72 | + bool matched; |
| 73 | + int ret = 0; |
| 74 | + |
| 75 | + mutex_lock(&hnae3_common_lock); |
| 76 | + /* one system should only have one client for every type */ |
| 77 | + list_for_each_entry(client_tmp, &hnae3_client_list, node) { |
| 78 | + if (client_tmp->type == client->type) |
| 79 | + goto exit; |
| 80 | + } |
| 81 | + |
| 82 | + list_add_tail(&client->node, &hnae3_client_list); |
| 83 | + |
| 84 | + /* initialize the client on every matched port */ |
| 85 | + list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
| 86 | + /* if the client could not be initialized on current port, for |
| 87 | + * any error reasons, move on to next available port |
| 88 | + */ |
| 89 | + ret = hnae3_match_n_instantiate(client, ae_dev, true, &matched); |
| 90 | + if (ret) |
| 91 | + dev_err(&ae_dev->pdev->dev, |
| 92 | + "match and instantiation failed for port\n"); |
| 93 | + } |
| 94 | + |
| 95 | +exit: |
| 96 | + mutex_unlock(&hnae3_common_lock); |
| 97 | + |
| 98 | + return ret; |
| 99 | +} |
| 100 | +EXPORT_SYMBOL(hnae3_register_client); |
| 101 | + |
| 102 | +void hnae3_unregister_client(struct hnae3_client *client) |
| 103 | +{ |
| 104 | + struct hnae3_ae_dev *ae_dev; |
| 105 | + bool matched; |
| 106 | + |
| 107 | + mutex_lock(&hnae3_common_lock); |
| 108 | + /* un-initialize the client on every matched port */ |
| 109 | + list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
| 110 | + hnae3_match_n_instantiate(client, ae_dev, false, &matched); |
| 111 | + } |
| 112 | + |
| 113 | + list_del(&client->node); |
| 114 | + mutex_unlock(&hnae3_common_lock); |
| 115 | +} |
| 116 | +EXPORT_SYMBOL(hnae3_unregister_client); |
| 117 | + |
| 118 | +/* hnae3_register_ae_algo - register a AE algorithm to hnae3 framework |
| 119 | + * @ae_algo: AE algorithm |
| 120 | + * NOTE: the duplicated name will not be checked |
| 121 | + */ |
| 122 | +int hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo) |
| 123 | +{ |
| 124 | + const struct pci_device_id *id; |
| 125 | + struct hnae3_ae_dev *ae_dev; |
| 126 | + struct hnae3_client *client; |
| 127 | + bool matched; |
| 128 | + int ret = 0; |
| 129 | + |
| 130 | + mutex_lock(&hnae3_common_lock); |
| 131 | + |
| 132 | + list_add_tail(&ae_algo->node, &hnae3_ae_algo_list); |
| 133 | + |
| 134 | + /* Check if this algo/ops matches the list of ae_devs */ |
| 135 | + list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
| 136 | + id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev); |
| 137 | + if (!id) |
| 138 | + continue; |
| 139 | + |
| 140 | + /* ae_dev init should set flag */ |
| 141 | + ae_dev->ops = ae_algo->ops; |
| 142 | + ret = ae_algo->ops->init_ae_dev(ae_dev); |
| 143 | + if (ret) { |
| 144 | + dev_err(&ae_dev->pdev->dev, "init ae_dev error.\n"); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1); |
| 149 | + |
| 150 | + /* check the client list for the match with this ae_dev type and |
| 151 | + * initialize the figure out client instance |
| 152 | + */ |
| 153 | + list_for_each_entry(client, &hnae3_client_list, node) { |
| 154 | + ret = hnae3_match_n_instantiate(client, ae_dev, true, |
| 155 | + &matched); |
| 156 | + if (ret) |
| 157 | + dev_err(&ae_dev->pdev->dev, |
| 158 | + "match and instantiation failed\n"); |
| 159 | + if (matched) |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + mutex_unlock(&hnae3_common_lock); |
| 165 | + |
| 166 | + return ret; |
| 167 | +} |
| 168 | +EXPORT_SYMBOL(hnae3_register_ae_algo); |
| 169 | + |
| 170 | +/* hnae3_unregister_ae_algo - unregisters a AE algorithm |
| 171 | + * @ae_algo: the AE algorithm to unregister |
| 172 | + */ |
| 173 | +void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo) |
| 174 | +{ |
| 175 | + const struct pci_device_id *id; |
| 176 | + struct hnae3_ae_dev *ae_dev; |
| 177 | + struct hnae3_client *client; |
| 178 | + bool matched; |
| 179 | + |
| 180 | + mutex_lock(&hnae3_common_lock); |
| 181 | + /* Check if there are matched ae_dev */ |
| 182 | + list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
| 183 | + id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev); |
| 184 | + if (!id) |
| 185 | + continue; |
| 186 | + |
| 187 | + /* check the client list for the match with this ae_dev type and |
| 188 | + * un-initialize the figure out client instance |
| 189 | + */ |
| 190 | + list_for_each_entry(client, &hnae3_client_list, node) { |
| 191 | + hnae3_match_n_instantiate(client, ae_dev, false, |
| 192 | + &matched); |
| 193 | + if (matched) |
| 194 | + break; |
| 195 | + } |
| 196 | + |
| 197 | + ae_algo->ops->uninit_ae_dev(ae_dev); |
| 198 | + hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0); |
| 199 | + } |
| 200 | + |
| 201 | + list_del(&ae_algo->node); |
| 202 | + mutex_unlock(&hnae3_common_lock); |
| 203 | +} |
| 204 | +EXPORT_SYMBOL(hnae3_unregister_ae_algo); |
| 205 | + |
| 206 | +/* hnae3_register_ae_dev - registers a AE device to hnae3 framework |
| 207 | + * @ae_dev: the AE device |
| 208 | + * NOTE: the duplicated name will not be checked |
| 209 | + */ |
| 210 | +int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev) |
| 211 | +{ |
| 212 | + const struct pci_device_id *id; |
| 213 | + struct hnae3_ae_algo *ae_algo; |
| 214 | + struct hnae3_client *client; |
| 215 | + bool matched; |
| 216 | + int ret = 0; |
| 217 | + |
| 218 | + mutex_lock(&hnae3_common_lock); |
| 219 | + list_add_tail(&ae_dev->node, &hnae3_ae_dev_list); |
| 220 | + |
| 221 | + /* Check if there are matched ae_algo */ |
| 222 | + list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) { |
| 223 | + id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev); |
| 224 | + if (!id) |
| 225 | + continue; |
| 226 | + |
| 227 | + ae_dev->ops = ae_algo->ops; |
| 228 | + |
| 229 | + if (!ae_dev->ops) { |
| 230 | + dev_err(&ae_dev->pdev->dev, "ae_dev ops are null\n"); |
| 231 | + goto out_err; |
| 232 | + } |
| 233 | + |
| 234 | + /* ae_dev init should set flag */ |
| 235 | + ret = ae_dev->ops->init_ae_dev(ae_dev); |
| 236 | + if (ret) { |
| 237 | + dev_err(&ae_dev->pdev->dev, "init ae_dev error\n"); |
| 238 | + goto out_err; |
| 239 | + } |
| 240 | + |
| 241 | + hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 1); |
| 242 | + break; |
| 243 | + } |
| 244 | + |
| 245 | + /* check the client list for the match with this ae_dev type and |
| 246 | + * initialize the figure out client instance |
| 247 | + */ |
| 248 | + list_for_each_entry(client, &hnae3_client_list, node) { |
| 249 | + ret = hnae3_match_n_instantiate(client, ae_dev, true, |
| 250 | + &matched); |
| 251 | + if (ret) |
| 252 | + dev_err(&ae_dev->pdev->dev, |
| 253 | + "match and instantiation failed\n"); |
| 254 | + if (matched) |
| 255 | + break; |
| 256 | + } |
| 257 | + |
| 258 | +out_err: |
| 259 | + mutex_unlock(&hnae3_common_lock); |
| 260 | + |
| 261 | + return ret; |
| 262 | +} |
| 263 | +EXPORT_SYMBOL(hnae3_register_ae_dev); |
| 264 | + |
| 265 | +/* hnae3_unregister_ae_dev - unregisters a AE device |
| 266 | + * @ae_dev: the AE device to unregister |
| 267 | + */ |
| 268 | +void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev) |
| 269 | +{ |
| 270 | + const struct pci_device_id *id; |
| 271 | + struct hnae3_ae_algo *ae_algo; |
| 272 | + struct hnae3_client *client; |
| 273 | + bool matched; |
| 274 | + |
| 275 | + mutex_lock(&hnae3_common_lock); |
| 276 | + /* Check if there are matched ae_algo */ |
| 277 | + list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) { |
| 278 | + id = pci_match_id(ae_algo->pdev_id_table, ae_dev->pdev); |
| 279 | + if (!id) |
| 280 | + continue; |
| 281 | + |
| 282 | + list_for_each_entry(client, &hnae3_client_list, node) { |
| 283 | + hnae3_match_n_instantiate(client, ae_dev, false, |
| 284 | + &matched); |
| 285 | + if (matched) |
| 286 | + break; |
| 287 | + } |
| 288 | + |
| 289 | + ae_algo->ops->uninit_ae_dev(ae_dev); |
| 290 | + hnae_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0); |
| 291 | + } |
| 292 | + |
| 293 | + list_del(&ae_dev->node); |
| 294 | + mutex_unlock(&hnae3_common_lock); |
| 295 | +} |
| 296 | +EXPORT_SYMBOL(hnae3_unregister_ae_dev); |
| 297 | + |
| 298 | +MODULE_AUTHOR("Huawei Tech. Co., Ltd."); |
| 299 | +MODULE_LICENSE("GPL"); |
| 300 | +MODULE_DESCRIPTION("HNAE3(Hisilicon Network Acceleration Engine) Framework"); |
0 commit comments