xref: /linux/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c (revision 13091aa30535b719e269f20a7bc34002bf5afae5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/pci.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/log2.h>
17 #include <linux/err.h>
18 
19 #include "hinic_hw_if.h"
20 #include "hinic_hw_eqs.h"
21 #include "hinic_hw_mgmt.h"
22 #include "hinic_hw_qp_ctxt.h"
23 #include "hinic_hw_qp.h"
24 #include "hinic_hw_io.h"
25 #include "hinic_hw_dev.h"
26 
27 #define IO_STATUS_TIMEOUT               100
28 #define OUTBOUND_STATE_TIMEOUT          100
29 #define DB_STATE_TIMEOUT                100
30 
31 #define MAX_IRQS(max_qps, num_aeqs, num_ceqs)   \
32 		 (2 * (max_qps) + (num_aeqs) + (num_ceqs))
33 
34 #define ADDR_IN_4BYTES(addr)            ((addr) >> 2)
35 
36 enum intr_type {
37 	INTR_MSIX_TYPE,
38 };
39 
40 enum io_status {
41 	IO_STOPPED = 0,
42 	IO_RUNNING = 1,
43 };
44 
45 enum hw_ioctxt_set_cmdq_depth {
46 	HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT,
47 };
48 
49 /* HW struct */
50 struct hinic_dev_cap {
51 	u8      status;
52 	u8      version;
53 	u8      rsvd0[6];
54 
55 	u8      rsvd1[5];
56 	u8      intr_type;
57 	u8      rsvd2[66];
58 	u16     max_sqs;
59 	u16     max_rqs;
60 	u8      rsvd3[208];
61 };
62 
63 /**
64  * get_capability - convert device capabilities to NIC capabilities
65  * @hwdev: the HW device to set and convert device capabilities for
66  * @dev_cap: device capabilities from FW
67  *
68  * Return 0 - Success, negative - Failure
69  **/
70 static int get_capability(struct hinic_hwdev *hwdev,
71 			  struct hinic_dev_cap *dev_cap)
72 {
73 	struct hinic_cap *nic_cap = &hwdev->nic_cap;
74 	int num_aeqs, num_ceqs, num_irqs;
75 
76 	if (!HINIC_IS_PF(hwdev->hwif) && !HINIC_IS_PPF(hwdev->hwif))
77 		return -EINVAL;
78 
79 	if (dev_cap->intr_type != INTR_MSIX_TYPE)
80 		return -EFAULT;
81 
82 	num_aeqs = HINIC_HWIF_NUM_AEQS(hwdev->hwif);
83 	num_ceqs = HINIC_HWIF_NUM_CEQS(hwdev->hwif);
84 	num_irqs = HINIC_HWIF_NUM_IRQS(hwdev->hwif);
85 
86 	/* Each QP has its own (SQ + RQ) interrupts */
87 	nic_cap->num_qps = (num_irqs - (num_aeqs + num_ceqs)) / 2;
88 
89 	if (nic_cap->num_qps > HINIC_Q_CTXT_MAX)
90 		nic_cap->num_qps = HINIC_Q_CTXT_MAX;
91 
92 	/* num_qps must be power of 2 */
93 	nic_cap->num_qps = BIT(fls(nic_cap->num_qps) - 1);
94 
95 	nic_cap->max_qps = dev_cap->max_sqs + 1;
96 	if (nic_cap->max_qps != (dev_cap->max_rqs + 1))
97 		return -EFAULT;
98 
99 	if (nic_cap->num_qps > nic_cap->max_qps)
100 		nic_cap->num_qps = nic_cap->max_qps;
101 
102 	return 0;
103 }
104 
105 /**
106  * get_cap_from_fw - get device capabilities from FW
107  * @pfhwdev: the PF HW device to get capabilities for
108  *
109  * Return 0 - Success, negative - Failure
110  **/
111 static int get_cap_from_fw(struct hinic_pfhwdev *pfhwdev)
112 {
113 	struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
114 	struct hinic_hwif *hwif = hwdev->hwif;
115 	struct pci_dev *pdev = hwif->pdev;
116 	struct hinic_dev_cap dev_cap;
117 	u16 in_len, out_len;
118 	int err;
119 
120 	in_len = 0;
121 	out_len = sizeof(dev_cap);
122 
123 	err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_CFGM,
124 				HINIC_CFG_NIC_CAP, &dev_cap, in_len, &dev_cap,
125 				&out_len, HINIC_MGMT_MSG_SYNC);
126 	if (err) {
127 		dev_err(&pdev->dev, "Failed to get capability from FW\n");
128 		return err;
129 	}
130 
131 	return get_capability(hwdev, &dev_cap);
132 }
133 
134 /**
135  * get_dev_cap - get device capabilities
136  * @hwdev: the NIC HW device to get capabilities for
137  *
138  * Return 0 - Success, negative - Failure
139  **/
140 static int get_dev_cap(struct hinic_hwdev *hwdev)
141 {
142 	struct hinic_hwif *hwif = hwdev->hwif;
143 	struct pci_dev *pdev = hwif->pdev;
144 	struct hinic_pfhwdev *pfhwdev;
145 	int err;
146 
147 	switch (HINIC_FUNC_TYPE(hwif)) {
148 	case HINIC_PPF:
149 	case HINIC_PF:
150 		pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
151 
152 		err = get_cap_from_fw(pfhwdev);
153 		if (err) {
154 			dev_err(&pdev->dev, "Failed to get capability from FW\n");
155 			return err;
156 		}
157 		break;
158 
159 	default:
160 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
161 		return -EINVAL;
162 	}
163 
164 	return 0;
165 }
166 
167 /**
168  * init_msix - enable the msix and save the entries
169  * @hwdev: the NIC HW device
170  *
171  * Return 0 - Success, negative - Failure
172  **/
173 static int init_msix(struct hinic_hwdev *hwdev)
174 {
175 	struct hinic_hwif *hwif = hwdev->hwif;
176 	struct pci_dev *pdev = hwif->pdev;
177 	int nr_irqs, num_aeqs, num_ceqs;
178 	size_t msix_entries_size;
179 	int i, err;
180 
181 	num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
182 	num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
183 	nr_irqs = MAX_IRQS(HINIC_MAX_QPS, num_aeqs, num_ceqs);
184 	if (nr_irqs > HINIC_HWIF_NUM_IRQS(hwif))
185 		nr_irqs = HINIC_HWIF_NUM_IRQS(hwif);
186 
187 	msix_entries_size = nr_irqs * sizeof(*hwdev->msix_entries);
188 	hwdev->msix_entries = devm_kzalloc(&pdev->dev, msix_entries_size,
189 					   GFP_KERNEL);
190 	if (!hwdev->msix_entries)
191 		return -ENOMEM;
192 
193 	for (i = 0; i < nr_irqs; i++)
194 		hwdev->msix_entries[i].entry = i;
195 
196 	err = pci_enable_msix_exact(pdev, hwdev->msix_entries, nr_irqs);
197 	if (err) {
198 		dev_err(&pdev->dev, "Failed to enable pci msix\n");
199 		return err;
200 	}
201 
202 	return 0;
203 }
204 
205 /**
206  * disable_msix - disable the msix
207  * @hwdev: the NIC HW device
208  **/
209 static void disable_msix(struct hinic_hwdev *hwdev)
210 {
211 	struct hinic_hwif *hwif = hwdev->hwif;
212 	struct pci_dev *pdev = hwif->pdev;
213 
214 	pci_disable_msix(pdev);
215 }
216 
217 /**
218  * hinic_port_msg_cmd - send port msg to mgmt
219  * @hwdev: the NIC HW device
220  * @cmd: the port command
221  * @buf_in: input buffer
222  * @in_size: input size
223  * @buf_out: output buffer
224  * @out_size: returned output size
225  *
226  * Return 0 - Success, negative - Failure
227  **/
228 int hinic_port_msg_cmd(struct hinic_hwdev *hwdev, enum hinic_port_cmd cmd,
229 		       void *buf_in, u16 in_size, void *buf_out, u16 *out_size)
230 {
231 	struct hinic_hwif *hwif = hwdev->hwif;
232 	struct pci_dev *pdev = hwif->pdev;
233 	struct hinic_pfhwdev *pfhwdev;
234 
235 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
236 		dev_err(&pdev->dev, "unsupported PCI Function type\n");
237 		return -EINVAL;
238 	}
239 
240 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
241 
242 	return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC, cmd,
243 				 buf_in, in_size, buf_out, out_size,
244 				 HINIC_MGMT_MSG_SYNC);
245 }
246 
247 /**
248  * init_fw_ctxt- Init Firmware tables before network mgmt and io operations
249  * @hwdev: the NIC HW device
250  *
251  * Return 0 - Success, negative - Failure
252  **/
253 static int init_fw_ctxt(struct hinic_hwdev *hwdev)
254 {
255 	struct hinic_hwif *hwif = hwdev->hwif;
256 	struct pci_dev *pdev = hwif->pdev;
257 	struct hinic_cmd_fw_ctxt fw_ctxt;
258 	u16 out_size;
259 	int err;
260 
261 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
262 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
263 		return -EINVAL;
264 	}
265 
266 	fw_ctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
267 	fw_ctxt.rx_buf_sz = HINIC_RX_BUF_SZ;
268 
269 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_FWCTXT_INIT,
270 				 &fw_ctxt, sizeof(fw_ctxt),
271 				 &fw_ctxt, &out_size);
272 	if (err || (out_size != sizeof(fw_ctxt)) || fw_ctxt.status) {
273 		dev_err(&pdev->dev, "Failed to init FW ctxt, ret = %d\n",
274 			fw_ctxt.status);
275 		return -EFAULT;
276 	}
277 
278 	return 0;
279 }
280 
281 /**
282  * set_hw_ioctxt - set the shape of the IO queues in FW
283  * @hwdev: the NIC HW device
284  * @rq_depth: rq depth
285  * @sq_depth: sq depth
286  *
287  * Return 0 - Success, negative - Failure
288  **/
289 static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
290 			 unsigned int sq_depth)
291 {
292 	struct hinic_hwif *hwif = hwdev->hwif;
293 	struct hinic_cmd_hw_ioctxt hw_ioctxt;
294 	struct pci_dev *pdev = hwif->pdev;
295 	struct hinic_pfhwdev *pfhwdev;
296 
297 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
298 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
299 		return -EINVAL;
300 	}
301 
302 	hw_ioctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
303 
304 	hw_ioctxt.set_cmdq_depth = HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT;
305 	hw_ioctxt.cmdq_depth = 0;
306 
307 	hw_ioctxt.lro_en = 1;
308 
309 	hw_ioctxt.rq_depth  = ilog2(rq_depth);
310 
311 	hw_ioctxt.rx_buf_sz_idx = HINIC_RX_BUF_SZ_IDX;
312 
313 	hw_ioctxt.sq_depth  = ilog2(sq_depth);
314 
315 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
316 
317 	return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
318 				 HINIC_COMM_CMD_HWCTXT_SET,
319 				 &hw_ioctxt, sizeof(hw_ioctxt), NULL,
320 				 NULL, HINIC_MGMT_MSG_SYNC);
321 }
322 
323 static int wait_for_outbound_state(struct hinic_hwdev *hwdev)
324 {
325 	enum hinic_outbound_state outbound_state;
326 	struct hinic_hwif *hwif = hwdev->hwif;
327 	struct pci_dev *pdev = hwif->pdev;
328 	unsigned long end;
329 
330 	end = jiffies + msecs_to_jiffies(OUTBOUND_STATE_TIMEOUT);
331 	do {
332 		outbound_state = hinic_outbound_state_get(hwif);
333 
334 		if (outbound_state == HINIC_OUTBOUND_ENABLE)
335 			return 0;
336 
337 		msleep(20);
338 	} while (time_before(jiffies, end));
339 
340 	dev_err(&pdev->dev, "Wait for OUTBOUND - Timeout\n");
341 	return -EFAULT;
342 }
343 
344 static int wait_for_db_state(struct hinic_hwdev *hwdev)
345 {
346 	struct hinic_hwif *hwif = hwdev->hwif;
347 	struct pci_dev *pdev = hwif->pdev;
348 	enum hinic_db_state db_state;
349 	unsigned long end;
350 
351 	end = jiffies + msecs_to_jiffies(DB_STATE_TIMEOUT);
352 	do {
353 		db_state = hinic_db_state_get(hwif);
354 
355 		if (db_state == HINIC_DB_ENABLE)
356 			return 0;
357 
358 		msleep(20);
359 	} while (time_before(jiffies, end));
360 
361 	dev_err(&pdev->dev, "Wait for DB - Timeout\n");
362 	return -EFAULT;
363 }
364 
365 static int wait_for_io_stopped(struct hinic_hwdev *hwdev)
366 {
367 	struct hinic_cmd_io_status cmd_io_status;
368 	struct hinic_hwif *hwif = hwdev->hwif;
369 	struct pci_dev *pdev = hwif->pdev;
370 	struct hinic_pfhwdev *pfhwdev;
371 	unsigned long end;
372 	u16 out_size;
373 	int err;
374 
375 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
376 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
377 		return -EINVAL;
378 	}
379 
380 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
381 
382 	cmd_io_status.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
383 
384 	end = jiffies + msecs_to_jiffies(IO_STATUS_TIMEOUT);
385 	do {
386 		err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
387 					HINIC_COMM_CMD_IO_STATUS_GET,
388 					&cmd_io_status, sizeof(cmd_io_status),
389 					&cmd_io_status, &out_size,
390 					HINIC_MGMT_MSG_SYNC);
391 		if ((err) || (out_size != sizeof(cmd_io_status))) {
392 			dev_err(&pdev->dev, "Failed to get IO status, ret = %d\n",
393 				err);
394 			return err;
395 		}
396 
397 		if (cmd_io_status.status == IO_STOPPED) {
398 			dev_info(&pdev->dev, "IO stopped\n");
399 			return 0;
400 		}
401 
402 		msleep(20);
403 	} while (time_before(jiffies, end));
404 
405 	dev_err(&pdev->dev, "Wait for IO stopped - Timeout\n");
406 	return -ETIMEDOUT;
407 }
408 
409 /**
410  * clear_io_resource - set the IO resources as not active in the NIC
411  * @hwdev: the NIC HW device
412  *
413  * Return 0 - Success, negative - Failure
414  **/
415 static int clear_io_resources(struct hinic_hwdev *hwdev)
416 {
417 	struct hinic_cmd_clear_io_res cmd_clear_io_res;
418 	struct hinic_hwif *hwif = hwdev->hwif;
419 	struct pci_dev *pdev = hwif->pdev;
420 	struct hinic_pfhwdev *pfhwdev;
421 	int err;
422 
423 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
424 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
425 		return -EINVAL;
426 	}
427 
428 	err = wait_for_io_stopped(hwdev);
429 	if (err) {
430 		dev_err(&pdev->dev, "IO has not stopped yet\n");
431 		return err;
432 	}
433 
434 	cmd_clear_io_res.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
435 
436 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
437 
438 	err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
439 				HINIC_COMM_CMD_IO_RES_CLEAR, &cmd_clear_io_res,
440 				sizeof(cmd_clear_io_res), NULL, NULL,
441 				HINIC_MGMT_MSG_SYNC);
442 	if (err) {
443 		dev_err(&pdev->dev, "Failed to clear IO resources\n");
444 		return err;
445 	}
446 
447 	return 0;
448 }
449 
450 /**
451  * set_resources_state - set the state of the resources in the NIC
452  * @hwdev: the NIC HW device
453  * @state: the state to set
454  *
455  * Return 0 - Success, negative - Failure
456  **/
457 static int set_resources_state(struct hinic_hwdev *hwdev,
458 			       enum hinic_res_state state)
459 {
460 	struct hinic_cmd_set_res_state res_state;
461 	struct hinic_hwif *hwif = hwdev->hwif;
462 	struct pci_dev *pdev = hwif->pdev;
463 	struct hinic_pfhwdev *pfhwdev;
464 
465 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
466 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
467 		return -EINVAL;
468 	}
469 
470 	res_state.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
471 	res_state.state = state;
472 
473 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
474 
475 	return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
476 				 HINIC_MOD_COMM,
477 				 HINIC_COMM_CMD_RES_STATE_SET,
478 				 &res_state, sizeof(res_state), NULL,
479 				 NULL, HINIC_MGMT_MSG_SYNC);
480 }
481 
482 /**
483  * get_base_qpn - get the first qp number
484  * @hwdev: the NIC HW device
485  * @base_qpn: returned qp number
486  *
487  * Return 0 - Success, negative - Failure
488  **/
489 static int get_base_qpn(struct hinic_hwdev *hwdev, u16 *base_qpn)
490 {
491 	struct hinic_cmd_base_qpn cmd_base_qpn;
492 	struct hinic_hwif *hwif = hwdev->hwif;
493 	struct pci_dev *pdev = hwif->pdev;
494 	u16 out_size;
495 	int err;
496 
497 	cmd_base_qpn.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
498 
499 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_GET_GLOBAL_QPN,
500 				 &cmd_base_qpn, sizeof(cmd_base_qpn),
501 				 &cmd_base_qpn, &out_size);
502 	if (err || (out_size != sizeof(cmd_base_qpn)) || cmd_base_qpn.status) {
503 		dev_err(&pdev->dev, "Failed to get base qpn, status = %d\n",
504 			cmd_base_qpn.status);
505 		return -EFAULT;
506 	}
507 
508 	*base_qpn = cmd_base_qpn.qpn;
509 	return 0;
510 }
511 
512 /**
513  * hinic_hwdev_ifup - Preparing the HW for passing IO
514  * @hwdev: the NIC HW device
515  *
516  * Return 0 - Success, negative - Failure
517  **/
518 int hinic_hwdev_ifup(struct hinic_hwdev *hwdev)
519 {
520 	struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
521 	struct hinic_cap *nic_cap = &hwdev->nic_cap;
522 	struct hinic_hwif *hwif = hwdev->hwif;
523 	int err, num_aeqs, num_ceqs, num_qps;
524 	struct msix_entry *ceq_msix_entries;
525 	struct msix_entry *sq_msix_entries;
526 	struct msix_entry *rq_msix_entries;
527 	struct pci_dev *pdev = hwif->pdev;
528 	u16 base_qpn;
529 
530 	err = get_base_qpn(hwdev, &base_qpn);
531 	if (err) {
532 		dev_err(&pdev->dev, "Failed to get global base qp number\n");
533 		return err;
534 	}
535 
536 	num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
537 	num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
538 
539 	ceq_msix_entries = &hwdev->msix_entries[num_aeqs];
540 
541 	err = hinic_io_init(func_to_io, hwif, nic_cap->max_qps, num_ceqs,
542 			    ceq_msix_entries);
543 	if (err) {
544 		dev_err(&pdev->dev, "Failed to init IO channel\n");
545 		return err;
546 	}
547 
548 	num_qps = nic_cap->num_qps;
549 	sq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs];
550 	rq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs + num_qps];
551 
552 	err = hinic_io_create_qps(func_to_io, base_qpn, num_qps,
553 				  sq_msix_entries, rq_msix_entries);
554 	if (err) {
555 		dev_err(&pdev->dev, "Failed to create QPs\n");
556 		goto err_create_qps;
557 	}
558 
559 	err = wait_for_db_state(hwdev);
560 	if (err) {
561 		dev_warn(&pdev->dev, "db - disabled, try again\n");
562 		hinic_db_state_set(hwif, HINIC_DB_ENABLE);
563 	}
564 
565 	err = set_hw_ioctxt(hwdev, HINIC_SQ_DEPTH, HINIC_RQ_DEPTH);
566 	if (err) {
567 		dev_err(&pdev->dev, "Failed to set HW IO ctxt\n");
568 		goto err_hw_ioctxt;
569 	}
570 
571 	return 0;
572 
573 err_hw_ioctxt:
574 	hinic_io_destroy_qps(func_to_io, num_qps);
575 
576 err_create_qps:
577 	hinic_io_free(func_to_io);
578 	return err;
579 }
580 
581 /**
582  * hinic_hwdev_ifdown - Closing the HW for passing IO
583  * @hwdev: the NIC HW device
584  *
585  **/
586 void hinic_hwdev_ifdown(struct hinic_hwdev *hwdev)
587 {
588 	struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
589 	struct hinic_cap *nic_cap = &hwdev->nic_cap;
590 
591 	clear_io_resources(hwdev);
592 
593 	hinic_io_destroy_qps(func_to_io, nic_cap->num_qps);
594 	hinic_io_free(func_to_io);
595 }
596 
597 /**
598  * hinic_hwdev_cb_register - register callback handler for MGMT events
599  * @hwdev: the NIC HW device
600  * @cmd: the mgmt event
601  * @handle: private data for the handler
602  * @handler: event handler
603  **/
604 void hinic_hwdev_cb_register(struct hinic_hwdev *hwdev,
605 			     enum hinic_mgmt_msg_cmd cmd, void *handle,
606 			     void (*handler)(void *handle, void *buf_in,
607 					     u16 in_size, void *buf_out,
608 					     u16 *out_size))
609 {
610 	struct hinic_hwif *hwif = hwdev->hwif;
611 	struct pci_dev *pdev = hwif->pdev;
612 	struct hinic_pfhwdev *pfhwdev;
613 	struct hinic_nic_cb *nic_cb;
614 	u8 cmd_cb;
615 
616 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
617 		dev_err(&pdev->dev, "unsupported PCI Function type\n");
618 		return;
619 	}
620 
621 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
622 
623 	cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
624 	nic_cb = &pfhwdev->nic_cb[cmd_cb];
625 
626 	nic_cb->handler = handler;
627 	nic_cb->handle = handle;
628 	nic_cb->cb_state = HINIC_CB_ENABLED;
629 }
630 
631 /**
632  * hinic_hwdev_cb_unregister - unregister callback handler for MGMT events
633  * @hwdev: the NIC HW device
634  * @cmd: the mgmt event
635  **/
636 void hinic_hwdev_cb_unregister(struct hinic_hwdev *hwdev,
637 			       enum hinic_mgmt_msg_cmd cmd)
638 {
639 	struct hinic_hwif *hwif = hwdev->hwif;
640 	struct pci_dev *pdev = hwif->pdev;
641 	struct hinic_pfhwdev *pfhwdev;
642 	struct hinic_nic_cb *nic_cb;
643 	u8 cmd_cb;
644 
645 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
646 		dev_err(&pdev->dev, "unsupported PCI Function type\n");
647 		return;
648 	}
649 
650 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
651 
652 	cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
653 	nic_cb = &pfhwdev->nic_cb[cmd_cb];
654 
655 	nic_cb->cb_state &= ~HINIC_CB_ENABLED;
656 
657 	while (nic_cb->cb_state & HINIC_CB_RUNNING)
658 		schedule();
659 
660 	nic_cb->handler = NULL;
661 }
662 
663 /**
664  * nic_mgmt_msg_handler - nic mgmt event handler
665  * @handle: private data for the handler
666  * @buf_in: input buffer
667  * @in_size: input size
668  * @buf_out: output buffer
669  * @out_size: returned output size
670  **/
671 static void nic_mgmt_msg_handler(void *handle, u8 cmd, void *buf_in,
672 				 u16 in_size, void *buf_out, u16 *out_size)
673 {
674 	struct hinic_pfhwdev *pfhwdev = handle;
675 	enum hinic_cb_state cb_state;
676 	struct hinic_nic_cb *nic_cb;
677 	struct hinic_hwdev *hwdev;
678 	struct hinic_hwif *hwif;
679 	struct pci_dev *pdev;
680 	u8 cmd_cb;
681 
682 	hwdev = &pfhwdev->hwdev;
683 	hwif = hwdev->hwif;
684 	pdev = hwif->pdev;
685 
686 	if ((cmd < HINIC_MGMT_MSG_CMD_BASE) ||
687 	    (cmd >= HINIC_MGMT_MSG_CMD_MAX)) {
688 		dev_err(&pdev->dev, "unknown L2NIC event, cmd = %d\n", cmd);
689 		return;
690 	}
691 
692 	cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
693 
694 	nic_cb = &pfhwdev->nic_cb[cmd_cb];
695 
696 	cb_state = cmpxchg(&nic_cb->cb_state,
697 			   HINIC_CB_ENABLED,
698 			   HINIC_CB_ENABLED | HINIC_CB_RUNNING);
699 
700 	if ((cb_state == HINIC_CB_ENABLED) && (nic_cb->handler))
701 		nic_cb->handler(nic_cb->handle, buf_in,
702 				in_size, buf_out, out_size);
703 	else
704 		dev_err(&pdev->dev, "Unhandled NIC Event %d\n", cmd);
705 
706 	nic_cb->cb_state &= ~HINIC_CB_RUNNING;
707 }
708 
709 /**
710  * init_pfhwdev - Initialize the extended components of PF
711  * @pfhwdev: the HW device for PF
712  *
713  * Return 0 - success, negative - failure
714  **/
715 static int init_pfhwdev(struct hinic_pfhwdev *pfhwdev)
716 {
717 	struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
718 	struct hinic_hwif *hwif = hwdev->hwif;
719 	struct pci_dev *pdev = hwif->pdev;
720 	int err;
721 
722 	err = hinic_pf_to_mgmt_init(&pfhwdev->pf_to_mgmt, hwif);
723 	if (err) {
724 		dev_err(&pdev->dev, "Failed to initialize PF to MGMT channel\n");
725 		return err;
726 	}
727 
728 	hinic_register_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
729 				   pfhwdev, nic_mgmt_msg_handler);
730 
731 	hinic_set_pf_action(hwif, HINIC_PF_MGMT_ACTIVE);
732 	return 0;
733 }
734 
735 /**
736  * free_pfhwdev - Free the extended components of PF
737  * @pfhwdev: the HW device for PF
738  **/
739 static void free_pfhwdev(struct hinic_pfhwdev *pfhwdev)
740 {
741 	struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
742 
743 	hinic_set_pf_action(hwdev->hwif, HINIC_PF_MGMT_INIT);
744 
745 	hinic_unregister_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC);
746 
747 	hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt);
748 }
749 
750 /**
751  * hinic_init_hwdev - Initialize the NIC HW
752  * @pdev: the NIC pci device
753  *
754  * Return initialized NIC HW device
755  *
756  * Initialize the NIC HW device and return a pointer to it
757  **/
758 struct hinic_hwdev *hinic_init_hwdev(struct pci_dev *pdev)
759 {
760 	struct hinic_pfhwdev *pfhwdev;
761 	struct hinic_hwdev *hwdev;
762 	struct hinic_hwif *hwif;
763 	int err, num_aeqs;
764 
765 	hwif = devm_kzalloc(&pdev->dev, sizeof(*hwif), GFP_KERNEL);
766 	if (!hwif)
767 		return ERR_PTR(-ENOMEM);
768 
769 	err = hinic_init_hwif(hwif, pdev);
770 	if (err) {
771 		dev_err(&pdev->dev, "Failed to init HW interface\n");
772 		return ERR_PTR(err);
773 	}
774 
775 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
776 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
777 		err = -EFAULT;
778 		goto err_func_type;
779 	}
780 
781 	pfhwdev = devm_kzalloc(&pdev->dev, sizeof(*pfhwdev), GFP_KERNEL);
782 	if (!pfhwdev) {
783 		err = -ENOMEM;
784 		goto err_pfhwdev_alloc;
785 	}
786 
787 	hwdev = &pfhwdev->hwdev;
788 	hwdev->hwif = hwif;
789 
790 	err = init_msix(hwdev);
791 	if (err) {
792 		dev_err(&pdev->dev, "Failed to init msix\n");
793 		goto err_init_msix;
794 	}
795 
796 	err = wait_for_outbound_state(hwdev);
797 	if (err) {
798 		dev_warn(&pdev->dev, "outbound - disabled, try again\n");
799 		hinic_outbound_state_set(hwif, HINIC_OUTBOUND_ENABLE);
800 	}
801 
802 	num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
803 
804 	err = hinic_aeqs_init(&hwdev->aeqs, hwif, num_aeqs,
805 			      HINIC_DEFAULT_AEQ_LEN, HINIC_EQ_PAGE_SIZE,
806 			      hwdev->msix_entries);
807 	if (err) {
808 		dev_err(&pdev->dev, "Failed to init async event queues\n");
809 		goto err_aeqs_init;
810 	}
811 
812 	err = init_pfhwdev(pfhwdev);
813 	if (err) {
814 		dev_err(&pdev->dev, "Failed to init PF HW device\n");
815 		goto err_init_pfhwdev;
816 	}
817 
818 	err = get_dev_cap(hwdev);
819 	if (err) {
820 		dev_err(&pdev->dev, "Failed to get device capabilities\n");
821 		goto err_dev_cap;
822 	}
823 
824 	err = init_fw_ctxt(hwdev);
825 	if (err) {
826 		dev_err(&pdev->dev, "Failed to init function table\n");
827 		goto err_init_fw_ctxt;
828 	}
829 
830 	err = set_resources_state(hwdev, HINIC_RES_ACTIVE);
831 	if (err) {
832 		dev_err(&pdev->dev, "Failed to set resources state\n");
833 		goto err_resources_state;
834 	}
835 
836 	return hwdev;
837 
838 err_resources_state:
839 err_init_fw_ctxt:
840 err_dev_cap:
841 	free_pfhwdev(pfhwdev);
842 
843 err_init_pfhwdev:
844 	hinic_aeqs_free(&hwdev->aeqs);
845 
846 err_aeqs_init:
847 	disable_msix(hwdev);
848 
849 err_init_msix:
850 err_pfhwdev_alloc:
851 err_func_type:
852 	hinic_free_hwif(hwif);
853 	return ERR_PTR(err);
854 }
855 
856 /**
857  * hinic_free_hwdev - Free the NIC HW device
858  * @hwdev: the NIC HW device
859  **/
860 void hinic_free_hwdev(struct hinic_hwdev *hwdev)
861 {
862 	struct hinic_pfhwdev *pfhwdev = container_of(hwdev,
863 						     struct hinic_pfhwdev,
864 						     hwdev);
865 
866 	set_resources_state(hwdev, HINIC_RES_CLEAN);
867 
868 	free_pfhwdev(pfhwdev);
869 
870 	hinic_aeqs_free(&hwdev->aeqs);
871 
872 	disable_msix(hwdev);
873 
874 	hinic_free_hwif(hwdev->hwif);
875 }
876 
877 /**
878  * hinic_hwdev_num_qps - return the number QPs available for use
879  * @hwdev: the NIC HW device
880  *
881  * Return number QPs available for use
882  **/
883 int hinic_hwdev_num_qps(struct hinic_hwdev *hwdev)
884 {
885 	struct hinic_cap *nic_cap = &hwdev->nic_cap;
886 
887 	return nic_cap->num_qps;
888 }
889 
890 /**
891  * hinic_hwdev_get_sq - get SQ
892  * @hwdev: the NIC HW device
893  * @i: the position of the SQ
894  *
895  * Return: the SQ in the i position
896  **/
897 struct hinic_sq *hinic_hwdev_get_sq(struct hinic_hwdev *hwdev, int i)
898 {
899 	struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
900 	struct hinic_qp *qp = &func_to_io->qps[i];
901 
902 	if (i >= hinic_hwdev_num_qps(hwdev))
903 		return NULL;
904 
905 	return &qp->sq;
906 }
907 
908 /**
909  * hinic_hwdev_get_sq - get RQ
910  * @hwdev: the NIC HW device
911  * @i: the position of the RQ
912  *
913  * Return: the RQ in the i position
914  **/
915 struct hinic_rq *hinic_hwdev_get_rq(struct hinic_hwdev *hwdev, int i)
916 {
917 	struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
918 	struct hinic_qp *qp = &func_to_io->qps[i];
919 
920 	if (i >= hinic_hwdev_num_qps(hwdev))
921 		return NULL;
922 
923 	return &qp->rq;
924 }
925 
926 /**
927  * hinic_hwdev_msix_cnt_set - clear message attribute counters for msix entry
928  * @hwdev: the NIC HW device
929  * @msix_index: msix_index
930  *
931  * Return 0 - Success, negative - Failure
932  **/
933 int hinic_hwdev_msix_cnt_set(struct hinic_hwdev *hwdev, u16 msix_index)
934 {
935 	return hinic_msix_attr_cnt_clear(hwdev->hwif, msix_index);
936 }
937 
938 /**
939  * hinic_hwdev_msix_set - set message attribute for msix entry
940  * @hwdev: the NIC HW device
941  * @msix_index: msix_index
942  * @pending_limit: the maximum pending interrupt events (unit 8)
943  * @coalesc_timer: coalesc period for interrupt (unit 8 us)
944  * @lli_timer: replenishing period for low latency credit (unit 8 us)
945  * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
946  * @resend_timer: maximum wait for resending msix (unit coalesc period)
947  *
948  * Return 0 - Success, negative - Failure
949  **/
950 int hinic_hwdev_msix_set(struct hinic_hwdev *hwdev, u16 msix_index,
951 			 u8 pending_limit, u8 coalesc_timer,
952 			 u8 lli_timer_cfg, u8 lli_credit_limit,
953 			 u8 resend_timer)
954 {
955 	return hinic_msix_attr_set(hwdev->hwif, msix_index,
956 				   pending_limit, coalesc_timer,
957 				   lli_timer_cfg, lli_credit_limit,
958 				   resend_timer);
959 }
960 
961 /**
962  * hinic_hwdev_hw_ci_addr_set - set cons idx addr and attributes in HW for sq
963  * @hwdev: the NIC HW device
964  * @sq: send queue
965  * @pending_limit: the maximum pending update ci events (unit 8)
966  * @coalesc_timer: coalesc period for update ci (unit 8 us)
967  *
968  * Return 0 - Success, negative - Failure
969  **/
970 int hinic_hwdev_hw_ci_addr_set(struct hinic_hwdev *hwdev, struct hinic_sq *sq,
971 			       u8 pending_limit, u8 coalesc_timer)
972 {
973 	struct hinic_qp *qp = container_of(sq, struct hinic_qp, sq);
974 	struct hinic_hwif *hwif = hwdev->hwif;
975 	struct pci_dev *pdev = hwif->pdev;
976 	struct hinic_pfhwdev *pfhwdev;
977 	struct hinic_cmd_hw_ci hw_ci;
978 
979 	if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
980 		dev_err(&pdev->dev, "Unsupported PCI Function type\n");
981 		return -EINVAL;
982 	}
983 
984 	hw_ci.dma_attr_off  = 0;
985 	hw_ci.pending_limit = pending_limit;
986 	hw_ci.coalesc_timer = coalesc_timer;
987 
988 	hw_ci.msix_en = 1;
989 	hw_ci.msix_entry_idx = sq->msix_entry;
990 
991 	hw_ci.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
992 
993 	hw_ci.sq_id = qp->q_id;
994 
995 	hw_ci.ci_addr = ADDR_IN_4BYTES(sq->hw_ci_dma_addr);
996 
997 	pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
998 	return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
999 				 HINIC_MOD_COMM,
1000 				 HINIC_COMM_CMD_SQ_HI_CI_SET,
1001 				 &hw_ci, sizeof(hw_ci), NULL,
1002 				 NULL, HINIC_MGMT_MSG_SYNC);
1003 }
1004 
1005 /**
1006  * hinic_hwdev_set_msix_state- set msix state
1007  * @hwdev: the NIC HW device
1008  * @msix_index: IRQ corresponding index number
1009  * @flag: msix state
1010  *
1011  **/
1012 void hinic_hwdev_set_msix_state(struct hinic_hwdev *hwdev, u16 msix_index,
1013 				enum hinic_msix_state flag)
1014 {
1015 	hinic_set_msix_state(hwdev->hwif, msix_index, flag);
1016 }
1017