1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2016 Cavium, Inc.
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
18 #include <linux/pci.h>
19 #include <linux/netdevice.h>
20 #include <linux/vmalloc.h>
21 #include "liquidio_common.h"
22 #include "octeon_droq.h"
23 #include "octeon_iq.h"
24 #include "response_manager.h"
25 #include "octeon_device.h"
26 #include "octeon_main.h"
27 #include "octeon_network.h"
28 #include "cn66xx_regs.h"
29 #include "cn66xx_device.h"
30 #include "cn23xx_pf_device.h"
31 #include "cn23xx_vf_device.h"
33 /** Default configuration
34 * for CN66XX OCTEON Models.
36 static struct octeon_config default_cn66xx_conf = {
37 .card_type = LIO_210SV,
38 .card_name = LIO_210SV_NAME,
42 .max_iqs = CN6XXX_CFG_IO_QUEUES,
44 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
45 .instr_type = OCTEON_64BYTE_INSTR,
46 .db_min = CN6XXX_DB_MIN,
47 .db_timeout = CN6XXX_DB_TIMEOUT,
53 .max_oqs = CN6XXX_CFG_IO_QUEUES,
54 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
55 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
56 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
57 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
58 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
62 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_66XX,
63 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
64 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
65 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
67 /* For ethernet interface 0: Port cfg Attributes */
69 /* Max Txqs: Half for each of the two ports :max_iq/2 */
70 .max_txqs = MAX_TXQS_PER_INTF,
72 /* Actual configured value. Range could be: 1...max_txqs */
73 .num_txqs = DEF_TXQS_PER_INTF,
75 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
76 .max_rxqs = MAX_RXQS_PER_INTF,
78 /* Actual configured value. Range could be: 1...max_rxqs */
79 .num_rxqs = DEF_RXQS_PER_INTF,
81 /* Num of desc for rx rings */
82 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
84 /* Num of desc for tx rings */
85 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
87 /* SKB size, We need not change buf size even for Jumbo frames.
88 * Octeon can send jumbo frames in 4 consecutive descriptors,
90 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
92 .base_queue = BASE_QUEUE_NOT_REQUESTED,
98 /* Max Txqs: Half for each of the two ports :max_iq/2 */
99 .max_txqs = MAX_TXQS_PER_INTF,
101 /* Actual configured value. Range could be: 1...max_txqs */
102 .num_txqs = DEF_TXQS_PER_INTF,
104 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
105 .max_rxqs = MAX_RXQS_PER_INTF,
107 /* Actual configured value. Range could be: 1...max_rxqs */
108 .num_rxqs = DEF_RXQS_PER_INTF,
110 /* Num of desc for rx rings */
111 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
113 /* Num of desc for tx rings */
114 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
116 /* SKB size, We need not change buf size even for Jumbo frames.
117 * Octeon can send jumbo frames in 4 consecutive descriptors,
119 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
121 .base_queue = BASE_QUEUE_NOT_REQUESTED,
126 /** Miscellaneous attributes */
128 /* Host driver link query interval */
129 .oct_link_query_interval = 100,
131 /* Octeon link query interval */
132 .host_link_query_interval = 500,
134 .enable_sli_oq_bp = 0,
136 /* Control queue group */
142 /** Default configuration
143 * for CN68XX OCTEON Model.
146 static struct octeon_config default_cn68xx_conf = {
147 .card_type = LIO_410NV,
148 .card_name = LIO_410NV_NAME,
152 .max_iqs = CN6XXX_CFG_IO_QUEUES,
154 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
155 .instr_type = OCTEON_64BYTE_INSTR,
156 .db_min = CN6XXX_DB_MIN,
157 .db_timeout = CN6XXX_DB_TIMEOUT,
163 .max_oqs = CN6XXX_CFG_IO_QUEUES,
164 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
165 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
166 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
167 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
168 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
172 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX,
173 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
174 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
175 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
178 /* Max Txqs: Half for each of the two ports :max_iq/2 */
179 .max_txqs = MAX_TXQS_PER_INTF,
181 /* Actual configured value. Range could be: 1...max_txqs */
182 .num_txqs = DEF_TXQS_PER_INTF,
184 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
185 .max_rxqs = MAX_RXQS_PER_INTF,
187 /* Actual configured value. Range could be: 1...max_rxqs */
188 .num_rxqs = DEF_RXQS_PER_INTF,
190 /* Num of desc for rx rings */
191 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
193 /* Num of desc for tx rings */
194 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
196 /* SKB size, We need not change buf size even for Jumbo frames.
197 * Octeon can send jumbo frames in 4 consecutive descriptors,
199 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
201 .base_queue = BASE_QUEUE_NOT_REQUESTED,
207 /* Max Txqs: Half for each of the two ports :max_iq/2 */
208 .max_txqs = MAX_TXQS_PER_INTF,
210 /* Actual configured value. Range could be: 1...max_txqs */
211 .num_txqs = DEF_TXQS_PER_INTF,
213 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
214 .max_rxqs = MAX_RXQS_PER_INTF,
216 /* Actual configured value. Range could be: 1...max_rxqs */
217 .num_rxqs = DEF_RXQS_PER_INTF,
219 /* Num of desc for rx rings */
220 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
222 /* Num of desc for tx rings */
223 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
225 /* SKB size, We need not change buf size even for Jumbo frames.
226 * Octeon can send jumbo frames in 4 consecutive descriptors,
228 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
230 .base_queue = BASE_QUEUE_NOT_REQUESTED,
236 /* Max Txqs: Half for each of the two ports :max_iq/2 */
237 .max_txqs = MAX_TXQS_PER_INTF,
239 /* Actual configured value. Range could be: 1...max_txqs */
240 .num_txqs = DEF_TXQS_PER_INTF,
242 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
243 .max_rxqs = MAX_RXQS_PER_INTF,
245 /* Actual configured value. Range could be: 1...max_rxqs */
246 .num_rxqs = DEF_RXQS_PER_INTF,
248 /* Num of desc for rx rings */
249 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
251 /* Num of desc for tx rings */
252 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
254 /* SKB size, We need not change buf size even for Jumbo frames.
255 * Octeon can send jumbo frames in 4 consecutive descriptors,
257 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
259 .base_queue = BASE_QUEUE_NOT_REQUESTED,
265 /* Max Txqs: Half for each of the two ports :max_iq/2 */
266 .max_txqs = MAX_TXQS_PER_INTF,
268 /* Actual configured value. Range could be: 1...max_txqs */
269 .num_txqs = DEF_TXQS_PER_INTF,
271 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
272 .max_rxqs = MAX_RXQS_PER_INTF,
274 /* Actual configured value. Range could be: 1...max_rxqs */
275 .num_rxqs = DEF_RXQS_PER_INTF,
277 /* Num of desc for rx rings */
278 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
280 /* Num of desc for tx rings */
281 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
283 /* SKB size, We need not change buf size even for Jumbo frames.
284 * Octeon can send jumbo frames in 4 consecutive descriptors,
286 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
288 .base_queue = BASE_QUEUE_NOT_REQUESTED,
293 /** Miscellaneous attributes */
295 /* Host driver link query interval */
296 .oct_link_query_interval = 100,
298 /* Octeon link query interval */
299 .host_link_query_interval = 500,
301 .enable_sli_oq_bp = 0,
303 /* Control queue group */
309 /** Default configuration
310 * for CN68XX OCTEON Model.
312 static struct octeon_config default_cn68xx_210nv_conf = {
313 .card_type = LIO_210NV,
314 .card_name = LIO_210NV_NAME,
319 .max_iqs = CN6XXX_CFG_IO_QUEUES,
321 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
322 .instr_type = OCTEON_64BYTE_INSTR,
323 .db_min = CN6XXX_DB_MIN,
324 .db_timeout = CN6XXX_DB_TIMEOUT,
330 .max_oqs = CN6XXX_CFG_IO_QUEUES,
331 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
332 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
333 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
334 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
335 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
339 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX_210NV,
340 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
341 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
342 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
345 /* Max Txqs: Half for each of the two ports :max_iq/2 */
346 .max_txqs = MAX_TXQS_PER_INTF,
348 /* Actual configured value. Range could be: 1...max_txqs */
349 .num_txqs = DEF_TXQS_PER_INTF,
351 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
352 .max_rxqs = MAX_RXQS_PER_INTF,
354 /* Actual configured value. Range could be: 1...max_rxqs */
355 .num_rxqs = DEF_RXQS_PER_INTF,
357 /* Num of desc for rx rings */
358 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
360 /* Num of desc for tx rings */
361 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
363 /* SKB size, We need not change buf size even for Jumbo frames.
364 * Octeon can send jumbo frames in 4 consecutive descriptors,
366 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
368 .base_queue = BASE_QUEUE_NOT_REQUESTED,
374 /* Max Txqs: Half for each of the two ports :max_iq/2 */
375 .max_txqs = MAX_TXQS_PER_INTF,
377 /* Actual configured value. Range could be: 1...max_txqs */
378 .num_txqs = DEF_TXQS_PER_INTF,
380 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
381 .max_rxqs = MAX_RXQS_PER_INTF,
383 /* Actual configured value. Range could be: 1...max_rxqs */
384 .num_rxqs = DEF_RXQS_PER_INTF,
386 /* Num of desc for rx rings */
387 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
389 /* Num of desc for tx rings */
390 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
392 /* SKB size, We need not change buf size even for Jumbo frames.
393 * Octeon can send jumbo frames in 4 consecutive descriptors,
395 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
397 .base_queue = BASE_QUEUE_NOT_REQUESTED,
402 /** Miscellaneous attributes */
404 /* Host driver link query interval */
405 .oct_link_query_interval = 100,
407 /* Octeon link query interval */
408 .host_link_query_interval = 500,
410 .enable_sli_oq_bp = 0,
412 /* Control queue group */
418 static struct octeon_config default_cn23xx_conf = {
419 .card_type = LIO_23XX,
420 .card_name = LIO_23XX_NAME,
423 .max_iqs = CN23XX_CFG_IO_QUEUES,
424 .pending_list_size = (CN23XX_MAX_IQ_DESCRIPTORS *
425 CN23XX_CFG_IO_QUEUES),
426 .instr_type = OCTEON_64BYTE_INSTR,
427 .db_min = CN23XX_DB_MIN,
428 .db_timeout = CN23XX_DB_TIMEOUT,
429 .iq_intr_pkt = CN23XX_DEF_IQ_INTR_THRESHOLD,
434 .max_oqs = CN23XX_CFG_IO_QUEUES,
435 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
436 .pkts_per_intr = CN23XX_OQ_PKTSPER_INTR,
437 .refill_threshold = CN23XX_OQ_REFIL_THRESHOLD,
438 .oq_intr_pkt = CN23XX_OQ_INTR_PKT,
439 .oq_intr_time = CN23XX_OQ_INTR_TIME,
442 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_23XX,
443 .num_def_rx_descs = CN23XX_MAX_OQ_DESCRIPTORS,
444 .num_def_tx_descs = CN23XX_MAX_IQ_DESCRIPTORS,
445 .def_rx_buf_size = CN23XX_OQ_BUF_SIZE,
447 /* For ethernet interface 0: Port cfg Attributes */
449 /* Max Txqs: Half for each of the two ports :max_iq/2 */
450 .max_txqs = MAX_TXQS_PER_INTF,
452 /* Actual configured value. Range could be: 1...max_txqs */
453 .num_txqs = DEF_TXQS_PER_INTF,
455 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
456 .max_rxqs = MAX_RXQS_PER_INTF,
458 /* Actual configured value. Range could be: 1...max_rxqs */
459 .num_rxqs = DEF_RXQS_PER_INTF,
461 /* Num of desc for rx rings */
462 .num_rx_descs = CN23XX_MAX_OQ_DESCRIPTORS,
464 /* Num of desc for tx rings */
465 .num_tx_descs = CN23XX_MAX_IQ_DESCRIPTORS,
467 /* SKB size, We need not change buf size even for Jumbo frames.
468 * Octeon can send jumbo frames in 4 consecutive descriptors,
470 .rx_buf_size = CN23XX_OQ_BUF_SIZE,
472 .base_queue = BASE_QUEUE_NOT_REQUESTED,
478 /* Max Txqs: Half for each of the two ports :max_iq/2 */
479 .max_txqs = MAX_TXQS_PER_INTF,
481 /* Actual configured value. Range could be: 1...max_txqs */
482 .num_txqs = DEF_TXQS_PER_INTF,
484 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
485 .max_rxqs = MAX_RXQS_PER_INTF,
487 /* Actual configured value. Range could be: 1...max_rxqs */
488 .num_rxqs = DEF_RXQS_PER_INTF,
490 /* Num of desc for rx rings */
491 .num_rx_descs = CN23XX_MAX_OQ_DESCRIPTORS,
493 /* Num of desc for tx rings */
494 .num_tx_descs = CN23XX_MAX_IQ_DESCRIPTORS,
496 /* SKB size, We need not change buf size even for Jumbo frames.
497 * Octeon can send jumbo frames in 4 consecutive descriptors,
499 .rx_buf_size = CN23XX_OQ_BUF_SIZE,
501 .base_queue = BASE_QUEUE_NOT_REQUESTED,
507 /* Host driver link query interval */
508 .oct_link_query_interval = 100,
510 /* Octeon link query interval */
511 .host_link_query_interval = 500,
513 .enable_sli_oq_bp = 0,
515 /* Control queue group */
520 static struct octeon_config_ptr {
522 } oct_conf_info[MAX_OCTEON_DEVICES] = {
524 OCTEON_CONFIG_TYPE_DEFAULT,
526 OCTEON_CONFIG_TYPE_DEFAULT,
528 OCTEON_CONFIG_TYPE_DEFAULT,
530 OCTEON_CONFIG_TYPE_DEFAULT,
534 static char oct_dev_state_str[OCT_DEV_STATES + 1][32] = {
535 "BEGIN", "PCI-MAP-DONE", "DISPATCH-INIT-DONE",
536 "IQ-INIT-DONE", "SCBUFF-POOL-INIT-DONE", "RESPLIST-INIT-DONE",
537 "DROQ-INIT-DONE", "IO-QUEUES-INIT-DONE", "CONSOLE-INIT-DONE",
538 "HOST-READY", "CORE-READY", "RUNNING", "IN-RESET",
542 static char oct_dev_app_str[CVM_DRV_APP_COUNT + 1][32] = {
543 "BASE", "NIC", "UNKNOWN"};
545 static struct octeon_device *octeon_device[MAX_OCTEON_DEVICES];
546 static u32 octeon_device_count;
548 static struct octeon_core_setup core_setup[MAX_OCTEON_DEVICES];
550 static void oct_set_config_info(int oct_id, int conf_type)
552 if (conf_type < 0 || conf_type > (NUM_OCTEON_CONFS - 1))
553 conf_type = OCTEON_CONFIG_TYPE_DEFAULT;
554 oct_conf_info[oct_id].conf_type = conf_type;
557 void octeon_init_device_list(int conf_type)
561 memset(octeon_device, 0, (sizeof(void *) * MAX_OCTEON_DEVICES));
562 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
563 oct_set_config_info(i, conf_type);
566 static void *__retrieve_octeon_config_info(struct octeon_device *oct,
569 u32 oct_id = oct->octeon_id;
572 switch (oct_conf_info[oct_id].conf_type) {
573 case OCTEON_CONFIG_TYPE_DEFAULT:
574 if (oct->chip_id == OCTEON_CN66XX) {
575 ret = (void *)&default_cn66xx_conf;
576 } else if ((oct->chip_id == OCTEON_CN68XX) &&
577 (card_type == LIO_210NV)) {
578 ret = (void *)&default_cn68xx_210nv_conf;
579 } else if ((oct->chip_id == OCTEON_CN68XX) &&
580 (card_type == LIO_410NV)) {
581 ret = (void *)&default_cn68xx_conf;
582 } else if (oct->chip_id == OCTEON_CN23XX_PF_VID) {
583 ret = (void *)&default_cn23xx_conf;
592 static int __verify_octeon_config_info(struct octeon_device *oct, void *conf)
594 switch (oct->chip_id) {
597 return lio_validate_cn6xxx_config_info(oct, conf);
598 case OCTEON_CN23XX_PF_VID:
607 void *oct_get_config_info(struct octeon_device *oct, u16 card_type)
611 conf = __retrieve_octeon_config_info(oct, card_type);
615 if (__verify_octeon_config_info(oct, conf)) {
616 dev_err(&oct->pci_dev->dev, "Configuration verification failed\n");
623 char *lio_get_state_string(atomic_t *state_ptr)
625 s32 istate = (s32)atomic_read(state_ptr);
627 if (istate > OCT_DEV_STATES || istate < 0)
628 return oct_dev_state_str[OCT_DEV_STATE_INVALID];
629 return oct_dev_state_str[istate];
632 static char *get_oct_app_string(u32 app_mode)
634 if (app_mode <= CVM_DRV_APP_END)
635 return oct_dev_app_str[app_mode - CVM_DRV_APP_START];
636 return oct_dev_app_str[CVM_DRV_INVALID_APP - CVM_DRV_APP_START];
639 void octeon_free_device_mem(struct octeon_device *oct)
643 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
644 if (oct->io_qmask.oq & BIT_ULL(i))
648 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
649 if (oct->io_qmask.iq & BIT_ULL(i))
650 vfree(oct->instr_queue[i]);
656 octeon_device[i] = NULL;
657 octeon_device_count--;
660 static struct octeon_device *octeon_allocate_device_mem(u32 pci_id,
663 struct octeon_device *oct;
665 u32 octdevsize = 0, configsize = 0, size;
670 configsize = sizeof(struct octeon_cn6xxx);
673 case OCTEON_CN23XX_PF_VID:
674 configsize = sizeof(struct octeon_cn23xx_pf);
676 case OCTEON_CN23XX_VF_VID:
677 configsize = sizeof(struct octeon_cn23xx_vf);
680 pr_err("%s: Unknown PCI Device: 0x%x\n",
686 if (configsize & 0x7)
687 configsize += (8 - (configsize & 0x7));
689 octdevsize = sizeof(struct octeon_device);
690 if (octdevsize & 0x7)
691 octdevsize += (8 - (octdevsize & 0x7));
694 priv_size += (8 - (priv_size & 0x7));
696 size = octdevsize + priv_size + configsize +
697 (sizeof(struct octeon_dispatch) * DISPATCH_LIST_SIZE);
703 memset(buf, 0, size);
705 oct = (struct octeon_device *)buf;
706 oct->priv = (void *)(buf + octdevsize);
707 oct->chip = (void *)(buf + octdevsize + priv_size);
708 oct->dispatch.dlist = (struct octeon_dispatch *)
709 (buf + octdevsize + priv_size + configsize);
714 struct octeon_device *octeon_allocate_device(u32 pci_id,
718 struct octeon_device *oct = NULL;
720 for (oct_idx = 0; oct_idx < MAX_OCTEON_DEVICES; oct_idx++)
721 if (!octeon_device[oct_idx])
724 if (oct_idx == MAX_OCTEON_DEVICES)
727 oct = octeon_allocate_device_mem(pci_id, priv_size);
731 spin_lock_init(&oct->pci_win_lock);
732 spin_lock_init(&oct->mem_access_lock);
734 octeon_device_count++;
735 octeon_device[oct_idx] = oct;
737 oct->octeon_id = oct_idx;
738 snprintf(oct->device_name, sizeof(oct->device_name),
739 "LiquidIO%d", (oct->octeon_id));
745 octeon_allocate_ioq_vector(struct octeon_device *oct)
748 struct octeon_ioq_vector *ioq_vector;
752 if (OCTEON_CN23XX_PF(oct))
753 num_ioqs = oct->sriov_info.num_pf_rings;
754 size = sizeof(struct octeon_ioq_vector) * num_ioqs;
756 oct->ioq_vector = vmalloc(size);
757 if (!oct->ioq_vector)
759 memset(oct->ioq_vector, 0, size);
760 for (i = 0; i < num_ioqs; i++) {
761 ioq_vector = &oct->ioq_vector[i];
762 ioq_vector->oct_dev = oct;
763 ioq_vector->iq_index = i;
764 ioq_vector->droq_index = i;
765 ioq_vector->mbox = oct->mbox[i];
767 cpu_num = i % num_online_cpus();
768 cpumask_set_cpu(cpu_num, &ioq_vector->affinity_mask);
770 if (oct->chip_id == OCTEON_CN23XX_PF_VID)
771 ioq_vector->ioq_num = i + oct->sriov_info.pf_srn;
773 ioq_vector->ioq_num = i;
779 octeon_free_ioq_vector(struct octeon_device *oct)
781 vfree(oct->ioq_vector);
784 /* this function is only for setting up the first queue */
785 int octeon_setup_instr_queues(struct octeon_device *oct)
789 union oct_txpciq txpciq;
790 int numa_node = cpu_to_node(iq_no % num_online_cpus());
792 if (OCTEON_CN6XXX(oct))
794 CFG_GET_NUM_DEF_TX_DESCS(CHIP_CONF(oct, cn6xxx));
795 else if (OCTEON_CN23XX_PF(oct))
796 num_descs = CFG_GET_NUM_DEF_TX_DESCS(CHIP_CONF(oct, cn23xx_pf));
800 oct->instr_queue[0] = vmalloc_node(sizeof(*oct->instr_queue[0]),
802 if (!oct->instr_queue[0])
803 oct->instr_queue[0] =
804 vmalloc(sizeof(struct octeon_instr_queue));
805 if (!oct->instr_queue[0])
807 memset(oct->instr_queue[0], 0, sizeof(struct octeon_instr_queue));
808 oct->instr_queue[0]->q_index = 0;
809 oct->instr_queue[0]->app_ctx = (void *)(size_t)0;
810 oct->instr_queue[0]->ifidx = 0;
812 txpciq.s.q_no = iq_no;
813 txpciq.s.pkind = oct->pfvf_hsword.pkind;
814 txpciq.s.use_qpg = 0;
816 if (octeon_init_instr_queue(oct, txpciq, num_descs)) {
817 /* prevent memory leak */
818 vfree(oct->instr_queue[0]);
819 oct->instr_queue[0] = NULL;
827 int octeon_setup_output_queues(struct octeon_device *oct)
832 int numa_node = cpu_to_node(oq_no % num_online_cpus());
834 if (OCTEON_CN6XXX(oct)) {
836 CFG_GET_NUM_DEF_RX_DESCS(CHIP_CONF(oct, cn6xxx));
838 CFG_GET_DEF_RX_BUF_SIZE(CHIP_CONF(oct, cn6xxx));
839 } else if (OCTEON_CN23XX_PF(oct)) {
840 num_descs = CFG_GET_NUM_DEF_RX_DESCS(CHIP_CONF(oct, cn23xx_pf));
841 desc_size = CFG_GET_DEF_RX_BUF_SIZE(CHIP_CONF(oct, cn23xx_pf));
844 oct->droq[0] = vmalloc_node(sizeof(*oct->droq[0]), numa_node);
846 oct->droq[0] = vmalloc(sizeof(*oct->droq[0]));
850 if (octeon_init_droq(oct, oq_no, num_descs, desc_size, NULL)) {
851 vfree(oct->droq[oq_no]);
852 oct->droq[oq_no] = NULL;
860 void octeon_set_io_queues_off(struct octeon_device *oct)
862 if (OCTEON_CN6XXX(oct)) {
863 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
864 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
868 void octeon_set_droq_pkt_op(struct octeon_device *oct,
874 /* Disable the i/p and o/p queues for this Octeon. */
875 if (OCTEON_CN6XXX(oct)) {
876 reg_val = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
879 reg_val = reg_val | (1 << q_no);
881 reg_val = reg_val & (~(1 << q_no));
883 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, reg_val);
887 int octeon_init_dispatch_list(struct octeon_device *oct)
891 oct->dispatch.count = 0;
893 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
894 oct->dispatch.dlist[i].opcode = 0;
895 INIT_LIST_HEAD(&oct->dispatch.dlist[i].list);
898 for (i = 0; i <= REQTYPE_LAST; i++)
899 octeon_register_reqtype_free_fn(oct, i, NULL);
901 spin_lock_init(&oct->dispatch.lock);
906 void octeon_delete_dispatch_list(struct octeon_device *oct)
909 struct list_head freelist, *temp, *tmp2;
911 INIT_LIST_HEAD(&freelist);
913 spin_lock_bh(&oct->dispatch.lock);
915 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
916 struct list_head *dispatch;
918 dispatch = &oct->dispatch.dlist[i].list;
919 while (dispatch->next != dispatch) {
920 temp = dispatch->next;
922 list_add_tail(temp, &freelist);
925 oct->dispatch.dlist[i].opcode = 0;
928 oct->dispatch.count = 0;
930 spin_unlock_bh(&oct->dispatch.lock);
932 list_for_each_safe(temp, tmp2, &freelist) {
939 octeon_get_dispatch(struct octeon_device *octeon_dev, u16 opcode,
943 struct list_head *dispatch;
944 octeon_dispatch_fn_t fn = NULL;
945 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
947 idx = combined_opcode & OCTEON_OPCODE_MASK;
949 spin_lock_bh(&octeon_dev->dispatch.lock);
951 if (octeon_dev->dispatch.count == 0) {
952 spin_unlock_bh(&octeon_dev->dispatch.lock);
956 if (!(octeon_dev->dispatch.dlist[idx].opcode)) {
957 spin_unlock_bh(&octeon_dev->dispatch.lock);
961 if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
962 fn = octeon_dev->dispatch.dlist[idx].dispatch_fn;
964 list_for_each(dispatch,
965 &octeon_dev->dispatch.dlist[idx].list) {
966 if (((struct octeon_dispatch *)dispatch)->opcode ==
968 fn = ((struct octeon_dispatch *)
969 dispatch)->dispatch_fn;
975 spin_unlock_bh(&octeon_dev->dispatch.lock);
979 /* octeon_register_dispatch_fn
981 * octeon_id - id of the octeon device.
982 * opcode - opcode for which driver should call the registered function
983 * subcode - subcode for which driver should call the registered function
984 * fn - The function to call when a packet with "opcode" arrives in
985 * octeon output queues.
986 * fn_arg - The argument to be passed when calling function "fn".
988 * Registers a function and its argument to be called when a packet
989 * arrives in Octeon output queues with "opcode".
997 octeon_register_dispatch_fn(struct octeon_device *oct,
1000 octeon_dispatch_fn_t fn, void *fn_arg)
1003 octeon_dispatch_fn_t pfn;
1004 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
1006 idx = combined_opcode & OCTEON_OPCODE_MASK;
1008 spin_lock_bh(&oct->dispatch.lock);
1009 /* Add dispatch function to first level of lookup table */
1010 if (oct->dispatch.dlist[idx].opcode == 0) {
1011 oct->dispatch.dlist[idx].opcode = combined_opcode;
1012 oct->dispatch.dlist[idx].dispatch_fn = fn;
1013 oct->dispatch.dlist[idx].arg = fn_arg;
1014 oct->dispatch.count++;
1015 spin_unlock_bh(&oct->dispatch.lock);
1019 spin_unlock_bh(&oct->dispatch.lock);
1021 /* Check if there was a function already registered for this
1024 pfn = octeon_get_dispatch(oct, opcode, subcode);
1026 struct octeon_dispatch *dispatch;
1028 dev_dbg(&oct->pci_dev->dev,
1029 "Adding opcode to dispatch list linked list\n");
1030 dispatch = (struct octeon_dispatch *)
1031 vmalloc(sizeof(struct octeon_dispatch));
1033 dev_err(&oct->pci_dev->dev,
1034 "No memory to add dispatch function\n");
1037 dispatch->opcode = combined_opcode;
1038 dispatch->dispatch_fn = fn;
1039 dispatch->arg = fn_arg;
1041 /* Add dispatch function to linked list of fn ptrs
1042 * at the hashed index.
1044 spin_lock_bh(&oct->dispatch.lock);
1045 list_add(&dispatch->list, &oct->dispatch.dlist[idx].list);
1046 oct->dispatch.count++;
1047 spin_unlock_bh(&oct->dispatch.lock);
1050 dev_err(&oct->pci_dev->dev,
1051 "Found previously registered dispatch fn for opcode/subcode: %x/%x\n",
1059 int octeon_core_drv_init(struct octeon_recv_info *recv_info, void *buf)
1063 struct octeon_device *oct = (struct octeon_device *)buf;
1064 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
1065 struct octeon_core_setup *cs = NULL;
1066 u32 num_nic_ports = 0;
1068 if (OCTEON_CN6XXX(oct))
1070 CFG_GET_NUM_NIC_PORTS(CHIP_CONF(oct, cn6xxx));
1071 else if (OCTEON_CN23XX_PF(oct))
1073 CFG_GET_NUM_NIC_PORTS(CHIP_CONF(oct, cn23xx_pf));
1075 if (atomic_read(&oct->status) >= OCT_DEV_RUNNING) {
1076 dev_err(&oct->pci_dev->dev, "Received CORE OK when device state is 0x%x\n",
1077 atomic_read(&oct->status));
1078 goto core_drv_init_err;
1083 (u32)recv_pkt->rh.r_core_drv_init.app_mode),
1084 sizeof(app_name) - 1);
1085 oct->app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
1086 if (recv_pkt->rh.r_core_drv_init.app_mode == CVM_DRV_NIC_APP) {
1087 oct->fw_info.max_nic_ports =
1088 (u32)recv_pkt->rh.r_core_drv_init.max_nic_ports;
1089 oct->fw_info.num_gmx_ports =
1090 (u32)recv_pkt->rh.r_core_drv_init.num_gmx_ports;
1093 if (oct->fw_info.max_nic_ports < num_nic_ports) {
1094 dev_err(&oct->pci_dev->dev,
1095 "Config has more ports than firmware allows (%d > %d).\n",
1096 num_nic_ports, oct->fw_info.max_nic_ports);
1097 goto core_drv_init_err;
1099 oct->fw_info.app_cap_flags = recv_pkt->rh.r_core_drv_init.app_cap_flags;
1100 oct->fw_info.app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
1101 oct->pfvf_hsword.app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
1103 oct->pfvf_hsword.pkind = recv_pkt->rh.r_core_drv_init.pkind;
1105 for (i = 0; i < oct->num_iqs; i++)
1106 oct->instr_queue[i]->txpciq.s.pkind = oct->pfvf_hsword.pkind;
1108 atomic_set(&oct->status, OCT_DEV_CORE_OK);
1110 cs = &core_setup[oct->octeon_id];
1112 if (recv_pkt->buffer_size[0] != sizeof(*cs)) {
1113 dev_dbg(&oct->pci_dev->dev, "Core setup bytes expected %u found %d\n",
1115 recv_pkt->buffer_size[0]);
1118 memcpy(cs, get_rbd(recv_pkt->buffer_ptr[0]), sizeof(*cs));
1119 strncpy(oct->boardinfo.name, cs->boardname, OCT_BOARD_NAME);
1120 strncpy(oct->boardinfo.serial_number, cs->board_serial_number,
1123 octeon_swap_8B_data((u64 *)cs, (sizeof(*cs) >> 3));
1125 oct->boardinfo.major = cs->board_rev_major;
1126 oct->boardinfo.minor = cs->board_rev_minor;
1128 dev_info(&oct->pci_dev->dev,
1129 "Running %s (%llu Hz)\n",
1130 app_name, CVM_CAST64(cs->corefreq));
1133 for (i = 0; i < recv_pkt->buffer_count; i++)
1134 recv_buffer_free(recv_pkt->buffer_ptr[i]);
1135 octeon_free_recv_info(recv_info);
1139 int octeon_get_tx_qsize(struct octeon_device *oct, u32 q_no)
1142 if (oct && (q_no < MAX_OCTEON_INSTR_QUEUES(oct)) &&
1143 (oct->io_qmask.iq & BIT_ULL(q_no)))
1144 return oct->instr_queue[q_no]->max_count;
1149 int octeon_get_rx_qsize(struct octeon_device *oct, u32 q_no)
1151 if (oct && (q_no < MAX_OCTEON_OUTPUT_QUEUES(oct)) &&
1152 (oct->io_qmask.oq & BIT_ULL(q_no)))
1153 return oct->droq[q_no]->max_count;
1157 /* Retruns the host firmware handshake OCTEON specific configuration */
1158 struct octeon_config *octeon_get_conf(struct octeon_device *oct)
1160 struct octeon_config *default_oct_conf = NULL;
1162 /* check the OCTEON Device model & return the corresponding octeon
1166 if (OCTEON_CN6XXX(oct)) {
1168 (struct octeon_config *)(CHIP_CONF(oct, cn6xxx));
1169 } else if (OCTEON_CN23XX_PF(oct)) {
1170 default_oct_conf = (struct octeon_config *)
1171 (CHIP_CONF(oct, cn23xx_pf));
1173 return default_oct_conf;
1176 /* scratch register address is same in all the OCT-II and CN70XX models */
1177 #define CNXX_SLI_SCRATCH1 0x3C0
1179 /** Get the octeon device pointer.
1180 * @param octeon_id - The id for which the octeon device pointer is required.
1181 * @return Success: Octeon device pointer.
1182 * @return Failure: NULL.
1184 struct octeon_device *lio_get_device(u32 octeon_id)
1186 if (octeon_id >= MAX_OCTEON_DEVICES)
1189 return octeon_device[octeon_id];
1192 u64 lio_pci_readq(struct octeon_device *oct, u64 addr)
1195 unsigned long flags;
1198 spin_lock_irqsave(&oct->pci_win_lock, flags);
1200 /* The windowed read happens when the LSB of the addr is written.
1201 * So write MSB first
1203 addrhi = (addr >> 32);
1204 if ((oct->chip_id == OCTEON_CN66XX) ||
1205 (oct->chip_id == OCTEON_CN68XX) ||
1206 (oct->chip_id == OCTEON_CN23XX_PF_VID))
1207 addrhi |= 0x00060000;
1208 writel(addrhi, oct->reg_list.pci_win_rd_addr_hi);
1210 /* Read back to preserve ordering of writes */
1211 val32 = readl(oct->reg_list.pci_win_rd_addr_hi);
1213 writel(addr & 0xffffffff, oct->reg_list.pci_win_rd_addr_lo);
1214 val32 = readl(oct->reg_list.pci_win_rd_addr_lo);
1216 val64 = readq(oct->reg_list.pci_win_rd_data);
1218 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1223 void lio_pci_writeq(struct octeon_device *oct,
1228 unsigned long flags;
1230 spin_lock_irqsave(&oct->pci_win_lock, flags);
1232 writeq(addr, oct->reg_list.pci_win_wr_addr);
1234 /* The write happens when the LSB is written. So write MSB first. */
1235 writel(val >> 32, oct->reg_list.pci_win_wr_data_hi);
1236 /* Read the MSB to ensure ordering of writes. */
1237 val32 = readl(oct->reg_list.pci_win_wr_data_hi);
1239 writel(val & 0xffffffff, oct->reg_list.pci_win_wr_data_lo);
1241 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1244 int octeon_mem_access_ok(struct octeon_device *oct)
1246 u64 access_okay = 0;
1249 /* Check to make sure a DDR interface is enabled */
1250 if (OCTEON_CN23XX_PF(oct)) {
1251 lmc0_reset_ctl = lio_pci_readq(oct, CN23XX_LMC0_RESET_CTL);
1253 (lmc0_reset_ctl & CN23XX_LMC0_RESET_CTL_DDR3RST_MASK);
1255 lmc0_reset_ctl = lio_pci_readq(oct, CN6XXX_LMC0_RESET_CTL);
1257 (lmc0_reset_ctl & CN6XXX_LMC0_RESET_CTL_DDR3RST_MASK);
1260 return access_okay ? 0 : 1;
1263 int octeon_wait_for_ddr_init(struct octeon_device *oct, u32 *timeout)
1271 for (ms = 0; (ret != 0) && ((*timeout == 0) || (ms <= *timeout));
1273 ret = octeon_mem_access_ok(oct);
1277 schedule_timeout_uninterruptible(HZ / 10);
1283 /** Get the octeon id assigned to the octeon device passed as argument.
1284 * This function is exported to other modules.
1285 * @param dev - octeon device pointer passed as a void *.
1286 * @return octeon device id
1288 int lio_get_device_id(void *dev)
1290 struct octeon_device *octeon_dev = (struct octeon_device *)dev;
1293 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
1294 if (octeon_device[i] == octeon_dev)
1295 return octeon_dev->octeon_id;
1299 void lio_enable_irq(struct octeon_droq *droq, struct octeon_instr_queue *iq)
1302 struct octeon_device *oct = NULL;
1304 /* the whole thing needs to be atomic, ideally */
1306 spin_lock_bh(&droq->lock);
1307 writel(droq->pkt_count, droq->pkts_sent_reg);
1308 droq->pkt_count = 0;
1309 spin_unlock_bh(&droq->lock);
1310 oct = droq->oct_dev;
1313 spin_lock_bh(&iq->lock);
1314 writel(iq->pkt_in_done, iq->inst_cnt_reg);
1315 iq->pkt_in_done = 0;
1316 spin_unlock_bh(&iq->lock);
1319 /*write resend. Writing RESEND in SLI_PKTX_CNTS should be enough
1320 *to trigger tx interrupts as well, if they are pending.
1322 if (oct && OCTEON_CN23XX_PF(oct)) {
1324 writeq(CN23XX_INTR_RESEND, droq->pkts_sent_reg);
1325 /*we race with firmrware here. read and write the IN_DONE_CNTS*/
1327 instr_cnt = readq(iq->inst_cnt_reg);
1328 writeq(((instr_cnt & 0xFFFFFFFF00000000ULL) |
1329 CN23XX_INTR_RESEND),