forked from ecdufcdrvr/bcmufctdrvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocs_node.c
2384 lines (2056 loc) · 67.3 KB
/
ocs_node.c
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD LICENSE
*
* Copyright (c) 2011-2018 Broadcom. All Rights Reserved.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* OCS driver remote node handler. This file contains code that is shared
* between fabric (ocs_fabric.c) and device (ocs_device.c) nodes.
*/
/*!
* @defgroup node_common Node common support
* @defgroup node_alloc Node allocation
*/
#include "ocs.h"
#include "spv.h"
#include "ocs_els.h"
#include "ocs_device.h"
#define SCSI_IOFMT "[%04x][i:%0*x t:%0*x h:%04x]"
#define SCSI_ITT_SIZE(ocs) ((ocs->ocs_xport == OCS_XPORT_FC) ? 4 : 8)
#define SCSI_IOFMT_ARGS(io) io->instance_index, SCSI_ITT_SIZE(io->ocs), io->init_task_tag, SCSI_ITT_SIZE(io->ocs), io->tgt_task_tag, io->hw_tag
#define scsi_io_printf(io, fmt, ...) ocs_log_debug(io->ocs, "[%s]" SCSI_IOFMT " %s: " fmt, \
io->node->display_name, SCSI_IOFMT_ARGS(io), __func__, ##__VA_ARGS__)
void ocs_mgmt_node_list(ocs_textbuf_t *textbuf, void *node);
void ocs_mgmt_node_get_all(ocs_textbuf_t *textbuf, void *node);
int ocs_mgmt_node_get(ocs_textbuf_t *textbuf, char *parent, char *name, void *node);
int ocs_mgmt_node_set(char *parent, char *name, char *value, void *node);
int ocs_mgmt_node_exec(char *parent, char *action, void *arg_in, uint32_t arg_in_length,
void *arg_out, uint32_t arg_out_length, void *node);
static ocs_mgmt_functions_t node_mgmt_functions = {
.get_list_handler = ocs_mgmt_node_list,
.get_handler = ocs_mgmt_node_get,
.get_all_handler = ocs_mgmt_node_get_all,
.set_handler = ocs_mgmt_node_set,
.exec_handler = ocs_mgmt_node_exec,
};
/**
* @ingroup node_common
* @brief Device node state machine wait for all ELS's to
* complete
*
* Abort all ELS's for given node.
*
* @param node node for which ELS's will be aborted
*/
void
ocs_node_abort_all_els(ocs_node_t *node)
{
ocs_io_t *els;
ocs_io_t *els_next;
ocs_node_cb_t cbdata = {0};
ocs_node_hold_frames(node);
ocs_lock(&node->active_ios_lock);
ocs_list_foreach_safe(&node->els_io_active_list, els, els_next) {
ocs_log_debug(node->ocs, "[%s] initiate ELS abort %s\n", node->display_name, els->display_name);
ocs_unlock(&node->active_ios_lock);
cbdata.els = els;
ocs_els_post_event(els, OCS_EVT_ABORT_ELS, &cbdata);
ocs_lock(&node->active_ios_lock);
}
ocs_unlock(&node->active_ios_lock);
}
/**
* @ingroup node_common
* @brief Handle remote node events from HAL
*
* Handle remote node events from HAL. Essentially the HAL event is translated into
* a node state machine event that is posted to the affected node.
*
* @param arg pointer to ocs
* @param event HAL event to proceoss
* @param data application specific data (pointer to the affected node)
*
* @return returns 0 for success, a negative error code value for failure.
*/
int32_t
ocs_remote_node_cb(void *arg, ocs_hal_remote_node_event_e event, void *data)
{
ocs_t *ocs = arg;
ocs_sm_event_t sm_event = OCS_EVT_LAST;
ocs_remote_node_t *rnode = data;
ocs_node_t *node = rnode->node;
switch (event) {
case OCS_HAL_NODE_ATTACH_OK:
sm_event = OCS_EVT_NODE_ATTACH_OK;
break;
case OCS_HAL_NODE_ATTACH_FAIL:
sm_event = OCS_EVT_NODE_ATTACH_FAIL;
break;
case OCS_HAL_NODE_FREE_OK:
sm_event = OCS_EVT_NODE_FREE_OK;
break;
case OCS_HAL_NODE_FREE_FAIL:
sm_event = OCS_EVT_NODE_FREE_FAIL;
break;
default:
ocs_log_test(ocs, "%s: unhandled event %#x\n", __func__, event);
return -1;
}
ocs_node_post_event(node, sm_event, NULL);
/* If we're using HLM, forward the NODE_ATTACH_OK/FAIL event to all nodes in the node group */
if ((node->node_group != NULL) &&
((sm_event == OCS_EVT_NODE_ATTACH_OK) || (sm_event == OCS_EVT_NODE_ATTACH_FAIL))) {
ocs_node_t *n = NULL;
uint8_t attach_ok = sm_event == OCS_EVT_NODE_ATTACH_OK;
ocs_sport_lock(node->sport);
{
ocs_list_foreach(&node->sport->node_list, n) {
if (node == n) {
continue;
}
ocs_node_lock(n);
if ((!n->rnode.attached) && (node->node_group == n->node_group)) {
n->rnode.attached = attach_ok;
node_printf(n, "rpi[%d] deferred HLM node attach %s posted\n",
n->rnode.index, attach_ok ? "ok" : "fail");
ocs_node_post_event(n, sm_event, NULL);
}
ocs_node_unlock(n);
}
}
ocs_sport_unlock(node->sport);
}
return 0;
}
/**
* @ingroup node_alloc
* @brief Find an FC node structure given the FC port ID
*
* @param sport the SPORT to search
* @param port_id FC port ID
*
* @return pointer to the object or NULL if not found
*/
ocs_node_t *
ocs_node_find(ocs_sport_t *sport, uint32_t port_id)
{
ocs_node_t *node;
ocs_assert(sport->lookup, NULL);
ocs_sport_lock(sport);
node = spv_get(sport->lookup, port_id);
ocs_sport_unlock(sport);
return node;
}
/**
* @ingroup node_alloc
* @brief Find an FC node structure given the WWPN
*
* @param sport the SPORT to search
* @param wwpn the WWPN to search for (host endian)
*
* @return pointer to the object or NULL if not found
*/
ocs_node_t *
ocs_node_find_wwpn(ocs_sport_t *sport, uint64_t wwpn)
{
ocs_node_t *node = NULL;;
ocs_assert(sport, NULL);
ocs_sport_lock(sport);
ocs_list_foreach(&sport->node_list, node) {
if (ocs_node_get_wwpn(node) == wwpn) {
ocs_sport_unlock(sport);
return node;
}
}
ocs_sport_unlock(sport);
return NULL;
}
/**
* @ingroup node_alloc
* @brief Find an FC node structure given the WWNN
*
* @param sport the SPORT to search
* @param wwnn the WWNN to search for (host endian)
*
* @return pointer to the object or NULL if not found
*/
ocs_node_t *
ocs_node_find_wwnn(ocs_sport_t *sport, uint64_t wwnn)
{
ocs_node_t *node = NULL;;
ocs_assert(sport, NULL);
ocs_sport_lock(sport);
ocs_list_foreach(&sport->node_list, node) {
if (ocs_node_get_wwnn(node) == wwnn) {
ocs_sport_unlock(sport);
return node;
}
}
ocs_sport_unlock(sport);
return NULL;
}
/**
* @ingroup node_alloc
* @brief allocate node object pool
*
* A pool of ocs_node_t objects is allocated.
*
* @param ocs pointer to driver instance context
* @param node_count count of nodes to allocate
*
* @return returns 0 for success, a negative error code value for failure.
*/
int32_t
ocs_node_create_pool(ocs_t *ocs, uint32_t node_count)
{
ocs_xport_t *xport = ocs->xport;
uint32_t i;
ocs_node_t *node;
uint32_t max_sge;
uint32_t num_sgl;
uint32_t max_xfer_size;
int32_t rc;
xport->nodes_count = node_count;
xport->nodes = ocs_malloc(ocs, node_count * sizeof(ocs_node_t), OCS_M_ZERO | OCS_M_NOWAIT);
if (xport->nodes == NULL) {
ocs_log_err(ocs, "%s: node allocation failed", __func__);
return -1;
}
if (0 == ocs_hal_get(&ocs->hal, OCS_HAL_MAX_SGE, &max_sge) &&
0 == ocs_hal_get(&ocs->hal, OCS_HAL_N_SGL, &num_sgl)) {
max_xfer_size = max_sge * num_sgl;
} else {
max_xfer_size = 65536;
}
ocs_list_init(&xport->nodes_free_list, ocs_node_t, link);
for (i = 0, node = xport->nodes; i < node_count; i ++, node ++) {
/* Assign any persistent field values */
node->instance_index = i;
node->max_wr_xfer_size = max_xfer_size;
node->rnode.indicator = UINT32_MAX;
rc = ocs_dma_alloc(ocs, &node->sparm_dma_buf, 256, 16);
if (rc) {
ocs_log_err(ocs, "%s: ocs_dma_alloc failed: %d\n", __func__, rc);
return -1;
}
ocs_list_add_tail(&xport->nodes_free_list, node);
}
return 0;
}
/**
* @ingroup node_alloc
* @brief free node object pool
*
* The pool of previously allocated node objects is freed
*
* @param ocs pointer to driver instance context
*
* @return none
*/
void
ocs_node_free_pool(ocs_t *ocs)
{
ocs_xport_t *xport = ocs->xport;
ocs_node_t *node;
uint32_t i;
ocs_device_lock(ocs);
if (xport->nodes != NULL) {
for (i = 0, node = xport->nodes;
i < xport->nodes_count;
i ++, node ++) {
/* free sparam_dma_buf */
ocs_dma_free(ocs, &node->sparm_dma_buf);
}
ocs_free(ocs, xport->nodes, xport->nodes_count * sizeof(ocs_node_t));
xport->nodes = NULL;
}
ocs_device_unlock(ocs);
}
/**
* @ingroup node_alloc
* @brief return pointer to node object given instance index
*
* A pointer to the node object given by an instance index is returned.
*
* @param ocs pointer to driver instance context
* @param index instance index
*
* @return returns pointer to node object, or NULL
*/
ocs_node_t *
ocs_node_get_instance(ocs_t *ocs, uint32_t index)
{
ocs_xport_t *xport = ocs->xport;
ocs_node_t *node = NULL;
if (index >= (xport->nodes_count)) {
ocs_log_test(ocs, "%s: invalid index: %d\n", __func__, index);
return NULL;
}
node = &xport->nodes[index];
return node->attached ? node : NULL;
}
/**
* @ingroup node_alloc
* @brief Allocate an fc node structure and add to node list
*
* @param sport pointer to the SPORT from which this node is allocated
* @param port_id FC port ID of new node
* @param init Port is an inititiator (sent a plogi)
* @param targ Port is potentially a target
*
* @return pointer to the object or NULL if none available
*/
ocs_node_t *
ocs_node_alloc(ocs_sport_t *sport, uint32_t port_id, uint8_t init, uint8_t targ)
{
int32_t rc;
ocs_node_t *node = NULL;
uint32_t instance_index;
uint32_t max_wr_xfer_size;
ocs_t *ocs = sport->ocs;
ocs_xport_t *xport = ocs->xport;
ocs_dma_t sparm_dma_buf;
ocs_assert(sport, NULL);
if (sport->shutting_down) {
ocs_log_debug(ocs, "%s: node allocation when shutting down %06x", __func__, port_id);
return NULL;
}
ocs_device_lock(ocs);
node = ocs_list_remove_head(&xport->nodes_free_list);
ocs_device_unlock(ocs);
if (node == NULL) {
ocs_log_err(ocs, "%s: node allocation failed %06x", __func__, port_id);
return NULL;
}
/* Save persistent values across memset zero */
instance_index = node->instance_index;
max_wr_xfer_size = node->max_wr_xfer_size;
sparm_dma_buf = node->sparm_dma_buf;
ocs_memset(node, 0, sizeof(*node));
node->instance_index = instance_index;
node->max_wr_xfer_size = max_wr_xfer_size;
node->sparm_dma_buf = sparm_dma_buf;
node->rnode.indicator = UINT32_MAX;
node->sport = sport;
ocs_sport_lock(sport);
ocs_list_add_tail(&sport->node_list, node);
node->ocs = ocs;
node->init = init;
node->targ = targ;
ocs_node_lock_init(node);
ocs_lock_init(ocs, &node->pend_frames_lock, "pend_frames_lock[%d]", node->instance_index);
ocs_list_init(&node->pend_frames, ocs_hal_sequence_t, link);
ocs_lock_init(ocs, &node->active_ios_lock, "active_ios[%d]", node->instance_index);
ocs_list_init(&node->active_ios, ocs_io_t, link);
ocs_list_init(&node->els_io_pend_list, ocs_io_t, link);
ocs_list_init(&node->els_io_active_list, ocs_io_t, link);
ocs_scsi_io_alloc_enable(node);
rc = ocs_hal_node_alloc(&ocs->hal, &node->rnode, port_id, sport);
if (rc) {
ocs_log_err(ocs, "%s: ocs_hal_node_alloc failed: %d\n", __func__, rc);
ocs_sport_unlock(sport);
return NULL;
}
/* zero the service parameters */
ocs_memset(node->sparm_dma_buf.virt, 0, node->sparm_dma_buf.size);
node->rnode.node = node;
node->sm.app = node;
node->evtdepth = 0;
ocs_node_update_display_name(node);
spv_set(sport->lookup, port_id, node);
ocs_sport_unlock(sport);
node->mgmt_functions = &node_mgmt_functions;
return node;
}
/**
* @ingroup node_alloc
* @brief free a node structure
*
* The node structure given by 'node' is free'd
*
* @param node the node to free
*
* @return returns 0 for success, a negative error code value for failure.
*/
int32_t
ocs_node_free(ocs_node_t *node)
{
ocs_sport_t *sport;
ocs_t *ocs;
ocs_xport_t *xport;
ocs_hal_rtn_e rc = 0;
ocs_node_t *ns = NULL;
int post_all_free = FALSE;
ocs_assert(node, -1);
ocs_assert(node->sport, -1);
ocs_assert(node->ocs, -1);
sport = node->sport;
ocs_assert(sport, -1);
ocs = node->ocs;
ocs_assert(ocs->xport, -1);
xport = ocs->xport;
node_printf(node, "Free'd\n");
if(node->refound) {
/*
* Save the name server node. We will send fake RSCN event at
* the end to handle ignored RSCN event during node deletion
*/
ns = ocs_node_find(node->sport, FC_ADDR_NAMESERVER);
}
/* Remove from node list */
ocs_sport_lock(sport);
ocs_list_remove(&sport->node_list, node);
/* Free HAL resources */
if (OCS_HAL_RTN_IS_ERROR((rc = ocs_hal_node_free_resources(&ocs->hal, &node->rnode)))) {
ocs_log_test(ocs, "%s: ocs_hal_node_free failed: %d\n", __func__, rc);
rc = -1;
}
/* if the gidft_delay_timer is still running, then delete it */
if (ocs_timer_pending(&node->gidft_delay_timer)) {
ocs_del_timer(&node->gidft_delay_timer);
}
/* remove entry from sparse vector list */
if (sport->lookup == NULL) {
ocs_log_test(node->ocs, "%s(%d): assertion failed: sport lookup is NULL\n",
__func__, __LINE__);
ocs_sport_unlock(sport);
return -1;
}
spv_set(sport->lookup, node->rnode.fc_id, NULL);
/*
* If the node_list is empty, then post a ALL_CHILD_NODES_FREE event to the sport,
* after the lock is released. The sport may be free'd as a result of the event.
*/
if (ocs_list_empty(&sport->node_list)) {
post_all_free = TRUE;
}
ocs_sport_unlock(sport);
if (post_all_free) {
ocs_sm_post_event(&sport->sm, OCS_EVT_ALL_CHILD_NODES_FREE, NULL);
}
node->sport = NULL;
node->sm.current_state = NULL;
ocs_node_lock_free(node);
ocs_lock_free(&node->pend_frames_lock);
ocs_lock_free(&node->active_ios_lock);
/* return to free list */
ocs_device_lock(ocs);
ocs_list_add_tail(&xport->nodes_free_list, node);
ocs_device_unlock(ocs);
if(ns != NULL) {
/* sending fake RSCN event to name server node */
ocs_node_post_event(ns, OCS_EVT_RSCN_RCVD, NULL);
}
return rc;
}
/**
* @brief free memory resources of a node object
*
* The node object's child objects are freed after which the
* node object is freed.
*
* @param node pointer to a node object
*
* @return none
*/
void
ocs_node_force_free(ocs_node_t *node)
{
ocs_io_t *io;
ocs_io_t *next;
ocs_io_t *els;
ocs_io_t *els_next;
/* shutdown sm processing */
ocs_sm_disable(&node->sm);
ocs_strncpy(node->prev_state_name, node->current_state_name, sizeof(node->prev_state_name));
ocs_strncpy(node->current_state_name, "disabled", sizeof(node->current_state_name));
/* Let the backend cleanup if needed */
ocs_scsi_notify_node_force_free(node);
ocs_lock(&node->active_ios_lock);
ocs_list_foreach_safe(&node->active_ios, io, next) {
ocs_list_remove(&io->node->active_ios, io);
ocs_io_free(node->ocs, io);
}
ocs_unlock(&node->active_ios_lock);
/* free all pending ELS IOs */
ocs_lock(&node->active_ios_lock);
ocs_list_foreach_safe(&node->els_io_pend_list, els, els_next) {
/* can't call ocs_els_io_free() because lock is held; cleanup manually */
ocs_list_remove(&node->els_io_pend_list, els);
ocs_dma_free(els->ocs, &els->els_rsp);
ocs_dma_free(els->ocs, &els->els_req);
ocs_io_free(node->ocs, els);
}
ocs_unlock(&node->active_ios_lock);
/* free all active ELS IOs */
ocs_lock(&node->active_ios_lock);
ocs_list_foreach_safe(&node->els_io_active_list, els, els_next) {
/* can't call ocs_els_io_free() because lock is held; cleanup manually */
ocs_list_remove(&node->els_io_active_list, els);
ocs_dma_free(els->ocs, &els->els_rsp);
ocs_dma_free(els->ocs, &els->els_req);
ocs_io_free(node->ocs, els);
}
ocs_unlock(&node->active_ios_lock);
/* manually purge pending frames (if any) */
ocs_node_purge_pending(node);
ocs_node_free(node);
}
/**
* @ingroup node_common
* @brief Perform HAL call to attach a remote node
*
* @param node pointer to node object
*
* @return 0 on success, non-zero otherwise
*/
int32_t
ocs_node_attach(ocs_node_t *node)
{
int32_t rc = 0;
ocs_sport_t *sport = node->sport;
ocs_domain_t *domain = sport->domain;
ocs_t *ocs = node->ocs;
if (!domain->attached) {
ocs_log_test(ocs, "Warning: ocs_node_attach with unattached domain\n");
return -1;
}
/* Update node->wwpn/wwnn */
ocs_node_build_eui_name(node->wwpn, sizeof(node->wwpn), ocs_node_get_wwpn(node));
ocs_node_build_eui_name(node->wwnn, sizeof(node->wwnn), ocs_node_get_wwnn(node));
if (ocs->enable_hlm) {
ocs_node_group_init(node);
}
ocs_dma_copy_in(&node->sparm_dma_buf, node->service_params+4, sizeof(node->service_params)-4);
/* take lock to protect node->rnode.attached */
ocs_node_lock(node);
rc = ocs_hal_node_attach(&ocs->hal, &node->rnode, &node->sparm_dma_buf);
if (OCS_HAL_RTN_IS_ERROR(rc)) {
ocs_log_test(ocs, "%s: ocs_hal_node_attach failed: %d\n", __func__, rc);
}
ocs_node_unlock(node);
return rc;
}
/**
* @ingroup node_common
* @brief Generate text for a node's fc_id
*
* The text for a nodes fc_id is generated, either as a well known name, or a 6 digit
* hex value.
*
* @param fc_id fc_id
* @param buffer text buffer
* @param buffer_length text buffer length in bytes
*
* @return none
*/
void
ocs_node_fcid_display(uint32_t fc_id, char *buffer, uint32_t buffer_length)
{
switch (fc_id) {
case FC_ADDR_FABRIC:
ocs_snprintf(buffer, buffer_length, "fabric");
break;
case FC_ADDR_CONTROLLER:
ocs_snprintf(buffer, buffer_length, "fabctl");
break;
case FC_ADDR_NAMESERVER:
ocs_snprintf(buffer, buffer_length, "nserve");
break;
default:
if (FC_ADDR_IS_DOMAIN_CTRL(fc_id)) {
ocs_snprintf(buffer, buffer_length, "dctl%02x",
FC_ADDR_GET_DOMAIN_CTRL(fc_id));
} else {
ocs_snprintf(buffer, buffer_length, "%06x", fc_id);
}
break;
}
}
/**
* @brief update the node's display name
*
* The node's display name is updated, sometimes needed because the sport part
* is updated after the node is allocated.
*
* @param node pointer to the node object
*
* @return none
*/
void
ocs_node_update_display_name(ocs_node_t *node)
{
uint32_t port_id = node->rnode.fc_id;
ocs_sport_t *sport = node->sport;
char portid_display[16];
ocs_assert(sport);
ocs_node_fcid_display(port_id, portid_display, sizeof(portid_display));
ocs_snprintf(node->display_name, sizeof(node->display_name), "%s.%s", sport->display_name, portid_display);
}
/**
* @brief cleans up an XRI for the pending link services accept by aborting the
* XRI if required.
*
* <h3 class="desc">Description</h3>
* This function is called when the LS accept is not sent.
*
* @param node Node for which should be cleaned up
*/
void
ocs_node_send_ls_io_cleanup(ocs_node_t *node)
{
ocs_t *ocs = node->ocs;
if (node->send_ls_acc != OCS_NODE_SEND_LS_ACC_NONE) {
ocs_assert(node->ls_acc_io);
ocs_log_debug(ocs, "[%s] cleaning up LS_ACC oxid=0x%x\n",
node->display_name, node->ls_acc_oxid);
node->ls_acc_io->hio = NULL;
ocs_els_io_free(node->ls_acc_io);
node->send_ls_acc = OCS_NODE_SEND_LS_ACC_NONE;
node->ls_acc_io = NULL;
}
}
/**
* @ingroup node_common
* @brief state: shutdown a node
*
* A node is shutdown,
*
* @param ctx remote node sm context
* @param evt event to process
* @param arg per event optional argument
*
* @return returns NULL
*
* @note
*/
void *
__ocs_node_shutdown(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *arg)
{
int32_t rc;
std_node_state_decl();
node_sm_trace();
switch(evt) {
case OCS_EVT_ENTER: {
ocs_node_hold_frames(node);
ocs_assert(ocs_node_active_ios_empty(node), NULL);
ocs_assert(ocs_els_io_list_empty(node, &node->els_io_active_list), NULL);
/* by default, we will be freeing node after we unwind */
node->req_free = 1;
switch (node->shutdown_reason) {
case OCS_NODE_SHUTDOWN_IMPLICIT_LOGO:
//sm: if shutdown reason is implicit logout / ocs_node_attach
/* Node shutdown b/c of PLOGI received when node already
* logged in. We have PLOGI service parameters, so submit
* node attach; we won't be freeing this node
*/
/* currently, only case for implicit logo is PLOGI recvd. Thus,
* node's ELS IO pending list won't be empty (PLOGI will be on it)
*/
ocs_assert(node->send_ls_acc == OCS_NODE_SEND_LS_ACC_PLOGI, NULL);
node_printf(node, "Shutdown reason: implicit logout, re-authenticate\n");
ocs_scsi_io_alloc_enable(node);
/* Re-attach node with the same HAL node resources */
node->req_free = 0;
rc = ocs_node_attach(node);
ocs_node_transition(node, __ocs_d_wait_node_attach, NULL);
if (rc == OCS_HAL_RTN_SUCCESS_SYNC) {
ocs_node_post_event(node, OCS_EVT_NODE_ATTACH_OK, NULL);
}
break;
case OCS_NODE_SHUTDOWN_EXPLICIT_LOGO: {
int8_t pend_frames_empty;
/* cleanup any pending LS_ACC ELSs */
ocs_node_send_ls_io_cleanup(node);
ocs_assert(ocs_els_io_list_empty(node, &node->els_io_pend_list), NULL);
ocs_lock(&node->pend_frames_lock);
pend_frames_empty = ocs_list_empty(&node->pend_frames);
ocs_unlock(&node->pend_frames_lock);
/* there are two scenarios where we want to keep this node alive:
* 1. there are pending frames that need to be processed or
* 2. we're an initiator and the remote node is a target and we
* need to re-authenticate
*/
node_printf(node, "Shutdown: explicit logo pend=%d sport.ini=%d node.tgt=%d\n",
!pend_frames_empty, node->sport->enable_ini, node->targ);
if((!pend_frames_empty) || (node->sport->enable_ini && node->targ)) {
uint8_t send_plogi = FALSE;
if (node->sport->enable_ini && node->targ) {
/* we're an initiator and node shutting down is a target; we'll
* need to re-authenticate in initial state
*/
send_plogi = TRUE;
}
/* transition to __ocs_d_init (will retain HAL node resources) */
ocs_scsi_io_alloc_enable(node);
node->req_free = 0;
/* either pending frames exist, or we're re-authenticating with PLOGI
* (or both); in either case, return to initial state
*/
ocs_node_init_device(node, send_plogi);
}
/* else: let node shutdown occur */
break;
}
case OCS_NODE_SHUTDOWN_DEFAULT:
default:
/* shutdown due to link down, node going away (xport event) or
* sport shutdown, purge pending and proceed to cleanup node
*/
/* cleanup any pending LS_ACC ELSs */
ocs_node_send_ls_io_cleanup(node);
ocs_assert(ocs_els_io_list_empty(node, &node->els_io_pend_list), NULL);
node_printf(node, "Shutdown reason: default, purge pending\n");
ocs_node_purge_pending(node);
break;
}
break;
}
case OCS_EVT_EXIT:
ocs_node_accept_frames(node);
break;
default:
__ocs_node_common(__func__, ctx, evt, arg);
return NULL;
}
return NULL;
}
/**
* @ingroup common_node
* @brief Checks to see if ELS's have been quiesced
*
* Check if ELS's have been quiesced. If so, transition to the
* next state in the shutdown process.
*
* @param node Node for which ELS's are checked
*
* @return Returns 1 if ELS's have been quiesced, 0 otherwise.
*/
static int
ocs_node_check_els_quiesced(ocs_node_t *node)
{
ocs_assert(node, -1);
/* check to see if ELS requests, completions are quiesced */
if ((node->els_req_cnt == 0) && (node->els_cmpl_cnt == 0) &&
ocs_els_io_list_empty(node, &node->els_io_active_list)) {
if (!node->attached) {
/* hal node detach already completed, proceed */
node_printf(node, "HAL node not attached\n");
ocs_node_transition(node, __ocs_node_wait_ios_shutdown, NULL);
} else {
/* hal node detach hasn't completed, transition and wait */
node_printf(node, "HAL node still attached\n");
ocs_node_transition(node, __ocs_node_wait_node_free, NULL);
}
return 1;
}
return 0;
}
/**
* @ingroup common_node
* @brief Initiate node IO cleanup.
*
* Note: this function must be called with a non-attached node
* or a node for which the node detach (ocs_hal_node_detach())
* has already been initiated.
*
* @param node Node for which shutdown is initiated
*
* @return Returns None.
*/
void
ocs_node_initiate_cleanup(ocs_node_t *node)
{
ocs_io_t *els;
ocs_io_t *els_next;
ocs_t *ocs;
ocs_assert(node);
ocs = node->ocs;
/* first cleanup ELS's that are pending (not yet active) */
ocs_lock(&node->active_ios_lock);
ocs_list_foreach_safe(&node->els_io_pend_list, els, els_next) {
/* skip the ELS IO for which a response will be sent after shutdown */
if ((node->send_ls_acc != OCS_NODE_SEND_LS_ACC_NONE) &&
(els == node->ls_acc_io)) {
continue;
}
/* can't call ocs_els_io_free() because lock is held; cleanup manually */
node_printf(node, "Freeing pending els %s\n", els->display_name);
ocs_list_remove(&node->els_io_pend_list, els);
ocs_dma_free(els->ocs, &els->els_rsp);
ocs_dma_free(els->ocs, &els->els_req);
ocs_io_free(node->ocs, els);
}
ocs_unlock(&node->active_ios_lock);
if (node->ls_acc_io && node->ls_acc_io->hio != NULL) {
/*
* if there's an IO that will result in an LS_ACC after
* shutdown and its HAL IO is non-NULL, it better be an
* implicit logout in vanilla sequence coalescing. In this
* case, force the LS_ACC to go out on another XRI (hio)
* since the previous will have been aborted by the UNREG_RPI
*/
ocs_assert(node->shutdown_reason == OCS_NODE_SHUTDOWN_IMPLICIT_LOGO);
ocs_assert(node->send_ls_acc == OCS_NODE_SEND_LS_ACC_PLOGI);
node_printf(node, "invalidating ls_acc_io due to implicit logo\n");
/* No need to abort because the unreg_rpi takes care of it, just free */
ocs_hal_io_free(&ocs->hal, node->ls_acc_io->hio);
/* NULL out hio to force the LS_ACC to grab a new XRI */
node->ls_acc_io->hio = NULL;
}
/*
* if ELS's have already been quiesced, will move to next state
* if ELS's have not been quiesced, abort them
*/
if (ocs_node_check_els_quiesced(node) == 0) {
/*
* Abort all ELS's since ELS's won't be aborted by HAL
* node free.
*/
ocs_node_abort_all_els(node);
ocs_node_transition(node, __ocs_node_wait_els_shutdown, NULL);
}
}
/**
* @ingroup node_common
* @brief Node state machine: Wait for all ELSs to complete.
*
* <h3 class="desc">Description</h3>
* State waits for all ELSs to complete after aborting all
* outstanding .
*
* @param ctx Remote node state machine context.
* @param evt Event to process.
* @param arg Per event optional argument.
*
* @return Returns NULL.