]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
[SCSI] lpfc 8.3.2 : Update of copyrights
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_nl.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45 /* AlpaArray for assignment of scsid for scan-down and bind_method */
46 static uint8_t lpfcAlpaArray[] = {
47 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60 };
61
62 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
63 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
64
65 void
66 lpfc_terminate_rport_io(struct fc_rport *rport)
67 {
68 struct lpfc_rport_data *rdata;
69 struct lpfc_nodelist * ndlp;
70 struct lpfc_hba *phba;
71
72 rdata = rport->dd_data;
73 ndlp = rdata->pnode;
74
75 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
76 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
77 printk(KERN_ERR "Cannot find remote node"
78 " to terminate I/O Data x%x\n",
79 rport->port_id);
80 return;
81 }
82
83 phba = ndlp->phba;
84
85 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
86 "rport terminate: sid:x%x did:x%x flg:x%x",
87 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
88
89 if (ndlp->nlp_sid != NLP_NO_SID) {
90 lpfc_sli_abort_iocb(ndlp->vport,
91 &phba->sli.ring[phba->sli.fcp_ring],
92 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
93 }
94 }
95
96 /*
97 * This function will be called when dev_loss_tmo fire.
98 */
99 void
100 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
101 {
102 struct lpfc_rport_data *rdata;
103 struct lpfc_nodelist * ndlp;
104 struct lpfc_vport *vport;
105 struct lpfc_hba *phba;
106 struct lpfc_work_evt *evtp;
107 int put_node;
108 int put_rport;
109
110 rdata = rport->dd_data;
111 ndlp = rdata->pnode;
112 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
113 return;
114
115 vport = ndlp->vport;
116 phba = vport->phba;
117
118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
119 "rport devlosscb: sid:x%x did:x%x flg:x%x",
120 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
121
122 /* Don't defer this if we are in the process of deleting the vport
123 * or unloading the driver. The unload will cleanup the node
124 * appropriately we just need to cleanup the ndlp rport info here.
125 */
126 if (vport->load_flag & FC_UNLOADING) {
127 put_node = rdata->pnode != NULL;
128 put_rport = ndlp->rport != NULL;
129 rdata->pnode = NULL;
130 ndlp->rport = NULL;
131 if (put_node)
132 lpfc_nlp_put(ndlp);
133 if (put_rport)
134 put_device(&rport->dev);
135 return;
136 }
137
138 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
139 return;
140
141 evtp = &ndlp->dev_loss_evt;
142
143 if (!list_empty(&evtp->evt_listp))
144 return;
145
146 spin_lock_irq(&phba->hbalock);
147 /* We need to hold the node by incrementing the reference
148 * count until this queued work is done
149 */
150 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
151 if (evtp->evt_arg1) {
152 evtp->evt = LPFC_EVT_DEV_LOSS;
153 list_add_tail(&evtp->evt_listp, &phba->work_list);
154 lpfc_worker_wake_up(phba);
155 }
156 spin_unlock_irq(&phba->hbalock);
157
158 return;
159 }
160
161 /*
162 * This function is called from the worker thread when dev_loss_tmo
163 * expire.
164 */
165 static void
166 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
167 {
168 struct lpfc_rport_data *rdata;
169 struct fc_rport *rport;
170 struct lpfc_vport *vport;
171 struct lpfc_hba *phba;
172 uint8_t *name;
173 int put_node;
174 int put_rport;
175 int warn_on = 0;
176
177 rport = ndlp->rport;
178
179 if (!rport)
180 return;
181
182 rdata = rport->dd_data;
183 name = (uint8_t *) &ndlp->nlp_portname;
184 vport = ndlp->vport;
185 phba = vport->phba;
186
187 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
188 "rport devlosstmo:did:x%x type:x%x id:x%x",
189 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
190
191 /* Don't defer this if we are in the process of deleting the vport
192 * or unloading the driver. The unload will cleanup the node
193 * appropriately we just need to cleanup the ndlp rport info here.
194 */
195 if (vport->load_flag & FC_UNLOADING) {
196 if (ndlp->nlp_sid != NLP_NO_SID) {
197 /* flush the target */
198 lpfc_sli_abort_iocb(vport,
199 &phba->sli.ring[phba->sli.fcp_ring],
200 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
201 }
202 put_node = rdata->pnode != NULL;
203 put_rport = ndlp->rport != NULL;
204 rdata->pnode = NULL;
205 ndlp->rport = NULL;
206 if (put_node)
207 lpfc_nlp_put(ndlp);
208 if (put_rport)
209 put_device(&rport->dev);
210 return;
211 }
212
213 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
214 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
215 "0284 Devloss timeout Ignored on "
216 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
217 "NPort x%x\n",
218 *name, *(name+1), *(name+2), *(name+3),
219 *(name+4), *(name+5), *(name+6), *(name+7),
220 ndlp->nlp_DID);
221 return;
222 }
223
224 if (ndlp->nlp_type & NLP_FABRIC) {
225 /* We will clean up these Nodes in linkup */
226 put_node = rdata->pnode != NULL;
227 put_rport = ndlp->rport != NULL;
228 rdata->pnode = NULL;
229 ndlp->rport = NULL;
230 if (put_node)
231 lpfc_nlp_put(ndlp);
232 if (put_rport)
233 put_device(&rport->dev);
234 return;
235 }
236
237 if (ndlp->nlp_sid != NLP_NO_SID) {
238 warn_on = 1;
239 /* flush the target */
240 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
241 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
242 }
243
244 if (warn_on) {
245 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
246 "0203 Devloss timeout on "
247 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
248 "NPort x%06x Data: x%x x%x x%x\n",
249 *name, *(name+1), *(name+2), *(name+3),
250 *(name+4), *(name+5), *(name+6), *(name+7),
251 ndlp->nlp_DID, ndlp->nlp_flag,
252 ndlp->nlp_state, ndlp->nlp_rpi);
253 } else {
254 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
255 "0204 Devloss timeout on "
256 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
257 "NPort x%06x Data: x%x x%x x%x\n",
258 *name, *(name+1), *(name+2), *(name+3),
259 *(name+4), *(name+5), *(name+6), *(name+7),
260 ndlp->nlp_DID, ndlp->nlp_flag,
261 ndlp->nlp_state, ndlp->nlp_rpi);
262 }
263
264 put_node = rdata->pnode != NULL;
265 put_rport = ndlp->rport != NULL;
266 rdata->pnode = NULL;
267 ndlp->rport = NULL;
268 if (put_node)
269 lpfc_nlp_put(ndlp);
270 if (put_rport)
271 put_device(&rport->dev);
272
273 if (!(vport->load_flag & FC_UNLOADING) &&
274 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
275 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
276 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
277 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
278
279 lpfc_unregister_unused_fcf(phba);
280 }
281
282 /**
283 * lpfc_alloc_fast_evt - Allocates data structure for posting event
284 * @phba: Pointer to hba context object.
285 *
286 * This function is called from the functions which need to post
287 * events from interrupt context. This function allocates data
288 * structure required for posting event. It also keeps track of
289 * number of events pending and prevent event storm when there are
290 * too many events.
291 **/
292 struct lpfc_fast_path_event *
293 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
294 struct lpfc_fast_path_event *ret;
295
296 /* If there are lot of fast event do not exhaust memory due to this */
297 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
298 return NULL;
299
300 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
301 GFP_ATOMIC);
302 if (ret) {
303 atomic_inc(&phba->fast_event_count);
304 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
305 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
306 }
307 return ret;
308 }
309
310 /**
311 * lpfc_free_fast_evt - Frees event data structure
312 * @phba: Pointer to hba context object.
313 * @evt: Event object which need to be freed.
314 *
315 * This function frees the data structure required for posting
316 * events.
317 **/
318 void
319 lpfc_free_fast_evt(struct lpfc_hba *phba,
320 struct lpfc_fast_path_event *evt) {
321
322 atomic_dec(&phba->fast_event_count);
323 kfree(evt);
324 }
325
326 /**
327 * lpfc_send_fastpath_evt - Posts events generated from fast path
328 * @phba: Pointer to hba context object.
329 * @evtp: Event data structure.
330 *
331 * This function is called from worker thread, when the interrupt
332 * context need to post an event. This function posts the event
333 * to fc transport netlink interface.
334 **/
335 static void
336 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
337 struct lpfc_work_evt *evtp)
338 {
339 unsigned long evt_category, evt_sub_category;
340 struct lpfc_fast_path_event *fast_evt_data;
341 char *evt_data;
342 uint32_t evt_data_size;
343 struct Scsi_Host *shost;
344
345 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
346 work_evt);
347
348 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
349 evt_sub_category = (unsigned long) fast_evt_data->un.
350 fabric_evt.subcategory;
351 shost = lpfc_shost_from_vport(fast_evt_data->vport);
352 if (evt_category == FC_REG_FABRIC_EVENT) {
353 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
354 evt_data = (char *) &fast_evt_data->un.read_check_error;
355 evt_data_size = sizeof(fast_evt_data->un.
356 read_check_error);
357 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
358 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
359 evt_data = (char *) &fast_evt_data->un.fabric_evt;
360 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
361 } else {
362 lpfc_free_fast_evt(phba, fast_evt_data);
363 return;
364 }
365 } else if (evt_category == FC_REG_SCSI_EVENT) {
366 switch (evt_sub_category) {
367 case LPFC_EVENT_QFULL:
368 case LPFC_EVENT_DEVBSY:
369 evt_data = (char *) &fast_evt_data->un.scsi_evt;
370 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
371 break;
372 case LPFC_EVENT_CHECK_COND:
373 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
374 evt_data_size = sizeof(fast_evt_data->un.
375 check_cond_evt);
376 break;
377 case LPFC_EVENT_VARQUEDEPTH:
378 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
379 evt_data_size = sizeof(fast_evt_data->un.
380 queue_depth_evt);
381 break;
382 default:
383 lpfc_free_fast_evt(phba, fast_evt_data);
384 return;
385 }
386 } else {
387 lpfc_free_fast_evt(phba, fast_evt_data);
388 return;
389 }
390
391 fc_host_post_vendor_event(shost,
392 fc_get_event_number(),
393 evt_data_size,
394 evt_data,
395 LPFC_NL_VENDOR_ID);
396
397 lpfc_free_fast_evt(phba, fast_evt_data);
398 return;
399 }
400
401 static void
402 lpfc_work_list_done(struct lpfc_hba *phba)
403 {
404 struct lpfc_work_evt *evtp = NULL;
405 struct lpfc_nodelist *ndlp;
406 int free_evt;
407
408 spin_lock_irq(&phba->hbalock);
409 while (!list_empty(&phba->work_list)) {
410 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
411 evt_listp);
412 spin_unlock_irq(&phba->hbalock);
413 free_evt = 1;
414 switch (evtp->evt) {
415 case LPFC_EVT_ELS_RETRY:
416 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
417 lpfc_els_retry_delay_handler(ndlp);
418 free_evt = 0; /* evt is part of ndlp */
419 /* decrement the node reference count held
420 * for this queued work
421 */
422 lpfc_nlp_put(ndlp);
423 break;
424 case LPFC_EVT_DEV_LOSS:
425 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
426 lpfc_dev_loss_tmo_handler(ndlp);
427 free_evt = 0;
428 /* decrement the node reference count held for
429 * this queued work
430 */
431 lpfc_nlp_put(ndlp);
432 break;
433 case LPFC_EVT_ONLINE:
434 if (phba->link_state < LPFC_LINK_DOWN)
435 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
436 else
437 *(int *) (evtp->evt_arg1) = 0;
438 complete((struct completion *)(evtp->evt_arg2));
439 break;
440 case LPFC_EVT_OFFLINE_PREP:
441 if (phba->link_state >= LPFC_LINK_DOWN)
442 lpfc_offline_prep(phba);
443 *(int *)(evtp->evt_arg1) = 0;
444 complete((struct completion *)(evtp->evt_arg2));
445 break;
446 case LPFC_EVT_OFFLINE:
447 lpfc_offline(phba);
448 lpfc_sli_brdrestart(phba);
449 *(int *)(evtp->evt_arg1) =
450 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
451 lpfc_unblock_mgmt_io(phba);
452 complete((struct completion *)(evtp->evt_arg2));
453 break;
454 case LPFC_EVT_WARM_START:
455 lpfc_offline(phba);
456 lpfc_reset_barrier(phba);
457 lpfc_sli_brdreset(phba);
458 lpfc_hba_down_post(phba);
459 *(int *)(evtp->evt_arg1) =
460 lpfc_sli_brdready(phba, HS_MBRDY);
461 lpfc_unblock_mgmt_io(phba);
462 complete((struct completion *)(evtp->evt_arg2));
463 break;
464 case LPFC_EVT_KILL:
465 lpfc_offline(phba);
466 *(int *)(evtp->evt_arg1)
467 = (phba->pport->stopped)
468 ? 0 : lpfc_sli_brdkill(phba);
469 lpfc_unblock_mgmt_io(phba);
470 complete((struct completion *)(evtp->evt_arg2));
471 break;
472 case LPFC_EVT_FASTPATH_MGMT_EVT:
473 lpfc_send_fastpath_evt(phba, evtp);
474 free_evt = 0;
475 break;
476 }
477 if (free_evt)
478 kfree(evtp);
479 spin_lock_irq(&phba->hbalock);
480 }
481 spin_unlock_irq(&phba->hbalock);
482
483 }
484
485 static void
486 lpfc_work_done(struct lpfc_hba *phba)
487 {
488 struct lpfc_sli_ring *pring;
489 uint32_t ha_copy, status, control, work_port_events;
490 struct lpfc_vport **vports;
491 struct lpfc_vport *vport;
492 int i;
493
494 spin_lock_irq(&phba->hbalock);
495 ha_copy = phba->work_ha;
496 phba->work_ha = 0;
497 spin_unlock_irq(&phba->hbalock);
498
499 /* First, try to post the next mailbox command to SLI4 device */
500 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
501 lpfc_sli4_post_async_mbox(phba);
502
503 if (ha_copy & HA_ERATT)
504 /* Handle the error attention event */
505 lpfc_handle_eratt(phba);
506
507 if (ha_copy & HA_MBATT)
508 lpfc_sli_handle_mb_event(phba);
509
510 if (ha_copy & HA_LATT)
511 lpfc_handle_latt(phba);
512
513 /* Process SLI4 events */
514 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
515 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
516 lpfc_sli4_fcp_xri_abort_event_proc(phba);
517 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
518 lpfc_sli4_els_xri_abort_event_proc(phba);
519 if (phba->hba_flag & ASYNC_EVENT)
520 lpfc_sli4_async_event_proc(phba);
521 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
522 spin_lock_irq(&phba->hbalock);
523 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
524 spin_unlock_irq(&phba->hbalock);
525 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
526 }
527 if (phba->hba_flag & HBA_RECEIVE_BUFFER)
528 lpfc_sli4_handle_received_buffer(phba);
529 }
530
531 vports = lpfc_create_vport_work_array(phba);
532 if (vports != NULL)
533 for (i = 0; i <= phba->max_vports; i++) {
534 /*
535 * We could have no vports in array if unloading, so if
536 * this happens then just use the pport
537 */
538 if (vports[i] == NULL && i == 0)
539 vport = phba->pport;
540 else
541 vport = vports[i];
542 if (vport == NULL)
543 break;
544 spin_lock_irq(&vport->work_port_lock);
545 work_port_events = vport->work_port_events;
546 vport->work_port_events &= ~work_port_events;
547 spin_unlock_irq(&vport->work_port_lock);
548 if (work_port_events & WORKER_DISC_TMO)
549 lpfc_disc_timeout_handler(vport);
550 if (work_port_events & WORKER_ELS_TMO)
551 lpfc_els_timeout_handler(vport);
552 if (work_port_events & WORKER_HB_TMO)
553 lpfc_hb_timeout_handler(phba);
554 if (work_port_events & WORKER_MBOX_TMO)
555 lpfc_mbox_timeout_handler(phba);
556 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
557 lpfc_unblock_fabric_iocbs(phba);
558 if (work_port_events & WORKER_FDMI_TMO)
559 lpfc_fdmi_timeout_handler(vport);
560 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
561 lpfc_ramp_down_queue_handler(phba);
562 if (work_port_events & WORKER_RAMP_UP_QUEUE)
563 lpfc_ramp_up_queue_handler(phba);
564 }
565 lpfc_destroy_vport_work_array(phba, vports);
566
567 pring = &phba->sli.ring[LPFC_ELS_RING];
568 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
569 status >>= (4*LPFC_ELS_RING);
570 if ((status & HA_RXMASK)
571 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
572 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
573 pring->flag |= LPFC_DEFERRED_RING_EVENT;
574 /* Set the lpfc data pending flag */
575 set_bit(LPFC_DATA_READY, &phba->data_flags);
576 } else {
577 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
578 lpfc_sli_handle_slow_ring_event(phba, pring,
579 (status &
580 HA_RXMASK));
581 }
582 /*
583 * Turn on Ring interrupts
584 */
585 if (phba->sli_rev <= LPFC_SLI_REV3) {
586 spin_lock_irq(&phba->hbalock);
587 control = readl(phba->HCregaddr);
588 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589 lpfc_debugfs_slow_ring_trc(phba,
590 "WRK Enable ring: cntl:x%x hacopy:x%x",
591 control, ha_copy, 0);
592
593 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594 writel(control, phba->HCregaddr);
595 readl(phba->HCregaddr); /* flush */
596 } else {
597 lpfc_debugfs_slow_ring_trc(phba,
598 "WRK Ring ok: cntl:x%x hacopy:x%x",
599 control, ha_copy, 0);
600 }
601 spin_unlock_irq(&phba->hbalock);
602 }
603 }
604 lpfc_work_list_done(phba);
605 }
606
607 int
608 lpfc_do_work(void *p)
609 {
610 struct lpfc_hba *phba = p;
611 int rc;
612
613 set_user_nice(current, -20);
614 phba->data_flags = 0;
615
616 while (!kthread_should_stop()) {
617 /* wait and check worker queue activities */
618 rc = wait_event_interruptible(phba->work_waitq,
619 (test_and_clear_bit(LPFC_DATA_READY,
620 &phba->data_flags)
621 || kthread_should_stop()));
622 /* Signal wakeup shall terminate the worker thread */
623 if (rc) {
624 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625 "0433 Wakeup on signal: rc=x%x\n", rc);
626 break;
627 }
628
629 /* Attend pending lpfc data processing */
630 lpfc_work_done(phba);
631 }
632 phba->worker_thread = NULL;
633 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634 "0432 Worker thread stopped.\n");
635 return 0;
636 }
637
638 /*
639 * This is only called to handle FC worker events. Since this a rare
640 * occurance, we allocate a struct lpfc_work_evt structure here instead of
641 * embedding it in the IOCB.
642 */
643 int
644 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
645 uint32_t evt)
646 {
647 struct lpfc_work_evt *evtp;
648 unsigned long flags;
649
650 /*
651 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652 * be queued to worker thread for processing
653 */
654 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
655 if (!evtp)
656 return 0;
657
658 evtp->evt_arg1 = arg1;
659 evtp->evt_arg2 = arg2;
660 evtp->evt = evt;
661
662 spin_lock_irqsave(&phba->hbalock, flags);
663 list_add_tail(&evtp->evt_listp, &phba->work_list);
664 spin_unlock_irqrestore(&phba->hbalock, flags);
665
666 lpfc_worker_wake_up(phba);
667
668 return 1;
669 }
670
671 void
672 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673 {
674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
675 struct lpfc_hba *phba = vport->phba;
676 struct lpfc_nodelist *ndlp, *next_ndlp;
677 int rc;
678
679 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
680 if (!NLP_CHK_NODE_ACT(ndlp))
681 continue;
682 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683 continue;
684 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685 ((vport->port_type == LPFC_NPIV_PORT) &&
686 (ndlp->nlp_DID == NameServer_DID)))
687 lpfc_unreg_rpi(vport, ndlp);
688
689 /* Leave Fabric nodes alone on link down */
690 if (!remove && ndlp->nlp_type & NLP_FABRIC)
691 continue;
692 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
693 remove
694 ? NLP_EVT_DEVICE_RM
695 : NLP_EVT_DEVICE_RECOVERY);
696 }
697 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
698 lpfc_mbx_unreg_vpi(vport);
699 spin_lock_irq(shost->host_lock);
700 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
701 spin_unlock_irq(shost->host_lock);
702 }
703 }
704
705 void
706 lpfc_port_link_failure(struct lpfc_vport *vport)
707 {
708 /* Cleanup any outstanding RSCN activity */
709 lpfc_els_flush_rscn(vport);
710
711 /* Cleanup any outstanding ELS commands */
712 lpfc_els_flush_cmd(vport);
713
714 lpfc_cleanup_rpis(vport, 0);
715
716 /* Turn off discovery timer if its running */
717 lpfc_can_disctmo(vport);
718 }
719
720 void
721 lpfc_linkdown_port(struct lpfc_vport *vport)
722 {
723 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
724
725 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
726
727 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
728 "Link Down: state:x%x rtry:x%x flg:x%x",
729 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
730
731 lpfc_port_link_failure(vport);
732
733 }
734
735 int
736 lpfc_linkdown(struct lpfc_hba *phba)
737 {
738 struct lpfc_vport *vport = phba->pport;
739 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
740 struct lpfc_vport **vports;
741 LPFC_MBOXQ_t *mb;
742 int i;
743
744 if (phba->link_state == LPFC_LINK_DOWN)
745 return 0;
746 spin_lock_irq(&phba->hbalock);
747 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
748 if (phba->link_state > LPFC_LINK_DOWN) {
749 phba->link_state = LPFC_LINK_DOWN;
750 phba->pport->fc_flag &= ~FC_LBIT;
751 }
752 spin_unlock_irq(&phba->hbalock);
753 vports = lpfc_create_vport_work_array(phba);
754 if (vports != NULL)
755 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
756 /* Issue a LINK DOWN event to all nodes */
757 lpfc_linkdown_port(vports[i]);
758 }
759 lpfc_destroy_vport_work_array(phba, vports);
760 /* Clean up any firmware default rpi's */
761 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
762 if (mb) {
763 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
764 mb->vport = vport;
765 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
766 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
767 == MBX_NOT_FINISHED) {
768 mempool_free(mb, phba->mbox_mem_pool);
769 }
770 }
771
772 /* Setup myDID for link up if we are in pt2pt mode */
773 if (phba->pport->fc_flag & FC_PT2PT) {
774 phba->pport->fc_myDID = 0;
775 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
776 if (mb) {
777 lpfc_config_link(phba, mb);
778 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
779 mb->vport = vport;
780 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
781 == MBX_NOT_FINISHED) {
782 mempool_free(mb, phba->mbox_mem_pool);
783 }
784 }
785 spin_lock_irq(shost->host_lock);
786 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
787 spin_unlock_irq(shost->host_lock);
788 }
789
790 return 0;
791 }
792
793 static void
794 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
795 {
796 struct lpfc_nodelist *ndlp;
797
798 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
799 if (!NLP_CHK_NODE_ACT(ndlp))
800 continue;
801 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
802 continue;
803 if (ndlp->nlp_type & NLP_FABRIC) {
804 /* On Linkup its safe to clean up the ndlp
805 * from Fabric connections.
806 */
807 if (ndlp->nlp_DID != Fabric_DID)
808 lpfc_unreg_rpi(vport, ndlp);
809 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
810 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
811 /* Fail outstanding IO now since device is
812 * marked for PLOGI.
813 */
814 lpfc_unreg_rpi(vport, ndlp);
815 }
816 }
817 }
818
819 static void
820 lpfc_linkup_port(struct lpfc_vport *vport)
821 {
822 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
823 struct lpfc_hba *phba = vport->phba;
824
825 if ((vport->load_flag & FC_UNLOADING) != 0)
826 return;
827
828 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
829 "Link Up: top:x%x speed:x%x flg:x%x",
830 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
831
832 /* If NPIV is not enabled, only bring the physical port up */
833 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
834 (vport != phba->pport))
835 return;
836
837 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
838
839 spin_lock_irq(shost->host_lock);
840 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
841 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
842 vport->fc_flag |= FC_NDISC_ACTIVE;
843 vport->fc_ns_retry = 0;
844 spin_unlock_irq(shost->host_lock);
845
846 if (vport->fc_flag & FC_LBIT)
847 lpfc_linkup_cleanup_nodes(vport);
848
849 }
850
851 static int
852 lpfc_linkup(struct lpfc_hba *phba)
853 {
854 struct lpfc_vport **vports;
855 int i;
856
857 phba->link_state = LPFC_LINK_UP;
858
859 /* Unblock fabric iocbs if they are blocked */
860 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
861 del_timer_sync(&phba->fabric_block_timer);
862
863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866 lpfc_linkup_port(vports[i]);
867 lpfc_destroy_vport_work_array(phba, vports);
868 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
869 (phba->sli_rev < LPFC_SLI_REV4))
870 lpfc_issue_clear_la(phba, phba->pport);
871
872 return 0;
873 }
874
875 /*
876 * This routine handles processing a CLEAR_LA mailbox
877 * command upon completion. It is setup in the LPFC_MBOXQ
878 * as the completion routine when the command is
879 * handed off to the SLI layer.
880 */
881 static void
882 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
883 {
884 struct lpfc_vport *vport = pmb->vport;
885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
886 struct lpfc_sli *psli = &phba->sli;
887 MAILBOX_t *mb = &pmb->u.mb;
888 uint32_t control;
889
890 /* Since we don't do discovery right now, turn these off here */
891 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
892 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
893 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
894
895 /* Check for error */
896 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
897 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
898 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
899 "0320 CLEAR_LA mbxStatus error x%x hba "
900 "state x%x\n",
901 mb->mbxStatus, vport->port_state);
902 phba->link_state = LPFC_HBA_ERROR;
903 goto out;
904 }
905
906 if (vport->port_type == LPFC_PHYSICAL_PORT)
907 phba->link_state = LPFC_HBA_READY;
908
909 spin_lock_irq(&phba->hbalock);
910 psli->sli_flag |= LPFC_PROCESS_LA;
911 control = readl(phba->HCregaddr);
912 control |= HC_LAINT_ENA;
913 writel(control, phba->HCregaddr);
914 readl(phba->HCregaddr); /* flush */
915 spin_unlock_irq(&phba->hbalock);
916 mempool_free(pmb, phba->mbox_mem_pool);
917 return;
918
919 out:
920 /* Device Discovery completes */
921 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
922 "0225 Device Discovery completes\n");
923 mempool_free(pmb, phba->mbox_mem_pool);
924
925 spin_lock_irq(shost->host_lock);
926 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
927 spin_unlock_irq(shost->host_lock);
928
929 lpfc_can_disctmo(vport);
930
931 /* turn on Link Attention interrupts */
932
933 spin_lock_irq(&phba->hbalock);
934 psli->sli_flag |= LPFC_PROCESS_LA;
935 control = readl(phba->HCregaddr);
936 control |= HC_LAINT_ENA;
937 writel(control, phba->HCregaddr);
938 readl(phba->HCregaddr); /* flush */
939 spin_unlock_irq(&phba->hbalock);
940
941 return;
942 }
943
944
945 static void
946 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
947 {
948 struct lpfc_vport *vport = pmb->vport;
949
950 if (pmb->u.mb.mbxStatus)
951 goto out;
952
953 mempool_free(pmb, phba->mbox_mem_pool);
954
955 if (phba->fc_topology == TOPOLOGY_LOOP &&
956 vport->fc_flag & FC_PUBLIC_LOOP &&
957 !(vport->fc_flag & FC_LBIT)) {
958 /* Need to wait for FAN - use discovery timer
959 * for timeout. port_state is identically
960 * LPFC_LOCAL_CFG_LINK while waiting for FAN
961 */
962 lpfc_set_disctmo(vport);
963 return;
964 }
965
966 /* Start discovery by sending a FLOGI. port_state is identically
967 * LPFC_FLOGI while waiting for FLOGI cmpl
968 */
969 if (vport->port_state != LPFC_FLOGI) {
970 lpfc_initial_flogi(vport);
971 }
972 return;
973
974 out:
975 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
976 "0306 CONFIG_LINK mbxStatus error x%x "
977 "HBA state x%x\n",
978 pmb->u.mb.mbxStatus, vport->port_state);
979 mempool_free(pmb, phba->mbox_mem_pool);
980
981 lpfc_linkdown(phba);
982
983 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
984 "0200 CONFIG_LINK bad hba state x%x\n",
985 vport->port_state);
986
987 lpfc_issue_clear_la(phba, vport);
988 return;
989 }
990
991 static void
992 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
993 {
994 struct lpfc_vport *vport = mboxq->vport;
995 unsigned long flags;
996
997 if (mboxq->u.mb.mbxStatus) {
998 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
999 "2017 REG_FCFI mbxStatus error x%x "
1000 "HBA state x%x\n",
1001 mboxq->u.mb.mbxStatus, vport->port_state);
1002 mempool_free(mboxq, phba->mbox_mem_pool);
1003 return;
1004 }
1005
1006 /* Start FCoE discovery by sending a FLOGI. */
1007 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1008 /* Set the FCFI registered flag */
1009 spin_lock_irqsave(&phba->hbalock, flags);
1010 phba->fcf.fcf_flag |= FCF_REGISTERED;
1011 spin_unlock_irqrestore(&phba->hbalock, flags);
1012 if (vport->port_state != LPFC_FLOGI) {
1013 spin_lock_irqsave(&phba->hbalock, flags);
1014 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1015 spin_unlock_irqrestore(&phba->hbalock, flags);
1016 lpfc_initial_flogi(vport);
1017 }
1018
1019 mempool_free(mboxq, phba->mbox_mem_pool);
1020 return;
1021 }
1022
1023 /**
1024 * lpfc_fab_name_match - Check if the fcf fabric name match.
1025 * @fab_name: pointer to fabric name.
1026 * @new_fcf_record: pointer to fcf record.
1027 *
1028 * This routine compare the fcf record's fabric name with provided
1029 * fabric name. If the fabric name are identical this function
1030 * returns 1 else return 0.
1031 **/
1032 static uint32_t
1033 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1034 {
1035 if ((fab_name[0] ==
1036 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1037 (fab_name[1] ==
1038 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1039 (fab_name[2] ==
1040 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1041 (fab_name[3] ==
1042 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1043 (fab_name[4] ==
1044 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1045 (fab_name[5] ==
1046 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1047 (fab_name[6] ==
1048 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1049 (fab_name[7] ==
1050 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1051 return 1;
1052 else
1053 return 0;
1054 }
1055
1056 /**
1057 * lpfc_mac_addr_match - Check if the fcf mac address match.
1058 * @phba: pointer to lpfc hba data structure.
1059 * @new_fcf_record: pointer to fcf record.
1060 *
1061 * This routine compare the fcf record's mac address with HBA's
1062 * FCF mac address. If the mac addresses are identical this function
1063 * returns 1 else return 0.
1064 **/
1065 static uint32_t
1066 lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1067 {
1068 if ((phba->fcf.mac_addr[0] ==
1069 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1070 (phba->fcf.mac_addr[1] ==
1071 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1072 (phba->fcf.mac_addr[2] ==
1073 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1074 (phba->fcf.mac_addr[3] ==
1075 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1076 (phba->fcf.mac_addr[4] ==
1077 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1078 (phba->fcf.mac_addr[5] ==
1079 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1080 return 1;
1081 else
1082 return 0;
1083 }
1084
1085 /**
1086 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1087 * @phba: pointer to lpfc hba data structure.
1088 * @new_fcf_record: pointer to fcf record.
1089 *
1090 * This routine copies the FCF information from the FCF
1091 * record to lpfc_hba data structure.
1092 **/
1093 static void
1094 lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1095 {
1096 phba->fcf.fabric_name[0] =
1097 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1098 phba->fcf.fabric_name[1] =
1099 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1100 phba->fcf.fabric_name[2] =
1101 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1102 phba->fcf.fabric_name[3] =
1103 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1104 phba->fcf.fabric_name[4] =
1105 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1106 phba->fcf.fabric_name[5] =
1107 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1108 phba->fcf.fabric_name[6] =
1109 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1110 phba->fcf.fabric_name[7] =
1111 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1112 phba->fcf.mac_addr[0] =
1113 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1114 phba->fcf.mac_addr[1] =
1115 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1116 phba->fcf.mac_addr[2] =
1117 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1118 phba->fcf.mac_addr[3] =
1119 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1120 phba->fcf.mac_addr[4] =
1121 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1122 phba->fcf.mac_addr[5] =
1123 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1124 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1125 phba->fcf.priority = new_fcf_record->fip_priority;
1126 }
1127
1128 /**
1129 * lpfc_register_fcf - Register the FCF with hba.
1130 * @phba: pointer to lpfc hba data structure.
1131 *
1132 * This routine issues a register fcfi mailbox command to register
1133 * the fcf with HBA.
1134 **/
1135 static void
1136 lpfc_register_fcf(struct lpfc_hba *phba)
1137 {
1138 LPFC_MBOXQ_t *fcf_mbxq;
1139 int rc;
1140 unsigned long flags;
1141
1142 spin_lock_irqsave(&phba->hbalock, flags);
1143
1144 /* If the FCF is not availabe do nothing. */
1145 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1146 spin_unlock_irqrestore(&phba->hbalock, flags);
1147 return;
1148 }
1149
1150 /* The FCF is already registered, start discovery */
1151 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1152 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1153 spin_unlock_irqrestore(&phba->hbalock, flags);
1154 if (phba->pport->port_state != LPFC_FLOGI)
1155 lpfc_initial_flogi(phba->pport);
1156 return;
1157 }
1158 spin_unlock_irqrestore(&phba->hbalock, flags);
1159
1160 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1161 GFP_KERNEL);
1162 if (!fcf_mbxq)
1163 return;
1164
1165 lpfc_reg_fcfi(phba, fcf_mbxq);
1166 fcf_mbxq->vport = phba->pport;
1167 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1168 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1169 if (rc == MBX_NOT_FINISHED)
1170 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1171
1172 return;
1173 }
1174
1175 /**
1176 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1177 * @phba: pointer to lpfc hba data structure.
1178 * @new_fcf_record: pointer to fcf record.
1179 * @boot_flag: Indicates if this record used by boot bios.
1180 * @addr_mode: The address mode to be used by this FCF
1181 *
1182 * This routine compare the fcf record with connect list obtained from the
1183 * config region to decide if this FCF can be used for SAN discovery. It returns
1184 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1185 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1186 * is used by boot bios and addr_mode will indicate the addressing mode to be
1187 * used for this FCF when the function returns.
1188 * If the FCF record need to be used with a particular vlan id, the vlan is
1189 * set in the vlan_id on return of the function. If not VLAN tagging need to
1190 * be used with the FCF vlan_id will be set to 0xFFFF;
1191 **/
1192 static int
1193 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1194 struct fcf_record *new_fcf_record,
1195 uint32_t *boot_flag, uint32_t *addr_mode,
1196 uint16_t *vlan_id)
1197 {
1198 struct lpfc_fcf_conn_entry *conn_entry;
1199
1200 if (!phba->cfg_enable_fip) {
1201 *boot_flag = 0;
1202 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1203 new_fcf_record);
1204 if (phba->valid_vlan)
1205 *vlan_id = phba->vlan_id;
1206 else
1207 *vlan_id = 0xFFFF;
1208 return 1;
1209 }
1210
1211 /*
1212 * If there are no FCF connection table entry, driver connect to all
1213 * FCFs.
1214 */
1215 if (list_empty(&phba->fcf_conn_rec_list)) {
1216 *boot_flag = 0;
1217 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1218 new_fcf_record);
1219 *vlan_id = 0xFFFF;
1220 return 1;
1221 }
1222
1223 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1224 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1225 continue;
1226
1227 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1228 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1229 new_fcf_record))
1230 continue;
1231
1232 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1233 /*
1234 * If the vlan bit map does not have the bit set for the
1235 * vlan id to be used, then it is not a match.
1236 */
1237 if (!(new_fcf_record->vlan_bitmap
1238 [conn_entry->conn_rec.vlan_tag / 8] &
1239 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1240 continue;
1241 }
1242
1243 /*
1244 * Check if the connection record specifies a required
1245 * addressing mode.
1246 */
1247 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1248 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1249
1250 /*
1251 * If SPMA required but FCF not support this continue.
1252 */
1253 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1254 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1255 new_fcf_record) & LPFC_FCF_SPMA))
1256 continue;
1257
1258 /*
1259 * If FPMA required but FCF not support this continue.
1260 */
1261 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1262 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1263 new_fcf_record) & LPFC_FCF_FPMA))
1264 continue;
1265 }
1266
1267 /*
1268 * This fcf record matches filtering criteria.
1269 */
1270 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1271 *boot_flag = 1;
1272 else
1273 *boot_flag = 0;
1274
1275 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1276 new_fcf_record);
1277 /*
1278 * If the user specified a required address mode, assign that
1279 * address mode
1280 */
1281 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1282 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1283 *addr_mode = (conn_entry->conn_rec.flags &
1284 FCFCNCT_AM_SPMA) ?
1285 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1286 /*
1287 * If the user specified a prefered address mode, use the
1288 * addr mode only if FCF support the addr_mode.
1289 */
1290 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1291 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1292 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1293 (*addr_mode & LPFC_FCF_SPMA))
1294 *addr_mode = LPFC_FCF_SPMA;
1295 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1296 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1297 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1298 (*addr_mode & LPFC_FCF_FPMA))
1299 *addr_mode = LPFC_FCF_FPMA;
1300 /*
1301 * If user did not specify any addressing mode, use FPMA if
1302 * possible else use SPMA.
1303 */
1304 else if (*addr_mode & LPFC_FCF_FPMA)
1305 *addr_mode = LPFC_FCF_FPMA;
1306
1307 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1308 *vlan_id = conn_entry->conn_rec.vlan_tag;
1309 else
1310 *vlan_id = 0xFFFF;
1311
1312 return 1;
1313 }
1314
1315 return 0;
1316 }
1317
1318 /**
1319 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1320 * @phba: pointer to lpfc hba data structure.
1321 * @mboxq: pointer to mailbox object.
1322 *
1323 * This function iterate through all the fcf records available in
1324 * HBA and choose the optimal FCF record for discovery. After finding
1325 * the FCF for discovery it register the FCF record and kick start
1326 * discovery.
1327 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1328 * use a FCF record which match fabric name and mac address of the
1329 * currently used FCF record.
1330 * If the driver support only one FCF, it will try to use the FCF record
1331 * used by BOOT_BIOS.
1332 */
1333 void
1334 lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1335 {
1336 void *virt_addr;
1337 dma_addr_t phys_addr;
1338 uint8_t *bytep;
1339 struct lpfc_mbx_sge sge;
1340 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1341 uint32_t shdr_status, shdr_add_status;
1342 union lpfc_sli4_cfg_shdr *shdr;
1343 struct fcf_record *new_fcf_record;
1344 int rc;
1345 uint32_t boot_flag, addr_mode;
1346 uint32_t next_fcf_index;
1347 unsigned long flags;
1348 uint16_t vlan_id;
1349
1350 /* Get the first SGE entry from the non-embedded DMA memory. This
1351 * routine only uses a single SGE.
1352 */
1353 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1354 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1355 if (unlikely(!mboxq->sge_array)) {
1356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1357 "2524 Failed to get the non-embedded SGE "
1358 "virtual address\n");
1359 goto out;
1360 }
1361 virt_addr = mboxq->sge_array->addr[0];
1362
1363 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1366 &shdr->response);
1367 /*
1368 * The FCF Record was read and there is no reason for the driver
1369 * to maintain the FCF record data or memory. Instead, just need
1370 * to book keeping the FCFIs can be used.
1371 */
1372 if (shdr_status || shdr_add_status) {
1373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1374 "2521 READ_FCF_RECORD mailbox failed "
1375 "with status x%x add_status x%x, mbx\n",
1376 shdr_status, shdr_add_status);
1377 goto out;
1378 }
1379 /* Interpreting the returned information of FCF records */
1380 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1381 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1382 sizeof(struct lpfc_mbx_read_fcf_tbl));
1383 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1384
1385 new_fcf_record = (struct fcf_record *)(virt_addr +
1386 sizeof(struct lpfc_mbx_read_fcf_tbl));
1387 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1388 sizeof(struct fcf_record));
1389 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1390
1391 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1392 &boot_flag, &addr_mode,
1393 &vlan_id);
1394 /*
1395 * If the fcf record does not match with connect list entries
1396 * read the next entry.
1397 */
1398 if (!rc)
1399 goto read_next_fcf;
1400 /*
1401 * If this is not the first FCF discovery of the HBA, use last
1402 * FCF record for the discovery.
1403 */
1404 spin_lock_irqsave(&phba->hbalock, flags);
1405 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1406 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1407 new_fcf_record) &&
1408 lpfc_mac_addr_match(phba, new_fcf_record)) {
1409 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1410 spin_unlock_irqrestore(&phba->hbalock, flags);
1411 goto out;
1412 }
1413 spin_unlock_irqrestore(&phba->hbalock, flags);
1414 goto read_next_fcf;
1415 }
1416 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1417 /*
1418 * If the current FCF record does not have boot flag
1419 * set and new fcf record has boot flag set, use the
1420 * new fcf record.
1421 */
1422 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1423 /* Use this FCF record */
1424 lpfc_copy_fcf_record(phba, new_fcf_record);
1425 phba->fcf.addr_mode = addr_mode;
1426 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1427 if (vlan_id != 0xFFFF) {
1428 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1429 phba->fcf.vlan_id = vlan_id;
1430 }
1431 spin_unlock_irqrestore(&phba->hbalock, flags);
1432 goto read_next_fcf;
1433 }
1434 /*
1435 * If the current FCF record has boot flag set and the
1436 * new FCF record does not have boot flag, read the next
1437 * FCF record.
1438 */
1439 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1440 spin_unlock_irqrestore(&phba->hbalock, flags);
1441 goto read_next_fcf;
1442 }
1443 /*
1444 * If there is a record with lower priority value for
1445 * the current FCF, use that record.
1446 */
1447 if (lpfc_fab_name_match(phba->fcf.fabric_name, new_fcf_record)
1448 && (new_fcf_record->fip_priority <
1449 phba->fcf.priority)) {
1450 /* Use this FCF record */
1451 lpfc_copy_fcf_record(phba, new_fcf_record);
1452 phba->fcf.addr_mode = addr_mode;
1453 if (vlan_id != 0xFFFF) {
1454 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1455 phba->fcf.vlan_id = vlan_id;
1456 }
1457 spin_unlock_irqrestore(&phba->hbalock, flags);
1458 goto read_next_fcf;
1459 }
1460 spin_unlock_irqrestore(&phba->hbalock, flags);
1461 goto read_next_fcf;
1462 }
1463 /*
1464 * This is the first available FCF record, use this
1465 * record.
1466 */
1467 lpfc_copy_fcf_record(phba, new_fcf_record);
1468 phba->fcf.addr_mode = addr_mode;
1469 if (boot_flag)
1470 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1471 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1472 if (vlan_id != 0xFFFF) {
1473 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1474 phba->fcf.vlan_id = vlan_id;
1475 }
1476 spin_unlock_irqrestore(&phba->hbalock, flags);
1477 goto read_next_fcf;
1478
1479 read_next_fcf:
1480 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1481 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1482 lpfc_register_fcf(phba);
1483 else
1484 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1485 return;
1486
1487 out:
1488 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1489 lpfc_register_fcf(phba);
1490
1491 return;
1492 }
1493
1494 /**
1495 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1496 * @phba: pointer to lpfc hba data structure.
1497 *
1498 * This function loops through the list of vports on the @phba and issues an
1499 * FDISC if possible.
1500 */
1501 void
1502 lpfc_start_fdiscs(struct lpfc_hba *phba)
1503 {
1504 struct lpfc_vport **vports;
1505 int i;
1506
1507 vports = lpfc_create_vport_work_array(phba);
1508 if (vports != NULL) {
1509 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1510 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1511 continue;
1512 /* There are no vpi for this vport */
1513 if (vports[i]->vpi > phba->max_vpi) {
1514 lpfc_vport_set_state(vports[i],
1515 FC_VPORT_FAILED);
1516 continue;
1517 }
1518 if (phba->fc_topology == TOPOLOGY_LOOP) {
1519 lpfc_vport_set_state(vports[i],
1520 FC_VPORT_LINKDOWN);
1521 continue;
1522 }
1523 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1524 lpfc_initial_fdisc(vports[i]);
1525 else {
1526 lpfc_vport_set_state(vports[i],
1527 FC_VPORT_NO_FABRIC_SUPP);
1528 lpfc_printf_vlog(vports[i], KERN_ERR,
1529 LOG_ELS,
1530 "0259 No NPIV "
1531 "Fabric support\n");
1532 }
1533 }
1534 }
1535 lpfc_destroy_vport_work_array(phba, vports);
1536 }
1537
1538 void
1539 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1540 {
1541 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1542 struct lpfc_vport *vport = mboxq->vport;
1543
1544 if (mboxq->u.mb.mbxStatus) {
1545 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1546 "2018 REG_VFI mbxStatus error x%x "
1547 "HBA state x%x\n",
1548 mboxq->u.mb.mbxStatus, vport->port_state);
1549 if (phba->fc_topology == TOPOLOGY_LOOP) {
1550 /* FLOGI failed, use loop map to make discovery list */
1551 lpfc_disc_list_loopmap(vport);
1552 /* Start discovery */
1553 lpfc_disc_start(vport);
1554 goto fail_free_mem;
1555 }
1556 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1557 goto fail_free_mem;
1558 }
1559 /* Mark the vport has registered with its VFI */
1560 vport->vfi_state |= LPFC_VFI_REGISTERED;
1561
1562 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1563 lpfc_start_fdiscs(phba);
1564 lpfc_do_scr_ns_plogi(phba, vport);
1565 }
1566
1567 fail_free_mem:
1568 mempool_free(mboxq, phba->mbox_mem_pool);
1569 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1570 kfree(dmabuf);
1571 return;
1572 }
1573
1574 static void
1575 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1576 {
1577 MAILBOX_t *mb = &pmb->u.mb;
1578 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
1579 struct lpfc_vport *vport = pmb->vport;
1580
1581
1582 /* Check for error */
1583 if (mb->mbxStatus) {
1584 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
1585 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1586 "0319 READ_SPARAM mbxStatus error x%x "
1587 "hba state x%x>\n",
1588 mb->mbxStatus, vport->port_state);
1589 lpfc_linkdown(phba);
1590 goto out;
1591 }
1592
1593 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
1594 sizeof (struct serv_parm));
1595 if (phba->cfg_soft_wwnn)
1596 u64_to_wwn(phba->cfg_soft_wwnn,
1597 vport->fc_sparam.nodeName.u.wwn);
1598 if (phba->cfg_soft_wwpn)
1599 u64_to_wwn(phba->cfg_soft_wwpn,
1600 vport->fc_sparam.portName.u.wwn);
1601 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1602 sizeof(vport->fc_nodename));
1603 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1604 sizeof(vport->fc_portname));
1605 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1606 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1607 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1608 }
1609
1610 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1611 kfree(mp);
1612 mempool_free(pmb, phba->mbox_mem_pool);
1613 return;
1614
1615 out:
1616 pmb->context1 = NULL;
1617 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1618 kfree(mp);
1619 lpfc_issue_clear_la(phba, vport);
1620 mempool_free(pmb, phba->mbox_mem_pool);
1621 return;
1622 }
1623
1624 static void
1625 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
1626 {
1627 struct lpfc_vport *vport = phba->pport;
1628 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
1629 int i;
1630 struct lpfc_dmabuf *mp;
1631 int rc;
1632 struct fcf_record *fcf_record;
1633
1634 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1635
1636 spin_lock_irq(&phba->hbalock);
1637 switch (la->UlnkSpeed) {
1638 case LA_1GHZ_LINK:
1639 phba->fc_linkspeed = LA_1GHZ_LINK;
1640 break;
1641 case LA_2GHZ_LINK:
1642 phba->fc_linkspeed = LA_2GHZ_LINK;
1643 break;
1644 case LA_4GHZ_LINK:
1645 phba->fc_linkspeed = LA_4GHZ_LINK;
1646 break;
1647 case LA_8GHZ_LINK:
1648 phba->fc_linkspeed = LA_8GHZ_LINK;
1649 break;
1650 default:
1651 phba->fc_linkspeed = LA_UNKNW_LINK;
1652 break;
1653 }
1654
1655 phba->fc_topology = la->topology;
1656 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
1657
1658 if (phba->fc_topology == TOPOLOGY_LOOP) {
1659 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
1660
1661 if (phba->cfg_enable_npiv)
1662 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1663 "1309 Link Up Event npiv not supported in loop "
1664 "topology\n");
1665 /* Get Loop Map information */
1666 if (la->il)
1667 vport->fc_flag |= FC_LBIT;
1668
1669 vport->fc_myDID = la->granted_AL_PA;
1670 i = la->un.lilpBde64.tus.f.bdeSize;
1671
1672 if (i == 0) {
1673 phba->alpa_map[0] = 0;
1674 } else {
1675 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
1676 int numalpa, j, k;
1677 union {
1678 uint8_t pamap[16];
1679 struct {
1680 uint32_t wd1;
1681 uint32_t wd2;
1682 uint32_t wd3;
1683 uint32_t wd4;
1684 } pa;
1685 } un;
1686 numalpa = phba->alpa_map[0];
1687 j = 0;
1688 while (j < numalpa) {
1689 memset(un.pamap, 0, 16);
1690 for (k = 1; j < numalpa; k++) {
1691 un.pamap[k - 1] =
1692 phba->alpa_map[j + 1];
1693 j++;
1694 if (k == 16)
1695 break;
1696 }
1697 /* Link Up Event ALPA map */
1698 lpfc_printf_log(phba,
1699 KERN_WARNING,
1700 LOG_LINK_EVENT,
1701 "1304 Link Up Event "
1702 "ALPA map Data: x%x "
1703 "x%x x%x x%x\n",
1704 un.pa.wd1, un.pa.wd2,
1705 un.pa.wd3, un.pa.wd4);
1706 }
1707 }
1708 }
1709 } else {
1710 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
1711 if (phba->max_vpi && phba->cfg_enable_npiv &&
1712 (phba->sli_rev == 3))
1713 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1714 }
1715 vport->fc_myDID = phba->fc_pref_DID;
1716 vport->fc_flag |= FC_LBIT;
1717 }
1718 spin_unlock_irq(&phba->hbalock);
1719
1720 lpfc_linkup(phba);
1721 if (sparam_mbox) {
1722 lpfc_read_sparam(phba, sparam_mbox, 0);
1723 sparam_mbox->vport = vport;
1724 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1725 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
1726 if (rc == MBX_NOT_FINISHED) {
1727 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1728 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1729 kfree(mp);
1730 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1731 goto out;
1732 }
1733 }
1734
1735 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1736 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1737 if (!cfglink_mbox)
1738 goto out;
1739 vport->port_state = LPFC_LOCAL_CFG_LINK;
1740 lpfc_config_link(phba, cfglink_mbox);
1741 cfglink_mbox->vport = vport;
1742 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
1743 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
1744 if (rc == MBX_NOT_FINISHED) {
1745 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
1746 goto out;
1747 }
1748 } else {
1749 /*
1750 * Add the driver's default FCF record at FCF index 0 now. This
1751 * is phase 1 implementation that support FCF index 0 and driver
1752 * defaults.
1753 */
1754 if (phba->cfg_enable_fip == 0) {
1755 fcf_record = kzalloc(sizeof(struct fcf_record),
1756 GFP_KERNEL);
1757 if (unlikely(!fcf_record)) {
1758 lpfc_printf_log(phba, KERN_ERR,
1759 LOG_MBOX | LOG_SLI,
1760 "2554 Could not allocate memmory for "
1761 "fcf record\n");
1762 rc = -ENODEV;
1763 goto out;
1764 }
1765
1766 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
1767 LPFC_FCOE_FCF_DEF_INDEX);
1768 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
1769 if (unlikely(rc)) {
1770 lpfc_printf_log(phba, KERN_ERR,
1771 LOG_MBOX | LOG_SLI,
1772 "2013 Could not manually add FCF "
1773 "record 0, status %d\n", rc);
1774 rc = -ENODEV;
1775 kfree(fcf_record);
1776 goto out;
1777 }
1778 kfree(fcf_record);
1779 }
1780 /*
1781 * The driver is expected to do FIP/FCF. Call the port
1782 * and get the FCF Table.
1783 */
1784 rc = lpfc_sli4_read_fcf_record(phba,
1785 LPFC_FCOE_FCF_GET_FIRST);
1786 if (rc)
1787 goto out;
1788 }
1789
1790 return;
1791 out:
1792 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1793 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1794 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1795 vport->port_state, sparam_mbox, cfglink_mbox);
1796 lpfc_issue_clear_la(phba, vport);
1797 return;
1798 }
1799
1800 static void
1801 lpfc_enable_la(struct lpfc_hba *phba)
1802 {
1803 uint32_t control;
1804 struct lpfc_sli *psli = &phba->sli;
1805 spin_lock_irq(&phba->hbalock);
1806 psli->sli_flag |= LPFC_PROCESS_LA;
1807 if (phba->sli_rev <= LPFC_SLI_REV3) {
1808 control = readl(phba->HCregaddr);
1809 control |= HC_LAINT_ENA;
1810 writel(control, phba->HCregaddr);
1811 readl(phba->HCregaddr); /* flush */
1812 }
1813 spin_unlock_irq(&phba->hbalock);
1814 }
1815
1816 static void
1817 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1818 {
1819 lpfc_linkdown(phba);
1820 lpfc_enable_la(phba);
1821 lpfc_unregister_unused_fcf(phba);
1822 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1823 }
1824
1825
1826 /*
1827 * This routine handles processing a READ_LA mailbox
1828 * command upon completion. It is setup in the LPFC_MBOXQ
1829 * as the completion routine when the command is
1830 * handed off to the SLI layer.
1831 */
1832 void
1833 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1834 {
1835 struct lpfc_vport *vport = pmb->vport;
1836 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1837 READ_LA_VAR *la;
1838 MAILBOX_t *mb = &pmb->u.mb;
1839 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1840
1841 /* Unblock ELS traffic */
1842 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1843 /* Check for error */
1844 if (mb->mbxStatus) {
1845 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1846 "1307 READ_LA mbox error x%x state x%x\n",
1847 mb->mbxStatus, vport->port_state);
1848 lpfc_mbx_issue_link_down(phba);
1849 phba->link_state = LPFC_HBA_ERROR;
1850 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1851 }
1852
1853 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
1854
1855 memcpy(&phba->alpa_map[0], mp->virt, 128);
1856
1857 spin_lock_irq(shost->host_lock);
1858 if (la->pb)
1859 vport->fc_flag |= FC_BYPASSED_MODE;
1860 else
1861 vport->fc_flag &= ~FC_BYPASSED_MODE;
1862 spin_unlock_irq(shost->host_lock);
1863
1864 if (((phba->fc_eventTag + 1) < la->eventTag) ||
1865 (phba->fc_eventTag == la->eventTag)) {
1866 phba->fc_stat.LinkMultiEvent++;
1867 if (la->attType == AT_LINK_UP)
1868 if (phba->fc_eventTag != 0)
1869 lpfc_linkdown(phba);
1870 }
1871
1872 phba->fc_eventTag = la->eventTag;
1873 if (la->mm)
1874 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1875 else
1876 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
1877
1878 if (la->attType == AT_LINK_UP && (!la->mm)) {
1879 phba->fc_stat.LinkUp++;
1880 if (phba->link_flag & LS_LOOPBACK_MODE) {
1881 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1882 "1306 Link Up Event in loop back mode "
1883 "x%x received Data: x%x x%x x%x x%x\n",
1884 la->eventTag, phba->fc_eventTag,
1885 la->granted_AL_PA, la->UlnkSpeed,
1886 phba->alpa_map[0]);
1887 } else {
1888 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1889 "1303 Link Up Event x%x received "
1890 "Data: x%x x%x x%x x%x x%x x%x %d\n",
1891 la->eventTag, phba->fc_eventTag,
1892 la->granted_AL_PA, la->UlnkSpeed,
1893 phba->alpa_map[0],
1894 la->mm, la->fa,
1895 phba->wait_4_mlo_maint_flg);
1896 }
1897 lpfc_mbx_process_link_up(phba, la);
1898 } else if (la->attType == AT_LINK_DOWN) {
1899 phba->fc_stat.LinkDown++;
1900 if (phba->link_flag & LS_LOOPBACK_MODE) {
1901 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1902 "1308 Link Down Event in loop back mode "
1903 "x%x received "
1904 "Data: x%x x%x x%x\n",
1905 la->eventTag, phba->fc_eventTag,
1906 phba->pport->port_state, vport->fc_flag);
1907 }
1908 else {
1909 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1910 "1305 Link Down Event x%x received "
1911 "Data: x%x x%x x%x x%x x%x\n",
1912 la->eventTag, phba->fc_eventTag,
1913 phba->pport->port_state, vport->fc_flag,
1914 la->mm, la->fa);
1915 }
1916 lpfc_mbx_issue_link_down(phba);
1917 }
1918 if (la->mm && la->attType == AT_LINK_UP) {
1919 if (phba->link_state != LPFC_LINK_DOWN) {
1920 phba->fc_stat.LinkDown++;
1921 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1922 "1312 Link Down Event x%x received "
1923 "Data: x%x x%x x%x\n",
1924 la->eventTag, phba->fc_eventTag,
1925 phba->pport->port_state, vport->fc_flag);
1926 lpfc_mbx_issue_link_down(phba);
1927 } else
1928 lpfc_enable_la(phba);
1929
1930 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1931 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1932 "Data: x%x x%x x%x\n",
1933 la->eventTag, phba->fc_eventTag,
1934 phba->pport->port_state, vport->fc_flag);
1935 /*
1936 * The cmnd that triggered this will be waiting for this
1937 * signal.
1938 */
1939 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1940 if (phba->wait_4_mlo_maint_flg) {
1941 phba->wait_4_mlo_maint_flg = 0;
1942 wake_up_interruptible(&phba->wait_4_mlo_m_q);
1943 }
1944 }
1945
1946 if (la->fa) {
1947 if (la->mm)
1948 lpfc_issue_clear_la(phba, vport);
1949 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1950 "1311 fa %d\n", la->fa);
1951 }
1952
1953 lpfc_mbx_cmpl_read_la_free_mbuf:
1954 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1955 kfree(mp);
1956 mempool_free(pmb, phba->mbox_mem_pool);
1957 return;
1958 }
1959
1960 /*
1961 * This routine handles processing a REG_LOGIN mailbox
1962 * command upon completion. It is setup in the LPFC_MBOXQ
1963 * as the completion routine when the command is
1964 * handed off to the SLI layer.
1965 */
1966 void
1967 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1968 {
1969 struct lpfc_vport *vport = pmb->vport;
1970 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1971 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1972
1973 pmb->context1 = NULL;
1974
1975 /* Good status, call state machine */
1976 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
1977 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1978 kfree(mp);
1979 mempool_free(pmb, phba->mbox_mem_pool);
1980 /* decrement the node reference count held for this callback
1981 * function.
1982 */
1983 lpfc_nlp_put(ndlp);
1984
1985 return;
1986 }
1987
1988 static void
1989 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1990 {
1991 MAILBOX_t *mb = &pmb->u.mb;
1992 struct lpfc_vport *vport = pmb->vport;
1993 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1994
1995 switch (mb->mbxStatus) {
1996 case 0x0011:
1997 case 0x0020:
1998 case 0x9700:
1999 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2000 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2001 mb->mbxStatus);
2002 break;
2003 }
2004 vport->unreg_vpi_cmpl = VPORT_OK;
2005 mempool_free(pmb, phba->mbox_mem_pool);
2006 /*
2007 * This shost reference might have been taken at the beginning of
2008 * lpfc_vport_delete()
2009 */
2010 if (vport->load_flag & FC_UNLOADING)
2011 scsi_host_put(shost);
2012 }
2013
2014 int
2015 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2016 {
2017 struct lpfc_hba *phba = vport->phba;
2018 LPFC_MBOXQ_t *mbox;
2019 int rc;
2020
2021 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2022 if (!mbox)
2023 return 1;
2024
2025 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2026 mbox->vport = vport;
2027 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
2028 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2029 if (rc == MBX_NOT_FINISHED) {
2030 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2031 "1800 Could not issue unreg_vpi\n");
2032 mempool_free(mbox, phba->mbox_mem_pool);
2033 vport->unreg_vpi_cmpl = VPORT_ERROR;
2034 return rc;
2035 }
2036 return 0;
2037 }
2038
2039 static void
2040 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2041 {
2042 struct lpfc_vport *vport = pmb->vport;
2043 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2044 MAILBOX_t *mb = &pmb->u.mb;
2045
2046 switch (mb->mbxStatus) {
2047 case 0x0011:
2048 case 0x9601:
2049 case 0x9602:
2050 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2051 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2052 mb->mbxStatus);
2053 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2054 spin_lock_irq(shost->host_lock);
2055 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2056 spin_unlock_irq(shost->host_lock);
2057 vport->fc_myDID = 0;
2058 goto out;
2059 }
2060
2061 vport->num_disc_nodes = 0;
2062 /* go thru NPR list and issue ELS PLOGIs */
2063 if (vport->fc_npr_cnt)
2064 lpfc_els_disc_plogi(vport);
2065
2066 if (!vport->num_disc_nodes) {
2067 spin_lock_irq(shost->host_lock);
2068 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2069 spin_unlock_irq(shost->host_lock);
2070 lpfc_can_disctmo(vport);
2071 }
2072 vport->port_state = LPFC_VPORT_READY;
2073
2074 out:
2075 mempool_free(pmb, phba->mbox_mem_pool);
2076 return;
2077 }
2078
2079 /*
2080 * This routine handles processing a Fabric REG_LOGIN mailbox
2081 * command upon completion. It is setup in the LPFC_MBOXQ
2082 * as the completion routine when the command is
2083 * handed off to the SLI layer.
2084 */
2085 void
2086 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2087 {
2088 struct lpfc_vport *vport = pmb->vport;
2089 MAILBOX_t *mb = &pmb->mb;
2090 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2091 struct lpfc_nodelist *ndlp;
2092 struct lpfc_vport **vports;
2093 int i;
2094
2095 ndlp = (struct lpfc_nodelist *) pmb->context2;
2096 pmb->context1 = NULL;
2097 pmb->context2 = NULL;
2098 if (mb->mbxStatus) {
2099 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2100 kfree(mp);
2101 mempool_free(pmb, phba->mbox_mem_pool);
2102
2103 if (phba->fc_topology == TOPOLOGY_LOOP) {
2104 /* FLOGI failed, use loop map to make discovery list */
2105 lpfc_disc_list_loopmap(vport);
2106
2107 /* Start discovery */
2108 lpfc_disc_start(vport);
2109 /* Decrement the reference count to ndlp after the
2110 * reference to the ndlp are done.
2111 */
2112 lpfc_nlp_put(ndlp);
2113 return;
2114 }
2115
2116 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2117 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2118 "0258 Register Fabric login error: 0x%x\n",
2119 mb->mbxStatus);
2120 /* Decrement the reference count to ndlp after the reference
2121 * to the ndlp are done.
2122 */
2123 lpfc_nlp_put(ndlp);
2124 return;
2125 }
2126
2127 ndlp->nlp_rpi = mb->un.varWords[0];
2128 ndlp->nlp_type |= NLP_FABRIC;
2129 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2130
2131 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2132 vports = lpfc_create_vport_work_array(phba);
2133 if (vports != NULL)
2134 for(i = 0;
2135 i <= phba->max_vpi && vports[i] != NULL;
2136 i++) {
2137 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2138 continue;
2139 if (phba->fc_topology == TOPOLOGY_LOOP) {
2140 lpfc_vport_set_state(vports[i],
2141 FC_VPORT_LINKDOWN);
2142 continue;
2143 }
2144 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2145 lpfc_initial_fdisc(vports[i]);
2146 else {
2147 lpfc_vport_set_state(vports[i],
2148 FC_VPORT_NO_FABRIC_SUPP);
2149 lpfc_printf_vlog(vport, KERN_ERR,
2150 LOG_ELS,
2151 "0259 No NPIV "
2152 "Fabric support\n");
2153 }
2154 }
2155 lpfc_destroy_vport_work_array(phba, vports);
2156 lpfc_do_scr_ns_plogi(phba, vport);
2157 }
2158
2159 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2160 kfree(mp);
2161 mempool_free(pmb, phba->mbox_mem_pool);
2162
2163 /* Drop the reference count from the mbox at the end after
2164 * all the current reference to the ndlp have been done.
2165 */
2166 lpfc_nlp_put(ndlp);
2167 return;
2168 }
2169
2170 /*
2171 * This routine handles processing a NameServer REG_LOGIN mailbox
2172 * command upon completion. It is setup in the LPFC_MBOXQ
2173 * as the completion routine when the command is
2174 * handed off to the SLI layer.
2175 */
2176 void
2177 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2178 {
2179 MAILBOX_t *mb = &pmb->mb;
2180 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2181 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2182 struct lpfc_vport *vport = pmb->vport;
2183
2184 if (mb->mbxStatus) {
2185 out:
2186 /* decrement the node reference count held for this
2187 * callback function.
2188 */
2189 lpfc_nlp_put(ndlp);
2190 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2191 kfree(mp);
2192 mempool_free(pmb, phba->mbox_mem_pool);
2193
2194 /* If no other thread is using the ndlp, free it */
2195 lpfc_nlp_not_used(ndlp);
2196
2197 if (phba->fc_topology == TOPOLOGY_LOOP) {
2198 /*
2199 * RegLogin failed, use loop map to make discovery
2200 * list
2201 */
2202 lpfc_disc_list_loopmap(vport);
2203
2204 /* Start discovery */
2205 lpfc_disc_start(vport);
2206 return;
2207 }
2208 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2209 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2210 "0260 Register NameServer error: 0x%x\n",
2211 mb->mbxStatus);
2212 return;
2213 }
2214
2215 pmb->context1 = NULL;
2216
2217 ndlp->nlp_rpi = mb->un.varWords[0];
2218 ndlp->nlp_type |= NLP_FABRIC;
2219 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2220
2221 if (vport->port_state < LPFC_VPORT_READY) {
2222 /* Link up discovery requires Fabric registration. */
2223 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2224 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2225 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2226 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2227 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2228
2229 /* Issue SCR just before NameServer GID_FT Query */
2230 lpfc_issue_els_scr(vport, SCR_DID, 0);
2231 }
2232
2233 vport->fc_ns_retry = 0;
2234 /* Good status, issue CT Request to NameServer */
2235 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
2236 /* Cannot issue NameServer Query, so finish up discovery */
2237 goto out;
2238 }
2239
2240 /* decrement the node reference count held for this
2241 * callback function.
2242 */
2243 lpfc_nlp_put(ndlp);
2244 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2245 kfree(mp);
2246 mempool_free(pmb, phba->mbox_mem_pool);
2247
2248 return;
2249 }
2250
2251 static void
2252 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2253 {
2254 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2255 struct fc_rport *rport;
2256 struct lpfc_rport_data *rdata;
2257 struct fc_rport_identifiers rport_ids;
2258 struct lpfc_hba *phba = vport->phba;
2259
2260 /* Remote port has reappeared. Re-register w/ FC transport */
2261 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2262 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2263 rport_ids.port_id = ndlp->nlp_DID;
2264 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2265
2266 /*
2267 * We leave our node pointer in rport->dd_data when we unregister a
2268 * FCP target port. But fc_remote_port_add zeros the space to which
2269 * rport->dd_data points. So, if we're reusing a previously
2270 * registered port, drop the reference that we took the last time we
2271 * registered the port.
2272 */
2273 if (ndlp->rport && ndlp->rport->dd_data &&
2274 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
2275 lpfc_nlp_put(ndlp);
2276
2277 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2278 "rport add: did:x%x flg:x%x type x%x",
2279 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2280
2281 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
2282 if (!rport || !get_device(&rport->dev)) {
2283 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2284 "Warning: fc_remote_port_add failed\n");
2285 return;
2286 }
2287
2288 /* initialize static port data */
2289 rport->maxframe_size = ndlp->nlp_maxframe;
2290 rport->supported_classes = ndlp->nlp_class_sup;
2291 rdata = rport->dd_data;
2292 rdata->pnode = lpfc_nlp_get(ndlp);
2293
2294 if (ndlp->nlp_type & NLP_FCP_TARGET)
2295 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2296 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2297 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2298
2299
2300 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2301 fc_remote_port_rolechg(rport, rport_ids.roles);
2302
2303 if ((rport->scsi_target_id != -1) &&
2304 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
2305 ndlp->nlp_sid = rport->scsi_target_id;
2306 }
2307 return;
2308 }
2309
2310 static void
2311 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
2312 {
2313 struct fc_rport *rport = ndlp->rport;
2314
2315 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2316 "rport delete: did:x%x flg:x%x type x%x",
2317 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2318
2319 fc_remote_port_delete(rport);
2320
2321 return;
2322 }
2323
2324 static void
2325 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
2326 {
2327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2328
2329 spin_lock_irq(shost->host_lock);
2330 switch (state) {
2331 case NLP_STE_UNUSED_NODE:
2332 vport->fc_unused_cnt += count;
2333 break;
2334 case NLP_STE_PLOGI_ISSUE:
2335 vport->fc_plogi_cnt += count;
2336 break;
2337 case NLP_STE_ADISC_ISSUE:
2338 vport->fc_adisc_cnt += count;
2339 break;
2340 case NLP_STE_REG_LOGIN_ISSUE:
2341 vport->fc_reglogin_cnt += count;
2342 break;
2343 case NLP_STE_PRLI_ISSUE:
2344 vport->fc_prli_cnt += count;
2345 break;
2346 case NLP_STE_UNMAPPED_NODE:
2347 vport->fc_unmap_cnt += count;
2348 break;
2349 case NLP_STE_MAPPED_NODE:
2350 vport->fc_map_cnt += count;
2351 break;
2352 case NLP_STE_NPR_NODE:
2353 vport->fc_npr_cnt += count;
2354 break;
2355 }
2356 spin_unlock_irq(shost->host_lock);
2357 }
2358
2359 static void
2360 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2361 int old_state, int new_state)
2362 {
2363 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2364
2365 if (new_state == NLP_STE_UNMAPPED_NODE) {
2366 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2367 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2368 ndlp->nlp_type |= NLP_FC_NODE;
2369 }
2370 if (new_state == NLP_STE_MAPPED_NODE)
2371 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2372 if (new_state == NLP_STE_NPR_NODE)
2373 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
2374
2375 /* Transport interface */
2376 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2377 old_state == NLP_STE_UNMAPPED_NODE)) {
2378 vport->phba->nport_event_cnt++;
2379 lpfc_unregister_remote_port(ndlp);
2380 }
2381
2382 if (new_state == NLP_STE_MAPPED_NODE ||
2383 new_state == NLP_STE_UNMAPPED_NODE) {
2384 vport->phba->nport_event_cnt++;
2385 /*
2386 * Tell the fc transport about the port, if we haven't
2387 * already. If we have, and it's a scsi entity, be
2388 * sure to unblock any attached scsi devices
2389 */
2390 lpfc_register_remote_port(vport, ndlp);
2391 }
2392 if ((new_state == NLP_STE_MAPPED_NODE) &&
2393 (vport->stat_data_enabled)) {
2394 /*
2395 * A new target is discovered, if there is no buffer for
2396 * statistical data collection allocate buffer.
2397 */
2398 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2399 sizeof(struct lpfc_scsicmd_bkt),
2400 GFP_KERNEL);
2401
2402 if (!ndlp->lat_data)
2403 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2404 "0286 lpfc_nlp_state_cleanup failed to "
2405 "allocate statistical data buffer DID "
2406 "0x%x\n", ndlp->nlp_DID);
2407 }
2408 /*
2409 * if we added to Mapped list, but the remote port
2410 * registration failed or assigned a target id outside
2411 * our presentable range - move the node to the
2412 * Unmapped List
2413 */
2414 if (new_state == NLP_STE_MAPPED_NODE &&
2415 (!ndlp->rport ||
2416 ndlp->rport->scsi_target_id == -1 ||
2417 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2418 spin_lock_irq(shost->host_lock);
2419 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2420 spin_unlock_irq(shost->host_lock);
2421 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2422 }
2423 }
2424
2425 static char *
2426 lpfc_nlp_state_name(char *buffer, size_t size, int state)
2427 {
2428 static char *states[] = {
2429 [NLP_STE_UNUSED_NODE] = "UNUSED",
2430 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2431 [NLP_STE_ADISC_ISSUE] = "ADISC",
2432 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2433 [NLP_STE_PRLI_ISSUE] = "PRLI",
2434 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2435 [NLP_STE_MAPPED_NODE] = "MAPPED",
2436 [NLP_STE_NPR_NODE] = "NPR",
2437 };
2438
2439 if (state < NLP_STE_MAX_STATE && states[state])
2440 strlcpy(buffer, states[state], size);
2441 else
2442 snprintf(buffer, size, "unknown (%d)", state);
2443 return buffer;
2444 }
2445
2446 void
2447 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2448 int state)
2449 {
2450 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2451 int old_state = ndlp->nlp_state;
2452 char name1[16], name2[16];
2453
2454 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2455 "0904 NPort state transition x%06x, %s -> %s\n",
2456 ndlp->nlp_DID,
2457 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2458 lpfc_nlp_state_name(name2, sizeof(name2), state));
2459
2460 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2461 "node statechg did:x%x old:%d ste:%d",
2462 ndlp->nlp_DID, old_state, state);
2463
2464 if (old_state == NLP_STE_NPR_NODE &&
2465 state != NLP_STE_NPR_NODE)
2466 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2467 if (old_state == NLP_STE_UNMAPPED_NODE) {
2468 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2469 ndlp->nlp_type &= ~NLP_FC_NODE;
2470 }
2471
2472 if (list_empty(&ndlp->nlp_listp)) {
2473 spin_lock_irq(shost->host_lock);
2474 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2475 spin_unlock_irq(shost->host_lock);
2476 } else if (old_state)
2477 lpfc_nlp_counters(vport, old_state, -1);
2478
2479 ndlp->nlp_state = state;
2480 lpfc_nlp_counters(vport, state, 1);
2481 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
2482 }
2483
2484 void
2485 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2486 {
2487 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2488
2489 if (list_empty(&ndlp->nlp_listp)) {
2490 spin_lock_irq(shost->host_lock);
2491 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2492 spin_unlock_irq(shost->host_lock);
2493 }
2494 }
2495
2496 void
2497 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2498 {
2499 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2500
2501 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2502 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2503 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2504 spin_lock_irq(shost->host_lock);
2505 list_del_init(&ndlp->nlp_listp);
2506 spin_unlock_irq(shost->host_lock);
2507 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2508 NLP_STE_UNUSED_NODE);
2509 }
2510
2511 static void
2512 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2513 {
2514 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2515 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2516 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2517 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2518 NLP_STE_UNUSED_NODE);
2519 }
2520 /**
2521 * lpfc_initialize_node - Initialize all fields of node object
2522 * @vport: Pointer to Virtual Port object.
2523 * @ndlp: Pointer to FC node object.
2524 * @did: FC_ID of the node.
2525 *
2526 * This function is always called when node object need to be initialized.
2527 * It initializes all the fields of the node object. Although the reference
2528 * to phba from @ndlp can be obtained indirectly through it's reference to
2529 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2530 * to the life-span of the @ndlp might go beyond the existence of @vport as
2531 * the final release of ndlp is determined by its reference count. And, the
2532 * operation on @ndlp needs the reference to phba.
2533 **/
2534 static inline void
2535 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2536 uint32_t did)
2537 {
2538 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2539 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2540 init_timer(&ndlp->nlp_delayfunc);
2541 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2542 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2543 ndlp->nlp_DID = did;
2544 ndlp->vport = vport;
2545 ndlp->phba = vport->phba;
2546 ndlp->nlp_sid = NLP_NO_SID;
2547 kref_init(&ndlp->kref);
2548 NLP_INT_NODE_ACT(ndlp);
2549 atomic_set(&ndlp->cmd_pending, 0);
2550 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2551 }
2552
2553 struct lpfc_nodelist *
2554 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2555 int state)
2556 {
2557 struct lpfc_hba *phba = vport->phba;
2558 uint32_t did;
2559 unsigned long flags;
2560
2561 if (!ndlp)
2562 return NULL;
2563
2564 spin_lock_irqsave(&phba->ndlp_lock, flags);
2565 /* The ndlp should not be in memory free mode */
2566 if (NLP_CHK_FREE_REQ(ndlp)) {
2567 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2568 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2569 "0277 lpfc_enable_node: ndlp:x%p "
2570 "usgmap:x%x refcnt:%d\n",
2571 (void *)ndlp, ndlp->nlp_usg_map,
2572 atomic_read(&ndlp->kref.refcount));
2573 return NULL;
2574 }
2575 /* The ndlp should not already be in active mode */
2576 if (NLP_CHK_NODE_ACT(ndlp)) {
2577 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2578 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2579 "0278 lpfc_enable_node: ndlp:x%p "
2580 "usgmap:x%x refcnt:%d\n",
2581 (void *)ndlp, ndlp->nlp_usg_map,
2582 atomic_read(&ndlp->kref.refcount));
2583 return NULL;
2584 }
2585
2586 /* Keep the original DID */
2587 did = ndlp->nlp_DID;
2588
2589 /* re-initialize ndlp except of ndlp linked list pointer */
2590 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2591 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
2592 lpfc_initialize_node(vport, ndlp, did);
2593
2594 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2595
2596 if (state != NLP_STE_UNUSED_NODE)
2597 lpfc_nlp_set_state(vport, ndlp, state);
2598
2599 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2600 "node enable: did:x%x",
2601 ndlp->nlp_DID, 0, 0);
2602 return ndlp;
2603 }
2604
2605 void
2606 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2607 {
2608 /*
2609 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
2610 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
2611 * the ndlp from the vport. The ndlp marked as UNUSED on the list
2612 * until ALL other outstanding threads have completed. We check
2613 * that the ndlp not already in the UNUSED state before we proceed.
2614 */
2615 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2616 return;
2617 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
2618 lpfc_nlp_put(ndlp);
2619 return;
2620 }
2621
2622 /*
2623 * Start / ReStart rescue timer for Discovery / RSCN handling
2624 */
2625 void
2626 lpfc_set_disctmo(struct lpfc_vport *vport)
2627 {
2628 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2629 struct lpfc_hba *phba = vport->phba;
2630 uint32_t tmo;
2631
2632 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
2633 /* For FAN, timeout should be greater than edtov */
2634 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
2635 } else {
2636 /* Normal discovery timeout should be > than ELS/CT timeout
2637 * FC spec states we need 3 * ratov for CT requests
2638 */
2639 tmo = ((phba->fc_ratov * 3) + 3);
2640 }
2641
2642
2643 if (!timer_pending(&vport->fc_disctmo)) {
2644 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2645 "set disc timer: tmo:x%x state:x%x flg:x%x",
2646 tmo, vport->port_state, vport->fc_flag);
2647 }
2648
2649 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
2650 spin_lock_irq(shost->host_lock);
2651 vport->fc_flag |= FC_DISC_TMO;
2652 spin_unlock_irq(shost->host_lock);
2653
2654 /* Start Discovery Timer state <hba_state> */
2655 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2656 "0247 Start Discovery Timer state x%x "
2657 "Data: x%x x%lx x%x x%x\n",
2658 vport->port_state, tmo,
2659 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
2660 vport->fc_adisc_cnt);
2661
2662 return;
2663 }
2664
2665 /*
2666 * Cancel rescue timer for Discovery / RSCN handling
2667 */
2668 int
2669 lpfc_can_disctmo(struct lpfc_vport *vport)
2670 {
2671 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2672 unsigned long iflags;
2673
2674 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2675 "can disc timer: state:x%x rtry:x%x flg:x%x",
2676 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2677
2678 /* Turn off discovery timer if its running */
2679 if (vport->fc_flag & FC_DISC_TMO) {
2680 spin_lock_irqsave(shost->host_lock, iflags);
2681 vport->fc_flag &= ~FC_DISC_TMO;
2682 spin_unlock_irqrestore(shost->host_lock, iflags);
2683 del_timer_sync(&vport->fc_disctmo);
2684 spin_lock_irqsave(&vport->work_port_lock, iflags);
2685 vport->work_port_events &= ~WORKER_DISC_TMO;
2686 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
2687 }
2688
2689 /* Cancel Discovery Timer state <hba_state> */
2690 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2691 "0248 Cancel Discovery Timer state x%x "
2692 "Data: x%x x%x x%x\n",
2693 vport->port_state, vport->fc_flag,
2694 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
2695 return 0;
2696 }
2697
2698 /*
2699 * Check specified ring for outstanding IOCB on the SLI queue
2700 * Return true if iocb matches the specified nport
2701 */
2702 int
2703 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
2704 struct lpfc_sli_ring *pring,
2705 struct lpfc_iocbq *iocb,
2706 struct lpfc_nodelist *ndlp)
2707 {
2708 struct lpfc_sli *psli = &phba->sli;
2709 IOCB_t *icmd = &iocb->iocb;
2710 struct lpfc_vport *vport = ndlp->vport;
2711
2712 if (iocb->vport != vport)
2713 return 0;
2714
2715 if (pring->ringno == LPFC_ELS_RING) {
2716 switch (icmd->ulpCommand) {
2717 case CMD_GEN_REQUEST64_CR:
2718 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
2719 return 1;
2720 case CMD_ELS_REQUEST64_CR:
2721 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
2722 return 1;
2723 case CMD_XMIT_ELS_RSP64_CX:
2724 if (iocb->context1 == (uint8_t *) ndlp)
2725 return 1;
2726 }
2727 } else if (pring->ringno == psli->extra_ring) {
2728
2729 } else if (pring->ringno == psli->fcp_ring) {
2730 /* Skip match check if waiting to relogin to FCP target */
2731 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
2732 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2733 return 0;
2734 }
2735 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2736 return 1;
2737 }
2738 } else if (pring->ringno == psli->next_ring) {
2739
2740 }
2741 return 0;
2742 }
2743
2744 /*
2745 * Free resources / clean up outstanding I/Os
2746 * associated with nlp_rpi in the LPFC_NODELIST entry.
2747 */
2748 static int
2749 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
2750 {
2751 LIST_HEAD(completions);
2752 struct lpfc_sli *psli;
2753 struct lpfc_sli_ring *pring;
2754 struct lpfc_iocbq *iocb, *next_iocb;
2755 uint32_t rpi, i;
2756
2757 lpfc_fabric_abort_nport(ndlp);
2758
2759 /*
2760 * Everything that matches on txcmplq will be returned
2761 * by firmware with a no rpi error.
2762 */
2763 psli = &phba->sli;
2764 rpi = ndlp->nlp_rpi;
2765 if (rpi) {
2766 /* Now process each ring */
2767 for (i = 0; i < psli->num_rings; i++) {
2768 pring = &psli->ring[i];
2769
2770 spin_lock_irq(&phba->hbalock);
2771 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2772 list) {
2773 /*
2774 * Check to see if iocb matches the nport we are
2775 * looking for
2776 */
2777 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
2778 ndlp))) {
2779 /* It matches, so deque and call compl
2780 with an error */
2781 list_move_tail(&iocb->list,
2782 &completions);
2783 pring->txq_cnt--;
2784 }
2785 }
2786 spin_unlock_irq(&phba->hbalock);
2787 }
2788 }
2789
2790 /* Cancel all the IOCBs from the completions list */
2791 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2792 IOERR_SLI_ABORTED);
2793
2794 return 0;
2795 }
2796
2797 /*
2798 * Free rpi associated with LPFC_NODELIST entry.
2799 * This routine is called from lpfc_freenode(), when we are removing
2800 * a LPFC_NODELIST entry. It is also called if the driver initiates a
2801 * LOGO that completes successfully, and we are waiting to PLOGI back
2802 * to the remote NPort. In addition, it is called after we receive
2803 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
2804 * we are waiting to PLOGI back to the remote NPort.
2805 */
2806 int
2807 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2808 {
2809 struct lpfc_hba *phba = vport->phba;
2810 LPFC_MBOXQ_t *mbox;
2811 int rc;
2812
2813 if (ndlp->nlp_rpi) {
2814 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2815 if (mbox) {
2816 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
2817 mbox->vport = vport;
2818 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2820 if (rc == MBX_NOT_FINISHED)
2821 mempool_free(mbox, phba->mbox_mem_pool);
2822 }
2823 lpfc_no_rpi(phba, ndlp);
2824 ndlp->nlp_rpi = 0;
2825 return 1;
2826 }
2827 return 0;
2828 }
2829
2830 void
2831 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2832 {
2833 struct lpfc_hba *phba = vport->phba;
2834 LPFC_MBOXQ_t *mbox;
2835 int rc;
2836
2837 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2838 if (mbox) {
2839 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2840 mbox->vport = vport;
2841 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2842 mbox->context1 = NULL;
2843 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2844 if (rc != MBX_TIMEOUT)
2845 mempool_free(mbox, phba->mbox_mem_pool);
2846
2847 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2848 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2849 "1836 Could not issue "
2850 "unreg_login(all_rpis) status %d\n", rc);
2851 }
2852 }
2853
2854 void
2855 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2856 {
2857 struct lpfc_hba *phba = vport->phba;
2858 LPFC_MBOXQ_t *mbox;
2859 int rc;
2860
2861 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2862 if (mbox) {
2863 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2864 mbox->vport = vport;
2865 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2866 mbox->context1 = NULL;
2867 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2868 if (rc != MBX_TIMEOUT)
2869 mempool_free(mbox, phba->mbox_mem_pool);
2870
2871 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2872 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2873 "1815 Could not issue "
2874 "unreg_did (default rpis) status %d\n",
2875 rc);
2876 }
2877 }
2878
2879 /*
2880 * Free resources associated with LPFC_NODELIST entry
2881 * so it can be freed.
2882 */
2883 static int
2884 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2885 {
2886 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2887 struct lpfc_hba *phba = vport->phba;
2888 LPFC_MBOXQ_t *mb, *nextmb;
2889 struct lpfc_dmabuf *mp;
2890
2891 /* Cleanup node for NPort <nlp_DID> */
2892 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2893 "0900 Cleanup node for NPort x%x "
2894 "Data: x%x x%x x%x\n",
2895 ndlp->nlp_DID, ndlp->nlp_flag,
2896 ndlp->nlp_state, ndlp->nlp_rpi);
2897 if (NLP_CHK_FREE_REQ(ndlp)) {
2898 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2899 "0280 lpfc_cleanup_node: ndlp:x%p "
2900 "usgmap:x%x refcnt:%d\n",
2901 (void *)ndlp, ndlp->nlp_usg_map,
2902 atomic_read(&ndlp->kref.refcount));
2903 lpfc_dequeue_node(vport, ndlp);
2904 } else {
2905 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2906 "0281 lpfc_cleanup_node: ndlp:x%p "
2907 "usgmap:x%x refcnt:%d\n",
2908 (void *)ndlp, ndlp->nlp_usg_map,
2909 atomic_read(&ndlp->kref.refcount));
2910 lpfc_disable_node(vport, ndlp);
2911 }
2912
2913 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2914 if ((mb = phba->sli.mbox_active)) {
2915 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
2916 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2917 mb->context2 = NULL;
2918 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2919 }
2920 }
2921
2922 spin_lock_irq(&phba->hbalock);
2923 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2924 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
2925 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2926 mp = (struct lpfc_dmabuf *) (mb->context1);
2927 if (mp) {
2928 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
2929 kfree(mp);
2930 }
2931 list_del(&mb->list);
2932 mempool_free(mb, phba->mbox_mem_pool);
2933 /* We shall not invoke the lpfc_nlp_put to decrement
2934 * the ndlp reference count as we are in the process
2935 * of lpfc_nlp_release.
2936 */
2937 }
2938 }
2939 spin_unlock_irq(&phba->hbalock);
2940
2941 lpfc_els_abort(phba, ndlp);
2942
2943 spin_lock_irq(shost->host_lock);
2944 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2945 spin_unlock_irq(shost->host_lock);
2946
2947 ndlp->nlp_last_elscmd = 0;
2948 del_timer_sync(&ndlp->nlp_delayfunc);
2949
2950 list_del_init(&ndlp->els_retry_evt.evt_listp);
2951 list_del_init(&ndlp->dev_loss_evt.evt_listp);
2952
2953 lpfc_unreg_rpi(vport, ndlp);
2954
2955 return 0;
2956 }
2957
2958 /*
2959 * Check to see if we can free the nlp back to the freelist.
2960 * If we are in the middle of using the nlp in the discovery state
2961 * machine, defer the free till we reach the end of the state machine.
2962 */
2963 static void
2964 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2965 {
2966 struct lpfc_hba *phba = vport->phba;
2967 struct lpfc_rport_data *rdata;
2968 LPFC_MBOXQ_t *mbox;
2969 int rc;
2970
2971 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2972 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2973 /* For this case we need to cleanup the default rpi
2974 * allocated by the firmware.
2975 */
2976 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2977 != NULL) {
2978 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2979 (uint8_t *) &vport->fc_sparam, mbox, 0);
2980 if (rc) {
2981 mempool_free(mbox, phba->mbox_mem_pool);
2982 }
2983 else {
2984 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2985 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2986 mbox->vport = vport;
2987 mbox->context2 = NULL;
2988 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2989 if (rc == MBX_NOT_FINISHED) {
2990 mempool_free(mbox, phba->mbox_mem_pool);
2991 }
2992 }
2993 }
2994 }
2995 lpfc_cleanup_node(vport, ndlp);
2996
2997 /*
2998 * We can get here with a non-NULL ndlp->rport because when we
2999 * unregister a rport we don't break the rport/node linkage. So if we
3000 * do, make sure we don't leaving any dangling pointers behind.
3001 */
3002 if (ndlp->rport) {
3003 rdata = ndlp->rport->dd_data;
3004 rdata->pnode = NULL;
3005 ndlp->rport = NULL;
3006 }
3007 }
3008
3009 static int
3010 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3011 uint32_t did)
3012 {
3013 D_ID mydid, ndlpdid, matchdid;
3014
3015 if (did == Bcast_DID)
3016 return 0;
3017
3018 /* First check for Direct match */
3019 if (ndlp->nlp_DID == did)
3020 return 1;
3021
3022 /* Next check for area/domain identically equals 0 match */
3023 mydid.un.word = vport->fc_myDID;
3024 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
3025 return 0;
3026 }
3027
3028 matchdid.un.word = did;
3029 ndlpdid.un.word = ndlp->nlp_DID;
3030 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3031 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3032 (mydid.un.b.area == matchdid.un.b.area)) {
3033 if ((ndlpdid.un.b.domain == 0) &&
3034 (ndlpdid.un.b.area == 0)) {
3035 if (ndlpdid.un.b.id)
3036 return 1;
3037 }
3038 return 0;
3039 }
3040
3041 matchdid.un.word = ndlp->nlp_DID;
3042 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3043 (mydid.un.b.area == ndlpdid.un.b.area)) {
3044 if ((matchdid.un.b.domain == 0) &&
3045 (matchdid.un.b.area == 0)) {
3046 if (matchdid.un.b.id)
3047 return 1;
3048 }
3049 }
3050 }
3051 return 0;
3052 }
3053
3054 /* Search for a nodelist entry */
3055 static struct lpfc_nodelist *
3056 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3057 {
3058 struct lpfc_nodelist *ndlp;
3059 uint32_t data1;
3060
3061 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3062 if (lpfc_matchdid(vport, ndlp, did)) {
3063 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3064 ((uint32_t) ndlp->nlp_xri << 16) |
3065 ((uint32_t) ndlp->nlp_type << 8) |
3066 ((uint32_t) ndlp->nlp_rpi & 0xff));
3067 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3068 "0929 FIND node DID "
3069 "Data: x%p x%x x%x x%x\n",
3070 ndlp, ndlp->nlp_DID,
3071 ndlp->nlp_flag, data1);
3072 return ndlp;
3073 }
3074 }
3075
3076 /* FIND node did <did> NOT FOUND */
3077 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3078 "0932 FIND node did x%x NOT FOUND.\n", did);
3079 return NULL;
3080 }
3081
3082 struct lpfc_nodelist *
3083 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3084 {
3085 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3086 struct lpfc_nodelist *ndlp;
3087
3088 spin_lock_irq(shost->host_lock);
3089 ndlp = __lpfc_findnode_did(vport, did);
3090 spin_unlock_irq(shost->host_lock);
3091 return ndlp;
3092 }
3093
3094 struct lpfc_nodelist *
3095 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3096 {
3097 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3098 struct lpfc_nodelist *ndlp;
3099
3100 ndlp = lpfc_findnode_did(vport, did);
3101 if (!ndlp) {
3102 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3103 lpfc_rscn_payload_check(vport, did) == 0)
3104 return NULL;
3105 ndlp = (struct lpfc_nodelist *)
3106 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
3107 if (!ndlp)
3108 return NULL;
3109 lpfc_nlp_init(vport, ndlp, did);
3110 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3111 spin_lock_irq(shost->host_lock);
3112 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3113 spin_unlock_irq(shost->host_lock);
3114 return ndlp;
3115 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3116 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3117 if (!ndlp)
3118 return NULL;
3119 spin_lock_irq(shost->host_lock);
3120 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3121 spin_unlock_irq(shost->host_lock);
3122 return ndlp;
3123 }
3124
3125 if ((vport->fc_flag & FC_RSCN_MODE) &&
3126 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
3127 if (lpfc_rscn_payload_check(vport, did)) {
3128 /* If we've already recieved a PLOGI from this NPort
3129 * we don't need to try to discover it again.
3130 */
3131 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3132 return NULL;
3133
3134 /* Since this node is marked for discovery,
3135 * delay timeout is not needed.
3136 */
3137 lpfc_cancel_retry_delay_tmo(vport, ndlp);
3138 spin_lock_irq(shost->host_lock);
3139 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3140 spin_unlock_irq(shost->host_lock);
3141 } else
3142 ndlp = NULL;
3143 } else {
3144 /* If we've already recieved a PLOGI from this NPort,
3145 * or we are already in the process of discovery on it,
3146 * we don't need to try to discover it again.
3147 */
3148 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
3149 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3150 ndlp->nlp_flag & NLP_RCV_PLOGI)
3151 return NULL;
3152 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3153 spin_lock_irq(shost->host_lock);
3154 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3155 spin_unlock_irq(shost->host_lock);
3156 }
3157 return ndlp;
3158 }
3159
3160 /* Build a list of nodes to discover based on the loopmap */
3161 void
3162 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
3163 {
3164 struct lpfc_hba *phba = vport->phba;
3165 int j;
3166 uint32_t alpa, index;
3167
3168 if (!lpfc_is_link_up(phba))
3169 return;
3170
3171 if (phba->fc_topology != TOPOLOGY_LOOP)
3172 return;
3173
3174 /* Check for loop map present or not */
3175 if (phba->alpa_map[0]) {
3176 for (j = 1; j <= phba->alpa_map[0]; j++) {
3177 alpa = phba->alpa_map[j];
3178 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
3179 continue;
3180 lpfc_setup_disc_node(vport, alpa);
3181 }
3182 } else {
3183 /* No alpamap, so try all alpa's */
3184 for (j = 0; j < FC_MAXLOOP; j++) {
3185 /* If cfg_scan_down is set, start from highest
3186 * ALPA (0xef) to lowest (0x1).
3187 */
3188 if (vport->cfg_scan_down)
3189 index = j;
3190 else
3191 index = FC_MAXLOOP - j - 1;
3192 alpa = lpfcAlpaArray[index];
3193 if ((vport->fc_myDID & 0xff) == alpa)
3194 continue;
3195 lpfc_setup_disc_node(vport, alpa);
3196 }
3197 }
3198 return;
3199 }
3200
3201 void
3202 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
3203 {
3204 LPFC_MBOXQ_t *mbox;
3205 struct lpfc_sli *psli = &phba->sli;
3206 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3207 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3208 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3209 int rc;
3210
3211 /*
3212 * if it's not a physical port or if we already send
3213 * clear_la then don't send it.
3214 */
3215 if ((phba->link_state >= LPFC_CLEAR_LA) ||
3216 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3217 (phba->sli_rev == LPFC_SLI_REV4))
3218 return;
3219
3220 /* Link up discovery */
3221 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3222 phba->link_state = LPFC_CLEAR_LA;
3223 lpfc_clear_la(phba, mbox);
3224 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3225 mbox->vport = vport;
3226 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3227 if (rc == MBX_NOT_FINISHED) {
3228 mempool_free(mbox, phba->mbox_mem_pool);
3229 lpfc_disc_flush_list(vport);
3230 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3231 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3232 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3233 phba->link_state = LPFC_HBA_ERROR;
3234 }
3235 }
3236 }
3237
3238 /* Reg_vpi to tell firmware to resume normal operations */
3239 void
3240 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3241 {
3242 LPFC_MBOXQ_t *regvpimbox;
3243
3244 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3245 if (regvpimbox) {
3246 lpfc_reg_vpi(vport, regvpimbox);
3247 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3248 regvpimbox->vport = vport;
3249 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
3250 == MBX_NOT_FINISHED) {
3251 mempool_free(regvpimbox, phba->mbox_mem_pool);
3252 }
3253 }
3254 }
3255
3256 /* Start Link up / RSCN discovery on NPR nodes */
3257 void
3258 lpfc_disc_start(struct lpfc_vport *vport)
3259 {
3260 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3261 struct lpfc_hba *phba = vport->phba;
3262 uint32_t num_sent;
3263 uint32_t clear_la_pending;
3264 int did_changed;
3265
3266 if (!lpfc_is_link_up(phba))
3267 return;
3268
3269 if (phba->link_state == LPFC_CLEAR_LA)
3270 clear_la_pending = 1;
3271 else
3272 clear_la_pending = 0;
3273
3274 if (vport->port_state < LPFC_VPORT_READY)
3275 vport->port_state = LPFC_DISC_AUTH;
3276
3277 lpfc_set_disctmo(vport);
3278
3279 if (vport->fc_prevDID == vport->fc_myDID)
3280 did_changed = 0;
3281 else
3282 did_changed = 1;
3283
3284 vport->fc_prevDID = vport->fc_myDID;
3285 vport->num_disc_nodes = 0;
3286
3287 /* Start Discovery state <hba_state> */
3288 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3289 "0202 Start Discovery hba state x%x "
3290 "Data: x%x x%x x%x\n",
3291 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3292 vport->fc_adisc_cnt);
3293
3294 /* First do ADISCs - if any */
3295 num_sent = lpfc_els_disc_adisc(vport);
3296
3297 if (num_sent)
3298 return;
3299
3300 /*
3301 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3302 * continue discovery.
3303 */
3304 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3305 !(vport->fc_flag & FC_PT2PT) &&
3306 !(vport->fc_flag & FC_RSCN_MODE) &&
3307 (phba->sli_rev < LPFC_SLI_REV4)) {
3308 lpfc_issue_reg_vpi(phba, vport);
3309 return;
3310 }
3311
3312 /*
3313 * For SLI2, we need to set port_state to READY and continue
3314 * discovery.
3315 */
3316 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3317 /* If we get here, there is nothing to ADISC */
3318 if (vport->port_type == LPFC_PHYSICAL_PORT)
3319 lpfc_issue_clear_la(phba, vport);
3320
3321 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
3322 vport->num_disc_nodes = 0;
3323 /* go thru NPR nodes and issue ELS PLOGIs */
3324 if (vport->fc_npr_cnt)
3325 lpfc_els_disc_plogi(vport);
3326
3327 if (!vport->num_disc_nodes) {
3328 spin_lock_irq(shost->host_lock);
3329 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3330 spin_unlock_irq(shost->host_lock);
3331 lpfc_can_disctmo(vport);
3332 }
3333 }
3334 vport->port_state = LPFC_VPORT_READY;
3335 } else {
3336 /* Next do PLOGIs - if any */
3337 num_sent = lpfc_els_disc_plogi(vport);
3338
3339 if (num_sent)
3340 return;
3341
3342 if (vport->fc_flag & FC_RSCN_MODE) {
3343 /* Check to see if more RSCNs came in while we
3344 * were processing this one.
3345 */
3346 if ((vport->fc_rscn_id_cnt == 0) &&
3347 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3348 spin_lock_irq(shost->host_lock);
3349 vport->fc_flag &= ~FC_RSCN_MODE;
3350 spin_unlock_irq(shost->host_lock);
3351 lpfc_can_disctmo(vport);
3352 } else
3353 lpfc_els_handle_rscn(vport);
3354 }
3355 }
3356 return;
3357 }
3358
3359 /*
3360 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3361 * ring the match the sppecified nodelist.
3362 */
3363 static void
3364 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
3365 {
3366 LIST_HEAD(completions);
3367 struct lpfc_sli *psli;
3368 IOCB_t *icmd;
3369 struct lpfc_iocbq *iocb, *next_iocb;
3370 struct lpfc_sli_ring *pring;
3371
3372 psli = &phba->sli;
3373 pring = &psli->ring[LPFC_ELS_RING];
3374
3375 /* Error matching iocb on txq or txcmplq
3376 * First check the txq.
3377 */
3378 spin_lock_irq(&phba->hbalock);
3379 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3380 if (iocb->context1 != ndlp) {
3381 continue;
3382 }
3383 icmd = &iocb->iocb;
3384 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3385 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3386
3387 list_move_tail(&iocb->list, &completions);
3388 pring->txq_cnt--;
3389 }
3390 }
3391
3392 /* Next check the txcmplq */
3393 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3394 if (iocb->context1 != ndlp) {
3395 continue;
3396 }
3397 icmd = &iocb->iocb;
3398 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3399 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
3400 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3401 }
3402 }
3403 spin_unlock_irq(&phba->hbalock);
3404
3405 /* Cancel all the IOCBs from the completions list */
3406 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3407 IOERR_SLI_ABORTED);
3408 }
3409
3410 static void
3411 lpfc_disc_flush_list(struct lpfc_vport *vport)
3412 {
3413 struct lpfc_nodelist *ndlp, *next_ndlp;
3414 struct lpfc_hba *phba = vport->phba;
3415
3416 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3417 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3418 nlp_listp) {
3419 if (!NLP_CHK_NODE_ACT(ndlp))
3420 continue;
3421 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3422 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3423 lpfc_free_tx(phba, ndlp);
3424 }
3425 }
3426 }
3427 }
3428
3429 void
3430 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3431 {
3432 lpfc_els_flush_rscn(vport);
3433 lpfc_els_flush_cmd(vport);
3434 lpfc_disc_flush_list(vport);
3435 }
3436
3437 /*****************************************************************************/
3438 /*
3439 * NAME: lpfc_disc_timeout
3440 *
3441 * FUNCTION: Fibre Channel driver discovery timeout routine.
3442 *
3443 * EXECUTION ENVIRONMENT: interrupt only
3444 *
3445 * CALLED FROM:
3446 * Timer function
3447 *
3448 * RETURNS:
3449 * none
3450 */
3451 /*****************************************************************************/
3452 void
3453 lpfc_disc_timeout(unsigned long ptr)
3454 {
3455 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3456 struct lpfc_hba *phba = vport->phba;
3457 uint32_t tmo_posted;
3458 unsigned long flags = 0;
3459
3460 if (unlikely(!phba))
3461 return;
3462
3463 spin_lock_irqsave(&vport->work_port_lock, flags);
3464 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3465 if (!tmo_posted)
3466 vport->work_port_events |= WORKER_DISC_TMO;
3467 spin_unlock_irqrestore(&vport->work_port_lock, flags);
3468
3469 if (!tmo_posted)
3470 lpfc_worker_wake_up(phba);
3471 return;
3472 }
3473
3474 static void
3475 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
3476 {
3477 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3478 struct lpfc_hba *phba = vport->phba;
3479 struct lpfc_sli *psli = &phba->sli;
3480 struct lpfc_nodelist *ndlp, *next_ndlp;
3481 LPFC_MBOXQ_t *initlinkmbox;
3482 int rc, clrlaerr = 0;
3483
3484 if (!(vport->fc_flag & FC_DISC_TMO))
3485 return;
3486
3487 spin_lock_irq(shost->host_lock);
3488 vport->fc_flag &= ~FC_DISC_TMO;
3489 spin_unlock_irq(shost->host_lock);
3490
3491 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3492 "disc timeout: state:x%x rtry:x%x flg:x%x",
3493 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3494
3495 switch (vport->port_state) {
3496
3497 case LPFC_LOCAL_CFG_LINK:
3498 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3499 * FAN
3500 */
3501 /* FAN timeout */
3502 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3503 "0221 FAN timeout\n");
3504 /* Start discovery by sending FLOGI, clean up old rpis */
3505 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3506 nlp_listp) {
3507 if (!NLP_CHK_NODE_ACT(ndlp))
3508 continue;
3509 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3510 continue;
3511 if (ndlp->nlp_type & NLP_FABRIC) {
3512 /* Clean up the ndlp on Fabric connections */
3513 lpfc_drop_node(vport, ndlp);
3514
3515 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3516 /* Fail outstanding IO now since device
3517 * is marked for PLOGI.
3518 */
3519 lpfc_unreg_rpi(vport, ndlp);
3520 }
3521 }
3522 if (vport->port_state != LPFC_FLOGI) {
3523 lpfc_initial_flogi(vport);
3524 return;
3525 }
3526 break;
3527
3528 case LPFC_FDISC:
3529 case LPFC_FLOGI:
3530 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
3531 /* Initial FLOGI timeout */
3532 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3533 "0222 Initial %s timeout\n",
3534 vport->vpi ? "FDISC" : "FLOGI");
3535
3536 /* Assume no Fabric and go on with discovery.
3537 * Check for outstanding ELS FLOGI to abort.
3538 */
3539
3540 /* FLOGI failed, so just use loop map to make discovery list */
3541 lpfc_disc_list_loopmap(vport);
3542
3543 /* Start discovery */
3544 lpfc_disc_start(vport);
3545 break;
3546
3547 case LPFC_FABRIC_CFG_LINK:
3548 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3549 NameServer login */
3550 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3551 "0223 Timeout while waiting for "
3552 "NameServer login\n");
3553 /* Next look for NameServer ndlp */
3554 ndlp = lpfc_findnode_did(vport, NameServer_DID);
3555 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3556 lpfc_els_abort(phba, ndlp);
3557
3558 /* ReStart discovery */
3559 goto restart_disc;
3560
3561 case LPFC_NS_QRY:
3562 /* Check for wait for NameServer Rsp timeout */
3563 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3564 "0224 NameServer Query timeout "
3565 "Data: x%x x%x\n",
3566 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
3567
3568 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3569 /* Try it one more time */
3570 vport->fc_ns_retry++;
3571 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3572 vport->fc_ns_retry, 0);
3573 if (rc == 0)
3574 break;
3575 }
3576 vport->fc_ns_retry = 0;
3577
3578 restart_disc:
3579 /*
3580 * Discovery is over.
3581 * set port_state to PORT_READY if SLI2.
3582 * cmpl_reg_vpi will set port_state to READY for SLI3.
3583 */
3584 if (phba->sli_rev < LPFC_SLI_REV4) {
3585 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3586 lpfc_issue_reg_vpi(phba, vport);
3587 else { /* NPIV Not enabled */
3588 lpfc_issue_clear_la(phba, vport);
3589 vport->port_state = LPFC_VPORT_READY;
3590 }
3591 }
3592
3593 /* Setup and issue mailbox INITIALIZE LINK command */
3594 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3595 if (!initlinkmbox) {
3596 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3597 "0206 Device Discovery "
3598 "completion error\n");
3599 phba->link_state = LPFC_HBA_ERROR;
3600 break;
3601 }
3602
3603 lpfc_linkdown(phba);
3604 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3605 phba->cfg_link_speed);
3606 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
3607 initlinkmbox->vport = vport;
3608 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3609 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
3610 lpfc_set_loopback_flag(phba);
3611 if (rc == MBX_NOT_FINISHED)
3612 mempool_free(initlinkmbox, phba->mbox_mem_pool);
3613
3614 break;
3615
3616 case LPFC_DISC_AUTH:
3617 /* Node Authentication timeout */
3618 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3619 "0227 Node Authentication timeout\n");
3620 lpfc_disc_flush_list(vport);
3621
3622 /*
3623 * set port_state to PORT_READY if SLI2.
3624 * cmpl_reg_vpi will set port_state to READY for SLI3.
3625 */
3626 if (phba->sli_rev < LPFC_SLI_REV4) {
3627 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3628 lpfc_issue_reg_vpi(phba, vport);
3629 else { /* NPIV Not enabled */
3630 lpfc_issue_clear_la(phba, vport);
3631 vport->port_state = LPFC_VPORT_READY;
3632 }
3633 }
3634 break;
3635
3636 case LPFC_VPORT_READY:
3637 if (vport->fc_flag & FC_RSCN_MODE) {
3638 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3639 "0231 RSCN timeout Data: x%x "
3640 "x%x\n",
3641 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
3642
3643 /* Cleanup any outstanding ELS commands */
3644 lpfc_els_flush_cmd(vport);
3645
3646 lpfc_els_flush_rscn(vport);
3647 lpfc_disc_flush_list(vport);
3648 }
3649 break;
3650
3651 default:
3652 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3653 "0273 Unexpected discovery timeout, "
3654 "vport State x%x\n", vport->port_state);
3655 break;
3656 }
3657
3658 switch (phba->link_state) {
3659 case LPFC_CLEAR_LA:
3660 /* CLEAR LA timeout */
3661 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3662 "0228 CLEAR LA timeout\n");
3663 clrlaerr = 1;
3664 break;
3665
3666 case LPFC_LINK_UP:
3667 lpfc_issue_clear_la(phba, vport);
3668 /* Drop thru */
3669 case LPFC_LINK_UNKNOWN:
3670 case LPFC_WARM_START:
3671 case LPFC_INIT_START:
3672 case LPFC_INIT_MBX_CMDS:
3673 case LPFC_LINK_DOWN:
3674 case LPFC_HBA_ERROR:
3675 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3676 "0230 Unexpected timeout, hba link "
3677 "state x%x\n", phba->link_state);
3678 clrlaerr = 1;
3679 break;
3680
3681 case LPFC_HBA_READY:
3682 break;
3683 }
3684
3685 if (clrlaerr) {
3686 lpfc_disc_flush_list(vport);
3687 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3688 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3689 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3690 vport->port_state = LPFC_VPORT_READY;
3691 }
3692
3693 return;
3694 }
3695
3696 /*
3697 * This routine handles processing a NameServer REG_LOGIN mailbox
3698 * command upon completion. It is setup in the LPFC_MBOXQ
3699 * as the completion routine when the command is
3700 * handed off to the SLI layer.
3701 */
3702 void
3703 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3704 {
3705 MAILBOX_t *mb = &pmb->u.mb;
3706 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3707 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3708 struct lpfc_vport *vport = pmb->vport;
3709
3710 pmb->context1 = NULL;
3711
3712 ndlp->nlp_rpi = mb->un.varWords[0];
3713 ndlp->nlp_type |= NLP_FABRIC;
3714 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
3715
3716 /*
3717 * Start issuing Fabric-Device Management Interface (FDMI) command to
3718 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
3719 * fdmi-on=2 (supporting RPA/hostnmae)
3720 */
3721
3722 if (vport->cfg_fdmi_on == 1)
3723 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
3724 else
3725 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
3726
3727 /* decrement the node reference count held for this callback
3728 * function.
3729 */
3730 lpfc_nlp_put(ndlp);
3731 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3732 kfree(mp);
3733 mempool_free(pmb, phba->mbox_mem_pool);
3734
3735 return;
3736 }
3737
3738 static int
3739 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
3740 {
3741 uint16_t *rpi = param;
3742
3743 return ndlp->nlp_rpi == *rpi;
3744 }
3745
3746 static int
3747 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
3748 {
3749 return memcmp(&ndlp->nlp_portname, param,
3750 sizeof(ndlp->nlp_portname)) == 0;
3751 }
3752
3753 static struct lpfc_nodelist *
3754 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
3755 {
3756 struct lpfc_nodelist *ndlp;
3757
3758 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3759 if (filter(ndlp, param))
3760 return ndlp;
3761 }
3762 return NULL;
3763 }
3764
3765 /*
3766 * This routine looks up the ndlp lists for the given RPI. If rpi found it
3767 * returns the node list element pointer else return NULL.
3768 */
3769 struct lpfc_nodelist *
3770 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
3771 {
3772 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
3773 }
3774
3775 /*
3776 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
3777 * returns the node element list pointer else return NULL.
3778 */
3779 struct lpfc_nodelist *
3780 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
3781 {
3782 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3783 struct lpfc_nodelist *ndlp;
3784
3785 spin_lock_irq(shost->host_lock);
3786 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
3787 spin_unlock_irq(shost->host_lock);
3788 return ndlp;
3789 }
3790
3791 void
3792 lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3793 uint32_t did)
3794 {
3795 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
3796
3797 lpfc_initialize_node(vport, ndlp, did);
3798 INIT_LIST_HEAD(&ndlp->nlp_listp);
3799
3800 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3801 "node init: did:x%x",
3802 ndlp->nlp_DID, 0, 0);
3803
3804 return;
3805 }
3806
3807 /* This routine releases all resources associated with a specifc NPort's ndlp
3808 * and mempool_free's the nodelist.
3809 */
3810 static void
3811 lpfc_nlp_release(struct kref *kref)
3812 {
3813 struct lpfc_hba *phba;
3814 unsigned long flags;
3815 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3816 kref);
3817
3818 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3819 "node release: did:x%x flg:x%x type:x%x",
3820 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3821
3822 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3823 "0279 lpfc_nlp_release: ndlp:x%p "
3824 "usgmap:x%x refcnt:%d\n",
3825 (void *)ndlp, ndlp->nlp_usg_map,
3826 atomic_read(&ndlp->kref.refcount));
3827
3828 /* remove ndlp from action. */
3829 lpfc_nlp_remove(ndlp->vport, ndlp);
3830
3831 /* clear the ndlp active flag for all release cases */
3832 phba = ndlp->phba;
3833 spin_lock_irqsave(&phba->ndlp_lock, flags);
3834 NLP_CLR_NODE_ACT(ndlp);
3835 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3836
3837 /* free ndlp memory for final ndlp release */
3838 if (NLP_CHK_FREE_REQ(ndlp)) {
3839 kfree(ndlp->lat_data);
3840 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
3841 }
3842 }
3843
3844 /* This routine bumps the reference count for a ndlp structure to ensure
3845 * that one discovery thread won't free a ndlp while another discovery thread
3846 * is using it.
3847 */
3848 struct lpfc_nodelist *
3849 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3850 {
3851 struct lpfc_hba *phba;
3852 unsigned long flags;
3853
3854 if (ndlp) {
3855 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3856 "node get: did:x%x flg:x%x refcnt:x%x",
3857 ndlp->nlp_DID, ndlp->nlp_flag,
3858 atomic_read(&ndlp->kref.refcount));
3859 /* The check of ndlp usage to prevent incrementing the
3860 * ndlp reference count that is in the process of being
3861 * released.
3862 */
3863 phba = ndlp->phba;
3864 spin_lock_irqsave(&phba->ndlp_lock, flags);
3865 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3866 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3867 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3868 "0276 lpfc_nlp_get: ndlp:x%p "
3869 "usgmap:x%x refcnt:%d\n",
3870 (void *)ndlp, ndlp->nlp_usg_map,
3871 atomic_read(&ndlp->kref.refcount));
3872 return NULL;
3873 } else
3874 kref_get(&ndlp->kref);
3875 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3876 }
3877 return ndlp;
3878 }
3879
3880 /* This routine decrements the reference count for a ndlp structure. If the
3881 * count goes to 0, this indicates the the associated nodelist should be
3882 * freed. Returning 1 indicates the ndlp resource has been released; on the
3883 * other hand, returning 0 indicates the ndlp resource has not been released
3884 * yet.
3885 */
3886 int
3887 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3888 {
3889 struct lpfc_hba *phba;
3890 unsigned long flags;
3891
3892 if (!ndlp)
3893 return 1;
3894
3895 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3896 "node put: did:x%x flg:x%x refcnt:x%x",
3897 ndlp->nlp_DID, ndlp->nlp_flag,
3898 atomic_read(&ndlp->kref.refcount));
3899 phba = ndlp->phba;
3900 spin_lock_irqsave(&phba->ndlp_lock, flags);
3901 /* Check the ndlp memory free acknowledge flag to avoid the
3902 * possible race condition that kref_put got invoked again
3903 * after previous one has done ndlp memory free.
3904 */
3905 if (NLP_CHK_FREE_ACK(ndlp)) {
3906 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3907 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3908 "0274 lpfc_nlp_put: ndlp:x%p "
3909 "usgmap:x%x refcnt:%d\n",
3910 (void *)ndlp, ndlp->nlp_usg_map,
3911 atomic_read(&ndlp->kref.refcount));
3912 return 1;
3913 }
3914 /* Check the ndlp inactivate log flag to avoid the possible
3915 * race condition that kref_put got invoked again after ndlp
3916 * is already in inactivating state.
3917 */
3918 if (NLP_CHK_IACT_REQ(ndlp)) {
3919 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3920 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3921 "0275 lpfc_nlp_put: ndlp:x%p "
3922 "usgmap:x%x refcnt:%d\n",
3923 (void *)ndlp, ndlp->nlp_usg_map,
3924 atomic_read(&ndlp->kref.refcount));
3925 return 1;
3926 }
3927 /* For last put, mark the ndlp usage flags to make sure no
3928 * other kref_get and kref_put on the same ndlp shall get
3929 * in between the process when the final kref_put has been
3930 * invoked on this ndlp.
3931 */
3932 if (atomic_read(&ndlp->kref.refcount) == 1) {
3933 /* Indicate ndlp is put to inactive state. */
3934 NLP_SET_IACT_REQ(ndlp);
3935 /* Acknowledge ndlp memory free has been seen. */
3936 if (NLP_CHK_FREE_REQ(ndlp))
3937 NLP_SET_FREE_ACK(ndlp);
3938 }
3939 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3940 /* Note, the kref_put returns 1 when decrementing a reference
3941 * count that was 1, it invokes the release callback function,
3942 * but it still left the reference count as 1 (not actually
3943 * performs the last decrementation). Otherwise, it actually
3944 * decrements the reference count and returns 0.
3945 */
3946 return kref_put(&ndlp->kref, lpfc_nlp_release);
3947 }
3948
3949 /* This routine free's the specified nodelist if it is not in use
3950 * by any other discovery thread. This routine returns 1 if the
3951 * ndlp has been freed. A return value of 0 indicates the ndlp is
3952 * not yet been released.
3953 */
3954 int
3955 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3956 {
3957 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3958 "node not used: did:x%x flg:x%x refcnt:x%x",
3959 ndlp->nlp_DID, ndlp->nlp_flag,
3960 atomic_read(&ndlp->kref.refcount));
3961 if (atomic_read(&ndlp->kref.refcount) == 1)
3962 if (lpfc_nlp_put(ndlp))
3963 return 1;
3964 return 0;
3965 }
3966
3967 /**
3968 * lpfc_fcf_inuse - Check if FCF can be unregistered.
3969 * @phba: Pointer to hba context object.
3970 *
3971 * This function iterate through all FC nodes associated
3972 * will all vports to check if there is any node with
3973 * fc_rports associated with it. If there is an fc_rport
3974 * associated with the node, then the node is either in
3975 * discovered state or its devloss_timer is pending.
3976 */
3977 static int
3978 lpfc_fcf_inuse(struct lpfc_hba *phba)
3979 {
3980 struct lpfc_vport **vports;
3981 int i, ret = 0;
3982 struct lpfc_nodelist *ndlp;
3983 struct Scsi_Host *shost;
3984
3985 vports = lpfc_create_vport_work_array(phba);
3986
3987 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3988 shost = lpfc_shost_from_vport(vports[i]);
3989 spin_lock_irq(shost->host_lock);
3990 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
3991 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
3992 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
3993 ret = 1;
3994 spin_unlock_irq(shost->host_lock);
3995 goto out;
3996 }
3997 }
3998 spin_unlock_irq(shost->host_lock);
3999 }
4000 out:
4001 lpfc_destroy_vport_work_array(phba, vports);
4002 return ret;
4003 }
4004
4005 /**
4006 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4007 * @phba: Pointer to hba context object.
4008 * @mboxq: Pointer to mailbox object.
4009 *
4010 * This function frees memory associated with the mailbox command.
4011 */
4012 static void
4013 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4014 {
4015 struct lpfc_vport *vport = mboxq->vport;
4016
4017 if (mboxq->u.mb.mbxStatus) {
4018 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4019 "2555 UNREG_VFI mbxStatus error x%x "
4020 "HBA state x%x\n",
4021 mboxq->u.mb.mbxStatus, vport->port_state);
4022 }
4023 mempool_free(mboxq, phba->mbox_mem_pool);
4024 return;
4025 }
4026
4027 /**
4028 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4029 * @phba: Pointer to hba context object.
4030 * @mboxq: Pointer to mailbox object.
4031 *
4032 * This function frees memory associated with the mailbox command.
4033 */
4034 static void
4035 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4036 {
4037 struct lpfc_vport *vport = mboxq->vport;
4038
4039 if (mboxq->u.mb.mbxStatus) {
4040 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4041 "2550 UNREG_FCFI mbxStatus error x%x "
4042 "HBA state x%x\n",
4043 mboxq->u.mb.mbxStatus, vport->port_state);
4044 }
4045 mempool_free(mboxq, phba->mbox_mem_pool);
4046 return;
4047 }
4048
4049 /**
4050 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4051 * @phba: Pointer to hba context object.
4052 *
4053 * This function check if there are any connected remote port for the FCF and
4054 * if all the devices are disconnected, this function unregister FCFI.
4055 * This function also tries to use another FCF for discovery.
4056 */
4057 void
4058 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4059 {
4060 LPFC_MBOXQ_t *mbox;
4061 int rc;
4062 struct lpfc_vport **vports;
4063 int i;
4064
4065 spin_lock_irq(&phba->hbalock);
4066 /*
4067 * If HBA is not running in FIP mode or
4068 * If HBA does not support FCoE or
4069 * If FCF is not registered.
4070 * do nothing.
4071 */
4072 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4073 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
4074 (phba->cfg_enable_fip == 0)) {
4075 spin_unlock_irq(&phba->hbalock);
4076 return;
4077 }
4078 spin_unlock_irq(&phba->hbalock);
4079
4080 if (lpfc_fcf_inuse(phba))
4081 return;
4082
4083
4084 /* Unregister VPIs */
4085 vports = lpfc_create_vport_work_array(phba);
4086 if (vports &&
4087 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4088 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4089 lpfc_mbx_unreg_vpi(vports[i]);
4090 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4091 vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
4092 }
4093 lpfc_destroy_vport_work_array(phba, vports);
4094
4095 /* Unregister VFI */
4096 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4097 if (!mbox) {
4098 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4099 "2556 UNREG_VFI mbox allocation failed"
4100 "HBA state x%x\n",
4101 phba->pport->port_state);
4102 return;
4103 }
4104
4105 lpfc_unreg_vfi(mbox, phba->pport->vfi);
4106 mbox->vport = phba->pport;
4107 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4108
4109 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4110 if (rc == MBX_NOT_FINISHED) {
4111 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4112 "2557 UNREG_VFI issue mbox failed rc x%x "
4113 "HBA state x%x\n",
4114 rc, phba->pport->port_state);
4115 mempool_free(mbox, phba->mbox_mem_pool);
4116 return;
4117 }
4118
4119 /* Unregister FCF */
4120 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4121 if (!mbox) {
4122 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4123 "2551 UNREG_FCFI mbox allocation failed"
4124 "HBA state x%x\n",
4125 phba->pport->port_state);
4126 return;
4127 }
4128
4129 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4130 mbox->vport = phba->pport;
4131 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4132 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4133
4134 if (rc == MBX_NOT_FINISHED) {
4135 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4136 "2552 UNREG_FCFI issue mbox failed rc x%x "
4137 "HBA state x%x\n",
4138 rc, phba->pport->port_state);
4139 mempool_free(mbox, phba->mbox_mem_pool);
4140 return;
4141 }
4142
4143 spin_lock_irq(&phba->hbalock);
4144 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4145 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4146 FCF_VALID_VLAN);
4147 spin_unlock_irq(&phba->hbalock);
4148
4149 /*
4150 * If driver is not unloading, check if there is any other
4151 * FCF record that can be used for discovery.
4152 */
4153 if ((phba->pport->load_flag & FC_UNLOADING) ||
4154 (phba->link_state < LPFC_LINK_UP))
4155 return;
4156
4157 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4158
4159 if (rc)
4160 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4161 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4162 " record HBA state x%x\n",
4163 phba->pport->port_state);
4164 }
4165
4166 /**
4167 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4168 * @phba: Pointer to hba context object.
4169 * @buff: Buffer containing the FCF connection table as in the config
4170 * region.
4171 * This function create driver data structure for the FCF connection
4172 * record table read from config region 23.
4173 */
4174 static void
4175 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4176 uint8_t *buff)
4177 {
4178 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4179 struct lpfc_fcf_conn_hdr *conn_hdr;
4180 struct lpfc_fcf_conn_rec *conn_rec;
4181 uint32_t record_count;
4182 int i;
4183
4184 /* Free the current connect table */
4185 list_for_each_entry_safe(conn_entry, next_conn_entry,
4186 &phba->fcf_conn_rec_list, list)
4187 kfree(conn_entry);
4188
4189 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4190 record_count = conn_hdr->length * sizeof(uint32_t)/
4191 sizeof(struct lpfc_fcf_conn_rec);
4192
4193 conn_rec = (struct lpfc_fcf_conn_rec *)
4194 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4195
4196 for (i = 0; i < record_count; i++) {
4197 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4198 continue;
4199 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4200 GFP_KERNEL);
4201 if (!conn_entry) {
4202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4203 "2566 Failed to allocate connection"
4204 " table entry\n");
4205 return;
4206 }
4207
4208 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4209 sizeof(struct lpfc_fcf_conn_rec));
4210 conn_entry->conn_rec.vlan_tag =
4211 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4212 conn_entry->conn_rec.flags =
4213 le16_to_cpu(conn_entry->conn_rec.flags);
4214 list_add_tail(&conn_entry->list,
4215 &phba->fcf_conn_rec_list);
4216 }
4217 }
4218
4219 /**
4220 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4221 * @phba: Pointer to hba context object.
4222 * @buff: Buffer containing the FCoE parameter data structure.
4223 *
4224 * This function update driver data structure with config
4225 * parameters read from config region 23.
4226 */
4227 static void
4228 lpfc_read_fcoe_param(struct lpfc_hba *phba,
4229 uint8_t *buff)
4230 {
4231 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4232 struct lpfc_fcoe_params *fcoe_param;
4233
4234 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4235 buff;
4236 fcoe_param = (struct lpfc_fcoe_params *)
4237 buff + sizeof(struct lpfc_fip_param_hdr);
4238
4239 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4240 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4241 return;
4242
4243 if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
4244 FIPP_MODE_ON)
4245 phba->cfg_enable_fip = 1;
4246
4247 if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
4248 FIPP_MODE_OFF)
4249 phba->cfg_enable_fip = 0;
4250
4251 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4252 phba->valid_vlan = 1;
4253 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4254 0xFFF;
4255 }
4256
4257 phba->fc_map[0] = fcoe_param->fc_map[0];
4258 phba->fc_map[1] = fcoe_param->fc_map[1];
4259 phba->fc_map[2] = fcoe_param->fc_map[2];
4260 return;
4261 }
4262
4263 /**
4264 * lpfc_get_rec_conf23 - Get a record type in config region data.
4265 * @buff: Buffer containing config region 23 data.
4266 * @size: Size of the data buffer.
4267 * @rec_type: Record type to be searched.
4268 *
4269 * This function searches config region data to find the begining
4270 * of the record specified by record_type. If record found, this
4271 * function return pointer to the record else return NULL.
4272 */
4273 static uint8_t *
4274 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4275 {
4276 uint32_t offset = 0, rec_length;
4277
4278 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4279 (size < sizeof(uint32_t)))
4280 return NULL;
4281
4282 rec_length = buff[offset + 1];
4283
4284 /*
4285 * One TLV record has one word header and number of data words
4286 * specified in the rec_length field of the record header.
4287 */
4288 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4289 <= size) {
4290 if (buff[offset] == rec_type)
4291 return &buff[offset];
4292
4293 if (buff[offset] == LPFC_REGION23_LAST_REC)
4294 return NULL;
4295
4296 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4297 rec_length = buff[offset + 1];
4298 }
4299 return NULL;
4300 }
4301
4302 /**
4303 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4304 * @phba: Pointer to lpfc_hba data structure.
4305 * @buff: Buffer containing config region 23 data.
4306 * @size: Size of the data buffer.
4307 *
4308 * This fuction parse the FCoE config parameters in config region 23 and
4309 * populate driver data structure with the parameters.
4310 */
4311 void
4312 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4313 uint8_t *buff,
4314 uint32_t size)
4315 {
4316 uint32_t offset = 0, rec_length;
4317 uint8_t *rec_ptr;
4318
4319 /*
4320 * If data size is less than 2 words signature and version cannot be
4321 * verified.
4322 */
4323 if (size < 2*sizeof(uint32_t))
4324 return;
4325
4326 /* Check the region signature first */
4327 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4329 "2567 Config region 23 has bad signature\n");
4330 return;
4331 }
4332
4333 offset += 4;
4334
4335 /* Check the data structure version */
4336 if (buff[offset] != LPFC_REGION23_VERSION) {
4337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4338 "2568 Config region 23 has bad version\n");
4339 return;
4340 }
4341 offset += 4;
4342
4343 rec_length = buff[offset + 1];
4344
4345 /* Read FCoE param record */
4346 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4347 size - offset, FCOE_PARAM_TYPE);
4348 if (rec_ptr)
4349 lpfc_read_fcoe_param(phba, rec_ptr);
4350
4351 /* Read FCF connection table */
4352 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4353 size - offset, FCOE_CONN_TBL_TYPE);
4354 if (rec_ptr)
4355 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4356
4357 }