1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */ 3 4 #include <linux/kernel.h> 5 #include <linux/module.h> 6 #include <linux/export.h> 7 #include <linux/err.h> 8 #include <linux/device.h> 9 #include <linux/pci.h> 10 #include <linux/interrupt.h> 11 #include <linux/wait.h> 12 #include <linux/types.h> 13 #include <linux/skbuff.h> 14 #include <linux/if_vlan.h> 15 #include <linux/log2.h> 16 #include <linux/string.h> 17 18 #include "pci_hw.h" 19 #include "pci.h" 20 #include "core.h" 21 #include "cmd.h" 22 #include "port.h" 23 #include "resources.h" 24 25 #define mlxsw_pci_write32(mlxsw_pci, reg, val) \ 26 iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg)) 27 #define mlxsw_pci_read32(mlxsw_pci, reg) \ 28 ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg)) 29 30 enum mlxsw_pci_queue_type { 31 MLXSW_PCI_QUEUE_TYPE_SDQ, 32 MLXSW_PCI_QUEUE_TYPE_RDQ, 33 MLXSW_PCI_QUEUE_TYPE_CQ, 34 MLXSW_PCI_QUEUE_TYPE_EQ, 35 }; 36 37 #define MLXSW_PCI_QUEUE_TYPE_COUNT 4 38 39 static const u16 mlxsw_pci_doorbell_type_offset[] = { 40 MLXSW_PCI_DOORBELL_SDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_SDQ */ 41 MLXSW_PCI_DOORBELL_RDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_RDQ */ 42 MLXSW_PCI_DOORBELL_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */ 43 MLXSW_PCI_DOORBELL_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */ 44 }; 45 46 static const u16 mlxsw_pci_doorbell_arm_type_offset[] = { 47 0, /* unused */ 48 0, /* unused */ 49 MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */ 50 MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */ 51 }; 52 53 struct mlxsw_pci_mem_item { 54 char *buf; 55 dma_addr_t mapaddr; 56 size_t size; 57 }; 58 59 struct mlxsw_pci_queue_elem_info { 60 char *elem; /* pointer to actual dma mapped element mem chunk */ 61 union { 62 struct { 63 struct sk_buff *skb; 64 } sdq; 65 struct { 66 struct sk_buff *skb; 67 } rdq; 68 } u; 69 }; 70 71 struct mlxsw_pci_queue { 72 spinlock_t lock; /* for queue accesses */ 73 struct mlxsw_pci_mem_item mem_item; 74 struct mlxsw_pci_queue_elem_info *elem_info; 75 u16 producer_counter; 76 u16 consumer_counter; 77 u16 count; /* number of elements in queue */ 78 u8 num; /* queue number */ 79 u8 elem_size; /* size of one element */ 80 enum mlxsw_pci_queue_type type; 81 struct tasklet_struct tasklet; /* queue processing tasklet */ 82 struct mlxsw_pci *pci; 83 union { 84 struct { 85 u32 comp_sdq_count; 86 u32 comp_rdq_count; 87 enum mlxsw_pci_cqe_v v; 88 } cq; 89 struct { 90 u32 ev_cmd_count; 91 u32 ev_comp_count; 92 u32 ev_other_count; 93 } eq; 94 } u; 95 }; 96 97 struct mlxsw_pci_queue_type_group { 98 struct mlxsw_pci_queue *q; 99 u8 count; /* number of queues in group */ 100 }; 101 102 struct mlxsw_pci { 103 struct pci_dev *pdev; 104 u8 __iomem *hw_addr; 105 u64 free_running_clock_offset; 106 struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT]; 107 u32 doorbell_offset; 108 struct mlxsw_core *core; 109 struct { 110 struct mlxsw_pci_mem_item *items; 111 unsigned int count; 112 } fw_area; 113 struct { 114 struct mlxsw_pci_mem_item out_mbox; 115 struct mlxsw_pci_mem_item in_mbox; 116 struct mutex lock; /* Lock access to command registers */ 117 bool nopoll; 118 wait_queue_head_t wait; 119 bool wait_done; 120 struct { 121 u8 status; 122 u64 out_param; 123 } comp; 124 } cmd; 125 struct mlxsw_bus_info bus_info; 126 const struct pci_device_id *id; 127 enum mlxsw_pci_cqe_v max_cqe_ver; /* Maximal supported CQE version */ 128 u8 num_sdq_cqs; /* Number of CQs used for SDQs */ 129 }; 130 131 static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q) 132 { 133 tasklet_schedule(&q->tasklet); 134 } 135 136 static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, 137 size_t elem_size, int elem_index) 138 { 139 return q->mem_item.buf + (elem_size * elem_index); 140 } 141 142 static struct mlxsw_pci_queue_elem_info * 143 mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index) 144 { 145 return &q->elem_info[elem_index]; 146 } 147 148 static struct mlxsw_pci_queue_elem_info * 149 mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q) 150 { 151 int index = q->producer_counter & (q->count - 1); 152 153 if ((u16) (q->producer_counter - q->consumer_counter) == q->count) 154 return NULL; 155 return mlxsw_pci_queue_elem_info_get(q, index); 156 } 157 158 static struct mlxsw_pci_queue_elem_info * 159 mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q) 160 { 161 int index = q->consumer_counter & (q->count - 1); 162 163 return mlxsw_pci_queue_elem_info_get(q, index); 164 } 165 166 static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index) 167 { 168 return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem; 169 } 170 171 static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit) 172 { 173 return owner_bit != !!(q->consumer_counter & q->count); 174 } 175 176 static struct mlxsw_pci_queue_type_group * 177 mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci, 178 enum mlxsw_pci_queue_type q_type) 179 { 180 return &mlxsw_pci->queues[q_type]; 181 } 182 183 static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci, 184 enum mlxsw_pci_queue_type q_type) 185 { 186 struct mlxsw_pci_queue_type_group *queue_group; 187 188 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type); 189 return queue_group->count; 190 } 191 192 static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci) 193 { 194 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ); 195 } 196 197 static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci) 198 { 199 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ); 200 } 201 202 static struct mlxsw_pci_queue * 203 __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci, 204 enum mlxsw_pci_queue_type q_type, u8 q_num) 205 { 206 return &mlxsw_pci->queues[q_type].q[q_num]; 207 } 208 209 static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci, 210 u8 q_num) 211 { 212 return __mlxsw_pci_queue_get(mlxsw_pci, 213 MLXSW_PCI_QUEUE_TYPE_SDQ, q_num); 214 } 215 216 static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci, 217 u8 q_num) 218 { 219 return __mlxsw_pci_queue_get(mlxsw_pci, 220 MLXSW_PCI_QUEUE_TYPE_RDQ, q_num); 221 } 222 223 static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci, 224 u8 q_num) 225 { 226 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num); 227 } 228 229 static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci, 230 u8 q_num) 231 { 232 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num); 233 } 234 235 static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci, 236 struct mlxsw_pci_queue *q, 237 u16 val) 238 { 239 mlxsw_pci_write32(mlxsw_pci, 240 DOORBELL(mlxsw_pci->doorbell_offset, 241 mlxsw_pci_doorbell_type_offset[q->type], 242 q->num), val); 243 } 244 245 static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci, 246 struct mlxsw_pci_queue *q, 247 u16 val) 248 { 249 mlxsw_pci_write32(mlxsw_pci, 250 DOORBELL(mlxsw_pci->doorbell_offset, 251 mlxsw_pci_doorbell_arm_type_offset[q->type], 252 q->num), val); 253 } 254 255 static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci, 256 struct mlxsw_pci_queue *q) 257 { 258 wmb(); /* ensure all writes are done before we ring a bell */ 259 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter); 260 } 261 262 static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci, 263 struct mlxsw_pci_queue *q) 264 { 265 wmb(); /* ensure all writes are done before we ring a bell */ 266 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, 267 q->consumer_counter + q->count); 268 } 269 270 static void 271 mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci, 272 struct mlxsw_pci_queue *q) 273 { 274 wmb(); /* ensure all writes are done before we ring a bell */ 275 __mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter); 276 } 277 278 static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q, 279 int page_index) 280 { 281 return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index; 282 } 283 284 static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 285 struct mlxsw_pci_queue *q) 286 { 287 int i; 288 int err; 289 290 q->producer_counter = 0; 291 q->consumer_counter = 0; 292 293 /* Set CQ of same number of this SDQ. */ 294 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num); 295 mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 3); 296 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */ 297 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) { 298 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i); 299 300 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr); 301 } 302 303 err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num); 304 if (err) 305 return err; 306 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q); 307 return 0; 308 } 309 310 static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci, 311 struct mlxsw_pci_queue *q) 312 { 313 mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num); 314 } 315 316 static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe, 317 int index, char *frag_data, size_t frag_len, 318 int direction) 319 { 320 struct pci_dev *pdev = mlxsw_pci->pdev; 321 dma_addr_t mapaddr; 322 323 mapaddr = pci_map_single(pdev, frag_data, frag_len, direction); 324 if (unlikely(pci_dma_mapping_error(pdev, mapaddr))) { 325 dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n"); 326 return -EIO; 327 } 328 mlxsw_pci_wqe_address_set(wqe, index, mapaddr); 329 mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len); 330 return 0; 331 } 332 333 static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe, 334 int index, int direction) 335 { 336 struct pci_dev *pdev = mlxsw_pci->pdev; 337 size_t frag_len = mlxsw_pci_wqe_byte_count_get(wqe, index); 338 dma_addr_t mapaddr = mlxsw_pci_wqe_address_get(wqe, index); 339 340 if (!frag_len) 341 return; 342 pci_unmap_single(pdev, mapaddr, frag_len, direction); 343 } 344 345 static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci, 346 struct mlxsw_pci_queue_elem_info *elem_info) 347 { 348 size_t buf_len = MLXSW_PORT_MAX_MTU; 349 char *wqe = elem_info->elem; 350 struct sk_buff *skb; 351 int err; 352 353 elem_info->u.rdq.skb = NULL; 354 skb = netdev_alloc_skb_ip_align(NULL, buf_len); 355 if (!skb) 356 return -ENOMEM; 357 358 /* Assume that wqe was previously zeroed. */ 359 360 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data, 361 buf_len, DMA_FROM_DEVICE); 362 if (err) 363 goto err_frag_map; 364 365 elem_info->u.rdq.skb = skb; 366 return 0; 367 368 err_frag_map: 369 dev_kfree_skb_any(skb); 370 return err; 371 } 372 373 static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci, 374 struct mlxsw_pci_queue_elem_info *elem_info) 375 { 376 struct sk_buff *skb; 377 char *wqe; 378 379 skb = elem_info->u.rdq.skb; 380 wqe = elem_info->elem; 381 382 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE); 383 dev_kfree_skb_any(skb); 384 } 385 386 static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 387 struct mlxsw_pci_queue *q) 388 { 389 struct mlxsw_pci_queue_elem_info *elem_info; 390 u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci); 391 int i; 392 int err; 393 394 q->producer_counter = 0; 395 q->consumer_counter = 0; 396 397 /* Set CQ of same number of this RDQ with base 398 * above SDQ count as the lower ones are assigned to SDQs. 399 */ 400 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num); 401 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */ 402 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) { 403 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i); 404 405 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr); 406 } 407 408 err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num); 409 if (err) 410 return err; 411 412 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q); 413 414 for (i = 0; i < q->count; i++) { 415 elem_info = mlxsw_pci_queue_elem_info_producer_get(q); 416 BUG_ON(!elem_info); 417 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info); 418 if (err) 419 goto rollback; 420 /* Everything is set up, ring doorbell to pass elem to HW */ 421 q->producer_counter++; 422 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q); 423 } 424 425 return 0; 426 427 rollback: 428 for (i--; i >= 0; i--) { 429 elem_info = mlxsw_pci_queue_elem_info_get(q, i); 430 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info); 431 } 432 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num); 433 434 return err; 435 } 436 437 static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci, 438 struct mlxsw_pci_queue *q) 439 { 440 struct mlxsw_pci_queue_elem_info *elem_info; 441 int i; 442 443 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num); 444 for (i = 0; i < q->count; i++) { 445 elem_info = mlxsw_pci_queue_elem_info_get(q, i); 446 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info); 447 } 448 } 449 450 static void mlxsw_pci_cq_pre_init(struct mlxsw_pci *mlxsw_pci, 451 struct mlxsw_pci_queue *q) 452 { 453 q->u.cq.v = mlxsw_pci->max_cqe_ver; 454 455 /* For SDQ it is pointless to use CQEv2, so use CQEv1 instead */ 456 if (q->u.cq.v == MLXSW_PCI_CQE_V2 && 457 q->num < mlxsw_pci->num_sdq_cqs) 458 q->u.cq.v = MLXSW_PCI_CQE_V1; 459 } 460 461 static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 462 struct mlxsw_pci_queue *q) 463 { 464 int i; 465 int err; 466 467 q->consumer_counter = 0; 468 469 for (i = 0; i < q->count; i++) { 470 char *elem = mlxsw_pci_queue_elem_get(q, i); 471 472 mlxsw_pci_cqe_owner_set(q->u.cq.v, elem, 1); 473 } 474 475 if (q->u.cq.v == MLXSW_PCI_CQE_V1) 476 mlxsw_cmd_mbox_sw2hw_cq_cqe_ver_set(mbox, 477 MLXSW_CMD_MBOX_SW2HW_CQ_CQE_VER_1); 478 else if (q->u.cq.v == MLXSW_PCI_CQE_V2) 479 mlxsw_cmd_mbox_sw2hw_cq_cqe_ver_set(mbox, 480 MLXSW_CMD_MBOX_SW2HW_CQ_CQE_VER_2); 481 482 mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM); 483 mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0); 484 mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count)); 485 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) { 486 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i); 487 488 mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr); 489 } 490 err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num); 491 if (err) 492 return err; 493 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q); 494 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q); 495 return 0; 496 } 497 498 static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci, 499 struct mlxsw_pci_queue *q) 500 { 501 mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num); 502 } 503 504 static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci, 505 struct mlxsw_pci_queue *q, 506 u16 consumer_counter_limit, 507 char *cqe) 508 { 509 struct pci_dev *pdev = mlxsw_pci->pdev; 510 struct mlxsw_pci_queue_elem_info *elem_info; 511 char *wqe; 512 struct sk_buff *skb; 513 int i; 514 515 spin_lock(&q->lock); 516 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q); 517 skb = elem_info->u.sdq.skb; 518 wqe = elem_info->elem; 519 for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++) 520 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE); 521 dev_kfree_skb_any(skb); 522 elem_info->u.sdq.skb = NULL; 523 524 if (q->consumer_counter++ != consumer_counter_limit) 525 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n"); 526 spin_unlock(&q->lock); 527 } 528 529 static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci, 530 struct mlxsw_pci_queue *q, 531 u16 consumer_counter_limit, 532 enum mlxsw_pci_cqe_v cqe_v, char *cqe) 533 { 534 struct pci_dev *pdev = mlxsw_pci->pdev; 535 struct mlxsw_pci_queue_elem_info *elem_info; 536 char *wqe; 537 struct sk_buff *skb; 538 struct mlxsw_rx_info rx_info; 539 u16 byte_count; 540 int err; 541 542 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q); 543 skb = elem_info->u.sdq.skb; 544 if (!skb) 545 return; 546 wqe = elem_info->elem; 547 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE); 548 549 if (q->consumer_counter++ != consumer_counter_limit) 550 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n"); 551 552 if (mlxsw_pci_cqe_lag_get(cqe_v, cqe)) { 553 rx_info.is_lag = true; 554 rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe_v, cqe); 555 rx_info.lag_port_index = 556 mlxsw_pci_cqe_lag_subport_get(cqe_v, cqe); 557 } else { 558 rx_info.is_lag = false; 559 rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe); 560 } 561 562 rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe); 563 564 byte_count = mlxsw_pci_cqe_byte_count_get(cqe); 565 if (mlxsw_pci_cqe_crc_get(cqe_v, cqe)) 566 byte_count -= ETH_FCS_LEN; 567 skb_put(skb, byte_count); 568 mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info); 569 570 memset(wqe, 0, q->elem_size); 571 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info); 572 if (err) 573 dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n"); 574 /* Everything is set up, ring doorbell to pass elem to HW */ 575 q->producer_counter++; 576 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q); 577 return; 578 } 579 580 static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q) 581 { 582 struct mlxsw_pci_queue_elem_info *elem_info; 583 char *elem; 584 bool owner_bit; 585 586 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q); 587 elem = elem_info->elem; 588 owner_bit = mlxsw_pci_cqe_owner_get(q->u.cq.v, elem); 589 if (mlxsw_pci_elem_hw_owned(q, owner_bit)) 590 return NULL; 591 q->consumer_counter++; 592 rmb(); /* make sure we read owned bit before the rest of elem */ 593 return elem; 594 } 595 596 static void mlxsw_pci_cq_tasklet(unsigned long data) 597 { 598 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data; 599 struct mlxsw_pci *mlxsw_pci = q->pci; 600 char *cqe; 601 int items = 0; 602 int credits = q->count >> 1; 603 604 while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) { 605 u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe); 606 u8 sendq = mlxsw_pci_cqe_sr_get(q->u.cq.v, cqe); 607 u8 dqn = mlxsw_pci_cqe_dqn_get(q->u.cq.v, cqe); 608 char ncqe[MLXSW_PCI_CQE_SIZE_MAX]; 609 610 memcpy(ncqe, cqe, q->elem_size); 611 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q); 612 613 if (sendq) { 614 struct mlxsw_pci_queue *sdq; 615 616 sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn); 617 mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq, 618 wqe_counter, ncqe); 619 q->u.cq.comp_sdq_count++; 620 } else { 621 struct mlxsw_pci_queue *rdq; 622 623 rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn); 624 mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq, 625 wqe_counter, q->u.cq.v, ncqe); 626 q->u.cq.comp_rdq_count++; 627 } 628 if (++items == credits) 629 break; 630 } 631 if (items) 632 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q); 633 } 634 635 static u16 mlxsw_pci_cq_elem_count(const struct mlxsw_pci_queue *q) 636 { 637 return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_COUNT : 638 MLXSW_PCI_CQE01_COUNT; 639 } 640 641 static u8 mlxsw_pci_cq_elem_size(const struct mlxsw_pci_queue *q) 642 { 643 return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_SIZE : 644 MLXSW_PCI_CQE01_SIZE; 645 } 646 647 static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 648 struct mlxsw_pci_queue *q) 649 { 650 int i; 651 int err; 652 653 q->consumer_counter = 0; 654 655 for (i = 0; i < q->count; i++) { 656 char *elem = mlxsw_pci_queue_elem_get(q, i); 657 658 mlxsw_pci_eqe_owner_set(elem, 1); 659 } 660 661 mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */ 662 mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */ 663 mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count)); 664 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) { 665 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i); 666 667 mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr); 668 } 669 err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num); 670 if (err) 671 return err; 672 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q); 673 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q); 674 return 0; 675 } 676 677 static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci, 678 struct mlxsw_pci_queue *q) 679 { 680 mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num); 681 } 682 683 static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe) 684 { 685 mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe); 686 mlxsw_pci->cmd.comp.out_param = 687 ((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 | 688 mlxsw_pci_eqe_cmd_out_param_l_get(eqe); 689 mlxsw_pci->cmd.wait_done = true; 690 wake_up(&mlxsw_pci->cmd.wait); 691 } 692 693 static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q) 694 { 695 struct mlxsw_pci_queue_elem_info *elem_info; 696 char *elem; 697 bool owner_bit; 698 699 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q); 700 elem = elem_info->elem; 701 owner_bit = mlxsw_pci_eqe_owner_get(elem); 702 if (mlxsw_pci_elem_hw_owned(q, owner_bit)) 703 return NULL; 704 q->consumer_counter++; 705 rmb(); /* make sure we read owned bit before the rest of elem */ 706 return elem; 707 } 708 709 static void mlxsw_pci_eq_tasklet(unsigned long data) 710 { 711 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data; 712 struct mlxsw_pci *mlxsw_pci = q->pci; 713 u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci); 714 unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)]; 715 char *eqe; 716 u8 cqn; 717 bool cq_handle = false; 718 int items = 0; 719 int credits = q->count >> 1; 720 721 memset(&active_cqns, 0, sizeof(active_cqns)); 722 723 while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) { 724 725 /* Command interface completion events are always received on 726 * queue MLXSW_PCI_EQ_ASYNC_NUM (EQ0) and completion events 727 * are mapped to queue MLXSW_PCI_EQ_COMP_NUM (EQ1). 728 */ 729 switch (q->num) { 730 case MLXSW_PCI_EQ_ASYNC_NUM: 731 mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe); 732 q->u.eq.ev_cmd_count++; 733 break; 734 case MLXSW_PCI_EQ_COMP_NUM: 735 cqn = mlxsw_pci_eqe_cqn_get(eqe); 736 set_bit(cqn, active_cqns); 737 cq_handle = true; 738 q->u.eq.ev_comp_count++; 739 break; 740 default: 741 q->u.eq.ev_other_count++; 742 } 743 if (++items == credits) 744 break; 745 } 746 if (items) { 747 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q); 748 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q); 749 } 750 751 if (!cq_handle) 752 return; 753 for_each_set_bit(cqn, active_cqns, cq_count) { 754 q = mlxsw_pci_cq_get(mlxsw_pci, cqn); 755 mlxsw_pci_queue_tasklet_schedule(q); 756 } 757 } 758 759 struct mlxsw_pci_queue_ops { 760 const char *name; 761 enum mlxsw_pci_queue_type type; 762 void (*pre_init)(struct mlxsw_pci *mlxsw_pci, 763 struct mlxsw_pci_queue *q); 764 int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox, 765 struct mlxsw_pci_queue *q); 766 void (*fini)(struct mlxsw_pci *mlxsw_pci, 767 struct mlxsw_pci_queue *q); 768 void (*tasklet)(unsigned long data); 769 u16 (*elem_count_f)(const struct mlxsw_pci_queue *q); 770 u8 (*elem_size_f)(const struct mlxsw_pci_queue *q); 771 u16 elem_count; 772 u8 elem_size; 773 }; 774 775 static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = { 776 .type = MLXSW_PCI_QUEUE_TYPE_SDQ, 777 .init = mlxsw_pci_sdq_init, 778 .fini = mlxsw_pci_sdq_fini, 779 .elem_count = MLXSW_PCI_WQE_COUNT, 780 .elem_size = MLXSW_PCI_WQE_SIZE, 781 }; 782 783 static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = { 784 .type = MLXSW_PCI_QUEUE_TYPE_RDQ, 785 .init = mlxsw_pci_rdq_init, 786 .fini = mlxsw_pci_rdq_fini, 787 .elem_count = MLXSW_PCI_WQE_COUNT, 788 .elem_size = MLXSW_PCI_WQE_SIZE 789 }; 790 791 static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = { 792 .type = MLXSW_PCI_QUEUE_TYPE_CQ, 793 .pre_init = mlxsw_pci_cq_pre_init, 794 .init = mlxsw_pci_cq_init, 795 .fini = mlxsw_pci_cq_fini, 796 .tasklet = mlxsw_pci_cq_tasklet, 797 .elem_count_f = mlxsw_pci_cq_elem_count, 798 .elem_size_f = mlxsw_pci_cq_elem_size 799 }; 800 801 static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = { 802 .type = MLXSW_PCI_QUEUE_TYPE_EQ, 803 .init = mlxsw_pci_eq_init, 804 .fini = mlxsw_pci_eq_fini, 805 .tasklet = mlxsw_pci_eq_tasklet, 806 .elem_count = MLXSW_PCI_EQE_COUNT, 807 .elem_size = MLXSW_PCI_EQE_SIZE 808 }; 809 810 static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 811 const struct mlxsw_pci_queue_ops *q_ops, 812 struct mlxsw_pci_queue *q, u8 q_num) 813 { 814 struct mlxsw_pci_mem_item *mem_item = &q->mem_item; 815 int i; 816 int err; 817 818 q->num = q_num; 819 if (q_ops->pre_init) 820 q_ops->pre_init(mlxsw_pci, q); 821 822 spin_lock_init(&q->lock); 823 q->count = q_ops->elem_count_f ? q_ops->elem_count_f(q) : 824 q_ops->elem_count; 825 q->elem_size = q_ops->elem_size_f ? q_ops->elem_size_f(q) : 826 q_ops->elem_size; 827 q->type = q_ops->type; 828 q->pci = mlxsw_pci; 829 830 if (q_ops->tasklet) 831 tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q); 832 833 mem_item->size = MLXSW_PCI_AQ_SIZE; 834 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev, 835 mem_item->size, 836 &mem_item->mapaddr); 837 if (!mem_item->buf) 838 return -ENOMEM; 839 memset(mem_item->buf, 0, mem_item->size); 840 841 q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL); 842 if (!q->elem_info) { 843 err = -ENOMEM; 844 goto err_elem_info_alloc; 845 } 846 847 /* Initialize dma mapped elements info elem_info for 848 * future easy access. 849 */ 850 for (i = 0; i < q->count; i++) { 851 struct mlxsw_pci_queue_elem_info *elem_info; 852 853 elem_info = mlxsw_pci_queue_elem_info_get(q, i); 854 elem_info->elem = 855 __mlxsw_pci_queue_elem_get(q, q->elem_size, i); 856 } 857 858 mlxsw_cmd_mbox_zero(mbox); 859 err = q_ops->init(mlxsw_pci, mbox, q); 860 if (err) 861 goto err_q_ops_init; 862 return 0; 863 864 err_q_ops_init: 865 kfree(q->elem_info); 866 err_elem_info_alloc: 867 pci_free_consistent(mlxsw_pci->pdev, mem_item->size, 868 mem_item->buf, mem_item->mapaddr); 869 return err; 870 } 871 872 static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci, 873 const struct mlxsw_pci_queue_ops *q_ops, 874 struct mlxsw_pci_queue *q) 875 { 876 struct mlxsw_pci_mem_item *mem_item = &q->mem_item; 877 878 q_ops->fini(mlxsw_pci, q); 879 kfree(q->elem_info); 880 pci_free_consistent(mlxsw_pci->pdev, mem_item->size, 881 mem_item->buf, mem_item->mapaddr); 882 } 883 884 static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 885 const struct mlxsw_pci_queue_ops *q_ops, 886 u8 num_qs) 887 { 888 struct mlxsw_pci_queue_type_group *queue_group; 889 int i; 890 int err; 891 892 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type); 893 queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL); 894 if (!queue_group->q) 895 return -ENOMEM; 896 897 for (i = 0; i < num_qs; i++) { 898 err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops, 899 &queue_group->q[i], i); 900 if (err) 901 goto err_queue_init; 902 } 903 queue_group->count = num_qs; 904 905 return 0; 906 907 err_queue_init: 908 for (i--; i >= 0; i--) 909 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]); 910 kfree(queue_group->q); 911 return err; 912 } 913 914 static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci, 915 const struct mlxsw_pci_queue_ops *q_ops) 916 { 917 struct mlxsw_pci_queue_type_group *queue_group; 918 int i; 919 920 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type); 921 for (i = 0; i < queue_group->count; i++) 922 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]); 923 kfree(queue_group->q); 924 } 925 926 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox) 927 { 928 struct pci_dev *pdev = mlxsw_pci->pdev; 929 u8 num_sdqs; 930 u8 sdq_log2sz; 931 u8 num_rdqs; 932 u8 rdq_log2sz; 933 u8 num_cqs; 934 u8 cq_log2sz; 935 u8 cqv2_log2sz; 936 u8 num_eqs; 937 u8 eq_log2sz; 938 int err; 939 940 mlxsw_cmd_mbox_zero(mbox); 941 err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox); 942 if (err) 943 return err; 944 945 num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox); 946 sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox); 947 num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox); 948 rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox); 949 num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox); 950 cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox); 951 cqv2_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cqv2_sz_get(mbox); 952 num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox); 953 eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox); 954 955 if (num_sdqs + num_rdqs > num_cqs || 956 num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) { 957 dev_err(&pdev->dev, "Unsupported number of queues\n"); 958 return -EINVAL; 959 } 960 961 if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) || 962 (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) || 963 (1 << cq_log2sz != MLXSW_PCI_CQE01_COUNT) || 964 (mlxsw_pci->max_cqe_ver == MLXSW_PCI_CQE_V2 && 965 (1 << cqv2_log2sz != MLXSW_PCI_CQE2_COUNT)) || 966 (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) { 967 dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n"); 968 return -EINVAL; 969 } 970 971 mlxsw_pci->num_sdq_cqs = num_sdqs; 972 973 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops, 974 num_eqs); 975 if (err) { 976 dev_err(&pdev->dev, "Failed to initialize event queues\n"); 977 return err; 978 } 979 980 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops, 981 num_cqs); 982 if (err) { 983 dev_err(&pdev->dev, "Failed to initialize completion queues\n"); 984 goto err_cqs_init; 985 } 986 987 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops, 988 num_sdqs); 989 if (err) { 990 dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n"); 991 goto err_sdqs_init; 992 } 993 994 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops, 995 num_rdqs); 996 if (err) { 997 dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n"); 998 goto err_rdqs_init; 999 } 1000 1001 /* We have to poll in command interface until queues are initialized */ 1002 mlxsw_pci->cmd.nopoll = true; 1003 return 0; 1004 1005 err_rdqs_init: 1006 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops); 1007 err_sdqs_init: 1008 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops); 1009 err_cqs_init: 1010 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops); 1011 return err; 1012 } 1013 1014 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci) 1015 { 1016 mlxsw_pci->cmd.nopoll = false; 1017 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops); 1018 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops); 1019 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops); 1020 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops); 1021 } 1022 1023 static void 1024 mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci, 1025 char *mbox, int index, 1026 const struct mlxsw_swid_config *swid) 1027 { 1028 u8 mask = 0; 1029 1030 if (swid->used_type) { 1031 mlxsw_cmd_mbox_config_profile_swid_config_type_set( 1032 mbox, index, swid->type); 1033 mask |= 1; 1034 } 1035 if (swid->used_properties) { 1036 mlxsw_cmd_mbox_config_profile_swid_config_properties_set( 1037 mbox, index, swid->properties); 1038 mask |= 2; 1039 } 1040 mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask); 1041 } 1042 1043 static int 1044 mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_pci *mlxsw_pci, 1045 const struct mlxsw_config_profile *profile, 1046 struct mlxsw_res *res) 1047 { 1048 u64 single_size, double_size, linear_size; 1049 int err; 1050 1051 err = mlxsw_core_kvd_sizes_get(mlxsw_pci->core, profile, 1052 &single_size, &double_size, 1053 &linear_size); 1054 if (err) 1055 return err; 1056 1057 MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size); 1058 MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size); 1059 MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size); 1060 1061 return 0; 1062 } 1063 1064 static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox, 1065 const struct mlxsw_config_profile *profile, 1066 struct mlxsw_res *res) 1067 { 1068 int i; 1069 int err; 1070 1071 mlxsw_cmd_mbox_zero(mbox); 1072 1073 if (profile->used_max_vepa_channels) { 1074 mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set( 1075 mbox, 1); 1076 mlxsw_cmd_mbox_config_profile_max_vepa_channels_set( 1077 mbox, profile->max_vepa_channels); 1078 } 1079 if (profile->used_max_mid) { 1080 mlxsw_cmd_mbox_config_profile_set_max_mid_set( 1081 mbox, 1); 1082 mlxsw_cmd_mbox_config_profile_max_mid_set( 1083 mbox, profile->max_mid); 1084 } 1085 if (profile->used_max_pgt) { 1086 mlxsw_cmd_mbox_config_profile_set_max_pgt_set( 1087 mbox, 1); 1088 mlxsw_cmd_mbox_config_profile_max_pgt_set( 1089 mbox, profile->max_pgt); 1090 } 1091 if (profile->used_max_system_port) { 1092 mlxsw_cmd_mbox_config_profile_set_max_system_port_set( 1093 mbox, 1); 1094 mlxsw_cmd_mbox_config_profile_max_system_port_set( 1095 mbox, profile->max_system_port); 1096 } 1097 if (profile->used_max_vlan_groups) { 1098 mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set( 1099 mbox, 1); 1100 mlxsw_cmd_mbox_config_profile_max_vlan_groups_set( 1101 mbox, profile->max_vlan_groups); 1102 } 1103 if (profile->used_max_regions) { 1104 mlxsw_cmd_mbox_config_profile_set_max_regions_set( 1105 mbox, 1); 1106 mlxsw_cmd_mbox_config_profile_max_regions_set( 1107 mbox, profile->max_regions); 1108 } 1109 if (profile->used_flood_tables) { 1110 mlxsw_cmd_mbox_config_profile_set_flood_tables_set( 1111 mbox, 1); 1112 mlxsw_cmd_mbox_config_profile_max_flood_tables_set( 1113 mbox, profile->max_flood_tables); 1114 mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set( 1115 mbox, profile->max_vid_flood_tables); 1116 mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set( 1117 mbox, profile->max_fid_offset_flood_tables); 1118 mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set( 1119 mbox, profile->fid_offset_flood_table_size); 1120 mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set( 1121 mbox, profile->max_fid_flood_tables); 1122 mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set( 1123 mbox, profile->fid_flood_table_size); 1124 } 1125 if (profile->used_flood_mode) { 1126 mlxsw_cmd_mbox_config_profile_set_flood_mode_set( 1127 mbox, 1); 1128 mlxsw_cmd_mbox_config_profile_flood_mode_set( 1129 mbox, profile->flood_mode); 1130 } 1131 if (profile->used_max_ib_mc) { 1132 mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set( 1133 mbox, 1); 1134 mlxsw_cmd_mbox_config_profile_max_ib_mc_set( 1135 mbox, profile->max_ib_mc); 1136 } 1137 if (profile->used_max_pkey) { 1138 mlxsw_cmd_mbox_config_profile_set_max_pkey_set( 1139 mbox, 1); 1140 mlxsw_cmd_mbox_config_profile_max_pkey_set( 1141 mbox, profile->max_pkey); 1142 } 1143 if (profile->used_ar_sec) { 1144 mlxsw_cmd_mbox_config_profile_set_ar_sec_set( 1145 mbox, 1); 1146 mlxsw_cmd_mbox_config_profile_ar_sec_set( 1147 mbox, profile->ar_sec); 1148 } 1149 if (profile->used_adaptive_routing_group_cap) { 1150 mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set( 1151 mbox, 1); 1152 mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set( 1153 mbox, profile->adaptive_routing_group_cap); 1154 } 1155 if (profile->used_kvd_sizes && MLXSW_RES_VALID(res, KVD_SIZE)) { 1156 err = mlxsw_pci_profile_get_kvd_sizes(mlxsw_pci, profile, res); 1157 if (err) 1158 return err; 1159 1160 mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1); 1161 mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox, 1162 MLXSW_RES_GET(res, KVD_LINEAR_SIZE)); 1163 mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox, 1164 1); 1165 mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox, 1166 MLXSW_RES_GET(res, KVD_SINGLE_SIZE)); 1167 mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set( 1168 mbox, 1); 1169 mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox, 1170 MLXSW_RES_GET(res, KVD_DOUBLE_SIZE)); 1171 } 1172 1173 for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++) 1174 mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i, 1175 &profile->swid_config[i]); 1176 1177 if (mlxsw_pci->max_cqe_ver > MLXSW_PCI_CQE_V0) { 1178 mlxsw_cmd_mbox_config_profile_set_cqe_version_set(mbox, 1); 1179 mlxsw_cmd_mbox_config_profile_cqe_version_set(mbox, 1); 1180 } 1181 1182 return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox); 1183 } 1184 1185 static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox) 1186 { 1187 struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info; 1188 int err; 1189 1190 mlxsw_cmd_mbox_zero(mbox); 1191 err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox); 1192 if (err) 1193 return err; 1194 mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd); 1195 mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid); 1196 return 0; 1197 } 1198 1199 static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox, 1200 u16 num_pages) 1201 { 1202 struct mlxsw_pci_mem_item *mem_item; 1203 int nent = 0; 1204 int i; 1205 int err; 1206 1207 mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item), 1208 GFP_KERNEL); 1209 if (!mlxsw_pci->fw_area.items) 1210 return -ENOMEM; 1211 mlxsw_pci->fw_area.count = num_pages; 1212 1213 mlxsw_cmd_mbox_zero(mbox); 1214 for (i = 0; i < num_pages; i++) { 1215 mem_item = &mlxsw_pci->fw_area.items[i]; 1216 1217 mem_item->size = MLXSW_PCI_PAGE_SIZE; 1218 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev, 1219 mem_item->size, 1220 &mem_item->mapaddr); 1221 if (!mem_item->buf) { 1222 err = -ENOMEM; 1223 goto err_alloc; 1224 } 1225 mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr); 1226 mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */ 1227 if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) { 1228 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent); 1229 if (err) 1230 goto err_cmd_map_fa; 1231 nent = 0; 1232 mlxsw_cmd_mbox_zero(mbox); 1233 } 1234 } 1235 1236 if (nent) { 1237 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent); 1238 if (err) 1239 goto err_cmd_map_fa; 1240 } 1241 1242 return 0; 1243 1244 err_cmd_map_fa: 1245 err_alloc: 1246 for (i--; i >= 0; i--) { 1247 mem_item = &mlxsw_pci->fw_area.items[i]; 1248 1249 pci_free_consistent(mlxsw_pci->pdev, mem_item->size, 1250 mem_item->buf, mem_item->mapaddr); 1251 } 1252 kfree(mlxsw_pci->fw_area.items); 1253 return err; 1254 } 1255 1256 static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci) 1257 { 1258 struct mlxsw_pci_mem_item *mem_item; 1259 int i; 1260 1261 mlxsw_cmd_unmap_fa(mlxsw_pci->core); 1262 1263 for (i = 0; i < mlxsw_pci->fw_area.count; i++) { 1264 mem_item = &mlxsw_pci->fw_area.items[i]; 1265 1266 pci_free_consistent(mlxsw_pci->pdev, mem_item->size, 1267 mem_item->buf, mem_item->mapaddr); 1268 } 1269 kfree(mlxsw_pci->fw_area.items); 1270 } 1271 1272 static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id) 1273 { 1274 struct mlxsw_pci *mlxsw_pci = dev_id; 1275 struct mlxsw_pci_queue *q; 1276 int i; 1277 1278 for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) { 1279 q = mlxsw_pci_eq_get(mlxsw_pci, i); 1280 mlxsw_pci_queue_tasklet_schedule(q); 1281 } 1282 return IRQ_HANDLED; 1283 } 1284 1285 static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci, 1286 struct mlxsw_pci_mem_item *mbox) 1287 { 1288 struct pci_dev *pdev = mlxsw_pci->pdev; 1289 int err = 0; 1290 1291 mbox->size = MLXSW_CMD_MBOX_SIZE; 1292 mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE, 1293 &mbox->mapaddr); 1294 if (!mbox->buf) { 1295 dev_err(&pdev->dev, "Failed allocating memory for mailbox\n"); 1296 err = -ENOMEM; 1297 } 1298 1299 return err; 1300 } 1301 1302 static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci, 1303 struct mlxsw_pci_mem_item *mbox) 1304 { 1305 struct pci_dev *pdev = mlxsw_pci->pdev; 1306 1307 pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf, 1308 mbox->mapaddr); 1309 } 1310 1311 static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci, 1312 const struct pci_device_id *id) 1313 { 1314 unsigned long end; 1315 char mrsr_pl[MLXSW_REG_MRSR_LEN]; 1316 int err; 1317 1318 mlxsw_reg_mrsr_pack(mrsr_pl); 1319 err = mlxsw_reg_write(mlxsw_pci->core, MLXSW_REG(mrsr), mrsr_pl); 1320 if (err) 1321 return err; 1322 if (id->device == PCI_DEVICE_ID_MELLANOX_SWITCHX2) { 1323 msleep(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS); 1324 return 0; 1325 } 1326 1327 /* We must wait for the HW to become responsive once again. */ 1328 msleep(MLXSW_PCI_SW_RESET_WAIT_MSECS); 1329 1330 end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS); 1331 do { 1332 u32 val = mlxsw_pci_read32(mlxsw_pci, FW_READY); 1333 1334 if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC) 1335 return 0; 1336 cond_resched(); 1337 } while (time_before(jiffies, end)); 1338 return -EBUSY; 1339 } 1340 1341 static int mlxsw_pci_alloc_irq_vectors(struct mlxsw_pci *mlxsw_pci) 1342 { 1343 int err; 1344 1345 err = pci_alloc_irq_vectors(mlxsw_pci->pdev, 1, 1, PCI_IRQ_MSIX); 1346 if (err < 0) 1347 dev_err(&mlxsw_pci->pdev->dev, "MSI-X init failed\n"); 1348 return err; 1349 } 1350 1351 static void mlxsw_pci_free_irq_vectors(struct mlxsw_pci *mlxsw_pci) 1352 { 1353 pci_free_irq_vectors(mlxsw_pci->pdev); 1354 } 1355 1356 static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core, 1357 const struct mlxsw_config_profile *profile, 1358 struct mlxsw_res *res) 1359 { 1360 struct mlxsw_pci *mlxsw_pci = bus_priv; 1361 struct pci_dev *pdev = mlxsw_pci->pdev; 1362 char *mbox; 1363 u16 num_pages; 1364 int err; 1365 1366 mutex_init(&mlxsw_pci->cmd.lock); 1367 init_waitqueue_head(&mlxsw_pci->cmd.wait); 1368 1369 mlxsw_pci->core = mlxsw_core; 1370 1371 mbox = mlxsw_cmd_mbox_alloc(); 1372 if (!mbox) 1373 return -ENOMEM; 1374 1375 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox); 1376 if (err) 1377 goto mbox_put; 1378 1379 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox); 1380 if (err) 1381 goto err_out_mbox_alloc; 1382 1383 err = mlxsw_pci_sw_reset(mlxsw_pci, mlxsw_pci->id); 1384 if (err) 1385 goto err_sw_reset; 1386 1387 err = mlxsw_pci_alloc_irq_vectors(mlxsw_pci); 1388 if (err < 0) { 1389 dev_err(&pdev->dev, "MSI-X init failed\n"); 1390 goto err_alloc_irq; 1391 } 1392 1393 err = mlxsw_cmd_query_fw(mlxsw_core, mbox); 1394 if (err) 1395 goto err_query_fw; 1396 1397 mlxsw_pci->bus_info.fw_rev.major = 1398 mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox); 1399 mlxsw_pci->bus_info.fw_rev.minor = 1400 mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox); 1401 mlxsw_pci->bus_info.fw_rev.subminor = 1402 mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox); 1403 1404 if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) { 1405 dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n"); 1406 err = -EINVAL; 1407 goto err_iface_rev; 1408 } 1409 if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) { 1410 dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n"); 1411 err = -EINVAL; 1412 goto err_doorbell_page_bar; 1413 } 1414 1415 mlxsw_pci->doorbell_offset = 1416 mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox); 1417 1418 if (mlxsw_cmd_mbox_query_fw_fr_rn_clk_bar_get(mbox) != 0) { 1419 dev_err(&pdev->dev, "Unsupported free running clock BAR queried from hw\n"); 1420 err = -EINVAL; 1421 goto err_fr_rn_clk_bar; 1422 } 1423 1424 mlxsw_pci->free_running_clock_offset = 1425 mlxsw_cmd_mbox_query_fw_free_running_clock_offset_get(mbox); 1426 1427 num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox); 1428 err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages); 1429 if (err) 1430 goto err_fw_area_init; 1431 1432 err = mlxsw_pci_boardinfo(mlxsw_pci, mbox); 1433 if (err) 1434 goto err_boardinfo; 1435 1436 err = mlxsw_core_resources_query(mlxsw_core, mbox, res); 1437 if (err) 1438 goto err_query_resources; 1439 1440 if (MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V2) && 1441 MLXSW_CORE_RES_GET(mlxsw_core, CQE_V2)) 1442 mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V2; 1443 else if (MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V1) && 1444 MLXSW_CORE_RES_GET(mlxsw_core, CQE_V1)) 1445 mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V1; 1446 else if ((MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V0) && 1447 MLXSW_CORE_RES_GET(mlxsw_core, CQE_V0)) || 1448 !MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V0)) { 1449 mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V0; 1450 } else { 1451 dev_err(&pdev->dev, "Invalid supported CQE version combination reported\n"); 1452 goto err_cqe_v_check; 1453 } 1454 1455 err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res); 1456 if (err) 1457 goto err_config_profile; 1458 1459 err = mlxsw_pci_aqs_init(mlxsw_pci, mbox); 1460 if (err) 1461 goto err_aqs_init; 1462 1463 err = request_irq(pci_irq_vector(pdev, 0), 1464 mlxsw_pci_eq_irq_handler, 0, 1465 mlxsw_pci->bus_info.device_kind, mlxsw_pci); 1466 if (err) { 1467 dev_err(&pdev->dev, "IRQ request failed\n"); 1468 goto err_request_eq_irq; 1469 } 1470 1471 goto mbox_put; 1472 1473 err_request_eq_irq: 1474 mlxsw_pci_aqs_fini(mlxsw_pci); 1475 err_aqs_init: 1476 err_config_profile: 1477 err_cqe_v_check: 1478 err_query_resources: 1479 err_boardinfo: 1480 mlxsw_pci_fw_area_fini(mlxsw_pci); 1481 err_fw_area_init: 1482 err_fr_rn_clk_bar: 1483 err_doorbell_page_bar: 1484 err_iface_rev: 1485 err_query_fw: 1486 mlxsw_pci_free_irq_vectors(mlxsw_pci); 1487 err_alloc_irq: 1488 err_sw_reset: 1489 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox); 1490 err_out_mbox_alloc: 1491 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox); 1492 mbox_put: 1493 mlxsw_cmd_mbox_free(mbox); 1494 return err; 1495 } 1496 1497 static void mlxsw_pci_fini(void *bus_priv) 1498 { 1499 struct mlxsw_pci *mlxsw_pci = bus_priv; 1500 1501 free_irq(pci_irq_vector(mlxsw_pci->pdev, 0), mlxsw_pci); 1502 mlxsw_pci_aqs_fini(mlxsw_pci); 1503 mlxsw_pci_fw_area_fini(mlxsw_pci); 1504 mlxsw_pci_free_irq_vectors(mlxsw_pci); 1505 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox); 1506 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox); 1507 } 1508 1509 static struct mlxsw_pci_queue * 1510 mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci, 1511 const struct mlxsw_tx_info *tx_info) 1512 { 1513 u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci); 1514 1515 return mlxsw_pci_sdq_get(mlxsw_pci, sdqn); 1516 } 1517 1518 static bool mlxsw_pci_skb_transmit_busy(void *bus_priv, 1519 const struct mlxsw_tx_info *tx_info) 1520 { 1521 struct mlxsw_pci *mlxsw_pci = bus_priv; 1522 struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info); 1523 1524 return !mlxsw_pci_queue_elem_info_producer_get(q); 1525 } 1526 1527 static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb, 1528 const struct mlxsw_tx_info *tx_info) 1529 { 1530 struct mlxsw_pci *mlxsw_pci = bus_priv; 1531 struct mlxsw_pci_queue *q; 1532 struct mlxsw_pci_queue_elem_info *elem_info; 1533 char *wqe; 1534 int i; 1535 int err; 1536 1537 if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) { 1538 err = skb_linearize(skb); 1539 if (err) 1540 return err; 1541 } 1542 1543 q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info); 1544 spin_lock_bh(&q->lock); 1545 elem_info = mlxsw_pci_queue_elem_info_producer_get(q); 1546 if (!elem_info) { 1547 /* queue is full */ 1548 err = -EAGAIN; 1549 goto unlock; 1550 } 1551 elem_info->u.sdq.skb = skb; 1552 1553 wqe = elem_info->elem; 1554 mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */ 1555 mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad); 1556 mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET); 1557 1558 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data, 1559 skb_headlen(skb), DMA_TO_DEVICE); 1560 if (err) 1561 goto unlock; 1562 1563 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1564 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1565 1566 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1, 1567 skb_frag_address(frag), 1568 skb_frag_size(frag), 1569 DMA_TO_DEVICE); 1570 if (err) 1571 goto unmap_frags; 1572 } 1573 1574 /* Set unused sq entries byte count to zero. */ 1575 for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++) 1576 mlxsw_pci_wqe_byte_count_set(wqe, i, 0); 1577 1578 /* Everything is set up, ring producer doorbell to get HW going */ 1579 q->producer_counter++; 1580 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q); 1581 1582 goto unlock; 1583 1584 unmap_frags: 1585 for (; i >= 0; i--) 1586 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE); 1587 unlock: 1588 spin_unlock_bh(&q->lock); 1589 return err; 1590 } 1591 1592 static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod, 1593 u32 in_mod, bool out_mbox_direct, 1594 char *in_mbox, size_t in_mbox_size, 1595 char *out_mbox, size_t out_mbox_size, 1596 u8 *p_status) 1597 { 1598 struct mlxsw_pci *mlxsw_pci = bus_priv; 1599 dma_addr_t in_mapaddr = 0, out_mapaddr = 0; 1600 bool evreq = mlxsw_pci->cmd.nopoll; 1601 unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS); 1602 bool *p_wait_done = &mlxsw_pci->cmd.wait_done; 1603 int err; 1604 1605 *p_status = MLXSW_CMD_STATUS_OK; 1606 1607 err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock); 1608 if (err) 1609 return err; 1610 1611 if (in_mbox) { 1612 memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size); 1613 in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr; 1614 } 1615 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr)); 1616 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr)); 1617 1618 if (out_mbox) 1619 out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr; 1620 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr)); 1621 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr)); 1622 1623 mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod); 1624 mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0); 1625 1626 *p_wait_done = false; 1627 1628 wmb(); /* all needs to be written before we write control register */ 1629 mlxsw_pci_write32(mlxsw_pci, CIR_CTRL, 1630 MLXSW_PCI_CIR_CTRL_GO_BIT | 1631 (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) | 1632 (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) | 1633 opcode); 1634 1635 if (!evreq) { 1636 unsigned long end; 1637 1638 end = jiffies + timeout; 1639 do { 1640 u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL); 1641 1642 if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) { 1643 *p_wait_done = true; 1644 *p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT; 1645 break; 1646 } 1647 cond_resched(); 1648 } while (time_before(jiffies, end)); 1649 } else { 1650 wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout); 1651 *p_status = mlxsw_pci->cmd.comp.status; 1652 } 1653 1654 err = 0; 1655 if (*p_wait_done) { 1656 if (*p_status) 1657 err = -EIO; 1658 } else { 1659 err = -ETIMEDOUT; 1660 } 1661 1662 if (!err && out_mbox && out_mbox_direct) { 1663 /* Some commands don't use output param as address to mailbox 1664 * but they store output directly into registers. In that case, 1665 * copy registers into mbox buffer. 1666 */ 1667 __be32 tmp; 1668 1669 if (!evreq) { 1670 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci, 1671 CIR_OUT_PARAM_HI)); 1672 memcpy(out_mbox, &tmp, sizeof(tmp)); 1673 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci, 1674 CIR_OUT_PARAM_LO)); 1675 memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp)); 1676 } 1677 } else if (!err && out_mbox) { 1678 memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size); 1679 } 1680 1681 mutex_unlock(&mlxsw_pci->cmd.lock); 1682 1683 return err; 1684 } 1685 1686 static u32 mlxsw_pci_read_frc_h(void *bus_priv) 1687 { 1688 struct mlxsw_pci *mlxsw_pci = bus_priv; 1689 u64 frc_offset; 1690 1691 frc_offset = mlxsw_pci->free_running_clock_offset; 1692 return mlxsw_pci_read32(mlxsw_pci, FREE_RUNNING_CLOCK_H(frc_offset)); 1693 } 1694 1695 static u32 mlxsw_pci_read_frc_l(void *bus_priv) 1696 { 1697 struct mlxsw_pci *mlxsw_pci = bus_priv; 1698 u64 frc_offset; 1699 1700 frc_offset = mlxsw_pci->free_running_clock_offset; 1701 return mlxsw_pci_read32(mlxsw_pci, FREE_RUNNING_CLOCK_L(frc_offset)); 1702 } 1703 1704 static const struct mlxsw_bus mlxsw_pci_bus = { 1705 .kind = "pci", 1706 .init = mlxsw_pci_init, 1707 .fini = mlxsw_pci_fini, 1708 .skb_transmit_busy = mlxsw_pci_skb_transmit_busy, 1709 .skb_transmit = mlxsw_pci_skb_transmit, 1710 .cmd_exec = mlxsw_pci_cmd_exec, 1711 .read_frc_h = mlxsw_pci_read_frc_h, 1712 .read_frc_l = mlxsw_pci_read_frc_l, 1713 .features = MLXSW_BUS_F_TXRX | MLXSW_BUS_F_RESET, 1714 }; 1715 1716 static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1717 { 1718 const char *driver_name = pdev->driver->name; 1719 struct mlxsw_pci *mlxsw_pci; 1720 int err; 1721 1722 mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL); 1723 if (!mlxsw_pci) 1724 return -ENOMEM; 1725 1726 err = pci_enable_device(pdev); 1727 if (err) { 1728 dev_err(&pdev->dev, "pci_enable_device failed\n"); 1729 goto err_pci_enable_device; 1730 } 1731 1732 err = pci_request_regions(pdev, driver_name); 1733 if (err) { 1734 dev_err(&pdev->dev, "pci_request_regions failed\n"); 1735 goto err_pci_request_regions; 1736 } 1737 1738 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 1739 if (!err) { 1740 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 1741 if (err) { 1742 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n"); 1743 goto err_pci_set_dma_mask; 1744 } 1745 } else { 1746 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 1747 if (err) { 1748 dev_err(&pdev->dev, "pci_set_dma_mask failed\n"); 1749 goto err_pci_set_dma_mask; 1750 } 1751 } 1752 1753 if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) { 1754 dev_err(&pdev->dev, "invalid PCI region size\n"); 1755 err = -EINVAL; 1756 goto err_pci_resource_len_check; 1757 } 1758 1759 mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0), 1760 pci_resource_len(pdev, 0)); 1761 if (!mlxsw_pci->hw_addr) { 1762 dev_err(&pdev->dev, "ioremap failed\n"); 1763 err = -EIO; 1764 goto err_ioremap; 1765 } 1766 pci_set_master(pdev); 1767 1768 mlxsw_pci->pdev = pdev; 1769 pci_set_drvdata(pdev, mlxsw_pci); 1770 1771 mlxsw_pci->bus_info.device_kind = driver_name; 1772 mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev); 1773 mlxsw_pci->bus_info.dev = &pdev->dev; 1774 mlxsw_pci->bus_info.read_frc_capable = true; 1775 mlxsw_pci->id = id; 1776 1777 err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info, 1778 &mlxsw_pci_bus, mlxsw_pci, false, 1779 NULL); 1780 if (err) { 1781 dev_err(&pdev->dev, "cannot register bus device\n"); 1782 goto err_bus_device_register; 1783 } 1784 1785 return 0; 1786 1787 err_bus_device_register: 1788 iounmap(mlxsw_pci->hw_addr); 1789 err_ioremap: 1790 err_pci_resource_len_check: 1791 err_pci_set_dma_mask: 1792 pci_release_regions(pdev); 1793 err_pci_request_regions: 1794 pci_disable_device(pdev); 1795 err_pci_enable_device: 1796 kfree(mlxsw_pci); 1797 return err; 1798 } 1799 1800 static void mlxsw_pci_remove(struct pci_dev *pdev) 1801 { 1802 struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev); 1803 1804 mlxsw_core_bus_device_unregister(mlxsw_pci->core, false); 1805 iounmap(mlxsw_pci->hw_addr); 1806 pci_release_regions(mlxsw_pci->pdev); 1807 pci_disable_device(mlxsw_pci->pdev); 1808 kfree(mlxsw_pci); 1809 } 1810 1811 int mlxsw_pci_driver_register(struct pci_driver *pci_driver) 1812 { 1813 pci_driver->probe = mlxsw_pci_probe; 1814 pci_driver->remove = mlxsw_pci_remove; 1815 return pci_register_driver(pci_driver); 1816 } 1817 EXPORT_SYMBOL(mlxsw_pci_driver_register); 1818 1819 void mlxsw_pci_driver_unregister(struct pci_driver *pci_driver) 1820 { 1821 pci_unregister_driver(pci_driver); 1822 } 1823 EXPORT_SYMBOL(mlxsw_pci_driver_unregister); 1824 1825 static int __init mlxsw_pci_module_init(void) 1826 { 1827 return 0; 1828 } 1829 1830 static void __exit mlxsw_pci_module_exit(void) 1831 { 1832 } 1833 1834 module_init(mlxsw_pci_module_init); 1835 module_exit(mlxsw_pci_module_exit); 1836 1837 MODULE_LICENSE("Dual BSD/GPL"); 1838 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); 1839 MODULE_DESCRIPTION("Mellanox switch PCI interface driver"); 1840