~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/ipxe/src/drivers/infiniband/hermon.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
 
3
 * Copyright (C) 2008 Mellanox Technologies Ltd.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
 * 02110-1301, USA.
 
19
 */
 
20
 
 
21
FILE_LICENCE ( GPL2_OR_LATER );
 
22
 
 
23
#include <stdint.h>
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <strings.h>
 
28
#include <unistd.h>
 
29
#include <errno.h>
 
30
#include <byteswap.h>
 
31
#include <ipxe/io.h>
 
32
#include <ipxe/pci.h>
 
33
#include <ipxe/pcibackup.h>
 
34
#include <ipxe/malloc.h>
 
35
#include <ipxe/umalloc.h>
 
36
#include <ipxe/iobuf.h>
 
37
#include <ipxe/netdevice.h>
 
38
#include <ipxe/infiniband.h>
 
39
#include <ipxe/ib_smc.h>
 
40
#include <ipxe/if_ether.h>
 
41
#include <ipxe/ethernet.h>
 
42
#include <ipxe/fcoe.h>
 
43
#include <ipxe/vlan.h>
 
44
#include <ipxe/bofm.h>
 
45
#include <ipxe/nvsvpd.h>
 
46
#include <ipxe/nvo.h>
 
47
#include "hermon.h"
 
48
 
 
49
/**
 
50
 * @file
 
51
 *
 
52
 * Mellanox Hermon Infiniband HCA
 
53
 *
 
54
 */
 
55
 
 
56
/***************************************************************************
 
57
 *
 
58
 * Queue number allocation
 
59
 *
 
60
 ***************************************************************************
 
61
 */
 
62
 
 
63
/**
 
64
 * Allocate offsets within usage bitmask
 
65
 *
 
66
 * @v bits              Usage bitmask
 
67
 * @v bits_len          Length of usage bitmask
 
68
 * @v num_bits          Number of contiguous bits to allocate within bitmask
 
69
 * @ret bit             First free bit within bitmask, or negative error
 
70
 */
 
71
static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
 
72
                                  unsigned int bits_len,
 
73
                                  unsigned int num_bits ) {
 
74
        unsigned int bit = 0;
 
75
        hermon_bitmask_t mask = 1;
 
76
        unsigned int found = 0;
 
77
 
 
78
        /* Search bits for num_bits contiguous free bits */
 
79
        while ( bit < bits_len ) {
 
80
                if ( ( mask & *bits ) == 0 ) {
 
81
                        if ( ++found == num_bits )
 
82
                                goto found;
 
83
                } else {
 
84
                        found = 0;
 
85
                }
 
86
                bit++;
 
87
                mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
 
88
                if ( mask == 1 )
 
89
                        bits++;
 
90
        }
 
91
        return -ENFILE;
 
92
 
 
93
 found:
 
94
        /* Mark bits as in-use */
 
95
        do {
 
96
                *bits |= mask;
 
97
                if ( mask == 1 )
 
98
                        bits--;
 
99
                mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
 
100
        } while ( --found );
 
101
 
 
102
        return ( bit - num_bits + 1 );
 
103
}
 
104
 
 
105
/**
 
106
 * Free offsets within usage bitmask
 
107
 *
 
108
 * @v bits              Usage bitmask
 
109
 * @v bit               Starting bit within bitmask
 
110
 * @v num_bits          Number of contiguous bits to free within bitmask
 
111
 */
 
112
static void hermon_bitmask_free ( hermon_bitmask_t *bits,
 
113
                                  int bit, unsigned int num_bits ) {
 
114
        hermon_bitmask_t mask;
 
115
 
 
116
        for ( ; num_bits ; bit++, num_bits-- ) {
 
117
                mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
 
118
                bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
 
119
        }
 
120
}
 
121
 
 
122
/***************************************************************************
 
123
 *
 
124
 * HCA commands
 
125
 *
 
126
 ***************************************************************************
 
127
 */
 
128
 
 
129
/**
 
130
 * Wait for Hermon command completion
 
131
 *
 
132
 * @v hermon            Hermon device
 
133
 * @v hcr               HCA command registers
 
134
 * @ret rc              Return status code
 
135
 */
 
136
static int hermon_cmd_wait ( struct hermon *hermon,
 
137
                             struct hermonprm_hca_command_register *hcr ) {
 
138
        unsigned int wait;
 
139
 
 
140
        for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
 
141
                hcr->u.dwords[6] =
 
142
                        readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
 
143
                if ( ( MLX_GET ( hcr, go ) == 0 ) &&
 
144
                     ( MLX_GET ( hcr, t ) == hermon->toggle ) )
 
145
                        return 0;
 
146
                mdelay ( 1 );
 
147
        }
 
148
        return -EBUSY;
 
149
}
 
150
 
 
151
/**
 
152
 * Issue HCA command
 
153
 *
 
154
 * @v hermon            Hermon device
 
155
 * @v command           Command opcode, flags and input/output lengths
 
156
 * @v op_mod            Opcode modifier (0 if no modifier applicable)
 
157
 * @v in                Input parameters
 
158
 * @v in_mod            Input modifier (0 if no modifier applicable)
 
159
 * @v out               Output parameters
 
160
 * @ret rc              Return status code
 
161
 */
 
162
static int hermon_cmd ( struct hermon *hermon, unsigned long command,
 
163
                        unsigned int op_mod, const void *in,
 
164
                        unsigned int in_mod, void *out ) {
 
165
        struct hermonprm_hca_command_register hcr;
 
166
        unsigned int opcode = HERMON_HCR_OPCODE ( command );
 
167
        size_t in_len = HERMON_HCR_IN_LEN ( command );
 
168
        size_t out_len = HERMON_HCR_OUT_LEN ( command );
 
169
        void *in_buffer;
 
170
        void *out_buffer;
 
171
        unsigned int status;
 
172
        unsigned int i;
 
173
        int rc;
 
174
 
 
175
        assert ( in_len <= HERMON_MBOX_SIZE );
 
176
        assert ( out_len <= HERMON_MBOX_SIZE );
 
177
 
 
178
        DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
 
179
                hermon, opcode, in_len,
 
180
                ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
 
181
                ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
 
182
 
 
183
        /* Check that HCR is free */
 
184
        if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
 
185
                DBGC ( hermon, "Hermon %p command interface locked\n",
 
186
                       hermon );
 
187
                return rc;
 
188
        }
 
189
 
 
190
        /* Flip HCR toggle */
 
191
        hermon->toggle = ( 1 - hermon->toggle );
 
192
 
 
193
        /* Prepare HCR */
 
194
        memset ( &hcr, 0, sizeof ( hcr ) );
 
195
        in_buffer = &hcr.u.dwords[0];
 
196
        if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
 
197
                memset ( hermon->mailbox_in, 0, HERMON_MBOX_SIZE );
 
198
                in_buffer = hermon->mailbox_in;
 
199
                MLX_FILL_H ( &hcr, 0, in_param_h, virt_to_bus ( in_buffer ) );
 
200
                MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
 
201
        }
 
202
        memcpy ( in_buffer, in, in_len );
 
203
        MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
 
204
        out_buffer = &hcr.u.dwords[3];
 
205
        if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
 
206
                out_buffer = hermon->mailbox_out;
 
207
                MLX_FILL_H ( &hcr, 3, out_param_h,
 
208
                             virt_to_bus ( out_buffer ) );
 
209
                MLX_FILL_1 ( &hcr, 4, out_param_l,
 
210
                             virt_to_bus ( out_buffer ) );
 
211
        }
 
212
        MLX_FILL_4 ( &hcr, 6,
 
213
                     opcode, opcode,
 
214
                     opcode_modifier, op_mod,
 
215
                     go, 1,
 
216
                     t, hermon->toggle );
 
217
        DBGC ( hermon, "Hermon %p issuing command %04x\n",
 
218
               hermon, opcode );
 
219
        DBGC2_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
 
220
                    &hcr, sizeof ( hcr ) );
 
221
        if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
 
222
                DBGC2 ( hermon, "Input mailbox:\n" );
 
223
                DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
 
224
                            ( ( in_len < 512 ) ? in_len : 512 ) );
 
225
        }
 
226
 
 
227
        /* Issue command */
 
228
        for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
 
229
              i++ ) {
 
230
                writel ( hcr.u.dwords[i],
 
231
                         hermon->config + HERMON_HCR_REG ( i ) );
 
232
                barrier();
 
233
        }
 
234
 
 
235
        /* Wait for command completion */
 
236
        if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
 
237
                DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
 
238
                       hermon );
 
239
                DBGC_HDA ( hermon,
 
240
                           virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
 
241
                           &hcr, sizeof ( hcr ) );
 
242
                return rc;
 
243
        }
 
244
 
 
245
        /* Check command status */
 
246
        status = MLX_GET ( &hcr, status );
 
247
        if ( status != 0 ) {
 
248
                DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
 
249
                       hermon, status );
 
250
                DBGC_HDA ( hermon,
 
251
                           virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
 
252
                           &hcr, sizeof ( hcr ) );
 
253
                return -EIO;
 
254
        }
 
255
 
 
256
        /* Read output parameters, if any */
 
257
        hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
 
258
        hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
 
259
        memcpy ( out, out_buffer, out_len );
 
260
        if ( out_len ) {
 
261
                DBGC2 ( hermon, "Output%s:\n",
 
262
                        ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
 
263
                DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
 
264
                            ( ( out_len < 512 ) ? out_len : 512 ) );
 
265
        }
 
266
 
 
267
        return 0;
 
268
}
 
269
 
 
270
static inline int
 
271
hermon_cmd_query_dev_cap ( struct hermon *hermon,
 
272
                           struct hermonprm_query_dev_cap *dev_cap ) {
 
273
        return hermon_cmd ( hermon,
 
274
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
 
275
                                                 1, sizeof ( *dev_cap ) ),
 
276
                            0, NULL, 0, dev_cap );
 
277
}
 
278
 
 
279
static inline int
 
280
hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
 
281
        return hermon_cmd ( hermon,
 
282
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
 
283
                                                 1, sizeof ( *fw ) ),
 
284
                            0, NULL, 0, fw );
 
285
}
 
286
 
 
287
static inline int
 
288
hermon_cmd_init_hca ( struct hermon *hermon,
 
289
                      const struct hermonprm_init_hca *init_hca ) {
 
290
        return hermon_cmd ( hermon,
 
291
                            HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
 
292
                                                1, sizeof ( *init_hca ) ),
 
293
                            0, init_hca, 0, NULL );
 
294
}
 
295
 
 
296
static inline int
 
297
hermon_cmd_close_hca ( struct hermon *hermon ) {
 
298
        return hermon_cmd ( hermon,
 
299
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
 
300
                            0, NULL, 0, NULL );
 
301
}
 
302
 
 
303
static inline int
 
304
hermon_cmd_init_port ( struct hermon *hermon, unsigned int port ) {
 
305
        return hermon_cmd ( hermon,
 
306
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_INIT_PORT ),
 
307
                            0, NULL, port, NULL );
 
308
}
 
309
 
 
310
static inline int
 
311
hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
 
312
        return hermon_cmd ( hermon,
 
313
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
 
314
                            0, NULL, port, NULL );
 
315
}
 
316
 
 
317
static inline int
 
318
hermon_cmd_set_port ( struct hermon *hermon, int is_ethernet,
 
319
                      unsigned int port_selector,
 
320
                      const union hermonprm_set_port *set_port ) {
 
321
        return hermon_cmd ( hermon,
 
322
                            HERMON_HCR_IN_CMD ( HERMON_HCR_SET_PORT,
 
323
                                                1, sizeof ( *set_port ) ),
 
324
                            is_ethernet, set_port, port_selector, NULL );
 
325
}
 
326
 
 
327
static inline int
 
328
hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
 
329
                       const struct hermonprm_mpt *mpt ) {
 
330
        return hermon_cmd ( hermon,
 
331
                            HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
 
332
                                                1, sizeof ( *mpt ) ),
 
333
                            0, mpt, index, NULL );
 
334
}
 
335
 
 
336
static inline int
 
337
hermon_cmd_write_mtt ( struct hermon *hermon,
 
338
                       const struct hermonprm_write_mtt *write_mtt ) {
 
339
        return hermon_cmd ( hermon,
 
340
                            HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
 
341
                                                1, sizeof ( *write_mtt ) ),
 
342
                            0, write_mtt, 1, NULL );
 
343
}
 
344
 
 
345
static inline int
 
346
hermon_cmd_map_eq ( struct hermon *hermon, unsigned long index_map,
 
347
                    const struct hermonprm_event_mask *mask ) {
 
348
        return hermon_cmd ( hermon,
 
349
                            HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_EQ,
 
350
                                                0, sizeof ( *mask ) ),
 
351
                            0, mask, index_map, NULL );
 
352
}
 
353
 
 
354
static inline int
 
355
hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
 
356
                      const struct hermonprm_eqc *eqctx ) {
 
357
        return hermon_cmd ( hermon,
 
358
                            HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
 
359
                                                1, sizeof ( *eqctx ) ),
 
360
                            0, eqctx, index, NULL );
 
361
}
 
362
 
 
363
static inline int
 
364
hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index,
 
365
                      struct hermonprm_eqc *eqctx ) {
 
366
        return hermon_cmd ( hermon,
 
367
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_EQ,
 
368
                                                 1, sizeof ( *eqctx ) ),
 
369
                            1, NULL, index, eqctx );
 
370
}
 
371
 
 
372
static inline int
 
373
hermon_cmd_query_eq ( struct hermon *hermon, unsigned int index,
 
374
                      struct hermonprm_eqc *eqctx ) {
 
375
        return hermon_cmd ( hermon,
 
376
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_EQ,
 
377
                                                 1, sizeof ( *eqctx ) ),
 
378
                            0, NULL, index, eqctx );
 
379
}
 
380
 
 
381
static inline int
 
382
hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
 
383
                      const struct hermonprm_completion_queue_context *cqctx ){
 
384
        return hermon_cmd ( hermon,
 
385
                            HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
 
386
                                                1, sizeof ( *cqctx ) ),
 
387
                            0, cqctx, cqn, NULL );
 
388
}
 
389
 
 
390
static inline int
 
391
hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
 
392
                      struct hermonprm_completion_queue_context *cqctx ) {
 
393
        return hermon_cmd ( hermon,
 
394
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
 
395
                                                 1, sizeof ( *cqctx ) ),
 
396
                            0, NULL, cqn, cqctx );
 
397
}
 
398
 
 
399
static inline int
 
400
hermon_cmd_query_cq ( struct hermon *hermon, unsigned long cqn,
 
401
                      struct hermonprm_completion_queue_context *cqctx ) {
 
402
        return hermon_cmd ( hermon,
 
403
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_CQ,
 
404
                                                 1, sizeof ( *cqctx ) ),
 
405
                            0, NULL, cqn, cqctx );
 
406
}
 
407
 
 
408
static inline int
 
409
hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
 
410
                         const struct hermonprm_qp_ee_state_transitions *ctx ){
 
411
        return hermon_cmd ( hermon,
 
412
                            HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
 
413
                                                1, sizeof ( *ctx ) ),
 
414
                            0, ctx, qpn, NULL );
 
415
}
 
416
 
 
417
static inline int
 
418
hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
 
419
                         const struct hermonprm_qp_ee_state_transitions *ctx ){
 
420
        return hermon_cmd ( hermon,
 
421
                            HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
 
422
                                                1, sizeof ( *ctx ) ),
 
423
                            0, ctx, qpn, NULL );
 
424
}
 
425
 
 
426
static inline int
 
427
hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
 
428
                        const struct hermonprm_qp_ee_state_transitions *ctx ) {
 
429
        return hermon_cmd ( hermon,
 
430
                            HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
 
431
                                                1, sizeof ( *ctx ) ),
 
432
                            0, ctx, qpn, NULL );
 
433
}
 
434
 
 
435
static inline int
 
436
hermon_cmd_rts2rts_qp ( struct hermon *hermon, unsigned long qpn,
 
437
                        const struct hermonprm_qp_ee_state_transitions *ctx ) {
 
438
        return hermon_cmd ( hermon,
 
439
                            HERMON_HCR_IN_CMD ( HERMON_HCR_RTS2RTS_QP,
 
440
                                                1, sizeof ( *ctx ) ),
 
441
                            0, ctx, qpn, NULL );
 
442
}
 
443
 
 
444
static inline int
 
445
hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
 
446
        return hermon_cmd ( hermon,
 
447
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
 
448
                            0x03, NULL, qpn, NULL );
 
449
}
 
450
 
 
451
static inline int
 
452
hermon_cmd_query_qp ( struct hermon *hermon, unsigned long qpn,
 
453
                      struct hermonprm_qp_ee_state_transitions *ctx ) {
 
454
        return hermon_cmd ( hermon,
 
455
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_QP,
 
456
                                                 1, sizeof ( *ctx ) ),
 
457
                            0, NULL, qpn, ctx );
 
458
}
 
459
 
 
460
static inline int
 
461
hermon_cmd_conf_special_qp ( struct hermon *hermon, unsigned int internal_qps,
 
462
                             unsigned long base_qpn ) {
 
463
        return hermon_cmd ( hermon,
 
464
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_CONF_SPECIAL_QP ),
 
465
                            internal_qps, NULL, base_qpn, NULL );
 
466
}
 
467
 
 
468
static inline int
 
469
hermon_cmd_mad_ifc ( struct hermon *hermon, unsigned int port,
 
470
                     union hermonprm_mad *mad ) {
 
471
        return hermon_cmd ( hermon,
 
472
                            HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
 
473
                                                   1, sizeof ( *mad ),
 
474
                                                   1, sizeof ( *mad ) ),
 
475
                            0x03, mad, port, mad );
 
476
}
 
477
 
 
478
static inline int
 
479
hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
 
480
                      struct hermonprm_mcg_entry *mcg ) {
 
481
        return hermon_cmd ( hermon,
 
482
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
 
483
                                                 1, sizeof ( *mcg ) ),
 
484
                            0, NULL, index, mcg );
 
485
}
 
486
 
 
487
static inline int
 
488
hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
 
489
                       const struct hermonprm_mcg_entry *mcg ) {
 
490
        return hermon_cmd ( hermon,
 
491
                            HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
 
492
                                                1, sizeof ( *mcg ) ),
 
493
                            0, mcg, index, NULL );
 
494
}
 
495
 
 
496
static inline int
 
497
hermon_cmd_mgid_hash ( struct hermon *hermon, const union ib_gid *gid,
 
498
                       struct hermonprm_mgm_hash *hash ) {
 
499
        return hermon_cmd ( hermon,
 
500
                            HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
 
501
                                                   1, sizeof ( *gid ),
 
502
                                                   0, sizeof ( *hash ) ),
 
503
                            0, gid, 0, hash );
 
504
}
 
505
 
 
506
static inline int
 
507
hermon_cmd_mod_stat_cfg ( struct hermon *hermon, unsigned int mode,
 
508
                          unsigned int input_mod,
 
509
                          struct hermonprm_scalar_parameter *portion ) {
 
510
        return hermon_cmd ( hermon,
 
511
                            HERMON_HCR_INOUT_CMD ( HERMON_HCR_MOD_STAT_CFG,
 
512
                                                   0, sizeof ( *portion ),
 
513
                                                   0, sizeof ( *portion ) ),
 
514
                            mode, portion, input_mod, portion );
 
515
}
 
516
 
 
517
static inline int
 
518
hermon_cmd_query_port ( struct hermon *hermon, unsigned int port,
 
519
                        struct hermonprm_query_port_cap *query_port ) {
 
520
        return hermon_cmd ( hermon,
 
521
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_PORT,
 
522
                                                 1, sizeof ( *query_port ) ),
 
523
                            0, NULL, port, query_port );
 
524
}
 
525
 
 
526
static inline int
 
527
hermon_cmd_sense_port ( struct hermon *hermon, unsigned int port,
 
528
                        struct hermonprm_sense_port *port_type ) {
 
529
        return hermon_cmd ( hermon,
 
530
                            HERMON_HCR_OUT_CMD ( HERMON_HCR_SENSE_PORT,
 
531
                                                 0, sizeof ( *port_type ) ),
 
532
                            0, NULL, port, port_type );
 
533
}
 
534
 
 
535
static inline int
 
536
hermon_cmd_run_fw ( struct hermon *hermon ) {
 
537
        return hermon_cmd ( hermon,
 
538
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
 
539
                            0, NULL, 0, NULL );
 
540
}
 
541
 
 
542
static inline int
 
543
hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
 
544
                       const struct hermonprm_scalar_parameter *offset ) {
 
545
        return hermon_cmd ( hermon,
 
546
                            HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
 
547
                                                0, sizeof ( *offset ) ),
 
548
                            0, offset, page_count, NULL );
 
549
}
 
550
 
 
551
static inline int
 
552
hermon_cmd_map_icm ( struct hermon *hermon,
 
553
                     const struct hermonprm_virtual_physical_mapping *map ) {
 
554
        return hermon_cmd ( hermon,
 
555
                            HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
 
556
                                                1, sizeof ( *map ) ),
 
557
                            0, map, 1, NULL );
 
558
}
 
559
 
 
560
static inline int
 
561
hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
 
562
        return hermon_cmd ( hermon,
 
563
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
 
564
                            0, NULL, 0, NULL );
 
565
}
 
566
 
 
567
static inline int
 
568
hermon_cmd_map_icm_aux ( struct hermon *hermon,
 
569
                       const struct hermonprm_virtual_physical_mapping *map ) {
 
570
        return hermon_cmd ( hermon,
 
571
                            HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
 
572
                                                1, sizeof ( *map ) ),
 
573
                            0, map, 1, NULL );
 
574
}
 
575
 
 
576
static inline int
 
577
hermon_cmd_set_icm_size ( struct hermon *hermon,
 
578
                          const struct hermonprm_scalar_parameter *icm_size,
 
579
                          struct hermonprm_scalar_parameter *icm_aux_size ) {
 
580
        return hermon_cmd ( hermon,
 
581
                            HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
 
582
                                                   0, sizeof ( *icm_size ),
 
583
                                                   0, sizeof (*icm_aux_size) ),
 
584
                            0, icm_size, 0, icm_aux_size );
 
585
}
 
586
 
 
587
static inline int
 
588
hermon_cmd_unmap_fa ( struct hermon *hermon ) {
 
589
        return hermon_cmd ( hermon,
 
590
                            HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
 
591
                            0, NULL, 0, NULL );
 
592
}
 
593
 
 
594
static inline int
 
595
hermon_cmd_map_fa ( struct hermon *hermon,
 
596
                    const struct hermonprm_virtual_physical_mapping *map ) {
 
597
        return hermon_cmd ( hermon,
 
598
                            HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
 
599
                                                1, sizeof ( *map ) ),
 
600
                            0, map, 1, NULL );
 
601
}
 
602
 
 
603
/***************************************************************************
 
604
 *
 
605
 * Memory translation table operations
 
606
 *
 
607
 ***************************************************************************
 
608
 */
 
609
 
 
610
/**
 
611
 * Allocate MTT entries
 
612
 *
 
613
 * @v hermon            Hermon device
 
614
 * @v memory            Memory to map into MTT
 
615
 * @v len               Length of memory to map
 
616
 * @v mtt               MTT descriptor to fill in
 
617
 * @ret rc              Return status code
 
618
 */
 
619
static int hermon_alloc_mtt ( struct hermon *hermon,
 
620
                              const void *memory, size_t len,
 
621
                              struct hermon_mtt *mtt ) {
 
622
        struct hermonprm_write_mtt write_mtt;
 
623
        physaddr_t start;
 
624
        physaddr_t addr;
 
625
        unsigned int page_offset;
 
626
        unsigned int num_pages;
 
627
        int mtt_offset;
 
628
        unsigned int mtt_base_addr;
 
629
        unsigned int i;
 
630
        int rc;
 
631
 
 
632
        /* Find available MTT entries */
 
633
        start = virt_to_phys ( memory );
 
634
        page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
 
635
        start -= page_offset;
 
636
        len += page_offset;
 
637
        num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
 
638
        mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
 
639
                                            num_pages );
 
640
        if ( mtt_offset < 0 ) {
 
641
                DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
 
642
                       hermon, num_pages );
 
643
                rc = mtt_offset;
 
644
                goto err_mtt_offset;
 
645
        }
 
646
        mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
 
647
                          hermon->cap.mtt_entry_size );
 
648
        addr = start;
 
649
 
 
650
        /* Fill in MTT structure */
 
651
        mtt->mtt_offset = mtt_offset;
 
652
        mtt->num_pages = num_pages;
 
653
        mtt->mtt_base_addr = mtt_base_addr;
 
654
        mtt->page_offset = page_offset;
 
655
 
 
656
        /* Construct and issue WRITE_MTT commands */
 
657
        for ( i = 0 ; i < num_pages ; i++ ) {
 
658
                memset ( &write_mtt, 0, sizeof ( write_mtt ) );
 
659
                MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
 
660
                             value, mtt_base_addr );
 
661
                MLX_FILL_H ( &write_mtt.mtt, 0, ptag_h, addr );
 
662
                MLX_FILL_2 ( &write_mtt.mtt, 1,
 
663
                             p, 1,
 
664
                             ptag_l, ( addr >> 3 ) );
 
665
                if ( ( rc = hermon_cmd_write_mtt ( hermon,
 
666
                                                   &write_mtt ) ) != 0 ) {
 
667
                        DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
 
668
                               hermon, mtt_base_addr );
 
669
                        goto err_write_mtt;
 
670
                }
 
671
                addr += HERMON_PAGE_SIZE;
 
672
                mtt_base_addr += hermon->cap.mtt_entry_size;
 
673
        }
 
674
 
 
675
        DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] for "
 
676
               "[%08lx,%08lx,%08lx,%08lx)\n", hermon, mtt->mtt_offset,
 
677
               ( mtt->mtt_offset + mtt->num_pages - 1 ), start,
 
678
               ( start + page_offset ), ( start + len ), addr );
 
679
 
 
680
        return 0;
 
681
 
 
682
 err_write_mtt:
 
683
        hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
 
684
 err_mtt_offset:
 
685
        return rc;
 
686
}
 
687
 
 
688
/**
 
689
 * Free MTT entries
 
690
 *
 
691
 * @v hermon            Hermon device
 
692
 * @v mtt               MTT descriptor
 
693
 */
 
694
static void hermon_free_mtt ( struct hermon *hermon,
 
695
                              struct hermon_mtt *mtt ) {
 
696
 
 
697
        DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] freed\n",
 
698
               hermon, mtt->mtt_offset,
 
699
               ( mtt->mtt_offset + mtt->num_pages - 1 ) );
 
700
        hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
 
701
                              mtt->num_pages );
 
702
}
 
703
 
 
704
/***************************************************************************
 
705
 *
 
706
 * Static configuration operations
 
707
 *
 
708
 ***************************************************************************
 
709
 */
 
710
 
 
711
/**
 
712
 * Calculate offset within static configuration
 
713
 *
 
714
 * @v field             Field
 
715
 * @ret offset          Offset
 
716
 */
 
717
#define HERMON_MOD_STAT_CFG_OFFSET( field )                                  \
 
718
        ( ( MLX_BIT_OFFSET ( struct hermonprm_mod_stat_cfg_st, field ) / 8 ) \
 
719
          & ~( sizeof ( struct hermonprm_scalar_parameter ) - 1 ) )
 
720
 
 
721
/**
 
722
 * Query or modify static configuration
 
723
 *
 
724
 * @v hermon            Hermon device
 
725
 * @v port              Port
 
726
 * @v mode              Command mode
 
727
 * @v offset            Offset within static configuration
 
728
 * @v stat_cfg          Static configuration
 
729
 * @ret rc              Return status code
 
730
 */
 
731
static int hermon_mod_stat_cfg ( struct hermon *hermon, unsigned int port,
 
732
                                 unsigned int mode, unsigned int offset,
 
733
                                 struct hermonprm_mod_stat_cfg *stat_cfg ) {
 
734
        struct hermonprm_scalar_parameter *portion =
 
735
                ( ( void * ) &stat_cfg->u.bytes[offset] );
 
736
        struct hermonprm_mod_stat_cfg_input_mod mod;
 
737
        int rc;
 
738
 
 
739
        /* Sanity check */
 
740
        assert ( ( offset % sizeof ( *portion ) ) == 0 );
 
741
 
 
742
        /* Construct input modifier */
 
743
        memset ( &mod, 0, sizeof ( mod ) );
 
744
        MLX_FILL_2 ( &mod, 0,
 
745
                     portnum, port,
 
746
                     offset, offset );
 
747
 
 
748
        /* Issue command */
 
749
        if ( ( rc = hermon_cmd_mod_stat_cfg ( hermon, mode,
 
750
                                              be32_to_cpu ( mod.u.dwords[0] ),
 
751
                                              portion ) ) != 0 )
 
752
                return rc;
 
753
 
 
754
        return 0;
 
755
}
 
756
 
 
757
/***************************************************************************
 
758
 *
 
759
 * MAD operations
 
760
 *
 
761
 ***************************************************************************
 
762
 */
 
763
 
 
764
/**
 
765
 * Issue management datagram
 
766
 *
 
767
 * @v ibdev             Infiniband device
 
768
 * @v mad               Management datagram
 
769
 * @ret rc              Return status code
 
770
 */
 
771
static int hermon_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
 
772
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
773
        union hermonprm_mad mad_ifc;
 
774
        int rc;
 
775
 
 
776
        linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
 
777
                        mad_size_mismatch );
 
778
 
 
779
        /* Copy in request packet */
 
780
        memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
 
781
 
 
782
        /* Issue MAD */
 
783
        if ( ( rc = hermon_cmd_mad_ifc ( hermon, ibdev->port,
 
784
                                         &mad_ifc ) ) != 0 ) {
 
785
                DBGC ( hermon, "Hermon %p port %d could not issue MAD IFC: "
 
786
                       "%s\n", hermon, ibdev->port, strerror ( rc ) );
 
787
                return rc;
 
788
        }
 
789
 
 
790
        /* Copy out reply packet */
 
791
        memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
 
792
 
 
793
        if ( mad->hdr.status != 0 ) {
 
794
                DBGC ( hermon, "Hermon %p port %d MAD IFC status %04x\n",
 
795
                       hermon, ibdev->port, ntohs ( mad->hdr.status ) );
 
796
                return -EIO;
 
797
        }
 
798
        return 0;
 
799
}
 
800
 
 
801
/***************************************************************************
 
802
 *
 
803
 * Completion queue operations
 
804
 *
 
805
 ***************************************************************************
 
806
 */
 
807
 
 
808
/**
 
809
 * Dump completion queue context (for debugging only)
 
810
 *
 
811
 * @v hermon            Hermon device
 
812
 * @v cq                Completion queue
 
813
 * @ret rc              Return status code
 
814
 */
 
815
static __attribute__ (( unused )) int
 
816
hermon_dump_cqctx ( struct hermon *hermon, struct ib_completion_queue *cq ) {
 
817
        struct hermonprm_completion_queue_context cqctx;
 
818
        int rc;
 
819
 
 
820
        memset ( &cqctx, 0, sizeof ( cqctx ) );
 
821
        if ( ( rc = hermon_cmd_query_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
 
822
                DBGC ( hermon, "Hermon %p CQN %#lx QUERY_CQ failed: %s\n",
 
823
                       hermon, cq->cqn, strerror ( rc ) );
 
824
                return rc;
 
825
        }
 
826
        DBGC ( hermon, "Hermon %p CQN %#lx context:\n", hermon, cq->cqn );
 
827
        DBGC_HDA ( hermon, 0, &cqctx, sizeof ( cqctx ) );
 
828
 
 
829
        return 0;
 
830
}
 
831
 
 
832
/**
 
833
 * Create completion queue
 
834
 *
 
835
 * @v ibdev             Infiniband device
 
836
 * @v cq                Completion queue
 
837
 * @ret rc              Return status code
 
838
 */
 
839
static int hermon_create_cq ( struct ib_device *ibdev,
 
840
                              struct ib_completion_queue *cq ) {
 
841
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
842
        struct hermon_completion_queue *hermon_cq;
 
843
        struct hermonprm_completion_queue_context cqctx;
 
844
        int cqn_offset;
 
845
        unsigned int i;
 
846
        int rc;
 
847
 
 
848
        /* Find a free completion queue number */
 
849
        cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
 
850
                                            HERMON_MAX_CQS, 1 );
 
851
        if ( cqn_offset < 0 ) {
 
852
                DBGC ( hermon, "Hermon %p out of completion queues\n",
 
853
                       hermon );
 
854
                rc = cqn_offset;
 
855
                goto err_cqn_offset;
 
856
        }
 
857
        cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
 
858
 
 
859
        /* Allocate control structures */
 
860
        hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
 
861
        if ( ! hermon_cq ) {
 
862
                rc = -ENOMEM;
 
863
                goto err_hermon_cq;
 
864
        }
 
865
 
 
866
        /* Allocate doorbell */
 
867
        hermon_cq->doorbell = malloc_dma ( sizeof ( hermon_cq->doorbell[0] ),
 
868
                                           sizeof ( hermon_cq->doorbell[0] ) );
 
869
        if ( ! hermon_cq->doorbell ) {
 
870
                rc = -ENOMEM;
 
871
                goto err_doorbell;
 
872
        }
 
873
        memset ( hermon_cq->doorbell, 0, sizeof ( hermon_cq->doorbell[0] ) );
 
874
 
 
875
        /* Allocate completion queue itself */
 
876
        hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
 
877
        hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
 
878
                                      sizeof ( hermon_cq->cqe[0] ) );
 
879
        if ( ! hermon_cq->cqe ) {
 
880
                rc = -ENOMEM;
 
881
                goto err_cqe;
 
882
        }
 
883
        memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
 
884
        for ( i = 0 ; i < cq->num_cqes ; i++ ) {
 
885
                MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
 
886
        }
 
887
        barrier();
 
888
 
 
889
        /* Allocate MTT entries */
 
890
        if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
 
891
                                       hermon_cq->cqe_size,
 
892
                                       &hermon_cq->mtt ) ) != 0 )
 
893
                goto err_alloc_mtt;
 
894
 
 
895
        /* Hand queue over to hardware */
 
896
        memset ( &cqctx, 0, sizeof ( cqctx ) );
 
897
        MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
 
898
        MLX_FILL_1 ( &cqctx, 2,
 
899
                     page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
 
900
        MLX_FILL_2 ( &cqctx, 3,
 
901
                     usr_page, HERMON_UAR_NON_EQ_PAGE,
 
902
                     log_cq_size, fls ( cq->num_cqes - 1 ) );
 
903
        MLX_FILL_1 ( &cqctx, 5, c_eqn, hermon->eq.eqn );
 
904
        MLX_FILL_H ( &cqctx, 6, mtt_base_addr_h,
 
905
                     hermon_cq->mtt.mtt_base_addr );
 
906
        MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
 
907
                     ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
 
908
        MLX_FILL_H ( &cqctx, 14, db_record_addr_h,
 
909
                     virt_to_phys ( hermon_cq->doorbell ) );
 
910
        MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
 
911
                     ( virt_to_phys ( hermon_cq->doorbell ) >> 3 ) );
 
912
        if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
 
913
                DBGC ( hermon, "Hermon %p CQN %#lx SW2HW_CQ failed: %s\n",
 
914
                       hermon, cq->cqn, strerror ( rc ) );
 
915
                goto err_sw2hw_cq;
 
916
        }
 
917
 
 
918
        DBGC ( hermon, "Hermon %p CQN %#lx ring [%08lx,%08lx), doorbell "
 
919
               "%08lx\n", hermon, cq->cqn, virt_to_phys ( hermon_cq->cqe ),
 
920
               ( virt_to_phys ( hermon_cq->cqe ) + hermon_cq->cqe_size ),
 
921
               virt_to_phys ( hermon_cq->doorbell ) );
 
922
        ib_cq_set_drvdata ( cq, hermon_cq );
 
923
        return 0;
 
924
 
 
925
 err_sw2hw_cq:
 
926
        hermon_free_mtt ( hermon, &hermon_cq->mtt );
 
927
 err_alloc_mtt:
 
928
        free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
 
929
 err_cqe:
 
930
        free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
 
931
 err_doorbell:
 
932
        free ( hermon_cq );
 
933
 err_hermon_cq:
 
934
        hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
 
935
 err_cqn_offset:
 
936
        return rc;
 
937
}
 
938
 
 
939
/**
 
940
 * Destroy completion queue
 
941
 *
 
942
 * @v ibdev             Infiniband device
 
943
 * @v cq                Completion queue
 
944
 */
 
945
static void hermon_destroy_cq ( struct ib_device *ibdev,
 
946
                                struct ib_completion_queue *cq ) {
 
947
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
948
        struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
 
949
        struct hermonprm_completion_queue_context cqctx;
 
950
        int cqn_offset;
 
951
        int rc;
 
952
 
 
953
        /* Take ownership back from hardware */
 
954
        if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
 
955
                DBGC ( hermon, "Hermon %p CQN %#lx FATAL HW2SW_CQ failed: "
 
956
                       "%s\n", hermon, cq->cqn, strerror ( rc ) );
 
957
                /* Leak memory and return; at least we avoid corruption */
 
958
                return;
 
959
        }
 
960
 
 
961
        /* Free MTT entries */
 
962
        hermon_free_mtt ( hermon, &hermon_cq->mtt );
 
963
 
 
964
        /* Free memory */
 
965
        free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
 
966
        free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
 
967
        free ( hermon_cq );
 
968
 
 
969
        /* Mark queue number as free */
 
970
        cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
 
971
        hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
 
972
 
 
973
        ib_cq_set_drvdata ( cq, NULL );
 
974
}
 
975
 
 
976
/***************************************************************************
 
977
 *
 
978
 * Queue pair operations
 
979
 *
 
980
 ***************************************************************************
 
981
 */
 
982
 
 
983
/**
 
984
 * Assign queue pair number
 
985
 *
 
986
 * @v ibdev             Infiniband device
 
987
 * @v qp                Queue pair
 
988
 * @ret rc              Return status code
 
989
 */
 
990
static int hermon_alloc_qpn ( struct ib_device *ibdev,
 
991
                              struct ib_queue_pair *qp ) {
 
992
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
993
        unsigned int port_offset;
 
994
        int qpn_offset;
 
995
 
 
996
        /* Calculate queue pair number */
 
997
        port_offset = ( ibdev->port - HERMON_PORT_BASE );
 
998
 
 
999
        switch ( qp->type ) {
 
1000
        case IB_QPT_SMI:
 
1001
                qp->qpn = ( hermon->special_qpn_base + port_offset );
 
1002
                return 0;
 
1003
        case IB_QPT_GSI:
 
1004
                qp->qpn = ( hermon->special_qpn_base + 2 + port_offset );
 
1005
                return 0;
 
1006
        case IB_QPT_UD:
 
1007
        case IB_QPT_RC:
 
1008
        case IB_QPT_ETH:
 
1009
                /* Find a free queue pair number */
 
1010
                qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
 
1011
                                                    HERMON_MAX_QPS, 1 );
 
1012
                if ( qpn_offset < 0 ) {
 
1013
                        DBGC ( hermon, "Hermon %p out of queue pairs\n",
 
1014
                               hermon );
 
1015
                        return qpn_offset;
 
1016
                }
 
1017
                qp->qpn = ( ( random() & HERMON_QPN_RANDOM_MASK ) |
 
1018
                            ( hermon->qpn_base + qpn_offset ) );
 
1019
                return 0;
 
1020
        default:
 
1021
                DBGC ( hermon, "Hermon %p unsupported QP type %d\n",
 
1022
                       hermon, qp->type );
 
1023
                return -ENOTSUP;
 
1024
        }
 
1025
}
 
1026
 
 
1027
/**
 
1028
 * Free queue pair number
 
1029
 *
 
1030
 * @v ibdev             Infiniband device
 
1031
 * @v qp                Queue pair
 
1032
 */
 
1033
static void hermon_free_qpn ( struct ib_device *ibdev,
 
1034
                              struct ib_queue_pair *qp ) {
 
1035
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1036
        int qpn_offset;
 
1037
 
 
1038
        qpn_offset = ( ( qp->qpn & ~HERMON_QPN_RANDOM_MASK )
 
1039
                       - hermon->qpn_base );
 
1040
        if ( qpn_offset >= 0 )
 
1041
                hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
 
1042
}
 
1043
 
 
1044
/**
 
1045
 * Calculate transmission rate
 
1046
 *
 
1047
 * @v av                Address vector
 
1048
 * @ret hermon_rate     Hermon rate
 
1049
 */
 
1050
static unsigned int hermon_rate ( struct ib_address_vector *av ) {
 
1051
        return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
 
1052
                 ? ( av->rate + 5 ) : 0 );
 
1053
}
 
1054
 
 
1055
/**
 
1056
 * Calculate schedule queue
 
1057
 *
 
1058
 * @v ibdev             Infiniband device
 
1059
 * @v qp                Queue pair
 
1060
 * @ret sched_queue     Schedule queue
 
1061
 */
 
1062
static unsigned int hermon_sched_queue ( struct ib_device *ibdev,
 
1063
                                         struct ib_queue_pair *qp ) {
 
1064
        return ( ( ( qp->type == IB_QPT_SMI ) ?
 
1065
                   HERMON_SCHED_QP0 : HERMON_SCHED_DEFAULT ) |
 
1066
                 ( ( ibdev->port - 1 ) << 6 ) );
 
1067
}
 
1068
 
 
1069
/** Queue pair transport service type map */
 
1070
static uint8_t hermon_qp_st[] = {
 
1071
        [IB_QPT_SMI] = HERMON_ST_MLX,
 
1072
        [IB_QPT_GSI] = HERMON_ST_MLX,
 
1073
        [IB_QPT_UD] = HERMON_ST_UD,
 
1074
        [IB_QPT_RC] = HERMON_ST_RC,
 
1075
        [IB_QPT_ETH] = HERMON_ST_MLX,
 
1076
};
 
1077
 
 
1078
/**
 
1079
 * Dump queue pair context (for debugging only)
 
1080
 *
 
1081
 * @v hermon            Hermon device
 
1082
 * @v qp                Queue pair
 
1083
 * @ret rc              Return status code
 
1084
 */
 
1085
static __attribute__ (( unused )) int
 
1086
hermon_dump_qpctx ( struct hermon *hermon, struct ib_queue_pair *qp ) {
 
1087
        struct hermonprm_qp_ee_state_transitions qpctx;
 
1088
        int rc;
 
1089
 
 
1090
        memset ( &qpctx, 0, sizeof ( qpctx ) );
 
1091
        if ( ( rc = hermon_cmd_query_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ) {
 
1092
                DBGC ( hermon, "Hermon %p QPN %#lx QUERY_QP failed: %s\n",
 
1093
                       hermon, qp->qpn, strerror ( rc ) );
 
1094
                return rc;
 
1095
        }
 
1096
        DBGC ( hermon, "Hermon %p QPN %#lx context:\n", hermon, qp->qpn );
 
1097
        DBGC_HDA ( hermon, 0, &qpctx.u.dwords[2], ( sizeof ( qpctx ) - 8 ) );
 
1098
 
 
1099
        return 0;
 
1100
}
 
1101
 
 
1102
/**
 
1103
 * Create queue pair
 
1104
 *
 
1105
 * @v ibdev             Infiniband device
 
1106
 * @v qp                Queue pair
 
1107
 * @ret rc              Return status code
 
1108
 */
 
1109
static int hermon_create_qp ( struct ib_device *ibdev,
 
1110
                              struct ib_queue_pair *qp ) {
 
1111
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1112
        struct hermon_queue_pair *hermon_qp;
 
1113
        struct hermonprm_qp_ee_state_transitions qpctx;
 
1114
        struct hermonprm_wqe_segment_data_ptr *data;
 
1115
        unsigned int i;
 
1116
        int rc;
 
1117
 
 
1118
        /* Calculate queue pair number */
 
1119
        if ( ( rc = hermon_alloc_qpn ( ibdev, qp ) ) != 0 )
 
1120
                goto err_alloc_qpn;
 
1121
 
 
1122
        /* Allocate control structures */
 
1123
        hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
 
1124
        if ( ! hermon_qp ) {
 
1125
                rc = -ENOMEM;
 
1126
                goto err_hermon_qp;
 
1127
        }
 
1128
 
 
1129
        /* Allocate doorbells */
 
1130
        hermon_qp->recv.doorbell =
 
1131
                malloc_dma ( sizeof ( hermon_qp->recv.doorbell[0] ),
 
1132
                             sizeof ( hermon_qp->recv.doorbell[0] ) );
 
1133
        if ( ! hermon_qp->recv.doorbell ) {
 
1134
                rc = -ENOMEM;
 
1135
                goto err_recv_doorbell;
 
1136
        }
 
1137
        memset ( hermon_qp->recv.doorbell, 0,
 
1138
                 sizeof ( hermon_qp->recv.doorbell[0] ) );
 
1139
        hermon_qp->send.doorbell =
 
1140
                ( hermon->uar + HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE +
 
1141
                  HERMON_DB_POST_SND_OFFSET );
 
1142
 
 
1143
        /* Allocate work queue buffer */
 
1144
        hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
 
1145
                                ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
 
1146
        hermon_qp->send.num_wqes =
 
1147
                ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
 
1148
        hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
 
1149
                                     sizeof ( hermon_qp->send.wqe[0] ) );
 
1150
        hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
 
1151
                                     sizeof ( hermon_qp->recv.wqe[0] ) );
 
1152
        if ( ( qp->type == IB_QPT_SMI ) || ( qp->type == IB_QPT_GSI ) ||
 
1153
             ( qp->type == IB_QPT_UD ) ) {
 
1154
                hermon_qp->recv.grh_size = ( qp->recv.num_wqes *
 
1155
                                             sizeof ( hermon_qp->recv.grh[0] ));
 
1156
        }
 
1157
        hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
 
1158
                                hermon_qp->recv.wqe_size +
 
1159
                                hermon_qp->recv.grh_size );
 
1160
        hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
 
1161
                                      sizeof ( hermon_qp->send.wqe[0] ) );
 
1162
        if ( ! hermon_qp->wqe ) {
 
1163
                rc = -ENOMEM;
 
1164
                goto err_alloc_wqe;
 
1165
        }
 
1166
        hermon_qp->send.wqe = hermon_qp->wqe;
 
1167
        hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
 
1168
        if ( hermon_qp->recv.grh_size ) {
 
1169
                hermon_qp->recv.grh = ( hermon_qp->wqe +
 
1170
                                        hermon_qp->send.wqe_size +
 
1171
                                        hermon_qp->recv.wqe_size );
 
1172
        }
 
1173
 
 
1174
        /* Initialise work queue entries */
 
1175
        memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
 
1176
        memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
 
1177
        data = &hermon_qp->recv.wqe[0].recv.data[0];
 
1178
        for ( i = 0 ; i < ( hermon_qp->recv.wqe_size / sizeof ( *data ) ); i++){
 
1179
                MLX_FILL_1 ( data, 1, l_key, HERMON_INVALID_LKEY );
 
1180
                data++;
 
1181
        }
 
1182
 
 
1183
        /* Allocate MTT entries */
 
1184
        if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
 
1185
                                       hermon_qp->wqe_size,
 
1186
                                       &hermon_qp->mtt ) ) != 0 ) {
 
1187
                goto err_alloc_mtt;
 
1188
        }
 
1189
 
 
1190
        /* Transition queue to INIT state */
 
1191
        memset ( &qpctx, 0, sizeof ( qpctx ) );
 
1192
        MLX_FILL_2 ( &qpctx, 2,
 
1193
                     qpc_eec_data.pm_state, HERMON_PM_STATE_MIGRATED,
 
1194
                     qpc_eec_data.st, hermon_qp_st[qp->type] );
 
1195
        MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
 
1196
        MLX_FILL_4 ( &qpctx, 4,
 
1197
                     qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
 
1198
                     qpc_eec_data.log_rq_stride,
 
1199
                     ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
 
1200
                     qpc_eec_data.log_sq_size,
 
1201
                     fls ( hermon_qp->send.num_wqes - 1 ),
 
1202
                     qpc_eec_data.log_sq_stride,
 
1203
                     ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
 
1204
        MLX_FILL_1 ( &qpctx, 5,
 
1205
                     qpc_eec_data.usr_page, HERMON_UAR_NON_EQ_PAGE );
 
1206
        MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
 
1207
        MLX_FILL_4 ( &qpctx, 38,
 
1208
                     qpc_eec_data.rre, 1,
 
1209
                     qpc_eec_data.rwe, 1,
 
1210
                     qpc_eec_data.rae, 1,
 
1211
                     qpc_eec_data.page_offset,
 
1212
                     ( hermon_qp->mtt.page_offset >> 6 ) );
 
1213
        MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
 
1214
        MLX_FILL_H ( &qpctx, 42, qpc_eec_data.db_record_addr_h,
 
1215
                     virt_to_phys ( hermon_qp->recv.doorbell ) );
 
1216
        MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
 
1217
                     ( virt_to_phys ( hermon_qp->recv.doorbell ) >> 2 ) );
 
1218
        MLX_FILL_H ( &qpctx, 52, qpc_eec_data.mtt_base_addr_h,
 
1219
                     hermon_qp->mtt.mtt_base_addr );
 
1220
        MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
 
1221
                     ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
 
1222
        if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
 
1223
                                             &qpctx ) ) != 0 ) {
 
1224
                DBGC ( hermon, "Hermon %p QPN %#lx RST2INIT_QP failed: %s\n",
 
1225
                       hermon, qp->qpn, strerror ( rc ) );
 
1226
                goto err_rst2init_qp;
 
1227
        }
 
1228
        hermon_qp->state = HERMON_QP_ST_INIT;
 
1229
 
 
1230
        DBGC ( hermon, "Hermon %p QPN %#lx send ring [%08lx,%08lx), doorbell "
 
1231
               "%08lx\n", hermon, qp->qpn,
 
1232
               virt_to_phys ( hermon_qp->send.wqe ),
 
1233
               ( virt_to_phys ( hermon_qp->send.wqe ) +
 
1234
                 hermon_qp->send.wqe_size ),
 
1235
               virt_to_phys ( hermon_qp->send.doorbell ) );
 
1236
        DBGC ( hermon, "Hermon %p QPN %#lx receive ring [%08lx,%08lx), "
 
1237
               "doorbell %08lx\n", hermon, qp->qpn,
 
1238
               virt_to_phys ( hermon_qp->recv.wqe ),
 
1239
               ( virt_to_phys ( hermon_qp->recv.wqe ) +
 
1240
                 hermon_qp->recv.wqe_size ),
 
1241
               virt_to_phys ( hermon_qp->recv.doorbell ) );
 
1242
        DBGC ( hermon, "Hermon %p QPN %#lx send CQN %#lx receive CQN %#lx\n",
 
1243
               hermon, qp->qpn, qp->send.cq->cqn, qp->recv.cq->cqn );
 
1244
        ib_qp_set_drvdata ( qp, hermon_qp );
 
1245
        return 0;
 
1246
 
 
1247
        hermon_cmd_2rst_qp ( hermon, qp->qpn );
 
1248
 err_rst2init_qp:
 
1249
        hermon_free_mtt ( hermon, &hermon_qp->mtt );
 
1250
 err_alloc_mtt:
 
1251
        free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
 
1252
 err_alloc_wqe:
 
1253
        free_dma ( hermon_qp->recv.doorbell,
 
1254
                   sizeof ( hermon_qp->recv.doorbell[0] ) );
 
1255
 err_recv_doorbell:
 
1256
        free ( hermon_qp );
 
1257
 err_hermon_qp:
 
1258
        hermon_free_qpn ( ibdev, qp );
 
1259
 err_alloc_qpn:
 
1260
        return rc;
 
1261
}
 
1262
 
 
1263
/**
 
1264
 * Modify queue pair
 
1265
 *
 
1266
 * @v ibdev             Infiniband device
 
1267
 * @v qp                Queue pair
 
1268
 * @ret rc              Return status code
 
1269
 */
 
1270
static int hermon_modify_qp ( struct ib_device *ibdev,
 
1271
                              struct ib_queue_pair *qp ) {
 
1272
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1273
        struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
 
1274
        struct hermonprm_qp_ee_state_transitions qpctx;
 
1275
        int rc;
 
1276
 
 
1277
        /* Transition queue to RTR state, if applicable */
 
1278
        if ( hermon_qp->state < HERMON_QP_ST_RTR ) {
 
1279
                memset ( &qpctx, 0, sizeof ( qpctx ) );
 
1280
                MLX_FILL_2 ( &qpctx, 4,
 
1281
                             qpc_eec_data.mtu,
 
1282
                             ( ( qp->type == IB_QPT_ETH ) ?
 
1283
                               HERMON_MTU_ETH : HERMON_MTU_2048 ),
 
1284
                             qpc_eec_data.msg_max, 31 );
 
1285
                MLX_FILL_1 ( &qpctx, 7,
 
1286
                             qpc_eec_data.remote_qpn_een, qp->av.qpn );
 
1287
                MLX_FILL_1 ( &qpctx, 9,
 
1288
                             qpc_eec_data.primary_address_path.rlid,
 
1289
                             qp->av.lid );
 
1290
                MLX_FILL_1 ( &qpctx, 10,
 
1291
                             qpc_eec_data.primary_address_path.max_stat_rate,
 
1292
                             hermon_rate ( &qp->av ) );
 
1293
                memcpy ( &qpctx.u.dwords[12], &qp->av.gid,
 
1294
                         sizeof ( qp->av.gid ) );
 
1295
                MLX_FILL_1 ( &qpctx, 16,
 
1296
                             qpc_eec_data.primary_address_path.sched_queue,
 
1297
                             hermon_sched_queue ( ibdev, qp ) );
 
1298
                MLX_FILL_1 ( &qpctx, 39,
 
1299
                             qpc_eec_data.next_rcv_psn, qp->recv.psn );
 
1300
                if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
 
1301
                                                     &qpctx ) ) != 0 ) {
 
1302
                        DBGC ( hermon, "Hermon %p QPN %#lx INIT2RTR_QP failed:"
 
1303
                               " %s\n", hermon, qp->qpn, strerror ( rc ) );
 
1304
                        return rc;
 
1305
                }
 
1306
                hermon_qp->state = HERMON_QP_ST_RTR;
 
1307
        }
 
1308
 
 
1309
        /* Transition queue to RTS state */
 
1310
        if ( hermon_qp->state < HERMON_QP_ST_RTS ) {
 
1311
                memset ( &qpctx, 0, sizeof ( qpctx ) );
 
1312
                MLX_FILL_1 ( &qpctx, 10,
 
1313
                             qpc_eec_data.primary_address_path.ack_timeout,
 
1314
                             14 /* 4.096us * 2^(14) = 67ms */ );
 
1315
                MLX_FILL_2 ( &qpctx, 30,
 
1316
                             qpc_eec_data.retry_count, HERMON_RETRY_MAX,
 
1317
                             qpc_eec_data.rnr_retry, HERMON_RETRY_MAX );
 
1318
                MLX_FILL_1 ( &qpctx, 32,
 
1319
                             qpc_eec_data.next_send_psn, qp->send.psn );
 
1320
                if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn,
 
1321
                                                    &qpctx ) ) != 0 ) {
 
1322
                        DBGC ( hermon, "Hermon %p QPN %#lx RTR2RTS_QP failed: "
 
1323
                               "%s\n", hermon, qp->qpn, strerror ( rc ) );
 
1324
                        return rc;
 
1325
                }
 
1326
                hermon_qp->state = HERMON_QP_ST_RTS;
 
1327
        }
 
1328
 
 
1329
        /* Update parameters in RTS state */
 
1330
        memset ( &qpctx, 0, sizeof ( qpctx ) );
 
1331
        MLX_FILL_1 ( &qpctx, 0, opt_param_mask, HERMON_QP_OPT_PARAM_QKEY );
 
1332
        MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
 
1333
        if ( ( rc = hermon_cmd_rts2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
 
1334
                DBGC ( hermon, "Hermon %p QPN %#lx RTS2RTS_QP failed: %s\n",
 
1335
                       hermon, qp->qpn, strerror ( rc ) );
 
1336
                return rc;
 
1337
        }
 
1338
 
 
1339
        return 0;
 
1340
}
 
1341
 
 
1342
/**
 
1343
 * Destroy queue pair
 
1344
 *
 
1345
 * @v ibdev             Infiniband device
 
1346
 * @v qp                Queue pair
 
1347
 */
 
1348
static void hermon_destroy_qp ( struct ib_device *ibdev,
 
1349
                                struct ib_queue_pair *qp ) {
 
1350
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1351
        struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
 
1352
        int rc;
 
1353
 
 
1354
        /* Take ownership back from hardware */
 
1355
        if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
 
1356
                DBGC ( hermon, "Hermon %p QPN %#lx FATAL 2RST_QP failed: %s\n",
 
1357
                       hermon, qp->qpn, strerror ( rc ) );
 
1358
                /* Leak memory and return; at least we avoid corruption */
 
1359
                return;
 
1360
        }
 
1361
 
 
1362
        /* Free MTT entries */
 
1363
        hermon_free_mtt ( hermon, &hermon_qp->mtt );
 
1364
 
 
1365
        /* Free memory */
 
1366
        free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
 
1367
        free_dma ( hermon_qp->recv.doorbell,
 
1368
                   sizeof ( hermon_qp->recv.doorbell[0] ) );
 
1369
        free ( hermon_qp );
 
1370
 
 
1371
        /* Mark queue number as free */
 
1372
        hermon_free_qpn ( ibdev, qp );
 
1373
 
 
1374
        ib_qp_set_drvdata ( qp, NULL );
 
1375
}
 
1376
 
 
1377
/***************************************************************************
 
1378
 *
 
1379
 * Work request operations
 
1380
 *
 
1381
 ***************************************************************************
 
1382
 */
 
1383
 
 
1384
/**
 
1385
 * Construct UD send work queue entry
 
1386
 *
 
1387
 * @v ibdev             Infiniband device
 
1388
 * @v qp                Queue pair
 
1389
 * @v dest              Destination address vector
 
1390
 * @v iobuf             I/O buffer
 
1391
 * @v wqe               Send work queue entry
 
1392
 * @ret opcode          Control opcode
 
1393
 */
 
1394
static __attribute__ (( unused )) unsigned int
 
1395
hermon_fill_nop_send_wqe ( struct ib_device *ibdev __unused,
 
1396
                           struct ib_queue_pair *qp __unused,
 
1397
                           struct ib_address_vector *dest __unused,
 
1398
                           struct io_buffer *iobuf __unused,
 
1399
                           union hermon_send_wqe *wqe ) {
 
1400
 
 
1401
        MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( wqe->ctrl ) / 16 ) );
 
1402
        MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
 
1403
        return HERMON_OPCODE_NOP;
 
1404
}
 
1405
 
 
1406
/**
 
1407
 * Construct UD send work queue entry
 
1408
 *
 
1409
 * @v ibdev             Infiniband device
 
1410
 * @v qp                Queue pair
 
1411
 * @v dest              Destination address vector
 
1412
 * @v iobuf             I/O buffer
 
1413
 * @v wqe               Send work queue entry
 
1414
 * @ret opcode          Control opcode
 
1415
 */
 
1416
static unsigned int
 
1417
hermon_fill_ud_send_wqe ( struct ib_device *ibdev,
 
1418
                          struct ib_queue_pair *qp __unused,
 
1419
                          struct ib_address_vector *dest,
 
1420
                          struct io_buffer *iobuf,
 
1421
                          union hermon_send_wqe *wqe ) {
 
1422
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1423
 
 
1424
        MLX_FILL_1 ( &wqe->ud.ctrl, 1, ds,
 
1425
                     ( ( offsetof ( typeof ( wqe->ud ), data[1] ) / 16 ) ) );
 
1426
        MLX_FILL_1 ( &wqe->ud.ctrl, 2, c, 0x03 /* generate completion */ );
 
1427
        MLX_FILL_2 ( &wqe->ud.ud, 0,
 
1428
                     ud_address_vector.pd, HERMON_GLOBAL_PD,
 
1429
                     ud_address_vector.port_number, ibdev->port );
 
1430
        MLX_FILL_2 ( &wqe->ud.ud, 1,
 
1431
                     ud_address_vector.rlid, dest->lid,
 
1432
                     ud_address_vector.g, dest->gid_present );
 
1433
        MLX_FILL_1 ( &wqe->ud.ud, 2,
 
1434
                     ud_address_vector.max_stat_rate, hermon_rate ( dest ) );
 
1435
        MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, dest->sl );
 
1436
        memcpy ( &wqe->ud.ud.u.dwords[4], &dest->gid, sizeof ( dest->gid ) );
 
1437
        MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, dest->qpn );
 
1438
        MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, dest->qkey );
 
1439
        MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
 
1440
        MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, hermon->lkey );
 
1441
        MLX_FILL_H ( &wqe->ud.data[0], 2,
 
1442
                     local_address_h, virt_to_bus ( iobuf->data ) );
 
1443
        MLX_FILL_1 ( &wqe->ud.data[0], 3,
 
1444
                     local_address_l, virt_to_bus ( iobuf->data ) );
 
1445
        return HERMON_OPCODE_SEND;
 
1446
}
 
1447
 
 
1448
/**
 
1449
 * Construct MLX send work queue entry
 
1450
 *
 
1451
 * @v ibdev             Infiniband device
 
1452
 * @v qp                Queue pair
 
1453
 * @v dest              Destination address vector
 
1454
 * @v iobuf             I/O buffer
 
1455
 * @v wqe               Send work queue entry
 
1456
 * @ret opcode          Control opcode
 
1457
 */
 
1458
static unsigned int
 
1459
hermon_fill_mlx_send_wqe ( struct ib_device *ibdev,
 
1460
                           struct ib_queue_pair *qp,
 
1461
                           struct ib_address_vector *dest,
 
1462
                           struct io_buffer *iobuf,
 
1463
                           union hermon_send_wqe *wqe ) {
 
1464
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1465
        struct io_buffer headers;
 
1466
 
 
1467
        /* Construct IB headers */
 
1468
        iob_populate ( &headers, &wqe->mlx.headers, 0,
 
1469
                       sizeof ( wqe->mlx.headers ) );
 
1470
        iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
 
1471
        ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
 
1472
 
 
1473
        /* Fill work queue entry */
 
1474
        MLX_FILL_1 ( &wqe->mlx.ctrl, 1, ds,
 
1475
                     ( ( offsetof ( typeof ( wqe->mlx ), data[2] ) / 16 ) ) );
 
1476
        MLX_FILL_5 ( &wqe->mlx.ctrl, 2,
 
1477
                     c, 0x03 /* generate completion */,
 
1478
                     icrc, 0 /* generate ICRC */,
 
1479
                     max_statrate, hermon_rate ( dest ),
 
1480
                     slr, 0,
 
1481
                     v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
 
1482
        MLX_FILL_1 ( &wqe->mlx.ctrl, 3, rlid, dest->lid );
 
1483
        MLX_FILL_1 ( &wqe->mlx.data[0], 0,
 
1484
                     byte_count, iob_len ( &headers ) );
 
1485
        MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, hermon->lkey );
 
1486
        MLX_FILL_H ( &wqe->mlx.data[0], 2,
 
1487
                     local_address_h, virt_to_bus ( headers.data ) );
 
1488
        MLX_FILL_1 ( &wqe->mlx.data[0], 3,
 
1489
                     local_address_l, virt_to_bus ( headers.data ) );
 
1490
        MLX_FILL_1 ( &wqe->mlx.data[1], 0,
 
1491
                     byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
 
1492
        MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, hermon->lkey );
 
1493
        MLX_FILL_H ( &wqe->mlx.data[1], 2,
 
1494
                     local_address_h, virt_to_bus ( iobuf->data ) );
 
1495
        MLX_FILL_1 ( &wqe->mlx.data[1], 3,
 
1496
                     local_address_l, virt_to_bus ( iobuf->data ) );
 
1497
        return HERMON_OPCODE_SEND;
 
1498
}
 
1499
 
 
1500
/**
 
1501
 * Construct RC send work queue entry
 
1502
 *
 
1503
 * @v ibdev             Infiniband device
 
1504
 * @v qp                Queue pair
 
1505
 * @v dest              Destination address vector
 
1506
 * @v iobuf             I/O buffer
 
1507
 * @v wqe               Send work queue entry
 
1508
 * @ret opcode          Control opcode
 
1509
 */
 
1510
static unsigned int
 
1511
hermon_fill_rc_send_wqe ( struct ib_device *ibdev,
 
1512
                          struct ib_queue_pair *qp __unused,
 
1513
                          struct ib_address_vector *dest __unused,
 
1514
                          struct io_buffer *iobuf,
 
1515
                          union hermon_send_wqe *wqe ) {
 
1516
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1517
 
 
1518
        MLX_FILL_1 ( &wqe->rc.ctrl, 1, ds,
 
1519
                     ( ( offsetof ( typeof ( wqe->rc ), data[1] ) / 16 ) ) );
 
1520
        MLX_FILL_1 ( &wqe->rc.ctrl, 2, c, 0x03 /* generate completion */ );
 
1521
        MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
 
1522
        MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, hermon->lkey );
 
1523
        MLX_FILL_H ( &wqe->rc.data[0], 2,
 
1524
                     local_address_h, virt_to_bus ( iobuf->data ) );
 
1525
        MLX_FILL_1 ( &wqe->rc.data[0], 3,
 
1526
                     local_address_l, virt_to_bus ( iobuf->data ) );
 
1527
        return HERMON_OPCODE_SEND;
 
1528
}
 
1529
 
 
1530
/**
 
1531
 * Construct Ethernet send work queue entry
 
1532
 *
 
1533
 * @v ibdev             Infiniband device
 
1534
 * @v qp                Queue pair
 
1535
 * @v dest              Destination address vector
 
1536
 * @v iobuf             I/O buffer
 
1537
 * @v wqe               Send work queue entry
 
1538
 * @ret opcode          Control opcode
 
1539
 */
 
1540
static unsigned int
 
1541
hermon_fill_eth_send_wqe ( struct ib_device *ibdev,
 
1542
                           struct ib_queue_pair *qp __unused,
 
1543
                           struct ib_address_vector *dest __unused,
 
1544
                           struct io_buffer *iobuf,
 
1545
                           union hermon_send_wqe *wqe ) {
 
1546
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1547
 
 
1548
        /* Fill work queue entry */
 
1549
        MLX_FILL_1 ( &wqe->eth.ctrl, 1, ds,
 
1550
                     ( ( offsetof ( typeof ( wqe->mlx ), data[1] ) / 16 ) ) );
 
1551
        MLX_FILL_2 ( &wqe->eth.ctrl, 2,
 
1552
                     c, 0x03 /* generate completion */,
 
1553
                     s, 1 /* inhibit ICRC */ );
 
1554
        MLX_FILL_1 ( &wqe->eth.data[0], 0,
 
1555
                     byte_count, iob_len ( iobuf ) );
 
1556
        MLX_FILL_1 ( &wqe->eth.data[0], 1, l_key, hermon->lkey );
 
1557
        MLX_FILL_H ( &wqe->eth.data[0], 2,
 
1558
                     local_address_h, virt_to_bus ( iobuf->data ) );
 
1559
        MLX_FILL_1 ( &wqe->eth.data[0], 3,
 
1560
                     local_address_l, virt_to_bus ( iobuf->data ) );
 
1561
        return HERMON_OPCODE_SEND;
 
1562
}
 
1563
 
 
1564
/** Work queue entry constructors */
 
1565
static unsigned int
 
1566
( * hermon_fill_send_wqe[] ) ( struct ib_device *ibdev,
 
1567
                               struct ib_queue_pair *qp,
 
1568
                               struct ib_address_vector *dest,
 
1569
                               struct io_buffer *iobuf,
 
1570
                               union hermon_send_wqe *wqe ) = {
 
1571
        [IB_QPT_SMI] = hermon_fill_mlx_send_wqe,
 
1572
        [IB_QPT_GSI] = hermon_fill_mlx_send_wqe,
 
1573
        [IB_QPT_UD] = hermon_fill_ud_send_wqe,
 
1574
        [IB_QPT_RC] = hermon_fill_rc_send_wqe,
 
1575
        [IB_QPT_ETH] = hermon_fill_eth_send_wqe,
 
1576
};
 
1577
 
 
1578
/**
 
1579
 * Post send work queue entry
 
1580
 *
 
1581
 * @v ibdev             Infiniband device
 
1582
 * @v qp                Queue pair
 
1583
 * @v dest              Destination address vector
 
1584
 * @v iobuf             I/O buffer
 
1585
 * @ret rc              Return status code
 
1586
 */
 
1587
static int hermon_post_send ( struct ib_device *ibdev,
 
1588
                              struct ib_queue_pair *qp,
 
1589
                              struct ib_address_vector *dest,
 
1590
                              struct io_buffer *iobuf ) {
 
1591
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1592
        struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
 
1593
        struct ib_work_queue *wq = &qp->send;
 
1594
        struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
 
1595
        union hermon_send_wqe *wqe;
 
1596
        union hermonprm_doorbell_register db_reg;
 
1597
        unsigned long wqe_idx_mask;
 
1598
        unsigned long wqe_idx;
 
1599
        unsigned int owner;
 
1600
        unsigned int opcode;
 
1601
 
 
1602
        /* Allocate work queue entry */
 
1603
        wqe_idx = ( wq->next_idx & ( hermon_send_wq->num_wqes - 1 ) );
 
1604
        owner = ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 );
 
1605
        wqe_idx_mask = ( wq->num_wqes - 1 );
 
1606
        if ( wq->iobufs[ wqe_idx & wqe_idx_mask ] ) {
 
1607
                DBGC ( hermon, "Hermon %p QPN %#lx send queue full",
 
1608
                       hermon, qp->qpn );
 
1609
                return -ENOBUFS;
 
1610
        }
 
1611
        wq->iobufs[ wqe_idx & wqe_idx_mask ] = iobuf;
 
1612
        wqe = &hermon_send_wq->wqe[wqe_idx];
 
1613
 
 
1614
        /* Construct work queue entry */
 
1615
        memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
 
1616
                   ( sizeof ( *wqe ) - 4 ) );
 
1617
        assert ( qp->type < ( sizeof ( hermon_fill_send_wqe ) /
 
1618
                              sizeof ( hermon_fill_send_wqe[0] ) ) );
 
1619
        assert ( hermon_fill_send_wqe[qp->type] != NULL );
 
1620
        opcode = hermon_fill_send_wqe[qp->type] ( ibdev, qp, dest, iobuf, wqe );
 
1621
        barrier();
 
1622
        MLX_FILL_2 ( &wqe->ctrl, 0,
 
1623
                     opcode, opcode,
 
1624
                     owner, owner );
 
1625
        DBGCP ( hermon, "Hermon %p QPN %#lx posting send WQE %#lx:\n",
 
1626
                hermon, qp->qpn, wqe_idx );
 
1627
        DBGCP_HDA ( hermon, virt_to_phys ( wqe ), wqe, sizeof ( *wqe ) );
 
1628
 
 
1629
        /* Ring doorbell register */
 
1630
        MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
 
1631
        barrier();
 
1632
        writel ( db_reg.dword[0], hermon_send_wq->doorbell );
 
1633
 
 
1634
        /* Update work queue's index */
 
1635
        wq->next_idx++;
 
1636
 
 
1637
        return 0;
 
1638
}
 
1639
 
 
1640
/**
 
1641
 * Post receive work queue entry
 
1642
 *
 
1643
 * @v ibdev             Infiniband device
 
1644
 * @v qp                Queue pair
 
1645
 * @v iobuf             I/O buffer
 
1646
 * @ret rc              Return status code
 
1647
 */
 
1648
static int hermon_post_recv ( struct ib_device *ibdev,
 
1649
                              struct ib_queue_pair *qp,
 
1650
                              struct io_buffer *iobuf ) {
 
1651
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1652
        struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
 
1653
        struct ib_work_queue *wq = &qp->recv;
 
1654
        struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
 
1655
        struct hermonprm_recv_wqe *wqe;
 
1656
        struct hermonprm_wqe_segment_data_ptr *data;
 
1657
        struct ib_global_route_header *grh;
 
1658
        unsigned int wqe_idx_mask;
 
1659
 
 
1660
        /* Allocate work queue entry */
 
1661
        wqe_idx_mask = ( wq->num_wqes - 1 );
 
1662
        if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
 
1663
                DBGC ( hermon, "Hermon %p QPN %#lx receive queue full",
 
1664
                       hermon, qp->qpn );
 
1665
                return -ENOBUFS;
 
1666
        }
 
1667
        wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
 
1668
        wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
 
1669
 
 
1670
        /* Construct work queue entry */
 
1671
        data = &wqe->data[0];
 
1672
        if ( hermon_qp->recv.grh ) {
 
1673
                grh = &hermon_qp->recv.grh[wq->next_idx & wqe_idx_mask];
 
1674
                MLX_FILL_1 ( data, 0, byte_count, sizeof ( *grh ) );
 
1675
                MLX_FILL_1 ( data, 1, l_key, hermon->lkey );
 
1676
                MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( grh ) );
 
1677
                MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( grh ) );
 
1678
                data++;
 
1679
        }
 
1680
        MLX_FILL_1 ( data, 0, byte_count, iob_tailroom ( iobuf ) );
 
1681
        MLX_FILL_1 ( data, 1, l_key, hermon->lkey );
 
1682
        MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( iobuf->data ) );
 
1683
        MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( iobuf->data ) );
 
1684
 
 
1685
        /* Update work queue's index */
 
1686
        wq->next_idx++;
 
1687
 
 
1688
        /* Update doorbell record */
 
1689
        barrier();
 
1690
        MLX_FILL_1 ( hermon_recv_wq->doorbell, 0, receive_wqe_counter,
 
1691
                     ( wq->next_idx & 0xffff ) );
 
1692
 
 
1693
        return 0;
 
1694
}
 
1695
 
 
1696
/**
 
1697
 * Handle completion
 
1698
 *
 
1699
 * @v ibdev             Infiniband device
 
1700
 * @v cq                Completion queue
 
1701
 * @v cqe               Hardware completion queue entry
 
1702
 * @ret rc              Return status code
 
1703
 */
 
1704
static int hermon_complete ( struct ib_device *ibdev,
 
1705
                             struct ib_completion_queue *cq,
 
1706
                             union hermonprm_completion_entry *cqe ) {
 
1707
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1708
        struct hermon_queue_pair *hermon_qp;
 
1709
        struct ib_work_queue *wq;
 
1710
        struct ib_queue_pair *qp;
 
1711
        struct io_buffer *iobuf;
 
1712
        struct ib_address_vector recv_dest;
 
1713
        struct ib_address_vector recv_source;
 
1714
        struct ib_global_route_header *grh;
 
1715
        struct ib_address_vector *source;
 
1716
        unsigned int opcode;
 
1717
        unsigned long qpn;
 
1718
        int is_send;
 
1719
        unsigned long wqe_idx;
 
1720
        unsigned long wqe_idx_mask;
 
1721
        size_t len;
 
1722
        int rc = 0;
 
1723
 
 
1724
        /* Parse completion */
 
1725
        qpn = MLX_GET ( &cqe->normal, qpn );
 
1726
        is_send = MLX_GET ( &cqe->normal, s_r );
 
1727
        opcode = MLX_GET ( &cqe->normal, opcode );
 
1728
        if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
 
1729
                /* "s" field is not valid for error opcodes */
 
1730
                is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
 
1731
                DBGC ( hermon, "Hermon %p CQN %#lx syndrome %x vendor %x\n",
 
1732
                       hermon, cq->cqn, MLX_GET ( &cqe->error, syndrome ),
 
1733
                       MLX_GET ( &cqe->error, vendor_error_syndrome ) );
 
1734
                rc = -EIO;
 
1735
                /* Don't return immediately; propagate error to completer */
 
1736
        }
 
1737
 
 
1738
        /* Identify work queue */
 
1739
        wq = ib_find_wq ( cq, qpn, is_send );
 
1740
        if ( ! wq ) {
 
1741
                DBGC ( hermon, "Hermon %p CQN %#lx unknown %s QPN %#lx\n",
 
1742
                       hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
 
1743
                return -EIO;
 
1744
        }
 
1745
        qp = wq->qp;
 
1746
        hermon_qp = ib_qp_get_drvdata ( qp );
 
1747
 
 
1748
        /* Identify work queue entry */
 
1749
        wqe_idx = MLX_GET ( &cqe->normal, wqe_counter );
 
1750
        wqe_idx_mask = ( wq->num_wqes - 1 );
 
1751
        DBGCP ( hermon, "Hermon %p CQN %#lx QPN %#lx %s WQE %#lx completed:\n",
 
1752
                hermon, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
 
1753
                wqe_idx );
 
1754
        DBGCP_HDA ( hermon, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
 
1755
 
 
1756
        /* Identify I/O buffer */
 
1757
        iobuf = wq->iobufs[ wqe_idx & wqe_idx_mask ];
 
1758
        if ( ! iobuf ) {
 
1759
                DBGC ( hermon, "Hermon %p CQN %#lx QPN %#lx empty %s WQE "
 
1760
                       "%#lx\n", hermon, cq->cqn, qp->qpn,
 
1761
                       ( is_send ? "send" : "recv" ), wqe_idx );
 
1762
                return -EIO;
 
1763
        }
 
1764
        wq->iobufs[ wqe_idx & wqe_idx_mask ] = NULL;
 
1765
 
 
1766
        if ( is_send ) {
 
1767
                /* Hand off to completion handler */
 
1768
                ib_complete_send ( ibdev, qp, iobuf, rc );
 
1769
        } else {
 
1770
                /* Set received length */
 
1771
                len = MLX_GET ( &cqe->normal, byte_cnt );
 
1772
                memset ( &recv_dest, 0, sizeof ( recv_dest ) );
 
1773
                recv_dest.qpn = qpn;
 
1774
                memset ( &recv_source, 0, sizeof ( recv_source ) );
 
1775
                switch ( qp->type ) {
 
1776
                case IB_QPT_SMI:
 
1777
                case IB_QPT_GSI:
 
1778
                case IB_QPT_UD:
 
1779
                        /* Locate corresponding GRH */
 
1780
                        assert ( hermon_qp->recv.grh != NULL );
 
1781
                        grh = &hermon_qp->recv.grh[ wqe_idx & wqe_idx_mask ];
 
1782
                        len -= sizeof ( *grh );
 
1783
                        /* Construct address vector */
 
1784
                        source = &recv_source;
 
1785
                        source->qpn = MLX_GET ( &cqe->normal, srq_rqpn );
 
1786
                        source->lid = MLX_GET ( &cqe->normal, slid_smac47_32 );
 
1787
                        source->sl = MLX_GET ( &cqe->normal, sl );
 
1788
                        recv_dest.gid_present = source->gid_present =
 
1789
                                MLX_GET ( &cqe->normal, g );
 
1790
                        memcpy ( &recv_dest.gid, &grh->dgid,
 
1791
                                 sizeof ( recv_dest.gid ) );
 
1792
                        memcpy ( &source->gid, &grh->sgid,
 
1793
                                 sizeof ( source->gid ) );
 
1794
                        break;
 
1795
                case IB_QPT_RC:
 
1796
                        source = &qp->av;
 
1797
                        break;
 
1798
                case IB_QPT_ETH:
 
1799
                        /* Construct address vector */
 
1800
                        source = &recv_source;
 
1801
                        source->vlan_present = MLX_GET ( &cqe->normal, vlan );
 
1802
                        source->vlan = MLX_GET ( &cqe->normal, vid );
 
1803
                        break;
 
1804
                default:
 
1805
                        assert ( 0 );
 
1806
                        return -EINVAL;
 
1807
                }
 
1808
                assert ( len <= iob_tailroom ( iobuf ) );
 
1809
                iob_put ( iobuf, len );
 
1810
                /* Hand off to completion handler */
 
1811
                ib_complete_recv ( ibdev, qp, &recv_dest, source, iobuf, rc );
 
1812
        }
 
1813
 
 
1814
        return rc;
 
1815
}
 
1816
 
 
1817
/**
 
1818
 * Poll completion queue
 
1819
 *
 
1820
 * @v ibdev             Infiniband device
 
1821
 * @v cq                Completion queue
 
1822
 */
 
1823
static void hermon_poll_cq ( struct ib_device *ibdev,
 
1824
                             struct ib_completion_queue *cq ) {
 
1825
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
1826
        struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
 
1827
        union hermonprm_completion_entry *cqe;
 
1828
        unsigned int cqe_idx_mask;
 
1829
        int rc;
 
1830
 
 
1831
        while ( 1 ) {
 
1832
                /* Look for completion entry */
 
1833
                cqe_idx_mask = ( cq->num_cqes - 1 );
 
1834
                cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
 
1835
                if ( MLX_GET ( &cqe->normal, owner ) ^
 
1836
                     ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
 
1837
                        /* Entry still owned by hardware; end of poll */
 
1838
                        break;
 
1839
                }
 
1840
 
 
1841
                /* Handle completion */
 
1842
                if ( ( rc = hermon_complete ( ibdev, cq, cqe ) ) != 0 ) {
 
1843
                        DBGC ( hermon, "Hermon %p CQN %#lx failed to complete:"
 
1844
                               " %s\n", hermon, cq->cqn, strerror ( rc ) );
 
1845
                        DBGC_HDA ( hermon, virt_to_phys ( cqe ),
 
1846
                                   cqe, sizeof ( *cqe ) );
 
1847
                }
 
1848
 
 
1849
                /* Update completion queue's index */
 
1850
                cq->next_idx++;
 
1851
 
 
1852
                /* Update doorbell record */
 
1853
                MLX_FILL_1 ( hermon_cq->doorbell, 0, update_ci,
 
1854
                             ( cq->next_idx & 0x00ffffffUL ) );
 
1855
        }
 
1856
}
 
1857
 
 
1858
/***************************************************************************
 
1859
 *
 
1860
 * Event queues
 
1861
 *
 
1862
 ***************************************************************************
 
1863
 */
 
1864
 
 
1865
/**
 
1866
 * Create event queue
 
1867
 *
 
1868
 * @v hermon            Hermon device
 
1869
 * @ret rc              Return status code
 
1870
 */
 
1871
static int hermon_create_eq ( struct hermon *hermon ) {
 
1872
        struct hermon_event_queue *hermon_eq = &hermon->eq;
 
1873
        struct hermonprm_eqc eqctx;
 
1874
        struct hermonprm_event_mask mask;
 
1875
        unsigned int i;
 
1876
        int rc;
 
1877
 
 
1878
        /* Select event queue number */
 
1879
        hermon_eq->eqn = ( 4 * hermon->cap.reserved_uars );
 
1880
        if ( hermon_eq->eqn < hermon->cap.reserved_eqs )
 
1881
                hermon_eq->eqn = hermon->cap.reserved_eqs;
 
1882
 
 
1883
        /* Calculate doorbell address */
 
1884
        hermon_eq->doorbell =
 
1885
                ( hermon->uar + HERMON_DB_EQ_OFFSET ( hermon_eq->eqn ) );
 
1886
 
 
1887
        /* Allocate event queue itself */
 
1888
        hermon_eq->eqe_size =
 
1889
                ( HERMON_NUM_EQES * sizeof ( hermon_eq->eqe[0] ) );
 
1890
        hermon_eq->eqe = malloc_dma ( hermon_eq->eqe_size,
 
1891
                                      sizeof ( hermon_eq->eqe[0] ) );
 
1892
        if ( ! hermon_eq->eqe ) {
 
1893
                rc = -ENOMEM;
 
1894
                goto err_eqe;
 
1895
        }
 
1896
        memset ( hermon_eq->eqe, 0, hermon_eq->eqe_size );
 
1897
        for ( i = 0 ; i < HERMON_NUM_EQES ; i++ ) {
 
1898
                MLX_FILL_1 ( &hermon_eq->eqe[i].generic, 7, owner, 1 );
 
1899
        }
 
1900
        barrier();
 
1901
 
 
1902
        /* Allocate MTT entries */
 
1903
        if ( ( rc = hermon_alloc_mtt ( hermon, hermon_eq->eqe,
 
1904
                                       hermon_eq->eqe_size,
 
1905
                                       &hermon_eq->mtt ) ) != 0 )
 
1906
                goto err_alloc_mtt;
 
1907
 
 
1908
        /* Hand queue over to hardware */
 
1909
        memset ( &eqctx, 0, sizeof ( eqctx ) );
 
1910
        MLX_FILL_2 ( &eqctx, 0,
 
1911
                     st, 0xa /* "Fired" */,
 
1912
                     oi, 1 );
 
1913
        MLX_FILL_1 ( &eqctx, 2,
 
1914
                     page_offset, ( hermon_eq->mtt.page_offset >> 5 ) );
 
1915
        MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( HERMON_NUM_EQES - 1 ) );
 
1916
        MLX_FILL_H ( &eqctx, 6, mtt_base_addr_h,
 
1917
                     hermon_eq->mtt.mtt_base_addr );
 
1918
        MLX_FILL_1 ( &eqctx, 7, mtt_base_addr_l,
 
1919
                     ( hermon_eq->mtt.mtt_base_addr >> 3 ) );
 
1920
        if ( ( rc = hermon_cmd_sw2hw_eq ( hermon, hermon_eq->eqn,
 
1921
                                          &eqctx ) ) != 0 ) {
 
1922
                DBGC ( hermon, "Hermon %p EQN %#lx SW2HW_EQ failed: %s\n",
 
1923
                       hermon, hermon_eq->eqn, strerror ( rc ) );
 
1924
                goto err_sw2hw_eq;
 
1925
        }
 
1926
 
 
1927
        /* Map all events to this event queue */
 
1928
        memset ( &mask, 0xff, sizeof ( mask ) );
 
1929
        if ( ( rc = hermon_cmd_map_eq ( hermon,
 
1930
                                        ( HERMON_MAP_EQ | hermon_eq->eqn ),
 
1931
                                        &mask ) ) != 0 ) {
 
1932
                DBGC ( hermon, "Hermon %p EQN %#lx MAP_EQ failed: %s\n",
 
1933
                       hermon, hermon_eq->eqn, strerror ( rc )  );
 
1934
                goto err_map_eq;
 
1935
        }
 
1936
 
 
1937
        DBGC ( hermon, "Hermon %p EQN %#lx ring [%08lx,%08lx), doorbell "
 
1938
               "%08lx\n", hermon, hermon_eq->eqn,
 
1939
               virt_to_phys ( hermon_eq->eqe ),
 
1940
               ( virt_to_phys ( hermon_eq->eqe ) + hermon_eq->eqe_size ),
 
1941
               virt_to_phys ( hermon_eq->doorbell ) );
 
1942
        return 0;
 
1943
 
 
1944
 err_map_eq:
 
1945
        hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn, &eqctx );
 
1946
 err_sw2hw_eq:
 
1947
        hermon_free_mtt ( hermon, &hermon_eq->mtt );
 
1948
 err_alloc_mtt:
 
1949
        free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
 
1950
 err_eqe:
 
1951
        memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
 
1952
        return rc;
 
1953
}
 
1954
 
 
1955
/**
 
1956
 * Destroy event queue
 
1957
 *
 
1958
 * @v hermon            Hermon device
 
1959
 */
 
1960
static void hermon_destroy_eq ( struct hermon *hermon ) {
 
1961
        struct hermon_event_queue *hermon_eq = &hermon->eq;
 
1962
        struct hermonprm_eqc eqctx;
 
1963
        struct hermonprm_event_mask mask;
 
1964
        int rc;
 
1965
 
 
1966
        /* Unmap events from event queue */
 
1967
        memset ( &mask, 0xff, sizeof ( mask ) );
 
1968
        if ( ( rc = hermon_cmd_map_eq ( hermon,
 
1969
                                        ( HERMON_UNMAP_EQ | hermon_eq->eqn ),
 
1970
                                        &mask ) ) != 0 ) {
 
1971
                DBGC ( hermon, "Hermon %p EQN %#lx FATAL MAP_EQ failed to "
 
1972
                       "unmap: %s\n", hermon, hermon_eq->eqn, strerror ( rc ) );
 
1973
                /* Continue; HCA may die but system should survive */
 
1974
        }
 
1975
 
 
1976
        /* Take ownership back from hardware */
 
1977
        if ( ( rc = hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn,
 
1978
                                          &eqctx ) ) != 0 ) {
 
1979
                DBGC ( hermon, "Hermon %p EQN %#lx FATAL HW2SW_EQ failed: %s\n",
 
1980
                       hermon, hermon_eq->eqn, strerror ( rc ) );
 
1981
                /* Leak memory and return; at least we avoid corruption */
 
1982
                return;
 
1983
        }
 
1984
 
 
1985
        /* Free MTT entries */
 
1986
        hermon_free_mtt ( hermon, &hermon_eq->mtt );
 
1987
 
 
1988
        /* Free memory */
 
1989
        free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
 
1990
        memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
 
1991
}
 
1992
 
 
1993
/**
 
1994
 * Handle port state event
 
1995
 *
 
1996
 * @v hermon            Hermon device
 
1997
 * @v eqe               Port state change event queue entry
 
1998
 */
 
1999
static void hermon_event_port_state_change ( struct hermon *hermon,
 
2000
                                             union hermonprm_event_entry *eqe){
 
2001
        unsigned int port;
 
2002
        int link_up;
 
2003
 
 
2004
        /* Get port and link status */
 
2005
        port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
 
2006
        link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
 
2007
        DBGC ( hermon, "Hermon %p port %d link %s\n", hermon, ( port + 1 ),
 
2008
               ( link_up ? "up" : "down" ) );
 
2009
 
 
2010
        /* Sanity check */
 
2011
        if ( port >= hermon->cap.num_ports ) {
 
2012
                DBGC ( hermon, "Hermon %p port %d does not exist!\n",
 
2013
                       hermon, ( port + 1 ) );
 
2014
                return;
 
2015
        }
 
2016
 
 
2017
        /* Notify device of port state change */
 
2018
        hermon->port[port].type->state_change ( hermon, &hermon->port[port],
 
2019
                                                link_up );
 
2020
}
 
2021
 
 
2022
/**
 
2023
 * Poll event queue
 
2024
 *
 
2025
 * @v ibdev             Infiniband device
 
2026
 */
 
2027
static void hermon_poll_eq ( struct ib_device *ibdev ) {
 
2028
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
2029
        struct hermon_event_queue *hermon_eq = &hermon->eq;
 
2030
        union hermonprm_event_entry *eqe;
 
2031
        union hermonprm_doorbell_register db_reg;
 
2032
        unsigned int eqe_idx_mask;
 
2033
        unsigned int event_type;
 
2034
 
 
2035
        /* No event is generated upon reaching INIT, so we must poll
 
2036
         * separately for link state changes while we remain DOWN.
 
2037
         */
 
2038
        if ( ib_is_open ( ibdev ) &&
 
2039
             ( ibdev->port_state == IB_PORT_STATE_DOWN ) ) {
 
2040
                ib_smc_update ( ibdev, hermon_mad );
 
2041
        }
 
2042
 
 
2043
        /* Poll event queue */
 
2044
        while ( 1 ) {
 
2045
                /* Look for event entry */
 
2046
                eqe_idx_mask = ( HERMON_NUM_EQES - 1 );
 
2047
                eqe = &hermon_eq->eqe[hermon_eq->next_idx & eqe_idx_mask];
 
2048
                if ( MLX_GET ( &eqe->generic, owner ) ^
 
2049
                     ( ( hermon_eq->next_idx & HERMON_NUM_EQES ) ? 1 : 0 ) ) {
 
2050
                        /* Entry still owned by hardware; end of poll */
 
2051
                        break;
 
2052
                }
 
2053
                DBGCP ( hermon, "Hermon %p EQN %#lx event:\n",
 
2054
                        hermon, hermon_eq->eqn );
 
2055
                DBGCP_HDA ( hermon, virt_to_phys ( eqe ),
 
2056
                            eqe, sizeof ( *eqe ) );
 
2057
 
 
2058
                /* Handle event */
 
2059
                event_type = MLX_GET ( &eqe->generic, event_type );
 
2060
                switch ( event_type ) {
 
2061
                case HERMON_EV_PORT_STATE_CHANGE:
 
2062
                        hermon_event_port_state_change ( hermon, eqe );
 
2063
                        break;
 
2064
                default:
 
2065
                        DBGC ( hermon, "Hermon %p EQN %#lx unrecognised event "
 
2066
                               "type %#x:\n",
 
2067
                               hermon, hermon_eq->eqn, event_type );
 
2068
                        DBGC_HDA ( hermon, virt_to_phys ( eqe ),
 
2069
                                   eqe, sizeof ( *eqe ) );
 
2070
                        break;
 
2071
                }
 
2072
 
 
2073
                /* Update event queue's index */
 
2074
                hermon_eq->next_idx++;
 
2075
 
 
2076
                /* Ring doorbell */
 
2077
                MLX_FILL_1 ( &db_reg.event, 0,
 
2078
                             ci, ( hermon_eq->next_idx & 0x00ffffffUL ) );
 
2079
                writel ( db_reg.dword[0], hermon_eq->doorbell );
 
2080
        }
 
2081
}
 
2082
 
 
2083
/***************************************************************************
 
2084
 *
 
2085
 * Firmware control
 
2086
 *
 
2087
 ***************************************************************************
 
2088
 */
 
2089
 
 
2090
/**
 
2091
 * Map virtual to physical address for firmware usage
 
2092
 *
 
2093
 * @v hermon            Hermon device
 
2094
 * @v map               Mapping function
 
2095
 * @v va                Virtual address
 
2096
 * @v pa                Physical address
 
2097
 * @v len               Length of region
 
2098
 * @ret rc              Return status code
 
2099
 */
 
2100
static int hermon_map_vpm ( struct hermon *hermon,
 
2101
                            int ( *map ) ( struct hermon *hermon,
 
2102
                            const struct hermonprm_virtual_physical_mapping* ),
 
2103
                            uint64_t va, physaddr_t pa, size_t len ) {
 
2104
        struct hermonprm_virtual_physical_mapping mapping;
 
2105
        physaddr_t start;
 
2106
        physaddr_t low;
 
2107
        physaddr_t high;
 
2108
        physaddr_t end;
 
2109
        size_t size;
 
2110
        int rc;
 
2111
 
 
2112
        /* Sanity checks */
 
2113
        assert ( ( va & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
 
2114
        assert ( ( pa & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
 
2115
        assert ( ( len & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
 
2116
 
 
2117
        /* Calculate starting points */
 
2118
        start = pa;
 
2119
        end = ( start + len );
 
2120
        size = ( 1UL << ( fls ( start ^ end ) - 1 ) );
 
2121
        low = high = ( end & ~( size - 1 ) );
 
2122
        assert ( start < low );
 
2123
        assert ( high <= end );
 
2124
 
 
2125
        /* These mappings tend to generate huge volumes of
 
2126
         * uninteresting debug data, which basically makes it
 
2127
         * impossible to use debugging otherwise.
 
2128
         */
 
2129
        DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
 
2130
 
 
2131
        /* Map blocks in descending order of size */
 
2132
        while ( size >= HERMON_PAGE_SIZE ) {
 
2133
 
 
2134
                /* Find the next candidate block */
 
2135
                if ( ( low - size ) >= start ) {
 
2136
                        low -= size;
 
2137
                        pa = low;
 
2138
                } else if ( ( high + size ) <= end ) {
 
2139
                        pa = high;
 
2140
                        high += size;
 
2141
                } else {
 
2142
                        size >>= 1;
 
2143
                        continue;
 
2144
                }
 
2145
                assert ( ( va & ( size - 1 ) ) == 0 );
 
2146
                assert ( ( pa & ( size - 1 ) ) == 0 );
 
2147
 
 
2148
                /* Map this block */
 
2149
                memset ( &mapping, 0, sizeof ( mapping ) );
 
2150
                MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
 
2151
                MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
 
2152
                MLX_FILL_H ( &mapping, 2, pa_h, pa );
 
2153
                MLX_FILL_2 ( &mapping, 3,
 
2154
                             log2size, ( ( fls ( size ) - 1 ) - 12 ),
 
2155
                             pa_l, ( pa >> 12 ) );
 
2156
                if ( ( rc = map ( hermon, &mapping ) ) != 0 ) {
 
2157
                        DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
 
2158
                        DBGC ( hermon, "Hermon %p could not map %08llx+%zx to "
 
2159
                               "%08lx: %s\n",
 
2160
                               hermon, va, size, pa, strerror ( rc ) );
 
2161
                        return rc;
 
2162
                }
 
2163
                va += size;
 
2164
        }
 
2165
        assert ( low == start );
 
2166
        assert ( high == end );
 
2167
 
 
2168
        DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
 
2169
        return 0;
 
2170
}
 
2171
 
 
2172
/**
 
2173
 * Start firmware running
 
2174
 *
 
2175
 * @v hermon            Hermon device
 
2176
 * @ret rc              Return status code
 
2177
 */
 
2178
static int hermon_start_firmware ( struct hermon *hermon ) {
 
2179
        struct hermonprm_query_fw fw;
 
2180
        unsigned int fw_pages;
 
2181
        size_t fw_len;
 
2182
        physaddr_t fw_base;
 
2183
        int rc;
 
2184
 
 
2185
        /* Get firmware parameters */
 
2186
        if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
 
2187
                DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
 
2188
                       hermon, strerror ( rc ) );
 
2189
                goto err_query_fw;
 
2190
        }
 
2191
        DBGC ( hermon, "Hermon %p firmware version %d.%d.%d\n", hermon,
 
2192
               MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
 
2193
               MLX_GET ( &fw, fw_rev_subminor ) );
 
2194
        fw_pages = MLX_GET ( &fw, fw_pages );
 
2195
        DBGC ( hermon, "Hermon %p requires %d pages (%d kB) for firmware\n",
 
2196
               hermon, fw_pages, ( fw_pages * 4 ) );
 
2197
 
 
2198
        /* Allocate firmware pages and map firmware area */
 
2199
        fw_len = ( fw_pages * HERMON_PAGE_SIZE );
 
2200
        if ( ! hermon->firmware_area ) {
 
2201
                hermon->firmware_len = fw_len;
 
2202
                hermon->firmware_area = umalloc ( hermon->firmware_len );
 
2203
                if ( ! hermon->firmware_area ) {
 
2204
                        rc = -ENOMEM;
 
2205
                        goto err_alloc_fa;
 
2206
                }
 
2207
        } else {
 
2208
                assert ( hermon->firmware_len == fw_len );
 
2209
        }
 
2210
        fw_base = user_to_phys ( hermon->firmware_area, 0 );
 
2211
        DBGC ( hermon, "Hermon %p firmware area at physical [%08lx,%08lx)\n",
 
2212
               hermon, fw_base, ( fw_base + fw_len ) );
 
2213
        if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_fa,
 
2214
                                     0, fw_base, fw_len ) ) != 0 ) {
 
2215
                DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
 
2216
                       hermon, strerror ( rc ) );
 
2217
                goto err_map_fa;
 
2218
        }
 
2219
 
 
2220
        /* Start firmware */
 
2221
        if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
 
2222
                DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
 
2223
                       hermon, strerror ( rc ) );
 
2224
                goto err_run_fw;
 
2225
        }
 
2226
 
 
2227
        DBGC ( hermon, "Hermon %p firmware started\n", hermon );
 
2228
        return 0;
 
2229
 
 
2230
 err_run_fw:
 
2231
 err_map_fa:
 
2232
        hermon_cmd_unmap_fa ( hermon );
 
2233
 err_alloc_fa:
 
2234
 err_query_fw:
 
2235
        return rc;
 
2236
}
 
2237
 
 
2238
/**
 
2239
 * Stop firmware running
 
2240
 *
 
2241
 * @v hermon            Hermon device
 
2242
 */
 
2243
static void hermon_stop_firmware ( struct hermon *hermon ) {
 
2244
        int rc;
 
2245
 
 
2246
        if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
 
2247
                DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
 
2248
                       hermon, strerror ( rc ) );
 
2249
                /* Leak memory and return; at least we avoid corruption */
 
2250
                hermon->firmware_area = UNULL;
 
2251
                return;
 
2252
        }
 
2253
}
 
2254
 
 
2255
/***************************************************************************
 
2256
 *
 
2257
 * Infinihost Context Memory management
 
2258
 *
 
2259
 ***************************************************************************
 
2260
 */
 
2261
 
 
2262
/**
 
2263
 * Get device limits
 
2264
 *
 
2265
 * @v hermon            Hermon device
 
2266
 * @ret rc              Return status code
 
2267
 */
 
2268
static int hermon_get_cap ( struct hermon *hermon ) {
 
2269
        struct hermonprm_query_dev_cap dev_cap;
 
2270
        int rc;
 
2271
 
 
2272
        if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
 
2273
                DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
 
2274
                       hermon, strerror ( rc ) );
 
2275
                return rc;
 
2276
        }
 
2277
 
 
2278
        hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
 
2279
        hermon->cap.reserved_qps =
 
2280
                ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
 
2281
        hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
 
2282
        hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
 
2283
        hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
 
2284
        hermon->cap.reserved_srqs =
 
2285
                ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
 
2286
        hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
 
2287
        hermon->cap.reserved_cqs =
 
2288
                ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
 
2289
        hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
 
2290
        hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
 
2291
        if ( hermon->cap.reserved_eqs == 0 ) {
 
2292
                /* Backward compatibility */
 
2293
                hermon->cap.reserved_eqs =
 
2294
                        ( 1 << MLX_GET ( &dev_cap, log2_rsvd_eqs ) );
 
2295
        }
 
2296
        hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
 
2297
        hermon->cap.reserved_mtts =
 
2298
                ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
 
2299
        hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
 
2300
        hermon->cap.reserved_mrws =
 
2301
                ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
 
2302
        hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
 
2303
        hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
 
2304
        hermon->cap.num_ports = MLX_GET ( &dev_cap, num_ports );
 
2305
        hermon->cap.dpdp = MLX_GET ( &dev_cap, dpdp );
 
2306
 
 
2307
        /* Sanity check */
 
2308
        if ( hermon->cap.num_ports > HERMON_MAX_PORTS ) {
 
2309
                DBGC ( hermon, "Hermon %p has %d ports (only %d supported)\n",
 
2310
                       hermon, hermon->cap.num_ports, HERMON_MAX_PORTS );
 
2311
                hermon->cap.num_ports = HERMON_MAX_PORTS;
 
2312
        }
 
2313
 
 
2314
        return 0;
 
2315
}
 
2316
 
 
2317
/**
 
2318
 * Align ICM table
 
2319
 *
 
2320
 * @v icm_offset        Current ICM offset
 
2321
 * @v len               ICM table length
 
2322
 * @ret icm_offset      ICM offset
 
2323
 */
 
2324
static uint64_t icm_align ( uint64_t icm_offset, size_t len ) {
 
2325
 
 
2326
        /* Round up to a multiple of the table size */
 
2327
        assert ( len == ( 1UL << ( fls ( len ) - 1 ) ) );
 
2328
        return ( ( icm_offset + len - 1 ) & ~( ( ( uint64_t ) len ) - 1 ) );
 
2329
}
 
2330
 
 
2331
/**
 
2332
 * Map ICM (allocating if necessary)
 
2333
 *
 
2334
 * @v hermon            Hermon device
 
2335
 * @v init_hca          INIT_HCA structure to fill in
 
2336
 * @ret rc              Return status code
 
2337
 */
 
2338
static int hermon_map_icm ( struct hermon *hermon,
 
2339
                            struct hermonprm_init_hca *init_hca ) {
 
2340
        struct hermonprm_scalar_parameter icm_size;
 
2341
        struct hermonprm_scalar_parameter icm_aux_size;
 
2342
        uint64_t icm_offset = 0;
 
2343
        unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
 
2344
        unsigned int log_num_mtts, log_num_mpts, log_num_mcs;
 
2345
        size_t cmpt_max_len;
 
2346
        size_t icm_len, icm_aux_len;
 
2347
        size_t len;
 
2348
        physaddr_t icm_phys;
 
2349
        int i;
 
2350
        int rc;
 
2351
 
 
2352
        /*
 
2353
         * Start by carving up the ICM virtual address space
 
2354
         *
 
2355
         */
 
2356
 
 
2357
        /* Calculate number of each object type within ICM */
 
2358
        log_num_qps = fls ( hermon->cap.reserved_qps +
 
2359
                            HERMON_RSVD_SPECIAL_QPS + HERMON_MAX_QPS - 1 );
 
2360
        log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
 
2361
        log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
 
2362
        log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
 
2363
        log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
 
2364
        log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
 
2365
        log_num_mcs = HERMON_LOG_MULTICAST_HASH_SIZE;
 
2366
 
 
2367
        /* ICM starts with the cMPT tables, which are sparse */
 
2368
        cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
 
2369
                         ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
 
2370
        len = ( ( ( ( 1 << log_num_qps ) * hermon->cap.cmpt_entry_size ) +
 
2371
                  HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
 
2372
        hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
 
2373
        hermon->icm_map[HERMON_ICM_QP_CMPT].len = len;
 
2374
        icm_offset += cmpt_max_len;
 
2375
        len = ( ( ( ( 1 << log_num_srqs ) * hermon->cap.cmpt_entry_size ) +
 
2376
                  HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
 
2377
        hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
 
2378
        hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = len;
 
2379
        icm_offset += cmpt_max_len;
 
2380
        len = ( ( ( ( 1 << log_num_cqs ) * hermon->cap.cmpt_entry_size ) +
 
2381
                  HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
 
2382
        hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
 
2383
        hermon->icm_map[HERMON_ICM_CQ_CMPT].len = len;
 
2384
        icm_offset += cmpt_max_len;
 
2385
        len = ( ( ( ( 1 << log_num_eqs ) * hermon->cap.cmpt_entry_size ) +
 
2386
                  HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
 
2387
        hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
 
2388
        hermon->icm_map[HERMON_ICM_EQ_CMPT].len = len;
 
2389
        icm_offset += cmpt_max_len;
 
2390
 
 
2391
        hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
 
2392
 
 
2393
        /* Queue pair contexts */
 
2394
        len = ( ( 1 << log_num_qps ) * hermon->cap.qpc_entry_size );
 
2395
        icm_offset = icm_align ( icm_offset, len );
 
2396
        MLX_FILL_1 ( init_hca, 12,
 
2397
                     qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
 
2398
                     ( icm_offset >> 32 ) );
 
2399
        MLX_FILL_2 ( init_hca, 13,
 
2400
                     qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
 
2401
                     ( icm_offset >> 5 ),
 
2402
                     qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
 
2403
                     log_num_qps );
 
2404
        DBGC ( hermon, "Hermon %p ICM QPC is %d x %#zx at [%08llx,%08llx)\n",
 
2405
               hermon, ( 1 << log_num_qps ), hermon->cap.qpc_entry_size,
 
2406
               icm_offset, ( icm_offset + len ) );
 
2407
        icm_offset += len;
 
2408
 
 
2409
        /* Extended alternate path contexts */
 
2410
        len = ( ( 1 << log_num_qps ) * hermon->cap.altc_entry_size );
 
2411
        icm_offset = icm_align ( icm_offset, len );
 
2412
        MLX_FILL_1 ( init_hca, 24,
 
2413
                     qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
 
2414
                     ( icm_offset >> 32 ) );
 
2415
        MLX_FILL_1 ( init_hca, 25,
 
2416
                     qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
 
2417
                     icm_offset );
 
2418
        DBGC ( hermon, "Hermon %p ICM ALTC is %d x %#zx at [%08llx,%08llx)\n",
 
2419
               hermon, ( 1 << log_num_qps ), hermon->cap.altc_entry_size,
 
2420
               icm_offset, ( icm_offset + len ) );
 
2421
        icm_offset += len;
 
2422
 
 
2423
        /* Extended auxiliary contexts */
 
2424
        len = ( ( 1 << log_num_qps ) * hermon->cap.auxc_entry_size );
 
2425
        icm_offset = icm_align ( icm_offset, len );
 
2426
        MLX_FILL_1 ( init_hca, 28,
 
2427
                     qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
 
2428
                     ( icm_offset >> 32 ) );
 
2429
        MLX_FILL_1 ( init_hca, 29,
 
2430
                     qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
 
2431
                     icm_offset );
 
2432
        DBGC ( hermon, "Hermon %p ICM AUXC is %d x %#zx at [%08llx,%08llx)\n",
 
2433
               hermon, ( 1 << log_num_qps ), hermon->cap.auxc_entry_size,
 
2434
               icm_offset, ( icm_offset + len ) );
 
2435
        icm_offset += len;
 
2436
 
 
2437
        /* Shared receive queue contexts */
 
2438
        len = ( ( 1 << log_num_srqs ) * hermon->cap.srqc_entry_size );
 
2439
        icm_offset = icm_align ( icm_offset, len );
 
2440
        MLX_FILL_1 ( init_hca, 18,
 
2441
                     qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
 
2442
                     ( icm_offset >> 32 ) );
 
2443
        MLX_FILL_2 ( init_hca, 19,
 
2444
                     qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
 
2445
                     ( icm_offset >> 5 ),
 
2446
                     qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
 
2447
                     log_num_srqs );
 
2448
        DBGC ( hermon, "Hermon %p ICM SRQC is %d x %#zx at [%08llx,%08llx)\n",
 
2449
               hermon, ( 1 << log_num_srqs ), hermon->cap.srqc_entry_size,
 
2450
               icm_offset, ( icm_offset + len ) );
 
2451
        icm_offset += len;
 
2452
 
 
2453
        /* Completion queue contexts */
 
2454
        len = ( ( 1 << log_num_cqs ) * hermon->cap.cqc_entry_size );
 
2455
        icm_offset = icm_align ( icm_offset, len );
 
2456
        MLX_FILL_1 ( init_hca, 20,
 
2457
                     qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
 
2458
                     ( icm_offset >> 32 ) );
 
2459
        MLX_FILL_2 ( init_hca, 21,
 
2460
                     qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
 
2461
                     ( icm_offset >> 5 ),
 
2462
                     qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
 
2463
                     log_num_cqs );
 
2464
        DBGC ( hermon, "Hermon %p ICM CQC is %d x %#zx at [%08llx,%08llx)\n",
 
2465
               hermon, ( 1 << log_num_cqs ), hermon->cap.cqc_entry_size,
 
2466
               icm_offset, ( icm_offset + len ) );
 
2467
        icm_offset += len;
 
2468
 
 
2469
        /* Event queue contexts */
 
2470
        len = ( ( 1 << log_num_eqs ) * hermon->cap.eqc_entry_size );
 
2471
        icm_offset = icm_align ( icm_offset, len );
 
2472
        MLX_FILL_1 ( init_hca, 32,
 
2473
                     qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
 
2474
                     ( icm_offset >> 32 ) );
 
2475
        MLX_FILL_2 ( init_hca, 33,
 
2476
                     qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
 
2477
                     ( icm_offset >> 5 ),
 
2478
                     qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
 
2479
                     log_num_eqs );
 
2480
        DBGC ( hermon, "Hermon %p ICM EQC is %d x %#zx at [%08llx,%08llx)\n",
 
2481
               hermon, ( 1 << log_num_eqs ), hermon->cap.eqc_entry_size,
 
2482
               icm_offset, ( icm_offset + len ) );
 
2483
        icm_offset += len;
 
2484
 
 
2485
        /* Memory translation table */
 
2486
        len = ( ( 1 << log_num_mtts ) * hermon->cap.mtt_entry_size );
 
2487
        icm_offset = icm_align ( icm_offset, len );
 
2488
        MLX_FILL_1 ( init_hca, 64,
 
2489
                     tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
 
2490
        MLX_FILL_1 ( init_hca, 65,
 
2491
                     tpt_parameters.mtt_base_addr_l, icm_offset );
 
2492
        DBGC ( hermon, "Hermon %p ICM MTT is %d x %#zx at [%08llx,%08llx)\n",
 
2493
               hermon, ( 1 << log_num_mtts ), hermon->cap.mtt_entry_size,
 
2494
               icm_offset, ( icm_offset + len ) );
 
2495
        icm_offset += len;
 
2496
 
 
2497
        /* Memory protection table */
 
2498
        len = ( ( 1 << log_num_mpts ) * hermon->cap.dmpt_entry_size );
 
2499
        icm_offset = icm_align ( icm_offset, len );
 
2500
        MLX_FILL_1 ( init_hca, 60,
 
2501
                     tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
 
2502
        MLX_FILL_1 ( init_hca, 61,
 
2503
                     tpt_parameters.dmpt_base_adr_l, icm_offset );
 
2504
        MLX_FILL_1 ( init_hca, 62,
 
2505
                     tpt_parameters.log_dmpt_sz, log_num_mpts );
 
2506
        DBGC ( hermon, "Hermon %p ICM DMPT is %d x %#zx at [%08llx,%08llx)\n",
 
2507
               hermon, ( 1 << log_num_mpts ), hermon->cap.dmpt_entry_size,
 
2508
               icm_offset, ( icm_offset + len ) );
 
2509
        icm_offset += len;
 
2510
 
 
2511
        /* Multicast table */
 
2512
        len = ( ( 1 << log_num_mcs ) * sizeof ( struct hermonprm_mcg_entry ) );
 
2513
        icm_offset = icm_align ( icm_offset, len );
 
2514
        MLX_FILL_1 ( init_hca, 48,
 
2515
                     multicast_parameters.mc_base_addr_h,
 
2516
                     ( icm_offset >> 32 ) );
 
2517
        MLX_FILL_1 ( init_hca, 49,
 
2518
                     multicast_parameters.mc_base_addr_l, icm_offset );
 
2519
        MLX_FILL_1 ( init_hca, 52,
 
2520
                     multicast_parameters.log_mc_table_entry_sz,
 
2521
                     fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
 
2522
        MLX_FILL_1 ( init_hca, 53,
 
2523
                     multicast_parameters.log_mc_table_hash_sz, log_num_mcs );
 
2524
        MLX_FILL_1 ( init_hca, 54,
 
2525
                     multicast_parameters.log_mc_table_sz, log_num_mcs );
 
2526
        DBGC ( hermon, "Hermon %p ICM MC is %d x %#zx at [%08llx,%08llx)\n",
 
2527
               hermon, ( 1 << log_num_mcs ),
 
2528
               sizeof ( struct hermonprm_mcg_entry ),
 
2529
               icm_offset, ( icm_offset + len ) );
 
2530
        icm_offset += len;
 
2531
 
 
2532
 
 
2533
        hermon->icm_map[HERMON_ICM_OTHER].len =
 
2534
                ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
 
2535
 
 
2536
        /*
 
2537
         * Allocate and map physical memory for (portions of) ICM
 
2538
         *
 
2539
         * Map is:
 
2540
         *   ICM AUX area (aligned to its own size)
 
2541
         *   cMPT areas
 
2542
         *   Other areas
 
2543
         */
 
2544
 
 
2545
        /* Calculate physical memory required for ICM */
 
2546
        icm_len = 0;
 
2547
        for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
 
2548
                icm_len += hermon->icm_map[i].len;
 
2549
        }
 
2550
 
 
2551
        /* Get ICM auxiliary area size */
 
2552
        memset ( &icm_size, 0, sizeof ( icm_size ) );
 
2553
        MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
 
2554
        MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
 
2555
        if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
 
2556
                                              &icm_aux_size ) ) != 0 ) {
 
2557
                DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
 
2558
                       hermon, strerror ( rc ) );
 
2559
                goto err_set_icm_size;
 
2560
        }
 
2561
        icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
 
2562
 
 
2563
        /* Allocate ICM data and auxiliary area */
 
2564
        DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
 
2565
               hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
 
2566
        if ( ! hermon->icm ) {
 
2567
                hermon->icm_len = icm_len;
 
2568
                hermon->icm_aux_len = icm_aux_len;
 
2569
                hermon->icm = umalloc ( hermon->icm_aux_len + hermon->icm_len );
 
2570
                if ( ! hermon->icm ) {
 
2571
                        rc = -ENOMEM;
 
2572
                        goto err_alloc;
 
2573
                }
 
2574
        } else {
 
2575
                assert ( hermon->icm_len == icm_len );
 
2576
                assert ( hermon->icm_aux_len == icm_aux_len );
 
2577
        }
 
2578
        icm_phys = user_to_phys ( hermon->icm, 0 );
 
2579
 
 
2580
        /* Map ICM auxiliary area */
 
2581
        DBGC ( hermon, "Hermon %p mapping ICM AUX => %08lx\n",
 
2582
               hermon, icm_phys );
 
2583
        if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm_aux,
 
2584
                                     0, icm_phys, icm_aux_len ) ) != 0 ) {
 
2585
                DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
 
2586
                       hermon, strerror ( rc ) );
 
2587
                goto err_map_icm_aux;
 
2588
        }
 
2589
        icm_phys += icm_aux_len;
 
2590
 
 
2591
        /* MAP ICM area */
 
2592
        for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
 
2593
                DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx => %08lx\n",
 
2594
                       hermon, hermon->icm_map[i].offset,
 
2595
                       hermon->icm_map[i].len, icm_phys );
 
2596
                if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm,
 
2597
                                             hermon->icm_map[i].offset,
 
2598
                                             icm_phys,
 
2599
                                             hermon->icm_map[i].len ) ) != 0 ){
 
2600
                        DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
 
2601
                               hermon, strerror ( rc ) );
 
2602
                        goto err_map_icm;
 
2603
                }
 
2604
                icm_phys += hermon->icm_map[i].len;
 
2605
        }
 
2606
 
 
2607
        return 0;
 
2608
 
 
2609
 err_map_icm:
 
2610
        assert ( i == 0 ); /* We don't handle partial failure at present */
 
2611
 err_map_icm_aux:
 
2612
        hermon_cmd_unmap_icm_aux ( hermon );
 
2613
 err_alloc:
 
2614
 err_set_icm_size:
 
2615
        return rc;
 
2616
}
 
2617
 
 
2618
/**
 
2619
 * Unmap ICM
 
2620
 *
 
2621
 * @v hermon            Hermon device
 
2622
 */
 
2623
static void hermon_unmap_icm ( struct hermon *hermon ) {
 
2624
        struct hermonprm_scalar_parameter unmap_icm;
 
2625
        int i;
 
2626
 
 
2627
        for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
 
2628
                memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
 
2629
                MLX_FILL_1 ( &unmap_icm, 0, value_hi,
 
2630
                             ( hermon->icm_map[i].offset >> 32 ) );
 
2631
                MLX_FILL_1 ( &unmap_icm, 1, value,
 
2632
                             hermon->icm_map[i].offset );
 
2633
                hermon_cmd_unmap_icm ( hermon,
 
2634
                                       ( 1 << fls ( ( hermon->icm_map[i].len /
 
2635
                                                      HERMON_PAGE_SIZE ) - 1)),
 
2636
                                       &unmap_icm );
 
2637
        }
 
2638
        hermon_cmd_unmap_icm_aux ( hermon );
 
2639
}
 
2640
 
 
2641
/***************************************************************************
 
2642
 *
 
2643
 * Initialisation and teardown
 
2644
 *
 
2645
 ***************************************************************************
 
2646
 */
 
2647
 
 
2648
/**
 
2649
 * Reset device
 
2650
 *
 
2651
 * @v hermon            Hermon device
 
2652
 */
 
2653
static void hermon_reset ( struct hermon *hermon ) {
 
2654
        struct pci_device *pci = hermon->pci;
 
2655
        struct pci_config_backup backup;
 
2656
        static const uint8_t backup_exclude[] =
 
2657
                PCI_CONFIG_BACKUP_EXCLUDE ( 0x58, 0x5c );
 
2658
 
 
2659
        /* Perform device reset and preserve PCI configuration */
 
2660
        pci_backup ( pci, &backup, backup_exclude );
 
2661
        writel ( HERMON_RESET_MAGIC,
 
2662
                 ( hermon->config + HERMON_RESET_OFFSET ) );
 
2663
        mdelay ( HERMON_RESET_WAIT_TIME_MS );
 
2664
        pci_restore ( pci, &backup, backup_exclude );
 
2665
 
 
2666
        /* Reset command interface toggle */
 
2667
        hermon->toggle = 0;
 
2668
}
 
2669
 
 
2670
/**
 
2671
 * Set up memory protection table
 
2672
 *
 
2673
 * @v hermon            Hermon device
 
2674
 * @ret rc              Return status code
 
2675
 */
 
2676
static int hermon_setup_mpt ( struct hermon *hermon ) {
 
2677
        struct hermonprm_mpt mpt;
 
2678
        uint32_t key;
 
2679
        int rc;
 
2680
 
 
2681
        /* Derive key */
 
2682
        key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
 
2683
        hermon->lkey = ( ( key << 8 ) | ( key >> 24 ) );
 
2684
 
 
2685
        /* Initialise memory protection table */
 
2686
        memset ( &mpt, 0, sizeof ( mpt ) );
 
2687
        MLX_FILL_7 ( &mpt, 0,
 
2688
                     atomic, 1,
 
2689
                     rw, 1,
 
2690
                     rr, 1,
 
2691
                     lw, 1,
 
2692
                     lr, 1,
 
2693
                     pa, 1,
 
2694
                     r_w, 1 );
 
2695
        MLX_FILL_1 ( &mpt, 2, mem_key, key );
 
2696
        MLX_FILL_1 ( &mpt, 3,
 
2697
                     pd, HERMON_GLOBAL_PD );
 
2698
        MLX_FILL_1 ( &mpt, 10, len64, 1 );
 
2699
        if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
 
2700
                                           hermon->cap.reserved_mrws,
 
2701
                                           &mpt ) ) != 0 ) {
 
2702
                DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
 
2703
                       hermon, strerror ( rc ) );
 
2704
                return rc;
 
2705
        }
 
2706
 
 
2707
        return 0;
 
2708
}
 
2709
 
 
2710
/**
 
2711
 * Configure special queue pairs
 
2712
 *
 
2713
 * @v hermon            Hermon device
 
2714
 * @ret rc              Return status code
 
2715
 */
 
2716
static int hermon_configure_special_qps ( struct hermon *hermon ) {
 
2717
        int rc;
 
2718
 
 
2719
        /* Special QP block must be aligned on its own size */
 
2720
        hermon->special_qpn_base = ( ( hermon->cap.reserved_qps +
 
2721
                                       HERMON_NUM_SPECIAL_QPS - 1 )
 
2722
                                     & ~( HERMON_NUM_SPECIAL_QPS - 1 ) );
 
2723
        hermon->qpn_base = ( hermon->special_qpn_base +
 
2724
                             HERMON_NUM_SPECIAL_QPS );
 
2725
        DBGC ( hermon, "Hermon %p special QPs at [%lx,%lx]\n", hermon,
 
2726
               hermon->special_qpn_base, ( hermon->qpn_base - 1 ) );
 
2727
 
 
2728
        /* Issue command to configure special QPs */
 
2729
        if ( ( rc = hermon_cmd_conf_special_qp ( hermon, 0x00,
 
2730
                                          hermon->special_qpn_base ) ) != 0 ) {
 
2731
                DBGC ( hermon, "Hermon %p could not configure special QPs: "
 
2732
                       "%s\n", hermon, strerror ( rc ) );
 
2733
                return rc;
 
2734
        }
 
2735
 
 
2736
        return 0;
 
2737
}
 
2738
 
 
2739
/**
 
2740
 * Start Hermon device
 
2741
 *
 
2742
 * @v hermon            Hermon device
 
2743
 * @v running           Firmware is already running
 
2744
 * @ret rc              Return status code
 
2745
 */
 
2746
static int hermon_start ( struct hermon *hermon, int running ) {
 
2747
        struct hermonprm_init_hca init_hca;
 
2748
        unsigned int i;
 
2749
        int rc;
 
2750
 
 
2751
        /* Start firmware if not already running */
 
2752
        if ( ! running ) {
 
2753
                if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
 
2754
                        goto err_start_firmware;
 
2755
        }
 
2756
 
 
2757
        /* Allocate and map ICM */
 
2758
        memset ( &init_hca, 0, sizeof ( init_hca ) );
 
2759
        if ( ( rc = hermon_map_icm ( hermon, &init_hca ) ) != 0 )
 
2760
                goto err_map_icm;
 
2761
 
 
2762
        /* Initialise HCA */
 
2763
        MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
 
2764
        MLX_FILL_1 ( &init_hca, 5, udp, 1 );
 
2765
        MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
 
2766
        if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
 
2767
                DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
 
2768
                       hermon, strerror ( rc ) );
 
2769
                goto err_init_hca;
 
2770
        }
 
2771
 
 
2772
        /* Set up memory protection */
 
2773
        if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
 
2774
                goto err_setup_mpt;
 
2775
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ )
 
2776
                hermon->port[i].ibdev->rdma_key = hermon->lkey;
 
2777
 
 
2778
        /* Set up event queue */
 
2779
        if ( ( rc = hermon_create_eq ( hermon ) ) != 0 )
 
2780
                goto err_create_eq;
 
2781
 
 
2782
        /* Configure special QPs */
 
2783
        if ( ( rc = hermon_configure_special_qps ( hermon ) ) != 0 )
 
2784
                goto err_conf_special_qps;
 
2785
 
 
2786
        return 0;
 
2787
 
 
2788
 err_conf_special_qps:
 
2789
        hermon_destroy_eq ( hermon );
 
2790
 err_create_eq:
 
2791
 err_setup_mpt:
 
2792
        hermon_cmd_close_hca ( hermon );
 
2793
 err_init_hca:
 
2794
        hermon_unmap_icm ( hermon );
 
2795
 err_map_icm:
 
2796
        hermon_stop_firmware ( hermon );
 
2797
 err_start_firmware:
 
2798
        return rc;
 
2799
}
 
2800
 
 
2801
/**
 
2802
 * Stop Hermon device
 
2803
 *
 
2804
 * @v hermon            Hermon device
 
2805
 */
 
2806
static void hermon_stop ( struct hermon *hermon ) {
 
2807
        hermon_destroy_eq ( hermon );
 
2808
        hermon_cmd_close_hca ( hermon );
 
2809
        hermon_unmap_icm ( hermon );
 
2810
        hermon_stop_firmware ( hermon );
 
2811
        hermon_reset ( hermon );
 
2812
}
 
2813
 
 
2814
/**
 
2815
 * Open Hermon device
 
2816
 *
 
2817
 * @v hermon            Hermon device
 
2818
 * @ret rc              Return status code
 
2819
 */
 
2820
static int hermon_open ( struct hermon *hermon ) {
 
2821
        int rc;
 
2822
 
 
2823
        /* Start device if applicable */
 
2824
        if ( hermon->open_count == 0 ) {
 
2825
                if ( ( rc = hermon_start ( hermon, 0 ) ) != 0 )
 
2826
                        return rc;
 
2827
        }
 
2828
 
 
2829
        /* Increment open counter */
 
2830
        hermon->open_count++;
 
2831
 
 
2832
        return 0;
 
2833
}
 
2834
 
 
2835
/**
 
2836
 * Close Hermon device
 
2837
 *
 
2838
 * @v hermon            Hermon device
 
2839
 */
 
2840
static void hermon_close ( struct hermon *hermon ) {
 
2841
 
 
2842
        /* Decrement open counter */
 
2843
        assert ( hermon->open_count != 0 );
 
2844
        hermon->open_count--;
 
2845
 
 
2846
        /* Stop device if applicable */
 
2847
        if ( hermon->open_count == 0 )
 
2848
                hermon_stop ( hermon );
 
2849
}
 
2850
 
 
2851
/***************************************************************************
 
2852
 *
 
2853
 * Infiniband link-layer operations
 
2854
 *
 
2855
 ***************************************************************************
 
2856
 */
 
2857
 
 
2858
/**
 
2859
 * Initialise Infiniband link
 
2860
 *
 
2861
 * @v ibdev             Infiniband device
 
2862
 * @ret rc              Return status code
 
2863
 */
 
2864
static int hermon_ib_open ( struct ib_device *ibdev ) {
 
2865
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
2866
        union hermonprm_set_port set_port;
 
2867
        int rc;
 
2868
 
 
2869
        /* Open hardware */
 
2870
        if ( ( rc = hermon_open ( hermon ) ) != 0 )
 
2871
                goto err_open;
 
2872
 
 
2873
        /* Set port parameters */
 
2874
        memset ( &set_port, 0, sizeof ( set_port ) );
 
2875
        MLX_FILL_8 ( &set_port.ib, 0,
 
2876
                     mmc, 1,
 
2877
                     mvc, 1,
 
2878
                     mp, 1,
 
2879
                     mg, 1,
 
2880
                     mtu_cap, IB_MTU_2048,
 
2881
                     vl_cap, IB_VL_0,
 
2882
                     rcm, 1,
 
2883
                     lss, 1 );
 
2884
        MLX_FILL_2 ( &set_port.ib, 10,
 
2885
                     max_pkey, 1,
 
2886
                     max_gid, 1 );
 
2887
        MLX_FILL_1 ( &set_port.ib, 28,
 
2888
                     link_speed_supported, 1 );
 
2889
        if ( ( rc = hermon_cmd_set_port ( hermon, 0, ibdev->port,
 
2890
                                          &set_port ) ) != 0 ) {
 
2891
                DBGC ( hermon, "Hermon %p port %d could not set port: %s\n",
 
2892
                       hermon, ibdev->port, strerror ( rc ) );
 
2893
                goto err_set_port;
 
2894
        }
 
2895
 
 
2896
        /* Initialise port */
 
2897
        if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
 
2898
                DBGC ( hermon, "Hermon %p port %d could not initialise port: "
 
2899
                       "%s\n", hermon, ibdev->port, strerror ( rc ) );
 
2900
                goto err_init_port;
 
2901
        }
 
2902
 
 
2903
        /* Update MAD parameters */
 
2904
        ib_smc_update ( ibdev, hermon_mad );
 
2905
 
 
2906
        return 0;
 
2907
 
 
2908
 err_init_port:
 
2909
 err_set_port:
 
2910
        hermon_close ( hermon );
 
2911
 err_open:
 
2912
        return rc;
 
2913
}
 
2914
 
 
2915
/**
 
2916
 * Close Infiniband link
 
2917
 *
 
2918
 * @v ibdev             Infiniband device
 
2919
 */
 
2920
static void hermon_ib_close ( struct ib_device *ibdev ) {
 
2921
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
2922
        int rc;
 
2923
 
 
2924
        /* Close port */
 
2925
        if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
 
2926
                DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
 
2927
                       hermon, ibdev->port, strerror ( rc ) );
 
2928
                /* Nothing we can do about this */
 
2929
        }
 
2930
 
 
2931
        /* Close hardware */
 
2932
        hermon_close ( hermon );
 
2933
}
 
2934
 
 
2935
/**
 
2936
 * Inform embedded subnet management agent of a received MAD
 
2937
 *
 
2938
 * @v ibdev             Infiniband device
 
2939
 * @v mad               MAD
 
2940
 * @ret rc              Return status code
 
2941
 */
 
2942
static int hermon_inform_sma ( struct ib_device *ibdev,
 
2943
                               union ib_mad *mad ) {
 
2944
        int rc;
 
2945
 
 
2946
        /* Send the MAD to the embedded SMA */
 
2947
        if ( ( rc = hermon_mad ( ibdev, mad ) ) != 0 )
 
2948
                return rc;
 
2949
 
 
2950
        /* Update parameters held in software */
 
2951
        ib_smc_update ( ibdev, hermon_mad );
 
2952
 
 
2953
        return 0;
 
2954
}
 
2955
 
 
2956
/***************************************************************************
 
2957
 *
 
2958
 * Multicast group operations
 
2959
 *
 
2960
 ***************************************************************************
 
2961
 */
 
2962
 
 
2963
/**
 
2964
 * Attach to multicast group
 
2965
 *
 
2966
 * @v ibdev             Infiniband device
 
2967
 * @v qp                Queue pair
 
2968
 * @v gid               Multicast GID
 
2969
 * @ret rc              Return status code
 
2970
 */
 
2971
static int hermon_mcast_attach ( struct ib_device *ibdev,
 
2972
                                 struct ib_queue_pair *qp,
 
2973
                                 union ib_gid *gid ) {
 
2974
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
2975
        struct hermonprm_mgm_hash hash;
 
2976
        struct hermonprm_mcg_entry mcg;
 
2977
        unsigned int index;
 
2978
        int rc;
 
2979
 
 
2980
        /* Generate hash table index */
 
2981
        if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
 
2982
                DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
 
2983
                       hermon, strerror ( rc ) );
 
2984
                return rc;
 
2985
        }
 
2986
        index = MLX_GET ( &hash, hash );
 
2987
 
 
2988
        /* Check for existing hash table entry */
 
2989
        if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
 
2990
                DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
 
2991
                       hermon, index, strerror ( rc ) );
 
2992
                return rc;
 
2993
        }
 
2994
        if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
 
2995
                /* FIXME: this implementation allows only a single QP
 
2996
                 * per multicast group, and doesn't handle hash
 
2997
                 * collisions.  Sufficient for IPoIB but may need to
 
2998
                 * be extended in future.
 
2999
                 */
 
3000
                DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
 
3001
                       hermon, index );
 
3002
                return -EBUSY;
 
3003
        }
 
3004
 
 
3005
        /* Update hash table entry */
 
3006
        MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
 
3007
        MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
 
3008
        memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
 
3009
        if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
 
3010
                DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
 
3011
                       hermon, index, strerror ( rc ) );
 
3012
                return rc;
 
3013
        }
 
3014
 
 
3015
        return 0;
 
3016
}
 
3017
 
 
3018
/**
 
3019
 * Detach from multicast group
 
3020
 *
 
3021
 * @v ibdev             Infiniband device
 
3022
 * @v qp                Queue pair
 
3023
 * @v gid               Multicast GID
 
3024
 */
 
3025
static void hermon_mcast_detach ( struct ib_device *ibdev,
 
3026
                                  struct ib_queue_pair *qp __unused,
 
3027
                                  union ib_gid *gid ) {
 
3028
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
3029
        struct hermonprm_mgm_hash hash;
 
3030
        struct hermonprm_mcg_entry mcg;
 
3031
        unsigned int index;
 
3032
        int rc;
 
3033
 
 
3034
        /* Generate hash table index */
 
3035
        if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
 
3036
                DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
 
3037
                       hermon, strerror ( rc ) );
 
3038
                return;
 
3039
        }
 
3040
        index = MLX_GET ( &hash, hash );
 
3041
 
 
3042
        /* Clear hash table entry */
 
3043
        memset ( &mcg, 0, sizeof ( mcg ) );
 
3044
        if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
 
3045
                DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
 
3046
                       hermon, index, strerror ( rc ) );
 
3047
                return;
 
3048
        }
 
3049
}
 
3050
 
 
3051
/** Hermon Infiniband operations */
 
3052
static struct ib_device_operations hermon_ib_operations = {
 
3053
        .create_cq      = hermon_create_cq,
 
3054
        .destroy_cq     = hermon_destroy_cq,
 
3055
        .create_qp      = hermon_create_qp,
 
3056
        .modify_qp      = hermon_modify_qp,
 
3057
        .destroy_qp     = hermon_destroy_qp,
 
3058
        .post_send      = hermon_post_send,
 
3059
        .post_recv      = hermon_post_recv,
 
3060
        .poll_cq        = hermon_poll_cq,
 
3061
        .poll_eq        = hermon_poll_eq,
 
3062
        .open           = hermon_ib_open,
 
3063
        .close          = hermon_ib_close,
 
3064
        .mcast_attach   = hermon_mcast_attach,
 
3065
        .mcast_detach   = hermon_mcast_detach,
 
3066
        .set_port_info  = hermon_inform_sma,
 
3067
        .set_pkey_table = hermon_inform_sma,
 
3068
};
 
3069
 
 
3070
/**
 
3071
 * Register Hermon Infiniband device
 
3072
 *
 
3073
 * @v hermon            Hermon device
 
3074
 * @v port              Hermon port
 
3075
 * @ret rc              Return status code
 
3076
 */
 
3077
static int hermon_register_ibdev ( struct hermon *hermon,
 
3078
                                   struct hermon_port *port ) {
 
3079
        struct ib_device *ibdev = port->ibdev;
 
3080
        int rc;
 
3081
 
 
3082
        /* Initialise parameters using SMC */
 
3083
        ib_smc_init ( ibdev, hermon_mad );
 
3084
 
 
3085
        /* Register Infiniband device */
 
3086
        if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
 
3087
                DBGC ( hermon, "Hermon %p port %d could not register IB "
 
3088
                       "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
 
3089
                return rc;
 
3090
        }
 
3091
 
 
3092
        return 0;
 
3093
}
 
3094
 
 
3095
/**
 
3096
 * Handle Hermon Infiniband device port state change
 
3097
 *
 
3098
 * @v hermon            Hermon device
 
3099
 * @v port              Hermon port
 
3100
 * @v link_up           Link is up
 
3101
 */
 
3102
static void hermon_state_change_ibdev ( struct hermon *hermon __unused,
 
3103
                                        struct hermon_port *port,
 
3104
                                        int link_up __unused ) {
 
3105
        struct ib_device *ibdev = port->ibdev;
 
3106
 
 
3107
        /* Update MAD parameters */
 
3108
        ib_smc_update ( ibdev, hermon_mad );
 
3109
}
 
3110
 
 
3111
/**
 
3112
 * Unregister Hermon Infiniband device
 
3113
 *
 
3114
 * @v hermon            Hermon device
 
3115
 * @v port              Hermon port
 
3116
 */
 
3117
static void hermon_unregister_ibdev ( struct hermon *hermon __unused,
 
3118
                                      struct hermon_port *port ) {
 
3119
        struct ib_device *ibdev = port->ibdev;
 
3120
 
 
3121
        unregister_ibdev ( ibdev );
 
3122
}
 
3123
 
 
3124
/** Hermon Infiniband port type */
 
3125
static struct hermon_port_type hermon_port_type_ib = {
 
3126
        .register_dev = hermon_register_ibdev,
 
3127
        .state_change = hermon_state_change_ibdev,
 
3128
        .unregister_dev = hermon_unregister_ibdev,
 
3129
};
 
3130
 
 
3131
/***************************************************************************
 
3132
 *
 
3133
 * Ethernet operation
 
3134
 *
 
3135
 ***************************************************************************
 
3136
 */
 
3137
 
 
3138
/** Number of Hermon Ethernet send work queue entries */
 
3139
#define HERMON_ETH_NUM_SEND_WQES 2
 
3140
 
 
3141
/** Number of Hermon Ethernet receive work queue entries */
 
3142
#define HERMON_ETH_NUM_RECV_WQES 4
 
3143
 
 
3144
/** Number of Hermon Ethernet completion entries */
 
3145
#define HERMON_ETH_NUM_CQES 8
 
3146
 
 
3147
/**
 
3148
 * Transmit packet via Hermon Ethernet device
 
3149
 *
 
3150
 * @v netdev            Network device
 
3151
 * @v iobuf             I/O buffer
 
3152
 * @ret rc              Return status code
 
3153
 */
 
3154
static int hermon_eth_transmit ( struct net_device *netdev,
 
3155
                                 struct io_buffer *iobuf ) {
 
3156
        struct hermon_port *port = netdev->priv;
 
3157
        struct ib_device *ibdev = port->ibdev;
 
3158
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
3159
        int rc;
 
3160
 
 
3161
        /* Transmit packet */
 
3162
        if ( ( rc = ib_post_send ( ibdev, port->eth_qp, NULL,
 
3163
                                   iobuf ) ) != 0 ) {
 
3164
                DBGC ( hermon, "Hermon %p port %d could not transmit: %s\n",
 
3165
                       hermon, ibdev->port, strerror ( rc ) );
 
3166
                return rc;
 
3167
        }
 
3168
 
 
3169
        return 0;
 
3170
}
 
3171
 
 
3172
/** Hermon Ethernet queue pair operations */
 
3173
static struct ib_queue_pair_operations hermon_eth_qp_op = {
 
3174
        .alloc_iob = alloc_iob,
 
3175
};
 
3176
 
 
3177
/**
 
3178
 * Handle Hermon Ethernet device send completion
 
3179
 *
 
3180
 * @v ibdev             Infiniband device
 
3181
 * @v qp                Queue pair
 
3182
 * @v iobuf             I/O buffer
 
3183
 * @v rc                Completion status code
 
3184
 */
 
3185
static void hermon_eth_complete_send ( struct ib_device *ibdev __unused,
 
3186
                                       struct ib_queue_pair *qp,
 
3187
                                       struct io_buffer *iobuf, int rc ) {
 
3188
        struct net_device *netdev = ib_qp_get_ownerdata ( qp );
 
3189
 
 
3190
        netdev_tx_complete_err ( netdev, iobuf, rc );
 
3191
}
 
3192
 
 
3193
/**
 
3194
 * Handle Hermon Ethernet device receive completion
 
3195
 *
 
3196
 * @v ibdev             Infiniband device
 
3197
 * @v qp                Queue pair
 
3198
 * @v dest              Destination address vector, or NULL
 
3199
 * @v source            Source address vector, or NULL
 
3200
 * @v iobuf             I/O buffer
 
3201
 * @v rc                Completion status code
 
3202
 */
 
3203
static void hermon_eth_complete_recv ( struct ib_device *ibdev __unused,
 
3204
                                       struct ib_queue_pair *qp,
 
3205
                                       struct ib_address_vector *dest __unused,
 
3206
                                       struct ib_address_vector *source,
 
3207
                                       struct io_buffer *iobuf, int rc ) {
 
3208
        struct net_device *netdev = ib_qp_get_ownerdata ( qp );
 
3209
        struct net_device *vlan;
 
3210
 
 
3211
        /* Find VLAN device, if applicable */
 
3212
        if ( source->vlan_present ) {
 
3213
                if ( ( vlan = vlan_find ( netdev, source->vlan ) ) != NULL ) {
 
3214
                        netdev = vlan;
 
3215
                } else if ( rc == 0 ) {
 
3216
                        rc = -ENODEV;
 
3217
                }
 
3218
        }
 
3219
 
 
3220
        /* Hand off to network layer */
 
3221
        if ( rc == 0 ) {
 
3222
                netdev_rx ( netdev, iobuf );
 
3223
        } else {
 
3224
                netdev_rx_err ( netdev, iobuf, rc );
 
3225
        }
 
3226
}
 
3227
 
 
3228
/** Hermon Ethernet device completion operations */
 
3229
static struct ib_completion_queue_operations hermon_eth_cq_op = {
 
3230
        .complete_send = hermon_eth_complete_send,
 
3231
        .complete_recv = hermon_eth_complete_recv,
 
3232
};
 
3233
 
 
3234
/**
 
3235
 * Poll Hermon Ethernet device
 
3236
 *
 
3237
 * @v netdev            Network device
 
3238
 */
 
3239
static void hermon_eth_poll ( struct net_device *netdev ) {
 
3240
        struct hermon_port *port = netdev->priv;
 
3241
        struct ib_device *ibdev = port->ibdev;
 
3242
 
 
3243
        ib_poll_eq ( ibdev );
 
3244
}
 
3245
 
 
3246
/**
 
3247
 * Open Hermon Ethernet device
 
3248
 *
 
3249
 * @v netdev            Network device
 
3250
 * @ret rc              Return status code
 
3251
 */
 
3252
static int hermon_eth_open ( struct net_device *netdev ) {
 
3253
        struct hermon_port *port = netdev->priv;
 
3254
        struct ib_device *ibdev = port->ibdev;
 
3255
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
3256
        union hermonprm_set_port set_port;
 
3257
        int rc;
 
3258
 
 
3259
        /* Open hardware */
 
3260
        if ( ( rc = hermon_open ( hermon ) ) != 0 )
 
3261
                goto err_open;
 
3262
 
 
3263
        /* Allocate completion queue */
 
3264
        port->eth_cq = ib_create_cq ( ibdev, HERMON_ETH_NUM_CQES,
 
3265
                                      &hermon_eth_cq_op );
 
3266
        if ( ! port->eth_cq ) {
 
3267
                DBGC ( hermon, "Hermon %p port %d could not create completion "
 
3268
                       "queue\n", hermon, ibdev->port );
 
3269
                rc = -ENOMEM;
 
3270
                goto err_create_cq;
 
3271
        }
 
3272
 
 
3273
        /* Allocate queue pair */
 
3274
        port->eth_qp = ib_create_qp ( ibdev, IB_QPT_ETH,
 
3275
                                      HERMON_ETH_NUM_SEND_WQES, port->eth_cq,
 
3276
                                      HERMON_ETH_NUM_RECV_WQES, port->eth_cq,
 
3277
                                      &hermon_eth_qp_op, netdev->name );
 
3278
        if ( ! port->eth_qp ) {
 
3279
                DBGC ( hermon, "Hermon %p port %d could not create queue "
 
3280
                       "pair\n", hermon, ibdev->port );
 
3281
                rc = -ENOMEM;
 
3282
                goto err_create_qp;
 
3283
        }
 
3284
        ib_qp_set_ownerdata ( port->eth_qp, netdev );
 
3285
 
 
3286
        /* Activate queue pair */
 
3287
        if ( ( rc = ib_modify_qp ( ibdev, port->eth_qp ) ) != 0 ) {
 
3288
                DBGC ( hermon, "Hermon %p port %d could not modify queue "
 
3289
                       "pair: %s\n", hermon, ibdev->port, strerror ( rc ) );
 
3290
                goto err_modify_qp;
 
3291
        }
 
3292
 
 
3293
        /* Fill receive rings */
 
3294
        ib_refill_recv ( ibdev, port->eth_qp );
 
3295
 
 
3296
        /* Set port general parameters */
 
3297
        memset ( &set_port, 0, sizeof ( set_port ) );
 
3298
        MLX_FILL_3 ( &set_port.general, 0,
 
3299
                     v_mtu, 1,
 
3300
                     v_pprx, 1,
 
3301
                     v_pptx, 1 );
 
3302
        MLX_FILL_1 ( &set_port.general, 1,
 
3303
                     mtu, ( ETH_FRAME_LEN + 40 /* Used by card */ ) );
 
3304
        MLX_FILL_1 ( &set_port.general, 2,
 
3305
                     pfctx, ( 1 << FCOE_VLAN_PRIORITY ) );
 
3306
        MLX_FILL_1 ( &set_port.general, 3,
 
3307
                     pfcrx, ( 1 << FCOE_VLAN_PRIORITY ) );
 
3308
        if ( ( rc = hermon_cmd_set_port ( hermon, 1,
 
3309
                                          ( HERMON_SET_PORT_GENERAL_PARAM |
 
3310
                                            ibdev->port ),
 
3311
                                          &set_port ) ) != 0 ) {
 
3312
                DBGC ( hermon, "Hermon %p port %d could not set port general "
 
3313
                       "parameters: %s\n",
 
3314
                       hermon, ibdev->port, strerror ( rc ) );
 
3315
                goto err_set_port_general_params;
 
3316
        }
 
3317
 
 
3318
        /* Set port receive QP */
 
3319
        memset ( &set_port, 0, sizeof ( set_port ) );
 
3320
        MLX_FILL_1 ( &set_port.rqp_calc, 0, base_qpn, port->eth_qp->qpn );
 
3321
        MLX_FILL_1 ( &set_port.rqp_calc, 2,
 
3322
                     mac_miss_index, 128 /* MAC misses go to promisc QP */ );
 
3323
        MLX_FILL_2 ( &set_port.rqp_calc, 3,
 
3324
                     vlan_miss_index, 127 /* VLAN misses go to promisc QP */,
 
3325
                     no_vlan_index, 126 /* VLAN-free go to promisc QP */ );
 
3326
        MLX_FILL_2 ( &set_port.rqp_calc, 5,
 
3327
                     promisc_qpn, port->eth_qp->qpn,
 
3328
                     en_uc_promisc, 1 );
 
3329
        MLX_FILL_2 ( &set_port.rqp_calc, 6,
 
3330
                     def_mcast_qpn, port->eth_qp->qpn,
 
3331
                     mc_promisc_mode, 2 /* Receive all multicasts */ );
 
3332
        if ( ( rc = hermon_cmd_set_port ( hermon, 1,
 
3333
                                          ( HERMON_SET_PORT_RECEIVE_QP |
 
3334
                                            ibdev->port ),
 
3335
                                          &set_port ) ) != 0 ) {
 
3336
                DBGC ( hermon, "Hermon %p port %d could not set port receive "
 
3337
                       "QP: %s\n", hermon, ibdev->port, strerror ( rc ) );
 
3338
                goto err_set_port_receive_qp;
 
3339
        }
 
3340
 
 
3341
        /* Initialise port */
 
3342
        if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
 
3343
                DBGC ( hermon, "Hermon %p port %d could not initialise port: "
 
3344
                       "%s\n", hermon, ibdev->port, strerror ( rc ) );
 
3345
                goto err_init_port;
 
3346
        }
 
3347
 
 
3348
        return 0;
 
3349
 
 
3350
 err_init_port:
 
3351
 err_set_port_receive_qp:
 
3352
 err_set_port_general_params:
 
3353
 err_modify_qp:
 
3354
        ib_destroy_qp ( ibdev, port->eth_qp );
 
3355
 err_create_qp:
 
3356
        ib_destroy_cq ( ibdev, port->eth_cq );
 
3357
 err_create_cq:
 
3358
        hermon_close ( hermon );
 
3359
 err_open:
 
3360
        return rc;
 
3361
}
 
3362
 
 
3363
/**
 
3364
 * Close Hermon Ethernet device
 
3365
 *
 
3366
 * @v netdev            Network device
 
3367
 */
 
3368
static void hermon_eth_close ( struct net_device *netdev ) {
 
3369
        struct hermon_port *port = netdev->priv;
 
3370
        struct ib_device *ibdev = port->ibdev;
 
3371
        struct hermon *hermon = ib_get_drvdata ( ibdev );
 
3372
        int rc;
 
3373
 
 
3374
        /* Close port */
 
3375
        if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
 
3376
                DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
 
3377
                       hermon, ibdev->port, strerror ( rc ) );
 
3378
                /* Nothing we can do about this */
 
3379
        }
 
3380
 
 
3381
        /* Tear down the queues */
 
3382
        ib_destroy_qp ( ibdev, port->eth_qp );
 
3383
        ib_destroy_cq ( ibdev, port->eth_cq );
 
3384
 
 
3385
        /* Close hardware */
 
3386
        hermon_close ( hermon );
 
3387
}
 
3388
 
 
3389
/** Hermon Ethernet network device operations */
 
3390
static struct net_device_operations hermon_eth_operations = {
 
3391
        .open           = hermon_eth_open,
 
3392
        .close          = hermon_eth_close,
 
3393
        .transmit       = hermon_eth_transmit,
 
3394
        .poll           = hermon_eth_poll,
 
3395
};
 
3396
 
 
3397
/**
 
3398
 * Register Hermon Ethernet device
 
3399
 *
 
3400
 * @v hermon            Hermon device
 
3401
 * @v port              Hermon port
 
3402
 * @ret rc              Return status code
 
3403
 */
 
3404
static int hermon_register_netdev ( struct hermon *hermon,
 
3405
                                    struct hermon_port *port ) {
 
3406
        struct net_device *netdev = port->netdev;
 
3407
        struct ib_device *ibdev = port->ibdev;
 
3408
        struct hermonprm_query_port_cap query_port;
 
3409
        union {
 
3410
                uint8_t bytes[8];
 
3411
                uint32_t dwords[2];
 
3412
        } mac;
 
3413
        int rc;
 
3414
 
 
3415
        /* Retrieve MAC address */
 
3416
        if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
 
3417
                                            &query_port ) ) != 0 ) {
 
3418
                DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
 
3419
                       hermon, ibdev->port, strerror ( rc ) );
 
3420
                goto err_query_port;
 
3421
        }
 
3422
        mac.dwords[0] = htonl ( MLX_GET ( &query_port, mac_47_32 ) );
 
3423
        mac.dwords[1] = htonl ( MLX_GET ( &query_port, mac_31_0 ) );
 
3424
        memcpy ( netdev->hw_addr,
 
3425
                 &mac.bytes[ sizeof ( mac.bytes ) - ETH_ALEN ], ETH_ALEN );
 
3426
 
 
3427
        /* Register network device */
 
3428
        if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
 
3429
                DBGC ( hermon, "Hermon %p port %d could not register network "
 
3430
                       "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
 
3431
                goto err_register_netdev;
 
3432
        }
 
3433
 
 
3434
        /* Register non-volatile options */
 
3435
        if ( ( rc = register_nvo ( &port->nvo,
 
3436
                                   netdev_settings ( netdev ) ) ) != 0 ) {
 
3437
                DBGC ( hermon, "Hermon %p port %d could not register non-"
 
3438
                       "volatile options: %s\n",
 
3439
                       hermon, ibdev->port, strerror ( rc ) );
 
3440
                goto err_register_nvo;
 
3441
        }
 
3442
 
 
3443
        return 0;
 
3444
 
 
3445
        unregister_nvo ( &port->nvo );
 
3446
 err_register_nvo:
 
3447
        unregister_netdev ( netdev );
 
3448
 err_register_netdev:
 
3449
 err_query_port:
 
3450
        return rc;
 
3451
}
 
3452
 
 
3453
/**
 
3454
 * Handle Hermon Ethernet device port state change
 
3455
 *
 
3456
 * @v hermon            Hermon device
 
3457
 * @v port              Hermon port
 
3458
 * @v link_up           Link is up
 
3459
 */
 
3460
static void hermon_state_change_netdev ( struct hermon *hermon __unused,
 
3461
                                         struct hermon_port *port,
 
3462
                                         int link_up ) {
 
3463
        struct net_device *netdev = port->netdev;
 
3464
 
 
3465
        if ( link_up ) {
 
3466
                netdev_link_up ( netdev );
 
3467
        } else {
 
3468
                netdev_link_down ( netdev );
 
3469
        }
 
3470
}
 
3471
 
 
3472
/**
 
3473
 * Unregister Hermon Ethernet device
 
3474
 *
 
3475
 * @v hermon            Hermon device
 
3476
 * @v port              Hermon port
 
3477
 */
 
3478
static void hermon_unregister_netdev ( struct hermon *hermon __unused,
 
3479
                                       struct hermon_port *port ) {
 
3480
        struct net_device *netdev = port->netdev;
 
3481
 
 
3482
        unregister_nvo ( &port->nvo );
 
3483
        unregister_netdev ( netdev );
 
3484
}
 
3485
 
 
3486
/** Hermon Ethernet port type */
 
3487
static struct hermon_port_type hermon_port_type_eth = {
 
3488
        .register_dev = hermon_register_netdev,
 
3489
        .state_change = hermon_state_change_netdev,
 
3490
        .unregister_dev = hermon_unregister_netdev,
 
3491
};
 
3492
 
 
3493
/***************************************************************************
 
3494
 *
 
3495
 * Port type detection
 
3496
 *
 
3497
 ***************************************************************************
 
3498
 */
 
3499
 
 
3500
/** Timeout for port sensing */
 
3501
#define HERMON_SENSE_PORT_TIMEOUT ( TICKS_PER_SEC / 2 )
 
3502
 
 
3503
/**
 
3504
 * Name port type
 
3505
 *
 
3506
 * @v port_type         Port type
 
3507
 * @v port_type_name    Port type name
 
3508
 */
 
3509
static inline const char * hermon_name_port_type ( unsigned int port_type ) {
 
3510
        switch ( port_type ) {
 
3511
        case HERMON_PORT_TYPE_UNKNOWN:  return "unknown";
 
3512
        case HERMON_PORT_TYPE_IB:       return "Infiniband";
 
3513
        case HERMON_PORT_TYPE_ETH:      return "Ethernet";
 
3514
        default:                        return "INVALID";
 
3515
        }
 
3516
}
 
3517
 
 
3518
/**
 
3519
 * Sense port type
 
3520
 *
 
3521
 * @v hermon            Hermon device
 
3522
 * @v port              Hermon port
 
3523
 * @ret port_type       Port type, or negative error
 
3524
 */
 
3525
static int hermon_sense_port_type ( struct hermon *hermon,
 
3526
                                    struct hermon_port *port ) {
 
3527
        struct ib_device *ibdev = port->ibdev;
 
3528
        struct hermonprm_sense_port sense_port;
 
3529
        int port_type;
 
3530
        int rc;
 
3531
 
 
3532
        /* If DPDP is not supported, always assume Infiniband */
 
3533
        if ( ! hermon->cap.dpdp ) {
 
3534
                port_type = HERMON_PORT_TYPE_IB;
 
3535
                DBGC ( hermon, "Hermon %p port %d does not support DPDP; "
 
3536
                       "assuming an %s network\n", hermon, ibdev->port,
 
3537
                       hermon_name_port_type ( port_type ) );
 
3538
                return port_type;
 
3539
        }
 
3540
 
 
3541
        /* Sense the port type */
 
3542
        if ( ( rc = hermon_cmd_sense_port ( hermon, ibdev->port,
 
3543
                                            &sense_port ) ) != 0 ) {
 
3544
                DBGC ( hermon, "Hermon %p port %d sense failed: %s\n",
 
3545
                       hermon, ibdev->port, strerror ( rc ) );
 
3546
                return rc;
 
3547
        }
 
3548
        port_type = MLX_GET ( &sense_port, port_type );
 
3549
 
 
3550
        DBGC ( hermon, "Hermon %p port %d sensed an %s network\n",
 
3551
               hermon, ibdev->port, hermon_name_port_type ( port_type ) );
 
3552
        return port_type;
 
3553
}
 
3554
 
 
3555
/**
 
3556
 * Set port type
 
3557
 *
 
3558
 * @v hermon            Hermon device
 
3559
 * @v port              Hermon port
 
3560
 * @ret rc              Return status code
 
3561
 */
 
3562
static int hermon_set_port_type ( struct hermon *hermon,
 
3563
                                  struct hermon_port *port ) {
 
3564
        struct ib_device *ibdev = port->ibdev;
 
3565
        struct hermonprm_query_port_cap query_port;
 
3566
        int ib_supported;
 
3567
        int eth_supported;
 
3568
        int port_type;
 
3569
        unsigned long start;
 
3570
        unsigned long elapsed;
 
3571
        int rc;
 
3572
 
 
3573
        /* Check to see which types are supported */
 
3574
        if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
 
3575
                                            &query_port ) ) != 0 ) {
 
3576
                DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
 
3577
                       hermon, ibdev->port, strerror ( rc ) );
 
3578
                return rc;
 
3579
        }
 
3580
        ib_supported = MLX_GET ( &query_port, ib );
 
3581
        eth_supported = MLX_GET ( &query_port, eth );
 
3582
        DBGC ( hermon, "Hermon %p port %d supports%s%s%s\n",
 
3583
               hermon, ibdev->port, ( ib_supported ? " Infiniband" : "" ),
 
3584
               ( ( ib_supported && eth_supported ) ? " and" : "" ),
 
3585
               ( eth_supported ? " Ethernet" : "" ) );
 
3586
 
 
3587
        /* Sense network, if applicable */
 
3588
        if ( ib_supported && eth_supported ) {
 
3589
 
 
3590
                /* Both types are supported; try sensing network */
 
3591
                start = currticks();
 
3592
                do {
 
3593
                        /* Try sensing port */
 
3594
                        port_type = hermon_sense_port_type ( hermon, port );
 
3595
                        if ( port_type < 0 ) {
 
3596
                                rc = port_type;
 
3597
                                return rc;
 
3598
                        }
 
3599
                } while ( ( port_type == HERMON_PORT_TYPE_UNKNOWN ) &&
 
3600
                          ( ( elapsed = ( currticks() - start ) ) <
 
3601
                            HERMON_SENSE_PORT_TIMEOUT ) );
 
3602
 
 
3603
                /* Set port type based on sensed network, defaulting
 
3604
                 * to Infiniband if nothing was sensed.
 
3605
                 */
 
3606
                switch ( port_type ) {
 
3607
                case HERMON_PORT_TYPE_ETH:
 
3608
                        port->type = &hermon_port_type_eth;
 
3609
                        break;
 
3610
                case HERMON_PORT_TYPE_IB:
 
3611
                case HERMON_PORT_TYPE_UNKNOWN:
 
3612
                        port->type = &hermon_port_type_ib;
 
3613
                        break;
 
3614
                default:
 
3615
                        return -EINVAL;
 
3616
                }
 
3617
 
 
3618
        } else if ( eth_supported ) {
 
3619
                port->type = &hermon_port_type_eth;
 
3620
        } else {
 
3621
                port->type = &hermon_port_type_ib;
 
3622
        }
 
3623
 
 
3624
        assert ( port->type != NULL );
 
3625
        return 0;
 
3626
}
 
3627
 
 
3628
/***************************************************************************
 
3629
 *
 
3630
 * BOFM interface
 
3631
 *
 
3632
 ***************************************************************************
 
3633
 */
 
3634
 
 
3635
/**
 
3636
 * Harvest Ethernet MAC for BOFM
 
3637
 *
 
3638
 * @v bofm              BOFM device
 
3639
 * @v mport             Multi-port index
 
3640
 * @v mac               MAC to fill in
 
3641
 * @ret rc              Return status code
 
3642
 */
 
3643
static int hermon_bofm_harvest ( struct bofm_device *bofm, unsigned int mport,
 
3644
                                 uint8_t *mac ) {
 
3645
        struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
 
3646
        struct hermonprm_mod_stat_cfg stat_cfg;
 
3647
        union {
 
3648
                uint8_t bytes[8];
 
3649
                uint32_t dwords[2];
 
3650
        } buf;
 
3651
        int rc;
 
3652
 
 
3653
        /* Query static configuration */
 
3654
        if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
 
3655
                                          HERMON_MOD_STAT_CFG_QUERY,
 
3656
                                          HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
 
3657
                                          &stat_cfg ) ) != 0 ) {
 
3658
                DBGC ( hermon, "Hermon %p port %d could not query "
 
3659
                       "configuration: %s\n", hermon, mport, strerror ( rc ) );
 
3660
                return rc;
 
3661
        }
 
3662
 
 
3663
        /* Retrieve MAC address */
 
3664
        buf.dwords[0] = htonl ( MLX_GET ( &stat_cfg, mac_high ) );
 
3665
        buf.dwords[1] = htonl ( MLX_GET ( &stat_cfg, mac_low ) );
 
3666
        memcpy ( mac, &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ],
 
3667
                 ETH_ALEN );
 
3668
 
 
3669
        DBGC ( hermon, "Hermon %p port %d harvested MAC address %s\n",
 
3670
               hermon, mport, eth_ntoa ( mac ) );
 
3671
 
 
3672
        return 0;
 
3673
}
 
3674
 
 
3675
/**
 
3676
 * Update Ethernet MAC for BOFM
 
3677
 *
 
3678
 * @v bofm              BOFM device
 
3679
 * @v mport             Multi-port index
 
3680
 * @v mac               MAC to fill in
 
3681
 * @ret rc              Return status code
 
3682
 */
 
3683
static int hermon_bofm_update ( struct bofm_device *bofm, unsigned int mport,
 
3684
                                const uint8_t *mac ) {
 
3685
        struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
 
3686
        struct hermonprm_mod_stat_cfg stat_cfg;
 
3687
        union {
 
3688
                uint8_t bytes[8];
 
3689
                uint32_t dwords[2];
 
3690
        } buf;
 
3691
        int rc;
 
3692
 
 
3693
        /* Prepare MAC address */
 
3694
        memset ( &buf, 0, sizeof ( buf ) );
 
3695
        memcpy ( &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ], mac,
 
3696
                 ETH_ALEN );
 
3697
 
 
3698
        /* Modify static configuration */
 
3699
        memset ( &stat_cfg, 0, sizeof ( stat_cfg ) );
 
3700
        MLX_FILL_2 ( &stat_cfg, 36,
 
3701
                     mac_m, 1,
 
3702
                     mac_high, ntohl ( buf.dwords[0] ) );
 
3703
        MLX_FILL_1 ( &stat_cfg, 37, mac_low, ntohl ( buf.dwords[1] ) );
 
3704
        if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
 
3705
                                          HERMON_MOD_STAT_CFG_SET,
 
3706
                                          HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
 
3707
                                          &stat_cfg ) ) != 0 ) {
 
3708
                DBGC ( hermon, "Hermon %p port %d could not modify "
 
3709
                       "configuration: %s\n", hermon, mport, strerror ( rc ) );
 
3710
                return rc;
 
3711
        }
 
3712
 
 
3713
        DBGC ( hermon, "Hermon %p port %d updated MAC address to %s\n",
 
3714
               hermon, mport, eth_ntoa ( mac ) );
 
3715
 
 
3716
        return 0;
 
3717
}
 
3718
 
 
3719
/** Hermon BOFM operations */
 
3720
static struct bofm_operations hermon_bofm_operations = {
 
3721
        .harvest = hermon_bofm_harvest,
 
3722
        .update = hermon_bofm_update,
 
3723
};
 
3724
 
 
3725
/***************************************************************************
 
3726
 *
 
3727
 * PCI interface
 
3728
 *
 
3729
 ***************************************************************************
 
3730
 */
 
3731
 
 
3732
/**
 
3733
 * Allocate Hermon device
 
3734
 *
 
3735
 * @v pci               PCI device
 
3736
 * @v id                PCI ID
 
3737
 * @ret rc              Return status code
 
3738
 */
 
3739
static struct hermon * hermon_alloc ( void ) {
 
3740
        struct hermon *hermon;
 
3741
 
 
3742
        /* Allocate Hermon device */
 
3743
        hermon = zalloc ( sizeof ( *hermon ) );
 
3744
        if ( ! hermon )
 
3745
                goto err_hermon;
 
3746
 
 
3747
        /* Allocate space for mailboxes */
 
3748
        hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
 
3749
                                          HERMON_MBOX_ALIGN );
 
3750
        if ( ! hermon->mailbox_in )
 
3751
                goto err_mailbox_in;
 
3752
        hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
 
3753
                                           HERMON_MBOX_ALIGN );
 
3754
        if ( ! hermon->mailbox_out )
 
3755
                goto err_mailbox_out;
 
3756
 
 
3757
        return hermon;
 
3758
 
 
3759
        free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
 
3760
 err_mailbox_out:
 
3761
        free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
 
3762
 err_mailbox_in:
 
3763
        free ( hermon );
 
3764
 err_hermon:
 
3765
        return NULL;
 
3766
}
 
3767
 
 
3768
/**
 
3769
 * Free Hermon device
 
3770
 *
 
3771
 * @v hermon            Hermon device
 
3772
 */
 
3773
static void hermon_free ( struct hermon *hermon ) {
 
3774
 
 
3775
        ufree ( hermon->icm );
 
3776
        ufree ( hermon->firmware_area );
 
3777
        free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
 
3778
        free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
 
3779
        free ( hermon );
 
3780
}
 
3781
 
 
3782
/**
 
3783
 * Probe PCI device
 
3784
 *
 
3785
 * @v pci               PCI device
 
3786
 * @v id                PCI ID
 
3787
 * @ret rc              Return status code
 
3788
 */
 
3789
static int hermon_probe ( struct pci_device *pci ) {
 
3790
        struct hermon *hermon;
 
3791
        struct ib_device *ibdev;
 
3792
        struct net_device *netdev;
 
3793
        struct hermon_port *port;
 
3794
        unsigned int i;
 
3795
        int rc;
 
3796
 
 
3797
        /* Allocate Hermon device */
 
3798
        hermon = hermon_alloc();
 
3799
        if ( ! hermon ) {
 
3800
                rc = -ENOMEM;
 
3801
                goto err_alloc;
 
3802
        }
 
3803
        pci_set_drvdata ( pci, hermon );
 
3804
        hermon->pci = pci;
 
3805
 
 
3806
        /* Fix up PCI device */
 
3807
        adjust_pci_device ( pci );
 
3808
 
 
3809
        /* Map PCI BARs */
 
3810
        hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR ),
 
3811
                                   HERMON_PCI_CONFIG_BAR_SIZE );
 
3812
        hermon->uar = ioremap ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ),
 
3813
                                HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE );
 
3814
 
 
3815
        /* Reset device */
 
3816
        hermon_reset ( hermon );
 
3817
 
 
3818
        /* Start firmware */
 
3819
        if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
 
3820
                goto err_start_firmware;
 
3821
 
 
3822
        /* Get device limits */
 
3823
        if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
 
3824
                goto err_get_cap;
 
3825
 
 
3826
        /* Allocate Infiniband devices */
 
3827
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
 
3828
                ibdev = alloc_ibdev ( 0 );
 
3829
                if ( ! ibdev ) {
 
3830
                        rc = -ENOMEM;
 
3831
                        goto err_alloc_ibdev;
 
3832
                }
 
3833
                hermon->port[i].ibdev = ibdev;
 
3834
                ibdev->op = &hermon_ib_operations;
 
3835
                ibdev->dev = &pci->dev;
 
3836
                ibdev->port = ( HERMON_PORT_BASE + i );
 
3837
                ib_set_drvdata ( ibdev, hermon );
 
3838
        }
 
3839
 
 
3840
        /* Allocate network devices */
 
3841
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
 
3842
                netdev = alloc_etherdev ( 0 );
 
3843
                if ( ! netdev ) {
 
3844
                        rc = -ENOMEM;
 
3845
                        goto err_alloc_netdev;
 
3846
                }
 
3847
                hermon->port[i].netdev = netdev;
 
3848
                netdev_init ( netdev, &hermon_eth_operations );
 
3849
                netdev->dev = &pci->dev;
 
3850
                netdev->priv = &hermon->port[i];
 
3851
        }
 
3852
 
 
3853
        /* Start device */
 
3854
        if ( ( rc = hermon_start ( hermon, 1 ) ) != 0 )
 
3855
                goto err_start;
 
3856
 
 
3857
        /* Determine port types */
 
3858
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
 
3859
                port = &hermon->port[i];
 
3860
                if ( ( rc = hermon_set_port_type ( hermon, port ) ) != 0 )
 
3861
                        goto err_set_port_type;
 
3862
        }
 
3863
 
 
3864
        /* Initialise non-volatile storage */
 
3865
        nvs_vpd_init ( &hermon->nvsvpd, pci );
 
3866
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
 
3867
                port = &hermon->port[i];
 
3868
                nvs_vpd_nvo_init ( &hermon->nvsvpd,
 
3869
                                   HERMON_VPD_FIELD ( port->ibdev->port ),
 
3870
                                   &port->nvo, NULL );
 
3871
        }
 
3872
 
 
3873
        /* Register devices */
 
3874
        for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
 
3875
                port = &hermon->port[i];
 
3876
                if ( ( rc = port->type->register_dev ( hermon, port ) ) != 0 )
 
3877
                        goto err_register;
 
3878
        }
 
3879
 
 
3880
        /* Leave device quiescent until opened */
 
3881
        if ( hermon->open_count == 0 )
 
3882
                hermon_stop ( hermon );
 
3883
 
 
3884
        return 0;
 
3885
 
 
3886
        i = hermon->cap.num_ports;
 
3887
 err_register:
 
3888
        for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
 
3889
                port = &hermon->port[i];
 
3890
                port->type->unregister_dev ( hermon, port );
 
3891
        }
 
3892
 err_set_port_type:
 
3893
        hermon_stop ( hermon );
 
3894
 err_start:
 
3895
        i = hermon->cap.num_ports;
 
3896
 err_alloc_netdev:
 
3897
        for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
 
3898
                netdev_nullify ( hermon->port[i].netdev );
 
3899
                netdev_put ( hermon->port[i].netdev );
 
3900
        }
 
3901
        i = hermon->cap.num_ports;
 
3902
 err_alloc_ibdev:
 
3903
        for ( i-- ; ( signed int ) i >= 0 ; i-- )
 
3904
                ibdev_put ( hermon->port[i].ibdev );
 
3905
 err_get_cap:
 
3906
        hermon_stop_firmware ( hermon );
 
3907
 err_start_firmware:
 
3908
        iounmap ( hermon->uar );
 
3909
        iounmap ( hermon->config );
 
3910
        hermon_free ( hermon );
 
3911
 err_alloc:
 
3912
        return rc;
 
3913
}
 
3914
 
 
3915
/**
 
3916
 * Remove PCI device
 
3917
 *
 
3918
 * @v pci               PCI device
 
3919
 */
 
3920
static void hermon_remove ( struct pci_device *pci ) {
 
3921
        struct hermon *hermon = pci_get_drvdata ( pci );
 
3922
        struct hermon_port *port;
 
3923
        int i;
 
3924
 
 
3925
        for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
 
3926
                port = &hermon->port[i];
 
3927
                port->type->unregister_dev ( hermon, port );
 
3928
        }
 
3929
        for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
 
3930
                netdev_nullify ( hermon->port[i].netdev );
 
3931
                netdev_put ( hermon->port[i].netdev );
 
3932
        }
 
3933
        for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- )
 
3934
                ibdev_put ( hermon->port[i].ibdev );
 
3935
        iounmap ( hermon->uar );
 
3936
        iounmap ( hermon->config );
 
3937
        hermon_free ( hermon );
 
3938
}
 
3939
 
 
3940
/**
 
3941
 * Probe PCI device for BOFM
 
3942
 *
 
3943
 * @v pci               PCI device
 
3944
 * @v id                PCI ID
 
3945
 * @ret rc              Return status code
 
3946
 */
 
3947
static int hermon_bofm_probe ( struct pci_device *pci ) {
 
3948
        struct hermon *hermon;
 
3949
        int rc;
 
3950
 
 
3951
        /* Allocate Hermon device */
 
3952
        hermon = hermon_alloc();
 
3953
        if ( ! hermon ) {
 
3954
                rc = -ENOMEM;
 
3955
                goto err_alloc;
 
3956
        }
 
3957
        pci_set_drvdata ( pci, hermon );
 
3958
        hermon->pci = pci;
 
3959
 
 
3960
        /* Fix up PCI device */
 
3961
        adjust_pci_device ( pci );
 
3962
 
 
3963
        /* Map PCI BAR */
 
3964
        hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR ),
 
3965
                                   HERMON_PCI_CONFIG_BAR_SIZE );
 
3966
 
 
3967
        /* Initialise BOFM device */
 
3968
        bofm_init ( &hermon->bofm, pci, &hermon_bofm_operations );
 
3969
 
 
3970
        /* Register BOFM device */
 
3971
        if ( ( rc = bofm_register ( &hermon->bofm ) ) != 0 ) {
 
3972
                DBGC ( hermon, "Hermon %p could not register BOFM device: "
 
3973
                       "%s\n", hermon, strerror ( rc ) );
 
3974
                goto err_bofm_register;
 
3975
        }
 
3976
 
 
3977
        return 0;
 
3978
 
 
3979
 err_bofm_register:
 
3980
        iounmap ( hermon->config );
 
3981
        hermon_free ( hermon );
 
3982
 err_alloc:
 
3983
        return rc;
 
3984
}
 
3985
 
 
3986
/**
 
3987
 * Remove PCI device for BOFM
 
3988
 *
 
3989
 * @v pci               PCI device
 
3990
 */
 
3991
static void hermon_bofm_remove ( struct pci_device *pci ) {
 
3992
        struct hermon *hermon = pci_get_drvdata ( pci );
 
3993
 
 
3994
        bofm_unregister ( &hermon->bofm );
 
3995
        iounmap ( hermon->config );
 
3996
        hermon_free ( hermon );
 
3997
}
 
3998
 
 
3999
static struct pci_device_id hermon_nics[] = {
 
4000
        PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver", 0 ),
 
4001
        PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver", 0 ),
 
4002
        PCI_ROM ( 0x15b3, 0x6732, "mt26418", "MT26418 HCA driver", 0 ),
 
4003
        PCI_ROM ( 0x15b3, 0x673c, "mt26428", "MT26428 HCA driver", 0 ),
 
4004
        PCI_ROM ( 0x15b3, 0x6746, "mt26438", "MT26438 HCA driver", 0 ),
 
4005
        PCI_ROM ( 0x15b3, 0x6778, "mt26488", "MT26488 HCA driver", 0 ),
 
4006
        PCI_ROM ( 0x15b3, 0x6368, "mt25448", "MT25448 HCA driver", 0 ),
 
4007
        PCI_ROM ( 0x15b3, 0x6750, "mt26448", "MT26448 HCA driver", 0 ),
 
4008
        PCI_ROM ( 0x15b3, 0x6372, "mt25458", "MT25458 HCA driver", 0 ),
 
4009
        PCI_ROM ( 0x15b3, 0x675a, "mt26458", "MT26458 HCA driver", 0 ),
 
4010
        PCI_ROM ( 0x15b3, 0x6764, "mt26468", "MT26468 HCA driver", 0 ),
 
4011
        PCI_ROM ( 0x15b3, 0x676e, "mt26478", "MT26478 HCA driver", 0 ),
 
4012
};
 
4013
 
 
4014
struct pci_driver hermon_driver __pci_driver = {
 
4015
        .ids = hermon_nics,
 
4016
        .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
 
4017
        .probe = hermon_probe,
 
4018
        .remove = hermon_remove,
 
4019
};
 
4020
 
 
4021
struct pci_driver hermon_bofm_driver __bofm_driver = {
 
4022
        .ids = hermon_nics,
 
4023
        .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
 
4024
        .probe = hermon_bofm_probe,
 
4025
        .remove = hermon_bofm_remove,
 
4026
};