blob: 541137a60145a27271abdc22133bdb0253f17424 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2* Author: Cavium, Inc.
3*
4* Contact: support@cavium.com
5* Please include "LiquidIO" in the subject.
6*
7* Copyright (c) 2003-2015 Cavium, Inc.
8*
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.
12*
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
17* details.
18*
19* This file may also be available under a different license from Cavium.
20* Contact Cavium, Inc. for more information
21**********************************************************************/
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070022#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include <linux/netdevice.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070024#include <linux/vmalloc.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070025#include "liquidio_common.h"
26#include "octeon_droq.h"
27#include "octeon_iq.h"
28#include "response_manager.h"
29#include "octeon_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070030#include "octeon_main.h"
31#include "octeon_network.h"
32#include "cn66xx_regs.h"
33#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070034
35/** Default configuration
36 * for CN66XX OCTEON Models.
37 */
38static struct octeon_config default_cn66xx_conf = {
39 .card_type = LIO_210SV,
40 .card_name = LIO_210SV_NAME,
41
42 /** IQ attributes */
43 .iq = {
44 .max_iqs = CN6XXX_CFG_IO_QUEUES,
45 .pending_list_size =
46 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
47 .instr_type = OCTEON_64BYTE_INSTR,
48 .db_min = CN6XXX_DB_MIN,
49 .db_timeout = CN6XXX_DB_TIMEOUT,
50 }
51 ,
52
53 /** OQ attributes */
54 .oq = {
55 .max_oqs = CN6XXX_CFG_IO_QUEUES,
56 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
57 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
58 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
59 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
60 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
61 }
62 ,
63
64 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_66XX,
65 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
66 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
67 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
68
69 /* For ethernet interface 0: Port cfg Attributes */
70 .nic_if_cfg[0] = {
71 /* Max Txqs: Half for each of the two ports :max_iq/2 */
72 .max_txqs = MAX_TXQS_PER_INTF,
73
74 /* Actual configured value. Range could be: 1...max_txqs */
75 .num_txqs = DEF_TXQS_PER_INTF,
76
77 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
78 .max_rxqs = MAX_RXQS_PER_INTF,
79
80 /* Actual configured value. Range could be: 1...max_rxqs */
81 .num_rxqs = DEF_RXQS_PER_INTF,
82
83 /* Num of desc for rx rings */
84 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
85
86 /* Num of desc for tx rings */
87 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
88
89 /* SKB size, We need not change buf size even for Jumbo frames.
90 * Octeon can send jumbo frames in 4 consecutive descriptors,
91 */
92 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
93
94 .base_queue = BASE_QUEUE_NOT_REQUESTED,
95
96 .gmx_port_id = 0,
97 },
98
99 .nic_if_cfg[1] = {
100 /* Max Txqs: Half for each of the two ports :max_iq/2 */
101 .max_txqs = MAX_TXQS_PER_INTF,
102
103 /* Actual configured value. Range could be: 1...max_txqs */
104 .num_txqs = DEF_TXQS_PER_INTF,
105
106 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
107 .max_rxqs = MAX_RXQS_PER_INTF,
108
109 /* Actual configured value. Range could be: 1...max_rxqs */
110 .num_rxqs = DEF_RXQS_PER_INTF,
111
112 /* Num of desc for rx rings */
113 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
114
115 /* Num of desc for tx rings */
116 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
117
118 /* SKB size, We need not change buf size even for Jumbo frames.
119 * Octeon can send jumbo frames in 4 consecutive descriptors,
120 */
121 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
122
123 .base_queue = BASE_QUEUE_NOT_REQUESTED,
124
125 .gmx_port_id = 1,
126 },
127
128 /** Miscellaneous attributes */
129 .misc = {
130 /* Host driver link query interval */
131 .oct_link_query_interval = 100,
132
133 /* Octeon link query interval */
134 .host_link_query_interval = 500,
135
136 .enable_sli_oq_bp = 0,
137
138 /* Control queue group */
139 .ctrlq_grp = 1,
140 }
141 ,
142};
143
144/** Default configuration
145 * for CN68XX OCTEON Model.
146 */
147
148static struct octeon_config default_cn68xx_conf = {
149 .card_type = LIO_410NV,
150 .card_name = LIO_410NV_NAME,
151
152 /** IQ attributes */
153 .iq = {
154 .max_iqs = CN6XXX_CFG_IO_QUEUES,
155 .pending_list_size =
156 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
157 .instr_type = OCTEON_64BYTE_INSTR,
158 .db_min = CN6XXX_DB_MIN,
159 .db_timeout = CN6XXX_DB_TIMEOUT,
160 }
161 ,
162
163 /** OQ attributes */
164 .oq = {
165 .max_oqs = CN6XXX_CFG_IO_QUEUES,
166 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
167 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
168 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
169 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
170 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
171 }
172 ,
173
174 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX,
175 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
176 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
177 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
178
179 .nic_if_cfg[0] = {
180 /* Max Txqs: Half for each of the two ports :max_iq/2 */
181 .max_txqs = MAX_TXQS_PER_INTF,
182
183 /* Actual configured value. Range could be: 1...max_txqs */
184 .num_txqs = DEF_TXQS_PER_INTF,
185
186 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
187 .max_rxqs = MAX_RXQS_PER_INTF,
188
189 /* Actual configured value. Range could be: 1...max_rxqs */
190 .num_rxqs = DEF_RXQS_PER_INTF,
191
192 /* Num of desc for rx rings */
193 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
194
195 /* Num of desc for tx rings */
196 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
197
198 /* SKB size, We need not change buf size even for Jumbo frames.
199 * Octeon can send jumbo frames in 4 consecutive descriptors,
200 */
201 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
202
203 .base_queue = BASE_QUEUE_NOT_REQUESTED,
204
205 .gmx_port_id = 0,
206 },
207
208 .nic_if_cfg[1] = {
209 /* Max Txqs: Half for each of the two ports :max_iq/2 */
210 .max_txqs = MAX_TXQS_PER_INTF,
211
212 /* Actual configured value. Range could be: 1...max_txqs */
213 .num_txqs = DEF_TXQS_PER_INTF,
214
215 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
216 .max_rxqs = MAX_RXQS_PER_INTF,
217
218 /* Actual configured value. Range could be: 1...max_rxqs */
219 .num_rxqs = DEF_RXQS_PER_INTF,
220
221 /* Num of desc for rx rings */
222 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
223
224 /* Num of desc for tx rings */
225 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
226
227 /* SKB size, We need not change buf size even for Jumbo frames.
228 * Octeon can send jumbo frames in 4 consecutive descriptors,
229 */
230 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
231
232 .base_queue = BASE_QUEUE_NOT_REQUESTED,
233
234 .gmx_port_id = 1,
235 },
236
237 .nic_if_cfg[2] = {
238 /* Max Txqs: Half for each of the two ports :max_iq/2 */
239 .max_txqs = MAX_TXQS_PER_INTF,
240
241 /* Actual configured value. Range could be: 1...max_txqs */
242 .num_txqs = DEF_TXQS_PER_INTF,
243
244 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
245 .max_rxqs = MAX_RXQS_PER_INTF,
246
247 /* Actual configured value. Range could be: 1...max_rxqs */
248 .num_rxqs = DEF_RXQS_PER_INTF,
249
250 /* Num of desc for rx rings */
251 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
252
253 /* Num of desc for tx rings */
254 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
255
256 /* SKB size, We need not change buf size even for Jumbo frames.
257 * Octeon can send jumbo frames in 4 consecutive descriptors,
258 */
259 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
260
261 .base_queue = BASE_QUEUE_NOT_REQUESTED,
262
263 .gmx_port_id = 2,
264 },
265
266 .nic_if_cfg[3] = {
267 /* Max Txqs: Half for each of the two ports :max_iq/2 */
268 .max_txqs = MAX_TXQS_PER_INTF,
269
270 /* Actual configured value. Range could be: 1...max_txqs */
271 .num_txqs = DEF_TXQS_PER_INTF,
272
273 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
274 .max_rxqs = MAX_RXQS_PER_INTF,
275
276 /* Actual configured value. Range could be: 1...max_rxqs */
277 .num_rxqs = DEF_RXQS_PER_INTF,
278
279 /* Num of desc for rx rings */
280 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
281
282 /* Num of desc for tx rings */
283 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
284
285 /* SKB size, We need not change buf size even for Jumbo frames.
286 * Octeon can send jumbo frames in 4 consecutive descriptors,
287 */
288 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
289
290 .base_queue = BASE_QUEUE_NOT_REQUESTED,
291
292 .gmx_port_id = 3,
293 },
294
295 /** Miscellaneous attributes */
296 .misc = {
297 /* Host driver link query interval */
298 .oct_link_query_interval = 100,
299
300 /* Octeon link query interval */
301 .host_link_query_interval = 500,
302
303 .enable_sli_oq_bp = 0,
304
305 /* Control queue group */
306 .ctrlq_grp = 1,
307 }
308 ,
309};
310
311/** Default configuration
312 * for CN68XX OCTEON Model.
313 */
314static struct octeon_config default_cn68xx_210nv_conf = {
315 .card_type = LIO_210NV,
316 .card_name = LIO_210NV_NAME,
317
318 /** IQ attributes */
319
320 .iq = {
321 .max_iqs = CN6XXX_CFG_IO_QUEUES,
322 .pending_list_size =
323 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
324 .instr_type = OCTEON_64BYTE_INSTR,
325 .db_min = CN6XXX_DB_MIN,
326 .db_timeout = CN6XXX_DB_TIMEOUT,
327 }
328 ,
329
330 /** OQ attributes */
331 .oq = {
332 .max_oqs = CN6XXX_CFG_IO_QUEUES,
333 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
334 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
335 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
336 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
337 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
338 }
339 ,
340
341 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX_210NV,
342 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
343 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
344 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
345
346 .nic_if_cfg[0] = {
347 /* Max Txqs: Half for each of the two ports :max_iq/2 */
348 .max_txqs = MAX_TXQS_PER_INTF,
349
350 /* Actual configured value. Range could be: 1...max_txqs */
351 .num_txqs = DEF_TXQS_PER_INTF,
352
353 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
354 .max_rxqs = MAX_RXQS_PER_INTF,
355
356 /* Actual configured value. Range could be: 1...max_rxqs */
357 .num_rxqs = DEF_RXQS_PER_INTF,
358
359 /* Num of desc for rx rings */
360 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
361
362 /* Num of desc for tx rings */
363 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
364
365 /* SKB size, We need not change buf size even for Jumbo frames.
366 * Octeon can send jumbo frames in 4 consecutive descriptors,
367 */
368 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
369
370 .base_queue = BASE_QUEUE_NOT_REQUESTED,
371
372 .gmx_port_id = 0,
373 },
374
375 .nic_if_cfg[1] = {
376 /* Max Txqs: Half for each of the two ports :max_iq/2 */
377 .max_txqs = MAX_TXQS_PER_INTF,
378
379 /* Actual configured value. Range could be: 1...max_txqs */
380 .num_txqs = DEF_TXQS_PER_INTF,
381
382 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
383 .max_rxqs = MAX_RXQS_PER_INTF,
384
385 /* Actual configured value. Range could be: 1...max_rxqs */
386 .num_rxqs = DEF_RXQS_PER_INTF,
387
388 /* Num of desc for rx rings */
389 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
390
391 /* Num of desc for tx rings */
392 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
393
394 /* SKB size, We need not change buf size even for Jumbo frames.
395 * Octeon can send jumbo frames in 4 consecutive descriptors,
396 */
397 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
398
399 .base_queue = BASE_QUEUE_NOT_REQUESTED,
400
401 .gmx_port_id = 1,
402 },
403
404 /** Miscellaneous attributes */
405 .misc = {
406 /* Host driver link query interval */
407 .oct_link_query_interval = 100,
408
409 /* Octeon link query interval */
410 .host_link_query_interval = 500,
411
412 .enable_sli_oq_bp = 0,
413
414 /* Control queue group */
415 .ctrlq_grp = 1,
416 }
417 ,
418};
419
420enum {
421 OCTEON_CONFIG_TYPE_DEFAULT = 0,
422 NUM_OCTEON_CONFS,
423};
424
425static struct octeon_config_ptr {
426 u32 conf_type;
427} oct_conf_info[MAX_OCTEON_DEVICES] = {
428 {
429 OCTEON_CONFIG_TYPE_DEFAULT,
430 }, {
431 OCTEON_CONFIG_TYPE_DEFAULT,
432 }, {
433 OCTEON_CONFIG_TYPE_DEFAULT,
434 }, {
435 OCTEON_CONFIG_TYPE_DEFAULT,
436 },
437};
438
439static char oct_dev_state_str[OCT_DEV_STATES + 1][32] = {
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -0700440 "BEGIN", "PCI-MAP-DONE", "DISPATCH-INIT-DONE",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700441 "IQ-INIT-DONE", "SCBUFF-POOL-INIT-DONE", "RESPLIST-INIT-DONE",
442 "DROQ-INIT-DONE", "IO-QUEUES-INIT-DONE", "CONSOLE-INIT-DONE",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -0700443 "HOST-READY", "CORE-READY", "RUNNING", "IN-RESET",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700444 "INVALID"
445};
446
447static char oct_dev_app_str[CVM_DRV_APP_COUNT + 1][32] = {
448 "BASE", "NIC", "UNKNOWN"};
449
450static struct octeon_device *octeon_device[MAX_OCTEON_DEVICES];
451static u32 octeon_device_count;
452
453static struct octeon_core_setup core_setup[MAX_OCTEON_DEVICES];
454
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700455static void oct_set_config_info(int oct_id, int conf_type)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700456{
457 if (conf_type < 0 || conf_type > (NUM_OCTEON_CONFS - 1))
458 conf_type = OCTEON_CONFIG_TYPE_DEFAULT;
459 oct_conf_info[oct_id].conf_type = conf_type;
460}
461
462void octeon_init_device_list(int conf_type)
463{
464 int i;
465
466 memset(octeon_device, 0, (sizeof(void *) * MAX_OCTEON_DEVICES));
467 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
468 oct_set_config_info(i, conf_type);
469}
470
471static void *__retrieve_octeon_config_info(struct octeon_device *oct,
472 u16 card_type)
473{
474 u32 oct_id = oct->octeon_id;
475 void *ret = NULL;
476
477 switch (oct_conf_info[oct_id].conf_type) {
478 case OCTEON_CONFIG_TYPE_DEFAULT:
479 if (oct->chip_id == OCTEON_CN66XX) {
480 ret = (void *)&default_cn66xx_conf;
481 } else if ((oct->chip_id == OCTEON_CN68XX) &&
482 (card_type == LIO_210NV)) {
483 ret = (void *)&default_cn68xx_210nv_conf;
484 } else if ((oct->chip_id == OCTEON_CN68XX) &&
485 (card_type == LIO_410NV)) {
486 ret = (void *)&default_cn68xx_conf;
487 }
488 break;
489 default:
490 break;
491 }
492 return ret;
493}
494
495static int __verify_octeon_config_info(struct octeon_device *oct, void *conf)
496{
497 switch (oct->chip_id) {
498 case OCTEON_CN66XX:
499 case OCTEON_CN68XX:
500 return lio_validate_cn6xxx_config_info(oct, conf);
501
502 default:
503 break;
504 }
505
506 return 1;
507}
508
509void *oct_get_config_info(struct octeon_device *oct, u16 card_type)
510{
511 void *conf = NULL;
512
513 conf = __retrieve_octeon_config_info(oct, card_type);
514 if (!conf)
515 return NULL;
516
517 if (__verify_octeon_config_info(oct, conf)) {
518 dev_err(&oct->pci_dev->dev, "Configuration verification failed\n");
519 return NULL;
520 }
521
522 return conf;
523}
524
525char *lio_get_state_string(atomic_t *state_ptr)
526{
527 s32 istate = (s32)atomic_read(state_ptr);
528
529 if (istate > OCT_DEV_STATES || istate < 0)
530 return oct_dev_state_str[OCT_DEV_STATE_INVALID];
531 return oct_dev_state_str[istate];
532}
533
534static char *get_oct_app_string(u32 app_mode)
535{
536 if (app_mode <= CVM_DRV_APP_END)
537 return oct_dev_app_str[app_mode - CVM_DRV_APP_START];
538 return oct_dev_app_str[CVM_DRV_INVALID_APP - CVM_DRV_APP_START];
539}
540
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700541void octeon_free_device_mem(struct octeon_device *oct)
542{
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -0700543 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700544
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700545 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -0700546 if (oct->io_qmask.oq & (1ULL << i))
547 vfree(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700548 }
549
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700550 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -0700551 if (oct->io_qmask.iq & (1ULL << i))
552 vfree(oct->instr_queue[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700553 }
554
555 i = oct->octeon_id;
556 vfree(oct);
557
558 octeon_device[i] = NULL;
559 octeon_device_count--;
560}
561
562static struct octeon_device *octeon_allocate_device_mem(u32 pci_id,
563 u32 priv_size)
564{
565 struct octeon_device *oct;
566 u8 *buf = NULL;
567 u32 octdevsize = 0, configsize = 0, size;
568
569 switch (pci_id) {
570 case OCTEON_CN68XX:
571 case OCTEON_CN66XX:
572 configsize = sizeof(struct octeon_cn6xxx);
573 break;
574
575 default:
576 pr_err("%s: Unknown PCI Device: 0x%x\n",
577 __func__,
578 pci_id);
579 return NULL;
580 }
581
582 if (configsize & 0x7)
583 configsize += (8 - (configsize & 0x7));
584
585 octdevsize = sizeof(struct octeon_device);
586 if (octdevsize & 0x7)
587 octdevsize += (8 - (octdevsize & 0x7));
588
589 if (priv_size & 0x7)
590 priv_size += (8 - (priv_size & 0x7));
591
592 size = octdevsize + priv_size + configsize +
593 (sizeof(struct octeon_dispatch) * DISPATCH_LIST_SIZE);
594
595 buf = vmalloc(size);
596 if (!buf)
597 return NULL;
598
599 memset(buf, 0, size);
600
601 oct = (struct octeon_device *)buf;
602 oct->priv = (void *)(buf + octdevsize);
603 oct->chip = (void *)(buf + octdevsize + priv_size);
604 oct->dispatch.dlist = (struct octeon_dispatch *)
605 (buf + octdevsize + priv_size + configsize);
606
607 return oct;
608}
609
610struct octeon_device *octeon_allocate_device(u32 pci_id,
611 u32 priv_size)
612{
613 u32 oct_idx = 0;
614 struct octeon_device *oct = NULL;
615
616 for (oct_idx = 0; oct_idx < MAX_OCTEON_DEVICES; oct_idx++)
617 if (!octeon_device[oct_idx])
618 break;
619
620 if (oct_idx == MAX_OCTEON_DEVICES)
621 return NULL;
622
623 oct = octeon_allocate_device_mem(pci_id, priv_size);
624 if (!oct)
625 return NULL;
626
627 spin_lock_init(&oct->pci_win_lock);
628 spin_lock_init(&oct->mem_access_lock);
629
630 octeon_device_count++;
631 octeon_device[oct_idx] = oct;
632
633 oct->octeon_id = oct_idx;
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700634 snprintf(oct->device_name, sizeof(oct->device_name),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700635 "LiquidIO%d", (oct->octeon_id));
636
637 return oct;
638}
639
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700640/* this function is only for setting up the first queue */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700641int octeon_setup_instr_queues(struct octeon_device *oct)
642{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700643 u32 num_descs = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700644 u32 iq_no = 0;
645 union oct_txpciq txpciq;
646 int numa_node = cpu_to_node(iq_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700647
648 /* this causes queue 0 to be default queue */
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700649 if (OCTEON_CN6XXX(oct))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700650 num_descs =
651 CFG_GET_NUM_DEF_TX_DESCS(CHIP_FIELD(oct, cn6xxx, conf));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700652
653 oct->num_iqs = 0;
654
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700655 oct->instr_queue[0] = vmalloc_node(sizeof(*oct->instr_queue[0]),
656 numa_node);
657 if (!oct->instr_queue[0])
658 oct->instr_queue[0] =
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700659 vmalloc(sizeof(struct octeon_instr_queue));
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700660 if (!oct->instr_queue[0])
661 return 1;
662 memset(oct->instr_queue[0], 0, sizeof(struct octeon_instr_queue));
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700663 oct->instr_queue[0]->q_index = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700664 oct->instr_queue[0]->app_ctx = (void *)(size_t)0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700665 oct->instr_queue[0]->ifidx = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700666 txpciq.u64 = 0;
667 txpciq.s.q_no = iq_no;
668 txpciq.s.use_qpg = 0;
669 txpciq.s.qpg = 0;
670 if (octeon_init_instr_queue(oct, txpciq, num_descs)) {
671 /* prevent memory leak */
672 vfree(oct->instr_queue[0]);
673 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700674 }
675
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700676 oct->num_iqs++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700677 return 0;
678}
679
680int octeon_setup_output_queues(struct octeon_device *oct)
681{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700682 u32 num_descs = 0;
683 u32 desc_size = 0;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700684 u32 oq_no = 0;
685 int numa_node = cpu_to_node(oq_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700686
687 /* this causes queue 0 to be default queue */
688 if (OCTEON_CN6XXX(oct)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700689 num_descs =
690 CFG_GET_NUM_DEF_RX_DESCS(CHIP_FIELD(oct, cn6xxx, conf));
691 desc_size =
692 CFG_GET_DEF_RX_BUF_SIZE(CHIP_FIELD(oct, cn6xxx, conf));
693 }
694
695 oct->num_oqs = 0;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700696 oct->droq[0] = vmalloc_node(sizeof(*oct->droq[0]), numa_node);
697 if (!oct->droq[0])
698 oct->droq[0] = vmalloc(sizeof(*oct->droq[0]));
699 if (!oct->droq[0])
700 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700701
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700702 if (octeon_init_droq(oct, oq_no, num_descs, desc_size, NULL))
703 return 1;
704 oct->num_oqs++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700705
706 return 0;
707}
708
709void octeon_set_io_queues_off(struct octeon_device *oct)
710{
711 /* Disable the i/p and o/p queues for this Octeon. */
712
713 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
714 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
715}
716
717void octeon_set_droq_pkt_op(struct octeon_device *oct,
718 u32 q_no,
719 u32 enable)
720{
721 u32 reg_val = 0;
722
723 /* Disable the i/p and o/p queues for this Octeon. */
724 reg_val = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
725
726 if (enable)
727 reg_val = reg_val | (1 << q_no);
728 else
729 reg_val = reg_val & (~(1 << q_no));
730
731 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, reg_val);
732}
733
734int octeon_init_dispatch_list(struct octeon_device *oct)
735{
736 u32 i;
737
738 oct->dispatch.count = 0;
739
740 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
741 oct->dispatch.dlist[i].opcode = 0;
742 INIT_LIST_HEAD(&oct->dispatch.dlist[i].list);
743 }
744
745 for (i = 0; i <= REQTYPE_LAST; i++)
746 octeon_register_reqtype_free_fn(oct, i, NULL);
747
748 spin_lock_init(&oct->dispatch.lock);
749
750 return 0;
751}
752
753void octeon_delete_dispatch_list(struct octeon_device *oct)
754{
755 u32 i;
756 struct list_head freelist, *temp, *tmp2;
757
758 INIT_LIST_HEAD(&freelist);
759
760 spin_lock_bh(&oct->dispatch.lock);
761
762 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
763 struct list_head *dispatch;
764
765 dispatch = &oct->dispatch.dlist[i].list;
766 while (dispatch->next != dispatch) {
767 temp = dispatch->next;
768 list_del(temp);
769 list_add_tail(temp, &freelist);
770 }
771
772 oct->dispatch.dlist[i].opcode = 0;
773 }
774
775 oct->dispatch.count = 0;
776
777 spin_unlock_bh(&oct->dispatch.lock);
778
779 list_for_each_safe(temp, tmp2, &freelist) {
780 list_del(temp);
781 vfree(temp);
782 }
783}
784
785octeon_dispatch_fn_t
786octeon_get_dispatch(struct octeon_device *octeon_dev, u16 opcode,
787 u16 subcode)
788{
789 u32 idx;
790 struct list_head *dispatch;
791 octeon_dispatch_fn_t fn = NULL;
792 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
793
794 idx = combined_opcode & OCTEON_OPCODE_MASK;
795
796 spin_lock_bh(&octeon_dev->dispatch.lock);
797
798 if (octeon_dev->dispatch.count == 0) {
799 spin_unlock_bh(&octeon_dev->dispatch.lock);
800 return NULL;
801 }
802
803 if (!(octeon_dev->dispatch.dlist[idx].opcode)) {
804 spin_unlock_bh(&octeon_dev->dispatch.lock);
805 return NULL;
806 }
807
808 if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
809 fn = octeon_dev->dispatch.dlist[idx].dispatch_fn;
810 } else {
811 list_for_each(dispatch,
812 &octeon_dev->dispatch.dlist[idx].list) {
813 if (((struct octeon_dispatch *)dispatch)->opcode ==
814 combined_opcode) {
815 fn = ((struct octeon_dispatch *)
816 dispatch)->dispatch_fn;
817 break;
818 }
819 }
820 }
821
822 spin_unlock_bh(&octeon_dev->dispatch.lock);
823 return fn;
824}
825
826/* octeon_register_dispatch_fn
827 * Parameters:
828 * octeon_id - id of the octeon device.
829 * opcode - opcode for which driver should call the registered function
830 * subcode - subcode for which driver should call the registered function
831 * fn - The function to call when a packet with "opcode" arrives in
832 * octeon output queues.
833 * fn_arg - The argument to be passed when calling function "fn".
834 * Description:
835 * Registers a function and its argument to be called when a packet
836 * arrives in Octeon output queues with "opcode".
837 * Returns:
838 * Success: 0
839 * Failure: 1
840 * Locks:
841 * No locks are held.
842 */
843int
844octeon_register_dispatch_fn(struct octeon_device *oct,
845 u16 opcode,
846 u16 subcode,
847 octeon_dispatch_fn_t fn, void *fn_arg)
848{
849 u32 idx;
850 octeon_dispatch_fn_t pfn;
851 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
852
853 idx = combined_opcode & OCTEON_OPCODE_MASK;
854
855 spin_lock_bh(&oct->dispatch.lock);
856 /* Add dispatch function to first level of lookup table */
857 if (oct->dispatch.dlist[idx].opcode == 0) {
858 oct->dispatch.dlist[idx].opcode = combined_opcode;
859 oct->dispatch.dlist[idx].dispatch_fn = fn;
860 oct->dispatch.dlist[idx].arg = fn_arg;
861 oct->dispatch.count++;
862 spin_unlock_bh(&oct->dispatch.lock);
863 return 0;
864 }
865
866 spin_unlock_bh(&oct->dispatch.lock);
867
868 /* Check if there was a function already registered for this
869 * opcode/subcode.
870 */
871 pfn = octeon_get_dispatch(oct, opcode, subcode);
872 if (!pfn) {
873 struct octeon_dispatch *dispatch;
874
875 dev_dbg(&oct->pci_dev->dev,
876 "Adding opcode to dispatch list linked list\n");
877 dispatch = (struct octeon_dispatch *)
878 vmalloc(sizeof(struct octeon_dispatch));
879 if (!dispatch) {
880 dev_err(&oct->pci_dev->dev,
881 "No memory to add dispatch function\n");
882 return 1;
883 }
884 dispatch->opcode = combined_opcode;
885 dispatch->dispatch_fn = fn;
886 dispatch->arg = fn_arg;
887
888 /* Add dispatch function to linked list of fn ptrs
889 * at the hashed index.
890 */
891 spin_lock_bh(&oct->dispatch.lock);
892 list_add(&dispatch->list, &oct->dispatch.dlist[idx].list);
893 oct->dispatch.count++;
894 spin_unlock_bh(&oct->dispatch.lock);
895
896 } else {
897 dev_err(&oct->pci_dev->dev,
898 "Found previously registered dispatch fn for opcode/subcode: %x/%x\n",
899 opcode, subcode);
900 return 1;
901 }
902
903 return 0;
904}
905
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700906int octeon_core_drv_init(struct octeon_recv_info *recv_info, void *buf)
907{
908 u32 i;
909 char app_name[16];
910 struct octeon_device *oct = (struct octeon_device *)buf;
911 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
912 struct octeon_core_setup *cs = NULL;
913 u32 num_nic_ports = 0;
914
915 if (OCTEON_CN6XXX(oct))
916 num_nic_ports =
917 CFG_GET_NUM_NIC_PORTS(CHIP_FIELD(oct, cn6xxx, conf));
918
919 if (atomic_read(&oct->status) >= OCT_DEV_RUNNING) {
920 dev_err(&oct->pci_dev->dev, "Received CORE OK when device state is 0x%x\n",
921 atomic_read(&oct->status));
922 goto core_drv_init_err;
923 }
924
925 strncpy(app_name,
926 get_oct_app_string(
927 (u32)recv_pkt->rh.r_core_drv_init.app_mode),
928 sizeof(app_name) - 1);
929 oct->app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700930 if (recv_pkt->rh.r_core_drv_init.app_mode == CVM_DRV_NIC_APP) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700931 oct->fw_info.max_nic_ports =
932 (u32)recv_pkt->rh.r_core_drv_init.max_nic_ports;
933 oct->fw_info.num_gmx_ports =
934 (u32)recv_pkt->rh.r_core_drv_init.num_gmx_ports;
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700935 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700936
937 if (oct->fw_info.max_nic_ports < num_nic_ports) {
938 dev_err(&oct->pci_dev->dev,
939 "Config has more ports than firmware allows (%d > %d).\n",
940 num_nic_ports, oct->fw_info.max_nic_ports);
941 goto core_drv_init_err;
942 }
943 oct->fw_info.app_cap_flags = recv_pkt->rh.r_core_drv_init.app_cap_flags;
944 oct->fw_info.app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
945
946 atomic_set(&oct->status, OCT_DEV_CORE_OK);
947
948 cs = &core_setup[oct->octeon_id];
949
950 if (recv_pkt->buffer_size[0] != sizeof(*cs)) {
951 dev_dbg(&oct->pci_dev->dev, "Core setup bytes expected %u found %d\n",
952 (u32)sizeof(*cs),
953 recv_pkt->buffer_size[0]);
954 }
955
956 memcpy(cs, get_rbd(recv_pkt->buffer_ptr[0]), sizeof(*cs));
957 strncpy(oct->boardinfo.name, cs->boardname, OCT_BOARD_NAME);
958 strncpy(oct->boardinfo.serial_number, cs->board_serial_number,
959 OCT_SERIAL_LEN);
960
961 octeon_swap_8B_data((u64 *)cs, (sizeof(*cs) >> 3));
962
963 oct->boardinfo.major = cs->board_rev_major;
964 oct->boardinfo.minor = cs->board_rev_minor;
965
966 dev_info(&oct->pci_dev->dev,
967 "Running %s (%llu Hz)\n",
968 app_name, CVM_CAST64(cs->corefreq));
969
970core_drv_init_err:
971 for (i = 0; i < recv_pkt->buffer_count; i++)
972 recv_buffer_free(recv_pkt->buffer_ptr[i]);
973 octeon_free_recv_info(recv_info);
974 return 0;
975}
976
977int octeon_get_tx_qsize(struct octeon_device *oct, u32 q_no)
978
979{
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700980 if (oct && (q_no < MAX_OCTEON_INSTR_QUEUES(oct)) &&
981 (oct->io_qmask.iq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700982 return oct->instr_queue[q_no]->max_count;
983
984 return -1;
985}
986
987int octeon_get_rx_qsize(struct octeon_device *oct, u32 q_no)
988{
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700989 if (oct && (q_no < MAX_OCTEON_OUTPUT_QUEUES(oct)) &&
990 (oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700991 return oct->droq[q_no]->max_count;
992 return -1;
993}
994
995/* Retruns the host firmware handshake OCTEON specific configuration */
996struct octeon_config *octeon_get_conf(struct octeon_device *oct)
997{
998 struct octeon_config *default_oct_conf = NULL;
999
1000 /* check the OCTEON Device model & return the corresponding octeon
1001 * configuration
1002 */
1003
1004 if (OCTEON_CN6XXX(oct)) {
1005 default_oct_conf =
1006 (struct octeon_config *)(CHIP_FIELD(oct, cn6xxx, conf));
1007 }
1008
1009 return default_oct_conf;
1010}
1011
1012/* scratch register address is same in all the OCT-II and CN70XX models */
1013#define CNXX_SLI_SCRATCH1 0x3C0
1014
1015/** Get the octeon device pointer.
1016 * @param octeon_id - The id for which the octeon device pointer is required.
1017 * @return Success: Octeon device pointer.
1018 * @return Failure: NULL.
1019 */
1020struct octeon_device *lio_get_device(u32 octeon_id)
1021{
1022 if (octeon_id >= MAX_OCTEON_DEVICES)
1023 return NULL;
1024 else
1025 return octeon_device[octeon_id];
1026}
1027
1028u64 lio_pci_readq(struct octeon_device *oct, u64 addr)
1029{
1030 u64 val64;
1031 unsigned long flags;
1032 u32 val32, addrhi;
1033
1034 spin_lock_irqsave(&oct->pci_win_lock, flags);
1035
1036 /* The windowed read happens when the LSB of the addr is written.
1037 * So write MSB first
1038 */
1039 addrhi = (addr >> 32);
1040 if ((oct->chip_id == OCTEON_CN66XX) || (oct->chip_id == OCTEON_CN68XX))
1041 addrhi |= 0x00060000;
1042 writel(addrhi, oct->reg_list.pci_win_rd_addr_hi);
1043
1044 /* Read back to preserve ordering of writes */
1045 val32 = readl(oct->reg_list.pci_win_rd_addr_hi);
1046
1047 writel(addr & 0xffffffff, oct->reg_list.pci_win_rd_addr_lo);
1048 val32 = readl(oct->reg_list.pci_win_rd_addr_lo);
1049
1050 val64 = readq(oct->reg_list.pci_win_rd_data);
1051
1052 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1053
1054 return val64;
1055}
1056
1057void lio_pci_writeq(struct octeon_device *oct,
1058 u64 val,
1059 u64 addr)
1060{
1061 u32 val32;
1062 unsigned long flags;
1063
1064 spin_lock_irqsave(&oct->pci_win_lock, flags);
1065
1066 writeq(addr, oct->reg_list.pci_win_wr_addr);
1067
1068 /* The write happens when the LSB is written. So write MSB first. */
1069 writel(val >> 32, oct->reg_list.pci_win_wr_data_hi);
1070 /* Read the MSB to ensure ordering of writes. */
1071 val32 = readl(oct->reg_list.pci_win_wr_data_hi);
1072
1073 writel(val & 0xffffffff, oct->reg_list.pci_win_wr_data_lo);
1074
1075 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1076}
1077
1078int octeon_mem_access_ok(struct octeon_device *oct)
1079{
1080 u64 access_okay = 0;
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001081 u64 lmc0_reset_ctl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001082
1083 /* Check to make sure a DDR interface is enabled */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001084 lmc0_reset_ctl = lio_pci_readq(oct, CN6XXX_LMC0_RESET_CTL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001085 access_okay = (lmc0_reset_ctl & CN6XXX_LMC0_RESET_CTL_DDR3RST_MASK);
1086
1087 return access_okay ? 0 : 1;
1088}
1089
1090int octeon_wait_for_ddr_init(struct octeon_device *oct, u32 *timeout)
1091{
1092 int ret = 1;
1093 u32 ms;
1094
1095 if (!timeout)
1096 return ret;
1097
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001098 for (ms = 0; (ret != 0) && ((*timeout == 0) || (ms <= *timeout));
1099 ms += HZ / 10) {
1100 ret = octeon_mem_access_ok(oct);
1101
1102 /* wait 100 ms */
1103 if (ret)
1104 schedule_timeout_uninterruptible(HZ / 10);
1105 }
1106
1107 return ret;
1108}
1109
1110/** Get the octeon id assigned to the octeon device passed as argument.
1111 * This function is exported to other modules.
1112 * @param dev - octeon device pointer passed as a void *.
1113 * @return octeon device id
1114 */
1115int lio_get_device_id(void *dev)
1116{
1117 struct octeon_device *octeon_dev = (struct octeon_device *)dev;
1118 u32 i;
1119
1120 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
1121 if (octeon_device[i] == octeon_dev)
1122 return octeon_dev->octeon_id;
1123 return -1;
1124}
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -07001125
1126void lio_enable_irq(struct octeon_droq *droq, struct octeon_instr_queue *iq)
1127{
1128 /* the whole thing needs to be atomic, ideally */
1129 if (droq) {
1130 spin_lock_bh(&droq->lock);
1131 writel(droq->pkt_count, droq->pkts_sent_reg);
1132 droq->pkt_count = 0;
1133 spin_unlock_bh(&droq->lock);
1134 }
1135 if (iq) {
1136 spin_lock_bh(&iq->lock);
1137 writel(iq->pkt_in_done, iq->inst_cnt_reg);
1138 iq->pkt_in_done = 0;
1139 spin_unlock_bh(&iq->lock);
1140 }
1141}