• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020, Mellanox Technologies inc.  All rights reserved. */
3 
4 #include "fw_reset.h"
5 #include "diag/fw_tracer.h"
6 
7 enum {
8 	MLX5_FW_RESET_FLAGS_RESET_REQUESTED,
9 	MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST,
10 	MLX5_FW_RESET_FLAGS_PENDING_COMP
11 };
12 
13 struct mlx5_fw_reset {
14 	struct mlx5_core_dev *dev;
15 	struct mlx5_nb nb;
16 	struct workqueue_struct *wq;
17 	struct work_struct fw_live_patch_work;
18 	struct work_struct reset_request_work;
19 	struct work_struct reset_reload_work;
20 	struct work_struct reset_now_work;
21 	struct work_struct reset_abort_work;
22 	unsigned long reset_flags;
23 	struct timer_list timer;
24 	struct completion done;
25 	int ret;
26 };
27 
mlx5_fw_reset_enable_remote_dev_reset_set(struct mlx5_core_dev * dev,bool enable)28 void mlx5_fw_reset_enable_remote_dev_reset_set(struct mlx5_core_dev *dev, bool enable)
29 {
30 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
31 
32 	if (enable)
33 		clear_bit(MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST, &fw_reset->reset_flags);
34 	else
35 		set_bit(MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST, &fw_reset->reset_flags);
36 }
37 
mlx5_fw_reset_enable_remote_dev_reset_get(struct mlx5_core_dev * dev)38 bool mlx5_fw_reset_enable_remote_dev_reset_get(struct mlx5_core_dev *dev)
39 {
40 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
41 
42 	return !test_bit(MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST, &fw_reset->reset_flags);
43 }
44 
mlx5_reg_mfrl_set(struct mlx5_core_dev * dev,u8 reset_level,u8 reset_type_sel,u8 sync_resp,bool sync_start)45 static int mlx5_reg_mfrl_set(struct mlx5_core_dev *dev, u8 reset_level,
46 			     u8 reset_type_sel, u8 sync_resp, bool sync_start)
47 {
48 	u32 out[MLX5_ST_SZ_DW(mfrl_reg)] = {};
49 	u32 in[MLX5_ST_SZ_DW(mfrl_reg)] = {};
50 
51 	MLX5_SET(mfrl_reg, in, reset_level, reset_level);
52 	MLX5_SET(mfrl_reg, in, rst_type_sel, reset_type_sel);
53 	MLX5_SET(mfrl_reg, in, pci_sync_for_fw_update_resp, sync_resp);
54 	MLX5_SET(mfrl_reg, in, pci_sync_for_fw_update_start, sync_start);
55 
56 	return mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out), MLX5_REG_MFRL, 0, 1);
57 }
58 
mlx5_reg_mfrl_query(struct mlx5_core_dev * dev,u8 * reset_level,u8 * reset_type)59 static int mlx5_reg_mfrl_query(struct mlx5_core_dev *dev, u8 *reset_level, u8 *reset_type)
60 {
61 	u32 out[MLX5_ST_SZ_DW(mfrl_reg)] = {};
62 	u32 in[MLX5_ST_SZ_DW(mfrl_reg)] = {};
63 	int err;
64 
65 	err = mlx5_core_access_reg(dev, in, sizeof(in), out, sizeof(out), MLX5_REG_MFRL, 0, 0);
66 	if (err)
67 		return err;
68 
69 	if (reset_level)
70 		*reset_level = MLX5_GET(mfrl_reg, out, reset_level);
71 	if (reset_type)
72 		*reset_type = MLX5_GET(mfrl_reg, out, reset_type);
73 
74 	return 0;
75 }
76 
mlx5_fw_reset_query(struct mlx5_core_dev * dev,u8 * reset_level,u8 * reset_type)77 int mlx5_fw_reset_query(struct mlx5_core_dev *dev, u8 *reset_level, u8 *reset_type)
78 {
79 	return mlx5_reg_mfrl_query(dev, reset_level, reset_type);
80 }
81 
mlx5_fw_reset_set_reset_sync(struct mlx5_core_dev * dev,u8 reset_type_sel)82 int mlx5_fw_reset_set_reset_sync(struct mlx5_core_dev *dev, u8 reset_type_sel)
83 {
84 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
85 	int err;
86 
87 	set_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags);
88 	err = mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL3, reset_type_sel, 0, true);
89 	if (err)
90 		clear_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags);
91 	return err;
92 }
93 
mlx5_fw_reset_set_live_patch(struct mlx5_core_dev * dev)94 int mlx5_fw_reset_set_live_patch(struct mlx5_core_dev *dev)
95 {
96 	return mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL0, 0, 0, false);
97 }
98 
mlx5_fw_reset_complete_reload(struct mlx5_core_dev * dev)99 static void mlx5_fw_reset_complete_reload(struct mlx5_core_dev *dev)
100 {
101 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
102 
103 	/* if this is the driver that initiated the fw reset, devlink completed the reload */
104 	if (test_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags)) {
105 		complete(&fw_reset->done);
106 	} else {
107 		mlx5_load_one(dev);
108 		devlink_remote_reload_actions_performed(priv_to_devlink(dev), 0,
109 							BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
110 							BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE));
111 	}
112 }
113 
mlx5_stop_sync_reset_poll(struct mlx5_core_dev * dev)114 static void mlx5_stop_sync_reset_poll(struct mlx5_core_dev *dev)
115 {
116 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
117 
118 	del_timer_sync(&fw_reset->timer);
119 }
120 
mlx5_sync_reset_clear_reset_requested(struct mlx5_core_dev * dev,bool poll_health)121 static int mlx5_sync_reset_clear_reset_requested(struct mlx5_core_dev *dev, bool poll_health)
122 {
123 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
124 
125 	if (!test_and_clear_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags)) {
126 		mlx5_core_warn(dev, "Reset request was already cleared\n");
127 		return -EALREADY;
128 	}
129 
130 	mlx5_stop_sync_reset_poll(dev);
131 	if (poll_health)
132 		mlx5_start_health_poll(dev);
133 	return 0;
134 }
135 
mlx5_sync_reset_reload_work(struct work_struct * work)136 static void mlx5_sync_reset_reload_work(struct work_struct *work)
137 {
138 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
139 						      reset_reload_work);
140 	struct mlx5_core_dev *dev = fw_reset->dev;
141 	int err;
142 
143 	mlx5_sync_reset_clear_reset_requested(dev, false);
144 	mlx5_enter_error_state(dev, true);
145 	mlx5_unload_one(dev);
146 	err = mlx5_health_wait_pci_up(dev);
147 	if (err)
148 		mlx5_core_err(dev, "reset reload flow aborted, PCI reads still not working\n");
149 	fw_reset->ret = err;
150 	mlx5_fw_reset_complete_reload(dev);
151 }
152 
153 #define MLX5_RESET_POLL_INTERVAL	(HZ / 10)
poll_sync_reset(struct timer_list * t)154 static void poll_sync_reset(struct timer_list *t)
155 {
156 	struct mlx5_fw_reset *fw_reset = from_timer(fw_reset, t, timer);
157 	struct mlx5_core_dev *dev = fw_reset->dev;
158 	u32 fatal_error;
159 
160 	if (!test_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags))
161 		return;
162 
163 	fatal_error = mlx5_health_check_fatal_sensors(dev);
164 
165 	if (fatal_error) {
166 		mlx5_core_warn(dev, "Got Device Reset\n");
167 		queue_work(fw_reset->wq, &fw_reset->reset_reload_work);
168 		return;
169 	}
170 
171 	mod_timer(&fw_reset->timer, round_jiffies(jiffies + MLX5_RESET_POLL_INTERVAL));
172 }
173 
mlx5_start_sync_reset_poll(struct mlx5_core_dev * dev)174 static void mlx5_start_sync_reset_poll(struct mlx5_core_dev *dev)
175 {
176 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
177 
178 	timer_setup(&fw_reset->timer, poll_sync_reset, 0);
179 	fw_reset->timer.expires = round_jiffies(jiffies + MLX5_RESET_POLL_INTERVAL);
180 	add_timer(&fw_reset->timer);
181 }
182 
mlx5_fw_reset_set_reset_sync_ack(struct mlx5_core_dev * dev)183 static int mlx5_fw_reset_set_reset_sync_ack(struct mlx5_core_dev *dev)
184 {
185 	return mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL3, 0, 1, false);
186 }
187 
mlx5_fw_reset_set_reset_sync_nack(struct mlx5_core_dev * dev)188 static int mlx5_fw_reset_set_reset_sync_nack(struct mlx5_core_dev *dev)
189 {
190 	return mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL3, 0, 2, false);
191 }
192 
mlx5_sync_reset_set_reset_requested(struct mlx5_core_dev * dev)193 static int mlx5_sync_reset_set_reset_requested(struct mlx5_core_dev *dev)
194 {
195 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
196 
197 	if (test_and_set_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags)) {
198 		mlx5_core_warn(dev, "Reset request was already set\n");
199 		return -EALREADY;
200 	}
201 	mlx5_stop_health_poll(dev, true);
202 	mlx5_start_sync_reset_poll(dev);
203 	return 0;
204 }
205 
mlx5_fw_live_patch_event(struct work_struct * work)206 static void mlx5_fw_live_patch_event(struct work_struct *work)
207 {
208 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
209 						      fw_live_patch_work);
210 	struct mlx5_core_dev *dev = fw_reset->dev;
211 
212 	mlx5_core_info(dev, "Live patch updated firmware version: %d.%d.%d\n", fw_rev_maj(dev),
213 		       fw_rev_min(dev), fw_rev_sub(dev));
214 
215 	if (mlx5_fw_tracer_reload(dev->tracer))
216 		mlx5_core_err(dev, "Failed to reload FW tracer\n");
217 }
218 
mlx5_sync_reset_request_event(struct work_struct * work)219 static void mlx5_sync_reset_request_event(struct work_struct *work)
220 {
221 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
222 						      reset_request_work);
223 	struct mlx5_core_dev *dev = fw_reset->dev;
224 	int err;
225 
226 	if (test_bit(MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST, &fw_reset->reset_flags)) {
227 		err = mlx5_fw_reset_set_reset_sync_nack(dev);
228 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Nack %s",
229 			       err ? "Failed" : "Sent");
230 		return;
231 	}
232 	if (mlx5_sync_reset_set_reset_requested(dev))
233 		return;
234 
235 	err = mlx5_fw_reset_set_reset_sync_ack(dev);
236 	if (err)
237 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Ack Failed. Error code: %d\n", err);
238 	else
239 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Ack. Device reset is expected.\n");
240 }
241 
242 #define MLX5_PCI_LINK_UP_TIMEOUT 2000
243 
mlx5_pci_link_toggle(struct mlx5_core_dev * dev)244 static int mlx5_pci_link_toggle(struct mlx5_core_dev *dev)
245 {
246 	struct pci_bus *bridge_bus = dev->pdev->bus;
247 	struct pci_dev *bridge = bridge_bus->self;
248 	u16 reg16, dev_id, sdev_id;
249 	unsigned long timeout;
250 	struct pci_dev *sdev;
251 	int cap, err;
252 	u32 reg32;
253 
254 	/* Check that all functions under the pci bridge are PFs of
255 	 * this device otherwise fail this function.
256 	 */
257 	err = pci_read_config_word(dev->pdev, PCI_DEVICE_ID, &dev_id);
258 	if (err)
259 		return err;
260 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
261 		err = pci_read_config_word(sdev, PCI_DEVICE_ID, &sdev_id);
262 		if (err)
263 			return err;
264 		if (sdev_id != dev_id)
265 			return -EPERM;
266 	}
267 
268 	cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
269 	if (!cap)
270 		return -EOPNOTSUPP;
271 
272 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
273 		pci_save_state(sdev);
274 		pci_cfg_access_lock(sdev);
275 	}
276 	/* PCI link toggle */
277 	err = pcie_capability_set_word(bridge, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_LD);
278 	if (err)
279 		return err;
280 	msleep(500);
281 	err = pcie_capability_clear_word(bridge, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_LD);
282 	if (err)
283 		return err;
284 
285 	/* Check link */
286 	err = pci_read_config_dword(bridge, cap + PCI_EXP_LNKCAP, &reg32);
287 	if (err)
288 		return err;
289 	if (!(reg32 & PCI_EXP_LNKCAP_DLLLARC)) {
290 		mlx5_core_warn(dev, "No PCI link reporting capability (0x%08x)\n", reg32);
291 		msleep(1000);
292 		goto restore;
293 	}
294 
295 	timeout = jiffies + msecs_to_jiffies(MLX5_PCI_LINK_UP_TIMEOUT);
296 	do {
297 		err = pci_read_config_word(bridge, cap + PCI_EXP_LNKSTA, &reg16);
298 		if (err)
299 			return err;
300 		if (reg16 & PCI_EXP_LNKSTA_DLLLA)
301 			break;
302 		msleep(20);
303 	} while (!time_after(jiffies, timeout));
304 
305 	if (reg16 & PCI_EXP_LNKSTA_DLLLA) {
306 		mlx5_core_info(dev, "PCI Link up\n");
307 	} else {
308 		mlx5_core_err(dev, "PCI link not ready (0x%04x) after %d ms\n",
309 			      reg16, MLX5_PCI_LINK_UP_TIMEOUT);
310 		err = -ETIMEDOUT;
311 	}
312 
313 restore:
314 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
315 		pci_cfg_access_unlock(sdev);
316 		pci_restore_state(sdev);
317 	}
318 
319 	return err;
320 }
321 
mlx5_sync_reset_now_event(struct work_struct * work)322 static void mlx5_sync_reset_now_event(struct work_struct *work)
323 {
324 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
325 						      reset_now_work);
326 	struct mlx5_core_dev *dev = fw_reset->dev;
327 	int err;
328 
329 	if (mlx5_sync_reset_clear_reset_requested(dev, false))
330 		return;
331 
332 	mlx5_core_warn(dev, "Sync Reset now. Device is going to reset.\n");
333 
334 	err = mlx5_cmd_fast_teardown_hca(dev);
335 	if (err) {
336 		mlx5_core_warn(dev, "Fast teardown failed, no reset done, err %d\n", err);
337 		goto done;
338 	}
339 
340 	err = mlx5_pci_link_toggle(dev);
341 	if (err) {
342 		mlx5_core_warn(dev, "mlx5_pci_link_toggle failed, no reset done, err %d\n", err);
343 		goto done;
344 	}
345 
346 	mlx5_enter_error_state(dev, true);
347 	mlx5_unload_one(dev);
348 done:
349 	fw_reset->ret = err;
350 	mlx5_fw_reset_complete_reload(dev);
351 }
352 
mlx5_sync_reset_abort_event(struct work_struct * work)353 static void mlx5_sync_reset_abort_event(struct work_struct *work)
354 {
355 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
356 						      reset_abort_work);
357 	struct mlx5_core_dev *dev = fw_reset->dev;
358 
359 	if (mlx5_sync_reset_clear_reset_requested(dev, true))
360 		return;
361 	mlx5_core_warn(dev, "PCI Sync FW Update Reset Aborted.\n");
362 }
363 
mlx5_sync_reset_events_handle(struct mlx5_fw_reset * fw_reset,struct mlx5_eqe * eqe)364 static void mlx5_sync_reset_events_handle(struct mlx5_fw_reset *fw_reset, struct mlx5_eqe *eqe)
365 {
366 	struct mlx5_eqe_sync_fw_update *sync_fw_update_eqe;
367 	u8 sync_event_rst_type;
368 
369 	sync_fw_update_eqe = &eqe->data.sync_fw_update;
370 	sync_event_rst_type = sync_fw_update_eqe->sync_rst_state & SYNC_RST_STATE_MASK;
371 	switch (sync_event_rst_type) {
372 	case MLX5_SYNC_RST_STATE_RESET_REQUEST:
373 		queue_work(fw_reset->wq, &fw_reset->reset_request_work);
374 		break;
375 	case MLX5_SYNC_RST_STATE_RESET_NOW:
376 		queue_work(fw_reset->wq, &fw_reset->reset_now_work);
377 		break;
378 	case MLX5_SYNC_RST_STATE_RESET_ABORT:
379 		queue_work(fw_reset->wq, &fw_reset->reset_abort_work);
380 		break;
381 	}
382 }
383 
fw_reset_event_notifier(struct notifier_block * nb,unsigned long action,void * data)384 static int fw_reset_event_notifier(struct notifier_block *nb, unsigned long action, void *data)
385 {
386 	struct mlx5_fw_reset *fw_reset = mlx5_nb_cof(nb, struct mlx5_fw_reset, nb);
387 	struct mlx5_eqe *eqe = data;
388 
389 	switch (eqe->sub_type) {
390 	case MLX5_GENERAL_SUBTYPE_FW_LIVE_PATCH_EVENT:
391 			queue_work(fw_reset->wq, &fw_reset->fw_live_patch_work);
392 		break;
393 	case MLX5_GENERAL_SUBTYPE_PCI_SYNC_FOR_FW_UPDATE_EVENT:
394 		mlx5_sync_reset_events_handle(fw_reset, eqe);
395 		break;
396 	default:
397 		return NOTIFY_DONE;
398 	}
399 
400 	return NOTIFY_OK;
401 }
402 
403 #define MLX5_FW_RESET_TIMEOUT_MSEC 5000
mlx5_fw_reset_wait_reset_done(struct mlx5_core_dev * dev)404 int mlx5_fw_reset_wait_reset_done(struct mlx5_core_dev *dev)
405 {
406 	unsigned long timeout = msecs_to_jiffies(MLX5_FW_RESET_TIMEOUT_MSEC);
407 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
408 	int err;
409 
410 	if (!wait_for_completion_timeout(&fw_reset->done, timeout)) {
411 		mlx5_core_warn(dev, "FW sync reset timeout after %d seconds\n",
412 			       MLX5_FW_RESET_TIMEOUT_MSEC / 1000);
413 		err = -ETIMEDOUT;
414 		goto out;
415 	}
416 	err = fw_reset->ret;
417 out:
418 	clear_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags);
419 	return err;
420 }
421 
mlx5_fw_reset_events_start(struct mlx5_core_dev * dev)422 void mlx5_fw_reset_events_start(struct mlx5_core_dev *dev)
423 {
424 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
425 
426 	MLX5_NB_INIT(&fw_reset->nb, fw_reset_event_notifier, GENERAL_EVENT);
427 	mlx5_eq_notifier_register(dev, &fw_reset->nb);
428 }
429 
mlx5_fw_reset_events_stop(struct mlx5_core_dev * dev)430 void mlx5_fw_reset_events_stop(struct mlx5_core_dev *dev)
431 {
432 	mlx5_eq_notifier_unregister(dev, &dev->priv.fw_reset->nb);
433 }
434 
mlx5_fw_reset_init(struct mlx5_core_dev * dev)435 int mlx5_fw_reset_init(struct mlx5_core_dev *dev)
436 {
437 	struct mlx5_fw_reset *fw_reset = kzalloc(sizeof(*fw_reset), GFP_KERNEL);
438 
439 	if (!fw_reset)
440 		return -ENOMEM;
441 	fw_reset->wq = create_singlethread_workqueue("mlx5_fw_reset_events");
442 	if (!fw_reset->wq) {
443 		kfree(fw_reset);
444 		return -ENOMEM;
445 	}
446 
447 	fw_reset->dev = dev;
448 	dev->priv.fw_reset = fw_reset;
449 
450 	INIT_WORK(&fw_reset->fw_live_patch_work, mlx5_fw_live_patch_event);
451 	INIT_WORK(&fw_reset->reset_request_work, mlx5_sync_reset_request_event);
452 	INIT_WORK(&fw_reset->reset_reload_work, mlx5_sync_reset_reload_work);
453 	INIT_WORK(&fw_reset->reset_now_work, mlx5_sync_reset_now_event);
454 	INIT_WORK(&fw_reset->reset_abort_work, mlx5_sync_reset_abort_event);
455 
456 	init_completion(&fw_reset->done);
457 	return 0;
458 }
459 
mlx5_fw_reset_cleanup(struct mlx5_core_dev * dev)460 void mlx5_fw_reset_cleanup(struct mlx5_core_dev *dev)
461 {
462 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
463 
464 	destroy_workqueue(fw_reset->wq);
465 	kfree(dev->priv.fw_reset);
466 }
467