xref: /linux/drivers/scsi/fnic/fdls_disc.c (revision 7db28abbea0f7dc1ec4fdfdc149db5fbd9e4c994)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
4  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
5  */
6 
7 #include <linux/workqueue.h>
8 #include "fnic.h"
9 #include "fdls_fc.h"
10 #include "fnic_fdls.h"
11 #include <scsi/fc/fc_fcp.h>
12 #include <scsi/scsi_transport_fc.h>
13 #include <linux/utsname.h>
14 
15 #define FC_FC4_TYPE_SCSI 0x08
16 #define FNIC_NVME_FEAT_TARG (1 << 0)
17 #define FNIC_NVME_FEAT_INIT (1 << 1)
18 #define FNIC_NVME_FEAT_DISC (1 << 2)
19 
20 #define PORT_SPEED_BIT_8 8
21 #define PORT_SPEED_BIT_9 9
22 #define PORT_SPEED_BIT_14 14
23 #define PORT_SPEED_BIT_15 15
24 
25 /* FNIC FDMI Register HBA Macros */
26 #define FNIC_FDMI_NUM_PORTS 1
27 #define FNIC_FDMI_NUM_HBA_ATTRS 9
28 #define FNIC_FDMI_TYPE_NODE_NAME	0X1
29 #define FNIC_FDMI_TYPE_MANUFACTURER	0X2
30 #define FNIC_FDMI_MANUFACTURER		"Cisco Systems"
31 #define FNIC_FDMI_TYPE_SERIAL_NUMBER	0X3
32 #define FNIC_FDMI_TYPE_MODEL		0X4
33 #define FNIC_FDMI_TYPE_MODEL_DES	0X5
34 #define FNIC_FDMI_MODEL_DESCRIPTION	"Cisco Virtual Interface Card"
35 #define FNIC_FDMI_TYPE_HARDWARE_VERSION	0X6
36 #define FNIC_FDMI_TYPE_DRIVER_VERSION	0X7
37 #define FNIC_FDMI_TYPE_ROM_VERSION	0X8
38 #define FNIC_FDMI_TYPE_FIRMWARE_VERSION	0X9
39 #define FNIC_FDMI_NN_LEN 8
40 #define FNIC_FDMI_MANU_LEN 20
41 #define FNIC_FDMI_SERIAL_LEN 16
42 #define FNIC_FDMI_MODEL_LEN 12
43 #define FNIC_FDMI_MODEL_DES_LEN 56
44 #define FNIC_FDMI_HW_VER_LEN 16
45 #define FNIC_FDMI_DR_VER_LEN 28
46 #define FNIC_FDMI_ROM_VER_LEN 8
47 #define FNIC_FDMI_FW_VER_LEN 16
48 
49 /* FNIC FDMI Register PA Macros */
50 #define FNIC_FDMI_TYPE_FC4_TYPES	0X1
51 #define FNIC_FDMI_TYPE_SUPPORTED_SPEEDS 0X2
52 #define FNIC_FDMI_TYPE_CURRENT_SPEED	0X3
53 #define FNIC_FDMI_TYPE_MAX_FRAME_SIZE	0X4
54 #define FNIC_FDMI_TYPE_OS_NAME		0X5
55 #define FNIC_FDMI_TYPE_HOST_NAME	0X6
56 #define FNIC_FDMI_NUM_PORT_ATTRS 6
57 #define FNIC_FDMI_FC4_LEN 32
58 #define FNIC_FDMI_SUPP_SPEED_LEN 4
59 #define FNIC_FDMI_CUR_SPEED_LEN 4
60 #define FNIC_FDMI_MFS_LEN 4
61 #define FNIC_FDMI_MFS 0x800
62 #define FNIC_FDMI_OS_NAME_LEN 16
63 #define FNIC_FDMI_HN_LEN 24
64 
65 #define FDLS_FDMI_PLOGI_PENDING 0x1
66 #define FDLS_FDMI_REG_HBA_PENDING 0x2
67 #define FDLS_FDMI_RPA_PENDING 0x4
68 #define FDLS_FDMI_ABORT_PENDING 0x8
69 #define FDLS_FDMI_MAX_RETRY 3
70 
71 #define RETRIES_EXHAUSTED(iport)      \
72 	(iport->fabric.retry_counter == FABRIC_LOGO_MAX_RETRY)
73 
74 #define FNIC_TPORT_MAX_NEXUS_RESTART (8)
75 
76 #define SCHEDULE_OXID_FREE_RETRY_TIME (300)
77 
78 /* Private Functions */
79 static void fdls_fdmi_register_hba(struct fnic_iport_s *iport);
80 static void fdls_fdmi_register_pa(struct fnic_iport_s *iport);
81 static void fdls_send_rpn_id(struct fnic_iport_s *iport);
82 static void fdls_process_flogi_rsp(struct fnic_iport_s *iport,
83 				   struct fc_frame_header *fchdr,
84 				   void *rx_frame);
85 static void fnic_fdls_start_plogi(struct fnic_iport_s *iport);
86 static void fnic_fdls_start_flogi(struct fnic_iport_s *iport);
87 static struct fnic_tport_s *fdls_create_tport(struct fnic_iport_s *iport,
88 					  uint32_t fcid,
89 					  uint64_t wwpn);
90 static void fdls_target_restart_nexus(struct fnic_tport_s *tport);
91 static void fdls_start_tport_timer(struct fnic_iport_s *iport,
92 					struct fnic_tport_s *tport, int timeout);
93 static void fdls_tport_timer_callback(struct timer_list *t);
94 static void fdls_send_fdmi_plogi(struct fnic_iport_s *iport);
95 static void fdls_start_fabric_timer(struct fnic_iport_s *iport,
96 			int timeout);
97 static void fdls_init_plogi_frame(uint8_t *frame, struct fnic_iport_s *iport);
98 static void fdls_init_els_acc_frame(uint8_t *frame, struct fnic_iport_s *iport);
99 static void fdls_init_els_rjt_frame(uint8_t *frame, struct fnic_iport_s *iport);
100 static void fdls_init_logo_frame(uint8_t *frame, struct fnic_iport_s *iport);
101 static void fdls_init_fabric_abts_frame(uint8_t *frame,
102 						struct fnic_iport_s *iport);
103 
104 uint8_t *fdls_alloc_frame(struct fnic_iport_s *iport)
105 {
106 	struct fnic *fnic = iport->fnic;
107 	uint8_t *frame = NULL;
108 
109 	frame = mempool_alloc(fnic->frame_pool, GFP_ATOMIC);
110 	if (frame == NULL) {
111 		FNIC_FCS_DBG(KERN_ERR, fnic,
112 				"Failed to allocate frame");
113 		return NULL;
114 	}
115 
116 	memset(frame, 0, FNIC_FCOE_FRAME_MAXSZ);
117 	return frame;
118 }
119 
120 /**
121  * fdls_alloc_oxid - Allocate an oxid from the bitmap based oxid pool
122  * @iport: Handle to iport instance
123  * @oxid_frame_type: Type of frame to allocate
124  * @active_oxid: the oxid which is in use
125  *
126  * Called with fnic lock held
127  */
128 uint16_t fdls_alloc_oxid(struct fnic_iport_s *iport, int oxid_frame_type,
129 	uint16_t *active_oxid)
130 {
131 	struct fnic *fnic = iport->fnic;
132 	struct fnic_oxid_pool_s *oxid_pool = &iport->oxid_pool;
133 	int idx;
134 	uint16_t oxid;
135 
136 	lockdep_assert_held(&fnic->fnic_lock);
137 
138 	/*
139 	 * Allocate next available oxid from bitmap
140 	 */
141 	idx = find_next_zero_bit(oxid_pool->bitmap, FNIC_OXID_POOL_SZ, oxid_pool->next_idx);
142 	if (idx == FNIC_OXID_POOL_SZ) {
143 		FNIC_FCS_DBG(KERN_INFO, fnic,
144 			"Alloc oxid: all oxid slots are busy iport state:%d\n",
145 			iport->state);
146 		return FNIC_UNASSIGNED_OXID;
147 	}
148 
149 	WARN_ON(test_and_set_bit(idx, oxid_pool->bitmap));
150 	oxid_pool->next_idx = (idx + 1) % FNIC_OXID_POOL_SZ;	/* cycle through the bitmap */
151 
152 	oxid = FNIC_OXID_ENCODE(idx, oxid_frame_type);
153 	*active_oxid = oxid;
154 
155 	FNIC_FCS_DBG(KERN_INFO, fnic,
156 	   "alloc oxid: 0x%x, iport state: %d\n",
157 	   oxid, iport->state);
158 	return oxid;
159 }
160 
161 /**
162  * fdls_free_oxid_idx - Free the oxid using the idx
163  * @iport: Handle to iport instance
164  * @oxid_idx: The index to free
165  *
166  * Free the oxid immediately and make it available for new requests
167  * Called with fnic lock held
168  */
169 static void fdls_free_oxid_idx(struct fnic_iport_s *iport, uint16_t oxid_idx)
170 {
171 	struct fnic *fnic = iport->fnic;
172 	struct fnic_oxid_pool_s *oxid_pool = &iport->oxid_pool;
173 
174 	lockdep_assert_held(&fnic->fnic_lock);
175 
176 		FNIC_FCS_DBG(KERN_INFO, fnic,
177 		"free oxid idx: 0x%x\n", oxid_idx);
178 
179 	WARN_ON(!test_and_clear_bit(oxid_idx, oxid_pool->bitmap));
180 }
181 
182 /**
183  * fdls_reclaim_oxid_handler - Callback handler for delayed_oxid_work
184  * @work: Handle to work_struct
185  *
186  * Scheduled when an oxid is to be freed later
187  * After freeing expired oxid(s), the handler schedules
188  * another callback with the remaining time
189  * of next unexpired entry in the reclaim list.
190  */
191 void fdls_reclaim_oxid_handler(struct work_struct *work)
192 {
193 	struct fnic_oxid_pool_s *oxid_pool = container_of(work,
194 		struct fnic_oxid_pool_s, oxid_reclaim_work.work);
195 	struct fnic_iport_s *iport = container_of(oxid_pool,
196 		struct fnic_iport_s, oxid_pool);
197 	struct fnic *fnic = iport->fnic;
198 	struct reclaim_entry_s *reclaim_entry, *next;
199 	unsigned long delay_j, cur_jiffies;
200 
201 	FNIC_FCS_DBG(KERN_INFO, fnic,
202 		"Reclaim oxid callback\n");
203 
204 	spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
205 
206 	/* Though the work was scheduled for one entry,
207 	 * walk through and free the expired entries which might have been scheduled
208 	 * at around the same time as the first entry
209 	 */
210 	list_for_each_entry_safe(reclaim_entry, next,
211 		&(oxid_pool->oxid_reclaim_list), links) {
212 
213 		/* The list is always maintained in the order of expiry time */
214 		cur_jiffies = jiffies;
215 		if (time_before(cur_jiffies, reclaim_entry->expires))
216 			break;
217 
218 		list_del(&reclaim_entry->links);
219 		fdls_free_oxid_idx(iport, reclaim_entry->oxid_idx);
220 		kfree(reclaim_entry);
221 	}
222 
223 	/* schedule to free up the next entry */
224 	if (!list_empty(&oxid_pool->oxid_reclaim_list)) {
225 		reclaim_entry = list_first_entry(&oxid_pool->oxid_reclaim_list,
226 			struct reclaim_entry_s, links);
227 
228 		delay_j = reclaim_entry->expires - cur_jiffies;
229 		schedule_delayed_work(&oxid_pool->oxid_reclaim_work, delay_j);
230 		FNIC_FCS_DBG(KERN_INFO, fnic,
231 			"Scheduling next callback at:%ld jiffies\n", delay_j);
232 	}
233 
234 	spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
235 }
236 
237 /**
238  * fdls_free_oxid - Helper function to free the oxid
239  * @iport: Handle to iport instance
240  * @oxid: oxid to free
241  * @active_oxid: the oxid which is in use
242  *
243  * Called with fnic lock held
244  */
245 void fdls_free_oxid(struct fnic_iport_s *iport,
246 		uint16_t oxid, uint16_t *active_oxid)
247 {
248 	fdls_free_oxid_idx(iport, FNIC_OXID_IDX(oxid));
249 	*active_oxid = FNIC_UNASSIGNED_OXID;
250 }
251 
252 /**
253  * fdls_schedule_oxid_free - Schedule oxid to be freed later
254  * @iport: Handle to iport instance
255  * @active_oxid: the oxid which is in use
256  *
257  * Gets called in a rare case scenario when both a command
258  * (fdls or target discovery) timed out and the following ABTS
259  * timed out as well, without a link change.
260  *
261  * Called with fnic lock held
262  */
263 void fdls_schedule_oxid_free(struct fnic_iport_s *iport, uint16_t *active_oxid)
264 {
265 	struct fnic *fnic = iport->fnic;
266 	struct fnic_oxid_pool_s *oxid_pool = &iport->oxid_pool;
267 	struct reclaim_entry_s *reclaim_entry;
268 	unsigned long delay_j = msecs_to_jiffies(OXID_RECLAIM_TOV(iport));
269 	int oxid_idx = FNIC_OXID_IDX(*active_oxid);
270 
271 	lockdep_assert_held(&fnic->fnic_lock);
272 
273 	FNIC_FCS_DBG(KERN_INFO, fnic,
274 		"Schedule oxid free. oxid: 0x%x\n", *active_oxid);
275 
276 	*active_oxid = FNIC_UNASSIGNED_OXID;
277 
278 	reclaim_entry = (struct reclaim_entry_s *)
279 		kzalloc_obj(struct reclaim_entry_s, GFP_ATOMIC);
280 
281 	if (!reclaim_entry) {
282 		FNIC_FCS_DBG(KERN_WARNING, fnic,
283 			"Failed to allocate memory for reclaim struct for oxid idx: %d\n",
284 			oxid_idx);
285 
286 		/* Retry the scheduling  */
287 		WARN_ON(test_and_set_bit(oxid_idx, oxid_pool->pending_schedule_free));
288 		schedule_delayed_work(&oxid_pool->schedule_oxid_free_retry, 0);
289 		return;
290 	}
291 
292 	reclaim_entry->oxid_idx = oxid_idx;
293 	reclaim_entry->expires = round_jiffies(jiffies + delay_j);
294 
295 	list_add_tail(&reclaim_entry->links, &oxid_pool->oxid_reclaim_list);
296 
297 	schedule_delayed_work(&oxid_pool->oxid_reclaim_work, delay_j);
298 }
299 
300 /**
301  * fdls_schedule_oxid_free_retry_work - Thread to schedule the
302  * oxid to be freed later
303  *
304  * @work: Handle to the work struct
305  */
306 void fdls_schedule_oxid_free_retry_work(struct work_struct *work)
307 {
308 	struct fnic_oxid_pool_s *oxid_pool = container_of(work,
309 		struct fnic_oxid_pool_s, schedule_oxid_free_retry.work);
310 	struct fnic_iport_s *iport = container_of(oxid_pool,
311 		struct fnic_iport_s, oxid_pool);
312 	struct fnic *fnic = iport->fnic;
313 	struct reclaim_entry_s *reclaim_entry;
314 	unsigned long delay_j = msecs_to_jiffies(OXID_RECLAIM_TOV(iport));
315 	unsigned long flags;
316 	int idx;
317 
318 	for_each_set_bit(idx, oxid_pool->pending_schedule_free, FNIC_OXID_POOL_SZ) {
319 
320 		FNIC_FCS_DBG(KERN_INFO, fnic,
321 			"Schedule oxid free. oxid idx: %d\n", idx);
322 
323 		reclaim_entry = kzalloc_obj(*reclaim_entry);
324 		if (!reclaim_entry) {
325 			schedule_delayed_work(&oxid_pool->schedule_oxid_free_retry,
326 				msecs_to_jiffies(SCHEDULE_OXID_FREE_RETRY_TIME));
327 			return;
328 		}
329 
330 		reclaim_entry->oxid_idx = idx;
331 		reclaim_entry->expires = round_jiffies(jiffies + delay_j);
332 
333 		spin_lock_irqsave(&fnic->fnic_lock, flags);
334 		/* Reset may have cleared this bit while this worker allocated. */
335 		if (!test_and_clear_bit(idx, oxid_pool->pending_schedule_free)) {
336 			spin_unlock_irqrestore(&fnic->fnic_lock, flags);
337 			kfree(reclaim_entry);
338 			continue;
339 		}
340 		list_add_tail(&reclaim_entry->links, &oxid_pool->oxid_reclaim_list);
341 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
342 
343 		schedule_delayed_work(&oxid_pool->oxid_reclaim_work, delay_j);
344 	}
345 }
346 
347 static bool fdls_is_oxid_fabric_req(uint16_t oxid)
348 {
349 	int oxid_frame_type = FNIC_FRAME_TYPE(oxid);
350 
351 	switch (oxid_frame_type) {
352 	case FNIC_FRAME_TYPE_FABRIC_FLOGI:
353 	case FNIC_FRAME_TYPE_FABRIC_PLOGI:
354 	case FNIC_FRAME_TYPE_FABRIC_RPN:
355 	case FNIC_FRAME_TYPE_FABRIC_RFT:
356 	case FNIC_FRAME_TYPE_FABRIC_RFF:
357 	case FNIC_FRAME_TYPE_FABRIC_GPN_FT:
358 	case FNIC_FRAME_TYPE_FABRIC_LOGO:
359 		break;
360 	default:
361 		return false;
362 	}
363 	return true;
364 }
365 
366 static bool fdls_is_oxid_fdmi_req(uint16_t oxid)
367 {
368 	int oxid_frame_type = FNIC_FRAME_TYPE(oxid);
369 
370 	switch (oxid_frame_type) {
371 	case FNIC_FRAME_TYPE_FDMI_PLOGI:
372 	case FNIC_FRAME_TYPE_FDMI_RHBA:
373 	case FNIC_FRAME_TYPE_FDMI_RPA:
374 		break;
375 	default:
376 		return false;
377 	}
378 	return true;
379 }
380 
381 static bool fdls_is_oxid_tgt_req(uint16_t oxid)
382 {
383 	int oxid_frame_type = FNIC_FRAME_TYPE(oxid);
384 
385 	switch (oxid_frame_type) {
386 	case FNIC_FRAME_TYPE_TGT_PLOGI:
387 	case FNIC_FRAME_TYPE_TGT_PRLI:
388 	case FNIC_FRAME_TYPE_TGT_ADISC:
389 	case FNIC_FRAME_TYPE_TGT_LOGO:
390 		break;
391 	default:
392 		return false;
393 	}
394 	return true;
395 }
396 
397 static inline bool fdls_is_oxid_nvme_req(uint16_t oxid)
398 {
399 	return FNIC_FRAME_TYPE(oxid) == FNIC_FRAME_TYPE_NVME_LS;
400 }
401 
402 static void fdls_reset_oxid_pool(struct fnic_iport_s *iport)
403 {
404 	struct fnic_oxid_pool_s *oxid_pool = &iport->oxid_pool;
405 	struct reclaim_entry_s *reclaim_entry, *next;
406 
407 	cancel_delayed_work(&oxid_pool->oxid_reclaim_work);
408 	cancel_delayed_work(&oxid_pool->schedule_oxid_free_retry);
409 	list_for_each_entry_safe(reclaim_entry, next,
410 				 &oxid_pool->oxid_reclaim_list, links) {
411 		list_del(&reclaim_entry->links);
412 		kfree(reclaim_entry);
413 	}
414 	bitmap_clear(oxid_pool->pending_schedule_free, 0, FNIC_OXID_POOL_SZ);
415 	bitmap_clear(oxid_pool->bitmap, 0, FNIC_OXID_POOL_SZ);
416 	oxid_pool->next_idx = 0;
417 }
418 
419 void fnic_del_fabric_timer_sync(struct fnic *fnic)
420 {
421 	fnic->iport.fabric.del_timer_inprogress = 1;
422 	spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
423 	timer_delete_sync(&fnic->iport.fabric.retry_timer);
424 	spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
425 	fnic->iport.fabric.del_timer_inprogress = 0;
426 }
427 
428 void fnic_del_tport_timer_sync(struct fnic *fnic,
429 						struct fnic_tport_s *tport)
430 {
431 	tport->del_timer_inprogress = 1;
432 	spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
433 	timer_delete_sync(&tport->retry_timer);
434 	spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
435 	tport->del_timer_inprogress = 0;
436 }
437 
438 static void
439 fdls_start_fabric_timer(struct fnic_iport_s *iport, int timeout)
440 {
441 	u64 fabric_tov;
442 	struct fnic *fnic = iport->fnic;
443 
444 	if (iport->fabric.timer_pending) {
445 		FNIC_FCS_DBG(KERN_INFO, fnic,
446 					 "iport fcid: 0x%x: Canceling fabric disc timer\n",
447 					 iport->fcid);
448 		fnic_del_fabric_timer_sync(fnic);
449 		iport->fabric.timer_pending = 0;
450 	}
451 
452 	if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED))
453 		iport->fabric.retry_counter++;
454 
455 	fabric_tov = jiffies + msecs_to_jiffies(timeout);
456 	mod_timer(&iport->fabric.retry_timer, round_jiffies(fabric_tov));
457 	iport->fabric.timer_pending = 1;
458 	FNIC_FCS_DBG(KERN_INFO, fnic,
459 				 "fabric timer is %d ", timeout);
460 }
461 
462 static void
463 fdls_start_tport_timer(struct fnic_iport_s *iport,
464 					   struct fnic_tport_s *tport, int timeout)
465 {
466 	u64 fabric_tov;
467 	struct fnic *fnic = iport->fnic;
468 
469 	if (tport->timer_pending) {
470 		FNIC_FCS_DBG(KERN_INFO, fnic,
471 					 "tport fcid 0x%x: Canceling disc timer\n",
472 					 tport->fcid);
473 		fnic_del_tport_timer_sync(fnic, tport);
474 		tport->timer_pending = 0;
475 	}
476 
477 	if (!(tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED))
478 		tport->retry_counter++;
479 
480 	fabric_tov = jiffies + msecs_to_jiffies(timeout);
481 	mod_timer(&tport->retry_timer, round_jiffies(fabric_tov));
482 	tport->timer_pending = 1;
483 }
484 
485 void fdls_init_plogi_frame(uint8_t *frame,
486 		struct fnic_iport_s *iport)
487 {
488 	struct fc_std_flogi *pplogi;
489 	uint8_t s_id[3];
490 
491 	pplogi = (struct fc_std_flogi *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
492 	*pplogi = (struct fc_std_flogi) {
493 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REQ, .fh_d_id = {0xFF, 0xFF, 0xFC},
494 		      .fh_type = FC_TYPE_ELS, .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
495 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
496 		.els = {
497 		    .fl_cmd = ELS_PLOGI,
498 		    .fl_csp = {.sp_hi_ver = FNIC_FC_PH_VER_HI,
499 			       .sp_lo_ver = FNIC_FC_PH_VER_LO,
500 			       .sp_bb_cred = cpu_to_be16(FNIC_FC_B2B_CREDIT),
501 			       .sp_features = cpu_to_be16(FC_SP_FT_CIRO),
502 			       .sp_bb_data = cpu_to_be16(FNIC_FC_B2B_RDF_SZ),
503 			       .sp_tot_seq = cpu_to_be16(FNIC_FC_CONCUR_SEQS),
504 			       .sp_rel_off = cpu_to_be16(FNIC_FC_RO_INFO),
505 			       .sp_e_d_tov = cpu_to_be32(FC_DEF_E_D_TOV)},
506 		    .fl_cssp[2].cp_class = cpu_to_be16(FC_CPC_VALID | FC_CPC_SEQ),
507 		    .fl_cssp[2].cp_rdfs = cpu_to_be16(0x800),
508 		    .fl_cssp[2].cp_con_seq = cpu_to_be16(0xFF),
509 		    .fl_cssp[2].cp_open_seq = 1}
510 	};
511 
512 	FNIC_STD_SET_NPORT_NAME(&pplogi->els.fl_wwpn, iport->wwpn);
513 	FNIC_STD_SET_NODE_NAME(&pplogi->els.fl_wwnn, iport->wwnn);
514 	FNIC_LOGI_SET_RDF_SIZE(pplogi->els, iport->max_payload_size);
515 
516 	hton24(s_id, iport->fcid);
517 	FNIC_STD_SET_S_ID(pplogi->fchdr, s_id);
518 }
519 
520 static void fdls_init_els_acc_frame(uint8_t *frame,
521 		struct fnic_iport_s *iport)
522 {
523 	struct fc_std_els_acc_rsp *pels_acc;
524 	uint8_t s_id[3];
525 
526 	pels_acc = (struct fc_std_els_acc_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
527 	*pels_acc = (struct fc_std_els_acc_rsp) {
528 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REP,
529 			  .fh_type = FC_TYPE_ELS, .fh_f_ctl = {FNIC_ELS_REP_FCTL, 0, 0}},
530 		.acc.la_cmd = ELS_LS_ACC,
531 	};
532 
533 	hton24(s_id, iport->fcid);
534 	FNIC_STD_SET_S_ID(pels_acc->fchdr, s_id);
535 	FNIC_STD_SET_RX_ID(pels_acc->fchdr, FNIC_UNASSIGNED_RXID);
536 }
537 
538 static void fdls_init_els_rjt_frame(uint8_t *frame,
539 		struct fnic_iport_s *iport)
540 {
541 	struct fc_std_els_rjt_rsp *pels_rjt;
542 
543 	pels_rjt = (struct fc_std_els_rjt_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
544 	*pels_rjt = (struct fc_std_els_rjt_rsp) {
545 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REP, .fh_type = FC_TYPE_ELS,
546 			  .fh_f_ctl = {FNIC_ELS_REP_FCTL, 0, 0}},
547 		.rej.er_cmd = ELS_LS_RJT,
548 	};
549 
550 	FNIC_STD_SET_RX_ID(pels_rjt->fchdr, FNIC_UNASSIGNED_RXID);
551 }
552 
553 static void fdls_init_logo_frame(uint8_t *frame,
554 		struct fnic_iport_s *iport)
555 {
556 	struct fc_std_logo *plogo;
557 	uint8_t s_id[3];
558 
559 	plogo = (struct fc_std_logo *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
560 	*plogo = (struct fc_std_logo) {
561 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REQ, .fh_type = FC_TYPE_ELS,
562 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0}},
563 		.els.fl_cmd = ELS_LOGO,
564 	};
565 
566 	hton24(s_id, iport->fcid);
567 	FNIC_STD_SET_S_ID(plogo->fchdr, s_id);
568 	memcpy(plogo->els.fl_n_port_id, s_id, 3);
569 
570 	FNIC_STD_SET_NPORT_NAME(&plogo->els.fl_n_port_wwn,
571 			    iport->wwpn);
572 }
573 
574 static void fdls_init_fabric_abts_frame(uint8_t *frame,
575 		struct fnic_iport_s *iport)
576 {
577 	struct fc_frame_header *pfabric_abts;
578 
579 	pfabric_abts = (struct fc_frame_header *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
580 	*pfabric_abts = (struct fc_frame_header) {
581 		.fh_r_ctl = FC_RCTL_BA_ABTS,	/* ABTS */
582 		.fh_s_id = {0x00, 0x00, 0x00},
583 		.fh_cs_ctl = 0x00, .fh_type = FC_TYPE_BLS,
584 		.fh_f_ctl = {FNIC_REQ_ABTS_FCTL, 0, 0}, .fh_seq_id = 0x00,
585 		.fh_df_ctl = 0x00, .fh_seq_cnt = 0x0000,
586 		.fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID),
587 		.fh_parm_offset = 0x00000000,	/* bit:0 = 0 Abort a exchange */
588 	};
589 }
590 
591 static void fdls_set_frame_type(struct fnic *fnic, uint8_t *frame_type)
592 {
593 	if (IS_FNIC_FCP_INITIATOR(fnic))
594 		*frame_type = FC_TYPE_FCP;
595 	else if (IS_FNIC_NVME_INITIATOR(fnic))
596 		*frame_type = FC_TYPE_NVME;
597 }
598 
599 static void fdls_set_feature_type(struct fnic *fnic, uint8_t *feature_type)
600 {
601 	if (IS_FNIC_FCP_INITIATOR(fnic))
602 		*feature_type = FCP_FEAT_INIT;
603 	else if (IS_FNIC_NVME_INITIATOR(fnic))
604 		*feature_type = FNIC_NVME_FEAT_INIT;
605 }
606 
607 static void
608 fdls_send_rscn_resp(struct fnic_iport_s *iport,
609 		    struct fc_frame_header *rscn_fchdr)
610 {
611 	uint8_t *frame;
612 	struct fc_std_els_acc_rsp *pels_acc;
613 	struct fnic *fnic = iport->fnic;
614 	uint16_t oxid;
615 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
616 			sizeof(struct fc_std_els_acc_rsp);
617 
618 	frame = fdls_alloc_frame(iport);
619 	if (frame == NULL) {
620 		FNIC_FCS_DBG(KERN_ERR, fnic,
621 				"Failed to allocate frame to send RSCN response");
622 		return;
623 	}
624 
625 	pels_acc = (struct fc_std_els_acc_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
626 	fdls_init_els_acc_frame(frame, iport);
627 
628 	FNIC_STD_SET_D_ID(pels_acc->fchdr, rscn_fchdr->fh_s_id);
629 
630 	oxid = FNIC_STD_GET_OX_ID(rscn_fchdr);
631 	FNIC_STD_SET_OX_ID(pels_acc->fchdr, oxid);
632 
633 	FNIC_FCS_DBG(KERN_INFO, fnic,
634 		 "0x%x: FDLS send RSCN response with oxid: 0x%x",
635 		 iport->fcid, oxid);
636 
637 	fnic_send_fcoe_frame(iport, frame, frame_size);
638 }
639 
640 static void
641 fdls_send_logo_resp(struct fnic_iport_s *iport,
642 		    struct fc_frame_header *req_fchdr)
643 {
644 	uint8_t *frame;
645 	struct fc_std_els_acc_rsp *plogo_resp;
646 	struct fnic *fnic = iport->fnic;
647 	uint16_t oxid;
648 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
649 			sizeof(struct fc_std_els_acc_rsp);
650 
651 	frame = fdls_alloc_frame(iport);
652 	if (frame == NULL) {
653 		FNIC_FCS_DBG(KERN_ERR, fnic,
654 				"Failed to allocate frame to send LOGO response");
655 		return;
656 	}
657 
658 	plogo_resp = (struct fc_std_els_acc_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
659 	fdls_init_els_acc_frame(frame, iport);
660 
661 	FNIC_STD_SET_D_ID(plogo_resp->fchdr, req_fchdr->fh_s_id);
662 
663 	oxid = FNIC_STD_GET_OX_ID(req_fchdr);
664 	FNIC_STD_SET_OX_ID(plogo_resp->fchdr, oxid);
665 
666 	FNIC_FCS_DBG(KERN_INFO, fnic,
667 		 "0x%x: FDLS send LOGO response with oxid: 0x%x",
668 		 iport->fcid, oxid);
669 
670 	fnic_send_fcoe_frame(iport, frame, frame_size);
671 }
672 
673 int fdls_send_ls_req_abts(struct fnic_iport_s *iport,
674 		struct fnic_tport_s *tport, unsigned int oxid)
675 {
676 	uint8_t *frame;
677 	uint8_t s_id[3];
678 	uint8_t d_id[3];
679 	struct fnic *fnic = iport->fnic;
680 	struct fc_frame_header *pls_req_abts;
681 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
682 			sizeof(struct fc_frame_header);
683 	int ret;
684 
685 	frame = fdls_alloc_frame(iport);
686 	if (frame == NULL) {
687 		FNIC_FCS_DBG(KERN_ERR, fnic,
688 				"Failed to allocate frame to send ls req ABTS");
689 		return -ENOMEM;
690 	}
691 
692 	pls_req_abts = (struct fc_frame_header *) (frame +
693 			FNIC_ETH_FCOE_HDRS_OFFSET);
694 	fdls_init_fabric_abts_frame(frame, iport);
695 
696 	hton24(s_id, iport->fcid);
697 	hton24(d_id, tport->fcid);
698 	FNIC_STD_SET_S_ID(*pls_req_abts, s_id);
699 	FNIC_STD_SET_D_ID(*pls_req_abts, d_id);
700 
701 	FNIC_STD_SET_OX_ID(*pls_req_abts, oxid);
702 
703 	FNIC_FCS_DBG(KERN_INFO, fnic,
704 		"iport 0x%x: tport: 0x%x FDLS sending ls req abts with oxid: 0x%x",
705 		iport->fcid, tport->fcid, oxid);
706 
707 	ret = fnic_send_fcoe_frame(iport, frame, frame_size);
708 	if (ret) {
709 		mempool_free(frame, fnic->frame_pool);
710 		return ret;
711 	}
712 
713 	return 0;
714 }
715 
716 
717 void
718 fdls_send_tport_abts(struct fnic_iport_s *iport,
719 					 struct fnic_tport_s *tport)
720 {
721 	uint8_t *frame;
722 	uint8_t s_id[3];
723 	uint8_t d_id[3];
724 	struct fnic *fnic = iport->fnic;
725 	struct fc_frame_header *ptport_abts;
726 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
727 			sizeof(struct fc_frame_header);
728 
729 	frame = fdls_alloc_frame(iport);
730 	if (frame == NULL) {
731 		FNIC_FCS_DBG(KERN_ERR, fnic,
732 				"Failed to allocate frame to send tport ABTS");
733 		return;
734 	}
735 
736 	ptport_abts = (struct fc_frame_header *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
737 	*ptport_abts = (struct fc_frame_header) {
738 		.fh_r_ctl = FC_RCTL_BA_ABTS,	/* ABTS */
739 		.fh_cs_ctl = 0x00, .fh_type = FC_TYPE_BLS,
740 		.fh_f_ctl = {FNIC_REQ_ABTS_FCTL, 0, 0}, .fh_seq_id = 0x00,
741 		.fh_df_ctl = 0x00, .fh_seq_cnt = 0x0000,
742 		.fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID),
743 		.fh_parm_offset = 0x00000000,	/* bit:0 = 0 Abort a exchange */
744 	};
745 
746 	hton24(s_id, iport->fcid);
747 	hton24(d_id, tport->fcid);
748 	FNIC_STD_SET_S_ID(*ptport_abts, s_id);
749 	FNIC_STD_SET_D_ID(*ptport_abts, d_id);
750 	tport->flags |= FNIC_FDLS_TGT_ABORT_ISSUED;
751 
752 	FNIC_STD_SET_OX_ID(*ptport_abts, tport->active_oxid);
753 
754 	FNIC_FCS_DBG(KERN_INFO, fnic,
755 				 "0x%x: FDLS send tport abts: tport->state: %d ",
756 				 iport->fcid, tport->state);
757 
758 	fnic_send_fcoe_frame(iport, frame, frame_size);
759 
760 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
761 	fdls_start_tport_timer(iport, tport, 2 * iport->e_d_tov);
762 }
763 static void fdls_send_fabric_abts(struct fnic_iport_s *iport)
764 {
765 	uint8_t *frame;
766 	uint8_t s_id[3];
767 	uint8_t d_id[3];
768 	struct fnic *fnic = iport->fnic;
769 	struct fc_frame_header *pfabric_abts;
770 	uint16_t oxid;
771 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
772 			sizeof(struct fc_frame_header);
773 
774 	frame = fdls_alloc_frame(iport);
775 	if (frame == NULL) {
776 		FNIC_FCS_DBG(KERN_ERR, fnic,
777 				"Failed to allocate frame to send fabric ABTS");
778 		return;
779 	}
780 
781 	pfabric_abts = (struct fc_frame_header *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
782 	fdls_init_fabric_abts_frame(frame, iport);
783 
784 	hton24(s_id, iport->fcid);
785 
786 	switch (iport->fabric.state) {
787 	case FDLS_STATE_FABRIC_LOGO:
788 		hton24(d_id, FC_FID_FLOGI);
789 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
790 		break;
791 
792 	case FDLS_STATE_FABRIC_FLOGI:
793 		hton24(d_id, FC_FID_FLOGI);
794 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
795 		break;
796 
797 	case FDLS_STATE_FABRIC_PLOGI:
798 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
799 		hton24(d_id, FC_FID_DIR_SERV);
800 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
801 		break;
802 
803 	case FDLS_STATE_RPN_ID:
804 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
805 		hton24(d_id, FC_FID_DIR_SERV);
806 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
807 		break;
808 
809 	case FDLS_STATE_SCR:
810 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
811 		hton24(d_id, FC_FID_FCTRL);
812 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
813 		break;
814 
815 	case FDLS_STATE_REGISTER_FC4_TYPES:
816 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
817 		hton24(d_id, FC_FID_DIR_SERV);
818 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
819 		break;
820 
821 	case FDLS_STATE_REGISTER_FC4_FEATURES:
822 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
823 		hton24(d_id, FC_FID_DIR_SERV);
824 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
825 		break;
826 
827 	case FDLS_STATE_GPN_FT:
828 		FNIC_STD_SET_S_ID(*pfabric_abts, s_id);
829 		hton24(d_id, FC_FID_DIR_SERV);
830 		FNIC_STD_SET_D_ID(*pfabric_abts, d_id);
831 		break;
832 	default:
833 		return;
834 	}
835 
836 	oxid = iport->active_oxid_fabric_req;
837 	FNIC_STD_SET_OX_ID(*pfabric_abts, oxid);
838 
839 	FNIC_FCS_DBG(KERN_INFO, fnic,
840 		 "0x%x: FDLS send fabric abts. iport->fabric.state: %d oxid: 0x%x",
841 		 iport->fcid, iport->fabric.state, oxid);
842 
843 	iport->fabric.flags |= FNIC_FDLS_FABRIC_ABORT_ISSUED;
844 
845 	fnic_send_fcoe_frame(iport, frame, frame_size);
846 
847 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
848 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
849 	iport->fabric.timer_pending = 1;
850 }
851 
852 static uint8_t *fdls_alloc_init_fdmi_abts_frame(struct fnic_iport_s *iport,
853 		uint16_t oxid)
854 {
855 	struct fc_frame_header *pfdmi_abts;
856 	uint8_t d_id[3];
857 	uint8_t *frame;
858 	struct fnic *fnic = iport->fnic;
859 
860 	frame = fdls_alloc_frame(iport);
861 	if (frame == NULL) {
862 		FNIC_FCS_DBG(KERN_ERR, fnic,
863 				"Failed to allocate frame to send FDMI ABTS");
864 		return NULL;
865 	}
866 
867 	pfdmi_abts = (struct fc_frame_header *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
868 	fdls_init_fabric_abts_frame(frame, iport);
869 
870 	hton24(d_id, FC_FID_MGMT_SERV);
871 	FNIC_STD_SET_D_ID(*pfdmi_abts, d_id);
872 	FNIC_STD_SET_OX_ID(*pfdmi_abts, oxid);
873 
874 	return frame;
875 }
876 
877 static void fdls_send_fdmi_abts(struct fnic_iport_s *iport)
878 {
879 	uint8_t *frame;
880 	struct fnic *fnic = iport->fnic;
881 	unsigned long fdmi_tov;
882 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
883 			sizeof(struct fc_frame_header);
884 
885 	if (iport->fabric.fdmi_pending & FDLS_FDMI_PLOGI_PENDING) {
886 		frame = fdls_alloc_init_fdmi_abts_frame(iport,
887 						iport->active_oxid_fdmi_plogi);
888 		if (frame == NULL)
889 			return;
890 
891 		FNIC_FCS_DBG(KERN_INFO, fnic,
892 			 "0x%x: FDLS send FDMI PLOGI abts. iport->fabric.state: %d oxid: 0x%x",
893 			 iport->fcid, iport->fabric.state, iport->active_oxid_fdmi_plogi);
894 		fnic_send_fcoe_frame(iport, frame, frame_size);
895 	} else {
896 		if (iport->fabric.fdmi_pending & FDLS_FDMI_REG_HBA_PENDING) {
897 			frame = fdls_alloc_init_fdmi_abts_frame(iport,
898 						iport->active_oxid_fdmi_rhba);
899 			if (frame == NULL)
900 				return;
901 
902 			FNIC_FCS_DBG(KERN_INFO, fnic,
903 				 "0x%x: FDLS send FDMI RHBA abts. iport->fabric.state: %d oxid: 0x%x",
904 				 iport->fcid, iport->fabric.state, iport->active_oxid_fdmi_rhba);
905 			fnic_send_fcoe_frame(iport, frame, frame_size);
906 		}
907 		if (iport->fabric.fdmi_pending & FDLS_FDMI_RPA_PENDING) {
908 			frame = fdls_alloc_init_fdmi_abts_frame(iport,
909 						iport->active_oxid_fdmi_rpa);
910 			if (frame == NULL) {
911 				if (iport->fabric.fdmi_pending & FDLS_FDMI_REG_HBA_PENDING)
912 					goto arm_timer;
913 				else
914 					return;
915 			}
916 
917 			FNIC_FCS_DBG(KERN_INFO, fnic,
918 				 "0x%x: FDLS send FDMI RPA abts. iport->fabric.state: %d oxid: 0x%x",
919 				 iport->fcid, iport->fabric.state, iport->active_oxid_fdmi_rpa);
920 			fnic_send_fcoe_frame(iport, frame, frame_size);
921 		}
922 	}
923 
924 arm_timer:
925 	fdmi_tov = jiffies + msecs_to_jiffies(2 * iport->e_d_tov);
926 	mod_timer(&iport->fabric.fdmi_timer, round_jiffies(fdmi_tov));
927 	iport->fabric.fdmi_pending |= FDLS_FDMI_ABORT_PENDING;
928 
929 	FNIC_FCS_DBG(KERN_INFO, fnic,
930 		 "0x%x: iport->fabric.fdmi_pending: 0x%x",
931 		 iport->fcid, iport->fabric.fdmi_pending);
932 }
933 
934 static void fdls_send_fabric_flogi(struct fnic_iport_s *iport)
935 {
936 	uint8_t *frame;
937 	struct fc_std_flogi *pflogi;
938 	struct fnic *fnic = iport->fnic;
939 	uint16_t oxid;
940 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
941 			sizeof(struct fc_std_flogi);
942 
943 	frame = fdls_alloc_frame(iport);
944 	if (frame == NULL) {
945 		FNIC_FCS_DBG(KERN_ERR, fnic,
946 		     "Failed to allocate frame to send FLOGI");
947 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
948 		goto err_out;
949 	}
950 
951 	pflogi = (struct fc_std_flogi *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
952 	*pflogi = (struct fc_std_flogi) {
953 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REQ, .fh_d_id = {0xFF, 0xFF, 0xFE},
954 		      .fh_type = FC_TYPE_ELS, .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
955 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
956 		.els.fl_cmd = ELS_FLOGI,
957 		.els.fl_csp = {.sp_hi_ver = FNIC_FC_PH_VER_HI,
958 			   .sp_lo_ver = FNIC_FC_PH_VER_LO,
959 			   .sp_bb_cred = cpu_to_be16(FNIC_FC_B2B_CREDIT),
960 			   .sp_bb_data = cpu_to_be16(FNIC_FC_B2B_RDF_SZ)},
961 		.els.fl_cssp[2].cp_class = cpu_to_be16(FC_CPC_VALID | FC_CPC_SEQ)
962 	};
963 
964 	FNIC_STD_SET_NPORT_NAME(&pflogi->els.fl_wwpn, iport->wwpn);
965 	FNIC_STD_SET_NODE_NAME(&pflogi->els.fl_wwnn, iport->wwnn);
966 	FNIC_LOGI_SET_RDF_SIZE(pflogi->els, iport->max_payload_size);
967 	FNIC_LOGI_SET_R_A_TOV(pflogi->els, iport->r_a_tov);
968 	FNIC_LOGI_SET_E_D_TOV(pflogi->els, iport->e_d_tov);
969 
970 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_FLOGI,
971 		&iport->active_oxid_fabric_req);
972 
973 	if (oxid == FNIC_UNASSIGNED_OXID) {
974 		FNIC_FCS_DBG(KERN_INFO, fnic,
975 		     "0x%x: Failed to allocate OXID to send FLOGI",
976 			 iport->fcid);
977 		mempool_free(frame, fnic->frame_pool);
978 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
979 		goto err_out;
980 	}
981 	FNIC_STD_SET_OX_ID(pflogi->fchdr, oxid);
982 
983 	FNIC_FCS_DBG(KERN_INFO, fnic,
984 		 "0x%x: FDLS send fabric FLOGI with oxid: 0x%x", iport->fcid,
985 		 oxid);
986 
987 	fnic_send_fcoe_frame(iport, frame, frame_size);
988 	atomic64_inc(&iport->iport_stats.fabric_flogi_sent);
989 err_out:
990 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
991 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
992 }
993 
994 static void fdls_send_fabric_plogi(struct fnic_iport_s *iport)
995 {
996 	uint8_t *frame;
997 	struct fc_std_flogi *pplogi;
998 	struct fnic *fnic = iport->fnic;
999 	uint16_t oxid;
1000 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1001 			sizeof(struct fc_std_flogi);
1002 
1003 	frame = fdls_alloc_frame(iport);
1004 	if (frame == NULL) {
1005 		FNIC_FCS_DBG(KERN_ERR, fnic,
1006 		     "Failed to allocate frame to send PLOGI");
1007 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1008 		goto err_out;
1009 	}
1010 
1011 	pplogi = (struct fc_std_flogi *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1012 	fdls_init_plogi_frame(frame, iport);
1013 
1014 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_PLOGI,
1015 		&iport->active_oxid_fabric_req);
1016 	if (oxid == FNIC_UNASSIGNED_OXID) {
1017 		FNIC_FCS_DBG(KERN_INFO, fnic,
1018 		     "0x%x: Failed to allocate OXID to send fabric PLOGI",
1019 			 iport->fcid);
1020 		mempool_free(frame, fnic->frame_pool);
1021 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1022 		goto err_out;
1023 	}
1024 	FNIC_STD_SET_OX_ID(pplogi->fchdr, oxid);
1025 
1026 	FNIC_FCS_DBG(KERN_INFO, fnic,
1027 		 "0x%x: FDLS send fabric PLOGI with oxid: 0x%x", iport->fcid,
1028 		 oxid);
1029 
1030 	fnic_send_fcoe_frame(iport, frame, frame_size);
1031 	atomic64_inc(&iport->iport_stats.fabric_plogi_sent);
1032 
1033 err_out:
1034 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1035 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1036 }
1037 
1038 static void fdls_send_fdmi_plogi(struct fnic_iport_s *iport)
1039 {
1040 	uint8_t *frame;
1041 	struct fc_std_flogi *pplogi;
1042 	struct fnic *fnic = iport->fnic;
1043 	uint16_t oxid;
1044 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1045 			sizeof(struct fc_std_flogi);
1046 	uint8_t d_id[3];
1047 	u64 fdmi_tov;
1048 
1049 	frame = fdls_alloc_frame(iport);
1050 	if (frame == NULL) {
1051 		FNIC_FCS_DBG(KERN_ERR, fnic,
1052 		     "Failed to allocate frame to send FDMI PLOGI");
1053 		goto err_out;
1054 	}
1055 
1056 	pplogi = (struct fc_std_flogi *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1057 	fdls_init_plogi_frame(frame, iport);
1058 
1059 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FDMI_PLOGI,
1060 		&iport->active_oxid_fdmi_plogi);
1061 
1062 	if (oxid == FNIC_UNASSIGNED_OXID) {
1063 		FNIC_FCS_DBG(KERN_INFO, fnic,
1064 			     "0x%x: Failed to allocate OXID to send FDMI PLOGI",
1065 			     iport->fcid);
1066 		mempool_free(frame, fnic->frame_pool);
1067 		goto err_out;
1068 	}
1069 	FNIC_STD_SET_OX_ID(pplogi->fchdr, oxid);
1070 
1071 	hton24(d_id, FC_FID_MGMT_SERV);
1072 	FNIC_STD_SET_D_ID(pplogi->fchdr, d_id);
1073 
1074 	FNIC_FCS_DBG(KERN_INFO, fnic,
1075 		     "0x%x: FDLS send FDMI PLOGI with oxid: 0x%x",
1076 		     iport->fcid, oxid);
1077 
1078 	fnic_send_fcoe_frame(iport, frame, frame_size);
1079 
1080 err_out:
1081 	fdmi_tov = jiffies + msecs_to_jiffies(2 * iport->e_d_tov);
1082 	mod_timer(&iport->fabric.fdmi_timer, round_jiffies(fdmi_tov));
1083 	iport->fabric.fdmi_pending = FDLS_FDMI_PLOGI_PENDING;
1084 }
1085 
1086 static void fdls_send_rpn_id(struct fnic_iport_s *iport)
1087 {
1088 	uint8_t *frame;
1089 	struct fc_std_rpn_id *prpn_id;
1090 	struct fnic *fnic = iport->fnic;
1091 	uint16_t oxid;
1092 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1093 			sizeof(struct fc_std_rpn_id);
1094 	uint8_t fcid[3];
1095 
1096 	frame = fdls_alloc_frame(iport);
1097 	if (frame == NULL) {
1098 		FNIC_FCS_DBG(KERN_ERR, fnic,
1099 		     "Failed to allocate frame to send RPN_ID");
1100 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1101 		goto err_out;
1102 	}
1103 
1104 	prpn_id = (struct fc_std_rpn_id *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1105 	*prpn_id = (struct fc_std_rpn_id) {
1106 		.fchdr = {.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
1107 		      .fh_d_id = {0xFF, 0xFF, 0xFC}, .fh_type = FC_TYPE_CT,
1108 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1109 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1110 		.fc_std_ct_hdr = {.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_DIR,
1111 			      .ct_fs_subtype = FC_NS_SUBTYPE,
1112 			      .ct_cmd = cpu_to_be16(FC_NS_RPN_ID)}
1113 	};
1114 
1115 	hton24(fcid, iport->fcid);
1116 	FNIC_STD_SET_S_ID(prpn_id->fchdr, fcid);
1117 
1118 	FNIC_STD_SET_PORT_ID(prpn_id->rpn_id, fcid);
1119 	FNIC_STD_SET_PORT_NAME(prpn_id->rpn_id, iport->wwpn);
1120 
1121 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_RPN,
1122 		&iport->active_oxid_fabric_req);
1123 
1124 	if (oxid == FNIC_UNASSIGNED_OXID) {
1125 		FNIC_FCS_DBG(KERN_INFO, fnic,
1126 		     "0x%x: Failed to allocate OXID to send RPN_ID",
1127 			 iport->fcid);
1128 		mempool_free(frame, fnic->frame_pool);
1129 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1130 		goto err_out;
1131 	}
1132 	FNIC_STD_SET_OX_ID(prpn_id->fchdr, oxid);
1133 
1134 	FNIC_FCS_DBG(KERN_INFO, fnic,
1135 		 "0x%x: FDLS send RPN ID with oxid: 0x%x", iport->fcid,
1136 		 oxid);
1137 
1138 	fnic_send_fcoe_frame(iport, frame, frame_size);
1139 
1140 err_out:
1141 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1142 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1143 }
1144 
1145 static void fdls_send_scr(struct fnic_iport_s *iport)
1146 {
1147 	uint8_t *frame;
1148 	struct fc_std_scr *pscr;
1149 	struct fnic *fnic = iport->fnic;
1150 	uint16_t oxid;
1151 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1152 			sizeof(struct fc_std_scr);
1153 	uint8_t fcid[3];
1154 
1155 	frame = fdls_alloc_frame(iport);
1156 	if (frame == NULL) {
1157 		FNIC_FCS_DBG(KERN_ERR, fnic,
1158 		     "Failed to allocate frame to send SCR");
1159 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1160 		goto err_out;
1161 	}
1162 
1163 	pscr = (struct fc_std_scr *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1164 	*pscr = (struct fc_std_scr) {
1165 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REQ,
1166 		      .fh_d_id = {0xFF, 0xFF, 0xFD}, .fh_type = FC_TYPE_ELS,
1167 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1168 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1169 		.scr = {.scr_cmd = ELS_SCR,
1170 		    .scr_reg_func = ELS_SCRF_FULL}
1171 	};
1172 
1173 	hton24(fcid, iport->fcid);
1174 	FNIC_STD_SET_S_ID(pscr->fchdr, fcid);
1175 
1176 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_SCR,
1177 		&iport->active_oxid_fabric_req);
1178 	if (oxid == FNIC_UNASSIGNED_OXID) {
1179 		FNIC_FCS_DBG(KERN_INFO, fnic,
1180 		     "0x%x: Failed to allocate OXID to send SCR",
1181 			 iport->fcid);
1182 		mempool_free(frame, fnic->frame_pool);
1183 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1184 		goto err_out;
1185 	}
1186 	FNIC_STD_SET_OX_ID(pscr->fchdr, oxid);
1187 
1188 	FNIC_FCS_DBG(KERN_INFO, fnic,
1189 		 "0x%x: FDLS send SCR with oxid: 0x%x", iport->fcid,
1190 		 oxid);
1191 
1192 	fnic_send_fcoe_frame(iport, frame, frame_size);
1193 	atomic64_inc(&iport->iport_stats.fabric_scr_sent);
1194 
1195 err_out:
1196 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1197 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1198 }
1199 
1200 static void fdls_send_gpn_ft(struct fnic_iport_s *iport, int fdls_state)
1201 {
1202 	uint8_t *frame;
1203 	struct fc_std_gpn_ft *pgpn_ft;
1204 	struct fnic *fnic = iport->fnic;
1205 	uint16_t oxid;
1206 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1207 			sizeof(struct fc_std_gpn_ft);
1208 	uint8_t fcid[3];
1209 
1210 	frame = fdls_alloc_frame(iport);
1211 	if (frame == NULL) {
1212 		FNIC_FCS_DBG(KERN_ERR, fnic,
1213 		     "Failed to allocate frame to send GPN FT");
1214 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1215 		goto err_out;
1216 	}
1217 
1218 	pgpn_ft = (struct fc_std_gpn_ft *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1219 	*pgpn_ft = (struct fc_std_gpn_ft) {
1220 		.fchdr = {.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
1221 		      .fh_d_id = {0xFF, 0xFF, 0xFC}, .fh_type = FC_TYPE_CT,
1222 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1223 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1224 		.fc_std_ct_hdr = {.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_DIR,
1225 			      .ct_fs_subtype = FC_NS_SUBTYPE,
1226 			      .ct_cmd = cpu_to_be16(FC_NS_GPN_FT)}
1227 	};
1228 
1229 	fdls_set_frame_type(fnic, &pgpn_ft->gpn_ft.fn_fc4_type);
1230 	hton24(fcid, iport->fcid);
1231 	FNIC_STD_SET_S_ID(pgpn_ft->fchdr, fcid);
1232 
1233 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_GPN_FT,
1234 		&iport->active_oxid_fabric_req);
1235 
1236 	if (oxid == FNIC_UNASSIGNED_OXID) {
1237 		FNIC_FCS_DBG(KERN_INFO, fnic,
1238 		     "0x%x: Failed to allocate OXID to send GPN FT",
1239 			 iport->fcid);
1240 		mempool_free(frame, fnic->frame_pool);
1241 		iport->fabric.flags |= FNIC_FDLS_RETRY_FRAME;
1242 		goto err_out;
1243 	}
1244 	FNIC_STD_SET_OX_ID(pgpn_ft->fchdr, oxid);
1245 
1246 	FNIC_FCS_DBG(KERN_INFO, fnic,
1247 		 "0x%x: FDLS send GPN FT with oxid: 0x%x", iport->fcid,
1248 		 oxid);
1249 
1250 	fnic_send_fcoe_frame(iport, frame, frame_size);
1251 
1252 err_out:
1253 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1254 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1255 	fdls_set_state((&iport->fabric), fdls_state);
1256 }
1257 
1258 static void
1259 fdls_send_tgt_adisc(struct fnic_iport_s *iport, struct fnic_tport_s *tport)
1260 {
1261 	uint8_t *frame;
1262 	struct fc_std_els_adisc *padisc;
1263 	uint8_t s_id[3];
1264 	uint8_t d_id[3];
1265 	uint16_t oxid;
1266 	struct fnic *fnic = iport->fnic;
1267 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1268 			sizeof(struct fc_std_els_adisc);
1269 
1270 	frame = fdls_alloc_frame(iport);
1271 	if (frame == NULL) {
1272 		FNIC_FCS_DBG(KERN_ERR, fnic,
1273 				"Failed to allocate frame to send TGT ADISC");
1274 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1275 		goto err_out;
1276 	}
1277 
1278 	padisc = (struct fc_std_els_adisc *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1279 
1280 	hton24(s_id, iport->fcid);
1281 	hton24(d_id, tport->fcid);
1282 	memcpy(padisc->els.adisc_port_id, s_id, 3);
1283 	FNIC_STD_SET_S_ID(padisc->fchdr, s_id);
1284 	FNIC_STD_SET_D_ID(padisc->fchdr, d_id);
1285 
1286 	FNIC_STD_SET_F_CTL(padisc->fchdr, FNIC_ELS_REQ_FCTL << 16);
1287 	FNIC_STD_SET_R_CTL(padisc->fchdr, FC_RCTL_ELS_REQ);
1288 	FNIC_STD_SET_TYPE(padisc->fchdr, FC_TYPE_ELS);
1289 
1290 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_TGT_ADISC, &tport->active_oxid);
1291 	if (oxid == FNIC_UNASSIGNED_OXID) {
1292 		FNIC_FCS_DBG(KERN_INFO, fnic,
1293 					 "0x%x: Failed to allocate OXID to send TGT ADISC",
1294 					 iport->fcid);
1295 		mempool_free(frame, fnic->frame_pool);
1296 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1297 		goto err_out;
1298 	}
1299 	FNIC_STD_SET_OX_ID(padisc->fchdr, oxid);
1300 	FNIC_STD_SET_RX_ID(padisc->fchdr, FNIC_UNASSIGNED_RXID);
1301 
1302 	tport->flags &= ~FNIC_FDLS_TGT_ABORT_ISSUED;
1303 
1304 	FNIC_STD_SET_NPORT_NAME(&padisc->els.adisc_wwpn,
1305 				iport->wwpn);
1306 	FNIC_STD_SET_NODE_NAME(&padisc->els.adisc_wwnn,
1307 			iport->wwnn);
1308 
1309 	padisc->els.adisc_cmd = ELS_ADISC;
1310 
1311 	FNIC_FCS_DBG(KERN_INFO, fnic,
1312 				 "0x%x: FDLS send ADISC to tgt fcid: 0x%x",
1313 				 iport->fcid, tport->fcid);
1314 
1315 	atomic64_inc(&iport->iport_stats.tport_adisc_sent);
1316 
1317 	fnic_send_fcoe_frame(iport, frame, frame_size);
1318 
1319 err_out:
1320 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1321 	fdls_start_tport_timer(iport, tport, 2 * iport->e_d_tov);
1322 }
1323 
1324 bool fdls_delete_tport(struct fnic_iport_s *iport, struct fnic_tport_s *tport)
1325 {
1326 	struct fnic_tport_event_s *tport_del_evt;
1327 	struct fnic *fnic = iport->fnic;
1328 
1329 	if ((tport->state == FDLS_TGT_STATE_OFFLINING)
1330 	    || (tport->state == FDLS_TGT_STATE_OFFLINE)) {
1331 		FNIC_FCS_DBG(KERN_INFO, fnic,
1332 			     "tport fcid 0x%x: tport state is offlining/offline\n",
1333 			     tport->fcid);
1334 		return false;
1335 	}
1336 
1337 	fdls_set_tport_state(tport, FDLS_TGT_STATE_OFFLINING);
1338 	/*
1339 	 * By setting this flag, the tport will not be seen in a look-up
1340 	 * in an RSCN. Even if we move to multithreaded model, this tport
1341 	 * will be destroyed and a new RSCN will have to create a new one
1342 	 */
1343 	tport->flags |= FNIC_FDLS_TPORT_TERMINATING;
1344 
1345 	if (tport->timer_pending) {
1346 		FNIC_FCS_DBG(KERN_INFO, fnic,
1347 					 "tport fcid 0x%x: Canceling disc timer\n",
1348 					 tport->fcid);
1349 		fnic_del_tport_timer_sync(fnic, tport);
1350 		tport->timer_pending = 0;
1351 	}
1352 
1353 	if (IS_FNIC_FCP_INITIATOR(fnic)) {
1354 		spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
1355 		fnic_rport_exch_reset(iport->fnic, tport->fcid);
1356 		spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
1357 	} else if (IS_FNIC_NVME_INITIATOR(fnic)) {
1358 		spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
1359 		nvfnic_exch_reset(iport, tport);
1360 		spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
1361 	}
1362 
1363 	if ((tport->flags & FNIC_FDLS_SCSI_REGISTERED) ||
1364 	    (tport->flags & FNIC_FDLS_NVME_REGISTERED)) {
1365 		tport_del_evt =
1366 			kzalloc_obj(struct fnic_tport_event_s, GFP_ATOMIC);
1367 		if (!tport_del_evt) {
1368 			FNIC_FCS_DBG(KERN_INFO, fnic,
1369 					"iport: 0x%x tport 0x%x: Failed to allocate memory\n",
1370 					iport->fcid, tport->fcid);
1371 			return false;
1372 		}
1373 		tport_del_evt->event = TGT_EV_RPORT_DEL;
1374 		tport_del_evt->arg1 = (void *) tport;
1375 		list_add_tail(&tport_del_evt->links, &fnic->tport_event_list);
1376 		queue_work(fnic_event_queue, &fnic->tport_work);
1377 	} else {
1378 		FNIC_FCS_DBG(KERN_INFO, fnic,
1379 				"tport 0x%x not registered, freeing locally\n",
1380 				tport->fcid);
1381 		list_del(&tport->links);
1382 		kfree(tport);
1383 	}
1384 
1385 	return true;
1386 }
1387 
1388 static void
1389 fdls_send_tgt_plogi(struct fnic_iport_s *iport, struct fnic_tport_s *tport)
1390 {
1391 	uint8_t *frame;
1392 	struct fc_std_flogi *pplogi;
1393 	struct fnic *fnic = iport->fnic;
1394 	uint16_t oxid;
1395 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1396 			sizeof(struct fc_std_flogi);
1397 	uint8_t d_id[3];
1398 	uint32_t timeout;
1399 
1400 	frame = fdls_alloc_frame(iport);
1401 	if (frame == NULL) {
1402 		FNIC_FCS_DBG(KERN_ERR, fnic,
1403 				"Failed to allocate frame to send TGT PLOGI");
1404 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1405 		goto err_out;
1406 	}
1407 
1408 	pplogi = (struct fc_std_flogi *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1409 	fdls_init_plogi_frame(frame, iport);
1410 
1411 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_TGT_PLOGI, &tport->active_oxid);
1412 	if (oxid == FNIC_UNASSIGNED_OXID) {
1413 		FNIC_FCS_DBG(KERN_INFO, fnic,
1414 				 "0x%x: Failed to allocate oxid to send PLOGI to fcid: 0x%x",
1415 				 iport->fcid, tport->fcid);
1416 		mempool_free(frame, fnic->frame_pool);
1417 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1418 		goto err_out;
1419 	}
1420 	FNIC_STD_SET_OX_ID(pplogi->fchdr, oxid);
1421 
1422 	tport->flags &= ~FNIC_FDLS_TGT_ABORT_ISSUED;
1423 
1424 	hton24(d_id, tport->fcid);
1425 	FNIC_STD_SET_D_ID(pplogi->fchdr, d_id);
1426 
1427 	FNIC_FCS_DBG(KERN_INFO, fnic,
1428 				 "0x%x: FDLS send tgt PLOGI to tgt: 0x%x with oxid: 0x%x",
1429 				 iport->fcid, tport->fcid, oxid);
1430 
1431 	fnic_send_fcoe_frame(iport, frame, frame_size);
1432 	atomic64_inc(&iport->iport_stats.tport_plogi_sent);
1433 
1434 err_out:
1435 	timeout = max(2 * iport->e_d_tov, iport->plogi_timeout);
1436 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1437 	fdls_start_tport_timer(iport, tport, timeout);
1438 }
1439 
1440 static uint16_t
1441 fnic_fc_plogi_rsp_rdf(struct fnic_iport_s *iport,
1442 		      struct fc_std_flogi *plogi_rsp)
1443 {
1444 	uint16_t b2b_rdf_size =
1445 	    be16_to_cpu(FNIC_LOGI_RDF_SIZE(plogi_rsp->els));
1446 	uint16_t spc3_rdf_size =
1447 	    be16_to_cpu(plogi_rsp->els.fl_cssp[2].cp_rdfs) & FNIC_FC_C3_RDF;
1448 	struct fnic *fnic = iport->fnic;
1449 
1450 	FNIC_FCS_DBG(KERN_INFO, fnic,
1451 			 "MFS: b2b_rdf_size: 0x%x spc3_rdf_size: 0x%x",
1452 			 b2b_rdf_size, spc3_rdf_size);
1453 
1454 	return min(b2b_rdf_size, spc3_rdf_size);
1455 }
1456 
1457 static void fdls_send_register_fc4_types(struct fnic_iport_s *iport)
1458 {
1459 	uint8_t *frame;
1460 	struct fc_std_rft_id *prft_id;
1461 	struct fnic *fnic = iport->fnic;
1462 	uint16_t oxid;
1463 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1464 			sizeof(struct fc_std_rft_id);
1465 	uint8_t fcid[3];
1466 
1467 	frame = fdls_alloc_frame(iport);
1468 	if (frame == NULL) {
1469 		FNIC_FCS_DBG(KERN_ERR, fnic,
1470 		     "Failed to allocate frame to send RFT");
1471 		return;
1472 	}
1473 
1474 	prft_id = (struct fc_std_rft_id *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1475 	*prft_id = (struct fc_std_rft_id) {
1476 		.fchdr = {.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
1477 		      .fh_d_id = {0xFF, 0xFF, 0xFC}, .fh_type = FC_TYPE_CT,
1478 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1479 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1480 		.fc_std_ct_hdr = {.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_DIR,
1481 			      .ct_fs_subtype = FC_NS_SUBTYPE,
1482 			      .ct_cmd = cpu_to_be16(FC_NS_RFT_ID)}
1483 	};
1484 
1485 	hton24(fcid, iport->fcid);
1486 	FNIC_STD_SET_S_ID(prft_id->fchdr, fcid);
1487 	FNIC_STD_SET_PORT_ID(prft_id->rft_id, fcid);
1488 
1489 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_RFT,
1490 		&iport->active_oxid_fabric_req);
1491 
1492 	if (oxid == FNIC_UNASSIGNED_OXID) {
1493 		FNIC_FCS_DBG(KERN_INFO, fnic,
1494 		     "0x%x: Failed to allocate OXID to send RFT",
1495 			 iport->fcid);
1496 		mempool_free(frame, fnic->frame_pool);
1497 		return;
1498 	}
1499 	FNIC_STD_SET_OX_ID(prft_id->fchdr, oxid);
1500 
1501 	if (IS_FNIC_NVME_INITIATOR(fnic))
1502 		prft_id->rft_id.fr_fts.ff_type_map[FC_TYPE_NVME / FC_NS_BPW] =
1503 			cpu_to_be32(1 << (FC_TYPE_NVME % FC_NS_BPW));
1504 	else if (IS_FNIC_FCP_INITIATOR(fnic))
1505 		prft_id->rft_id.fr_fts.ff_type_map[FC_TYPE_FCP / FC_NS_BPW] =
1506 			cpu_to_be32(1 << (FC_TYPE_FCP % FC_NS_BPW));
1507 
1508 	prft_id->rft_id.fr_fts.ff_type_map[FC_TYPE_CT / FC_NS_BPW] |=
1509 		cpu_to_be32(1 << (FC_TYPE_CT % FC_NS_BPW));
1510 
1511 	FNIC_FCS_DBG(KERN_INFO, fnic,
1512 		     "0x%x: FDLS send RFT 0x%08x 0x%08x 0x%08x with oxid: 0x%x",
1513 		     iport->fcid, prft_id->rft_id.fr_fts.ff_type_map[0],
1514 		     prft_id->rft_id.fr_fts.ff_type_map[1],
1515 		     prft_id->rft_id.fr_fts.ff_type_map[2],
1516 		     oxid);
1517 
1518 	fnic_send_fcoe_frame(iport, frame, frame_size);
1519 
1520 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1521 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1522 }
1523 
1524 static void fdls_send_register_fc4_features(struct fnic_iport_s *iport)
1525 {
1526 	uint8_t *frame;
1527 	struct fc_std_rff_id *prff_id;
1528 	struct fnic *fnic = iport->fnic;
1529 	uint16_t oxid;
1530 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1531 			sizeof(struct fc_std_rff_id);
1532 	uint8_t fcid[3];
1533 
1534 	frame = fdls_alloc_frame(iport);
1535 	if (frame == NULL) {
1536 		FNIC_FCS_DBG(KERN_ERR, fnic,
1537 		     "Failed to allocate frame to send RFF");
1538 		return;
1539 	}
1540 
1541 	prff_id = (struct fc_std_rff_id *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1542 	*prff_id = (struct fc_std_rff_id) {
1543 		.fchdr = {.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
1544 		      .fh_d_id = {0xFF, 0xFF, 0xFC}, .fh_type = FC_TYPE_CT,
1545 		      .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1546 		      .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1547 		.fc_std_ct_hdr = {.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_DIR,
1548 			      .ct_fs_subtype = FC_NS_SUBTYPE,
1549 			      .ct_cmd = cpu_to_be16(FC_NS_RFF_ID)},
1550 	};
1551 
1552 	fdls_set_frame_type(fnic, &prff_id->rff_id.fr_type);
1553 	fdls_set_feature_type(fnic, &prff_id->rff_id.fr_feat);
1554 
1555 	hton24(fcid, iport->fcid);
1556 	FNIC_STD_SET_S_ID(prff_id->fchdr, fcid);
1557 	FNIC_STD_SET_PORT_ID(prff_id->rff_id, fcid);
1558 
1559 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_RFF,
1560 		&iport->active_oxid_fabric_req);
1561 
1562 	if (oxid == FNIC_UNASSIGNED_OXID) {
1563 		FNIC_FCS_DBG(KERN_INFO, fnic,
1564 			     "0x%x: Failed to allocate OXID to send RFF",
1565 				 iport->fcid);
1566 		mempool_free(frame, fnic->frame_pool);
1567 		return;
1568 	}
1569 	FNIC_STD_SET_OX_ID(prff_id->fchdr, oxid);
1570 
1571 	FNIC_FCS_DBG(KERN_INFO, fnic,
1572 		"0x%x: FDLS send RFF with oxid: 0x%x type 0%x feat 0%x",
1573 		iport->fcid, oxid, prff_id->rff_id.fr_type,
1574 		prff_id->rff_id.fr_feat);
1575 
1576 	fnic_send_fcoe_frame(iport, frame, frame_size);
1577 
1578 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1579 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1580 }
1581 
1582 static void
1583 fdls_send_tgt_prli(struct fnic_iport_s *iport, struct fnic_tport_s *tport)
1584 {
1585 	uint8_t *frame;
1586 	struct fc_std_els_prli *pprli;
1587 	struct fnic *fnic = iport->fnic;
1588 	uint16_t oxid;
1589 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1590 			sizeof(struct fc_std_els_prli);
1591 	uint8_t s_id[3];
1592 	uint8_t d_id[3];
1593 	uint32_t timeout;
1594 
1595 	frame = fdls_alloc_frame(iport);
1596 	if (frame == NULL) {
1597 		FNIC_FCS_DBG(KERN_ERR, fnic,
1598 				"Failed to allocate frame to send TGT PRLI");
1599 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1600 		goto err_out;
1601 	}
1602 
1603 	pprli = (struct fc_std_els_prli *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1604 	*pprli = (struct fc_std_els_prli) {
1605 		.fchdr = {.fh_r_ctl = FC_RCTL_ELS_REQ, .fh_type = FC_TYPE_ELS,
1606 			  .fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1607 			  .fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)},
1608 		.els_prli = {.prli_cmd = ELS_PRLI},
1609 		.sp = {.spp_params = cpu_to_be32(iport->service_params)}
1610 	};
1611 
1612 	fdls_set_frame_type(fnic, &pprli->sp.spp_type);
1613 	if (IS_FNIC_FCP_INITIATOR(fnic)) {
1614 		pprli->els_prli.prli_spp_len = 16;
1615 		pprli->els_prli.prli_len = cpu_to_be16(0x14);
1616 		pprli->sp.spp_flags = FC_SPP_EST_IMG_PAIR;
1617 	} else if (IS_FNIC_NVME_INITIATOR(fnic)) {
1618 		/* Per FC-NVMe, Establish Image Pair must not be set for NVMe PRLI. */
1619 		pprli->els_prli.prli_spp_len = 20;
1620 		pprli->els_prli.prli_len = cpu_to_be16(0x18);
1621 	}
1622 
1623 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_TGT_PRLI, &tport->active_oxid);
1624 	if (oxid == FNIC_UNASSIGNED_OXID) {
1625 		FNIC_FCS_DBG(KERN_ERR, fnic,
1626 			"0x%x: Failed to allocate OXID to send TGT PRLI to 0x%x",
1627 			iport->fcid, tport->fcid);
1628 		mempool_free(frame, fnic->frame_pool);
1629 		tport->flags |= FNIC_FDLS_RETRY_FRAME;
1630 		goto err_out;
1631 	}
1632 
1633 	tport->flags &= ~FNIC_FDLS_TGT_ABORT_ISSUED;
1634 
1635 	hton24(s_id, iport->fcid);
1636 	hton24(d_id, tport->fcid);
1637 
1638 	FNIC_STD_SET_OX_ID(pprli->fchdr, oxid);
1639 	FNIC_STD_SET_S_ID(pprli->fchdr, s_id);
1640 	FNIC_STD_SET_D_ID(pprli->fchdr, d_id);
1641 
1642 	FNIC_FCS_DBG(KERN_INFO, fnic,
1643 			"0x%x: FDLS send PRLI to tgt: 0x%x with oxid: 0x%x",
1644 			iport->fcid, tport->fcid, oxid);
1645 
1646 	fnic_send_fcoe_frame(iport, frame, frame_size);
1647 	atomic64_inc(&iport->iport_stats.tport_prli_sent);
1648 
1649 err_out:
1650 	timeout = max(2 * iport->e_d_tov, iport->plogi_timeout);
1651 	/* Even if fnic_send_fcoe_frame() fails we want to retry after timeout */
1652 	fdls_start_tport_timer(iport, tport, timeout);
1653 }
1654 
1655 /**
1656  * fdls_send_fabric_logo - Send flogo to the fcf
1657  * @iport: Handle to fnic iport
1658  *
1659  * This function does not change or check the fabric state.
1660  * It the caller's responsibility to set the appropriate iport fabric
1661  * state when this is called. Normally it is FDLS_STATE_FABRIC_LOGO.
1662  * Currently this assumes to be called with fnic lock held.
1663  */
1664 void fdls_send_fabric_logo(struct fnic_iport_s *iport)
1665 {
1666 	uint8_t *frame;
1667 	struct fc_std_logo *plogo;
1668 	struct fnic *fnic = iport->fnic;
1669 	uint8_t d_id[3];
1670 	uint16_t oxid;
1671 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1672 			sizeof(struct fc_std_logo);
1673 
1674 	frame = fdls_alloc_frame(iport);
1675 	if (frame == NULL) {
1676 		FNIC_FCS_DBG(KERN_ERR, fnic,
1677 		     "Failed to allocate frame to send fabric LOGO");
1678 		return;
1679 	}
1680 
1681 	plogo = (struct fc_std_logo *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1682 	fdls_init_logo_frame(frame, iport);
1683 
1684 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FABRIC_LOGO,
1685 		&iport->active_oxid_fabric_req);
1686 
1687 	if (oxid == FNIC_UNASSIGNED_OXID) {
1688 		FNIC_FCS_DBG(KERN_INFO, fnic,
1689 		     "0x%x: Failed to allocate OXID to send fabric LOGO",
1690 			 iport->fcid);
1691 		mempool_free(frame, fnic->frame_pool);
1692 		return;
1693 	}
1694 	FNIC_STD_SET_OX_ID(plogo->fchdr, oxid);
1695 
1696 	hton24(d_id, FC_FID_FLOGI);
1697 	FNIC_STD_SET_D_ID(plogo->fchdr, d_id);
1698 
1699 	iport->fabric.flags &= ~FNIC_FDLS_FABRIC_ABORT_ISSUED;
1700 
1701 	FNIC_FCS_DBG(KERN_INFO, fnic,
1702 		     "0x%x: FDLS send fabric LOGO with oxid: 0x%x",
1703 		     iport->fcid, oxid);
1704 
1705 	fnic_send_fcoe_frame(iport, frame, frame_size);
1706 
1707 	fdls_start_fabric_timer(iport, 2 * iport->e_d_tov);
1708 }
1709 
1710 /**
1711  * fdls_tgt_logout - Send plogo to the remote port
1712  * @iport: Handle to fnic iport
1713  * @tport: Handle to remote port
1714  *
1715  * This function does not change or check the fabric/tport state.
1716  * It the caller's responsibility to set the appropriate tport/fabric
1717  * state when this is called. Normally that is fdls_tgt_state_plogo.
1718  * This could be used to send plogo to nameserver process
1719  * also not just target processes
1720  */
1721 void fdls_tgt_logout(struct fnic_iport_s *iport, struct fnic_tport_s *tport)
1722 {
1723 	uint8_t *frame;
1724 	struct fc_std_logo *plogo;
1725 	struct fnic *fnic = iport->fnic;
1726 	uint8_t d_id[3];
1727 	uint16_t oxid;
1728 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
1729 			sizeof(struct fc_std_logo);
1730 
1731 	frame = fdls_alloc_frame(iport);
1732 	if (frame == NULL) {
1733 		FNIC_FCS_DBG(KERN_ERR, fnic,
1734 		     "Failed to allocate frame to send fabric LOGO");
1735 		return;
1736 	}
1737 
1738 	plogo = (struct fc_std_logo *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1739 	fdls_init_logo_frame(frame, iport);
1740 
1741 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_TGT_LOGO, &tport->active_oxid);
1742 	if (oxid == FNIC_UNASSIGNED_OXID) {
1743 		FNIC_FCS_DBG(KERN_INFO, fnic,
1744 		     "0x%x: Failed to allocate OXID to send tgt LOGO",
1745 		     iport->fcid);
1746 		mempool_free(frame, fnic->frame_pool);
1747 		return;
1748 	}
1749 	FNIC_STD_SET_OX_ID(plogo->fchdr, oxid);
1750 
1751 	hton24(d_id, tport->fcid);
1752 	FNIC_STD_SET_D_ID(plogo->fchdr, d_id);
1753 
1754 	FNIC_FCS_DBG(KERN_INFO, fnic,
1755 		 "0x%x: FDLS send tgt LOGO with oxid: 0x%x",
1756 		 iport->fcid, oxid);
1757 
1758 	fnic_send_fcoe_frame(iport, frame, frame_size);
1759 
1760 	atomic64_inc(&iport->iport_stats.tport_logo_sent);
1761 }
1762 
1763 static void fdls_tgt_discovery_start(struct fnic_iport_s *iport)
1764 {
1765 	struct fnic_tport_s *tport, *next;
1766 	u32 old_link_down_cnt = iport->fnic->link_down_cnt;
1767 	struct fnic *fnic = iport->fnic;
1768 
1769 	FNIC_FCS_DBG(KERN_INFO, fnic,
1770 				 "0x%x: Starting FDLS target discovery", iport->fcid);
1771 
1772 	list_for_each_entry_safe(tport, next, &iport->tport_list, links) {
1773 		if ((old_link_down_cnt != iport->fnic->link_down_cnt)
1774 			|| (iport->state != FNIC_IPORT_STATE_READY)) {
1775 			break;
1776 		}
1777 		/* if we marked the tport as deleted due to GPN_FT
1778 		 * We should not send ADISC anymore
1779 		 */
1780 		if ((tport->state == FDLS_TGT_STATE_OFFLINING) ||
1781 			(tport->state == FDLS_TGT_STATE_OFFLINE))
1782 			continue;
1783 
1784 		/* For tports which have received RSCN */
1785 		if (tport->flags & FNIC_FDLS_TPORT_SEND_ADISC) {
1786 			tport->retry_counter = 0;
1787 			fdls_set_tport_state(tport, FDLS_TGT_STATE_ADISC);
1788 			tport->flags &= ~FNIC_FDLS_TPORT_SEND_ADISC;
1789 			fdls_send_tgt_adisc(iport, tport);
1790 			continue;
1791 		}
1792 		if (fdls_get_tport_state(tport) != FDLS_TGT_STATE_INIT) {
1793 			/* Not a new port, skip  */
1794 			continue;
1795 		}
1796 		tport->retry_counter = 0;
1797 		fdls_set_tport_state(tport, FDLS_TGT_STATE_PLOGI);
1798 		fdls_send_tgt_plogi(iport, tport);
1799 	}
1800 	fdls_set_state((&iport->fabric), FDLS_STATE_TGT_DISCOVERY);
1801 }
1802 
1803 /*
1804  * Function to restart the IT nexus if we received any out of
1805  * sequence PLOGI/PRLI  response from the target.
1806  * The memory for the new tport structure is allocated
1807  * inside fdls_create_tport and added to the iport's tport list.
1808  * This will get freed later during tport_offline/linkdown
1809  * or module unload. The new_tport pointer will go out of scope
1810  * safely since the memory it is
1811  * pointing to it will be freed later
1812  */
1813 static void fdls_target_restart_nexus(struct fnic_tport_s *tport)
1814 {
1815 	struct fnic_iport_s *iport = tport->iport;
1816 	struct fnic_tport_s *new_tport = NULL;
1817 	uint32_t fcid;
1818 	uint64_t wwpn;
1819 	int nexus_restart_count;
1820 	struct fnic *fnic = iport->fnic;
1821 	bool retval = true;
1822 
1823 	FNIC_FCS_DBG(KERN_INFO, fnic,
1824 				 "tport fcid: 0x%x state: %d restart_count: %d",
1825 				 tport->fcid, tport->state, tport->nexus_restart_count);
1826 
1827 	fcid = tport->fcid;
1828 	wwpn = tport->wwpn;
1829 	nexus_restart_count = tport->nexus_restart_count;
1830 
1831 	retval = fdls_delete_tport(iport, tport);
1832 	if (retval != true) {
1833 		FNIC_FCS_DBG(KERN_ERR, fnic,
1834 			     "Error deleting tport: 0x%x", fcid);
1835 		return;
1836 	}
1837 
1838 	if (nexus_restart_count >= FNIC_TPORT_MAX_NEXUS_RESTART) {
1839 		FNIC_FCS_DBG(KERN_INFO, fnic,
1840 			     "Exceeded nexus restart retries tport: 0x%x",
1841 			     fcid);
1842 		return;
1843 	}
1844 
1845 	/*
1846 	 * Allocate memory for the new tport and add it to
1847 	 * iport's tport list.
1848 	 * This memory will be freed during tport_offline/linkdown
1849 	 * or module unload. The pointer new_tport is safe to go
1850 	 * out of scope when this function returns, since the memory
1851 	 * it is pointing to is guaranteed to be freed later
1852 	 * as mentioned above.
1853 	 */
1854 	new_tport = fdls_create_tport(iport, fcid, wwpn);
1855 	if (!new_tport) {
1856 		FNIC_FCS_DBG(KERN_INFO, fnic,
1857 					 "Error creating new tport: 0x%x", fcid);
1858 		return;
1859 	}
1860 
1861 	new_tport->nexus_restart_count = nexus_restart_count + 1;
1862 	fdls_send_tgt_plogi(iport, new_tport);
1863 	fdls_set_tport_state(new_tport, FDLS_TGT_STATE_PLOGI);
1864 }
1865 
1866 struct fnic_tport_s *fnic_find_tport_by_fcid(struct fnic_iport_s *iport,
1867 									 uint32_t fcid)
1868 {
1869 	struct fnic_tport_s *tport, *next;
1870 
1871 	list_for_each_entry_safe(tport, next, &(iport->tport_list), links) {
1872 		if ((tport->fcid == fcid)
1873 			&& !(tport->flags & FNIC_FDLS_TPORT_TERMINATING))
1874 			return tport;
1875 	}
1876 	return NULL;
1877 }
1878 
1879 static struct fnic_tport_s *fdls_create_tport(struct fnic_iport_s *iport,
1880 								  uint32_t fcid, uint64_t wwpn)
1881 {
1882 	struct fnic_tport_s *tport;
1883 	struct fnic *fnic = iport->fnic;
1884 
1885 	FNIC_FCS_DBG(KERN_INFO, fnic,
1886 			 "FDLS create tport: fcid: 0x%x wwpn: 0x%llx", fcid, wwpn);
1887 
1888 	tport = kzalloc_obj(struct fnic_tport_s, GFP_ATOMIC);
1889 	if (!tport) {
1890 		FNIC_FCS_DBG(KERN_INFO, fnic,
1891 			 "Memory allocation failure while creating tport: 0x%x\n",
1892 			 fcid);
1893 		return NULL;
1894 	}
1895 
1896 	tport->max_payload_size = FNIC_FCOE_MAX_FRAME_SZ;
1897 	tport->r_a_tov = FC_DEF_R_A_TOV;
1898 	tport->e_d_tov = FC_DEF_E_D_TOV;
1899 	tport->fcid = fcid;
1900 	tport->wwpn = wwpn;
1901 	tport->iport = iport;
1902 	INIT_LIST_HEAD(&tport->ls_req_list);
1903 
1904 	FNIC_FCS_DBG(KERN_DEBUG, fnic,
1905 				 "Need to setup tport timer callback");
1906 
1907 	timer_setup(&tport->retry_timer, fdls_tport_timer_callback, 0);
1908 
1909 	FNIC_FCS_DBG(KERN_INFO, fnic,
1910 				 "Added tport 0x%x", tport->fcid);
1911 	fdls_set_tport_state(tport, FDLS_TGT_STATE_INIT);
1912 	list_add_tail(&tport->links, &iport->tport_list);
1913 	atomic_set(&tport->in_flight, 0);
1914 	return tport;
1915 }
1916 
1917 struct fnic_tport_s *fnic_find_tport_by_wwpn(struct fnic_iport_s *iport,
1918 									 uint64_t wwpn)
1919 {
1920 	struct fnic_tport_s *tport, *next;
1921 
1922 	list_for_each_entry_safe(tport, next, &(iport->tport_list), links) {
1923 		if ((tport->wwpn == wwpn)
1924 			&& !(tport->flags & FNIC_FDLS_TPORT_TERMINATING))
1925 			return tport;
1926 	}
1927 	return NULL;
1928 }
1929 
1930 static void
1931 fnic_fdmi_attr_set(void *attr_start, u16 type, u16 len,
1932 		void *data, u32 *off)
1933 {
1934 	u16 size = len + FC_FDMI_ATTR_ENTRY_HEADER_LEN;
1935 	struct fc_fdmi_attr_entry *fdmi_attr = (struct fc_fdmi_attr_entry *)
1936 		((u8 *)attr_start + *off);
1937 
1938 	put_unaligned_be16(type, &fdmi_attr->type);
1939 	put_unaligned_be16(size, &fdmi_attr->len);
1940 	memcpy(fdmi_attr->value, data, len);
1941 	*off += size;
1942 }
1943 
1944 static void fdls_fdmi_register_hba(struct fnic_iport_s *iport)
1945 {
1946 	uint8_t *frame;
1947 	struct fc_std_fdmi_rhba *prhba;
1948 	struct fc_fdmi_attr_entry *fdmi_attr;
1949 	uint8_t fcid[3];
1950 	int err;
1951 	struct fnic *fnic = iport->fnic;
1952 	struct vnic_devcmd_fw_info *fw_info = NULL;
1953 	uint16_t oxid;
1954 	u32 attr_off_bytes, len;
1955 	u8 data[64];
1956 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET;
1957 
1958 	frame = fdls_alloc_frame(iport);
1959 	if (frame == NULL) {
1960 		FNIC_FCS_DBG(KERN_ERR, fnic,
1961 		     "Failed to allocate frame to send FDMI RHBA");
1962 		return;
1963 	}
1964 
1965 	prhba = (struct fc_std_fdmi_rhba *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
1966 	*prhba = (struct fc_std_fdmi_rhba) {
1967 		.fchdr = {
1968 			.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
1969 			.fh_d_id = {0xFF, 0XFF, 0XFA},
1970 			.fh_type = FC_TYPE_CT,
1971 			.fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
1972 			.fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)
1973 		},
1974 		.fc_std_ct_hdr = {
1975 			.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_MGMT,
1976 			.ct_fs_subtype = FC_FDMI_SUBTYPE,
1977 			.ct_cmd = cpu_to_be16(FC_FDMI_RHBA)
1978 		},
1979 	};
1980 
1981 	hton24(fcid, iport->fcid);
1982 	FNIC_STD_SET_S_ID(prhba->fchdr, fcid);
1983 
1984 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FDMI_RHBA,
1985 		&iport->active_oxid_fdmi_rhba);
1986 
1987 	if (oxid == FNIC_UNASSIGNED_OXID) {
1988 		FNIC_FCS_DBG(KERN_INFO, fnic,
1989 			     "0x%x: Failed to allocate OXID to send FDMI RHBA",
1990 		     iport->fcid);
1991 		mempool_free(frame, fnic->frame_pool);
1992 		return;
1993 	}
1994 	FNIC_STD_SET_OX_ID(prhba->fchdr, oxid);
1995 
1996 	put_unaligned_be64(iport->wwpn, &prhba->rhba.hbaid.id);
1997 	put_unaligned_be32(FNIC_FDMI_NUM_PORTS, &prhba->rhba.port.numport);
1998 	put_unaligned_be64(iport->wwpn, &prhba->rhba.port.port[0].portname);
1999 	put_unaligned_be32(FNIC_FDMI_NUM_HBA_ATTRS,
2000 			&prhba->rhba.hba_attrs.numattrs);
2001 
2002 	fdmi_attr = prhba->rhba.hba_attrs.attr;
2003 	attr_off_bytes = 0;
2004 
2005 	put_unaligned_be64(iport->wwnn, data);
2006 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_NODE_NAME,
2007 		FNIC_FDMI_NN_LEN, data, &attr_off_bytes);
2008 
2009 	FNIC_FCS_DBG(KERN_INFO, fnic,
2010 			"NN set, off=%d", attr_off_bytes);
2011 
2012 	strscpy_pad(data, FNIC_FDMI_MANUFACTURER, FNIC_FDMI_MANU_LEN);
2013 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_MANUFACTURER,
2014 		FNIC_FDMI_MANU_LEN, data, &attr_off_bytes);
2015 
2016 	FNIC_FCS_DBG(KERN_INFO, fnic,
2017 			"MFG set <%s>, off=%d", data, attr_off_bytes);
2018 
2019 	err = vnic_dev_fw_info(fnic->vdev, &fw_info);
2020 	if (!err) {
2021 		strscpy_pad(data, fw_info->hw_serial_number,
2022 				FNIC_FDMI_SERIAL_LEN);
2023 		fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_SERIAL_NUMBER,
2024 			FNIC_FDMI_SERIAL_LEN, data, &attr_off_bytes);
2025 		FNIC_FCS_DBG(KERN_INFO, fnic,
2026 				"SERIAL set <%s>, off=%d", data, attr_off_bytes);
2027 
2028 	}
2029 
2030 	if (fnic->subsys_desc_len >= FNIC_FDMI_MODEL_LEN)
2031 		fnic->subsys_desc_len = FNIC_FDMI_MODEL_LEN - 1;
2032 	strscpy_pad(data, fnic->subsys_desc, FNIC_FDMI_MODEL_LEN);
2033 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_MODEL, FNIC_FDMI_MODEL_LEN,
2034 		data, &attr_off_bytes);
2035 
2036 	FNIC_FCS_DBG(KERN_INFO, fnic,
2037 			"MODEL set <%s>, off=%d", data, attr_off_bytes);
2038 
2039 	strscpy_pad(data, FNIC_FDMI_MODEL_DESCRIPTION, FNIC_FDMI_MODEL_DES_LEN);
2040 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_MODEL_DES,
2041 		FNIC_FDMI_MODEL_DES_LEN, data, &attr_off_bytes);
2042 
2043 	FNIC_FCS_DBG(KERN_INFO, fnic,
2044 			"MODEL_DESC set <%s>, off=%d", data, attr_off_bytes);
2045 
2046 	if (!err) {
2047 		strscpy_pad(data, fw_info->hw_version, FNIC_FDMI_HW_VER_LEN);
2048 		fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_HARDWARE_VERSION,
2049 			FNIC_FDMI_HW_VER_LEN, data, &attr_off_bytes);
2050 		FNIC_FCS_DBG(KERN_INFO, fnic,
2051 				"HW_VER set <%s>, off=%d", data, attr_off_bytes);
2052 
2053 	}
2054 
2055 	strscpy_pad(data, DRV_VERSION, FNIC_FDMI_DR_VER_LEN);
2056 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_DRIVER_VERSION,
2057 		FNIC_FDMI_DR_VER_LEN, data, &attr_off_bytes);
2058 
2059 	FNIC_FCS_DBG(KERN_INFO, fnic,
2060 			"DRV_VER set <%s>, off=%d", data, attr_off_bytes);
2061 
2062 	strscpy_pad(data, "N/A", FNIC_FDMI_ROM_VER_LEN);
2063 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_ROM_VERSION,
2064 		FNIC_FDMI_ROM_VER_LEN, data, &attr_off_bytes);
2065 
2066 	FNIC_FCS_DBG(KERN_INFO, fnic,
2067 			"ROM_VER set <%s>, off=%d", data, attr_off_bytes);
2068 
2069 	if (!err) {
2070 		strscpy_pad(data, fw_info->fw_version, FNIC_FDMI_FW_VER_LEN);
2071 		fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_FIRMWARE_VERSION,
2072 			FNIC_FDMI_FW_VER_LEN, data, &attr_off_bytes);
2073 
2074 		FNIC_FCS_DBG(KERN_INFO, fnic,
2075 				"FW_VER set <%s>, off=%d", data, attr_off_bytes);
2076 	}
2077 
2078 	len = sizeof(struct fc_std_fdmi_rhba) + attr_off_bytes;
2079 	frame_size += len;
2080 
2081 	FNIC_FCS_DBG(KERN_INFO, fnic,
2082 		 "0x%x: FDLS send FDMI RHBA with oxid: 0x%x fs: %d", iport->fcid,
2083 		 oxid, frame_size);
2084 
2085 	fnic_send_fcoe_frame(iport, frame, frame_size);
2086 	iport->fabric.fdmi_pending |= FDLS_FDMI_REG_HBA_PENDING;
2087 }
2088 
2089 static void fdls_fdmi_register_pa(struct fnic_iport_s *iport)
2090 {
2091 	uint8_t *frame;
2092 	struct fc_std_fdmi_rpa *prpa;
2093 	struct fc_fdmi_attr_entry *fdmi_attr;
2094 	uint8_t fcid[3];
2095 	struct fnic *fnic = iport->fnic;
2096 	u32 port_speed_bm;
2097 	u32 port_speed = vnic_dev_port_speed(fnic->vdev);
2098 	uint16_t oxid;
2099 	u32 attr_off_bytes, len;
2100 	u8 tmp_data[16], data[64];
2101 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET;
2102 
2103 	frame = fdls_alloc_frame(iport);
2104 	if (frame == NULL) {
2105 		FNIC_FCS_DBG(KERN_ERR, fnic,
2106 		     "Failed to allocate frame to send FDMI RPA");
2107 		return;
2108 	}
2109 
2110 	prpa = (struct fc_std_fdmi_rpa *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
2111 	*prpa = (struct fc_std_fdmi_rpa) {
2112 		.fchdr = {
2113 			.fh_r_ctl = FC_RCTL_DD_UNSOL_CTL,
2114 			.fh_d_id = {0xFF, 0xFF, 0xFA},
2115 			.fh_type = FC_TYPE_CT,
2116 			.fh_f_ctl = {FNIC_ELS_REQ_FCTL, 0, 0},
2117 			.fh_rx_id = cpu_to_be16(FNIC_UNASSIGNED_RXID)
2118 		},
2119 		.fc_std_ct_hdr = {
2120 			.ct_rev = FC_CT_REV, .ct_fs_type = FC_FST_MGMT,
2121 			.ct_fs_subtype = FC_FDMI_SUBTYPE,
2122 			.ct_cmd = cpu_to_be16(FC_FDMI_RPA)
2123 		},
2124 	};
2125 
2126 	hton24(fcid, iport->fcid);
2127 	FNIC_STD_SET_S_ID(prpa->fchdr, fcid);
2128 
2129 	oxid = fdls_alloc_oxid(iport, FNIC_FRAME_TYPE_FDMI_RPA,
2130 		&iport->active_oxid_fdmi_rpa);
2131 
2132 	if (oxid == FNIC_UNASSIGNED_OXID) {
2133 		FNIC_FCS_DBG(KERN_INFO, fnic,
2134 			     "0x%x: Failed to allocate OXID to send FDMI RPA",
2135 			     iport->fcid);
2136 		mempool_free(frame, fnic->frame_pool);
2137 		return;
2138 	}
2139 	FNIC_STD_SET_OX_ID(prpa->fchdr, oxid);
2140 
2141 	put_unaligned_be64(iport->wwpn, &prpa->rpa.port.portname);
2142 	put_unaligned_be32(FNIC_FDMI_NUM_PORT_ATTRS,
2143 				&prpa->rpa.hba_attrs.numattrs);
2144 
2145 	/* MDS does not support GIGE speed.
2146 	 * Bit shift standard definitions from scsi_transport_fc.h to
2147 	 * match FC spec.
2148 	 */
2149 	switch (port_speed) {
2150 	case DCEM_PORTSPEED_10G:
2151 	case DCEM_PORTSPEED_20G:
2152 		/* There is no bit for 20G */
2153 		port_speed_bm = FC_PORTSPEED_10GBIT << PORT_SPEED_BIT_14;
2154 		break;
2155 	case DCEM_PORTSPEED_25G:
2156 		port_speed_bm = FC_PORTSPEED_25GBIT << PORT_SPEED_BIT_8;
2157 		break;
2158 	case DCEM_PORTSPEED_40G:
2159 	case DCEM_PORTSPEED_4x10G:
2160 		port_speed_bm = FC_PORTSPEED_40GBIT << PORT_SPEED_BIT_9;
2161 		break;
2162 	case DCEM_PORTSPEED_100G:
2163 		port_speed_bm = FC_PORTSPEED_100GBIT << PORT_SPEED_BIT_8;
2164 		break;
2165 	default:
2166 		port_speed_bm = FC_PORTSPEED_1GBIT << PORT_SPEED_BIT_15;
2167 		break;
2168 	}
2169 	attr_off_bytes = 0;
2170 
2171 	fdmi_attr = prpa->rpa.hba_attrs.attr;
2172 
2173 	put_unaligned_be64(iport->wwnn, data);
2174 
2175 	memset(data, 0, FNIC_FDMI_FC4_LEN);
2176 	if (IS_FNIC_FCP_INITIATOR(fnic))
2177 		data[2] = 1;
2178 	else if (IS_FNIC_NVME_INITIATOR(fnic))
2179 		data[6] = 1;
2180 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_FC4_TYPES,
2181 		FNIC_FDMI_FC4_LEN, data, &attr_off_bytes);
2182 
2183 	put_unaligned_be32(port_speed_bm, data);
2184 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_SUPPORTED_SPEEDS,
2185 		FNIC_FDMI_SUPP_SPEED_LEN, data, &attr_off_bytes);
2186 
2187 	put_unaligned_be32(port_speed_bm, data);
2188 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_CURRENT_SPEED,
2189 		FNIC_FDMI_CUR_SPEED_LEN, data, &attr_off_bytes);
2190 
2191 	put_unaligned_be32(FNIC_FDMI_MFS, data);
2192 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_MAX_FRAME_SIZE,
2193 		FNIC_FDMI_MFS_LEN, data, &attr_off_bytes);
2194 
2195 	if (IS_FNIC_FCP_INITIATOR(fnic))
2196 		snprintf(tmp_data, FNIC_FDMI_OS_NAME_LEN - 1, "host%d",
2197 			fnic->host->host_no);
2198 	else if (IS_FNIC_NVME_INITIATOR(fnic))
2199 		snprintf(tmp_data, FNIC_FDMI_OS_NAME_LEN - 1, "nvfnic%d",
2200 			fnic->fnic_num);
2201 	strscpy_pad(data, tmp_data, FNIC_FDMI_OS_NAME_LEN);
2202 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_OS_NAME,
2203 		FNIC_FDMI_OS_NAME_LEN, data, &attr_off_bytes);
2204 
2205 	FNIC_FCS_DBG(KERN_INFO, fnic,
2206 			"OS name set <%s>, off=%d", data, attr_off_bytes);
2207 
2208 	if (IS_FNIC_FCP_INITIATOR(fnic))
2209 		sprintf(fc_host_system_hostname(fnic->host), "%s",
2210 			utsname()->nodename);
2211 
2212 	strscpy_pad(data, utsname()->nodename, FNIC_FDMI_HN_LEN);
2213 	fnic_fdmi_attr_set(fdmi_attr, FNIC_FDMI_TYPE_HOST_NAME,
2214 		FNIC_FDMI_HN_LEN, data, &attr_off_bytes);
2215 
2216 	FNIC_FCS_DBG(KERN_INFO, fnic,
2217 			"Host name set <%s>, off=%d", data, attr_off_bytes);
2218 
2219 	len = sizeof(struct fc_std_fdmi_rpa) + attr_off_bytes;
2220 	frame_size += len;
2221 
2222 	FNIC_FCS_DBG(KERN_INFO, fnic,
2223 		 "0x%x: FDLS send FDMI RPA with oxid: 0x%x fs: %d", iport->fcid,
2224 		 oxid, frame_size);
2225 
2226 	fnic_send_fcoe_frame(iport, frame, frame_size);
2227 	iport->fabric.fdmi_pending |= FDLS_FDMI_RPA_PENDING;
2228 }
2229 
2230 void fdls_fabric_timer_callback(struct timer_list *t)
2231 {
2232 	struct fnic_fdls_fabric_s *fabric = timer_container_of(fabric, t,
2233 							       retry_timer);
2234 	struct fnic_iport_s *iport =
2235 		container_of(fabric, struct fnic_iport_s, fabric);
2236 	struct fnic *fnic = iport->fnic;
2237 	unsigned long flags;
2238 
2239 	FNIC_FCS_DBG(KERN_INFO, fnic,
2240 		 "tp: %d fab state: %d fab retry counter: %d max_flogi_retries: %d",
2241 		 iport->fabric.timer_pending, iport->fabric.state,
2242 		 iport->fabric.retry_counter, iport->max_flogi_retries);
2243 
2244 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2245 
2246 	if (!iport->fabric.timer_pending) {
2247 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2248 		return;
2249 	}
2250 
2251 	if (iport->fabric.del_timer_inprogress) {
2252 		iport->fabric.del_timer_inprogress = 0;
2253 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2254 		FNIC_FCS_DBG(KERN_INFO, fnic,
2255 					 "fabric_del_timer inprogress(%d). Skip timer cb",
2256 					 iport->fabric.del_timer_inprogress);
2257 		return;
2258 	}
2259 
2260 	iport->fabric.timer_pending = 0;
2261 
2262 	/* The fabric state indicates which frames have time out, and we retry */
2263 	switch (iport->fabric.state) {
2264 	case FDLS_STATE_FABRIC_FLOGI:
2265 		/* Flogi received a LS_RJT with busy we retry from here */
2266 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2267 			&& (iport->fabric.retry_counter < iport->max_flogi_retries)) {
2268 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2269 			fdls_send_fabric_flogi(iport);
2270 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED)) {
2271 			/* Flogi has time out 2*ed_tov send abts */
2272 			fdls_send_fabric_abts(iport);
2273 		} else {
2274 			/* ABTS has timed out
2275 			 * Mark the OXID to be freed after 2 * r_a_tov and retry the req
2276 			 */
2277 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2278 			if (iport->fabric.retry_counter < iport->max_flogi_retries) {
2279 				iport->fabric.flags &= ~FNIC_FDLS_FABRIC_ABORT_ISSUED;
2280 				fdls_send_fabric_flogi(iport);
2281 			} else
2282 				FNIC_FCS_DBG(KERN_INFO, fnic,
2283 							 "Exceeded max FLOGI retries");
2284 		}
2285 		break;
2286 	case FDLS_STATE_FABRIC_PLOGI:
2287 		/* Plogi received a LS_RJT with busy we retry from here */
2288 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2289 			&& (iport->fabric.retry_counter < iport->max_plogi_retries)) {
2290 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2291 			fdls_send_fabric_plogi(iport);
2292 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED)) {
2293 		/* Plogi has timed out 2*ed_tov send abts */
2294 			fdls_send_fabric_abts(iport);
2295 		} else {
2296 			/* ABTS has timed out
2297 			 * Mark the OXID to be freed after 2 * r_a_tov and retry the req
2298 			 */
2299 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2300 			if (iport->fabric.retry_counter < iport->max_plogi_retries) {
2301 				iport->fabric.flags &= ~FNIC_FDLS_FABRIC_ABORT_ISSUED;
2302 				fdls_send_fabric_plogi(iport);
2303 			} else
2304 				FNIC_FCS_DBG(KERN_INFO, fnic,
2305 							 "Exceeded max PLOGI retries");
2306 		}
2307 		break;
2308 	case FDLS_STATE_RPN_ID:
2309 		/* Rpn_id received a LS_RJT with busy we retry from here */
2310 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2311 			&& (iport->fabric.retry_counter < FDLS_RETRY_COUNT)) {
2312 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2313 			fdls_send_rpn_id(iport);
2314 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED))
2315 			/* RPN has timed out. Send abts */
2316 			fdls_send_fabric_abts(iport);
2317 		else {
2318 			/* ABTS has timed out */
2319 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2320 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
2321 		}
2322 		break;
2323 	case FDLS_STATE_SCR:
2324 		/* scr received a LS_RJT with busy we retry from here */
2325 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2326 			&& (iport->fabric.retry_counter < FDLS_RETRY_COUNT)) {
2327 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2328 			fdls_send_scr(iport);
2329 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED))
2330 			/* scr has timed out. Send abts */
2331 			fdls_send_fabric_abts(iport);
2332 		else {
2333 			/* ABTS has timed out */
2334 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2335 			FNIC_FCS_DBG(KERN_INFO, fnic,
2336 						 "ABTS timed out. Starting PLOGI: %p", iport);
2337 			fnic_fdls_start_plogi(iport);
2338 		}
2339 		break;
2340 	case FDLS_STATE_REGISTER_FC4_TYPES:
2341 		/* scr received a LS_RJT with busy we retry from here */
2342 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2343 			&& (iport->fabric.retry_counter < FDLS_RETRY_COUNT)) {
2344 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2345 			fdls_send_register_fc4_types(iport);
2346 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED)) {
2347 			/* RFT_ID timed out send abts */
2348 			fdls_send_fabric_abts(iport);
2349 		} else {
2350 			/* ABTS has timed out */
2351 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2352 			FNIC_FCS_DBG(KERN_INFO, fnic,
2353 						 "ABTS timed out. Starting PLOGI: %p", iport);
2354 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
2355 		}
2356 		break;
2357 	case FDLS_STATE_REGISTER_FC4_FEATURES:
2358 		/* scr received a LS_RJT with busy we retry from here */
2359 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2360 			&& (iport->fabric.retry_counter < FDLS_RETRY_COUNT)) {
2361 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2362 			fdls_send_register_fc4_features(iport);
2363 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED))
2364 			/* SCR has timed out. Send abts */
2365 			fdls_send_fabric_abts(iport);
2366 		else {
2367 			/* ABTS has timed out */
2368 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2369 			FNIC_FCS_DBG(KERN_INFO, fnic,
2370 						 "ABTS timed out. Starting PLOGI %p", iport);
2371 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
2372 		}
2373 		break;
2374 	case FDLS_STATE_RSCN_GPN_FT:
2375 	case FDLS_STATE_SEND_GPNFT:
2376 	case FDLS_STATE_GPN_FT:
2377 		/* GPN_FT received a LS_RJT with busy we retry from here */
2378 		if ((iport->fabric.flags & FNIC_FDLS_RETRY_FRAME)
2379 			&& (iport->fabric.retry_counter < FDLS_RETRY_COUNT)) {
2380 			iport->fabric.flags &= ~FNIC_FDLS_RETRY_FRAME;
2381 			fdls_send_gpn_ft(iport, iport->fabric.state);
2382 		} else if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED)) {
2383 			/* gpn_ft has timed out. Send abts */
2384 			fdls_send_fabric_abts(iport);
2385 		} else {
2386 			/* ABTS has timed out */
2387 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fabric_req);
2388 			if (iport->fabric.retry_counter < FDLS_RETRY_COUNT) {
2389 				fdls_send_gpn_ft(iport, iport->fabric.state);
2390 			} else {
2391 				FNIC_FCS_DBG(KERN_INFO, fnic,
2392 					 "ABTS timeout for fabric GPN_FT. Check name server: %p",
2393 					 iport);
2394 			}
2395 		}
2396 		break;
2397 	default:
2398 		break;
2399 	}
2400 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2401 }
2402 
2403 void fdls_fdmi_retry_plogi(struct fnic_iport_s *iport)
2404 {
2405 	struct fnic *fnic = iport->fnic;
2406 
2407 	iport->fabric.fdmi_pending = 0;
2408 	/* If max retries not exhausted, start over from fdmi plogi */
2409 	if (iport->fabric.fdmi_retry < FDLS_FDMI_MAX_RETRY) {
2410 		iport->fabric.fdmi_retry++;
2411 		FNIC_FCS_DBG(KERN_INFO, fnic,
2412 					 "Retry FDMI PLOGI. FDMI retry: %d",
2413 					 iport->fabric.fdmi_retry);
2414 		fdls_send_fdmi_plogi(iport);
2415 	}
2416 }
2417 
2418 void fdls_fdmi_timer_callback(struct timer_list *t)
2419 {
2420 	struct fnic_fdls_fabric_s *fabric = timer_container_of(fabric, t,
2421 							       fdmi_timer);
2422 	struct fnic_iport_s *iport =
2423 		container_of(fabric, struct fnic_iport_s, fabric);
2424 	struct fnic *fnic = iport->fnic;
2425 	unsigned long flags;
2426 
2427 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2428 
2429 	FNIC_FCS_DBG(KERN_INFO, fnic,
2430 		"iport->fabric.fdmi_pending: 0x%x\n", iport->fabric.fdmi_pending);
2431 
2432 	if (!iport->fabric.fdmi_pending) {
2433 		/* timer expired after fdmi responses received. */
2434 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2435 		return;
2436 	}
2437 	FNIC_FCS_DBG(KERN_INFO, fnic,
2438 		"iport->fabric.fdmi_pending: 0x%x\n", iport->fabric.fdmi_pending);
2439 
2440 	/* if not abort pending, send an abort */
2441 	if (!(iport->fabric.fdmi_pending & FDLS_FDMI_ABORT_PENDING)) {
2442 		fdls_send_fdmi_abts(iport);
2443 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2444 		return;
2445 	}
2446 	FNIC_FCS_DBG(KERN_INFO, fnic,
2447 		"iport->fabric.fdmi_pending: 0x%x\n", iport->fabric.fdmi_pending);
2448 
2449 	/* ABTS pending for an active fdmi request that is pending.
2450 	 * That means FDMI ABTS timed out
2451 	 * Schedule to free the OXID after 2*r_a_tov and proceed
2452 	 */
2453 	if (iport->fabric.fdmi_pending & FDLS_FDMI_PLOGI_PENDING) {
2454 		FNIC_FCS_DBG(KERN_INFO, fnic,
2455 			"FDMI PLOGI ABTS timed out. Schedule oxid free: 0x%x\n",
2456 			iport->active_oxid_fdmi_plogi);
2457 		fdls_schedule_oxid_free(iport, &iport->active_oxid_fdmi_plogi);
2458 	} else {
2459 		if (iport->fabric.fdmi_pending & FDLS_FDMI_REG_HBA_PENDING) {
2460 			FNIC_FCS_DBG(KERN_INFO, fnic,
2461 						"FDMI RHBA ABTS timed out. Schedule oxid free: 0x%x\n",
2462 						iport->active_oxid_fdmi_rhba);
2463 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fdmi_rhba);
2464 		}
2465 		if (iport->fabric.fdmi_pending & FDLS_FDMI_RPA_PENDING) {
2466 			FNIC_FCS_DBG(KERN_INFO, fnic,
2467 						"FDMI RPA ABTS timed out. Schedule oxid free: 0x%x\n",
2468 						iport->active_oxid_fdmi_rpa);
2469 			fdls_schedule_oxid_free(iport, &iport->active_oxid_fdmi_rpa);
2470 		}
2471 	}
2472 	FNIC_FCS_DBG(KERN_INFO, fnic,
2473 		"iport->fabric.fdmi_pending: 0x%x\n", iport->fabric.fdmi_pending);
2474 
2475 	iport->fabric.fdmi_pending = 0;
2476 	/* If max retries not exhaused, start over from fdmi plogi */
2477 	if (iport->fabric.fdmi_retry < FDLS_FDMI_MAX_RETRY) {
2478 		iport->fabric.fdmi_retry++;
2479 		FNIC_FCS_DBG(KERN_INFO, fnic,
2480 					 "retry fdmi timer %d", iport->fabric.fdmi_retry);
2481 		fdls_send_fdmi_plogi(iport);
2482 	}
2483 	FNIC_FCS_DBG(KERN_INFO, fnic,
2484 		"iport->fabric.fdmi_pending: 0x%x\n", iport->fabric.fdmi_pending);
2485 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2486 }
2487 
2488 static void fdls_send_delete_tport_msg(struct fnic_tport_s *tport)
2489 {
2490 	struct fnic_iport_s *iport = (struct fnic_iport_s *) tport->iport;
2491 	struct fnic *fnic = iport->fnic;
2492 	struct fnic_tport_event_s *tport_del_evt;
2493 
2494 	tport_del_evt = kzalloc_obj(struct fnic_tport_event_s, GFP_ATOMIC);
2495 	if (!tport_del_evt) {
2496 		FNIC_FCS_DBG(KERN_INFO, fnic,
2497 			 "Failed to allocate memory for tport event fcid: 0x%x",
2498 			 tport->fcid);
2499 		return;
2500 	}
2501 	tport_del_evt->event = TGT_EV_TPORT_DELETE;
2502 	tport_del_evt->arg1 = (void *) tport;
2503 	list_add_tail(&tport_del_evt->links, &fnic->tport_event_list);
2504 	queue_work(fnic_event_queue, &fnic->tport_work);
2505 }
2506 
2507 static void fdls_tport_timer_callback(struct timer_list *t)
2508 {
2509 	struct fnic_tport_s *tport = timer_container_of(tport, t, retry_timer);
2510 	struct fnic_iport_s *iport = (struct fnic_iport_s *) tport->iport;
2511 	struct fnic *fnic = iport->fnic;
2512 	uint16_t oxid;
2513 	unsigned long flags;
2514 	struct fc_frame_header fchdr = {0};
2515 	uint8_t fcid[3];
2516 
2517 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2518 	if (!tport->timer_pending) {
2519 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2520 		return;
2521 	}
2522 
2523 	if (iport->state != FNIC_IPORT_STATE_READY) {
2524 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2525 		return;
2526 	}
2527 
2528 	if (tport->del_timer_inprogress) {
2529 		tport->del_timer_inprogress = 0;
2530 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2531 		FNIC_FCS_DBG(KERN_INFO, fnic,
2532 			 "tport_del_timer inprogress. Skip timer cb tport fcid: 0x%x\n",
2533 			 tport->fcid);
2534 		return;
2535 	}
2536 
2537 	FNIC_FCS_DBG(KERN_INFO, fnic,
2538 		 "tport fcid: 0x%x timer pending: %d state: %d retry counter: %d",
2539 		 tport->fcid, tport->timer_pending, tport->state,
2540 		 tport->retry_counter);
2541 
2542 	tport->timer_pending = 0;
2543 	oxid = tport->active_oxid;
2544 
2545 	/* We retry plogi/prli/adisc frames depending on the tport state */
2546 	switch (tport->state) {
2547 	case FDLS_TGT_STATE_PLOGI:
2548 		/* PLOGI frame received a LS_RJT with busy, we retry from here */
2549 		if ((tport->flags & FNIC_FDLS_RETRY_FRAME)
2550 			&& (tport->retry_counter < iport->max_plogi_retries)) {
2551 			tport->flags &= ~FNIC_FDLS_RETRY_FRAME;
2552 			fdls_send_tgt_plogi(iport, tport);
2553 		} else if (!(tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2554 			/* Plogi frame has timed out, send abts */
2555 			fdls_send_tport_abts(iport, tport);
2556 		} else if (tport->retry_counter < iport->max_plogi_retries) {
2557 			/*
2558 			 * ABTS has timed out
2559 			 */
2560 			fdls_schedule_oxid_free(iport, &tport->active_oxid);
2561 			fdls_send_tgt_plogi(iport, tport);
2562 		} else {
2563 			/* exceeded plogi retry count */
2564 			fdls_schedule_oxid_free(iport, &tport->active_oxid);
2565 			fdls_send_delete_tport_msg(tport);
2566 		}
2567 		break;
2568 	case FDLS_TGT_STATE_PRLI:
2569 		/* PRLI received a LS_RJT with busy , hence we retry from here */
2570 		if ((tport->flags & FNIC_FDLS_RETRY_FRAME)
2571 			&& (tport->retry_counter < FDLS_RETRY_COUNT)) {
2572 			tport->flags &= ~FNIC_FDLS_RETRY_FRAME;
2573 			fdls_send_tgt_prli(iport, tport);
2574 		} else if (!(tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2575 			/* PRLI has time out, send abts */
2576 			fdls_send_tport_abts(iport, tport);
2577 		} else {
2578 			/* ABTS has timed out for prli, we go back to PLOGI */
2579 			fdls_schedule_oxid_free(iport, &tport->active_oxid);
2580 			fdls_send_tgt_plogi(iport, tport);
2581 			fdls_set_tport_state(tport, FDLS_TGT_STATE_PLOGI);
2582 		}
2583 		break;
2584 	case FDLS_TGT_STATE_ADISC:
2585 		/* ADISC timed out send an ABTS */
2586 		if (!(tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2587 			fdls_send_tport_abts(iport, tport);
2588 		} else if ((tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)
2589 				   && (tport->retry_counter < FDLS_RETRY_COUNT)) {
2590 			/*
2591 			 * ABTS has timed out
2592 			 */
2593 			fdls_schedule_oxid_free(iport, &tport->active_oxid);
2594 			fdls_send_tgt_adisc(iport, tport);
2595 		} else {
2596 			/* exceeded retry count */
2597 			fdls_schedule_oxid_free(iport, &tport->active_oxid);
2598 			FNIC_FCS_DBG(KERN_INFO, fnic,
2599 					 "ADISC not responding. Deleting target port: 0x%x",
2600 					 tport->fcid);
2601 			fdls_send_delete_tport_msg(tport);
2602 		}
2603 		break;
2604 	default:
2605 		FNIC_FCS_DBG(KERN_INFO, fnic,
2606 			"0x%x timeout tport 0x%x oxid 0x%x state %d\n",
2607 			iport->fcid, tport->fcid, oxid, tport->state);
2608 		if (IS_FNIC_NVME_INITIATOR(fnic)) {
2609 			hton24(fcid, tport->fcid);
2610 			FNIC_STD_SET_S_ID(fchdr, fcid);
2611 			FNIC_STD_SET_OX_ID(fchdr, oxid);
2612 			nvfnic_process_ls_abts_rsp(iport, &fchdr);
2613 		}
2614 		break;
2615 	}
2616 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2617 }
2618 
2619 static void fnic_fdls_start_flogi(struct fnic_iport_s *iport)
2620 {
2621 	iport->fabric.retry_counter = 0;
2622 	fdls_send_fabric_flogi(iport);
2623 	fdls_set_state((&iport->fabric), FDLS_STATE_FABRIC_FLOGI);
2624 	iport->fabric.flags = 0;
2625 }
2626 
2627 static void fnic_fdls_start_plogi(struct fnic_iport_s *iport)
2628 {
2629 	iport->fabric.retry_counter = 0;
2630 	fdls_send_fabric_plogi(iport);
2631 	fdls_set_state((&iport->fabric), FDLS_STATE_FABRIC_PLOGI);
2632 	iport->fabric.flags &= ~FNIC_FDLS_FABRIC_ABORT_ISSUED;
2633 
2634 	if ((fnic_fdmi_support == 1) && (!(iport->flags & FNIC_FDMI_ACTIVE))) {
2635 		/* we can do FDMI at the same time */
2636 		iport->fabric.fdmi_retry = 0;
2637 		timer_setup(&iport->fabric.fdmi_timer, fdls_fdmi_timer_callback,
2638 					0);
2639 		fdls_send_fdmi_plogi(iport);
2640 		iport->flags |= FNIC_FDMI_ACTIVE;
2641 	}
2642 }
2643 static void
2644 fdls_process_tgt_adisc_rsp(struct fnic_iport_s *iport,
2645 			   struct fc_frame_header *fchdr)
2646 {
2647 	uint32_t tgt_fcid;
2648 	struct fnic_tport_s *tport;
2649 	uint8_t *fcid;
2650 	uint64_t frame_wwnn;
2651 	uint64_t frame_wwpn;
2652 	uint16_t oxid;
2653 	struct fc_std_els_adisc *adisc_rsp = (struct fc_std_els_adisc *)fchdr;
2654 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *)fchdr;
2655 	struct fnic *fnic = iport->fnic;
2656 
2657 	fcid = FNIC_STD_GET_S_ID(fchdr);
2658 	tgt_fcid = ntoh24(fcid);
2659 	tport = fnic_find_tport_by_fcid(iport, tgt_fcid);
2660 
2661 	if (!tport) {
2662 		FNIC_FCS_DBG(KERN_INFO, fnic,
2663 					 "Tgt ADISC response tport not found: 0x%x", tgt_fcid);
2664 		return;
2665 	}
2666 	if ((iport->state != FNIC_IPORT_STATE_READY)
2667 		|| (tport->state != FDLS_TGT_STATE_ADISC)
2668 		|| (tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2669 		FNIC_FCS_DBG(KERN_INFO, fnic,
2670 				 "Dropping this ADISC response");
2671 		FNIC_FCS_DBG(KERN_INFO, fnic,
2672 			 "iport state: %d tport state: %d Is abort issued on PRLI? %d",
2673 			 iport->state, tport->state,
2674 			 (tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED));
2675 		return;
2676 	}
2677 	if (FNIC_STD_GET_OX_ID(fchdr) != tport->active_oxid) {
2678 		FNIC_FCS_DBG(KERN_INFO, fnic,
2679 			 "Dropping frame from target: 0x%x",
2680 			 tgt_fcid);
2681 		FNIC_FCS_DBG(KERN_INFO, fnic,
2682 			 "Reason: Stale ADISC/Aborted ADISC/OOO frame delivery");
2683 		return;
2684 	}
2685 
2686 	oxid = FNIC_STD_GET_OX_ID(fchdr);
2687 	fdls_free_oxid(iport, oxid, &tport->active_oxid);
2688 
2689 	switch (adisc_rsp->els.adisc_cmd) {
2690 	case ELS_LS_ACC:
2691 		atomic64_inc(&iport->iport_stats.tport_adisc_ls_accepts);
2692 		if (tport->timer_pending) {
2693 			FNIC_FCS_DBG(KERN_INFO, fnic,
2694 						 "tport 0x%p Canceling fabric disc timer\n",
2695 						 tport);
2696 			fnic_del_tport_timer_sync(fnic, tport);
2697 		}
2698 		tport->timer_pending = 0;
2699 		tport->retry_counter = 0;
2700 		frame_wwnn = get_unaligned_be64(&adisc_rsp->els.adisc_wwnn);
2701 		frame_wwpn = get_unaligned_be64(&adisc_rsp->els.adisc_wwpn);
2702 		if ((frame_wwnn == tport->wwnn) && (frame_wwpn == tport->wwpn)) {
2703 			FNIC_FCS_DBG(KERN_INFO, fnic,
2704 				 "ADISC accepted from target: 0x%x. Target logged in",
2705 				 tgt_fcid);
2706 			fdls_set_tport_state(tport, FDLS_TGT_STATE_READY);
2707 		} else {
2708 			FNIC_FCS_DBG(KERN_INFO, fnic,
2709 						 "Error mismatch frame: ADISC");
2710 		}
2711 		break;
2712 
2713 	case ELS_LS_RJT:
2714 		atomic64_inc(&iport->iport_stats.tport_adisc_ls_rejects);
2715 		if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
2716 		     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
2717 			&& (tport->retry_counter < FDLS_RETRY_COUNT)) {
2718 			FNIC_FCS_DBG(KERN_INFO, fnic,
2719 				 "ADISC ret ELS_LS_RJT BUSY. Retry from timer routine: 0x%x",
2720 				 tgt_fcid);
2721 
2722 			/* Retry ADISC again from the timer routine. */
2723 			tport->flags |= FNIC_FDLS_RETRY_FRAME;
2724 		} else {
2725 			FNIC_FCS_DBG(KERN_INFO, fnic,
2726 						 "ADISC returned ELS_LS_RJT from target: 0x%x",
2727 						 tgt_fcid);
2728 			fdls_delete_tport(iport, tport);
2729 		}
2730 		break;
2731 	}
2732 }
2733 static void
2734 fdls_process_tgt_plogi_rsp(struct fnic_iport_s *iport,
2735 			   struct fc_frame_header *fchdr)
2736 {
2737 	uint32_t tgt_fcid;
2738 	struct fnic_tport_s *tport;
2739 	uint8_t *fcid;
2740 	uint16_t oxid;
2741 	struct fc_std_flogi *plogi_rsp = (struct fc_std_flogi *)fchdr;
2742 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *)fchdr;
2743 	uint16_t max_payload_size;
2744 	struct fnic *fnic = iport->fnic;
2745 
2746 	fcid = FNIC_STD_GET_S_ID(fchdr);
2747 	tgt_fcid = ntoh24(fcid);
2748 
2749 	FNIC_FCS_DBG(KERN_INFO, fnic,
2750 				 "FDLS processing target PLOGI response: tgt_fcid: 0x%x",
2751 				 tgt_fcid);
2752 
2753 	tport = fnic_find_tport_by_fcid(iport, tgt_fcid);
2754 	if (!tport) {
2755 		FNIC_FCS_DBG(KERN_INFO, fnic,
2756 					 "tport not found: 0x%x", tgt_fcid);
2757 		return;
2758 	}
2759 	if ((iport->state != FNIC_IPORT_STATE_READY)
2760 		|| (tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2761 		FNIC_FCS_DBG(KERN_INFO, fnic,
2762 					 "Dropping frame! iport state: %d tport state: %d",
2763 					 iport->state, tport->state);
2764 		return;
2765 	}
2766 
2767 	if (tport->state != FDLS_TGT_STATE_PLOGI) {
2768 		FNIC_FCS_DBG(KERN_INFO, fnic,
2769 			     "PLOGI rsp recvd in wrong state. Drop the frame and restart nexus");
2770 		fdls_target_restart_nexus(tport);
2771 		return;
2772 	}
2773 
2774 	if (FNIC_STD_GET_OX_ID(fchdr) != tport->active_oxid) {
2775 		FNIC_FCS_DBG(KERN_INFO, fnic,
2776 			 "PLOGI response from target: 0x%x. Dropping frame",
2777 			 tgt_fcid);
2778 		return;
2779 	}
2780 
2781 	oxid = FNIC_STD_GET_OX_ID(fchdr);
2782 	fdls_free_oxid(iport, oxid, &tport->active_oxid);
2783 
2784 	switch (plogi_rsp->els.fl_cmd) {
2785 	case ELS_LS_ACC:
2786 		atomic64_inc(&iport->iport_stats.tport_plogi_ls_accepts);
2787 		FNIC_FCS_DBG(KERN_INFO, fnic,
2788 					 "PLOGI accepted by target: 0x%x", tgt_fcid);
2789 		break;
2790 
2791 	case ELS_LS_RJT:
2792 		atomic64_inc(&iport->iport_stats.tport_plogi_ls_rejects);
2793 		if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
2794 		     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
2795 			&& (tport->retry_counter < iport->max_plogi_retries)) {
2796 			FNIC_FCS_DBG(KERN_INFO, fnic,
2797 				 "PLOGI ret ELS_LS_RJT BUSY. Retry from timer routine: 0x%x",
2798 				 tgt_fcid);
2799 			/* Retry plogi again from the timer routine. */
2800 			tport->flags |= FNIC_FDLS_RETRY_FRAME;
2801 			return;
2802 		}
2803 		FNIC_FCS_DBG(KERN_INFO, fnic,
2804 					 "PLOGI returned ELS_LS_RJT from target: 0x%x",
2805 					 tgt_fcid);
2806 		fdls_delete_tport(iport, tport);
2807 		return;
2808 
2809 	default:
2810 		atomic64_inc(&iport->iport_stats.tport_plogi_misc_rejects);
2811 		FNIC_FCS_DBG(KERN_INFO, fnic,
2812 					 "PLOGI not accepted from target fcid: 0x%x",
2813 					 tgt_fcid);
2814 		return;
2815 	}
2816 
2817 	FNIC_FCS_DBG(KERN_INFO, fnic,
2818 				 "Found the PLOGI target: 0x%x and state: %d",
2819 				 (unsigned int) tgt_fcid, tport->state);
2820 
2821 	if (tport->timer_pending) {
2822 		FNIC_FCS_DBG(KERN_INFO, fnic,
2823 					 "tport fcid 0x%x: Canceling disc timer\n",
2824 					 tport->fcid);
2825 		fnic_del_tport_timer_sync(fnic, tport);
2826 	}
2827 
2828 	tport->timer_pending = 0;
2829 	tport->wwpn = get_unaligned_be64(&FNIC_LOGI_PORT_NAME(plogi_rsp->els));
2830 	tport->wwnn = get_unaligned_be64(&FNIC_LOGI_NODE_NAME(plogi_rsp->els));
2831 
2832 	/* Learn the Service Params */
2833 
2834 	/* Max frame size - choose the lowest */
2835 	max_payload_size = fnic_fc_plogi_rsp_rdf(iport, plogi_rsp);
2836 	tport->max_payload_size =
2837 		min(max_payload_size, iport->max_payload_size);
2838 
2839 	if (tport->max_payload_size < FNIC_MIN_DATA_FIELD_SIZE) {
2840 		FNIC_FCS_DBG(KERN_INFO, fnic,
2841 			 "MFS: tport max frame size below spec bounds: %d",
2842 			 tport->max_payload_size);
2843 		tport->max_payload_size = FNIC_MIN_DATA_FIELD_SIZE;
2844 	}
2845 
2846 	FNIC_FCS_DBG(KERN_INFO, fnic,
2847 		 "MAX frame size: %u iport max_payload_size: %d tport mfs: %d",
2848 		 max_payload_size, iport->max_payload_size,
2849 		 tport->max_payload_size);
2850 
2851 	tport->max_concur_seqs = FNIC_FC_PLOGI_RSP_CONCUR_SEQ(plogi_rsp);
2852 
2853 	tport->retry_counter = 0;
2854 	fdls_set_tport_state(tport, FDLS_TGT_STATE_PRLI);
2855 	fdls_send_tgt_prli(iport, tport);
2856 }
2857 static void
2858 fdls_process_tgt_prli_rsp(struct fnic_iport_s *iport,
2859 			  struct fc_frame_header *fchdr)
2860 {
2861 	uint32_t tgt_fcid;
2862 	struct fnic_tport_s *tport;
2863 	uint8_t *fcid;
2864 	uint16_t oxid;
2865 	struct fc_std_els_prli *prli_rsp = (struct fc_std_els_prli *)fchdr;
2866 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *)fchdr;
2867 	struct fnic_tport_event_s *tport_add_evt;
2868 	struct fnic *fnic = iport->fnic;
2869 	bool mismatched_tgt = false;
2870 
2871 	fcid = FNIC_STD_GET_S_ID(fchdr);
2872 	tgt_fcid = ntoh24(fcid);
2873 
2874 	FNIC_FCS_DBG(KERN_INFO, fnic,
2875 				 "FDLS process tgt PRLI response: 0x%x", tgt_fcid);
2876 
2877 	tport = fnic_find_tport_by_fcid(iport, tgt_fcid);
2878 	if (!tport) {
2879 		FNIC_FCS_DBG(KERN_INFO, fnic,
2880 					 "tport not found: 0x%x", tgt_fcid);
2881 		/* Handle or just drop? */
2882 		return;
2883 	}
2884 
2885 	if ((iport->state != FNIC_IPORT_STATE_READY)
2886 		|| (tport->flags & FNIC_FDLS_TGT_ABORT_ISSUED)) {
2887 		FNIC_FCS_DBG(KERN_INFO, fnic,
2888 			 "Dropping frame! iport st: %d tport st: %d tport fcid: 0x%x",
2889 			 iport->state, tport->state, tport->fcid);
2890 		return;
2891 	}
2892 
2893 	if (tport->state != FDLS_TGT_STATE_PRLI) {
2894 		FNIC_FCS_DBG(KERN_INFO, fnic,
2895 			     "PRLI rsp recvd in wrong state. Drop frame. Restarting nexus");
2896 		fdls_target_restart_nexus(tport);
2897 		return;
2898 	}
2899 
2900 	if (FNIC_STD_GET_OX_ID(fchdr) != tport->active_oxid) {
2901 		FNIC_FCS_DBG(KERN_INFO, fnic,
2902 			 "Dropping PRLI response from target: 0x%x ",
2903 			 tgt_fcid);
2904 		FNIC_FCS_DBG(KERN_INFO, fnic,
2905 			 "Reason: Stale PRLI response/Aborted PDISC/OOO frame delivery");
2906 		return;
2907 	}
2908 
2909 	oxid = FNIC_STD_GET_OX_ID(fchdr);
2910 	fdls_free_oxid(iport, oxid, &tport->active_oxid);
2911 
2912 	switch (prli_rsp->els_prli.prli_cmd) {
2913 	case ELS_LS_ACC:
2914 		atomic64_inc(&iport->iport_stats.tport_prli_ls_accepts);
2915 		FNIC_FCS_DBG(KERN_INFO, fnic,
2916 					 "PRLI accepted from target: 0x%x", tgt_fcid);
2917 
2918 		if (IS_FNIC_FCP_INITIATOR(fnic) &&
2919 		    prli_rsp->sp.spp_type != FC_FC4_TYPE_SCSI) {
2920 			FNIC_FCS_DBG(KERN_INFO, fnic,
2921 				 "mismatched target zoned with FC SCSI initiator: 0x%x",
2922 				 tgt_fcid);
2923 			mismatched_tgt = true;
2924 		} else if (IS_FNIC_NVME_INITIATOR(fnic) &&
2925 			   prli_rsp->sp.spp_type != FC_TYPE_NVME) {
2926 			FNIC_FCS_DBG(KERN_ERR, fnic,
2927 				 "mismatched target zoned with NVME initiator: 0x%x",
2928 				 tgt_fcid);
2929 			mismatched_tgt = true;
2930 		}
2931 		if (mismatched_tgt) {
2932 			fdls_tgt_logout(iport, tport);
2933 			fdls_delete_tport(iport, tport);
2934 			return;
2935 		}
2936 		break;
2937 	case ELS_LS_RJT:
2938 		atomic64_inc(&iport->iport_stats.tport_prli_ls_rejects);
2939 		if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
2940 		     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
2941 			&& (tport->retry_counter < FDLS_RETRY_COUNT)) {
2942 
2943 			FNIC_FCS_DBG(KERN_INFO, fnic,
2944 				 "PRLI ret ELS_LS_RJT BUSY. Retry from timer routine: 0x%x",
2945 				 tgt_fcid);
2946 
2947 			/*Retry Plogi again from the timer routine. */
2948 			tport->flags |= FNIC_FDLS_RETRY_FRAME;
2949 			return;
2950 		}
2951 		FNIC_FCS_DBG(KERN_INFO, fnic,
2952 					 "PRLI returned ELS_LS_RJT from target: 0x%x",
2953 					 tgt_fcid);
2954 
2955 		fdls_tgt_logout(iport, tport);
2956 		fdls_delete_tport(iport, tport);
2957 		return;
2958 	default:
2959 		atomic64_inc(&iport->iport_stats.tport_prli_misc_rejects);
2960 		FNIC_FCS_DBG(KERN_INFO, fnic,
2961 					 "PRLI not accepted from target: 0x%x", tgt_fcid);
2962 		return;
2963 	}
2964 
2965 	FNIC_FCS_DBG(KERN_INFO, fnic,
2966 				 "Found the PRLI target: 0x%x and state: %d",
2967 				 (unsigned int) tgt_fcid, tport->state);
2968 
2969 	if (tport->timer_pending) {
2970 		FNIC_FCS_DBG(KERN_INFO, fnic,
2971 					 "tport fcid 0x%x: Canceling disc timer\n",
2972 					 tport->fcid);
2973 		fnic_del_tport_timer_sync(fnic, tport);
2974 	}
2975 	tport->timer_pending = 0;
2976 
2977 	/* Learn Service Params */
2978 	tport->fcp_csp = be32_to_cpu(prli_rsp->sp.spp_params);
2979 	tport->retry_counter = 0;
2980 
2981 	if (IS_FNIC_FCP_INITIATOR(fnic)) {
2982 		if (tport->fcp_csp & FCP_SPPF_RETRY)
2983 			tport->tgt_flags |= FNIC_FC_RP_FLAGS_RETRY;
2984 	} else if (IS_FNIC_NVME_INITIATOR(fnic)) {
2985 		if (tport->fcp_csp & FNIC_NVME_SP_SLER)
2986 			tport->tgt_flags |= FNIC_FC_RP_FLAGS_RETRY;
2987 	}
2988 
2989 	/* Check if the device plays Target Mode Function */
2990 	if ((IS_FNIC_FCP_INITIATOR(fnic) &&
2991 	     !(tport->fcp_csp & FCP_PRLI_FUNC_TARGET)) ||
2992 	    (IS_FNIC_NVME_INITIATOR(fnic) &&
2993 	     !(tport->fcp_csp & FCP_PRLI_FUNC_TARGET))) {
2994 		FNIC_FCS_DBG(KERN_INFO, fnic,
2995 				 "Remote port(0x%x): no target support. Deleting it\n",
2996 				 tgt_fcid);
2997 		fdls_tgt_logout(iport, tport);
2998 		fdls_delete_tport(iport, tport);
2999 		return;
3000 	}
3001 
3002 	fdls_set_tport_state(tport, FDLS_TGT_STATE_READY);
3003 
3004 	/* Inform the driver about new target added */
3005 	if (IS_FNIC_FCP_INITIATOR(fnic) ||
3006 	    IS_FNIC_NVME_INITIATOR(fnic)) {
3007 		tport_add_evt = kzalloc_obj(struct fnic_tport_event_s, GFP_ATOMIC);
3008 		if (!tport_add_evt) {
3009 			FNIC_FCS_DBG(KERN_INFO, fnic,
3010 				     "iport fcid: 0x%x tport event memory allocation failure: 0x%0x\n",
3011 				     iport->fcid, tport->fcid);
3012 			return;
3013 		}
3014 		tport_add_evt->event = TGT_EV_RPORT_ADD;
3015 		tport_add_evt->arg1 = (void *)tport;
3016 		FNIC_FCS_DBG(KERN_INFO, fnic,
3017 			     "iport fcid: 0x%x add tport event fcid: 0x%x\n",
3018 			     tport->fcid, iport->fcid);
3019 		list_add_tail(&tport_add_evt->links, &fnic->tport_event_list);
3020 		queue_work(fnic_event_queue, &fnic->tport_work);
3021 	}
3022 }
3023 
3024 
3025 static void
3026 fdls_process_rff_id_rsp(struct fnic_iport_s *iport,
3027 			struct fc_frame_header *fchdr)
3028 {
3029 	struct fnic *fnic = iport->fnic;
3030 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3031 	struct fc_std_rff_id *rff_rsp = (struct fc_std_rff_id *) fchdr;
3032 	uint16_t rsp;
3033 	uint8_t reason_code;
3034 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3035 
3036 	if (fdls_get_state(fdls) != FDLS_STATE_REGISTER_FC4_FEATURES) {
3037 		FNIC_FCS_DBG(KERN_INFO, fnic,
3038 					 "RFF_ID resp recvd in state(%d). Dropping.",
3039 					 fdls_get_state(fdls));
3040 		return;
3041 	}
3042 
3043 	if (iport->active_oxid_fabric_req != oxid) {
3044 		FNIC_FCS_DBG(KERN_INFO, fnic,
3045 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3046 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3047 		return;
3048 	}
3049 
3050 	rsp = FNIC_STD_GET_FC_CT_CMD((&rff_rsp->fc_std_ct_hdr));
3051 	FNIC_FCS_DBG(KERN_INFO, fnic,
3052 				 "0x%x: FDLS process RFF ID response: 0x%04x", iport->fcid,
3053 				 (uint32_t) rsp);
3054 
3055 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3056 
3057 	switch (rsp) {
3058 	case FC_FS_ACC:
3059 		if (iport->fabric.timer_pending) {
3060 			FNIC_FCS_DBG(KERN_INFO, fnic,
3061 						 "Canceling fabric disc timer %p\n", iport);
3062 			fnic_del_fabric_timer_sync(fnic);
3063 		}
3064 		iport->fabric.timer_pending = 0;
3065 		fdls->retry_counter = 0;
3066 		fdls_set_state((&iport->fabric), FDLS_STATE_SCR);
3067 		fdls_send_scr(iport);
3068 		break;
3069 	case FC_FS_RJT:
3070 		reason_code = rff_rsp->fc_std_ct_hdr.ct_reason;
3071 		if (((reason_code == FC_FS_RJT_BSY)
3072 			|| (reason_code == FC_FS_RJT_UNABL))
3073 			&& (fdls->retry_counter < FDLS_RETRY_COUNT)) {
3074 			FNIC_FCS_DBG(KERN_INFO, fnic,
3075 					 "RFF_ID ret ELS_LS_RJT BUSY. Retry from timer routine %p",
3076 					 iport);
3077 
3078 			/* Retry again from the timer routine */
3079 			fdls->flags |= FNIC_FDLS_RETRY_FRAME;
3080 		} else {
3081 			FNIC_FCS_DBG(KERN_INFO, fnic,
3082 			 "RFF_ID returned ELS_LS_RJT. Halting discovery %p",
3083 			 iport);
3084 			if (iport->fabric.timer_pending) {
3085 				FNIC_FCS_DBG(KERN_INFO, fnic,
3086 							 "Canceling fabric disc timer %p\n", iport);
3087 				fnic_del_fabric_timer_sync(fnic);
3088 			}
3089 			fdls->timer_pending = 0;
3090 			fdls->retry_counter = 0;
3091 		}
3092 		break;
3093 	default:
3094 		break;
3095 	}
3096 }
3097 
3098 static void
3099 fdls_process_rft_id_rsp(struct fnic_iport_s *iport,
3100 			struct fc_frame_header *fchdr)
3101 {
3102 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3103 	struct fc_std_rft_id *rft_rsp = (struct fc_std_rft_id *) fchdr;
3104 	uint16_t rsp;
3105 	uint8_t reason_code;
3106 	struct fnic *fnic = iport->fnic;
3107 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3108 
3109 	if (fdls_get_state(fdls) != FDLS_STATE_REGISTER_FC4_TYPES) {
3110 		FNIC_FCS_DBG(KERN_INFO, fnic,
3111 					 "RFT_ID resp recvd in state(%d). Dropping.",
3112 					 fdls_get_state(fdls));
3113 		return;
3114 	}
3115 
3116 	if (iport->active_oxid_fabric_req != oxid) {
3117 		FNIC_FCS_DBG(KERN_INFO, fnic,
3118 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3119 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3120 		return;
3121 	}
3122 
3123 
3124 	rsp = FNIC_STD_GET_FC_CT_CMD((&rft_rsp->fc_std_ct_hdr));
3125 	FNIC_FCS_DBG(KERN_INFO, fnic,
3126 				 "0x%x: FDLS process RFT ID response: 0x%04x", iport->fcid,
3127 				 (uint32_t) rsp);
3128 
3129 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3130 
3131 	switch (rsp) {
3132 	case FC_FS_ACC:
3133 		if (iport->fabric.timer_pending) {
3134 			FNIC_FCS_DBG(KERN_INFO, fnic,
3135 						 "Canceling fabric disc timer %p\n", iport);
3136 			fnic_del_fabric_timer_sync(fnic);
3137 		}
3138 		iport->fabric.timer_pending = 0;
3139 		fdls->retry_counter = 0;
3140 		fdls_send_register_fc4_features(iport);
3141 		fdls_set_state((&iport->fabric), FDLS_STATE_REGISTER_FC4_FEATURES);
3142 		break;
3143 	case FC_FS_RJT:
3144 		reason_code = rft_rsp->fc_std_ct_hdr.ct_reason;
3145 		if (((reason_code == FC_FS_RJT_BSY)
3146 			|| (reason_code == FC_FS_RJT_UNABL))
3147 			&& (fdls->retry_counter < FDLS_RETRY_COUNT)) {
3148 			FNIC_FCS_DBG(KERN_INFO, fnic,
3149 				 "0x%x: RFT_ID ret ELS_LS_RJT BUSY. Retry from timer routine",
3150 				 iport->fcid);
3151 
3152 			/* Retry again from the timer routine */
3153 			fdls->flags |= FNIC_FDLS_RETRY_FRAME;
3154 		} else {
3155 			FNIC_FCS_DBG(KERN_INFO, fnic,
3156 				 "0x%x: RFT_ID REJ. Halting discovery reason %d expl %d",
3157 				 iport->fcid, reason_code,
3158 			 rft_rsp->fc_std_ct_hdr.ct_explan);
3159 			if (iport->fabric.timer_pending) {
3160 				FNIC_FCS_DBG(KERN_INFO, fnic,
3161 							 "Canceling fabric disc timer %p\n", iport);
3162 				fnic_del_fabric_timer_sync(fnic);
3163 			}
3164 			fdls->timer_pending = 0;
3165 			fdls->retry_counter = 0;
3166 		}
3167 		break;
3168 	default:
3169 		break;
3170 	}
3171 }
3172 
3173 static void
3174 fdls_process_rpn_id_rsp(struct fnic_iport_s *iport,
3175 			struct fc_frame_header *fchdr)
3176 {
3177 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3178 	struct fc_std_rpn_id *rpn_rsp = (struct fc_std_rpn_id *) fchdr;
3179 	uint16_t rsp;
3180 	uint8_t reason_code;
3181 	struct fnic *fnic = iport->fnic;
3182 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3183 
3184 	if (fdls_get_state(fdls) != FDLS_STATE_RPN_ID) {
3185 		FNIC_FCS_DBG(KERN_INFO, fnic,
3186 					 "RPN_ID resp recvd in state(%d). Dropping.",
3187 					 fdls_get_state(fdls));
3188 		return;
3189 	}
3190 	if (iport->active_oxid_fabric_req != oxid) {
3191 		FNIC_FCS_DBG(KERN_INFO, fnic,
3192 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3193 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3194 		return;
3195 	}
3196 
3197 	rsp = FNIC_STD_GET_FC_CT_CMD((&rpn_rsp->fc_std_ct_hdr));
3198 	FNIC_FCS_DBG(KERN_INFO, fnic,
3199 				 "0x%x: FDLS process RPN ID response: 0x%04x", iport->fcid,
3200 				 (uint32_t) rsp);
3201 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3202 
3203 	switch (rsp) {
3204 	case FC_FS_ACC:
3205 		if (iport->fabric.timer_pending) {
3206 			FNIC_FCS_DBG(KERN_INFO, fnic,
3207 						 "Canceling fabric disc timer %p\n", iport);
3208 			fnic_del_fabric_timer_sync(fnic);
3209 		}
3210 		iport->fabric.timer_pending = 0;
3211 		fdls->retry_counter = 0;
3212 		fdls_send_register_fc4_types(iport);
3213 		fdls_set_state((&iport->fabric), FDLS_STATE_REGISTER_FC4_TYPES);
3214 		break;
3215 	case FC_FS_RJT:
3216 		reason_code = rpn_rsp->fc_std_ct_hdr.ct_reason;
3217 		if (((reason_code == FC_FS_RJT_BSY)
3218 			|| (reason_code == FC_FS_RJT_UNABL))
3219 			&& (fdls->retry_counter < FDLS_RETRY_COUNT)) {
3220 			FNIC_FCS_DBG(KERN_INFO, fnic,
3221 					 "RPN_ID returned REJ BUSY. Retry from timer routine %p",
3222 					 iport);
3223 
3224 			/* Retry again from the timer routine */
3225 			fdls->flags |= FNIC_FDLS_RETRY_FRAME;
3226 		} else {
3227 			FNIC_FCS_DBG(KERN_INFO, fnic,
3228 						 "RPN_ID ELS_LS_RJT. Halting discovery %p", iport);
3229 			if (iport->fabric.timer_pending) {
3230 				FNIC_FCS_DBG(KERN_INFO, fnic,
3231 							 "Canceling fabric disc timer %p\n", iport);
3232 				fnic_del_fabric_timer_sync(fnic);
3233 			}
3234 			fdls->timer_pending = 0;
3235 			fdls->retry_counter = 0;
3236 		}
3237 		break;
3238 	default:
3239 		break;
3240 	}
3241 }
3242 
3243 static void
3244 fdls_process_scr_rsp(struct fnic_iport_s *iport,
3245 		     struct fc_frame_header *fchdr)
3246 {
3247 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3248 	struct fc_std_scr *scr_rsp = (struct fc_std_scr *) fchdr;
3249 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *) fchdr;
3250 	struct fnic *fnic = iport->fnic;
3251 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3252 
3253 	FNIC_FCS_DBG(KERN_INFO, fnic,
3254 				 "FDLS process SCR response: 0x%04x",
3255 		 (uint32_t) scr_rsp->scr.scr_cmd);
3256 
3257 	if (fdls_get_state(fdls) != FDLS_STATE_SCR) {
3258 		FNIC_FCS_DBG(KERN_INFO, fnic,
3259 					 "SCR resp recvd in state(%d). Dropping.",
3260 					 fdls_get_state(fdls));
3261 		return;
3262 	}
3263 	if (iport->active_oxid_fabric_req != oxid) {
3264 		FNIC_FCS_DBG(KERN_INFO, fnic,
3265 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3266 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3267 	}
3268 
3269 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3270 
3271 	switch (scr_rsp->scr.scr_cmd) {
3272 	case ELS_LS_ACC:
3273 		atomic64_inc(&iport->iport_stats.fabric_scr_ls_accepts);
3274 		if (iport->fabric.timer_pending) {
3275 			FNIC_FCS_DBG(KERN_INFO, fnic,
3276 						 "Canceling fabric disc timer %p\n", iport);
3277 			fnic_del_fabric_timer_sync(fnic);
3278 		}
3279 		iport->fabric.timer_pending = 0;
3280 		iport->fabric.retry_counter = 0;
3281 		fdls_send_gpn_ft(iport, FDLS_STATE_GPN_FT);
3282 		break;
3283 
3284 	case ELS_LS_RJT:
3285 		atomic64_inc(&iport->iport_stats.fabric_scr_ls_rejects);
3286 		if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
3287 	     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
3288 			&& (fdls->retry_counter < FDLS_RETRY_COUNT)) {
3289 			FNIC_FCS_DBG(KERN_INFO, fnic,
3290 						 "SCR ELS_LS_RJT BUSY. Retry from timer routine %p",
3291 						 iport);
3292 			/* Retry again from the timer routine */
3293 			fdls->flags |= FNIC_FDLS_RETRY_FRAME;
3294 		} else {
3295 			FNIC_FCS_DBG(KERN_INFO, fnic,
3296 						 "SCR returned ELS_LS_RJT. Halting discovery %p",
3297 						 iport);
3298 			if (iport->fabric.timer_pending) {
3299 				FNIC_FCS_DBG(KERN_INFO, fnic,
3300 					     "Canceling fabric disc timer %p\n",
3301 					     iport);
3302 				fnic_del_fabric_timer_sync(fnic);
3303 			}
3304 			fdls->timer_pending = 0;
3305 			fdls->retry_counter = 0;
3306 		}
3307 		break;
3308 
3309 	default:
3310 		atomic64_inc(&iport->iport_stats.fabric_scr_misc_rejects);
3311 		break;
3312 	}
3313 }
3314 
3315 static void
3316 fdls_process_gpn_ft_tgt_list(struct fnic_iport_s *iport,
3317 			     struct fc_frame_header *fchdr, int len)
3318 {
3319 	struct fc_gpn_ft_rsp_iu *gpn_ft_tgt;
3320 	struct fnic_tport_s *tport, *next;
3321 	uint32_t fcid;
3322 	uint64_t wwpn;
3323 	int rem_len = len;
3324 	u32 old_link_down_cnt = iport->fnic->link_down_cnt;
3325 	struct fnic *fnic = iport->fnic;
3326 
3327 	FNIC_FCS_DBG(KERN_INFO, fnic,
3328 				 "0x%x: FDLS process GPN_FT tgt list", iport->fcid);
3329 
3330 	gpn_ft_tgt =
3331 	    (struct fc_gpn_ft_rsp_iu *)((uint8_t *) fchdr +
3332 					sizeof(struct fc_frame_header)
3333 					+ sizeof(struct fc_ct_hdr));
3334 	len -= sizeof(struct fc_frame_header) + sizeof(struct fc_ct_hdr);
3335 
3336 	while (rem_len > 0) {
3337 
3338 		fcid = ntoh24(gpn_ft_tgt->fcid);
3339 		wwpn = be64_to_cpu(gpn_ft_tgt->wwpn);
3340 
3341 		FNIC_FCS_DBG(KERN_INFO, fnic,
3342 				 "tport: 0x%x: ctrl:0x%x", fcid, gpn_ft_tgt->ctrl);
3343 
3344 		if (fcid == iport->fcid) {
3345 			if (gpn_ft_tgt->ctrl & FC_NS_FID_LAST)
3346 				break;
3347 			gpn_ft_tgt++;
3348 			rem_len -= sizeof(struct fc_gpn_ft_rsp_iu);
3349 			continue;
3350 		}
3351 
3352 		tport = fnic_find_tport_by_wwpn(iport, wwpn);
3353 		if (!tport) {
3354 			/*
3355 			 * New port registered with the switch or first time query
3356 			 */
3357 			tport = fdls_create_tport(iport, fcid, wwpn);
3358 			if (!tport)
3359 				return;
3360 		}
3361 		/*
3362 		 * check if this was an existing tport with same fcid
3363 		 * but whose wwpn has changed now ,then remove it and
3364 		 * create a new one
3365 		 */
3366 		if (tport->fcid != fcid) {
3367 			fdls_delete_tport(iport, tport);
3368 			tport = fdls_create_tport(iport, fcid, wwpn);
3369 			if (!tport)
3370 				return;
3371 		}
3372 
3373 		/*
3374 		 * If this GPN_FT rsp is after RSCN then mark the tports which
3375 		 * matches with the new GPN_FT list, if some tport is not
3376 		 * found in GPN_FT we went to delete that tport later.
3377 		 */
3378 		if (fdls_get_state((&iport->fabric)) == FDLS_STATE_RSCN_GPN_FT)
3379 			tport->flags |= FNIC_FDLS_TPORT_IN_GPN_FT_LIST;
3380 
3381 		if (gpn_ft_tgt->ctrl & FC_NS_FID_LAST)
3382 			break;
3383 
3384 		gpn_ft_tgt++;
3385 		rem_len -= sizeof(struct fc_gpn_ft_rsp_iu);
3386 	}
3387 	if (rem_len <= 0) {
3388 		FNIC_FCS_DBG(KERN_INFO, fnic,
3389 			 "GPN_FT response: malformed/corrupt frame rxlen: %d remlen: %d",
3390 			 len, rem_len);
3391 }
3392 
3393 	/*remove those ports which was not listed in GPN_FT */
3394 	if (fdls_get_state((&iport->fabric)) == FDLS_STATE_RSCN_GPN_FT) {
3395 		list_for_each_entry_safe(tport, next, &iport->tport_list, links) {
3396 
3397 			if (!(tport->flags & FNIC_FDLS_TPORT_IN_GPN_FT_LIST)) {
3398 				FNIC_FCS_DBG(KERN_INFO, fnic,
3399 					 "Remove port: 0x%x not found in GPN_FT list",
3400 					 tport->fcid);
3401 				fdls_delete_tport(iport, tport);
3402 			} else {
3403 				tport->flags &= ~FNIC_FDLS_TPORT_IN_GPN_FT_LIST;
3404 			}
3405 			if ((old_link_down_cnt != iport->fnic->link_down_cnt)
3406 				|| (iport->state != FNIC_IPORT_STATE_READY)) {
3407 				return;
3408 			}
3409 		}
3410 	}
3411 }
3412 
3413 static void
3414 fdls_process_gpn_ft_rsp(struct fnic_iport_s *iport,
3415 			struct fc_frame_header *fchdr, int len)
3416 {
3417 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3418 	struct fc_std_gpn_ft *gpn_ft_rsp = (struct fc_std_gpn_ft *) fchdr;
3419 	uint16_t rsp;
3420 	uint8_t reason_code;
3421 	int count = 0;
3422 	struct fnic_tport_s *tport, *next;
3423 	u32 old_link_down_cnt = iport->fnic->link_down_cnt;
3424 	struct fnic *fnic = iport->fnic;
3425 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3426 
3427 	FNIC_FCS_DBG(KERN_INFO, fnic,
3428 				 "FDLS process GPN_FT response: iport state: %d len: %d",
3429 				 iport->state, len);
3430 
3431 	/*
3432 	 * GPNFT response :-
3433 	 *  FDLS_STATE_GPN_FT      : GPNFT send after SCR state
3434 	 *  during fabric discovery(FNIC_IPORT_STATE_FABRIC_DISC)
3435 	 *  FDLS_STATE_RSCN_GPN_FT : GPNFT send in response to RSCN
3436 	 *  FDLS_STATE_SEND_GPNFT  : GPNFT send after deleting a Target,
3437 	 *  e.g. after receiving Target LOGO
3438 	 *  FDLS_STATE_TGT_DISCOVERY :Target discovery is currently in progress
3439 	 *  from previous GPNFT response,a new GPNFT response has come.
3440 	 */
3441 	if (!(((iport->state == FNIC_IPORT_STATE_FABRIC_DISC)
3442 		   && (fdls_get_state(fdls) == FDLS_STATE_GPN_FT))
3443 		  || ((iport->state == FNIC_IPORT_STATE_READY)
3444 			  && ((fdls_get_state(fdls) == FDLS_STATE_RSCN_GPN_FT)
3445 				  || (fdls_get_state(fdls) == FDLS_STATE_SEND_GPNFT)
3446 				  || (fdls_get_state(fdls) == FDLS_STATE_TGT_DISCOVERY))))) {
3447 		FNIC_FCS_DBG(KERN_INFO, fnic,
3448 			 "GPNFT resp recvd in fab state(%d) iport_state(%d). Dropping.",
3449 			 fdls_get_state(fdls), iport->state);
3450 		return;
3451 	}
3452 
3453 	if (iport->active_oxid_fabric_req != oxid) {
3454 		FNIC_FCS_DBG(KERN_INFO, fnic,
3455 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3456 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3457 	}
3458 
3459 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3460 
3461 	iport->state = FNIC_IPORT_STATE_READY;
3462 	rsp = FNIC_STD_GET_FC_CT_CMD((&gpn_ft_rsp->fc_std_ct_hdr));
3463 
3464 	switch (rsp) {
3465 
3466 	case FC_FS_ACC:
3467 		FNIC_FCS_DBG(KERN_INFO, fnic,
3468 					 "0x%x: GPNFT_RSP accept", iport->fcid);
3469 		if (iport->fabric.timer_pending) {
3470 			FNIC_FCS_DBG(KERN_INFO, fnic,
3471 						 "0x%x: Canceling fabric disc timer\n",
3472 						 iport->fcid);
3473 			fnic_del_fabric_timer_sync(fnic);
3474 		}
3475 		iport->fabric.timer_pending = 0;
3476 		iport->fabric.retry_counter = 0;
3477 		fdls_process_gpn_ft_tgt_list(iport, fchdr, len);
3478 
3479 		/*
3480 		 * iport state can change only if link down event happened
3481 		 * We don't need to undo fdls_process_gpn_ft_tgt_list,
3482 		 * that will be taken care in next link up event
3483 		 */
3484 		if (iport->state != FNIC_IPORT_STATE_READY) {
3485 			FNIC_FCS_DBG(KERN_INFO, fnic,
3486 				 "Halting target discovery: fab st: %d iport st: %d ",
3487 				 fdls_get_state(fdls), iport->state);
3488 			break;
3489 		}
3490 		fdls_tgt_discovery_start(iport);
3491 		break;
3492 
3493 	case FC_FS_RJT:
3494 		reason_code = gpn_ft_rsp->fc_std_ct_hdr.ct_reason;
3495 		FNIC_FCS_DBG(KERN_INFO, fnic,
3496 			 "0x%x: GPNFT_RSP Reject reason: %d", iport->fcid, reason_code);
3497 
3498 		if (((reason_code == FC_FS_RJT_BSY)
3499 		     || (reason_code == FC_FS_RJT_UNABL))
3500 			&& (fdls->retry_counter < FDLS_RETRY_COUNT)) {
3501 			FNIC_FCS_DBG(KERN_INFO, fnic,
3502 				 "0x%x: GPNFT_RSP ret REJ/BSY. Retry from timer routine",
3503 				 iport->fcid);
3504 			/* Retry again from the timer routine */
3505 			fdls->flags |= FNIC_FDLS_RETRY_FRAME;
3506 		} else {
3507 			FNIC_FCS_DBG(KERN_INFO, fnic,
3508 						 "0x%x: GPNFT_RSP reject", iport->fcid);
3509 			if (iport->fabric.timer_pending) {
3510 				FNIC_FCS_DBG(KERN_INFO, fnic,
3511 							 "0x%x: Canceling fabric disc timer\n",
3512 							 iport->fcid);
3513 				fnic_del_fabric_timer_sync(fnic);
3514 			}
3515 			iport->fabric.timer_pending = 0;
3516 			iport->fabric.retry_counter = 0;
3517 			/*
3518 			 * If GPN_FT ls_rjt then we should delete
3519 			 * all existing tports
3520 			 */
3521 			count = 0;
3522 			list_for_each_entry_safe(tport, next, &iport->tport_list,
3523 									 links) {
3524 				FNIC_FCS_DBG(KERN_INFO, fnic,
3525 							 "GPN_FT_REJECT: Remove port: 0x%x",
3526 							 tport->fcid);
3527 				fdls_delete_tport(iport, tport);
3528 				if ((old_link_down_cnt != iport->fnic->link_down_cnt)
3529 					|| (iport->state != FNIC_IPORT_STATE_READY)) {
3530 					return;
3531 				}
3532 				count++;
3533 			}
3534 			FNIC_FCS_DBG(KERN_INFO, fnic,
3535 						 "GPN_FT_REJECT: Removed (0x%x) ports", count);
3536 		}
3537 		break;
3538 
3539 	default:
3540 		break;
3541 	}
3542 }
3543 
3544 /**
3545  * fdls_process_fabric_logo_rsp - Handle an flogo response from the fcf
3546  * @iport: Handle to fnic iport
3547  * @fchdr: Incoming frame
3548  */
3549 static void
3550 fdls_process_fabric_logo_rsp(struct fnic_iport_s *iport,
3551 			     struct fc_frame_header *fchdr)
3552 {
3553 	struct fc_std_flogi *flogo_rsp = (struct fc_std_flogi *) fchdr;
3554 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3555 	struct fnic *fnic = iport->fnic;
3556 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3557 
3558 	if (iport->active_oxid_fabric_req != oxid) {
3559 		FNIC_FCS_DBG(KERN_INFO, fnic,
3560 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3561 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3562 	}
3563 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3564 
3565 	switch (flogo_rsp->els.fl_cmd) {
3566 	case ELS_LS_ACC:
3567 		if (iport->fabric.state != FDLS_STATE_FABRIC_LOGO) {
3568 			FNIC_FCS_DBG(KERN_INFO, fnic,
3569 				 "Flogo response. Fabric not in LOGO state. Dropping! %p",
3570 				 iport);
3571 			return;
3572 		}
3573 
3574 		iport->fabric.state = FDLS_STATE_FLOGO_DONE;
3575 		iport->state = FNIC_IPORT_STATE_LINK_WAIT;
3576 
3577 		if (iport->fabric.timer_pending) {
3578 			FNIC_FCS_DBG(KERN_INFO, fnic,
3579 			 "iport 0x%p Canceling fabric disc timer\n",
3580 						 iport);
3581 			fnic_del_fabric_timer_sync(fnic);
3582 		}
3583 		iport->fabric.timer_pending = 0;
3584 		FNIC_FCS_DBG(KERN_INFO, fnic,
3585 					 "Flogo response from Fabric for did: 0x%x",
3586 		     ntoh24(fchdr->fh_d_id));
3587 		return;
3588 
3589 	case ELS_LS_RJT:
3590 		FNIC_FCS_DBG(KERN_INFO, fnic,
3591 			 "Flogo response from Fabric for did: 0x%x returned ELS_LS_RJT",
3592 		     ntoh24(fchdr->fh_d_id));
3593 		return;
3594 
3595 	default:
3596 		FNIC_FCS_DBG(KERN_INFO, fnic,
3597 					 "FLOGO response not accepted or rejected: 0x%x",
3598 		     flogo_rsp->els.fl_cmd);
3599 	}
3600 }
3601 
3602 static void
3603 fdls_process_flogi_rsp(struct fnic_iport_s *iport,
3604 		       struct fc_frame_header *fchdr, void *rx_frame)
3605 {
3606 	struct fnic_fdls_fabric_s *fabric = &iport->fabric;
3607 	struct fc_std_flogi *flogi_rsp = (struct fc_std_flogi *) fchdr;
3608 	uint8_t *fcid;
3609 	uint16_t rdf_size;
3610 	uint8_t fcmac[6] = { 0x0E, 0XFC, 0x00, 0x00, 0x00, 0x00 };
3611 	struct fnic *fnic = iport->fnic;
3612 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3613 
3614 	FNIC_FCS_DBG(KERN_INFO, fnic,
3615 				 "0x%x: FDLS processing FLOGI response", iport->fcid);
3616 
3617 	if (fdls_get_state(fabric) != FDLS_STATE_FABRIC_FLOGI) {
3618 		FNIC_FCS_DBG(KERN_INFO, fnic,
3619 					 "FLOGI response received in state (%d). Dropping frame",
3620 					 fdls_get_state(fabric));
3621 		return;
3622 	}
3623 	if (iport->active_oxid_fabric_req != oxid) {
3624 		FNIC_FCS_DBG(KERN_INFO, fnic,
3625 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3626 			fdls_get_state(fabric), oxid, iport->active_oxid_fabric_req);
3627 		return;
3628 	}
3629 
3630 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3631 
3632 	switch (flogi_rsp->els.fl_cmd) {
3633 	case ELS_LS_ACC:
3634 		atomic64_inc(&iport->iport_stats.fabric_flogi_ls_accepts);
3635 		if (iport->fabric.timer_pending) {
3636 			FNIC_FCS_DBG(KERN_INFO, fnic,
3637 						 "iport fcid: 0x%x Canceling fabric disc timer\n",
3638 						 iport->fcid);
3639 			fnic_del_fabric_timer_sync(fnic);
3640 		}
3641 
3642 		iport->fabric.timer_pending = 0;
3643 		iport->fabric.retry_counter = 0;
3644 		fcid = FNIC_STD_GET_D_ID(fchdr);
3645 		iport->fcid = ntoh24(fcid);
3646 		FNIC_FCS_DBG(KERN_INFO, fnic,
3647 					 "0x%x: FLOGI response accepted", iport->fcid);
3648 
3649 		/* Learn the Service Params */
3650 		rdf_size = be16_to_cpu(FNIC_LOGI_RDF_SIZE(flogi_rsp->els));
3651 		if ((rdf_size >= FNIC_MIN_DATA_FIELD_SIZE)
3652 			&& (rdf_size < FNIC_FC_MAX_PAYLOAD_LEN))
3653 			iport->max_payload_size = min(rdf_size,
3654 								  iport->max_payload_size);
3655 
3656 		FNIC_FCS_DBG(KERN_INFO, fnic,
3657 					 "max_payload_size from fabric: %u set: %d", rdf_size,
3658 					 iport->max_payload_size);
3659 
3660 		iport->r_a_tov = be32_to_cpu(FNIC_LOGI_R_A_TOV(flogi_rsp->els));
3661 		iport->e_d_tov = be32_to_cpu(FNIC_LOGI_E_D_TOV(flogi_rsp->els));
3662 
3663 		if (FNIC_LOGI_FEATURES(flogi_rsp->els) & FNIC_FC_EDTOV_NSEC)
3664 			iport->e_d_tov = iport->e_d_tov / FNIC_NSEC_TO_MSEC;
3665 
3666 		FNIC_FCS_DBG(KERN_INFO, fnic,
3667 					 "From fabric: R_A_TOV: %d E_D_TOV: %d",
3668 					 iport->r_a_tov, iport->e_d_tov);
3669 
3670 		if (IS_FNIC_FCP_INITIATOR(fnic)) {
3671 			fc_host_fabric_name(iport->fnic->host) =
3672 					get_unaligned_be64(&FNIC_LOGI_NODE_NAME(flogi_rsp->els));
3673 			fc_host_port_id(iport->fnic->host) = iport->fcid;
3674 		}
3675 
3676 		fnic_fdls_learn_fcoe_macs(iport, rx_frame, fcid);
3677 
3678 		if (fnic_fdls_register_portid(iport, iport->fcid, rx_frame) != 0) {
3679 			FNIC_FCS_DBG(KERN_INFO, fnic,
3680 						 "0x%x: FLOGI registration failed", iport->fcid);
3681 			break;
3682 		}
3683 
3684 		memcpy(&fcmac[3], fcid, 3);
3685 		FNIC_FCS_DBG(KERN_INFO, fnic,
3686 			 "Adding vNIC device MAC addr: %02x:%02x:%02x:%02x:%02x:%02x",
3687 			 fcmac[0], fcmac[1], fcmac[2], fcmac[3], fcmac[4],
3688 			 fcmac[5]);
3689 		vnic_dev_add_addr(iport->fnic->vdev, fcmac);
3690 
3691 		if (fdls_get_state(fabric) == FDLS_STATE_FABRIC_FLOGI) {
3692 			fnic_fdls_start_plogi(iport);
3693 			FNIC_FCS_DBG(KERN_INFO, fnic,
3694 						 "FLOGI response received. Starting PLOGI");
3695 		} else {
3696 			/* From FDLS_STATE_FABRIC_FLOGI state fabric can only go to
3697 			 * FDLS_STATE_LINKDOWN
3698 			 * state, hence we don't have to worry about undoing:
3699 			 * the fnic_fdls_register_portid and vnic_dev_add_addr
3700 			 */
3701 			FNIC_FCS_DBG(KERN_INFO, fnic,
3702 				 "FLOGI response received in state (%d). Dropping frame",
3703 				 fdls_get_state(fabric));
3704 		}
3705 		break;
3706 
3707 	case ELS_LS_RJT:
3708 		atomic64_inc(&iport->iport_stats.fabric_flogi_ls_rejects);
3709 		if (fabric->retry_counter < iport->max_flogi_retries) {
3710 			FNIC_FCS_DBG(KERN_INFO, fnic,
3711 				 "FLOGI returned ELS_LS_RJT BUSY. Retry from timer routine %p",
3712 				 iport);
3713 
3714 			/* Retry Flogi again from the timer routine. */
3715 			fabric->flags |= FNIC_FDLS_RETRY_FRAME;
3716 
3717 		} else {
3718 			FNIC_FCS_DBG(KERN_INFO, fnic,
3719 			 "FLOGI returned ELS_LS_RJT. Halting discovery %p",
3720 			 iport);
3721 			if (iport->fabric.timer_pending) {
3722 				FNIC_FCS_DBG(KERN_INFO, fnic,
3723 							 "iport 0x%p Canceling fabric disc timer\n",
3724 							 iport);
3725 				fnic_del_fabric_timer_sync(fnic);
3726 			}
3727 			fabric->timer_pending = 0;
3728 			fabric->retry_counter = 0;
3729 		}
3730 		break;
3731 
3732 	default:
3733 		FNIC_FCS_DBG(KERN_INFO, fnic,
3734 					 "FLOGI response not accepted: 0x%x",
3735 		     flogi_rsp->els.fl_cmd);
3736 		atomic64_inc(&iport->iport_stats.fabric_flogi_misc_rejects);
3737 		break;
3738 	}
3739 }
3740 
3741 static void
3742 fdls_process_fabric_plogi_rsp(struct fnic_iport_s *iport,
3743 			      struct fc_frame_header *fchdr)
3744 {
3745 	struct fc_std_flogi *plogi_rsp = (struct fc_std_flogi *) fchdr;
3746 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *) fchdr;
3747 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3748 	struct fnic *fnic = iport->fnic;
3749 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3750 
3751 	if (fdls_get_state((&iport->fabric)) != FDLS_STATE_FABRIC_PLOGI) {
3752 		FNIC_FCS_DBG(KERN_INFO, fnic,
3753 			 "Fabric PLOGI response received in state (%d). Dropping frame",
3754 			 fdls_get_state(&iport->fabric));
3755 		return;
3756 	}
3757 	if (iport->active_oxid_fabric_req != oxid) {
3758 		FNIC_FCS_DBG(KERN_INFO, fnic,
3759 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3760 			fdls_get_state(fdls), oxid, iport->active_oxid_fabric_req);
3761 		return;
3762 	}
3763 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
3764 
3765 	switch (plogi_rsp->els.fl_cmd) {
3766 	case ELS_LS_ACC:
3767 		atomic64_inc(&iport->iport_stats.fabric_plogi_ls_accepts);
3768 		if (iport->fabric.timer_pending) {
3769 			FNIC_FCS_DBG(KERN_INFO, fnic,
3770 				 "iport fcid: 0x%x fabric PLOGI response: Accepted\n",
3771 				 iport->fcid);
3772 			fnic_del_fabric_timer_sync(fnic);
3773 		}
3774 		iport->fabric.timer_pending = 0;
3775 		iport->fabric.retry_counter = 0;
3776 		fdls_set_state(&iport->fabric, FDLS_STATE_RPN_ID);
3777 		fdls_send_rpn_id(iport);
3778 		break;
3779 	case ELS_LS_RJT:
3780 		atomic64_inc(&iport->iport_stats.fabric_plogi_ls_rejects);
3781 		if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
3782 	     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
3783 			&& (iport->fabric.retry_counter < iport->max_plogi_retries)) {
3784 			FNIC_FCS_DBG(KERN_INFO, fnic,
3785 				 "0x%x: Fabric PLOGI ELS_LS_RJT BUSY. Retry from timer routine",
3786 				 iport->fcid);
3787 		} else {
3788 			FNIC_FCS_DBG(KERN_INFO, fnic,
3789 				 "0x%x: Fabric PLOGI ELS_LS_RJT. Halting discovery",
3790 				 iport->fcid);
3791 			if (iport->fabric.timer_pending) {
3792 				FNIC_FCS_DBG(KERN_INFO, fnic,
3793 							 "iport fcid: 0x%x Canceling fabric disc timer\n",
3794 							 iport->fcid);
3795 				fnic_del_fabric_timer_sync(fnic);
3796 			}
3797 			iport->fabric.timer_pending = 0;
3798 			iport->fabric.retry_counter = 0;
3799 			return;
3800 		}
3801 		break;
3802 	default:
3803 		FNIC_FCS_DBG(KERN_INFO, fnic,
3804 					 "PLOGI response not accepted: 0x%x",
3805 		     plogi_rsp->els.fl_cmd);
3806 		atomic64_inc(&iport->iport_stats.fabric_plogi_misc_rejects);
3807 		break;
3808 	}
3809 }
3810 
3811 static void fdls_process_fdmi_plogi_rsp(struct fnic_iport_s *iport,
3812 					struct fc_frame_header *fchdr)
3813 {
3814 	struct fc_std_flogi *plogi_rsp = (struct fc_std_flogi *)fchdr;
3815 	struct fc_std_els_rjt_rsp *els_rjt = (struct fc_std_els_rjt_rsp *)fchdr;
3816 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
3817 	struct fnic *fnic = iport->fnic;
3818 	u64 fdmi_tov;
3819 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
3820 
3821 	if (iport->active_oxid_fdmi_plogi != oxid) {
3822 		FNIC_FCS_DBG(KERN_INFO, fnic,
3823 			"Incorrect OXID in response. state: %d, oxid recvd: 0x%x, active oxid: 0x%x\n",
3824 			fdls_get_state(fdls), oxid, iport->active_oxid_fdmi_plogi);
3825 		return;
3826 	}
3827 
3828 	iport->fabric.fdmi_pending &= ~FDLS_FDMI_PLOGI_PENDING;
3829 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_plogi);
3830 
3831 	if (ntoh24(fchdr->fh_s_id) == FC_FID_MGMT_SERV) {
3832 		timer_delete_sync(&iport->fabric.fdmi_timer);
3833 		iport->fabric.fdmi_pending = 0;
3834 		switch (plogi_rsp->els.fl_cmd) {
3835 		case ELS_LS_ACC:
3836 			FNIC_FCS_DBG(KERN_INFO, fnic,
3837 				 "FDLS process fdmi PLOGI response status: ELS_LS_ACC\n");
3838 			FNIC_FCS_DBG(KERN_INFO, fnic,
3839 				 "Sending fdmi registration for port 0x%x\n",
3840 				 iport->fcid);
3841 
3842 			fdls_fdmi_register_hba(iport);
3843 			fdls_fdmi_register_pa(iport);
3844 			fdmi_tov = jiffies + msecs_to_jiffies(5000);
3845 			mod_timer(&iport->fabric.fdmi_timer,
3846 				  round_jiffies(fdmi_tov));
3847 			break;
3848 		case ELS_LS_RJT:
3849 			FNIC_FCS_DBG(KERN_INFO, fnic,
3850 				 "Fabric FDMI PLOGI returned ELS_LS_RJT reason: 0x%x",
3851 				     els_rjt->rej.er_reason);
3852 
3853 			if (((els_rjt->rej.er_reason == ELS_RJT_BUSY)
3854 			     || (els_rjt->rej.er_reason == ELS_RJT_UNAB))
3855 				&& (iport->fabric.fdmi_retry < 7)) {
3856 				iport->fabric.fdmi_retry++;
3857 				fdls_send_fdmi_plogi(iport);
3858 			}
3859 			break;
3860 		default:
3861 			break;
3862 		}
3863 	}
3864 }
3865 static void fdls_process_fdmi_reg_ack(struct fnic_iport_s *iport,
3866 				      struct fc_frame_header *fchdr,
3867 				      int rsp_type)
3868 {
3869 	struct fnic *fnic = iport->fnic;
3870 	uint16_t oxid;
3871 
3872 	if (!iport->fabric.fdmi_pending) {
3873 		FNIC_FCS_DBG(KERN_ERR, fnic,
3874 			     "Received FDMI ack while not waiting: 0x%x\n",
3875 			     FNIC_STD_GET_OX_ID(fchdr));
3876 		return;
3877 	}
3878 
3879 	oxid =  FNIC_STD_GET_OX_ID(fchdr);
3880 
3881 	if ((iport->active_oxid_fdmi_rhba != oxid) &&
3882 		(iport->active_oxid_fdmi_rpa != oxid))  {
3883 		FNIC_FCS_DBG(KERN_INFO, fnic,
3884 			"Incorrect OXID in response. oxid recvd: 0x%x, active oxids(rhba,rpa): 0x%x, 0x%x\n",
3885 			oxid, iport->active_oxid_fdmi_rhba, iport->active_oxid_fdmi_rpa);
3886 		return;
3887 	}
3888 	if (FNIC_FRAME_TYPE(oxid) == FNIC_FRAME_TYPE_FDMI_RHBA) {
3889 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_REG_HBA_PENDING;
3890 		fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_rhba);
3891 	} else {
3892 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_RPA_PENDING;
3893 		fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_rpa);
3894 	}
3895 
3896 	FNIC_FCS_DBG(KERN_INFO, fnic,
3897 		"iport fcid: 0x%x: Received FDMI registration ack\n",
3898 		 iport->fcid);
3899 
3900 	if (!iport->fabric.fdmi_pending) {
3901 		timer_delete_sync(&iport->fabric.fdmi_timer);
3902 		FNIC_FCS_DBG(KERN_INFO, fnic,
3903 					 "iport fcid: 0x%x: Canceling FDMI timer\n",
3904 					 iport->fcid);
3905 	}
3906 }
3907 
3908 static void fdls_process_fdmi_abts_rsp(struct fnic_iport_s *iport,
3909 				       struct fc_frame_header *fchdr)
3910 {
3911 	uint32_t s_id;
3912 	struct fnic *fnic = iport->fnic;
3913 	uint16_t oxid;
3914 
3915 	s_id = ntoh24(FNIC_STD_GET_S_ID(fchdr));
3916 
3917 	if (!(s_id != FC_FID_MGMT_SERV)) {
3918 		FNIC_FCS_DBG(KERN_INFO, fnic,
3919 			     "Received abts rsp with invalid SID: 0x%x. Dropping frame",
3920 			     s_id);
3921 		return;
3922 	}
3923 
3924 	oxid =  FNIC_STD_GET_OX_ID(fchdr);
3925 
3926 	switch (FNIC_FRAME_TYPE(oxid)) {
3927 	case FNIC_FRAME_TYPE_FDMI_PLOGI:
3928 		FNIC_FCS_DBG(KERN_INFO, fnic,
3929 			"Received FDMI PLOGI ABTS rsp with oxid: 0x%x", oxid);
3930 		FNIC_FCS_DBG(KERN_INFO, fnic,
3931 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3932 			 iport->fcid, iport->fabric.fdmi_pending);
3933 		fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_plogi);
3934 
3935 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_PLOGI_PENDING;
3936 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_ABORT_PENDING;
3937 		FNIC_FCS_DBG(KERN_INFO, fnic,
3938 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3939 			 iport->fcid, iport->fabric.fdmi_pending);
3940 		break;
3941 	case FNIC_FRAME_TYPE_FDMI_RHBA:
3942 		FNIC_FCS_DBG(KERN_INFO, fnic,
3943 			"Received FDMI RHBA ABTS rsp with oxid: 0x%x", oxid);
3944 		FNIC_FCS_DBG(KERN_INFO, fnic,
3945 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3946 			 iport->fcid, iport->fabric.fdmi_pending);
3947 
3948 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_REG_HBA_PENDING;
3949 
3950 		/* If RPA is still pending, don't turn off ABORT PENDING.
3951 		 * We count on the timer to detect the ABTS timeout and take
3952 		 * corrective action.
3953 		 */
3954 		if (!(iport->fabric.fdmi_pending & FDLS_FDMI_RPA_PENDING))
3955 			iport->fabric.fdmi_pending &= ~FDLS_FDMI_ABORT_PENDING;
3956 
3957 		fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_rhba);
3958 		FNIC_FCS_DBG(KERN_INFO, fnic,
3959 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3960 			 iport->fcid, iport->fabric.fdmi_pending);
3961 		break;
3962 	case FNIC_FRAME_TYPE_FDMI_RPA:
3963 		FNIC_FCS_DBG(KERN_INFO, fnic,
3964 			"Received FDMI RPA ABTS rsp with oxid: 0x%x", oxid);
3965 		FNIC_FCS_DBG(KERN_INFO, fnic,
3966 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3967 			 iport->fcid, iport->fabric.fdmi_pending);
3968 
3969 		iport->fabric.fdmi_pending &= ~FDLS_FDMI_RPA_PENDING;
3970 
3971 		/* If RHBA is still pending, don't turn off ABORT PENDING.
3972 		 * We count on the timer to detect the ABTS timeout and take
3973 		 * corrective action.
3974 		 */
3975 		if (!(iport->fabric.fdmi_pending & FDLS_FDMI_REG_HBA_PENDING))
3976 			iport->fabric.fdmi_pending &= ~FDLS_FDMI_ABORT_PENDING;
3977 
3978 		fdls_free_oxid(iport, oxid, &iport->active_oxid_fdmi_rpa);
3979 		FNIC_FCS_DBG(KERN_INFO, fnic,
3980 			 "0x%x: iport->fabric.fdmi_pending: 0x%x",
3981 			 iport->fcid, iport->fabric.fdmi_pending);
3982 		break;
3983 	default:
3984 		FNIC_FCS_DBG(KERN_INFO, fnic,
3985 			"Received abts rsp with invalid oxid: 0x%x. Dropping frame",
3986 			oxid);
3987 		break;
3988 	}
3989 
3990 	/*
3991 	 * Only if ABORT PENDING is off, delete the timer, and if no other
3992 	 * operations are pending, retry FDMI.
3993 	 * Otherwise, let the timer pop and take the appropriate action.
3994 	 */
3995 	if (!(iport->fabric.fdmi_pending & FDLS_FDMI_ABORT_PENDING)) {
3996 		timer_delete_sync(&iport->fabric.fdmi_timer);
3997 		if (!iport->fabric.fdmi_pending)
3998 			fdls_fdmi_retry_plogi(iport);
3999 	}
4000 }
4001 
4002 static void
4003 fdls_process_fabric_abts_rsp(struct fnic_iport_s *iport,
4004 			     struct fc_frame_header *fchdr)
4005 {
4006 	uint32_t s_id;
4007 	struct fc_std_abts_ba_acc *ba_acc = (struct fc_std_abts_ba_acc *)fchdr;
4008 	struct fc_std_abts_ba_rjt *ba_rjt;
4009 	uint32_t fabric_state = iport->fabric.state;
4010 	struct fnic *fnic = iport->fnic;
4011 	int frame_type;
4012 	uint16_t oxid;
4013 
4014 	s_id = ntoh24(fchdr->fh_s_id);
4015 	ba_rjt = (struct fc_std_abts_ba_rjt *) fchdr;
4016 
4017 	if (!((s_id == FC_FID_DIR_SERV) || (s_id == FC_FID_FLOGI)
4018 		  || (s_id == FC_FID_FCTRL))) {
4019 		FNIC_FCS_DBG(KERN_INFO, fnic,
4020 			 "Received abts rsp with invalid SID: 0x%x. Dropping frame",
4021 			 s_id);
4022 		return;
4023 	}
4024 
4025 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4026 	if (iport->active_oxid_fabric_req != oxid) {
4027 		FNIC_FCS_DBG(KERN_INFO, fnic,
4028 			"Received abts rsp with invalid oxid: 0x%x. Dropping frame",
4029 			oxid);
4030 		return;
4031 	}
4032 
4033 	if (iport->fabric.timer_pending) {
4034 		FNIC_FCS_DBG(KERN_INFO, fnic,
4035 					 "Canceling fabric disc timer %p\n", iport);
4036 		fnic_del_fabric_timer_sync(fnic);
4037 	}
4038 	iport->fabric.timer_pending = 0;
4039 	iport->fabric.flags &= ~FNIC_FDLS_FABRIC_ABORT_ISSUED;
4040 
4041 	if (fchdr->fh_r_ctl == FC_RCTL_BA_ACC) {
4042 		FNIC_FCS_DBG(KERN_INFO, fnic,
4043 			 "Received abts rsp BA_ACC for fabric_state: %d OX_ID: 0x%x",
4044 		     fabric_state, be16_to_cpu(ba_acc->acc.ba_ox_id));
4045 	} else if (fchdr->fh_r_ctl == FC_RCTL_BA_RJT) {
4046 		FNIC_FCS_DBG(KERN_INFO, fnic,
4047 			 "BA_RJT fs: %d OX_ID: 0x%x rc: 0x%x rce: 0x%x",
4048 		     fabric_state, FNIC_STD_GET_OX_ID(&ba_rjt->fchdr),
4049 		     ba_rjt->rjt.br_reason, ba_rjt->rjt.br_explan);
4050 	}
4051 
4052 	frame_type = FNIC_FRAME_TYPE(oxid);
4053 	fdls_free_oxid(iport, oxid, &iport->active_oxid_fabric_req);
4054 
4055 	/* currently error handling/retry logic is same for ABTS BA_ACC & BA_RJT */
4056 	switch (frame_type) {
4057 	case FNIC_FRAME_TYPE_FABRIC_FLOGI:
4058 		if (iport->fabric.retry_counter < iport->max_flogi_retries)
4059 			fdls_send_fabric_flogi(iport);
4060 		else
4061 			FNIC_FCS_DBG(KERN_INFO, fnic,
4062 				 "Exceeded max FLOGI retries");
4063 		break;
4064 	case FNIC_FRAME_TYPE_FABRIC_LOGO:
4065 		if (iport->fabric.retry_counter < FABRIC_LOGO_MAX_RETRY)
4066 			fdls_send_fabric_logo(iport);
4067 		break;
4068 	case FNIC_FRAME_TYPE_FABRIC_PLOGI:
4069 		if (iport->fabric.retry_counter < iport->max_plogi_retries)
4070 			fdls_send_fabric_plogi(iport);
4071 		else
4072 			FNIC_FCS_DBG(KERN_INFO, fnic,
4073 				 "Exceeded max PLOGI retries");
4074 		break;
4075 	case FNIC_FRAME_TYPE_FABRIC_RPN:
4076 		if (iport->fabric.retry_counter < FDLS_RETRY_COUNT)
4077 			fdls_send_rpn_id(iport);
4078 		else
4079 			/* go back to fabric Plogi */
4080 			fnic_fdls_start_plogi(iport);
4081 		break;
4082 	case FNIC_FRAME_TYPE_FABRIC_SCR:
4083 		if (iport->fabric.retry_counter < FDLS_RETRY_COUNT)
4084 			fdls_send_scr(iport);
4085 		else {
4086 			FNIC_FCS_DBG(KERN_INFO, fnic,
4087 				"SCR exhausted retries. Start fabric PLOGI %p",
4088 				 iport);
4089 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
4090 		}
4091 		break;
4092 	case FNIC_FRAME_TYPE_FABRIC_RFT:
4093 		if (iport->fabric.retry_counter < FDLS_RETRY_COUNT)
4094 			fdls_send_register_fc4_types(iport);
4095 		else {
4096 			FNIC_FCS_DBG(KERN_INFO, fnic,
4097 				"RFT exhausted retries. Start fabric PLOGI %p",
4098 				 iport);
4099 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
4100 		}
4101 		break;
4102 	case FNIC_FRAME_TYPE_FABRIC_RFF:
4103 		if (iport->fabric.retry_counter < FDLS_RETRY_COUNT)
4104 			fdls_send_register_fc4_features(iport);
4105 		else {
4106 			FNIC_FCS_DBG(KERN_INFO, fnic,
4107 				"RFF exhausted retries. Start fabric PLOGI %p",
4108 				 iport);
4109 			fnic_fdls_start_plogi(iport);	/* go back to fabric Plogi */
4110 		}
4111 		break;
4112 	case FNIC_FRAME_TYPE_FABRIC_GPN_FT:
4113 		if (iport->fabric.retry_counter <= FDLS_RETRY_COUNT)
4114 			fdls_send_gpn_ft(iport, fabric_state);
4115 		else
4116 			FNIC_FCS_DBG(KERN_INFO, fnic,
4117 				"GPN FT exhausted retries. Start fabric PLOGI %p",
4118 				iport);
4119 		break;
4120 	default:
4121 		/*
4122 		 * We should not be here since we already validated rx oxid with
4123 		 * our active_oxid_fabric_req
4124 		 */
4125 		FNIC_FCS_DBG(KERN_INFO, fnic,
4126 			"Invalid OXID/active oxid 0x%x\n", oxid);
4127 		WARN_ON(true);
4128 		return;
4129 	}
4130 }
4131 
4132 static void
4133 fdls_process_abts_req(struct fnic_iport_s *iport, struct fc_frame_header *fchdr)
4134 {
4135 	uint8_t *frame;
4136 	struct fc_std_abts_ba_acc *pba_acc;
4137 	uint32_t nport_id;
4138 	uint16_t oxid = FNIC_STD_GET_OX_ID(fchdr);
4139 	struct fnic_tport_s *tport;
4140 	struct fnic *fnic = iport->fnic;
4141 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4142 			sizeof(struct fc_std_abts_ba_acc);
4143 
4144 	nport_id = ntoh24(fchdr->fh_s_id);
4145 	FNIC_FCS_DBG(KERN_INFO, fnic,
4146 				 "Received abort from SID 0x%8x", nport_id);
4147 
4148 	tport = fnic_find_tport_by_fcid(iport, nport_id);
4149 	if (tport) {
4150 		if (tport->active_oxid == oxid) {
4151 			tport->flags |= FNIC_FDLS_TGT_ABORT_ISSUED;
4152 			fdls_free_oxid(iport, oxid, &tport->active_oxid);
4153 		}
4154 	}
4155 
4156 	frame = fdls_alloc_frame(iport);
4157 	if (frame == NULL) {
4158 		FNIC_FCS_DBG(KERN_ERR, fnic,
4159 				"0x%x: Failed to allocate frame to send response for ABTS req",
4160 				iport->fcid);
4161 		return;
4162 	}
4163 
4164 	pba_acc = (struct fc_std_abts_ba_acc *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4165 	*pba_acc = (struct fc_std_abts_ba_acc) {
4166 		.fchdr = {.fh_r_ctl = FC_RCTL_BA_ACC,
4167 				.fh_f_ctl = {FNIC_FCP_RSP_FCTL, 0, 0}},
4168 		.acc = {.ba_low_seq_cnt = 0, .ba_high_seq_cnt = cpu_to_be16(0xFFFF)}
4169 	};
4170 
4171 	FNIC_STD_SET_S_ID(pba_acc->fchdr, fchdr->fh_d_id);
4172 	FNIC_STD_SET_D_ID(pba_acc->fchdr, fchdr->fh_s_id);
4173 	FNIC_STD_SET_OX_ID(pba_acc->fchdr, FNIC_STD_GET_OX_ID(fchdr));
4174 	FNIC_STD_SET_RX_ID(pba_acc->fchdr, FNIC_STD_GET_RX_ID(fchdr));
4175 
4176 	pba_acc->acc.ba_rx_id = cpu_to_be16(FNIC_STD_GET_RX_ID(fchdr));
4177 	pba_acc->acc.ba_ox_id = cpu_to_be16(FNIC_STD_GET_OX_ID(fchdr));
4178 
4179 	FNIC_FCS_DBG(KERN_INFO, fnic,
4180 		 "0x%x: FDLS send BA ACC with oxid: 0x%x",
4181 		 iport->fcid, oxid);
4182 
4183 	fnic_send_fcoe_frame(iport, frame, frame_size);
4184 }
4185 
4186 static void
4187 fdls_process_unsupported_els_req(struct fnic_iport_s *iport,
4188 				 struct fc_frame_header *fchdr)
4189 {
4190 	uint8_t *frame;
4191 	struct fc_std_els_rjt_rsp *pls_rsp;
4192 	uint16_t oxid;
4193 	uint32_t d_id = ntoh24(fchdr->fh_d_id);
4194 	struct fnic *fnic = iport->fnic;
4195 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4196 			sizeof(struct fc_std_els_rjt_rsp);
4197 
4198 	if (iport->fcid != d_id) {
4199 		FNIC_FCS_DBG(KERN_INFO, fnic,
4200 			 "Dropping unsupported ELS with illegal frame bits 0x%x\n",
4201 			 d_id);
4202 		atomic64_inc(&iport->iport_stats.unsupported_frames_dropped);
4203 		return;
4204 	}
4205 
4206 	if ((iport->state != FNIC_IPORT_STATE_READY)
4207 		&& (iport->state != FNIC_IPORT_STATE_FABRIC_DISC)) {
4208 		FNIC_FCS_DBG(KERN_INFO, fnic,
4209 			 "Dropping unsupported ELS request in iport state: %d",
4210 			 iport->state);
4211 		atomic64_inc(&iport->iport_stats.unsupported_frames_dropped);
4212 		return;
4213 	}
4214 
4215 	frame = fdls_alloc_frame(iport);
4216 	if (frame == NULL) {
4217 		FNIC_FCS_DBG(KERN_ERR, fnic,
4218 			"Failed to allocate frame to send response to unsupported ELS request");
4219 		return;
4220 	}
4221 
4222 	pls_rsp = (struct fc_std_els_rjt_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4223 	fdls_init_els_rjt_frame(frame, iport);
4224 
4225 	FNIC_FCS_DBG(KERN_INFO, fnic,
4226 				 "0x%x: Process unsupported ELS request from SID: 0x%x",
4227 		     iport->fcid, ntoh24(fchdr->fh_s_id));
4228 
4229 	/* We don't support this ELS request, send a reject */
4230 	pls_rsp->rej.er_reason = 0x0B;
4231 	pls_rsp->rej.er_explan = 0x0;
4232 	pls_rsp->rej.er_vendor = 0x0;
4233 
4234 	FNIC_STD_SET_S_ID(pls_rsp->fchdr, fchdr->fh_d_id);
4235 	FNIC_STD_SET_D_ID(pls_rsp->fchdr, fchdr->fh_s_id);
4236 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4237 	FNIC_STD_SET_OX_ID(pls_rsp->fchdr, oxid);
4238 
4239 	fnic_send_fcoe_frame(iport, frame, frame_size);
4240 }
4241 
4242 static void
4243 fdls_process_rls_req(struct fnic_iport_s *iport, struct fc_frame_header *fchdr)
4244 {
4245 	uint8_t *frame;
4246 	struct fc_std_rls_acc *prls_acc_rsp;
4247 	uint16_t oxid;
4248 	struct fnic *fnic = iport->fnic;
4249 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4250 			sizeof(struct fc_std_rls_acc);
4251 
4252 	FNIC_FCS_DBG(KERN_INFO, fnic,
4253 				 "Process RLS request %d", iport->fnic->fnic_num);
4254 
4255 	if ((iport->state != FNIC_IPORT_STATE_READY)
4256 		&& (iport->state != FNIC_IPORT_STATE_FABRIC_DISC)) {
4257 		FNIC_FCS_DBG(KERN_INFO, fnic,
4258 			 "Received RLS req in iport state: %d. Dropping the frame.",
4259 			 iport->state);
4260 		return;
4261 	}
4262 
4263 	frame = fdls_alloc_frame(iport);
4264 	if (frame == NULL) {
4265 		FNIC_FCS_DBG(KERN_ERR, fnic,
4266 				"Failed to allocate frame to send RLS accept");
4267 		return;
4268 	}
4269 	prls_acc_rsp = (struct fc_std_rls_acc *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4270 
4271 	FNIC_STD_SET_S_ID(prls_acc_rsp->fchdr, fchdr->fh_d_id);
4272 	FNIC_STD_SET_D_ID(prls_acc_rsp->fchdr, fchdr->fh_s_id);
4273 
4274 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4275 	FNIC_STD_SET_OX_ID(prls_acc_rsp->fchdr, oxid);
4276 	FNIC_STD_SET_RX_ID(prls_acc_rsp->fchdr, FNIC_UNASSIGNED_RXID);
4277 
4278 	FNIC_STD_SET_F_CTL(prls_acc_rsp->fchdr, FNIC_ELS_REP_FCTL << 16);
4279 	FNIC_STD_SET_R_CTL(prls_acc_rsp->fchdr, FC_RCTL_ELS_REP);
4280 	FNIC_STD_SET_TYPE(prls_acc_rsp->fchdr, FC_TYPE_ELS);
4281 
4282 	prls_acc_rsp->els.rls_cmd = ELS_LS_ACC;
4283 	prls_acc_rsp->els.rls_lesb.lesb_link_fail =
4284 	    cpu_to_be32(iport->fnic->link_down_cnt);
4285 
4286 	fnic_send_fcoe_frame(iport, frame, frame_size);
4287 }
4288 
4289 static void
4290 fdls_process_els_req(struct fnic_iport_s *iport, struct fc_frame_header *fchdr,
4291 					 uint32_t len)
4292 {
4293 	uint8_t *frame;
4294 	struct fc_std_els_acc_rsp *pels_acc;
4295 	uint16_t oxid;
4296 	uint8_t *fc_payload;
4297 	uint8_t type;
4298 	struct fnic *fnic = iport->fnic;
4299 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET;
4300 
4301 	fc_payload = (uint8_t *) fchdr + sizeof(struct fc_frame_header);
4302 	type = *fc_payload;
4303 
4304 	if ((iport->state != FNIC_IPORT_STATE_READY)
4305 		&& (iport->state != FNIC_IPORT_STATE_FABRIC_DISC)) {
4306 		FNIC_FCS_DBG(KERN_INFO, fnic,
4307 				 "Dropping ELS frame type: 0x%x in iport state: %d",
4308 				 type, iport->state);
4309 		return;
4310 	}
4311 	switch (type) {
4312 	case ELS_ECHO:
4313 		FNIC_FCS_DBG(KERN_INFO, fnic,
4314 					 "sending LS_ACC for ECHO request %d\n",
4315 					 iport->fnic->fnic_num);
4316 		break;
4317 
4318 	case ELS_RRQ:
4319 		FNIC_FCS_DBG(KERN_INFO, fnic,
4320 					 "sending LS_ACC for RRQ request %d\n",
4321 					 iport->fnic->fnic_num);
4322 		break;
4323 
4324 	default:
4325 		FNIC_FCS_DBG(KERN_INFO, fnic,
4326 					 "sending LS_ACC for 0x%x ELS frame\n", type);
4327 		break;
4328 	}
4329 
4330 	frame = fdls_alloc_frame(iport);
4331 	if (frame == NULL) {
4332 		FNIC_FCS_DBG(KERN_ERR, fnic,
4333 				"Failed to allocate frame to send ELS response for 0x%x",
4334 				type);
4335 		return;
4336 	}
4337 
4338 	if (type == ELS_ECHO) {
4339 		/* Brocade sends a longer payload, copy all frame back */
4340 		memcpy(frame, fchdr, len);
4341 	}
4342 
4343 	pels_acc = (struct fc_std_els_acc_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4344 	fdls_init_els_acc_frame(frame, iport);
4345 
4346 	FNIC_STD_SET_D_ID(pels_acc->fchdr, fchdr->fh_s_id);
4347 
4348 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4349 	FNIC_STD_SET_OX_ID(pels_acc->fchdr, oxid);
4350 
4351 	if (type == ELS_ECHO)
4352 		frame_size += len;
4353 	else
4354 		frame_size += sizeof(struct fc_std_els_acc_rsp);
4355 
4356 	fnic_send_fcoe_frame(iport, frame, frame_size);
4357 }
4358 
4359 static void
4360 fdls_process_tgt_abts_rsp(struct fnic_iport_s *iport,
4361 			  struct fc_frame_header *fchdr)
4362 {
4363 	uint32_t s_id;
4364 	struct fnic_tport_s *tport;
4365 	uint32_t tport_state;
4366 	struct fc_std_abts_ba_acc *ba_acc;
4367 	struct fc_std_abts_ba_rjt *ba_rjt;
4368 	uint16_t oxid;
4369 	struct fnic *fnic = iport->fnic;
4370 	int frame_type;
4371 
4372 	s_id = ntoh24(fchdr->fh_s_id);
4373 	ba_acc = (struct fc_std_abts_ba_acc *)fchdr;
4374 	ba_rjt = (struct fc_std_abts_ba_rjt *)fchdr;
4375 
4376 	tport = fnic_find_tport_by_fcid(iport, s_id);
4377 	if (!tport) {
4378 		FNIC_FCS_DBG(KERN_ERR, fnic,
4379 					 "Received tgt abts rsp with invalid SID: 0x%x", s_id);
4380 		return;
4381 	}
4382 	if (tport->timer_pending) {
4383 		FNIC_FCS_DBG(KERN_ERR, fnic,
4384 					 "tport 0x%p Canceling fabric disc timer\n", tport);
4385 		fnic_del_tport_timer_sync(fnic, tport);
4386 	}
4387 	if (iport->state != FNIC_IPORT_STATE_READY) {
4388 		FNIC_FCS_DBG(KERN_ERR, fnic,
4389 					 "Received tgt abts rsp in iport state(%d). Dropping.",
4390 					 iport->state);
4391 		return;
4392 	}
4393 	tport->timer_pending = 0;
4394 	tport->flags &= ~FNIC_FDLS_TGT_ABORT_ISSUED;
4395 	tport_state = tport->state;
4396 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4397 
4398 	/*This abort rsp is for ADISC */
4399 	frame_type = FNIC_FRAME_TYPE(oxid);
4400 	switch (frame_type) {
4401 	case FNIC_FRAME_TYPE_TGT_ADISC:
4402 		if (fchdr->fh_r_ctl == FC_RCTL_BA_ACC) {
4403 			FNIC_FCS_DBG(KERN_ERR, fnic,
4404 				     "OX_ID: 0x%x tgt_fcid: 0x%x rcvd tgt adisc abts resp BA_ACC",
4405 				     be16_to_cpu(ba_acc->acc.ba_ox_id),
4406 				     tport->fcid);
4407 		} else if (fchdr->fh_r_ctl == FC_RCTL_BA_RJT) {
4408 			FNIC_FCS_DBG(KERN_ERR, fnic,
4409 				 "ADISC BA_RJT rcvd tport_fcid: 0x%x tport_state: %d ",
4410 				 tport->fcid, tport_state);
4411 			FNIC_FCS_DBG(KERN_ERR, fnic,
4412 				 "reason code: 0x%x reason code explanation:0x%x ",
4413 				     ba_rjt->rjt.br_reason,
4414 				     ba_rjt->rjt.br_explan);
4415 		}
4416 		if ((tport->retry_counter < FDLS_RETRY_COUNT)
4417 		    && (fchdr->fh_r_ctl == FC_RCTL_BA_ACC)) {
4418 			fdls_free_oxid(iport, oxid, &tport->active_oxid);
4419 			fdls_send_tgt_adisc(iport, tport);
4420 			return;
4421 		}
4422 		fdls_free_oxid(iport, oxid, &tport->active_oxid);
4423 		FNIC_FCS_DBG(KERN_ERR, fnic,
4424 					 "ADISC not responding. Deleting target port: 0x%x",
4425 					 tport->fcid);
4426 		fdls_delete_tport(iport, tport);
4427 		/* Restart discovery of targets */
4428 		if ((iport->state == FNIC_IPORT_STATE_READY)
4429 			&& (iport->fabric.state != FDLS_STATE_SEND_GPNFT)
4430 			&& (iport->fabric.state != FDLS_STATE_RSCN_GPN_FT)) {
4431 			fdls_send_gpn_ft(iport, FDLS_STATE_SEND_GPNFT);
4432 		}
4433 		break;
4434 	case FNIC_FRAME_TYPE_TGT_PLOGI:
4435 		if (fchdr->fh_r_ctl == FC_RCTL_BA_ACC) {
4436 			FNIC_FCS_DBG(KERN_ERR, fnic,
4437 				 "Received tgt PLOGI abts response BA_ACC tgt_fcid: 0x%x",
4438 				 tport->fcid);
4439 		} else if (fchdr->fh_r_ctl == FC_RCTL_BA_RJT) {
4440 			FNIC_FCS_DBG(KERN_INFO, fnic,
4441 				 "PLOGI BA_RJT received for tport_fcid: 0x%x OX_ID: 0x%x",
4442 				     tport->fcid, FNIC_STD_GET_OX_ID(fchdr));
4443 			FNIC_FCS_DBG(KERN_INFO, fnic,
4444 				 "reason code: 0x%x reason code explanation: 0x%x",
4445 				     ba_rjt->rjt.br_reason,
4446 				     ba_rjt->rjt.br_explan);
4447 		}
4448 		if ((tport->retry_counter < iport->max_plogi_retries)
4449 		    && (fchdr->fh_r_ctl == FC_RCTL_BA_ACC)) {
4450 			fdls_free_oxid(iport, oxid, &tport->active_oxid);
4451 			fdls_send_tgt_plogi(iport, tport);
4452 			return;
4453 		}
4454 
4455 		fdls_free_oxid(iport, oxid, &tport->active_oxid);
4456 		fdls_delete_tport(iport, tport);
4457 		/* Restart discovery of targets */
4458 		if ((iport->state == FNIC_IPORT_STATE_READY)
4459 			&& (iport->fabric.state != FDLS_STATE_SEND_GPNFT)
4460 			&& (iport->fabric.state != FDLS_STATE_RSCN_GPN_FT)) {
4461 			fdls_send_gpn_ft(iport, FDLS_STATE_SEND_GPNFT);
4462 		}
4463 		break;
4464 	case FNIC_FRAME_TYPE_TGT_PRLI:
4465 		if (fchdr->fh_r_ctl == FC_RCTL_BA_ACC) {
4466 			FNIC_FCS_DBG(KERN_INFO, fnic,
4467 				 "0x%x: Received tgt PRLI abts response BA_ACC",
4468 				 tport->fcid);
4469 		} else if (fchdr->fh_r_ctl == FC_RCTL_BA_RJT) {
4470 			FNIC_FCS_DBG(KERN_INFO, fnic,
4471 				 "PRLI BA_RJT received for tport_fcid: 0x%x OX_ID: 0x%x ",
4472 				     tport->fcid, FNIC_STD_GET_OX_ID(fchdr));
4473 			FNIC_FCS_DBG(KERN_INFO, fnic,
4474 				 "reason code: 0x%x reason code explanation: 0x%x",
4475 				     ba_rjt->rjt.br_reason,
4476 				     ba_rjt->rjt.br_explan);
4477 		}
4478 		if ((tport->retry_counter < FDLS_RETRY_COUNT)
4479 		    && (fchdr->fh_r_ctl == FC_RCTL_BA_ACC)) {
4480 			fdls_free_oxid(iport, oxid, &tport->active_oxid);
4481 			fdls_send_tgt_prli(iport, tport);
4482 			return;
4483 		}
4484 		fdls_free_oxid(iport, oxid, &tport->active_oxid);
4485 		fdls_send_tgt_plogi(iport, tport);	/* go back to plogi */
4486 		fdls_set_tport_state(tport, FDLS_TGT_STATE_PLOGI);
4487 		break;
4488 	default:
4489 		FNIC_FCS_DBG(KERN_INFO, fnic,
4490 			"Received ABTS response for unknown frame %p", iport);
4491 		break;
4492 	}
4493 
4494 }
4495 
4496 static void
4497 fdls_process_plogi_req(struct fnic_iport_s *iport,
4498 		       struct fc_frame_header *fchdr)
4499 {
4500 	uint8_t *frame;
4501 	struct fc_std_els_rjt_rsp *pplogi_rsp;
4502 	uint16_t oxid;
4503 	uint32_t d_id = ntoh24(fchdr->fh_d_id);
4504 	struct fnic *fnic = iport->fnic;
4505 	uint16_t frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4506 			sizeof(struct fc_std_els_rjt_rsp);
4507 
4508 	if (iport->fcid != d_id) {
4509 		FNIC_FCS_DBG(KERN_INFO, fnic,
4510 			 "Received PLOGI with illegal frame bits. Dropping frame from 0x%x",
4511 			 d_id);
4512 		return;
4513 	}
4514 
4515 	if (iport->state != FNIC_IPORT_STATE_READY) {
4516 		FNIC_FCS_DBG(KERN_INFO, fnic,
4517 			 "Received PLOGI request in iport state: %d Dropping frame",
4518 			 iport->state);
4519 		return;
4520 	}
4521 
4522 	frame = fdls_alloc_frame(iport);
4523 	if (frame == NULL) {
4524 		FNIC_FCS_DBG(KERN_ERR, fnic,
4525 			"Failed to allocate frame to send response to PLOGI request");
4526 		return;
4527 	}
4528 
4529 	pplogi_rsp = (struct fc_std_els_rjt_rsp *) (frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4530 	fdls_init_els_rjt_frame(frame, iport);
4531 
4532 	FNIC_FCS_DBG(KERN_INFO, fnic,
4533 				 "0x%x: Process PLOGI request from SID: 0x%x",
4534 				 iport->fcid, ntoh24(fchdr->fh_s_id));
4535 
4536 	/* We don't support PLOGI request, send a reject */
4537 	pplogi_rsp->rej.er_reason = 0x0B;
4538 	pplogi_rsp->rej.er_explan = 0x0;
4539 	pplogi_rsp->rej.er_vendor = 0x0;
4540 
4541 	FNIC_STD_SET_S_ID(pplogi_rsp->fchdr, fchdr->fh_d_id);
4542 	FNIC_STD_SET_D_ID(pplogi_rsp->fchdr, fchdr->fh_s_id);
4543 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4544 	FNIC_STD_SET_OX_ID(pplogi_rsp->fchdr, oxid);
4545 
4546 	fnic_send_fcoe_frame(iport, frame, frame_size);
4547 }
4548 
4549 static void
4550 fdls_process_logo_req(struct fnic_iport_s *iport, struct fc_frame_header *fchdr)
4551 {
4552 	struct fc_std_logo *logo = (struct fc_std_logo *)fchdr;
4553 	uint32_t nport_id;
4554 	uint64_t nport_name;
4555 	struct fnic_tport_s *tport;
4556 	struct fnic *fnic = iport->fnic;
4557 	uint16_t oxid;
4558 
4559 	nport_id = ntoh24(logo->els.fl_n_port_id);
4560 	nport_name = be64_to_cpu(logo->els.fl_n_port_wwn);
4561 
4562 	FNIC_FCS_DBG(KERN_INFO, fnic,
4563 				 "Process LOGO request from fcid: 0x%x", nport_id);
4564 
4565 	if (iport->state != FNIC_IPORT_STATE_READY) {
4566 		FNIC_FCS_DBG(KERN_ERR, fnic,
4567 			 "Dropping LOGO req from 0x%x in iport state: %d",
4568 			 nport_id, iport->state);
4569 		return;
4570 	}
4571 
4572 	tport = fnic_find_tport_by_fcid(iport, nport_id);
4573 
4574 	if (!tport) {
4575 		/* We are not logged in with the nport, log and drop... */
4576 		FNIC_FCS_DBG(KERN_ERR, fnic,
4577 			 "Received LOGO from an nport not logged in: 0x%x(0x%llx)",
4578 			 nport_id, nport_name);
4579 		return;
4580 	}
4581 	if (tport->fcid != nport_id) {
4582 		FNIC_FCS_DBG(KERN_ERR, fnic,
4583 		 "Received LOGO with invalid target port fcid: 0x%x(0x%llx)",
4584 		 nport_id, nport_name);
4585 		return;
4586 	}
4587 	if (tport->timer_pending) {
4588 		FNIC_FCS_DBG(KERN_ERR, fnic,
4589 					 "tport fcid 0x%x: Canceling disc timer\n",
4590 					 tport->fcid);
4591 		fnic_del_tport_timer_sync(fnic, tport);
4592 		tport->timer_pending = 0;
4593 	}
4594 
4595 	/* got a logo in response to adisc to a target which has logged out */
4596 	if (tport->state == FDLS_TGT_STATE_ADISC) {
4597 		tport->retry_counter = 0;
4598 		oxid = tport->active_oxid;
4599 		fdls_free_oxid(iport, oxid, &tport->active_oxid);
4600 		fdls_delete_tport(iport, tport);
4601 		fdls_send_logo_resp(iport, &logo->fchdr);
4602 		if ((iport->state == FNIC_IPORT_STATE_READY)
4603 			&& (fdls_get_state(&iport->fabric) != FDLS_STATE_SEND_GPNFT)
4604 			&& (fdls_get_state(&iport->fabric) != FDLS_STATE_RSCN_GPN_FT)) {
4605 			FNIC_FCS_DBG(KERN_ERR, fnic,
4606 						 "Sending GPNFT in response to LOGO from Target:0x%x",
4607 						 nport_id);
4608 			fdls_send_gpn_ft(iport, FDLS_STATE_SEND_GPNFT);
4609 			return;
4610 		}
4611 	} else {
4612 		fdls_delete_tport(iport, tport);
4613 	}
4614 	if (iport->state == FNIC_IPORT_STATE_READY) {
4615 		fdls_send_logo_resp(iport, &logo->fchdr);
4616 		if ((fdls_get_state(&iport->fabric) != FDLS_STATE_SEND_GPNFT) &&
4617 			(fdls_get_state(&iport->fabric) != FDLS_STATE_RSCN_GPN_FT)) {
4618 			FNIC_FCS_DBG(KERN_ERR, fnic,
4619 						 "Sending GPNFT in response to LOGO from Target:0x%x",
4620 						 nport_id);
4621 			fdls_send_gpn_ft(iport, FDLS_STATE_SEND_GPNFT);
4622 		}
4623 	}
4624 }
4625 
4626 static void
4627 fdls_process_rscn(struct fnic_iport_s *iport, struct fc_frame_header *fchdr)
4628 {
4629 	struct fc_std_rscn *rscn;
4630 	struct fc_els_rscn_page *rscn_port = NULL;
4631 	int num_ports;
4632 	struct fnic_tport_s *tport, *next;
4633 	uint32_t nport_id;
4634 	uint8_t fcid[3];
4635 	int newports = 0;
4636 	struct fnic_fdls_fabric_s *fdls = &iport->fabric;
4637 	struct fnic *fnic = iport->fnic;
4638 	int rscn_type = NOT_PC_RSCN;
4639 	uint32_t sid = ntoh24(fchdr->fh_s_id);
4640 	unsigned long reset_fnic_list_lock_flags = 0;
4641 	uint16_t rscn_payload_len;
4642 
4643 	atomic64_inc(&iport->iport_stats.num_rscns);
4644 
4645 	FNIC_FCS_DBG(KERN_INFO, fnic,
4646 				 "FDLS process RSCN %p", iport);
4647 
4648 	if (iport->state != FNIC_IPORT_STATE_READY) {
4649 		FNIC_FCS_DBG(KERN_INFO, fnic,
4650 					 "FDLS RSCN received in state(%d). Dropping",
4651 					 fdls_get_state(fdls));
4652 		return;
4653 	}
4654 
4655 	rscn = (struct fc_std_rscn *)fchdr;
4656 	rscn_payload_len = be16_to_cpu(rscn->els.rscn_plen);
4657 
4658 	/* frame validation */
4659 	if ((rscn_payload_len % 4 != 0) || (rscn_payload_len < 8)
4660 	    || (rscn_payload_len > 1024)
4661 	    || (rscn->els.rscn_page_len != 4)) {
4662 		num_ports = 0;
4663 		if ((rscn_payload_len == 0xFFFF)
4664 		    && (sid == FC_FID_FCTRL)) {
4665 			rscn_type = PC_RSCN;
4666 			FNIC_FCS_DBG(KERN_INFO, fnic,
4667 				     "pcrscn: PCRSCN received. sid: 0x%x payload len: 0x%x",
4668 				     sid, rscn_payload_len);
4669 		} else {
4670 			FNIC_FCS_DBG(KERN_INFO, fnic,
4671 					 "RSCN payload_len: 0x%x page_len: 0x%x",
4672 				     rscn_payload_len, rscn->els.rscn_page_len);
4673 			/* if this happens then we need to send ADISC to all the tports. */
4674 			list_for_each_entry_safe(tport, next, &iport->tport_list, links) {
4675 				if (tport->state == FDLS_TGT_STATE_READY)
4676 					tport->flags |= FNIC_FDLS_TPORT_SEND_ADISC;
4677 				FNIC_FCS_DBG(KERN_INFO, fnic,
4678 					 "RSCN for port id: 0x%x", tport->fcid);
4679 			}
4680 		} /* end else */
4681 	} else {
4682 		num_ports = (rscn_payload_len - 4) / rscn->els.rscn_page_len;
4683 		rscn_port = (struct fc_els_rscn_page *)(rscn + 1);
4684 	}
4685 	FNIC_FCS_DBG(KERN_INFO, fnic,
4686 			 "RSCN received for num_ports: %d payload_len: %d page_len: %d ",
4687 		     num_ports, rscn_payload_len, rscn->els.rscn_page_len);
4688 
4689 	/*
4690 	 * RSCN have at least one Port_ID page , but may not have any port_id
4691 	 * in it. If no port_id is specified in the Port_ID page , we send
4692 	 * ADISC to all the tports
4693 	 */
4694 
4695 	while (num_ports) {
4696 
4697 		memcpy(fcid, rscn_port->rscn_fid, 3);
4698 
4699 		nport_id = ntoh24(fcid);
4700 		rscn_port++;
4701 		num_ports--;
4702 		/* if this happens then we need to send ADISC to all the tports. */
4703 		if (nport_id == 0) {
4704 			list_for_each_entry_safe(tport, next, &iport->tport_list,
4705 									 links) {
4706 				if (tport->state == FDLS_TGT_STATE_READY)
4707 					tport->flags |= FNIC_FDLS_TPORT_SEND_ADISC;
4708 
4709 				FNIC_FCS_DBG(KERN_INFO, fnic,
4710 							 "RSCN for port id: 0x%x", tport->fcid);
4711 			}
4712 			break;
4713 		}
4714 		tport = fnic_find_tport_by_fcid(iport, nport_id);
4715 
4716 		FNIC_FCS_DBG(KERN_INFO, fnic,
4717 					 "RSCN port id list: 0x%x", nport_id);
4718 
4719 		if (!tport) {
4720 			newports++;
4721 			continue;
4722 		}
4723 		if (tport->state == FDLS_TGT_STATE_READY)
4724 			tport->flags |= FNIC_FDLS_TPORT_SEND_ADISC;
4725 	}
4726 
4727 	if (pc_rscn_handling_feature_flag == PC_RSCN_HANDLING_FEATURE_ON &&
4728 		rscn_type == PC_RSCN && fnic->role == FNIC_ROLE_FCP_INITIATOR) {
4729 
4730 		if (fnic->pc_rscn_handling_status == PC_RSCN_HANDLING_IN_PROGRESS) {
4731 			FNIC_FCS_DBG(KERN_INFO, fnic,
4732 				 "PCRSCN handling already in progress. Skip host reset: %d",
4733 				 iport->fnic->fnic_num);
4734 			return;
4735 		}
4736 
4737 		FNIC_FCS_DBG(KERN_INFO, fnic,
4738 			 "Processing PCRSCN. Queuing fnic for host reset: %d",
4739 			 iport->fnic->fnic_num);
4740 		fnic->pc_rscn_handling_status = PC_RSCN_HANDLING_IN_PROGRESS;
4741 
4742 		spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
4743 
4744 		spin_lock_irqsave(&reset_fnic_list_lock,
4745 						  reset_fnic_list_lock_flags);
4746 		list_add_tail(&fnic->links, &reset_fnic_list);
4747 		spin_unlock_irqrestore(&reset_fnic_list_lock,
4748 							   reset_fnic_list_lock_flags);
4749 
4750 		queue_work(reset_fnic_work_queue, &reset_fnic_work);
4751 		spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
4752 	} else {
4753 		FNIC_FCS_DBG(KERN_INFO, fnic,
4754 		 "FDLS process RSCN sending GPN_FT: newports: %d", newports);
4755 		fdls_send_gpn_ft(iport, FDLS_STATE_RSCN_GPN_FT);
4756 		fdls_send_rscn_resp(iport, fchdr);
4757 	}
4758 }
4759 
4760 void fnic_fdls_disc_start(struct fnic_iport_s *iport)
4761 {
4762 	struct fnic *fnic = iport->fnic;
4763 
4764 	if (IS_FNIC_FCP_INITIATOR(fnic)) {
4765 		fc_host_fabric_name(iport->fnic->host) = 0;
4766 		fc_host_post_event(iport->fnic->host, fc_get_event_number(),
4767 						FCH_EVT_LIPRESET, 0);
4768 	}
4769 
4770 	if (!iport->usefip) {
4771 		if (iport->flags & FNIC_FIRST_LINK_UP) {
4772 			spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
4773 			fnic_fcpio_reset(iport->fnic);
4774 			spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
4775 
4776 			iport->flags &= ~FNIC_FIRST_LINK_UP;
4777 		}
4778 		fnic_fdls_start_flogi(iport);
4779 	} else
4780 		fnic_fdls_start_plogi(iport);
4781 }
4782 
4783 static void
4784 fdls_process_adisc_req(struct fnic_iport_s *iport,
4785 		       struct fc_frame_header *fchdr)
4786 {
4787 	struct fc_std_els_adisc *padisc_acc;
4788 	struct fc_std_els_adisc *adisc_req = (struct fc_std_els_adisc *)fchdr;
4789 	uint64_t frame_wwnn;
4790 	uint64_t frame_wwpn;
4791 	uint32_t tgt_fcid;
4792 	struct fnic_tport_s *tport;
4793 	uint8_t *fcid;
4794 	uint8_t *rjt_frame;
4795 	uint8_t *acc_frame;
4796 	struct fc_std_els_rjt_rsp *prjts_rsp;
4797 	uint16_t oxid;
4798 	struct fnic *fnic = iport->fnic;
4799 	uint16_t rjt_frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4800 			sizeof(struct fc_std_els_rjt_rsp);
4801 	uint16_t acc_frame_size = FNIC_ETH_FCOE_HDRS_OFFSET +
4802 			sizeof(struct fc_std_els_adisc);
4803 
4804 	FNIC_FCS_DBG(KERN_INFO, fnic,
4805 				 "Process ADISC request %d", iport->fnic->fnic_num);
4806 
4807 	fcid = FNIC_STD_GET_S_ID(fchdr);
4808 	tgt_fcid = ntoh24(fcid);
4809 	tport = fnic_find_tport_by_fcid(iport, tgt_fcid);
4810 	if (!tport) {
4811 		FNIC_FCS_DBG(KERN_ERR, fnic,
4812 					 "tport for fcid: 0x%x not found. Dropping ADISC req.",
4813 					 tgt_fcid);
4814 		return;
4815 	}
4816 	if (iport->state != FNIC_IPORT_STATE_READY) {
4817 		FNIC_FCS_DBG(KERN_ERR, fnic,
4818 			 "Dropping ADISC req from fcid: 0x%x in iport state: %d",
4819 			 tgt_fcid, iport->state);
4820 		return;
4821 	}
4822 
4823 	frame_wwnn = be64_to_cpu(adisc_req->els.adisc_wwnn);
4824 	frame_wwpn = be64_to_cpu(adisc_req->els.adisc_wwpn);
4825 
4826 	if ((frame_wwnn != tport->wwnn) || (frame_wwpn != tport->wwpn)) {
4827 		/* send reject */
4828 		FNIC_FCS_DBG(KERN_ERR, fnic,
4829 			 "ADISC req from fcid: 0x%x mismatch wwpn: 0x%llx wwnn: 0x%llx",
4830 			 tgt_fcid, frame_wwpn, frame_wwnn);
4831 		FNIC_FCS_DBG(KERN_ERR, fnic,
4832 			 "local tport wwpn: 0x%llx wwnn: 0x%llx. Sending RJT",
4833 			 tport->wwpn, tport->wwnn);
4834 
4835 		rjt_frame = fdls_alloc_frame(iport);
4836 		if (rjt_frame == NULL) {
4837 			FNIC_FCS_DBG(KERN_ERR, fnic,
4838 				"Failed to allocate rjt_frame to send response to ADISC request");
4839 			return;
4840 		}
4841 
4842 		prjts_rsp = (struct fc_std_els_rjt_rsp *) (rjt_frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4843 		fdls_init_els_rjt_frame(rjt_frame, iport);
4844 
4845 		prjts_rsp->rej.er_reason = 0x03;	/*  logical error */
4846 		prjts_rsp->rej.er_explan = 0x1E;	/*  N_port login required */
4847 		prjts_rsp->rej.er_vendor = 0x0;
4848 
4849 		FNIC_STD_SET_S_ID(prjts_rsp->fchdr, fchdr->fh_d_id);
4850 		FNIC_STD_SET_D_ID(prjts_rsp->fchdr, fchdr->fh_s_id);
4851 		oxid = FNIC_STD_GET_OX_ID(fchdr);
4852 		FNIC_STD_SET_OX_ID(prjts_rsp->fchdr, oxid);
4853 
4854 		fnic_send_fcoe_frame(iport, rjt_frame, rjt_frame_size);
4855 		return;
4856 	}
4857 
4858 	acc_frame = fdls_alloc_frame(iport);
4859 	if (acc_frame == NULL) {
4860 		FNIC_FCS_DBG(KERN_ERR, fnic,
4861 				"Failed to allocate frame to send ADISC accept");
4862 		return;
4863 	}
4864 
4865 	padisc_acc = (struct fc_std_els_adisc *) (acc_frame + FNIC_ETH_FCOE_HDRS_OFFSET);
4866 
4867 	FNIC_STD_SET_S_ID(padisc_acc->fchdr, fchdr->fh_d_id);
4868 	FNIC_STD_SET_D_ID(padisc_acc->fchdr, fchdr->fh_s_id);
4869 
4870 	FNIC_STD_SET_F_CTL(padisc_acc->fchdr, FNIC_ELS_REP_FCTL << 16);
4871 	FNIC_STD_SET_R_CTL(padisc_acc->fchdr, FC_RCTL_ELS_REP);
4872 	FNIC_STD_SET_TYPE(padisc_acc->fchdr, FC_TYPE_ELS);
4873 
4874 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4875 	FNIC_STD_SET_OX_ID(padisc_acc->fchdr, oxid);
4876 	FNIC_STD_SET_RX_ID(padisc_acc->fchdr, FNIC_UNASSIGNED_RXID);
4877 
4878 	padisc_acc->els.adisc_cmd = ELS_LS_ACC;
4879 
4880 	FNIC_STD_SET_NPORT_NAME(&padisc_acc->els.adisc_wwpn,
4881 			iport->wwpn);
4882 	FNIC_STD_SET_NODE_NAME(&padisc_acc->els.adisc_wwnn,
4883 			iport->wwnn);
4884 	memcpy(padisc_acc->els.adisc_port_id, fchdr->fh_d_id, 3);
4885 
4886 	fnic_send_fcoe_frame(iport, acc_frame, acc_frame_size);
4887 }
4888 
4889 /*
4890  * Performs a validation for all FCOE frames and return the frame type
4891  */
4892 int
4893 fnic_fdls_validate_and_get_frame_type(struct fnic_iport_s *iport,
4894 	struct fc_frame_header *fchdr)
4895 {
4896 	uint8_t type;
4897 	uint8_t *fc_payload;
4898 	uint16_t oxid;
4899 	uint32_t s_id;
4900 	uint32_t d_id;
4901 	struct fnic *fnic = iport->fnic;
4902 	struct fnic_fdls_fabric_s *fabric = &iport->fabric;
4903 	int oxid_frame_type;
4904 
4905 	oxid = FNIC_STD_GET_OX_ID(fchdr);
4906 	fc_payload = (uint8_t *) fchdr + sizeof(struct fc_frame_header);
4907 	type = *fc_payload;
4908 	s_id = ntoh24(fchdr->fh_s_id);
4909 	d_id = ntoh24(fchdr->fh_d_id);
4910 
4911 	/* some common validation */
4912 	if (fdls_get_state(fabric) > FDLS_STATE_FABRIC_FLOGI) {
4913 		if (iport->fcid != d_id || (!FNIC_FC_FRAME_CS_CTL(fchdr))) {
4914 			FNIC_FCS_DBG(KERN_INFO, fnic,
4915 				     "invalid frame received. Dropping frame");
4916 			return -1;
4917 		}
4918 	}
4919 
4920 	/*  BLS ABTS response */
4921 	if ((fchdr->fh_r_ctl == FC_RCTL_BA_ACC)
4922 	|| (fchdr->fh_r_ctl == FC_RCTL_BA_RJT)) {
4923 		if (!(FNIC_FC_FRAME_TYPE_BLS(fchdr))) {
4924 			FNIC_FCS_DBG(KERN_INFO, fnic,
4925 						 "Received ABTS invalid frame. Dropping frame");
4926 			return -1;
4927 
4928 		}
4929 		if (fdls_is_oxid_fabric_req(oxid)) {
4930 			if (!(iport->fabric.flags & FNIC_FDLS_FABRIC_ABORT_ISSUED)) {
4931 				FNIC_FCS_DBG(KERN_INFO, fnic,
4932 					"Received unexpected ABTS RSP(oxid:0x%x) from 0x%x. Dropping frame",
4933 					oxid, s_id);
4934 				return -1;
4935 		}
4936 			return FNIC_FABRIC_BLS_ABTS_RSP;
4937 		} else if (fdls_is_oxid_fdmi_req(oxid)) {
4938 			return FNIC_FDMI_BLS_ABTS_RSP;
4939 		} else if (fdls_is_oxid_tgt_req(oxid)) {
4940 			return FNIC_TPORT_BLS_ABTS_RSP;
4941 		} else if (fdls_is_oxid_nvme_req(oxid)) {
4942 			return FNIC_LS_REQ_ABTS_RSP;
4943 		}
4944 		FNIC_FCS_DBG(KERN_INFO, fnic,
4945 			"Received ABTS rsp with unknown oxid(0x%x) from 0x%x. Dropping frame",
4946 			oxid, s_id);
4947 		return -1;
4948 	}
4949 
4950 	/* BLS ABTS Req */
4951 	if ((fchdr->fh_r_ctl == FC_RCTL_BA_ABTS)
4952 	&& (FNIC_FC_FRAME_TYPE_BLS(fchdr))) {
4953 		FNIC_FCS_DBG(KERN_INFO, fnic,
4954 					 "Receiving Abort Request from s_id: 0x%x", s_id);
4955 		return FNIC_BLS_ABTS_REQ;
4956 	}
4957 
4958 	/* unsolicited requests frames */
4959 	if (FNIC_FC_FRAME_UNSOLICITED(fchdr)) {
4960 		switch (type) {
4961 		case ELS_LOGO:
4962 			if ((!FNIC_FC_FRAME_FCTL_FIRST_LAST_SEQINIT(fchdr))
4963 				|| (!FNIC_FC_FRAME_UNSOLICITED(fchdr))
4964 				|| (!FNIC_FC_FRAME_TYPE_ELS(fchdr))) {
4965 				FNIC_FCS_DBG(KERN_INFO, fnic,
4966 							 "Received LOGO invalid frame. Dropping frame");
4967 				return -1;
4968 			}
4969 			return FNIC_ELS_LOGO_REQ;
4970 		case ELS_RSCN:
4971 			if ((!FNIC_FC_FRAME_FCTL_FIRST_LAST_SEQINIT(fchdr))
4972 				|| (!FNIC_FC_FRAME_TYPE_ELS(fchdr))
4973 				|| (!FNIC_FC_FRAME_UNSOLICITED(fchdr))) {
4974 				FNIC_FCS_DBG(KERN_INFO, fnic,
4975 						 "Received RSCN invalid FCTL. Dropping frame");
4976 				return -1;
4977 			}
4978 			if (s_id != FC_FID_FCTRL)
4979 				FNIC_FCS_DBG(KERN_INFO, fnic,
4980 				     "Received RSCN from target FCTL: 0x%x type: 0x%x s_id: 0x%x.",
4981 				     fchdr->fh_f_ctl[0], fchdr->fh_type, s_id);
4982 			return FNIC_ELS_RSCN_REQ;
4983 		case ELS_PLOGI:
4984 			return FNIC_ELS_PLOGI_REQ;
4985 		case ELS_ECHO:
4986 			return FNIC_ELS_ECHO_REQ;
4987 		case ELS_ADISC:
4988 			return FNIC_ELS_ADISC;
4989 		case ELS_RLS:
4990 			return FNIC_ELS_RLS;
4991 		case ELS_RRQ:
4992 			return FNIC_ELS_RRQ;
4993 		default:
4994 			FNIC_FCS_DBG(KERN_INFO, fnic,
4995 				 "Unsupported frame (type:0x%02x) from fcid: 0x%x",
4996 				 type, s_id);
4997 			return FNIC_ELS_UNSUPPORTED_REQ;
4998 		}
4999 	}
5000 
5001 	/* solicited response from fabric or target */
5002 	oxid_frame_type = FNIC_FRAME_TYPE(oxid);
5003 	FNIC_FCS_DBG(KERN_INFO, fnic,
5004 			"oxid frame code: 0x%x, oxid: 0x%x\n", oxid_frame_type, oxid);
5005 	switch (oxid_frame_type) {
5006 	case FNIC_FRAME_TYPE_FABRIC_FLOGI:
5007 		if (type == ELS_LS_ACC) {
5008 			if ((s_id != FC_FID_FLOGI)
5009 				|| (!FNIC_FC_FRAME_TYPE_ELS(fchdr))) {
5010 				FNIC_FCS_DBG(KERN_INFO, fnic,
5011 					 "Received unknown frame. Dropping frame");
5012 				return -1;
5013 			}
5014 		}
5015 		return FNIC_FABRIC_FLOGI_RSP;
5016 
5017 	case FNIC_FRAME_TYPE_FABRIC_PLOGI:
5018 		if (type == ELS_LS_ACC) {
5019 			if ((s_id != FC_FID_DIR_SERV)
5020 				|| (!FNIC_FC_FRAME_TYPE_ELS(fchdr))) {
5021 				FNIC_FCS_DBG(KERN_INFO, fnic,
5022 					 "Received unknown frame. Dropping frame");
5023 				return -1;
5024 			}
5025 		}
5026 		return FNIC_FABRIC_PLOGI_RSP;
5027 
5028 	case FNIC_FRAME_TYPE_FABRIC_SCR:
5029 		if (type == ELS_LS_ACC) {
5030 			if ((s_id != FC_FID_FCTRL)
5031 				|| (!FNIC_FC_FRAME_TYPE_ELS(fchdr))) {
5032 				FNIC_FCS_DBG(KERN_INFO, fnic,
5033 					 "Received unknown frame. Dropping frame");
5034 				return -1;
5035 			}
5036 		}
5037 		return FNIC_FABRIC_SCR_RSP;
5038 
5039 	case FNIC_FRAME_TYPE_FABRIC_RPN:
5040 		if ((s_id != FC_FID_DIR_SERV) || (!FNIC_FC_FRAME_TYPE_FC_GS(fchdr))) {
5041 			FNIC_FCS_DBG(KERN_INFO, fnic,
5042 					 "Received unknown frame. Dropping frame");
5043 			return -1;
5044 		}
5045 		return FNIC_FABRIC_RPN_RSP;
5046 
5047 	case FNIC_FRAME_TYPE_FABRIC_RFT:
5048 		if ((s_id != FC_FID_DIR_SERV) || (!FNIC_FC_FRAME_TYPE_FC_GS(fchdr))) {
5049 			FNIC_FCS_DBG(KERN_INFO, fnic,
5050 					 "Received unknown frame. Dropping frame");
5051 			return -1;
5052 		}
5053 		return FNIC_FABRIC_RFT_RSP;
5054 
5055 	case FNIC_FRAME_TYPE_FABRIC_RFF:
5056 		if ((s_id != FC_FID_DIR_SERV) || (!FNIC_FC_FRAME_TYPE_FC_GS(fchdr))) {
5057 			FNIC_FCS_DBG(KERN_INFO, fnic,
5058 					 "Received unknown frame. Dropping frame");
5059 			return -1;
5060 		}
5061 		return FNIC_FABRIC_RFF_RSP;
5062 
5063 	case FNIC_FRAME_TYPE_FABRIC_GPN_FT:
5064 		if ((s_id != FC_FID_DIR_SERV) || (!FNIC_FC_FRAME_TYPE_FC_GS(fchdr))) {
5065 			FNIC_FCS_DBG(KERN_INFO, fnic,
5066 					 "Received unknown frame. Dropping frame");
5067 			return -1;
5068 		}
5069 		return FNIC_FABRIC_GPN_FT_RSP;
5070 
5071 	case FNIC_FRAME_TYPE_FABRIC_LOGO:
5072 		return FNIC_FABRIC_LOGO_RSP;
5073 	case FNIC_FRAME_TYPE_FDMI_PLOGI:
5074 		return FNIC_FDMI_PLOGI_RSP;
5075 	case FNIC_FRAME_TYPE_FDMI_RHBA:
5076 		return FNIC_FDMI_REG_HBA_RSP;
5077 	case FNIC_FRAME_TYPE_FDMI_RPA:
5078 		return FNIC_FDMI_RPA_RSP;
5079 	case FNIC_FRAME_TYPE_TGT_PLOGI:
5080 		return FNIC_TPORT_PLOGI_RSP;
5081 	case FNIC_FRAME_TYPE_TGT_PRLI:
5082 		return FNIC_TPORT_PRLI_RSP;
5083 	case FNIC_FRAME_TYPE_TGT_ADISC:
5084 		return FNIC_TPORT_ADISC_RSP;
5085 	case FNIC_FRAME_TYPE_TGT_LOGO:
5086 		if (!FNIC_FC_FRAME_TYPE_ELS(fchdr)) {
5087 			FNIC_FCS_DBG(KERN_INFO, fnic,
5088 				"Dropping Unknown frame in tport solicited exchange range type: 0x%x.",
5089 				     fchdr->fh_type);
5090 			return -1;
5091 		}
5092 		return FNIC_TPORT_LOGO_RSP;
5093 	default:
5094 		/* Drop the Rx frame and log/stats it */
5095 		FNIC_FCS_DBG(KERN_INFO, fnic,
5096 					 "Solicited response: unknown OXID: 0x%x", oxid);
5097 		return -1;
5098 	}
5099 
5100 	return -1;
5101 }
5102 
5103 void fnic_fdls_recv_frame(struct fnic_iport_s *iport, void *rx_frame,
5104 						  int len, int fchdr_offset)
5105 {
5106 	struct fc_frame_header *fchdr;
5107 	uint32_t s_id = 0;
5108 	uint32_t d_id = 0;
5109 	struct fnic *fnic = iport->fnic;
5110 	int frame_type;
5111 
5112 	fchdr = (struct fc_frame_header *) ((uint8_t *) rx_frame + fchdr_offset);
5113 	s_id = ntoh24(fchdr->fh_s_id);
5114 	d_id = ntoh24(fchdr->fh_d_id);
5115 
5116 	fnic_debug_dump_fc_frame(fnic, fchdr, len, "Incoming");
5117 
5118 	frame_type =
5119 		fnic_fdls_validate_and_get_frame_type(iport, fchdr);
5120 
5121 	/*if we are in flogo drop everything else */
5122 	if (iport->fabric.state == FDLS_STATE_FABRIC_LOGO &&
5123 		frame_type != FNIC_FABRIC_LOGO_RSP)
5124 		return;
5125 
5126 	switch (frame_type) {
5127 	case FNIC_FABRIC_FLOGI_RSP:
5128 		fdls_process_flogi_rsp(iport, fchdr, rx_frame);
5129 		break;
5130 	case FNIC_FABRIC_PLOGI_RSP:
5131 		fdls_process_fabric_plogi_rsp(iport, fchdr);
5132 		break;
5133 	case FNIC_FDMI_PLOGI_RSP:
5134 		fdls_process_fdmi_plogi_rsp(iport, fchdr);
5135 		break;
5136 	case FNIC_FABRIC_RPN_RSP:
5137 		fdls_process_rpn_id_rsp(iport, fchdr);
5138 		break;
5139 	case FNIC_FABRIC_RFT_RSP:
5140 		fdls_process_rft_id_rsp(iport, fchdr);
5141 		break;
5142 	case FNIC_FABRIC_RFF_RSP:
5143 		fdls_process_rff_id_rsp(iport, fchdr);
5144 		break;
5145 	case FNIC_FABRIC_SCR_RSP:
5146 		fdls_process_scr_rsp(iport, fchdr);
5147 		break;
5148 	case FNIC_FABRIC_GPN_FT_RSP:
5149 		fdls_process_gpn_ft_rsp(iport, fchdr, len);
5150 		break;
5151 	case FNIC_TPORT_PLOGI_RSP:
5152 		fdls_process_tgt_plogi_rsp(iport, fchdr);
5153 		break;
5154 	case FNIC_TPORT_PRLI_RSP:
5155 		fdls_process_tgt_prli_rsp(iport, fchdr);
5156 		break;
5157 	case FNIC_TPORT_ADISC_RSP:
5158 		fdls_process_tgt_adisc_rsp(iport, fchdr);
5159 		break;
5160 	case FNIC_TPORT_BLS_ABTS_RSP:
5161 		fdls_process_tgt_abts_rsp(iport, fchdr);
5162 		break;
5163 	case FNIC_TPORT_LOGO_RSP:
5164 		/* Logo response from tgt which we have deleted */
5165 		FNIC_FCS_DBG(KERN_INFO, fnic,
5166 					 "Logo response from tgt: 0x%x",
5167 			     ntoh24(fchdr->fh_s_id));
5168 		break;
5169 	case FNIC_FABRIC_LOGO_RSP:
5170 		fdls_process_fabric_logo_rsp(iport, fchdr);
5171 		break;
5172 	case FNIC_FABRIC_BLS_ABTS_RSP:
5173 			fdls_process_fabric_abts_rsp(iport, fchdr);
5174 		break;
5175 	case FNIC_LS_REQ_ABTS_RSP:
5176 		nvfnic_process_ls_abts_rsp(iport, fchdr);
5177 		break;
5178 	case FNIC_FDMI_BLS_ABTS_RSP:
5179 		fdls_process_fdmi_abts_rsp(iport, fchdr);
5180 		break;
5181 	case FNIC_BLS_ABTS_REQ:
5182 		fdls_process_abts_req(iport, fchdr);
5183 		break;
5184 	case FNIC_ELS_UNSUPPORTED_REQ:
5185 		fdls_process_unsupported_els_req(iport, fchdr);
5186 		break;
5187 	case FNIC_ELS_PLOGI_REQ:
5188 		fdls_process_plogi_req(iport, fchdr);
5189 		break;
5190 	case FNIC_ELS_RSCN_REQ:
5191 		fdls_process_rscn(iport, fchdr);
5192 		break;
5193 	case FNIC_ELS_LOGO_REQ:
5194 		fdls_process_logo_req(iport, fchdr);
5195 		break;
5196 	case FNIC_ELS_RRQ:
5197 	case FNIC_ELS_ECHO_REQ:
5198 		fdls_process_els_req(iport, fchdr, len);
5199 		break;
5200 	case FNIC_ELS_ADISC:
5201 		fdls_process_adisc_req(iport, fchdr);
5202 		break;
5203 	case FNIC_ELS_RLS:
5204 		fdls_process_rls_req(iport, fchdr);
5205 		break;
5206 	case FNIC_FDMI_REG_HBA_RSP:
5207 	case FNIC_FDMI_RPA_RSP:
5208 		fdls_process_fdmi_reg_ack(iport, fchdr, frame_type);
5209 		break;
5210 	default:
5211 		FNIC_FCS_DBG(KERN_INFO, fnic,
5212 			 "s_id: 0x%x d_did: 0x%x", s_id, d_id);
5213 		FNIC_FCS_DBG(KERN_INFO, fnic,
5214 			 "Received unknown FCoE frame of len: %d. Dropping frame", len);
5215 		break;
5216 	}
5217 }
5218 
5219 void fnic_fdls_disc_init(struct fnic_iport_s *iport)
5220 {
5221 	fdls_reset_oxid_pool(iport);
5222 	fdls_set_state((&iport->fabric), FDLS_STATE_INIT);
5223 }
5224 
5225 void fnic_fdls_link_down(struct fnic_iport_s *iport)
5226 {
5227 	struct fnic_tport_s *tport, *next;
5228 	struct fnic *fnic = iport->fnic;
5229 
5230 	FNIC_FCS_DBG(KERN_INFO, fnic,
5231 				 "0x%x: FDLS processing link down", iport->fcid);
5232 
5233 	fdls_set_state((&iport->fabric), FDLS_STATE_LINKDOWN);
5234 	iport->fabric.flags = 0;
5235 
5236 	if (IS_FNIC_FCP_INITIATOR(fnic) ||
5237 	    IS_FNIC_NVME_INITIATOR(fnic)) {
5238 		spin_unlock_irqrestore(&fnic->fnic_lock, fnic->lock_flags);
5239 		fnic_fcpio_reset(iport->fnic);
5240 		spin_lock_irqsave(&fnic->fnic_lock, fnic->lock_flags);
5241 		list_for_each_entry_safe(tport, next, &iport->tport_list, links) {
5242 			FNIC_FCS_DBG(KERN_INFO, fnic,
5243 					 "removing rport: 0x%x", tport->fcid);
5244 			fdls_delete_tport(iport, tport);
5245 		}
5246 	}
5247 	fdls_reset_oxid_pool(iport);
5248 
5249 	if (fnic_fdmi_support == 1) {
5250 		if (iport->fabric.fdmi_pending > 0) {
5251 			timer_delete_sync(&iport->fabric.fdmi_timer);
5252 			iport->fabric.fdmi_pending = 0;
5253 		}
5254 		iport->flags &= ~FNIC_FDMI_ACTIVE;
5255 	}
5256 
5257 	FNIC_FCS_DBG(KERN_INFO, fnic,
5258 				 "0x%x: FDLS finish processing link down", iport->fcid);
5259 }
5260