blob: e1ca61738b634fbad6ce38c825d3d093fde7d585 [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/types.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/crc32.h>
27#include <linux/kthread.h>
28#include <linux/netdevice.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070029#include <linux/vmalloc.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070030#include "octeon_config.h"
31#include "liquidio_common.h"
32#include "octeon_droq.h"
33#include "octeon_iq.h"
34#include "response_manager.h"
35#include "octeon_device.h"
36#include "octeon_nic.h"
37#include "octeon_main.h"
38#include "octeon_network.h"
39#include "cn66xx_regs.h"
40#include "cn66xx_device.h"
41#include "cn68xx_regs.h"
42#include "cn68xx_device.h"
43#include "liquidio_image.h"
44#include "octeon_mem_ops.h"
45
46/** Default configuration
47 * for CN66XX OCTEON Models.
48 */
49static struct octeon_config default_cn66xx_conf = {
50 .card_type = LIO_210SV,
51 .card_name = LIO_210SV_NAME,
52
53 /** IQ attributes */
54 .iq = {
55 .max_iqs = CN6XXX_CFG_IO_QUEUES,
56 .pending_list_size =
57 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
58 .instr_type = OCTEON_64BYTE_INSTR,
59 .db_min = CN6XXX_DB_MIN,
60 .db_timeout = CN6XXX_DB_TIMEOUT,
61 }
62 ,
63
64 /** OQ attributes */
65 .oq = {
66 .max_oqs = CN6XXX_CFG_IO_QUEUES,
67 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
68 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
69 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
70 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
71 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
72 }
73 ,
74
75 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_66XX,
76 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
77 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
78 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
79
80 /* For ethernet interface 0: Port cfg Attributes */
81 .nic_if_cfg[0] = {
82 /* Max Txqs: Half for each of the two ports :max_iq/2 */
83 .max_txqs = MAX_TXQS_PER_INTF,
84
85 /* Actual configured value. Range could be: 1...max_txqs */
86 .num_txqs = DEF_TXQS_PER_INTF,
87
88 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
89 .max_rxqs = MAX_RXQS_PER_INTF,
90
91 /* Actual configured value. Range could be: 1...max_rxqs */
92 .num_rxqs = DEF_RXQS_PER_INTF,
93
94 /* Num of desc for rx rings */
95 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
96
97 /* Num of desc for tx rings */
98 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
99
100 /* SKB size, We need not change buf size even for Jumbo frames.
101 * Octeon can send jumbo frames in 4 consecutive descriptors,
102 */
103 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
104
105 .base_queue = BASE_QUEUE_NOT_REQUESTED,
106
107 .gmx_port_id = 0,
108 },
109
110 .nic_if_cfg[1] = {
111 /* Max Txqs: Half for each of the two ports :max_iq/2 */
112 .max_txqs = MAX_TXQS_PER_INTF,
113
114 /* Actual configured value. Range could be: 1...max_txqs */
115 .num_txqs = DEF_TXQS_PER_INTF,
116
117 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
118 .max_rxqs = MAX_RXQS_PER_INTF,
119
120 /* Actual configured value. Range could be: 1...max_rxqs */
121 .num_rxqs = DEF_RXQS_PER_INTF,
122
123 /* Num of desc for rx rings */
124 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
125
126 /* Num of desc for tx rings */
127 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
128
129 /* SKB size, We need not change buf size even for Jumbo frames.
130 * Octeon can send jumbo frames in 4 consecutive descriptors,
131 */
132 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
133
134 .base_queue = BASE_QUEUE_NOT_REQUESTED,
135
136 .gmx_port_id = 1,
137 },
138
139 /** Miscellaneous attributes */
140 .misc = {
141 /* Host driver link query interval */
142 .oct_link_query_interval = 100,
143
144 /* Octeon link query interval */
145 .host_link_query_interval = 500,
146
147 .enable_sli_oq_bp = 0,
148
149 /* Control queue group */
150 .ctrlq_grp = 1,
151 }
152 ,
153};
154
155/** Default configuration
156 * for CN68XX OCTEON Model.
157 */
158
159static struct octeon_config default_cn68xx_conf = {
160 .card_type = LIO_410NV,
161 .card_name = LIO_410NV_NAME,
162
163 /** IQ attributes */
164 .iq = {
165 .max_iqs = CN6XXX_CFG_IO_QUEUES,
166 .pending_list_size =
167 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
168 .instr_type = OCTEON_64BYTE_INSTR,
169 .db_min = CN6XXX_DB_MIN,
170 .db_timeout = CN6XXX_DB_TIMEOUT,
171 }
172 ,
173
174 /** OQ attributes */
175 .oq = {
176 .max_oqs = CN6XXX_CFG_IO_QUEUES,
177 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
178 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
179 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
180 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
181 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
182 }
183 ,
184
185 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX,
186 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
187 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
188 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
189
190 .nic_if_cfg[0] = {
191 /* Max Txqs: Half for each of the two ports :max_iq/2 */
192 .max_txqs = MAX_TXQS_PER_INTF,
193
194 /* Actual configured value. Range could be: 1...max_txqs */
195 .num_txqs = DEF_TXQS_PER_INTF,
196
197 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
198 .max_rxqs = MAX_RXQS_PER_INTF,
199
200 /* Actual configured value. Range could be: 1...max_rxqs */
201 .num_rxqs = DEF_RXQS_PER_INTF,
202
203 /* Num of desc for rx rings */
204 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
205
206 /* Num of desc for tx rings */
207 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
208
209 /* SKB size, We need not change buf size even for Jumbo frames.
210 * Octeon can send jumbo frames in 4 consecutive descriptors,
211 */
212 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
213
214 .base_queue = BASE_QUEUE_NOT_REQUESTED,
215
216 .gmx_port_id = 0,
217 },
218
219 .nic_if_cfg[1] = {
220 /* Max Txqs: Half for each of the two ports :max_iq/2 */
221 .max_txqs = MAX_TXQS_PER_INTF,
222
223 /* Actual configured value. Range could be: 1...max_txqs */
224 .num_txqs = DEF_TXQS_PER_INTF,
225
226 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
227 .max_rxqs = MAX_RXQS_PER_INTF,
228
229 /* Actual configured value. Range could be: 1...max_rxqs */
230 .num_rxqs = DEF_RXQS_PER_INTF,
231
232 /* Num of desc for rx rings */
233 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
234
235 /* Num of desc for tx rings */
236 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
237
238 /* SKB size, We need not change buf size even for Jumbo frames.
239 * Octeon can send jumbo frames in 4 consecutive descriptors,
240 */
241 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
242
243 .base_queue = BASE_QUEUE_NOT_REQUESTED,
244
245 .gmx_port_id = 1,
246 },
247
248 .nic_if_cfg[2] = {
249 /* Max Txqs: Half for each of the two ports :max_iq/2 */
250 .max_txqs = MAX_TXQS_PER_INTF,
251
252 /* Actual configured value. Range could be: 1...max_txqs */
253 .num_txqs = DEF_TXQS_PER_INTF,
254
255 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
256 .max_rxqs = MAX_RXQS_PER_INTF,
257
258 /* Actual configured value. Range could be: 1...max_rxqs */
259 .num_rxqs = DEF_RXQS_PER_INTF,
260
261 /* Num of desc for rx rings */
262 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
263
264 /* Num of desc for tx rings */
265 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
266
267 /* SKB size, We need not change buf size even for Jumbo frames.
268 * Octeon can send jumbo frames in 4 consecutive descriptors,
269 */
270 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
271
272 .base_queue = BASE_QUEUE_NOT_REQUESTED,
273
274 .gmx_port_id = 2,
275 },
276
277 .nic_if_cfg[3] = {
278 /* Max Txqs: Half for each of the two ports :max_iq/2 */
279 .max_txqs = MAX_TXQS_PER_INTF,
280
281 /* Actual configured value. Range could be: 1...max_txqs */
282 .num_txqs = DEF_TXQS_PER_INTF,
283
284 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
285 .max_rxqs = MAX_RXQS_PER_INTF,
286
287 /* Actual configured value. Range could be: 1...max_rxqs */
288 .num_rxqs = DEF_RXQS_PER_INTF,
289
290 /* Num of desc for rx rings */
291 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
292
293 /* Num of desc for tx rings */
294 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
295
296 /* SKB size, We need not change buf size even for Jumbo frames.
297 * Octeon can send jumbo frames in 4 consecutive descriptors,
298 */
299 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
300
301 .base_queue = BASE_QUEUE_NOT_REQUESTED,
302
303 .gmx_port_id = 3,
304 },
305
306 /** Miscellaneous attributes */
307 .misc = {
308 /* Host driver link query interval */
309 .oct_link_query_interval = 100,
310
311 /* Octeon link query interval */
312 .host_link_query_interval = 500,
313
314 .enable_sli_oq_bp = 0,
315
316 /* Control queue group */
317 .ctrlq_grp = 1,
318 }
319 ,
320};
321
322/** Default configuration
323 * for CN68XX OCTEON Model.
324 */
325static struct octeon_config default_cn68xx_210nv_conf = {
326 .card_type = LIO_210NV,
327 .card_name = LIO_210NV_NAME,
328
329 /** IQ attributes */
330
331 .iq = {
332 .max_iqs = CN6XXX_CFG_IO_QUEUES,
333 .pending_list_size =
334 (CN6XXX_MAX_IQ_DESCRIPTORS * CN6XXX_CFG_IO_QUEUES),
335 .instr_type = OCTEON_64BYTE_INSTR,
336 .db_min = CN6XXX_DB_MIN,
337 .db_timeout = CN6XXX_DB_TIMEOUT,
338 }
339 ,
340
341 /** OQ attributes */
342 .oq = {
343 .max_oqs = CN6XXX_CFG_IO_QUEUES,
344 .info_ptr = OCTEON_OQ_INFOPTR_MODE,
345 .refill_threshold = CN6XXX_OQ_REFIL_THRESHOLD,
346 .oq_intr_pkt = CN6XXX_OQ_INTR_PKT,
347 .oq_intr_time = CN6XXX_OQ_INTR_TIME,
348 .pkts_per_intr = CN6XXX_OQ_PKTSPER_INTR,
349 }
350 ,
351
352 .num_nic_ports = DEFAULT_NUM_NIC_PORTS_68XX_210NV,
353 .num_def_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
354 .num_def_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
355 .def_rx_buf_size = CN6XXX_OQ_BUF_SIZE,
356
357 .nic_if_cfg[0] = {
358 /* Max Txqs: Half for each of the two ports :max_iq/2 */
359 .max_txqs = MAX_TXQS_PER_INTF,
360
361 /* Actual configured value. Range could be: 1...max_txqs */
362 .num_txqs = DEF_TXQS_PER_INTF,
363
364 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
365 .max_rxqs = MAX_RXQS_PER_INTF,
366
367 /* Actual configured value. Range could be: 1...max_rxqs */
368 .num_rxqs = DEF_RXQS_PER_INTF,
369
370 /* Num of desc for rx rings */
371 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
372
373 /* Num of desc for tx rings */
374 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
375
376 /* SKB size, We need not change buf size even for Jumbo frames.
377 * Octeon can send jumbo frames in 4 consecutive descriptors,
378 */
379 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
380
381 .base_queue = BASE_QUEUE_NOT_REQUESTED,
382
383 .gmx_port_id = 0,
384 },
385
386 .nic_if_cfg[1] = {
387 /* Max Txqs: Half for each of the two ports :max_iq/2 */
388 .max_txqs = MAX_TXQS_PER_INTF,
389
390 /* Actual configured value. Range could be: 1...max_txqs */
391 .num_txqs = DEF_TXQS_PER_INTF,
392
393 /* Max Rxqs: Half for each of the two ports :max_oq/2 */
394 .max_rxqs = MAX_RXQS_PER_INTF,
395
396 /* Actual configured value. Range could be: 1...max_rxqs */
397 .num_rxqs = DEF_RXQS_PER_INTF,
398
399 /* Num of desc for rx rings */
400 .num_rx_descs = CN6XXX_MAX_OQ_DESCRIPTORS,
401
402 /* Num of desc for tx rings */
403 .num_tx_descs = CN6XXX_MAX_IQ_DESCRIPTORS,
404
405 /* SKB size, We need not change buf size even for Jumbo frames.
406 * Octeon can send jumbo frames in 4 consecutive descriptors,
407 */
408 .rx_buf_size = CN6XXX_OQ_BUF_SIZE,
409
410 .base_queue = BASE_QUEUE_NOT_REQUESTED,
411
412 .gmx_port_id = 1,
413 },
414
415 /** Miscellaneous attributes */
416 .misc = {
417 /* Host driver link query interval */
418 .oct_link_query_interval = 100,
419
420 /* Octeon link query interval */
421 .host_link_query_interval = 500,
422
423 .enable_sli_oq_bp = 0,
424
425 /* Control queue group */
426 .ctrlq_grp = 1,
427 }
428 ,
429};
430
431enum {
432 OCTEON_CONFIG_TYPE_DEFAULT = 0,
433 NUM_OCTEON_CONFS,
434};
435
436static struct octeon_config_ptr {
437 u32 conf_type;
438} oct_conf_info[MAX_OCTEON_DEVICES] = {
439 {
440 OCTEON_CONFIG_TYPE_DEFAULT,
441 }, {
442 OCTEON_CONFIG_TYPE_DEFAULT,
443 }, {
444 OCTEON_CONFIG_TYPE_DEFAULT,
445 }, {
446 OCTEON_CONFIG_TYPE_DEFAULT,
447 },
448};
449
450static char oct_dev_state_str[OCT_DEV_STATES + 1][32] = {
451 "BEGIN", "PCI-MAP-DONE", "DISPATCH-INIT-DONE",
452 "IQ-INIT-DONE", "SCBUFF-POOL-INIT-DONE", "RESPLIST-INIT-DONE",
453 "DROQ-INIT-DONE", "IO-QUEUES-INIT-DONE", "CONSOLE-INIT-DONE",
454 "HOST-READY", "CORE-READY", "RUNNING", "IN-RESET",
455 "INVALID"
456};
457
458static char oct_dev_app_str[CVM_DRV_APP_COUNT + 1][32] = {
459 "BASE", "NIC", "UNKNOWN"};
460
461static struct octeon_device *octeon_device[MAX_OCTEON_DEVICES];
462static u32 octeon_device_count;
463
464static struct octeon_core_setup core_setup[MAX_OCTEON_DEVICES];
465
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700466static void oct_set_config_info(int oct_id, int conf_type)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700467{
468 if (conf_type < 0 || conf_type > (NUM_OCTEON_CONFS - 1))
469 conf_type = OCTEON_CONFIG_TYPE_DEFAULT;
470 oct_conf_info[oct_id].conf_type = conf_type;
471}
472
473void octeon_init_device_list(int conf_type)
474{
475 int i;
476
477 memset(octeon_device, 0, (sizeof(void *) * MAX_OCTEON_DEVICES));
478 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
479 oct_set_config_info(i, conf_type);
480}
481
482static void *__retrieve_octeon_config_info(struct octeon_device *oct,
483 u16 card_type)
484{
485 u32 oct_id = oct->octeon_id;
486 void *ret = NULL;
487
488 switch (oct_conf_info[oct_id].conf_type) {
489 case OCTEON_CONFIG_TYPE_DEFAULT:
490 if (oct->chip_id == OCTEON_CN66XX) {
491 ret = (void *)&default_cn66xx_conf;
492 } else if ((oct->chip_id == OCTEON_CN68XX) &&
493 (card_type == LIO_210NV)) {
494 ret = (void *)&default_cn68xx_210nv_conf;
495 } else if ((oct->chip_id == OCTEON_CN68XX) &&
496 (card_type == LIO_410NV)) {
497 ret = (void *)&default_cn68xx_conf;
498 }
499 break;
500 default:
501 break;
502 }
503 return ret;
504}
505
506static int __verify_octeon_config_info(struct octeon_device *oct, void *conf)
507{
508 switch (oct->chip_id) {
509 case OCTEON_CN66XX:
510 case OCTEON_CN68XX:
511 return lio_validate_cn6xxx_config_info(oct, conf);
512
513 default:
514 break;
515 }
516
517 return 1;
518}
519
520void *oct_get_config_info(struct octeon_device *oct, u16 card_type)
521{
522 void *conf = NULL;
523
524 conf = __retrieve_octeon_config_info(oct, card_type);
525 if (!conf)
526 return NULL;
527
528 if (__verify_octeon_config_info(oct, conf)) {
529 dev_err(&oct->pci_dev->dev, "Configuration verification failed\n");
530 return NULL;
531 }
532
533 return conf;
534}
535
536char *lio_get_state_string(atomic_t *state_ptr)
537{
538 s32 istate = (s32)atomic_read(state_ptr);
539
540 if (istate > OCT_DEV_STATES || istate < 0)
541 return oct_dev_state_str[OCT_DEV_STATE_INVALID];
542 return oct_dev_state_str[istate];
543}
544
545static char *get_oct_app_string(u32 app_mode)
546{
547 if (app_mode <= CVM_DRV_APP_END)
548 return oct_dev_app_str[app_mode - CVM_DRV_APP_START];
549 return oct_dev_app_str[CVM_DRV_INVALID_APP - CVM_DRV_APP_START];
550}
551
552int octeon_download_firmware(struct octeon_device *oct, const u8 *data,
553 size_t size)
554{
555 int ret = 0;
556 u8 *p;
557 u8 *buffer;
558 u32 crc32_result;
559 u64 load_addr;
560 u32 image_len;
561 struct octeon_firmware_file_header *h;
562 u32 i;
563
564 if (size < sizeof(struct octeon_firmware_file_header)) {
565 dev_err(&oct->pci_dev->dev, "Firmware file too small (%d < %d).\n",
566 (u32)size,
567 (u32)sizeof(struct octeon_firmware_file_header));
568 return -EINVAL;
569 }
570
571 h = (struct octeon_firmware_file_header *)data;
572
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700573 if (be32_to_cpu(h->magic) != LIO_NIC_MAGIC) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700574 dev_err(&oct->pci_dev->dev, "Unrecognized firmware file.\n");
575 return -EINVAL;
576 }
577
578 crc32_result =
579 crc32(~0, data,
580 sizeof(struct octeon_firmware_file_header) -
581 sizeof(u32)) ^ ~0U;
582 if (crc32_result != be32_to_cpu(h->crc32)) {
583 dev_err(&oct->pci_dev->dev, "Firmware CRC mismatch (0x%08x != 0x%08x).\n",
584 crc32_result, be32_to_cpu(h->crc32));
585 return -EINVAL;
586 }
587
588 if (memcmp(LIQUIDIO_VERSION, h->version, strlen(LIQUIDIO_VERSION))) {
589 dev_err(&oct->pci_dev->dev, "Unmatched firmware version. Expected %s, got %s.\n",
590 LIQUIDIO_VERSION, h->version);
591 return -EINVAL;
592 }
593
594 if (be32_to_cpu(h->num_images) > LIO_MAX_IMAGES) {
595 dev_err(&oct->pci_dev->dev, "Too many images in firmware file (%d).\n",
596 be32_to_cpu(h->num_images));
597 return -EINVAL;
598 }
599
600 dev_info(&oct->pci_dev->dev, "Firmware version: %s\n", h->version);
601 snprintf(oct->fw_info.liquidio_firmware_version, 32, "LIQUIDIO: %s",
602 h->version);
603
Muhammad Falak R Wani7c542772016-05-19 19:22:49 +0530604 buffer = kmemdup(data, size, GFP_KERNEL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700605 if (!buffer)
606 return -ENOMEM;
607
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700608 p = buffer + sizeof(struct octeon_firmware_file_header);
609
610 /* load all images */
611 for (i = 0; i < be32_to_cpu(h->num_images); i++) {
612 load_addr = be64_to_cpu(h->desc[i].addr);
613 image_len = be32_to_cpu(h->desc[i].len);
614
615 /* validate the image */
616 crc32_result = crc32(~0, p, image_len) ^ ~0U;
617 if (crc32_result != be32_to_cpu(h->desc[i].crc32)) {
618 dev_err(&oct->pci_dev->dev,
619 "Firmware CRC mismatch in image %d (0x%08x != 0x%08x).\n",
620 i, crc32_result,
621 be32_to_cpu(h->desc[i].crc32));
622 ret = -EINVAL;
623 goto done_downloading;
624 }
625
626 /* download the image */
627 octeon_pci_write_core_mem(oct, load_addr, p, image_len);
628
629 p += image_len;
630 dev_dbg(&oct->pci_dev->dev,
631 "Downloaded image %d (%d bytes) to address 0x%016llx\n",
632 i, image_len, load_addr);
633 }
634
635 /* Invoke the bootcmd */
636 ret = octeon_console_send_cmd(oct, h->bootcmd, 50);
637
638done_downloading:
639 kfree(buffer);
640
641 return ret;
642}
643
644void octeon_free_device_mem(struct octeon_device *oct)
645{
646 u32 i;
647
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700648 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700649 /* could check mask as well */
Markus Elfring9686f312015-06-29 12:22:24 +0200650 vfree(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700651 }
652
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700653 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700654 /* could check mask as well */
Markus Elfring9686f312015-06-29 12:22:24 +0200655 vfree(oct->instr_queue[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700656 }
657
658 i = oct->octeon_id;
659 vfree(oct);
660
661 octeon_device[i] = NULL;
662 octeon_device_count--;
663}
664
665static struct octeon_device *octeon_allocate_device_mem(u32 pci_id,
666 u32 priv_size)
667{
668 struct octeon_device *oct;
669 u8 *buf = NULL;
670 u32 octdevsize = 0, configsize = 0, size;
671
672 switch (pci_id) {
673 case OCTEON_CN68XX:
674 case OCTEON_CN66XX:
675 configsize = sizeof(struct octeon_cn6xxx);
676 break;
677
678 default:
679 pr_err("%s: Unknown PCI Device: 0x%x\n",
680 __func__,
681 pci_id);
682 return NULL;
683 }
684
685 if (configsize & 0x7)
686 configsize += (8 - (configsize & 0x7));
687
688 octdevsize = sizeof(struct octeon_device);
689 if (octdevsize & 0x7)
690 octdevsize += (8 - (octdevsize & 0x7));
691
692 if (priv_size & 0x7)
693 priv_size += (8 - (priv_size & 0x7));
694
695 size = octdevsize + priv_size + configsize +
696 (sizeof(struct octeon_dispatch) * DISPATCH_LIST_SIZE);
697
698 buf = vmalloc(size);
699 if (!buf)
700 return NULL;
701
702 memset(buf, 0, size);
703
704 oct = (struct octeon_device *)buf;
705 oct->priv = (void *)(buf + octdevsize);
706 oct->chip = (void *)(buf + octdevsize + priv_size);
707 oct->dispatch.dlist = (struct octeon_dispatch *)
708 (buf + octdevsize + priv_size + configsize);
709
710 return oct;
711}
712
713struct octeon_device *octeon_allocate_device(u32 pci_id,
714 u32 priv_size)
715{
716 u32 oct_idx = 0;
717 struct octeon_device *oct = NULL;
718
719 for (oct_idx = 0; oct_idx < MAX_OCTEON_DEVICES; oct_idx++)
720 if (!octeon_device[oct_idx])
721 break;
722
723 if (oct_idx == MAX_OCTEON_DEVICES)
724 return NULL;
725
726 oct = octeon_allocate_device_mem(pci_id, priv_size);
727 if (!oct)
728 return NULL;
729
730 spin_lock_init(&oct->pci_win_lock);
731 spin_lock_init(&oct->mem_access_lock);
732
733 octeon_device_count++;
734 octeon_device[oct_idx] = oct;
735
736 oct->octeon_id = oct_idx;
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700737 snprintf(oct->device_name, sizeof(oct->device_name),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700738 "LiquidIO%d", (oct->octeon_id));
739
740 return oct;
741}
742
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700743/* this function is only for setting up the first queue */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700744int octeon_setup_instr_queues(struct octeon_device *oct)
745{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700746 u32 num_iqs = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700747 u32 num_descs = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700748 u32 iq_no = 0;
749 union oct_txpciq txpciq;
750 int numa_node = cpu_to_node(iq_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700751
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700752 num_iqs = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700753 /* this causes queue 0 to be default queue */
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700754 if (OCTEON_CN6XXX(oct))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700755 num_descs =
756 CFG_GET_NUM_DEF_TX_DESCS(CHIP_FIELD(oct, cn6xxx, conf));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700757
758 oct->num_iqs = 0;
759
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700760 oct->instr_queue[0] = vmalloc_node(sizeof(*oct->instr_queue[0]),
761 numa_node);
762 if (!oct->instr_queue[0])
763 oct->instr_queue[0] =
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700764 vmalloc(sizeof(struct octeon_instr_queue));
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700765 if (!oct->instr_queue[0])
766 return 1;
767 memset(oct->instr_queue[0], 0, sizeof(struct octeon_instr_queue));
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700768 oct->instr_queue[0]->q_index = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700769 oct->instr_queue[0]->app_ctx = (void *)(size_t)0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700770 oct->instr_queue[0]->ifidx = 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700771 txpciq.u64 = 0;
772 txpciq.s.q_no = iq_no;
773 txpciq.s.use_qpg = 0;
774 txpciq.s.qpg = 0;
775 if (octeon_init_instr_queue(oct, txpciq, num_descs)) {
776 /* prevent memory leak */
777 vfree(oct->instr_queue[0]);
778 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700779 }
780
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700781 oct->num_iqs++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700782 return 0;
783}
784
785int octeon_setup_output_queues(struct octeon_device *oct)
786{
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700787 u32 num_oqs = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700788 u32 num_descs = 0;
789 u32 desc_size = 0;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700790 u32 oq_no = 0;
791 int numa_node = cpu_to_node(oq_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700792
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700793 num_oqs = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700794 /* this causes queue 0 to be default queue */
795 if (OCTEON_CN6XXX(oct)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700796 num_descs =
797 CFG_GET_NUM_DEF_RX_DESCS(CHIP_FIELD(oct, cn6xxx, conf));
798 desc_size =
799 CFG_GET_DEF_RX_BUF_SIZE(CHIP_FIELD(oct, cn6xxx, conf));
800 }
801
802 oct->num_oqs = 0;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700803 oct->droq[0] = vmalloc_node(sizeof(*oct->droq[0]), numa_node);
804 if (!oct->droq[0])
805 oct->droq[0] = vmalloc(sizeof(*oct->droq[0]));
806 if (!oct->droq[0])
807 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700808
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700809 if (octeon_init_droq(oct, oq_no, num_descs, desc_size, NULL))
810 return 1;
811 oct->num_oqs++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700812
813 return 0;
814}
815
816void octeon_set_io_queues_off(struct octeon_device *oct)
817{
818 /* Disable the i/p and o/p queues for this Octeon. */
819
820 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
821 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
822}
823
824void octeon_set_droq_pkt_op(struct octeon_device *oct,
825 u32 q_no,
826 u32 enable)
827{
828 u32 reg_val = 0;
829
830 /* Disable the i/p and o/p queues for this Octeon. */
831 reg_val = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
832
833 if (enable)
834 reg_val = reg_val | (1 << q_no);
835 else
836 reg_val = reg_val & (~(1 << q_no));
837
838 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, reg_val);
839}
840
841int octeon_init_dispatch_list(struct octeon_device *oct)
842{
843 u32 i;
844
845 oct->dispatch.count = 0;
846
847 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
848 oct->dispatch.dlist[i].opcode = 0;
849 INIT_LIST_HEAD(&oct->dispatch.dlist[i].list);
850 }
851
852 for (i = 0; i <= REQTYPE_LAST; i++)
853 octeon_register_reqtype_free_fn(oct, i, NULL);
854
855 spin_lock_init(&oct->dispatch.lock);
856
857 return 0;
858}
859
860void octeon_delete_dispatch_list(struct octeon_device *oct)
861{
862 u32 i;
863 struct list_head freelist, *temp, *tmp2;
864
865 INIT_LIST_HEAD(&freelist);
866
867 spin_lock_bh(&oct->dispatch.lock);
868
869 for (i = 0; i < DISPATCH_LIST_SIZE; i++) {
870 struct list_head *dispatch;
871
872 dispatch = &oct->dispatch.dlist[i].list;
873 while (dispatch->next != dispatch) {
874 temp = dispatch->next;
875 list_del(temp);
876 list_add_tail(temp, &freelist);
877 }
878
879 oct->dispatch.dlist[i].opcode = 0;
880 }
881
882 oct->dispatch.count = 0;
883
884 spin_unlock_bh(&oct->dispatch.lock);
885
886 list_for_each_safe(temp, tmp2, &freelist) {
887 list_del(temp);
888 vfree(temp);
889 }
890}
891
892octeon_dispatch_fn_t
893octeon_get_dispatch(struct octeon_device *octeon_dev, u16 opcode,
894 u16 subcode)
895{
896 u32 idx;
897 struct list_head *dispatch;
898 octeon_dispatch_fn_t fn = NULL;
899 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
900
901 idx = combined_opcode & OCTEON_OPCODE_MASK;
902
903 spin_lock_bh(&octeon_dev->dispatch.lock);
904
905 if (octeon_dev->dispatch.count == 0) {
906 spin_unlock_bh(&octeon_dev->dispatch.lock);
907 return NULL;
908 }
909
910 if (!(octeon_dev->dispatch.dlist[idx].opcode)) {
911 spin_unlock_bh(&octeon_dev->dispatch.lock);
912 return NULL;
913 }
914
915 if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
916 fn = octeon_dev->dispatch.dlist[idx].dispatch_fn;
917 } else {
918 list_for_each(dispatch,
919 &octeon_dev->dispatch.dlist[idx].list) {
920 if (((struct octeon_dispatch *)dispatch)->opcode ==
921 combined_opcode) {
922 fn = ((struct octeon_dispatch *)
923 dispatch)->dispatch_fn;
924 break;
925 }
926 }
927 }
928
929 spin_unlock_bh(&octeon_dev->dispatch.lock);
930 return fn;
931}
932
933/* octeon_register_dispatch_fn
934 * Parameters:
935 * octeon_id - id of the octeon device.
936 * opcode - opcode for which driver should call the registered function
937 * subcode - subcode for which driver should call the registered function
938 * fn - The function to call when a packet with "opcode" arrives in
939 * octeon output queues.
940 * fn_arg - The argument to be passed when calling function "fn".
941 * Description:
942 * Registers a function and its argument to be called when a packet
943 * arrives in Octeon output queues with "opcode".
944 * Returns:
945 * Success: 0
946 * Failure: 1
947 * Locks:
948 * No locks are held.
949 */
950int
951octeon_register_dispatch_fn(struct octeon_device *oct,
952 u16 opcode,
953 u16 subcode,
954 octeon_dispatch_fn_t fn, void *fn_arg)
955{
956 u32 idx;
957 octeon_dispatch_fn_t pfn;
958 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
959
960 idx = combined_opcode & OCTEON_OPCODE_MASK;
961
962 spin_lock_bh(&oct->dispatch.lock);
963 /* Add dispatch function to first level of lookup table */
964 if (oct->dispatch.dlist[idx].opcode == 0) {
965 oct->dispatch.dlist[idx].opcode = combined_opcode;
966 oct->dispatch.dlist[idx].dispatch_fn = fn;
967 oct->dispatch.dlist[idx].arg = fn_arg;
968 oct->dispatch.count++;
969 spin_unlock_bh(&oct->dispatch.lock);
970 return 0;
971 }
972
973 spin_unlock_bh(&oct->dispatch.lock);
974
975 /* Check if there was a function already registered for this
976 * opcode/subcode.
977 */
978 pfn = octeon_get_dispatch(oct, opcode, subcode);
979 if (!pfn) {
980 struct octeon_dispatch *dispatch;
981
982 dev_dbg(&oct->pci_dev->dev,
983 "Adding opcode to dispatch list linked list\n");
984 dispatch = (struct octeon_dispatch *)
985 vmalloc(sizeof(struct octeon_dispatch));
986 if (!dispatch) {
987 dev_err(&oct->pci_dev->dev,
988 "No memory to add dispatch function\n");
989 return 1;
990 }
991 dispatch->opcode = combined_opcode;
992 dispatch->dispatch_fn = fn;
993 dispatch->arg = fn_arg;
994
995 /* Add dispatch function to linked list of fn ptrs
996 * at the hashed index.
997 */
998 spin_lock_bh(&oct->dispatch.lock);
999 list_add(&dispatch->list, &oct->dispatch.dlist[idx].list);
1000 oct->dispatch.count++;
1001 spin_unlock_bh(&oct->dispatch.lock);
1002
1003 } else {
1004 dev_err(&oct->pci_dev->dev,
1005 "Found previously registered dispatch fn for opcode/subcode: %x/%x\n",
1006 opcode, subcode);
1007 return 1;
1008 }
1009
1010 return 0;
1011}
1012
1013/* octeon_unregister_dispatch_fn
1014 * Parameters:
1015 * oct - octeon device
1016 * opcode - driver should unregister the function for this opcode
1017 * subcode - driver should unregister the function for this subcode
1018 * Description:
1019 * Unregister the function set for this opcode+subcode.
1020 * Returns:
1021 * Success: 0
1022 * Failure: 1
1023 * Locks:
1024 * No locks are held.
1025 */
1026int
1027octeon_unregister_dispatch_fn(struct octeon_device *oct, u16 opcode,
1028 u16 subcode)
1029{
1030 int retval = 0;
1031 u32 idx;
1032 struct list_head *dispatch, *dfree = NULL, *tmp2;
1033 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
1034
1035 idx = combined_opcode & OCTEON_OPCODE_MASK;
1036
1037 spin_lock_bh(&oct->dispatch.lock);
1038
1039 if (oct->dispatch.count == 0) {
1040 spin_unlock_bh(&oct->dispatch.lock);
1041 dev_err(&oct->pci_dev->dev,
1042 "No dispatch functions registered for this device\n");
1043 return 1;
1044 }
1045
1046 if (oct->dispatch.dlist[idx].opcode == combined_opcode) {
1047 dispatch = &oct->dispatch.dlist[idx].list;
1048 if (dispatch->next != dispatch) {
1049 dispatch = dispatch->next;
1050 oct->dispatch.dlist[idx].opcode =
1051 ((struct octeon_dispatch *)dispatch)->opcode;
1052 oct->dispatch.dlist[idx].dispatch_fn =
1053 ((struct octeon_dispatch *)
1054 dispatch)->dispatch_fn;
1055 oct->dispatch.dlist[idx].arg =
1056 ((struct octeon_dispatch *)dispatch)->arg;
1057 list_del(dispatch);
1058 dfree = dispatch;
1059 } else {
1060 oct->dispatch.dlist[idx].opcode = 0;
1061 oct->dispatch.dlist[idx].dispatch_fn = NULL;
1062 oct->dispatch.dlist[idx].arg = NULL;
1063 }
1064 } else {
1065 retval = 1;
1066 list_for_each_safe(dispatch, tmp2,
1067 &(oct->dispatch.dlist[idx].
1068 list)) {
1069 if (((struct octeon_dispatch *)dispatch)->opcode ==
1070 combined_opcode) {
1071 list_del(dispatch);
1072 dfree = dispatch;
1073 retval = 0;
1074 }
1075 }
1076 }
1077
1078 if (!retval)
1079 oct->dispatch.count--;
1080
1081 spin_unlock_bh(&oct->dispatch.lock);
Markus Elfring9686f312015-06-29 12:22:24 +02001082 vfree(dfree);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001083 return retval;
1084}
1085
1086int octeon_core_drv_init(struct octeon_recv_info *recv_info, void *buf)
1087{
1088 u32 i;
1089 char app_name[16];
1090 struct octeon_device *oct = (struct octeon_device *)buf;
1091 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
1092 struct octeon_core_setup *cs = NULL;
1093 u32 num_nic_ports = 0;
1094
1095 if (OCTEON_CN6XXX(oct))
1096 num_nic_ports =
1097 CFG_GET_NUM_NIC_PORTS(CHIP_FIELD(oct, cn6xxx, conf));
1098
1099 if (atomic_read(&oct->status) >= OCT_DEV_RUNNING) {
1100 dev_err(&oct->pci_dev->dev, "Received CORE OK when device state is 0x%x\n",
1101 atomic_read(&oct->status));
1102 goto core_drv_init_err;
1103 }
1104
1105 strncpy(app_name,
1106 get_oct_app_string(
1107 (u32)recv_pkt->rh.r_core_drv_init.app_mode),
1108 sizeof(app_name) - 1);
1109 oct->app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07001110 if (recv_pkt->rh.r_core_drv_init.app_mode == CVM_DRV_NIC_APP) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001111 oct->fw_info.max_nic_ports =
1112 (u32)recv_pkt->rh.r_core_drv_init.max_nic_ports;
1113 oct->fw_info.num_gmx_ports =
1114 (u32)recv_pkt->rh.r_core_drv_init.num_gmx_ports;
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07001115 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001116
1117 if (oct->fw_info.max_nic_ports < num_nic_ports) {
1118 dev_err(&oct->pci_dev->dev,
1119 "Config has more ports than firmware allows (%d > %d).\n",
1120 num_nic_ports, oct->fw_info.max_nic_ports);
1121 goto core_drv_init_err;
1122 }
1123 oct->fw_info.app_cap_flags = recv_pkt->rh.r_core_drv_init.app_cap_flags;
1124 oct->fw_info.app_mode = (u32)recv_pkt->rh.r_core_drv_init.app_mode;
1125
1126 atomic_set(&oct->status, OCT_DEV_CORE_OK);
1127
1128 cs = &core_setup[oct->octeon_id];
1129
1130 if (recv_pkt->buffer_size[0] != sizeof(*cs)) {
1131 dev_dbg(&oct->pci_dev->dev, "Core setup bytes expected %u found %d\n",
1132 (u32)sizeof(*cs),
1133 recv_pkt->buffer_size[0]);
1134 }
1135
1136 memcpy(cs, get_rbd(recv_pkt->buffer_ptr[0]), sizeof(*cs));
1137 strncpy(oct->boardinfo.name, cs->boardname, OCT_BOARD_NAME);
1138 strncpy(oct->boardinfo.serial_number, cs->board_serial_number,
1139 OCT_SERIAL_LEN);
1140
1141 octeon_swap_8B_data((u64 *)cs, (sizeof(*cs) >> 3));
1142
1143 oct->boardinfo.major = cs->board_rev_major;
1144 oct->boardinfo.minor = cs->board_rev_minor;
1145
1146 dev_info(&oct->pci_dev->dev,
1147 "Running %s (%llu Hz)\n",
1148 app_name, CVM_CAST64(cs->corefreq));
1149
1150core_drv_init_err:
1151 for (i = 0; i < recv_pkt->buffer_count; i++)
1152 recv_buffer_free(recv_pkt->buffer_ptr[i]);
1153 octeon_free_recv_info(recv_info);
1154 return 0;
1155}
1156
1157int octeon_get_tx_qsize(struct octeon_device *oct, u32 q_no)
1158
1159{
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001160 if (oct && (q_no < MAX_OCTEON_INSTR_QUEUES(oct)) &&
1161 (oct->io_qmask.iq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001162 return oct->instr_queue[q_no]->max_count;
1163
1164 return -1;
1165}
1166
1167int octeon_get_rx_qsize(struct octeon_device *oct, u32 q_no)
1168{
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001169 if (oct && (q_no < MAX_OCTEON_OUTPUT_QUEUES(oct)) &&
1170 (oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001171 return oct->droq[q_no]->max_count;
1172 return -1;
1173}
1174
1175/* Retruns the host firmware handshake OCTEON specific configuration */
1176struct octeon_config *octeon_get_conf(struct octeon_device *oct)
1177{
1178 struct octeon_config *default_oct_conf = NULL;
1179
1180 /* check the OCTEON Device model & return the corresponding octeon
1181 * configuration
1182 */
1183
1184 if (OCTEON_CN6XXX(oct)) {
1185 default_oct_conf =
1186 (struct octeon_config *)(CHIP_FIELD(oct, cn6xxx, conf));
1187 }
1188
1189 return default_oct_conf;
1190}
1191
1192/* scratch register address is same in all the OCT-II and CN70XX models */
1193#define CNXX_SLI_SCRATCH1 0x3C0
1194
1195/** Get the octeon device pointer.
1196 * @param octeon_id - The id for which the octeon device pointer is required.
1197 * @return Success: Octeon device pointer.
1198 * @return Failure: NULL.
1199 */
1200struct octeon_device *lio_get_device(u32 octeon_id)
1201{
1202 if (octeon_id >= MAX_OCTEON_DEVICES)
1203 return NULL;
1204 else
1205 return octeon_device[octeon_id];
1206}
1207
1208u64 lio_pci_readq(struct octeon_device *oct, u64 addr)
1209{
1210 u64 val64;
1211 unsigned long flags;
1212 u32 val32, addrhi;
1213
1214 spin_lock_irqsave(&oct->pci_win_lock, flags);
1215
1216 /* The windowed read happens when the LSB of the addr is written.
1217 * So write MSB first
1218 */
1219 addrhi = (addr >> 32);
1220 if ((oct->chip_id == OCTEON_CN66XX) || (oct->chip_id == OCTEON_CN68XX))
1221 addrhi |= 0x00060000;
1222 writel(addrhi, oct->reg_list.pci_win_rd_addr_hi);
1223
1224 /* Read back to preserve ordering of writes */
1225 val32 = readl(oct->reg_list.pci_win_rd_addr_hi);
1226
1227 writel(addr & 0xffffffff, oct->reg_list.pci_win_rd_addr_lo);
1228 val32 = readl(oct->reg_list.pci_win_rd_addr_lo);
1229
1230 val64 = readq(oct->reg_list.pci_win_rd_data);
1231
1232 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1233
1234 return val64;
1235}
1236
1237void lio_pci_writeq(struct octeon_device *oct,
1238 u64 val,
1239 u64 addr)
1240{
1241 u32 val32;
1242 unsigned long flags;
1243
1244 spin_lock_irqsave(&oct->pci_win_lock, flags);
1245
1246 writeq(addr, oct->reg_list.pci_win_wr_addr);
1247
1248 /* The write happens when the LSB is written. So write MSB first. */
1249 writel(val >> 32, oct->reg_list.pci_win_wr_data_hi);
1250 /* Read the MSB to ensure ordering of writes. */
1251 val32 = readl(oct->reg_list.pci_win_wr_data_hi);
1252
1253 writel(val & 0xffffffff, oct->reg_list.pci_win_wr_data_lo);
1254
1255 spin_unlock_irqrestore(&oct->pci_win_lock, flags);
1256}
1257
1258int octeon_mem_access_ok(struct octeon_device *oct)
1259{
1260 u64 access_okay = 0;
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001261 u64 lmc0_reset_ctl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001262
1263 /* Check to make sure a DDR interface is enabled */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001264 lmc0_reset_ctl = lio_pci_readq(oct, CN6XXX_LMC0_RESET_CTL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001265 access_okay = (lmc0_reset_ctl & CN6XXX_LMC0_RESET_CTL_DDR3RST_MASK);
1266
1267 return access_okay ? 0 : 1;
1268}
1269
1270int octeon_wait_for_ddr_init(struct octeon_device *oct, u32 *timeout)
1271{
1272 int ret = 1;
1273 u32 ms;
1274
1275 if (!timeout)
1276 return ret;
1277
1278 while (*timeout == 0)
1279 schedule_timeout_uninterruptible(HZ / 10);
1280
1281 for (ms = 0; (ret != 0) && ((*timeout == 0) || (ms <= *timeout));
1282 ms += HZ / 10) {
1283 ret = octeon_mem_access_ok(oct);
1284
1285 /* wait 100 ms */
1286 if (ret)
1287 schedule_timeout_uninterruptible(HZ / 10);
1288 }
1289
1290 return ret;
1291}
1292
1293/** Get the octeon id assigned to the octeon device passed as argument.
1294 * This function is exported to other modules.
1295 * @param dev - octeon device pointer passed as a void *.
1296 * @return octeon device id
1297 */
1298int lio_get_device_id(void *dev)
1299{
1300 struct octeon_device *octeon_dev = (struct octeon_device *)dev;
1301 u32 i;
1302
1303 for (i = 0; i < MAX_OCTEON_DEVICES; i++)
1304 if (octeon_device[i] == octeon_dev)
1305 return octeon_dev->octeon_id;
1306 return -1;
1307}