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, false);
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_sync_reset_reload_work(struct work_struct * work)114 static void mlx5_sync_reset_reload_work(struct work_struct *work)
115 {
116 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
117 						      reset_reload_work);
118 	struct mlx5_core_dev *dev = fw_reset->dev;
119 	int err;
120 
121 	mlx5_enter_error_state(dev, true);
122 	mlx5_unload_one(dev, false);
123 	err = mlx5_health_wait_pci_up(dev);
124 	if (err)
125 		mlx5_core_err(dev, "reset reload flow aborted, PCI reads still not working\n");
126 	fw_reset->ret = err;
127 	mlx5_fw_reset_complete_reload(dev);
128 }
129 
mlx5_stop_sync_reset_poll(struct mlx5_core_dev * dev)130 static void mlx5_stop_sync_reset_poll(struct mlx5_core_dev *dev)
131 {
132 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
133 
134 	del_timer(&fw_reset->timer);
135 }
136 
mlx5_sync_reset_clear_reset_requested(struct mlx5_core_dev * dev,bool poll_health)137 static void mlx5_sync_reset_clear_reset_requested(struct mlx5_core_dev *dev, bool poll_health)
138 {
139 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
140 
141 	mlx5_stop_sync_reset_poll(dev);
142 	clear_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags);
143 	if (poll_health)
144 		mlx5_start_health_poll(dev);
145 }
146 
147 #define MLX5_RESET_POLL_INTERVAL	(HZ / 10)
poll_sync_reset(struct timer_list * t)148 static void poll_sync_reset(struct timer_list *t)
149 {
150 	struct mlx5_fw_reset *fw_reset = from_timer(fw_reset, t, timer);
151 	struct mlx5_core_dev *dev = fw_reset->dev;
152 	u32 fatal_error;
153 
154 	if (!test_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags))
155 		return;
156 
157 	fatal_error = mlx5_health_check_fatal_sensors(dev);
158 
159 	if (fatal_error) {
160 		mlx5_core_warn(dev, "Got Device Reset\n");
161 		mlx5_sync_reset_clear_reset_requested(dev, false);
162 		queue_work(fw_reset->wq, &fw_reset->reset_reload_work);
163 		return;
164 	}
165 
166 	mod_timer(&fw_reset->timer, round_jiffies(jiffies + MLX5_RESET_POLL_INTERVAL));
167 }
168 
mlx5_start_sync_reset_poll(struct mlx5_core_dev * dev)169 static void mlx5_start_sync_reset_poll(struct mlx5_core_dev *dev)
170 {
171 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
172 
173 	timer_setup(&fw_reset->timer, poll_sync_reset, 0);
174 	fw_reset->timer.expires = round_jiffies(jiffies + MLX5_RESET_POLL_INTERVAL);
175 	add_timer(&fw_reset->timer);
176 }
177 
mlx5_fw_reset_set_reset_sync_ack(struct mlx5_core_dev * dev)178 static int mlx5_fw_reset_set_reset_sync_ack(struct mlx5_core_dev *dev)
179 {
180 	return mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL3, 0, 1, false);
181 }
182 
mlx5_fw_reset_set_reset_sync_nack(struct mlx5_core_dev * dev)183 static int mlx5_fw_reset_set_reset_sync_nack(struct mlx5_core_dev *dev)
184 {
185 	return mlx5_reg_mfrl_set(dev, MLX5_MFRL_REG_RESET_LEVEL3, 0, 2, false);
186 }
187 
mlx5_sync_reset_set_reset_requested(struct mlx5_core_dev * dev)188 static void mlx5_sync_reset_set_reset_requested(struct mlx5_core_dev *dev)
189 {
190 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
191 
192 	mlx5_stop_health_poll(dev, true);
193 	set_bit(MLX5_FW_RESET_FLAGS_RESET_REQUESTED, &fw_reset->reset_flags);
194 	mlx5_start_sync_reset_poll(dev);
195 }
196 
mlx5_fw_live_patch_event(struct work_struct * work)197 static void mlx5_fw_live_patch_event(struct work_struct *work)
198 {
199 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
200 						      fw_live_patch_work);
201 	struct mlx5_core_dev *dev = fw_reset->dev;
202 	struct mlx5_fw_tracer *tracer;
203 
204 	mlx5_core_info(dev, "Live patch updated firmware version: %d.%d.%d\n", fw_rev_maj(dev),
205 		       fw_rev_min(dev), fw_rev_sub(dev));
206 
207 	tracer = dev->tracer;
208 	if (IS_ERR_OR_NULL(tracer))
209 		return;
210 
211 	if (mlx5_fw_tracer_reload(tracer))
212 		mlx5_core_err(dev, "Failed to reload FW tracer\n");
213 }
214 
mlx5_sync_reset_request_event(struct work_struct * work)215 static void mlx5_sync_reset_request_event(struct work_struct *work)
216 {
217 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
218 						      reset_request_work);
219 	struct mlx5_core_dev *dev = fw_reset->dev;
220 	int err;
221 
222 	if (test_bit(MLX5_FW_RESET_FLAGS_NACK_RESET_REQUEST, &fw_reset->reset_flags)) {
223 		err = mlx5_fw_reset_set_reset_sync_nack(dev);
224 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Nack %s",
225 			       err ? "Failed" : "Sent");
226 		return;
227 	}
228 	mlx5_sync_reset_set_reset_requested(dev);
229 	err = mlx5_fw_reset_set_reset_sync_ack(dev);
230 	if (err)
231 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Ack Failed. Error code: %d\n", err);
232 	else
233 		mlx5_core_warn(dev, "PCI Sync FW Update Reset Ack. Device reset is expected.\n");
234 }
235 
236 #define MLX5_PCI_LINK_UP_TIMEOUT 2000
237 
mlx5_pci_link_toggle(struct mlx5_core_dev * dev)238 static int mlx5_pci_link_toggle(struct mlx5_core_dev *dev)
239 {
240 	struct pci_bus *bridge_bus = dev->pdev->bus;
241 	struct pci_dev *bridge = bridge_bus->self;
242 	u16 reg16, dev_id, sdev_id;
243 	unsigned long timeout;
244 	struct pci_dev *sdev;
245 	int cap, err;
246 	u32 reg32;
247 
248 	/* Check that all functions under the pci bridge are PFs of
249 	 * this device otherwise fail this function.
250 	 */
251 	err = pci_read_config_word(dev->pdev, PCI_DEVICE_ID, &dev_id);
252 	if (err)
253 		return err;
254 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
255 		err = pci_read_config_word(sdev, PCI_DEVICE_ID, &sdev_id);
256 		if (err)
257 			return err;
258 		if (sdev_id != dev_id)
259 			return -EPERM;
260 	}
261 
262 	cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
263 	if (!cap)
264 		return -EOPNOTSUPP;
265 
266 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
267 		pci_save_state(sdev);
268 		pci_cfg_access_lock(sdev);
269 	}
270 	/* PCI link toggle */
271 	err = pci_read_config_word(bridge, cap + PCI_EXP_LNKCTL, &reg16);
272 	if (err)
273 		return err;
274 	reg16 |= PCI_EXP_LNKCTL_LD;
275 	err = pci_write_config_word(bridge, cap + PCI_EXP_LNKCTL, reg16);
276 	if (err)
277 		return err;
278 	msleep(500);
279 	reg16 &= ~PCI_EXP_LNKCTL_LD;
280 	err = pci_write_config_word(bridge, cap + PCI_EXP_LNKCTL, reg16);
281 	if (err)
282 		return err;
283 
284 	/* Check link */
285 	err = pci_read_config_dword(bridge, cap + PCI_EXP_LNKCAP, &reg32);
286 	if (err)
287 		return err;
288 	if (!(reg32 & PCI_EXP_LNKCAP_DLLLARC)) {
289 		mlx5_core_warn(dev, "No PCI link reporting capability (0x%08x)\n", reg32);
290 		msleep(1000);
291 		goto restore;
292 	}
293 
294 	timeout = jiffies + msecs_to_jiffies(MLX5_PCI_LINK_UP_TIMEOUT);
295 	do {
296 		err = pci_read_config_word(bridge, cap + PCI_EXP_LNKSTA, &reg16);
297 		if (err)
298 			return err;
299 		if (reg16 & PCI_EXP_LNKSTA_DLLLA)
300 			break;
301 		msleep(20);
302 	} while (!time_after(jiffies, timeout));
303 
304 	if (reg16 & PCI_EXP_LNKSTA_DLLLA) {
305 		mlx5_core_info(dev, "PCI Link up\n");
306 	} else {
307 		mlx5_core_err(dev, "PCI link not ready (0x%04x) after %d ms\n",
308 			      reg16, MLX5_PCI_LINK_UP_TIMEOUT);
309 		err = -ETIMEDOUT;
310 	}
311 
312 restore:
313 	list_for_each_entry(sdev, &bridge_bus->devices, bus_list) {
314 		pci_cfg_access_unlock(sdev);
315 		pci_restore_state(sdev);
316 	}
317 
318 	return err;
319 }
320 
mlx5_sync_reset_now_event(struct work_struct * work)321 static void mlx5_sync_reset_now_event(struct work_struct *work)
322 {
323 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
324 						      reset_now_work);
325 	struct mlx5_core_dev *dev = fw_reset->dev;
326 	int err;
327 
328 	mlx5_sync_reset_clear_reset_requested(dev, false);
329 
330 	mlx5_core_warn(dev, "Sync Reset now. Device is going to reset.\n");
331 
332 	err = mlx5_cmd_fast_teardown_hca(dev);
333 	if (err) {
334 		mlx5_core_warn(dev, "Fast teardown failed, no reset done, err %d\n", err);
335 		goto done;
336 	}
337 
338 	err = mlx5_pci_link_toggle(dev);
339 	if (err) {
340 		mlx5_core_warn(dev, "mlx5_pci_link_toggle failed, no reset done, err %d\n", err);
341 		goto done;
342 	}
343 
344 	mlx5_enter_error_state(dev, true);
345 	mlx5_unload_one(dev, false);
346 done:
347 	fw_reset->ret = err;
348 	mlx5_fw_reset_complete_reload(dev);
349 }
350 
mlx5_sync_reset_abort_event(struct work_struct * work)351 static void mlx5_sync_reset_abort_event(struct work_struct *work)
352 {
353 	struct mlx5_fw_reset *fw_reset = container_of(work, struct mlx5_fw_reset,
354 						      reset_abort_work);
355 	struct mlx5_core_dev *dev = fw_reset->dev;
356 
357 	mlx5_sync_reset_clear_reset_requested(dev, true);
358 	mlx5_core_warn(dev, "PCI Sync FW Update Reset Aborted.\n");
359 }
360 
mlx5_sync_reset_events_handle(struct mlx5_fw_reset * fw_reset,struct mlx5_eqe * eqe)361 static void mlx5_sync_reset_events_handle(struct mlx5_fw_reset *fw_reset, struct mlx5_eqe *eqe)
362 {
363 	struct mlx5_eqe_sync_fw_update *sync_fw_update_eqe;
364 	u8 sync_event_rst_type;
365 
366 	sync_fw_update_eqe = &eqe->data.sync_fw_update;
367 	sync_event_rst_type = sync_fw_update_eqe->sync_rst_state & SYNC_RST_STATE_MASK;
368 	switch (sync_event_rst_type) {
369 	case MLX5_SYNC_RST_STATE_RESET_REQUEST:
370 		queue_work(fw_reset->wq, &fw_reset->reset_request_work);
371 		break;
372 	case MLX5_SYNC_RST_STATE_RESET_NOW:
373 		queue_work(fw_reset->wq, &fw_reset->reset_now_work);
374 		break;
375 	case MLX5_SYNC_RST_STATE_RESET_ABORT:
376 		queue_work(fw_reset->wq, &fw_reset->reset_abort_work);
377 		break;
378 	}
379 }
380 
fw_reset_event_notifier(struct notifier_block * nb,unsigned long action,void * data)381 static int fw_reset_event_notifier(struct notifier_block *nb, unsigned long action, void *data)
382 {
383 	struct mlx5_fw_reset *fw_reset = mlx5_nb_cof(nb, struct mlx5_fw_reset, nb);
384 	struct mlx5_eqe *eqe = data;
385 
386 	switch (eqe->sub_type) {
387 	case MLX5_GENERAL_SUBTYPE_FW_LIVE_PATCH_EVENT:
388 			queue_work(fw_reset->wq, &fw_reset->fw_live_patch_work);
389 		break;
390 	case MLX5_GENERAL_SUBTYPE_PCI_SYNC_FOR_FW_UPDATE_EVENT:
391 		mlx5_sync_reset_events_handle(fw_reset, eqe);
392 		break;
393 	default:
394 		return NOTIFY_DONE;
395 	}
396 
397 	return NOTIFY_OK;
398 }
399 
400 #define MLX5_FW_RESET_TIMEOUT_MSEC 5000
mlx5_fw_reset_wait_reset_done(struct mlx5_core_dev * dev)401 int mlx5_fw_reset_wait_reset_done(struct mlx5_core_dev *dev)
402 {
403 	unsigned long timeout = msecs_to_jiffies(MLX5_FW_RESET_TIMEOUT_MSEC);
404 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
405 	int err;
406 
407 	if (!wait_for_completion_timeout(&fw_reset->done, timeout)) {
408 		mlx5_core_warn(dev, "FW sync reset timeout after %d seconds\n",
409 			       MLX5_FW_RESET_TIMEOUT_MSEC / 1000);
410 		err = -ETIMEDOUT;
411 		goto out;
412 	}
413 	err = fw_reset->ret;
414 out:
415 	clear_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags);
416 	return err;
417 }
418 
mlx5_fw_reset_events_start(struct mlx5_core_dev * dev)419 void mlx5_fw_reset_events_start(struct mlx5_core_dev *dev)
420 {
421 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
422 
423 	MLX5_NB_INIT(&fw_reset->nb, fw_reset_event_notifier, GENERAL_EVENT);
424 	mlx5_eq_notifier_register(dev, &fw_reset->nb);
425 }
426 
mlx5_fw_reset_events_stop(struct mlx5_core_dev * dev)427 void mlx5_fw_reset_events_stop(struct mlx5_core_dev *dev)
428 {
429 	mlx5_eq_notifier_unregister(dev, &dev->priv.fw_reset->nb);
430 }
431 
mlx5_fw_reset_init(struct mlx5_core_dev * dev)432 int mlx5_fw_reset_init(struct mlx5_core_dev *dev)
433 {
434 	struct mlx5_fw_reset *fw_reset = kzalloc(sizeof(*fw_reset), GFP_KERNEL);
435 
436 	if (!fw_reset)
437 		return -ENOMEM;
438 	fw_reset->wq = create_singlethread_workqueue("mlx5_fw_reset_events");
439 	if (!fw_reset->wq) {
440 		kfree(fw_reset);
441 		return -ENOMEM;
442 	}
443 
444 	fw_reset->dev = dev;
445 	dev->priv.fw_reset = fw_reset;
446 
447 	INIT_WORK(&fw_reset->fw_live_patch_work, mlx5_fw_live_patch_event);
448 	INIT_WORK(&fw_reset->reset_request_work, mlx5_sync_reset_request_event);
449 	INIT_WORK(&fw_reset->reset_reload_work, mlx5_sync_reset_reload_work);
450 	INIT_WORK(&fw_reset->reset_now_work, mlx5_sync_reset_now_event);
451 	INIT_WORK(&fw_reset->reset_abort_work, mlx5_sync_reset_abort_event);
452 
453 	init_completion(&fw_reset->done);
454 	return 0;
455 }
456 
mlx5_fw_reset_cleanup(struct mlx5_core_dev * dev)457 void mlx5_fw_reset_cleanup(struct mlx5_core_dev *dev)
458 {
459 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
460 
461 	destroy_workqueue(fw_reset->wq);
462 	kfree(dev->priv.fw_reset);
463 }
464