Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / infiniband / arbel.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * Based in part upon the original driver by Mellanox Technologies
5  * Ltd.  Portions may be Copyright (c) Mellanox Technologies Ltd.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  */
22
23 FILE_LICENCE ( GPL2_OR_LATER );
24
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <byteswap.h>
33 #include <ipxe/io.h>
34 #include <ipxe/pci.h>
35 #include <ipxe/pcibackup.h>
36 #include <ipxe/malloc.h>
37 #include <ipxe/umalloc.h>
38 #include <ipxe/iobuf.h>
39 #include <ipxe/netdevice.h>
40 #include <ipxe/infiniband.h>
41 #include <ipxe/ib_smc.h>
42 #include "arbel.h"
43
44 /**
45  * @file
46  *
47  * Mellanox Arbel Infiniband HCA
48  *
49  */
50
51 /***************************************************************************
52  *
53  * Queue number allocation
54  *
55  ***************************************************************************
56  */
57
58 /**
59  * Allocate offset within usage bitmask
60  *
61  * @v bits              Usage bitmask
62  * @v bits_len          Length of usage bitmask
63  * @ret bit             First free bit within bitmask, or negative error
64  */
65 static int arbel_bitmask_alloc ( arbel_bitmask_t *bits,
66                                  unsigned int bits_len ) {
67         unsigned int bit = 0;
68         arbel_bitmask_t mask = 1;
69
70         while ( bit < bits_len ) {
71                 if ( ( mask & *bits ) == 0 ) {
72                         *bits |= mask;
73                         return bit;
74                 }
75                 bit++;
76                 mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
77                 if ( mask == 1 )
78                         bits++;
79         }
80         return -ENFILE;
81 }
82
83 /**
84  * Free offset within usage bitmask
85  *
86  * @v bits              Usage bitmask
87  * @v bit               Bit within bitmask
88  */
89 static void arbel_bitmask_free ( arbel_bitmask_t *bits, int bit ) {
90         arbel_bitmask_t mask;
91
92         mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
93         bits += ( bit / ( 8 * sizeof ( mask ) ) );
94         *bits &= ~mask;
95 }
96
97 /***************************************************************************
98  *
99  * HCA commands
100  *
101  ***************************************************************************
102  */
103
104 /**
105  * Wait for Arbel command completion
106  *
107  * @v arbel             Arbel device
108  * @ret rc              Return status code
109  */
110 static int arbel_cmd_wait ( struct arbel *arbel,
111                             struct arbelprm_hca_command_register *hcr ) {
112         unsigned int wait;
113
114         for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
115                 hcr->u.dwords[6] =
116                         readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
117                 if ( MLX_GET ( hcr, go ) == 0 )
118                         return 0;
119                 mdelay ( 1 );
120         }
121         return -EBUSY;
122 }
123
124 /**
125  * Issue HCA command
126  *
127  * @v arbel             Arbel device
128  * @v command           Command opcode, flags and input/output lengths
129  * @v op_mod            Opcode modifier (0 if no modifier applicable)
130  * @v in                Input parameters
131  * @v in_mod            Input modifier (0 if no modifier applicable)
132  * @v out               Output parameters
133  * @ret rc              Return status code
134  */
135 static int arbel_cmd ( struct arbel *arbel, unsigned long command,
136                        unsigned int op_mod, const void *in,
137                        unsigned int in_mod, void *out ) {
138         struct arbelprm_hca_command_register hcr;
139         unsigned int opcode = ARBEL_HCR_OPCODE ( command );
140         size_t in_len = ARBEL_HCR_IN_LEN ( command );
141         size_t out_len = ARBEL_HCR_OUT_LEN ( command );
142         void *in_buffer;
143         void *out_buffer;
144         unsigned int status;
145         unsigned int i;
146         int rc;
147
148         assert ( in_len <= ARBEL_MBOX_SIZE );
149         assert ( out_len <= ARBEL_MBOX_SIZE );
150
151         DBGC2 ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
152                 arbel, opcode, in_len,
153                 ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
154                 ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
155
156         /* Check that HCR is free */
157         if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
158                 DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
159                 return rc;
160         }
161
162         /* Prepare HCR */
163         memset ( &hcr, 0, sizeof ( hcr ) );
164         in_buffer = &hcr.u.dwords[0];
165         if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
166                 in_buffer = arbel->mailbox_in;
167                 MLX_FILL_H ( &hcr, 0, in_param_h, virt_to_bus ( in_buffer ) );
168                 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
169         }
170         memcpy ( in_buffer, in, in_len );
171         MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
172         out_buffer = &hcr.u.dwords[3];
173         if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
174                 out_buffer = arbel->mailbox_out;
175                 MLX_FILL_H ( &hcr, 3, out_param_h,
176                              virt_to_bus ( out_buffer ) );
177                 MLX_FILL_1 ( &hcr, 4, out_param_l,
178                              virt_to_bus ( out_buffer ) );
179         }
180         MLX_FILL_3 ( &hcr, 6,
181                      opcode, opcode,
182                      opcode_modifier, op_mod,
183                      go, 1 );
184         DBGC ( arbel, "Arbel %p issuing command %04x\n", arbel, opcode );
185         DBGC2_HDA ( arbel, virt_to_phys ( arbel->config + ARBEL_HCR_BASE ),
186                     &hcr, sizeof ( hcr ) );
187         if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
188                 DBGC2 ( arbel, "Input mailbox:\n" );
189                 DBGC2_HDA ( arbel, virt_to_phys ( in_buffer ), in_buffer,
190                             ( ( in_len < 512 ) ? in_len : 512 ) );
191         }
192
193         /* Issue command */
194         for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
195               i++ ) {
196                 writel ( hcr.u.dwords[i],
197                          arbel->config + ARBEL_HCR_REG ( i ) );
198                 barrier();
199         }
200
201         /* Wait for command completion */
202         if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
203                 DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
204                        arbel );
205                 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
206                 return rc;
207         }
208
209         /* Check command status */
210         status = MLX_GET ( &hcr, status );
211         if ( status != 0 ) {
212                 DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
213                        arbel, status );
214                 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
215                 return -EIO;
216         }
217
218         /* Read output parameters, if any */
219         hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
220         hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
221         memcpy ( out, out_buffer, out_len );
222         if ( out_len ) {
223                 DBGC2 ( arbel, "Output%s:\n",
224                         ( command & ARBEL_HCR_OUT_MBOX ) ? " mailbox" : "" );
225                 DBGC2_HDA ( arbel, virt_to_phys ( out_buffer ), out_buffer,
226                             ( ( out_len < 512 ) ? out_len : 512 ) );
227         }
228
229         return 0;
230 }
231
232 static inline int
233 arbel_cmd_query_dev_lim ( struct arbel *arbel,
234                           struct arbelprm_query_dev_lim *dev_lim ) {
235         return arbel_cmd ( arbel,
236                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
237                                                1, sizeof ( *dev_lim ) ),
238                            0, NULL, 0, dev_lim );
239 }
240
241 static inline int
242 arbel_cmd_query_fw ( struct arbel *arbel, struct arbelprm_query_fw *fw ) {
243         return arbel_cmd ( arbel,
244                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW, 
245                                                1, sizeof ( *fw ) ),
246                            0, NULL, 0, fw );
247 }
248
249 static inline int
250 arbel_cmd_init_hca ( struct arbel *arbel,
251                      const struct arbelprm_init_hca *init_hca ) {
252         return arbel_cmd ( arbel,
253                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_HCA,
254                                               1, sizeof ( *init_hca ) ),
255                            0, init_hca, 0, NULL );
256 }
257
258 static inline int
259 arbel_cmd_close_hca ( struct arbel *arbel ) {
260         return arbel_cmd ( arbel,
261                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_HCA ),
262                            0, NULL, 0, NULL );
263 }
264
265 static inline int
266 arbel_cmd_init_ib ( struct arbel *arbel, unsigned int port,
267                     const struct arbelprm_init_ib *init_ib ) {
268         return arbel_cmd ( arbel,
269                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_IB,
270                                               1, sizeof ( *init_ib ) ),
271                            0, init_ib, port, NULL );
272 }
273
274 static inline int
275 arbel_cmd_close_ib ( struct arbel *arbel, unsigned int port ) {
276         return arbel_cmd ( arbel,
277                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_IB ),
278                            0, NULL, port, NULL );
279 }
280
281 static inline int
282 arbel_cmd_sw2hw_mpt ( struct arbel *arbel, unsigned int index,
283                       const struct arbelprm_mpt *mpt ) {
284         return arbel_cmd ( arbel,
285                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_MPT,
286                                               1, sizeof ( *mpt ) ),
287                            0, mpt, index, NULL );
288 }
289
290 static inline int
291 arbel_cmd_map_eq ( struct arbel *arbel, unsigned long index_map,
292                    const struct arbelprm_event_mask *mask ) {
293         return arbel_cmd ( arbel,
294                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_EQ,
295                                               0, sizeof ( *mask ) ),
296                            0, mask, index_map, NULL );
297 }
298
299 static inline int
300 arbel_cmd_sw2hw_eq ( struct arbel *arbel, unsigned int index,
301                      const struct arbelprm_eqc *eqctx ) {
302         return arbel_cmd ( arbel,
303                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_EQ,
304                                               1, sizeof ( *eqctx ) ),
305                            0, eqctx, index, NULL );
306 }
307
308 static inline int
309 arbel_cmd_hw2sw_eq ( struct arbel *arbel, unsigned int index,
310                      struct arbelprm_eqc *eqctx ) {
311         return arbel_cmd ( arbel,
312                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_EQ,
313                                                1, sizeof ( *eqctx ) ),
314                            1, NULL, index, eqctx );
315 }
316
317 static inline int
318 arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
319                      const struct arbelprm_completion_queue_context *cqctx ) {
320         return arbel_cmd ( arbel,
321                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
322                                               1, sizeof ( *cqctx ) ),
323                            0, cqctx, cqn, NULL );
324 }
325
326 static inline int
327 arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn,
328                      struct arbelprm_completion_queue_context *cqctx) {
329         return arbel_cmd ( arbel,
330                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ,
331                                                1, sizeof ( *cqctx ) ),
332                            0, NULL, cqn, cqctx );
333 }
334
335 static inline int
336 arbel_cmd_query_cq ( struct arbel *arbel, unsigned long cqn,
337                      struct arbelprm_completion_queue_context *cqctx ) {
338         return arbel_cmd ( arbel,
339                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_CQ,
340                                                1, sizeof ( *cqctx ) ),
341                            0, NULL, cqn, cqctx );
342 }
343
344 static inline int
345 arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
346                           const struct arbelprm_qp_ee_state_transitions *ctx ){
347         return arbel_cmd ( arbel,
348                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
349                                               1, sizeof ( *ctx ) ),
350                            0, ctx, qpn, NULL );
351 }
352
353 static inline int
354 arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
355                           const struct arbelprm_qp_ee_state_transitions *ctx ){
356         return arbel_cmd ( arbel,
357                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
358                                               1, sizeof ( *ctx ) ),
359                            0, ctx, qpn, NULL );
360 }
361
362 static inline int
363 arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
364                          const struct arbelprm_qp_ee_state_transitions *ctx ) {
365         return arbel_cmd ( arbel,
366                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
367                                               1, sizeof ( *ctx ) ),
368                            0, ctx, qpn, NULL );
369 }
370
371 static inline int
372 arbel_cmd_rts2rts_qpee ( struct arbel *arbel, unsigned long qpn,
373                          const struct arbelprm_qp_ee_state_transitions *ctx ) {
374         return arbel_cmd ( arbel,
375                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTS2RTS_QPEE,
376                                               1, sizeof ( *ctx ) ),
377                            0, ctx, qpn, NULL );
378 }
379
380 static inline int
381 arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
382         return arbel_cmd ( arbel,
383                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
384                            0x03, NULL, qpn, NULL );
385 }
386
387 static inline int
388 arbel_cmd_query_qpee ( struct arbel *arbel, unsigned long qpn,
389                        struct arbelprm_qp_ee_state_transitions *ctx ) {
390         return arbel_cmd ( arbel,
391                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_QPEE,
392                                                1, sizeof ( *ctx ) ),
393                            0, NULL, qpn, ctx );
394 }
395
396 static inline int
397 arbel_cmd_conf_special_qp ( struct arbel *arbel, unsigned int qp_type,
398                             unsigned long base_qpn ) {
399         return arbel_cmd ( arbel,
400                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CONF_SPECIAL_QP ),
401                            qp_type, NULL, base_qpn, NULL );
402 }
403
404 static inline int
405 arbel_cmd_mad_ifc ( struct arbel *arbel, unsigned int port,
406                     union arbelprm_mad *mad ) {
407         return arbel_cmd ( arbel,
408                            ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC,
409                                                  1, sizeof ( *mad ),
410                                                  1, sizeof ( *mad ) ),
411                            0x03, mad, port, mad );
412 }
413
414 static inline int
415 arbel_cmd_read_mgm ( struct arbel *arbel, unsigned int index,
416                      struct arbelprm_mgm_entry *mgm ) {
417         return arbel_cmd ( arbel,
418                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM,
419                                                1, sizeof ( *mgm ) ),
420                            0, NULL, index, mgm );
421 }
422
423 static inline int
424 arbel_cmd_write_mgm ( struct arbel *arbel, unsigned int index,
425                       const struct arbelprm_mgm_entry *mgm ) {
426         return arbel_cmd ( arbel,
427                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM,
428                                               1, sizeof ( *mgm ) ),
429                            0, mgm, index, NULL );
430 }
431
432 static inline int
433 arbel_cmd_mgid_hash ( struct arbel *arbel, const union ib_gid *gid,
434                       struct arbelprm_mgm_hash *hash ) {
435         return arbel_cmd ( arbel,
436                            ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH,
437                                                  1, sizeof ( *gid ),
438                                                  0, sizeof ( *hash ) ),
439                            0, gid, 0, hash );
440 }
441
442 static inline int
443 arbel_cmd_run_fw ( struct arbel *arbel ) {
444         return arbel_cmd ( arbel,
445                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_RUN_FW ),
446                            0, NULL, 0, NULL );
447 }
448
449 static inline int
450 arbel_cmd_disable_lam ( struct arbel *arbel ) {
451         return arbel_cmd ( arbel,
452                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_DISABLE_LAM ),
453                            0, NULL, 0, NULL );
454 }
455
456 static inline int
457 arbel_cmd_enable_lam ( struct arbel *arbel, struct arbelprm_access_lam *lam ) {
458         return arbel_cmd ( arbel,
459                            ARBEL_HCR_OUT_CMD ( ARBEL_HCR_ENABLE_LAM,
460                                                1, sizeof ( *lam ) ),
461                            1, NULL, 0, lam );
462 }
463
464 static inline int
465 arbel_cmd_unmap_icm ( struct arbel *arbel, unsigned int page_count,
466                       const struct arbelprm_scalar_parameter *offset ) {
467         return arbel_cmd ( arbel,
468                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_UNMAP_ICM, 0,
469                                               sizeof ( *offset ) ),
470                            0, offset, page_count, NULL );
471 }
472
473 static inline int
474 arbel_cmd_map_icm ( struct arbel *arbel,
475                     const struct arbelprm_virtual_physical_mapping *map ) {
476         return arbel_cmd ( arbel,
477                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM,
478                                               1, sizeof ( *map ) ),
479                            0, map, 1, NULL );
480 }
481
482 static inline int
483 arbel_cmd_unmap_icm_aux ( struct arbel *arbel ) {
484         return arbel_cmd ( arbel,
485                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM_AUX ),
486                            0, NULL, 0, NULL );
487 }
488
489 static inline int
490 arbel_cmd_map_icm_aux ( struct arbel *arbel,
491                         const struct arbelprm_virtual_physical_mapping *map ) {
492         return arbel_cmd ( arbel,
493                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM_AUX,
494                                               1, sizeof ( *map ) ),
495                            0, map, 1, NULL );
496 }
497
498 static inline int
499 arbel_cmd_set_icm_size ( struct arbel *arbel,
500                          const struct arbelprm_scalar_parameter *icm_size,
501                          struct arbelprm_scalar_parameter *icm_aux_size ) {
502         return arbel_cmd ( arbel,
503                            ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_SET_ICM_SIZE,
504                                                  0, sizeof ( *icm_size ),
505                                                  0, sizeof ( *icm_aux_size ) ),
506                            0, icm_size, 0, icm_aux_size );
507 }
508
509 static inline int
510 arbel_cmd_unmap_fa ( struct arbel *arbel ) {
511         return arbel_cmd ( arbel,
512                            ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_FA ),
513                            0, NULL, 0, NULL );
514 }
515
516 static inline int
517 arbel_cmd_map_fa ( struct arbel *arbel,
518                    const struct arbelprm_virtual_physical_mapping *map ) {
519         return arbel_cmd ( arbel,
520                            ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_FA,
521                                               1, sizeof ( *map ) ),
522                            0, map, 1, NULL );
523 }
524
525 /***************************************************************************
526  *
527  * MAD operations
528  *
529  ***************************************************************************
530  */
531
532 /**
533  * Issue management datagram
534  *
535  * @v ibdev             Infiniband device
536  * @v mad               Management datagram
537  * @ret rc              Return status code
538  */
539 static int arbel_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
540         struct arbel *arbel = ib_get_drvdata ( ibdev );
541         union arbelprm_mad mad_ifc;
542         int rc;
543
544         linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
545                         mad_size_mismatch );
546
547         /* Copy in request packet */
548         memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
549
550         /* Issue MAD */
551         if ( ( rc = arbel_cmd_mad_ifc ( arbel, ibdev->port,
552                                         &mad_ifc ) ) != 0 ) {
553                 DBGC ( arbel, "Arbel %p port %d could not issue MAD IFC: %s\n",
554                        arbel, ibdev->port, strerror ( rc ) );
555                 return rc;
556         }
557
558         /* Copy out reply packet */
559         memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
560
561         if ( mad->hdr.status != 0 ) {
562                 DBGC ( arbel, "Arbel %p port %d MAD IFC status %04x\n",
563                        arbel, ibdev->port, ntohs ( mad->hdr.status ) );
564                 return -EIO;
565         }
566         return 0;
567 }
568
569 /***************************************************************************
570  *
571  * Completion queue operations
572  *
573  ***************************************************************************
574  */
575
576 /**
577  * Dump completion queue context (for debugging only)
578  *
579  * @v arbel             Arbel device
580  * @v cq                Completion queue
581  * @ret rc              Return status code
582  */
583 static __attribute__ (( unused )) int
584 arbel_dump_cqctx ( struct arbel *arbel, struct ib_completion_queue *cq ) {
585         struct arbelprm_completion_queue_context cqctx;
586         int rc;
587
588         memset ( &cqctx, 0, sizeof ( cqctx ) );
589         if ( ( rc = arbel_cmd_query_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
590                 DBGC ( arbel, "Arbel %p CQN %#lx QUERY_CQ failed: %s\n",
591                        arbel, cq->cqn, strerror ( rc ) );
592                 return rc;
593         }
594         DBGC ( arbel, "Arbel %p CQN %#lx context:\n", arbel, cq->cqn );
595         DBGC_HDA ( arbel, 0, &cqctx, sizeof ( cqctx ) );
596
597         return 0;
598 }
599
600 /**
601  * Create completion queue
602  *
603  * @v ibdev             Infiniband device
604  * @v cq                Completion queue
605  * @ret rc              Return status code
606  */
607 static int arbel_create_cq ( struct ib_device *ibdev,
608                              struct ib_completion_queue *cq ) {
609         struct arbel *arbel = ib_get_drvdata ( ibdev );
610         struct arbel_completion_queue *arbel_cq;
611         struct arbelprm_completion_queue_context cqctx;
612         struct arbelprm_cq_ci_db_record *ci_db_rec;
613         struct arbelprm_cq_arm_db_record *arm_db_rec;
614         int cqn_offset;
615         unsigned int i;
616         int rc;
617
618         /* Find a free completion queue number */
619         cqn_offset = arbel_bitmask_alloc ( arbel->cq_inuse, ARBEL_MAX_CQS );
620         if ( cqn_offset < 0 ) {
621                 DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
622                 rc = cqn_offset;
623                 goto err_cqn_offset;
624         }
625         cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
626
627         /* Allocate control structures */
628         arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
629         if ( ! arbel_cq ) {
630                 rc = -ENOMEM;
631                 goto err_arbel_cq;
632         }
633         arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( arbel, cq );
634         arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( arbel, cq );
635
636         /* Allocate completion queue itself */
637         arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
638         arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
639                                      sizeof ( arbel_cq->cqe[0] ) );
640         if ( ! arbel_cq->cqe ) {
641                 rc = -ENOMEM;
642                 goto err_cqe;
643         }
644         memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
645         for ( i = 0 ; i < cq->num_cqes ; i++ ) {
646                 MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
647         }
648         barrier();
649
650         /* Initialise doorbell records */
651         ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
652         MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
653         MLX_FILL_2 ( ci_db_rec, 1,
654                      res, ARBEL_UAR_RES_CQ_CI,
655                      cq_number, cq->cqn );
656         arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
657         MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
658         MLX_FILL_2 ( arm_db_rec, 1,
659                      res, ARBEL_UAR_RES_CQ_ARM,
660                      cq_number, cq->cqn );
661
662         /* Hand queue over to hardware */
663         memset ( &cqctx, 0, sizeof ( cqctx ) );
664         MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
665         MLX_FILL_H ( &cqctx, 1, start_address_h,
666                      virt_to_bus ( arbel_cq->cqe ) );
667         MLX_FILL_1 ( &cqctx, 2, start_address_l,
668                      virt_to_bus ( arbel_cq->cqe ) );
669         MLX_FILL_2 ( &cqctx, 3,
670                      usr_page, arbel->limits.reserved_uars,
671                      log_cq_size, fls ( cq->num_cqes - 1 ) );
672         MLX_FILL_1 ( &cqctx, 5, c_eqn, arbel->eq.eqn );
673         MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
674         MLX_FILL_1 ( &cqctx, 7, l_key, arbel->lkey );
675         MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
676         MLX_FILL_1 ( &cqctx, 13,
677                      cq_ci_db_record, arbel_cq->ci_doorbell_idx );
678         MLX_FILL_1 ( &cqctx, 14,
679                      cq_state_db_record, arbel_cq->arm_doorbell_idx );
680         if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
681                 DBGC ( arbel, "Arbel %p CQN %#lx SW2HW_CQ failed: %s\n",
682                        arbel, cq->cqn, strerror ( rc ) );
683                 goto err_sw2hw_cq;
684         }
685
686         DBGC ( arbel, "Arbel %p CQN %#lx ring [%08lx,%08lx), doorbell %08lx\n",
687                arbel, cq->cqn, virt_to_phys ( arbel_cq->cqe ),
688                ( virt_to_phys ( arbel_cq->cqe ) + arbel_cq->cqe_size ),
689                virt_to_phys ( ci_db_rec ) );
690         ib_cq_set_drvdata ( cq, arbel_cq );
691         return 0;
692
693  err_sw2hw_cq:
694         MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
695         MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
696         free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
697  err_cqe:
698         free ( arbel_cq );
699  err_arbel_cq:
700         arbel_bitmask_free ( arbel->cq_inuse, cqn_offset );
701  err_cqn_offset:
702         return rc;
703 }
704
705 /**
706  * Destroy completion queue
707  *
708  * @v ibdev             Infiniband device
709  * @v cq                Completion queue
710  */
711 static void arbel_destroy_cq ( struct ib_device *ibdev,
712                                struct ib_completion_queue *cq ) {
713         struct arbel *arbel = ib_get_drvdata ( ibdev );
714         struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
715         struct arbelprm_completion_queue_context cqctx;
716         struct arbelprm_cq_ci_db_record *ci_db_rec;
717         struct arbelprm_cq_arm_db_record *arm_db_rec;
718         int cqn_offset;
719         int rc;
720
721         /* Take ownership back from hardware */
722         if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
723                 DBGC ( arbel, "Arbel %p CQN %#lx FATAL HW2SW_CQ failed: "
724                        "%s\n", arbel, cq->cqn, strerror ( rc ) );
725                 /* Leak memory and return; at least we avoid corruption */
726                 return;
727         }
728
729         /* Clear doorbell records */
730         ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
731         arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
732         MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
733         MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
734
735         /* Free memory */
736         free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
737         free ( arbel_cq );
738
739         /* Mark queue number as free */
740         cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
741         arbel_bitmask_free ( arbel->cq_inuse, cqn_offset );
742
743         ib_cq_set_drvdata ( cq, NULL );
744 }
745
746 /***************************************************************************
747  *
748  * Queue pair operations
749  *
750  ***************************************************************************
751  */
752
753 /**
754  * Assign queue pair number
755  *
756  * @v ibdev             Infiniband device
757  * @v qp                Queue pair
758  * @ret rc              Return status code
759  */
760 static int arbel_alloc_qpn ( struct ib_device *ibdev,
761                              struct ib_queue_pair *qp ) {
762         struct arbel *arbel = ib_get_drvdata ( ibdev );
763         unsigned int port_offset;
764         int qpn_offset;
765
766         /* Calculate queue pair number */
767         port_offset = ( ibdev->port - ARBEL_PORT_BASE );
768
769         switch ( qp->type ) {
770         case IB_QPT_SMI:
771                 qp->qpn = ( arbel->special_qpn_base + port_offset );
772                 return 0;
773         case IB_QPT_GSI:
774                 qp->qpn = ( arbel->special_qpn_base + 2 + port_offset );
775                 return 0;
776         case IB_QPT_UD:
777         case IB_QPT_RC:
778                 /* Find a free queue pair number */
779                 qpn_offset = arbel_bitmask_alloc ( arbel->qp_inuse,
780                                                    ARBEL_MAX_QPS );
781                 if ( qpn_offset < 0 ) {
782                         DBGC ( arbel, "Arbel %p out of queue pairs\n",
783                                arbel );
784                         return qpn_offset;
785                 }
786                 qp->qpn = ( ( random() & ARBEL_QPN_RANDOM_MASK ) |
787                             ( arbel->qpn_base + qpn_offset ) );
788                 return 0;
789         default:
790                 DBGC ( arbel, "Arbel %p unsupported QP type %d\n",
791                        arbel, qp->type );
792                 return -ENOTSUP;
793         }
794 }
795
796 /**
797  * Free queue pair number
798  *
799  * @v ibdev             Infiniband device
800  * @v qp                Queue pair
801  */
802 static void arbel_free_qpn ( struct ib_device *ibdev,
803                              struct ib_queue_pair *qp ) {
804         struct arbel *arbel = ib_get_drvdata ( ibdev );
805         int qpn_offset;
806
807         qpn_offset = ( ( qp->qpn & ~ARBEL_QPN_RANDOM_MASK ) - arbel->qpn_base );
808         if ( qpn_offset >= 0 )
809                 arbel_bitmask_free ( arbel->qp_inuse, qpn_offset );
810 }
811
812 /**
813  * Calculate transmission rate
814  *
815  * @v av                Address vector
816  * @ret arbel_rate      Arbel rate
817  */
818 static unsigned int arbel_rate ( struct ib_address_vector *av ) {
819         return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
820                  ? ( av->rate + 5 ) : 0 );
821 }
822
823 /** Queue pair transport service type map */
824 static uint8_t arbel_qp_st[] = {
825         [IB_QPT_SMI] = ARBEL_ST_MLX,
826         [IB_QPT_GSI] = ARBEL_ST_MLX,
827         [IB_QPT_UD] = ARBEL_ST_UD,
828         [IB_QPT_RC] = ARBEL_ST_RC,
829 };
830
831 /**
832  * Dump queue pair context (for debugging only)
833  *
834  * @v arbel             Arbel device
835  * @v qp                Queue pair
836  * @ret rc              Return status code
837  */
838 static __attribute__ (( unused )) int
839 arbel_dump_qpctx ( struct arbel *arbel, struct ib_queue_pair *qp ) {
840         struct arbelprm_qp_ee_state_transitions qpctx;
841         int rc;
842
843         memset ( &qpctx, 0, sizeof ( qpctx ) );
844         if ( ( rc = arbel_cmd_query_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ) {
845                 DBGC ( arbel, "Arbel %p QPN %#lx QUERY_QPEE failed: %s\n",
846                        arbel, qp->qpn, strerror ( rc ) );
847                 return rc;
848         }
849         DBGC ( arbel, "Arbel %p QPN %#lx context:\n", arbel, qp->qpn );
850         DBGC_HDA ( arbel, 0, &qpctx.u.dwords[2], ( sizeof ( qpctx ) - 8 ) );
851
852         return 0;
853 }
854
855 /**
856  * Create send work queue
857  *
858  * @v arbel_send_wq     Send work queue
859  * @v num_wqes          Number of work queue entries
860  * @ret rc              Return status code
861  */
862 static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
863                                   unsigned int num_wqes ) {
864         union arbel_send_wqe *wqe;
865         union arbel_send_wqe *next_wqe;
866         unsigned int wqe_idx_mask;
867         unsigned int i;
868
869         /* Allocate work queue */
870         arbel_send_wq->wqe_size = ( num_wqes *
871                                     sizeof ( arbel_send_wq->wqe[0] ) );
872         arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
873                                           sizeof ( arbel_send_wq->wqe[0] ) );
874         if ( ! arbel_send_wq->wqe )
875                 return -ENOMEM;
876         memset ( arbel_send_wq->wqe, 0, arbel_send_wq->wqe_size );
877
878         /* Link work queue entries */
879         wqe_idx_mask = ( num_wqes - 1 );
880         for ( i = 0 ; i < num_wqes ; i++ ) {
881                 wqe = &arbel_send_wq->wqe[i];
882                 next_wqe = &arbel_send_wq->wqe[ ( i + 1 ) & wqe_idx_mask ];
883                 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
884                              ( virt_to_bus ( next_wqe ) >> 6 ) );
885                 MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
886         }
887         
888         return 0;
889 }
890
891 /**
892  * Create receive work queue
893  *
894  * @v arbel_recv_wq     Receive work queue
895  * @v num_wqes          Number of work queue entries
896  * @ret rc              Return status code
897  */
898 static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
899                                   unsigned int num_wqes ) {
900         struct arbelprm_recv_wqe *wqe;
901         struct arbelprm_recv_wqe *next_wqe;
902         unsigned int wqe_idx_mask;
903         size_t nds;
904         unsigned int i;
905         unsigned int j;
906
907         /* Allocate work queue */
908         arbel_recv_wq->wqe_size = ( num_wqes *
909                                     sizeof ( arbel_recv_wq->wqe[0] ) );
910         arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
911                                           sizeof ( arbel_recv_wq->wqe[0] ) );
912         if ( ! arbel_recv_wq->wqe )
913                 return -ENOMEM;
914         memset ( arbel_recv_wq->wqe, 0, arbel_recv_wq->wqe_size );
915
916         /* Link work queue entries */
917         wqe_idx_mask = ( num_wqes - 1 );
918         nds = ( ( offsetof ( typeof ( *wqe ), data ) +
919                   sizeof ( wqe->data[0] ) ) >> 4 );
920         for ( i = 0 ; i < num_wqes ; i++ ) {
921                 wqe = &arbel_recv_wq->wqe[i].recv;
922                 next_wqe = &arbel_recv_wq->wqe[( i + 1 ) & wqe_idx_mask].recv;
923                 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
924                              ( virt_to_bus ( next_wqe ) >> 6 ) );
925                 MLX_FILL_1 ( &wqe->next, 1, nds, nds );
926                 for ( j = 0 ; ( ( ( void * ) &wqe->data[j] ) <
927                                 ( ( void * ) ( wqe + 1 ) ) ) ; j++ ) {
928                         MLX_FILL_1 ( &wqe->data[j], 1,
929                                      l_key, ARBEL_INVALID_LKEY );
930                 }
931         }
932         
933         return 0;
934 }
935
936 /**
937  * Create queue pair
938  *
939  * @v ibdev             Infiniband device
940  * @v qp                Queue pair
941  * @ret rc              Return status code
942  */
943 static int arbel_create_qp ( struct ib_device *ibdev,
944                              struct ib_queue_pair *qp ) {
945         struct arbel *arbel = ib_get_drvdata ( ibdev );
946         struct arbel_queue_pair *arbel_qp;
947         struct arbelprm_qp_ee_state_transitions qpctx;
948         struct arbelprm_qp_db_record *send_db_rec;
949         struct arbelprm_qp_db_record *recv_db_rec;
950         physaddr_t send_wqe_base_adr;
951         physaddr_t recv_wqe_base_adr;
952         physaddr_t wqe_base_adr;
953         int rc;
954
955         /* Warn about dysfunctional code
956          *
957          * Arbel seems to crash the system as soon as the first send
958          * WQE completes on an RC queue pair.  (NOPs complete
959          * successfully, so this is a problem specific to the work
960          * queue rather than the completion queue.)  The cause of this
961          * problem has remained unknown for over a year.  Patches to
962          * fix this are welcome.
963          */
964         if ( qp->type == IB_QPT_RC )
965                 DBG ( "*** WARNING: Arbel RC support is non-functional ***\n" );
966
967         /* Calculate queue pair number */
968         if ( ( rc = arbel_alloc_qpn ( ibdev, qp ) ) != 0 )
969                 goto err_alloc_qpn;
970
971         /* Allocate control structures */
972         arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
973         if ( ! arbel_qp ) {
974                 rc = -ENOMEM;
975                 goto err_arbel_qp;
976         }
977         arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( arbel, qp );
978         arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( arbel, qp );
979
980         /* Create send and receive work queues */
981         if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
982                                            qp->send.num_wqes ) ) != 0 )
983                 goto err_create_send_wq;
984         if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv,
985                                            qp->recv.num_wqes ) ) != 0 )
986                 goto err_create_recv_wq;
987
988         /* Send and receive work queue entries must be within the same 4GB */
989         send_wqe_base_adr = virt_to_bus ( arbel_qp->send.wqe );
990         recv_wqe_base_adr = virt_to_bus ( arbel_qp->recv.wqe );
991         if ( ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) &&
992              ( ( ( ( uint64_t ) send_wqe_base_adr ) >> 32 ) !=
993                ( ( ( uint64_t ) recv_wqe_base_adr ) >> 32 ) ) ) {
994                 DBGC ( arbel, "Arbel %p QPN %#lx cannot support send %08lx "
995                        "recv %08lx\n", arbel, qp->qpn,
996                        send_wqe_base_adr, recv_wqe_base_adr );
997                 rc = -ENOTSUP;
998                 goto err_unsupported_address_split;
999         }
1000         wqe_base_adr = send_wqe_base_adr;
1001
1002         /* Initialise doorbell records */
1003         send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
1004         MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
1005         MLX_FILL_2 ( send_db_rec, 1,
1006                      res, ARBEL_UAR_RES_SQ,
1007                      qp_number, qp->qpn );
1008         recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
1009         MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
1010         MLX_FILL_2 ( recv_db_rec, 1,
1011                      res, ARBEL_UAR_RES_RQ,
1012                      qp_number, qp->qpn );
1013
1014         /* Transition queue to INIT state */
1015         memset ( &qpctx, 0, sizeof ( qpctx ) );
1016         MLX_FILL_3 ( &qpctx, 2,
1017                      qpc_eec_data.de, 1,
1018                      qpc_eec_data.pm_state, ARBEL_PM_STATE_MIGRATED,
1019                      qpc_eec_data.st, arbel_qp_st[qp->type] );
1020         MLX_FILL_4 ( &qpctx, 4,
1021                      qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
1022                      qpc_eec_data.log_rq_stride,
1023                      ( fls ( sizeof ( arbel_qp->recv.wqe[0] ) - 1 ) - 4 ),
1024                      qpc_eec_data.log_sq_size, fls ( qp->send.num_wqes - 1 ),
1025                      qpc_eec_data.log_sq_stride,
1026                      ( fls ( sizeof ( arbel_qp->send.wqe[0] ) - 1 ) - 4 ) );
1027         MLX_FILL_1 ( &qpctx, 5,
1028                      qpc_eec_data.usr_page, arbel->limits.reserved_uars );
1029         MLX_FILL_1 ( &qpctx, 10, qpc_eec_data.primary_address_path.port_number,
1030                      ibdev->port );
1031         MLX_FILL_1 ( &qpctx, 27, qpc_eec_data.pd, ARBEL_GLOBAL_PD );
1032         MLX_FILL_H ( &qpctx, 28, qpc_eec_data.wqe_base_adr_h, wqe_base_adr );
1033         MLX_FILL_1 ( &qpctx, 29, qpc_eec_data.wqe_lkey, arbel->lkey );
1034         MLX_FILL_1 ( &qpctx, 30, qpc_eec_data.ssc, 1 );
1035         MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
1036         MLX_FILL_1 ( &qpctx, 34, qpc_eec_data.snd_wqe_base_adr_l,
1037                      ( send_wqe_base_adr >> 6 ) );
1038         MLX_FILL_1 ( &qpctx, 35, qpc_eec_data.snd_db_record_index,
1039                      arbel_qp->send.doorbell_idx );
1040         MLX_FILL_4 ( &qpctx, 38,
1041                      qpc_eec_data.rre, 1,
1042                      qpc_eec_data.rwe, 1,
1043                      qpc_eec_data.rae, 1,
1044                      qpc_eec_data.rsc, 1 );
1045         MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
1046         MLX_FILL_1 ( &qpctx, 42, qpc_eec_data.rcv_wqe_base_adr_l,
1047                      ( recv_wqe_base_adr >> 6 ) );
1048         MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.rcv_db_record_index,
1049                      arbel_qp->recv.doorbell_idx );
1050         if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
1051                 DBGC ( arbel, "Arbel %p QPN %#lx RST2INIT_QPEE failed: %s\n",
1052                        arbel, qp->qpn, strerror ( rc ) );
1053                 goto err_rst2init_qpee;
1054         }
1055         arbel_qp->state = ARBEL_QP_ST_INIT;
1056
1057         DBGC ( arbel, "Arbel %p QPN %#lx send ring [%08lx,%08lx), doorbell "
1058                "%08lx\n", arbel, qp->qpn, virt_to_phys ( arbel_qp->send.wqe ),
1059                ( virt_to_phys ( arbel_qp->send.wqe ) +
1060                  arbel_qp->send.wqe_size ),
1061                virt_to_phys ( send_db_rec ) );
1062         DBGC ( arbel, "Arbel %p QPN %#lx receive ring [%08lx,%08lx), doorbell "
1063                "%08lx\n", arbel, qp->qpn, virt_to_phys ( arbel_qp->recv.wqe ),
1064                ( virt_to_phys ( arbel_qp->recv.wqe ) +
1065                  arbel_qp->recv.wqe_size ),
1066                virt_to_phys ( recv_db_rec ) );
1067         DBGC ( arbel, "Arbel %p QPN %#lx send CQN %#lx receive CQN %#lx\n",
1068                arbel, qp->qpn, qp->send.cq->cqn, qp->recv.cq->cqn );
1069         ib_qp_set_drvdata ( qp, arbel_qp );
1070         return 0;
1071
1072         arbel_cmd_2rst_qpee ( arbel, qp->qpn );
1073  err_rst2init_qpee:
1074         MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
1075         MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
1076  err_unsupported_address_split:
1077         free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
1078  err_create_recv_wq:
1079         free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
1080  err_create_send_wq:
1081         free ( arbel_qp );
1082  err_arbel_qp:
1083         arbel_free_qpn ( ibdev, qp );
1084  err_alloc_qpn:
1085         return rc;
1086 }
1087
1088 /**
1089  * Modify queue pair
1090  *
1091  * @v ibdev             Infiniband device
1092  * @v qp                Queue pair
1093  * @ret rc              Return status code
1094  */
1095 static int arbel_modify_qp ( struct ib_device *ibdev,
1096                              struct ib_queue_pair *qp ) {
1097         struct arbel *arbel = ib_get_drvdata ( ibdev );
1098         struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
1099         struct arbelprm_qp_ee_state_transitions qpctx;
1100         int rc;
1101
1102         /* Transition queue to RTR state, if applicable */
1103         if ( arbel_qp->state < ARBEL_QP_ST_RTR ) {
1104                 memset ( &qpctx, 0, sizeof ( qpctx ) );
1105                 MLX_FILL_2 ( &qpctx, 4,
1106                              qpc_eec_data.mtu, ARBEL_MTU_2048,
1107                              qpc_eec_data.msg_max, 31 );
1108                 MLX_FILL_1 ( &qpctx, 7,
1109                              qpc_eec_data.remote_qpn_een, qp->av.qpn );
1110                 MLX_FILL_2 ( &qpctx, 11,
1111                              qpc_eec_data.primary_address_path.rnr_retry,
1112                              ARBEL_RETRY_MAX,
1113                              qpc_eec_data.primary_address_path.rlid,
1114                              qp->av.lid );
1115                 MLX_FILL_2 ( &qpctx, 12,
1116                              qpc_eec_data.primary_address_path.ack_timeout,
1117                              14 /* 4.096us * 2^(14) = 67ms */,
1118                              qpc_eec_data.primary_address_path.max_stat_rate,
1119                              arbel_rate ( &qp->av ) );
1120                 memcpy ( &qpctx.u.dwords[14], &qp->av.gid,
1121                          sizeof ( qp->av.gid ) );
1122                 MLX_FILL_1 ( &qpctx, 30,
1123                              qpc_eec_data.retry_count, ARBEL_RETRY_MAX );
1124                 MLX_FILL_1 ( &qpctx, 39,
1125                              qpc_eec_data.next_rcv_psn, qp->recv.psn );
1126                 MLX_FILL_1 ( &qpctx, 40,
1127                              qpc_eec_data.ra_buff_indx,
1128                              ( arbel->limits.reserved_rdbs +
1129                                ( ( qp->qpn & ~ARBEL_QPN_RANDOM_MASK ) -
1130                                  arbel->special_qpn_base ) ) );
1131                 if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn,
1132                                                       &qpctx ) ) != 0 ) {
1133                         DBGC ( arbel, "Arbel %p QPN %#lx INIT2RTR_QPEE failed:"
1134                                " %s\n", arbel, qp->qpn, strerror ( rc ) );
1135                         return rc;
1136                 }
1137                 arbel_qp->state = ARBEL_QP_ST_RTR;
1138         }
1139
1140         /* Transition queue to RTS state, if applicable */
1141         if ( arbel_qp->state < ARBEL_QP_ST_RTS ) {
1142                 memset ( &qpctx, 0, sizeof ( qpctx ) );
1143                 MLX_FILL_1 ( &qpctx, 11,
1144                              qpc_eec_data.primary_address_path.rnr_retry,
1145                              ARBEL_RETRY_MAX );
1146                 MLX_FILL_1 ( &qpctx, 12,
1147                              qpc_eec_data.primary_address_path.ack_timeout,
1148                              14 /* 4.096us * 2^(14) = 67ms */ );
1149                 MLX_FILL_2 ( &qpctx, 30,
1150                              qpc_eec_data.retry_count, ARBEL_RETRY_MAX,
1151                              qpc_eec_data.sic, 1 );
1152                 MLX_FILL_1 ( &qpctx, 32,
1153                              qpc_eec_data.next_send_psn, qp->send.psn );
1154                 if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn,
1155                                                      &qpctx ) ) != 0 ) {
1156                         DBGC ( arbel, "Arbel %p QPN %#lx RTR2RTS_QPEE failed: "
1157                                "%s\n", arbel, qp->qpn, strerror ( rc ) );
1158                         return rc;
1159                 }
1160                 arbel_qp->state = ARBEL_QP_ST_RTS;
1161         }
1162
1163         /* Update parameters in RTS state */
1164         memset ( &qpctx, 0, sizeof ( qpctx ) );
1165         MLX_FILL_1 ( &qpctx, 0, opt_param_mask, ARBEL_QPEE_OPT_PARAM_QKEY );
1166         MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
1167         if ( ( rc = arbel_cmd_rts2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
1168                 DBGC ( arbel, "Arbel %p QPN %#lx RTS2RTS_QPEE failed: %s\n",
1169                        arbel, qp->qpn, strerror ( rc ) );
1170                 return rc;
1171         }
1172
1173         return 0;
1174 }
1175
1176 /**
1177  * Destroy queue pair
1178  *
1179  * @v ibdev             Infiniband device
1180  * @v qp                Queue pair
1181  */
1182 static void arbel_destroy_qp ( struct ib_device *ibdev,
1183                                struct ib_queue_pair *qp ) {
1184         struct arbel *arbel = ib_get_drvdata ( ibdev );
1185         struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
1186         struct arbelprm_qp_db_record *send_db_rec;
1187         struct arbelprm_qp_db_record *recv_db_rec;
1188         int rc;
1189
1190         /* Take ownership back from hardware */
1191         if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
1192                 DBGC ( arbel, "Arbel %p QPN %#lx FATAL 2RST_QPEE failed: "
1193                        "%s\n", arbel, qp->qpn, strerror ( rc ) );
1194                 /* Leak memory and return; at least we avoid corruption */
1195                 return;
1196         }
1197
1198         /* Clear doorbell records */
1199         send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
1200         recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
1201         MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
1202         MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
1203
1204         /* Free memory */
1205         free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
1206         free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
1207         free ( arbel_qp );
1208
1209         /* Mark queue number as free */
1210         arbel_free_qpn ( ibdev, qp );
1211
1212         ib_qp_set_drvdata ( qp, NULL );
1213 }
1214
1215 /***************************************************************************
1216  *
1217  * Work request operations
1218  *
1219  ***************************************************************************
1220  */
1221
1222 /**
1223  * Ring doorbell register in UAR
1224  *
1225  * @v arbel             Arbel device
1226  * @v db_reg            Doorbell register structure
1227  * @v offset            Address of doorbell
1228  */
1229 static void arbel_ring_doorbell ( struct arbel *arbel,
1230                                   union arbelprm_doorbell_register *db_reg,
1231                                   unsigned int offset ) {
1232
1233         DBGC2 ( arbel, "Arbel %p ringing doorbell %08x:%08x at %lx\n",
1234                 arbel, ntohl ( db_reg->dword[0] ), ntohl ( db_reg->dword[1] ),
1235                 virt_to_phys ( arbel->uar + offset ) );
1236
1237         barrier();
1238         writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
1239         barrier();
1240         writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
1241 }
1242
1243 /** GID used for GID-less send work queue entries */
1244 static const union ib_gid arbel_no_gid = {
1245         .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 },
1246 };
1247
1248 /**
1249  * Construct UD send work queue entry
1250  *
1251  * @v ibdev             Infiniband device
1252  * @v qp                Queue pair
1253  * @v dest              Destination address vector
1254  * @v iobuf             I/O buffer
1255  * @v wqe               Send work queue entry
1256  * @ret nds             Work queue entry size
1257  */
1258 static size_t arbel_fill_ud_send_wqe ( struct ib_device *ibdev,
1259                                        struct ib_queue_pair *qp __unused,
1260                                        struct ib_address_vector *dest,
1261                                        struct io_buffer *iobuf,
1262                                        union arbel_send_wqe *wqe ) {
1263         struct arbel *arbel = ib_get_drvdata ( ibdev );
1264         const union ib_gid *gid;
1265
1266         /* Construct this work queue entry */
1267         MLX_FILL_1 ( &wqe->ud.ctrl, 0, always1, 1 );
1268         MLX_FILL_2 ( &wqe->ud.ud, 0,
1269                      ud_address_vector.pd, ARBEL_GLOBAL_PD,
1270                      ud_address_vector.port_number, ibdev->port );
1271         MLX_FILL_2 ( &wqe->ud.ud, 1,
1272                      ud_address_vector.rlid, dest->lid,
1273                      ud_address_vector.g, dest->gid_present );
1274         MLX_FILL_2 ( &wqe->ud.ud, 2,
1275                      ud_address_vector.max_stat_rate, arbel_rate ( dest ),
1276                      ud_address_vector.msg, 3 );
1277         MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, dest->sl );
1278         gid = ( dest->gid_present ? &dest->gid : &arbel_no_gid );
1279         memcpy ( &wqe->ud.ud.u.dwords[4], gid, sizeof ( *gid ) );
1280         MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, dest->qpn );
1281         MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, dest->qkey );
1282         MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
1283         MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, arbel->lkey );
1284         MLX_FILL_H ( &wqe->ud.data[0], 2,
1285                      local_address_h, virt_to_bus ( iobuf->data ) );
1286         MLX_FILL_1 ( &wqe->ud.data[0], 3,
1287                      local_address_l, virt_to_bus ( iobuf->data ) );
1288
1289         return ( offsetof ( typeof ( wqe->ud ), data[1] ) >> 4 );
1290 }
1291
1292 /**
1293  * Construct MLX send work queue entry
1294  *
1295  * @v ibdev             Infiniband device
1296  * @v qp                Queue pair
1297  * @v dest              Destination address vector
1298  * @v iobuf             I/O buffer
1299  * @v wqe               Send work queue entry
1300  * @ret nds             Work queue entry size
1301  */
1302 static size_t arbel_fill_mlx_send_wqe ( struct ib_device *ibdev,
1303                                         struct ib_queue_pair *qp,
1304                                         struct ib_address_vector *dest,
1305                                         struct io_buffer *iobuf,
1306                                         union arbel_send_wqe *wqe ) {
1307         struct arbel *arbel = ib_get_drvdata ( ibdev );
1308         struct io_buffer headers;
1309
1310         /* Construct IB headers */
1311         iob_populate ( &headers, &wqe->mlx.headers, 0,
1312                        sizeof ( wqe->mlx.headers ) );
1313         iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
1314         ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
1315
1316         /* Construct this work queue entry */
1317         MLX_FILL_5 ( &wqe->mlx.ctrl, 0,
1318                      c, 1 /* generate completion */,
1319                      icrc, 0 /* generate ICRC */,
1320                      max_statrate, arbel_rate ( dest ),
1321                      slr, 0,
1322                      v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
1323         MLX_FILL_1 ( &wqe->mlx.ctrl, 1, rlid, dest->lid );
1324         MLX_FILL_1 ( &wqe->mlx.data[0], 0,
1325                      byte_count, iob_len ( &headers ) );
1326         MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, arbel->lkey );
1327         MLX_FILL_H ( &wqe->mlx.data[0], 2,
1328                      local_address_h, virt_to_bus ( headers.data ) );
1329         MLX_FILL_1 ( &wqe->mlx.data[0], 3,
1330                      local_address_l, virt_to_bus ( headers.data ) );
1331         MLX_FILL_1 ( &wqe->mlx.data[1], 0,
1332                      byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
1333         MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, arbel->lkey );
1334         MLX_FILL_H ( &wqe->mlx.data[1], 2,
1335                      local_address_h, virt_to_bus ( iobuf->data ) );
1336         MLX_FILL_1 ( &wqe->mlx.data[1], 3,
1337                      local_address_l, virt_to_bus ( iobuf->data ) );
1338
1339         return ( offsetof ( typeof ( wqe->mlx ), data[2] ) >> 4 );
1340 }
1341
1342 /**
1343  * Construct RC send work queue entry
1344  *
1345  * @v ibdev             Infiniband device
1346  * @v qp                Queue pair
1347  * @v dest              Destination address vector
1348  * @v iobuf             I/O buffer
1349  * @v wqe               Send work queue entry
1350  * @ret nds             Work queue entry size
1351  */
1352 static size_t arbel_fill_rc_send_wqe ( struct ib_device *ibdev,
1353                                        struct ib_queue_pair *qp __unused,
1354                                        struct ib_address_vector *dest __unused,
1355                                        struct io_buffer *iobuf,
1356                                        union arbel_send_wqe *wqe ) {
1357         struct arbel *arbel = ib_get_drvdata ( ibdev );
1358
1359         /* Construct this work queue entry */
1360         MLX_FILL_1 ( &wqe->rc.ctrl, 0, always1, 1 );
1361         MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
1362         MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, arbel->lkey );
1363         MLX_FILL_H ( &wqe->rc.data[0], 2,
1364                      local_address_h, virt_to_bus ( iobuf->data ) );
1365         MLX_FILL_1 ( &wqe->rc.data[0], 3,
1366                      local_address_l, virt_to_bus ( iobuf->data ) );
1367
1368         return ( offsetof ( typeof ( wqe->rc ), data[1] ) >> 4 );
1369 }
1370
1371 /** Work queue entry constructors */
1372 static size_t
1373 ( * arbel_fill_send_wqe[] ) ( struct ib_device *ibdev,
1374                               struct ib_queue_pair *qp,
1375                               struct ib_address_vector *dest,
1376                               struct io_buffer *iobuf,
1377                               union arbel_send_wqe *wqe ) = {
1378         [IB_QPT_SMI] = arbel_fill_mlx_send_wqe,
1379         [IB_QPT_GSI] = arbel_fill_mlx_send_wqe,
1380         [IB_QPT_UD] = arbel_fill_ud_send_wqe,
1381         [IB_QPT_RC] = arbel_fill_rc_send_wqe,
1382 };
1383
1384 /**
1385  * Post 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  * @ret rc              Return status code
1392  */
1393 static int arbel_post_send ( struct ib_device *ibdev,
1394                              struct ib_queue_pair *qp,
1395                              struct ib_address_vector *dest,
1396                              struct io_buffer *iobuf ) {
1397         struct arbel *arbel = ib_get_drvdata ( ibdev );
1398         struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
1399         struct ib_work_queue *wq = &qp->send;
1400         struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
1401         union arbel_send_wqe *prev_wqe;
1402         union arbel_send_wqe *wqe;
1403         struct arbelprm_qp_db_record *qp_db_rec;
1404         union arbelprm_doorbell_register db_reg;
1405         unsigned long wqe_idx_mask;
1406         size_t nds;
1407
1408         /* Allocate work queue entry */
1409         wqe_idx_mask = ( wq->num_wqes - 1 );
1410         if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1411                 DBGC ( arbel, "Arbel %p QPN %#lx send queue full",
1412                        arbel, qp->qpn );
1413                 return -ENOBUFS;
1414         }
1415         wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1416         prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask];
1417         wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask];
1418
1419         /* Construct work queue entry */
1420         memset ( ( ( ( void * ) wqe ) + sizeof ( wqe->next ) ), 0,
1421                  ( sizeof ( *wqe ) - sizeof ( wqe->next ) ) );
1422         assert ( qp->type < ( sizeof ( arbel_fill_send_wqe ) /
1423                               sizeof ( arbel_fill_send_wqe[0] ) ) );
1424         assert ( arbel_fill_send_wqe[qp->type] != NULL );
1425         nds = arbel_fill_send_wqe[qp->type] ( ibdev, qp, dest, iobuf, wqe );
1426         DBGCP ( arbel, "Arbel %p QPN %#lx posting send WQE %#lx:\n",
1427                 arbel, qp->qpn, ( wq->next_idx & wqe_idx_mask ) );
1428         DBGCP_HDA ( arbel, virt_to_phys ( wqe ), wqe, sizeof ( *wqe ) );
1429
1430         /* Update previous work queue entry's "next" field */
1431         MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
1432         MLX_FILL_3 ( &prev_wqe->next, 1,
1433                      nds, nds,
1434                      f, 0,
1435                      always1, 1 );
1436
1437         /* Update doorbell record */
1438         barrier();
1439         qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
1440         MLX_FILL_1 ( qp_db_rec, 0,
1441                      counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
1442
1443         /* Ring doorbell register */
1444         MLX_FILL_4 ( &db_reg.send, 0,
1445                      nopcode, ARBEL_OPCODE_SEND,
1446                      f, 0,
1447                      wqe_counter, ( wq->next_idx & 0xffff ),
1448                      wqe_cnt, 1 );
1449         MLX_FILL_2 ( &db_reg.send, 1,
1450                      nds, nds,
1451                      qpn, qp->qpn );
1452         arbel_ring_doorbell ( arbel, &db_reg, ARBEL_DB_POST_SND_OFFSET );
1453
1454         /* Update work queue's index */
1455         wq->next_idx++;
1456
1457         return 0;
1458 }
1459
1460 /**
1461  * Post receive work queue entry
1462  *
1463  * @v ibdev             Infiniband device
1464  * @v qp                Queue pair
1465  * @v iobuf             I/O buffer
1466  * @ret rc              Return status code
1467  */
1468 static int arbel_post_recv ( struct ib_device *ibdev,
1469                              struct ib_queue_pair *qp,
1470                              struct io_buffer *iobuf ) {
1471         struct arbel *arbel = ib_get_drvdata ( ibdev );
1472         struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
1473         struct ib_work_queue *wq = &qp->recv;
1474         struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
1475         struct arbelprm_recv_wqe *wqe;
1476         union arbelprm_doorbell_record *db_rec;
1477         unsigned int wqe_idx_mask;
1478
1479         /* Allocate work queue entry */
1480         wqe_idx_mask = ( wq->num_wqes - 1 );
1481         if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1482                 DBGC ( arbel, "Arbel %p QPN %#lx receive queue full\n",
1483                        arbel, qp->qpn );
1484                 return -ENOBUFS;
1485         }
1486         wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1487         wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
1488
1489         /* Construct work queue entry */
1490         MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
1491         MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->lkey );
1492         MLX_FILL_H ( &wqe->data[0], 2,
1493                      local_address_h, virt_to_bus ( iobuf->data ) );
1494         MLX_FILL_1 ( &wqe->data[0], 3,
1495                      local_address_l, virt_to_bus ( iobuf->data ) );
1496
1497         /* Update doorbell record */
1498         barrier();
1499         db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
1500         MLX_FILL_1 ( &db_rec->qp, 0,
1501                      counter, ( ( wq->next_idx + 1 ) & 0xffff ) );      
1502
1503         /* Update work queue's index */
1504         wq->next_idx++;
1505
1506         return 0;       
1507 }
1508
1509 /**
1510  * Handle completion
1511  *
1512  * @v ibdev             Infiniband device
1513  * @v cq                Completion queue
1514  * @v cqe               Hardware completion queue entry
1515  * @ret rc              Return status code
1516  */
1517 static int arbel_complete ( struct ib_device *ibdev,
1518                             struct ib_completion_queue *cq,
1519                             union arbelprm_completion_entry *cqe ) {
1520         struct arbel *arbel = ib_get_drvdata ( ibdev );
1521         struct ib_work_queue *wq;
1522         struct ib_queue_pair *qp;
1523         struct arbel_queue_pair *arbel_qp;
1524         struct arbel_send_work_queue *arbel_send_wq;
1525         struct arbel_recv_work_queue *arbel_recv_wq;
1526         struct arbelprm_recv_wqe *recv_wqe;
1527         struct io_buffer *iobuf;
1528         struct ib_address_vector recv_dest;
1529         struct ib_address_vector recv_source;
1530         struct ib_global_route_header *grh;
1531         struct ib_address_vector *source;
1532         unsigned int opcode;
1533         unsigned long qpn;
1534         int is_send;
1535         unsigned long wqe_adr;
1536         unsigned long wqe_idx;
1537         size_t len;
1538         int rc = 0;
1539
1540         /* Parse completion */
1541         qpn = MLX_GET ( &cqe->normal, my_qpn );
1542         is_send = MLX_GET ( &cqe->normal, s );
1543         wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
1544         opcode = MLX_GET ( &cqe->normal, opcode );
1545         if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
1546                 /* "s" field is not valid for error opcodes */
1547                 is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
1548                 DBGC ( arbel, "Arbel %p CQN %#lx %s QPN %#lx syndrome %#x "
1549                        "vendor %#x\n", arbel, cq->cqn,
1550                        ( is_send ? "send" : "recv" ), qpn,
1551                        MLX_GET ( &cqe->error, syndrome ),
1552                        MLX_GET ( &cqe->error, vendor_code ) );
1553                 DBGC_HDA ( arbel, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
1554                 rc = -EIO;
1555                 /* Don't return immediately; propagate error to completer */
1556         }
1557
1558         /* Identify work queue */
1559         wq = ib_find_wq ( cq, qpn, is_send );
1560         if ( ! wq ) {
1561                 DBGC ( arbel, "Arbel %p CQN %#lx unknown %s QPN %#lx\n",
1562                        arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1563                 return -EIO;
1564         }
1565         qp = wq->qp;
1566         arbel_qp = ib_qp_get_drvdata ( qp );
1567         arbel_send_wq = &arbel_qp->send;
1568         arbel_recv_wq = &arbel_qp->recv;
1569
1570         /* Identify work queue entry index */
1571         if ( is_send ) {
1572                 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
1573                             sizeof ( arbel_send_wq->wqe[0] ) );
1574                 assert ( wqe_idx < qp->send.num_wqes );
1575         } else {
1576                 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
1577                             sizeof ( arbel_recv_wq->wqe[0] ) );
1578                 assert ( wqe_idx < qp->recv.num_wqes );
1579         }
1580
1581         DBGCP ( arbel, "Arbel %p CQN %#lx QPN %#lx %s WQE %#lx completed:\n",
1582                 arbel, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
1583                 wqe_idx );
1584         DBGCP_HDA ( arbel, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
1585
1586         /* Identify I/O buffer */
1587         iobuf = wq->iobufs[wqe_idx];
1588         if ( ! iobuf ) {
1589                 DBGC ( arbel, "Arbel %p CQN %#lx QPN %#lx empty %s WQE %#lx\n",
1590                        arbel, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
1591                        wqe_idx );
1592                 return -EIO;
1593         }
1594         wq->iobufs[wqe_idx] = NULL;
1595
1596         if ( is_send ) {
1597                 /* Hand off to completion handler */
1598                 ib_complete_send ( ibdev, qp, iobuf, rc );
1599         } else {
1600                 /* Set received length */
1601                 len = MLX_GET ( &cqe->normal, byte_cnt );
1602                 recv_wqe = &arbel_recv_wq->wqe[wqe_idx].recv;
1603                 assert ( MLX_GET ( &recv_wqe->data[0], local_address_l ) ==
1604                          virt_to_bus ( iobuf->data ) );
1605                 assert ( MLX_GET ( &recv_wqe->data[0], byte_count ) ==
1606                          iob_tailroom ( iobuf ) );
1607                 MLX_FILL_1 ( &recv_wqe->data[0], 0, byte_count, 0 );
1608                 MLX_FILL_1 ( &recv_wqe->data[0], 1,
1609                              l_key, ARBEL_INVALID_LKEY );
1610                 assert ( len <= iob_tailroom ( iobuf ) );
1611                 iob_put ( iobuf, len );
1612                 memset ( &recv_dest, 0, sizeof ( recv_dest ) );
1613                 recv_dest.qpn = qpn;
1614                 switch ( qp->type ) {
1615                 case IB_QPT_SMI:
1616                 case IB_QPT_GSI:
1617                 case IB_QPT_UD:
1618                         assert ( iob_len ( iobuf ) >= sizeof ( *grh ) );
1619                         grh = iobuf->data;
1620                         iob_pull ( iobuf, sizeof ( *grh ) );
1621                         /* Construct address vector */
1622                         source = &recv_source;
1623                         memset ( source, 0, sizeof ( *source ) );
1624                         source->qpn = MLX_GET ( &cqe->normal, rqpn );
1625                         source->lid = MLX_GET ( &cqe->normal, rlid );
1626                         source->sl = MLX_GET ( &cqe->normal, sl );
1627                         recv_dest.gid_present = source->gid_present =
1628                                 MLX_GET ( &cqe->normal, g );
1629                         memcpy ( &recv_dest.gid, &grh->dgid,
1630                                  sizeof ( recv_dest.gid ) );
1631                         memcpy ( &source->gid, &grh->sgid,
1632                                  sizeof ( source->gid ) );
1633                         break;
1634                 case IB_QPT_RC:
1635                         source = &qp->av;
1636                         break;
1637                 default:
1638                         assert ( 0 );
1639                         return -EINVAL;
1640                 }
1641                 /* Hand off to completion handler */
1642                 ib_complete_recv ( ibdev, qp, &recv_dest, source, iobuf, rc );
1643         }
1644
1645         return rc;
1646 }                            
1647
1648 /**
1649  * Poll completion queue
1650  *
1651  * @v ibdev             Infiniband device
1652  * @v cq                Completion queue
1653  */
1654 static void arbel_poll_cq ( struct ib_device *ibdev,
1655                             struct ib_completion_queue *cq ) {
1656         struct arbel *arbel = ib_get_drvdata ( ibdev );
1657         struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
1658         struct arbelprm_cq_ci_db_record *ci_db_rec;
1659         union arbelprm_completion_entry *cqe;
1660         unsigned int cqe_idx_mask;
1661         int rc;
1662
1663         while ( 1 ) {
1664                 /* Look for completion entry */
1665                 cqe_idx_mask = ( cq->num_cqes - 1 );
1666                 cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
1667                 if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
1668                         /* Entry still owned by hardware; end of poll */
1669                         break;
1670                 }
1671
1672                 /* Handle completion */
1673                 if ( ( rc = arbel_complete ( ibdev, cq, cqe ) ) != 0 ) {
1674                         DBGC ( arbel, "Arbel %p CQN %#lx failed to complete: "
1675                                "%s\n", arbel, cq->cqn, strerror ( rc ) );
1676                         DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
1677                 }
1678
1679                 /* Return ownership to hardware */
1680                 MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
1681                 barrier();
1682                 /* Update completion queue's index */
1683                 cq->next_idx++;
1684                 /* Update doorbell record */
1685                 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
1686                 MLX_FILL_1 ( ci_db_rec, 0,
1687                              counter, ( cq->next_idx & 0xffffffffUL ) );
1688         }
1689 }
1690
1691 /***************************************************************************
1692  *
1693  * Event queues
1694  *
1695  ***************************************************************************
1696  */
1697
1698 /**
1699  * Create event queue
1700  *
1701  * @v arbel             Arbel device
1702  * @ret rc              Return status code
1703  */
1704 static int arbel_create_eq ( struct arbel *arbel ) {
1705         struct arbel_event_queue *arbel_eq = &arbel->eq;
1706         struct arbelprm_eqc eqctx;
1707         struct arbelprm_event_mask mask;
1708         unsigned int i;
1709         int rc;
1710
1711         /* Select event queue number */
1712         arbel_eq->eqn = arbel->limits.reserved_eqs;
1713
1714         /* Calculate doorbell address */
1715         arbel_eq->doorbell = ( arbel->eq_ci_doorbells +
1716                                ARBEL_DB_EQ_OFFSET ( arbel_eq->eqn ) );
1717
1718         /* Allocate event queue itself */
1719         arbel_eq->eqe_size =
1720                 ( ARBEL_NUM_EQES * sizeof ( arbel_eq->eqe[0] ) );
1721         arbel_eq->eqe = malloc_dma ( arbel_eq->eqe_size,
1722                                      sizeof ( arbel_eq->eqe[0] ) );
1723         if ( ! arbel_eq->eqe ) {
1724                 rc = -ENOMEM;
1725                 goto err_eqe;
1726         }
1727         memset ( arbel_eq->eqe, 0, arbel_eq->eqe_size );
1728         for ( i = 0 ; i < ARBEL_NUM_EQES ; i++ ) {
1729                 MLX_FILL_1 ( &arbel_eq->eqe[i].generic, 7, owner, 1 );
1730         }
1731         barrier();
1732
1733         /* Hand queue over to hardware */
1734         memset ( &eqctx, 0, sizeof ( eqctx ) );
1735         MLX_FILL_1 ( &eqctx, 0, st, 0xa /* "Fired" */ );
1736         MLX_FILL_H ( &eqctx, 1,
1737                      start_address_h, virt_to_phys ( arbel_eq->eqe ) );
1738         MLX_FILL_1 ( &eqctx, 2,
1739                      start_address_l, virt_to_phys ( arbel_eq->eqe ) );
1740         MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( ARBEL_NUM_EQES - 1 ) );
1741         MLX_FILL_1 ( &eqctx, 6, pd, ARBEL_GLOBAL_PD );
1742         MLX_FILL_1 ( &eqctx, 7, lkey, arbel->lkey );
1743         if ( ( rc = arbel_cmd_sw2hw_eq ( arbel, arbel_eq->eqn,
1744                                          &eqctx ) ) != 0 ) {
1745                 DBGC ( arbel, "Arbel %p EQN %#lx SW2HW_EQ failed: %s\n",
1746                        arbel, arbel_eq->eqn, strerror ( rc ) );
1747                 goto err_sw2hw_eq;
1748         }
1749
1750         /* Map events to this event queue */
1751         memset ( &mask, 0xff, sizeof ( mask ) );
1752         if ( ( rc = arbel_cmd_map_eq ( arbel,
1753                                        ( ARBEL_MAP_EQ | arbel_eq->eqn ),
1754                                        &mask ) ) != 0 ) {
1755                 DBGC ( arbel, "Arbel %p EQN %#lx MAP_EQ failed: %s\n",
1756                        arbel, arbel_eq->eqn, strerror ( rc )  );
1757                 goto err_map_eq;
1758         }
1759
1760         DBGC ( arbel, "Arbel %p EQN %#lx ring [%08lx,%08lx), doorbell %08lx\n",
1761                arbel, arbel_eq->eqn, virt_to_phys ( arbel_eq->eqe ),
1762                ( virt_to_phys ( arbel_eq->eqe ) + arbel_eq->eqe_size ),
1763                virt_to_phys ( arbel_eq->doorbell ) );
1764         return 0;
1765
1766  err_map_eq:
1767         arbel_cmd_hw2sw_eq ( arbel, arbel_eq->eqn, &eqctx );
1768  err_sw2hw_eq:
1769         free_dma ( arbel_eq->eqe, arbel_eq->eqe_size );
1770  err_eqe:
1771         memset ( arbel_eq, 0, sizeof ( *arbel_eq ) );
1772         return rc;
1773 }
1774
1775 /**
1776  * Destroy event queue
1777  *
1778  * @v arbel             Arbel device
1779  */
1780 static void arbel_destroy_eq ( struct arbel *arbel ) {
1781         struct arbel_event_queue *arbel_eq = &arbel->eq;
1782         struct arbelprm_eqc eqctx;
1783         struct arbelprm_event_mask mask;
1784         int rc;
1785
1786         /* Unmap events from event queue */
1787         memset ( &mask, 0, sizeof ( mask ) );
1788         MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
1789         if ( ( rc = arbel_cmd_map_eq ( arbel,
1790                                        ( ARBEL_UNMAP_EQ | arbel_eq->eqn ),
1791                                        &mask ) ) != 0 ) {
1792                 DBGC ( arbel, "Arbel %p EQN %#lx FATAL MAP_EQ failed to "
1793                        "unmap: %s\n", arbel, arbel_eq->eqn, strerror ( rc ) );
1794                 /* Continue; HCA may die but system should survive */
1795         }
1796
1797         /* Take ownership back from hardware */
1798         if ( ( rc = arbel_cmd_hw2sw_eq ( arbel, arbel_eq->eqn,
1799                                          &eqctx ) ) != 0 ) {
1800                 DBGC ( arbel, "Arbel %p EQN %#lx FATAL HW2SW_EQ failed: %s\n",
1801                        arbel, arbel_eq->eqn, strerror ( rc ) );
1802                 /* Leak memory and return; at least we avoid corruption */
1803                 return;
1804         }
1805
1806         /* Free memory */
1807         free_dma ( arbel_eq->eqe, arbel_eq->eqe_size );
1808         memset ( arbel_eq, 0, sizeof ( *arbel_eq ) );
1809 }
1810
1811 /**
1812  * Handle port state event
1813  *
1814  * @v arbel             Arbel device
1815  * @v eqe               Port state change event queue entry
1816  */
1817 static void arbel_event_port_state_change ( struct arbel *arbel,
1818                                             union arbelprm_event_entry *eqe){
1819         unsigned int port;
1820         int link_up;
1821
1822         /* Get port and link status */
1823         port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
1824         link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
1825         DBGC ( arbel, "Arbel %p port %d link %s\n", arbel, ( port + 1 ),
1826                ( link_up ? "up" : "down" ) );
1827
1828         /* Sanity check */
1829         if ( port >= ARBEL_NUM_PORTS ) {
1830                 DBGC ( arbel, "Arbel %p port %d does not exist!\n",
1831                        arbel, ( port + 1 ) );
1832                 return;
1833         }
1834
1835         /* Update MAD parameters */
1836         ib_smc_update ( arbel->ibdev[port], arbel_mad );
1837 }
1838
1839 /**
1840  * Poll event queue
1841  *
1842  * @v ibdev             Infiniband device
1843  */
1844 static void arbel_poll_eq ( struct ib_device *ibdev ) {
1845         struct arbel *arbel = ib_get_drvdata ( ibdev );
1846         struct arbel_event_queue *arbel_eq = &arbel->eq;
1847         union arbelprm_event_entry *eqe;
1848         union arbelprm_eq_doorbell_register db_reg;
1849         unsigned int eqe_idx_mask;
1850         unsigned int event_type;
1851
1852         /* No event is generated upon reaching INIT, so we must poll
1853          * separately for link state changes while we remain DOWN.
1854          */
1855         if ( ib_is_open ( ibdev ) &&
1856              ( ibdev->port_state == IB_PORT_STATE_DOWN ) ) {
1857                 ib_smc_update ( ibdev, arbel_mad );
1858         }
1859
1860         /* Poll event queue */
1861         while ( 1 ) {
1862                 /* Look for event entry */
1863                 eqe_idx_mask = ( ARBEL_NUM_EQES - 1 );
1864                 eqe = &arbel_eq->eqe[arbel_eq->next_idx & eqe_idx_mask];
1865                 if ( MLX_GET ( &eqe->generic, owner ) != 0 ) {
1866                         /* Entry still owned by hardware; end of poll */
1867                         break;
1868                 }
1869                 DBGCP ( arbel, "Arbel %p EQN %#lx event:\n",
1870                         arbel, arbel_eq->eqn );
1871                 DBGCP_HDA ( arbel, virt_to_phys ( eqe ),
1872                             eqe, sizeof ( *eqe ) );
1873
1874                 /* Handle event */
1875                 event_type = MLX_GET ( &eqe->generic, event_type );
1876                 switch ( event_type ) {
1877                 case ARBEL_EV_PORT_STATE_CHANGE:
1878                         arbel_event_port_state_change ( arbel, eqe );
1879                         break;
1880                 default:
1881                         DBGC ( arbel, "Arbel %p EQN %#lx unrecognised event "
1882                                "type %#x:\n",
1883                                arbel, arbel_eq->eqn, event_type );
1884                         DBGC_HDA ( arbel, virt_to_phys ( eqe ),
1885                                    eqe, sizeof ( *eqe ) );
1886                         break;
1887                 }
1888
1889                 /* Return ownership to hardware */
1890                 MLX_FILL_1 ( &eqe->generic, 7, owner, 1 );
1891                 barrier();
1892
1893                 /* Update event queue's index */
1894                 arbel_eq->next_idx++;
1895
1896                 /* Ring doorbell */
1897                 MLX_FILL_1 ( &db_reg.ci, 0, ci, arbel_eq->next_idx );
1898                 writel ( db_reg.dword[0], arbel_eq->doorbell );
1899         }
1900 }
1901
1902 /***************************************************************************
1903  *
1904  * Firmware control
1905  *
1906  ***************************************************************************
1907  */
1908
1909 /**
1910  * Map virtual to physical address for firmware usage
1911  *
1912  * @v arbel             Arbel device
1913  * @v map               Mapping function
1914  * @v va                Virtual address
1915  * @v pa                Physical address
1916  * @v len               Length of region
1917  * @ret rc              Return status code
1918  */
1919 static int arbel_map_vpm ( struct arbel *arbel,
1920                            int ( *map ) ( struct arbel *arbel,
1921                              const struct arbelprm_virtual_physical_mapping* ),
1922                            uint64_t va, physaddr_t pa, size_t len ) {
1923         struct arbelprm_virtual_physical_mapping mapping;
1924         physaddr_t start;
1925         physaddr_t low;
1926         physaddr_t high;
1927         physaddr_t end;
1928         size_t size;
1929         int rc;
1930
1931         /* Sanity checks */
1932         assert ( ( va & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
1933         assert ( ( pa & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
1934         assert ( ( len & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
1935
1936         /* Calculate starting points */
1937         start = pa;
1938         end = ( start + len );
1939         size = ( 1UL << ( fls ( start ^ end ) - 1 ) );
1940         low = high = ( end & ~( size - 1 ) );
1941         assert ( start < low );
1942         assert ( high <= end );
1943
1944         /* These mappings tend to generate huge volumes of
1945          * uninteresting debug data, which basically makes it
1946          * impossible to use debugging otherwise.
1947          */
1948         DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
1949
1950         /* Map blocks in descending order of size */
1951         while ( size >= ARBEL_PAGE_SIZE ) {
1952
1953                 /* Find the next candidate block */
1954                 if ( ( low - size ) >= start ) {
1955                         low -= size;
1956                         pa = low;
1957                 } else if ( ( high + size ) <= end ) {
1958                         pa = high;
1959                         high += size;
1960                 } else {
1961                         size >>= 1;
1962                         continue;
1963                 }
1964                 assert ( ( va & ( size - 1 ) ) == 0 );
1965                 assert ( ( pa & ( size - 1 ) ) == 0 );
1966
1967                 /* Map this block */
1968                 memset ( &mapping, 0, sizeof ( mapping ) );
1969                 MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
1970                 MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
1971                 MLX_FILL_H ( &mapping, 2, pa_h, pa );
1972                 MLX_FILL_2 ( &mapping, 3,
1973                              log2size, ( ( fls ( size ) - 1 ) - 12 ),
1974                              pa_l, ( pa >> 12 ) );
1975                 if ( ( rc = map ( arbel, &mapping ) ) != 0 ) {
1976                         DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
1977                         DBGC ( arbel, "Arbel %p could not map %08llx+%zx to "
1978                                "%08lx: %s\n",
1979                                arbel, va, size, pa, strerror ( rc ) );
1980                         return rc;
1981                 }
1982                 va += size;
1983         }
1984         assert ( low == start );
1985         assert ( high == end );
1986
1987         DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
1988         return 0;
1989 }
1990
1991 /**
1992  * Start firmware running
1993  *
1994  * @v arbel             Arbel device
1995  * @ret rc              Return status code
1996  */
1997 static int arbel_start_firmware ( struct arbel *arbel ) {
1998         struct arbelprm_query_fw fw;
1999         struct arbelprm_access_lam lam;
2000         unsigned int fw_pages;
2001         size_t fw_len;
2002         physaddr_t fw_base;
2003         uint64_t eq_set_ci_base_addr;
2004         int rc;
2005
2006         /* Get firmware parameters */
2007         if ( ( rc = arbel_cmd_query_fw ( arbel, &fw ) ) != 0 ) {
2008                 DBGC ( arbel, "Arbel %p could not query firmware: %s\n",
2009                        arbel, strerror ( rc ) );
2010                 goto err_query_fw;
2011         }
2012         DBGC ( arbel, "Arbel %p firmware version %d.%d.%d\n", arbel,
2013                MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
2014                MLX_GET ( &fw, fw_rev_subminor ) );
2015         fw_pages = MLX_GET ( &fw, fw_pages );
2016         DBGC ( arbel, "Arbel %p requires %d kB for firmware\n",
2017                arbel, ( fw_pages * 4 ) );
2018         eq_set_ci_base_addr =
2019                 ( ( (uint64_t) MLX_GET ( &fw, eq_set_ci_base_addr_h ) << 32 ) |
2020                   ( (uint64_t) MLX_GET ( &fw, eq_set_ci_base_addr_l ) ) );
2021         arbel->eq_ci_doorbells = ioremap ( eq_set_ci_base_addr, 0x200 );
2022
2023         /* Enable locally-attached memory.  Ignore failure; there may
2024          * be no attached memory.
2025          */
2026         arbel_cmd_enable_lam ( arbel, &lam );
2027
2028         /* Allocate firmware pages and map firmware area */
2029         fw_len = ( fw_pages * ARBEL_PAGE_SIZE );
2030         if ( ! arbel->firmware_area ) {
2031                 arbel->firmware_len = fw_len;
2032                 arbel->firmware_area = umalloc ( arbel->firmware_len );
2033                 if ( ! arbel->firmware_area ) {
2034                         rc = -ENOMEM;
2035                         goto err_alloc_fa;
2036                 }
2037         } else {
2038                 assert ( arbel->firmware_len == fw_len );
2039         }
2040         fw_base = user_to_phys ( arbel->firmware_area, 0 );
2041         DBGC ( arbel, "Arbel %p firmware area at [%08lx,%08lx)\n",
2042                arbel, fw_base, ( fw_base + fw_len ) );
2043         if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_fa,
2044                                     0, fw_base, fw_len ) ) != 0 ) {
2045                 DBGC ( arbel, "Arbel %p could not map firmware: %s\n",
2046                        arbel, strerror ( rc ) );
2047                 goto err_map_fa;
2048         }
2049
2050         /* Start firmware */
2051         if ( ( rc = arbel_cmd_run_fw ( arbel ) ) != 0 ) {
2052                 DBGC ( arbel, "Arbel %p could not run firmware: %s\n",
2053                        arbel, strerror ( rc ) );
2054                 goto err_run_fw;
2055         }
2056
2057         DBGC ( arbel, "Arbel %p firmware started\n", arbel );
2058         return 0;
2059
2060  err_run_fw:
2061         arbel_cmd_unmap_fa ( arbel );
2062  err_map_fa:
2063  err_alloc_fa:
2064  err_query_fw:
2065         return rc;
2066 }
2067
2068 /**
2069  * Stop firmware running
2070  *
2071  * @v arbel             Arbel device
2072  */
2073 static void arbel_stop_firmware ( struct arbel *arbel ) {
2074         int rc;
2075
2076         if ( ( rc = arbel_cmd_unmap_fa ( arbel ) ) != 0 ) {
2077                 DBGC ( arbel, "Arbel %p FATAL could not stop firmware: %s\n",
2078                        arbel, strerror ( rc ) );
2079                 /* Leak memory and return; at least we avoid corruption */
2080                 arbel->firmware_area = UNULL;
2081                 return;
2082         }
2083 }
2084
2085 /***************************************************************************
2086  *
2087  * Infinihost Context Memory management
2088  *
2089  ***************************************************************************
2090  */
2091
2092 /**
2093  * Get device limits
2094  *
2095  * @v arbel             Arbel device
2096  * @ret rc              Return status code
2097  */
2098 static int arbel_get_limits ( struct arbel *arbel ) {
2099         struct arbelprm_query_dev_lim dev_lim;
2100         int rc;
2101
2102         if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
2103                 DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
2104                        arbel, strerror ( rc ) );
2105                 return rc;
2106         }
2107
2108         arbel->limits.reserved_qps =
2109                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
2110         arbel->limits.qpc_entry_size = MLX_GET ( &dev_lim, qpc_entry_sz );
2111         arbel->limits.eqpc_entry_size = MLX_GET ( &dev_lim, eqpc_entry_sz );
2112         arbel->limits.reserved_srqs =
2113                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_srqs ) );
2114         arbel->limits.srqc_entry_size = MLX_GET ( &dev_lim, srq_entry_sz );
2115         arbel->limits.reserved_ees =
2116                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_ees ) );
2117         arbel->limits.eec_entry_size = MLX_GET ( &dev_lim, eec_entry_sz );
2118         arbel->limits.eeec_entry_size = MLX_GET ( &dev_lim, eeec_entry_sz );
2119         arbel->limits.reserved_cqs =
2120                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
2121         arbel->limits.cqc_entry_size = MLX_GET ( &dev_lim, cqc_entry_sz );
2122         arbel->limits.reserved_mtts =
2123                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mtts ) );
2124         arbel->limits.mtt_entry_size = MLX_GET ( &dev_lim, mtt_entry_sz );
2125         arbel->limits.reserved_mrws =
2126                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mrws ) );
2127         arbel->limits.mpt_entry_size = MLX_GET ( &dev_lim, mpt_entry_sz );
2128         arbel->limits.reserved_rdbs =
2129                 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_rdbs ) );
2130         arbel->limits.reserved_eqs = MLX_GET ( &dev_lim, num_rsvd_eqs );
2131         arbel->limits.eqc_entry_size = MLX_GET ( &dev_lim, eqc_entry_sz );
2132         arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
2133         arbel->limits.uar_scratch_entry_size =
2134                 MLX_GET ( &dev_lim, uar_scratch_entry_sz );
2135
2136         DBGC ( arbel, "Arbel %p reserves %d x %#zx QPC, %d x %#zx EQPC, "
2137                "%d x %#zx SRQC\n", arbel,
2138                arbel->limits.reserved_qps, arbel->limits.qpc_entry_size,
2139                arbel->limits.reserved_qps, arbel->limits.eqpc_entry_size,
2140                arbel->limits.reserved_srqs, arbel->limits.srqc_entry_size );
2141         DBGC ( arbel, "Arbel %p reserves %d x %#zx EEC, %d x %#zx EEEC, "
2142                "%d x %#zx CQC\n", arbel,
2143                arbel->limits.reserved_ees, arbel->limits.eec_entry_size,
2144                arbel->limits.reserved_ees, arbel->limits.eeec_entry_size,
2145                arbel->limits.reserved_cqs, arbel->limits.cqc_entry_size );
2146         DBGC ( arbel, "Arbel %p reserves %d x %#zx EQC, %d x %#zx MTT, "
2147                "%d x %#zx MPT\n", arbel,
2148                arbel->limits.reserved_eqs, arbel->limits.eqc_entry_size,
2149                arbel->limits.reserved_mtts, arbel->limits.mtt_entry_size,
2150                arbel->limits.reserved_mrws, arbel->limits.mpt_entry_size );
2151         DBGC ( arbel, "Arbel %p reserves %d x %#zx RDB, %d x %#zx UAR, "
2152                "%d x %#zx UAR scratchpad\n", arbel,
2153                arbel->limits.reserved_rdbs, ARBEL_RDB_ENTRY_SIZE,
2154                arbel->limits.reserved_uars, ARBEL_PAGE_SIZE,
2155                arbel->limits.reserved_uars,
2156                arbel->limits.uar_scratch_entry_size );
2157
2158         return 0;
2159 }
2160
2161 /**
2162  * Align ICM table
2163  *
2164  * @v icm_offset        Current ICM offset
2165  * @v len               ICM table length
2166  * @ret icm_offset      ICM offset
2167  */
2168 static size_t icm_align ( size_t icm_offset, size_t len ) {
2169
2170         /* Round up to a multiple of the table size */
2171         assert ( len == ( 1UL << ( fls ( len ) - 1 ) ) );
2172         return ( ( icm_offset + len - 1 ) & ~( len - 1 ) );
2173 }
2174
2175 /**
2176  * Allocate ICM
2177  *
2178  * @v arbel             Arbel device
2179  * @v init_hca          INIT_HCA structure to fill in
2180  * @ret rc              Return status code
2181  */
2182 static int arbel_alloc_icm ( struct arbel *arbel,
2183                              struct arbelprm_init_hca *init_hca ) {
2184         struct arbelprm_scalar_parameter icm_size;
2185         struct arbelprm_scalar_parameter icm_aux_size;
2186         struct arbelprm_scalar_parameter unmap_icm;
2187         union arbelprm_doorbell_record *db_rec;
2188         size_t icm_offset = 0;
2189         unsigned int log_num_uars, log_num_qps, log_num_srqs, log_num_ees;
2190         unsigned int log_num_cqs, log_num_mtts, log_num_mpts, log_num_rdbs;
2191         unsigned int log_num_eqs, log_num_mcs;
2192         size_t icm_len, icm_aux_len;
2193         size_t len;
2194         physaddr_t icm_phys;
2195         int rc;
2196
2197         /* Calculate number of each object type within ICM */
2198         log_num_qps = fls ( arbel->limits.reserved_qps +
2199                             ARBEL_RSVD_SPECIAL_QPS + ARBEL_MAX_QPS - 1 );
2200         log_num_srqs = fls ( arbel->limits.reserved_srqs - 1 );
2201         log_num_ees = fls ( arbel->limits.reserved_ees - 1 );
2202         log_num_cqs = fls ( arbel->limits.reserved_cqs + ARBEL_MAX_CQS - 1 );
2203         log_num_eqs = fls ( arbel->limits.reserved_eqs + ARBEL_MAX_EQS - 1 );
2204         log_num_mtts = fls ( arbel->limits.reserved_mtts - 1 );
2205         log_num_mpts = fls ( arbel->limits.reserved_mrws + 1 - 1 );
2206         log_num_rdbs = fls ( arbel->limits.reserved_rdbs +
2207                              ARBEL_RSVD_SPECIAL_QPS + ARBEL_MAX_QPS - 1 );
2208         log_num_uars = fls ( arbel->limits.reserved_uars +
2209                              1 /* single UAR used */ - 1 );
2210         log_num_mcs = ARBEL_LOG_MULTICAST_HASH_SIZE;
2211
2212         /* Queue pair contexts */
2213         len = ( ( 1 << log_num_qps ) * arbel->limits.qpc_entry_size );
2214         icm_offset = icm_align ( icm_offset, len );
2215         MLX_FILL_2 ( init_hca, 13,
2216                      qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
2217                      ( icm_offset >> 7 ),
2218                      qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
2219                      log_num_qps );
2220         DBGC ( arbel, "Arbel %p ICM QPC is %d x %#zx at [%zx,%zx)\n",
2221                arbel, ( 1 << log_num_qps ), arbel->limits.qpc_entry_size,
2222                icm_offset, ( icm_offset + len ) );
2223         icm_offset += len;
2224
2225         /* Extended queue pair contexts */
2226         len = ( ( 1 << log_num_qps ) * arbel->limits.eqpc_entry_size );
2227         icm_offset = icm_align ( icm_offset, len );
2228         MLX_FILL_1 ( init_hca, 25,
2229                      qpc_eec_cqc_eqc_rdb_parameters.eqpc_base_addr_l,
2230                      icm_offset );
2231         DBGC ( arbel, "Arbel %p ICM EQPC is %d x %#zx at [%zx,%zx)\n",
2232                arbel, ( 1 << log_num_qps ), arbel->limits.eqpc_entry_size,
2233                icm_offset, ( icm_offset + len ) );
2234         icm_offset += len;
2235
2236         /* Completion queue contexts */
2237         len = ( ( 1 << log_num_cqs ) * arbel->limits.cqc_entry_size );
2238         icm_offset = icm_align ( icm_offset, len );
2239         MLX_FILL_2 ( init_hca, 21,
2240                      qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
2241                      ( icm_offset >> 6 ),
2242                      qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
2243                      log_num_cqs );
2244         DBGC ( arbel, "Arbel %p ICM CQC is %d x %#zx at [%zx,%zx)\n",
2245                arbel, ( 1 << log_num_cqs ), arbel->limits.cqc_entry_size,
2246                icm_offset, ( icm_offset + len ) );
2247         icm_offset += len;
2248
2249         /* Event queue contexts */
2250         len = ( ( 1 << log_num_eqs ) * arbel->limits.eqc_entry_size );
2251         icm_offset = icm_align ( icm_offset, len );
2252         MLX_FILL_2 ( init_hca, 33,
2253                      qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
2254                      ( icm_offset >> 6 ),
2255                      qpc_eec_cqc_eqc_rdb_parameters.log_num_eq,
2256                      log_num_eqs );
2257         DBGC ( arbel, "Arbel %p ICM EQC is %d x %#zx at [%zx,%zx)\n",
2258                arbel, ( 1 << log_num_eqs ), arbel->limits.eqc_entry_size,
2259                icm_offset, ( icm_offset + len ) );
2260         icm_offset += len;
2261
2262         /* End-to-end contexts */
2263         len = ( ( 1 << log_num_ees ) * arbel->limits.eec_entry_size );
2264         icm_offset = icm_align ( icm_offset, len );
2265         MLX_FILL_2 ( init_hca, 17,
2266                      qpc_eec_cqc_eqc_rdb_parameters.eec_base_addr_l,
2267                      ( icm_offset >> 7 ),
2268                      qpc_eec_cqc_eqc_rdb_parameters.log_num_of_ee,
2269                      log_num_ees );
2270         DBGC ( arbel, "Arbel %p ICM EEC is %d x %#zx at [%zx,%zx)\n",
2271                arbel, ( 1 << log_num_ees ), arbel->limits.eec_entry_size,
2272                icm_offset, ( icm_offset + len ) );
2273         icm_offset += len;
2274
2275         /* Shared receive queue contexts */
2276         len = ( ( 1 << log_num_srqs ) * arbel->limits.srqc_entry_size );
2277         icm_offset = icm_align ( icm_offset, len );
2278         MLX_FILL_2 ( init_hca, 19,
2279                      qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
2280                      ( icm_offset >> 5 ),
2281                      qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
2282                      log_num_srqs );
2283         DBGC ( arbel, "Arbel %p ICM SRQC is %d x %#zx at [%zx,%zx)\n",
2284                arbel, ( 1 << log_num_srqs ), arbel->limits.srqc_entry_size,
2285                icm_offset, ( icm_offset + len ) );
2286         icm_offset += len;
2287
2288         /* Memory protection table */
2289         len = ( ( 1 << log_num_mpts ) * arbel->limits.mpt_entry_size );
2290         icm_offset = icm_align ( icm_offset, len );
2291         MLX_FILL_1 ( init_hca, 61,
2292                      tpt_parameters.mpt_base_adr_l, icm_offset );
2293         MLX_FILL_1 ( init_hca, 62,
2294                      tpt_parameters.log_mpt_sz, log_num_mpts );
2295         DBGC ( arbel, "Arbel %p ICM MPT is %d x %#zx at [%zx,%zx)\n",
2296                arbel, ( 1 << log_num_mpts ), arbel->limits.mpt_entry_size,
2297                icm_offset, ( icm_offset + len ) );
2298         icm_offset += len;
2299
2300         /* Remote read data base table */
2301         len = ( ( 1 << log_num_rdbs ) * ARBEL_RDB_ENTRY_SIZE );
2302         icm_offset = icm_align ( icm_offset, len );
2303         MLX_FILL_1 ( init_hca, 37,
2304                      qpc_eec_cqc_eqc_rdb_parameters.rdb_base_addr_l,
2305                      icm_offset );
2306         DBGC ( arbel, "Arbel %p ICM RDB is %d x %#zx at [%zx,%zx)\n",
2307                arbel, ( 1 << log_num_rdbs ), ARBEL_RDB_ENTRY_SIZE,
2308                icm_offset, ( icm_offset + len ) );
2309         icm_offset += len;
2310
2311         /* Extended end-to-end contexts */
2312         len = ( ( 1 << log_num_ees ) * arbel->limits.eeec_entry_size );
2313         icm_offset = icm_align ( icm_offset, len );
2314         MLX_FILL_1 ( init_hca, 29,
2315                      qpc_eec_cqc_eqc_rdb_parameters.eeec_base_addr_l,
2316                      icm_offset );
2317         DBGC ( arbel, "Arbel %p ICM EEEC is %d x %#zx at [%zx,%zx)\n",
2318                arbel, ( 1 << log_num_ees ), arbel->limits.eeec_entry_size,
2319                icm_offset, ( icm_offset + len ) );
2320         icm_offset += len;
2321
2322         /* Multicast table */
2323         len = ( ( 1 << log_num_mcs ) * sizeof ( struct arbelprm_mgm_entry ) );
2324         icm_offset = icm_align ( icm_offset, len );
2325         MLX_FILL_1 ( init_hca, 49,
2326                      multicast_parameters.mc_base_addr_l, icm_offset );
2327         MLX_FILL_1 ( init_hca, 52,
2328                      multicast_parameters.log_mc_table_entry_sz,
2329                      fls ( sizeof ( struct arbelprm_mgm_entry ) - 1 ) );
2330         MLX_FILL_1 ( init_hca, 53,
2331                      multicast_parameters.mc_table_hash_sz,
2332                      ( 1 << log_num_mcs ) );
2333         MLX_FILL_1 ( init_hca, 54,
2334                      multicast_parameters.log_mc_table_sz,
2335                      log_num_mcs /* Only one entry per hash */ );
2336         DBGC ( arbel, "Arbel %p ICM MC is %d x %#zx at [%zx,%zx)\n", arbel,
2337                ( 1 << log_num_mcs ), sizeof ( struct arbelprm_mgm_entry ),
2338                icm_offset, ( icm_offset + len ) );
2339         icm_offset += len;
2340
2341         /* Memory translation table */
2342         len = ( ( 1 << log_num_mtts ) * arbel->limits.mtt_entry_size );
2343         icm_offset = icm_align ( icm_offset, len );
2344         MLX_FILL_1 ( init_hca, 65,
2345                      tpt_parameters.mtt_base_addr_l, icm_offset );
2346         DBGC ( arbel, "Arbel %p ICM MTT is %d x %#zx at [%zx,%zx)\n",
2347                arbel, ( 1 << log_num_mtts ), arbel->limits.mtt_entry_size,
2348                icm_offset, ( icm_offset + len ) );
2349         icm_offset += len;
2350
2351         /* User access region scratchpads */
2352         len = ( ( 1 << log_num_uars ) * arbel->limits.uar_scratch_entry_size );
2353         icm_offset = icm_align ( icm_offset, len );
2354         MLX_FILL_1 ( init_hca, 77,
2355                      uar_parameters.uar_scratch_base_addr_l, icm_offset );
2356         DBGC ( arbel, "Arbel %p UAR scratchpad is %d x %#zx at [%zx,%zx)\n",
2357                arbel, ( 1 << log_num_uars ),
2358                arbel->limits.uar_scratch_entry_size,
2359                icm_offset, ( icm_offset + len ) );
2360         icm_offset += len;
2361
2362         /* Record amount of ICM to be allocated */
2363         icm_offset = icm_align ( icm_offset, ARBEL_PAGE_SIZE );
2364         icm_len = icm_offset;
2365
2366         /* User access region contexts
2367          *
2368          * The reserved UAR(s) do not need to be backed by physical
2369          * memory, and our UAR is allocated separately; neither are
2370          * part of the umalloc()ed ICM block, but both contribute to
2371          * the total length of ICM virtual address space.
2372          */
2373         len = ( ( 1 << log_num_uars ) * ARBEL_PAGE_SIZE );
2374         icm_offset = icm_align ( icm_offset, len );
2375         MLX_FILL_1 ( init_hca, 74, uar_parameters.log_max_uars, log_num_uars );
2376         MLX_FILL_1 ( init_hca, 79,
2377                      uar_parameters.uar_context_base_addr_l, icm_offset );
2378         arbel->db_rec_offset =
2379                 ( icm_offset +
2380                   ( arbel->limits.reserved_uars * ARBEL_PAGE_SIZE ) );
2381         DBGC ( arbel, "Arbel %p UAR is %d x %#zx at [%zx,%zx), doorbells "
2382                "[%zx,%zx)\n", arbel, ( 1 << log_num_uars ), ARBEL_PAGE_SIZE,
2383                icm_offset, ( icm_offset + len ), arbel->db_rec_offset,
2384                ( arbel->db_rec_offset + ARBEL_PAGE_SIZE ) );
2385         icm_offset += len;
2386
2387         /* Get ICM auxiliary area size */
2388         memset ( &icm_size, 0, sizeof ( icm_size ) );
2389         MLX_FILL_1 ( &icm_size, 1, value, icm_len );
2390         if ( ( rc = arbel_cmd_set_icm_size ( arbel, &icm_size,
2391                                              &icm_aux_size ) ) != 0 ) {
2392                 DBGC ( arbel, "Arbel %p could not set ICM size: %s\n",
2393                        arbel, strerror ( rc ) );
2394                 goto err_set_icm_size;
2395         }
2396         icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * ARBEL_PAGE_SIZE );
2397
2398         /* Allocate ICM data and auxiliary area */
2399         DBGC ( arbel, "Arbel %p requires %zd kB ICM and %zd kB AUX ICM\n",
2400                arbel, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
2401         if ( ! arbel->icm ) {
2402                 arbel->icm_len = icm_len;
2403                 arbel->icm_aux_len = icm_aux_len;
2404                 arbel->icm = umalloc ( arbel->icm_len + arbel->icm_aux_len );
2405                 if ( ! arbel->icm ) {
2406                         rc = -ENOMEM;
2407                         goto err_alloc_icm;
2408                 }
2409         } else {
2410                 assert ( arbel->icm_len == icm_len );
2411                 assert ( arbel->icm_aux_len == icm_aux_len );
2412         }
2413         icm_phys = user_to_phys ( arbel->icm, 0 );
2414
2415         /* Allocate doorbell UAR */
2416         arbel->db_rec = malloc_dma ( ARBEL_PAGE_SIZE, ARBEL_PAGE_SIZE );
2417         if ( ! arbel->db_rec ) {
2418                 rc = -ENOMEM;
2419                 goto err_alloc_doorbell;
2420         }
2421
2422         /* Map ICM auxiliary area */
2423         DBGC ( arbel, "Arbel %p ICM AUX at [%08lx,%08lx)\n",
2424                arbel, icm_phys, ( icm_phys + arbel->icm_aux_len ) );
2425         if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm_aux,
2426                                     0, icm_phys, arbel->icm_aux_len ) ) != 0 ){
2427                 DBGC ( arbel, "Arbel %p could not map AUX ICM: %s\n",
2428                        arbel, strerror ( rc ) );
2429                 goto err_map_icm_aux;
2430         }
2431         icm_phys += arbel->icm_aux_len;
2432
2433         /* Map ICM area */
2434         DBGC ( arbel, "Arbel %p ICM at [%08lx,%08lx)\n",
2435                arbel, icm_phys, ( icm_phys + arbel->icm_len ) );
2436         if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm,
2437                                     0, icm_phys, arbel->icm_len ) ) != 0 ) {
2438                 DBGC ( arbel, "Arbel %p could not map ICM: %s\n",
2439                        arbel, strerror ( rc ) );
2440                 goto err_map_icm;
2441         }
2442         icm_phys += arbel->icm_len;
2443
2444         /* Map doorbell UAR */
2445         DBGC ( arbel, "Arbel %p UAR at [%08lx,%08lx)\n",
2446                arbel, virt_to_phys ( arbel->db_rec ),
2447                ( virt_to_phys ( arbel->db_rec ) + ARBEL_PAGE_SIZE ) );
2448         if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm,
2449                                     arbel->db_rec_offset,
2450                                     virt_to_phys ( arbel->db_rec ),
2451                                     ARBEL_PAGE_SIZE ) ) != 0 ) {
2452                 DBGC ( arbel, "Arbel %p could not map doorbell UAR: %s\n",
2453                        arbel, strerror ( rc ) );
2454                 goto err_map_doorbell;
2455         }
2456
2457         /* Initialise doorbell records */
2458         memset ( arbel->db_rec, 0, ARBEL_PAGE_SIZE );
2459         db_rec = &arbel->db_rec[ARBEL_GROUP_SEPARATOR_DOORBELL];
2460         MLX_FILL_1 ( &db_rec->qp, 1, res, ARBEL_UAR_RES_GROUP_SEP );
2461
2462         return 0;
2463
2464         memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
2465         MLX_FILL_1 ( &unmap_icm, 1, value, arbel->db_rec_offset );
2466         arbel_cmd_unmap_icm ( arbel, 1, &unmap_icm );
2467  err_map_doorbell:
2468         memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
2469         arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / ARBEL_PAGE_SIZE ),
2470                               &unmap_icm );
2471  err_map_icm:
2472         arbel_cmd_unmap_icm_aux ( arbel );
2473  err_map_icm_aux:
2474         free_dma ( arbel->db_rec, ARBEL_PAGE_SIZE );
2475         arbel->db_rec= NULL;
2476  err_alloc_doorbell:
2477  err_alloc_icm:
2478  err_set_icm_size:
2479         return rc;
2480 }
2481
2482 /**
2483  * Free ICM
2484  *
2485  * @v arbel             Arbel device
2486  */
2487 static void arbel_free_icm ( struct arbel *arbel ) {
2488         struct arbelprm_scalar_parameter unmap_icm;
2489
2490         memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
2491         MLX_FILL_1 ( &unmap_icm, 1, value, arbel->db_rec_offset );
2492         arbel_cmd_unmap_icm ( arbel, 1, &unmap_icm );
2493         memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
2494         arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / ARBEL_PAGE_SIZE ),
2495                               &unmap_icm );
2496         arbel_cmd_unmap_icm_aux ( arbel );
2497         free_dma ( arbel->db_rec, ARBEL_PAGE_SIZE );
2498         arbel->db_rec = NULL;
2499 }
2500
2501 /***************************************************************************
2502  *
2503  * Initialisation and teardown
2504  *
2505  ***************************************************************************
2506  */
2507
2508 /**
2509  * Reset device
2510  *
2511  * @v arbel             Arbel device
2512  */
2513 static void arbel_reset ( struct arbel *arbel ) {
2514         struct pci_device *pci = arbel->pci;
2515         struct pci_config_backup backup;
2516         static const uint8_t backup_exclude[] =
2517                 PCI_CONFIG_BACKUP_EXCLUDE ( 0x58, 0x5c );
2518         uint16_t vendor;
2519         unsigned int i;
2520
2521         /* Perform device reset and preserve PCI configuration */
2522         pci_backup ( pci, &backup, backup_exclude );
2523         writel ( ARBEL_RESET_MAGIC,
2524                  ( arbel->config + ARBEL_RESET_OFFSET ) );
2525         for ( i = 0 ; i < ARBEL_RESET_WAIT_TIME_MS ; i++ ) {
2526                 mdelay ( 1 );
2527                 pci_read_config_word ( pci, PCI_VENDOR_ID, &vendor );
2528                 if ( vendor != 0xffff )
2529                         break;
2530         }
2531         pci_restore ( pci, &backup, backup_exclude );
2532 }
2533
2534 /**
2535  * Set up memory protection table
2536  *
2537  * @v arbel             Arbel device
2538  * @ret rc              Return status code
2539  */
2540 static int arbel_setup_mpt ( struct arbel *arbel ) {
2541         struct arbelprm_mpt mpt;
2542         uint32_t key;
2543         int rc;
2544
2545         /* Derive key */
2546         key = ( arbel->limits.reserved_mrws | ARBEL_MKEY_PREFIX );
2547         arbel->lkey = ( ( key << 8 ) | ( key >> 24 ) );
2548
2549         /* Initialise memory protection table */
2550         memset ( &mpt, 0, sizeof ( mpt ) );
2551         MLX_FILL_7 ( &mpt, 0,
2552                      a, 1,
2553                      rw, 1,
2554                      rr, 1,
2555                      lw, 1,
2556                      lr, 1,
2557                      pa, 1,
2558                      r_w, 1 );
2559         MLX_FILL_1 ( &mpt, 2, mem_key, key );
2560         MLX_FILL_2 ( &mpt, 3,
2561                      pd, ARBEL_GLOBAL_PD,
2562                      rae, 1 );
2563         MLX_FILL_1 ( &mpt, 6, reg_wnd_len_h, 0xffffffffUL );
2564         MLX_FILL_1 ( &mpt, 7, reg_wnd_len_l, 0xffffffffUL );
2565         if ( ( rc = arbel_cmd_sw2hw_mpt ( arbel, arbel->limits.reserved_mrws,
2566                                           &mpt ) ) != 0 ) {
2567                 DBGC ( arbel, "Arbel %p could not set up MPT: %s\n",
2568                        arbel, strerror ( rc ) );
2569                 return rc;
2570         }
2571
2572         return 0;
2573 }
2574
2575 /**
2576  * Configure special queue pairs
2577  *
2578  * @v arbel             Arbel device
2579  * @ret rc              Return status code
2580  */
2581 static int arbel_configure_special_qps ( struct arbel *arbel ) {
2582         unsigned int smi_qpn_base;
2583         unsigned int gsi_qpn_base;
2584         int rc;
2585
2586         /* Special QP block must be aligned on an even number */
2587         arbel->special_qpn_base = ( ( arbel->limits.reserved_qps + 1 ) & ~1 );
2588         arbel->qpn_base = ( arbel->special_qpn_base +
2589                             ARBEL_NUM_SPECIAL_QPS );
2590         DBGC ( arbel, "Arbel %p special QPs at [%lx,%lx]\n", arbel,
2591                arbel->special_qpn_base, ( arbel->qpn_base - 1 ) );
2592         smi_qpn_base = arbel->special_qpn_base;
2593         gsi_qpn_base = ( smi_qpn_base + 2 );
2594
2595         /* Issue commands to configure special QPs */
2596         if ( ( rc = arbel_cmd_conf_special_qp ( arbel, 0,
2597                                                 smi_qpn_base ) ) != 0 ) {
2598                 DBGC ( arbel, "Arbel %p could not configure SMI QPs: %s\n",
2599                        arbel, strerror ( rc ) );
2600                 return rc;
2601         }
2602         if ( ( rc = arbel_cmd_conf_special_qp ( arbel, 1,
2603                                                 gsi_qpn_base ) ) != 0 ) {
2604                 DBGC ( arbel, "Arbel %p could not configure GSI QPs: %s\n",
2605                        arbel, strerror ( rc ) );
2606                 return rc;
2607         }
2608
2609         return 0;
2610 }
2611
2612 /**
2613  * Start Arbel device
2614  *
2615  * @v arbel             Arbel device
2616  * @v running           Firmware is already running
2617  * @ret rc              Return status code
2618  */
2619 static int arbel_start ( struct arbel *arbel, int running ) {
2620         struct arbelprm_init_hca init_hca;
2621         unsigned int i;
2622         int rc;
2623
2624         /* Start firmware if not already running */
2625         if ( ! running ) {
2626                 if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
2627                         goto err_start_firmware;
2628         }
2629
2630         /* Allocate ICM */
2631         memset ( &init_hca, 0, sizeof ( init_hca ) );
2632         if ( ( rc = arbel_alloc_icm ( arbel, &init_hca ) ) != 0 )
2633                 goto err_alloc_icm;
2634
2635         /* Initialise HCA */
2636         if ( ( rc = arbel_cmd_init_hca ( arbel, &init_hca ) ) != 0 ) {
2637                 DBGC ( arbel, "Arbel %p could not initialise HCA: %s\n",
2638                        arbel, strerror ( rc ) );
2639                 goto err_init_hca;
2640         }
2641
2642         /* Set up memory protection */
2643         if ( ( rc = arbel_setup_mpt ( arbel ) ) != 0 )
2644                 goto err_setup_mpt;
2645         for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ )
2646                 arbel->ibdev[i]->rdma_key = arbel->lkey;
2647
2648         /* Set up event queue */
2649         if ( ( rc = arbel_create_eq ( arbel ) ) != 0 )
2650                 goto err_create_eq;
2651
2652         /* Configure special QPs */
2653         if ( ( rc = arbel_configure_special_qps ( arbel ) ) != 0 )
2654                 goto err_conf_special_qps;
2655
2656         return 0;
2657
2658  err_conf_special_qps:
2659         arbel_destroy_eq ( arbel );
2660  err_create_eq:
2661  err_setup_mpt:
2662         arbel_cmd_close_hca ( arbel );
2663  err_init_hca:
2664         arbel_free_icm ( arbel );
2665  err_alloc_icm:
2666         arbel_stop_firmware ( arbel );
2667  err_start_firmware:
2668         return rc;
2669 }
2670
2671 /**
2672  * Stop Arbel device
2673  *
2674  * @v arbel             Arbel device
2675  */
2676 static void arbel_stop ( struct arbel *arbel ) {
2677         arbel_destroy_eq ( arbel );
2678         arbel_cmd_close_hca ( arbel );
2679         arbel_free_icm ( arbel );
2680         arbel_stop_firmware ( arbel );
2681         arbel_reset ( arbel );
2682 }
2683
2684 /**
2685  * Open Arbel device
2686  *
2687  * @v arbel             Arbel device
2688  * @ret rc              Return status code
2689  */
2690 static int arbel_open ( struct arbel *arbel ) {
2691         int rc;
2692
2693         /* Start device if applicable */
2694         if ( arbel->open_count == 0 ) {
2695                 if ( ( rc = arbel_start ( arbel, 0 ) ) != 0 )
2696                         return rc;
2697         }
2698
2699         /* Increment open counter */
2700         arbel->open_count++;
2701
2702         return 0;
2703 }
2704
2705 /**
2706  * Close Arbel device
2707  *
2708  * @v arbel             Arbel device
2709  */
2710 static void arbel_close ( struct arbel *arbel ) {
2711
2712         /* Decrement open counter */
2713         assert ( arbel->open_count != 0 );
2714         arbel->open_count--;
2715
2716         /* Stop device if applicable */
2717         if ( arbel->open_count == 0 )
2718                 arbel_stop ( arbel );
2719 }
2720
2721 /***************************************************************************
2722  *
2723  * Infiniband link-layer operations
2724  *
2725  ***************************************************************************
2726  */
2727
2728 /**
2729  * Initialise Infiniband link
2730  *
2731  * @v ibdev             Infiniband device
2732  * @ret rc              Return status code
2733  */
2734 static int arbel_ib_open ( struct ib_device *ibdev ) {
2735         struct arbel *arbel = ib_get_drvdata ( ibdev );
2736         struct arbelprm_init_ib init_ib;
2737         int rc;
2738
2739         /* Open hardware */
2740         if ( ( rc = arbel_open ( arbel ) ) != 0 )
2741                 goto err_open;
2742
2743         /* Initialise IB */
2744         memset ( &init_ib, 0, sizeof ( init_ib ) );
2745         MLX_FILL_3 ( &init_ib, 0,
2746                      mtu_cap, ARBEL_MTU_2048,
2747                      port_width_cap, 3,
2748                      vl_cap, 1 );
2749         MLX_FILL_1 ( &init_ib, 1, max_gid, 1 );
2750         MLX_FILL_1 ( &init_ib, 2, max_pkey, 64 );
2751         if ( ( rc = arbel_cmd_init_ib ( arbel, ibdev->port,
2752                                         &init_ib ) ) != 0 ) {
2753                 DBGC ( arbel, "Arbel %p port %d could not intialise IB: %s\n",
2754                        arbel, ibdev->port, strerror ( rc ) );
2755                 goto err_init_ib;
2756         }
2757
2758         /* Update MAD parameters */
2759         ib_smc_update ( ibdev, arbel_mad );
2760
2761         return 0;
2762
2763  err_init_ib:
2764         arbel_close ( arbel );
2765  err_open:
2766         return rc;
2767 }
2768
2769 /**
2770  * Close Infiniband link
2771  *
2772  * @v ibdev             Infiniband device
2773  */
2774 static void arbel_ib_close ( struct ib_device *ibdev ) {
2775         struct arbel *arbel = ib_get_drvdata ( ibdev );
2776         int rc;
2777
2778         /* Close IB */
2779         if ( ( rc = arbel_cmd_close_ib ( arbel, ibdev->port ) ) != 0 ) {
2780                 DBGC ( arbel, "Arbel %p port %d could not close IB: %s\n",
2781                        arbel, ibdev->port, strerror ( rc ) );
2782                 /* Nothing we can do about this */
2783         }
2784
2785         /* Close hardware */
2786         arbel_close ( arbel );
2787 }
2788
2789 /**
2790  * Inform embedded subnet management agent of a received MAD
2791  *
2792  * @v ibdev             Infiniband device
2793  * @v mad               MAD
2794  * @ret rc              Return status code
2795  */
2796 static int arbel_inform_sma ( struct ib_device *ibdev, union ib_mad *mad ) {
2797         int rc;
2798
2799         /* Send the MAD to the embedded SMA */
2800         if ( ( rc = arbel_mad ( ibdev, mad ) ) != 0 )
2801                 return rc;
2802
2803         /* Update parameters held in software */
2804         ib_smc_update ( ibdev, arbel_mad );
2805
2806         return 0;
2807 }
2808
2809 /***************************************************************************
2810  *
2811  * Multicast group operations
2812  *
2813  ***************************************************************************
2814  */
2815
2816 /**
2817  * Attach to multicast group
2818  *
2819  * @v ibdev             Infiniband device
2820  * @v qp                Queue pair
2821  * @v gid               Multicast GID
2822  * @ret rc              Return status code
2823  */
2824 static int arbel_mcast_attach ( struct ib_device *ibdev,
2825                                 struct ib_queue_pair *qp,
2826                                 union ib_gid *gid ) {
2827         struct arbel *arbel = ib_get_drvdata ( ibdev );
2828         struct arbelprm_mgm_hash hash;
2829         struct arbelprm_mgm_entry mgm;
2830         unsigned int index;
2831         int rc;
2832
2833         /* Generate hash table index */
2834         if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
2835                 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
2836                        arbel, strerror ( rc ) );
2837                 return rc;
2838         }
2839         index = MLX_GET ( &hash, hash );
2840
2841         /* Check for existing hash table entry */
2842         if ( ( rc = arbel_cmd_read_mgm ( arbel, index, &mgm ) ) != 0 ) {
2843                 DBGC ( arbel, "Arbel %p could not read MGM %#x: %s\n",
2844                        arbel, index, strerror ( rc ) );
2845                 return rc;
2846         }
2847         if ( MLX_GET ( &mgm, mgmqp_0.qi ) != 0 ) {
2848                 /* FIXME: this implementation allows only a single QP
2849                  * per multicast group, and doesn't handle hash
2850                  * collisions.  Sufficient for IPoIB but may need to
2851                  * be extended in future.
2852                  */
2853                 DBGC ( arbel, "Arbel %p MGID index %#x already in use\n",
2854                        arbel, index );
2855                 return -EBUSY;
2856         }
2857
2858         /* Update hash table entry */
2859         MLX_FILL_2 ( &mgm, 8,
2860                      mgmqp_0.qpn_i, qp->qpn,
2861                      mgmqp_0.qi, 1 );
2862         memcpy ( &mgm.u.dwords[4], gid, sizeof ( *gid ) );
2863         if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
2864                 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
2865                        arbel, index, strerror ( rc ) );
2866                 return rc;
2867         }
2868
2869         return 0;
2870 }
2871
2872 /**
2873  * Detach from multicast group
2874  *
2875  * @v ibdev             Infiniband device
2876  * @v qp                Queue pair
2877  * @v gid               Multicast GID
2878  */
2879 static void arbel_mcast_detach ( struct ib_device *ibdev,
2880                                  struct ib_queue_pair *qp __unused,
2881                                  union ib_gid *gid ) {
2882         struct arbel *arbel = ib_get_drvdata ( ibdev );
2883         struct arbelprm_mgm_hash hash;
2884         struct arbelprm_mgm_entry mgm;
2885         unsigned int index;
2886         int rc;
2887
2888         /* Generate hash table index */
2889         if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
2890                 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
2891                        arbel, strerror ( rc ) );
2892                 return;
2893         }
2894         index = MLX_GET ( &hash, hash );
2895
2896         /* Clear hash table entry */
2897         memset ( &mgm, 0, sizeof ( mgm ) );
2898         if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
2899                 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
2900                        arbel, index, strerror ( rc ) );
2901                 return;
2902         }
2903 }
2904
2905 /** Arbel Infiniband operations */
2906 static struct ib_device_operations arbel_ib_operations = {
2907         .create_cq      = arbel_create_cq,
2908         .destroy_cq     = arbel_destroy_cq,
2909         .create_qp      = arbel_create_qp,
2910         .modify_qp      = arbel_modify_qp,
2911         .destroy_qp     = arbel_destroy_qp,
2912         .post_send      = arbel_post_send,
2913         .post_recv      = arbel_post_recv,
2914         .poll_cq        = arbel_poll_cq,
2915         .poll_eq        = arbel_poll_eq,
2916         .open           = arbel_ib_open,
2917         .close          = arbel_ib_close,
2918         .mcast_attach   = arbel_mcast_attach,
2919         .mcast_detach   = arbel_mcast_detach,
2920         .set_port_info  = arbel_inform_sma,
2921         .set_pkey_table = arbel_inform_sma,
2922 };
2923
2924 /***************************************************************************
2925  *
2926  * PCI interface
2927  *
2928  ***************************************************************************
2929  */
2930
2931 /**
2932  * Allocate Arbel device
2933  *
2934  * @ret arbel           Arbel device
2935  */
2936 static struct arbel * arbel_alloc ( void ) {
2937         struct arbel *arbel;
2938
2939         /* Allocate Arbel device */
2940         arbel = zalloc ( sizeof ( *arbel ) );
2941         if ( ! arbel )
2942                 goto err_arbel;
2943
2944         /* Allocate space for mailboxes */
2945         arbel->mailbox_in = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
2946         if ( ! arbel->mailbox_in )
2947                 goto err_mailbox_in;
2948         arbel->mailbox_out = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
2949         if ( ! arbel->mailbox_out )
2950                 goto err_mailbox_out;
2951
2952         return arbel;
2953
2954         free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
2955  err_mailbox_out:
2956         free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
2957  err_mailbox_in:
2958         free ( arbel );
2959  err_arbel:
2960         return NULL;
2961 }
2962
2963 /**
2964  * Free Arbel device
2965  *
2966  * @v arbel             Arbel device
2967  */
2968 static void arbel_free ( struct arbel *arbel ) {
2969
2970         ufree ( arbel->icm );
2971         ufree ( arbel->firmware_area );
2972         free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
2973         free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
2974         free ( arbel );
2975 }
2976
2977 /**
2978  * Probe PCI device
2979  *
2980  * @v pci               PCI device
2981  * @v id                PCI ID
2982  * @ret rc              Return status code
2983  */
2984 static int arbel_probe ( struct pci_device *pci ) {
2985         struct arbel *arbel;
2986         struct ib_device *ibdev;
2987         int i;
2988         int rc;
2989
2990         /* Allocate Arbel device */
2991         arbel = arbel_alloc();
2992         if ( ! arbel ) {
2993                 rc = -ENOMEM;
2994                 goto err_alloc;
2995         }
2996         pci_set_drvdata ( pci, arbel );
2997         arbel->pci = pci;
2998
2999         /* Allocate Infiniband devices */
3000         for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
3001                 ibdev = alloc_ibdev ( 0 );
3002                 if ( ! ibdev ) {
3003                         rc = -ENOMEM;
3004                         goto err_alloc_ibdev;
3005                 }
3006                 arbel->ibdev[i] = ibdev;
3007                 ibdev->op = &arbel_ib_operations;
3008                 ibdev->dev = &pci->dev;
3009                 ibdev->port = ( ARBEL_PORT_BASE + i );
3010                 ib_set_drvdata ( ibdev, arbel );
3011         }
3012
3013         /* Fix up PCI device */
3014         adjust_pci_device ( pci );
3015
3016         /* Get PCI BARs */
3017         arbel->config = ioremap ( pci_bar_start ( pci, ARBEL_PCI_CONFIG_BAR ),
3018                                   ARBEL_PCI_CONFIG_BAR_SIZE );
3019         arbel->uar = ioremap ( ( pci_bar_start ( pci, ARBEL_PCI_UAR_BAR ) +
3020                                  ARBEL_PCI_UAR_IDX * ARBEL_PCI_UAR_SIZE ),
3021                                ARBEL_PCI_UAR_SIZE );
3022
3023         /* Reset device */
3024         arbel_reset ( arbel );
3025
3026         /* Start firmware */
3027         if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
3028                 goto err_start_firmware;
3029
3030         /* Get device limits */
3031         if ( ( rc = arbel_get_limits ( arbel ) ) != 0 )
3032                 goto err_get_limits;
3033
3034         /* Start device */
3035         if ( ( rc = arbel_start ( arbel, 1 ) ) != 0 )
3036                 goto err_start;
3037
3038         /* Initialise parameters using SMC */
3039         for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ )
3040                 ib_smc_init ( arbel->ibdev[i], arbel_mad );
3041
3042         /* Register Infiniband devices */
3043         for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
3044                 if ( ( rc = register_ibdev ( arbel->ibdev[i] ) ) != 0 ) {
3045                         DBGC ( arbel, "Arbel %p port %d could not register IB "
3046                                "device: %s\n", arbel,
3047                                arbel->ibdev[i]->port, strerror ( rc ) );
3048                         goto err_register_ibdev;
3049                 }
3050         }
3051
3052         /* Leave device quiescent until opened */
3053         if ( arbel->open_count == 0 )
3054                 arbel_stop ( arbel );
3055
3056         return 0;
3057
3058         i = ARBEL_NUM_PORTS;
3059  err_register_ibdev:
3060         for ( i-- ; i >= 0 ; i-- )
3061                 unregister_ibdev ( arbel->ibdev[i] );
3062         arbel_stop ( arbel );
3063  err_start:
3064  err_get_limits:
3065         arbel_stop_firmware ( arbel );
3066  err_start_firmware:
3067         i = ARBEL_NUM_PORTS;
3068  err_alloc_ibdev:
3069         for ( i-- ; i >= 0 ; i-- )
3070                 ibdev_put ( arbel->ibdev[i] );
3071         arbel_free ( arbel );
3072  err_alloc:
3073         return rc;
3074 }
3075
3076 /**
3077  * Remove PCI device
3078  *
3079  * @v pci               PCI device
3080  */
3081 static void arbel_remove ( struct pci_device *pci ) {
3082         struct arbel *arbel = pci_get_drvdata ( pci );
3083         int i;
3084
3085         for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
3086                 unregister_ibdev ( arbel->ibdev[i] );
3087         for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
3088                 ibdev_put ( arbel->ibdev[i] );
3089         arbel_free ( arbel );
3090 }
3091
3092 static struct pci_device_id arbel_nics[] = {
3093         PCI_ROM ( 0x15b3, 0x6282, "mt25218", "MT25218 HCA driver", 0 ),
3094         PCI_ROM ( 0x15b3, 0x6274, "mt25204", "MT25204 HCA driver", 0 ),
3095 };
3096
3097 struct pci_driver arbel_driver __pci_driver = {
3098         .ids = arbel_nics,
3099         .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
3100         .probe = arbel_probe,
3101         .remove = arbel_remove,
3102 };