2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * Based in part upon the original driver by Mellanox Technologies
5 * Ltd. Portions may be Copyright (c) Mellanox Technologies Ltd.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <gpxe/malloc.h>
33 #include <gpxe/umalloc.h>
34 #include <gpxe/iobuf.h>
35 #include <gpxe/netdevice.h>
36 #include <gpxe/infiniband.h>
37 #include <gpxe/ipoib.h>
43 * Mellanox Hermon Infiniband HCA
50 /***************************************************************************
52 * Queue number allocation
54 ***************************************************************************
58 * Allocate offsets within usage bitmask
60 * @v bits Usage bitmask
61 * @v bits_len Length of usage bitmask
62 * @v num_bits Number of contiguous bits to allocate within bitmask
63 * @ret bit First free bit within bitmask, or negative error
65 static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
66 unsigned int bits_len,
67 unsigned int num_bits ) {
69 hermon_bitmask_t mask = 1;
70 unsigned int found = 0;
72 /* Search bits for num_bits contiguous free bits */
73 while ( bit < bits_len ) {
74 if ( ( mask & *bits ) == 0 ) {
75 if ( ++found == num_bits )
81 mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
88 /* Mark bits as in-use */
93 mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
96 return ( bit - num_bits + 1 );
100 * Free offsets within usage bitmask
102 * @v bits Usage bitmask
103 * @v bit Starting bit within bitmask
104 * @v num_bits Number of contiguous bits to free within bitmask
106 static void hermon_bitmask_free ( hermon_bitmask_t *bits,
107 int bit, unsigned int num_bits ) {
108 hermon_bitmask_t mask;
110 for ( ; num_bits ; bit++, num_bits-- ) {
111 mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
112 bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
116 /***************************************************************************
120 ***************************************************************************
124 * Wait for Hermon command completion
126 * @v hermon Hermon device
127 * @v hcr HCA command registers
128 * @ret rc Return status code
130 static int hermon_cmd_wait ( struct hermon *hermon,
131 struct hermonprm_hca_command_register *hcr ) {
134 for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
136 readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
137 if ( ( MLX_GET ( hcr, go ) == 0 ) &&
138 ( MLX_GET ( hcr, t ) == hermon->toggle ) )
148 * @v hermon Hermon device
149 * @v command Command opcode, flags and input/output lengths
150 * @v op_mod Opcode modifier (0 if no modifier applicable)
151 * @v in Input parameters
152 * @v in_mod Input modifier (0 if no modifier applicable)
153 * @v out Output parameters
154 * @ret rc Return status code
156 static int hermon_cmd ( struct hermon *hermon, unsigned long command,
157 unsigned int op_mod, const void *in,
158 unsigned int in_mod, void *out ) {
159 struct hermonprm_hca_command_register hcr;
160 unsigned int opcode = HERMON_HCR_OPCODE ( command );
161 size_t in_len = HERMON_HCR_IN_LEN ( command );
162 size_t out_len = HERMON_HCR_OUT_LEN ( command );
169 assert ( in_len <= HERMON_MBOX_SIZE );
170 assert ( out_len <= HERMON_MBOX_SIZE );
172 DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
173 hermon, opcode, in_len,
174 ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
175 ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
177 /* Check that HCR is free */
178 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
179 DBGC ( hermon, "Hermon %p command interface locked\n",
184 /* Flip HCR toggle */
185 hermon->toggle = ( 1 - hermon->toggle );
188 memset ( &hcr, 0, sizeof ( hcr ) );
189 in_buffer = &hcr.u.dwords[0];
190 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
191 in_buffer = hermon->mailbox_in;
192 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
194 memcpy ( in_buffer, in, in_len );
195 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
196 out_buffer = &hcr.u.dwords[3];
197 if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
198 out_buffer = hermon->mailbox_out;
199 MLX_FILL_1 ( &hcr, 4, out_param_l,
200 virt_to_bus ( out_buffer ) );
202 MLX_FILL_4 ( &hcr, 6,
204 opcode_modifier, op_mod,
207 DBGC ( hermon, "Hermon %p issuing command:\n", hermon );
208 DBGC_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
209 &hcr, sizeof ( hcr ) );
210 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
211 DBGC2 ( hermon, "Input mailbox:\n" );
212 DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
213 ( ( in_len < 512 ) ? in_len : 512 ) );
217 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
219 writel ( hcr.u.dwords[i],
220 hermon->config + HERMON_HCR_REG ( i ) );
224 /* Wait for command completion */
225 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
226 DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
229 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
230 &hcr, sizeof ( hcr ) );
234 /* Check command status */
235 status = MLX_GET ( &hcr, status );
237 DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
240 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
241 &hcr, sizeof ( hcr ) );
245 /* Read output parameters, if any */
246 hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
247 hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
248 memcpy ( out, out_buffer, out_len );
250 DBGC2 ( hermon, "Output%s:\n",
251 ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
252 DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
253 ( ( out_len < 512 ) ? out_len : 512 ) );
260 hermon_cmd_query_dev_cap ( struct hermon *hermon,
261 struct hermonprm_query_dev_cap *dev_cap ) {
262 return hermon_cmd ( hermon,
263 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
264 1, sizeof ( *dev_cap ) ),
265 0, NULL, 0, dev_cap );
269 hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
270 return hermon_cmd ( hermon,
271 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
277 hermon_cmd_init_hca ( struct hermon *hermon,
278 const struct hermonprm_init_hca *init_hca ) {
279 return hermon_cmd ( hermon,
280 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
281 1, sizeof ( *init_hca ) ),
282 0, init_hca, 0, NULL );
286 hermon_cmd_close_hca ( struct hermon *hermon ) {
287 return hermon_cmd ( hermon,
288 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
293 hermon_cmd_init_port ( struct hermon *hermon, unsigned int port,
294 const struct hermonprm_init_port *init_port ) {
295 return hermon_cmd ( hermon,
296 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_PORT,
297 1, sizeof ( *init_port ) ),
298 0, init_port, port, NULL );
302 hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
303 return hermon_cmd ( hermon,
304 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
305 0, NULL, port, NULL );
309 hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
310 const struct hermonprm_mpt *mpt ) {
311 return hermon_cmd ( hermon,
312 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
313 1, sizeof ( *mpt ) ),
314 0, mpt, index, NULL );
318 hermon_cmd_write_mtt ( struct hermon *hermon,
319 const struct hermonprm_write_mtt *write_mtt ) {
320 return hermon_cmd ( hermon,
321 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
322 1, sizeof ( *write_mtt ) ),
323 0, write_mtt, 1, NULL );
327 hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
328 const struct hermonprm_eqc *eqc ) {
329 return hermon_cmd ( hermon,
330 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
331 1, sizeof ( *eqc ) ),
332 0, eqc, index, NULL );
336 hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index ) {
337 return hermon_cmd ( hermon,
338 HERMON_HCR_VOID_CMD ( HERMON_HCR_HW2SW_EQ ),
339 1, NULL, index, NULL );
343 hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
344 const struct hermonprm_completion_queue_context *cqctx ){
345 return hermon_cmd ( hermon,
346 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
347 1, sizeof ( *cqctx ) ),
348 0, cqctx, cqn, NULL );
352 hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
353 struct hermonprm_completion_queue_context *cqctx) {
354 return hermon_cmd ( hermon,
355 HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
356 1, sizeof ( *cqctx ) ),
357 0, NULL, cqn, cqctx );
361 hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
362 const struct hermonprm_qp_ee_state_transitions *ctx ){
363 return hermon_cmd ( hermon,
364 HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
365 1, sizeof ( *ctx ) ),
370 hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
371 const struct hermonprm_qp_ee_state_transitions *ctx ){
372 return hermon_cmd ( hermon,
373 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
374 1, sizeof ( *ctx ) ),
379 hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
380 const struct hermonprm_qp_ee_state_transitions *ctx ) {
381 return hermon_cmd ( hermon,
382 HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
383 1, sizeof ( *ctx ) ),
388 hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
389 return hermon_cmd ( hermon,
390 HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
391 0x03, NULL, qpn, NULL );
395 hermon_cmd_mad_ifc ( struct hermon *hermon, union hermonprm_mad *mad ) {
396 return hermon_cmd ( hermon,
397 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
399 1, sizeof ( *mad ) ),
400 0x03, mad, PXE_IB_PORT, mad );
404 hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
405 struct hermonprm_mcg_entry *mcg ) {
406 return hermon_cmd ( hermon,
407 HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
408 1, sizeof ( *mcg ) ),
409 0, NULL, index, mcg );
413 hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
414 const struct hermonprm_mcg_entry *mcg ) {
415 return hermon_cmd ( hermon,
416 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
417 1, sizeof ( *mcg ) ),
418 0, mcg, index, NULL );
422 hermon_cmd_mgid_hash ( struct hermon *hermon, const struct ib_gid *gid,
423 struct hermonprm_mgm_hash *hash ) {
424 return hermon_cmd ( hermon,
425 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
427 0, sizeof ( *hash ) ),
432 hermon_cmd_run_fw ( struct hermon *hermon ) {
433 return hermon_cmd ( hermon,
434 HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
439 hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
440 const struct hermonprm_scalar_parameter *offset ) {
441 return hermon_cmd ( hermon,
442 HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
443 0, sizeof ( *offset ) ),
444 0, offset, page_count, NULL );
448 hermon_cmd_map_icm ( struct hermon *hermon,
449 const struct hermonprm_virtual_physical_mapping *map ) {
450 return hermon_cmd ( hermon,
451 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
452 1, sizeof ( *map ) ),
457 hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
458 return hermon_cmd ( hermon,
459 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
464 hermon_cmd_map_icm_aux ( struct hermon *hermon,
465 const struct hermonprm_virtual_physical_mapping *map ) {
466 return hermon_cmd ( hermon,
467 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
468 1, sizeof ( *map ) ),
473 hermon_cmd_set_icm_size ( struct hermon *hermon,
474 const struct hermonprm_scalar_parameter *icm_size,
475 struct hermonprm_scalar_parameter *icm_aux_size ) {
476 return hermon_cmd ( hermon,
477 HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
478 0, sizeof ( *icm_size ),
479 0, sizeof (*icm_aux_size) ),
480 0, icm_size, 0, icm_aux_size );
484 hermon_cmd_unmap_fa ( struct hermon *hermon ) {
485 return hermon_cmd ( hermon,
486 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
491 hermon_cmd_map_fa ( struct hermon *hermon,
492 const struct hermonprm_virtual_physical_mapping *map ) {
493 return hermon_cmd ( hermon,
494 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
495 1, sizeof ( *map ) ),
499 /***************************************************************************
501 * Memory translation table operations
503 ***************************************************************************
507 * Allocate MTT entries
509 * @v hermon Hermon device
510 * @v memory Memory to map into MTT
511 * @v len Length of memory to map
512 * @v mtt MTT descriptor to fill in
513 * @ret rc Return status code
515 static int hermon_alloc_mtt ( struct hermon *hermon,
516 const void *memory, size_t len,
517 struct hermon_mtt *mtt ) {
518 struct hermonprm_write_mtt write_mtt;
520 unsigned int page_offset;
521 unsigned int num_pages;
523 unsigned int mtt_base_addr;
527 /* Find available MTT entries */
528 start = virt_to_phys ( memory );
529 page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
530 start -= page_offset;
532 num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
533 mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
535 if ( mtt_offset < 0 ) {
536 DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
541 mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
542 hermon->cap.mtt_entry_size );
544 /* Fill in MTT structure */
545 mtt->mtt_offset = mtt_offset;
546 mtt->num_pages = num_pages;
547 mtt->mtt_base_addr = mtt_base_addr;
548 mtt->page_offset = page_offset;
550 /* Construct and issue WRITE_MTT commands */
551 for ( i = 0 ; i < num_pages ; i++ ) {
552 memset ( &write_mtt, 0, sizeof ( write_mtt ) );
553 MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
554 value, mtt_base_addr );
555 MLX_FILL_2 ( &write_mtt.mtt, 1,
557 ptag_l, ( start >> 3 ) );
558 if ( ( rc = hermon_cmd_write_mtt ( hermon,
559 &write_mtt ) ) != 0 ) {
560 DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
561 hermon, mtt_base_addr );
564 start += HERMON_PAGE_SIZE;
565 mtt_base_addr += hermon->cap.mtt_entry_size;
571 hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
579 * @v hermon Hermon device
580 * @v mtt MTT descriptor
582 static void hermon_free_mtt ( struct hermon *hermon,
583 struct hermon_mtt *mtt ) {
584 hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
588 /***************************************************************************
590 * Completion queue operations
592 ***************************************************************************
596 * Create completion queue
598 * @v ibdev Infiniband device
599 * @v cq Completion queue
600 * @ret rc Return status code
602 static int hermon_create_cq ( struct ib_device *ibdev,
603 struct ib_completion_queue *cq ) {
604 struct hermon *hermon = ibdev->dev_priv;
605 struct hermon_completion_queue *hermon_cq;
606 struct hermonprm_completion_queue_context cqctx;
611 /* Find a free completion queue number */
612 cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
614 if ( cqn_offset < 0 ) {
615 DBGC ( hermon, "Hermon %p out of completion queues\n",
620 cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
622 /* Allocate control structures */
623 hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
629 /* Allocate completion queue itself */
630 hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
631 hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
632 sizeof ( hermon_cq->cqe[0] ) );
633 if ( ! hermon_cq->cqe ) {
637 memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
638 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
639 MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
643 /* Allocate MTT entries */
644 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
646 &hermon_cq->mtt ) ) != 0 )
649 /* Hand queue over to hardware */
650 memset ( &cqctx, 0, sizeof ( cqctx ) );
651 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
652 MLX_FILL_1 ( &cqctx, 2,
653 page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
654 MLX_FILL_2 ( &cqctx, 3,
655 usr_page, HERMON_UAR_PAGE,
656 log_cq_size, fls ( cq->num_cqes - 1 ) );
657 MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
658 ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
659 MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
660 ( virt_to_phys ( &hermon_cq->doorbell ) >> 3 ) );
661 if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
662 DBGC ( hermon, "Hermon %p SW2HW_CQ failed: %s\n",
663 hermon, strerror ( rc ) );
667 DBGC ( hermon, "Hermon %p CQN %#lx ring at [%p,%p)\n",
668 hermon, cq->cqn, hermon_cq->cqe,
669 ( ( ( void * ) hermon_cq->cqe ) + hermon_cq->cqe_size ) );
670 cq->dev_priv = hermon_cq;
674 hermon_free_mtt ( hermon, &hermon_cq->mtt );
676 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
680 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
686 * Destroy completion queue
688 * @v ibdev Infiniband device
689 * @v cq Completion queue
691 static void hermon_destroy_cq ( struct ib_device *ibdev,
692 struct ib_completion_queue *cq ) {
693 struct hermon *hermon = ibdev->dev_priv;
694 struct hermon_completion_queue *hermon_cq = cq->dev_priv;
695 struct hermonprm_completion_queue_context cqctx;
699 /* Take ownership back from hardware */
700 if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
701 DBGC ( hermon, "Hermon %p FATAL HW2SW_CQ failed on CQN %#lx: "
702 "%s\n", hermon, cq->cqn, strerror ( rc ) );
703 /* Leak memory and return; at least we avoid corruption */
707 /* Free MTT entries */
708 hermon_free_mtt ( hermon, &hermon_cq->mtt );
711 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
714 /* Mark queue number as free */
715 cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
716 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
721 /***************************************************************************
723 * Queue pair operations
725 ***************************************************************************
731 * @v ibdev Infiniband device
733 * @ret rc Return status code
735 static int hermon_create_qp ( struct ib_device *ibdev,
736 struct ib_queue_pair *qp ) {
737 struct hermon *hermon = ibdev->dev_priv;
738 struct hermon_queue_pair *hermon_qp;
739 struct hermonprm_qp_ee_state_transitions qpctx;
743 /* Find a free queue pair number */
744 qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
746 if ( qpn_offset < 0 ) {
747 DBGC ( hermon, "Hermon %p out of queue pairs\n", hermon );
751 qp->qpn = ( HERMON_QPN_BASE + hermon->cap.reserved_qps +
754 /* Allocate control structures */
755 hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
761 /* Allocate work queue buffer */
762 hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
763 ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
764 hermon_qp->send.num_wqes =
765 ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
766 hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
767 sizeof ( hermon_qp->send.wqe[0] ) );
768 hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
769 sizeof ( hermon_qp->recv.wqe[0] ) );
770 hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
771 hermon_qp->recv.wqe_size );
772 hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
773 sizeof ( hermon_qp->send.wqe[0] ) );
774 if ( ! hermon_qp->wqe ) {
778 hermon_qp->send.wqe = hermon_qp->wqe;
779 memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
780 hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
781 memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
783 /* Allocate MTT entries */
784 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
786 &hermon_qp->mtt ) ) != 0 ) {
790 /* Hand queue over to hardware */
791 memset ( &qpctx, 0, sizeof ( qpctx ) );
792 MLX_FILL_2 ( &qpctx, 2,
793 qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
794 qpc_eec_data.st, HERMON_ST_UD );
795 MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
796 MLX_FILL_4 ( &qpctx, 4,
797 qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
798 qpc_eec_data.log_rq_stride,
799 ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
800 qpc_eec_data.log_sq_size,
801 fls ( hermon_qp->send.num_wqes - 1 ),
802 qpc_eec_data.log_sq_stride,
803 ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
804 MLX_FILL_1 ( &qpctx, 5,
805 qpc_eec_data.usr_page, HERMON_UAR_PAGE );
806 MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
807 MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.page_offset,
808 ( hermon_qp->mtt.page_offset >> 6 ) );
809 MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
810 MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
811 ( virt_to_phys ( &hermon_qp->recv.doorbell ) >> 2 ) );
812 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
813 MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
814 ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
815 if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
817 DBGC ( hermon, "Hermon %p RST2INIT_QP failed: %s\n",
818 hermon, strerror ( rc ) );
819 goto err_rst2init_qp;
822 memset ( &qpctx, 0, sizeof ( qpctx ) );
823 MLX_FILL_2 ( &qpctx, 4,
824 qpc_eec_data.mtu, HERMON_MTU_2048,
825 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
826 MLX_FILL_1 ( &qpctx, 16,
827 qpc_eec_data.primary_address_path.sched_queue,
828 ( 0x83 /* default policy */ |
829 ( ( PXE_IB_PORT - 1 ) << 6 ) ) );
830 if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
832 DBGC ( hermon, "Hermon %p INIT2RTR_QP failed: %s\n",
833 hermon, strerror ( rc ) );
834 goto err_init2rtr_qp;
836 memset ( &qpctx, 0, sizeof ( qpctx ) );
837 if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
838 DBGC ( hermon, "Hermon %p RTR2RTS_QP failed: %s\n",
839 hermon, strerror ( rc ) );
843 DBGC ( hermon, "Hermon %p QPN %#lx send ring at [%p,%p)\n",
844 hermon, qp->qpn, hermon_qp->send.wqe,
845 ( ((void *)hermon_qp->send.wqe ) + hermon_qp->send.wqe_size ) );
846 DBGC ( hermon, "Hermon %p QPN %#lx receive ring at [%p,%p)\n",
847 hermon, qp->qpn, hermon_qp->recv.wqe,
848 ( ((void *)hermon_qp->recv.wqe ) + hermon_qp->recv.wqe_size ) );
849 qp->dev_priv = hermon_qp;
854 hermon_cmd_2rst_qp ( hermon, qp->qpn );
856 hermon_free_mtt ( hermon, &hermon_qp->mtt );
858 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
862 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
870 * @v ibdev Infiniband device
873 static void hermon_destroy_qp ( struct ib_device *ibdev,
874 struct ib_queue_pair *qp ) {
875 struct hermon *hermon = ibdev->dev_priv;
876 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
880 /* Take ownership back from hardware */
881 if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
882 DBGC ( hermon, "Hermon %p FATAL 2RST_QP failed on QPN %#lx: "
883 "%s\n", hermon, qp->qpn, strerror ( rc ) );
884 /* Leak memory and return; at least we avoid corruption */
888 /* Free MTT entries */
889 hermon_free_mtt ( hermon, &hermon_qp->mtt );
892 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
895 /* Mark queue number as free */
896 qpn_offset = ( qp->qpn - HERMON_QPN_BASE -
897 hermon->cap.reserved_qps );
898 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
903 /***************************************************************************
905 * Work request operations
907 ***************************************************************************
910 /** GID used for GID-less send work queue entries */
911 static const struct ib_gid hermon_no_gid = {
912 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
916 * Post send work queue entry
918 * @v ibdev Infiniband device
920 * @v av Address vector
921 * @v iobuf I/O buffer
922 * @ret rc Return status code
924 static int hermon_post_send ( struct ib_device *ibdev,
925 struct ib_queue_pair *qp,
926 struct ib_address_vector *av,
927 struct io_buffer *iobuf ) {
928 struct hermon *hermon = ibdev->dev_priv;
929 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
930 struct ib_work_queue *wq = &qp->send;
931 struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
932 struct hermonprm_ud_send_wqe *wqe;
933 const struct ib_gid *gid;
934 union hermonprm_doorbell_register db_reg;
935 unsigned int wqe_idx_mask;
937 /* Allocate work queue entry */
938 wqe_idx_mask = ( wq->num_wqes - 1 );
939 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
940 DBGC ( hermon, "Hermon %p send queue full", hermon );
943 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
944 wqe = &hermon_send_wq->wqe[ wq->next_idx &
945 ( hermon_send_wq->num_wqes - 1 ) ].ud;
947 /* Construct work queue entry */
948 memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
949 ( sizeof ( *wqe ) - 4 ) );
950 MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( *wqe ) / 16 ) );
951 MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
952 MLX_FILL_2 ( &wqe->ud, 0,
953 ud_address_vector.pd, HERMON_GLOBAL_PD,
954 ud_address_vector.port_number, PXE_IB_PORT );
955 MLX_FILL_2 ( &wqe->ud, 1,
956 ud_address_vector.rlid, av->dlid,
957 ud_address_vector.g, av->gid_present );
958 MLX_FILL_1 ( &wqe->ud, 2,
959 ud_address_vector.max_stat_rate,
960 ( ( ( av->rate < 2 ) || ( av->rate > 10 ) ) ?
961 8 : ( av->rate + 5 ) ) );
962 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
963 gid = ( av->gid_present ? &av->gid : &hermon_no_gid );
964 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
965 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
966 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
967 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
968 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
969 MLX_FILL_1 ( &wqe->data[0], 3,
970 local_address_l, virt_to_bus ( iobuf->data ) );
972 MLX_FILL_2 ( &wqe->ctrl, 0,
973 opcode, HERMON_OPCODE_SEND,
975 ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 ) );
976 DBGCP ( hermon, "Hermon %p posting send WQE:\n", hermon );
977 DBGCP_HD ( hermon, wqe, sizeof ( *wqe ) );
980 /* Ring doorbell register */
981 MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
982 DBGCP ( hermon, "Ringing doorbell %08lx with %08lx\n",
983 virt_to_phys ( hermon->uar + HERMON_DB_POST_SND_OFFSET ),
985 writel ( db_reg.dword[0], ( hermon->uar + HERMON_DB_POST_SND_OFFSET ));
987 /* Update work queue's index */
994 * Post receive work queue entry
996 * @v ibdev Infiniband device
998 * @v iobuf I/O buffer
999 * @ret rc Return status code
1001 static int hermon_post_recv ( struct ib_device *ibdev,
1002 struct ib_queue_pair *qp,
1003 struct io_buffer *iobuf ) {
1004 struct hermon *hermon = ibdev->dev_priv;
1005 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
1006 struct ib_work_queue *wq = &qp->recv;
1007 struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
1008 struct hermonprm_recv_wqe *wqe;
1009 unsigned int wqe_idx_mask;
1011 /* Allocate work queue entry */
1012 wqe_idx_mask = ( wq->num_wqes - 1 );
1013 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1014 DBGC ( hermon, "Hermon %p receive queue full", hermon );
1017 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1018 wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
1020 /* Construct work queue entry */
1021 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
1022 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
1023 MLX_FILL_1 ( &wqe->data[0], 3,
1024 local_address_l, virt_to_bus ( iobuf->data ) );
1026 /* Update work queue's index */
1029 /* Update doorbell record */
1031 MLX_FILL_1 ( &hermon_recv_wq->doorbell, 0, receive_wqe_counter,
1032 ( wq->next_idx & 0xffff ) );
1040 * @v ibdev Infiniband device
1041 * @v cq Completion queue
1042 * @v cqe Hardware completion queue entry
1043 * @v complete_send Send completion handler
1044 * @v complete_recv Receive completion handler
1045 * @ret rc Return status code
1047 static int hermon_complete ( struct ib_device *ibdev,
1048 struct ib_completion_queue *cq,
1049 union hermonprm_completion_entry *cqe,
1050 ib_completer_t complete_send,
1051 ib_completer_t complete_recv ) {
1052 struct hermon *hermon = ibdev->dev_priv;
1053 struct ib_completion completion;
1054 struct ib_work_queue *wq;
1055 struct ib_queue_pair *qp;
1056 struct hermon_queue_pair *hermon_qp;
1057 struct io_buffer *iobuf;
1058 ib_completer_t complete;
1059 unsigned int opcode;
1062 unsigned int wqe_idx;
1065 /* Parse completion */
1066 memset ( &completion, 0, sizeof ( completion ) );
1067 qpn = MLX_GET ( &cqe->normal, qpn );
1068 is_send = MLX_GET ( &cqe->normal, s_r );
1069 opcode = MLX_GET ( &cqe->normal, opcode );
1070 if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
1071 /* "s" field is not valid for error opcodes */
1072 is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
1073 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
1074 DBGC ( hermon, "Hermon %p CQN %lx syndrome %x vendor %lx\n",
1075 hermon, cq->cqn, completion.syndrome,
1076 MLX_GET ( &cqe->error, vendor_error_syndrome ) );
1078 /* Don't return immediately; propagate error to completer */
1081 /* Identify work queue */
1082 wq = ib_find_wq ( cq, qpn, is_send );
1084 DBGC ( hermon, "Hermon %p CQN %lx unknown %s QPN %lx\n",
1085 hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1089 hermon_qp = qp->dev_priv;
1091 /* Identify I/O buffer */
1092 wqe_idx = ( MLX_GET ( &cqe->normal, wqe_counter ) &
1093 ( wq->num_wqes - 1 ) );
1094 iobuf = wq->iobufs[wqe_idx];
1096 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx empty WQE %x\n",
1097 hermon, cq->cqn, qpn, wqe_idx );
1100 wq->iobufs[wqe_idx] = NULL;
1102 /* Fill in length for received packets */
1104 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
1105 if ( completion.len > iob_tailroom ( iobuf ) ) {
1106 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx IDX %x "
1107 "overlength received packet length %zd\n",
1108 hermon, cq->cqn, qpn, wqe_idx, completion.len );
1113 /* Pass off to caller's completion handler */
1114 complete = ( is_send ? complete_send : complete_recv );
1115 complete ( ibdev, qp, &completion, iobuf );
1121 * Poll completion queue
1123 * @v ibdev Infiniband device
1124 * @v cq Completion queue
1125 * @v complete_send Send completion handler
1126 * @v complete_recv Receive completion handler
1128 static void hermon_poll_cq ( struct ib_device *ibdev,
1129 struct ib_completion_queue *cq,
1130 ib_completer_t complete_send,
1131 ib_completer_t complete_recv ) {
1132 struct hermon *hermon = ibdev->dev_priv;
1133 struct hermon_completion_queue *hermon_cq = cq->dev_priv;
1134 union hermonprm_completion_entry *cqe;
1135 unsigned int cqe_idx_mask;
1139 /* Look for completion entry */
1140 cqe_idx_mask = ( cq->num_cqes - 1 );
1141 cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
1142 if ( MLX_GET ( &cqe->normal, owner ) ^
1143 ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
1144 /* Entry still owned by hardware; end of poll */
1147 DBGCP ( hermon, "Hermon %p completion:\n", hermon );
1148 DBGCP_HD ( hermon, cqe, sizeof ( *cqe ) );
1150 /* Handle completion */
1151 if ( ( rc = hermon_complete ( ibdev, cq, cqe, complete_send,
1152 complete_recv ) ) != 0 ) {
1153 DBGC ( hermon, "Hermon %p failed to complete: %s\n",
1154 hermon, strerror ( rc ) );
1155 DBGC_HD ( hermon, cqe, sizeof ( *cqe ) );
1158 /* Update completion queue's index */
1161 /* Update doorbell record */
1162 MLX_FILL_1 ( &hermon_cq->doorbell, 0, update_ci,
1163 ( cq->next_idx & 0xffffffUL ) );
1167 /***************************************************************************
1169 * Multicast group operations
1171 ***************************************************************************
1175 * Attach to multicast group
1177 * @v ibdev Infiniband device
1179 * @v gid Multicast GID
1180 * @ret rc Return status code
1182 static int hermon_mcast_attach ( struct ib_device *ibdev,
1183 struct ib_queue_pair *qp,
1184 struct ib_gid *gid ) {
1185 struct hermon *hermon = ibdev->dev_priv;
1186 struct hermonprm_mgm_hash hash;
1187 struct hermonprm_mcg_entry mcg;
1191 /* Generate hash table index */
1192 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1193 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1194 hermon, strerror ( rc ) );
1197 index = MLX_GET ( &hash, hash );
1199 /* Check for existing hash table entry */
1200 if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
1201 DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
1202 hermon, index, strerror ( rc ) );
1205 if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
1206 /* FIXME: this implementation allows only a single QP
1207 * per multicast group, and doesn't handle hash
1208 * collisions. Sufficient for IPoIB but may need to
1209 * be extended in future.
1211 DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
1216 /* Update hash table entry */
1217 MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
1218 MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
1219 memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
1220 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1221 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1222 hermon, index, strerror ( rc ) );
1230 * Detach from multicast group
1232 * @v ibdev Infiniband device
1234 * @v gid Multicast GID
1236 static void hermon_mcast_detach ( struct ib_device *ibdev,
1237 struct ib_queue_pair *qp __unused,
1238 struct ib_gid *gid ) {
1239 struct hermon *hermon = ibdev->dev_priv;
1240 struct hermonprm_mgm_hash hash;
1241 struct hermonprm_mcg_entry mcg;
1245 /* Generate hash table index */
1246 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1247 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1248 hermon, strerror ( rc ) );
1251 index = MLX_GET ( &hash, hash );
1253 /* Clear hash table entry */
1254 memset ( &mcg, 0, sizeof ( mcg ) );
1255 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1256 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1257 hermon, index, strerror ( rc ) );
1262 /** Hermon Infiniband operations */
1263 static struct ib_device_operations hermon_ib_operations = {
1264 .create_cq = hermon_create_cq,
1265 .destroy_cq = hermon_destroy_cq,
1266 .create_qp = hermon_create_qp,
1267 .destroy_qp = hermon_destroy_qp,
1268 .post_send = hermon_post_send,
1269 .post_recv = hermon_post_recv,
1270 .poll_cq = hermon_poll_cq,
1271 .mcast_attach = hermon_mcast_attach,
1272 .mcast_detach = hermon_mcast_detach,
1275 /***************************************************************************
1277 * MAD IFC operations
1279 ***************************************************************************
1282 static int hermon_mad_ifc ( struct hermon *hermon,
1283 union hermonprm_mad *mad ) {
1284 struct ib_mad_hdr *hdr = &mad->mad.mad_hdr;
1287 hdr->base_version = IB_MGMT_BASE_VERSION;
1288 if ( ( rc = hermon_cmd_mad_ifc ( hermon, mad ) ) != 0 ) {
1289 DBGC ( hermon, "Hermon %p could not issue MAD IFC: %s\n",
1290 hermon, strerror ( rc ) );
1293 if ( hdr->status != 0 ) {
1294 DBGC ( hermon, "Hermon %p MAD IFC status %04x\n",
1295 hermon, ntohs ( hdr->status ) );
1301 static int hermon_get_port_info ( struct hermon *hermon,
1302 struct ib_mad_port_info *port_info ) {
1303 union hermonprm_mad mad;
1304 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1307 memset ( &mad, 0, sizeof ( mad ) );
1308 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1309 hdr->class_version = 1;
1310 hdr->method = IB_MGMT_METHOD_GET;
1311 hdr->attr_id = htons ( IB_SMP_ATTR_PORT_INFO );
1312 hdr->attr_mod = htonl ( PXE_IB_PORT );
1313 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1314 DBGC ( hermon, "Hermon %p could not get port info: %s\n",
1315 hermon, strerror ( rc ) );
1318 memcpy ( port_info, &mad.mad.port_info, sizeof ( *port_info ) );
1322 static int hermon_get_guid_info ( struct hermon *hermon,
1323 struct ib_mad_guid_info *guid_info ) {
1324 union hermonprm_mad mad;
1325 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1328 memset ( &mad, 0, sizeof ( mad ) );
1329 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1330 hdr->class_version = 1;
1331 hdr->method = IB_MGMT_METHOD_GET;
1332 hdr->attr_id = htons ( IB_SMP_ATTR_GUID_INFO );
1333 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1334 DBGC ( hermon, "Hermon %p could not get GUID info: %s\n",
1335 hermon, strerror ( rc ) );
1338 memcpy ( guid_info, &mad.mad.guid_info, sizeof ( *guid_info ) );
1342 static int hermon_get_pkey_table ( struct hermon *hermon,
1343 struct ib_mad_pkey_table *pkey_table ) {
1344 union hermonprm_mad mad;
1345 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1348 memset ( &mad, 0, sizeof ( mad ) );
1349 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1350 hdr->class_version = 1;
1351 hdr->method = IB_MGMT_METHOD_GET;
1352 hdr->attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE );
1353 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1354 DBGC ( hermon, "Hermon %p could not get pkey table: %s\n",
1355 hermon, strerror ( rc ) );
1358 memcpy ( pkey_table, &mad.mad.pkey_table, sizeof ( *pkey_table ) );
1362 static int hermon_get_port_gid ( struct hermon *hermon,
1363 struct ib_gid *port_gid ) {
1365 /* This union exists just to save stack space */
1366 struct ib_mad_port_info port_info;
1367 struct ib_mad_guid_info guid_info;
1371 /* Port info gives us the first half of the port GID */
1372 if ( ( rc = hermon_get_port_info ( hermon, &u.port_info ) ) != 0 )
1374 memcpy ( &port_gid->u.bytes[0], u.port_info.gid_prefix, 8 );
1376 /* GUID info gives us the second half of the port GID */
1377 if ( ( rc = hermon_get_guid_info ( hermon, &u.guid_info ) ) != 0 )
1379 memcpy ( &port_gid->u.bytes[8], u.guid_info.gid_local, 8 );
1384 static int hermon_get_sm_lid ( struct hermon *hermon,
1385 unsigned long *sm_lid ) {
1386 struct ib_mad_port_info port_info;
1389 if ( ( rc = hermon_get_port_info ( hermon, &port_info ) ) != 0 )
1391 *sm_lid = ntohs ( port_info.mastersm_lid );
1395 static int hermon_get_pkey ( struct hermon *hermon, unsigned int *pkey ) {
1396 struct ib_mad_pkey_table pkey_table;
1399 if ( ( rc = hermon_get_pkey_table ( hermon, &pkey_table ) ) != 0 )
1401 *pkey = ntohs ( pkey_table.pkey[0][0] );
1408 * @v hermon Hermon device
1409 * @ret rc Return status code
1411 * This function shouldn't really exist. Unfortunately, IB links take
1412 * a long time to come up, and we can't get various key parameters
1413 * e.g. our own IPoIB MAC address without information from the subnet
1414 * manager). We should eventually make link-up an asynchronous event.
1416 static int hermon_wait_for_link ( struct hermon *hermon ) {
1417 struct ib_mad_port_info port_info;
1418 unsigned int retries;
1421 printf ( "Waiting for Infiniband link-up..." );
1422 for ( retries = 20 ; retries ; retries-- ) {
1423 if ( ( rc = hermon_get_port_info ( hermon,
1424 &port_info ) ) != 0 )
1426 if ( ( ( port_info.port_state__link_speed_supported ) & 0xf )
1434 printf ( "failed\n" );
1439 * Get MAD parameters
1441 * @v hermon Hermon device
1442 * @ret rc Return status code
1444 static int hermon_get_mad_params ( struct ib_device *ibdev ) {
1445 struct hermon *hermon = ibdev->dev_priv;
1448 /* Get subnet manager LID */
1449 if ( ( rc = hermon_get_sm_lid ( hermon, &ibdev->sm_lid ) ) != 0 ) {
1450 DBGC ( hermon, "Hermon %p could not determine subnet manager "
1451 "LID: %s\n", hermon, strerror ( rc ) );
1456 if ( ( rc = hermon_get_port_gid ( hermon, &ibdev->port_gid ) ) != 0 ) {
1457 DBGC ( hermon, "Hermon %p could not determine port GID: %s\n",
1458 hermon, strerror ( rc ) );
1462 /* Get partition key */
1463 if ( ( rc = hermon_get_pkey ( hermon, &ibdev->pkey ) ) != 0 ) {
1464 DBGC ( hermon, "Hermon %p could not determine partition key: "
1465 "%s\n", hermon, strerror ( rc ) );
1472 /***************************************************************************
1476 ***************************************************************************
1480 * Start firmware running
1482 * @v hermon Hermon device
1483 * @ret rc Return status code
1485 static int hermon_start_firmware ( struct hermon *hermon ) {
1486 struct hermonprm_query_fw fw;
1487 struct hermonprm_virtual_physical_mapping map_fa;
1488 unsigned int fw_pages;
1489 unsigned int log2_fw_pages;
1494 /* Get firmware parameters */
1495 if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
1496 DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
1497 hermon, strerror ( rc ) );
1500 DBGC ( hermon, "Hermon %p firmware version %ld.%ld.%ld\n", hermon,
1501 MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
1502 MLX_GET ( &fw, fw_rev_subminor ) );
1503 fw_pages = MLX_GET ( &fw, fw_pages );
1504 log2_fw_pages = fls ( fw_pages - 1 );
1505 fw_pages = ( 1 << log2_fw_pages );
1506 DBGC ( hermon, "Hermon %p requires %d kB for firmware\n",
1507 hermon, ( fw_pages * 4 ) );
1509 /* Allocate firmware pages and map firmware area */
1510 fw_size = ( fw_pages * HERMON_PAGE_SIZE );
1511 hermon->firmware_area = umalloc ( fw_size );
1512 if ( ! hermon->firmware_area ) {
1516 fw_base = ( user_to_phys ( hermon->firmware_area, fw_size ) &
1518 DBGC ( hermon, "Hermon %p firmware area at physical [%lx,%lx)\n",
1519 hermon, fw_base, ( fw_base + fw_size ) );
1520 memset ( &map_fa, 0, sizeof ( map_fa ) );
1521 MLX_FILL_2 ( &map_fa, 3,
1522 log2size, log2_fw_pages,
1523 pa_l, ( fw_base >> 12 ) );
1524 if ( ( rc = hermon_cmd_map_fa ( hermon, &map_fa ) ) != 0 ) {
1525 DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
1526 hermon, strerror ( rc ) );
1530 /* Start firmware */
1531 if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
1532 DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
1533 hermon, strerror ( rc ) );
1537 DBGC ( hermon, "Hermon %p firmware started\n", hermon );
1541 hermon_cmd_unmap_fa ( hermon );
1543 ufree ( hermon->firmware_area );
1544 hermon->firmware_area = UNULL;
1551 * Stop firmware running
1553 * @v hermon Hermon device
1555 static void hermon_stop_firmware ( struct hermon *hermon ) {
1558 if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
1559 DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
1560 hermon, strerror ( rc ) );
1561 /* Leak memory and return; at least we avoid corruption */
1564 ufree ( hermon->firmware_area );
1565 hermon->firmware_area = UNULL;
1568 /***************************************************************************
1570 * Infinihost Context Memory management
1572 ***************************************************************************
1578 * @v hermon Hermon device
1579 * @ret rc Return status code
1581 static int hermon_get_cap ( struct hermon *hermon ) {
1582 struct hermonprm_query_dev_cap dev_cap;
1585 if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
1586 DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
1587 hermon, strerror ( rc ) );
1591 hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
1592 hermon->cap.reserved_qps =
1593 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
1594 hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
1595 hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
1596 hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
1597 hermon->cap.reserved_srqs =
1598 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
1599 hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
1600 hermon->cap.reserved_cqs =
1601 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
1602 hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
1603 hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
1604 hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
1605 hermon->cap.reserved_mtts =
1606 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
1607 hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
1608 hermon->cap.reserved_mrws =
1609 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
1610 hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
1611 hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
1619 * @v log_num_entries Log2 of the number of entries
1620 * @v entry_size Entry size
1621 * @ret usage Usage size in ICM
1623 static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
1626 usage = ( ( 1 << log_num_entries ) * entry_size );
1627 usage = ( ( usage + HERMON_PAGE_SIZE - 1 ) &
1628 ~( HERMON_PAGE_SIZE - 1 ) );
1635 * @v hermon Hermon device
1636 * @v init_hca INIT_HCA structure to fill in
1637 * @ret rc Return status code
1639 static int hermon_alloc_icm ( struct hermon *hermon,
1640 struct hermonprm_init_hca *init_hca ) {
1641 struct hermonprm_scalar_parameter icm_size;
1642 struct hermonprm_scalar_parameter icm_aux_size;
1643 struct hermonprm_virtual_physical_mapping map_icm_aux;
1644 struct hermonprm_virtual_physical_mapping map_icm;
1645 uint64_t icm_offset = 0;
1646 unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
1647 unsigned int log_num_mtts, log_num_mpts;
1648 size_t cmpt_max_len;
1649 size_t qp_cmpt_len, srq_cmpt_len, cq_cmpt_len, eq_cmpt_len;
1650 size_t icm_len, icm_aux_len;
1651 physaddr_t icm_phys;
1656 * Start by carving up the ICM virtual address space
1660 /* Calculate number of each object type within ICM */
1661 log_num_qps = fls ( hermon->cap.reserved_qps + HERMON_MAX_QPS - 1 );
1662 log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
1663 log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
1664 log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
1665 log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
1667 /* ICM starts with the cMPT tables, which are sparse */
1668 cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
1669 ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
1670 qp_cmpt_len = icm_usage ( log_num_qps, hermon->cap.cmpt_entry_size );
1671 hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
1672 hermon->icm_map[HERMON_ICM_QP_CMPT].len = qp_cmpt_len;
1673 icm_offset += cmpt_max_len;
1674 srq_cmpt_len = icm_usage ( log_num_srqs, hermon->cap.cmpt_entry_size );
1675 hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
1676 hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = srq_cmpt_len;
1677 icm_offset += cmpt_max_len;
1678 cq_cmpt_len = icm_usage ( log_num_cqs, hermon->cap.cmpt_entry_size );
1679 hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
1680 hermon->icm_map[HERMON_ICM_CQ_CMPT].len = cq_cmpt_len;
1681 icm_offset += cmpt_max_len;
1682 eq_cmpt_len = icm_usage ( log_num_eqs, hermon->cap.cmpt_entry_size );
1683 hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
1684 hermon->icm_map[HERMON_ICM_EQ_CMPT].len = eq_cmpt_len;
1685 icm_offset += cmpt_max_len;
1687 hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
1689 /* Queue pair contexts */
1690 MLX_FILL_1 ( init_hca, 12,
1691 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
1692 ( icm_offset >> 32 ) );
1693 MLX_FILL_2 ( init_hca, 13,
1694 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
1695 ( icm_offset >> 5 ),
1696 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
1698 DBGC ( hermon, "Hermon %p ICM QPC base = %llx\n", hermon, icm_offset );
1699 icm_offset += icm_usage ( log_num_qps, hermon->cap.qpc_entry_size );
1701 /* Extended alternate path contexts */
1702 MLX_FILL_1 ( init_hca, 24,
1703 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
1704 ( icm_offset >> 32 ) );
1705 MLX_FILL_1 ( init_hca, 25,
1706 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
1708 DBGC ( hermon, "Hermon %p ICM ALTC base = %llx\n", hermon, icm_offset);
1709 icm_offset += icm_usage ( log_num_qps,
1710 hermon->cap.altc_entry_size );
1712 /* Extended auxiliary contexts */
1713 MLX_FILL_1 ( init_hca, 28,
1714 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
1715 ( icm_offset >> 32 ) );
1716 MLX_FILL_1 ( init_hca, 29,
1717 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
1719 DBGC ( hermon, "Hermon %p ICM AUXC base = %llx\n", hermon, icm_offset);
1720 icm_offset += icm_usage ( log_num_qps,
1721 hermon->cap.auxc_entry_size );
1723 /* Shared receive queue contexts */
1724 MLX_FILL_1 ( init_hca, 18,
1725 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
1726 ( icm_offset >> 32 ) );
1727 MLX_FILL_2 ( init_hca, 19,
1728 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
1729 ( icm_offset >> 5 ),
1730 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
1732 DBGC ( hermon, "Hermon %p ICM SRQC base = %llx\n", hermon, icm_offset);
1733 icm_offset += icm_usage ( log_num_srqs,
1734 hermon->cap.srqc_entry_size );
1736 /* Completion queue contexts */
1737 MLX_FILL_1 ( init_hca, 20,
1738 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
1739 ( icm_offset >> 32 ) );
1740 MLX_FILL_2 ( init_hca, 21,
1741 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
1742 ( icm_offset >> 5 ),
1743 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
1745 DBGC ( hermon, "Hermon %p ICM CQC base = %llx\n", hermon, icm_offset );
1746 icm_offset += icm_usage ( log_num_cqs, hermon->cap.cqc_entry_size );
1748 /* Event queue contexts */
1749 MLX_FILL_1 ( init_hca, 32,
1750 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
1751 ( icm_offset >> 32 ) );
1752 MLX_FILL_2 ( init_hca, 33,
1753 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
1754 ( icm_offset >> 5 ),
1755 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
1757 DBGC ( hermon, "Hermon %p ICM EQC base = %llx\n", hermon, icm_offset );
1758 icm_offset += icm_usage ( log_num_eqs, hermon->cap.eqc_entry_size );
1760 /* Memory translation table */
1761 MLX_FILL_1 ( init_hca, 64,
1762 tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
1763 MLX_FILL_1 ( init_hca, 65,
1764 tpt_parameters.mtt_base_addr_l, icm_offset );
1765 DBGC ( hermon, "Hermon %p ICM MTT base = %llx\n", hermon, icm_offset );
1766 icm_offset += icm_usage ( log_num_mtts,
1767 hermon->cap.mtt_entry_size );
1769 /* Memory protection table */
1770 log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
1771 MLX_FILL_1 ( init_hca, 60,
1772 tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
1773 MLX_FILL_1 ( init_hca, 61,
1774 tpt_parameters.dmpt_base_adr_l, icm_offset );
1775 MLX_FILL_1 ( init_hca, 62,
1776 tpt_parameters.log_dmpt_sz, log_num_mpts );
1777 DBGC ( hermon, "Hermon %p ICM DMPT base = %llx\n", hermon, icm_offset);
1778 icm_offset += icm_usage ( log_num_mpts,
1779 hermon->cap.dmpt_entry_size );
1781 /* Multicast table */
1782 MLX_FILL_1 ( init_hca, 48,
1783 multicast_parameters.mc_base_addr_h,
1784 ( icm_offset >> 32 ) );
1785 MLX_FILL_1 ( init_hca, 49,
1786 multicast_parameters.mc_base_addr_l, icm_offset );
1787 MLX_FILL_1 ( init_hca, 52,
1788 multicast_parameters.log_mc_table_entry_sz,
1789 fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
1790 MLX_FILL_1 ( init_hca, 53,
1791 multicast_parameters.log_mc_table_hash_sz, 3 );
1792 MLX_FILL_1 ( init_hca, 54,
1793 multicast_parameters.log_mc_table_sz, 3 );
1794 DBGC ( hermon, "Hermon %p ICM MC base = %llx\n", hermon, icm_offset );
1795 icm_offset += ( ( 8 * sizeof ( struct hermonprm_mcg_entry ) +
1796 HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
1798 hermon->icm_map[HERMON_ICM_OTHER].len =
1799 ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
1802 * Allocate and map physical memory for (portions of) ICM
1805 * ICM AUX area (aligned to its own size)
1810 /* Calculate physical memory required for ICM */
1812 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
1813 icm_len += hermon->icm_map[i].len;
1816 /* Get ICM auxiliary area size */
1817 memset ( &icm_size, 0, sizeof ( icm_size ) );
1818 MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
1819 MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
1820 if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
1821 &icm_aux_size ) ) != 0 ) {
1822 DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
1823 hermon, strerror ( rc ) );
1824 goto err_set_icm_size;
1826 icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
1827 /* Must round up to nearest power of two :( */
1828 icm_aux_len = ( 1 << fls ( icm_aux_len - 1 ) );
1830 /* Allocate ICM data and auxiliary area */
1831 DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
1832 hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
1833 hermon->icm = umalloc ( 2 * icm_aux_len + icm_len );
1834 if ( ! hermon->icm ) {
1838 icm_phys = user_to_phys ( hermon->icm, 0 );
1840 /* Map ICM auxiliary area */
1841 icm_phys = ( ( icm_phys + icm_aux_len - 1 ) & ~( icm_aux_len - 1 ) );
1842 memset ( &map_icm_aux, 0, sizeof ( map_icm_aux ) );
1843 MLX_FILL_2 ( &map_icm_aux, 3,
1844 log2size, fls ( ( icm_aux_len / HERMON_PAGE_SIZE ) - 1 ),
1845 pa_l, ( icm_phys >> 12 ) );
1846 DBGC ( hermon, "Hermon %p mapping ICM AUX (2^%d pages) => %08lx\n",
1847 hermon, fls ( ( icm_aux_len / HERMON_PAGE_SIZE ) - 1 ),
1849 if ( ( rc = hermon_cmd_map_icm_aux ( hermon, &map_icm_aux ) ) != 0 ) {
1850 DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
1851 hermon, strerror ( rc ) );
1852 goto err_map_icm_aux;
1854 icm_phys += icm_aux_len;
1857 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
1858 memset ( &map_icm, 0, sizeof ( map_icm ) );
1859 MLX_FILL_1 ( &map_icm, 0,
1860 va_h, ( hermon->icm_map[i].offset >> 32 ) );
1861 MLX_FILL_1 ( &map_icm, 1,
1862 va_l, ( hermon->icm_map[i].offset >> 12 ) );
1863 MLX_FILL_2 ( &map_icm, 3,
1865 fls ( ( hermon->icm_map[i].len /
1866 HERMON_PAGE_SIZE ) - 1 ),
1867 pa_l, ( icm_phys >> 12 ) );
1868 DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx (2^%d pages) "
1869 "=> %08lx\n", hermon, hermon->icm_map[i].offset,
1870 hermon->icm_map[i].len,
1871 fls ( ( hermon->icm_map[i].len /
1872 HERMON_PAGE_SIZE ) - 1 ), icm_phys );
1873 if ( ( rc = hermon_cmd_map_icm ( hermon, &map_icm ) ) != 0 ) {
1874 DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
1875 hermon, strerror ( rc ) );
1878 icm_phys += hermon->icm_map[i].len;
1884 assert ( i == 0 ); /* We don't handle partial failure at present */
1885 hermon_cmd_unmap_icm_aux ( hermon );
1887 ufree ( hermon->icm );
1888 hermon->icm = UNULL;
1897 * @v hermon Hermon device
1899 static void hermon_free_icm ( struct hermon *hermon ) {
1900 struct hermonprm_scalar_parameter unmap_icm;
1903 for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
1904 memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
1905 MLX_FILL_1 ( &unmap_icm, 0, value_hi,
1906 ( hermon->icm_map[i].offset >> 32 ) );
1907 MLX_FILL_1 ( &unmap_icm, 1, value,
1908 hermon->icm_map[i].offset );
1909 hermon_cmd_unmap_icm ( hermon,
1910 ( 1 << fls ( ( hermon->icm_map[i].len /
1911 HERMON_PAGE_SIZE ) - 1)),
1914 hermon_cmd_unmap_icm_aux ( hermon );
1915 ufree ( hermon->icm );
1916 hermon->icm = UNULL;
1919 /***************************************************************************
1921 * Infiniband link-layer operations
1923 ***************************************************************************
1927 * Initialise Infiniband link
1929 * @v hermon Hermon device
1930 * @ret rc Return status code
1932 static int hermon_init_port ( struct hermon *hermon ) {
1933 struct hermonprm_init_port init_port;
1936 memset ( &init_port, 0, sizeof ( init_port ) );
1937 MLX_FILL_2 ( &init_port, 0,
1940 MLX_FILL_2 ( &init_port, 1,
1941 mtu, HERMON_MTU_2048,
1943 MLX_FILL_1 ( &init_port, 2, max_pkey, 64 );
1944 if ( ( rc = hermon_cmd_init_port ( hermon, PXE_IB_PORT,
1945 &init_port ) ) != 0 ) {
1946 DBGC ( hermon, "Hermon %p could not intialise port: %s\n",
1947 hermon, strerror ( rc ) );
1955 * Close Infiniband link
1957 * @v hermon Hermon device
1959 static void hermon_close_port ( struct hermon *hermon ) {
1962 if ( ( rc = hermon_cmd_close_port ( hermon, PXE_IB_PORT ) ) != 0 ) {
1963 DBGC ( hermon, "Hermon %p could not close port: %s\n",
1964 hermon, strerror ( rc ) );
1965 /* Nothing we can do about this */
1969 /***************************************************************************
1973 ***************************************************************************
1977 * Set up memory protection table
1979 * @v hermon Hermon device
1980 * @ret rc Return status code
1982 static int hermon_setup_mpt ( struct hermon *hermon ) {
1983 struct hermonprm_mpt mpt;
1988 key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
1989 hermon->reserved_lkey = ( ( key << 8 ) | ( key >> 24 ) );
1991 /* Initialise memory protection table */
1992 memset ( &mpt, 0, sizeof ( mpt ) );
1993 MLX_FILL_4 ( &mpt, 0,
1998 MLX_FILL_1 ( &mpt, 2, mem_key, key );
1999 MLX_FILL_1 ( &mpt, 3, pd, HERMON_GLOBAL_PD );
2000 MLX_FILL_1 ( &mpt, 10, len64, 1 );
2001 if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
2002 hermon->cap.reserved_mrws,
2004 DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
2005 hermon, strerror ( rc ) );
2017 * @ret rc Return status code
2019 static int hermon_probe ( struct pci_device *pci,
2020 const struct pci_device_id *id __unused ) {
2021 struct ib_device *ibdev;
2022 struct hermon *hermon;
2023 struct hermonprm_init_hca init_hca;
2026 /* Allocate Infiniband device */
2027 ibdev = alloc_ibdev ( sizeof ( *hermon ) );
2032 ibdev->op = &hermon_ib_operations;
2033 pci_set_drvdata ( pci, ibdev );
2034 ibdev->dev = &pci->dev;
2035 hermon = ibdev->dev_priv;
2036 memset ( hermon, 0, sizeof ( *hermon ) );
2038 /* Fix up PCI device */
2039 adjust_pci_device ( pci );
2042 hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR),
2043 HERMON_PCI_CONFIG_BAR_SIZE );
2044 hermon->uar = ioremap ( ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ) +
2045 HERMON_UAR_PAGE * HERMON_PAGE_SIZE ),
2048 /* Allocate space for mailboxes */
2049 hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
2050 HERMON_MBOX_ALIGN );
2051 if ( ! hermon->mailbox_in ) {
2053 goto err_mailbox_in;
2055 hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
2056 HERMON_MBOX_ALIGN );
2057 if ( ! hermon->mailbox_out ) {
2059 goto err_mailbox_out;
2062 /* Start firmware */
2063 if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
2064 goto err_start_firmware;
2066 /* Get device limits */
2067 if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
2071 memset ( &init_hca, 0, sizeof ( init_hca ) );
2072 if ( ( rc = hermon_alloc_icm ( hermon, &init_hca ) ) != 0 )
2075 /* Initialise HCA */
2076 MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
2077 MLX_FILL_1 ( &init_hca, 5, udp, 1 );
2078 MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
2079 if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
2080 DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
2081 hermon, strerror ( rc ) );
2085 /* Set up memory protection */
2086 if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
2089 /* Bring up IB layer */
2090 if ( ( rc = hermon_init_port ( hermon ) ) != 0 )
2094 if ( ( rc = hermon_wait_for_link ( hermon ) ) != 0 )
2095 goto err_wait_for_link;
2097 /* Get MAD parameters */
2098 if ( ( rc = hermon_get_mad_params ( ibdev ) ) != 0 )
2099 goto err_get_mad_params;
2101 DBGC ( hermon, "Hermon %p port GID is %08lx:%08lx:%08lx:%08lx\n",
2102 hermon, htonl ( ibdev->port_gid.u.dwords[0] ),
2103 htonl ( ibdev->port_gid.u.dwords[1] ),
2104 htonl ( ibdev->port_gid.u.dwords[2] ),
2105 htonl ( ibdev->port_gid.u.dwords[3] ) );
2107 /* Add IPoIB device */
2108 if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
2109 DBGC ( hermon, "Hermon %p could not add IPoIB device: %s\n",
2110 hermon, strerror ( rc ) );
2111 goto err_ipoib_probe;
2119 hermon_close_port ( hermon );
2122 hermon_cmd_close_hca ( hermon );
2124 hermon_free_icm ( hermon );
2127 hermon_stop_firmware ( hermon );
2129 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2131 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2133 free_ibdev ( ibdev );
2143 static void hermon_remove ( struct pci_device *pci ) {
2144 struct ib_device *ibdev = pci_get_drvdata ( pci );
2145 struct hermon *hermon = ibdev->dev_priv;
2147 ipoib_remove ( ibdev );
2148 hermon_close_port ( hermon );
2149 hermon_cmd_close_hca ( hermon );
2150 hermon_free_icm ( hermon );
2151 hermon_stop_firmware ( hermon );
2152 hermon_stop_firmware ( hermon );
2153 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2154 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2155 free_ibdev ( ibdev );
2158 static struct pci_device_id hermon_nics[] = {
2159 PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver" ),
2160 PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver" ),
2163 struct pci_driver hermon_driver __pci_driver = {
2165 .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
2166 .probe = hermon_probe,
2167 .remove = hermon_remove,