.. | .. |
---|
37 | 37 | */ |
---|
38 | 38 | static DEFINE_MUTEX(hnae3_common_lock); |
---|
39 | 39 | |
---|
40 | | -static bool hnae3_client_match(enum hnae3_client_type client_type, |
---|
41 | | - enum hnae3_dev_type dev_type) |
---|
| 40 | +static bool hnae3_client_match(enum hnae3_client_type client_type) |
---|
42 | 41 | { |
---|
43 | | - if ((dev_type == HNAE3_DEV_KNIC) && (client_type == HNAE3_CLIENT_KNIC || |
---|
44 | | - client_type == HNAE3_CLIENT_ROCE)) |
---|
45 | | - return true; |
---|
46 | | - |
---|
47 | | - if (dev_type == HNAE3_DEV_UNIC && client_type == HNAE3_CLIENT_UNIC) |
---|
| 42 | + if (client_type == HNAE3_CLIENT_KNIC || |
---|
| 43 | + client_type == HNAE3_CLIENT_ROCE) |
---|
48 | 44 | return true; |
---|
49 | 45 | |
---|
50 | 46 | return false; |
---|
51 | 47 | } |
---|
52 | 48 | |
---|
53 | 49 | void hnae3_set_client_init_flag(struct hnae3_client *client, |
---|
54 | | - struct hnae3_ae_dev *ae_dev, int inited) |
---|
| 50 | + struct hnae3_ae_dev *ae_dev, |
---|
| 51 | + unsigned int inited) |
---|
55 | 52 | { |
---|
| 53 | + if (!client || !ae_dev) |
---|
| 54 | + return; |
---|
| 55 | + |
---|
56 | 56 | switch (client->type) { |
---|
57 | 57 | case HNAE3_CLIENT_KNIC: |
---|
58 | 58 | hnae3_set_bit(ae_dev->flag, HNAE3_KNIC_CLIENT_INITED_B, inited); |
---|
59 | | - break; |
---|
60 | | - case HNAE3_CLIENT_UNIC: |
---|
61 | | - hnae3_set_bit(ae_dev->flag, HNAE3_UNIC_CLIENT_INITED_B, inited); |
---|
62 | 59 | break; |
---|
63 | 60 | case HNAE3_CLIENT_ROCE: |
---|
64 | 61 | hnae3_set_bit(ae_dev->flag, HNAE3_ROCE_CLIENT_INITED_B, inited); |
---|
.. | .. |
---|
70 | 67 | EXPORT_SYMBOL(hnae3_set_client_init_flag); |
---|
71 | 68 | |
---|
72 | 69 | static int hnae3_get_client_init_flag(struct hnae3_client *client, |
---|
73 | | - struct hnae3_ae_dev *ae_dev) |
---|
| 70 | + struct hnae3_ae_dev *ae_dev) |
---|
74 | 71 | { |
---|
75 | 72 | int inited = 0; |
---|
76 | 73 | |
---|
.. | .. |
---|
78 | 75 | case HNAE3_CLIENT_KNIC: |
---|
79 | 76 | inited = hnae3_get_bit(ae_dev->flag, |
---|
80 | 77 | HNAE3_KNIC_CLIENT_INITED_B); |
---|
81 | | - break; |
---|
82 | | - case HNAE3_CLIENT_UNIC: |
---|
83 | | - inited = hnae3_get_bit(ae_dev->flag, |
---|
84 | | - HNAE3_UNIC_CLIENT_INITED_B); |
---|
85 | 78 | break; |
---|
86 | 79 | case HNAE3_CLIENT_ROCE: |
---|
87 | 80 | inited = hnae3_get_bit(ae_dev->flag, |
---|
.. | .. |
---|
94 | 87 | return inited; |
---|
95 | 88 | } |
---|
96 | 89 | |
---|
97 | | -static int hnae3_match_n_instantiate(struct hnae3_client *client, |
---|
98 | | - struct hnae3_ae_dev *ae_dev, bool is_reg) |
---|
| 90 | +static int hnae3_init_client_instance(struct hnae3_client *client, |
---|
| 91 | + struct hnae3_ae_dev *ae_dev) |
---|
99 | 92 | { |
---|
100 | 93 | int ret; |
---|
101 | 94 | |
---|
102 | 95 | /* check if this client matches the type of ae_dev */ |
---|
103 | | - if (!(hnae3_client_match(client->type, ae_dev->dev_type) && |
---|
| 96 | + if (!(hnae3_client_match(client->type) && |
---|
104 | 97 | hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) { |
---|
105 | 98 | return 0; |
---|
106 | 99 | } |
---|
107 | 100 | |
---|
108 | | - /* now, (un-)instantiate client by calling lower layer */ |
---|
109 | | - if (is_reg) { |
---|
110 | | - ret = ae_dev->ops->init_client_instance(client, ae_dev); |
---|
111 | | - if (ret) |
---|
112 | | - dev_err(&ae_dev->pdev->dev, |
---|
113 | | - "fail to instantiate client, ret = %d\n", ret); |
---|
| 101 | + ret = ae_dev->ops->init_client_instance(client, ae_dev); |
---|
| 102 | + if (ret) |
---|
| 103 | + dev_err(&ae_dev->pdev->dev, |
---|
| 104 | + "fail to instantiate client, ret = %d\n", ret); |
---|
114 | 105 | |
---|
115 | | - return ret; |
---|
116 | | - } |
---|
| 106 | + return ret; |
---|
| 107 | +} |
---|
| 108 | + |
---|
| 109 | +static void hnae3_uninit_client_instance(struct hnae3_client *client, |
---|
| 110 | + struct hnae3_ae_dev *ae_dev) |
---|
| 111 | +{ |
---|
| 112 | + /* check if this client matches the type of ae_dev */ |
---|
| 113 | + if (!(hnae3_client_match(client->type) && |
---|
| 114 | + hnae3_get_bit(ae_dev->flag, HNAE3_DEV_INITED_B))) |
---|
| 115 | + return; |
---|
117 | 116 | |
---|
118 | 117 | if (hnae3_get_client_init_flag(client, ae_dev)) { |
---|
119 | 118 | ae_dev->ops->uninit_client_instance(client, ae_dev); |
---|
120 | 119 | |
---|
121 | 120 | hnae3_set_client_init_flag(client, ae_dev, 0); |
---|
122 | 121 | } |
---|
123 | | - |
---|
124 | | - return 0; |
---|
125 | 122 | } |
---|
126 | 123 | |
---|
127 | 124 | int hnae3_register_client(struct hnae3_client *client) |
---|
128 | 125 | { |
---|
129 | 126 | struct hnae3_client *client_tmp; |
---|
130 | 127 | struct hnae3_ae_dev *ae_dev; |
---|
131 | | - int ret = 0; |
---|
| 128 | + |
---|
| 129 | + if (!client) |
---|
| 130 | + return -ENODEV; |
---|
132 | 131 | |
---|
133 | 132 | mutex_lock(&hnae3_common_lock); |
---|
134 | 133 | /* one system should only have one client for every type */ |
---|
.. | .. |
---|
144 | 143 | /* if the client could not be initialized on current port, for |
---|
145 | 144 | * any error reasons, move on to next available port |
---|
146 | 145 | */ |
---|
147 | | - ret = hnae3_match_n_instantiate(client, ae_dev, true); |
---|
| 146 | + int ret = hnae3_init_client_instance(client, ae_dev); |
---|
148 | 147 | if (ret) |
---|
149 | 148 | dev_err(&ae_dev->pdev->dev, |
---|
150 | 149 | "match and instantiation failed for port, ret = %d\n", |
---|
.. | .. |
---|
160 | 159 | |
---|
161 | 160 | void hnae3_unregister_client(struct hnae3_client *client) |
---|
162 | 161 | { |
---|
| 162 | + struct hnae3_client *client_tmp; |
---|
163 | 163 | struct hnae3_ae_dev *ae_dev; |
---|
| 164 | + bool existed = false; |
---|
| 165 | + |
---|
| 166 | + if (!client) |
---|
| 167 | + return; |
---|
164 | 168 | |
---|
165 | 169 | mutex_lock(&hnae3_common_lock); |
---|
| 170 | + /* one system should only have one client for every type */ |
---|
| 171 | + list_for_each_entry(client_tmp, &hnae3_client_list, node) { |
---|
| 172 | + if (client_tmp->type == client->type) { |
---|
| 173 | + existed = true; |
---|
| 174 | + break; |
---|
| 175 | + } |
---|
| 176 | + } |
---|
| 177 | + |
---|
| 178 | + if (!existed) { |
---|
| 179 | + mutex_unlock(&hnae3_common_lock); |
---|
| 180 | + pr_err("client %s does not exist!\n", client->name); |
---|
| 181 | + return; |
---|
| 182 | + } |
---|
| 183 | + |
---|
166 | 184 | /* un-initialize the client on every matched port */ |
---|
167 | 185 | list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
---|
168 | | - hnae3_match_n_instantiate(client, ae_dev, false); |
---|
| 186 | + hnae3_uninit_client_instance(client, ae_dev); |
---|
169 | 187 | } |
---|
170 | 188 | |
---|
171 | 189 | list_del(&client->node); |
---|
.. | .. |
---|
182 | 200 | const struct pci_device_id *id; |
---|
183 | 201 | struct hnae3_ae_dev *ae_dev; |
---|
184 | 202 | struct hnae3_client *client; |
---|
185 | | - int ret = 0; |
---|
| 203 | + int ret; |
---|
| 204 | + |
---|
| 205 | + if (!ae_algo) |
---|
| 206 | + return; |
---|
186 | 207 | |
---|
187 | 208 | mutex_lock(&hnae3_common_lock); |
---|
188 | 209 | |
---|
.. | .. |
---|
214 | 235 | * initialize the figure out client instance |
---|
215 | 236 | */ |
---|
216 | 237 | list_for_each_entry(client, &hnae3_client_list, node) { |
---|
217 | | - ret = hnae3_match_n_instantiate(client, ae_dev, true); |
---|
| 238 | + ret = hnae3_init_client_instance(client, ae_dev); |
---|
218 | 239 | if (ret) |
---|
219 | 240 | dev_err(&ae_dev->pdev->dev, |
---|
220 | 241 | "match and instantiation failed, ret = %d\n", |
---|
.. | .. |
---|
235 | 256 | struct hnae3_ae_dev *ae_dev; |
---|
236 | 257 | struct hnae3_client *client; |
---|
237 | 258 | |
---|
| 259 | + if (!ae_algo) |
---|
| 260 | + return; |
---|
| 261 | + |
---|
238 | 262 | mutex_lock(&hnae3_common_lock); |
---|
239 | 263 | /* Check if there are matched ae_dev */ |
---|
240 | 264 | list_for_each_entry(ae_dev, &hnae3_ae_dev_list, node) { |
---|
.. | .. |
---|
249 | 273 | * un-initialize the figure out client instance |
---|
250 | 274 | */ |
---|
251 | 275 | list_for_each_entry(client, &hnae3_client_list, node) |
---|
252 | | - hnae3_match_n_instantiate(client, ae_dev, false); |
---|
| 276 | + hnae3_uninit_client_instance(client, ae_dev); |
---|
253 | 277 | |
---|
254 | 278 | ae_algo->ops->uninit_ae_dev(ae_dev); |
---|
255 | 279 | hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0); |
---|
.. | .. |
---|
270 | 294 | const struct pci_device_id *id; |
---|
271 | 295 | struct hnae3_ae_algo *ae_algo; |
---|
272 | 296 | struct hnae3_client *client; |
---|
273 | | - int ret = 0; |
---|
| 297 | + int ret; |
---|
| 298 | + |
---|
| 299 | + if (!ae_dev) |
---|
| 300 | + return -ENODEV; |
---|
274 | 301 | |
---|
275 | 302 | mutex_lock(&hnae3_common_lock); |
---|
276 | 303 | |
---|
.. | .. |
---|
305 | 332 | * initialize the figure out client instance |
---|
306 | 333 | */ |
---|
307 | 334 | list_for_each_entry(client, &hnae3_client_list, node) { |
---|
308 | | - ret = hnae3_match_n_instantiate(client, ae_dev, true); |
---|
| 335 | + ret = hnae3_init_client_instance(client, ae_dev); |
---|
309 | 336 | if (ret) |
---|
310 | 337 | dev_err(&ae_dev->pdev->dev, |
---|
311 | 338 | "match and instantiation failed, ret = %d\n", |
---|
.. | .. |
---|
333 | 360 | struct hnae3_ae_algo *ae_algo; |
---|
334 | 361 | struct hnae3_client *client; |
---|
335 | 362 | |
---|
| 363 | + if (!ae_dev) |
---|
| 364 | + return; |
---|
| 365 | + |
---|
336 | 366 | mutex_lock(&hnae3_common_lock); |
---|
337 | 367 | /* Check if there are matched ae_algo */ |
---|
338 | 368 | list_for_each_entry(ae_algo, &hnae3_ae_algo_list, node) { |
---|
.. | .. |
---|
344 | 374 | continue; |
---|
345 | 375 | |
---|
346 | 376 | list_for_each_entry(client, &hnae3_client_list, node) |
---|
347 | | - hnae3_match_n_instantiate(client, ae_dev, false); |
---|
| 377 | + hnae3_uninit_client_instance(client, ae_dev); |
---|
348 | 378 | |
---|
349 | 379 | ae_algo->ops->uninit_ae_dev(ae_dev); |
---|
350 | 380 | hnae3_set_bit(ae_dev->flag, HNAE3_DEV_INITED_B, 0); |
---|