bfa_fcs_fcpim.c 21.6 KB
Newer Older
1
/*
A
Anil Gurumurthy 已提交
2 3
 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
 * Copyright (c) 2014- QLogic Corporation.
4
 * All rights reserved
A
Anil Gurumurthy 已提交
5
 * www.qlogic.com
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License (GPL) Version 2 as
 * published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

J
Jing Huang 已提交
19
/*
20 21 22
 *  fcpim.c - FCP initiator mode i-t nexus state machine
 */

M
Maggie Zhang 已提交
23
#include "bfad_drv.h"
K
Krishna Gudipati 已提交
24 25 26
#include "bfa_fcs.h"
#include "bfa_fcbuild.h"
#include "bfad_im.h"
27 28 29 30 31 32

BFA_TRC_FILE(FCS, FCPIM);

/*
 * forward declarations
 */
K
Krishna Gudipati 已提交
33 34 35
static void	bfa_fcs_itnim_timeout(void *arg);
static void	bfa_fcs_itnim_free(struct bfa_fcs_itnim_s *itnim);
static void	bfa_fcs_itnim_send_prli(void *itnim_cbarg,
36
					struct bfa_fcxp_s *fcxp_alloced);
K
Krishna Gudipati 已提交
37 38 39 40
static void	bfa_fcs_itnim_prli_response(void *fcsarg,
			 struct bfa_fcxp_s *fcxp, void *cbarg,
			    bfa_status_t req_status, u32 rsp_len,
			    u32 resid_len, struct fchs_s *rsp_fchs);
41 42
static void	bfa_fcs_itnim_aen_post(struct bfa_fcs_itnim_s *itnim,
			enum bfa_itnim_aen_event event);
43

K
Krishna Gudipati 已提交
44
static void	bfa_fcs_itnim_sm_offline(struct bfa_fcs_itnim_s *itnim,
45
					 enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
46
static void	bfa_fcs_itnim_sm_prli_send(struct bfa_fcs_itnim_s *itnim,
47
					   enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
48
static void	bfa_fcs_itnim_sm_prli(struct bfa_fcs_itnim_s *itnim,
49
				      enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
50
static void	bfa_fcs_itnim_sm_prli_retry(struct bfa_fcs_itnim_s *itnim,
51
					    enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
52
static void	bfa_fcs_itnim_sm_hcb_online(struct bfa_fcs_itnim_s *itnim,
53
					    enum bfa_fcs_itnim_event event);
54 55
static void	bfa_fcs_itnim_sm_hal_rport_online(struct bfa_fcs_itnim_s *itnim,
					enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
56
static void	bfa_fcs_itnim_sm_online(struct bfa_fcs_itnim_s *itnim,
57
					enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
58
static void	bfa_fcs_itnim_sm_hcb_offline(struct bfa_fcs_itnim_s *itnim,
59
					     enum bfa_fcs_itnim_event event);
K
Krishna Gudipati 已提交
60
static void	bfa_fcs_itnim_sm_initiator(struct bfa_fcs_itnim_s *itnim,
61 62 63 64 65 66 67 68 69 70 71 72 73
					   enum bfa_fcs_itnim_event event);

static struct bfa_sm_table_s itnim_sm_table[] = {
	{BFA_SM(bfa_fcs_itnim_sm_offline), BFA_ITNIM_OFFLINE},
	{BFA_SM(bfa_fcs_itnim_sm_prli_send), BFA_ITNIM_PRLI_SEND},
	{BFA_SM(bfa_fcs_itnim_sm_prli), BFA_ITNIM_PRLI_SENT},
	{BFA_SM(bfa_fcs_itnim_sm_prli_retry), BFA_ITNIM_PRLI_RETRY},
	{BFA_SM(bfa_fcs_itnim_sm_hcb_online), BFA_ITNIM_HCB_ONLINE},
	{BFA_SM(bfa_fcs_itnim_sm_online), BFA_ITNIM_ONLINE},
	{BFA_SM(bfa_fcs_itnim_sm_hcb_offline), BFA_ITNIM_HCB_OFFLINE},
	{BFA_SM(bfa_fcs_itnim_sm_initiator), BFA_ITNIM_INITIATIOR},
};

J
Jing Huang 已提交
74
/*
75 76 77 78 79
 *  fcs_itnim_sm FCS itnim state machine
 */

static void
bfa_fcs_itnim_sm_offline(struct bfa_fcs_itnim_s *itnim,
K
Krishna Gudipati 已提交
80
		 enum bfa_fcs_itnim_event event)
81 82 83 84 85
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
86
	case BFA_FCS_ITNIM_SM_FCS_ONLINE:
87
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_prli_send);
J
Jing Huang 已提交
88
		itnim->prli_retries = 0;
89 90 91 92
		bfa_fcs_itnim_send_prli(itnim, NULL);
		break;

	case BFA_FCS_ITNIM_SM_OFFLINE:
93
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
94 95 96 97 98 99 100 101 102 103 104
		break;

	case BFA_FCS_ITNIM_SM_INITIATOR:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_initiator);
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_fcs_itnim_free(itnim);
		break;

	default:
105
		bfa_sm_fault(itnim->fcs, event);
106 107 108 109 110 111
	}

}

static void
bfa_fcs_itnim_sm_prli_send(struct bfa_fcs_itnim_s *itnim,
K
Krishna Gudipati 已提交
112
		 enum bfa_fcs_itnim_event event)
113 114 115 116 117 118 119 120 121 122 123 124
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_FRMSENT:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_prli);
		break;

	case BFA_FCS_ITNIM_SM_INITIATOR:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_initiator);
		bfa_fcxp_walloc_cancel(itnim->fcs->bfa, &itnim->fcxp_wqe);
125
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_FCS_ONLINE);
126 127 128 129 130
		break;

	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcxp_walloc_cancel(itnim->fcs->bfa, &itnim->fcxp_wqe);
131
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
132 133 134 135 136 137 138 139 140
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcxp_walloc_cancel(itnim->fcs->bfa, &itnim->fcxp_wqe);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
141
		bfa_sm_fault(itnim->fcs, event);
142 143 144 145 146
	}
}

static void
bfa_fcs_itnim_sm_prli(struct bfa_fcs_itnim_s *itnim,
K
Krishna Gudipati 已提交
147
		 enum bfa_fcs_itnim_event event)
148 149 150 151 152 153
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_RSP_OK:
154
		if (itnim->rport->scsi_function == BFA_RPORT_INITIATOR)
155
			bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_initiator);
156 157 158 159 160
		else
			bfa_sm_set_state(itnim,
				bfa_fcs_itnim_sm_hal_rport_online);

		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_FCS_ONLINE);
161 162 163 164 165 166 167 168 169
		break;

	case BFA_FCS_ITNIM_SM_RSP_ERROR:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_prli_retry);
		bfa_timer_start(itnim->fcs->bfa, &itnim->timer,
				bfa_fcs_itnim_timeout, itnim,
				BFA_FCS_RETRY_TIMEOUT);
		break;

K
Krishna Gudipati 已提交
170 171 172 173
	case BFA_FCS_ITNIM_SM_RSP_NOT_SUPP:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		break;

174 175 176
	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcxp_discard(itnim->fcxp);
177
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
178 179 180 181
		break;

	case BFA_FCS_ITNIM_SM_INITIATOR:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_initiator);
182
		bfa_fcxp_discard(itnim->fcxp);
183
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_FCS_ONLINE);
184 185 186 187 188 189 190 191 192
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcxp_discard(itnim->fcxp);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
193
		bfa_sm_fault(itnim->fcs, event);
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
static void
bfa_fcs_itnim_sm_hal_rport_online(struct bfa_fcs_itnim_s *itnim,
				enum bfa_fcs_itnim_event event)
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_HAL_ONLINE:
		if (!itnim->bfa_itnim)
			itnim->bfa_itnim = bfa_itnim_create(itnim->fcs->bfa,
					itnim->rport->bfa_rport, itnim);

		if (itnim->bfa_itnim) {
			bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_hcb_online);
			bfa_itnim_online(itnim->bfa_itnim, itnim->seq_rec);
		} else {
			bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
			bfa_sm_send_event(itnim->rport, RPSM_EVENT_DELETE);
		}

		break;

	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
		bfa_sm_fault(itnim->fcs, event);
	}
}

235 236 237 238 239 240 241 242 243
static void
bfa_fcs_itnim_sm_prli_retry(struct bfa_fcs_itnim_s *itnim,
			    enum bfa_fcs_itnim_event event)
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_TIMEOUT:
J
Jing Huang 已提交
244 245 246 247 248 249 250 251
		if (itnim->prli_retries < BFA_FCS_RPORT_MAX_RETRIES) {
			itnim->prli_retries++;
			bfa_trc(itnim->fcs, itnim->prli_retries);
			bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_prli_send);
			bfa_fcs_itnim_send_prli(itnim, NULL);
		} else {
			/* invoke target offline */
			bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
252
			bfa_sm_send_event(itnim->rport, RPSM_EVENT_LOGO_IMP);
J
Jing Huang 已提交
253
		}
254 255
		break;

K
Krishna Gudipati 已提交
256

257 258 259
	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_timer_stop(&itnim->timer);
260
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
261 262 263 264 265
		break;

	case BFA_FCS_ITNIM_SM_INITIATOR:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_initiator);
		bfa_timer_stop(&itnim->timer);
266
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_FCS_ONLINE);
267 268 269 270 271 272 273 274 275
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_timer_stop(&itnim->timer);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
276
		bfa_sm_fault(itnim->fcs, event);
277 278 279 280 281 282 283
	}
}

static void
bfa_fcs_itnim_sm_hcb_online(struct bfa_fcs_itnim_s *itnim,
			    enum bfa_fcs_itnim_event event)
{
K
Krishna Gudipati 已提交
284 285 286 287
	struct bfad_s *bfad = (struct bfad_s *)itnim->fcs->bfad;
	char	lpwwn_buf[BFA_STRING_32];
	char	rpwwn_buf[BFA_STRING_32];

288 289 290 291 292 293 294
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_HCB_ONLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_online);
		bfa_fcb_itnim_online(itnim->itnim_drv);
K
Krishna Gudipati 已提交
295 296
		wwn2str(lpwwn_buf, bfa_fcs_lport_get_pwwn(itnim->rport->port));
		wwn2str(rpwwn_buf, itnim->rport->pwwn);
297
		BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
298 299
		"Target (WWN = %s) is online for initiator (WWN = %s)\n",
		rpwwn_buf, lpwwn_buf);
300
		bfa_fcs_itnim_aen_post(itnim, BFA_ITNIM_AEN_ONLINE);
301 302 303
		break;

	case BFA_FCS_ITNIM_SM_OFFLINE:
304
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_hcb_offline);
305 306 307 308 309 310 311 312 313
		bfa_itnim_offline(itnim->bfa_itnim);
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
314
		bfa_sm_fault(itnim->fcs, event);
315 316 317 318 319
	}
}

static void
bfa_fcs_itnim_sm_online(struct bfa_fcs_itnim_s *itnim,
K
Krishna Gudipati 已提交
320
		 enum bfa_fcs_itnim_event event)
321
{
K
Krishna Gudipati 已提交
322 323 324 325
	struct bfad_s *bfad = (struct bfad_s *)itnim->fcs->bfad;
	char	lpwwn_buf[BFA_STRING_32];
	char	rpwwn_buf[BFA_STRING_32];

326 327 328 329 330 331 332 333
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_hcb_offline);
		bfa_fcb_itnim_offline(itnim->itnim_drv);
		bfa_itnim_offline(itnim->bfa_itnim);
K
Krishna Gudipati 已提交
334 335
		wwn2str(lpwwn_buf, bfa_fcs_lport_get_pwwn(itnim->rport->port));
		wwn2str(rpwwn_buf, itnim->rport->pwwn);
336
		if (bfa_fcs_lport_is_online(itnim->rport->port) == BFA_TRUE) {
337
			BFA_LOG(KERN_ERR, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
338 339
			"Target (WWN = %s) connectivity lost for "
			"initiator (WWN = %s)\n", rpwwn_buf, lpwwn_buf);
340 341
			bfa_fcs_itnim_aen_post(itnim, BFA_ITNIM_AEN_DISCONNECT);
		} else {
342
			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
K
Krishna Gudipati 已提交
343 344
			"Target (WWN = %s) offlined by initiator (WWN = %s)\n",
			rpwwn_buf, lpwwn_buf);
345 346
			bfa_fcs_itnim_aen_post(itnim, BFA_ITNIM_AEN_OFFLINE);
		}
347 348 349 350 351 352 353 354
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
355
		bfa_sm_fault(itnim->fcs, event);
356 357 358 359 360 361 362 363 364 365 366 367 368
	}
}

static void
bfa_fcs_itnim_sm_hcb_offline(struct bfa_fcs_itnim_s *itnim,
			     enum bfa_fcs_itnim_event event)
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_HCB_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
369
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
370 371 372 373 374 375 376 377
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
378
		bfa_sm_fault(itnim->fcs, event);
379 380 381 382 383 384 385 386 387 388
	}
}

/*
 * This state is set when a discovered rport is also in intiator mode.
 * This ITN is marked as no_op and is not active and will not be truned into
 * online state.
 */
static void
bfa_fcs_itnim_sm_initiator(struct bfa_fcs_itnim_s *itnim,
K
Krishna Gudipati 已提交
389
		 enum bfa_fcs_itnim_event event)
390 391 392 393 394 395 396
{
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_trc(itnim->fcs, event);

	switch (event) {
	case BFA_FCS_ITNIM_SM_OFFLINE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
397
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_OFFLINE);
398 399
		break;

400 401 402 403 404 405 406
	/*
	 * fcs_online is expected here for well known initiator ports
	 */
	case BFA_FCS_ITNIM_SM_FCS_ONLINE:
		bfa_sm_send_event(itnim->rport, RPSM_EVENT_FC4_FCS_ONLINE);
		break;

407 408 409 410 411 412 413 414 415 416
	case BFA_FCS_ITNIM_SM_RSP_ERROR:
	case BFA_FCS_ITNIM_SM_INITIATOR:
		break;

	case BFA_FCS_ITNIM_SM_DELETE:
		bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);
		bfa_fcs_itnim_free(itnim);
		break;

	default:
417
		bfa_sm_fault(itnim->fcs, event);
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
static void
bfa_fcs_itnim_aen_post(struct bfa_fcs_itnim_s *itnim,
			enum bfa_itnim_aen_event event)
{
	struct bfa_fcs_rport_s *rport = itnim->rport;
	struct bfad_s *bfad = (struct bfad_s *)itnim->fcs->bfad;
	struct bfa_aen_entry_s	*aen_entry;

	/* Don't post events for well known addresses */
	if (BFA_FCS_PID_IS_WKA(rport->pid))
		return;

	bfad_get_aen_entry(bfad, aen_entry);
	if (!aen_entry)
		return;

	aen_entry->aen_data.itnim.vf_id = rport->port->fabric->vf_id;
	aen_entry->aen_data.itnim.ppwwn = bfa_fcs_lport_get_pwwn(
					bfa_fcs_get_base_port(itnim->fcs));
	aen_entry->aen_data.itnim.lpwwn = bfa_fcs_lport_get_pwwn(rport->port);
	aen_entry->aen_data.itnim.rpwwn = rport->pwwn;

	/* Send the AEN notification */
	bfad_im_post_vendor_event(aen_entry, bfad, ++rport->fcs->fcs_aen_seq,
				  BFA_AEN_CAT_ITNIM, event);
}

448 449 450 451 452
static void
bfa_fcs_itnim_send_prli(void *itnim_cbarg, struct bfa_fcxp_s *fcxp_alloced)
{
	struct bfa_fcs_itnim_s *itnim = itnim_cbarg;
	struct bfa_fcs_rport_s *rport = itnim->rport;
K
Krishna Gudipati 已提交
453 454
	struct bfa_fcs_lport_s *port = rport->port;
	struct fchs_s	fchs;
455
	struct bfa_fcxp_s *fcxp;
K
Krishna Gudipati 已提交
456
	int		len;
457 458 459

	bfa_trc(itnim->fcs, itnim->rport->pwwn);

460 461
	fcxp = fcxp_alloced ? fcxp_alloced :
	       bfa_fcs_fcxp_alloc(port->fcs, BFA_TRUE);
462 463
	if (!fcxp) {
		itnim->stats.fcxp_alloc_wait++;
K
Krishna Gudipati 已提交
464
		bfa_fcs_fcxp_alloc_wait(port->fcs->bfa, &itnim->fcxp_wqe,
465
				bfa_fcs_itnim_send_prli, itnim, BFA_TRUE);
466 467 468 469
		return;
	}
	itnim->fcxp = fcxp;

K
Krishna Gudipati 已提交
470 471
	len = fc_prli_build(&fchs, bfa_fcxp_get_reqbuf(fcxp),
			    itnim->rport->pid, bfa_fcs_lport_get_fcid(port), 0);
472 473 474

	bfa_fcxp_send(fcxp, rport->bfa_rport, port->fabric->vf_id, port->lp_tag,
		      BFA_FALSE, FC_CLASS_3, len, &fchs,
K
Krishna Gudipati 已提交
475 476
		      bfa_fcs_itnim_prli_response, (void *)itnim,
		      FC_MAX_PDUSZ, FC_ELS_TOV);
477 478 479 480 481 482 483 484 485 486

	itnim->stats.prli_sent++;
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_FRMSENT);
}

static void
bfa_fcs_itnim_prli_response(void *fcsarg, struct bfa_fcxp_s *fcxp, void *cbarg,
			    bfa_status_t req_status, u32 rsp_len,
			    u32 resid_len, struct fchs_s *rsp_fchs)
{
K
Krishna Gudipati 已提交
487 488 489 490
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cbarg;
	struct fc_els_cmd_s *els_cmd;
	struct fc_prli_s *prli_resp;
	struct fc_ls_rjt_s *ls_rjt;
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
	struct fc_prli_params_s *sparams;

	bfa_trc(itnim->fcs, req_status);

	/*
	 * Sanity Checks
	 */
	if (req_status != BFA_STATUS_OK) {
		itnim->stats.prli_rsp_err++;
		bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_RSP_ERROR);
		return;
	}

	els_cmd = (struct fc_els_cmd_s *) BFA_FCXP_RSP_PLD(fcxp);

	if (els_cmd->els_code == FC_ELS_ACC) {
		prli_resp = (struct fc_prli_s *) els_cmd;

		if (fc_prli_rsp_parse(prli_resp, rsp_len) != FC_PARSE_OK) {
			bfa_trc(itnim->fcs, rsp_len);
			/*
			 * Check if this  r-port is also in Initiator mode.
			 * If so, we need to set this ITN as a no-op.
			 */
			if (prli_resp->parampage.servparams.initiator) {
				bfa_trc(itnim->fcs, prli_resp->parampage.type);
				itnim->rport->scsi_function =
518
						BFA_RPORT_INITIATOR;
519
				itnim->stats.prli_rsp_acc++;
K
Krishna Gudipati 已提交
520
				itnim->stats.initiator++;
521
				bfa_sm_send_event(itnim,
522
						  BFA_FCS_ITNIM_SM_RSP_OK);
523 524 525 526 527 528 529 530 531
				return;
			}

			itnim->stats.prli_rsp_parse_err++;
			return;
		}
		itnim->rport->scsi_function = BFA_RPORT_TARGET;

		sparams = &prli_resp->parampage.servparams;
K
Krishna Gudipati 已提交
532 533
		itnim->seq_rec	     = sparams->retry;
		itnim->rec_support   = sparams->rec_support;
534
		itnim->task_retry_id = sparams->task_retry_id;
K
Krishna Gudipati 已提交
535
		itnim->conf_comp     = sparams->confirm;
536 537 538 539 540 541 542 543 544 545

		itnim->stats.prli_rsp_acc++;
		bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_RSP_OK);
	} else {
		ls_rjt = (struct fc_ls_rjt_s *) BFA_FCXP_RSP_PLD(fcxp);

		bfa_trc(itnim->fcs, ls_rjt->reason_code);
		bfa_trc(itnim->fcs, ls_rjt->reason_code_expl);

		itnim->stats.prli_rsp_rjt++;
K
Krishna Gudipati 已提交
546 547 548 549
		if (ls_rjt->reason_code == FC_LS_RJT_RSN_CMD_NOT_SUPP) {
			bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_RSP_NOT_SUPP);
			return;
		}
550 551 552 553 554 555 556
		bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_RSP_ERROR);
	}
}

static void
bfa_fcs_itnim_timeout(void *arg)
{
K
Krishna Gudipati 已提交
557
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) arg;
558 559 560 561 562 563 564 565

	itnim->stats.timeout++;
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_TIMEOUT);
}

static void
bfa_fcs_itnim_free(struct bfa_fcs_itnim_s *itnim)
{
566 567 568 569 570
	if (itnim->bfa_itnim) {
		bfa_itnim_delete(itnim->bfa_itnim);
		itnim->bfa_itnim = NULL;
	}

571 572 573 574 575
	bfa_fcb_itnim_free(itnim->fcs->bfad, itnim->itnim_drv);
}



J
Jing Huang 已提交
576
/*
577 578 579
 *  itnim_public FCS ITNIM public interfaces
 */

J
Jing Huang 已提交
580
/*
K
Krishna Gudipati 已提交
581
 *	Called by rport when a new rport is created.
582 583 584 585 586 587
 *
 * @param[in] rport	-  remote port.
 */
struct bfa_fcs_itnim_s *
bfa_fcs_itnim_create(struct bfa_fcs_rport_s *rport)
{
K
Krishna Gudipati 已提交
588
	struct bfa_fcs_lport_s *port = rport->port;
589
	struct bfa_fcs_itnim_s *itnim;
K
Krishna Gudipati 已提交
590
	struct bfad_itnim_s   *itnim_drv;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

	/*
	 * call bfad to allocate the itnim
	 */
	bfa_fcb_itnim_alloc(port->fcs->bfad, &itnim, &itnim_drv);
	if (itnim == NULL) {
		bfa_trc(port->fcs, rport->pwwn);
		return NULL;
	}

	/*
	 * Initialize itnim
	 */
	itnim->rport = rport;
	itnim->fcs = rport->fcs;
	itnim->itnim_drv = itnim_drv;

608
	itnim->bfa_itnim     = NULL;
K
Krishna Gudipati 已提交
609 610 611
	itnim->seq_rec	     = BFA_FALSE;
	itnim->rec_support   = BFA_FALSE;
	itnim->conf_comp     = BFA_FALSE;
612 613 614 615 616 617 618 619 620 621
	itnim->task_retry_id = BFA_FALSE;

	/*
	 * Set State machine
	 */
	bfa_sm_set_state(itnim, bfa_fcs_itnim_sm_offline);

	return itnim;
}

J
Jing Huang 已提交
622
/*
K
Krishna Gudipati 已提交
623
 *	Called by rport to delete  the instance of FCPIM.
624 625 626 627 628 629 630 631 632 633
 *
 * @param[in] rport	-  remote port.
 */
void
bfa_fcs_itnim_delete(struct bfa_fcs_itnim_s *itnim)
{
	bfa_trc(itnim->fcs, itnim->rport->pid);
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_DELETE);
}

J
Jing Huang 已提交
634
/*
635 636 637
 * Notification from rport that PLOGI is complete to initiate FC-4 session.
 */
void
638
bfa_fcs_itnim_brp_online(struct bfa_fcs_itnim_s *itnim)
639 640 641
{
	itnim->stats.onlines++;

642 643
	if (!BFA_FCS_PID_IS_WKA(itnim->rport->pid))
		bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_HAL_ONLINE);
644 645
}

J
Jing Huang 已提交
646
/*
647 648 649 650 651 652 653 654 655
 * Called by rport to handle a remote device offline.
 */
void
bfa_fcs_itnim_rport_offline(struct bfa_fcs_itnim_s *itnim)
{
	itnim->stats.offlines++;
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_OFFLINE);
}

J
Jing Huang 已提交
656
/*
657 658 659 660 661 662 663 664 665 666 667
 * Called by rport when remote port is known to be an initiator from
 * PRLI received.
 */
void
bfa_fcs_itnim_is_initiator(struct bfa_fcs_itnim_s *itnim)
{
	bfa_trc(itnim->fcs, itnim->rport->pid);
	itnim->stats.initiator++;
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_INITIATOR);
}

J
Jing Huang 已提交
668
/*
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
 * Called by rport to check if the itnim is online.
 */
bfa_status_t
bfa_fcs_itnim_get_online_state(struct bfa_fcs_itnim_s *itnim)
{
	bfa_trc(itnim->fcs, itnim->rport->pid);
	switch (bfa_sm_to_state(itnim_sm_table, itnim->sm)) {
	case BFA_ITNIM_ONLINE:
	case BFA_ITNIM_INITIATIOR:
		return BFA_STATUS_OK;

	default:
		return BFA_STATUS_NO_FCPIM_NEXUS;
	}
}

J
Jing Huang 已提交
685
/*
686 687 688 689 690
 * BFA completion callback for bfa_itnim_online().
 */
void
bfa_cb_itnim_online(void *cbarg)
{
K
Krishna Gudipati 已提交
691
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cbarg;
692 693 694 695 696

	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_HCB_ONLINE);
}

J
Jing Huang 已提交
697
/*
698 699 700 701 702
 * BFA completion callback for bfa_itnim_offline().
 */
void
bfa_cb_itnim_offline(void *cb_arg)
{
K
Krishna Gudipati 已提交
703
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cb_arg;
704 705 706 707 708

	bfa_trc(itnim->fcs, itnim->rport->pwwn);
	bfa_sm_send_event(itnim, BFA_FCS_ITNIM_SM_HCB_OFFLINE);
}

J
Jing Huang 已提交
709
/*
710 711 712 713 714 715
 * Mark the beginning of PATH TOV handling. IO completion callbacks
 * are still pending.
 */
void
bfa_cb_itnim_tov_begin(void *cb_arg)
{
K
Krishna Gudipati 已提交
716
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cb_arg;
717 718 719 720

	bfa_trc(itnim->fcs, itnim->rport->pwwn);
}

J
Jing Huang 已提交
721
/*
722 723 724 725 726
 * Mark the end of PATH TOV handling. All pending IOs are already cleaned up.
 */
void
bfa_cb_itnim_tov(void *cb_arg)
{
K
Krishna Gudipati 已提交
727 728
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cb_arg;
	struct bfad_itnim_s *itnim_drv = itnim->itnim_drv;
729 730

	bfa_trc(itnim->fcs, itnim->rport->pwwn);
K
Krishna Gudipati 已提交
731
	itnim_drv->state = ITNIM_STATE_TIMEOUT;
732 733
}

J
Jing Huang 已提交
734
/*
K
Krishna Gudipati 已提交
735
 *		BFA notification to FCS/driver for second level error recovery.
736 737 738 739 740 741 742 743
 *
 * Atleast one I/O request has timedout and target is unresponsive to
 * repeated abort requests. Second level error recovery should be initiated
 * by starting implicit logout and recovery procedures.
 */
void
bfa_cb_itnim_sler(void *cb_arg)
{
K
Krishna Gudipati 已提交
744
	struct bfa_fcs_itnim_s *itnim = (struct bfa_fcs_itnim_s *) cb_arg;
745 746 747

	itnim->stats.sler++;
	bfa_trc(itnim->fcs, itnim->rport->pwwn);
748
	bfa_sm_send_event(itnim->rport, RPSM_EVENT_LOGO_IMP);
749 750 751
}

struct bfa_fcs_itnim_s *
K
Krishna Gudipati 已提交
752
bfa_fcs_itnim_lookup(struct bfa_fcs_lport_s *port, wwn_t rpwwn)
753 754 755 756 757 758 759
{
	struct bfa_fcs_rport_s *rport;
	rport = bfa_fcs_rport_lookup(port, rpwwn);

	if (!rport)
		return NULL;

760
	WARN_ON(rport->itnim == NULL);
761
	return rport->itnim;
762 763 764
}

bfa_status_t
K
Krishna Gudipati 已提交
765
bfa_fcs_itnim_attr_get(struct bfa_fcs_lport_s *port, wwn_t rpwwn,
766 767 768 769 770 771 772 773 774
		       struct bfa_itnim_attr_s *attr)
{
	struct bfa_fcs_itnim_s *itnim = NULL;

	itnim = bfa_fcs_itnim_lookup(port, rpwwn);

	if (itnim == NULL)
		return BFA_STATUS_NO_FCPIM_NEXUS;

K
Krishna Gudipati 已提交
775 776 777 778
	attr->state	    = bfa_sm_to_state(itnim_sm_table, itnim->sm);
	attr->retry	    = itnim->seq_rec;
	attr->rec_support   = itnim->rec_support;
	attr->conf_comp	    = itnim->conf_comp;
779 780 781 782 783
	attr->task_retry_id = itnim->task_retry_id;
	return BFA_STATUS_OK;
}

bfa_status_t
K
Krishna Gudipati 已提交
784
bfa_fcs_itnim_stats_get(struct bfa_fcs_lport_s *port, wwn_t rpwwn,
785 786 787 788
			struct bfa_itnim_stats_s *stats)
{
	struct bfa_fcs_itnim_s *itnim = NULL;

789
	WARN_ON(port == NULL);
790 791 792 793 794 795

	itnim = bfa_fcs_itnim_lookup(port, rpwwn);

	if (itnim == NULL)
		return BFA_STATUS_NO_FCPIM_NEXUS;

796
	memcpy(stats, &itnim->stats, sizeof(struct bfa_itnim_stats_s));
797 798 799 800 801

	return BFA_STATUS_OK;
}

bfa_status_t
K
Krishna Gudipati 已提交
802
bfa_fcs_itnim_stats_clear(struct bfa_fcs_lport_s *port, wwn_t rpwwn)
803 804 805
{
	struct bfa_fcs_itnim_s *itnim = NULL;

806
	WARN_ON(port == NULL);
807 808 809 810 811 812

	itnim = bfa_fcs_itnim_lookup(port, rpwwn);

	if (itnim == NULL)
		return BFA_STATUS_NO_FCPIM_NEXUS;

813
	memset(&itnim->stats, 0, sizeof(struct bfa_itnim_stats_s));
814 815 816 817
	return BFA_STATUS_OK;
}

void
K
Krishna Gudipati 已提交
818 819
bfa_fcs_fcpim_uf_recv(struct bfa_fcs_itnim_s *itnim,
			struct fchs_s *fchs, u16 len)
820
{
K
Krishna Gudipati 已提交
821
	struct fc_els_cmd_s *els_cmd;
822 823 824 825 826 827 828 829 830 831 832 833

	bfa_trc(itnim->fcs, fchs->type);

	if (fchs->type != FC_TYPE_ELS)
		return;

	els_cmd = (struct fc_els_cmd_s *) (fchs + 1);

	bfa_trc(itnim->fcs, els_cmd->els_code);

	switch (els_cmd->els_code) {
	case FC_ELS_PRLO:
834
		bfa_fcs_rport_prlo(itnim->rport, fchs->ox_id);
835 836 837
		break;

	default:
838
		WARN_ON(1);
839 840
	}
}