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