1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Broadcom. All Rights Reserved. The term
* “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
*/
#include "efct_driver.h"
#include "efct_unsol.h"
static struct dentry *efct_debugfs_root;
static atomic_t efct_debugfs_count;
static struct scsi_host_template efct_template = {
.module = THIS_MODULE,
.name = EFCT_DRIVER_NAME,
.supported_mode = MODE_TARGET,
};
/* globals */
static struct fc_function_template efct_xport_functions;
static struct fc_function_template efct_vport_functions;
static struct scsi_transport_template *efct_xport_fc_tt;
static struct scsi_transport_template *efct_vport_fc_tt;
struct efct_xport *
efct_xport_alloc(struct efct *efct)
{
struct efct_xport *xport;
xport = kzalloc(sizeof(*xport), GFP_KERNEL);
if (!xport)
return xport;
xport->efct = efct;
return xport;
}
static int
efct_xport_init_debugfs(struct efct *efct)
{
/* Setup efct debugfs root directory */
if (!efct_debugfs_root) {
efct_debugfs_root = debugfs_create_dir("efct", NULL);
atomic_set(&efct_debugfs_count, 0);
if (!efct_debugfs_root) {
efc_log_err(efct, "failed to create debugfs entry\n");
goto debugfs_fail;
}
}
/* Create a directory for sessions in root */
if (!efct->sess_debugfs_dir) {
efct->sess_debugfs_dir = debugfs_create_dir("sessions", NULL);
if (!efct->sess_debugfs_dir) {
efc_log_err(efct,
"failed to create debugfs entry for sessions\n");
goto debugfs_fail;
}
atomic_inc(&efct_debugfs_count);
}
return 0;
debugfs_fail:
return -EIO;
}
static void efct_xport_delete_debugfs(struct efct *efct)
{
/* Remove session debugfs directory */
debugfs_remove(efct->sess_debugfs_dir);
efct->sess_debugfs_dir = NULL;
atomic_dec(&efct_debugfs_count);
if (atomic_read(&efct_debugfs_count) == 0) {
/* remove root debugfs directory */
debugfs_remove(efct_debugfs_root);
efct_debugfs_root = NULL;
}
}
int
efct_xport_attach(struct efct_xport *xport)
{
struct efct *efct = xport->efct;
int rc;
rc = efct_hw_setup(&efct->hw, efct, efct->pci);
if (rc) {
efc_log_err(efct, "%s: Can't setup hardware\n", efct->desc);
return rc;
}
efct_hw_parse_filter(&efct->hw, (void *)efct->filter_def);
xport->io_pool = efct_io_pool_create(efct, efct->hw.config.n_sgl);
if (!xport->io_pool) {
efc_log_err(efct, "Can't allocate IO pool\n");
return -ENOMEM;
}
return 0;
}
static void
efct_xport_link_stats_cb(int status, u32 num_counters,
struct efct_hw_link_stat_counts *counters, void *arg)
{
union efct_xport_stats_u *result = arg;
result->stats.link_stats.link_failure_error_count =
counters[EFCT_HW_LINK_STAT_LINK_FAILURE_COUNT].counter;
result->stats.link_stats.loss_of_sync_error_count =
counters[EFCT_HW_LINK_STAT_LOSS_OF_SYNC_COUNT].counter;
result->stats.link_stats.primitive_sequence_error_count =
counters[EFCT_HW_LINK_STAT_PRIMITIVE_SEQ_COUNT].counter;
result->stats.link_stats.invalid_transmission_word_error_count =
counters[EFCT_HW_LINK_STAT_INVALID_XMIT_WORD_COUNT].counter;
result->stats.link_stats.crc_error_count =
counters[EFCT_HW_LINK_STAT_CRC_COUNT].counter;
complete(&result->stats.done);
}
static void
efct_xport_host_stats_cb(int status, u32 num_counters,
struct efct_hw_host_stat_counts *counters, void *arg)
{
union efct_xport_stats_u *result = arg;
result->stats.host_stats.transmit_kbyte_count =
counters[EFCT_HW_HOST_STAT_TX_KBYTE_COUNT].counter;
result->stats.host_stats.receive_kbyte_count =
counters[EFCT_HW_HOST_STAT_RX_KBYTE_COUNT].counter;
result->stats.host_stats.transmit_frame_count =
counters[EFCT_HW_HOST_STAT_TX_FRAME_COUNT].counter;
result->stats.host_stats.receive_frame_count =
counters[EFCT_HW_HOST_STAT_RX_FRAME_COUNT].counter;
complete(&result->stats.done);
}
static void
efct_xport_async_link_stats_cb(int status, u32 num_counters,
struct efct_hw_link_stat_counts *counters,
void *arg)
{
union efct_xport_stats_u *result = arg;
result->stats.link_stats.link_failure_error_count =
counters[EFCT_HW_LINK_STAT_LINK_FAILURE_COUNT].counter;
result->stats.link_stats.loss_of_sync_error_count =
counters[EFCT_HW_LINK_STAT_LOSS_OF_SYNC_COUNT].counter;
result->stats.link_stats.primitive_sequence_error_count =
counters[EFCT_HW_LINK_STAT_PRIMITIVE_SEQ_COUNT].counter;
result->stats.link_stats.invalid_transmission_word_error_count =
counters[EFCT_HW_LINK_STAT_INVALID_XMIT_WORD_COUNT].counter;
result->stats.link_stats.crc_error_count =
counters[EFCT_HW_LINK_STAT_CRC_COUNT].counter;
}
static void
efct_xport_async_host_stats_cb(int status, u32 num_counters,
struct efct_hw_host_stat_counts *counters,
void *arg)
{
union efct_xport_stats_u *result = arg;
result->stats.host_stats.transmit_kbyte_count =
counters[EFCT_HW_HOST_STAT_TX_KBYTE_COUNT].counter;
result->stats.host_stats.receive_kbyte_count =
counters[EFCT_HW_HOST_STAT_RX_KBYTE_COUNT].counter;
result->stats.host_stats.transmit_frame_count =
counters[EFCT_HW_HOST_STAT_TX_FRAME_COUNT].counter;
result->stats.host_stats.receive_frame_count =
counters[EFCT_HW_HOST_STAT_RX_FRAME_COUNT].counter;
}
static void
efct_xport_config_stats_timer(struct efct *efct);
static void
efct_xport_stats_timer_cb(struct timer_list *t)
{
struct efct_xport *xport = from_timer(xport, t, stats_timer);
struct efct *efct = xport->efct;
efct_xport_config_stats_timer(efct);
}
static void
efct_xport_config_stats_timer(struct efct *efct)
{
u32 timeout = 3 * 1000;
struct efct_xport *xport = NULL;
if (!efct) {
pr_err("%s: failed to locate EFCT device\n", __func__);
return;
}
xport = efct->xport;
efct_hw_get_link_stats(&efct->hw, 0, 0, 0,
efct_xport_async_link_stats_cb,
&xport->fc_xport_stats);
efct_hw_get_host_stats(&efct->hw, 0, efct_xport_async_host_stats_cb,
&xport->fc_xport_stats);
timer_setup(&xport->stats_timer,
&efct_xport_stats_timer_cb, 0);
mod_timer(&xport->stats_timer,
jiffies + msecs_to_jiffies(timeout));
}
int
efct_xport_initialize(struct efct_xport *xport)
{
struct efct *efct = xport->efct;
int rc = 0;
/* Initialize io lists */
spin_lock_init(&xport->io_pending_lock);
INIT_LIST_HEAD(&xport->io_pending_list);
atomic_set(&xport->io_active_count, 0);
atomic_set(&xport->io_pending_count, 0);
atomic_set(&xport->io_total_free, 0);
atomic_set(&xport->io_total_pending, 0);
atomic_set(&xport->io_alloc_failed_count, 0);
atomic_set(&xport->io_pending_recursing, 0);
rc = efct_hw_init(&efct->hw);
if (rc) {
efc_log_err(efct, "efct_hw_init failure\n");
goto out;
}
rc = efct_scsi_tgt_new_device(efct);
if (rc) {
efc_log_err(efct, "failed to initialize target\n");
goto hw_init_out;
}
rc = efct_scsi_new_device(efct);
if (rc) {
efc_log_err(efct, "failed to initialize initiator\n");
goto tgt_dev_out;
}
/* Get FC link and host statistics perodically*/
efct_xport_config_stats_timer(efct);
efct_xport_init_debugfs(efct);
return rc;
tgt_dev_out:
efct_scsi_tgt_del_device(efct);
hw_init_out:
efct_hw_teardown(&efct->hw);
out:
return rc;
}
int
efct_xport_status(struct efct_xport *xport, enum efct_xport_status cmd,
union efct_xport_stats_u *result)
{
int rc = 0;
struct efct *efct = NULL;
union efct_xport_stats_u value;
efct = xport->efct;
switch (cmd) {
case EFCT_XPORT_CONFIG_PORT_STATUS:
if (xport->configured_link_state == 0) {
/*
* Initial state is offline. configured_link_state is
* set to online explicitly when port is brought online
*/
xport->configured_link_state = EFCT_XPORT_PORT_OFFLINE;
}
result->value = xport->configured_link_state;
break;
case EFCT_XPORT_PORT_STATUS:
/* Determine port status based on link speed. */
value.value = efct_hw_get_link_speed(&efct->hw);
if (value.value == 0)
result->value = EFCT_XPORT_PORT_OFFLINE;
else
result->value = EFCT_XPORT_PORT_ONLINE;
break;
case EFCT_XPORT_LINK_SPEED:
result->value = efct_hw_get_link_speed(&efct->hw);
break;
case EFCT_XPORT_LINK_STATISTICS:
memcpy((void *)result, &efct->xport->fc_xport_stats,
sizeof(union efct_xport_stats_u));
break;
case EFCT_XPORT_LINK_STAT_RESET: {
/* Create a completion to synchronize the stat reset process */
init_completion(&result->stats.done);
/* First reset the link stats */
rc = efct_hw_get_link_stats(&efct->hw, 0, 1, 1,
efct_xport_link_stats_cb, result);
if (rc)
break;
/* Wait for completion to be signaled when the cmd completes */
if (wait_for_completion_interruptible(&result->stats.done)) {
/* Undefined failure */
efc_log_debug(efct, "sem wait failed\n");
rc = -EIO;
break;
}
/* Next reset the host stats */
rc = efct_hw_get_host_stats(&efct->hw, 1,
efct_xport_host_stats_cb, result);
if (rc)
break;
/* Wait for completion to be signaled when the cmd completes */
if (wait_for_completion_interruptible(&result->stats.done)) {
/* Undefined failure */
efc_log_debug(efct, "sem wait failed\n");
rc = -EIO;
break;
}
break;
}
default:
rc = -EIO;
break;
}
return rc;
}
static int
efct_get_link_supported_speeds(struct efct *efct)
{
u32 supported_speeds = 0;
u32 link_module_type, i;
struct {
u32 lmt_speed;
u32 speed;
} supported_speed_list[] = {
{SLI4_LINK_MODULE_TYPE_1GB, FC_PORTSPEED_1GBIT},
{SLI4_LINK_MODULE_TYPE_2GB, FC_PORTSPEED_2GBIT},
{SLI4_LINK_MODULE_TYPE_4GB, FC_PORTSPEED_4GBIT},
{SLI4_LINK_MODULE_TYPE_8GB, FC_PORTSPEED_8GBIT},
{SLI4_LINK_MODULE_TYPE_16GB, FC_PORTSPEED_16GBIT},
{SLI4_LINK_MODULE_TYPE_32GB, FC_PORTSPEED_32GBIT},
{SLI4_LINK_MODULE_TYPE_64GB, FC_PORTSPEED_64GBIT},
{SLI4_LINK_MODULE_TYPE_128GB, FC_PORTSPEED_128GBIT},
};
link_module_type = sli_get_lmt(&efct->hw.sli);
/* populate link supported speeds */
for (i = 0; i < ARRAY_SIZE(supported_speed_list); i++) {
if (link_module_type & supported_speed_list[i].lmt_speed)
supported_speeds |= supported_speed_list[i].speed;
}
return supported_speeds;
}
int
efct_scsi_new_device(struct efct *efct)
{
struct Scsi_Host *shost = NULL;
int error = 0;
struct efct_vport *vport = NULL;
shost = scsi_host_alloc(&efct_template, sizeof(*vport));
if (!shost) {
efc_log_err(efct, "failed to allocate Scsi_Host struct\n");
return -ENOMEM;
}
/* save shost to initiator-client context */
efct->shost = shost;
/* save efct information to shost LLD-specific space */
vport = (struct efct_vport *)shost->hostdata;
vport->efct = efct;
/*
* Set initial can_queue value to the max SCSI IOs. This is the maximum
* global queue depth (as opposed to the per-LUN queue depth --
* .cmd_per_lun This may need to be adjusted for I+T mode.
*/
shost->can_queue = efct->hw.config.n_io;
shost->max_cmd_len = 16; /* 16-byte CDBs */
shost->max_id = 0xffff;
shost->max_lun = 0xffffffff;
/*
* can only accept (from mid-layer) as many SGEs as we've
* pre-registered
*/
shost->sg_tablesize = sli_get_max_sgl(&efct->hw.sli);
/* attach FC Transport template to shost */
shost->transportt = efct_xport_fc_tt;
efc_log_debug(efct, "transport template=%p\n", efct_xport_fc_tt);
/* get pci_dev structure and add host to SCSI ML */
error = scsi_add_host_with_dma(shost, &efct->pci->dev,
&efct->pci->dev);
if (error) {
efc_log_debug(efct, "failed scsi_add_host_with_dma\n");
return -EIO;
}
/* Set symbolic name for host port */
snprintf(fc_host_symbolic_name(shost),
sizeof(fc_host_symbolic_name(shost)),
"Emulex %s FV%s DV%s", efct->model,
efct->hw.sli.fw_name[0], EFCT_DRIVER_VERSION);
/* Set host port supported classes */
fc_host_supported_classes(shost) = FC_COS_CLASS3;
fc_host_supported_speeds(shost) = efct_get_link_supported_speeds(efct);
fc_host_node_name(shost) = efct_get_wwnn(&efct->hw);
fc_host_port_name(shost) = efct_get_wwpn(&efct->hw);
fc_host_max_npiv_vports(shost) = 128;
return 0;
}
struct scsi_transport_template *
efct_attach_fc_transport(void)
{
struct scsi_transport_template *efct_fc_template = NULL;
efct_fc_template = fc_attach_transport(&efct_xport_functions);
if (!efct_fc_template)
pr_err("failed to attach EFCT with fc transport\n");
return efct_fc_template;
}
struct scsi_transport_template *
efct_attach_vport_fc_transport(void)
{
struct scsi_transport_template *efct_fc_template = NULL;
efct_fc_template = fc_attach_transport(&efct_vport_functions);
if (!efct_fc_template)
pr_err("failed to attach EFCT with fc transport\n");
return efct_fc_template;
}
int
efct_scsi_reg_fc_transport(void)
{
/* attach to appropriate scsi_tranport_* module */
efct_xport_fc_tt = efct_attach_fc_transport();
if (!efct_xport_fc_tt) {
pr_err("%s: failed to attach to scsi_transport_*", __func__);
return -EIO;
}
efct_vport_fc_tt = efct_attach_vport_fc_transport();
if (!efct_vport_fc_tt) {
pr_err("%s: failed to attach to scsi_transport_*", __func__);
efct_release_fc_transport(efct_xport_fc_tt);
efct_xport_fc_tt = NULL;
return -EIO;
}
return 0;
}
void
efct_scsi_release_fc_transport(void)
{
/* detach from scsi_transport_* */
efct_release_fc_transport(efct_xport_fc_tt);
efct_xport_fc_tt = NULL;
if (efct_vport_fc_tt)
efct_release_fc_transport(efct_vport_fc_tt);
efct_vport_fc_tt = NULL;
}
void
efct_xport_detach(struct efct_xport *xport)
{
struct efct *efct = xport->efct;
/* free resources associated with target-server and initiator-client */
efct_scsi_tgt_del_device(efct);
efct_scsi_del_device(efct);
/*Shutdown FC Statistics timer*/
if (timer_pending(&xport->stats_timer))
del_timer(&xport->stats_timer);
efct_hw_teardown(&efct->hw);
efct_xport_delete_debugfs(efct);
}
static void
efct_xport_domain_free_cb(struct efc *efc, void *arg)
{
struct completion *done = arg;
complete(done);
}
int
efct_xport_control(struct efct_xport *xport, enum efct_xport_ctrl cmd, ...)
{
u32 rc = 0;
struct efct *efct = NULL;
va_list argp;
efct = xport->efct;
switch (cmd) {
case EFCT_XPORT_PORT_ONLINE: {
/* Bring the port on-line */
rc = efct_hw_port_control(&efct->hw, EFCT_HW_PORT_INIT, 0,
NULL, NULL);
if (rc)
efc_log_err(efct,
"%s: Can't init port\n", efct->desc);
else
xport->configured_link_state = cmd;
break;
}
case EFCT_XPORT_PORT_OFFLINE: {
if (efct_hw_port_control(&efct->hw, EFCT_HW_PORT_SHUTDOWN, 0,
NULL, NULL))
efc_log_err(efct, "port shutdown failed\n");
else
xport->configured_link_state = cmd;
break;
}
case EFCT_XPORT_SHUTDOWN: {
struct completion done;
unsigned long timeout;
/* if a PHYSDEV reset was performed (e.g. hw dump), will affect
* all PCI functions; orderly shutdown won't work,
* just force free
*/
if (sli_reset_required(&efct->hw.sli)) {
struct efc_domain *domain = efct->efcport->domain;
if (domain)
efc_domain_cb(efct->efcport, EFC_HW_DOMAIN_LOST,
domain);
} else {
efct_hw_port_control(&efct->hw, EFCT_HW_PORT_SHUTDOWN,
0, NULL, NULL);
}
init_completion(&done);
efc_register_domain_free_cb(efct->efcport,
efct_xport_domain_free_cb, &done);
efc_log_debug(efct, "Waiting %d seconds for domain shutdown\n",
(EFC_SHUTDOWN_TIMEOUT_USEC / 1000000));
timeout = usecs_to_jiffies(EFC_SHUTDOWN_TIMEOUT_USEC);
if (!wait_for_completion_timeout(&done, timeout)) {
efc_log_err(efct, "Domain shutdown timed out!!\n");
WARN_ON(1);
}
efc_register_domain_free_cb(efct->efcport, NULL, NULL);
/* Free up any saved virtual ports */
efc_vport_del_all(efct->efcport);
break;
}
/*
* Set wwnn for the port. This will be used instead of the default
* provided by FW.
*/
case EFCT_XPORT_WWNN_SET: {
u64 wwnn;
/* Retrieve arguments */
va_start(argp, cmd);
wwnn = va_arg(argp, uint64_t);
va_end(argp);
efc_log_debug(efct, " WWNN %016llx\n", wwnn);
xport->req_wwnn = wwnn;
break;
}
/*
* Set wwpn for the port. This will be used instead of the default
* provided by FW.
*/
case EFCT_XPORT_WWPN_SET: {
u64 wwpn;
/* Retrieve arguments */
va_start(argp, cmd);
wwpn = va_arg(argp, uint64_t);
va_end(argp);
efc_log_debug(efct, " WWPN %016llx\n", wwpn);
xport->req_wwpn = wwpn;
break;
}
default:
break;
}
return rc;
}
void
efct_xport_free(struct efct_xport *xport)
{
if (xport) {
efct_io_pool_free(xport->io_pool);
kfree(xport);
}
}
void
efct_release_fc_transport(struct scsi_transport_template *transport_template)
{
if (transport_template)
pr_err("releasing transport layer\n");
/* Releasing FC transport */
fc_release_transport(transport_template);
}
static void
efct_xport_remove_host(struct Scsi_Host *shost)
{
fc_remove_host(shost);
}
void
efct_scsi_del_device(struct efct *efct)
{
if (!efct->shost)
return;
efc_log_debug(efct, "Unregistering with Transport Layer\n");
efct_xport_remove_host(efct->shost);
efc_log_debug(efct, "Unregistering with SCSI Midlayer\n");
scsi_remove_host(efct->shost);
scsi_host_put(efct->shost);
efct->shost = NULL;
}
|