xref: /linux/drivers/scsi/csiostor/csio_lnode.c (revision 2fe05e1139a555ae91f00a812cb9520e7d3022ab)
1 /*
2  * This file is part of the Chelsio FCoE driver for Linux.
3  *
4  * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include <linux/utsname.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_transport_fc.h>
41 #include <asm/unaligned.h>
42 #include <scsi/fc/fc_els.h>
43 #include <scsi/fc/fc_fs.h>
44 #include <scsi/fc/fc_gs.h>
45 #include <scsi/fc/fc_ms.h>
46 
47 #include "csio_hw.h"
48 #include "csio_mb.h"
49 #include "csio_lnode.h"
50 #include "csio_rnode.h"
51 
52 int csio_fcoe_rnodes = 1024;
53 int csio_fdmi_enable = 1;
54 
55 #define PORT_ID_PTR(_x)         ((uint8_t *)(&_x) + 1)
56 
57 /* Lnode SM declarations */
58 static void csio_lns_uninit(struct csio_lnode *, enum csio_ln_ev);
59 static void csio_lns_online(struct csio_lnode *, enum csio_ln_ev);
60 static void csio_lns_ready(struct csio_lnode *, enum csio_ln_ev);
61 static void csio_lns_offline(struct csio_lnode *, enum csio_ln_ev);
62 
63 static int csio_ln_mgmt_submit_req(struct csio_ioreq *,
64 		void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
65 		enum fcoe_cmn_type, struct csio_dma_buf *, uint32_t);
66 
67 /* LN event mapping */
68 static enum csio_ln_ev fwevt_to_lnevt[] = {
69 	CSIO_LNE_NONE,		/* None */
70 	CSIO_LNE_NONE,		/* PLOGI_ACC_RCVD  */
71 	CSIO_LNE_NONE,		/* PLOGI_RJT_RCVD  */
72 	CSIO_LNE_NONE,		/* PLOGI_RCVD	   */
73 	CSIO_LNE_NONE,		/* PLOGO_RCVD	   */
74 	CSIO_LNE_NONE,		/* PRLI_ACC_RCVD   */
75 	CSIO_LNE_NONE,		/* PRLI_RJT_RCVD   */
76 	CSIO_LNE_NONE,		/* PRLI_RCVD	   */
77 	CSIO_LNE_NONE,		/* PRLO_RCVD	   */
78 	CSIO_LNE_NONE,		/* NPORT_ID_CHGD   */
79 	CSIO_LNE_LOGO,		/* FLOGO_RCVD	   */
80 	CSIO_LNE_LOGO,		/* CLR_VIRT_LNK_RCVD */
81 	CSIO_LNE_FAB_INIT_DONE,/* FLOGI_ACC_RCVD   */
82 	CSIO_LNE_NONE,		/* FLOGI_RJT_RCVD   */
83 	CSIO_LNE_FAB_INIT_DONE,/* FDISC_ACC_RCVD   */
84 	CSIO_LNE_NONE,		/* FDISC_RJT_RCVD   */
85 	CSIO_LNE_NONE,		/* FLOGI_TMO_MAX_RETRY */
86 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_ACC */
87 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_RJT */
88 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_CNFLT */
89 	CSIO_LNE_NONE,		/* PRLI_TMO		*/
90 	CSIO_LNE_NONE,		/* ADISC_TMO		*/
91 	CSIO_LNE_NONE,		/* RSCN_DEV_LOST */
92 	CSIO_LNE_NONE,		/* SCR_ACC_RCVD */
93 	CSIO_LNE_NONE,		/* ADISC_RJT_RCVD */
94 	CSIO_LNE_NONE,		/* LOGO_SNT */
95 	CSIO_LNE_NONE,		/* PROTO_ERR_IMPL_LOGO */
96 };
97 
98 #define CSIO_FWE_TO_LNE(_evt)	((_evt > PROTO_ERR_IMPL_LOGO) ?		\
99 						CSIO_LNE_NONE :	\
100 						fwevt_to_lnevt[_evt])
101 
102 #define csio_ct_rsp(cp)		(((struct fc_ct_hdr *)cp)->ct_cmd)
103 #define csio_ct_reason(cp)	(((struct fc_ct_hdr *)cp)->ct_reason)
104 #define csio_ct_expl(cp)	(((struct fc_ct_hdr *)cp)->ct_explan)
105 #define csio_ct_get_pld(cp)	((void *)(((uint8_t *)cp) + FC_CT_HDR_LEN))
106 
107 /*
108  * csio_ln_match_by_portid - lookup lnode using given portid.
109  * @hw: HW module
110  * @portid: port-id.
111  *
112  * If found, returns lnode matching given portid otherwise returns NULL.
113  */
114 static struct csio_lnode *
115 csio_ln_lookup_by_portid(struct csio_hw *hw, uint8_t portid)
116 {
117 	struct csio_lnode *ln = hw->rln;
118 	struct list_head *tmp;
119 
120 	/* Match siblings lnode with portid */
121 	list_for_each(tmp, &hw->sln_head) {
122 		ln = (struct csio_lnode *) tmp;
123 		if (ln->portid == portid)
124 			return ln;
125 	}
126 
127 	return NULL;
128 }
129 
130 /*
131  * csio_ln_lookup_by_vnpi - Lookup lnode using given vnp id.
132  * @hw - HW module
133  * @vnpi - vnp index.
134  * Returns - If found, returns lnode matching given vnp id
135  * otherwise returns NULL.
136  */
137 static struct csio_lnode *
138 csio_ln_lookup_by_vnpi(struct csio_hw *hw, uint32_t vnp_id)
139 {
140 	struct list_head *tmp1, *tmp2;
141 	struct csio_lnode *sln = NULL, *cln = NULL;
142 
143 	if (list_empty(&hw->sln_head)) {
144 		CSIO_INC_STATS(hw, n_lnlkup_miss);
145 		return NULL;
146 	}
147 	/* Traverse sibling lnodes */
148 	list_for_each(tmp1, &hw->sln_head) {
149 		sln = (struct csio_lnode *) tmp1;
150 
151 		/* Match sibling lnode */
152 		if (sln->vnp_flowid == vnp_id)
153 			return sln;
154 
155 		if (list_empty(&sln->cln_head))
156 			continue;
157 
158 		/* Traverse children lnodes */
159 		list_for_each(tmp2, &sln->cln_head) {
160 			cln = (struct csio_lnode *) tmp2;
161 
162 			if (cln->vnp_flowid == vnp_id)
163 				return cln;
164 		}
165 	}
166 	CSIO_INC_STATS(hw, n_lnlkup_miss);
167 	return NULL;
168 }
169 
170 /**
171  * csio_lnode_lookup_by_wwpn - Lookup lnode using given wwpn.
172  * @hw:		HW module.
173  * @wwpn:	WWPN.
174  *
175  * If found, returns lnode matching given wwpn, returns NULL otherwise.
176  */
177 struct csio_lnode *
178 csio_lnode_lookup_by_wwpn(struct csio_hw *hw, uint8_t *wwpn)
179 {
180 	struct list_head *tmp1, *tmp2;
181 	struct csio_lnode *sln = NULL, *cln = NULL;
182 
183 	if (list_empty(&hw->sln_head)) {
184 		CSIO_INC_STATS(hw, n_lnlkup_miss);
185 		return NULL;
186 	}
187 	/* Traverse sibling lnodes */
188 	list_for_each(tmp1, &hw->sln_head) {
189 		sln = (struct csio_lnode *) tmp1;
190 
191 		/* Match sibling lnode */
192 		if (!memcmp(csio_ln_wwpn(sln), wwpn, 8))
193 			return sln;
194 
195 		if (list_empty(&sln->cln_head))
196 			continue;
197 
198 		/* Traverse children lnodes */
199 		list_for_each(tmp2, &sln->cln_head) {
200 			cln = (struct csio_lnode *) tmp2;
201 
202 			if (!memcmp(csio_ln_wwpn(cln), wwpn, 8))
203 				return cln;
204 		}
205 	}
206 	return NULL;
207 }
208 
209 /* FDMI */
210 static void
211 csio_fill_ct_iu(void *buf, uint8_t type, uint8_t sub_type, uint16_t op)
212 {
213 	struct fc_ct_hdr *cmd = (struct fc_ct_hdr *)buf;
214 	cmd->ct_rev = FC_CT_REV;
215 	cmd->ct_fs_type = type;
216 	cmd->ct_fs_subtype = sub_type;
217 	cmd->ct_cmd = htons(op);
218 }
219 
220 static int
221 csio_hostname(uint8_t *buf, size_t buf_len)
222 {
223 	if (snprintf(buf, buf_len, "%s", init_utsname()->nodename) > 0)
224 		return 0;
225 	return -1;
226 }
227 
228 static int
229 csio_osname(uint8_t *buf, size_t buf_len)
230 {
231 	if (snprintf(buf, buf_len, "%s %s %s",
232 		     init_utsname()->sysname,
233 		     init_utsname()->release,
234 		     init_utsname()->version) > 0)
235 		return 0;
236 
237 	return -1;
238 }
239 
240 static inline void
241 csio_append_attrib(uint8_t **ptr, uint16_t type, void *val, size_t val_len)
242 {
243 	uint16_t len;
244 	struct fc_fdmi_attr_entry *ae = (struct fc_fdmi_attr_entry *)*ptr;
245 
246 	if (WARN_ON(val_len > U16_MAX))
247 		return;
248 
249 	len = val_len;
250 
251 	ae->type = htons(type);
252 	len += 4;		/* includes attribute type and length */
253 	len = (len + 3) & ~3;	/* should be multiple of 4 bytes */
254 	ae->len = htons(len);
255 	memcpy(ae->value, val, val_len);
256 	if (len > val_len)
257 		memset(ae->value + val_len, 0, len - val_len);
258 	*ptr += len;
259 }
260 
261 /*
262  * csio_ln_fdmi_done - FDMI registeration completion
263  * @hw: HW context
264  * @fdmi_req: fdmi request
265  */
266 static void
267 csio_ln_fdmi_done(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
268 {
269 	void *cmd;
270 	struct csio_lnode *ln = fdmi_req->lnode;
271 
272 	if (fdmi_req->wr_status != FW_SUCCESS) {
273 		csio_ln_dbg(ln, "WR error:%x in processing fdmi rpa cmd\n",
274 			    fdmi_req->wr_status);
275 		CSIO_INC_STATS(ln, n_fdmi_err);
276 	}
277 
278 	cmd = fdmi_req->dma_buf.vaddr;
279 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
280 		csio_ln_dbg(ln, "fdmi rpa cmd rejected reason %x expl %x\n",
281 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
282 	}
283 }
284 
285 /*
286  * csio_ln_fdmi_rhba_cbfn - RHBA completion
287  * @hw: HW context
288  * @fdmi_req: fdmi request
289  */
290 static void
291 csio_ln_fdmi_rhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
292 {
293 	void *cmd;
294 	uint8_t *pld;
295 	uint32_t len = 0;
296 	__be32 val;
297 	__be16 mfs;
298 	uint32_t numattrs = 0;
299 	struct csio_lnode *ln = fdmi_req->lnode;
300 	struct fs_fdmi_attrs *attrib_blk;
301 	struct fc_fdmi_port_name *port_name;
302 	uint8_t buf[64];
303 	uint8_t *fc4_type;
304 
305 	if (fdmi_req->wr_status != FW_SUCCESS) {
306 		csio_ln_dbg(ln, "WR error:%x in processing fdmi rhba cmd\n",
307 			    fdmi_req->wr_status);
308 		CSIO_INC_STATS(ln, n_fdmi_err);
309 	}
310 
311 	cmd = fdmi_req->dma_buf.vaddr;
312 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
313 		csio_ln_dbg(ln, "fdmi rhba cmd rejected reason %x expl %x\n",
314 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
315 	}
316 
317 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
318 		CSIO_INC_STATS(ln, n_fdmi_err);
319 		return;
320 	}
321 
322 	/* Prepare CT hdr for RPA cmd */
323 	memset(cmd, 0, FC_CT_HDR_LEN);
324 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RPA);
325 
326 	/* Prepare RPA payload */
327 	pld = (uint8_t *)csio_ct_get_pld(cmd);
328 	port_name = (struct fc_fdmi_port_name *)pld;
329 	memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
330 	pld += sizeof(*port_name);
331 
332 	/* Start appending Port attributes */
333 	attrib_blk = (struct fs_fdmi_attrs *)pld;
334 	attrib_blk->numattrs = 0;
335 	len += sizeof(attrib_blk->numattrs);
336 	pld += sizeof(attrib_blk->numattrs);
337 
338 	fc4_type = &buf[0];
339 	memset(fc4_type, 0, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
340 	fc4_type[2] = 1;
341 	fc4_type[7] = 1;
342 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_FC4TYPES,
343 			   fc4_type, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
344 	numattrs++;
345 	val = htonl(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
346 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_SUPPORTEDSPEED,
347 			   &val,
348 			   FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN);
349 	numattrs++;
350 
351 	if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_1G)
352 		val = htonl(FC_PORTSPEED_1GBIT);
353 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_10G)
354 		val = htonl(FC_PORTSPEED_10GBIT);
355 	else
356 		val = htonl(CSIO_HBA_PORTSPEED_UNKNOWN);
357 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED,
358 			   &val, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN);
359 	numattrs++;
360 
361 	mfs = ln->ln_sparm.csp.sp_bb_data;
362 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_MAXFRAMESIZE,
363 			   &mfs, sizeof(mfs));
364 	numattrs++;
365 
366 	strcpy(buf, "csiostor");
367 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_OSDEVICENAME, buf,
368 			   strlen(buf));
369 	numattrs++;
370 
371 	if (!csio_hostname(buf, sizeof(buf))) {
372 		csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_HOSTNAME,
373 				   buf, strlen(buf));
374 		numattrs++;
375 	}
376 	attrib_blk->numattrs = htonl(numattrs);
377 	len = (uint32_t)(pld - (uint8_t *)cmd);
378 
379 	/* Submit FDMI RPA request */
380 	spin_lock_irq(&hw->lock);
381 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_done,
382 				FCOE_CT, &fdmi_req->dma_buf, len)) {
383 		CSIO_INC_STATS(ln, n_fdmi_err);
384 		csio_ln_dbg(ln, "Failed to issue fdmi rpa req\n");
385 	}
386 	spin_unlock_irq(&hw->lock);
387 }
388 
389 /*
390  * csio_ln_fdmi_dprt_cbfn - DPRT completion
391  * @hw: HW context
392  * @fdmi_req: fdmi request
393  */
394 static void
395 csio_ln_fdmi_dprt_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
396 {
397 	void *cmd;
398 	uint8_t *pld;
399 	uint32_t len = 0;
400 	uint32_t numattrs = 0;
401 	__be32  maxpayload = htonl(65536);
402 	struct fc_fdmi_hba_identifier *hbaid;
403 	struct csio_lnode *ln = fdmi_req->lnode;
404 	struct fc_fdmi_rpl *reg_pl;
405 	struct fs_fdmi_attrs *attrib_blk;
406 	uint8_t buf[64];
407 
408 	if (fdmi_req->wr_status != FW_SUCCESS) {
409 		csio_ln_dbg(ln, "WR error:%x in processing fdmi dprt cmd\n",
410 			    fdmi_req->wr_status);
411 		CSIO_INC_STATS(ln, n_fdmi_err);
412 	}
413 
414 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
415 		CSIO_INC_STATS(ln, n_fdmi_err);
416 		return;
417 	}
418 	cmd = fdmi_req->dma_buf.vaddr;
419 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
420 		csio_ln_dbg(ln, "fdmi dprt cmd rejected reason %x expl %x\n",
421 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
422 	}
423 
424 	/* Prepare CT hdr for RHBA cmd */
425 	memset(cmd, 0, FC_CT_HDR_LEN);
426 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RHBA);
427 	len = FC_CT_HDR_LEN;
428 
429 	/* Prepare RHBA payload */
430 	pld = (uint8_t *)csio_ct_get_pld(cmd);
431 	hbaid = (struct fc_fdmi_hba_identifier *)pld;
432 	memcpy(&hbaid->id, csio_ln_wwpn(ln), 8); /* HBA identifer */
433 	pld += sizeof(*hbaid);
434 
435 	/* Register one port per hba */
436 	reg_pl = (struct fc_fdmi_rpl *)pld;
437 	reg_pl->numport = htonl(1);
438 	memcpy(&reg_pl->port[0].portname, csio_ln_wwpn(ln), 8);
439 	pld += sizeof(*reg_pl);
440 
441 	/* Start appending HBA attributes hba */
442 	attrib_blk = (struct fs_fdmi_attrs *)pld;
443 	attrib_blk->numattrs = 0;
444 	len += sizeof(attrib_blk->numattrs);
445 	pld += sizeof(attrib_blk->numattrs);
446 
447 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_NODENAME, csio_ln_wwnn(ln),
448 			   FC_FDMI_HBA_ATTR_NODENAME_LEN);
449 	numattrs++;
450 
451 	memset(buf, 0, sizeof(buf));
452 
453 	strcpy(buf, "Chelsio Communications");
454 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MANUFACTURER, buf,
455 			   strlen(buf));
456 	numattrs++;
457 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_SERIALNUMBER,
458 			   hw->vpd.sn, sizeof(hw->vpd.sn));
459 	numattrs++;
460 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODEL, hw->vpd.id,
461 			   sizeof(hw->vpd.id));
462 	numattrs++;
463 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODELDESCRIPTION,
464 			   hw->model_desc, strlen(hw->model_desc));
465 	numattrs++;
466 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_HARDWAREVERSION,
467 			   hw->hw_ver, sizeof(hw->hw_ver));
468 	numattrs++;
469 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_FIRMWAREVERSION,
470 			   hw->fwrev_str, strlen(hw->fwrev_str));
471 	numattrs++;
472 
473 	if (!csio_osname(buf, sizeof(buf))) {
474 		csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_OSNAMEVERSION,
475 				   buf, strlen(buf));
476 		numattrs++;
477 	}
478 
479 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD,
480 			   &maxpayload, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN);
481 	len = (uint32_t)(pld - (uint8_t *)cmd);
482 	numattrs++;
483 	attrib_blk->numattrs = htonl(numattrs);
484 
485 	/* Submit FDMI RHBA request */
486 	spin_lock_irq(&hw->lock);
487 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_rhba_cbfn,
488 				FCOE_CT, &fdmi_req->dma_buf, len)) {
489 		CSIO_INC_STATS(ln, n_fdmi_err);
490 		csio_ln_dbg(ln, "Failed to issue fdmi rhba req\n");
491 	}
492 	spin_unlock_irq(&hw->lock);
493 }
494 
495 /*
496  * csio_ln_fdmi_dhba_cbfn - DHBA completion
497  * @hw: HW context
498  * @fdmi_req: fdmi request
499  */
500 static void
501 csio_ln_fdmi_dhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
502 {
503 	struct csio_lnode *ln = fdmi_req->lnode;
504 	void *cmd;
505 	struct fc_fdmi_port_name *port_name;
506 	uint32_t len;
507 
508 	if (fdmi_req->wr_status != FW_SUCCESS) {
509 		csio_ln_dbg(ln, "WR error:%x in processing fdmi dhba cmd\n",
510 			    fdmi_req->wr_status);
511 		CSIO_INC_STATS(ln, n_fdmi_err);
512 	}
513 
514 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
515 		CSIO_INC_STATS(ln, n_fdmi_err);
516 		return;
517 	}
518 	cmd = fdmi_req->dma_buf.vaddr;
519 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
520 		csio_ln_dbg(ln, "fdmi dhba cmd rejected reason %x expl %x\n",
521 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
522 	}
523 
524 	/* Send FDMI cmd to de-register any Port attributes if registered
525 	 * before
526 	 */
527 
528 	/* Prepare FDMI DPRT cmd */
529 	memset(cmd, 0, FC_CT_HDR_LEN);
530 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DPRT);
531 	len = FC_CT_HDR_LEN;
532 	port_name = (struct fc_fdmi_port_name *)csio_ct_get_pld(cmd);
533 	memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
534 	len += sizeof(*port_name);
535 
536 	/* Submit FDMI request */
537 	spin_lock_irq(&hw->lock);
538 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dprt_cbfn,
539 				FCOE_CT, &fdmi_req->dma_buf, len)) {
540 		CSIO_INC_STATS(ln, n_fdmi_err);
541 		csio_ln_dbg(ln, "Failed to issue fdmi dprt req\n");
542 	}
543 	spin_unlock_irq(&hw->lock);
544 }
545 
546 /**
547  * csio_ln_fdmi_start - Start an FDMI request.
548  * @ln:		lnode
549  * @context:	session context
550  *
551  * Issued with lock held.
552  */
553 int
554 csio_ln_fdmi_start(struct csio_lnode *ln, void *context)
555 {
556 	struct csio_ioreq *fdmi_req;
557 	struct csio_rnode *fdmi_rn = (struct csio_rnode *)context;
558 	void *cmd;
559 	struct fc_fdmi_hba_identifier *hbaid;
560 	uint32_t len;
561 
562 	if (!(ln->flags & CSIO_LNF_FDMI_ENABLE))
563 		return -EPROTONOSUPPORT;
564 
565 	if (!csio_is_rnode_ready(fdmi_rn))
566 		CSIO_INC_STATS(ln, n_fdmi_err);
567 
568 	/* Send FDMI cmd to de-register any HBA attributes if registered
569 	 * before
570 	 */
571 
572 	fdmi_req = ln->mgmt_req;
573 	fdmi_req->lnode = ln;
574 	fdmi_req->rnode = fdmi_rn;
575 
576 	/* Prepare FDMI DHBA cmd */
577 	cmd = fdmi_req->dma_buf.vaddr;
578 	memset(cmd, 0, FC_CT_HDR_LEN);
579 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DHBA);
580 	len = FC_CT_HDR_LEN;
581 
582 	hbaid = (struct fc_fdmi_hba_identifier *)csio_ct_get_pld(cmd);
583 	memcpy(&hbaid->id, csio_ln_wwpn(ln), 8);
584 	len += sizeof(*hbaid);
585 
586 	/* Submit FDMI request */
587 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dhba_cbfn,
588 					FCOE_CT, &fdmi_req->dma_buf, len)) {
589 		CSIO_INC_STATS(ln, n_fdmi_err);
590 		csio_ln_dbg(ln, "Failed to issue fdmi dhba req\n");
591 	}
592 
593 	return 0;
594 }
595 
596 /*
597  * csio_ln_vnp_read_cbfn - vnp read completion handler.
598  * @hw: HW lnode
599  * @cbfn: Completion handler.
600  *
601  * Reads vnp response and updates ln parameters.
602  */
603 static void
604 csio_ln_vnp_read_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
605 {
606 	struct csio_lnode *ln = ((struct csio_lnode *)mbp->priv);
607 	struct fw_fcoe_vnp_cmd *rsp = (struct fw_fcoe_vnp_cmd *)(mbp->mb);
608 	struct fc_els_csp *csp;
609 	struct fc_els_cssp *clsp;
610 	enum fw_retval retval;
611 	__be32 nport_id;
612 
613 	retval = FW_CMD_RETVAL_G(ntohl(rsp->alloc_to_len16));
614 	if (retval != FW_SUCCESS) {
615 		csio_err(hw, "FCOE VNP read cmd returned error:0x%x\n", retval);
616 		mempool_free(mbp, hw->mb_mempool);
617 		return;
618 	}
619 
620 	spin_lock_irq(&hw->lock);
621 
622 	memcpy(ln->mac, rsp->vnport_mac, sizeof(ln->mac));
623 	memcpy(&nport_id, &rsp->vnport_mac[3], sizeof(uint8_t)*3);
624 	ln->nport_id = ntohl(nport_id);
625 	ln->nport_id = ln->nport_id >> 8;
626 
627 	/* Update WWNs */
628 	/*
629 	 * This may look like a duplication of what csio_fcoe_enable_link()
630 	 * does, but is absolutely necessary if the vnpi changes between
631 	 * a FCOE LINK UP and FCOE LINK DOWN.
632 	 */
633 	memcpy(csio_ln_wwnn(ln), rsp->vnport_wwnn, 8);
634 	memcpy(csio_ln_wwpn(ln), rsp->vnport_wwpn, 8);
635 
636 	/* Copy common sparam */
637 	csp = (struct fc_els_csp *)rsp->cmn_srv_parms;
638 	ln->ln_sparm.csp.sp_hi_ver = csp->sp_hi_ver;
639 	ln->ln_sparm.csp.sp_lo_ver = csp->sp_lo_ver;
640 	ln->ln_sparm.csp.sp_bb_cred = csp->sp_bb_cred;
641 	ln->ln_sparm.csp.sp_features = csp->sp_features;
642 	ln->ln_sparm.csp.sp_bb_data = csp->sp_bb_data;
643 	ln->ln_sparm.csp.sp_r_a_tov = csp->sp_r_a_tov;
644 	ln->ln_sparm.csp.sp_e_d_tov = csp->sp_e_d_tov;
645 
646 	/* Copy word 0 & word 1 of class sparam */
647 	clsp = (struct fc_els_cssp *)rsp->clsp_word_0_1;
648 	ln->ln_sparm.clsp[2].cp_class = clsp->cp_class;
649 	ln->ln_sparm.clsp[2].cp_init = clsp->cp_init;
650 	ln->ln_sparm.clsp[2].cp_recip = clsp->cp_recip;
651 	ln->ln_sparm.clsp[2].cp_rdfs = clsp->cp_rdfs;
652 
653 	spin_unlock_irq(&hw->lock);
654 
655 	mempool_free(mbp, hw->mb_mempool);
656 
657 	/* Send an event to update local attribs */
658 	csio_lnode_async_event(ln, CSIO_LN_FC_ATTRIB_UPDATE);
659 }
660 
661 /*
662  * csio_ln_vnp_read - Read vnp params.
663  * @ln: lnode
664  * @cbfn: Completion handler.
665  *
666  * Issued with lock held.
667  */
668 static int
669 csio_ln_vnp_read(struct csio_lnode *ln,
670 		void (*cbfn) (struct csio_hw *, struct csio_mb *))
671 {
672 	struct csio_hw *hw = ln->hwp;
673 	struct csio_mb  *mbp;
674 
675 	/* Allocate Mbox request */
676 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
677 	if (!mbp) {
678 		CSIO_INC_STATS(hw, n_err_nomem);
679 		return -ENOMEM;
680 	}
681 
682 	/* Prepare VNP Command */
683 	csio_fcoe_vnp_read_init_mb(ln, mbp,
684 				    CSIO_MB_DEFAULT_TMO,
685 				    ln->fcf_flowid,
686 				    ln->vnp_flowid,
687 				    cbfn);
688 
689 	/* Issue MBOX cmd */
690 	if (csio_mb_issue(hw, mbp)) {
691 		csio_err(hw, "Failed to issue mbox FCoE VNP command\n");
692 		mempool_free(mbp, hw->mb_mempool);
693 		return -EINVAL;
694 	}
695 
696 	return 0;
697 }
698 
699 /*
700  * csio_fcoe_enable_link - Enable fcoe link.
701  * @ln: lnode
702  * @enable: enable/disable
703  * Issued with lock held.
704  * Issues mbox cmd to bring up FCOE link on port associated with given ln.
705  */
706 static int
707 csio_fcoe_enable_link(struct csio_lnode *ln, bool enable)
708 {
709 	struct csio_hw *hw = ln->hwp;
710 	struct csio_mb  *mbp;
711 	enum fw_retval retval;
712 	uint8_t portid;
713 	uint8_t sub_op;
714 	struct fw_fcoe_link_cmd *lcmd;
715 	int i;
716 
717 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
718 	if (!mbp) {
719 		CSIO_INC_STATS(hw, n_err_nomem);
720 		return -ENOMEM;
721 	}
722 
723 	portid = ln->portid;
724 	sub_op = enable ? FCOE_LINK_UP : FCOE_LINK_DOWN;
725 
726 	csio_dbg(hw, "bringing FCOE LINK %s on Port:%d\n",
727 		 sub_op ? "UP" : "DOWN", portid);
728 
729 	csio_write_fcoe_link_cond_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
730 					  portid, sub_op, 0, 0, 0, NULL);
731 
732 	if (csio_mb_issue(hw, mbp)) {
733 		csio_err(hw, "failed to issue FCOE LINK cmd on port[%d]\n",
734 			portid);
735 		mempool_free(mbp, hw->mb_mempool);
736 		return -EINVAL;
737 	}
738 
739 	retval = csio_mb_fw_retval(mbp);
740 	if (retval != FW_SUCCESS) {
741 		csio_err(hw,
742 			 "FCOE LINK %s cmd on port[%d] failed with "
743 			 "ret:x%x\n", sub_op ? "UP" : "DOWN", portid, retval);
744 		mempool_free(mbp, hw->mb_mempool);
745 		return -EINVAL;
746 	}
747 
748 	if (!enable)
749 		goto out;
750 
751 	lcmd = (struct fw_fcoe_link_cmd *)mbp->mb;
752 
753 	memcpy(csio_ln_wwnn(ln), lcmd->vnport_wwnn, 8);
754 	memcpy(csio_ln_wwpn(ln), lcmd->vnport_wwpn, 8);
755 
756 	for (i = 0; i < CSIO_MAX_PPORTS; i++)
757 		if (hw->pport[i].portid == portid)
758 			memcpy(hw->pport[i].mac, lcmd->phy_mac, 6);
759 
760 out:
761 	mempool_free(mbp, hw->mb_mempool);
762 	return 0;
763 }
764 
765 /*
766  * csio_ln_read_fcf_cbfn - Read fcf parameters
767  * @ln: lnode
768  *
769  * read fcf response and Update ln fcf information.
770  */
771 static void
772 csio_ln_read_fcf_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
773 {
774 	struct csio_lnode *ln = (struct csio_lnode *)mbp->priv;
775 	struct csio_fcf_info	*fcf_info;
776 	struct fw_fcoe_fcf_cmd *rsp =
777 				(struct fw_fcoe_fcf_cmd *)(mbp->mb);
778 	enum fw_retval retval;
779 
780 	retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
781 	if (retval != FW_SUCCESS) {
782 		csio_ln_err(ln, "FCOE FCF cmd failed with ret x%x\n",
783 				retval);
784 		mempool_free(mbp, hw->mb_mempool);
785 		return;
786 	}
787 
788 	spin_lock_irq(&hw->lock);
789 	fcf_info = ln->fcfinfo;
790 	fcf_info->priority = FW_FCOE_FCF_CMD_PRIORITY_GET(
791 					ntohs(rsp->priority_pkd));
792 	fcf_info->vf_id = ntohs(rsp->vf_id);
793 	fcf_info->vlan_id = rsp->vlan_id;
794 	fcf_info->max_fcoe_size = ntohs(rsp->max_fcoe_size);
795 	fcf_info->fka_adv = be32_to_cpu(rsp->fka_adv);
796 	fcf_info->fcfi = FW_FCOE_FCF_CMD_FCFI_GET(ntohl(rsp->op_to_fcfi));
797 	fcf_info->fpma = FW_FCOE_FCF_CMD_FPMA_GET(rsp->fpma_to_portid);
798 	fcf_info->spma = FW_FCOE_FCF_CMD_SPMA_GET(rsp->fpma_to_portid);
799 	fcf_info->login = FW_FCOE_FCF_CMD_LOGIN_GET(rsp->fpma_to_portid);
800 	fcf_info->portid = FW_FCOE_FCF_CMD_PORTID_GET(rsp->fpma_to_portid);
801 	memcpy(fcf_info->fc_map, rsp->fc_map, sizeof(fcf_info->fc_map));
802 	memcpy(fcf_info->mac, rsp->mac, sizeof(fcf_info->mac));
803 	memcpy(fcf_info->name_id, rsp->name_id, sizeof(fcf_info->name_id));
804 	memcpy(fcf_info->fabric, rsp->fabric, sizeof(fcf_info->fabric));
805 	memcpy(fcf_info->spma_mac, rsp->spma_mac, sizeof(fcf_info->spma_mac));
806 
807 	spin_unlock_irq(&hw->lock);
808 
809 	mempool_free(mbp, hw->mb_mempool);
810 }
811 
812 /*
813  * csio_ln_read_fcf_entry - Read fcf entry.
814  * @ln: lnode
815  * @cbfn: Completion handler.
816  *
817  * Issued with lock held.
818  */
819 static int
820 csio_ln_read_fcf_entry(struct csio_lnode *ln,
821 			void (*cbfn) (struct csio_hw *, struct csio_mb *))
822 {
823 	struct csio_hw *hw = ln->hwp;
824 	struct csio_mb  *mbp;
825 
826 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
827 	if (!mbp) {
828 		CSIO_INC_STATS(hw, n_err_nomem);
829 		return -ENOMEM;
830 	}
831 
832 	/* Get FCoE FCF information */
833 	csio_fcoe_read_fcf_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
834 				      ln->portid, ln->fcf_flowid, cbfn);
835 
836 	if (csio_mb_issue(hw, mbp)) {
837 		csio_err(hw, "failed to issue FCOE FCF cmd\n");
838 		mempool_free(mbp, hw->mb_mempool);
839 		return -EINVAL;
840 	}
841 
842 	return 0;
843 }
844 
845 /*
846  * csio_handle_link_up - Logical Linkup event.
847  * @hw - HW module.
848  * @portid - Physical port number
849  * @fcfi - FCF index.
850  * @vnpi - VNP index.
851  * Returns - none.
852  *
853  * This event is received from FW, when virtual link is established between
854  * Physical port[ENode] and FCF. If its new vnpi, then local node object is
855  * created on this FCF and set to [ONLINE] state.
856  * Lnode waits for FW_RDEV_CMD event to be received indicating that
857  * Fabric login is completed and lnode moves to [READY] state.
858  *
859  * This called with hw lock held
860  */
861 static void
862 csio_handle_link_up(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
863 		    uint32_t vnpi)
864 {
865 	struct csio_lnode *ln = NULL;
866 
867 	/* Lookup lnode based on vnpi */
868 	ln = csio_ln_lookup_by_vnpi(hw, vnpi);
869 	if (!ln) {
870 		/* Pick lnode based on portid */
871 		ln = csio_ln_lookup_by_portid(hw, portid);
872 		if (!ln) {
873 			csio_err(hw, "failed to lookup fcoe lnode on port:%d\n",
874 				portid);
875 			CSIO_DB_ASSERT(0);
876 			return;
877 		}
878 
879 		/* Check if lnode has valid vnp flowid */
880 		if (ln->vnp_flowid != CSIO_INVALID_IDX) {
881 			/* New VN-Port */
882 			spin_unlock_irq(&hw->lock);
883 			csio_lnode_alloc(hw);
884 			spin_lock_irq(&hw->lock);
885 			if (!ln) {
886 				csio_err(hw,
887 					 "failed to allocate fcoe lnode"
888 					 "for port:%d vnpi:x%x\n",
889 					 portid, vnpi);
890 				CSIO_DB_ASSERT(0);
891 				return;
892 			}
893 			ln->portid = portid;
894 		}
895 		ln->vnp_flowid = vnpi;
896 		ln->dev_num &= ~0xFFFF;
897 		ln->dev_num |= vnpi;
898 	}
899 
900 	/*Initialize fcfi */
901 	ln->fcf_flowid = fcfi;
902 
903 	csio_info(hw, "Port:%d - FCOE LINK UP\n", portid);
904 
905 	CSIO_INC_STATS(ln, n_link_up);
906 
907 	/* Send LINKUP event to SM */
908 	csio_post_event(&ln->sm, CSIO_LNE_LINKUP);
909 }
910 
911 /*
912  * csio_post_event_rns
913  * @ln - FCOE lnode
914  * @evt - Given rnode event
915  * Returns - none
916  *
917  * Posts given rnode event to all FCOE rnodes connected with given Lnode.
918  * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
919  * event.
920  *
921  * This called with hw lock held
922  */
923 static void
924 csio_post_event_rns(struct csio_lnode *ln, enum csio_rn_ev evt)
925 {
926 	struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
927 	struct list_head *tmp, *next;
928 	struct csio_rnode *rn;
929 
930 	list_for_each_safe(tmp, next, &rnhead->sm.sm_list) {
931 		rn = (struct csio_rnode *) tmp;
932 		csio_post_event(&rn->sm, evt);
933 	}
934 }
935 
936 /*
937  * csio_cleanup_rns
938  * @ln - FCOE lnode
939  * Returns - none
940  *
941  * Frees all FCOE rnodes connected with given Lnode.
942  *
943  * This called with hw lock held
944  */
945 static void
946 csio_cleanup_rns(struct csio_lnode *ln)
947 {
948 	struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
949 	struct list_head *tmp, *next_rn;
950 	struct csio_rnode *rn;
951 
952 	list_for_each_safe(tmp, next_rn, &rnhead->sm.sm_list) {
953 		rn = (struct csio_rnode *) tmp;
954 		csio_put_rnode(ln, rn);
955 	}
956 
957 }
958 
959 /*
960  * csio_post_event_lns
961  * @ln - FCOE lnode
962  * @evt - Given lnode event
963  * Returns - none
964  *
965  * Posts given lnode event to all FCOE lnodes connected with given Lnode.
966  * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
967  * event.
968  *
969  * This called with hw lock held
970  */
971 static void
972 csio_post_event_lns(struct csio_lnode *ln, enum csio_ln_ev evt)
973 {
974 	struct list_head *tmp;
975 	struct csio_lnode *cln, *sln;
976 
977 	/* If NPIV lnode, send evt only to that and return */
978 	if (csio_is_npiv_ln(ln)) {
979 		csio_post_event(&ln->sm, evt);
980 		return;
981 	}
982 
983 	sln = ln;
984 	/* Traverse children lnodes list and send evt */
985 	list_for_each(tmp, &sln->cln_head) {
986 		cln = (struct csio_lnode *) tmp;
987 		csio_post_event(&cln->sm, evt);
988 	}
989 
990 	/* Send evt to parent lnode */
991 	csio_post_event(&ln->sm, evt);
992 }
993 
994 /*
995  * csio_ln_down - Lcoal nport is down
996  * @ln - FCOE Lnode
997  * Returns - none
998  *
999  * Sends LINK_DOWN events to Lnode and its associated NPIVs lnodes.
1000  *
1001  * This called with hw lock held
1002  */
1003 static void
1004 csio_ln_down(struct csio_lnode *ln)
1005 {
1006 	csio_post_event_lns(ln, CSIO_LNE_LINK_DOWN);
1007 }
1008 
1009 /*
1010  * csio_handle_link_down - Logical Linkdown event.
1011  * @hw - HW module.
1012  * @portid - Physical port number
1013  * @fcfi - FCF index.
1014  * @vnpi - VNP index.
1015  * Returns - none
1016  *
1017  * This event is received from FW, when virtual link goes down between
1018  * Physical port[ENode] and FCF. Lnode and its associated NPIVs lnode hosted on
1019  * this vnpi[VN-Port] will be de-instantiated.
1020  *
1021  * This called with hw lock held
1022  */
1023 static void
1024 csio_handle_link_down(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
1025 		      uint32_t vnpi)
1026 {
1027 	struct csio_fcf_info *fp;
1028 	struct csio_lnode *ln;
1029 
1030 	/* Lookup lnode based on vnpi */
1031 	ln = csio_ln_lookup_by_vnpi(hw, vnpi);
1032 	if (ln) {
1033 		fp = ln->fcfinfo;
1034 		CSIO_INC_STATS(ln, n_link_down);
1035 
1036 		/*Warn if linkdown received if lnode is not in ready state */
1037 		if (!csio_is_lnode_ready(ln)) {
1038 			csio_ln_warn(ln,
1039 				"warn: FCOE link is already in offline "
1040 				"Ignoring Fcoe linkdown event on portid %d\n",
1041 				 portid);
1042 			CSIO_INC_STATS(ln, n_evt_drop);
1043 			return;
1044 		}
1045 
1046 		/* Verify portid */
1047 		if (fp->portid != portid) {
1048 			csio_ln_warn(ln,
1049 				"warn: FCOE linkdown recv with "
1050 				"invalid port %d\n", portid);
1051 			CSIO_INC_STATS(ln, n_evt_drop);
1052 			return;
1053 		}
1054 
1055 		/* verify fcfi */
1056 		if (ln->fcf_flowid != fcfi) {
1057 			csio_ln_warn(ln,
1058 				"warn: FCOE linkdown recv with "
1059 				"invalid fcfi x%x\n", fcfi);
1060 			CSIO_INC_STATS(ln, n_evt_drop);
1061 			return;
1062 		}
1063 
1064 		csio_info(hw, "Port:%d - FCOE LINK DOWN\n", portid);
1065 
1066 		/* Send LINK_DOWN event to lnode s/m */
1067 		csio_ln_down(ln);
1068 
1069 		return;
1070 	} else {
1071 		csio_warn(hw,
1072 			  "warn: FCOE linkdown recv with invalid vnpi x%x\n",
1073 			  vnpi);
1074 		CSIO_INC_STATS(hw, n_evt_drop);
1075 	}
1076 }
1077 
1078 /*
1079  * csio_is_lnode_ready - Checks FCOE lnode is in ready state.
1080  * @ln: Lnode module
1081  *
1082  * Returns True if FCOE lnode is in ready state.
1083  */
1084 int
1085 csio_is_lnode_ready(struct csio_lnode *ln)
1086 {
1087 	return (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_ready));
1088 }
1089 
1090 /*****************************************************************************/
1091 /* START: Lnode SM                                                           */
1092 /*****************************************************************************/
1093 /*
1094  * csio_lns_uninit - The request in uninit state.
1095  * @ln - FCOE lnode.
1096  * @evt - Event to be processed.
1097  *
1098  * Process the given lnode event which is currently in "uninit" state.
1099  * Invoked with HW lock held.
1100  * Return - none.
1101  */
1102 static void
1103 csio_lns_uninit(struct csio_lnode *ln, enum csio_ln_ev evt)
1104 {
1105 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1106 	struct csio_lnode *rln = hw->rln;
1107 	int rv;
1108 
1109 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1110 	switch (evt) {
1111 	case CSIO_LNE_LINKUP:
1112 		csio_set_state(&ln->sm, csio_lns_online);
1113 		/* Read FCF only for physical lnode */
1114 		if (csio_is_phys_ln(ln)) {
1115 			rv = csio_ln_read_fcf_entry(ln,
1116 					csio_ln_read_fcf_cbfn);
1117 			if (rv != 0) {
1118 				/* TODO: Send HW RESET event */
1119 				CSIO_INC_STATS(ln, n_err);
1120 				break;
1121 			}
1122 
1123 			/* Add FCF record */
1124 			list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
1125 		}
1126 
1127 		rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
1128 		if (rv != 0) {
1129 			/* TODO: Send HW RESET event */
1130 			CSIO_INC_STATS(ln, n_err);
1131 		}
1132 		break;
1133 
1134 	case CSIO_LNE_DOWN_LINK:
1135 		break;
1136 
1137 	default:
1138 		csio_ln_dbg(ln,
1139 			    "unexp ln event %d recv from did:x%x in "
1140 			    "ln state[uninit].\n", evt, ln->nport_id);
1141 		CSIO_INC_STATS(ln, n_evt_unexp);
1142 		break;
1143 	} /* switch event */
1144 }
1145 
1146 /*
1147  * csio_lns_online - The request in online state.
1148  * @ln - FCOE lnode.
1149  * @evt - Event to be processed.
1150  *
1151  * Process the given lnode event which is currently in "online" state.
1152  * Invoked with HW lock held.
1153  * Return - none.
1154  */
1155 static void
1156 csio_lns_online(struct csio_lnode *ln, enum csio_ln_ev evt)
1157 {
1158 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1159 
1160 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1161 	switch (evt) {
1162 	case CSIO_LNE_LINKUP:
1163 		csio_ln_warn(ln,
1164 			     "warn: FCOE link is up already "
1165 			     "Ignoring linkup on port:%d\n", ln->portid);
1166 		CSIO_INC_STATS(ln, n_evt_drop);
1167 		break;
1168 
1169 	case CSIO_LNE_FAB_INIT_DONE:
1170 		csio_set_state(&ln->sm, csio_lns_ready);
1171 
1172 		spin_unlock_irq(&hw->lock);
1173 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKUP);
1174 		spin_lock_irq(&hw->lock);
1175 
1176 		break;
1177 
1178 	case CSIO_LNE_LINK_DOWN:
1179 		/* Fall through */
1180 	case CSIO_LNE_DOWN_LINK:
1181 		csio_set_state(&ln->sm, csio_lns_uninit);
1182 		if (csio_is_phys_ln(ln)) {
1183 			/* Remove FCF entry */
1184 			list_del_init(&ln->fcfinfo->list);
1185 		}
1186 		break;
1187 
1188 	default:
1189 		csio_ln_dbg(ln,
1190 			    "unexp ln event %d recv from did:x%x in "
1191 			    "ln state[uninit].\n", evt, ln->nport_id);
1192 		CSIO_INC_STATS(ln, n_evt_unexp);
1193 
1194 		break;
1195 	} /* switch event */
1196 }
1197 
1198 /*
1199  * csio_lns_ready - The request in ready state.
1200  * @ln - FCOE lnode.
1201  * @evt - Event to be processed.
1202  *
1203  * Process the given lnode event which is currently in "ready" state.
1204  * Invoked with HW lock held.
1205  * Return - none.
1206  */
1207 static void
1208 csio_lns_ready(struct csio_lnode *ln, enum csio_ln_ev evt)
1209 {
1210 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1211 
1212 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1213 	switch (evt) {
1214 	case CSIO_LNE_FAB_INIT_DONE:
1215 		csio_ln_dbg(ln,
1216 			    "ignoring event %d recv from did x%x"
1217 			    "in ln state[ready].\n", evt, ln->nport_id);
1218 		CSIO_INC_STATS(ln, n_evt_drop);
1219 		break;
1220 
1221 	case CSIO_LNE_LINK_DOWN:
1222 		csio_set_state(&ln->sm, csio_lns_offline);
1223 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1224 
1225 		spin_unlock_irq(&hw->lock);
1226 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
1227 		spin_lock_irq(&hw->lock);
1228 
1229 		if (csio_is_phys_ln(ln)) {
1230 			/* Remove FCF entry */
1231 			list_del_init(&ln->fcfinfo->list);
1232 		}
1233 		break;
1234 
1235 	case CSIO_LNE_DOWN_LINK:
1236 		csio_set_state(&ln->sm, csio_lns_offline);
1237 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1238 
1239 		/* Host need to issue aborts in case if FW has not returned
1240 		 * WRs with status "ABORTED"
1241 		 */
1242 		spin_unlock_irq(&hw->lock);
1243 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
1244 		spin_lock_irq(&hw->lock);
1245 
1246 		if (csio_is_phys_ln(ln)) {
1247 			/* Remove FCF entry */
1248 			list_del_init(&ln->fcfinfo->list);
1249 		}
1250 		break;
1251 
1252 	case CSIO_LNE_CLOSE:
1253 		csio_set_state(&ln->sm, csio_lns_uninit);
1254 		csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
1255 		break;
1256 
1257 	case CSIO_LNE_LOGO:
1258 		csio_set_state(&ln->sm, csio_lns_offline);
1259 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1260 		break;
1261 
1262 	default:
1263 		csio_ln_dbg(ln,
1264 			    "unexp ln event %d recv from did:x%x in "
1265 			    "ln state[uninit].\n", evt, ln->nport_id);
1266 		CSIO_INC_STATS(ln, n_evt_unexp);
1267 		CSIO_DB_ASSERT(0);
1268 		break;
1269 	} /* switch event */
1270 }
1271 
1272 /*
1273  * csio_lns_offline - The request in offline state.
1274  * @ln - FCOE lnode.
1275  * @evt - Event to be processed.
1276  *
1277  * Process the given lnode event which is currently in "offline" state.
1278  * Invoked with HW lock held.
1279  * Return - none.
1280  */
1281 static void
1282 csio_lns_offline(struct csio_lnode *ln, enum csio_ln_ev evt)
1283 {
1284 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1285 	struct csio_lnode *rln = hw->rln;
1286 	int rv;
1287 
1288 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1289 	switch (evt) {
1290 	case CSIO_LNE_LINKUP:
1291 		csio_set_state(&ln->sm, csio_lns_online);
1292 		/* Read FCF only for physical lnode */
1293 		if (csio_is_phys_ln(ln)) {
1294 			rv = csio_ln_read_fcf_entry(ln,
1295 					csio_ln_read_fcf_cbfn);
1296 			if (rv != 0) {
1297 				/* TODO: Send HW RESET event */
1298 				CSIO_INC_STATS(ln, n_err);
1299 				break;
1300 			}
1301 
1302 			/* Add FCF record */
1303 			list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
1304 		}
1305 
1306 		rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
1307 		if (rv != 0) {
1308 			/* TODO: Send HW RESET event */
1309 			CSIO_INC_STATS(ln, n_err);
1310 		}
1311 		break;
1312 
1313 	case CSIO_LNE_LINK_DOWN:
1314 	case CSIO_LNE_DOWN_LINK:
1315 	case CSIO_LNE_LOGO:
1316 		csio_ln_dbg(ln,
1317 			    "ignoring event %d recv from did x%x"
1318 			    "in ln state[offline].\n", evt, ln->nport_id);
1319 		CSIO_INC_STATS(ln, n_evt_drop);
1320 		break;
1321 
1322 	case CSIO_LNE_CLOSE:
1323 		csio_set_state(&ln->sm, csio_lns_uninit);
1324 		csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
1325 		break;
1326 
1327 	default:
1328 		csio_ln_dbg(ln,
1329 			    "unexp ln event %d recv from did:x%x in "
1330 			    "ln state[offline]\n", evt, ln->nport_id);
1331 		CSIO_INC_STATS(ln, n_evt_unexp);
1332 		CSIO_DB_ASSERT(0);
1333 		break;
1334 	} /* switch event */
1335 }
1336 
1337 /*****************************************************************************/
1338 /* END: Lnode SM                                                             */
1339 /*****************************************************************************/
1340 
1341 static void
1342 csio_free_fcfinfo(struct kref *kref)
1343 {
1344 	struct csio_fcf_info *fcfinfo = container_of(kref,
1345 						struct csio_fcf_info, kref);
1346 	kfree(fcfinfo);
1347 }
1348 
1349 /* Helper routines for attributes  */
1350 /*
1351  * csio_lnode_state_to_str - Get current state of FCOE lnode.
1352  * @ln - lnode
1353  * @str - state of lnode.
1354  *
1355  */
1356 void
1357 csio_lnode_state_to_str(struct csio_lnode *ln, int8_t *str)
1358 {
1359 	if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_uninit)) {
1360 		strcpy(str, "UNINIT");
1361 		return;
1362 	}
1363 	if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_ready)) {
1364 		strcpy(str, "READY");
1365 		return;
1366 	}
1367 	if (csio_get_state(ln) == ((csio_sm_state_t)csio_lns_offline)) {
1368 		strcpy(str, "OFFLINE");
1369 		return;
1370 	}
1371 	strcpy(str, "UNKNOWN");
1372 } /* csio_lnode_state_to_str */
1373 
1374 
1375 int
1376 csio_get_phy_port_stats(struct csio_hw *hw, uint8_t portid,
1377 			struct fw_fcoe_port_stats *port_stats)
1378 {
1379 	struct csio_mb  *mbp;
1380 	struct fw_fcoe_port_cmd_params portparams;
1381 	enum fw_retval retval;
1382 	int idx;
1383 
1384 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1385 	if (!mbp) {
1386 		csio_err(hw, "FCoE FCF PARAMS command out of memory!\n");
1387 		return -EINVAL;
1388 	}
1389 	portparams.portid = portid;
1390 
1391 	for (idx = 1; idx <= 3; idx++) {
1392 		portparams.idx = (idx-1)*6 + 1;
1393 		portparams.nstats = 6;
1394 		if (idx == 3)
1395 			portparams.nstats = 4;
1396 		csio_fcoe_read_portparams_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO,
1397 							&portparams, NULL);
1398 		if (csio_mb_issue(hw, mbp)) {
1399 			csio_err(hw, "Issue of FCoE port params failed!\n");
1400 			mempool_free(mbp, hw->mb_mempool);
1401 			return -EINVAL;
1402 		}
1403 		csio_mb_process_portparams_rsp(hw, mbp, &retval,
1404 						&portparams, port_stats);
1405 	}
1406 
1407 	mempool_free(mbp, hw->mb_mempool);
1408 	return 0;
1409 }
1410 
1411 /*
1412  * csio_ln_mgmt_wr_handler -Mgmt Work Request handler.
1413  * @wr - WR.
1414  * @len - WR len.
1415  * This handler is invoked when an outstanding mgmt WR is completed.
1416  * Its invoked in the context of FW event worker thread for every
1417  * mgmt event received.
1418  * Return - none.
1419  */
1420 
1421 static void
1422 csio_ln_mgmt_wr_handler(struct csio_hw *hw, void *wr, uint32_t len)
1423 {
1424 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
1425 	struct csio_ioreq *io_req = NULL;
1426 	struct fw_fcoe_els_ct_wr *wr_cmd;
1427 
1428 
1429 	wr_cmd = (struct fw_fcoe_els_ct_wr *) wr;
1430 
1431 	if (len < sizeof(struct fw_fcoe_els_ct_wr)) {
1432 		csio_err(mgmtm->hw,
1433 			 "Invalid ELS CT WR length recvd, len:%x\n", len);
1434 		mgmtm->stats.n_err++;
1435 		return;
1436 	}
1437 
1438 	io_req = (struct csio_ioreq *) ((uintptr_t) wr_cmd->cookie);
1439 	io_req->wr_status = csio_wr_status(wr_cmd);
1440 
1441 	/* lookup ioreq exists in our active Q */
1442 	spin_lock_irq(&hw->lock);
1443 	if (csio_mgmt_req_lookup(mgmtm, io_req) != 0) {
1444 		csio_err(mgmtm->hw,
1445 			"Error- Invalid IO handle recv in WR. handle: %p\n",
1446 			io_req);
1447 		mgmtm->stats.n_err++;
1448 		spin_unlock_irq(&hw->lock);
1449 		return;
1450 	}
1451 
1452 	mgmtm = csio_hw_to_mgmtm(hw);
1453 
1454 	/* Dequeue from active queue */
1455 	list_del_init(&io_req->sm.sm_list);
1456 	mgmtm->stats.n_active--;
1457 	spin_unlock_irq(&hw->lock);
1458 
1459 	/* io_req will be freed by completion handler */
1460 	if (io_req->io_cbfn)
1461 		io_req->io_cbfn(hw, io_req);
1462 }
1463 
1464 /**
1465  * csio_fcoe_fwevt_handler - Event handler for Firmware FCoE events.
1466  * @hw:		HW module
1467  * @cpl_op:	CPL opcode
1468  * @cmd:	FW cmd/WR.
1469  *
1470  * Process received FCoE cmd/WR event from FW.
1471  */
1472 void
1473 csio_fcoe_fwevt_handler(struct csio_hw *hw, __u8 cpl_op, __be64 *cmd)
1474 {
1475 	struct csio_lnode *ln;
1476 	struct csio_rnode *rn;
1477 	uint8_t portid, opcode = *(uint8_t *)cmd;
1478 	struct fw_fcoe_link_cmd *lcmd;
1479 	struct fw_wr_hdr *wr;
1480 	struct fw_rdev_wr *rdev_wr;
1481 	enum fw_fcoe_link_status lstatus;
1482 	uint32_t fcfi, rdev_flowid, vnpi;
1483 	enum csio_ln_ev evt;
1484 
1485 	if (cpl_op == CPL_FW6_MSG && opcode == FW_FCOE_LINK_CMD) {
1486 
1487 		lcmd = (struct fw_fcoe_link_cmd *)cmd;
1488 		lstatus = lcmd->lstatus;
1489 		portid = FW_FCOE_LINK_CMD_PORTID_GET(
1490 					ntohl(lcmd->op_to_portid));
1491 		fcfi = FW_FCOE_LINK_CMD_FCFI_GET(ntohl(lcmd->sub_opcode_fcfi));
1492 		vnpi = FW_FCOE_LINK_CMD_VNPI_GET(ntohl(lcmd->vnpi_pkd));
1493 
1494 		if (lstatus == FCOE_LINKUP) {
1495 
1496 			/* HW lock here */
1497 			spin_lock_irq(&hw->lock);
1498 			csio_handle_link_up(hw, portid, fcfi, vnpi);
1499 			spin_unlock_irq(&hw->lock);
1500 			/* HW un lock here */
1501 
1502 		} else if (lstatus == FCOE_LINKDOWN) {
1503 
1504 			/* HW lock here */
1505 			spin_lock_irq(&hw->lock);
1506 			csio_handle_link_down(hw, portid, fcfi, vnpi);
1507 			spin_unlock_irq(&hw->lock);
1508 			/* HW un lock here */
1509 		} else {
1510 			csio_warn(hw, "Unexpected FCOE LINK status:0x%x\n",
1511 				  lcmd->lstatus);
1512 			CSIO_INC_STATS(hw, n_cpl_unexp);
1513 		}
1514 	} else if (cpl_op == CPL_FW6_PLD) {
1515 		wr = (struct fw_wr_hdr *) (cmd + 4);
1516 		if (FW_WR_OP_G(be32_to_cpu(wr->hi))
1517 			== FW_RDEV_WR) {
1518 
1519 			rdev_wr = (struct fw_rdev_wr *) (cmd + 4);
1520 
1521 			rdev_flowid = FW_RDEV_WR_FLOWID_GET(
1522 					ntohl(rdev_wr->alloc_to_len16));
1523 			vnpi = FW_RDEV_WR_ASSOC_FLOWID_GET(
1524 				    ntohl(rdev_wr->flags_to_assoc_flowid));
1525 
1526 			csio_dbg(hw,
1527 				"FW_RDEV_WR: flowid:x%x ev_cause:x%x "
1528 				"vnpi:0x%x\n", rdev_flowid,
1529 				rdev_wr->event_cause, vnpi);
1530 
1531 			if (rdev_wr->protocol != PROT_FCOE) {
1532 				csio_err(hw,
1533 					"FW_RDEV_WR: invalid proto:x%x "
1534 					"received with flowid:x%x\n",
1535 					rdev_wr->protocol,
1536 					rdev_flowid);
1537 				CSIO_INC_STATS(hw, n_evt_drop);
1538 				return;
1539 			}
1540 
1541 			/* HW lock here */
1542 			spin_lock_irq(&hw->lock);
1543 			ln = csio_ln_lookup_by_vnpi(hw, vnpi);
1544 			if (!ln) {
1545 				csio_err(hw,
1546 					"FW_DEV_WR: invalid vnpi:x%x received "
1547 					"with flowid:x%x\n", vnpi, rdev_flowid);
1548 				CSIO_INC_STATS(hw, n_evt_drop);
1549 				goto out_pld;
1550 			}
1551 
1552 			rn = csio_confirm_rnode(ln, rdev_flowid,
1553 					&rdev_wr->u.fcoe_rdev);
1554 			if (!rn) {
1555 				csio_ln_dbg(ln,
1556 					"Failed to confirm rnode "
1557 					"for flowid:x%x\n", rdev_flowid);
1558 				CSIO_INC_STATS(hw, n_evt_drop);
1559 				goto out_pld;
1560 			}
1561 
1562 			/* save previous event for debugging */
1563 			ln->prev_evt = ln->cur_evt;
1564 			ln->cur_evt = rdev_wr->event_cause;
1565 			CSIO_INC_STATS(ln, n_evt_fw[rdev_wr->event_cause]);
1566 
1567 			/* Translate all the fabric events to lnode SM events */
1568 			evt = CSIO_FWE_TO_LNE(rdev_wr->event_cause);
1569 			if (evt) {
1570 				csio_ln_dbg(ln,
1571 					"Posting event to lnode event:%d "
1572 					"cause:%d flowid:x%x\n", evt,
1573 					rdev_wr->event_cause, rdev_flowid);
1574 				csio_post_event(&ln->sm, evt);
1575 			}
1576 
1577 			/* Handover event to rn SM here. */
1578 			csio_rnode_fwevt_handler(rn, rdev_wr->event_cause);
1579 out_pld:
1580 			spin_unlock_irq(&hw->lock);
1581 			return;
1582 		} else {
1583 			csio_warn(hw, "unexpected WR op(0x%x) recv\n",
1584 				  FW_WR_OP_G(be32_to_cpu((wr->hi))));
1585 			CSIO_INC_STATS(hw, n_cpl_unexp);
1586 		}
1587 	} else if (cpl_op == CPL_FW6_MSG) {
1588 		wr = (struct fw_wr_hdr *) (cmd);
1589 		if (FW_WR_OP_G(be32_to_cpu(wr->hi)) == FW_FCOE_ELS_CT_WR) {
1590 			csio_ln_mgmt_wr_handler(hw, wr,
1591 					sizeof(struct fw_fcoe_els_ct_wr));
1592 		} else {
1593 			csio_warn(hw, "unexpected WR op(0x%x) recv\n",
1594 				  FW_WR_OP_G(be32_to_cpu((wr->hi))));
1595 			CSIO_INC_STATS(hw, n_cpl_unexp);
1596 		}
1597 	} else {
1598 		csio_warn(hw, "unexpected CPL op(0x%x) recv\n", opcode);
1599 		CSIO_INC_STATS(hw, n_cpl_unexp);
1600 	}
1601 }
1602 
1603 /**
1604  * csio_lnode_start - Kickstart lnode discovery.
1605  * @ln:		lnode
1606  *
1607  * This routine kickstarts the discovery by issuing an FCOE_LINK (up) command.
1608  */
1609 int
1610 csio_lnode_start(struct csio_lnode *ln)
1611 {
1612 	int rv = 0;
1613 	if (csio_is_phys_ln(ln) && !(ln->flags & CSIO_LNF_LINK_ENABLE)) {
1614 		rv = csio_fcoe_enable_link(ln, 1);
1615 		ln->flags |= CSIO_LNF_LINK_ENABLE;
1616 	}
1617 
1618 	return rv;
1619 }
1620 
1621 /**
1622  * csio_lnode_stop - Stop the lnode.
1623  * @ln:		lnode
1624  *
1625  * This routine is invoked by HW module to stop lnode and its associated NPIV
1626  * lnodes.
1627  */
1628 void
1629 csio_lnode_stop(struct csio_lnode *ln)
1630 {
1631 	csio_post_event_lns(ln, CSIO_LNE_DOWN_LINK);
1632 	if (csio_is_phys_ln(ln) && (ln->flags & CSIO_LNF_LINK_ENABLE)) {
1633 		csio_fcoe_enable_link(ln, 0);
1634 		ln->flags &= ~CSIO_LNF_LINK_ENABLE;
1635 	}
1636 	csio_ln_dbg(ln, "stopping ln :%p\n", ln);
1637 }
1638 
1639 /**
1640  * csio_lnode_close - Close an lnode.
1641  * @ln:		lnode
1642  *
1643  * This routine is invoked by HW module to close an lnode and its
1644  * associated NPIV lnodes. Lnode and its associated NPIV lnodes are
1645  * set to uninitialized state.
1646  */
1647 void
1648 csio_lnode_close(struct csio_lnode *ln)
1649 {
1650 	csio_post_event_lns(ln, CSIO_LNE_CLOSE);
1651 	if (csio_is_phys_ln(ln))
1652 		ln->vnp_flowid = CSIO_INVALID_IDX;
1653 
1654 	csio_ln_dbg(ln, "closed ln :%p\n", ln);
1655 }
1656 
1657 /*
1658  * csio_ln_prep_ecwr - Prepare ELS/CT WR.
1659  * @io_req - IO request.
1660  * @wr_len - WR len
1661  * @immd_len - WR immediate data
1662  * @sub_op - Sub opcode
1663  * @sid - source portid.
1664  * @did - destination portid
1665  * @flow_id - flowid
1666  * @fw_wr - ELS/CT WR to be prepared.
1667  * Returns: 0 - on success
1668  */
1669 static int
1670 csio_ln_prep_ecwr(struct csio_ioreq *io_req, uint32_t wr_len,
1671 		      uint32_t immd_len, uint8_t sub_op, uint32_t sid,
1672 		      uint32_t did, uint32_t flow_id, uint8_t *fw_wr)
1673 {
1674 	struct fw_fcoe_els_ct_wr *wr;
1675 	__be32 port_id;
1676 
1677 	wr  = (struct fw_fcoe_els_ct_wr *)fw_wr;
1678 	wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_FCOE_ELS_CT_WR) |
1679 				     FW_FCOE_ELS_CT_WR_IMMDLEN(immd_len));
1680 
1681 	wr_len =  DIV_ROUND_UP(wr_len, 16);
1682 	wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(flow_id) |
1683 				       FW_WR_LEN16_V(wr_len));
1684 	wr->els_ct_type = sub_op;
1685 	wr->ctl_pri = 0;
1686 	wr->cp_en_class = 0;
1687 	wr->cookie = io_req->fw_handle;
1688 	wr->iqid = cpu_to_be16(csio_q_physiqid(
1689 					io_req->lnode->hwp, io_req->iq_idx));
1690 	wr->fl_to_sp =  FW_FCOE_ELS_CT_WR_SP(1);
1691 	wr->tmo_val = (uint8_t) io_req->tmo;
1692 	port_id = htonl(sid);
1693 	memcpy(wr->l_id, PORT_ID_PTR(port_id), 3);
1694 	port_id = htonl(did);
1695 	memcpy(wr->r_id, PORT_ID_PTR(port_id), 3);
1696 
1697 	/* Prepare RSP SGL */
1698 	wr->rsp_dmalen = cpu_to_be32(io_req->dma_buf.len);
1699 	wr->rsp_dmaaddr = cpu_to_be64(io_req->dma_buf.paddr);
1700 	return 0;
1701 }
1702 
1703 /*
1704  * csio_ln_mgmt_submit_wr - Post elsct work request.
1705  * @mgmtm - mgmtm
1706  * @io_req - io request.
1707  * @sub_op - ELS or CT request type
1708  * @pld - Dma Payload buffer
1709  * @pld_len - Payload len
1710  * Prepares ELSCT Work request and sents it to FW.
1711  * Returns: 0 - on success
1712  */
1713 static int
1714 csio_ln_mgmt_submit_wr(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req,
1715 		uint8_t sub_op, struct csio_dma_buf *pld,
1716 		uint32_t pld_len)
1717 {
1718 	struct csio_wr_pair wrp;
1719 	struct csio_lnode *ln = io_req->lnode;
1720 	struct csio_rnode *rn = io_req->rnode;
1721 	struct	csio_hw	*hw = mgmtm->hw;
1722 	uint8_t fw_wr[64];
1723 	struct ulptx_sgl dsgl;
1724 	uint32_t wr_size = 0;
1725 	uint8_t im_len = 0;
1726 	uint32_t wr_off = 0;
1727 
1728 	int ret = 0;
1729 
1730 	/* Calculate WR Size for this ELS REQ */
1731 	wr_size = sizeof(struct fw_fcoe_els_ct_wr);
1732 
1733 	/* Send as immediate data if pld < 256 */
1734 	if (pld_len < 256) {
1735 		wr_size += ALIGN(pld_len, 8);
1736 		im_len = (uint8_t)pld_len;
1737 	} else
1738 		wr_size += sizeof(struct ulptx_sgl);
1739 
1740 	/* Roundup WR size in units of 16 bytes */
1741 	wr_size = ALIGN(wr_size, 16);
1742 
1743 	/* Get WR to send ELS REQ */
1744 	ret = csio_wr_get(hw, mgmtm->eq_idx, wr_size, &wrp);
1745 	if (ret != 0) {
1746 		csio_err(hw, "Failed to get WR for ec_req %p ret:%d\n",
1747 			io_req, ret);
1748 		return ret;
1749 	}
1750 
1751 	/* Prepare Generic WR used by all ELS/CT cmd */
1752 	csio_ln_prep_ecwr(io_req, wr_size, im_len, sub_op,
1753 				ln->nport_id, rn->nport_id,
1754 				csio_rn_flowid(rn),
1755 				&fw_wr[0]);
1756 
1757 	/* Copy ELS/CT WR CMD */
1758 	csio_wr_copy_to_wrp(&fw_wr[0], &wrp, wr_off,
1759 			sizeof(struct fw_fcoe_els_ct_wr));
1760 	wr_off += sizeof(struct fw_fcoe_els_ct_wr);
1761 
1762 	/* Copy payload to Immediate section of WR */
1763 	if (im_len)
1764 		csio_wr_copy_to_wrp(pld->vaddr, &wrp, wr_off, im_len);
1765 	else {
1766 		/* Program DSGL to dma payload */
1767 		dsgl.cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
1768 					ULPTX_MORE_F | ULPTX_NSGE_V(1));
1769 		dsgl.len0 = cpu_to_be32(pld_len);
1770 		dsgl.addr0 = cpu_to_be64(pld->paddr);
1771 		csio_wr_copy_to_wrp(&dsgl, &wrp, ALIGN(wr_off, 8),
1772 				   sizeof(struct ulptx_sgl));
1773 	}
1774 
1775 	/* Issue work request to xmit ELS/CT req to FW */
1776 	csio_wr_issue(mgmtm->hw, mgmtm->eq_idx, false);
1777 	return ret;
1778 }
1779 
1780 /*
1781  * csio_ln_mgmt_submit_req - Submit FCOE Mgmt request.
1782  * @io_req - IO Request
1783  * @io_cbfn - Completion handler.
1784  * @req_type - ELS or CT request type
1785  * @pld - Dma Payload buffer
1786  * @pld_len - Payload len
1787  *
1788  *
1789  * This API used submit managment ELS/CT request.
1790  * This called with hw lock held
1791  * Returns: 0 - on success
1792  *	    -ENOMEM	- on error.
1793  */
1794 static int
1795 csio_ln_mgmt_submit_req(struct csio_ioreq *io_req,
1796 		void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
1797 		enum fcoe_cmn_type req_type, struct csio_dma_buf *pld,
1798 		uint32_t pld_len)
1799 {
1800 	struct csio_hw *hw = csio_lnode_to_hw(io_req->lnode);
1801 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
1802 	int rv;
1803 
1804 	BUG_ON(pld_len > pld->len);
1805 
1806 	io_req->io_cbfn = io_cbfn;	/* Upper layer callback handler */
1807 	io_req->fw_handle = (uintptr_t) (io_req);
1808 	io_req->eq_idx = mgmtm->eq_idx;
1809 	io_req->iq_idx = mgmtm->iq_idx;
1810 
1811 	rv = csio_ln_mgmt_submit_wr(mgmtm, io_req, req_type, pld, pld_len);
1812 	if (rv == 0) {
1813 		list_add_tail(&io_req->sm.sm_list, &mgmtm->active_q);
1814 		mgmtm->stats.n_active++;
1815 	}
1816 	return rv;
1817 }
1818 
1819 /*
1820  * csio_ln_fdmi_init - FDMI Init entry point.
1821  * @ln: lnode
1822  */
1823 static int
1824 csio_ln_fdmi_init(struct csio_lnode *ln)
1825 {
1826 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1827 	struct csio_dma_buf	*dma_buf;
1828 
1829 	/* Allocate MGMT request required for FDMI */
1830 	ln->mgmt_req = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
1831 	if (!ln->mgmt_req) {
1832 		csio_ln_err(ln, "Failed to alloc ioreq for FDMI\n");
1833 		CSIO_INC_STATS(hw, n_err_nomem);
1834 		return -ENOMEM;
1835 	}
1836 
1837 	/* Allocate Dma buffers for FDMI response Payload */
1838 	dma_buf = &ln->mgmt_req->dma_buf;
1839 	dma_buf->len = 2048;
1840 	dma_buf->vaddr = pci_alloc_consistent(hw->pdev, dma_buf->len,
1841 						&dma_buf->paddr);
1842 	if (!dma_buf->vaddr) {
1843 		csio_err(hw, "Failed to alloc DMA buffer for FDMI!\n");
1844 		kfree(ln->mgmt_req);
1845 		ln->mgmt_req = NULL;
1846 		return -ENOMEM;
1847 	}
1848 
1849 	ln->flags |= CSIO_LNF_FDMI_ENABLE;
1850 	return 0;
1851 }
1852 
1853 /*
1854  * csio_ln_fdmi_exit - FDMI exit entry point.
1855  * @ln: lnode
1856  */
1857 static int
1858 csio_ln_fdmi_exit(struct csio_lnode *ln)
1859 {
1860 	struct csio_dma_buf *dma_buf;
1861 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1862 
1863 	if (!ln->mgmt_req)
1864 		return 0;
1865 
1866 	dma_buf = &ln->mgmt_req->dma_buf;
1867 	if (dma_buf->vaddr)
1868 		pci_free_consistent(hw->pdev, dma_buf->len, dma_buf->vaddr,
1869 				    dma_buf->paddr);
1870 
1871 	kfree(ln->mgmt_req);
1872 	return 0;
1873 }
1874 
1875 int
1876 csio_scan_done(struct csio_lnode *ln, unsigned long ticks,
1877 		unsigned long time, unsigned long max_scan_ticks,
1878 		unsigned long delta_scan_ticks)
1879 {
1880 	int rv = 0;
1881 
1882 	if (time >= max_scan_ticks)
1883 		return 1;
1884 
1885 	if (!ln->tgt_scan_tick)
1886 		ln->tgt_scan_tick = ticks;
1887 
1888 	if (((ticks - ln->tgt_scan_tick) >= delta_scan_ticks)) {
1889 		if (!ln->last_scan_ntgts)
1890 			ln->last_scan_ntgts = ln->n_scsi_tgts;
1891 		else {
1892 			if (ln->last_scan_ntgts == ln->n_scsi_tgts)
1893 				return 1;
1894 
1895 			ln->last_scan_ntgts = ln->n_scsi_tgts;
1896 		}
1897 		ln->tgt_scan_tick = ticks;
1898 	}
1899 	return rv;
1900 }
1901 
1902 /*
1903  * csio_notify_lnodes:
1904  * @hw: HW module
1905  * @note: Notification
1906  *
1907  * Called from the HW SM to fan out notifications to the
1908  * Lnode SM. Since the HW SM is entered with lock held,
1909  * there is no need to hold locks here.
1910  *
1911  */
1912 void
1913 csio_notify_lnodes(struct csio_hw *hw, enum csio_ln_notify note)
1914 {
1915 	struct list_head *tmp;
1916 	struct csio_lnode *ln;
1917 
1918 	csio_dbg(hw, "Notifying all nodes of event %d\n", note);
1919 
1920 	/* Traverse children lnodes list and send evt */
1921 	list_for_each(tmp, &hw->sln_head) {
1922 		ln = (struct csio_lnode *) tmp;
1923 
1924 		switch (note) {
1925 		case CSIO_LN_NOTIFY_HWREADY:
1926 			csio_lnode_start(ln);
1927 			break;
1928 
1929 		case CSIO_LN_NOTIFY_HWRESET:
1930 		case CSIO_LN_NOTIFY_HWREMOVE:
1931 			csio_lnode_close(ln);
1932 			break;
1933 
1934 		case CSIO_LN_NOTIFY_HWSTOP:
1935 			csio_lnode_stop(ln);
1936 			break;
1937 
1938 		default:
1939 			break;
1940 
1941 		}
1942 	}
1943 }
1944 
1945 /*
1946  * csio_disable_lnodes:
1947  * @hw: HW module
1948  * @portid:port id
1949  * @disable: disable/enable flag.
1950  * If disable=1, disables all lnode hosted on given physical port.
1951  * otherwise enables all the lnodes on given phsysical port.
1952  * This routine need to called with hw lock held.
1953  */
1954 void
1955 csio_disable_lnodes(struct csio_hw *hw, uint8_t portid, bool disable)
1956 {
1957 	struct list_head *tmp;
1958 	struct csio_lnode *ln;
1959 
1960 	csio_dbg(hw, "Notifying event to all nodes of port:%d\n", portid);
1961 
1962 	/* Traverse sibling lnodes list and send evt */
1963 	list_for_each(tmp, &hw->sln_head) {
1964 		ln = (struct csio_lnode *) tmp;
1965 		if (ln->portid != portid)
1966 			continue;
1967 
1968 		if (disable)
1969 			csio_lnode_stop(ln);
1970 		else
1971 			csio_lnode_start(ln);
1972 	}
1973 }
1974 
1975 /*
1976  * csio_ln_init - Initialize an lnode.
1977  * @ln:		lnode
1978  *
1979  */
1980 static int
1981 csio_ln_init(struct csio_lnode *ln)
1982 {
1983 	int rv = -EINVAL;
1984 	struct csio_lnode *rln, *pln;
1985 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1986 
1987 	csio_init_state(&ln->sm, csio_lns_uninit);
1988 	ln->vnp_flowid = CSIO_INVALID_IDX;
1989 	ln->fcf_flowid = CSIO_INVALID_IDX;
1990 
1991 	if (csio_is_root_ln(ln)) {
1992 
1993 		/* This is the lnode used during initialization */
1994 
1995 		ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info), GFP_KERNEL);
1996 		if (!ln->fcfinfo) {
1997 			csio_ln_err(ln, "Failed to alloc FCF record\n");
1998 			CSIO_INC_STATS(hw, n_err_nomem);
1999 			goto err;
2000 		}
2001 
2002 		INIT_LIST_HEAD(&ln->fcf_lsthead);
2003 		kref_init(&ln->fcfinfo->kref);
2004 
2005 		if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
2006 			goto err;
2007 
2008 	} else { /* Either a non-root physical or a virtual lnode */
2009 
2010 		/*
2011 		 * THe rest is common for non-root physical and NPIV lnodes.
2012 		 * Just get references to all other modules
2013 		 */
2014 		rln = csio_root_lnode(ln);
2015 
2016 		if (csio_is_npiv_ln(ln)) {
2017 			/* NPIV */
2018 			pln = csio_parent_lnode(ln);
2019 			kref_get(&pln->fcfinfo->kref);
2020 			ln->fcfinfo = pln->fcfinfo;
2021 		} else {
2022 			/* Another non-root physical lnode (FCF) */
2023 			ln->fcfinfo = kzalloc(sizeof(struct csio_fcf_info),
2024 								GFP_KERNEL);
2025 			if (!ln->fcfinfo) {
2026 				csio_ln_err(ln, "Failed to alloc FCF info\n");
2027 				CSIO_INC_STATS(hw, n_err_nomem);
2028 				goto err;
2029 			}
2030 
2031 			kref_init(&ln->fcfinfo->kref);
2032 
2033 			if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
2034 				goto err;
2035 		}
2036 
2037 	} /* if (!csio_is_root_ln(ln)) */
2038 
2039 	return 0;
2040 err:
2041 	return rv;
2042 }
2043 
2044 static void
2045 csio_ln_exit(struct csio_lnode *ln)
2046 {
2047 	struct csio_lnode *pln;
2048 
2049 	csio_cleanup_rns(ln);
2050 	if (csio_is_npiv_ln(ln)) {
2051 		pln = csio_parent_lnode(ln);
2052 		kref_put(&pln->fcfinfo->kref, csio_free_fcfinfo);
2053 	} else {
2054 		kref_put(&ln->fcfinfo->kref, csio_free_fcfinfo);
2055 		if (csio_fdmi_enable)
2056 			csio_ln_fdmi_exit(ln);
2057 	}
2058 	ln->fcfinfo = NULL;
2059 }
2060 
2061 /**
2062  * csio_lnode_init - Initialize the members of an lnode.
2063  * @ln:		lnode
2064  *
2065  */
2066 int
2067 csio_lnode_init(struct csio_lnode *ln, struct csio_hw *hw,
2068 		struct csio_lnode *pln)
2069 {
2070 	int rv = -EINVAL;
2071 
2072 	/* Link this lnode to hw */
2073 	csio_lnode_to_hw(ln)	= hw;
2074 
2075 	/* Link child to parent if child lnode */
2076 	if (pln)
2077 		ln->pln = pln;
2078 	else
2079 		ln->pln = NULL;
2080 
2081 	/* Initialize scsi_tgt and timers to zero */
2082 	ln->n_scsi_tgts = 0;
2083 	ln->last_scan_ntgts = 0;
2084 	ln->tgt_scan_tick = 0;
2085 
2086 	/* Initialize rnode list */
2087 	INIT_LIST_HEAD(&ln->rnhead);
2088 	INIT_LIST_HEAD(&ln->cln_head);
2089 
2090 	/* Initialize log level for debug */
2091 	ln->params.log_level	= hw->params.log_level;
2092 
2093 	if (csio_ln_init(ln))
2094 		goto err;
2095 
2096 	/* Add lnode to list of sibling or children lnodes */
2097 	spin_lock_irq(&hw->lock);
2098 	list_add_tail(&ln->sm.sm_list, pln ? &pln->cln_head : &hw->sln_head);
2099 	if (pln)
2100 		pln->num_vports++;
2101 	spin_unlock_irq(&hw->lock);
2102 
2103 	hw->num_lns++;
2104 
2105 	return 0;
2106 err:
2107 	csio_lnode_to_hw(ln) = NULL;
2108 	return rv;
2109 }
2110 
2111 /**
2112  * csio_lnode_exit - De-instantiate an lnode.
2113  * @ln:		lnode
2114  *
2115  */
2116 void
2117 csio_lnode_exit(struct csio_lnode *ln)
2118 {
2119 	struct csio_hw *hw = csio_lnode_to_hw(ln);
2120 
2121 	csio_ln_exit(ln);
2122 
2123 	/* Remove this lnode from hw->sln_head */
2124 	spin_lock_irq(&hw->lock);
2125 
2126 	list_del_init(&ln->sm.sm_list);
2127 
2128 	/* If it is children lnode, decrement the
2129 	 * counter in its parent lnode
2130 	 */
2131 	if (ln->pln)
2132 		ln->pln->num_vports--;
2133 
2134 	/* Update root lnode pointer */
2135 	if (list_empty(&hw->sln_head))
2136 		hw->rln = NULL;
2137 	else
2138 		hw->rln = (struct csio_lnode *)csio_list_next(&hw->sln_head);
2139 
2140 	spin_unlock_irq(&hw->lock);
2141 
2142 	csio_lnode_to_hw(ln)	= NULL;
2143 	hw->num_lns--;
2144 }
2145