1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3
4 #include "idpf_controlq.h"
5
6 /**
7 * idpf_ctlq_setup_regs - initialize control queue registers
8 * @cq: pointer to the specific control queue
9 * @q_create_info: structs containing info for each queue to be initialized
10 */
idpf_ctlq_setup_regs(struct idpf_ctlq_info * cq,struct idpf_ctlq_create_info * q_create_info)11 static void idpf_ctlq_setup_regs(struct idpf_ctlq_info *cq,
12 struct idpf_ctlq_create_info *q_create_info)
13 {
14 /* set control queue registers in our local struct */
15 cq->reg.head = q_create_info->reg.head;
16 cq->reg.tail = q_create_info->reg.tail;
17 cq->reg.len = q_create_info->reg.len;
18 cq->reg.bah = q_create_info->reg.bah;
19 cq->reg.bal = q_create_info->reg.bal;
20 cq->reg.len_mask = q_create_info->reg.len_mask;
21 cq->reg.len_ena_mask = q_create_info->reg.len_ena_mask;
22 cq->reg.head_mask = q_create_info->reg.head_mask;
23 }
24
25 /**
26 * idpf_ctlq_init_regs - Initialize control queue registers
27 * @hw: pointer to hw struct
28 * @cq: pointer to the specific Control queue
29 * @is_rxq: true if receive control queue, false otherwise
30 *
31 * Initialize registers. The caller is expected to have already initialized the
32 * descriptor ring memory and buffer memory
33 */
idpf_ctlq_init_regs(struct idpf_hw * hw,struct idpf_ctlq_info * cq,bool is_rxq)34 static void idpf_ctlq_init_regs(struct idpf_hw *hw, struct idpf_ctlq_info *cq,
35 bool is_rxq)
36 {
37 /* Update tail to post pre-allocated buffers for rx queues */
38 if (is_rxq)
39 wr32(hw, cq->reg.tail, (u32)(cq->ring_size - 1));
40
41 /* For non-Mailbox control queues only TAIL need to be set */
42 if (cq->q_id != -1)
43 return;
44
45 /* Clear Head for both send or receive */
46 wr32(hw, cq->reg.head, 0);
47
48 /* set starting point */
49 wr32(hw, cq->reg.bal, lower_32_bits(cq->desc_ring.pa));
50 wr32(hw, cq->reg.bah, upper_32_bits(cq->desc_ring.pa));
51 wr32(hw, cq->reg.len, (cq->ring_size | cq->reg.len_ena_mask));
52 }
53
54 /**
55 * idpf_ctlq_init_rxq_bufs - populate receive queue descriptors with buf
56 * @cq: pointer to the specific Control queue
57 *
58 * Record the address of the receive queue DMA buffers in the descriptors.
59 * The buffers must have been previously allocated.
60 */
idpf_ctlq_init_rxq_bufs(struct idpf_ctlq_info * cq)61 static void idpf_ctlq_init_rxq_bufs(struct idpf_ctlq_info *cq)
62 {
63 int i;
64
65 for (i = 0; i < cq->ring_size; i++) {
66 struct idpf_ctlq_desc *desc = IDPF_CTLQ_DESC(cq, i);
67 struct idpf_dma_mem *bi = cq->bi.rx_buff[i];
68
69 /* No buffer to post to descriptor, continue */
70 if (!bi)
71 continue;
72
73 desc->flags =
74 cpu_to_le16(IDPF_CTLQ_FLAG_BUF | IDPF_CTLQ_FLAG_RD);
75 desc->opcode = 0;
76 desc->datalen = cpu_to_le16(bi->size);
77 desc->ret_val = 0;
78 desc->v_opcode_dtype = 0;
79 desc->v_retval = 0;
80 desc->params.indirect.addr_high =
81 cpu_to_le32(upper_32_bits(bi->pa));
82 desc->params.indirect.addr_low =
83 cpu_to_le32(lower_32_bits(bi->pa));
84 desc->params.indirect.param0 = 0;
85 desc->params.indirect.sw_cookie = 0;
86 desc->params.indirect.v_flags = 0;
87 }
88 }
89
90 /**
91 * idpf_ctlq_shutdown - shutdown the CQ
92 * @hw: pointer to hw struct
93 * @cq: pointer to the specific Control queue
94 *
95 * The main shutdown routine for any controq queue
96 */
idpf_ctlq_shutdown(struct idpf_hw * hw,struct idpf_ctlq_info * cq)97 static void idpf_ctlq_shutdown(struct idpf_hw *hw, struct idpf_ctlq_info *cq)
98 {
99 mutex_lock(&cq->cq_lock);
100
101 /* free ring buffers and the ring itself */
102 idpf_ctlq_dealloc_ring_res(hw, cq);
103
104 /* Set ring_size to 0 to indicate uninitialized queue */
105 cq->ring_size = 0;
106
107 mutex_unlock(&cq->cq_lock);
108 mutex_destroy(&cq->cq_lock);
109 }
110
111 /**
112 * idpf_ctlq_add - add one control queue
113 * @hw: pointer to hardware struct
114 * @qinfo: info for queue to be created
115 * @cq_out: (output) double pointer to control queue to be created
116 *
117 * Allocate and initialize a control queue and add it to the control queue list.
118 * The cq parameter will be allocated/initialized and passed back to the caller
119 * if no errors occur.
120 *
121 * Note: idpf_ctlq_init must be called prior to any calls to idpf_ctlq_add
122 */
idpf_ctlq_add(struct idpf_hw * hw,struct idpf_ctlq_create_info * qinfo,struct idpf_ctlq_info ** cq_out)123 int idpf_ctlq_add(struct idpf_hw *hw,
124 struct idpf_ctlq_create_info *qinfo,
125 struct idpf_ctlq_info **cq_out)
126 {
127 struct idpf_ctlq_info *cq;
128 bool is_rxq = false;
129 int err;
130
131 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
132 if (!cq)
133 return -ENOMEM;
134
135 cq->cq_type = qinfo->type;
136 cq->q_id = qinfo->id;
137 cq->buf_size = qinfo->buf_size;
138 cq->ring_size = qinfo->len;
139
140 cq->next_to_use = 0;
141 cq->next_to_clean = 0;
142 cq->next_to_post = cq->ring_size - 1;
143
144 switch (qinfo->type) {
145 case IDPF_CTLQ_TYPE_MAILBOX_RX:
146 is_rxq = true;
147 fallthrough;
148 case IDPF_CTLQ_TYPE_MAILBOX_TX:
149 err = idpf_ctlq_alloc_ring_res(hw, cq);
150 break;
151 default:
152 err = -EBADR;
153 break;
154 }
155
156 if (err)
157 goto init_free_q;
158
159 if (is_rxq) {
160 idpf_ctlq_init_rxq_bufs(cq);
161 } else {
162 /* Allocate the array of msg pointers for TX queues */
163 cq->bi.tx_msg = kcalloc(qinfo->len,
164 sizeof(struct idpf_ctlq_msg *),
165 GFP_KERNEL);
166 if (!cq->bi.tx_msg) {
167 err = -ENOMEM;
168 goto init_dealloc_q_mem;
169 }
170 }
171
172 idpf_ctlq_setup_regs(cq, qinfo);
173
174 idpf_ctlq_init_regs(hw, cq, is_rxq);
175
176 mutex_init(&cq->cq_lock);
177
178 list_add(&cq->cq_list, &hw->cq_list_head);
179
180 *cq_out = cq;
181
182 return 0;
183
184 init_dealloc_q_mem:
185 /* free ring buffers and the ring itself */
186 idpf_ctlq_dealloc_ring_res(hw, cq);
187 init_free_q:
188 kfree(cq);
189
190 return err;
191 }
192
193 /**
194 * idpf_ctlq_remove - deallocate and remove specified control queue
195 * @hw: pointer to hardware struct
196 * @cq: pointer to control queue to be removed
197 */
idpf_ctlq_remove(struct idpf_hw * hw,struct idpf_ctlq_info * cq)198 void idpf_ctlq_remove(struct idpf_hw *hw,
199 struct idpf_ctlq_info *cq)
200 {
201 list_del(&cq->cq_list);
202 idpf_ctlq_shutdown(hw, cq);
203 kfree(cq);
204 }
205
206 /**
207 * idpf_ctlq_init - main initialization routine for all control queues
208 * @hw: pointer to hardware struct
209 * @num_q: number of queues to initialize
210 * @q_info: array of structs containing info for each queue to be initialized
211 *
212 * This initializes any number and any type of control queues. This is an all
213 * or nothing routine; if one fails, all previously allocated queues will be
214 * destroyed. This must be called prior to using the individual add/remove
215 * APIs.
216 */
idpf_ctlq_init(struct idpf_hw * hw,u8 num_q,struct idpf_ctlq_create_info * q_info)217 int idpf_ctlq_init(struct idpf_hw *hw, u8 num_q,
218 struct idpf_ctlq_create_info *q_info)
219 {
220 struct idpf_ctlq_info *cq, *tmp;
221 int err;
222 int i;
223
224 INIT_LIST_HEAD(&hw->cq_list_head);
225
226 for (i = 0; i < num_q; i++) {
227 struct idpf_ctlq_create_info *qinfo = q_info + i;
228
229 err = idpf_ctlq_add(hw, qinfo, &cq);
230 if (err)
231 goto init_destroy_qs;
232 }
233
234 return 0;
235
236 init_destroy_qs:
237 list_for_each_entry_safe(cq, tmp, &hw->cq_list_head, cq_list)
238 idpf_ctlq_remove(hw, cq);
239
240 return err;
241 }
242
243 /**
244 * idpf_ctlq_deinit - destroy all control queues
245 * @hw: pointer to hw struct
246 */
idpf_ctlq_deinit(struct idpf_hw * hw)247 void idpf_ctlq_deinit(struct idpf_hw *hw)
248 {
249 struct idpf_ctlq_info *cq, *tmp;
250
251 list_for_each_entry_safe(cq, tmp, &hw->cq_list_head, cq_list)
252 idpf_ctlq_remove(hw, cq);
253 }
254
255 /**
256 * idpf_ctlq_send - send command to Control Queue (CTQ)
257 * @hw: pointer to hw struct
258 * @cq: handle to control queue struct to send on
259 * @num_q_msg: number of messages to send on control queue
260 * @q_msg: pointer to array of queue messages to be sent
261 *
262 * The caller is expected to allocate DMAable buffers and pass them to the
263 * send routine via the q_msg struct / control queue specific data struct.
264 * The control queue will hold a reference to each send message until
265 * the completion for that message has been cleaned.
266 */
idpf_ctlq_send(struct idpf_hw * hw,struct idpf_ctlq_info * cq,u16 num_q_msg,struct idpf_ctlq_msg q_msg[])267 int idpf_ctlq_send(struct idpf_hw *hw, struct idpf_ctlq_info *cq,
268 u16 num_q_msg, struct idpf_ctlq_msg q_msg[])
269 {
270 struct idpf_ctlq_desc *desc;
271 int num_desc_avail;
272 int err = 0;
273 int i;
274
275 mutex_lock(&cq->cq_lock);
276
277 /* Ensure there are enough descriptors to send all messages */
278 num_desc_avail = IDPF_CTLQ_DESC_UNUSED(cq);
279 if (num_desc_avail == 0 || num_desc_avail < num_q_msg) {
280 err = -ENOSPC;
281 goto err_unlock;
282 }
283
284 for (i = 0; i < num_q_msg; i++) {
285 struct idpf_ctlq_msg *msg = &q_msg[i];
286
287 desc = IDPF_CTLQ_DESC(cq, cq->next_to_use);
288
289 desc->opcode = cpu_to_le16(msg->opcode);
290 desc->pfid_vfid = cpu_to_le16(msg->func_id);
291
292 desc->v_opcode_dtype = cpu_to_le32(msg->cookie.mbx.chnl_opcode);
293 desc->v_retval = cpu_to_le32(msg->cookie.mbx.chnl_retval);
294
295 desc->flags = cpu_to_le16((msg->host_id & IDPF_HOST_ID_MASK) <<
296 IDPF_CTLQ_FLAG_HOST_ID_S);
297 if (msg->data_len) {
298 struct idpf_dma_mem *buff = msg->ctx.indirect.payload;
299
300 desc->datalen |= cpu_to_le16(msg->data_len);
301 desc->flags |= cpu_to_le16(IDPF_CTLQ_FLAG_BUF);
302 desc->flags |= cpu_to_le16(IDPF_CTLQ_FLAG_RD);
303
304 /* Update the address values in the desc with the pa
305 * value for respective buffer
306 */
307 desc->params.indirect.addr_high =
308 cpu_to_le32(upper_32_bits(buff->pa));
309 desc->params.indirect.addr_low =
310 cpu_to_le32(lower_32_bits(buff->pa));
311
312 memcpy(&desc->params, msg->ctx.indirect.context,
313 IDPF_INDIRECT_CTX_SIZE);
314 } else {
315 memcpy(&desc->params, msg->ctx.direct,
316 IDPF_DIRECT_CTX_SIZE);
317 }
318
319 /* Store buffer info */
320 cq->bi.tx_msg[cq->next_to_use] = msg;
321
322 (cq->next_to_use)++;
323 if (cq->next_to_use == cq->ring_size)
324 cq->next_to_use = 0;
325 }
326
327 /* Force memory write to complete before letting hardware
328 * know that there are new descriptors to fetch.
329 */
330 dma_wmb();
331
332 wr32(hw, cq->reg.tail, cq->next_to_use);
333
334 err_unlock:
335 mutex_unlock(&cq->cq_lock);
336
337 return err;
338 }
339
340 /**
341 * idpf_ctlq_clean_sq - reclaim send descriptors on HW write back for the
342 * requested queue
343 * @cq: pointer to the specific Control queue
344 * @clean_count: (input|output) number of descriptors to clean as input, and
345 * number of descriptors actually cleaned as output
346 * @msg_status: (output) pointer to msg pointer array to be populated; needs
347 * to be allocated by caller
348 *
349 * Returns an array of message pointers associated with the cleaned
350 * descriptors. The pointers are to the original ctlq_msgs sent on the cleaned
351 * descriptors. The status will be returned for each; any messages that failed
352 * to send will have a non-zero status. The caller is expected to free original
353 * ctlq_msgs and free or reuse the DMA buffers.
354 */
idpf_ctlq_clean_sq(struct idpf_ctlq_info * cq,u16 * clean_count,struct idpf_ctlq_msg * msg_status[])355 int idpf_ctlq_clean_sq(struct idpf_ctlq_info *cq, u16 *clean_count,
356 struct idpf_ctlq_msg *msg_status[])
357 {
358 struct idpf_ctlq_desc *desc;
359 u16 i, num_to_clean;
360 u16 ntc, desc_err;
361
362 if (*clean_count == 0)
363 return 0;
364 if (*clean_count > cq->ring_size)
365 return -EBADR;
366
367 mutex_lock(&cq->cq_lock);
368
369 ntc = cq->next_to_clean;
370
371 num_to_clean = *clean_count;
372
373 for (i = 0; i < num_to_clean; i++) {
374 /* Fetch next descriptor and check if marked as done */
375 desc = IDPF_CTLQ_DESC(cq, ntc);
376 if (!(le16_to_cpu(desc->flags) & IDPF_CTLQ_FLAG_DD))
377 break;
378
379 /* Ensure no other fields are read until DD flag is checked */
380 dma_rmb();
381
382 /* strip off FW internal code */
383 desc_err = le16_to_cpu(desc->ret_val) & 0xff;
384
385 msg_status[i] = cq->bi.tx_msg[ntc];
386 msg_status[i]->status = desc_err;
387
388 cq->bi.tx_msg[ntc] = NULL;
389
390 /* Zero out any stale data */
391 memset(desc, 0, sizeof(*desc));
392
393 ntc++;
394 if (ntc == cq->ring_size)
395 ntc = 0;
396 }
397
398 cq->next_to_clean = ntc;
399
400 mutex_unlock(&cq->cq_lock);
401
402 /* Return number of descriptors actually cleaned */
403 *clean_count = i;
404
405 return 0;
406 }
407
408 /**
409 * idpf_ctlq_post_rx_buffs - post buffers to descriptor ring
410 * @hw: pointer to hw struct
411 * @cq: pointer to control queue handle
412 * @buff_count: (input|output) input is number of buffers caller is trying to
413 * return; output is number of buffers that were not posted
414 * @buffs: array of pointers to dma mem structs to be given to hardware
415 *
416 * Caller uses this function to return DMA buffers to the descriptor ring after
417 * consuming them; buff_count will be the number of buffers.
418 *
419 * Note: this function needs to be called after a receive call even
420 * if there are no DMA buffers to be returned, i.e. buff_count = 0,
421 * buffs = NULL to support direct commands
422 */
idpf_ctlq_post_rx_buffs(struct idpf_hw * hw,struct idpf_ctlq_info * cq,u16 * buff_count,struct idpf_dma_mem ** buffs)423 int idpf_ctlq_post_rx_buffs(struct idpf_hw *hw, struct idpf_ctlq_info *cq,
424 u16 *buff_count, struct idpf_dma_mem **buffs)
425 {
426 struct idpf_ctlq_desc *desc;
427 u16 ntp = cq->next_to_post;
428 bool buffs_avail = false;
429 u16 tbp = ntp + 1;
430 int i = 0;
431
432 if (*buff_count > cq->ring_size)
433 return -EBADR;
434
435 if (*buff_count > 0)
436 buffs_avail = true;
437
438 mutex_lock(&cq->cq_lock);
439
440 if (tbp >= cq->ring_size)
441 tbp = 0;
442
443 if (tbp == cq->next_to_clean)
444 /* Nothing to do */
445 goto post_buffs_out;
446
447 /* Post buffers for as many as provided or up until the last one used */
448 while (ntp != cq->next_to_clean) {
449 desc = IDPF_CTLQ_DESC(cq, ntp);
450
451 if (cq->bi.rx_buff[ntp])
452 goto fill_desc;
453 if (!buffs_avail) {
454 /* If the caller hasn't given us any buffers or
455 * there are none left, search the ring itself
456 * for an available buffer to move to this
457 * entry starting at the next entry in the ring
458 */
459 tbp = ntp + 1;
460
461 /* Wrap ring if necessary */
462 if (tbp >= cq->ring_size)
463 tbp = 0;
464
465 while (tbp != cq->next_to_clean) {
466 if (cq->bi.rx_buff[tbp]) {
467 cq->bi.rx_buff[ntp] =
468 cq->bi.rx_buff[tbp];
469 cq->bi.rx_buff[tbp] = NULL;
470
471 /* Found a buffer, no need to
472 * search anymore
473 */
474 break;
475 }
476
477 /* Wrap ring if necessary */
478 tbp++;
479 if (tbp >= cq->ring_size)
480 tbp = 0;
481 }
482
483 if (tbp == cq->next_to_clean)
484 goto post_buffs_out;
485 } else {
486 /* Give back pointer to DMA buffer */
487 cq->bi.rx_buff[ntp] = buffs[i];
488 i++;
489
490 if (i >= *buff_count)
491 buffs_avail = false;
492 }
493
494 fill_desc:
495 desc->flags =
496 cpu_to_le16(IDPF_CTLQ_FLAG_BUF | IDPF_CTLQ_FLAG_RD);
497
498 /* Post buffers to descriptor */
499 desc->datalen = cpu_to_le16(cq->bi.rx_buff[ntp]->size);
500 desc->params.indirect.addr_high =
501 cpu_to_le32(upper_32_bits(cq->bi.rx_buff[ntp]->pa));
502 desc->params.indirect.addr_low =
503 cpu_to_le32(lower_32_bits(cq->bi.rx_buff[ntp]->pa));
504
505 ntp++;
506 if (ntp == cq->ring_size)
507 ntp = 0;
508 }
509
510 post_buffs_out:
511 /* Only update tail if buffers were actually posted */
512 if (cq->next_to_post != ntp) {
513 if (ntp)
514 /* Update next_to_post to ntp - 1 since current ntp
515 * will not have a buffer
516 */
517 cq->next_to_post = ntp - 1;
518 else
519 /* Wrap to end of end ring since current ntp is 0 */
520 cq->next_to_post = cq->ring_size - 1;
521
522 dma_wmb();
523
524 wr32(hw, cq->reg.tail, cq->next_to_post);
525 }
526
527 mutex_unlock(&cq->cq_lock);
528
529 /* return the number of buffers that were not posted */
530 *buff_count = *buff_count - i;
531
532 return 0;
533 }
534
535 /**
536 * idpf_ctlq_recv - receive control queue message call back
537 * @cq: pointer to control queue handle to receive on
538 * @num_q_msg: (input|output) input number of messages that should be received;
539 * output number of messages actually received
540 * @q_msg: (output) array of received control queue messages on this q;
541 * needs to be pre-allocated by caller for as many messages as requested
542 *
543 * Called by interrupt handler or polling mechanism. Caller is expected
544 * to free buffers
545 */
idpf_ctlq_recv(struct idpf_ctlq_info * cq,u16 * num_q_msg,struct idpf_ctlq_msg * q_msg)546 int idpf_ctlq_recv(struct idpf_ctlq_info *cq, u16 *num_q_msg,
547 struct idpf_ctlq_msg *q_msg)
548 {
549 u16 num_to_clean, ntc, flags;
550 struct idpf_ctlq_desc *desc;
551 int err = 0;
552 u16 i;
553
554 /* take the lock before we start messing with the ring */
555 mutex_lock(&cq->cq_lock);
556
557 ntc = cq->next_to_clean;
558
559 num_to_clean = *num_q_msg;
560
561 for (i = 0; i < num_to_clean; i++) {
562 /* Fetch next descriptor and check if marked as done */
563 desc = IDPF_CTLQ_DESC(cq, ntc);
564 flags = le16_to_cpu(desc->flags);
565
566 if (!(flags & IDPF_CTLQ_FLAG_DD))
567 break;
568
569 /* Ensure no other fields are read until DD flag is checked */
570 dma_rmb();
571
572 q_msg[i].vmvf_type = (flags &
573 (IDPF_CTLQ_FLAG_FTYPE_VM |
574 IDPF_CTLQ_FLAG_FTYPE_PF)) >>
575 IDPF_CTLQ_FLAG_FTYPE_S;
576
577 if (flags & IDPF_CTLQ_FLAG_ERR)
578 err = -EBADMSG;
579
580 q_msg[i].cookie.mbx.chnl_opcode =
581 le32_to_cpu(desc->v_opcode_dtype);
582 q_msg[i].cookie.mbx.chnl_retval =
583 le32_to_cpu(desc->v_retval);
584
585 q_msg[i].opcode = le16_to_cpu(desc->opcode);
586 q_msg[i].data_len = le16_to_cpu(desc->datalen);
587 q_msg[i].status = le16_to_cpu(desc->ret_val);
588
589 if (desc->datalen) {
590 memcpy(q_msg[i].ctx.indirect.context,
591 &desc->params.indirect, IDPF_INDIRECT_CTX_SIZE);
592
593 /* Assign pointer to dma buffer to ctlq_msg array
594 * to be given to upper layer
595 */
596 q_msg[i].ctx.indirect.payload = cq->bi.rx_buff[ntc];
597
598 /* Zero out pointer to DMA buffer info;
599 * will be repopulated by post buffers API
600 */
601 cq->bi.rx_buff[ntc] = NULL;
602 } else {
603 memcpy(q_msg[i].ctx.direct, desc->params.raw,
604 IDPF_DIRECT_CTX_SIZE);
605 }
606
607 /* Zero out stale data in descriptor */
608 memset(desc, 0, sizeof(struct idpf_ctlq_desc));
609
610 ntc++;
611 if (ntc == cq->ring_size)
612 ntc = 0;
613 }
614
615 cq->next_to_clean = ntc;
616
617 mutex_unlock(&cq->cq_lock);
618
619 *num_q_msg = i;
620 if (*num_q_msg == 0)
621 err = -ENOMSG;
622
623 return err;
624 }
625