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