2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 * Copyright (C) 2008 Mellanox Technologies Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <gpxe/malloc.h>
30 #include <gpxe/umalloc.h>
31 #include <gpxe/iobuf.h>
32 #include <gpxe/netdevice.h>
33 #include <gpxe/infiniband.h>
39 * Mellanox Hermon Infiniband HCA
43 /***************************************************************************
45 * Queue number allocation
47 ***************************************************************************
51 * Allocate offsets within usage bitmask
53 * @v bits Usage bitmask
54 * @v bits_len Length of usage bitmask
55 * @v num_bits Number of contiguous bits to allocate within bitmask
56 * @ret bit First free bit within bitmask, or negative error
58 static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
59 unsigned int bits_len,
60 unsigned int num_bits ) {
62 hermon_bitmask_t mask = 1;
63 unsigned int found = 0;
65 /* Search bits for num_bits contiguous free bits */
66 while ( bit < bits_len ) {
67 if ( ( mask & *bits ) == 0 ) {
68 if ( ++found == num_bits )
74 mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
81 /* Mark bits as in-use */
86 mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
89 return ( bit - num_bits + 1 );
93 * Free offsets within usage bitmask
95 * @v bits Usage bitmask
96 * @v bit Starting bit within bitmask
97 * @v num_bits Number of contiguous bits to free within bitmask
99 static void hermon_bitmask_free ( hermon_bitmask_t *bits,
100 int bit, unsigned int num_bits ) {
101 hermon_bitmask_t mask;
103 for ( ; num_bits ; bit++, num_bits-- ) {
104 mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
105 bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
109 /***************************************************************************
113 ***************************************************************************
117 * Wait for Hermon command completion
119 * @v hermon Hermon device
120 * @v hcr HCA command registers
121 * @ret rc Return status code
123 static int hermon_cmd_wait ( struct hermon *hermon,
124 struct hermonprm_hca_command_register *hcr ) {
127 for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
129 readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
130 if ( ( MLX_GET ( hcr, go ) == 0 ) &&
131 ( MLX_GET ( hcr, t ) == hermon->toggle ) )
141 * @v hermon Hermon device
142 * @v command Command opcode, flags and input/output lengths
143 * @v op_mod Opcode modifier (0 if no modifier applicable)
144 * @v in Input parameters
145 * @v in_mod Input modifier (0 if no modifier applicable)
146 * @v out Output parameters
147 * @ret rc Return status code
149 static int hermon_cmd ( struct hermon *hermon, unsigned long command,
150 unsigned int op_mod, const void *in,
151 unsigned int in_mod, void *out ) {
152 struct hermonprm_hca_command_register hcr;
153 unsigned int opcode = HERMON_HCR_OPCODE ( command );
154 size_t in_len = HERMON_HCR_IN_LEN ( command );
155 size_t out_len = HERMON_HCR_OUT_LEN ( command );
162 assert ( in_len <= HERMON_MBOX_SIZE );
163 assert ( out_len <= HERMON_MBOX_SIZE );
165 DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
166 hermon, opcode, in_len,
167 ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
168 ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
170 /* Check that HCR is free */
171 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
172 DBGC ( hermon, "Hermon %p command interface locked\n",
177 /* Flip HCR toggle */
178 hermon->toggle = ( 1 - hermon->toggle );
181 memset ( &hcr, 0, sizeof ( hcr ) );
182 in_buffer = &hcr.u.dwords[0];
183 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
184 in_buffer = hermon->mailbox_in;
185 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
187 memcpy ( in_buffer, in, in_len );
188 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
189 out_buffer = &hcr.u.dwords[3];
190 if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
191 out_buffer = hermon->mailbox_out;
192 MLX_FILL_1 ( &hcr, 4, out_param_l,
193 virt_to_bus ( out_buffer ) );
195 MLX_FILL_4 ( &hcr, 6,
197 opcode_modifier, op_mod,
200 DBGC ( hermon, "Hermon %p issuing command:\n", hermon );
201 DBGC_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
202 &hcr, sizeof ( hcr ) );
203 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
204 DBGC2 ( hermon, "Input mailbox:\n" );
205 DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
206 ( ( in_len < 512 ) ? in_len : 512 ) );
210 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
212 writel ( hcr.u.dwords[i],
213 hermon->config + HERMON_HCR_REG ( i ) );
217 /* Wait for command completion */
218 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
219 DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
222 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
223 &hcr, sizeof ( hcr ) );
227 /* Check command status */
228 status = MLX_GET ( &hcr, status );
230 DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
233 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
234 &hcr, sizeof ( hcr ) );
238 /* Read output parameters, if any */
239 hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
240 hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
241 memcpy ( out, out_buffer, out_len );
243 DBGC2 ( hermon, "Output%s:\n",
244 ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
245 DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
246 ( ( out_len < 512 ) ? out_len : 512 ) );
253 hermon_cmd_query_dev_cap ( struct hermon *hermon,
254 struct hermonprm_query_dev_cap *dev_cap ) {
255 return hermon_cmd ( hermon,
256 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
257 1, sizeof ( *dev_cap ) ),
258 0, NULL, 0, dev_cap );
262 hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
263 return hermon_cmd ( hermon,
264 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
270 hermon_cmd_init_hca ( struct hermon *hermon,
271 const struct hermonprm_init_hca *init_hca ) {
272 return hermon_cmd ( hermon,
273 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
274 1, sizeof ( *init_hca ) ),
275 0, init_hca, 0, NULL );
279 hermon_cmd_close_hca ( struct hermon *hermon ) {
280 return hermon_cmd ( hermon,
281 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
286 hermon_cmd_init_port ( struct hermon *hermon, unsigned int port,
287 const struct hermonprm_init_port *init_port ) {
288 return hermon_cmd ( hermon,
289 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_PORT,
290 1, sizeof ( *init_port ) ),
291 0, init_port, port, NULL );
295 hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
296 return hermon_cmd ( hermon,
297 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
298 0, NULL, port, NULL );
302 hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
303 const struct hermonprm_mpt *mpt ) {
304 return hermon_cmd ( hermon,
305 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
306 1, sizeof ( *mpt ) ),
307 0, mpt, index, NULL );
311 hermon_cmd_write_mtt ( struct hermon *hermon,
312 const struct hermonprm_write_mtt *write_mtt ) {
313 return hermon_cmd ( hermon,
314 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
315 1, sizeof ( *write_mtt ) ),
316 0, write_mtt, 1, NULL );
320 hermon_cmd_map_eq ( struct hermon *hermon, unsigned long index_map,
321 const struct hermonprm_event_mask *mask ) {
322 return hermon_cmd ( hermon,
323 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_EQ,
324 0, sizeof ( *mask ) ),
325 0, mask, index_map, NULL );
329 hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
330 const struct hermonprm_eqc *eqctx ) {
331 return hermon_cmd ( hermon,
332 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
333 1, sizeof ( *eqctx ) ),
334 0, eqctx, index, NULL );
338 hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index,
339 struct hermonprm_eqc *eqctx ) {
340 return hermon_cmd ( hermon,
341 HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_EQ,
342 1, sizeof ( *eqctx ) ),
343 1, NULL, index, eqctx );
347 hermon_cmd_query_eq ( struct hermon *hermon, unsigned int index,
348 struct hermonprm_eqc *eqctx ) {
349 return hermon_cmd ( hermon,
350 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_EQ,
351 1, sizeof ( *eqctx ) ),
352 0, NULL, index, eqctx );
356 hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
357 const struct hermonprm_completion_queue_context *cqctx ){
358 return hermon_cmd ( hermon,
359 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
360 1, sizeof ( *cqctx ) ),
361 0, cqctx, cqn, NULL );
365 hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
366 struct hermonprm_completion_queue_context *cqctx) {
367 return hermon_cmd ( hermon,
368 HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
369 1, sizeof ( *cqctx ) ),
370 0, NULL, cqn, cqctx );
374 hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
375 const struct hermonprm_qp_ee_state_transitions *ctx ){
376 return hermon_cmd ( hermon,
377 HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
378 1, sizeof ( *ctx ) ),
383 hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
384 const struct hermonprm_qp_ee_state_transitions *ctx ){
385 return hermon_cmd ( hermon,
386 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
387 1, sizeof ( *ctx ) ),
392 hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
393 const struct hermonprm_qp_ee_state_transitions *ctx ) {
394 return hermon_cmd ( hermon,
395 HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
396 1, sizeof ( *ctx ) ),
401 hermon_cmd_rts2rts_qp ( struct hermon *hermon, unsigned long qpn,
402 const struct hermonprm_qp_ee_state_transitions *ctx ) {
403 return hermon_cmd ( hermon,
404 HERMON_HCR_IN_CMD ( HERMON_HCR_RTS2RTS_QP,
405 1, sizeof ( *ctx ) ),
410 hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
411 return hermon_cmd ( hermon,
412 HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
413 0x03, NULL, qpn, NULL );
417 hermon_cmd_mad_ifc ( struct hermon *hermon, unsigned int port,
418 union hermonprm_mad *mad ) {
419 return hermon_cmd ( hermon,
420 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
422 1, sizeof ( *mad ) ),
423 0x03, mad, port, mad );
427 hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
428 struct hermonprm_mcg_entry *mcg ) {
429 return hermon_cmd ( hermon,
430 HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
431 1, sizeof ( *mcg ) ),
432 0, NULL, index, mcg );
436 hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
437 const struct hermonprm_mcg_entry *mcg ) {
438 return hermon_cmd ( hermon,
439 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
440 1, sizeof ( *mcg ) ),
441 0, mcg, index, NULL );
445 hermon_cmd_mgid_hash ( struct hermon *hermon, const struct ib_gid *gid,
446 struct hermonprm_mgm_hash *hash ) {
447 return hermon_cmd ( hermon,
448 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
450 0, sizeof ( *hash ) ),
455 hermon_cmd_run_fw ( struct hermon *hermon ) {
456 return hermon_cmd ( hermon,
457 HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
462 hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
463 const struct hermonprm_scalar_parameter *offset ) {
464 return hermon_cmd ( hermon,
465 HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
466 0, sizeof ( *offset ) ),
467 0, offset, page_count, NULL );
471 hermon_cmd_map_icm ( struct hermon *hermon,
472 const struct hermonprm_virtual_physical_mapping *map ) {
473 return hermon_cmd ( hermon,
474 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
475 1, sizeof ( *map ) ),
480 hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
481 return hermon_cmd ( hermon,
482 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
487 hermon_cmd_map_icm_aux ( struct hermon *hermon,
488 const struct hermonprm_virtual_physical_mapping *map ) {
489 return hermon_cmd ( hermon,
490 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
491 1, sizeof ( *map ) ),
496 hermon_cmd_set_icm_size ( struct hermon *hermon,
497 const struct hermonprm_scalar_parameter *icm_size,
498 struct hermonprm_scalar_parameter *icm_aux_size ) {
499 return hermon_cmd ( hermon,
500 HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
501 0, sizeof ( *icm_size ),
502 0, sizeof (*icm_aux_size) ),
503 0, icm_size, 0, icm_aux_size );
507 hermon_cmd_unmap_fa ( struct hermon *hermon ) {
508 return hermon_cmd ( hermon,
509 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
514 hermon_cmd_map_fa ( struct hermon *hermon,
515 const struct hermonprm_virtual_physical_mapping *map ) {
516 return hermon_cmd ( hermon,
517 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
518 1, sizeof ( *map ) ),
522 /***************************************************************************
524 * Memory translation table operations
526 ***************************************************************************
530 * Allocate MTT entries
532 * @v hermon Hermon device
533 * @v memory Memory to map into MTT
534 * @v len Length of memory to map
535 * @v mtt MTT descriptor to fill in
536 * @ret rc Return status code
538 static int hermon_alloc_mtt ( struct hermon *hermon,
539 const void *memory, size_t len,
540 struct hermon_mtt *mtt ) {
541 struct hermonprm_write_mtt write_mtt;
543 unsigned int page_offset;
544 unsigned int num_pages;
546 unsigned int mtt_base_addr;
550 /* Find available MTT entries */
551 start = virt_to_phys ( memory );
552 page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
553 start -= page_offset;
555 num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
556 mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
558 if ( mtt_offset < 0 ) {
559 DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
564 mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
565 hermon->cap.mtt_entry_size );
567 /* Fill in MTT structure */
568 mtt->mtt_offset = mtt_offset;
569 mtt->num_pages = num_pages;
570 mtt->mtt_base_addr = mtt_base_addr;
571 mtt->page_offset = page_offset;
573 /* Construct and issue WRITE_MTT commands */
574 for ( i = 0 ; i < num_pages ; i++ ) {
575 memset ( &write_mtt, 0, sizeof ( write_mtt ) );
576 MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
577 value, mtt_base_addr );
578 MLX_FILL_2 ( &write_mtt.mtt, 1,
580 ptag_l, ( start >> 3 ) );
581 if ( ( rc = hermon_cmd_write_mtt ( hermon,
582 &write_mtt ) ) != 0 ) {
583 DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
584 hermon, mtt_base_addr );
587 start += HERMON_PAGE_SIZE;
588 mtt_base_addr += hermon->cap.mtt_entry_size;
594 hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
602 * @v hermon Hermon device
603 * @v mtt MTT descriptor
605 static void hermon_free_mtt ( struct hermon *hermon,
606 struct hermon_mtt *mtt ) {
607 hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
611 /***************************************************************************
613 * Completion queue operations
615 ***************************************************************************
619 * Create completion queue
621 * @v ibdev Infiniband device
622 * @v cq Completion queue
623 * @ret rc Return status code
625 static int hermon_create_cq ( struct ib_device *ibdev,
626 struct ib_completion_queue *cq ) {
627 struct hermon *hermon = ib_get_drvdata ( ibdev );
628 struct hermon_completion_queue *hermon_cq;
629 struct hermonprm_completion_queue_context cqctx;
634 /* Find a free completion queue number */
635 cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
637 if ( cqn_offset < 0 ) {
638 DBGC ( hermon, "Hermon %p out of completion queues\n",
643 cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
645 /* Allocate control structures */
646 hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
652 /* Allocate completion queue itself */
653 hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
654 hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
655 sizeof ( hermon_cq->cqe[0] ) );
656 if ( ! hermon_cq->cqe ) {
660 memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
661 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
662 MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
666 /* Allocate MTT entries */
667 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
669 &hermon_cq->mtt ) ) != 0 )
672 /* Hand queue over to hardware */
673 memset ( &cqctx, 0, sizeof ( cqctx ) );
674 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
675 MLX_FILL_1 ( &cqctx, 2,
676 page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
677 MLX_FILL_2 ( &cqctx, 3,
678 usr_page, HERMON_UAR_NON_EQ_PAGE,
679 log_cq_size, fls ( cq->num_cqes - 1 ) );
680 MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
681 ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
682 MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
683 ( virt_to_phys ( &hermon_cq->doorbell ) >> 3 ) );
684 if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
685 DBGC ( hermon, "Hermon %p SW2HW_CQ failed: %s\n",
686 hermon, strerror ( rc ) );
690 DBGC ( hermon, "Hermon %p CQN %#lx ring at [%p,%p)\n",
691 hermon, cq->cqn, hermon_cq->cqe,
692 ( ( ( void * ) hermon_cq->cqe ) + hermon_cq->cqe_size ) );
693 ib_cq_set_drvdata ( cq, hermon_cq );
697 hermon_free_mtt ( hermon, &hermon_cq->mtt );
699 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
703 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
709 * Destroy completion queue
711 * @v ibdev Infiniband device
712 * @v cq Completion queue
714 static void hermon_destroy_cq ( struct ib_device *ibdev,
715 struct ib_completion_queue *cq ) {
716 struct hermon *hermon = ib_get_drvdata ( ibdev );
717 struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
718 struct hermonprm_completion_queue_context cqctx;
722 /* Take ownership back from hardware */
723 if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
724 DBGC ( hermon, "Hermon %p FATAL HW2SW_CQ failed on CQN %#lx: "
725 "%s\n", hermon, cq->cqn, strerror ( rc ) );
726 /* Leak memory and return; at least we avoid corruption */
730 /* Free MTT entries */
731 hermon_free_mtt ( hermon, &hermon_cq->mtt );
734 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
737 /* Mark queue number as free */
738 cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
739 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
741 ib_cq_set_drvdata ( cq, NULL );
744 /***************************************************************************
746 * Queue pair operations
748 ***************************************************************************
754 * @v ibdev Infiniband device
756 * @ret rc Return status code
758 static int hermon_create_qp ( struct ib_device *ibdev,
759 struct ib_queue_pair *qp ) {
760 struct hermon *hermon = ib_get_drvdata ( ibdev );
761 struct hermon_queue_pair *hermon_qp;
762 struct hermonprm_qp_ee_state_transitions qpctx;
766 /* Find a free queue pair number */
767 qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
769 if ( qpn_offset < 0 ) {
770 DBGC ( hermon, "Hermon %p out of queue pairs\n", hermon );
774 qp->qpn = ( HERMON_QPN_BASE + hermon->cap.reserved_qps +
777 /* Allocate control structures */
778 hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
784 /* Calculate doorbell address */
785 hermon_qp->send.doorbell =
786 ( hermon->uar + HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE +
787 HERMON_DB_POST_SND_OFFSET );
789 /* Allocate work queue buffer */
790 hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
791 ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
792 hermon_qp->send.num_wqes =
793 ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
794 hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
795 sizeof ( hermon_qp->send.wqe[0] ) );
796 hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
797 sizeof ( hermon_qp->recv.wqe[0] ) );
798 hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
799 hermon_qp->recv.wqe_size );
800 hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
801 sizeof ( hermon_qp->send.wqe[0] ) );
802 if ( ! hermon_qp->wqe ) {
806 hermon_qp->send.wqe = hermon_qp->wqe;
807 memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
808 hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
809 memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
811 /* Allocate MTT entries */
812 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
814 &hermon_qp->mtt ) ) != 0 ) {
818 /* Transition queue to INIT state */
819 memset ( &qpctx, 0, sizeof ( qpctx ) );
820 MLX_FILL_2 ( &qpctx, 2,
821 qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
822 qpc_eec_data.st, HERMON_ST_UD );
823 MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
824 MLX_FILL_4 ( &qpctx, 4,
825 qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
826 qpc_eec_data.log_rq_stride,
827 ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
828 qpc_eec_data.log_sq_size,
829 fls ( hermon_qp->send.num_wqes - 1 ),
830 qpc_eec_data.log_sq_stride,
831 ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
832 MLX_FILL_1 ( &qpctx, 5,
833 qpc_eec_data.usr_page, HERMON_UAR_NON_EQ_PAGE );
834 MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
835 MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.page_offset,
836 ( hermon_qp->mtt.page_offset >> 6 ) );
837 MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
838 MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
839 ( virt_to_phys ( &hermon_qp->recv.doorbell ) >> 2 ) );
840 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
841 MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
842 ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
843 if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
845 DBGC ( hermon, "Hermon %p RST2INIT_QP failed: %s\n",
846 hermon, strerror ( rc ) );
847 goto err_rst2init_qp;
850 /* Transition queue to RTR state */
851 memset ( &qpctx, 0, sizeof ( qpctx ) );
852 MLX_FILL_2 ( &qpctx, 4,
853 qpc_eec_data.mtu, HERMON_MTU_2048,
854 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
855 MLX_FILL_1 ( &qpctx, 16,
856 qpc_eec_data.primary_address_path.sched_queue,
857 ( 0x83 /* default policy */ |
858 ( ( ibdev->port - 1 ) << 6 ) ) );
859 if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
861 DBGC ( hermon, "Hermon %p INIT2RTR_QP failed: %s\n",
862 hermon, strerror ( rc ) );
863 goto err_init2rtr_qp;
865 memset ( &qpctx, 0, sizeof ( qpctx ) );
866 if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
867 DBGC ( hermon, "Hermon %p RTR2RTS_QP failed: %s\n",
868 hermon, strerror ( rc ) );
872 DBGC ( hermon, "Hermon %p QPN %#lx send ring at [%p,%p)\n",
873 hermon, qp->qpn, hermon_qp->send.wqe,
874 ( ((void *)hermon_qp->send.wqe ) + hermon_qp->send.wqe_size ) );
875 DBGC ( hermon, "Hermon %p QPN %#lx receive ring at [%p,%p)\n",
876 hermon, qp->qpn, hermon_qp->recv.wqe,
877 ( ((void *)hermon_qp->recv.wqe ) + hermon_qp->recv.wqe_size ) );
878 ib_qp_set_drvdata ( qp, hermon_qp );
883 hermon_cmd_2rst_qp ( hermon, qp->qpn );
885 hermon_free_mtt ( hermon, &hermon_qp->mtt );
887 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
891 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
899 * @v ibdev Infiniband device
901 * @v mod_list Modification list
902 * @ret rc Return status code
904 static int hermon_modify_qp ( struct ib_device *ibdev,
905 struct ib_queue_pair *qp,
906 unsigned long mod_list ) {
907 struct hermon *hermon = ib_get_drvdata ( ibdev );
908 struct hermonprm_qp_ee_state_transitions qpctx;
909 unsigned long optparammask = 0;
912 /* Construct optparammask */
913 if ( mod_list & IB_MODIFY_QKEY )
914 optparammask |= HERMON_QP_OPT_PARAM_QKEY;
916 /* Issue RTS2RTS_QP */
917 memset ( &qpctx, 0, sizeof ( qpctx ) );
918 MLX_FILL_1 ( &qpctx, 0, opt_param_mask, optparammask );
919 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
920 if ( ( rc = hermon_cmd_rts2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
921 DBGC ( hermon, "Hermon %p RTS2RTS_QP failed: %s\n",
922 hermon, strerror ( rc ) );
932 * @v ibdev Infiniband device
935 static void hermon_destroy_qp ( struct ib_device *ibdev,
936 struct ib_queue_pair *qp ) {
937 struct hermon *hermon = ib_get_drvdata ( ibdev );
938 struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
942 /* Take ownership back from hardware */
943 if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
944 DBGC ( hermon, "Hermon %p FATAL 2RST_QP failed on QPN %#lx: "
945 "%s\n", hermon, qp->qpn, strerror ( rc ) );
946 /* Leak memory and return; at least we avoid corruption */
950 /* Free MTT entries */
951 hermon_free_mtt ( hermon, &hermon_qp->mtt );
954 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
957 /* Mark queue number as free */
958 qpn_offset = ( qp->qpn - HERMON_QPN_BASE -
959 hermon->cap.reserved_qps );
960 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
962 ib_qp_set_drvdata ( qp, NULL );
965 /***************************************************************************
967 * Work request operations
969 ***************************************************************************
972 /** GID used for GID-less send work queue entries */
973 static const struct ib_gid hermon_no_gid = {
974 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
978 * Post send work queue entry
980 * @v ibdev Infiniband device
982 * @v av Address vector
983 * @v iobuf I/O buffer
984 * @ret rc Return status code
986 static int hermon_post_send ( struct ib_device *ibdev,
987 struct ib_queue_pair *qp,
988 struct ib_address_vector *av,
989 struct io_buffer *iobuf ) {
990 struct hermon *hermon = ib_get_drvdata ( ibdev );
991 struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
992 struct ib_work_queue *wq = &qp->send;
993 struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
994 struct hermonprm_ud_send_wqe *wqe;
995 const struct ib_gid *gid;
996 union hermonprm_doorbell_register db_reg;
997 unsigned int wqe_idx_mask;
999 /* Allocate work queue entry */
1000 wqe_idx_mask = ( wq->num_wqes - 1 );
1001 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1002 DBGC ( hermon, "Hermon %p send queue full", hermon );
1005 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1006 wqe = &hermon_send_wq->wqe[ wq->next_idx &
1007 ( hermon_send_wq->num_wqes - 1 ) ].ud;
1009 /* Construct work queue entry */
1010 memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
1011 ( sizeof ( *wqe ) - 4 ) );
1012 MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( *wqe ) / 16 ) );
1013 MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
1014 MLX_FILL_2 ( &wqe->ud, 0,
1015 ud_address_vector.pd, HERMON_GLOBAL_PD,
1016 ud_address_vector.port_number, ibdev->port );
1017 MLX_FILL_2 ( &wqe->ud, 1,
1018 ud_address_vector.rlid, av->dlid,
1019 ud_address_vector.g, av->gid_present );
1020 MLX_FILL_1 ( &wqe->ud, 2,
1021 ud_address_vector.max_stat_rate,
1022 ( ( ( av->rate < 2 ) || ( av->rate > 10 ) ) ?
1023 8 : ( av->rate + 5 ) ) );
1024 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
1025 gid = ( av->gid_present ? &av->gid : &hermon_no_gid );
1026 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
1027 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
1028 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
1029 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
1030 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
1031 MLX_FILL_1 ( &wqe->data[0], 3,
1032 local_address_l, virt_to_bus ( iobuf->data ) );
1034 MLX_FILL_2 ( &wqe->ctrl, 0,
1035 opcode, HERMON_OPCODE_SEND,
1037 ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 ) );
1038 DBGCP ( hermon, "Hermon %p posting send WQE:\n", hermon );
1039 DBGCP_HD ( hermon, wqe, sizeof ( *wqe ) );
1042 /* Ring doorbell register */
1043 MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
1044 DBGCP ( hermon, "Ringing doorbell %08lx with %08lx\n",
1045 virt_to_phys ( hermon_send_wq->doorbell ), db_reg.dword[0] );
1046 writel ( db_reg.dword[0], ( hermon_send_wq->doorbell ) );
1048 /* Update work queue's index */
1055 * Post receive work queue entry
1057 * @v ibdev Infiniband device
1059 * @v iobuf I/O buffer
1060 * @ret rc Return status code
1062 static int hermon_post_recv ( struct ib_device *ibdev,
1063 struct ib_queue_pair *qp,
1064 struct io_buffer *iobuf ) {
1065 struct hermon *hermon = ib_get_drvdata ( ibdev );
1066 struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
1067 struct ib_work_queue *wq = &qp->recv;
1068 struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
1069 struct hermonprm_recv_wqe *wqe;
1070 unsigned int wqe_idx_mask;
1072 /* Allocate work queue entry */
1073 wqe_idx_mask = ( wq->num_wqes - 1 );
1074 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1075 DBGC ( hermon, "Hermon %p receive queue full", hermon );
1078 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1079 wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
1081 /* Construct work queue entry */
1082 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
1083 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
1084 MLX_FILL_1 ( &wqe->data[0], 3,
1085 local_address_l, virt_to_bus ( iobuf->data ) );
1087 /* Update work queue's index */
1090 /* Update doorbell record */
1092 MLX_FILL_1 ( &hermon_recv_wq->doorbell, 0, receive_wqe_counter,
1093 ( wq->next_idx & 0xffff ) );
1101 * @v ibdev Infiniband device
1102 * @v cq Completion queue
1103 * @v cqe Hardware completion queue entry
1104 * @v complete_send Send completion handler
1105 * @v complete_recv Receive completion handler
1106 * @ret rc Return status code
1108 static int hermon_complete ( struct ib_device *ibdev,
1109 struct ib_completion_queue *cq,
1110 union hermonprm_completion_entry *cqe,
1111 ib_completer_t complete_send,
1112 ib_completer_t complete_recv ) {
1113 struct hermon *hermon = ib_get_drvdata ( ibdev );
1114 struct ib_completion completion;
1115 struct ib_work_queue *wq;
1116 struct ib_queue_pair *qp;
1117 struct hermon_queue_pair *hermon_qp;
1118 struct io_buffer *iobuf;
1119 ib_completer_t complete;
1120 unsigned int opcode;
1123 unsigned int wqe_idx;
1126 /* Parse completion */
1127 memset ( &completion, 0, sizeof ( completion ) );
1128 qpn = MLX_GET ( &cqe->normal, qpn );
1129 is_send = MLX_GET ( &cqe->normal, s_r );
1130 opcode = MLX_GET ( &cqe->normal, opcode );
1131 if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
1132 /* "s" field is not valid for error opcodes */
1133 is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
1134 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
1135 DBGC ( hermon, "Hermon %p CQN %lx syndrome %x vendor %lx\n",
1136 hermon, cq->cqn, completion.syndrome,
1137 MLX_GET ( &cqe->error, vendor_error_syndrome ) );
1139 /* Don't return immediately; propagate error to completer */
1142 /* Identify work queue */
1143 wq = ib_find_wq ( cq, qpn, is_send );
1145 DBGC ( hermon, "Hermon %p CQN %lx unknown %s QPN %lx\n",
1146 hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1150 hermon_qp = ib_qp_get_drvdata ( qp );
1152 /* Identify I/O buffer */
1153 wqe_idx = ( MLX_GET ( &cqe->normal, wqe_counter ) &
1154 ( wq->num_wqes - 1 ) );
1155 iobuf = wq->iobufs[wqe_idx];
1157 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx empty WQE %x\n",
1158 hermon, cq->cqn, qpn, wqe_idx );
1161 wq->iobufs[wqe_idx] = NULL;
1163 /* Fill in length for received packets */
1165 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
1166 if ( completion.len > iob_tailroom ( iobuf ) ) {
1167 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx IDX %x "
1168 "overlength received packet length %zd\n",
1169 hermon, cq->cqn, qpn, wqe_idx, completion.len );
1174 /* Pass off to caller's completion handler */
1175 complete = ( is_send ? complete_send : complete_recv );
1176 complete ( ibdev, qp, &completion, iobuf );
1182 * Poll completion queue
1184 * @v ibdev Infiniband device
1185 * @v cq Completion queue
1186 * @v complete_send Send completion handler
1187 * @v complete_recv Receive completion handler
1189 static void hermon_poll_cq ( struct ib_device *ibdev,
1190 struct ib_completion_queue *cq,
1191 ib_completer_t complete_send,
1192 ib_completer_t complete_recv ) {
1193 struct hermon *hermon = ib_get_drvdata ( ibdev );
1194 struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
1195 union hermonprm_completion_entry *cqe;
1196 unsigned int cqe_idx_mask;
1200 /* Look for completion entry */
1201 cqe_idx_mask = ( cq->num_cqes - 1 );
1202 cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
1203 if ( MLX_GET ( &cqe->normal, owner ) ^
1204 ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
1205 /* Entry still owned by hardware; end of poll */
1208 DBGCP ( hermon, "Hermon %p completion:\n", hermon );
1209 DBGCP_HD ( hermon, cqe, sizeof ( *cqe ) );
1211 /* Handle completion */
1212 if ( ( rc = hermon_complete ( ibdev, cq, cqe, complete_send,
1213 complete_recv ) ) != 0 ) {
1214 DBGC ( hermon, "Hermon %p failed to complete: %s\n",
1215 hermon, strerror ( rc ) );
1216 DBGC_HD ( hermon, cqe, sizeof ( *cqe ) );
1219 /* Update completion queue's index */
1222 /* Update doorbell record */
1223 MLX_FILL_1 ( &hermon_cq->doorbell, 0, update_ci,
1224 ( cq->next_idx & 0x00ffffffUL ) );
1228 /***************************************************************************
1232 ***************************************************************************
1236 * Create event queue
1238 * @v hermon Hermon device
1239 * @ret rc Return status code
1241 static int hermon_create_eq ( struct hermon *hermon ) {
1242 struct hermon_event_queue *hermon_eq = &hermon->eq;
1243 struct hermonprm_eqc eqctx;
1244 struct hermonprm_event_mask mask;
1248 /* Select event queue number */
1249 hermon_eq->eqn = ( 4 * hermon->cap.reserved_uars );
1250 if ( hermon_eq->eqn < hermon->cap.reserved_eqs )
1251 hermon_eq->eqn = hermon->cap.reserved_eqs;
1253 /* Calculate doorbell address */
1254 hermon_eq->doorbell =
1255 ( hermon->uar + HERMON_DB_EQ_OFFSET ( hermon_eq->eqn ) );
1257 /* Allocate event queue itself */
1258 hermon_eq->eqe_size =
1259 ( HERMON_NUM_EQES * sizeof ( hermon_eq->eqe[0] ) );
1260 hermon_eq->eqe = malloc_dma ( hermon_eq->eqe_size,
1261 sizeof ( hermon_eq->eqe[0] ) );
1262 if ( ! hermon_eq->eqe ) {
1266 memset ( hermon_eq->eqe, 0, hermon_eq->eqe_size );
1267 for ( i = 0 ; i < HERMON_NUM_EQES ; i++ ) {
1268 MLX_FILL_1 ( &hermon_eq->eqe[i].generic, 7, owner, 1 );
1272 /* Allocate MTT entries */
1273 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_eq->eqe,
1274 hermon_eq->eqe_size,
1275 &hermon_eq->mtt ) ) != 0 )
1278 /* Hand queue over to hardware */
1279 memset ( &eqctx, 0, sizeof ( eqctx ) );
1280 MLX_FILL_1 ( &eqctx, 0, st, 0xa /* "Fired" */ );
1281 MLX_FILL_1 ( &eqctx, 2,
1282 page_offset, ( hermon_eq->mtt.page_offset >> 5 ) );
1283 MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( HERMON_NUM_EQES - 1 ) );
1284 MLX_FILL_1 ( &eqctx, 7, mtt_base_addr_l,
1285 ( hermon_eq->mtt.mtt_base_addr >> 3 ) );
1286 if ( ( rc = hermon_cmd_sw2hw_eq ( hermon, hermon_eq->eqn,
1288 DBGC ( hermon, "Hermon %p SW2HW_EQ failed: %s\n",
1289 hermon, strerror ( rc ) );
1293 /* Map events to this event queue */
1294 memset ( &mask, 0, sizeof ( mask ) );
1295 MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
1296 if ( ( rc = hermon_cmd_map_eq ( hermon,
1297 ( HERMON_MAP_EQ | hermon_eq->eqn ),
1299 DBGC ( hermon, "Hermon %p MAP_EQ failed: %s\n",
1300 hermon, strerror ( rc ) );
1304 DBGC ( hermon, "Hermon %p EQN %#lx ring at [%p,%p])\n",
1305 hermon, hermon_eq->eqn, hermon_eq->eqe,
1306 ( ( ( void * ) hermon_eq->eqe ) + hermon_eq->eqe_size ) );
1310 hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn, &eqctx );
1312 hermon_free_mtt ( hermon, &hermon_eq->mtt );
1314 free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
1316 memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
1321 * Destroy event queue
1323 * @v hermon Hermon device
1325 static void hermon_destroy_eq ( struct hermon *hermon ) {
1326 struct hermon_event_queue *hermon_eq = &hermon->eq;
1327 struct hermonprm_eqc eqctx;
1328 struct hermonprm_event_mask mask;
1331 /* Unmap events from event queue */
1332 memset ( &mask, 0, sizeof ( mask ) );
1333 MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
1334 if ( ( rc = hermon_cmd_map_eq ( hermon,
1335 ( HERMON_UNMAP_EQ | hermon_eq->eqn ),
1337 DBGC ( hermon, "Hermon %p FATAL MAP_EQ failed to unmap: %s\n",
1338 hermon, strerror ( rc ) );
1339 /* Continue; HCA may die but system should survive */
1342 /* Take ownership back from hardware */
1343 if ( ( rc = hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn,
1345 DBGC ( hermon, "Hermon %p FATAL HW2SW_EQ failed: %s\n",
1346 hermon, strerror ( rc ) );
1347 /* Leak memory and return; at least we avoid corruption */
1351 /* Free MTT entries */
1352 hermon_free_mtt ( hermon, &hermon_eq->mtt );
1355 free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
1356 memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
1360 * Handle port state event
1362 * @v hermon Hermon device
1363 * @v eqe Port state change event queue entry
1365 static void hermon_event_port_state_change ( struct hermon *hermon,
1366 union hermonprm_event_entry *eqe){
1370 /* Get port and link status */
1371 port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
1372 link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
1373 DBGC ( hermon, "Hermon %p port %d link %s\n", hermon, ( port + 1 ),
1374 ( link_up ? "up" : "down" ) );
1377 if ( port >= HERMON_NUM_PORTS ) {
1378 DBGC ( hermon, "Hermon %p port %d does not exist!\n",
1379 hermon, ( port + 1 ) );
1383 /* Notify Infiniband core of link state change */
1384 ib_link_state_changed ( hermon->ibdev[port] );
1390 * @v ibdev Infiniband device
1392 static void hermon_poll_eq ( struct ib_device *ibdev ) {
1393 struct hermon *hermon = ib_get_drvdata ( ibdev );
1394 struct hermon_event_queue *hermon_eq = &hermon->eq;
1395 union hermonprm_event_entry *eqe;
1396 union hermonprm_doorbell_register db_reg;
1397 unsigned int eqe_idx_mask;
1398 unsigned int event_type;
1401 /* Look for event entry */
1402 eqe_idx_mask = ( HERMON_NUM_EQES - 1 );
1403 eqe = &hermon_eq->eqe[hermon_eq->next_idx & eqe_idx_mask];
1404 if ( MLX_GET ( &eqe->generic, owner ) ^
1405 ( ( hermon_eq->next_idx & HERMON_NUM_EQES ) ? 1 : 0 ) ) {
1406 /* Entry still owned by hardware; end of poll */
1409 DBGCP ( hermon, "Hermon %p event:\n", hermon );
1410 DBGCP_HD ( hermon, eqe, sizeof ( *eqe ) );
1413 event_type = MLX_GET ( &eqe->generic, event_type );
1414 switch ( event_type ) {
1415 case HERMON_EV_PORT_STATE_CHANGE:
1416 hermon_event_port_state_change ( hermon, eqe );
1419 DBGC ( hermon, "Hermon %p unrecognised event type "
1420 "%#x:\n", hermon, event_type );
1421 DBGC_HD ( hermon, eqe, sizeof ( *eqe ) );
1425 /* Update event queue's index */
1426 hermon_eq->next_idx++;
1429 MLX_FILL_1 ( &db_reg.event, 0,
1430 ci, ( hermon_eq->next_idx & 0x00ffffffUL ) );
1431 DBGCP ( hermon, "Ringing doorbell %08lx with %08lx\n",
1432 virt_to_phys ( hermon_eq->doorbell ),
1434 writel ( db_reg.dword[0], hermon_eq->doorbell );
1438 /***************************************************************************
1440 * Infiniband link-layer operations
1442 ***************************************************************************
1446 * Initialise Infiniband link
1448 * @v ibdev Infiniband device
1449 * @ret rc Return status code
1451 static int hermon_open ( struct ib_device *ibdev ) {
1452 struct hermon *hermon = ib_get_drvdata ( ibdev );
1453 struct hermonprm_init_port init_port;
1456 memset ( &init_port, 0, sizeof ( init_port ) );
1457 MLX_FILL_2 ( &init_port, 0,
1460 MLX_FILL_2 ( &init_port, 1,
1461 mtu, HERMON_MTU_2048,
1463 MLX_FILL_1 ( &init_port, 2, max_pkey, 64 );
1464 if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port,
1465 &init_port ) ) != 0 ) {
1466 DBGC ( hermon, "Hermon %p could not intialise port: %s\n",
1467 hermon, strerror ( rc ) );
1475 * Close Infiniband link
1477 * @v ibdev Infiniband device
1479 static void hermon_close ( struct ib_device *ibdev ) {
1480 struct hermon *hermon = ib_get_drvdata ( ibdev );
1483 if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
1484 DBGC ( hermon, "Hermon %p could not close port: %s\n",
1485 hermon, strerror ( rc ) );
1486 /* Nothing we can do about this */
1490 /***************************************************************************
1492 * Multicast group operations
1494 ***************************************************************************
1498 * Attach to multicast group
1500 * @v ibdev Infiniband device
1502 * @v gid Multicast GID
1503 * @ret rc Return status code
1505 static int hermon_mcast_attach ( struct ib_device *ibdev,
1506 struct ib_queue_pair *qp,
1507 struct ib_gid *gid ) {
1508 struct hermon *hermon = ib_get_drvdata ( ibdev );
1509 struct hermonprm_mgm_hash hash;
1510 struct hermonprm_mcg_entry mcg;
1514 /* Generate hash table index */
1515 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1516 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1517 hermon, strerror ( rc ) );
1520 index = MLX_GET ( &hash, hash );
1522 /* Check for existing hash table entry */
1523 if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
1524 DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
1525 hermon, index, strerror ( rc ) );
1528 if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
1529 /* FIXME: this implementation allows only a single QP
1530 * per multicast group, and doesn't handle hash
1531 * collisions. Sufficient for IPoIB but may need to
1532 * be extended in future.
1534 DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
1539 /* Update hash table entry */
1540 MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
1541 MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
1542 memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
1543 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1544 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1545 hermon, index, strerror ( rc ) );
1553 * Detach from multicast group
1555 * @v ibdev Infiniband device
1557 * @v gid Multicast GID
1559 static void hermon_mcast_detach ( struct ib_device *ibdev,
1560 struct ib_queue_pair *qp __unused,
1561 struct ib_gid *gid ) {
1562 struct hermon *hermon = ib_get_drvdata ( ibdev );
1563 struct hermonprm_mgm_hash hash;
1564 struct hermonprm_mcg_entry mcg;
1568 /* Generate hash table index */
1569 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1570 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1571 hermon, strerror ( rc ) );
1574 index = MLX_GET ( &hash, hash );
1576 /* Clear hash table entry */
1577 memset ( &mcg, 0, sizeof ( mcg ) );
1578 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1579 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1580 hermon, index, strerror ( rc ) );
1585 /***************************************************************************
1589 ***************************************************************************
1593 * Issue management datagram
1595 * @v ibdev Infiniband device
1596 * @v mad Management datagram
1597 * @v len Length of management datagram
1598 * @ret rc Return status code
1600 static int hermon_mad ( struct ib_device *ibdev, struct ib_mad_hdr *mad,
1602 struct hermon *hermon = ib_get_drvdata ( ibdev );
1603 union hermonprm_mad mad_ifc;
1606 /* Copy in request packet */
1607 memset ( &mad_ifc, 0, sizeof ( mad_ifc ) );
1608 assert ( len <= sizeof ( mad_ifc.mad ) );
1609 memcpy ( &mad_ifc.mad, mad, len );
1612 if ( ( rc = hermon_cmd_mad_ifc ( hermon, ibdev->port,
1613 &mad_ifc ) ) != 0 ) {
1614 DBGC ( hermon, "Hermon %p could not issue MAD IFC: %s\n",
1615 hermon, strerror ( rc ) );
1619 /* Copy out reply packet */
1620 memcpy ( mad, &mad_ifc.mad, len );
1622 if ( mad->status != 0 ) {
1623 DBGC ( hermon, "Hermon %p MAD IFC status %04x\n",
1624 hermon, ntohs ( mad->status ) );
1630 /** Hermon Infiniband operations */
1631 static struct ib_device_operations hermon_ib_operations = {
1632 .create_cq = hermon_create_cq,
1633 .destroy_cq = hermon_destroy_cq,
1634 .create_qp = hermon_create_qp,
1635 .modify_qp = hermon_modify_qp,
1636 .destroy_qp = hermon_destroy_qp,
1637 .post_send = hermon_post_send,
1638 .post_recv = hermon_post_recv,
1639 .poll_cq = hermon_poll_cq,
1640 .poll_eq = hermon_poll_eq,
1641 .open = hermon_open,
1642 .close = hermon_close,
1643 .mcast_attach = hermon_mcast_attach,
1644 .mcast_detach = hermon_mcast_detach,
1648 /***************************************************************************
1652 ***************************************************************************
1656 * Map virtual to physical address for firmware usage
1658 * @v hermon Hermon device
1659 * @v map Mapping function
1660 * @v va Virtual address
1661 * @v pa Physical address
1662 * @v len Length of region
1663 * @ret rc Return status code
1665 static int hermon_map_vpm ( struct hermon *hermon,
1666 int ( *map ) ( struct hermon *hermon,
1667 const struct hermonprm_virtual_physical_mapping* ),
1668 uint64_t va, physaddr_t pa, size_t len ) {
1669 struct hermonprm_virtual_physical_mapping mapping;
1672 assert ( ( va & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
1673 assert ( ( pa & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
1674 assert ( ( len & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
1677 memset ( &mapping, 0, sizeof ( mapping ) );
1678 MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
1679 MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
1680 MLX_FILL_2 ( &mapping, 3,
1682 pa_l, ( pa >> 12 ) );
1683 if ( ( rc = map ( hermon, &mapping ) ) != 0 ) {
1684 DBGC ( hermon, "Hermon %p could not map %llx => %lx: "
1685 "%s\n", hermon, va, pa, strerror ( rc ) );
1688 pa += HERMON_PAGE_SIZE;
1689 va += HERMON_PAGE_SIZE;
1690 len -= HERMON_PAGE_SIZE;
1697 * Start firmware running
1699 * @v hermon Hermon device
1700 * @ret rc Return status code
1702 static int hermon_start_firmware ( struct hermon *hermon ) {
1703 struct hermonprm_query_fw fw;
1704 unsigned int fw_pages;
1709 /* Get firmware parameters */
1710 if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
1711 DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
1712 hermon, strerror ( rc ) );
1715 DBGC ( hermon, "Hermon %p firmware version %ld.%ld.%ld\n", hermon,
1716 MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
1717 MLX_GET ( &fw, fw_rev_subminor ) );
1718 fw_pages = MLX_GET ( &fw, fw_pages );
1719 DBGC ( hermon, "Hermon %p requires %d pages (%d kB) for firmware\n",
1720 hermon, fw_pages, ( fw_pages * ( HERMON_PAGE_SIZE / 1024 ) ) );
1722 /* Allocate firmware pages and map firmware area */
1723 fw_size = ( fw_pages * HERMON_PAGE_SIZE );
1724 hermon->firmware_area = umalloc ( fw_size );
1725 if ( ! hermon->firmware_area ) {
1729 fw_base = user_to_phys ( hermon->firmware_area, 0 );
1730 DBGC ( hermon, "Hermon %p firmware area at physical [%lx,%lx)\n",
1731 hermon, fw_base, ( fw_base + fw_size ) );
1732 if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_fa,
1733 0, fw_base, fw_size ) ) != 0 ) {
1734 DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
1735 hermon, strerror ( rc ) );
1739 /* Start firmware */
1740 if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
1741 DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
1742 hermon, strerror ( rc ) );
1746 DBGC ( hermon, "Hermon %p firmware started\n", hermon );
1751 hermon_cmd_unmap_fa ( hermon );
1752 ufree ( hermon->firmware_area );
1753 hermon->firmware_area = UNULL;
1760 * Stop firmware running
1762 * @v hermon Hermon device
1764 static void hermon_stop_firmware ( struct hermon *hermon ) {
1767 if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
1768 DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
1769 hermon, strerror ( rc ) );
1770 /* Leak memory and return; at least we avoid corruption */
1773 ufree ( hermon->firmware_area );
1774 hermon->firmware_area = UNULL;
1777 /***************************************************************************
1779 * Infinihost Context Memory management
1781 ***************************************************************************
1787 * @v hermon Hermon device
1788 * @ret rc Return status code
1790 static int hermon_get_cap ( struct hermon *hermon ) {
1791 struct hermonprm_query_dev_cap dev_cap;
1794 if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
1795 DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
1796 hermon, strerror ( rc ) );
1800 hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
1801 hermon->cap.reserved_qps =
1802 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
1803 hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
1804 hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
1805 hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
1806 hermon->cap.reserved_srqs =
1807 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
1808 hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
1809 hermon->cap.reserved_cqs =
1810 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
1811 hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
1812 hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
1813 hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
1814 hermon->cap.reserved_mtts =
1815 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
1816 hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
1817 hermon->cap.reserved_mrws =
1818 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
1819 hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
1820 hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
1828 * @v log_num_entries Log2 of the number of entries
1829 * @v entry_size Entry size
1830 * @ret usage Usage size in ICM
1832 static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
1835 usage = ( ( 1 << log_num_entries ) * entry_size );
1836 usage = ( ( usage + HERMON_PAGE_SIZE - 1 ) &
1837 ~( HERMON_PAGE_SIZE - 1 ) );
1844 * @v hermon Hermon device
1845 * @v init_hca INIT_HCA structure to fill in
1846 * @ret rc Return status code
1848 static int hermon_alloc_icm ( struct hermon *hermon,
1849 struct hermonprm_init_hca *init_hca ) {
1850 struct hermonprm_scalar_parameter icm_size;
1851 struct hermonprm_scalar_parameter icm_aux_size;
1852 uint64_t icm_offset = 0;
1853 unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
1854 unsigned int log_num_mtts, log_num_mpts;
1855 size_t cmpt_max_len;
1856 size_t qp_cmpt_len, srq_cmpt_len, cq_cmpt_len, eq_cmpt_len;
1857 size_t icm_len, icm_aux_len;
1858 physaddr_t icm_phys;
1863 * Start by carving up the ICM virtual address space
1867 /* Calculate number of each object type within ICM */
1868 log_num_qps = fls ( hermon->cap.reserved_qps + HERMON_MAX_QPS - 1 );
1869 log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
1870 log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
1871 log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
1872 log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
1874 /* ICM starts with the cMPT tables, which are sparse */
1875 cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
1876 ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
1877 qp_cmpt_len = icm_usage ( log_num_qps, hermon->cap.cmpt_entry_size );
1878 hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
1879 hermon->icm_map[HERMON_ICM_QP_CMPT].len = qp_cmpt_len;
1880 icm_offset += cmpt_max_len;
1881 srq_cmpt_len = icm_usage ( log_num_srqs, hermon->cap.cmpt_entry_size );
1882 hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
1883 hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = srq_cmpt_len;
1884 icm_offset += cmpt_max_len;
1885 cq_cmpt_len = icm_usage ( log_num_cqs, hermon->cap.cmpt_entry_size );
1886 hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
1887 hermon->icm_map[HERMON_ICM_CQ_CMPT].len = cq_cmpt_len;
1888 icm_offset += cmpt_max_len;
1889 eq_cmpt_len = icm_usage ( log_num_eqs, hermon->cap.cmpt_entry_size );
1890 hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
1891 hermon->icm_map[HERMON_ICM_EQ_CMPT].len = eq_cmpt_len;
1892 icm_offset += cmpt_max_len;
1894 hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
1896 /* Queue pair contexts */
1897 MLX_FILL_1 ( init_hca, 12,
1898 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
1899 ( icm_offset >> 32 ) );
1900 MLX_FILL_2 ( init_hca, 13,
1901 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
1902 ( icm_offset >> 5 ),
1903 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
1905 DBGC ( hermon, "Hermon %p ICM QPC base = %llx\n", hermon, icm_offset );
1906 icm_offset += icm_usage ( log_num_qps, hermon->cap.qpc_entry_size );
1908 /* Extended alternate path contexts */
1909 MLX_FILL_1 ( init_hca, 24,
1910 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
1911 ( icm_offset >> 32 ) );
1912 MLX_FILL_1 ( init_hca, 25,
1913 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
1915 DBGC ( hermon, "Hermon %p ICM ALTC base = %llx\n", hermon, icm_offset);
1916 icm_offset += icm_usage ( log_num_qps,
1917 hermon->cap.altc_entry_size );
1919 /* Extended auxiliary contexts */
1920 MLX_FILL_1 ( init_hca, 28,
1921 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
1922 ( icm_offset >> 32 ) );
1923 MLX_FILL_1 ( init_hca, 29,
1924 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
1926 DBGC ( hermon, "Hermon %p ICM AUXC base = %llx\n", hermon, icm_offset);
1927 icm_offset += icm_usage ( log_num_qps,
1928 hermon->cap.auxc_entry_size );
1930 /* Shared receive queue contexts */
1931 MLX_FILL_1 ( init_hca, 18,
1932 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
1933 ( icm_offset >> 32 ) );
1934 MLX_FILL_2 ( init_hca, 19,
1935 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
1936 ( icm_offset >> 5 ),
1937 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
1939 DBGC ( hermon, "Hermon %p ICM SRQC base = %llx\n", hermon, icm_offset);
1940 icm_offset += icm_usage ( log_num_srqs,
1941 hermon->cap.srqc_entry_size );
1943 /* Completion queue contexts */
1944 MLX_FILL_1 ( init_hca, 20,
1945 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
1946 ( icm_offset >> 32 ) );
1947 MLX_FILL_2 ( init_hca, 21,
1948 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
1949 ( icm_offset >> 5 ),
1950 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
1952 DBGC ( hermon, "Hermon %p ICM CQC base = %llx\n", hermon, icm_offset );
1953 icm_offset += icm_usage ( log_num_cqs, hermon->cap.cqc_entry_size );
1955 /* Event queue contexts */
1956 MLX_FILL_1 ( init_hca, 32,
1957 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
1958 ( icm_offset >> 32 ) );
1959 MLX_FILL_2 ( init_hca, 33,
1960 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
1961 ( icm_offset >> 5 ),
1962 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
1964 DBGC ( hermon, "Hermon %p ICM EQC base = %llx\n", hermon, icm_offset );
1965 icm_offset += icm_usage ( log_num_eqs, hermon->cap.eqc_entry_size );
1967 /* Memory translation table */
1968 MLX_FILL_1 ( init_hca, 64,
1969 tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
1970 MLX_FILL_1 ( init_hca, 65,
1971 tpt_parameters.mtt_base_addr_l, icm_offset );
1972 DBGC ( hermon, "Hermon %p ICM MTT base = %llx\n", hermon, icm_offset );
1973 icm_offset += icm_usage ( log_num_mtts,
1974 hermon->cap.mtt_entry_size );
1976 /* Memory protection table */
1977 log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
1978 MLX_FILL_1 ( init_hca, 60,
1979 tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
1980 MLX_FILL_1 ( init_hca, 61,
1981 tpt_parameters.dmpt_base_adr_l, icm_offset );
1982 MLX_FILL_1 ( init_hca, 62,
1983 tpt_parameters.log_dmpt_sz, log_num_mpts );
1984 DBGC ( hermon, "Hermon %p ICM DMPT base = %llx\n", hermon, icm_offset);
1985 icm_offset += icm_usage ( log_num_mpts,
1986 hermon->cap.dmpt_entry_size );
1988 /* Multicast table */
1989 MLX_FILL_1 ( init_hca, 48,
1990 multicast_parameters.mc_base_addr_h,
1991 ( icm_offset >> 32 ) );
1992 MLX_FILL_1 ( init_hca, 49,
1993 multicast_parameters.mc_base_addr_l, icm_offset );
1994 MLX_FILL_1 ( init_hca, 52,
1995 multicast_parameters.log_mc_table_entry_sz,
1996 fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
1997 MLX_FILL_1 ( init_hca, 53,
1998 multicast_parameters.log_mc_table_hash_sz, 3 );
1999 MLX_FILL_1 ( init_hca, 54,
2000 multicast_parameters.log_mc_table_sz, 3 );
2001 DBGC ( hermon, "Hermon %p ICM MC base = %llx\n", hermon, icm_offset );
2002 icm_offset += ( ( 8 * sizeof ( struct hermonprm_mcg_entry ) +
2003 HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
2005 hermon->icm_map[HERMON_ICM_OTHER].len =
2006 ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
2009 * Allocate and map physical memory for (portions of) ICM
2012 * ICM AUX area (aligned to its own size)
2017 /* Calculate physical memory required for ICM */
2019 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
2020 icm_len += hermon->icm_map[i].len;
2023 /* Get ICM auxiliary area size */
2024 memset ( &icm_size, 0, sizeof ( icm_size ) );
2025 MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
2026 MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
2027 if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
2028 &icm_aux_size ) ) != 0 ) {
2029 DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
2030 hermon, strerror ( rc ) );
2031 goto err_set_icm_size;
2033 icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
2035 /* Allocate ICM data and auxiliary area */
2036 DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
2037 hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
2038 hermon->icm = umalloc ( icm_aux_len + icm_len );
2039 if ( ! hermon->icm ) {
2043 icm_phys = user_to_phys ( hermon->icm, 0 );
2045 /* Map ICM auxiliary area */
2046 DBGC ( hermon, "Hermon %p mapping ICM AUX => %08lx\n",
2048 if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm_aux,
2049 0, icm_phys, icm_aux_len ) ) != 0 ) {
2050 DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
2051 hermon, strerror ( rc ) );
2052 goto err_map_icm_aux;
2054 icm_phys += icm_aux_len;
2057 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
2058 DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx => %08lx\n",
2059 hermon, hermon->icm_map[i].offset,
2060 hermon->icm_map[i].len, icm_phys );
2061 if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm,
2062 hermon->icm_map[i].offset,
2064 hermon->icm_map[i].len ) ) != 0 ){
2065 DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
2066 hermon, strerror ( rc ) );
2069 icm_phys += hermon->icm_map[i].len;
2075 assert ( i == 0 ); /* We don't handle partial failure at present */
2077 hermon_cmd_unmap_icm_aux ( hermon );
2078 ufree ( hermon->icm );
2079 hermon->icm = UNULL;
2088 * @v hermon Hermon device
2090 static void hermon_free_icm ( struct hermon *hermon ) {
2091 struct hermonprm_scalar_parameter unmap_icm;
2094 for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
2095 memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
2096 MLX_FILL_1 ( &unmap_icm, 0, value_hi,
2097 ( hermon->icm_map[i].offset >> 32 ) );
2098 MLX_FILL_1 ( &unmap_icm, 1, value,
2099 hermon->icm_map[i].offset );
2100 hermon_cmd_unmap_icm ( hermon,
2101 ( 1 << fls ( ( hermon->icm_map[i].len /
2102 HERMON_PAGE_SIZE ) - 1)),
2105 hermon_cmd_unmap_icm_aux ( hermon );
2106 ufree ( hermon->icm );
2107 hermon->icm = UNULL;
2110 /***************************************************************************
2114 ***************************************************************************
2118 * Set up memory protection table
2120 * @v hermon Hermon device
2121 * @ret rc Return status code
2123 static int hermon_setup_mpt ( struct hermon *hermon ) {
2124 struct hermonprm_mpt mpt;
2129 key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
2130 hermon->reserved_lkey = ( ( key << 8 ) | ( key >> 24 ) );
2132 /* Initialise memory protection table */
2133 memset ( &mpt, 0, sizeof ( mpt ) );
2134 MLX_FILL_4 ( &mpt, 0,
2139 MLX_FILL_1 ( &mpt, 2, mem_key, key );
2140 MLX_FILL_1 ( &mpt, 3, pd, HERMON_GLOBAL_PD );
2141 MLX_FILL_1 ( &mpt, 10, len64, 1 );
2142 if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
2143 hermon->cap.reserved_mrws,
2145 DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
2146 hermon, strerror ( rc ) );
2158 * @ret rc Return status code
2160 static int hermon_probe ( struct pci_device *pci,
2161 const struct pci_device_id *id __unused ) {
2162 struct hermon *hermon;
2163 struct ib_device *ibdev;
2164 struct hermonprm_init_hca init_hca;
2168 /* Allocate Hermon device */
2169 hermon = zalloc ( sizeof ( *hermon ) );
2172 goto err_alloc_hermon;
2174 pci_set_drvdata ( pci, hermon );
2176 /* Allocate Infiniband devices */
2177 for ( i = 0 ; i < HERMON_NUM_PORTS ; i++ ) {
2178 ibdev = alloc_ibdev ( 0 );
2181 goto err_alloc_ibdev;
2183 hermon->ibdev[i] = ibdev;
2184 ibdev->op = &hermon_ib_operations;
2185 ibdev->dev = &pci->dev;
2186 ibdev->port = ( HERMON_PORT_BASE + i );
2187 ib_set_drvdata ( ibdev, hermon );
2190 /* Fix up PCI device */
2191 adjust_pci_device ( pci );
2194 hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR),
2195 HERMON_PCI_CONFIG_BAR_SIZE );
2196 hermon->uar = ioremap ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ),
2197 HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE );
2199 /* Allocate space for mailboxes */
2200 hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
2201 HERMON_MBOX_ALIGN );
2202 if ( ! hermon->mailbox_in ) {
2204 goto err_mailbox_in;
2206 hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
2207 HERMON_MBOX_ALIGN );
2208 if ( ! hermon->mailbox_out ) {
2210 goto err_mailbox_out;
2213 /* Start firmware */
2214 if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
2215 goto err_start_firmware;
2217 /* Get device limits */
2218 if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
2222 memset ( &init_hca, 0, sizeof ( init_hca ) );
2223 if ( ( rc = hermon_alloc_icm ( hermon, &init_hca ) ) != 0 )
2226 /* Initialise HCA */
2227 MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
2228 MLX_FILL_1 ( &init_hca, 5, udp, 1 );
2229 MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
2230 if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
2231 DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
2232 hermon, strerror ( rc ) );
2236 /* Set up memory protection */
2237 if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
2240 /* Set up event queue */
2241 if ( ( rc = hermon_create_eq ( hermon ) ) != 0 )
2244 /* Register Infiniband devices */
2245 for ( i = 0 ; i < HERMON_NUM_PORTS ; i++ ) {
2246 if ( ( rc = register_ibdev ( hermon->ibdev[i] ) ) != 0 ) {
2247 DBGC ( hermon, "Hermon %p could not register IB "
2248 "device: %s\n", hermon, strerror ( rc ) );
2249 goto err_register_ibdev;
2255 i = HERMON_NUM_PORTS;
2257 for ( i-- ; i >= 0 ; i-- )
2258 unregister_ibdev ( hermon->ibdev[i] );
2259 hermon_destroy_eq ( hermon );
2262 hermon_cmd_close_hca ( hermon );
2264 hermon_free_icm ( hermon );
2267 hermon_stop_firmware ( hermon );
2269 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2271 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2273 i = HERMON_NUM_PORTS;
2275 for ( i-- ; i >= 0 ; i-- )
2276 ibdev_put ( hermon->ibdev[i] );
2287 static void hermon_remove ( struct pci_device *pci ) {
2288 struct hermon *hermon = pci_get_drvdata ( pci );
2291 for ( i = ( HERMON_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
2292 unregister_ibdev ( hermon->ibdev[i] );
2293 hermon_destroy_eq ( hermon );
2294 hermon_cmd_close_hca ( hermon );
2295 hermon_free_icm ( hermon );
2296 hermon_stop_firmware ( hermon );
2297 hermon_stop_firmware ( hermon );
2298 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2299 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2300 for ( i = ( HERMON_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
2301 ibdev_put ( hermon->ibdev[i] );
2305 static struct pci_device_id hermon_nics[] = {
2306 PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver" ),
2307 PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver" ),
2308 PCI_ROM ( 0x15b3, 0x6732, "mt26418", "MT26418 HCA driver" ),
2311 struct pci_driver hermon_driver __pci_driver = {
2313 .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
2314 .probe = hermon_probe,
2315 .remove = hermon_remove,