1 /*
2  * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17 
18 /*
19  *  bfa_attr.c Linux driver configuration interface module.
20  */
21 
22 #include "bfad_drv.h"
23 #include "bfad_im.h"
24 
25 /*
26  * FC transport template entry, get SCSI target port ID.
27  */
28 static void
bfad_im_get_starget_port_id(struct scsi_target * starget)29 bfad_im_get_starget_port_id(struct scsi_target *starget)
30 {
31 	struct Scsi_Host *shost;
32 	struct bfad_im_port_s *im_port;
33 	struct bfad_s         *bfad;
34 	struct bfad_itnim_s   *itnim = NULL;
35 	u32        fc_id = -1;
36 	unsigned long   flags;
37 
38 	shost = dev_to_shost(starget->dev.parent);
39 	im_port = (struct bfad_im_port_s *) shost->hostdata[0];
40 	bfad = im_port->bfad;
41 	spin_lock_irqsave(&bfad->bfad_lock, flags);
42 
43 	itnim = bfad_get_itnim(im_port, starget->id);
44 	if (itnim)
45 		fc_id = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
46 
47 	fc_starget_port_id(starget) = fc_id;
48 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
49 }
50 
51 /*
52  * FC transport template entry, get SCSI target nwwn.
53  */
54 static void
bfad_im_get_starget_node_name(struct scsi_target * starget)55 bfad_im_get_starget_node_name(struct scsi_target *starget)
56 {
57 	struct Scsi_Host *shost;
58 	struct bfad_im_port_s *im_port;
59 	struct bfad_s         *bfad;
60 	struct bfad_itnim_s   *itnim = NULL;
61 	u64             node_name = 0;
62 	unsigned long   flags;
63 
64 	shost = dev_to_shost(starget->dev.parent);
65 	im_port = (struct bfad_im_port_s *) shost->hostdata[0];
66 	bfad = im_port->bfad;
67 	spin_lock_irqsave(&bfad->bfad_lock, flags);
68 
69 	itnim = bfad_get_itnim(im_port, starget->id);
70 	if (itnim)
71 		node_name = bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim);
72 
73 	fc_starget_node_name(starget) = cpu_to_be64(node_name);
74 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
75 }
76 
77 /*
78  * FC transport template entry, get SCSI target pwwn.
79  */
80 static void
bfad_im_get_starget_port_name(struct scsi_target * starget)81 bfad_im_get_starget_port_name(struct scsi_target *starget)
82 {
83 	struct Scsi_Host *shost;
84 	struct bfad_im_port_s *im_port;
85 	struct bfad_s         *bfad;
86 	struct bfad_itnim_s   *itnim = NULL;
87 	u64             port_name = 0;
88 	unsigned long   flags;
89 
90 	shost = dev_to_shost(starget->dev.parent);
91 	im_port = (struct bfad_im_port_s *) shost->hostdata[0];
92 	bfad = im_port->bfad;
93 	spin_lock_irqsave(&bfad->bfad_lock, flags);
94 
95 	itnim = bfad_get_itnim(im_port, starget->id);
96 	if (itnim)
97 		port_name = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
98 
99 	fc_starget_port_name(starget) = cpu_to_be64(port_name);
100 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
101 }
102 
103 /*
104  * FC transport template entry, get SCSI host port ID.
105  */
106 static void
bfad_im_get_host_port_id(struct Scsi_Host * shost)107 bfad_im_get_host_port_id(struct Scsi_Host *shost)
108 {
109 	struct bfad_im_port_s *im_port =
110 			(struct bfad_im_port_s *) shost->hostdata[0];
111 	struct bfad_port_s    *port = im_port->port;
112 
113 	fc_host_port_id(shost) =
114 			bfa_hton3b(bfa_fcs_lport_get_fcid(port->fcs_port));
115 }
116 
117 /*
118  * FC transport template entry, get SCSI host port type.
119  */
120 static void
bfad_im_get_host_port_type(struct Scsi_Host * shost)121 bfad_im_get_host_port_type(struct Scsi_Host *shost)
122 {
123 	struct bfad_im_port_s *im_port =
124 			(struct bfad_im_port_s *) shost->hostdata[0];
125 	struct bfad_s         *bfad = im_port->bfad;
126 	struct bfa_lport_attr_s port_attr;
127 
128 	bfa_fcs_lport_get_attr(&bfad->bfa_fcs.fabric.bport, &port_attr);
129 
130 	switch (port_attr.port_type) {
131 	case BFA_PORT_TYPE_NPORT:
132 		fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
133 		break;
134 	case BFA_PORT_TYPE_NLPORT:
135 		fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
136 		break;
137 	case BFA_PORT_TYPE_P2P:
138 		fc_host_port_type(shost) = FC_PORTTYPE_PTP;
139 		break;
140 	case BFA_PORT_TYPE_LPORT:
141 		fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
142 		break;
143 	default:
144 		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
145 		break;
146 	}
147 }
148 
149 /*
150  * FC transport template entry, get SCSI host port state.
151  */
152 static void
bfad_im_get_host_port_state(struct Scsi_Host * shost)153 bfad_im_get_host_port_state(struct Scsi_Host *shost)
154 {
155 	struct bfad_im_port_s *im_port =
156 			(struct bfad_im_port_s *) shost->hostdata[0];
157 	struct bfad_s         *bfad = im_port->bfad;
158 	struct bfa_port_attr_s attr;
159 
160 	bfa_fcport_get_attr(&bfad->bfa, &attr);
161 
162 	switch (attr.port_state) {
163 	case BFA_PORT_ST_LINKDOWN:
164 		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
165 		break;
166 	case BFA_PORT_ST_LINKUP:
167 		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
168 		break;
169 	case BFA_PORT_ST_DISABLED:
170 	case BFA_PORT_ST_STOPPED:
171 	case BFA_PORT_ST_IOCDOWN:
172 	case BFA_PORT_ST_IOCDIS:
173 		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
174 		break;
175 	case BFA_PORT_ST_UNINIT:
176 	case BFA_PORT_ST_ENABLING_QWAIT:
177 	case BFA_PORT_ST_ENABLING:
178 	case BFA_PORT_ST_DISABLING_QWAIT:
179 	case BFA_PORT_ST_DISABLING:
180 	default:
181 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
182 		break;
183 	}
184 }
185 
186 /*
187  * FC transport template entry, get SCSI host active fc4s.
188  */
189 static void
bfad_im_get_host_active_fc4s(struct Scsi_Host * shost)190 bfad_im_get_host_active_fc4s(struct Scsi_Host *shost)
191 {
192 	struct bfad_im_port_s *im_port =
193 			(struct bfad_im_port_s *) shost->hostdata[0];
194 	struct bfad_port_s    *port = im_port->port;
195 
196 	memset(fc_host_active_fc4s(shost), 0,
197 	       sizeof(fc_host_active_fc4s(shost)));
198 
199 	if (port->supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
200 		fc_host_active_fc4s(shost)[2] = 1;
201 
202 	fc_host_active_fc4s(shost)[7] = 1;
203 }
204 
205 /*
206  * FC transport template entry, get SCSI host link speed.
207  */
208 static void
bfad_im_get_host_speed(struct Scsi_Host * shost)209 bfad_im_get_host_speed(struct Scsi_Host *shost)
210 {
211 	struct bfad_im_port_s *im_port =
212 			(struct bfad_im_port_s *) shost->hostdata[0];
213 	struct bfad_s         *bfad = im_port->bfad;
214 	struct bfa_port_attr_s attr;
215 
216 	bfa_fcport_get_attr(&bfad->bfa, &attr);
217 	switch (attr.speed) {
218 	case BFA_PORT_SPEED_10GBPS:
219 		fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
220 		break;
221 	case BFA_PORT_SPEED_16GBPS:
222 		fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
223 		break;
224 	case BFA_PORT_SPEED_8GBPS:
225 		fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
226 		break;
227 	case BFA_PORT_SPEED_4GBPS:
228 		fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
229 		break;
230 	case BFA_PORT_SPEED_2GBPS:
231 		fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
232 		break;
233 	case BFA_PORT_SPEED_1GBPS:
234 		fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
235 		break;
236 	default:
237 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
238 		break;
239 	}
240 }
241 
242 /*
243  * FC transport template entry, get SCSI host port type.
244  */
245 static void
bfad_im_get_host_fabric_name(struct Scsi_Host * shost)246 bfad_im_get_host_fabric_name(struct Scsi_Host *shost)
247 {
248 	struct bfad_im_port_s *im_port =
249 			(struct bfad_im_port_s *) shost->hostdata[0];
250 	struct bfad_port_s    *port = im_port->port;
251 	wwn_t           fabric_nwwn = 0;
252 
253 	fabric_nwwn = bfa_fcs_lport_get_fabric_name(port->fcs_port);
254 
255 	fc_host_fabric_name(shost) = cpu_to_be64(fabric_nwwn);
256 
257 }
258 
259 /*
260  * FC transport template entry, get BFAD statistics.
261  */
262 static struct fc_host_statistics *
bfad_im_get_stats(struct Scsi_Host * shost)263 bfad_im_get_stats(struct Scsi_Host *shost)
264 {
265 	struct bfad_im_port_s *im_port =
266 			(struct bfad_im_port_s *) shost->hostdata[0];
267 	struct bfad_s         *bfad = im_port->bfad;
268 	struct bfad_hal_comp fcomp;
269 	union bfa_port_stats_u *fcstats;
270 	struct fc_host_statistics *hstats;
271 	bfa_status_t    rc;
272 	unsigned long   flags;
273 
274 	fcstats = kzalloc(sizeof(union bfa_port_stats_u), GFP_KERNEL);
275 	if (fcstats == NULL)
276 		return NULL;
277 
278 	hstats = &bfad->link_stats;
279 	init_completion(&fcomp.comp);
280 	spin_lock_irqsave(&bfad->bfad_lock, flags);
281 	memset(hstats, 0, sizeof(struct fc_host_statistics));
282 	rc = bfa_port_get_stats(BFA_FCPORT(&bfad->bfa),
283 				fcstats, bfad_hcb_comp, &fcomp);
284 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
285 	if (rc != BFA_STATUS_OK)
286 		return NULL;
287 
288 	wait_for_completion(&fcomp.comp);
289 
290 	/* Fill the fc_host_statistics structure */
291 	hstats->seconds_since_last_reset = fcstats->fc.secs_reset;
292 	hstats->tx_frames = fcstats->fc.tx_frames;
293 	hstats->tx_words  = fcstats->fc.tx_words;
294 	hstats->rx_frames = fcstats->fc.rx_frames;
295 	hstats->rx_words  = fcstats->fc.rx_words;
296 	hstats->lip_count = fcstats->fc.lip_count;
297 	hstats->nos_count = fcstats->fc.nos_count;
298 	hstats->error_frames = fcstats->fc.error_frames;
299 	hstats->dumped_frames = fcstats->fc.dropped_frames;
300 	hstats->link_failure_count = fcstats->fc.link_failures;
301 	hstats->loss_of_sync_count = fcstats->fc.loss_of_syncs;
302 	hstats->loss_of_signal_count = fcstats->fc.loss_of_signals;
303 	hstats->prim_seq_protocol_err_count = fcstats->fc.primseq_errs;
304 	hstats->invalid_crc_count = fcstats->fc.invalid_crcs;
305 
306 	kfree(fcstats);
307 	return hstats;
308 }
309 
310 /*
311  * FC transport template entry, reset BFAD statistics.
312  */
313 static void
bfad_im_reset_stats(struct Scsi_Host * shost)314 bfad_im_reset_stats(struct Scsi_Host *shost)
315 {
316 	struct bfad_im_port_s *im_port =
317 			(struct bfad_im_port_s *) shost->hostdata[0];
318 	struct bfad_s         *bfad = im_port->bfad;
319 	struct bfad_hal_comp fcomp;
320 	unsigned long   flags;
321 	bfa_status_t    rc;
322 
323 	init_completion(&fcomp.comp);
324 	spin_lock_irqsave(&bfad->bfad_lock, flags);
325 	rc = bfa_port_clear_stats(BFA_FCPORT(&bfad->bfa), bfad_hcb_comp,
326 					&fcomp);
327 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
328 
329 	if (rc != BFA_STATUS_OK)
330 		return;
331 
332 	wait_for_completion(&fcomp.comp);
333 
334 	return;
335 }
336 
337 /*
338  * FC transport template entry, get rport loss timeout.
339  */
340 static void
bfad_im_get_rport_loss_tmo(struct fc_rport * rport)341 bfad_im_get_rport_loss_tmo(struct fc_rport *rport)
342 {
343 	struct bfad_itnim_data_s *itnim_data = rport->dd_data;
344 	struct bfad_itnim_s   *itnim = itnim_data->itnim;
345 	struct bfad_s         *bfad = itnim->im->bfad;
346 	unsigned long   flags;
347 
348 	spin_lock_irqsave(&bfad->bfad_lock, flags);
349 	rport->dev_loss_tmo = bfa_fcpim_path_tov_get(&bfad->bfa);
350 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
351 }
352 
353 /*
354  * FC transport template entry, set rport loss timeout.
355  */
356 static void
bfad_im_set_rport_loss_tmo(struct fc_rport * rport,u32 timeout)357 bfad_im_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
358 {
359 	struct bfad_itnim_data_s *itnim_data = rport->dd_data;
360 	struct bfad_itnim_s   *itnim = itnim_data->itnim;
361 	struct bfad_s         *bfad = itnim->im->bfad;
362 	unsigned long   flags;
363 
364 	if (timeout > 0) {
365 		spin_lock_irqsave(&bfad->bfad_lock, flags);
366 		bfa_fcpim_path_tov_set(&bfad->bfa, timeout);
367 		rport->dev_loss_tmo = bfa_fcpim_path_tov_get(&bfad->bfa);
368 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
369 	}
370 
371 }
372 
373 static int
bfad_im_vport_create(struct fc_vport * fc_vport,bool disable)374 bfad_im_vport_create(struct fc_vport *fc_vport, bool disable)
375 {
376 	char *vname = fc_vport->symbolic_name;
377 	struct Scsi_Host *shost = fc_vport->shost;
378 	struct bfad_im_port_s *im_port =
379 		(struct bfad_im_port_s *) shost->hostdata[0];
380 	struct bfad_s *bfad = im_port->bfad;
381 	struct bfa_lport_cfg_s port_cfg;
382 	struct bfad_vport_s *vp;
383 	int status = 0, rc;
384 	unsigned long flags;
385 
386 	memset(&port_cfg, 0, sizeof(port_cfg));
387 	u64_to_wwn(fc_vport->node_name, (u8 *)&port_cfg.nwwn);
388 	u64_to_wwn(fc_vport->port_name, (u8 *)&port_cfg.pwwn);
389 	if (strlen(vname) > 0)
390 		strcpy((char *)&port_cfg.sym_name, vname);
391 	port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
392 
393 	spin_lock_irqsave(&bfad->bfad_lock, flags);
394 	list_for_each_entry(vp, &bfad->pbc_vport_list, list_entry) {
395 		if (port_cfg.pwwn ==
396 				vp->fcs_vport.lport.port_cfg.pwwn) {
397 			port_cfg.preboot_vp =
398 				vp->fcs_vport.lport.port_cfg.preboot_vp;
399 			break;
400 		}
401 	}
402 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
403 
404 	rc = bfad_vport_create(bfad, 0, &port_cfg, &fc_vport->dev);
405 	if (rc == BFA_STATUS_OK) {
406 		struct bfad_vport_s *vport;
407 		struct bfa_fcs_vport_s *fcs_vport;
408 		struct Scsi_Host *vshost;
409 
410 		spin_lock_irqsave(&bfad->bfad_lock, flags);
411 		fcs_vport = bfa_fcs_vport_lookup(&bfad->bfa_fcs, 0,
412 					port_cfg.pwwn);
413 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
414 		if (fcs_vport == NULL)
415 			return VPCERR_BAD_WWN;
416 
417 		fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
418 		if (disable) {
419 			spin_lock_irqsave(&bfad->bfad_lock, flags);
420 			bfa_fcs_vport_stop(fcs_vport);
421 			spin_unlock_irqrestore(&bfad->bfad_lock, flags);
422 			fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
423 		}
424 
425 		vport = fcs_vport->vport_drv;
426 		vshost = vport->drv_port.im_port->shost;
427 		fc_host_node_name(vshost) = wwn_to_u64((u8 *)&port_cfg.nwwn);
428 		fc_host_port_name(vshost) = wwn_to_u64((u8 *)&port_cfg.pwwn);
429 		fc_vport->dd_data = vport;
430 		vport->drv_port.im_port->fc_vport = fc_vport;
431 	} else if (rc == BFA_STATUS_INVALID_WWN)
432 		return VPCERR_BAD_WWN;
433 	else if (rc == BFA_STATUS_VPORT_EXISTS)
434 		return VPCERR_BAD_WWN;
435 	else if (rc == BFA_STATUS_VPORT_MAX)
436 		return VPCERR_NO_FABRIC_SUPP;
437 	else if (rc == BFA_STATUS_VPORT_WWN_BP)
438 		return VPCERR_BAD_WWN;
439 	else
440 		return FC_VPORT_FAILED;
441 
442 	return status;
443 }
444 
445 int
bfad_im_issue_fc_host_lip(struct Scsi_Host * shost)446 bfad_im_issue_fc_host_lip(struct Scsi_Host *shost)
447 {
448 	struct bfad_im_port_s *im_port =
449 			(struct bfad_im_port_s *) shost->hostdata[0];
450 	struct bfad_s *bfad = im_port->bfad;
451 	struct bfad_hal_comp fcomp;
452 	unsigned long flags;
453 	uint32_t status;
454 
455 	init_completion(&fcomp.comp);
456 	spin_lock_irqsave(&bfad->bfad_lock, flags);
457 	status = bfa_port_disable(&bfad->bfa.modules.port,
458 					bfad_hcb_comp, &fcomp);
459 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
460 
461 	if (status != BFA_STATUS_OK)
462 		return -EIO;
463 
464 	wait_for_completion(&fcomp.comp);
465 	if (fcomp.status != BFA_STATUS_OK)
466 		return -EIO;
467 
468 	spin_lock_irqsave(&bfad->bfad_lock, flags);
469 	status = bfa_port_enable(&bfad->bfa.modules.port,
470 					bfad_hcb_comp, &fcomp);
471 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
472 	if (status != BFA_STATUS_OK)
473 		return -EIO;
474 
475 	wait_for_completion(&fcomp.comp);
476 	if (fcomp.status != BFA_STATUS_OK)
477 		return -EIO;
478 
479 	return 0;
480 }
481 
482 static int
bfad_im_vport_delete(struct fc_vport * fc_vport)483 bfad_im_vport_delete(struct fc_vport *fc_vport)
484 {
485 	struct bfad_vport_s *vport = (struct bfad_vport_s *)fc_vport->dd_data;
486 	struct bfad_im_port_s *im_port =
487 			(struct bfad_im_port_s *) vport->drv_port.im_port;
488 	struct bfad_s *bfad = im_port->bfad;
489 	struct bfad_port_s *port;
490 	struct bfa_fcs_vport_s *fcs_vport;
491 	struct Scsi_Host *vshost;
492 	wwn_t   pwwn;
493 	int rc;
494 	unsigned long flags;
495 	struct completion fcomp;
496 
497 	if (im_port->flags & BFAD_PORT_DELETE) {
498 		bfad_scsi_host_free(bfad, im_port);
499 		list_del(&vport->list_entry);
500 		return 0;
501 	}
502 
503 	port = im_port->port;
504 
505 	vshost = vport->drv_port.im_port->shost;
506 	u64_to_wwn(fc_host_port_name(vshost), (u8 *)&pwwn);
507 
508 	spin_lock_irqsave(&bfad->bfad_lock, flags);
509 	fcs_vport = bfa_fcs_vport_lookup(&bfad->bfa_fcs, 0, pwwn);
510 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
511 
512 	if (fcs_vport == NULL)
513 		return VPCERR_BAD_WWN;
514 
515 	vport->drv_port.flags |= BFAD_PORT_DELETE;
516 
517 	vport->comp_del = &fcomp;
518 	init_completion(vport->comp_del);
519 
520 	spin_lock_irqsave(&bfad->bfad_lock, flags);
521 	rc = bfa_fcs_vport_delete(&vport->fcs_vport);
522 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
523 
524 	if (rc == BFA_STATUS_PBC) {
525 		vport->drv_port.flags &= ~BFAD_PORT_DELETE;
526 		vport->comp_del = NULL;
527 		return -1;
528 	}
529 
530 	wait_for_completion(vport->comp_del);
531 
532 	bfad_scsi_host_free(bfad, im_port);
533 	list_del(&vport->list_entry);
534 	kfree(vport);
535 
536 	return 0;
537 }
538 
539 static int
bfad_im_vport_disable(struct fc_vport * fc_vport,bool disable)540 bfad_im_vport_disable(struct fc_vport *fc_vport, bool disable)
541 {
542 	struct bfad_vport_s *vport;
543 	struct bfad_s *bfad;
544 	struct bfa_fcs_vport_s *fcs_vport;
545 	struct Scsi_Host *vshost;
546 	wwn_t   pwwn;
547 	unsigned long flags;
548 
549 	vport = (struct bfad_vport_s *)fc_vport->dd_data;
550 	bfad = vport->drv_port.bfad;
551 	vshost = vport->drv_port.im_port->shost;
552 	u64_to_wwn(fc_host_port_name(vshost), (u8 *)&pwwn);
553 
554 	spin_lock_irqsave(&bfad->bfad_lock, flags);
555 	fcs_vport = bfa_fcs_vport_lookup(&bfad->bfa_fcs, 0, pwwn);
556 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
557 
558 	if (fcs_vport == NULL)
559 		return VPCERR_BAD_WWN;
560 
561 	if (disable) {
562 		bfa_fcs_vport_stop(fcs_vport);
563 		fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
564 	} else {
565 		bfa_fcs_vport_start(fcs_vport);
566 		fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
567 	}
568 
569 	return 0;
570 }
571 
572 struct fc_function_template bfad_im_fc_function_template = {
573 
574 	/* Target dynamic attributes */
575 	.get_starget_port_id = bfad_im_get_starget_port_id,
576 	.show_starget_port_id = 1,
577 	.get_starget_node_name = bfad_im_get_starget_node_name,
578 	.show_starget_node_name = 1,
579 	.get_starget_port_name = bfad_im_get_starget_port_name,
580 	.show_starget_port_name = 1,
581 
582 	/* Host dynamic attribute */
583 	.get_host_port_id = bfad_im_get_host_port_id,
584 	.show_host_port_id = 1,
585 
586 	/* Host fixed attributes */
587 	.show_host_node_name = 1,
588 	.show_host_port_name = 1,
589 	.show_host_supported_classes = 1,
590 	.show_host_supported_fc4s = 1,
591 	.show_host_supported_speeds = 1,
592 	.show_host_maxframe_size = 1,
593 
594 	/* More host dynamic attributes */
595 	.show_host_port_type = 1,
596 	.get_host_port_type = bfad_im_get_host_port_type,
597 	.show_host_port_state = 1,
598 	.get_host_port_state = bfad_im_get_host_port_state,
599 	.show_host_active_fc4s = 1,
600 	.get_host_active_fc4s = bfad_im_get_host_active_fc4s,
601 	.show_host_speed = 1,
602 	.get_host_speed = bfad_im_get_host_speed,
603 	.show_host_fabric_name = 1,
604 	.get_host_fabric_name = bfad_im_get_host_fabric_name,
605 
606 	.show_host_symbolic_name = 1,
607 
608 	/* Statistics */
609 	.get_fc_host_stats = bfad_im_get_stats,
610 	.reset_fc_host_stats = bfad_im_reset_stats,
611 
612 	/* Allocation length for host specific data */
613 	.dd_fcrport_size = sizeof(struct bfad_itnim_data_s *),
614 
615 	/* Remote port fixed attributes */
616 	.show_rport_maxframe_size = 1,
617 	.show_rport_supported_classes = 1,
618 	.show_rport_dev_loss_tmo = 1,
619 	.get_rport_dev_loss_tmo = bfad_im_get_rport_loss_tmo,
620 	.set_rport_dev_loss_tmo = bfad_im_set_rport_loss_tmo,
621 	.issue_fc_host_lip = bfad_im_issue_fc_host_lip,
622 	.vport_create = bfad_im_vport_create,
623 	.vport_delete = bfad_im_vport_delete,
624 	.vport_disable = bfad_im_vport_disable,
625 	.bsg_request = bfad_im_bsg_request,
626 	.bsg_timeout = bfad_im_bsg_timeout,
627 };
628 
629 struct fc_function_template bfad_im_vport_fc_function_template = {
630 
631 	/* Target dynamic attributes */
632 	.get_starget_port_id = bfad_im_get_starget_port_id,
633 	.show_starget_port_id = 1,
634 	.get_starget_node_name = bfad_im_get_starget_node_name,
635 	.show_starget_node_name = 1,
636 	.get_starget_port_name = bfad_im_get_starget_port_name,
637 	.show_starget_port_name = 1,
638 
639 	/* Host dynamic attribute */
640 	.get_host_port_id = bfad_im_get_host_port_id,
641 	.show_host_port_id = 1,
642 
643 	/* Host fixed attributes */
644 	.show_host_node_name = 1,
645 	.show_host_port_name = 1,
646 	.show_host_supported_classes = 1,
647 	.show_host_supported_fc4s = 1,
648 	.show_host_supported_speeds = 1,
649 	.show_host_maxframe_size = 1,
650 
651 	/* More host dynamic attributes */
652 	.show_host_port_type = 1,
653 	.get_host_port_type = bfad_im_get_host_port_type,
654 	.show_host_port_state = 1,
655 	.get_host_port_state = bfad_im_get_host_port_state,
656 	.show_host_active_fc4s = 1,
657 	.get_host_active_fc4s = bfad_im_get_host_active_fc4s,
658 	.show_host_speed = 1,
659 	.get_host_speed = bfad_im_get_host_speed,
660 	.show_host_fabric_name = 1,
661 	.get_host_fabric_name = bfad_im_get_host_fabric_name,
662 
663 	.show_host_symbolic_name = 1,
664 
665 	/* Statistics */
666 	.get_fc_host_stats = bfad_im_get_stats,
667 	.reset_fc_host_stats = bfad_im_reset_stats,
668 
669 	/* Allocation length for host specific data */
670 	.dd_fcrport_size = sizeof(struct bfad_itnim_data_s *),
671 
672 	/* Remote port fixed attributes */
673 	.show_rport_maxframe_size = 1,
674 	.show_rport_supported_classes = 1,
675 	.show_rport_dev_loss_tmo = 1,
676 	.get_rport_dev_loss_tmo = bfad_im_get_rport_loss_tmo,
677 	.set_rport_dev_loss_tmo = bfad_im_set_rport_loss_tmo,
678 };
679 
680 /*
681  *  Scsi_Host_attrs SCSI host attributes
682  */
683 static ssize_t
bfad_im_serial_num_show(struct device * dev,struct device_attribute * attr,char * buf)684 bfad_im_serial_num_show(struct device *dev, struct device_attribute *attr,
685 			 char *buf)
686 {
687 	struct Scsi_Host *shost = class_to_shost(dev);
688 	struct bfad_im_port_s *im_port =
689 			(struct bfad_im_port_s *) shost->hostdata[0];
690 	struct bfad_s *bfad = im_port->bfad;
691 	char serial_num[BFA_ADAPTER_SERIAL_NUM_LEN];
692 
693 	bfa_get_adapter_serial_num(&bfad->bfa, serial_num);
694 	return snprintf(buf, PAGE_SIZE, "%s\n", serial_num);
695 }
696 
697 static ssize_t
bfad_im_model_show(struct device * dev,struct device_attribute * attr,char * buf)698 bfad_im_model_show(struct device *dev, struct device_attribute *attr,
699 			char *buf)
700 {
701 	struct Scsi_Host *shost = class_to_shost(dev);
702 	struct bfad_im_port_s *im_port =
703 			(struct bfad_im_port_s *) shost->hostdata[0];
704 	struct bfad_s *bfad = im_port->bfad;
705 	char model[BFA_ADAPTER_MODEL_NAME_LEN];
706 
707 	bfa_get_adapter_model(&bfad->bfa, model);
708 	return snprintf(buf, PAGE_SIZE, "%s\n", model);
709 }
710 
711 static ssize_t
bfad_im_model_desc_show(struct device * dev,struct device_attribute * attr,char * buf)712 bfad_im_model_desc_show(struct device *dev, struct device_attribute *attr,
713 				 char *buf)
714 {
715 	struct Scsi_Host *shost = class_to_shost(dev);
716 	struct bfad_im_port_s *im_port =
717 			(struct bfad_im_port_s *) shost->hostdata[0];
718 	struct bfad_s *bfad = im_port->bfad;
719 	char model[BFA_ADAPTER_MODEL_NAME_LEN];
720 	char model_descr[BFA_ADAPTER_MODEL_DESCR_LEN];
721 	int nports = 0;
722 
723 	bfa_get_adapter_model(&bfad->bfa, model);
724 	nports = bfa_get_nports(&bfad->bfa);
725 	if (!strcmp(model, "Brocade-425"))
726 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
727 			"Brocade 4Gbps PCIe dual port FC HBA");
728 	else if (!strcmp(model, "Brocade-825"))
729 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
730 			"Brocade 8Gbps PCIe dual port FC HBA");
731 	else if (!strcmp(model, "Brocade-42B"))
732 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
733 			"Brocade 4Gbps PCIe dual port FC HBA for HP");
734 	else if (!strcmp(model, "Brocade-82B"))
735 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
736 			"Brocade 8Gbps PCIe dual port FC HBA for HP");
737 	else if (!strcmp(model, "Brocade-1010"))
738 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
739 			"Brocade 10Gbps single port CNA");
740 	else if (!strcmp(model, "Brocade-1020"))
741 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
742 			"Brocade 10Gbps dual port CNA");
743 	else if (!strcmp(model, "Brocade-1007"))
744 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
745 			"Brocade 10Gbps CNA for IBM Blade Center");
746 	else if (!strcmp(model, "Brocade-415"))
747 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
748 			"Brocade 4Gbps PCIe single port FC HBA");
749 	else if (!strcmp(model, "Brocade-815"))
750 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
751 			"Brocade 8Gbps PCIe single port FC HBA");
752 	else if (!strcmp(model, "Brocade-41B"))
753 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
754 			"Brocade 4Gbps PCIe single port FC HBA for HP");
755 	else if (!strcmp(model, "Brocade-81B"))
756 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
757 			"Brocade 8Gbps PCIe single port FC HBA for HP");
758 	else if (!strcmp(model, "Brocade-804"))
759 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
760 			"Brocade 8Gbps FC HBA for HP Bladesystem C-class");
761 	else if (!strcmp(model, "Brocade-902") ||
762 		 !strcmp(model, "Brocade-1741"))
763 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
764 			"Brocade 10Gbps CNA for Dell M-Series Blade Servers");
765 	else if (strstr(model, "Brocade-1560")) {
766 		if (nports == 1)
767 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
768 				"Brocade 16Gbps PCIe single port FC HBA");
769 		else
770 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
771 				"Brocade 16Gbps PCIe dual port FC HBA");
772 	} else if (strstr(model, "Brocade-1710")) {
773 		if (nports == 1)
774 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
775 				"Brocade 10Gbps single port CNA");
776 		else
777 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
778 				"Brocade 10Gbps dual port CNA");
779 	} else if (strstr(model, "Brocade-1860")) {
780 		if (nports == 1 && bfa_ioc_is_cna(&bfad->bfa.ioc))
781 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
782 				"Brocade 10Gbps single port CNA");
783 		else if (nports == 1 && !bfa_ioc_is_cna(&bfad->bfa.ioc))
784 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
785 				"Brocade 16Gbps PCIe single port FC HBA");
786 		else if (nports == 2 && bfa_ioc_is_cna(&bfad->bfa.ioc))
787 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
788 				"Brocade 10Gbps dual port CNA");
789 		else if (nports == 2 && !bfa_ioc_is_cna(&bfad->bfa.ioc))
790 			snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
791 				"Brocade 16Gbps PCIe dual port FC HBA");
792 	} else
793 		snprintf(model_descr, BFA_ADAPTER_MODEL_DESCR_LEN,
794 			"Invalid Model");
795 
796 	return snprintf(buf, PAGE_SIZE, "%s\n", model_descr);
797 }
798 
799 static ssize_t
bfad_im_node_name_show(struct device * dev,struct device_attribute * attr,char * buf)800 bfad_im_node_name_show(struct device *dev, struct device_attribute *attr,
801 				 char *buf)
802 {
803 	struct Scsi_Host *shost = class_to_shost(dev);
804 	struct bfad_im_port_s *im_port =
805 			(struct bfad_im_port_s *) shost->hostdata[0];
806 	struct bfad_port_s    *port = im_port->port;
807 	u64        nwwn;
808 
809 	nwwn = bfa_fcs_lport_get_nwwn(port->fcs_port);
810 	return snprintf(buf, PAGE_SIZE, "0x%llx\n", cpu_to_be64(nwwn));
811 }
812 
813 static ssize_t
bfad_im_symbolic_name_show(struct device * dev,struct device_attribute * attr,char * buf)814 bfad_im_symbolic_name_show(struct device *dev, struct device_attribute *attr,
815 				 char *buf)
816 {
817 	struct Scsi_Host *shost = class_to_shost(dev);
818 	struct bfad_im_port_s *im_port =
819 			(struct bfad_im_port_s *) shost->hostdata[0];
820 	struct bfad_s *bfad = im_port->bfad;
821 	struct bfa_lport_attr_s port_attr;
822 	char symname[BFA_SYMNAME_MAXLEN];
823 
824 	bfa_fcs_lport_get_attr(&bfad->bfa_fcs.fabric.bport, &port_attr);
825 	strncpy(symname, port_attr.port_cfg.sym_name.symname,
826 			BFA_SYMNAME_MAXLEN);
827 	return snprintf(buf, PAGE_SIZE, "%s\n", symname);
828 }
829 
830 static ssize_t
bfad_im_hw_version_show(struct device * dev,struct device_attribute * attr,char * buf)831 bfad_im_hw_version_show(struct device *dev, struct device_attribute *attr,
832 				char *buf)
833 {
834 	struct Scsi_Host *shost = class_to_shost(dev);
835 	struct bfad_im_port_s *im_port =
836 			(struct bfad_im_port_s *) shost->hostdata[0];
837 	struct bfad_s *bfad = im_port->bfad;
838 	char hw_ver[BFA_VERSION_LEN];
839 
840 	bfa_get_pci_chip_rev(&bfad->bfa, hw_ver);
841 	return snprintf(buf, PAGE_SIZE, "%s\n", hw_ver);
842 }
843 
844 static ssize_t
bfad_im_drv_version_show(struct device * dev,struct device_attribute * attr,char * buf)845 bfad_im_drv_version_show(struct device *dev, struct device_attribute *attr,
846 				char *buf)
847 {
848 	return snprintf(buf, PAGE_SIZE, "%s\n", BFAD_DRIVER_VERSION);
849 }
850 
851 static ssize_t
bfad_im_optionrom_version_show(struct device * dev,struct device_attribute * attr,char * buf)852 bfad_im_optionrom_version_show(struct device *dev,
853 			 struct device_attribute *attr, char *buf)
854 {
855 	struct Scsi_Host *shost = class_to_shost(dev);
856 	struct bfad_im_port_s *im_port =
857 			(struct bfad_im_port_s *) shost->hostdata[0];
858 	struct bfad_s *bfad = im_port->bfad;
859 	char optrom_ver[BFA_VERSION_LEN];
860 
861 	bfa_get_adapter_optrom_ver(&bfad->bfa, optrom_ver);
862 	return snprintf(buf, PAGE_SIZE, "%s\n", optrom_ver);
863 }
864 
865 static ssize_t
bfad_im_fw_version_show(struct device * dev,struct device_attribute * attr,char * buf)866 bfad_im_fw_version_show(struct device *dev, struct device_attribute *attr,
867 				 char *buf)
868 {
869 	struct Scsi_Host *shost = class_to_shost(dev);
870 	struct bfad_im_port_s *im_port =
871 			(struct bfad_im_port_s *) shost->hostdata[0];
872 	struct bfad_s *bfad = im_port->bfad;
873 	char fw_ver[BFA_VERSION_LEN];
874 
875 	bfa_get_adapter_fw_ver(&bfad->bfa, fw_ver);
876 	return snprintf(buf, PAGE_SIZE, "%s\n", fw_ver);
877 }
878 
879 static ssize_t
bfad_im_num_of_ports_show(struct device * dev,struct device_attribute * attr,char * buf)880 bfad_im_num_of_ports_show(struct device *dev, struct device_attribute *attr,
881 				char *buf)
882 {
883 	struct Scsi_Host *shost = class_to_shost(dev);
884 	struct bfad_im_port_s *im_port =
885 			(struct bfad_im_port_s *) shost->hostdata[0];
886 	struct bfad_s *bfad = im_port->bfad;
887 
888 	return snprintf(buf, PAGE_SIZE, "%d\n",
889 			bfa_get_nports(&bfad->bfa));
890 }
891 
892 static ssize_t
bfad_im_drv_name_show(struct device * dev,struct device_attribute * attr,char * buf)893 bfad_im_drv_name_show(struct device *dev, struct device_attribute *attr,
894 				char *buf)
895 {
896 	return snprintf(buf, PAGE_SIZE, "%s\n", BFAD_DRIVER_NAME);
897 }
898 
899 static ssize_t
bfad_im_num_of_discovered_ports_show(struct device * dev,struct device_attribute * attr,char * buf)900 bfad_im_num_of_discovered_ports_show(struct device *dev,
901 			struct device_attribute *attr, char *buf)
902 {
903 	struct Scsi_Host *shost = class_to_shost(dev);
904 	struct bfad_im_port_s *im_port =
905 			(struct bfad_im_port_s *) shost->hostdata[0];
906 	struct bfad_port_s    *port = im_port->port;
907 	struct bfad_s         *bfad = im_port->bfad;
908 	int        nrports = 2048;
909 	wwn_t          *rports = NULL;
910 	unsigned long   flags;
911 
912 	rports = kzalloc(sizeof(wwn_t) * nrports , GFP_ATOMIC);
913 	if (rports == NULL)
914 		return snprintf(buf, PAGE_SIZE, "Failed\n");
915 
916 	spin_lock_irqsave(&bfad->bfad_lock, flags);
917 	bfa_fcs_lport_get_rports(port->fcs_port, rports, &nrports);
918 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
919 	kfree(rports);
920 
921 	return snprintf(buf, PAGE_SIZE, "%d\n", nrports);
922 }
923 
924 static          DEVICE_ATTR(serial_number, S_IRUGO,
925 				bfad_im_serial_num_show, NULL);
926 static          DEVICE_ATTR(model, S_IRUGO, bfad_im_model_show, NULL);
927 static          DEVICE_ATTR(model_description, S_IRUGO,
928 				bfad_im_model_desc_show, NULL);
929 static          DEVICE_ATTR(node_name, S_IRUGO, bfad_im_node_name_show, NULL);
930 static          DEVICE_ATTR(symbolic_name, S_IRUGO,
931 				bfad_im_symbolic_name_show, NULL);
932 static          DEVICE_ATTR(hardware_version, S_IRUGO,
933 				bfad_im_hw_version_show, NULL);
934 static          DEVICE_ATTR(driver_version, S_IRUGO,
935 				bfad_im_drv_version_show, NULL);
936 static          DEVICE_ATTR(option_rom_version, S_IRUGO,
937 				bfad_im_optionrom_version_show, NULL);
938 static          DEVICE_ATTR(firmware_version, S_IRUGO,
939 				bfad_im_fw_version_show, NULL);
940 static          DEVICE_ATTR(number_of_ports, S_IRUGO,
941 				bfad_im_num_of_ports_show, NULL);
942 static          DEVICE_ATTR(driver_name, S_IRUGO, bfad_im_drv_name_show, NULL);
943 static          DEVICE_ATTR(number_of_discovered_ports, S_IRUGO,
944 				bfad_im_num_of_discovered_ports_show, NULL);
945 
946 struct device_attribute *bfad_im_host_attrs[] = {
947 	&dev_attr_serial_number,
948 	&dev_attr_model,
949 	&dev_attr_model_description,
950 	&dev_attr_node_name,
951 	&dev_attr_symbolic_name,
952 	&dev_attr_hardware_version,
953 	&dev_attr_driver_version,
954 	&dev_attr_option_rom_version,
955 	&dev_attr_firmware_version,
956 	&dev_attr_number_of_ports,
957 	&dev_attr_driver_name,
958 	&dev_attr_number_of_discovered_ports,
959 	NULL,
960 };
961 
962 struct device_attribute *bfad_im_vport_attrs[] = {
963 	&dev_attr_serial_number,
964 	&dev_attr_model,
965 	&dev_attr_model_description,
966 	&dev_attr_node_name,
967 	&dev_attr_symbolic_name,
968 	&dev_attr_hardware_version,
969 	&dev_attr_driver_version,
970 	&dev_attr_option_rom_version,
971 	&dev_attr_firmware_version,
972 	&dev_attr_number_of_ports,
973 	&dev_attr_driver_name,
974 	&dev_attr_number_of_discovered_ports,
975 	NULL,
976 };
977 
978 
979