1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/semaphore.h>
13 #include <linux/completion.h>
14 #include <linux/slab.h>
15 #include <asm/barrier.h>
17 #include "hinic_hw_if.h"
18 #include "hinic_hw_eqs.h"
19 #include "hinic_hw_api_cmd.h"
20 #include "hinic_hw_mgmt.h"
21 #include "hinic_hw_dev.h"
23 #define SYNC_MSG_ID_MASK 0x1FF
25 #define SYNC_MSG_ID(pf_to_mgmt) ((pf_to_mgmt)->sync_msg_id)
27 #define SYNC_MSG_ID_INC(pf_to_mgmt) (SYNC_MSG_ID(pf_to_mgmt) = \
28 ((SYNC_MSG_ID(pf_to_mgmt) + 1) & \
31 #define MSG_SZ_IS_VALID(in_size) ((in_size) <= MAX_MSG_LEN)
33 #define MGMT_MSG_LEN_MIN 20
34 #define MGMT_MSG_LEN_STEP 16
35 #define MGMT_MSG_RSVD_FOR_DEV 8
37 #define SEGMENT_LEN 48
39 #define MAX_PF_MGMT_BUF_SIZE 2048
41 /* Data should be SEG LEN size aligned */
42 #define MAX_MSG_LEN 2016
44 #define MSG_NOT_RESP 0xFFFF
46 #define MGMT_MSG_TIMEOUT 1000
48 #define mgmt_to_pfhwdev(pf_mgmt) \
49 container_of(pf_mgmt, struct hinic_pfhwdev, pf_to_mgmt)
51 enum msg_segment_type {
56 enum mgmt_direction_type {
67 * hinic_register_mgmt_msg_cb - register msg handler for a msg from a module
68 * @pf_to_mgmt: PF to MGMT channel
69 * @mod: module in the chip that this handler will handle its messages
70 * @handle: private data for the callback
71 * @callback: the handler that will handle messages
73 void hinic_register_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
74 enum hinic_mod_type mod,
76 void (*callback)(void *handle,
78 u16 in_size, void *buf_out,
81 struct hinic_mgmt_cb *mgmt_cb = &pf_to_mgmt->mgmt_cb[mod];
83 mgmt_cb->cb = callback;
84 mgmt_cb->handle = handle;
85 mgmt_cb->state = HINIC_MGMT_CB_ENABLED;
89 * hinic_unregister_mgmt_msg_cb - unregister msg handler for a msg from a module
90 * @pf_to_mgmt: PF to MGMT channel
91 * @mod: module in the chip that this handler handles its messages
93 void hinic_unregister_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
94 enum hinic_mod_type mod)
96 struct hinic_mgmt_cb *mgmt_cb = &pf_to_mgmt->mgmt_cb[mod];
98 mgmt_cb->state &= ~HINIC_MGMT_CB_ENABLED;
100 while (mgmt_cb->state & HINIC_MGMT_CB_RUNNING)
107 * prepare_header - prepare the header of the message
108 * @pf_to_mgmt: PF to MGMT channel
109 * @msg_len: the length of the message
110 * @mod: module in the chip that will get the message
111 * @ack_type: ask for response
112 * @direction: the direction of the message
113 * @cmd: command of the message
114 * @msg_id: message id
116 * Return the prepared header value
118 static u64 prepare_header(struct hinic_pf_to_mgmt *pf_to_mgmt,
119 u16 msg_len, enum hinic_mod_type mod,
120 enum msg_ack_type ack_type,
121 enum mgmt_direction_type direction,
124 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
126 return HINIC_MSG_HEADER_SET(msg_len, MSG_LEN) |
127 HINIC_MSG_HEADER_SET(mod, MODULE) |
128 HINIC_MSG_HEADER_SET(SEGMENT_LEN, SEG_LEN) |
129 HINIC_MSG_HEADER_SET(ack_type, NO_ACK) |
130 HINIC_MSG_HEADER_SET(0, ASYNC_MGMT_TO_PF) |
131 HINIC_MSG_HEADER_SET(0, SEQID) |
132 HINIC_MSG_HEADER_SET(LAST_SEGMENT, LAST) |
133 HINIC_MSG_HEADER_SET(direction, DIRECTION) |
134 HINIC_MSG_HEADER_SET(cmd, CMD) |
135 HINIC_MSG_HEADER_SET(HINIC_HWIF_PCI_INTF(hwif), PCI_INTF) |
136 HINIC_MSG_HEADER_SET(HINIC_HWIF_PF_IDX(hwif), PF_IDX) |
137 HINIC_MSG_HEADER_SET(msg_id, MSG_ID);
141 * prepare_mgmt_cmd - prepare the mgmt command
142 * @mgmt_cmd: pointer to the command to prepare
143 * @header: pointer of the header for the message
144 * @msg: the data of the message
145 * @msg_len: the length of the message
147 static void prepare_mgmt_cmd(u8 *mgmt_cmd, u64 *header, u8 *msg, u16 msg_len)
149 memset(mgmt_cmd, 0, MGMT_MSG_RSVD_FOR_DEV);
151 mgmt_cmd += MGMT_MSG_RSVD_FOR_DEV;
152 memcpy(mgmt_cmd, header, sizeof(*header));
154 mgmt_cmd += sizeof(*header);
155 memcpy(mgmt_cmd, msg, msg_len);
159 * mgmt_msg_len - calculate the total message length
160 * @msg_data_len: the length of the message data
162 * Return the total message length
164 static u16 mgmt_msg_len(u16 msg_data_len)
166 /* RSVD + HEADER_SIZE + DATA_LEN */
167 u16 msg_len = MGMT_MSG_RSVD_FOR_DEV + sizeof(u64) + msg_data_len;
169 if (msg_len > MGMT_MSG_LEN_MIN)
170 msg_len = MGMT_MSG_LEN_MIN +
171 ALIGN((msg_len - MGMT_MSG_LEN_MIN),
174 msg_len = MGMT_MSG_LEN_MIN;
180 * send_msg_to_mgmt - send message to mgmt by API CMD
181 * @pf_to_mgmt: PF to MGMT channel
182 * @mod: module in the chip that will get the message
183 * @cmd: command of the message
184 * @data: the msg data
185 * @data_len: the msg data length
186 * @ack_type: ask for response
187 * @direction: the direction of the original message
188 * @resp_msg_id: msg id to response for
190 * Return 0 - Success, negative - Failure
192 static int send_msg_to_mgmt(struct hinic_pf_to_mgmt *pf_to_mgmt,
193 enum hinic_mod_type mod, u8 cmd,
194 u8 *data, u16 data_len,
195 enum msg_ack_type ack_type,
196 enum mgmt_direction_type direction,
199 struct hinic_api_cmd_chain *chain;
203 msg_id = SYNC_MSG_ID(pf_to_mgmt);
205 if (direction == MGMT_RESP) {
206 header = prepare_header(pf_to_mgmt, data_len, mod, ack_type,
207 direction, cmd, resp_msg_id);
209 SYNC_MSG_ID_INC(pf_to_mgmt);
210 header = prepare_header(pf_to_mgmt, data_len, mod, ack_type,
211 direction, cmd, msg_id);
214 prepare_mgmt_cmd(pf_to_mgmt->sync_msg_buf, &header, data, data_len);
216 chain = pf_to_mgmt->cmd_chain[HINIC_API_CMD_WRITE_TO_MGMT_CPU];
217 return hinic_api_cmd_write(chain, HINIC_NODE_ID_MGMT,
218 pf_to_mgmt->sync_msg_buf,
219 mgmt_msg_len(data_len));
223 * msg_to_mgmt_sync - send sync message to mgmt
224 * @pf_to_mgmt: PF to MGMT channel
225 * @mod: module in the chip that will get the message
226 * @cmd: command of the message
227 * @buf_in: the msg data
228 * @in_size: the msg data length
230 * @out_size: response length
231 * @direction: the direction of the original message
232 * @resp_msg_id: msg id to response for
234 * Return 0 - Success, negative - Failure
236 static int msg_to_mgmt_sync(struct hinic_pf_to_mgmt *pf_to_mgmt,
237 enum hinic_mod_type mod, u8 cmd,
238 u8 *buf_in, u16 in_size,
239 u8 *buf_out, u16 *out_size,
240 enum mgmt_direction_type direction,
243 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
244 struct pci_dev *pdev = hwif->pdev;
245 struct hinic_recv_msg *recv_msg;
246 struct completion *recv_done;
250 /* Lock the sync_msg_buf */
251 down(&pf_to_mgmt->sync_msg_lock);
253 recv_msg = &pf_to_mgmt->recv_resp_msg_from_mgmt;
254 recv_done = &recv_msg->recv_done;
256 if (resp_msg_id == MSG_NOT_RESP)
257 msg_id = SYNC_MSG_ID(pf_to_mgmt);
259 msg_id = resp_msg_id;
261 init_completion(recv_done);
263 err = send_msg_to_mgmt(pf_to_mgmt, mod, cmd, buf_in, in_size,
264 MSG_ACK, direction, resp_msg_id);
266 dev_err(&pdev->dev, "Failed to send sync msg to mgmt\n");
267 goto unlock_sync_msg;
270 if (!wait_for_completion_timeout(recv_done, MGMT_MSG_TIMEOUT)) {
271 dev_err(&pdev->dev, "MGMT timeout, MSG id = %d\n", msg_id);
273 goto unlock_sync_msg;
276 smp_rmb(); /* verify reading after completion */
278 if (recv_msg->msg_id != msg_id) {
279 dev_err(&pdev->dev, "incorrect MSG for id = %d\n", msg_id);
281 goto unlock_sync_msg;
284 if ((buf_out) && (recv_msg->msg_len <= MAX_PF_MGMT_BUF_SIZE)) {
285 memcpy(buf_out, recv_msg->msg, recv_msg->msg_len);
286 *out_size = recv_msg->msg_len;
290 up(&pf_to_mgmt->sync_msg_lock);
295 * msg_to_mgmt_async - send message to mgmt without response
296 * @pf_to_mgmt: PF to MGMT channel
297 * @mod: module in the chip that will get the message
298 * @cmd: command of the message
299 * @buf_in: the msg data
300 * @in_size: the msg data length
301 * @direction: the direction of the original message
302 * @resp_msg_id: msg id to response for
304 * Return 0 - Success, negative - Failure
306 static int msg_to_mgmt_async(struct hinic_pf_to_mgmt *pf_to_mgmt,
307 enum hinic_mod_type mod, u8 cmd,
308 u8 *buf_in, u16 in_size,
309 enum mgmt_direction_type direction,
314 /* Lock the sync_msg_buf */
315 down(&pf_to_mgmt->sync_msg_lock);
317 err = send_msg_to_mgmt(pf_to_mgmt, mod, cmd, buf_in, in_size,
318 MSG_NO_ACK, direction, resp_msg_id);
320 up(&pf_to_mgmt->sync_msg_lock);
325 * hinic_msg_to_mgmt - send message to mgmt
326 * @pf_to_mgmt: PF to MGMT channel
327 * @mod: module in the chip that will get the message
328 * @cmd: command of the message
329 * @buf_in: the msg data
330 * @in_size: the msg data length
332 * @out_size: returned response length
333 * @sync: sync msg or async msg
335 * Return 0 - Success, negative - Failure
337 int hinic_msg_to_mgmt(struct hinic_pf_to_mgmt *pf_to_mgmt,
338 enum hinic_mod_type mod, u8 cmd,
339 void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
340 enum hinic_mgmt_msg_type sync)
342 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
343 struct pci_dev *pdev = hwif->pdev;
345 if (sync != HINIC_MGMT_MSG_SYNC) {
346 dev_err(&pdev->dev, "Invalid MGMT msg type\n");
350 if (!MSG_SZ_IS_VALID(in_size)) {
351 dev_err(&pdev->dev, "Invalid MGMT msg buffer size\n");
355 return msg_to_mgmt_sync(pf_to_mgmt, mod, cmd, buf_in, in_size,
356 buf_out, out_size, MGMT_DIRECT_SEND,
361 * mgmt_recv_msg_handler - handler for message from mgmt cpu
362 * @pf_to_mgmt: PF to MGMT channel
363 * @recv_msg: received message details
365 static void mgmt_recv_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
366 struct hinic_recv_msg *recv_msg)
368 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
369 struct pci_dev *pdev = hwif->pdev;
370 u8 *buf_out = recv_msg->buf_out;
371 struct hinic_mgmt_cb *mgmt_cb;
372 unsigned long cb_state;
375 if (recv_msg->mod >= HINIC_MOD_MAX) {
376 dev_err(&pdev->dev, "Unknown MGMT MSG module = %d\n",
381 mgmt_cb = &pf_to_mgmt->mgmt_cb[recv_msg->mod];
383 cb_state = cmpxchg(&mgmt_cb->state,
384 HINIC_MGMT_CB_ENABLED,
385 HINIC_MGMT_CB_ENABLED | HINIC_MGMT_CB_RUNNING);
387 if ((cb_state == HINIC_MGMT_CB_ENABLED) && (mgmt_cb->cb))
388 mgmt_cb->cb(mgmt_cb->handle, recv_msg->cmd,
389 recv_msg->msg, recv_msg->msg_len,
392 dev_err(&pdev->dev, "No MGMT msg handler, mod = %d\n",
395 mgmt_cb->state &= ~HINIC_MGMT_CB_RUNNING;
397 if (!recv_msg->async_mgmt_to_pf)
398 /* MGMT sent sync msg, send the response */
399 msg_to_mgmt_async(pf_to_mgmt, recv_msg->mod, recv_msg->cmd,
400 buf_out, out_size, MGMT_RESP,
405 * mgmt_resp_msg_handler - handler for a response message from mgmt cpu
406 * @pf_to_mgmt: PF to MGMT channel
407 * @recv_msg: received message details
409 static void mgmt_resp_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
410 struct hinic_recv_msg *recv_msg)
412 wmb(); /* verify writing all, before reading */
414 complete(&recv_msg->recv_done);
418 * recv_mgmt_msg_handler - handler for a message from mgmt cpu
419 * @pf_to_mgmt: PF to MGMT channel
420 * @header: the header of the message
421 * @recv_msg: received message details
423 static void recv_mgmt_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
424 u64 *header, struct hinic_recv_msg *recv_msg)
426 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
427 struct pci_dev *pdev = hwif->pdev;
431 seq_id = HINIC_MSG_HEADER_GET(*header, SEQID);
432 seg_len = HINIC_MSG_HEADER_GET(*header, SEG_LEN);
434 if (seq_id >= (MAX_MSG_LEN / SEGMENT_LEN)) {
435 dev_err(&pdev->dev, "recv big mgmt msg\n");
439 msg_body = (u8 *)header + sizeof(*header);
440 memcpy(recv_msg->msg + seq_id * SEGMENT_LEN, msg_body, seg_len);
442 if (!HINIC_MSG_HEADER_GET(*header, LAST))
445 recv_msg->cmd = HINIC_MSG_HEADER_GET(*header, CMD);
446 recv_msg->mod = HINIC_MSG_HEADER_GET(*header, MODULE);
447 recv_msg->async_mgmt_to_pf = HINIC_MSG_HEADER_GET(*header,
449 recv_msg->msg_len = HINIC_MSG_HEADER_GET(*header, MSG_LEN);
450 recv_msg->msg_id = HINIC_MSG_HEADER_GET(*header, MSG_ID);
452 if (HINIC_MSG_HEADER_GET(*header, DIRECTION) == MGMT_RESP)
453 mgmt_resp_msg_handler(pf_to_mgmt, recv_msg);
455 mgmt_recv_msg_handler(pf_to_mgmt, recv_msg);
459 * mgmt_msg_aeqe_handler - handler for a mgmt message event
460 * @handle: PF to MGMT channel
461 * @data: the header of the message
464 static void mgmt_msg_aeqe_handler(void *handle, void *data, u8 size)
466 struct hinic_pf_to_mgmt *pf_to_mgmt = handle;
467 struct hinic_recv_msg *recv_msg;
468 u64 *header = (u64 *)data;
470 recv_msg = HINIC_MSG_HEADER_GET(*header, DIRECTION) ==
472 &pf_to_mgmt->recv_msg_from_mgmt :
473 &pf_to_mgmt->recv_resp_msg_from_mgmt;
475 recv_mgmt_msg_handler(pf_to_mgmt, header, recv_msg);
479 * alloc_recv_msg - allocate receive message memory
480 * @pf_to_mgmt: PF to MGMT channel
481 * @recv_msg: pointer that will hold the allocated data
483 * Return 0 - Success, negative - Failure
485 static int alloc_recv_msg(struct hinic_pf_to_mgmt *pf_to_mgmt,
486 struct hinic_recv_msg *recv_msg)
488 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
489 struct pci_dev *pdev = hwif->pdev;
491 recv_msg->msg = devm_kzalloc(&pdev->dev, MAX_PF_MGMT_BUF_SIZE,
496 recv_msg->buf_out = devm_kzalloc(&pdev->dev, MAX_PF_MGMT_BUF_SIZE,
498 if (!recv_msg->buf_out)
505 * alloc_msg_buf - allocate all the message buffers of PF to MGMT channel
506 * @pf_to_mgmt: PF to MGMT channel
508 * Return 0 - Success, negative - Failure
510 static int alloc_msg_buf(struct hinic_pf_to_mgmt *pf_to_mgmt)
512 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
513 struct pci_dev *pdev = hwif->pdev;
516 err = alloc_recv_msg(pf_to_mgmt,
517 &pf_to_mgmt->recv_msg_from_mgmt);
519 dev_err(&pdev->dev, "Failed to allocate recv msg\n");
523 err = alloc_recv_msg(pf_to_mgmt,
524 &pf_to_mgmt->recv_resp_msg_from_mgmt);
526 dev_err(&pdev->dev, "Failed to allocate resp recv msg\n");
530 pf_to_mgmt->sync_msg_buf = devm_kzalloc(&pdev->dev,
531 MAX_PF_MGMT_BUF_SIZE,
533 if (!pf_to_mgmt->sync_msg_buf)
540 * hinic_pf_to_mgmt_init - initialize PF to MGMT channel
541 * @pf_to_mgmt: PF to MGMT channel
542 * @hwif: HW interface the PF to MGMT will use for accessing HW
544 * Return 0 - Success, negative - Failure
546 int hinic_pf_to_mgmt_init(struct hinic_pf_to_mgmt *pf_to_mgmt,
547 struct hinic_hwif *hwif)
549 struct hinic_pfhwdev *pfhwdev = mgmt_to_pfhwdev(pf_to_mgmt);
550 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
551 struct pci_dev *pdev = hwif->pdev;
554 pf_to_mgmt->hwif = hwif;
556 sema_init(&pf_to_mgmt->sync_msg_lock, 1);
557 pf_to_mgmt->sync_msg_id = 0;
559 err = alloc_msg_buf(pf_to_mgmt);
561 dev_err(&pdev->dev, "Failed to allocate msg buffers\n");
565 err = hinic_api_cmd_init(pf_to_mgmt->cmd_chain, hwif);
567 dev_err(&pdev->dev, "Failed to initialize cmd chains\n");
571 hinic_aeq_register_hw_cb(&hwdev->aeqs, HINIC_MSG_FROM_MGMT_CPU,
573 mgmt_msg_aeqe_handler);
578 * hinic_pf_to_mgmt_free - free PF to MGMT channel
579 * @pf_to_mgmt: PF to MGMT channel
581 void hinic_pf_to_mgmt_free(struct hinic_pf_to_mgmt *pf_to_mgmt)
583 struct hinic_pfhwdev *pfhwdev = mgmt_to_pfhwdev(pf_to_mgmt);
584 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
586 hinic_aeq_unregister_hw_cb(&hwdev->aeqs, HINIC_MSG_FROM_MGMT_CPU);
587 hinic_api_cmd_free(pf_to_mgmt->cmd_chain);