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