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.
30 #include <gpxe/malloc.h>
31 #include <gpxe/umalloc.h>
32 #include <gpxe/iobuf.h>
33 #include <gpxe/netdevice.h>
34 #include <gpxe/infiniband.h>
35 #include <gpxe/ipoib.h>
41 * Mellanox Hermon Infiniband HCA
48 /***************************************************************************
50 * Queue number allocation
52 ***************************************************************************
56 * Allocate offsets within usage bitmask
58 * @v bits Usage bitmask
59 * @v bits_len Length of usage bitmask
60 * @v num_bits Number of contiguous bits to allocate within bitmask
61 * @ret bit First free bit within bitmask, or negative error
63 static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
64 unsigned int bits_len,
65 unsigned int num_bits ) {
67 hermon_bitmask_t mask = 1;
68 unsigned int found = 0;
70 /* Search bits for num_bits contiguous free bits */
71 while ( bit < bits_len ) {
72 if ( ( mask & *bits ) == 0 ) {
73 if ( ++found == num_bits )
79 mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
86 /* Mark bits as in-use */
91 mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
94 return ( bit - num_bits + 1 );
98 * Free offsets within usage bitmask
100 * @v bits Usage bitmask
101 * @v bit Starting bit within bitmask
102 * @v num_bits Number of contiguous bits to free within bitmask
104 static void hermon_bitmask_free ( hermon_bitmask_t *bits,
105 int bit, unsigned int num_bits ) {
106 hermon_bitmask_t mask;
108 for ( ; num_bits ; bit++, num_bits-- ) {
109 mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
110 bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
114 /***************************************************************************
118 ***************************************************************************
122 * Wait for Hermon command completion
124 * @v hermon Hermon device
125 * @v hcr HCA command registers
126 * @ret rc Return status code
128 static int hermon_cmd_wait ( struct hermon *hermon,
129 struct hermonprm_hca_command_register *hcr ) {
132 for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
134 readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
135 if ( ( MLX_GET ( hcr, go ) == 0 ) &&
136 ( MLX_GET ( hcr, t ) == hermon->toggle ) )
146 * @v hermon Hermon device
147 * @v command Command opcode, flags and input/output lengths
148 * @v op_mod Opcode modifier (0 if no modifier applicable)
149 * @v in Input parameters
150 * @v in_mod Input modifier (0 if no modifier applicable)
151 * @v out Output parameters
152 * @ret rc Return status code
154 static int hermon_cmd ( struct hermon *hermon, unsigned long command,
155 unsigned int op_mod, const void *in,
156 unsigned int in_mod, void *out ) {
157 struct hermonprm_hca_command_register hcr;
158 unsigned int opcode = HERMON_HCR_OPCODE ( command );
159 size_t in_len = HERMON_HCR_IN_LEN ( command );
160 size_t out_len = HERMON_HCR_OUT_LEN ( command );
167 assert ( in_len <= HERMON_MBOX_SIZE );
168 assert ( out_len <= HERMON_MBOX_SIZE );
170 DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
171 hermon, opcode, in_len,
172 ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
173 ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
175 /* Check that HCR is free */
176 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
177 DBGC ( hermon, "Hermon %p command interface locked\n",
182 /* Flip HCR toggle */
183 hermon->toggle = ( 1 - hermon->toggle );
186 memset ( &hcr, 0, sizeof ( hcr ) );
187 in_buffer = &hcr.u.dwords[0];
188 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
189 in_buffer = hermon->mailbox_in;
190 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
192 memcpy ( in_buffer, in, in_len );
193 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
194 out_buffer = &hcr.u.dwords[3];
195 if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
196 out_buffer = hermon->mailbox_out;
197 MLX_FILL_1 ( &hcr, 4, out_param_l,
198 virt_to_bus ( out_buffer ) );
200 MLX_FILL_4 ( &hcr, 6,
202 opcode_modifier, op_mod,
205 DBGC ( hermon, "Hermon %p issuing command:\n", hermon );
206 DBGC_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
207 &hcr, sizeof ( hcr ) );
208 if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
209 DBGC2 ( hermon, "Input mailbox:\n" );
210 DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
211 ( ( in_len < 512 ) ? in_len : 512 ) );
215 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
217 writel ( hcr.u.dwords[i],
218 hermon->config + HERMON_HCR_REG ( i ) );
222 /* Wait for command completion */
223 if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
224 DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
227 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
228 &hcr, sizeof ( hcr ) );
232 /* Check command status */
233 status = MLX_GET ( &hcr, status );
235 DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
238 virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
239 &hcr, sizeof ( hcr ) );
243 /* Read output parameters, if any */
244 hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
245 hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
246 memcpy ( out, out_buffer, out_len );
248 DBGC2 ( hermon, "Output%s:\n",
249 ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
250 DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
251 ( ( out_len < 512 ) ? out_len : 512 ) );
258 hermon_cmd_query_dev_cap ( struct hermon *hermon,
259 struct hermonprm_query_dev_cap *dev_cap ) {
260 return hermon_cmd ( hermon,
261 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
262 1, sizeof ( *dev_cap ) ),
263 0, NULL, 0, dev_cap );
267 hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
268 return hermon_cmd ( hermon,
269 HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
275 hermon_cmd_init_hca ( struct hermon *hermon,
276 const struct hermonprm_init_hca *init_hca ) {
277 return hermon_cmd ( hermon,
278 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
279 1, sizeof ( *init_hca ) ),
280 0, init_hca, 0, NULL );
284 hermon_cmd_close_hca ( struct hermon *hermon ) {
285 return hermon_cmd ( hermon,
286 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
291 hermon_cmd_init_port ( struct hermon *hermon, unsigned int port,
292 const struct hermonprm_init_port *init_port ) {
293 return hermon_cmd ( hermon,
294 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_PORT,
295 1, sizeof ( *init_port ) ),
296 0, init_port, port, NULL );
300 hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
301 return hermon_cmd ( hermon,
302 HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
303 0, NULL, port, NULL );
307 hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
308 const struct hermonprm_mpt *mpt ) {
309 return hermon_cmd ( hermon,
310 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
311 1, sizeof ( *mpt ) ),
312 0, mpt, index, NULL );
316 hermon_cmd_write_mtt ( struct hermon *hermon,
317 const struct hermonprm_write_mtt *write_mtt ) {
318 return hermon_cmd ( hermon,
319 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
320 1, sizeof ( *write_mtt ) ),
321 0, write_mtt, 1, NULL );
325 hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
326 const struct hermonprm_eqc *eqc ) {
327 return hermon_cmd ( hermon,
328 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
329 1, sizeof ( *eqc ) ),
330 0, eqc, index, NULL );
334 hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index ) {
335 return hermon_cmd ( hermon,
336 HERMON_HCR_VOID_CMD ( HERMON_HCR_HW2SW_EQ ),
337 1, NULL, index, NULL );
341 hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
342 const struct hermonprm_completion_queue_context *cqctx ){
343 return hermon_cmd ( hermon,
344 HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
345 1, sizeof ( *cqctx ) ),
346 0, cqctx, cqn, NULL );
350 hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
351 struct hermonprm_completion_queue_context *cqctx) {
352 return hermon_cmd ( hermon,
353 HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
354 1, sizeof ( *cqctx ) ),
355 0, NULL, cqn, cqctx );
359 hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
360 const struct hermonprm_qp_ee_state_transitions *ctx ){
361 return hermon_cmd ( hermon,
362 HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
363 1, sizeof ( *ctx ) ),
368 hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
369 const struct hermonprm_qp_ee_state_transitions *ctx ){
370 return hermon_cmd ( hermon,
371 HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
372 1, sizeof ( *ctx ) ),
377 hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
378 const struct hermonprm_qp_ee_state_transitions *ctx ) {
379 return hermon_cmd ( hermon,
380 HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
381 1, sizeof ( *ctx ) ),
386 hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
387 return hermon_cmd ( hermon,
388 HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
389 0x03, NULL, qpn, NULL );
393 hermon_cmd_mad_ifc ( struct hermon *hermon, union hermonprm_mad *mad ) {
394 return hermon_cmd ( hermon,
395 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
397 1, sizeof ( *mad ) ),
398 0x03, mad, PXE_IB_PORT, mad );
402 hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
403 struct hermonprm_mcg_entry *mcg ) {
404 return hermon_cmd ( hermon,
405 HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
406 1, sizeof ( *mcg ) ),
407 0, NULL, index, mcg );
411 hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
412 const struct hermonprm_mcg_entry *mcg ) {
413 return hermon_cmd ( hermon,
414 HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
415 1, sizeof ( *mcg ) ),
416 0, mcg, index, NULL );
420 hermon_cmd_mgid_hash ( struct hermon *hermon, const struct ib_gid *gid,
421 struct hermonprm_mgm_hash *hash ) {
422 return hermon_cmd ( hermon,
423 HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
425 0, sizeof ( *hash ) ),
430 hermon_cmd_run_fw ( struct hermon *hermon ) {
431 return hermon_cmd ( hermon,
432 HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
437 hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
438 const struct hermonprm_scalar_parameter *offset ) {
439 return hermon_cmd ( hermon,
440 HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
441 0, sizeof ( *offset ) ),
442 0, offset, page_count, NULL );
446 hermon_cmd_map_icm ( struct hermon *hermon,
447 const struct hermonprm_virtual_physical_mapping *map ) {
448 return hermon_cmd ( hermon,
449 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
450 1, sizeof ( *map ) ),
455 hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
456 return hermon_cmd ( hermon,
457 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
462 hermon_cmd_map_icm_aux ( struct hermon *hermon,
463 const struct hermonprm_virtual_physical_mapping *map ) {
464 return hermon_cmd ( hermon,
465 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
466 1, sizeof ( *map ) ),
471 hermon_cmd_set_icm_size ( struct hermon *hermon,
472 const struct hermonprm_scalar_parameter *icm_size,
473 struct hermonprm_scalar_parameter *icm_aux_size ) {
474 return hermon_cmd ( hermon,
475 HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
476 0, sizeof ( *icm_size ),
477 0, sizeof (*icm_aux_size) ),
478 0, icm_size, 0, icm_aux_size );
482 hermon_cmd_unmap_fa ( struct hermon *hermon ) {
483 return hermon_cmd ( hermon,
484 HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
489 hermon_cmd_map_fa ( struct hermon *hermon,
490 const struct hermonprm_virtual_physical_mapping *map ) {
491 return hermon_cmd ( hermon,
492 HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
493 1, sizeof ( *map ) ),
497 /***************************************************************************
499 * Memory translation table operations
501 ***************************************************************************
505 * Allocate MTT entries
507 * @v hermon Hermon device
508 * @v memory Memory to map into MTT
509 * @v len Length of memory to map
510 * @v mtt MTT descriptor to fill in
511 * @ret rc Return status code
513 static int hermon_alloc_mtt ( struct hermon *hermon,
514 const void *memory, size_t len,
515 struct hermon_mtt *mtt ) {
516 struct hermonprm_write_mtt write_mtt;
518 unsigned int page_offset;
519 unsigned int num_pages;
521 unsigned int mtt_base_addr;
525 /* Find available MTT entries */
526 start = virt_to_phys ( memory );
527 page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
528 start -= page_offset;
530 num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
531 mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
533 if ( mtt_offset < 0 ) {
534 DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
539 mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
540 hermon->cap.mtt_entry_size );
542 /* Fill in MTT structure */
543 mtt->mtt_offset = mtt_offset;
544 mtt->num_pages = num_pages;
545 mtt->mtt_base_addr = mtt_base_addr;
546 mtt->page_offset = page_offset;
548 /* Construct and issue WRITE_MTT commands */
549 for ( i = 0 ; i < num_pages ; i++ ) {
550 memset ( &write_mtt, 0, sizeof ( write_mtt ) );
551 MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
552 value, mtt_base_addr );
553 MLX_FILL_2 ( &write_mtt.mtt, 1,
555 ptag_l, ( start >> 3 ) );
556 if ( ( rc = hermon_cmd_write_mtt ( hermon,
557 &write_mtt ) ) != 0 ) {
558 DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
559 hermon, mtt_base_addr );
562 start += HERMON_PAGE_SIZE;
563 mtt_base_addr += hermon->cap.mtt_entry_size;
569 hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
577 * @v hermon Hermon device
578 * @v mtt MTT descriptor
580 static void hermon_free_mtt ( struct hermon *hermon,
581 struct hermon_mtt *mtt ) {
582 hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
586 /***************************************************************************
588 * Completion queue operations
590 ***************************************************************************
594 * Create completion queue
596 * @v ibdev Infiniband device
597 * @v cq Completion queue
598 * @ret rc Return status code
600 static int hermon_create_cq ( struct ib_device *ibdev,
601 struct ib_completion_queue *cq ) {
602 struct hermon *hermon = ibdev->dev_priv;
603 struct hermon_completion_queue *hermon_cq;
604 struct hermonprm_completion_queue_context cqctx;
609 /* Find a free completion queue number */
610 cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
612 if ( cqn_offset < 0 ) {
613 DBGC ( hermon, "Hermon %p out of completion queues\n",
618 cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
620 /* Allocate control structures */
621 hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
627 /* Allocate completion queue itself */
628 hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
629 hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
630 sizeof ( hermon_cq->cqe[0] ) );
631 if ( ! hermon_cq->cqe ) {
635 memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
636 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
637 MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
641 /* Allocate MTT entries */
642 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
644 &hermon_cq->mtt ) ) != 0 )
647 /* Hand queue over to hardware */
648 memset ( &cqctx, 0, sizeof ( cqctx ) );
649 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
650 MLX_FILL_1 ( &cqctx, 2,
651 page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
652 MLX_FILL_2 ( &cqctx, 3,
653 usr_page, HERMON_UAR_PAGE,
654 log_cq_size, fls ( cq->num_cqes - 1 ) );
655 MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
656 ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
657 MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
658 ( virt_to_phys ( &hermon_cq->doorbell ) >> 3 ) );
659 if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
660 DBGC ( hermon, "Hermon %p SW2HW_CQ failed: %s\n",
661 hermon, strerror ( rc ) );
665 DBGC ( hermon, "Hermon %p CQN %#lx ring at [%p,%p)\n",
666 hermon, cq->cqn, hermon_cq->cqe,
667 ( ( ( void * ) hermon_cq->cqe ) + hermon_cq->cqe_size ) );
668 cq->dev_priv = hermon_cq;
672 hermon_free_mtt ( hermon, &hermon_cq->mtt );
674 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
678 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
684 * Destroy completion queue
686 * @v ibdev Infiniband device
687 * @v cq Completion queue
689 static void hermon_destroy_cq ( struct ib_device *ibdev,
690 struct ib_completion_queue *cq ) {
691 struct hermon *hermon = ibdev->dev_priv;
692 struct hermon_completion_queue *hermon_cq = cq->dev_priv;
693 struct hermonprm_completion_queue_context cqctx;
697 /* Take ownership back from hardware */
698 if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
699 DBGC ( hermon, "Hermon %p FATAL HW2SW_CQ failed on CQN %#lx: "
700 "%s\n", hermon, cq->cqn, strerror ( rc ) );
701 /* Leak memory and return; at least we avoid corruption */
705 /* Free MTT entries */
706 hermon_free_mtt ( hermon, &hermon_cq->mtt );
709 free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
712 /* Mark queue number as free */
713 cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
714 hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
719 /***************************************************************************
721 * Queue pair operations
723 ***************************************************************************
729 * @v ibdev Infiniband device
731 * @ret rc Return status code
733 static int hermon_create_qp ( struct ib_device *ibdev,
734 struct ib_queue_pair *qp ) {
735 struct hermon *hermon = ibdev->dev_priv;
736 struct hermon_queue_pair *hermon_qp;
737 struct hermonprm_qp_ee_state_transitions qpctx;
741 /* Find a free queue pair number */
742 qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
744 if ( qpn_offset < 0 ) {
745 DBGC ( hermon, "Hermon %p out of queue pairs\n", hermon );
749 qp->qpn = ( HERMON_QPN_BASE + hermon->cap.reserved_qps +
752 /* Allocate control structures */
753 hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
759 /* Allocate work queue buffer */
760 hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
761 ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
762 hermon_qp->send.num_wqes =
763 ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
764 hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
765 sizeof ( hermon_qp->send.wqe[0] ) );
766 hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
767 sizeof ( hermon_qp->recv.wqe[0] ) );
768 hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
769 hermon_qp->recv.wqe_size );
770 hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
771 sizeof ( hermon_qp->send.wqe[0] ) );
772 if ( ! hermon_qp->wqe ) {
776 hermon_qp->send.wqe = hermon_qp->wqe;
777 memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
778 hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
779 memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
781 /* Allocate MTT entries */
782 if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
784 &hermon_qp->mtt ) ) != 0 ) {
788 /* Hand queue over to hardware */
789 memset ( &qpctx, 0, sizeof ( qpctx ) );
790 MLX_FILL_2 ( &qpctx, 2,
791 qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
792 qpc_eec_data.st, HERMON_ST_UD );
793 MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
794 MLX_FILL_4 ( &qpctx, 4,
795 qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
796 qpc_eec_data.log_rq_stride,
797 ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
798 qpc_eec_data.log_sq_size,
799 fls ( hermon_qp->send.num_wqes - 1 ),
800 qpc_eec_data.log_sq_stride,
801 ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
802 MLX_FILL_1 ( &qpctx, 5,
803 qpc_eec_data.usr_page, HERMON_UAR_PAGE );
804 MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
805 MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.page_offset,
806 ( hermon_qp->mtt.page_offset >> 6 ) );
807 MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
808 MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
809 ( virt_to_phys ( &hermon_qp->recv.doorbell ) >> 2 ) );
810 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
811 MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
812 ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
813 if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
815 DBGC ( hermon, "Hermon %p RST2INIT_QP failed: %s\n",
816 hermon, strerror ( rc ) );
817 goto err_rst2init_qp;
820 memset ( &qpctx, 0, sizeof ( qpctx ) );
821 MLX_FILL_2 ( &qpctx, 4,
822 qpc_eec_data.mtu, HERMON_MTU_2048,
823 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
824 MLX_FILL_1 ( &qpctx, 16,
825 qpc_eec_data.primary_address_path.sched_queue,
826 ( 0x83 /* default policy */ |
827 ( ( PXE_IB_PORT - 1 ) << 6 ) ) );
828 if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
830 DBGC ( hermon, "Hermon %p INIT2RTR_QP failed: %s\n",
831 hermon, strerror ( rc ) );
832 goto err_init2rtr_qp;
834 memset ( &qpctx, 0, sizeof ( qpctx ) );
835 if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
836 DBGC ( hermon, "Hermon %p RTR2RTS_QP failed: %s\n",
837 hermon, strerror ( rc ) );
841 DBGC ( hermon, "Hermon %p QPN %#lx send ring at [%p,%p)\n",
842 hermon, qp->qpn, hermon_qp->send.wqe,
843 ( ((void *)hermon_qp->send.wqe ) + hermon_qp->send.wqe_size ) );
844 DBGC ( hermon, "Hermon %p QPN %#lx receive ring at [%p,%p)\n",
845 hermon, qp->qpn, hermon_qp->recv.wqe,
846 ( ((void *)hermon_qp->recv.wqe ) + hermon_qp->recv.wqe_size ) );
847 qp->dev_priv = hermon_qp;
852 hermon_cmd_2rst_qp ( hermon, qp->qpn );
854 hermon_free_mtt ( hermon, &hermon_qp->mtt );
856 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
860 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
868 * @v ibdev Infiniband device
871 static void hermon_destroy_qp ( struct ib_device *ibdev,
872 struct ib_queue_pair *qp ) {
873 struct hermon *hermon = ibdev->dev_priv;
874 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
878 /* Take ownership back from hardware */
879 if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
880 DBGC ( hermon, "Hermon %p FATAL 2RST_QP failed on QPN %#lx: "
881 "%s\n", hermon, qp->qpn, strerror ( rc ) );
882 /* Leak memory and return; at least we avoid corruption */
886 /* Free MTT entries */
887 hermon_free_mtt ( hermon, &hermon_qp->mtt );
890 free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
893 /* Mark queue number as free */
894 qpn_offset = ( qp->qpn - HERMON_QPN_BASE -
895 hermon->cap.reserved_qps );
896 hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
901 /***************************************************************************
903 * Work request operations
905 ***************************************************************************
908 /** GID used for GID-less send work queue entries */
909 static const struct ib_gid hermon_no_gid = {
910 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
914 * Post send work queue entry
916 * @v ibdev Infiniband device
918 * @v av Address vector
919 * @v iobuf I/O buffer
920 * @ret rc Return status code
922 static int hermon_post_send ( struct ib_device *ibdev,
923 struct ib_queue_pair *qp,
924 struct ib_address_vector *av,
925 struct io_buffer *iobuf ) {
926 struct hermon *hermon = ibdev->dev_priv;
927 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
928 struct ib_work_queue *wq = &qp->send;
929 struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
930 struct hermonprm_ud_send_wqe *wqe;
931 const struct ib_gid *gid;
932 union hermonprm_doorbell_register db_reg;
933 unsigned int wqe_idx_mask;
935 /* Allocate work queue entry */
936 wqe_idx_mask = ( wq->num_wqes - 1 );
937 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
938 DBGC ( hermon, "Hermon %p send queue full", hermon );
941 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
942 wqe = &hermon_send_wq->wqe[ wq->next_idx &
943 ( hermon_send_wq->num_wqes - 1 ) ].ud;
945 /* Construct work queue entry */
946 memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
947 ( sizeof ( *wqe ) - 4 ) );
948 MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( *wqe ) / 16 ) );
949 MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
950 MLX_FILL_2 ( &wqe->ud, 0,
951 ud_address_vector.pd, HERMON_GLOBAL_PD,
952 ud_address_vector.port_number, PXE_IB_PORT );
953 MLX_FILL_2 ( &wqe->ud, 1,
954 ud_address_vector.rlid, av->dlid,
955 ud_address_vector.g, av->gid_present );
956 MLX_FILL_1 ( &wqe->ud, 2,
957 ud_address_vector.max_stat_rate,
958 ( ( ( av->rate < 2 ) || ( av->rate > 10 ) ) ?
959 8 : ( av->rate + 5 ) ) );
960 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
961 gid = ( av->gid_present ? &av->gid : &hermon_no_gid );
962 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
963 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
964 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
965 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
966 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
967 MLX_FILL_1 ( &wqe->data[0], 3,
968 local_address_l, virt_to_bus ( iobuf->data ) );
970 MLX_FILL_2 ( &wqe->ctrl, 0,
971 opcode, HERMON_OPCODE_SEND,
973 ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 ) );
974 DBGCP ( hermon, "Hermon %p posting send WQE:\n", hermon );
975 DBGCP_HD ( hermon, wqe, sizeof ( *wqe ) );
978 /* Ring doorbell register */
979 MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
980 DBGCP ( hermon, "Ringing doorbell %08lx with %08lx\n",
981 virt_to_phys ( hermon->uar + HERMON_DB_POST_SND_OFFSET ),
983 writel ( db_reg.dword[0], ( hermon->uar + HERMON_DB_POST_SND_OFFSET ));
985 /* Update work queue's index */
992 * Post receive work queue entry
994 * @v ibdev Infiniband device
996 * @v iobuf I/O buffer
997 * @ret rc Return status code
999 static int hermon_post_recv ( struct ib_device *ibdev,
1000 struct ib_queue_pair *qp,
1001 struct io_buffer *iobuf ) {
1002 struct hermon *hermon = ibdev->dev_priv;
1003 struct hermon_queue_pair *hermon_qp = qp->dev_priv;
1004 struct ib_work_queue *wq = &qp->recv;
1005 struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
1006 struct hermonprm_recv_wqe *wqe;
1007 unsigned int wqe_idx_mask;
1009 /* Allocate work queue entry */
1010 wqe_idx_mask = ( wq->num_wqes - 1 );
1011 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1012 DBGC ( hermon, "Hermon %p receive queue full", hermon );
1015 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1016 wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
1018 /* Construct work queue entry */
1019 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
1020 MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->reserved_lkey );
1021 MLX_FILL_1 ( &wqe->data[0], 3,
1022 local_address_l, virt_to_bus ( iobuf->data ) );
1024 /* Update work queue's index */
1027 /* Update doorbell record */
1029 MLX_FILL_1 ( &hermon_recv_wq->doorbell, 0, receive_wqe_counter,
1030 ( wq->next_idx & 0xffff ) );
1038 * @v ibdev Infiniband device
1039 * @v cq Completion queue
1040 * @v cqe Hardware completion queue entry
1041 * @v complete_send Send completion handler
1042 * @v complete_recv Receive completion handler
1043 * @ret rc Return status code
1045 static int hermon_complete ( struct ib_device *ibdev,
1046 struct ib_completion_queue *cq,
1047 union hermonprm_completion_entry *cqe,
1048 ib_completer_t complete_send,
1049 ib_completer_t complete_recv ) {
1050 struct hermon *hermon = ibdev->dev_priv;
1051 struct ib_completion completion;
1052 struct ib_work_queue *wq;
1053 struct ib_queue_pair *qp;
1054 struct hermon_queue_pair *hermon_qp;
1055 struct io_buffer *iobuf;
1056 ib_completer_t complete;
1057 unsigned int opcode;
1060 unsigned int wqe_idx;
1063 /* Parse completion */
1064 memset ( &completion, 0, sizeof ( completion ) );
1065 qpn = MLX_GET ( &cqe->normal, qpn );
1066 is_send = MLX_GET ( &cqe->normal, s_r );
1067 opcode = MLX_GET ( &cqe->normal, opcode );
1068 if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
1069 /* "s" field is not valid for error opcodes */
1070 is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
1071 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
1072 DBGC ( hermon, "Hermon %p CQN %lx syndrome %x vendor %lx\n",
1073 hermon, cq->cqn, completion.syndrome,
1074 MLX_GET ( &cqe->error, vendor_error_syndrome ) );
1076 /* Don't return immediately; propagate error to completer */
1079 /* Identify work queue */
1080 wq = ib_find_wq ( cq, qpn, is_send );
1082 DBGC ( hermon, "Hermon %p CQN %lx unknown %s QPN %lx\n",
1083 hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1087 hermon_qp = qp->dev_priv;
1089 /* Identify I/O buffer */
1090 wqe_idx = ( MLX_GET ( &cqe->normal, wqe_counter ) &
1091 ( wq->num_wqes - 1 ) );
1092 iobuf = wq->iobufs[wqe_idx];
1094 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx empty WQE %x\n",
1095 hermon, cq->cqn, qpn, wqe_idx );
1098 wq->iobufs[wqe_idx] = NULL;
1100 /* Fill in length for received packets */
1102 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
1103 if ( completion.len > iob_tailroom ( iobuf ) ) {
1104 DBGC ( hermon, "Hermon %p CQN %lx QPN %lx IDX %x "
1105 "overlength received packet length %zd\n",
1106 hermon, cq->cqn, qpn, wqe_idx, completion.len );
1111 /* Pass off to caller's completion handler */
1112 complete = ( is_send ? complete_send : complete_recv );
1113 complete ( ibdev, qp, &completion, iobuf );
1119 * Poll completion queue
1121 * @v ibdev Infiniband device
1122 * @v cq Completion queue
1123 * @v complete_send Send completion handler
1124 * @v complete_recv Receive completion handler
1126 static void hermon_poll_cq ( struct ib_device *ibdev,
1127 struct ib_completion_queue *cq,
1128 ib_completer_t complete_send,
1129 ib_completer_t complete_recv ) {
1130 struct hermon *hermon = ibdev->dev_priv;
1131 struct hermon_completion_queue *hermon_cq = cq->dev_priv;
1132 union hermonprm_completion_entry *cqe;
1133 unsigned int cqe_idx_mask;
1137 /* Look for completion entry */
1138 cqe_idx_mask = ( cq->num_cqes - 1 );
1139 cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
1140 if ( MLX_GET ( &cqe->normal, owner ) ^
1141 ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
1142 /* Entry still owned by hardware; end of poll */
1145 DBGCP ( hermon, "Hermon %p completion:\n", hermon );
1146 DBGCP_HD ( hermon, cqe, sizeof ( *cqe ) );
1148 /* Handle completion */
1149 if ( ( rc = hermon_complete ( ibdev, cq, cqe, complete_send,
1150 complete_recv ) ) != 0 ) {
1151 DBGC ( hermon, "Hermon %p failed to complete: %s\n",
1152 hermon, strerror ( rc ) );
1153 DBGC_HD ( hermon, cqe, sizeof ( *cqe ) );
1156 /* Update completion queue's index */
1159 /* Update doorbell record */
1160 MLX_FILL_1 ( &hermon_cq->doorbell, 0, update_ci,
1161 ( cq->next_idx & 0xffffffUL ) );
1165 /***************************************************************************
1167 * Multicast group operations
1169 ***************************************************************************
1173 * Attach to multicast group
1175 * @v ibdev Infiniband device
1177 * @v gid Multicast GID
1178 * @ret rc Return status code
1180 static int hermon_mcast_attach ( struct ib_device *ibdev,
1181 struct ib_queue_pair *qp,
1182 struct ib_gid *gid ) {
1183 struct hermon *hermon = ibdev->dev_priv;
1184 struct hermonprm_mgm_hash hash;
1185 struct hermonprm_mcg_entry mcg;
1189 /* Generate hash table index */
1190 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1191 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1192 hermon, strerror ( rc ) );
1195 index = MLX_GET ( &hash, hash );
1197 /* Check for existing hash table entry */
1198 if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
1199 DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
1200 hermon, index, strerror ( rc ) );
1203 if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
1204 /* FIXME: this implementation allows only a single QP
1205 * per multicast group, and doesn't handle hash
1206 * collisions. Sufficient for IPoIB but may need to
1207 * be extended in future.
1209 DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
1214 /* Update hash table entry */
1215 MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
1216 MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
1217 memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
1218 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1219 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1220 hermon, index, strerror ( rc ) );
1228 * Detach from multicast group
1230 * @v ibdev Infiniband device
1232 * @v gid Multicast GID
1234 static void hermon_mcast_detach ( struct ib_device *ibdev,
1235 struct ib_queue_pair *qp __unused,
1236 struct ib_gid *gid ) {
1237 struct hermon *hermon = ibdev->dev_priv;
1238 struct hermonprm_mgm_hash hash;
1239 struct hermonprm_mcg_entry mcg;
1243 /* Generate hash table index */
1244 if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
1245 DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
1246 hermon, strerror ( rc ) );
1249 index = MLX_GET ( &hash, hash );
1251 /* Clear hash table entry */
1252 memset ( &mcg, 0, sizeof ( mcg ) );
1253 if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
1254 DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
1255 hermon, index, strerror ( rc ) );
1260 /** Hermon Infiniband operations */
1261 static struct ib_device_operations hermon_ib_operations = {
1262 .create_cq = hermon_create_cq,
1263 .destroy_cq = hermon_destroy_cq,
1264 .create_qp = hermon_create_qp,
1265 .destroy_qp = hermon_destroy_qp,
1266 .post_send = hermon_post_send,
1267 .post_recv = hermon_post_recv,
1268 .poll_cq = hermon_poll_cq,
1269 .mcast_attach = hermon_mcast_attach,
1270 .mcast_detach = hermon_mcast_detach,
1273 /***************************************************************************
1275 * MAD IFC operations
1277 ***************************************************************************
1280 static int hermon_mad_ifc ( struct hermon *hermon,
1281 union hermonprm_mad *mad ) {
1282 struct ib_mad_hdr *hdr = &mad->mad.mad_hdr;
1285 hdr->base_version = IB_MGMT_BASE_VERSION;
1286 if ( ( rc = hermon_cmd_mad_ifc ( hermon, mad ) ) != 0 ) {
1287 DBGC ( hermon, "Hermon %p could not issue MAD IFC: %s\n",
1288 hermon, strerror ( rc ) );
1291 if ( hdr->status != 0 ) {
1292 DBGC ( hermon, "Hermon %p MAD IFC status %04x\n",
1293 hermon, ntohs ( hdr->status ) );
1299 static int hermon_get_port_info ( struct hermon *hermon,
1300 struct ib_mad_port_info *port_info ) {
1301 union hermonprm_mad mad;
1302 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1305 memset ( &mad, 0, sizeof ( mad ) );
1306 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1307 hdr->class_version = 1;
1308 hdr->method = IB_MGMT_METHOD_GET;
1309 hdr->attr_id = htons ( IB_SMP_ATTR_PORT_INFO );
1310 hdr->attr_mod = htonl ( PXE_IB_PORT );
1311 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1312 DBGC ( hermon, "Hermon %p could not get port info: %s\n",
1313 hermon, strerror ( rc ) );
1316 memcpy ( port_info, &mad.mad.port_info, sizeof ( *port_info ) );
1320 static int hermon_get_guid_info ( struct hermon *hermon,
1321 struct ib_mad_guid_info *guid_info ) {
1322 union hermonprm_mad mad;
1323 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1326 memset ( &mad, 0, sizeof ( mad ) );
1327 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1328 hdr->class_version = 1;
1329 hdr->method = IB_MGMT_METHOD_GET;
1330 hdr->attr_id = htons ( IB_SMP_ATTR_GUID_INFO );
1331 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1332 DBGC ( hermon, "Hermon %p could not get GUID info: %s\n",
1333 hermon, strerror ( rc ) );
1336 memcpy ( guid_info, &mad.mad.guid_info, sizeof ( *guid_info ) );
1340 static int hermon_get_pkey_table ( struct hermon *hermon,
1341 struct ib_mad_pkey_table *pkey_table ) {
1342 union hermonprm_mad mad;
1343 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1346 memset ( &mad, 0, sizeof ( mad ) );
1347 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1348 hdr->class_version = 1;
1349 hdr->method = IB_MGMT_METHOD_GET;
1350 hdr->attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE );
1351 if ( ( rc = hermon_mad_ifc ( hermon, &mad ) ) != 0 ) {
1352 DBGC ( hermon, "Hermon %p could not get pkey table: %s\n",
1353 hermon, strerror ( rc ) );
1356 memcpy ( pkey_table, &mad.mad.pkey_table, sizeof ( *pkey_table ) );
1360 static int hermon_get_port_gid ( struct hermon *hermon,
1361 struct ib_gid *port_gid ) {
1363 /* This union exists just to save stack space */
1364 struct ib_mad_port_info port_info;
1365 struct ib_mad_guid_info guid_info;
1369 /* Port info gives us the first half of the port GID */
1370 if ( ( rc = hermon_get_port_info ( hermon, &u.port_info ) ) != 0 )
1372 memcpy ( &port_gid->u.bytes[0], u.port_info.gid_prefix, 8 );
1374 /* GUID info gives us the second half of the port GID */
1375 if ( ( rc = hermon_get_guid_info ( hermon, &u.guid_info ) ) != 0 )
1377 memcpy ( &port_gid->u.bytes[8], u.guid_info.gid_local, 8 );
1382 static int hermon_get_sm_lid ( struct hermon *hermon,
1383 unsigned long *sm_lid ) {
1384 struct ib_mad_port_info port_info;
1387 if ( ( rc = hermon_get_port_info ( hermon, &port_info ) ) != 0 )
1389 *sm_lid = ntohs ( port_info.mastersm_lid );
1393 static int hermon_get_pkey ( struct hermon *hermon, unsigned int *pkey ) {
1394 struct ib_mad_pkey_table pkey_table;
1397 if ( ( rc = hermon_get_pkey_table ( hermon, &pkey_table ) ) != 0 )
1399 *pkey = ntohs ( pkey_table.pkey[0][0] );
1406 * @v hermon Hermon device
1407 * @ret rc Return status code
1409 * This function shouldn't really exist. Unfortunately, IB links take
1410 * a long time to come up, and we can't get various key parameters
1411 * e.g. our own IPoIB MAC address without information from the subnet
1412 * manager). We should eventually make link-up an asynchronous event.
1414 static int hermon_wait_for_link ( struct hermon *hermon ) {
1415 struct ib_mad_port_info port_info;
1416 unsigned int retries;
1419 printf ( "Waiting for Infiniband link-up..." );
1420 for ( retries = 20 ; retries ; retries-- ) {
1421 if ( ( rc = hermon_get_port_info ( hermon,
1422 &port_info ) ) != 0 )
1424 if ( ( ( port_info.port_state__link_speed_supported ) & 0xf )
1432 printf ( "failed\n" );
1437 * Get MAD parameters
1439 * @v hermon Hermon device
1440 * @ret rc Return status code
1442 static int hermon_get_mad_params ( struct ib_device *ibdev ) {
1443 struct hermon *hermon = ibdev->dev_priv;
1446 /* Get subnet manager LID */
1447 if ( ( rc = hermon_get_sm_lid ( hermon, &ibdev->sm_lid ) ) != 0 ) {
1448 DBGC ( hermon, "Hermon %p could not determine subnet manager "
1449 "LID: %s\n", hermon, strerror ( rc ) );
1454 if ( ( rc = hermon_get_port_gid ( hermon, &ibdev->port_gid ) ) != 0 ) {
1455 DBGC ( hermon, "Hermon %p could not determine port GID: %s\n",
1456 hermon, strerror ( rc ) );
1460 /* Get partition key */
1461 if ( ( rc = hermon_get_pkey ( hermon, &ibdev->pkey ) ) != 0 ) {
1462 DBGC ( hermon, "Hermon %p could not determine partition key: "
1463 "%s\n", hermon, strerror ( rc ) );
1470 /***************************************************************************
1474 ***************************************************************************
1478 * Start firmware running
1480 * @v hermon Hermon device
1481 * @ret rc Return status code
1483 static int hermon_start_firmware ( struct hermon *hermon ) {
1484 struct hermonprm_query_fw fw;
1485 struct hermonprm_virtual_physical_mapping map_fa;
1486 unsigned int fw_pages;
1487 unsigned int log2_fw_pages;
1492 /* Get firmware parameters */
1493 if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
1494 DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
1495 hermon, strerror ( rc ) );
1498 DBGC ( hermon, "Hermon %p firmware version %ld.%ld.%ld\n", hermon,
1499 MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
1500 MLX_GET ( &fw, fw_rev_subminor ) );
1501 fw_pages = MLX_GET ( &fw, fw_pages );
1502 log2_fw_pages = fls ( fw_pages - 1 );
1503 fw_pages = ( 1 << log2_fw_pages );
1504 DBGC ( hermon, "Hermon %p requires %d kB for firmware\n",
1505 hermon, ( fw_pages * 4 ) );
1507 /* Allocate firmware pages and map firmware area */
1508 fw_size = ( fw_pages * HERMON_PAGE_SIZE );
1509 hermon->firmware_area = umalloc ( fw_size );
1510 if ( ! hermon->firmware_area ) {
1514 fw_base = ( user_to_phys ( hermon->firmware_area, fw_size ) &
1516 DBGC ( hermon, "Hermon %p firmware area at physical [%lx,%lx)\n",
1517 hermon, fw_base, ( fw_base + fw_size ) );
1518 memset ( &map_fa, 0, sizeof ( map_fa ) );
1519 MLX_FILL_2 ( &map_fa, 3,
1520 log2size, log2_fw_pages,
1521 pa_l, ( fw_base >> 12 ) );
1522 if ( ( rc = hermon_cmd_map_fa ( hermon, &map_fa ) ) != 0 ) {
1523 DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
1524 hermon, strerror ( rc ) );
1528 /* Start firmware */
1529 if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
1530 DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
1531 hermon, strerror ( rc ) );
1535 DBGC ( hermon, "Hermon %p firmware started\n", hermon );
1539 hermon_cmd_unmap_fa ( hermon );
1541 ufree ( hermon->firmware_area );
1542 hermon->firmware_area = UNULL;
1549 * Stop firmware running
1551 * @v hermon Hermon device
1553 static void hermon_stop_firmware ( struct hermon *hermon ) {
1556 if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
1557 DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
1558 hermon, strerror ( rc ) );
1559 /* Leak memory and return; at least we avoid corruption */
1562 ufree ( hermon->firmware_area );
1563 hermon->firmware_area = UNULL;
1566 /***************************************************************************
1568 * Infinihost Context Memory management
1570 ***************************************************************************
1576 * @v hermon Hermon device
1577 * @ret rc Return status code
1579 static int hermon_get_cap ( struct hermon *hermon ) {
1580 struct hermonprm_query_dev_cap dev_cap;
1583 if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
1584 DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
1585 hermon, strerror ( rc ) );
1589 hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
1590 hermon->cap.reserved_qps =
1591 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
1592 hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
1593 hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
1594 hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
1595 hermon->cap.reserved_srqs =
1596 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
1597 hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
1598 hermon->cap.reserved_cqs =
1599 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
1600 hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
1601 hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
1602 hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
1603 hermon->cap.reserved_mtts =
1604 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
1605 hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
1606 hermon->cap.reserved_mrws =
1607 ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
1608 hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
1609 hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
1617 * @v log_num_entries Log2 of the number of entries
1618 * @v entry_size Entry size
1619 * @ret usage Usage size in ICM
1621 static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
1624 usage = ( ( 1 << log_num_entries ) * entry_size );
1625 usage = ( ( usage + HERMON_PAGE_SIZE - 1 ) &
1626 ~( HERMON_PAGE_SIZE - 1 ) );
1633 * @v hermon Hermon device
1634 * @v init_hca INIT_HCA structure to fill in
1635 * @ret rc Return status code
1637 static int hermon_alloc_icm ( struct hermon *hermon,
1638 struct hermonprm_init_hca *init_hca ) {
1639 struct hermonprm_scalar_parameter icm_size;
1640 struct hermonprm_scalar_parameter icm_aux_size;
1641 struct hermonprm_virtual_physical_mapping map_icm_aux;
1642 struct hermonprm_virtual_physical_mapping map_icm;
1643 uint64_t icm_offset = 0;
1644 unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
1645 unsigned int log_num_mtts, log_num_mpts;
1646 size_t cmpt_max_len;
1647 size_t qp_cmpt_len, srq_cmpt_len, cq_cmpt_len, eq_cmpt_len;
1648 size_t icm_len, icm_aux_len;
1649 physaddr_t icm_phys;
1654 * Start by carving up the ICM virtual address space
1658 /* Calculate number of each object type within ICM */
1659 log_num_qps = fls ( hermon->cap.reserved_qps + HERMON_MAX_QPS - 1 );
1660 log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
1661 log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
1662 log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
1663 log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
1665 /* ICM starts with the cMPT tables, which are sparse */
1666 cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
1667 ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
1668 qp_cmpt_len = icm_usage ( log_num_qps, hermon->cap.cmpt_entry_size );
1669 hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
1670 hermon->icm_map[HERMON_ICM_QP_CMPT].len = qp_cmpt_len;
1671 icm_offset += cmpt_max_len;
1672 srq_cmpt_len = icm_usage ( log_num_srqs, hermon->cap.cmpt_entry_size );
1673 hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
1674 hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = srq_cmpt_len;
1675 icm_offset += cmpt_max_len;
1676 cq_cmpt_len = icm_usage ( log_num_cqs, hermon->cap.cmpt_entry_size );
1677 hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
1678 hermon->icm_map[HERMON_ICM_CQ_CMPT].len = cq_cmpt_len;
1679 icm_offset += cmpt_max_len;
1680 eq_cmpt_len = icm_usage ( log_num_eqs, hermon->cap.cmpt_entry_size );
1681 hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
1682 hermon->icm_map[HERMON_ICM_EQ_CMPT].len = eq_cmpt_len;
1683 icm_offset += cmpt_max_len;
1685 hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
1687 /* Queue pair contexts */
1688 MLX_FILL_1 ( init_hca, 12,
1689 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
1690 ( icm_offset >> 32 ) );
1691 MLX_FILL_2 ( init_hca, 13,
1692 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
1693 ( icm_offset >> 5 ),
1694 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
1696 DBGC ( hermon, "Hermon %p ICM QPC base = %llx\n", hermon, icm_offset );
1697 icm_offset += icm_usage ( log_num_qps, hermon->cap.qpc_entry_size );
1699 /* Extended alternate path contexts */
1700 MLX_FILL_1 ( init_hca, 24,
1701 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
1702 ( icm_offset >> 32 ) );
1703 MLX_FILL_1 ( init_hca, 25,
1704 qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
1706 DBGC ( hermon, "Hermon %p ICM ALTC base = %llx\n", hermon, icm_offset);
1707 icm_offset += icm_usage ( log_num_qps,
1708 hermon->cap.altc_entry_size );
1710 /* Extended auxiliary contexts */
1711 MLX_FILL_1 ( init_hca, 28,
1712 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
1713 ( icm_offset >> 32 ) );
1714 MLX_FILL_1 ( init_hca, 29,
1715 qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
1717 DBGC ( hermon, "Hermon %p ICM AUXC base = %llx\n", hermon, icm_offset);
1718 icm_offset += icm_usage ( log_num_qps,
1719 hermon->cap.auxc_entry_size );
1721 /* Shared receive queue contexts */
1722 MLX_FILL_1 ( init_hca, 18,
1723 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
1724 ( icm_offset >> 32 ) );
1725 MLX_FILL_2 ( init_hca, 19,
1726 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
1727 ( icm_offset >> 5 ),
1728 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
1730 DBGC ( hermon, "Hermon %p ICM SRQC base = %llx\n", hermon, icm_offset);
1731 icm_offset += icm_usage ( log_num_srqs,
1732 hermon->cap.srqc_entry_size );
1734 /* Completion queue contexts */
1735 MLX_FILL_1 ( init_hca, 20,
1736 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
1737 ( icm_offset >> 32 ) );
1738 MLX_FILL_2 ( init_hca, 21,
1739 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
1740 ( icm_offset >> 5 ),
1741 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
1743 DBGC ( hermon, "Hermon %p ICM CQC base = %llx\n", hermon, icm_offset );
1744 icm_offset += icm_usage ( log_num_cqs, hermon->cap.cqc_entry_size );
1746 /* Event queue contexts */
1747 MLX_FILL_1 ( init_hca, 32,
1748 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
1749 ( icm_offset >> 32 ) );
1750 MLX_FILL_2 ( init_hca, 33,
1751 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
1752 ( icm_offset >> 5 ),
1753 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
1755 DBGC ( hermon, "Hermon %p ICM EQC base = %llx\n", hermon, icm_offset );
1756 icm_offset += icm_usage ( log_num_eqs, hermon->cap.eqc_entry_size );
1758 /* Memory translation table */
1759 MLX_FILL_1 ( init_hca, 64,
1760 tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
1761 MLX_FILL_1 ( init_hca, 65,
1762 tpt_parameters.mtt_base_addr_l, icm_offset );
1763 DBGC ( hermon, "Hermon %p ICM MTT base = %llx\n", hermon, icm_offset );
1764 icm_offset += icm_usage ( log_num_mtts,
1765 hermon->cap.mtt_entry_size );
1767 /* Memory protection table */
1768 log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
1769 MLX_FILL_1 ( init_hca, 60,
1770 tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
1771 MLX_FILL_1 ( init_hca, 61,
1772 tpt_parameters.dmpt_base_adr_l, icm_offset );
1773 MLX_FILL_1 ( init_hca, 62,
1774 tpt_parameters.log_dmpt_sz, log_num_mpts );
1775 DBGC ( hermon, "Hermon %p ICM DMPT base = %llx\n", hermon, icm_offset);
1776 icm_offset += icm_usage ( log_num_mpts,
1777 hermon->cap.dmpt_entry_size );
1779 /* Multicast table */
1780 MLX_FILL_1 ( init_hca, 48,
1781 multicast_parameters.mc_base_addr_h,
1782 ( icm_offset >> 32 ) );
1783 MLX_FILL_1 ( init_hca, 49,
1784 multicast_parameters.mc_base_addr_l, icm_offset );
1785 MLX_FILL_1 ( init_hca, 52,
1786 multicast_parameters.log_mc_table_entry_sz,
1787 fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
1788 MLX_FILL_1 ( init_hca, 53,
1789 multicast_parameters.log_mc_table_hash_sz, 3 );
1790 MLX_FILL_1 ( init_hca, 54,
1791 multicast_parameters.log_mc_table_sz, 3 );
1792 DBGC ( hermon, "Hermon %p ICM MC base = %llx\n", hermon, icm_offset );
1793 icm_offset += ( ( 8 * sizeof ( struct hermonprm_mcg_entry ) +
1794 HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
1796 hermon->icm_map[HERMON_ICM_OTHER].len =
1797 ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
1800 * Allocate and map physical memory for (portions of) ICM
1803 * ICM AUX area (aligned to its own size)
1808 /* Calculate physical memory required for ICM */
1810 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
1811 icm_len += hermon->icm_map[i].len;
1814 /* Get ICM auxiliary area size */
1815 memset ( &icm_size, 0, sizeof ( icm_size ) );
1816 MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
1817 MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
1818 if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
1819 &icm_aux_size ) ) != 0 ) {
1820 DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
1821 hermon, strerror ( rc ) );
1822 goto err_set_icm_size;
1824 icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
1825 /* Must round up to nearest power of two :( */
1826 icm_aux_len = ( 1 << fls ( icm_aux_len - 1 ) );
1828 /* Allocate ICM data and auxiliary area */
1829 DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
1830 hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
1831 hermon->icm = umalloc ( 2 * icm_aux_len + icm_len );
1832 if ( ! hermon->icm ) {
1836 icm_phys = user_to_phys ( hermon->icm, 0 );
1838 /* Map ICM auxiliary area */
1839 icm_phys = ( ( icm_phys + icm_aux_len - 1 ) & ~( icm_aux_len - 1 ) );
1840 memset ( &map_icm_aux, 0, sizeof ( map_icm_aux ) );
1841 MLX_FILL_2 ( &map_icm_aux, 3,
1842 log2size, fls ( ( icm_aux_len / HERMON_PAGE_SIZE ) - 1 ),
1843 pa_l, ( icm_phys >> 12 ) );
1844 DBGC ( hermon, "Hermon %p mapping ICM AUX (2^%d pages) => %08lx\n",
1845 hermon, fls ( ( icm_aux_len / HERMON_PAGE_SIZE ) - 1 ),
1847 if ( ( rc = hermon_cmd_map_icm_aux ( hermon, &map_icm_aux ) ) != 0 ) {
1848 DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
1849 hermon, strerror ( rc ) );
1850 goto err_map_icm_aux;
1852 icm_phys += icm_aux_len;
1855 for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
1856 memset ( &map_icm, 0, sizeof ( map_icm ) );
1857 MLX_FILL_1 ( &map_icm, 0,
1858 va_h, ( hermon->icm_map[i].offset >> 32 ) );
1859 MLX_FILL_1 ( &map_icm, 1,
1860 va_l, ( hermon->icm_map[i].offset >> 12 ) );
1861 MLX_FILL_2 ( &map_icm, 3,
1863 fls ( ( hermon->icm_map[i].len /
1864 HERMON_PAGE_SIZE ) - 1 ),
1865 pa_l, ( icm_phys >> 12 ) );
1866 DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx (2^%d pages) "
1867 "=> %08lx\n", hermon, hermon->icm_map[i].offset,
1868 hermon->icm_map[i].len,
1869 fls ( ( hermon->icm_map[i].len /
1870 HERMON_PAGE_SIZE ) - 1 ), icm_phys );
1871 if ( ( rc = hermon_cmd_map_icm ( hermon, &map_icm ) ) != 0 ) {
1872 DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
1873 hermon, strerror ( rc ) );
1876 icm_phys += hermon->icm_map[i].len;
1882 assert ( i == 0 ); /* We don't handle partial failure at present */
1883 hermon_cmd_unmap_icm_aux ( hermon );
1885 ufree ( hermon->icm );
1886 hermon->icm = UNULL;
1895 * @v hermon Hermon device
1897 static void hermon_free_icm ( struct hermon *hermon ) {
1898 struct hermonprm_scalar_parameter unmap_icm;
1901 for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
1902 memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
1903 MLX_FILL_1 ( &unmap_icm, 0, value_hi,
1904 ( hermon->icm_map[i].offset >> 32 ) );
1905 MLX_FILL_1 ( &unmap_icm, 1, value,
1906 hermon->icm_map[i].offset );
1907 hermon_cmd_unmap_icm ( hermon,
1908 ( 1 << fls ( ( hermon->icm_map[i].len /
1909 HERMON_PAGE_SIZE ) - 1)),
1912 hermon_cmd_unmap_icm_aux ( hermon );
1913 ufree ( hermon->icm );
1914 hermon->icm = UNULL;
1917 /***************************************************************************
1919 * Infiniband link-layer operations
1921 ***************************************************************************
1925 * Initialise Infiniband link
1927 * @v hermon Hermon device
1928 * @ret rc Return status code
1930 static int hermon_init_port ( struct hermon *hermon ) {
1931 struct hermonprm_init_port init_port;
1934 memset ( &init_port, 0, sizeof ( init_port ) );
1935 MLX_FILL_2 ( &init_port, 0,
1938 MLX_FILL_2 ( &init_port, 1,
1939 mtu, HERMON_MTU_2048,
1941 MLX_FILL_1 ( &init_port, 2, max_pkey, 64 );
1942 if ( ( rc = hermon_cmd_init_port ( hermon, PXE_IB_PORT,
1943 &init_port ) ) != 0 ) {
1944 DBGC ( hermon, "Hermon %p could not intialise port: %s\n",
1945 hermon, strerror ( rc ) );
1953 * Close Infiniband link
1955 * @v hermon Hermon device
1957 static void hermon_close_port ( struct hermon *hermon ) {
1960 if ( ( rc = hermon_cmd_close_port ( hermon, PXE_IB_PORT ) ) != 0 ) {
1961 DBGC ( hermon, "Hermon %p could not close port: %s\n",
1962 hermon, strerror ( rc ) );
1963 /* Nothing we can do about this */
1967 /***************************************************************************
1971 ***************************************************************************
1975 * Set up memory protection table
1977 * @v hermon Hermon device
1978 * @ret rc Return status code
1980 static int hermon_setup_mpt ( struct hermon *hermon ) {
1981 struct hermonprm_mpt mpt;
1986 key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
1987 hermon->reserved_lkey = ( ( key << 8 ) | ( key >> 24 ) );
1989 /* Initialise memory protection table */
1990 memset ( &mpt, 0, sizeof ( mpt ) );
1991 MLX_FILL_4 ( &mpt, 0,
1996 MLX_FILL_1 ( &mpt, 2, mem_key, key );
1997 MLX_FILL_1 ( &mpt, 3, pd, HERMON_GLOBAL_PD );
1998 MLX_FILL_1 ( &mpt, 10, len64, 1 );
1999 if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
2000 hermon->cap.reserved_mrws,
2002 DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
2003 hermon, strerror ( rc ) );
2015 * @ret rc Return status code
2017 static int hermon_probe ( struct pci_device *pci,
2018 const struct pci_device_id *id __unused ) {
2019 struct ib_device *ibdev;
2020 struct hermon *hermon;
2021 struct hermonprm_init_hca init_hca;
2024 /* Allocate Infiniband device */
2025 ibdev = alloc_ibdev ( sizeof ( *hermon ) );
2030 ibdev->op = &hermon_ib_operations;
2031 pci_set_drvdata ( pci, ibdev );
2032 ibdev->dev = &pci->dev;
2033 hermon = ibdev->dev_priv;
2034 memset ( hermon, 0, sizeof ( *hermon ) );
2036 /* Fix up PCI device */
2037 adjust_pci_device ( pci );
2040 hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR),
2041 HERMON_PCI_CONFIG_BAR_SIZE );
2042 hermon->uar = ioremap ( ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ) +
2043 HERMON_UAR_PAGE * HERMON_PAGE_SIZE ),
2046 /* Allocate space for mailboxes */
2047 hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
2048 HERMON_MBOX_ALIGN );
2049 if ( ! hermon->mailbox_in ) {
2051 goto err_mailbox_in;
2053 hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
2054 HERMON_MBOX_ALIGN );
2055 if ( ! hermon->mailbox_out ) {
2057 goto err_mailbox_out;
2060 /* Start firmware */
2061 if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
2062 goto err_start_firmware;
2064 /* Get device limits */
2065 if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
2069 memset ( &init_hca, 0, sizeof ( init_hca ) );
2070 if ( ( rc = hermon_alloc_icm ( hermon, &init_hca ) ) != 0 )
2073 /* Initialise HCA */
2074 MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
2075 MLX_FILL_1 ( &init_hca, 5, udp, 1 );
2076 MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
2077 if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
2078 DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
2079 hermon, strerror ( rc ) );
2083 /* Set up memory protection */
2084 if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
2087 /* Bring up IB layer */
2088 if ( ( rc = hermon_init_port ( hermon ) ) != 0 )
2092 if ( ( rc = hermon_wait_for_link ( hermon ) ) != 0 )
2093 goto err_wait_for_link;
2095 /* Get MAD parameters */
2096 if ( ( rc = hermon_get_mad_params ( ibdev ) ) != 0 )
2097 goto err_get_mad_params;
2099 DBGC ( hermon, "Hermon %p port GID is %08lx:%08lx:%08lx:%08lx\n",
2100 hermon, htonl ( ibdev->port_gid.u.dwords[0] ),
2101 htonl ( ibdev->port_gid.u.dwords[1] ),
2102 htonl ( ibdev->port_gid.u.dwords[2] ),
2103 htonl ( ibdev->port_gid.u.dwords[3] ) );
2105 /* Add IPoIB device */
2106 if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
2107 DBGC ( hermon, "Hermon %p could not add IPoIB device: %s\n",
2108 hermon, strerror ( rc ) );
2109 goto err_ipoib_probe;
2117 hermon_close_port ( hermon );
2120 hermon_cmd_close_hca ( hermon );
2122 hermon_free_icm ( hermon );
2125 hermon_stop_firmware ( hermon );
2127 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2129 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2131 free_ibdev ( ibdev );
2141 static void hermon_remove ( struct pci_device *pci ) {
2142 struct ib_device *ibdev = pci_get_drvdata ( pci );
2143 struct hermon *hermon = ibdev->dev_priv;
2145 ipoib_remove ( ibdev );
2146 hermon_close_port ( hermon );
2147 hermon_cmd_close_hca ( hermon );
2148 hermon_free_icm ( hermon );
2149 hermon_stop_firmware ( hermon );
2150 hermon_stop_firmware ( hermon );
2151 free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
2152 free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
2153 free_ibdev ( ibdev );
2156 static struct pci_device_id hermon_nics[] = {
2157 PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver" ),
2158 PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver" ),
2161 struct pci_driver hermon_driver __pci_driver = {
2163 .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
2164 .probe = hermon_probe,
2165 .remove = hermon_remove,