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 5000
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,
271 msecs_to_jiffies(MGMT_MSG_TIMEOUT))) {
272 dev_err(&pdev->dev, "MGMT timeout, MSG id = %d\n", msg_id);
274 goto unlock_sync_msg;
277 smp_rmb(); /* verify reading after completion */
279 if (recv_msg->msg_id != msg_id) {
280 dev_err(&pdev->dev, "incorrect MSG for id = %d\n", msg_id);
282 goto unlock_sync_msg;
285 if ((buf_out) && (recv_msg->msg_len <= MAX_PF_MGMT_BUF_SIZE)) {
286 memcpy(buf_out, recv_msg->msg, recv_msg->msg_len);
287 *out_size = recv_msg->msg_len;
291 up(&pf_to_mgmt->sync_msg_lock);
296 * msg_to_mgmt_async - send message to mgmt without response
297 * @pf_to_mgmt: PF to MGMT channel
298 * @mod: module in the chip that will get the message
299 * @cmd: command of the message
300 * @buf_in: the msg data
301 * @in_size: the msg data length
302 * @direction: the direction of the original message
303 * @resp_msg_id: msg id to response for
305 * Return 0 - Success, negative - Failure
307 static int msg_to_mgmt_async(struct hinic_pf_to_mgmt *pf_to_mgmt,
308 enum hinic_mod_type mod, u8 cmd,
309 u8 *buf_in, u16 in_size,
310 enum mgmt_direction_type direction,
315 /* Lock the sync_msg_buf */
316 down(&pf_to_mgmt->sync_msg_lock);
318 err = send_msg_to_mgmt(pf_to_mgmt, mod, cmd, buf_in, in_size,
319 MSG_NO_ACK, direction, resp_msg_id);
321 up(&pf_to_mgmt->sync_msg_lock);
326 * hinic_msg_to_mgmt - send message to mgmt
327 * @pf_to_mgmt: PF to MGMT channel
328 * @mod: module in the chip that will get the message
329 * @cmd: command of the message
330 * @buf_in: the msg data
331 * @in_size: the msg data length
333 * @out_size: returned response length
334 * @sync: sync msg or async msg
336 * Return 0 - Success, negative - Failure
338 int hinic_msg_to_mgmt(struct hinic_pf_to_mgmt *pf_to_mgmt,
339 enum hinic_mod_type mod, u8 cmd,
340 void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
341 enum hinic_mgmt_msg_type sync)
343 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
344 struct pci_dev *pdev = hwif->pdev;
346 if (sync != HINIC_MGMT_MSG_SYNC) {
347 dev_err(&pdev->dev, "Invalid MGMT msg type\n");
351 if (!MSG_SZ_IS_VALID(in_size)) {
352 dev_err(&pdev->dev, "Invalid MGMT msg buffer size\n");
356 return msg_to_mgmt_sync(pf_to_mgmt, mod, cmd, buf_in, in_size,
357 buf_out, out_size, MGMT_DIRECT_SEND,
362 * mgmt_recv_msg_handler - handler for message from mgmt cpu
363 * @pf_to_mgmt: PF to MGMT channel
364 * @recv_msg: received message details
366 static void mgmt_recv_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
367 struct hinic_recv_msg *recv_msg)
369 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
370 struct pci_dev *pdev = hwif->pdev;
371 u8 *buf_out = recv_msg->buf_out;
372 struct hinic_mgmt_cb *mgmt_cb;
373 unsigned long cb_state;
376 if (recv_msg->mod >= HINIC_MOD_MAX) {
377 dev_err(&pdev->dev, "Unknown MGMT MSG module = %d\n",
382 mgmt_cb = &pf_to_mgmt->mgmt_cb[recv_msg->mod];
384 cb_state = cmpxchg(&mgmt_cb->state,
385 HINIC_MGMT_CB_ENABLED,
386 HINIC_MGMT_CB_ENABLED | HINIC_MGMT_CB_RUNNING);
388 if ((cb_state == HINIC_MGMT_CB_ENABLED) && (mgmt_cb->cb))
389 mgmt_cb->cb(mgmt_cb->handle, recv_msg->cmd,
390 recv_msg->msg, recv_msg->msg_len,
393 dev_err(&pdev->dev, "No MGMT msg handler, mod = %d\n",
396 mgmt_cb->state &= ~HINIC_MGMT_CB_RUNNING;
398 if (!recv_msg->async_mgmt_to_pf)
399 /* MGMT sent sync msg, send the response */
400 msg_to_mgmt_async(pf_to_mgmt, recv_msg->mod, recv_msg->cmd,
401 buf_out, out_size, MGMT_RESP,
406 * mgmt_resp_msg_handler - handler for a response message from mgmt cpu
407 * @pf_to_mgmt: PF to MGMT channel
408 * @recv_msg: received message details
410 static void mgmt_resp_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
411 struct hinic_recv_msg *recv_msg)
413 wmb(); /* verify writing all, before reading */
415 complete(&recv_msg->recv_done);
419 * recv_mgmt_msg_handler - handler for a message from mgmt cpu
420 * @pf_to_mgmt: PF to MGMT channel
421 * @header: the header of the message
422 * @recv_msg: received message details
424 static void recv_mgmt_msg_handler(struct hinic_pf_to_mgmt *pf_to_mgmt,
425 u64 *header, struct hinic_recv_msg *recv_msg)
427 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
428 struct pci_dev *pdev = hwif->pdev;
432 seq_id = HINIC_MSG_HEADER_GET(*header, SEQID);
433 seg_len = HINIC_MSG_HEADER_GET(*header, SEG_LEN);
435 if (seq_id >= (MAX_MSG_LEN / SEGMENT_LEN)) {
436 dev_err(&pdev->dev, "recv big mgmt msg\n");
440 msg_body = (u8 *)header + sizeof(*header);
441 memcpy(recv_msg->msg + seq_id * SEGMENT_LEN, msg_body, seg_len);
443 if (!HINIC_MSG_HEADER_GET(*header, LAST))
446 recv_msg->cmd = HINIC_MSG_HEADER_GET(*header, CMD);
447 recv_msg->mod = HINIC_MSG_HEADER_GET(*header, MODULE);
448 recv_msg->async_mgmt_to_pf = HINIC_MSG_HEADER_GET(*header,
450 recv_msg->msg_len = HINIC_MSG_HEADER_GET(*header, MSG_LEN);
451 recv_msg->msg_id = HINIC_MSG_HEADER_GET(*header, MSG_ID);
453 if (HINIC_MSG_HEADER_GET(*header, DIRECTION) == MGMT_RESP)
454 mgmt_resp_msg_handler(pf_to_mgmt, recv_msg);
456 mgmt_recv_msg_handler(pf_to_mgmt, recv_msg);
460 * mgmt_msg_aeqe_handler - handler for a mgmt message event
461 * @handle: PF to MGMT channel
462 * @data: the header of the message
465 static void mgmt_msg_aeqe_handler(void *handle, void *data, u8 size)
467 struct hinic_pf_to_mgmt *pf_to_mgmt = handle;
468 struct hinic_recv_msg *recv_msg;
469 u64 *header = (u64 *)data;
471 recv_msg = HINIC_MSG_HEADER_GET(*header, DIRECTION) ==
473 &pf_to_mgmt->recv_msg_from_mgmt :
474 &pf_to_mgmt->recv_resp_msg_from_mgmt;
476 recv_mgmt_msg_handler(pf_to_mgmt, header, recv_msg);
480 * alloc_recv_msg - allocate receive message memory
481 * @pf_to_mgmt: PF to MGMT channel
482 * @recv_msg: pointer that will hold the allocated data
484 * Return 0 - Success, negative - Failure
486 static int alloc_recv_msg(struct hinic_pf_to_mgmt *pf_to_mgmt,
487 struct hinic_recv_msg *recv_msg)
489 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
490 struct pci_dev *pdev = hwif->pdev;
492 recv_msg->msg = devm_kzalloc(&pdev->dev, MAX_PF_MGMT_BUF_SIZE,
497 recv_msg->buf_out = devm_kzalloc(&pdev->dev, MAX_PF_MGMT_BUF_SIZE,
499 if (!recv_msg->buf_out)
506 * alloc_msg_buf - allocate all the message buffers of PF to MGMT channel
507 * @pf_to_mgmt: PF to MGMT channel
509 * Return 0 - Success, negative - Failure
511 static int alloc_msg_buf(struct hinic_pf_to_mgmt *pf_to_mgmt)
513 struct hinic_hwif *hwif = pf_to_mgmt->hwif;
514 struct pci_dev *pdev = hwif->pdev;
517 err = alloc_recv_msg(pf_to_mgmt,
518 &pf_to_mgmt->recv_msg_from_mgmt);
520 dev_err(&pdev->dev, "Failed to allocate recv msg\n");
524 err = alloc_recv_msg(pf_to_mgmt,
525 &pf_to_mgmt->recv_resp_msg_from_mgmt);
527 dev_err(&pdev->dev, "Failed to allocate resp recv msg\n");
531 pf_to_mgmt->sync_msg_buf = devm_kzalloc(&pdev->dev,
532 MAX_PF_MGMT_BUF_SIZE,
534 if (!pf_to_mgmt->sync_msg_buf)
541 * hinic_pf_to_mgmt_init - initialize PF to MGMT channel
542 * @pf_to_mgmt: PF to MGMT channel
543 * @hwif: HW interface the PF to MGMT will use for accessing HW
545 * Return 0 - Success, negative - Failure
547 int hinic_pf_to_mgmt_init(struct hinic_pf_to_mgmt *pf_to_mgmt,
548 struct hinic_hwif *hwif)
550 struct hinic_pfhwdev *pfhwdev = mgmt_to_pfhwdev(pf_to_mgmt);
551 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
552 struct pci_dev *pdev = hwif->pdev;
555 pf_to_mgmt->hwif = hwif;
557 sema_init(&pf_to_mgmt->sync_msg_lock, 1);
558 pf_to_mgmt->sync_msg_id = 0;
560 err = alloc_msg_buf(pf_to_mgmt);
562 dev_err(&pdev->dev, "Failed to allocate msg buffers\n");
566 err = hinic_api_cmd_init(pf_to_mgmt->cmd_chain, hwif);
568 dev_err(&pdev->dev, "Failed to initialize cmd chains\n");
572 hinic_aeq_register_hw_cb(&hwdev->aeqs, HINIC_MSG_FROM_MGMT_CPU,
574 mgmt_msg_aeqe_handler);
579 * hinic_pf_to_mgmt_free - free PF to MGMT channel
580 * @pf_to_mgmt: PF to MGMT channel
582 void hinic_pf_to_mgmt_free(struct hinic_pf_to_mgmt *pf_to_mgmt)
584 struct hinic_pfhwdev *pfhwdev = mgmt_to_pfhwdev(pf_to_mgmt);
585 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
587 hinic_aeq_unregister_hw_cb(&hwdev->aeqs, HINIC_MSG_FROM_MGMT_CPU);
588 hinic_api_cmd_free(pf_to_mgmt->cmd_chain);