]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/qla2xxx/qla_init.c
scsi: qla2xxx: Fix Management Server NPort handle reservation logic
[linux.git] / drivers / scsi / qla2xxx / qla_init.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
9
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13
14 #include "qla_devtbl.h"
15
16 #ifdef CONFIG_SPARC
17 #include <asm/prom.h>
18 #endif
19
20 #include <target/target_core_base.h>
21 #include "qla_target.h"
22
23 /*
24 *  QLogic ISP2x00 Hardware Support Function Prototypes.
25 */
26 static int qla2x00_isp_firmware(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static int qla2x00_fw_ready(scsi_qla_host_t *);
29 static int qla2x00_configure_hba(scsi_qla_host_t *);
30 static int qla2x00_configure_loop(scsi_qla_host_t *);
31 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
32 static int qla2x00_configure_fabric(scsi_qla_host_t *);
33 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
34 static int qla2x00_restart_isp(scsi_qla_host_t *);
35
36 static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37 static int qla84xx_init_chip(scsi_qla_host_t *);
38 static int qla25xx_init_queues(struct qla_hw_data *);
39 static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
40 static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41     struct event_arg *);
42 static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43     struct event_arg *);
44 static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
45
46 /* SRB Extensions ---------------------------------------------------------- */
47
48 void
49 qla2x00_sp_timeout(struct timer_list *t)
50 {
51         srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
52         struct srb_iocb *iocb;
53         scsi_qla_host_t *vha = sp->vha;
54         struct req_que *req;
55         unsigned long flags;
56
57         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
58         req = vha->hw->req_q_map[0];
59         req->outstanding_cmds[sp->handle] = NULL;
60         iocb = &sp->u.iocb_cmd;
61         iocb->timeout(sp);
62         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
63 }
64
65 void
66 qla2x00_sp_free(void *ptr)
67 {
68         srb_t *sp = ptr;
69         struct srb_iocb *iocb = &sp->u.iocb_cmd;
70
71         del_timer(&iocb->timer);
72         qla2x00_rel_sp(sp);
73 }
74
75 /* Asynchronous Login/Logout Routines -------------------------------------- */
76
77 unsigned long
78 qla2x00_get_async_timeout(struct scsi_qla_host *vha)
79 {
80         unsigned long tmo;
81         struct qla_hw_data *ha = vha->hw;
82
83         /* Firmware should use switch negotiated r_a_tov for timeout. */
84         tmo = ha->r_a_tov / 10 * 2;
85         if (IS_QLAFX00(ha)) {
86                 tmo = FX00_DEF_RATOV * 2;
87         } else if (!IS_FWI2_CAPABLE(ha)) {
88                 /*
89                  * Except for earlier ISPs where the timeout is seeded from the
90                  * initialization control block.
91                  */
92                 tmo = ha->login_timeout;
93         }
94         return tmo;
95 }
96
97 void
98 qla2x00_async_iocb_timeout(void *data)
99 {
100         srb_t *sp = data;
101         fc_port_t *fcport = sp->fcport;
102         struct srb_iocb *lio = &sp->u.iocb_cmd;
103
104         if (fcport) {
105                 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
106                     "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
107                     sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
108
109                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
110         } else {
111                 pr_info("Async-%s timeout - hdl=%x.\n",
112                     sp->name, sp->handle);
113         }
114
115         switch (sp->type) {
116         case SRB_LOGIN_CMD:
117                 /* Retry as needed. */
118                 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
119                 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
120                         QLA_LOGIO_LOGIN_RETRIED : 0;
121                 sp->done(sp, QLA_FUNCTION_TIMEOUT);
122                 break;
123         case SRB_LOGOUT_CMD:
124         case SRB_CT_PTHRU_CMD:
125         case SRB_MB_IOCB:
126         case SRB_NACK_PLOGI:
127         case SRB_NACK_PRLI:
128         case SRB_NACK_LOGO:
129         case SRB_CTRL_VP:
130                 sp->done(sp, QLA_FUNCTION_TIMEOUT);
131                 break;
132         }
133 }
134
135 static void
136 qla2x00_async_login_sp_done(void *ptr, int res)
137 {
138         srb_t *sp = ptr;
139         struct scsi_qla_host *vha = sp->vha;
140         struct srb_iocb *lio = &sp->u.iocb_cmd;
141         struct event_arg ea;
142
143         ql_dbg(ql_dbg_disc, vha, 0x20dd,
144             "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
145
146         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
147
148         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
149                 memset(&ea, 0, sizeof(ea));
150                 ea.event = FCME_PLOGI_DONE;
151                 ea.fcport = sp->fcport;
152                 ea.data[0] = lio->u.logio.data[0];
153                 ea.data[1] = lio->u.logio.data[1];
154                 ea.iop[0] = lio->u.logio.iop[0];
155                 ea.iop[1] = lio->u.logio.iop[1];
156                 ea.sp = sp;
157                 qla2x00_fcport_event_handler(vha, &ea);
158         }
159
160         sp->free(sp);
161 }
162
163 static inline bool
164 fcport_is_smaller(fc_port_t *fcport)
165 {
166         if (wwn_to_u64(fcport->port_name) <
167             wwn_to_u64(fcport->vha->port_name))
168                 return true;
169         else
170                 return false;
171 }
172
173 static inline bool
174 fcport_is_bigger(fc_port_t *fcport)
175 {
176         return !fcport_is_smaller(fcport);
177 }
178
179 int
180 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
181     uint16_t *data)
182 {
183         srb_t *sp;
184         struct srb_iocb *lio;
185         int rval = QLA_FUNCTION_FAILED;
186
187         if (!vha->flags.online)
188                 goto done;
189
190         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
191         if (!sp)
192                 goto done;
193
194         fcport->flags |= FCF_ASYNC_SENT;
195         fcport->logout_completed = 0;
196
197         fcport->disc_state = DSC_LOGIN_PEND;
198         sp->type = SRB_LOGIN_CMD;
199         sp->name = "login";
200         sp->gen1 = fcport->rscn_gen;
201         sp->gen2 = fcport->login_gen;
202
203         lio = &sp->u.iocb_cmd;
204         lio->timeout = qla2x00_async_iocb_timeout;
205         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
206
207         sp->done = qla2x00_async_login_sp_done;
208         if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport)) {
209                 lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
210         } else {
211                 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
212
213                 if (fcport->fc4f_nvme)
214                         lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
215
216         }
217
218         rval = qla2x00_start_sp(sp);
219         if (rval != QLA_SUCCESS) {
220                 fcport->flags |= FCF_LOGIN_NEEDED;
221                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
222                 goto done_free_sp;
223         }
224
225         ql_dbg(ql_dbg_disc, vha, 0x2072,
226             "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
227                 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
228             fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
229             fcport->login_retry);
230         return rval;
231
232 done_free_sp:
233         sp->free(sp);
234         fcport->flags &= ~FCF_ASYNC_SENT;
235 done:
236         fcport->flags &= ~FCF_ASYNC_ACTIVE;
237         return rval;
238 }
239
240 static void
241 qla2x00_async_logout_sp_done(void *ptr, int res)
242 {
243         srb_t *sp = ptr;
244
245         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
246         sp->fcport->login_gen++;
247         qlt_logo_completion_handler(sp->fcport, res);
248         sp->free(sp);
249 }
250
251 int
252 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
253 {
254         srb_t *sp;
255         struct srb_iocb *lio;
256         int rval = QLA_FUNCTION_FAILED;
257
258         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
259                 return rval;
260
261         fcport->flags |= FCF_ASYNC_SENT;
262         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
263         if (!sp)
264                 goto done;
265
266         sp->type = SRB_LOGOUT_CMD;
267         sp->name = "logout";
268
269         lio = &sp->u.iocb_cmd;
270         lio->timeout = qla2x00_async_iocb_timeout;
271         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
272
273         sp->done = qla2x00_async_logout_sp_done;
274         rval = qla2x00_start_sp(sp);
275         if (rval != QLA_SUCCESS)
276                 goto done_free_sp;
277
278         ql_dbg(ql_dbg_disc, vha, 0x2070,
279             "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
280             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
281                 fcport->d_id.b.area, fcport->d_id.b.al_pa,
282                 fcport->port_name);
283         return rval;
284
285 done_free_sp:
286         sp->free(sp);
287 done:
288         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
289         return rval;
290 }
291
292 void
293 qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
294     uint16_t *data)
295 {
296         fcport->flags &= ~FCF_ASYNC_ACTIVE;
297         /* Don't re-login in target mode */
298         if (!fcport->tgt_session)
299                 qla2x00_mark_device_lost(vha, fcport, 1, 0);
300         qlt_logo_completion_handler(fcport, data[0]);
301 }
302
303 static void
304 qla2x00_async_prlo_sp_done(void *s, int res)
305 {
306         srb_t *sp = (srb_t *)s;
307         struct srb_iocb *lio = &sp->u.iocb_cmd;
308         struct scsi_qla_host *vha = sp->vha;
309
310         sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
311         if (!test_bit(UNLOADING, &vha->dpc_flags))
312                 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
313                     lio->u.logio.data);
314         sp->free(sp);
315 }
316
317 int
318 qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
319 {
320         srb_t *sp;
321         struct srb_iocb *lio;
322         int rval;
323
324         rval = QLA_FUNCTION_FAILED;
325         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
326         if (!sp)
327                 goto done;
328
329         sp->type = SRB_PRLO_CMD;
330         sp->name = "prlo";
331
332         lio = &sp->u.iocb_cmd;
333         lio->timeout = qla2x00_async_iocb_timeout;
334         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
335
336         sp->done = qla2x00_async_prlo_sp_done;
337         rval = qla2x00_start_sp(sp);
338         if (rval != QLA_SUCCESS)
339                 goto done_free_sp;
340
341         ql_dbg(ql_dbg_disc, vha, 0x2070,
342             "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
343             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
344             fcport->d_id.b.area, fcport->d_id.b.al_pa);
345         return rval;
346
347 done_free_sp:
348         sp->free(sp);
349 done:
350         fcport->flags &= ~FCF_ASYNC_ACTIVE;
351         return rval;
352 }
353
354 static
355 void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
356 {
357         struct fc_port *fcport = ea->fcport;
358
359         ql_dbg(ql_dbg_disc, vha, 0x20d2,
360             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
361             __func__, fcport->port_name, fcport->disc_state,
362             fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
363             fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
364
365         if (ea->data[0] != MBS_COMMAND_COMPLETE) {
366                 ql_dbg(ql_dbg_disc, vha, 0x2066,
367                     "%s %8phC: adisc fail: post delete\n",
368                     __func__, ea->fcport->port_name);
369                 qlt_schedule_sess_for_deletion(ea->fcport);
370                 return;
371         }
372
373         if (ea->fcport->disc_state == DSC_DELETE_PEND)
374                 return;
375
376         if (ea->sp->gen2 != ea->fcport->login_gen) {
377                 /* target side must have changed it. */
378                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
379                     "%s %8phC generation changed\n",
380                     __func__, ea->fcport->port_name);
381                 return;
382         } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
383                 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
384                     __func__, __LINE__, ea->fcport->port_name);
385                 qla24xx_post_gidpn_work(vha, ea->fcport);
386                 return;
387         }
388
389         __qla24xx_handle_gpdb_event(vha, ea);
390 }
391
392 static void
393 qla2x00_async_adisc_sp_done(void *ptr, int res)
394 {
395         srb_t *sp = ptr;
396         struct scsi_qla_host *vha = sp->vha;
397         struct event_arg ea;
398         struct srb_iocb *lio = &sp->u.iocb_cmd;
399
400         ql_dbg(ql_dbg_disc, vha, 0x2066,
401             "Async done-%s res %x %8phC\n",
402             sp->name, res, sp->fcport->port_name);
403
404         sp->fcport->flags &= ~FCF_ASYNC_SENT;
405
406         memset(&ea, 0, sizeof(ea));
407         ea.event = FCME_ADISC_DONE;
408         ea.rc = res;
409         ea.data[0] = lio->u.logio.data[0];
410         ea.data[1] = lio->u.logio.data[1];
411         ea.iop[0] = lio->u.logio.iop[0];
412         ea.iop[1] = lio->u.logio.iop[1];
413         ea.fcport = sp->fcport;
414         ea.sp = sp;
415
416         qla2x00_fcport_event_handler(vha, &ea);
417
418         sp->free(sp);
419 }
420
421 int
422 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
423     uint16_t *data)
424 {
425         srb_t *sp;
426         struct srb_iocb *lio;
427         int rval;
428
429         rval = QLA_FUNCTION_FAILED;
430         fcport->flags |= FCF_ASYNC_SENT;
431         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
432         if (!sp)
433                 goto done;
434
435         sp->type = SRB_ADISC_CMD;
436         sp->name = "adisc";
437
438         lio = &sp->u.iocb_cmd;
439         lio->timeout = qla2x00_async_iocb_timeout;
440         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
441
442         sp->done = qla2x00_async_adisc_sp_done;
443         if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
444                 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
445         rval = qla2x00_start_sp(sp);
446         if (rval != QLA_SUCCESS)
447                 goto done_free_sp;
448
449         ql_dbg(ql_dbg_disc, vha, 0x206f,
450             "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
451             sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
452         return rval;
453
454 done_free_sp:
455         sp->free(sp);
456 done:
457         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
458         qla2x00_post_async_adisc_work(vha, fcport, data);
459         return rval;
460 }
461
462 static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
463         struct event_arg *ea)
464 {
465         fc_port_t *fcport, *conflict_fcport;
466         struct get_name_list_extended *e;
467         u16 i, n, found = 0, loop_id;
468         port_id_t id;
469         u64 wwn;
470         u16 data[2];
471         u8 current_login_state;
472
473         fcport = ea->fcport;
474         ql_dbg(ql_dbg_disc, vha, 0xffff,
475             "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
476             __func__, fcport->port_name, fcport->disc_state,
477             fcport->fw_login_state, ea->rc,
478             fcport->login_gen, fcport->last_login_gen,
479             fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
480
481         if (fcport->disc_state == DSC_DELETE_PEND)
482                 return;
483
484         if (ea->rc) { /* rval */
485                 if (fcport->login_retry == 0) {
486                         ql_dbg(ql_dbg_disc, vha, 0x20de,
487                             "GNL failed Port login retry %8phN, retry cnt=%d.\n",
488                             fcport->port_name, fcport->login_retry);
489                 }
490                 return;
491         }
492
493         if (fcport->last_rscn_gen != fcport->rscn_gen) {
494                 ql_dbg(ql_dbg_disc, vha, 0x20df,
495                     "%s %8phC rscn gen changed rscn %d|%d \n",
496                     __func__, fcport->port_name,
497                     fcport->last_rscn_gen, fcport->rscn_gen);
498                 qla24xx_post_gidpn_work(vha, fcport);
499                 return;
500         } else if (fcport->last_login_gen != fcport->login_gen) {
501                 ql_dbg(ql_dbg_disc, vha, 0x20e0,
502                     "%s %8phC login gen changed\n",
503                     __func__, fcport->port_name);
504                 return;
505         }
506
507         n = ea->data[0] / sizeof(struct get_name_list_extended);
508
509         ql_dbg(ql_dbg_disc, vha, 0x20e1,
510             "%s %d %8phC n %d %02x%02x%02x lid %d \n",
511             __func__, __LINE__, fcport->port_name, n,
512             fcport->d_id.b.domain, fcport->d_id.b.area,
513             fcport->d_id.b.al_pa, fcport->loop_id);
514
515         for (i = 0; i < n; i++) {
516                 e = &vha->gnl.l[i];
517                 wwn = wwn_to_u64(e->port_name);
518                 id.b.domain = e->port_id[2];
519                 id.b.area = e->port_id[1];
520                 id.b.al_pa = e->port_id[0];
521                 id.b.rsvd_1 = 0;
522
523                 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
524                         continue;
525
526                 if (IS_SW_RESV_ADDR(id))
527                         continue;
528
529                 found = 1;
530
531                 loop_id = le16_to_cpu(e->nport_handle);
532                 loop_id = (loop_id & 0x7fff);
533
534                 ql_dbg(ql_dbg_disc, vha, 0x20e2,
535                     "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
536                     __func__, fcport->port_name,
537                     e->current_login_state, fcport->fw_login_state,
538                     id.b.domain, id.b.area, id.b.al_pa,
539                     fcport->d_id.b.domain, fcport->d_id.b.area,
540                     fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
541
542                 switch (fcport->disc_state) {
543                 case DSC_DELETE_PEND:
544                 case DSC_DELETED:
545                         break;
546                 default:
547                         if ((id.b24 != fcport->d_id.b24) ||
548                             ((fcport->loop_id != FC_NO_LOOP_ID) &&
549                                 (fcport->loop_id != loop_id))) {
550                                 qlt_schedule_sess_for_deletion(fcport);
551                                 return;
552                         }
553                         break;
554                 }
555
556                 fcport->loop_id = loop_id;
557
558                 wwn = wwn_to_u64(fcport->port_name);
559                 qlt_find_sess_invalidate_other(vha, wwn,
560                         id, loop_id, &conflict_fcport);
561
562                 if (conflict_fcport) {
563                         /*
564                          * Another share fcport share the same loop_id &
565                          * nport id. Conflict fcport needs to finish
566                          * cleanup before this fcport can proceed to login.
567                          */
568                         conflict_fcport->conflict = fcport;
569                         fcport->login_pause = 1;
570                 }
571
572                 if (fcport->fc4f_nvme)
573                         current_login_state = e->current_login_state >> 4;
574                 else
575                         current_login_state = e->current_login_state & 0xf;
576
577                 switch (vha->hw->current_topology) {
578                 default:
579                         switch (current_login_state) {
580                         case DSC_LS_PRLI_COMP:
581                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
582                                     vha, 0x20e4, "%s %d %8phC post gpdb\n",
583                                     __func__, __LINE__, fcport->port_name);
584
585                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
586                                         fcport->port_type = FCT_INITIATOR;
587                                 else
588                                         fcport->port_type = FCT_TARGET;
589                                 data[0] = data[1] = 0;
590                                 qla2x00_post_async_adisc_work(vha, fcport,
591                                     data);
592                                 break;
593                         case DSC_LS_PORT_UNAVAIL:
594                         default:
595                                 if (fcport->loop_id != FC_NO_LOOP_ID)
596                                         qla2x00_clear_loop_id(fcport);
597
598                                 fcport->loop_id = loop_id;
599                                 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
600                                 qla24xx_fcport_handle_login(vha, fcport);
601                                 break;
602                         }
603                         break;
604                 case ISP_CFG_N:
605                         switch (current_login_state) {
606                         case DSC_LS_PRLI_COMP:
607                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
608                                         fcport->port_type = FCT_INITIATOR;
609                                 else
610                                         fcport->port_type = FCT_TARGET;
611
612                                 data[0] = data[1] = 0;
613                                 qla2x00_post_async_adisc_work(vha, fcport,
614                                     data);
615                                 break;
616                         case DSC_LS_PLOGI_COMP:
617                                 if (fcport_is_bigger(fcport)) {
618                                         /* local adapter is smaller */
619                                         if (fcport->loop_id != FC_NO_LOOP_ID)
620                                                 qla2x00_clear_loop_id(fcport);
621
622                                         fcport->loop_id = loop_id;
623                                         qla24xx_fcport_handle_login(vha,
624                                             fcport);
625                                         break;
626                                 }
627                                 /* drop through */
628                         default:
629                                 if (fcport_is_smaller(fcport)) {
630                                         /* local adapter is bigger */
631                                         if (fcport->loop_id != FC_NO_LOOP_ID)
632                                                 qla2x00_clear_loop_id(fcport);
633
634                                         fcport->loop_id = loop_id;
635                                         qla24xx_fcport_handle_login(vha,
636                                             fcport);
637                                 }
638                                 break;
639                         }
640                         break;
641                 } /* switch (ha->current_topology) */
642         }
643
644         if (!found) {
645                 switch (vha->hw->current_topology) {
646                 case ISP_CFG_F:
647                 case ISP_CFG_FL:
648                         for (i = 0; i < n; i++) {
649                                 e = &vha->gnl.l[i];
650                                 id.b.domain = e->port_id[0];
651                                 id.b.area = e->port_id[1];
652                                 id.b.al_pa = e->port_id[2];
653                                 id.b.rsvd_1 = 0;
654                                 loop_id = le16_to_cpu(e->nport_handle);
655
656                                 if (fcport->d_id.b24 == id.b24) {
657                                         conflict_fcport =
658                                             qla2x00_find_fcport_by_wwpn(vha,
659                                                 e->port_name, 0);
660                                         ql_dbg(ql_dbg_disc + ql_dbg_verbose,
661                                             vha, 0x20e5,
662                                             "%s %d %8phC post del sess\n",
663                                             __func__, __LINE__,
664                                             conflict_fcport->port_name);
665                                         qlt_schedule_sess_for_deletion
666                                                 (conflict_fcport);
667                                 }
668                                 /*
669                                  * FW already picked this loop id for
670                                  * another fcport
671                                  */
672                                 if (fcport->loop_id == loop_id)
673                                         fcport->loop_id = FC_NO_LOOP_ID;
674                         }
675                         qla24xx_fcport_handle_login(vha, fcport);
676                         break;
677                 case ISP_CFG_N:
678                         /*
679                          * FW handles the initial login for n2n.
680                          * Do link reinit to trigger this auto login.
681                          */
682                         set_bit(N2N_LINK_RESET, &vha->dpc_flags);
683                         qla2xxx_wake_dpc(vha);
684                         break;
685                 default:
686                         break;
687                 }
688         }
689 } /* gnl_event */
690
691 static void
692 qla24xx_async_gnl_sp_done(void *s, int res)
693 {
694         struct srb *sp = s;
695         struct scsi_qla_host *vha = sp->vha;
696         unsigned long flags;
697         struct fc_port *fcport = NULL, *tf;
698         u16 i, n = 0, loop_id;
699         struct event_arg ea;
700         struct get_name_list_extended *e;
701         u64 wwn;
702         struct list_head h;
703         bool found = false;
704
705         ql_dbg(ql_dbg_disc, vha, 0x20e7,
706             "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
707             sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
708             sp->u.iocb_cmd.u.mbx.in_mb[2]);
709
710         memset(&ea, 0, sizeof(ea));
711         ea.sp = sp;
712         ea.rc = res;
713         ea.event = FCME_GNL_DONE;
714
715         if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
716             sizeof(struct get_name_list_extended)) {
717                 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
718                     sizeof(struct get_name_list_extended);
719                 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
720         }
721
722         for (i = 0; i < n; i++) {
723                 e = &vha->gnl.l[i];
724                 loop_id = le16_to_cpu(e->nport_handle);
725                 /* mask out reserve bit */
726                 loop_id = (loop_id & 0x7fff);
727                 set_bit(loop_id, vha->hw->loop_id_map);
728                 wwn = wwn_to_u64(e->port_name);
729
730                 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
731                     "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
732                     __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
733                     e->port_id[0], e->current_login_state, e->last_login_state,
734                     (loop_id & 0x7fff));
735         }
736
737         spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
738
739         INIT_LIST_HEAD(&h);
740         fcport = tf = NULL;
741         if (!list_empty(&vha->gnl.fcports))
742                 list_splice_init(&vha->gnl.fcports, &h);
743
744         list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
745                 list_del_init(&fcport->gnl_entry);
746                 spin_lock(&vha->hw->tgt.sess_lock);
747                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
748                 spin_unlock(&vha->hw->tgt.sess_lock);
749                 ea.fcport = fcport;
750
751                 qla2x00_fcport_event_handler(vha, &ea);
752         }
753         spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
754
755         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
756         /* create new fcport if fw has knowledge of new sessions */
757         for (i = 0; i < n; i++) {
758                 port_id_t id;
759                 u64 wwnn;
760
761                 e = &vha->gnl.l[i];
762                 wwn = wwn_to_u64(e->port_name);
763
764                 found = false;
765                 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
766                         if (!memcmp((u8 *)&wwn, fcport->port_name,
767                             WWN_SIZE)) {
768                                 found = true;
769                                 break;
770                         }
771                 }
772
773                 id.b.domain = e->port_id[2];
774                 id.b.area = e->port_id[1];
775                 id.b.al_pa = e->port_id[0];
776                 id.b.rsvd_1 = 0;
777
778                 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
779                         ql_dbg(ql_dbg_disc, vha, 0x2065,
780                             "%s %d %8phC %06x post new sess\n",
781                             __func__, __LINE__, (u8 *)&wwn, id.b24);
782                         wwnn = wwn_to_u64(e->node_name);
783                         qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
784                             (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
785                 }
786         }
787
788         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
789
790         sp->free(sp);
791 }
792
793 int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
794 {
795         srb_t *sp;
796         struct srb_iocb *mbx;
797         int rval = QLA_FUNCTION_FAILED;
798         unsigned long flags;
799         u16 *mb;
800
801         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
802                 return rval;
803
804         ql_dbg(ql_dbg_disc, vha, 0x20d9,
805             "Async-gnlist WWPN %8phC \n", fcport->port_name);
806
807         spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
808         if (!list_empty(&fcport->gnl_entry)) {
809                 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
810                 rval = QLA_SUCCESS;
811                 goto done;
812         }
813
814         spin_lock(&vha->hw->tgt.sess_lock);
815         fcport->disc_state = DSC_GNL;
816         fcport->last_rscn_gen = fcport->rscn_gen;
817         fcport->last_login_gen = fcport->login_gen;
818         spin_unlock(&vha->hw->tgt.sess_lock);
819
820         list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
821         spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
822
823         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
824         if (!sp)
825                 goto done;
826
827         fcport->flags |= FCF_ASYNC_SENT;
828         sp->type = SRB_MB_IOCB;
829         sp->name = "gnlist";
830         sp->gen1 = fcport->rscn_gen;
831         sp->gen2 = fcport->login_gen;
832
833         mbx = &sp->u.iocb_cmd;
834         mbx->timeout = qla2x00_async_iocb_timeout;
835         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
836
837         mb = sp->u.iocb_cmd.u.mbx.out_mb;
838         mb[0] = MBC_PORT_NODE_NAME_LIST;
839         mb[1] = BIT_2 | BIT_3;
840         mb[2] = MSW(vha->gnl.ldma);
841         mb[3] = LSW(vha->gnl.ldma);
842         mb[6] = MSW(MSD(vha->gnl.ldma));
843         mb[7] = LSW(MSD(vha->gnl.ldma));
844         mb[8] = vha->gnl.size;
845         mb[9] = vha->vp_idx;
846
847         sp->done = qla24xx_async_gnl_sp_done;
848
849         rval = qla2x00_start_sp(sp);
850         if (rval != QLA_SUCCESS)
851                 goto done_free_sp;
852
853         ql_dbg(ql_dbg_disc, vha, 0x20da,
854             "Async-%s - OUT WWPN %8phC hndl %x\n",
855             sp->name, fcport->port_name, sp->handle);
856
857         return rval;
858
859 done_free_sp:
860         sp->free(sp);
861         fcport->flags &= ~FCF_ASYNC_SENT;
862 done:
863         return rval;
864 }
865
866 int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
867 {
868         struct qla_work_evt *e;
869
870         e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
871         if (!e)
872                 return QLA_FUNCTION_FAILED;
873
874         e->u.fcport.fcport = fcport;
875         fcport->flags |= FCF_ASYNC_ACTIVE;
876         return qla2x00_post_work(vha, e);
877 }
878
879 static
880 void qla24xx_async_gpdb_sp_done(void *s, int res)
881 {
882         struct srb *sp = s;
883         struct scsi_qla_host *vha = sp->vha;
884         struct qla_hw_data *ha = vha->hw;
885         fc_port_t *fcport = sp->fcport;
886         u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
887         struct event_arg ea;
888
889         ql_dbg(ql_dbg_disc, vha, 0x20db,
890             "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
891             sp->name, res, fcport->port_name, mb[1], mb[2]);
892
893         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
894
895         memset(&ea, 0, sizeof(ea));
896         ea.event = FCME_GPDB_DONE;
897         ea.fcport = fcport;
898         ea.sp = sp;
899
900         qla2x00_fcport_event_handler(vha, &ea);
901
902         dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
903                 sp->u.iocb_cmd.u.mbx.in_dma);
904
905         sp->free(sp);
906 }
907
908 static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
909 {
910         struct qla_work_evt *e;
911
912         e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
913         if (!e)
914                 return QLA_FUNCTION_FAILED;
915
916         e->u.fcport.fcport = fcport;
917
918         return qla2x00_post_work(vha, e);
919 }
920
921 static void
922 qla2x00_async_prli_sp_done(void *ptr, int res)
923 {
924         srb_t *sp = ptr;
925         struct scsi_qla_host *vha = sp->vha;
926         struct srb_iocb *lio = &sp->u.iocb_cmd;
927         struct event_arg ea;
928
929         ql_dbg(ql_dbg_disc, vha, 0x2129,
930             "%s %8phC res %d \n", __func__,
931             sp->fcport->port_name, res);
932
933         sp->fcport->flags &= ~FCF_ASYNC_SENT;
934
935         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
936                 memset(&ea, 0, sizeof(ea));
937                 ea.event = FCME_PRLI_DONE;
938                 ea.fcport = sp->fcport;
939                 ea.data[0] = lio->u.logio.data[0];
940                 ea.data[1] = lio->u.logio.data[1];
941                 ea.iop[0] = lio->u.logio.iop[0];
942                 ea.iop[1] = lio->u.logio.iop[1];
943                 ea.sp = sp;
944
945                 qla2x00_fcport_event_handler(vha, &ea);
946         }
947
948         sp->free(sp);
949 }
950
951 int
952 qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
953 {
954         srb_t *sp;
955         struct srb_iocb *lio;
956         int rval = QLA_FUNCTION_FAILED;
957
958         if (!vha->flags.online)
959                 return rval;
960
961         if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
962             fcport->fw_login_state == DSC_LS_PRLI_PEND)
963                 return rval;
964
965         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
966         if (!sp)
967                 return rval;
968
969         fcport->flags |= FCF_ASYNC_SENT;
970         fcport->logout_completed = 0;
971
972         sp->type = SRB_PRLI_CMD;
973         sp->name = "prli";
974
975         lio = &sp->u.iocb_cmd;
976         lio->timeout = qla2x00_async_iocb_timeout;
977         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
978
979         sp->done = qla2x00_async_prli_sp_done;
980         lio->u.logio.flags = 0;
981
982         if  (fcport->fc4f_nvme)
983                 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
984
985         rval = qla2x00_start_sp(sp);
986         if (rval != QLA_SUCCESS) {
987                 fcport->flags |= FCF_LOGIN_NEEDED;
988                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
989                 goto done_free_sp;
990         }
991
992         ql_dbg(ql_dbg_disc, vha, 0x211b,
993             "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
994             fcport->port_name, sp->handle, fcport->loop_id,
995             fcport->d_id.b24, fcport->login_retry);
996
997         return rval;
998
999 done_free_sp:
1000         sp->free(sp);
1001         fcport->flags &= ~FCF_ASYNC_SENT;
1002         return rval;
1003 }
1004
1005 int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1006 {
1007         struct qla_work_evt *e;
1008
1009         e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
1010         if (!e)
1011                 return QLA_FUNCTION_FAILED;
1012
1013         e->u.fcport.fcport = fcport;
1014         e->u.fcport.opt = opt;
1015         fcport->flags |= FCF_ASYNC_ACTIVE;
1016         return qla2x00_post_work(vha, e);
1017 }
1018
1019 int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1020 {
1021         srb_t *sp;
1022         struct srb_iocb *mbx;
1023         int rval = QLA_FUNCTION_FAILED;
1024         u16 *mb;
1025         dma_addr_t pd_dma;
1026         struct port_database_24xx *pd;
1027         struct qla_hw_data *ha = vha->hw;
1028
1029         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1030                 return rval;
1031
1032         fcport->disc_state = DSC_GPDB;
1033
1034         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1035         if (!sp)
1036                 goto done;
1037
1038         fcport->flags |= FCF_ASYNC_SENT;
1039         sp->type = SRB_MB_IOCB;
1040         sp->name = "gpdb";
1041         sp->gen1 = fcport->rscn_gen;
1042         sp->gen2 = fcport->login_gen;
1043
1044         mbx = &sp->u.iocb_cmd;
1045         mbx->timeout = qla2x00_async_iocb_timeout;
1046         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1047
1048         pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1049         if (pd == NULL) {
1050                 ql_log(ql_log_warn, vha, 0xd043,
1051                     "Failed to allocate port database structure.\n");
1052                 goto done_free_sp;
1053         }
1054
1055         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1056         mb[0] = MBC_GET_PORT_DATABASE;
1057         mb[1] = fcport->loop_id;
1058         mb[2] = MSW(pd_dma);
1059         mb[3] = LSW(pd_dma);
1060         mb[6] = MSW(MSD(pd_dma));
1061         mb[7] = LSW(MSD(pd_dma));
1062         mb[9] = vha->vp_idx;
1063         mb[10] = opt;
1064
1065         mbx->u.mbx.in = (void *)pd;
1066         mbx->u.mbx.in_dma = pd_dma;
1067
1068         sp->done = qla24xx_async_gpdb_sp_done;
1069
1070         rval = qla2x00_start_sp(sp);
1071         if (rval != QLA_SUCCESS)
1072                 goto done_free_sp;
1073
1074         ql_dbg(ql_dbg_disc, vha, 0x20dc,
1075             "Async-%s %8phC hndl %x opt %x\n",
1076             sp->name, fcport->port_name, sp->handle, opt);
1077
1078         return rval;
1079
1080 done_free_sp:
1081         if (pd)
1082                 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1083
1084         sp->free(sp);
1085         fcport->flags &= ~FCF_ASYNC_SENT;
1086 done:
1087         qla24xx_post_gpdb_work(vha, fcport, opt);
1088         return rval;
1089 }
1090
1091 static
1092 void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1093 {
1094         unsigned long flags;
1095
1096         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1097         ea->fcport->login_gen++;
1098         ea->fcport->deleted = 0;
1099         ea->fcport->logout_on_delete = 1;
1100
1101         if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1102                 vha->fcport_count++;
1103                 ea->fcport->login_succ = 1;
1104
1105                 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1106                     "%s %d %8phC post upd_fcport fcp_cnt %d\n",
1107                     __func__, __LINE__,  ea->fcport->port_name,
1108                     vha->fcport_count);
1109                 qla24xx_post_upd_fcport_work(vha, ea->fcport);
1110         } else if (ea->fcport->login_succ) {
1111                 /*
1112                  * We have an existing session. A late RSCN delivery
1113                  * must have triggered the session to be re-validate.
1114                  * Session is still valid.
1115                  */
1116                 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1117                     "%s %d %8phC session revalidate success\n",
1118                     __func__, __LINE__, ea->fcport->port_name);
1119                 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
1120         }
1121         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1122 }
1123
1124 static
1125 void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1126 {
1127         fc_port_t *fcport = ea->fcport;
1128         struct port_database_24xx *pd;
1129         struct srb *sp = ea->sp;
1130         uint8_t ls;
1131
1132         pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1133
1134         fcport->flags &= ~FCF_ASYNC_SENT;
1135
1136         ql_dbg(ql_dbg_disc, vha, 0x20d2,
1137             "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1138             fcport->disc_state, pd->current_login_state, ea->rc);
1139
1140         if (fcport->disc_state == DSC_DELETE_PEND)
1141                 return;
1142
1143         if (fcport->fc4f_nvme)
1144                 ls = pd->current_login_state >> 4;
1145         else
1146                 ls = pd->current_login_state & 0xf;
1147
1148         switch (ls) {
1149         case PDS_PRLI_COMPLETE:
1150                 __qla24xx_parse_gpdb(vha, fcport, pd);
1151                 break;
1152         case PDS_PLOGI_PENDING:
1153         case PDS_PLOGI_COMPLETE:
1154         case PDS_PRLI_PENDING:
1155         case PDS_PRLI2_PENDING:
1156                 /* Set discovery state back to GNL to Relogin attempt */
1157                 if (qla_dual_mode_enabled(vha) ||
1158                     qla_ini_mode_enabled(vha)) {
1159                         fcport->disc_state = DSC_GNL;
1160                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1161                 }
1162                 return;
1163         case PDS_LOGO_PENDING:
1164         case PDS_PORT_UNAVAILABLE:
1165         default:
1166                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1167                     __func__, __LINE__, fcport->port_name);
1168                 qlt_schedule_sess_for_deletion(fcport);
1169                 return;
1170         }
1171         __qla24xx_handle_gpdb_event(vha, ea);
1172 } /* gpdb event */
1173
1174 static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1175 {
1176         u8 login = 0;
1177         int rc;
1178
1179         if (qla_tgt_mode_enabled(vha))
1180                 return;
1181
1182         if (qla_dual_mode_enabled(vha)) {
1183                 if (N2N_TOPO(vha->hw)) {
1184                         u64 mywwn, wwn;
1185
1186                         mywwn = wwn_to_u64(vha->port_name);
1187                         wwn = wwn_to_u64(fcport->port_name);
1188                         if (mywwn > wwn)
1189                                 login = 1;
1190                         else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1191                             && time_after_eq(jiffies,
1192                                     fcport->plogi_nack_done_deadline))
1193                                 login = 1;
1194                 } else {
1195                         login = 1;
1196                 }
1197         } else {
1198                 /* initiator mode */
1199                 login = 1;
1200         }
1201
1202         if (login) {
1203                 if (fcport->loop_id == FC_NO_LOOP_ID) {
1204                         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1205                         rc = qla2x00_find_new_loop_id(vha, fcport);
1206                         if (rc) {
1207                                 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1208                                     "%s %d %8phC post del sess - out of loopid\n",
1209                                     __func__, __LINE__, fcport->port_name);
1210                                 fcport->scan_state = 0;
1211                                 qlt_schedule_sess_for_deletion(fcport);
1212                                 return;
1213                         }
1214                 }
1215                 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1216                     "%s %d %8phC post login\n",
1217                     __func__, __LINE__, fcport->port_name);
1218                 qla2x00_post_async_login_work(vha, fcport, NULL);
1219         }
1220 }
1221
1222 int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1223 {
1224         u16 data[2];
1225         u64 wwn;
1226
1227         ql_dbg(ql_dbg_disc, vha, 0x20d8,
1228             "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d retry %d lid %d scan %d\n",
1229             __func__, fcport->port_name, fcport->disc_state,
1230             fcport->fw_login_state, fcport->login_pause, fcport->flags,
1231             fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1232             fcport->login_gen, fcport->login_retry,
1233             fcport->loop_id, fcport->scan_state);
1234
1235         if (fcport->scan_state != QLA_FCPORT_FOUND)
1236                 return 0;
1237
1238         if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1239             ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1240              (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
1241                 return 0;
1242
1243         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1244                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1245                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1246                         return 0;
1247                 }
1248         }
1249
1250         /* for pure Target Mode. Login will not be initiated */
1251         if (vha->host->active_mode == MODE_TARGET)
1252                 return 0;
1253
1254         if (fcport->flags & FCF_ASYNC_SENT) {
1255                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1256                 return 0;
1257         }
1258
1259
1260         switch (fcport->disc_state) {
1261         case DSC_DELETED:
1262                 fcport->login_retry--;
1263                 wwn = wwn_to_u64(fcport->node_name);
1264                 if (wwn == 0) {
1265                         ql_dbg(ql_dbg_disc, vha, 0xffff,
1266                             "%s %d %8phC post GNNID\n",
1267                             __func__, __LINE__, fcport->port_name);
1268                         qla24xx_post_gnnid_work(vha, fcport);
1269                 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
1270                         ql_dbg(ql_dbg_disc, vha, 0x20bd,
1271                             "%s %d %8phC post gnl\n",
1272                             __func__, __LINE__, fcport->port_name);
1273                         qla24xx_post_gnl_work(vha, fcport);
1274                 } else {
1275                         fcport->login_retry--;
1276                         qla_chk_n2n_b4_login(vha, fcport);
1277                 }
1278                 break;
1279
1280         case DSC_GNL:
1281                 if (fcport->login_pause) {
1282                         fcport->last_rscn_gen = fcport->rscn_gen;
1283                         fcport->last_login_gen = fcport->login_gen;
1284                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1285                         break;
1286                 }
1287
1288                 qla_chk_n2n_b4_login(vha, fcport);
1289                 break;
1290
1291         case DSC_LOGIN_FAILED:
1292                 fcport->login_retry--;
1293                 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1294                     "%s %d %8phC post gidpn\n",
1295                     __func__, __LINE__, fcport->port_name);
1296                 if (N2N_TOPO(vha->hw))
1297                         qla_chk_n2n_b4_login(vha, fcport);
1298                 else
1299                         qla24xx_post_gidpn_work(vha, fcport);
1300                 break;
1301
1302         case DSC_LOGIN_COMPLETE:
1303                 /* recheck login state */
1304                 ql_dbg(ql_dbg_disc, vha, 0x20d1,
1305                     "%s %d %8phC post adisc\n",
1306                     __func__, __LINE__, fcport->port_name);
1307                 fcport->login_retry--;
1308                 data[0] = data[1] = 0;
1309                 qla2x00_post_async_adisc_work(vha, fcport, data);
1310                 break;
1311
1312         case DSC_LOGIN_PEND:
1313                 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1314                         qla24xx_post_prli_work(vha, fcport);
1315                 break;
1316
1317         default:
1318                 break;
1319         }
1320
1321         return 0;
1322 }
1323
1324 static
1325 void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1326 {
1327         fcport->rscn_gen++;
1328
1329         ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1330             "%s %8phC DS %d LS %d\n",
1331             __func__, fcport->port_name, fcport->disc_state,
1332             fcport->fw_login_state);
1333
1334         if (fcport->flags & FCF_ASYNC_SENT)
1335                 return;
1336
1337         switch (fcport->disc_state) {
1338         case DSC_DELETED:
1339         case DSC_LOGIN_COMPLETE:
1340                 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
1341                 break;
1342         default:
1343                 break;
1344         }
1345 }
1346
1347 int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1348     u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
1349 {
1350         struct qla_work_evt *e;
1351         e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1352         if (!e)
1353                 return QLA_FUNCTION_FAILED;
1354
1355         e->u.new_sess.id = *id;
1356         e->u.new_sess.pla = pla;
1357         e->u.new_sess.fc4_type = fc4_type;
1358         memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1359         if (node_name)
1360                 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
1361
1362         return qla2x00_post_work(vha, e);
1363 }
1364
1365 static
1366 void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1367         struct event_arg *ea)
1368 {
1369         fc_port_t *fcport = ea->fcport;
1370
1371         ql_dbg(ql_dbg_disc, vha, 0x2102,
1372             "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1373             __func__, fcport->port_name, fcport->disc_state,
1374             fcport->fw_login_state, fcport->login_pause,
1375             fcport->deleted, fcport->conflict,
1376             fcport->last_rscn_gen, fcport->rscn_gen,
1377             fcport->last_login_gen, fcport->login_gen,
1378             fcport->flags);
1379
1380         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1381             (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1382                 return;
1383
1384         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1385                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1386                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1387                         return;
1388                 }
1389         }
1390
1391         if (fcport->last_rscn_gen != fcport->rscn_gen) {
1392                 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
1393                     __func__, __LINE__, fcport->port_name);
1394
1395                 qla24xx_post_gidpn_work(vha, fcport);
1396                 return;
1397         }
1398
1399         qla24xx_fcport_handle_login(vha, fcport);
1400 }
1401
1402 void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
1403 {
1404         fc_port_t *f, *tf;
1405         uint32_t id = 0, mask, rid;
1406         unsigned long flags;
1407         fc_port_t *fcport;
1408
1409         switch (ea->event) {
1410         case FCME_RELOGIN:
1411                 if (test_bit(UNLOADING, &vha->dpc_flags))
1412                         return;
1413
1414                 qla24xx_handle_relogin_event(vha, ea);
1415                 break;
1416         case FCME_RSCN:
1417                 if (test_bit(UNLOADING, &vha->dpc_flags))
1418                         return;
1419                 switch (ea->id.b.rsvd_1) {
1420                 case RSCN_PORT_ADDR:
1421                         fcport = qla2x00_find_fcport_by_nportid
1422                                 (vha, &ea->id, 1);
1423                         if (fcport)
1424                                 fcport->rscn_rcvd = 1;
1425
1426                         spin_lock_irqsave(&vha->work_lock, flags);
1427                         if (vha->scan.scan_flags == 0) {
1428                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
1429                                     "%s: schedule\n", __func__);
1430                                 vha->scan.scan_flags |= SF_QUEUED;
1431                                 schedule_delayed_work(&vha->scan.scan_work, 5);
1432                         }
1433                         spin_unlock_irqrestore(&vha->work_lock, flags);
1434
1435                         break;
1436                 case RSCN_AREA_ADDR:
1437                 case RSCN_DOM_ADDR:
1438                         if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1439                                 mask = 0xffff00;
1440                                 ql_dbg(ql_dbg_async, vha, 0x5044,
1441                                     "RSCN: Area 0x%06x was affected\n",
1442                                     ea->id.b24);
1443                         } else {
1444                                 mask = 0xff0000;
1445                                 ql_dbg(ql_dbg_async, vha, 0x507a,
1446                                     "RSCN: Domain 0x%06x was affected\n",
1447                                     ea->id.b24);
1448                         }
1449
1450                         rid = ea->id.b24 & mask;
1451                         list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1452                             list) {
1453                                 id = f->d_id.b24 & mask;
1454                                 if (rid == id) {
1455                                         ea->fcport = f;
1456                                         qla24xx_handle_rscn_event(f, ea);
1457                                 }
1458                         }
1459                         break;
1460                 case RSCN_FAB_ADDR:
1461                 default:
1462                         ql_log(ql_log_warn, vha, 0xd045,
1463                             "RSCN: Fabric was affected. Addr format %d\n",
1464                             ea->id.b.rsvd_1);
1465                         qla2x00_mark_all_devices_lost(vha, 1);
1466                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1467                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1468                 }
1469                 break;
1470         case FCME_GIDPN_DONE:
1471                 qla24xx_handle_gidpn_event(vha, ea);
1472                 break;
1473         case FCME_GNL_DONE:
1474                 qla24xx_handle_gnl_done_event(vha, ea);
1475                 break;
1476         case FCME_GPSC_DONE:
1477                 qla24xx_handle_gpsc_event(vha, ea);
1478                 break;
1479         case FCME_PLOGI_DONE:   /* Initiator side sent LLIOCB */
1480                 qla24xx_handle_plogi_done_event(vha, ea);
1481                 break;
1482         case FCME_PRLI_DONE:
1483                 qla24xx_handle_prli_done_event(vha, ea);
1484                 break;
1485         case FCME_GPDB_DONE:
1486                 qla24xx_handle_gpdb_event(vha, ea);
1487                 break;
1488         case FCME_GPNID_DONE:
1489                 qla24xx_handle_gpnid_event(vha, ea);
1490                 break;
1491         case FCME_GFFID_DONE:
1492                 qla24xx_handle_gffid_event(vha, ea);
1493                 break;
1494         case FCME_ADISC_DONE:
1495                 qla24xx_handle_adisc_event(vha, ea);
1496                 break;
1497         case FCME_GNNID_DONE:
1498                 qla24xx_handle_gnnid_event(vha, ea);
1499                 break;
1500         case FCME_GFPNID_DONE:
1501                 qla24xx_handle_gfpnid_event(vha, ea);
1502                 break;
1503         default:
1504                 BUG_ON(1);
1505                 break;
1506         }
1507 }
1508
1509 static void
1510 qla2x00_tmf_iocb_timeout(void *data)
1511 {
1512         srb_t *sp = data;
1513         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1514
1515         tmf->u.tmf.comp_status = CS_TIMEOUT;
1516         complete(&tmf->u.tmf.comp);
1517 }
1518
1519 static void
1520 qla2x00_tmf_sp_done(void *ptr, int res)
1521 {
1522         srb_t *sp = ptr;
1523         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1524
1525         complete(&tmf->u.tmf.comp);
1526 }
1527
1528 int
1529 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
1530         uint32_t tag)
1531 {
1532         struct scsi_qla_host *vha = fcport->vha;
1533         struct srb_iocb *tm_iocb;
1534         srb_t *sp;
1535         int rval = QLA_FUNCTION_FAILED;
1536
1537         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1538         if (!sp)
1539                 goto done;
1540
1541         tm_iocb = &sp->u.iocb_cmd;
1542         sp->type = SRB_TM_CMD;
1543         sp->name = "tmf";
1544
1545         tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1546         init_completion(&tm_iocb->u.tmf.comp);
1547         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1548
1549         tm_iocb->u.tmf.flags = flags;
1550         tm_iocb->u.tmf.lun = lun;
1551         tm_iocb->u.tmf.data = tag;
1552         sp->done = qla2x00_tmf_sp_done;
1553
1554         rval = qla2x00_start_sp(sp);
1555         if (rval != QLA_SUCCESS)
1556                 goto done_free_sp;
1557
1558         ql_dbg(ql_dbg_taskm, vha, 0x802f,
1559             "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1560             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1561             fcport->d_id.b.area, fcport->d_id.b.al_pa);
1562
1563         wait_for_completion(&tm_iocb->u.tmf.comp);
1564
1565         rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1566             QLA_SUCCESS : QLA_FUNCTION_FAILED;
1567
1568         if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1569                 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1570                     "TM IOCB failed (%x).\n", rval);
1571         }
1572
1573         if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1574                 flags = tm_iocb->u.tmf.flags;
1575                 lun = (uint16_t)tm_iocb->u.tmf.lun;
1576
1577                 /* Issue Marker IOCB */
1578                 qla2x00_marker(vha, vha->hw->req_q_map[0],
1579                     vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1580                     flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1581         }
1582
1583 done_free_sp:
1584         sp->free(sp);
1585         sp->fcport->flags &= ~FCF_ASYNC_SENT;
1586 done:
1587         return rval;
1588 }
1589
1590 static void
1591 qla24xx_abort_iocb_timeout(void *data)
1592 {
1593         srb_t *sp = data;
1594         struct srb_iocb *abt = &sp->u.iocb_cmd;
1595
1596         abt->u.abt.comp_status = CS_TIMEOUT;
1597         complete(&abt->u.abt.comp);
1598 }
1599
1600 static void
1601 qla24xx_abort_sp_done(void *ptr, int res)
1602 {
1603         srb_t *sp = ptr;
1604         struct srb_iocb *abt = &sp->u.iocb_cmd;
1605
1606         if (del_timer(&sp->u.iocb_cmd.timer))
1607                 complete(&abt->u.abt.comp);
1608 }
1609
1610 int
1611 qla24xx_async_abort_cmd(srb_t *cmd_sp)
1612 {
1613         scsi_qla_host_t *vha = cmd_sp->vha;
1614         fc_port_t *fcport = cmd_sp->fcport;
1615         struct srb_iocb *abt_iocb;
1616         srb_t *sp;
1617         int rval = QLA_FUNCTION_FAILED;
1618
1619         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1620         if (!sp)
1621                 goto done;
1622
1623         abt_iocb = &sp->u.iocb_cmd;
1624         sp->type = SRB_ABT_CMD;
1625         sp->name = "abort";
1626
1627         abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1628         init_completion(&abt_iocb->u.abt.comp);
1629         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1630
1631         abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1632
1633         if (vha->flags.qpairs_available && cmd_sp->qpair)
1634                 abt_iocb->u.abt.req_que_no =
1635                     cpu_to_le16(cmd_sp->qpair->req->id);
1636         else
1637                 abt_iocb->u.abt.req_que_no = cpu_to_le16(vha->req->id);
1638
1639         sp->done = qla24xx_abort_sp_done;
1640
1641         rval = qla2x00_start_sp(sp);
1642         if (rval != QLA_SUCCESS)
1643                 goto done_free_sp;
1644
1645         ql_dbg(ql_dbg_async, vha, 0x507c,
1646             "Abort command issued - hdl=%x, target_id=%x\n",
1647             cmd_sp->handle, fcport->tgt_id);
1648
1649         wait_for_completion(&abt_iocb->u.abt.comp);
1650
1651         rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1652             QLA_SUCCESS : QLA_FUNCTION_FAILED;
1653
1654 done_free_sp:
1655         sp->free(sp);
1656 done:
1657         return rval;
1658 }
1659
1660 int
1661 qla24xx_async_abort_command(srb_t *sp)
1662 {
1663         unsigned long   flags = 0;
1664
1665         uint32_t        handle;
1666         fc_port_t       *fcport = sp->fcport;
1667         struct scsi_qla_host *vha = fcport->vha;
1668         struct qla_hw_data *ha = vha->hw;
1669         struct req_que *req = vha->req;
1670
1671         if (vha->flags.qpairs_available && sp->qpair)
1672                 req = sp->qpair->req;
1673
1674         spin_lock_irqsave(&ha->hardware_lock, flags);
1675         for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1676                 if (req->outstanding_cmds[handle] == sp)
1677                         break;
1678         }
1679         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1680         if (handle == req->num_outstanding_cmds) {
1681                 /* Command not found. */
1682                 return QLA_FUNCTION_FAILED;
1683         }
1684         if (sp->type == SRB_FXIOCB_DCMD)
1685                 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1686                     FXDISC_ABORT_IOCTL);
1687
1688         return qla24xx_async_abort_cmd(sp);
1689 }
1690
1691 static void
1692 qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1693 {
1694         switch (ea->data[0]) {
1695         case MBS_COMMAND_COMPLETE:
1696                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1697                     "%s %d %8phC post gpdb\n",
1698                     __func__, __LINE__, ea->fcport->port_name);
1699
1700                 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1701                 ea->fcport->logout_on_delete = 1;
1702                 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1703                 break;
1704         default:
1705                 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
1706                     (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
1707                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1708                         ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
1709                         break;
1710                 }
1711
1712                 if (ea->fcport->n2n_flag) {
1713                         ql_dbg(ql_dbg_disc, vha, 0x2118,
1714                                 "%s %d %8phC post fc4 prli\n",
1715                                 __func__, __LINE__, ea->fcport->port_name);
1716                         ea->fcport->fc4f_nvme = 0;
1717                         ea->fcport->n2n_flag = 0;
1718                         qla24xx_post_prli_work(vha, ea->fcport);
1719                 }
1720                 ql_dbg(ql_dbg_disc, vha, 0x2119,
1721                     "%s %d %8phC unhandle event of %x\n",
1722                     __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1723                 break;
1724         }
1725 }
1726
1727 static void
1728 qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1729 {
1730         port_id_t cid;  /* conflict Nport id */
1731         u16 lid;
1732         struct fc_port *conflict_fcport;
1733         unsigned long flags;
1734         struct fc_port *fcport = ea->fcport;
1735
1736         ql_dbg(ql_dbg_disc, vha, 0xffff,
1737             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1738             __func__, fcport->port_name, fcport->disc_state,
1739             fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1740             ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1741             ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1742
1743         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1744             (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1745                 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1746                     "%s %d %8phC Remote is trying to login\n",
1747                     __func__, __LINE__, fcport->port_name);
1748                 return;
1749         }
1750
1751         if (fcport->disc_state == DSC_DELETE_PEND)
1752                 return;
1753
1754         if (ea->sp->gen2 != fcport->login_gen) {
1755                 /* target side must have changed it. */
1756                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1757                     "%s %8phC generation changed\n",
1758                     __func__, fcport->port_name);
1759                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1760                 return;
1761         } else if (ea->sp->gen1 != fcport->rscn_gen) {
1762                 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1763                     __func__, __LINE__, fcport->port_name);
1764                 qla24xx_post_gidpn_work(vha, fcport);
1765                 return;
1766         }
1767
1768         switch (ea->data[0]) {
1769         case MBS_COMMAND_COMPLETE:
1770                 /*
1771                  * Driver must validate login state - If PRLI not complete,
1772                  * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1773                  * requests.
1774                  */
1775                 if (ea->fcport->fc4f_nvme) {
1776                         ql_dbg(ql_dbg_disc, vha, 0x2117,
1777                                 "%s %d %8phC post prli\n",
1778                                 __func__, __LINE__, ea->fcport->port_name);
1779                         qla24xx_post_prli_work(vha, ea->fcport);
1780                 } else {
1781                         ql_dbg(ql_dbg_disc, vha, 0x20ea,
1782                             "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1783                             __func__, __LINE__, ea->fcport->port_name,
1784                             ea->fcport->loop_id, ea->fcport->d_id.b24);
1785
1786                         set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1787                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1788                         ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1789                         ea->fcport->logout_on_delete = 1;
1790                         ea->fcport->send_els_logo = 0;
1791                         ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1792                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1793
1794                         qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1795                 }
1796                 break;
1797         case MBS_COMMAND_ERROR:
1798                 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
1799                     __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1800
1801                 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1802                 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1803                 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
1804                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1805                 else
1806                         qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
1807                 break;
1808         case MBS_LOOP_ID_USED:
1809                 /* data[1] = IO PARAM 1 = nport ID  */
1810                 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1811                 cid.b.area   = (ea->iop[1] >>  8) & 0xff;
1812                 cid.b.al_pa  = ea->iop[1] & 0xff;
1813                 cid.b.rsvd_1 = 0;
1814
1815                 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1816                     "%s %d %8phC LoopID 0x%x in use post gnl\n",
1817                     __func__, __LINE__, ea->fcport->port_name,
1818                     ea->fcport->loop_id);
1819
1820                 if (IS_SW_RESV_ADDR(cid)) {
1821                         set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1822                         ea->fcport->loop_id = FC_NO_LOOP_ID;
1823                 } else {
1824                         qla2x00_clear_loop_id(ea->fcport);
1825                 }
1826                 qla24xx_post_gnl_work(vha, ea->fcport);
1827                 break;
1828         case MBS_PORT_ID_USED:
1829                 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1830                     "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1831                     __func__, __LINE__, ea->fcport->port_name,
1832                     ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1833                     ea->fcport->d_id.b.al_pa);
1834
1835                 lid = ea->iop[1] & 0xffff;
1836                 qlt_find_sess_invalidate_other(vha,
1837                     wwn_to_u64(ea->fcport->port_name),
1838                     ea->fcport->d_id, lid, &conflict_fcport);
1839
1840                 if (conflict_fcport) {
1841                         /*
1842                          * Another fcport share the same loop_id/nport id.
1843                          * Conflict fcport needs to finish cleanup before this
1844                          * fcport can proceed to login.
1845                          */
1846                         conflict_fcport->conflict = ea->fcport;
1847                         ea->fcport->login_pause = 1;
1848
1849                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
1850                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1851                             __func__, __LINE__, ea->fcport->port_name,
1852                             ea->fcport->d_id.b24, lid);
1853                         qla2x00_clear_loop_id(ea->fcport);
1854                         qla24xx_post_gidpn_work(vha, ea->fcport);
1855                 } else {
1856                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
1857                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1858                             __func__, __LINE__, ea->fcport->port_name,
1859                             ea->fcport->d_id.b24, lid);
1860
1861                         qla2x00_clear_loop_id(ea->fcport);
1862                         set_bit(lid, vha->hw->loop_id_map);
1863                         ea->fcport->loop_id = lid;
1864                         ea->fcport->keep_nport_handle = 0;
1865                         qlt_schedule_sess_for_deletion(ea->fcport);
1866                 }
1867                 break;
1868         }
1869         return;
1870 }
1871
1872 void
1873 qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1874     uint16_t *data)
1875 {
1876         qlt_logo_completion_handler(fcport, data[0]);
1877         fcport->login_gen++;
1878         fcport->flags &= ~FCF_ASYNC_ACTIVE;
1879         return;
1880 }
1881
1882 void
1883 qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1884     uint16_t *data)
1885 {
1886         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1887         if (data[0] == MBS_COMMAND_COMPLETE) {
1888                 qla2x00_update_fcport(vha, fcport);
1889
1890                 return;
1891         }
1892
1893         /* Retry login. */
1894         if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1895                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1896         else
1897                 qla2x00_mark_device_lost(vha, fcport, 1, 0);
1898
1899         return;
1900 }
1901
1902 /****************************************************************************/
1903 /*                QLogic ISP2x00 Hardware Support Functions.                */
1904 /****************************************************************************/
1905
1906 static int
1907 qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1908 {
1909         int rval = QLA_SUCCESS;
1910         struct qla_hw_data *ha = vha->hw;
1911         uint32_t idc_major_ver, idc_minor_ver;
1912         uint16_t config[4];
1913
1914         qla83xx_idc_lock(vha, 0);
1915
1916         /* SV: TODO: Assign initialization timeout from
1917          * flash-info / other param
1918          */
1919         ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1920         ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1921
1922         /* Set our fcoe function presence */
1923         if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1924                 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1925                     "Error while setting DRV-Presence.\n");
1926                 rval = QLA_FUNCTION_FAILED;
1927                 goto exit;
1928         }
1929
1930         /* Decide the reset ownership */
1931         qla83xx_reset_ownership(vha);
1932
1933         /*
1934          * On first protocol driver load:
1935          * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1936          * register.
1937          * Others: Check compatibility with current IDC Major version.
1938          */
1939         qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1940         if (ha->flags.nic_core_reset_owner) {
1941                 /* Set IDC Major version */
1942                 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1943                 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1944
1945                 /* Clearing IDC-Lock-Recovery register */
1946                 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1947         } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1948                 /*
1949                  * Clear further IDC participation if we are not compatible with
1950                  * the current IDC Major Version.
1951                  */
1952                 ql_log(ql_log_warn, vha, 0xb07d,
1953                     "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1954                     idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1955                 __qla83xx_clear_drv_presence(vha);
1956                 rval = QLA_FUNCTION_FAILED;
1957                 goto exit;
1958         }
1959         /* Each function sets its supported Minor version. */
1960         qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1961         idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1962         qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1963
1964         if (ha->flags.nic_core_reset_owner) {
1965                 memset(config, 0, sizeof(config));
1966                 if (!qla81xx_get_port_config(vha, config))
1967                         qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1968                             QLA8XXX_DEV_READY);
1969         }
1970
1971         rval = qla83xx_idc_state_handler(vha);
1972
1973 exit:
1974         qla83xx_idc_unlock(vha, 0);
1975
1976         return rval;
1977 }
1978
1979 /*
1980 * qla2x00_initialize_adapter
1981 *      Initialize board.
1982 *
1983 * Input:
1984 *      ha = adapter block pointer.
1985 *
1986 * Returns:
1987 *      0 = success
1988 */
1989 int
1990 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
1991 {
1992         int     rval;
1993         struct qla_hw_data *ha = vha->hw;
1994         struct req_que *req = ha->req_q_map[0];
1995
1996         memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1997         memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1998
1999         /* Clear adapter flags. */
2000         vha->flags.online = 0;
2001         ha->flags.chip_reset_done = 0;
2002         vha->flags.reset_active = 0;
2003         ha->flags.pci_channel_io_perm_failure = 0;
2004         ha->flags.eeh_busy = 0;
2005         vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2006         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
2007         atomic_set(&vha->loop_state, LOOP_DOWN);
2008         vha->device_flags = DFLG_NO_CABLE;
2009         vha->dpc_flags = 0;
2010         vha->flags.management_server_logged_in = 0;
2011         vha->marker_needed = 0;
2012         ha->isp_abort_cnt = 0;
2013         ha->beacon_blink_led = 0;
2014
2015         set_bit(0, ha->req_qid_map);
2016         set_bit(0, ha->rsp_qid_map);
2017
2018         ql_dbg(ql_dbg_init, vha, 0x0040,
2019             "Configuring PCI space...\n");
2020         rval = ha->isp_ops->pci_config(vha);
2021         if (rval) {
2022                 ql_log(ql_log_warn, vha, 0x0044,
2023                     "Unable to configure PCI space.\n");
2024                 return (rval);
2025         }
2026
2027         ha->isp_ops->reset_chip(vha);
2028
2029         rval = qla2xxx_get_flash_info(vha);
2030         if (rval) {
2031                 ql_log(ql_log_fatal, vha, 0x004f,
2032                     "Unable to validate FLASH data.\n");
2033                 return rval;
2034         }
2035
2036         if (IS_QLA8044(ha)) {
2037                 qla8044_read_reset_template(vha);
2038
2039                 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
2040                  * If DONRESET_BIT0 is set, drivers should not set dev_state
2041                  * to NEED_RESET. But if NEED_RESET is set, drivers should
2042                  * should honor the reset. */
2043                 if (ql2xdontresethba == 1)
2044                         qla8044_set_idc_dontreset(vha);
2045         }
2046
2047         ha->isp_ops->get_flash_version(vha, req->ring);
2048         ql_dbg(ql_dbg_init, vha, 0x0061,
2049             "Configure NVRAM parameters...\n");
2050
2051         ha->isp_ops->nvram_config(vha);
2052
2053         if (ha->flags.disable_serdes) {
2054                 /* Mask HBA via NVRAM settings? */
2055                 ql_log(ql_log_info, vha, 0x0077,
2056                     "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
2057                 return QLA_FUNCTION_FAILED;
2058         }
2059
2060         ql_dbg(ql_dbg_init, vha, 0x0078,
2061             "Verifying loaded RISC code...\n");
2062
2063         if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2064                 rval = ha->isp_ops->chip_diag(vha);
2065                 if (rval)
2066                         return (rval);
2067                 rval = qla2x00_setup_chip(vha);
2068                 if (rval)
2069                         return (rval);
2070         }
2071
2072         if (IS_QLA84XX(ha)) {
2073                 ha->cs84xx = qla84xx_get_chip(vha);
2074                 if (!ha->cs84xx) {
2075                         ql_log(ql_log_warn, vha, 0x00d0,
2076                             "Unable to configure ISP84XX.\n");
2077                         return QLA_FUNCTION_FAILED;
2078                 }
2079         }
2080
2081         if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
2082                 rval = qla2x00_init_rings(vha);
2083
2084         ha->flags.chip_reset_done = 1;
2085
2086         if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
2087                 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
2088                 rval = qla84xx_init_chip(vha);
2089                 if (rval != QLA_SUCCESS) {
2090                         ql_log(ql_log_warn, vha, 0x00d4,
2091                             "Unable to initialize ISP84XX.\n");
2092                         qla84xx_put_chip(vha);
2093                 }
2094         }
2095
2096         /* Load the NIC Core f/w if we are the first protocol driver. */
2097         if (IS_QLA8031(ha)) {
2098                 rval = qla83xx_nic_core_fw_load(vha);
2099                 if (rval)
2100                         ql_log(ql_log_warn, vha, 0x0124,
2101                             "Error in initializing NIC Core f/w.\n");
2102         }
2103
2104         if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2105                 qla24xx_read_fcp_prio_cfg(vha);
2106
2107         if (IS_P3P_TYPE(ha))
2108                 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2109         else
2110                 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2111
2112         return (rval);
2113 }
2114
2115 /**
2116  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
2117  * @vha: HA context
2118  *
2119  * Returns 0 on success.
2120  */
2121 int
2122 qla2100_pci_config(scsi_qla_host_t *vha)
2123 {
2124         uint16_t w;
2125         unsigned long flags;
2126         struct qla_hw_data *ha = vha->hw;
2127         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2128
2129         pci_set_master(ha->pdev);
2130         pci_try_set_mwi(ha->pdev);
2131
2132         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2133         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2134         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2135
2136         pci_disable_rom(ha->pdev);
2137
2138         /* Get PCI bus information. */
2139         spin_lock_irqsave(&ha->hardware_lock, flags);
2140         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2141         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2142
2143         return QLA_SUCCESS;
2144 }
2145
2146 /**
2147  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2148  * @vha: HA context
2149  *
2150  * Returns 0 on success.
2151  */
2152 int
2153 qla2300_pci_config(scsi_qla_host_t *vha)
2154 {
2155         uint16_t        w;
2156         unsigned long   flags = 0;
2157         uint32_t        cnt;
2158         struct qla_hw_data *ha = vha->hw;
2159         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2160
2161         pci_set_master(ha->pdev);
2162         pci_try_set_mwi(ha->pdev);
2163
2164         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2165         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2166
2167         if (IS_QLA2322(ha) || IS_QLA6322(ha))
2168                 w &= ~PCI_COMMAND_INTX_DISABLE;
2169         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2170
2171         /*
2172          * If this is a 2300 card and not 2312, reset the
2173          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2174          * the 2310 also reports itself as a 2300 so we need to get the
2175          * fb revision level -- a 6 indicates it really is a 2300 and
2176          * not a 2310.
2177          */
2178         if (IS_QLA2300(ha)) {
2179                 spin_lock_irqsave(&ha->hardware_lock, flags);
2180
2181                 /* Pause RISC. */
2182                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2183                 for (cnt = 0; cnt < 30000; cnt++) {
2184                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2185                                 break;
2186
2187                         udelay(10);
2188                 }
2189
2190                 /* Select FPM registers. */
2191                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2192                 RD_REG_WORD(&reg->ctrl_status);
2193
2194                 /* Get the fb rev level */
2195                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
2196
2197                 if (ha->fb_rev == FPM_2300)
2198                         pci_clear_mwi(ha->pdev);
2199
2200                 /* Deselect FPM registers. */
2201                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2202                 RD_REG_WORD(&reg->ctrl_status);
2203
2204                 /* Release RISC module. */
2205                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2206                 for (cnt = 0; cnt < 30000; cnt++) {
2207                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
2208                                 break;
2209
2210                         udelay(10);
2211                 }
2212
2213                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2214         }
2215
2216         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2217
2218         pci_disable_rom(ha->pdev);
2219
2220         /* Get PCI bus information. */
2221         spin_lock_irqsave(&ha->hardware_lock, flags);
2222         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2223         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2224
2225         return QLA_SUCCESS;
2226 }
2227
2228 /**
2229  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2230  * @vha: HA context
2231  *
2232  * Returns 0 on success.
2233  */
2234 int
2235 qla24xx_pci_config(scsi_qla_host_t *vha)
2236 {
2237         uint16_t w;
2238         unsigned long flags = 0;
2239         struct qla_hw_data *ha = vha->hw;
2240         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2241
2242         pci_set_master(ha->pdev);
2243         pci_try_set_mwi(ha->pdev);
2244
2245         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2246         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2247         w &= ~PCI_COMMAND_INTX_DISABLE;
2248         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2249
2250         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2251
2252         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
2253         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2254                 pcix_set_mmrbc(ha->pdev, 2048);
2255
2256         /* PCIe -- adjust Maximum Read Request Size (2048). */
2257         if (pci_is_pcie(ha->pdev))
2258                 pcie_set_readrq(ha->pdev, 4096);
2259
2260         pci_disable_rom(ha->pdev);
2261
2262         ha->chip_revision = ha->pdev->revision;
2263
2264         /* Get PCI bus information. */
2265         spin_lock_irqsave(&ha->hardware_lock, flags);
2266         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2267         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2268
2269         return QLA_SUCCESS;
2270 }
2271
2272 /**
2273  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2274  * @vha: HA context
2275  *
2276  * Returns 0 on success.
2277  */
2278 int
2279 qla25xx_pci_config(scsi_qla_host_t *vha)
2280 {
2281         uint16_t w;
2282         struct qla_hw_data *ha = vha->hw;
2283
2284         pci_set_master(ha->pdev);
2285         pci_try_set_mwi(ha->pdev);
2286
2287         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2288         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2289         w &= ~PCI_COMMAND_INTX_DISABLE;
2290         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2291
2292         /* PCIe -- adjust Maximum Read Request Size (2048). */
2293         if (pci_is_pcie(ha->pdev))
2294                 pcie_set_readrq(ha->pdev, 4096);
2295
2296         pci_disable_rom(ha->pdev);
2297
2298         ha->chip_revision = ha->pdev->revision;
2299
2300         return QLA_SUCCESS;
2301 }
2302
2303 /**
2304  * qla2x00_isp_firmware() - Choose firmware image.
2305  * @vha: HA context
2306  *
2307  * Returns 0 on success.
2308  */
2309 static int
2310 qla2x00_isp_firmware(scsi_qla_host_t *vha)
2311 {
2312         int  rval;
2313         uint16_t loop_id, topo, sw_cap;
2314         uint8_t domain, area, al_pa;
2315         struct qla_hw_data *ha = vha->hw;
2316
2317         /* Assume loading risc code */
2318         rval = QLA_FUNCTION_FAILED;
2319
2320         if (ha->flags.disable_risc_code_load) {
2321                 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
2322
2323                 /* Verify checksum of loaded RISC code. */
2324                 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
2325                 if (rval == QLA_SUCCESS) {
2326                         /* And, verify we are not in ROM code. */
2327                         rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2328                             &area, &domain, &topo, &sw_cap);
2329                 }
2330         }
2331
2332         if (rval)
2333                 ql_dbg(ql_dbg_init, vha, 0x007a,
2334                     "**** Load RISC code ****.\n");
2335
2336         return (rval);
2337 }
2338
2339 /**
2340  * qla2x00_reset_chip() - Reset ISP chip.
2341  * @vha: HA context
2342  *
2343  * Returns 0 on success.
2344  */
2345 void
2346 qla2x00_reset_chip(scsi_qla_host_t *vha)
2347 {
2348         unsigned long   flags = 0;
2349         struct qla_hw_data *ha = vha->hw;
2350         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2351         uint32_t        cnt;
2352         uint16_t        cmd;
2353
2354         if (unlikely(pci_channel_offline(ha->pdev)))
2355                 return;
2356
2357         ha->isp_ops->disable_intrs(ha);
2358
2359         spin_lock_irqsave(&ha->hardware_lock, flags);
2360
2361         /* Turn off master enable */
2362         cmd = 0;
2363         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2364         cmd &= ~PCI_COMMAND_MASTER;
2365         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2366
2367         if (!IS_QLA2100(ha)) {
2368                 /* Pause RISC. */
2369                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2370                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2371                         for (cnt = 0; cnt < 30000; cnt++) {
2372                                 if ((RD_REG_WORD(&reg->hccr) &
2373                                     HCCR_RISC_PAUSE) != 0)
2374                                         break;
2375                                 udelay(100);
2376                         }
2377                 } else {
2378                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
2379                         udelay(10);
2380                 }
2381
2382                 /* Select FPM registers. */
2383                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2384                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2385
2386                 /* FPM Soft Reset. */
2387                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2388                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
2389
2390                 /* Toggle Fpm Reset. */
2391                 if (!IS_QLA2200(ha)) {
2392                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2393                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2394                 }
2395
2396                 /* Select frame buffer registers. */
2397                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2398                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2399
2400                 /* Reset frame buffer FIFOs. */
2401                 if (IS_QLA2200(ha)) {
2402                         WRT_FB_CMD_REG(ha, reg, 0xa000);
2403                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
2404                 } else {
2405                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
2406
2407                         /* Read back fb_cmd until zero or 3 seconds max */
2408                         for (cnt = 0; cnt < 3000; cnt++) {
2409                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2410                                         break;
2411                                 udelay(100);
2412                         }
2413                 }
2414
2415                 /* Select RISC module registers. */
2416                 WRT_REG_WORD(&reg->ctrl_status, 0);
2417                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2418
2419                 /* Reset RISC processor. */
2420                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2421                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2422
2423                 /* Release RISC processor. */
2424                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2425                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2426         }
2427
2428         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2429         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2430
2431         /* Reset ISP chip. */
2432         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2433
2434         /* Wait for RISC to recover from reset. */
2435         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2436                 /*
2437                  * It is necessary to for a delay here since the card doesn't
2438                  * respond to PCI reads during a reset. On some architectures
2439                  * this will result in an MCA.
2440                  */
2441                 udelay(20);
2442                 for (cnt = 30000; cnt; cnt--) {
2443                         if ((RD_REG_WORD(&reg->ctrl_status) &
2444                             CSR_ISP_SOFT_RESET) == 0)
2445                                 break;
2446                         udelay(100);
2447                 }
2448         } else
2449                 udelay(10);
2450
2451         /* Reset RISC processor. */
2452         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2453
2454         WRT_REG_WORD(&reg->semaphore, 0);
2455
2456         /* Release RISC processor. */
2457         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2458         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
2459
2460         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2461                 for (cnt = 0; cnt < 30000; cnt++) {
2462                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
2463                                 break;
2464
2465                         udelay(100);
2466                 }
2467         } else
2468                 udelay(100);
2469
2470         /* Turn on master enable */
2471         cmd |= PCI_COMMAND_MASTER;
2472         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2473
2474         /* Disable RISC pause on FPM parity error. */
2475         if (!IS_QLA2100(ha)) {
2476                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2477                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2478         }
2479
2480         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2481 }
2482
2483 /**
2484  * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2485  * @vha: HA context
2486  *
2487  * Returns 0 on success.
2488  */
2489 static int
2490 qla81xx_reset_mpi(scsi_qla_host_t *vha)
2491 {
2492         uint16_t mb[4] = {0x1010, 0, 1, 0};
2493
2494         if (!IS_QLA81XX(vha->hw))
2495                 return QLA_SUCCESS;
2496
2497         return qla81xx_write_mpi_register(vha, mb);
2498 }
2499
2500 /**
2501  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
2502  * @vha: HA context
2503  *
2504  * Returns 0 on success.
2505  */
2506 static inline int
2507 qla24xx_reset_risc(scsi_qla_host_t *vha)
2508 {
2509         unsigned long flags = 0;
2510         struct qla_hw_data *ha = vha->hw;
2511         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2512         uint32_t cnt;
2513         uint16_t wd;
2514         static int abts_cnt; /* ISP abort retry counts */
2515         int rval = QLA_SUCCESS;
2516
2517         spin_lock_irqsave(&ha->hardware_lock, flags);
2518
2519         /* Reset RISC. */
2520         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2521         for (cnt = 0; cnt < 30000; cnt++) {
2522                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2523                         break;
2524
2525                 udelay(10);
2526         }
2527
2528         if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2529                 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2530
2531         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2532             "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2533             RD_REG_DWORD(&reg->hccr),
2534             RD_REG_DWORD(&reg->ctrl_status),
2535             (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2536
2537         WRT_REG_DWORD(&reg->ctrl_status,
2538             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2539         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2540
2541         udelay(100);
2542
2543         /* Wait for firmware to complete NVRAM accesses. */
2544         RD_REG_WORD(&reg->mailbox0);
2545         for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2546             rval == QLA_SUCCESS; cnt--) {
2547                 barrier();
2548                 if (cnt)
2549                         udelay(5);
2550                 else
2551                         rval = QLA_FUNCTION_TIMEOUT;
2552         }
2553
2554         if (rval == QLA_SUCCESS)
2555                 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2556
2557         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2558             "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2559             RD_REG_DWORD(&reg->hccr),
2560             RD_REG_DWORD(&reg->mailbox0));
2561
2562         /* Wait for soft-reset to complete. */
2563         RD_REG_DWORD(&reg->ctrl_status);
2564         for (cnt = 0; cnt < 60; cnt++) {
2565                 barrier();
2566                 if ((RD_REG_DWORD(&reg->ctrl_status) &
2567                     CSRX_ISP_SOFT_RESET) == 0)
2568                         break;
2569
2570                 udelay(5);
2571         }
2572         if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2573                 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2574
2575         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2576             "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2577             RD_REG_DWORD(&reg->hccr),
2578             RD_REG_DWORD(&reg->ctrl_status));
2579
2580         /* If required, do an MPI FW reset now */
2581         if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2582                 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2583                         if (++abts_cnt < 5) {
2584                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2585                                 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2586                         } else {
2587                                 /*
2588                                  * We exhausted the ISP abort retries. We have to
2589                                  * set the board offline.
2590                                  */
2591                                 abts_cnt = 0;
2592                                 vha->flags.online = 0;
2593                         }
2594                 }
2595         }
2596
2597         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2598         RD_REG_DWORD(&reg->hccr);
2599
2600         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2601         RD_REG_DWORD(&reg->hccr);
2602
2603         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2604         RD_REG_DWORD(&reg->hccr);
2605
2606         RD_REG_WORD(&reg->mailbox0);
2607         for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
2608             rval == QLA_SUCCESS; cnt--) {
2609                 barrier();
2610                 if (cnt)
2611                         udelay(5);
2612                 else
2613                         rval = QLA_FUNCTION_TIMEOUT;
2614         }
2615         if (rval == QLA_SUCCESS)
2616                 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2617
2618         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2619             "Host Risc 0x%x, mailbox0 0x%x\n",
2620             RD_REG_DWORD(&reg->hccr),
2621              RD_REG_WORD(&reg->mailbox0));
2622
2623         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2624
2625         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2626             "Driver in %s mode\n",
2627             IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2628
2629         if (IS_NOPOLLING_TYPE(ha))
2630                 ha->isp_ops->enable_intrs(ha);
2631
2632         return rval;
2633 }
2634
2635 static void
2636 qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2637 {
2638         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2639
2640         WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2641         *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2642
2643 }
2644
2645 static void
2646 qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2647 {
2648         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2649
2650         WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2651         WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2652 }
2653
2654 static void
2655 qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2656 {
2657         uint32_t wd32 = 0;
2658         uint delta_msec = 100;
2659         uint elapsed_msec = 0;
2660         uint timeout_msec;
2661         ulong n;
2662
2663         if (vha->hw->pdev->subsystem_device != 0x0175 &&
2664             vha->hw->pdev->subsystem_device != 0x0240)
2665                 return;
2666
2667         WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2668         udelay(100);
2669
2670 attempt:
2671         timeout_msec = TIMEOUT_SEMAPHORE;
2672         n = timeout_msec / delta_msec;
2673         while (n--) {
2674                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2675                 qla25xx_read_risc_sema_reg(vha, &wd32);
2676                 if (wd32 & RISC_SEMAPHORE)
2677                         break;
2678                 msleep(delta_msec);
2679                 elapsed_msec += delta_msec;
2680                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2681                         goto force;
2682         }
2683
2684         if (!(wd32 & RISC_SEMAPHORE))
2685                 goto force;
2686
2687         if (!(wd32 & RISC_SEMAPHORE_FORCE))
2688                 goto acquired;
2689
2690         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2691         timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2692         n = timeout_msec / delta_msec;
2693         while (n--) {
2694                 qla25xx_read_risc_sema_reg(vha, &wd32);
2695                 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2696                         break;
2697                 msleep(delta_msec);
2698                 elapsed_msec += delta_msec;
2699                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2700                         goto force;
2701         }
2702
2703         if (wd32 & RISC_SEMAPHORE_FORCE)
2704                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2705
2706         goto attempt;
2707
2708 force:
2709         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2710
2711 acquired:
2712         return;
2713 }
2714
2715 /**
2716  * qla24xx_reset_chip() - Reset ISP24xx chip.
2717  * @vha: HA context
2718  *
2719  * Returns 0 on success.
2720  */
2721 void
2722 qla24xx_reset_chip(scsi_qla_host_t *vha)
2723 {
2724         struct qla_hw_data *ha = vha->hw;
2725
2726         if (pci_channel_offline(ha->pdev) &&
2727             ha->flags.pci_channel_io_perm_failure) {
2728                 return;
2729         }
2730
2731         ha->isp_ops->disable_intrs(ha);
2732
2733         qla25xx_manipulate_risc_semaphore(vha);
2734
2735         /* Perform RISC reset. */
2736         qla24xx_reset_risc(vha);
2737 }
2738
2739 /**
2740  * qla2x00_chip_diag() - Test chip for proper operation.
2741  * @vha: HA context
2742  *
2743  * Returns 0 on success.
2744  */
2745 int
2746 qla2x00_chip_diag(scsi_qla_host_t *vha)
2747 {
2748         int             rval;
2749         struct qla_hw_data *ha = vha->hw;
2750         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2751         unsigned long   flags = 0;
2752         uint16_t        data;
2753         uint32_t        cnt;
2754         uint16_t        mb[5];
2755         struct req_que *req = ha->req_q_map[0];
2756
2757         /* Assume a failed state */
2758         rval = QLA_FUNCTION_FAILED;
2759
2760         ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2761                &reg->flash_address);
2762
2763         spin_lock_irqsave(&ha->hardware_lock, flags);
2764
2765         /* Reset ISP chip. */
2766         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2767
2768         /*
2769          * We need to have a delay here since the card will not respond while
2770          * in reset causing an MCA on some architectures.
2771          */
2772         udelay(20);
2773         data = qla2x00_debounce_register(&reg->ctrl_status);
2774         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2775                 udelay(5);
2776                 data = RD_REG_WORD(&reg->ctrl_status);
2777                 barrier();
2778         }
2779
2780         if (!cnt)
2781                 goto chip_diag_failed;
2782
2783         ql_dbg(ql_dbg_init, vha, 0x007c,
2784             "Reset register cleared by chip reset.\n");
2785
2786         /* Reset RISC processor. */
2787         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2788         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2789
2790         /* Workaround for QLA2312 PCI parity error */
2791         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2792                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2793                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2794                         udelay(5);
2795                         data = RD_MAILBOX_REG(ha, reg, 0);
2796                         barrier();
2797                 }
2798         } else
2799                 udelay(10);
2800
2801         if (!cnt)
2802                 goto chip_diag_failed;
2803
2804         /* Check product ID of chip */
2805         ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
2806
2807         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2808         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2809         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2810         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2811         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2812             mb[3] != PROD_ID_3) {
2813                 ql_log(ql_log_warn, vha, 0x0062,
2814                     "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2815                     mb[1], mb[2], mb[3]);
2816
2817                 goto chip_diag_failed;
2818         }
2819         ha->product_id[0] = mb[1];
2820         ha->product_id[1] = mb[2];
2821         ha->product_id[2] = mb[3];
2822         ha->product_id[3] = mb[4];
2823
2824         /* Adjust fw RISC transfer size */
2825         if (req->length > 1024)
2826                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2827         else
2828                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
2829                     req->length;
2830
2831         if (IS_QLA2200(ha) &&
2832             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2833                 /* Limit firmware transfer size with a 2200A */
2834                 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
2835
2836                 ha->device_type |= DT_ISP2200A;
2837                 ha->fw_transfer_size = 128;
2838         }
2839
2840         /* Wrap Incoming Mailboxes Test. */
2841         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2842
2843         ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
2844         rval = qla2x00_mbx_reg_test(vha);
2845         if (rval)
2846                 ql_log(ql_log_warn, vha, 0x0080,
2847                     "Failed mailbox send register test.\n");
2848         else
2849                 /* Flag a successful rval */
2850                 rval = QLA_SUCCESS;
2851         spin_lock_irqsave(&ha->hardware_lock, flags);
2852
2853 chip_diag_failed:
2854         if (rval)
2855                 ql_log(ql_log_info, vha, 0x0081,
2856                     "Chip diagnostics **** FAILED ****.\n");
2857
2858         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2859
2860         return (rval);
2861 }
2862
2863 /**
2864  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2865  * @vha: HA context
2866  *
2867  * Returns 0 on success.
2868  */
2869 int
2870 qla24xx_chip_diag(scsi_qla_host_t *vha)
2871 {
2872         int rval;
2873         struct qla_hw_data *ha = vha->hw;
2874         struct req_que *req = ha->req_q_map[0];
2875
2876         if (IS_P3P_TYPE(ha))
2877                 return QLA_SUCCESS;
2878
2879         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
2880
2881         rval = qla2x00_mbx_reg_test(vha);
2882         if (rval) {
2883                 ql_log(ql_log_warn, vha, 0x0082,
2884                     "Failed mailbox send register test.\n");
2885         } else {
2886                 /* Flag a successful rval */
2887                 rval = QLA_SUCCESS;
2888         }
2889
2890         return rval;
2891 }
2892
2893 static void
2894 qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
2895 {
2896         int rval;
2897         dma_addr_t tc_dma;
2898         void *tc;
2899         struct qla_hw_data *ha = vha->hw;
2900
2901         if (ha->eft) {
2902                 ql_dbg(ql_dbg_init, vha, 0x00bd,
2903                     "%s: Offload Mem is already allocated.\n",
2904                     __func__);
2905                 return;
2906         }
2907
2908         if (IS_FWI2_CAPABLE(ha)) {
2909                 /* Allocate memory for Fibre Channel Event Buffer. */
2910                 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2911                     !IS_QLA27XX(ha))
2912                         goto try_eft;
2913
2914                 if (ha->fce)
2915                         dma_free_coherent(&ha->pdev->dev,
2916                             FCE_SIZE, ha->fce, ha->fce_dma);
2917
2918                 /* Allocate memory for Fibre Channel Event Buffer. */
2919                 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2920                                          GFP_KERNEL);
2921                 if (!tc) {
2922                         ql_log(ql_log_warn, vha, 0x00be,
2923                             "Unable to allocate (%d KB) for FCE.\n",
2924                             FCE_SIZE / 1024);
2925                         goto try_eft;
2926                 }
2927
2928                 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2929                     ha->fce_mb, &ha->fce_bufs);
2930                 if (rval) {
2931                         ql_log(ql_log_warn, vha, 0x00bf,
2932                             "Unable to initialize FCE (%d).\n", rval);
2933                         dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2934                             tc_dma);
2935                         ha->flags.fce_enabled = 0;
2936                         goto try_eft;
2937                 }
2938                 ql_dbg(ql_dbg_init, vha, 0x00c0,
2939                     "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2940
2941                 ha->flags.fce_enabled = 1;
2942                 ha->fce_dma = tc_dma;
2943                 ha->fce = tc;
2944
2945 try_eft:
2946                 if (ha->eft)
2947                         dma_free_coherent(&ha->pdev->dev,
2948                             EFT_SIZE, ha->eft, ha->eft_dma);
2949
2950                 /* Allocate memory for Extended Trace Buffer. */
2951                 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2952                                          GFP_KERNEL);
2953                 if (!tc) {
2954                         ql_log(ql_log_warn, vha, 0x00c1,
2955                             "Unable to allocate (%d KB) for EFT.\n",
2956                             EFT_SIZE / 1024);
2957                         goto eft_err;
2958                 }
2959
2960                 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2961                 if (rval) {
2962                         ql_log(ql_log_warn, vha, 0x00c2,
2963                             "Unable to initialize EFT (%d).\n", rval);
2964                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2965                             tc_dma);
2966                         goto eft_err;
2967                 }
2968                 ql_dbg(ql_dbg_init, vha, 0x00c3,
2969                     "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2970
2971                 ha->eft_dma = tc_dma;
2972                 ha->eft = tc;
2973         }
2974
2975 eft_err:
2976         return;
2977 }
2978
2979 void
2980 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2981 {
2982         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2983             eft_size, fce_size, mq_size;
2984         struct qla_hw_data *ha = vha->hw;
2985         struct req_que *req = ha->req_q_map[0];
2986         struct rsp_que *rsp = ha->rsp_q_map[0];
2987         struct qla2xxx_fw_dump *fw_dump;
2988
2989         dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2990         req_q_size = rsp_q_size = 0;
2991
2992         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2993                 fixed_size = sizeof(struct qla2100_fw_dump);
2994         } else if (IS_QLA23XX(ha)) {
2995                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2996                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2997                     sizeof(uint16_t);
2998         } else if (IS_FWI2_CAPABLE(ha)) {
2999                 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
3000                         fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
3001                 else if (IS_QLA81XX(ha))
3002                         fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
3003                 else if (IS_QLA25XX(ha))
3004                         fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
3005                 else
3006                         fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
3007
3008                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
3009                     sizeof(uint32_t);
3010                 if (ha->mqenable) {
3011                         if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
3012                                 mq_size = sizeof(struct qla2xxx_mq_chain);
3013                         /*
3014                          * Allocate maximum buffer size for all queues.
3015                          * Resizing must be done at end-of-dump processing.
3016                          */
3017                         mq_size += ha->max_req_queues *
3018                             (req->length * sizeof(request_t));
3019                         mq_size += ha->max_rsp_queues *
3020                             (rsp->length * sizeof(response_t));
3021                 }
3022                 if (ha->tgt.atio_ring)
3023                         mq_size += ha->tgt.atio_q_length * sizeof(request_t);
3024                 /* Allocate memory for Fibre Channel Event Buffer. */
3025                 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3026                     !IS_QLA27XX(ha))
3027                         goto try_eft;
3028
3029                 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
3030 try_eft:
3031                 ql_dbg(ql_dbg_init, vha, 0x00c3,
3032                     "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3033                 eft_size = EFT_SIZE;
3034         }
3035
3036         if (IS_QLA27XX(ha)) {
3037                 if (!ha->fw_dump_template) {
3038                         ql_log(ql_log_warn, vha, 0x00ba,
3039                             "Failed missing fwdump template\n");
3040                         return;
3041                 }
3042                 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
3043                 ql_dbg(ql_dbg_init, vha, 0x00fa,
3044                     "-> allocating fwdump (%x bytes)...\n", dump_size);
3045                 goto allocate;
3046         }
3047
3048         req_q_size = req->length * sizeof(request_t);
3049         rsp_q_size = rsp->length * sizeof(response_t);
3050         dump_size = offsetof(struct qla2xxx_fw_dump, isp);
3051         dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
3052         ha->chain_offset = dump_size;
3053         dump_size += mq_size + fce_size;
3054
3055         if (ha->exchoffld_buf)
3056                 dump_size += sizeof(struct qla2xxx_offld_chain) +
3057                         ha->exchoffld_size;
3058         if (ha->exlogin_buf)
3059                 dump_size += sizeof(struct qla2xxx_offld_chain) +
3060                         ha->exlogin_size;
3061
3062 allocate:
3063         if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
3064                 fw_dump = vmalloc(dump_size);
3065                 if (!fw_dump) {
3066                         ql_log(ql_log_warn, vha, 0x00c4,
3067                             "Unable to allocate (%d KB) for firmware dump.\n",
3068                             dump_size / 1024);
3069                 } else {
3070                         if (ha->fw_dump)
3071                                 vfree(ha->fw_dump);
3072                         ha->fw_dump = fw_dump;
3073
3074                         ha->fw_dump_len = dump_size;
3075                         ql_dbg(ql_dbg_init, vha, 0x00c5,
3076                             "Allocated (%d KB) for firmware dump.\n",
3077                             dump_size / 1024);
3078
3079                         if (IS_QLA27XX(ha))
3080                                 return;
3081
3082                         ha->fw_dump->signature[0] = 'Q';
3083                         ha->fw_dump->signature[1] = 'L';
3084                         ha->fw_dump->signature[2] = 'G';
3085                         ha->fw_dump->signature[3] = 'C';
3086                         ha->fw_dump->version = htonl(1);
3087
3088                         ha->fw_dump->fixed_size = htonl(fixed_size);
3089                         ha->fw_dump->mem_size = htonl(mem_size);
3090                         ha->fw_dump->req_q_size = htonl(req_q_size);
3091                         ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3092
3093                         ha->fw_dump->eft_size = htonl(eft_size);
3094                         ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3095                         ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3096
3097                         ha->fw_dump->header_size =
3098                                 htonl(offsetof(struct qla2xxx_fw_dump, isp));
3099                 }
3100         }
3101 }
3102
3103 static int
3104 qla81xx_mpi_sync(scsi_qla_host_t *vha)
3105 {
3106 #define MPS_MASK        0xe0
3107         int rval;
3108         uint16_t dc;
3109         uint32_t dw;
3110
3111         if (!IS_QLA81XX(vha->hw))
3112                 return QLA_SUCCESS;
3113
3114         rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3115         if (rval != QLA_SUCCESS) {
3116                 ql_log(ql_log_warn, vha, 0x0105,
3117                     "Unable to acquire semaphore.\n");
3118                 goto done;
3119         }
3120
3121         pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3122         rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3123         if (rval != QLA_SUCCESS) {
3124                 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
3125                 goto done_release;
3126         }
3127
3128         dc &= MPS_MASK;
3129         if (dc == (dw & MPS_MASK))
3130                 goto done_release;
3131
3132         dw &= ~MPS_MASK;
3133         dw |= dc;
3134         rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3135         if (rval != QLA_SUCCESS) {
3136                 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
3137         }
3138
3139 done_release:
3140         rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3141         if (rval != QLA_SUCCESS) {
3142                 ql_log(ql_log_warn, vha, 0x006d,
3143                     "Unable to release semaphore.\n");
3144         }
3145
3146 done:
3147         return rval;
3148 }
3149
3150 int
3151 qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3152 {
3153         /* Don't try to reallocate the array */
3154         if (req->outstanding_cmds)
3155                 return QLA_SUCCESS;
3156
3157         if (!IS_FWI2_CAPABLE(ha))
3158                 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3159         else {
3160                 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3161                         req->num_outstanding_cmds = ha->cur_fw_xcb_count;
3162                 else
3163                         req->num_outstanding_cmds = ha->cur_fw_iocb_count;
3164         }
3165
3166         req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3167                                         sizeof(srb_t *),
3168                                         GFP_KERNEL);
3169
3170         if (!req->outstanding_cmds) {
3171                 /*
3172                  * Try to allocate a minimal size just so we can get through
3173                  * initialization.
3174                  */
3175                 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3176                 req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3177                                                 sizeof(srb_t *),
3178                                                 GFP_KERNEL);
3179
3180                 if (!req->outstanding_cmds) {
3181                         ql_log(ql_log_fatal, NULL, 0x0126,
3182                             "Failed to allocate memory for "
3183                             "outstanding_cmds for req_que %p.\n", req);
3184                         req->num_outstanding_cmds = 0;
3185                         return QLA_FUNCTION_FAILED;
3186                 }
3187         }
3188
3189         return QLA_SUCCESS;
3190 }
3191
3192 #define PRINT_FIELD(_field, _flag, _str) {              \
3193         if (a0->_field & _flag) {\
3194                 if (p) {\
3195                         strcat(ptr, "|");\
3196                         ptr++;\
3197                         leftover--;\
3198                 } \
3199                 len = snprintf(ptr, leftover, "%s", _str);      \
3200                 p = 1;\
3201                 leftover -= len;\
3202                 ptr += len; \
3203         } \
3204 }
3205
3206 static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3207 {
3208 #define STR_LEN 64
3209         struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3210         u8 str[STR_LEN], *ptr, p;
3211         int leftover, len;
3212
3213         memset(str, 0, STR_LEN);
3214         snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3215         ql_dbg(ql_dbg_init, vha, 0x015a,
3216             "SFP MFG Name: %s\n", str);
3217
3218         memset(str, 0, STR_LEN);
3219         snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3220         ql_dbg(ql_dbg_init, vha, 0x015c,
3221             "SFP Part Name: %s\n", str);
3222
3223         /* media */
3224         memset(str, 0, STR_LEN);
3225         ptr = str;
3226         leftover = STR_LEN;
3227         p = len = 0;
3228         PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3229         PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3230         PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3231         PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3232         PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3233         PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3234         PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3235         ql_dbg(ql_dbg_init, vha, 0x0160,
3236             "SFP Media: %s\n", str);
3237
3238         /* link length */
3239         memset(str, 0, STR_LEN);
3240         ptr = str;
3241         leftover = STR_LEN;
3242         p = len = 0;
3243         PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3244         PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3245         PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3246         PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3247         PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3248         ql_dbg(ql_dbg_init, vha, 0x0196,
3249             "SFP Link Length: %s\n", str);
3250
3251         memset(str, 0, STR_LEN);
3252         ptr = str;
3253         leftover = STR_LEN;
3254         p = len = 0;
3255         PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3256         PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3257         PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3258         PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3259         PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3260         ql_dbg(ql_dbg_init, vha, 0x016e,
3261             "SFP FC Link Tech: %s\n", str);
3262
3263         if (a0->length_km)
3264                 ql_dbg(ql_dbg_init, vha, 0x016f,
3265                     "SFP Distant: %d km\n", a0->length_km);
3266         if (a0->length_100m)
3267                 ql_dbg(ql_dbg_init, vha, 0x0170,
3268                     "SFP Distant: %d m\n", a0->length_100m*100);
3269         if (a0->length_50um_10m)
3270                 ql_dbg(ql_dbg_init, vha, 0x0189,
3271                     "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3272         if (a0->length_62um_10m)
3273                 ql_dbg(ql_dbg_init, vha, 0x018a,
3274                   "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3275         if (a0->length_om4_10m)
3276                 ql_dbg(ql_dbg_init, vha, 0x0194,
3277                     "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3278         if (a0->length_om3_10m)
3279                 ql_dbg(ql_dbg_init, vha, 0x0195,
3280                     "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3281 }
3282
3283
3284 /*
3285  * Return Code:
3286  *   QLA_SUCCESS: no action
3287  *   QLA_INTERFACE_ERROR: SFP is not there.
3288  *   QLA_FUNCTION_FAILED: detected New SFP
3289  */
3290 int
3291 qla24xx_detect_sfp(scsi_qla_host_t *vha)
3292 {
3293         int rc = QLA_SUCCESS;
3294         struct sff_8247_a0 *a;
3295         struct qla_hw_data *ha = vha->hw;
3296
3297         if (!AUTO_DETECT_SFP_SUPPORT(vha))
3298                 goto out;
3299
3300         rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3301         if (rc)
3302                 goto out;
3303
3304         a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3305         qla2xxx_print_sfp_info(vha);
3306
3307         if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3308                 /* long range */
3309                 ha->flags.detected_lr_sfp = 1;
3310
3311                 if (a->length_km > 5 || a->length_100m > 50)
3312                         ha->long_range_distance = LR_DISTANCE_10K;
3313                 else
3314                         ha->long_range_distance = LR_DISTANCE_5K;
3315
3316                 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3317                         ql_dbg(ql_dbg_async, vha, 0x507b,
3318                             "Detected Long Range SFP.\n");
3319         } else {
3320                 /* short range */
3321                 ha->flags.detected_lr_sfp = 0;
3322                 if (ha->flags.using_lr_setting)
3323                         ql_dbg(ql_dbg_async, vha, 0x5084,
3324                             "Detected Short Range SFP.\n");
3325         }
3326
3327         if (!vha->flags.init_done)
3328                 rc = QLA_SUCCESS;
3329 out:
3330         return rc;
3331 }
3332
3333 /**
3334  * qla2x00_setup_chip() - Load and start RISC firmware.
3335  * @vha: HA context
3336  *
3337  * Returns 0 on success.
3338  */
3339 static int
3340 qla2x00_setup_chip(scsi_qla_host_t *vha)
3341 {
3342         int rval;
3343         uint32_t srisc_address = 0;
3344         struct qla_hw_data *ha = vha->hw;
3345         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3346         unsigned long flags;
3347         uint16_t fw_major_version;
3348
3349         if (IS_P3P_TYPE(ha)) {
3350                 rval = ha->isp_ops->load_risc(vha, &srisc_address);
3351                 if (rval == QLA_SUCCESS) {
3352                         qla2x00_stop_firmware(vha);
3353                         goto enable_82xx_npiv;
3354                 } else
3355                         goto failed;
3356         }
3357
3358         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3359                 /* Disable SRAM, Instruction RAM and GP RAM parity.  */
3360                 spin_lock_irqsave(&ha->hardware_lock, flags);
3361                 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3362                 RD_REG_WORD(&reg->hccr);
3363                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3364         }
3365
3366         qla81xx_mpi_sync(vha);
3367
3368         /* Load firmware sequences */
3369         rval = ha->isp_ops->load_risc(vha, &srisc_address);
3370         if (rval == QLA_SUCCESS) {
3371                 ql_dbg(ql_dbg_init, vha, 0x00c9,
3372                     "Verifying Checksum of loaded RISC code.\n");
3373
3374                 rval = qla2x00_verify_checksum(vha, srisc_address);
3375                 if (rval == QLA_SUCCESS) {
3376                         /* Start firmware execution. */
3377                         ql_dbg(ql_dbg_init, vha, 0x00ca,
3378                             "Starting firmware.\n");
3379
3380                         if (ql2xexlogins)
3381                                 ha->flags.exlogins_enabled = 1;
3382
3383                         if (qla_is_exch_offld_enabled(vha))
3384                                 ha->flags.exchoffld_enabled = 1;
3385
3386                         rval = qla2x00_execute_fw(vha, srisc_address);
3387                         /* Retrieve firmware information. */
3388                         if (rval == QLA_SUCCESS) {
3389                                 qla24xx_detect_sfp(vha);
3390
3391                                 rval = qla2x00_set_exlogins_buffer(vha);
3392                                 if (rval != QLA_SUCCESS)
3393                                         goto failed;
3394
3395                                 rval = qla2x00_set_exchoffld_buffer(vha);
3396                                 if (rval != QLA_SUCCESS)
3397                                         goto failed;
3398
3399 enable_82xx_npiv:
3400                                 fw_major_version = ha->fw_major_version;
3401                                 if (IS_P3P_TYPE(ha))
3402                                         qla82xx_check_md_needed(vha);
3403                                 else
3404                                         rval = qla2x00_get_fw_version(vha);
3405                                 if (rval != QLA_SUCCESS)
3406                                         goto failed;
3407                                 ha->flags.npiv_supported = 0;
3408                                 if (IS_QLA2XXX_MIDTYPE(ha) &&
3409                                          (ha->fw_attributes & BIT_2)) {
3410                                         ha->flags.npiv_supported = 1;
3411                                         if ((!ha->max_npiv_vports) ||
3412                                             ((ha->max_npiv_vports + 1) %
3413                                             MIN_MULTI_ID_FABRIC))
3414                                                 ha->max_npiv_vports =
3415                                                     MIN_MULTI_ID_FABRIC - 1;
3416                                 }
3417                                 qla2x00_get_resource_cnts(vha);
3418
3419                                 /*
3420                                  * Allocate the array of outstanding commands
3421                                  * now that we know the firmware resources.
3422                                  */
3423                                 rval = qla2x00_alloc_outstanding_cmds(ha,
3424                                     vha->req);
3425                                 if (rval != QLA_SUCCESS)
3426                                         goto failed;
3427
3428                                 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3429                                         qla2x00_alloc_offload_mem(vha);
3430
3431                                 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
3432                                         qla2x00_alloc_fw_dump(vha);
3433
3434                         } else {
3435                                 goto failed;
3436                         }
3437                 } else {
3438                         ql_log(ql_log_fatal, vha, 0x00cd,
3439                             "ISP Firmware failed checksum.\n");
3440                         goto failed;
3441                 }
3442         } else
3443                 goto failed;
3444
3445         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3446                 /* Enable proper parity. */
3447                 spin_lock_irqsave(&ha->hardware_lock, flags);
3448                 if (IS_QLA2300(ha))
3449                         /* SRAM parity */
3450                         WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3451                 else
3452                         /* SRAM, Instruction RAM and GP RAM parity */
3453                         WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3454                 RD_REG_WORD(&reg->hccr);
3455                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3456         }
3457
3458         if (IS_QLA27XX(ha))
3459                 ha->flags.fac_supported = 1;
3460         else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
3461                 uint32_t size;
3462
3463                 rval = qla81xx_fac_get_sector_size(vha, &size);
3464                 if (rval == QLA_SUCCESS) {
3465                         ha->flags.fac_supported = 1;
3466                         ha->fdt_block_size = size << 2;
3467                 } else {
3468                         ql_log(ql_log_warn, vha, 0x00ce,
3469                             "Unsupported FAC firmware (%d.%02d.%02d).\n",
3470                             ha->fw_major_version, ha->fw_minor_version,
3471                             ha->fw_subminor_version);
3472
3473                         if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3474                                 ha->flags.fac_supported = 0;
3475                                 rval = QLA_SUCCESS;
3476                         }
3477                 }
3478         }
3479 failed:
3480         if (rval) {
3481                 ql_log(ql_log_fatal, vha, 0x00cf,
3482                     "Setup chip ****FAILED****.\n");
3483         }
3484
3485         return (rval);
3486 }
3487
3488 /**
3489  * qla2x00_init_response_q_entries() - Initializes response queue entries.
3490  * @rsp: response queue
3491  *
3492  * Beginning of request ring has initialization control block already built
3493  * by nvram config routine.
3494  *
3495  * Returns 0 on success.
3496  */
3497 void
3498 qla2x00_init_response_q_entries(struct rsp_que *rsp)
3499 {
3500         uint16_t cnt;
3501         response_t *pkt;
3502
3503         rsp->ring_ptr = rsp->ring;
3504         rsp->ring_index    = 0;
3505         rsp->status_srb = NULL;
3506         pkt = rsp->ring_ptr;
3507         for (cnt = 0; cnt < rsp->length; cnt++) {
3508                 pkt->signature = RESPONSE_PROCESSED;
3509                 pkt++;
3510         }
3511 }
3512
3513 /**
3514  * qla2x00_update_fw_options() - Read and process firmware options.
3515  * @vha: HA context
3516  *
3517  * Returns 0 on success.
3518  */
3519 void
3520 qla2x00_update_fw_options(scsi_qla_host_t *vha)
3521 {
3522         uint16_t swing, emphasis, tx_sens, rx_sens;
3523         struct qla_hw_data *ha = vha->hw;
3524
3525         memset(ha->fw_options, 0, sizeof(ha->fw_options));
3526         qla2x00_get_fw_options(vha, ha->fw_options);
3527
3528         if (IS_QLA2100(ha) || IS_QLA2200(ha))
3529                 return;
3530
3531         /* Serial Link options. */
3532         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3533             "Serial link options.\n");
3534         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3535             (uint8_t *)&ha->fw_seriallink_options,
3536             sizeof(ha->fw_seriallink_options));
3537
3538         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3539         if (ha->fw_seriallink_options[3] & BIT_2) {
3540                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3541
3542                 /*  1G settings */
3543                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3544                 emphasis = (ha->fw_seriallink_options[2] &
3545                     (BIT_4 | BIT_3)) >> 3;
3546                 tx_sens = ha->fw_seriallink_options[0] &
3547                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3548                 rx_sens = (ha->fw_seriallink_options[0] &
3549                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3550                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3551                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3552                         if (rx_sens == 0x0)
3553                                 rx_sens = 0x3;
3554                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3555                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3556                         ha->fw_options[10] |= BIT_5 |
3557                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3558                             (tx_sens & (BIT_1 | BIT_0));
3559
3560                 /*  2G settings */
3561                 swing = (ha->fw_seriallink_options[2] &
3562                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
3563                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3564                 tx_sens = ha->fw_seriallink_options[1] &
3565                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3566                 rx_sens = (ha->fw_seriallink_options[1] &
3567                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3568                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3569                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3570                         if (rx_sens == 0x0)
3571                                 rx_sens = 0x3;
3572                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3573                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3574                         ha->fw_options[11] |= BIT_5 |
3575                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3576                             (tx_sens & (BIT_1 | BIT_0));
3577         }
3578
3579         /* FCP2 options. */
3580         /*  Return command IOCBs without waiting for an ABTS to complete. */
3581         ha->fw_options[3] |= BIT_13;
3582
3583         /* LED scheme. */
3584         if (ha->flags.enable_led_scheme)
3585                 ha->fw_options[2] |= BIT_12;
3586
3587         /* Detect ISP6312. */
3588         if (IS_QLA6312(ha))
3589                 ha->fw_options[2] |= BIT_13;
3590
3591         /* Set Retry FLOGI in case of P2P connection */
3592         if (ha->operating_mode == P2P) {
3593                 ha->fw_options[2] |= BIT_3;
3594                 ql_dbg(ql_dbg_disc, vha, 0x2100,
3595                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3596                         __func__, ha->fw_options[2]);
3597         }
3598
3599         /* Update firmware options. */
3600         qla2x00_set_fw_options(vha, ha->fw_options);
3601 }
3602
3603 void
3604 qla24xx_update_fw_options(scsi_qla_host_t *vha)
3605 {
3606         int rval;
3607         struct qla_hw_data *ha = vha->hw;
3608
3609         if (IS_P3P_TYPE(ha))
3610                 return;
3611
3612         /*  Hold status IOCBs until ABTS response received. */
3613         if (ql2xfwholdabts)
3614                 ha->fw_options[3] |= BIT_12;
3615
3616         /* Set Retry FLOGI in case of P2P connection */
3617         if (ha->operating_mode == P2P) {
3618                 ha->fw_options[2] |= BIT_3;
3619                 ql_dbg(ql_dbg_disc, vha, 0x2101,
3620                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3621                         __func__, ha->fw_options[2]);
3622         }
3623
3624         /* Move PUREX, ABTS RX & RIDA to ATIOQ */
3625         if (ql2xmvasynctoatio &&
3626             (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
3627                 if (qla_tgt_mode_enabled(vha) ||
3628                     qla_dual_mode_enabled(vha))
3629                         ha->fw_options[2] |= BIT_11;
3630                 else
3631                         ha->fw_options[2] &= ~BIT_11;
3632         }
3633
3634         if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3635                 /*
3636                  * Tell FW to track each exchange to prevent
3637                  * driver from using stale exchange.
3638                  */
3639                 if (qla_tgt_mode_enabled(vha) ||
3640                     qla_dual_mode_enabled(vha))
3641                         ha->fw_options[2] |= BIT_4;
3642                 else
3643                         ha->fw_options[2] &= ~BIT_4;
3644
3645                 /* Reserve 1/2 of emergency exchanges for ELS.*/
3646                 if (qla2xuseresexchforels)
3647                         ha->fw_options[2] |= BIT_8;
3648                 else
3649                         ha->fw_options[2] &= ~BIT_8;
3650         }
3651
3652         ql_dbg(ql_dbg_init, vha, 0x00e8,
3653             "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3654             __func__, ha->fw_options[1], ha->fw_options[2],
3655             ha->fw_options[3], vha->host->active_mode);
3656
3657         if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3658                 qla2x00_set_fw_options(vha, ha->fw_options);
3659
3660         /* Update Serial Link options. */
3661         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
3662                 return;
3663
3664         rval = qla2x00_set_serdes_params(vha,
3665             le16_to_cpu(ha->fw_seriallink_options24[1]),
3666             le16_to_cpu(ha->fw_seriallink_options24[2]),
3667             le16_to_cpu(ha->fw_seriallink_options24[3]));
3668         if (rval != QLA_SUCCESS) {
3669                 ql_log(ql_log_warn, vha, 0x0104,
3670                     "Unable to update Serial Link options (%x).\n", rval);
3671         }
3672 }
3673
3674 void
3675 qla2x00_config_rings(struct scsi_qla_host *vha)
3676 {
3677         struct qla_hw_data *ha = vha->hw;
3678         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3679         struct req_que *req = ha->req_q_map[0];
3680         struct rsp_que *rsp = ha->rsp_q_map[0];
3681
3682         /* Setup ring parameters in initialization control block. */
3683         ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3684         ha->init_cb->response_q_inpointer = cpu_to_le16(0);
3685         ha->init_cb->request_q_length = cpu_to_le16(req->length);
3686         ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3687         ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3688         ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3689         ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3690         ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
3691
3692         WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3693         WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3694         WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3695         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3696         RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
3697 }
3698
3699 void
3700 qla24xx_config_rings(struct scsi_qla_host *vha)
3701 {
3702         struct qla_hw_data *ha = vha->hw;
3703         device_reg_t *reg = ISP_QUE_REG(ha, 0);
3704         struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3705         struct qla_msix_entry *msix;
3706         struct init_cb_24xx *icb;
3707         uint16_t rid = 0;
3708         struct req_que *req = ha->req_q_map[0];
3709         struct rsp_que *rsp = ha->rsp_q_map[0];
3710
3711         /* Setup ring parameters in initialization control block. */
3712         icb = (struct init_cb_24xx *)ha->init_cb;
3713         icb->request_q_outpointer = cpu_to_le16(0);
3714         icb->response_q_inpointer = cpu_to_le16(0);
3715         icb->request_q_length = cpu_to_le16(req->length);
3716         icb->response_q_length = cpu_to_le16(rsp->length);
3717         icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3718         icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3719         icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3720         icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
3721
3722         /* Setup ATIO queue dma pointers for target mode */
3723         icb->atio_q_inpointer = cpu_to_le16(0);
3724         icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3725         icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3726         icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3727
3728         if (IS_SHADOW_REG_CAPABLE(ha))
3729                 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
3730
3731         if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3732                 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3733                 icb->rid = cpu_to_le16(rid);
3734                 if (ha->flags.msix_enabled) {
3735                         msix = &ha->msix_entries[1];
3736                         ql_dbg(ql_dbg_init, vha, 0x0019,
3737                             "Registering vector 0x%x for base que.\n",
3738                             msix->entry);
3739                         icb->msix = cpu_to_le16(msix->entry);
3740                 }
3741                 /* Use alternate PCI bus number */
3742                 if (MSB(rid))
3743                         icb->firmware_options_2 |= cpu_to_le32(BIT_19);
3744                 /* Use alternate PCI devfn */
3745                 if (LSB(rid))
3746                         icb->firmware_options_2 |= cpu_to_le32(BIT_18);
3747
3748                 /* Use Disable MSIX Handshake mode for capable adapters */
3749                 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3750                     (ha->flags.msix_enabled)) {
3751                         icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
3752                         ha->flags.disable_msix_handshake = 1;
3753                         ql_dbg(ql_dbg_init, vha, 0x00fe,
3754                             "MSIX Handshake Disable Mode turned on.\n");
3755                 } else {
3756                         icb->firmware_options_2 |= cpu_to_le32(BIT_22);
3757                 }
3758                 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
3759
3760                 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3761                 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3762                 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3763                 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3764         } else {
3765                 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3766                 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3767                 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3768                 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3769         }
3770         qlt_24xx_config_rings(vha);
3771
3772         /* PCI posting */
3773         RD_REG_DWORD(&ioreg->hccr);
3774 }
3775
3776 /**
3777  * qla2x00_init_rings() - Initializes firmware.
3778  * @vha: HA context
3779  *
3780  * Beginning of request ring has initialization control block already built
3781  * by nvram config routine.
3782  *
3783  * Returns 0 on success.
3784  */
3785 int
3786 qla2x00_init_rings(scsi_qla_host_t *vha)
3787 {
3788         int     rval;
3789         unsigned long flags = 0;
3790         int cnt, que;
3791         struct qla_hw_data *ha = vha->hw;
3792         struct req_que *req;
3793         struct rsp_que *rsp;
3794         struct mid_init_cb_24xx *mid_init_cb =
3795             (struct mid_init_cb_24xx *) ha->init_cb;
3796
3797         spin_lock_irqsave(&ha->hardware_lock, flags);
3798
3799         /* Clear outstanding commands array. */
3800         for (que = 0; que < ha->max_req_queues; que++) {
3801                 req = ha->req_q_map[que];
3802                 if (!req || !test_bit(que, ha->req_qid_map))
3803                         continue;
3804                 req->out_ptr = (void *)(req->ring + req->length);
3805                 *req->out_ptr = 0;
3806                 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
3807                         req->outstanding_cmds[cnt] = NULL;
3808
3809                 req->current_outstanding_cmd = 1;
3810
3811                 /* Initialize firmware. */
3812                 req->ring_ptr  = req->ring;
3813                 req->ring_index    = 0;
3814                 req->cnt      = req->length;
3815         }
3816
3817         for (que = 0; que < ha->max_rsp_queues; que++) {
3818                 rsp = ha->rsp_q_map[que];
3819                 if (!rsp || !test_bit(que, ha->rsp_qid_map))
3820                         continue;
3821                 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3822                 *rsp->in_ptr = 0;
3823                 /* Initialize response queue entries */
3824                 if (IS_QLAFX00(ha))
3825                         qlafx00_init_response_q_entries(rsp);
3826                 else
3827                         qla2x00_init_response_q_entries(rsp);
3828         }
3829
3830         ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3831         ha->tgt.atio_ring_index = 0;
3832         /* Initialize ATIO queue entries */
3833         qlt_init_atio_q_entries(vha);
3834
3835         ha->isp_ops->config_rings(vha);
3836
3837         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3838
3839         ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3840
3841         if (IS_QLAFX00(ha)) {
3842                 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3843                 goto next_check;
3844         }
3845
3846         /* Update any ISP specific firmware options before initialization. */
3847         ha->isp_ops->update_fw_options(vha);
3848
3849         if (ha->flags.npiv_supported) {
3850                 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
3851                         ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
3852                 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
3853         }
3854
3855         if (IS_FWI2_CAPABLE(ha)) {
3856                 mid_init_cb->options = cpu_to_le16(BIT_1);
3857                 mid_init_cb->init_cb.execution_throttle =
3858                     cpu_to_le16(ha->cur_fw_xcb_count);
3859                 ha->flags.dport_enabled =
3860                     (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3861                 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3862                     (ha->flags.dport_enabled) ? "enabled" : "disabled");
3863                 /* FA-WWPN Status */
3864                 ha->flags.fawwpn_enabled =
3865                     (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
3866                 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
3867                     (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
3868         }
3869
3870         rval = qla2x00_init_firmware(vha, ha->init_cb_size);
3871 next_check:
3872         if (rval) {
3873                 ql_log(ql_log_fatal, vha, 0x00d2,
3874                     "Init Firmware **** FAILED ****.\n");
3875         } else {
3876                 ql_dbg(ql_dbg_init, vha, 0x00d3,
3877                     "Init Firmware -- success.\n");
3878                 QLA_FW_STARTED(ha);
3879         }
3880
3881         return (rval);
3882 }
3883
3884 /**
3885  * qla2x00_fw_ready() - Waits for firmware ready.
3886  * @vha: HA context
3887  *
3888  * Returns 0 on success.
3889  */
3890 static int
3891 qla2x00_fw_ready(scsi_qla_host_t *vha)
3892 {
3893         int             rval;
3894         unsigned long   wtime, mtime, cs84xx_time;
3895         uint16_t        min_wait;       /* Minimum wait time if loop is down */
3896         uint16_t        wait_time;      /* Wait time if loop is coming ready */
3897         uint16_t        state[6];
3898         struct qla_hw_data *ha = vha->hw;
3899
3900         if (IS_QLAFX00(vha->hw))
3901                 return qlafx00_fw_ready(vha);
3902
3903         rval = QLA_SUCCESS;
3904
3905         /* Time to wait for loop down */
3906         if (IS_P3P_TYPE(ha))
3907                 min_wait = 30;
3908         else
3909                 min_wait = 20;
3910
3911         /*
3912          * Firmware should take at most one RATOV to login, plus 5 seconds for
3913          * our own processing.
3914          */
3915         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3916                 wait_time = min_wait;
3917         }
3918
3919         /* Min wait time if loop down */
3920         mtime = jiffies + (min_wait * HZ);
3921
3922         /* wait time before firmware ready */
3923         wtime = jiffies + (wait_time * HZ);
3924
3925         /* Wait for ISP to finish LIP */
3926         if (!vha->flags.init_done)
3927                 ql_log(ql_log_info, vha, 0x801e,
3928                     "Waiting for LIP to complete.\n");
3929
3930         do {
3931                 memset(state, -1, sizeof(state));
3932                 rval = qla2x00_get_firmware_state(vha, state);
3933                 if (rval == QLA_SUCCESS) {
3934                         if (state[0] < FSTATE_LOSS_OF_SYNC) {
3935                                 vha->device_flags &= ~DFLG_NO_CABLE;
3936                         }
3937                         if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
3938                                 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3939                                     "fw_state=%x 84xx=%x.\n", state[0],
3940                                     state[2]);
3941                                 if ((state[2] & FSTATE_LOGGED_IN) &&
3942                                      (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
3943                                         ql_dbg(ql_dbg_taskm, vha, 0x8028,
3944                                             "Sending verify iocb.\n");
3945
3946                                         cs84xx_time = jiffies;
3947                                         rval = qla84xx_init_chip(vha);
3948                                         if (rval != QLA_SUCCESS) {
3949                                                 ql_log(ql_log_warn,
3950                                                     vha, 0x8007,
3951                                                     "Init chip failed.\n");
3952                                                 break;
3953                                         }
3954
3955                                         /* Add time taken to initialize. */
3956                                         cs84xx_time = jiffies - cs84xx_time;
3957                                         wtime += cs84xx_time;
3958                                         mtime += cs84xx_time;
3959                                         ql_dbg(ql_dbg_taskm, vha, 0x8008,
3960                                             "Increasing wait time by %ld. "
3961                                             "New time %ld.\n", cs84xx_time,
3962                                             wtime);
3963                                 }
3964                         } else if (state[0] == FSTATE_READY) {
3965                                 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3966                                     "F/W Ready - OK.\n");
3967
3968                                 qla2x00_get_retry_cnt(vha, &ha->retry_count,
3969                                     &ha->login_timeout, &ha->r_a_tov);
3970
3971                                 rval = QLA_SUCCESS;
3972                                 break;
3973                         }
3974
3975                         rval = QLA_FUNCTION_FAILED;
3976
3977                         if (atomic_read(&vha->loop_down_timer) &&
3978                             state[0] != FSTATE_READY) {
3979                                 /* Loop down. Timeout on min_wait for states
3980                                  * other than Wait for Login.
3981                                  */
3982                                 if (time_after_eq(jiffies, mtime)) {
3983                                         ql_log(ql_log_info, vha, 0x8038,
3984                                             "Cable is unplugged...\n");
3985
3986                                         vha->device_flags |= DFLG_NO_CABLE;
3987                                         break;
3988                                 }
3989                         }
3990                 } else {
3991                         /* Mailbox cmd failed. Timeout on min_wait. */
3992                         if (time_after_eq(jiffies, mtime) ||
3993                                 ha->flags.isp82xx_fw_hung)
3994                                 break;
3995                 }
3996
3997                 if (time_after_eq(jiffies, wtime))
3998                         break;
3999
4000                 /* Delay for a while */
4001                 msleep(500);
4002         } while (1);
4003
4004         ql_dbg(ql_dbg_taskm, vha, 0x803a,
4005             "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
4006             state[1], state[2], state[3], state[4], state[5], jiffies);
4007
4008         if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
4009                 ql_log(ql_log_warn, vha, 0x803b,
4010                     "Firmware ready **** FAILED ****.\n");
4011         }
4012
4013         return (rval);
4014 }
4015
4016 /*
4017 *  qla2x00_configure_hba
4018 *      Setup adapter context.
4019 *
4020 * Input:
4021 *      ha = adapter state pointer.
4022 *
4023 * Returns:
4024 *      0 = success
4025 *
4026 * Context:
4027 *      Kernel context.
4028 */
4029 static int
4030 qla2x00_configure_hba(scsi_qla_host_t *vha)
4031 {
4032         int       rval;
4033         uint16_t      loop_id;
4034         uint16_t      topo;
4035         uint16_t      sw_cap;
4036         uint8_t       al_pa;
4037         uint8_t       area;
4038         uint8_t       domain;
4039         char            connect_type[22];
4040         struct qla_hw_data *ha = vha->hw;
4041         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
4042         port_id_t id;
4043         unsigned long flags;
4044
4045         /* Get host addresses. */
4046         rval = qla2x00_get_adapter_id(vha,
4047             &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
4048         if (rval != QLA_SUCCESS) {
4049                 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
4050                     IS_CNA_CAPABLE(ha) ||
4051                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
4052                         ql_dbg(ql_dbg_disc, vha, 0x2008,
4053                             "Loop is in a transition state.\n");
4054                 } else {
4055                         ql_log(ql_log_warn, vha, 0x2009,
4056                             "Unable to get host loop ID.\n");
4057                         if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
4058                             (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4059                                 ql_log(ql_log_warn, vha, 0x1151,
4060                                     "Doing link init.\n");
4061                                 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4062                                         return rval;
4063                         }
4064                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4065                 }
4066                 return (rval);
4067         }
4068
4069         if (topo == 4) {
4070                 ql_log(ql_log_info, vha, 0x200a,
4071                     "Cannot get topology - retrying.\n");
4072                 return (QLA_FUNCTION_FAILED);
4073         }
4074
4075         vha->loop_id = loop_id;
4076
4077         /* initialize */
4078         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4079         ha->operating_mode = LOOP;
4080         ha->switch_cap = 0;
4081
4082         switch (topo) {
4083         case 0:
4084                 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
4085                 ha->current_topology = ISP_CFG_NL;
4086                 strcpy(connect_type, "(Loop)");
4087                 break;
4088
4089         case 1:
4090                 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
4091                 ha->switch_cap = sw_cap;
4092                 ha->current_topology = ISP_CFG_FL;
4093                 strcpy(connect_type, "(FL_Port)");
4094                 break;
4095
4096         case 2:
4097                 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
4098                 ha->operating_mode = P2P;
4099                 ha->current_topology = ISP_CFG_N;
4100                 strcpy(connect_type, "(N_Port-to-N_Port)");
4101                 break;
4102
4103         case 3:
4104                 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
4105                 ha->switch_cap = sw_cap;
4106                 ha->operating_mode = P2P;
4107                 ha->current_topology = ISP_CFG_F;
4108                 strcpy(connect_type, "(F_Port)");
4109                 break;
4110
4111         default:
4112                 ql_dbg(ql_dbg_disc, vha, 0x200f,
4113                     "HBA in unknown topology %x, using NL.\n", topo);
4114                 ha->current_topology = ISP_CFG_NL;
4115                 strcpy(connect_type, "(Loop)");
4116                 break;
4117         }
4118
4119         /* Save Host port and loop ID. */
4120         /* byte order - Big Endian */
4121         id.b.domain = domain;
4122         id.b.area = area;
4123         id.b.al_pa = al_pa;
4124         id.b.rsvd_1 = 0;
4125         spin_lock_irqsave(&ha->hardware_lock, flags);
4126         qlt_update_host_map(vha, id);
4127         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4128
4129         if (!vha->flags.init_done)
4130                 ql_log(ql_log_info, vha, 0x2010,
4131                     "Topology - %s, Host Loop address 0x%x.\n",
4132                     connect_type, vha->loop_id);
4133
4134         return(rval);
4135 }
4136
4137 inline void
4138 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4139         char *def)
4140 {
4141         char *st, *en;
4142         uint16_t index;
4143         struct qla_hw_data *ha = vha->hw;
4144         int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
4145             !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
4146
4147         if (memcmp(model, BINZERO, len) != 0) {
4148                 strncpy(ha->model_number, model, len);
4149                 st = en = ha->model_number;
4150                 en += len - 1;
4151                 while (en > st) {
4152                         if (*en != 0x20 && *en != 0x00)
4153                                 break;
4154                         *en-- = '\0';
4155                 }
4156
4157                 index = (ha->pdev->subsystem_device & 0xff);
4158                 if (use_tbl &&
4159                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4160                     index < QLA_MODEL_NAMES)
4161                         strncpy(ha->model_desc,
4162                             qla2x00_model_name[index * 2 + 1],
4163                             sizeof(ha->model_desc) - 1);
4164         } else {
4165                 index = (ha->pdev->subsystem_device & 0xff);
4166                 if (use_tbl &&
4167                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4168                     index < QLA_MODEL_NAMES) {
4169                         strcpy(ha->model_number,
4170                             qla2x00_model_name[index * 2]);
4171                         strncpy(ha->model_desc,
4172                             qla2x00_model_name[index * 2 + 1],
4173                             sizeof(ha->model_desc) - 1);
4174                 } else {
4175                         strcpy(ha->model_number, def);
4176                 }
4177         }
4178         if (IS_FWI2_CAPABLE(ha))
4179                 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
4180                     sizeof(ha->model_desc));
4181 }
4182
4183 /* On sparc systems, obtain port and node WWN from firmware
4184  * properties.
4185  */
4186 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4187 {
4188 #ifdef CONFIG_SPARC
4189         struct qla_hw_data *ha = vha->hw;
4190         struct pci_dev *pdev = ha->pdev;
4191         struct device_node *dp = pci_device_to_OF_node(pdev);
4192         const u8 *val;
4193         int len;
4194
4195         val = of_get_property(dp, "port-wwn", &len);
4196         if (val && len >= WWN_SIZE)
4197                 memcpy(nv->port_name, val, WWN_SIZE);
4198
4199         val = of_get_property(dp, "node-wwn", &len);
4200         if (val && len >= WWN_SIZE)
4201                 memcpy(nv->node_name, val, WWN_SIZE);
4202 #endif
4203 }
4204
4205 /*
4206 * NVRAM configuration for ISP 2xxx
4207 *
4208 * Input:
4209 *      ha                = adapter block pointer.
4210 *
4211 * Output:
4212 *      initialization control block in response_ring
4213 *      host adapters parameters in host adapter block
4214 *
4215 * Returns:
4216 *      0 = success.
4217 */
4218 int
4219 qla2x00_nvram_config(scsi_qla_host_t *vha)
4220 {
4221         int             rval;
4222         uint8_t         chksum = 0;
4223         uint16_t        cnt;
4224         uint8_t         *dptr1, *dptr2;
4225         struct qla_hw_data *ha = vha->hw;
4226         init_cb_t       *icb = ha->init_cb;
4227         nvram_t         *nv = ha->nvram;
4228         uint8_t         *ptr = ha->nvram;
4229         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4230
4231         rval = QLA_SUCCESS;
4232
4233         /* Determine NVRAM starting address. */
4234         ha->nvram_size = sizeof(nvram_t);
4235         ha->nvram_base = 0;
4236         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4237                 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4238                         ha->nvram_base = 0x80;
4239
4240         /* Get NVRAM data and calculate checksum. */
4241         ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
4242         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4243                 chksum += *ptr++;
4244
4245         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4246             "Contents of NVRAM.\n");
4247         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4248             (uint8_t *)nv, ha->nvram_size);
4249
4250         /* Bad NVRAM data, set defaults parameters. */
4251         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4252             nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4253                 /* Reset NVRAM data. */
4254                 ql_log(ql_log_warn, vha, 0x0064,
4255                     "Inconsistent NVRAM "
4256                     "detected: checksum=0x%x id=%c version=0x%x.\n",
4257                     chksum, nv->id[0], nv->nvram_version);
4258                 ql_log(ql_log_warn, vha, 0x0065,
4259                     "Falling back to "
4260                     "functioning (yet invalid -- WWPN) defaults.\n");
4261
4262                 /*
4263                  * Set default initialization control block.
4264                  */
4265                 memset(nv, 0, ha->nvram_size);
4266                 nv->parameter_block_version = ICB_VERSION;
4267
4268                 if (IS_QLA23XX(ha)) {
4269                         nv->firmware_options[0] = BIT_2 | BIT_1;
4270                         nv->firmware_options[1] = BIT_7 | BIT_5;
4271                         nv->add_firmware_options[0] = BIT_5;
4272                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4273                         nv->frame_payload_size = 2048;
4274                         nv->special_options[1] = BIT_7;
4275                 } else if (IS_QLA2200(ha)) {
4276                         nv->firmware_options[0] = BIT_2 | BIT_1;
4277                         nv->firmware_options[1] = BIT_7 | BIT_5;
4278                         nv->add_firmware_options[0] = BIT_5;
4279                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4280                         nv->frame_payload_size = 1024;
4281                 } else if (IS_QLA2100(ha)) {
4282                         nv->firmware_options[0] = BIT_3 | BIT_1;
4283                         nv->firmware_options[1] = BIT_5;
4284                         nv->frame_payload_size = 1024;
4285                 }
4286
4287                 nv->max_iocb_allocation = cpu_to_le16(256);
4288                 nv->execution_throttle = cpu_to_le16(16);
4289                 nv->retry_count = 8;
4290                 nv->retry_delay = 1;
4291
4292                 nv->port_name[0] = 33;
4293                 nv->port_name[3] = 224;
4294                 nv->port_name[4] = 139;
4295
4296                 qla2xxx_nvram_wwn_from_ofw(vha, nv);
4297
4298                 nv->login_timeout = 4;
4299
4300                 /*
4301                  * Set default host adapter parameters
4302                  */
4303                 nv->host_p[1] = BIT_2;
4304                 nv->reset_delay = 5;
4305                 nv->port_down_retry_count = 8;
4306                 nv->max_luns_per_target = cpu_to_le16(8);
4307                 nv->link_down_timeout = 60;
4308
4309                 rval = 1;
4310         }
4311
4312 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4313         /*
4314          * The SN2 does not provide BIOS emulation which means you can't change
4315          * potentially bogus BIOS settings. Force the use of default settings
4316          * for link rate and frame size.  Hope that the rest of the settings
4317          * are valid.
4318          */
4319         if (ia64_platform_is("sn2")) {
4320                 nv->frame_payload_size = 2048;
4321                 if (IS_QLA23XX(ha))
4322                         nv->special_options[1] = BIT_7;
4323         }
4324 #endif
4325
4326         /* Reset Initialization control block */
4327         memset(icb, 0, ha->init_cb_size);
4328
4329         /*
4330          * Setup driver NVRAM options.
4331          */
4332         nv->firmware_options[0] |= (BIT_6 | BIT_1);
4333         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4334         nv->firmware_options[1] |= (BIT_5 | BIT_0);
4335         nv->firmware_options[1] &= ~BIT_4;
4336
4337         if (IS_QLA23XX(ha)) {
4338                 nv->firmware_options[0] |= BIT_2;
4339                 nv->firmware_options[0] &= ~BIT_3;
4340                 nv->special_options[0] &= ~BIT_6;
4341                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
4342
4343                 if (IS_QLA2300(ha)) {
4344                         if (ha->fb_rev == FPM_2310) {
4345                                 strcpy(ha->model_number, "QLA2310");
4346                         } else {
4347                                 strcpy(ha->model_number, "QLA2300");
4348                         }
4349                 } else {
4350                         qla2x00_set_model_info(vha, nv->model_number,
4351                             sizeof(nv->model_number), "QLA23xx");
4352                 }
4353         } else if (IS_QLA2200(ha)) {
4354                 nv->firmware_options[0] |= BIT_2;
4355                 /*
4356                  * 'Point-to-point preferred, else loop' is not a safe
4357                  * connection mode setting.
4358                  */
4359                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4360                     (BIT_5 | BIT_4)) {
4361                         /* Force 'loop preferred, else point-to-point'. */
4362                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4363                         nv->add_firmware_options[0] |= BIT_5;
4364                 }
4365                 strcpy(ha->model_number, "QLA22xx");
4366         } else /*if (IS_QLA2100(ha))*/ {
4367                 strcpy(ha->model_number, "QLA2100");
4368         }
4369
4370         /*
4371          * Copy over NVRAM RISC parameter block to initialization control block.
4372          */
4373         dptr1 = (uint8_t *)icb;
4374         dptr2 = (uint8_t *)&nv->parameter_block_version;
4375         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4376         while (cnt--)
4377                 *dptr1++ = *dptr2++;
4378
4379         /* Copy 2nd half. */
4380         dptr1 = (uint8_t *)icb->add_firmware_options;
4381         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4382         while (cnt--)
4383                 *dptr1++ = *dptr2++;
4384
4385         /* Use alternate WWN? */
4386         if (nv->host_p[1] & BIT_7) {
4387                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4388                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4389         }
4390
4391         /* Prepare nodename */
4392         if ((icb->firmware_options[1] & BIT_6) == 0) {
4393                 /*
4394                  * Firmware will apply the following mask if the nodename was
4395                  * not provided.
4396                  */
4397                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4398                 icb->node_name[0] &= 0xF0;
4399         }
4400
4401         /*
4402          * Set host adapter parameters.
4403          */
4404
4405         /*
4406          * BIT_7 in the host-parameters section allows for modification to
4407          * internal driver logging.
4408          */
4409         if (nv->host_p[0] & BIT_7)
4410                 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
4411         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4412         /* Always load RISC code on non ISP2[12]00 chips. */
4413         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4414                 ha->flags.disable_risc_code_load = 0;
4415         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4416         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4417         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
4418         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
4419         ha->flags.disable_serdes = 0;
4420
4421         ha->operating_mode =
4422             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4423
4424         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4425             sizeof(ha->fw_seriallink_options));
4426
4427         /* save HBA serial number */
4428         ha->serial0 = icb->port_name[5];
4429         ha->serial1 = icb->port_name[6];
4430         ha->serial2 = icb->port_name[7];
4431         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4432         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
4433
4434         icb->execution_throttle = cpu_to_le16(0xFFFF);
4435
4436         ha->retry_count = nv->retry_count;
4437
4438         /* Set minimum login_timeout to 4 seconds. */
4439         if (nv->login_timeout != ql2xlogintimeout)
4440                 nv->login_timeout = ql2xlogintimeout;
4441         if (nv->login_timeout < 4)
4442                 nv->login_timeout = 4;
4443         ha->login_timeout = nv->login_timeout;
4444
4445         /* Set minimum RATOV to 100 tenths of a second. */
4446         ha->r_a_tov = 100;
4447
4448         ha->loop_reset_delay = nv->reset_delay;
4449
4450         /* Link Down Timeout = 0:
4451          *
4452          *      When Port Down timer expires we will start returning
4453          *      I/O's to OS with "DID_NO_CONNECT".
4454          *
4455          * Link Down Timeout != 0:
4456          *
4457          *       The driver waits for the link to come up after link down
4458          *       before returning I/Os to OS with "DID_NO_CONNECT".
4459          */
4460         if (nv->link_down_timeout == 0) {
4461                 ha->loop_down_abort_time =
4462                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4463         } else {
4464                 ha->link_down_timeout =  nv->link_down_timeout;
4465                 ha->loop_down_abort_time =
4466                     (LOOP_DOWN_TIME - ha->link_down_timeout);
4467         }
4468
4469         /*
4470          * Need enough time to try and get the port back.
4471          */
4472         ha->port_down_retry_count = nv->port_down_retry_count;
4473         if (qlport_down_retry)
4474                 ha->port_down_retry_count = qlport_down_retry;
4475         /* Set login_retry_count */
4476         ha->login_retry_count  = nv->retry_count;
4477         if (ha->port_down_retry_count == nv->port_down_retry_count &&
4478             ha->port_down_retry_count > 3)
4479                 ha->login_retry_count = ha->port_down_retry_count;
4480         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4481                 ha->login_retry_count = ha->port_down_retry_count;
4482         if (ql2xloginretrycount)
4483                 ha->login_retry_count = ql2xloginretrycount;
4484
4485         icb->lun_enables = cpu_to_le16(0);
4486         icb->command_resource_count = 0;
4487         icb->immediate_notify_resource_count = 0;
4488         icb->timeout = cpu_to_le16(0);
4489
4490         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4491                 /* Enable RIO */
4492                 icb->firmware_options[0] &= ~BIT_3;
4493                 icb->add_firmware_options[0] &=
4494                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4495                 icb->add_firmware_options[0] |= BIT_2;
4496                 icb->response_accumulation_timer = 3;
4497                 icb->interrupt_delay_timer = 5;
4498
4499                 vha->flags.process_response_queue = 1;
4500         } else {
4501                 /* Enable ZIO. */
4502                 if (!vha->flags.init_done) {
4503                         ha->zio_mode = icb->add_firmware_options[0] &
4504                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4505                         ha->zio_timer = icb->interrupt_delay_timer ?
4506                             icb->interrupt_delay_timer: 2;
4507                 }
4508                 icb->add_firmware_options[0] &=
4509                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4510                 vha->flags.process_response_queue = 0;
4511                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4512                         ha->zio_mode = QLA_ZIO_MODE_6;
4513
4514                         ql_log(ql_log_info, vha, 0x0068,
4515                             "ZIO mode %d enabled; timer delay (%d us).\n",
4516                             ha->zio_mode, ha->zio_timer * 100);
4517
4518                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4519                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
4520                         vha->flags.process_response_queue = 1;
4521                 }
4522         }
4523
4524         if (rval) {
4525                 ql_log(ql_log_warn, vha, 0x0069,
4526                     "NVRAM configuration failed.\n");
4527         }
4528         return (rval);
4529 }
4530
4531 static void
4532 qla2x00_rport_del(void *data)
4533 {
4534         fc_port_t *fcport = data;
4535         struct fc_rport *rport;
4536         unsigned long flags;
4537
4538         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
4539         rport = fcport->drport ? fcport->drport: fcport->rport;
4540         fcport->drport = NULL;
4541         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
4542         if (rport) {
4543                 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4544                     "%s %8phN. rport %p roles %x\n",
4545                     __func__, fcport->port_name, rport,
4546                     rport->roles);
4547
4548                 fc_remote_port_delete(rport);
4549         }
4550 }
4551
4552 /**
4553  * qla2x00_alloc_fcport() - Allocate a generic fcport.
4554  * @vha: HA context
4555  * @flags: allocation flags
4556  *
4557  * Returns a pointer to the allocated fcport, or NULL, if none available.
4558  */
4559 fc_port_t *
4560 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
4561 {
4562         fc_port_t *fcport;
4563
4564         fcport = kzalloc(sizeof(fc_port_t), flags);
4565         if (!fcport)
4566                 return NULL;
4567
4568         /* Setup fcport template structure. */
4569         fcport->vha = vha;
4570         fcport->port_type = FCT_UNKNOWN;
4571         fcport->loop_id = FC_NO_LOOP_ID;
4572         qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
4573         fcport->supported_classes = FC_COS_UNSPECIFIED;
4574
4575         fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4576                 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
4577                 flags);
4578         fcport->disc_state = DSC_DELETED;
4579         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4580         fcport->deleted = QLA_SESS_DELETED;
4581         fcport->login_retry = vha->hw->login_retry_count;
4582         fcport->logout_on_delete = 1;
4583
4584         if (!fcport->ct_desc.ct_sns) {
4585                 ql_log(ql_log_warn, vha, 0xd049,
4586                     "Failed to allocate ct_sns request.\n");
4587                 kfree(fcport);
4588                 fcport = NULL;
4589         }
4590         INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4591         INIT_LIST_HEAD(&fcport->gnl_entry);
4592         INIT_LIST_HEAD(&fcport->list);
4593
4594         return fcport;
4595 }
4596
4597 void
4598 qla2x00_free_fcport(fc_port_t *fcport)
4599 {
4600         if (fcport->ct_desc.ct_sns) {
4601                 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4602                         sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4603                         fcport->ct_desc.ct_sns_dma);
4604
4605                 fcport->ct_desc.ct_sns = NULL;
4606         }
4607         kfree(fcport);
4608 }
4609
4610 /*
4611  * qla2x00_configure_loop
4612  *      Updates Fibre Channel Device Database with what is actually on loop.
4613  *
4614  * Input:
4615  *      ha                = adapter block pointer.
4616  *
4617  * Returns:
4618  *      0 = success.
4619  *      1 = error.
4620  *      2 = database was full and device was not configured.
4621  */
4622 static int
4623 qla2x00_configure_loop(scsi_qla_host_t *vha)
4624 {
4625         int  rval;
4626         unsigned long flags, save_flags;
4627         struct qla_hw_data *ha = vha->hw;
4628         rval = QLA_SUCCESS;
4629
4630         /* Get Initiator ID */
4631         if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4632                 rval = qla2x00_configure_hba(vha);
4633                 if (rval != QLA_SUCCESS) {
4634                         ql_dbg(ql_dbg_disc, vha, 0x2013,
4635                             "Unable to configure HBA.\n");
4636                         return (rval);
4637                 }
4638         }
4639
4640         save_flags = flags = vha->dpc_flags;
4641         ql_dbg(ql_dbg_disc, vha, 0x2014,
4642             "Configure loop -- dpc flags = 0x%lx.\n", flags);
4643
4644         /*
4645          * If we have both an RSCN and PORT UPDATE pending then handle them
4646          * both at the same time.
4647          */
4648         clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4649         clear_bit(RSCN_UPDATE, &vha->dpc_flags);
4650
4651         qla2x00_get_data_rate(vha);
4652
4653         /* Determine what we need to do */
4654         if (ha->current_topology == ISP_CFG_FL &&
4655             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4656
4657                 set_bit(RSCN_UPDATE, &flags);
4658
4659         } else if (ha->current_topology == ISP_CFG_F &&
4660             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4661
4662                 set_bit(RSCN_UPDATE, &flags);
4663                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4664
4665         } else if (ha->current_topology == ISP_CFG_N) {
4666                 clear_bit(RSCN_UPDATE, &flags);
4667                 if (qla_tgt_mode_enabled(vha)) {
4668                         /* allow the other side to start the login */
4669                         clear_bit(LOCAL_LOOP_UPDATE, &flags);
4670                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4671                 }
4672         } else if (ha->current_topology == ISP_CFG_NL) {
4673                 clear_bit(RSCN_UPDATE, &flags);
4674                 set_bit(LOCAL_LOOP_UPDATE, &flags);
4675         } else if (!vha->flags.online ||
4676             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
4677                 set_bit(RSCN_UPDATE, &flags);
4678                 set_bit(LOCAL_LOOP_UPDATE, &flags);
4679         }
4680
4681         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
4682                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4683                         ql_dbg(ql_dbg_disc, vha, 0x2015,
4684                             "Loop resync needed, failing.\n");
4685                         rval = QLA_FUNCTION_FAILED;
4686                 } else
4687                         rval = qla2x00_configure_local_loop(vha);
4688         }
4689
4690         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
4691                 if (LOOP_TRANSITION(vha)) {
4692                         ql_dbg(ql_dbg_disc, vha, 0x2099,
4693                             "Needs RSCN update and loop transition.\n");
4694                         rval = QLA_FUNCTION_FAILED;
4695                 }
4696                 else
4697                         rval = qla2x00_configure_fabric(vha);
4698         }
4699
4700         if (rval == QLA_SUCCESS) {
4701                 if (atomic_read(&vha->loop_down_timer) ||
4702                     test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4703                         rval = QLA_FUNCTION_FAILED;
4704                 } else {
4705                         atomic_set(&vha->loop_state, LOOP_READY);
4706                         ql_dbg(ql_dbg_disc, vha, 0x2069,
4707                             "LOOP READY.\n");
4708                         ha->flags.fw_init_done = 1;
4709
4710                         /*
4711                          * Process any ATIO queue entries that came in
4712                          * while we weren't online.
4713                          */
4714                         if (qla_tgt_mode_enabled(vha) ||
4715                             qla_dual_mode_enabled(vha)) {
4716                                 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4717                                         spin_lock_irqsave(&ha->tgt.atio_lock,
4718                                             flags);
4719                                         qlt_24xx_process_atio_queue(vha, 0);
4720                                         spin_unlock_irqrestore(
4721                                             &ha->tgt.atio_lock, flags);
4722                                 } else {
4723                                         spin_lock_irqsave(&ha->hardware_lock,
4724                                             flags);
4725                                         qlt_24xx_process_atio_queue(vha, 1);
4726                                         spin_unlock_irqrestore(
4727                                             &ha->hardware_lock, flags);
4728                                 }
4729                         }
4730                 }
4731         }
4732
4733         if (rval) {
4734                 ql_dbg(ql_dbg_disc, vha, 0x206a,
4735                     "%s *** FAILED ***.\n", __func__);
4736         } else {
4737                 ql_dbg(ql_dbg_disc, vha, 0x206b,
4738                     "%s: exiting normally.\n", __func__);
4739         }
4740
4741         /* Restore state if a resync event occurred during processing */
4742         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4743                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
4744                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4745                 if (test_bit(RSCN_UPDATE, &save_flags)) {
4746                         set_bit(RSCN_UPDATE, &vha->dpc_flags);
4747                 }
4748         }
4749
4750         return (rval);
4751 }
4752
4753 /*
4754  * qla2x00_configure_local_loop
4755  *      Updates Fibre Channel Device Database with local loop devices.
4756  *
4757  * Input:
4758  *      ha = adapter block pointer.
4759  *
4760  * Returns:
4761  *      0 = success.
4762  */
4763 static int
4764 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
4765 {
4766         int             rval, rval2;
4767         int             found_devs;
4768         int             found;
4769         fc_port_t       *fcport, *new_fcport;
4770
4771         uint16_t        index;
4772         uint16_t        entries;
4773         char            *id_iter;
4774         uint16_t        loop_id;
4775         uint8_t         domain, area, al_pa;
4776         struct qla_hw_data *ha = vha->hw;
4777         unsigned long flags;
4778
4779         found_devs = 0;
4780         new_fcport = NULL;
4781         entries = MAX_FIBRE_DEVICES_LOOP;
4782
4783         /* Get list of logged in devices. */
4784         memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
4785         rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
4786             &entries);
4787         if (rval != QLA_SUCCESS)
4788                 goto cleanup_allocation;
4789
4790         ql_dbg(ql_dbg_disc, vha, 0x2011,
4791             "Entries in ID list (%d).\n", entries);
4792         ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4793             (uint8_t *)ha->gid_list,
4794             entries * sizeof(struct gid_list_info));
4795
4796         list_for_each_entry(fcport, &vha->vp_fcports, list) {
4797                 fcport->scan_state = QLA_FCPORT_SCAN;
4798         }
4799
4800         /* Allocate temporary fcport for any new fcports discovered. */
4801         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
4802         if (new_fcport == NULL) {
4803                 ql_log(ql_log_warn, vha, 0x2012,
4804                     "Memory allocation failed for fcport.\n");
4805                 rval = QLA_MEMORY_ALLOC_FAILED;
4806                 goto cleanup_allocation;
4807         }
4808         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4809
4810         /* Add devices to port list. */
4811         id_iter = (char *)ha->gid_list;
4812         for (index = 0; index < entries; index++) {
4813                 domain = ((struct gid_list_info *)id_iter)->domain;
4814                 area = ((struct gid_list_info *)id_iter)->area;
4815                 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
4816                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
4817                         loop_id = (uint16_t)
4818                             ((struct gid_list_info *)id_iter)->loop_id_2100;
4819                 else
4820                         loop_id = le16_to_cpu(
4821                             ((struct gid_list_info *)id_iter)->loop_id);
4822                 id_iter += ha->gid_list_info_size;
4823
4824                 /* Bypass reserved domain fields. */
4825                 if ((domain & 0xf0) == 0xf0)
4826                         continue;
4827
4828                 /* Bypass if not same domain and area of adapter. */
4829                 if (area && domain &&
4830                     (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
4831                         continue;
4832
4833                 /* Bypass invalid local loop ID. */
4834                 if (loop_id > LAST_LOCAL_LOOP_ID)
4835                         continue;
4836
4837                 memset(new_fcport->port_name, 0, WWN_SIZE);
4838
4839                 /* Fill in member data. */
4840                 new_fcport->d_id.b.domain = domain;
4841                 new_fcport->d_id.b.area = area;
4842                 new_fcport->d_id.b.al_pa = al_pa;
4843                 new_fcport->loop_id = loop_id;
4844                 new_fcport->scan_state = QLA_FCPORT_FOUND;
4845
4846                 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
4847                 if (rval2 != QLA_SUCCESS) {
4848                         ql_dbg(ql_dbg_disc, vha, 0x2097,
4849                             "Failed to retrieve fcport information "
4850                             "-- get_port_database=%x, loop_id=0x%04x.\n",
4851                             rval2, new_fcport->loop_id);
4852                         /* Skip retry if N2N */
4853                         if (ha->current_topology != ISP_CFG_N) {
4854                                 ql_dbg(ql_dbg_disc, vha, 0x2105,
4855                                     "Scheduling resync.\n");
4856                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4857                                 continue;
4858                         }
4859                 }
4860
4861                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4862                 /* Check for matching device in port list. */
4863                 found = 0;
4864                 fcport = NULL;
4865                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4866                         if (memcmp(new_fcport->port_name, fcport->port_name,
4867                             WWN_SIZE))
4868                                 continue;
4869
4870                         fcport->flags &= ~FCF_FABRIC_DEVICE;
4871                         fcport->loop_id = new_fcport->loop_id;
4872                         fcport->port_type = new_fcport->port_type;
4873                         fcport->d_id.b24 = new_fcport->d_id.b24;
4874                         memcpy(fcport->node_name, new_fcport->node_name,
4875                             WWN_SIZE);
4876                         fcport->scan_state = QLA_FCPORT_FOUND;
4877                         found++;
4878                         break;
4879                 }
4880
4881                 if (!found) {
4882                         /* New device, add to fcports list. */
4883                         list_add_tail(&new_fcport->list, &vha->vp_fcports);
4884
4885                         /* Allocate a new replacement fcport. */
4886                         fcport = new_fcport;
4887
4888                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4889
4890                         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
4891
4892                         if (new_fcport == NULL) {
4893                                 ql_log(ql_log_warn, vha, 0xd031,
4894                                     "Failed to allocate memory for fcport.\n");
4895                                 rval = QLA_MEMORY_ALLOC_FAILED;
4896                                 goto cleanup_allocation;
4897                         }
4898                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4899                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4900                 }
4901
4902                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4903
4904                 /* Base iIDMA settings on HBA port speed. */
4905                 fcport->fp_speed = ha->link_data_rate;
4906
4907                 found_devs++;
4908         }
4909
4910         list_for_each_entry(fcport, &vha->vp_fcports, list) {
4911                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4912                         break;
4913
4914                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4915                         if ((qla_dual_mode_enabled(vha) ||
4916                             qla_ini_mode_enabled(vha)) &&
4917                             atomic_read(&fcport->state) == FCS_ONLINE) {
4918                                 qla2x00_mark_device_lost(vha, fcport,
4919                                         ql2xplogiabsentdevice, 0);
4920                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
4921                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4922                                     fcport->port_type != FCT_INITIATOR &&
4923                                     fcport->port_type != FCT_BROADCAST) {
4924                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
4925                                             "%s %d %8phC post del sess\n",
4926                                             __func__, __LINE__,
4927                                             fcport->port_name);
4928
4929                                         qlt_schedule_sess_for_deletion(fcport);
4930                                         continue;
4931                                 }
4932                         }
4933                 }
4934
4935                 if (fcport->scan_state == QLA_FCPORT_FOUND)
4936                         qla24xx_fcport_handle_login(vha, fcport);
4937         }
4938
4939 cleanup_allocation:
4940         kfree(new_fcport);
4941
4942         if (rval != QLA_SUCCESS) {
4943                 ql_dbg(ql_dbg_disc, vha, 0x2098,
4944                     "Configure local loop error exit: rval=%x.\n", rval);
4945         }
4946
4947         return (rval);
4948 }
4949
4950 static void
4951 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
4952 {
4953         int rval;
4954         uint16_t mb[MAILBOX_REGISTER_COUNT];
4955         struct qla_hw_data *ha = vha->hw;
4956
4957         if (!IS_IIDMA_CAPABLE(ha))
4958                 return;
4959
4960         if (atomic_read(&fcport->state) != FCS_ONLINE)
4961                 return;
4962
4963         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4964             fcport->fp_speed > ha->link_data_rate ||
4965             !ha->flags.gpsc_supported)
4966                 return;
4967
4968         rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
4969             mb);
4970         if (rval != QLA_SUCCESS) {
4971                 ql_dbg(ql_dbg_disc, vha, 0x2004,
4972                     "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
4973                     fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
4974         } else {
4975                 ql_dbg(ql_dbg_disc, vha, 0x2005,
4976                     "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
4977                     qla2x00_get_link_speed_str(ha, fcport->fp_speed),
4978                     fcport->fp_speed, fcport->port_name);
4979         }
4980 }
4981
4982 void qla_do_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4983 {
4984         qla2x00_iidma_fcport(vha, fcport);
4985         qla24xx_update_fcport_fcp_prio(vha, fcport);
4986 }
4987
4988 int qla_post_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
4989 {
4990         struct qla_work_evt *e;
4991
4992         e = qla2x00_alloc_work(vha, QLA_EVT_IIDMA);
4993         if (!e)
4994                 return QLA_FUNCTION_FAILED;
4995
4996         e->u.fcport.fcport = fcport;
4997         return qla2x00_post_work(vha, e);
4998 }
4999
5000 /* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
5001 static void
5002 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
5003 {
5004         struct fc_rport_identifiers rport_ids;
5005         struct fc_rport *rport;
5006         unsigned long flags;
5007
5008         if (atomic_read(&fcport->state) == FCS_ONLINE)
5009                 return;
5010
5011         rport_ids.node_name = wwn_to_u64(fcport->node_name);
5012         rport_ids.port_name = wwn_to_u64(fcport->port_name);
5013         rport_ids.port_id = fcport->d_id.b.domain << 16 |
5014             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5015         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
5016         fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
5017         if (!rport) {
5018                 ql_log(ql_log_warn, vha, 0x2006,
5019                     "Unable to allocate fc remote port.\n");
5020                 return;
5021         }
5022
5023         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5024         *((fc_port_t **)rport->dd_data) = fcport;
5025         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5026
5027         rport->supported_classes = fcport->supported_classes;
5028
5029         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
5030         if (fcport->port_type == FCT_INITIATOR)
5031                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5032         if (fcport->port_type == FCT_TARGET)
5033                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
5034
5035         ql_dbg(ql_dbg_disc, vha, 0x20ee,
5036             "%s %8phN. rport %p is %s mode\n",
5037             __func__, fcport->port_name, rport,
5038             (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
5039
5040         fc_remote_port_rolechg(rport, rport_ids.roles);
5041 }
5042
5043 /*
5044  * qla2x00_update_fcport
5045  *      Updates device on list.
5046  *
5047  * Input:
5048  *      ha = adapter block pointer.
5049  *      fcport = port structure pointer.
5050  *
5051  * Return:
5052  *      0  - Success
5053  *  BIT_0 - error
5054  *
5055  * Context:
5056  *      Kernel context.
5057  */
5058 void
5059 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5060 {
5061         fcport->vha = vha;
5062
5063         if (IS_SW_RESV_ADDR(fcport->d_id))
5064                 return;
5065
5066         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5067         fcport->disc_state = DSC_LOGIN_COMPLETE;
5068         fcport->deleted = 0;
5069         fcport->logout_on_delete = 1;
5070         fcport->login_retry = vha->hw->login_retry_count;
5071
5072         qla2x00_iidma_fcport(vha, fcport);
5073
5074         if (fcport->fc4f_nvme) {
5075                 qla_nvme_register_remote(vha, fcport);
5076                 fcport->disc_state = DSC_LOGIN_COMPLETE;
5077                 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5078                 return;
5079         }
5080
5081         qla24xx_update_fcport_fcp_prio(vha, fcport);
5082
5083         switch (vha->host->active_mode) {
5084         case MODE_INITIATOR:
5085                 qla2x00_reg_remote_port(vha, fcport);
5086                 break;
5087         case MODE_TARGET:
5088                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5089                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5090                         qlt_fc_port_added(vha, fcport);
5091                 break;
5092         case MODE_DUAL:
5093                 qla2x00_reg_remote_port(vha, fcport);
5094                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5095                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5096                         qlt_fc_port_added(vha, fcport);
5097                 break;
5098         default:
5099                 break;
5100         }
5101
5102         if (IS_IIDMA_CAPABLE(vha->hw) && vha->hw->flags.gpsc_supported) {
5103                 if (fcport->id_changed) {
5104                         fcport->id_changed = 0;
5105                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5106                             "%s %d %8phC post gfpnid fcp_cnt %d\n",
5107                             __func__, __LINE__, fcport->port_name,
5108                             vha->fcport_count);
5109                         qla24xx_post_gfpnid_work(vha, fcport);
5110                 } else {
5111                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5112                             "%s %d %8phC post gpsc fcp_cnt %d\n",
5113                             __func__, __LINE__, fcport->port_name,
5114                             vha->fcport_count);
5115                         qla24xx_post_gpsc_work(vha, fcport);
5116                 }
5117         }
5118         qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5119 }
5120
5121 /*
5122  * qla2x00_configure_fabric
5123  *      Setup SNS devices with loop ID's.
5124  *
5125  * Input:
5126  *      ha = adapter block pointer.
5127  *
5128  * Returns:
5129  *      0 = success.
5130  *      BIT_0 = error
5131  */
5132 static int
5133 qla2x00_configure_fabric(scsi_qla_host_t *vha)
5134 {
5135         int     rval;
5136         fc_port_t       *fcport;
5137         uint16_t        mb[MAILBOX_REGISTER_COUNT];
5138         uint16_t        loop_id;
5139         LIST_HEAD(new_fcports);
5140         struct qla_hw_data *ha = vha->hw;
5141         int             discovery_gen;
5142
5143         /* If FL port exists, then SNS is present */
5144         if (IS_FWI2_CAPABLE(ha))
5145                 loop_id = NPH_F_PORT;
5146         else
5147                 loop_id = SNS_FL_PORT;
5148         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
5149         if (rval != QLA_SUCCESS) {
5150                 ql_dbg(ql_dbg_disc, vha, 0x20a0,
5151                     "MBX_GET_PORT_NAME failed, No FL Port.\n");
5152
5153                 vha->device_flags &= ~SWITCH_FOUND;
5154                 return (QLA_SUCCESS);
5155         }
5156         vha->device_flags |= SWITCH_FOUND;
5157
5158
5159         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5160                 rval = qla2x00_send_change_request(vha, 0x3, 0);
5161                 if (rval != QLA_SUCCESS)
5162                         ql_log(ql_log_warn, vha, 0x121,
5163                                 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5164                                 rval);
5165         }
5166
5167
5168         do {
5169                 qla2x00_mgmt_svr_login(vha);
5170
5171                 /* FDMI support. */
5172                 if (ql2xfdmienable &&
5173                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5174                         qla2x00_fdmi_register(vha);
5175
5176                 /* Ensure we are logged into the SNS. */
5177                 loop_id = NPH_SNS_LID(ha);
5178                 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5179                     0xfc, mb, BIT_1|BIT_0);
5180                 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5181                         ql_dbg(ql_dbg_disc, vha, 0x20a1,
5182                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5183                             loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
5184                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5185                         return rval;
5186                 }
5187                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5188                         if (qla2x00_rft_id(vha)) {
5189                                 /* EMPTY */
5190                                 ql_dbg(ql_dbg_disc, vha, 0x20a2,
5191                                     "Register FC-4 TYPE failed.\n");
5192                                 if (test_bit(LOOP_RESYNC_NEEDED,
5193                                     &vha->dpc_flags))
5194                                         break;
5195                         }
5196                         if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
5197                                 /* EMPTY */
5198                                 ql_dbg(ql_dbg_disc, vha, 0x209a,
5199                                     "Register FC-4 Features failed.\n");
5200                                 if (test_bit(LOOP_RESYNC_NEEDED,
5201                                     &vha->dpc_flags))
5202                                         break;
5203                         }
5204                         if (vha->flags.nvme_enabled) {
5205                                 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5206                                         ql_dbg(ql_dbg_disc, vha, 0x2049,
5207                                             "Register NVME FC Type Features failed.\n");
5208                                 }
5209                         }
5210                         if (qla2x00_rnn_id(vha)) {
5211                                 /* EMPTY */
5212                                 ql_dbg(ql_dbg_disc, vha, 0x2104,
5213                                     "Register Node Name failed.\n");
5214                                 if (test_bit(LOOP_RESYNC_NEEDED,
5215                                     &vha->dpc_flags))
5216                                         break;
5217                         } else if (qla2x00_rsnn_nn(vha)) {
5218                                 /* EMPTY */
5219                                 ql_dbg(ql_dbg_disc, vha, 0x209b,
5220                                     "Register Symbolic Node Name failed.\n");
5221                                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5222                                         break;
5223                         }
5224                 }
5225
5226
5227                 /* Mark the time right before querying FW for connected ports.
5228                  * This process is long, asynchronous and by the time it's done,
5229                  * collected information might not be accurate anymore. E.g.
5230                  * disconnected port might have re-connected and a brand new
5231                  * session has been created. In this case session's generation
5232                  * will be newer than discovery_gen. */
5233                 qlt_do_generation_tick(vha, &discovery_gen);
5234
5235                 if (USE_ASYNC_SCAN(ha)) {
5236                         rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
5237                             NULL);
5238                         if (rval)
5239                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5240                 } else  {
5241                         list_for_each_entry(fcport, &vha->vp_fcports, list)
5242                                 fcport->scan_state = QLA_FCPORT_SCAN;
5243
5244                         rval = qla2x00_find_all_fabric_devs(vha);
5245                 }
5246                 if (rval != QLA_SUCCESS)
5247                         break;
5248         } while (0);
5249
5250         if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5251                 qla_nvme_register_hba(vha);
5252
5253         if (rval)
5254                 ql_dbg(ql_dbg_disc, vha, 0x2068,
5255                     "Configure fabric error exit rval=%d.\n", rval);
5256
5257         return (rval);
5258 }
5259
5260 /*
5261  * qla2x00_find_all_fabric_devs
5262  *
5263  * Input:
5264  *      ha = adapter block pointer.
5265  *      dev = database device entry pointer.
5266  *
5267  * Returns:
5268  *      0 = success.
5269  *
5270  * Context:
5271  *      Kernel context.
5272  */
5273 static int
5274 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
5275 {
5276         int             rval;
5277         uint16_t        loop_id;
5278         fc_port_t       *fcport, *new_fcport;
5279         int             found;
5280
5281         sw_info_t       *swl;
5282         int             swl_idx;
5283         int             first_dev, last_dev;
5284         port_id_t       wrap = {}, nxt_d_id;
5285         struct qla_hw_data *ha = vha->hw;
5286         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
5287         unsigned long flags;
5288
5289         rval = QLA_SUCCESS;
5290
5291         /* Try GID_PT to get device list, else GAN. */
5292         if (!ha->swl)
5293                 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
5294                     GFP_KERNEL);
5295         swl = ha->swl;
5296         if (!swl) {
5297                 /*EMPTY*/
5298                 ql_dbg(ql_dbg_disc, vha, 0x209c,
5299                     "GID_PT allocations failed, fallback on GA_NXT.\n");
5300         } else {
5301                 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
5302                 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
5303                         swl = NULL;
5304                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5305                                 return rval;
5306                 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
5307                         swl = NULL;
5308                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5309                                 return rval;
5310                 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
5311                         swl = NULL;
5312                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5313                                 return rval;
5314                 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5315                         swl = NULL;
5316                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5317                                 return rval;
5318                 }
5319
5320                 /* If other queries succeeded probe for FC-4 type */
5321                 if (swl) {
5322                         qla2x00_gff_id(vha, swl);
5323                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5324                                 return rval;
5325                 }
5326         }
5327         swl_idx = 0;
5328
5329         /* Allocate temporary fcport for any new fcports discovered. */
5330         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5331         if (new_fcport == NULL) {
5332                 ql_log(ql_log_warn, vha, 0x209d,
5333                     "Failed to allocate memory for fcport.\n");
5334                 return (QLA_MEMORY_ALLOC_FAILED);
5335         }
5336         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5337         /* Set start port ID scan at adapter ID. */
5338         first_dev = 1;
5339         last_dev = 0;
5340
5341         /* Starting free loop ID. */
5342         loop_id = ha->min_external_loopid;
5343         for (; loop_id <= ha->max_loop_id; loop_id++) {
5344                 if (qla2x00_is_reserved_id(vha, loop_id))
5345                         continue;
5346
5347                 if (ha->current_topology == ISP_CFG_FL &&
5348                     (atomic_read(&vha->loop_down_timer) ||
5349                      LOOP_TRANSITION(vha))) {
5350                         atomic_set(&vha->loop_down_timer, 0);
5351                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5352                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5353                         break;
5354                 }
5355
5356                 if (swl != NULL) {
5357                         if (last_dev) {
5358                                 wrap.b24 = new_fcport->d_id.b24;
5359                         } else {
5360                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5361                                 memcpy(new_fcport->node_name,
5362                                     swl[swl_idx].node_name, WWN_SIZE);
5363                                 memcpy(new_fcport->port_name,
5364                                     swl[swl_idx].port_name, WWN_SIZE);
5365                                 memcpy(new_fcport->fabric_port_name,
5366                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
5367                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
5368                                 new_fcport->fc4_type = swl[swl_idx].fc4_type;
5369
5370                                 new_fcport->nvme_flag = 0;
5371                                 new_fcport->fc4f_nvme = 0;
5372                                 if (vha->flags.nvme_enabled &&
5373                                     swl[swl_idx].fc4f_nvme) {
5374                                         new_fcport->fc4f_nvme =
5375                                             swl[swl_idx].fc4f_nvme;
5376                                         ql_log(ql_log_info, vha, 0x2131,
5377                                             "FOUND: NVME port %8phC as FC Type 28h\n",
5378                                             new_fcport->port_name);
5379                                 }
5380
5381                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5382                                         last_dev = 1;
5383                                 }
5384                                 swl_idx++;
5385                         }
5386                 } else {
5387                         /* Send GA_NXT to the switch */
5388                         rval = qla2x00_ga_nxt(vha, new_fcport);
5389                         if (rval != QLA_SUCCESS) {
5390                                 ql_log(ql_log_warn, vha, 0x209e,
5391                                     "SNS scan failed -- assuming "
5392                                     "zero-entry result.\n");
5393                                 rval = QLA_SUCCESS;
5394                                 break;
5395                         }
5396                 }
5397
5398                 /* If wrap on switch device list, exit. */
5399                 if (first_dev) {
5400                         wrap.b24 = new_fcport->d_id.b24;
5401                         first_dev = 0;
5402                 } else if (new_fcport->d_id.b24 == wrap.b24) {
5403                         ql_dbg(ql_dbg_disc, vha, 0x209f,
5404                             "Device wrap (%02x%02x%02x).\n",
5405                             new_fcport->d_id.b.domain,
5406                             new_fcport->d_id.b.area,
5407                             new_fcport->d_id.b.al_pa);
5408                         break;
5409                 }
5410
5411                 /* Bypass if same physical adapter. */
5412                 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
5413                         continue;
5414
5415                 /* Bypass virtual ports of the same host. */
5416                 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5417                         continue;
5418
5419                 /* Bypass if same domain and area of adapter. */
5420                 if (((new_fcport->d_id.b24 & 0xffff00) ==
5421                     (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
5422                         ISP_CFG_FL)
5423                             continue;
5424
5425                 /* Bypass reserved domain fields. */
5426                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5427                         continue;
5428
5429                 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
5430                 if (ql2xgffidenable &&
5431                     (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5432                     new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
5433                         continue;
5434
5435                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5436
5437                 /* Locate matching device in database. */
5438                 found = 0;
5439                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5440                         if (memcmp(new_fcport->port_name, fcport->port_name,
5441                             WWN_SIZE))
5442                                 continue;
5443
5444                         fcport->scan_state = QLA_FCPORT_FOUND;
5445
5446                         found++;
5447
5448                         /* Update port state. */
5449                         memcpy(fcport->fabric_port_name,
5450                             new_fcport->fabric_port_name, WWN_SIZE);
5451                         fcport->fp_speed = new_fcport->fp_speed;
5452
5453                         /*
5454                          * If address the same and state FCS_ONLINE
5455                          * (or in target mode), nothing changed.
5456                          */
5457                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
5458                             (atomic_read(&fcport->state) == FCS_ONLINE ||
5459                              (vha->host->active_mode == MODE_TARGET))) {
5460                                 break;
5461                         }
5462
5463                         /*
5464                          * If device was not a fabric device before.
5465                          */
5466                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5467                                 fcport->d_id.b24 = new_fcport->d_id.b24;
5468                                 qla2x00_clear_loop_id(fcport);
5469                                 fcport->flags |= (FCF_FABRIC_DEVICE |
5470                                     FCF_LOGIN_NEEDED);
5471                                 break;
5472                         }
5473
5474                         /*
5475                          * Port ID changed or device was marked to be updated;
5476                          * Log it out if still logged in and mark it for
5477                          * relogin later.
5478                          */
5479                         if (qla_tgt_mode_enabled(base_vha)) {
5480                                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5481                                          "port changed FC ID, %8phC"
5482                                          " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5483                                          fcport->port_name,
5484                                          fcport->d_id.b.domain,
5485                                          fcport->d_id.b.area,
5486                                          fcport->d_id.b.al_pa,
5487                                          fcport->loop_id,
5488                                          new_fcport->d_id.b.domain,
5489                                          new_fcport->d_id.b.area,
5490                                          new_fcport->d_id.b.al_pa);
5491                                 fcport->d_id.b24 = new_fcport->d_id.b24;
5492                                 break;
5493                         }
5494
5495                         fcport->d_id.b24 = new_fcport->d_id.b24;
5496                         fcport->flags |= FCF_LOGIN_NEEDED;
5497                         break;
5498                 }
5499
5500                 if (fcport->fc4f_nvme) {
5501                         if (fcport->disc_state == DSC_DELETE_PEND) {
5502                                 fcport->disc_state = DSC_GNL;
5503                                 vha->fcport_count--;
5504                                 fcport->login_succ = 0;
5505                         }
5506                 }
5507
5508                 if (found) {
5509                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5510                         continue;
5511                 }
5512                 /* If device was not in our fcports list, then add it. */
5513                 new_fcport->scan_state = QLA_FCPORT_FOUND;
5514                 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5515
5516                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5517
5518
5519                 /* Allocate a new replacement fcport. */
5520                 nxt_d_id.b24 = new_fcport->d_id.b24;
5521                 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5522                 if (new_fcport == NULL) {
5523                         ql_log(ql_log_warn, vha, 0xd032,
5524                             "Memory allocation failed for fcport.\n");
5525                         return (QLA_MEMORY_ALLOC_FAILED);
5526                 }
5527                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5528                 new_fcport->d_id.b24 = nxt_d_id.b24;
5529         }
5530
5531         qla2x00_free_fcport(new_fcport);
5532
5533         /*
5534          * Logout all previous fabric dev marked lost, except FCP2 devices.
5535          */
5536         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5537                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5538                         break;
5539
5540                 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5541                     (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5542                         continue;
5543
5544                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5545                         if ((qla_dual_mode_enabled(vha) ||
5546                             qla_ini_mode_enabled(vha)) &&
5547                             atomic_read(&fcport->state) == FCS_ONLINE) {
5548                                 qla2x00_mark_device_lost(vha, fcport,
5549                                         ql2xplogiabsentdevice, 0);
5550                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
5551                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5552                                     fcport->port_type != FCT_INITIATOR &&
5553                                     fcport->port_type != FCT_BROADCAST) {
5554                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
5555                                             "%s %d %8phC post del sess\n",
5556                                             __func__, __LINE__,
5557                                             fcport->port_name);
5558                                         qlt_schedule_sess_for_deletion(fcport);
5559                                         continue;
5560                                 }
5561                         }
5562                 }
5563
5564                 if (fcport->scan_state == QLA_FCPORT_FOUND)
5565                         qla24xx_fcport_handle_login(vha, fcport);
5566         }
5567         return (rval);
5568 }
5569
5570 /*
5571  * qla2x00_find_new_loop_id
5572  *      Scan through our port list and find a new usable loop ID.
5573  *
5574  * Input:
5575  *      ha:     adapter state pointer.
5576  *      dev:    port structure pointer.
5577  *
5578  * Returns:
5579  *      qla2x00 local function return status code.
5580  *
5581  * Context:
5582  *      Kernel context.
5583  */
5584 int
5585 qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
5586 {
5587         int     rval;
5588         struct qla_hw_data *ha = vha->hw;
5589         unsigned long flags = 0;
5590
5591         rval = QLA_SUCCESS;
5592
5593         spin_lock_irqsave(&ha->vport_slock, flags);
5594
5595         dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5596             LOOPID_MAP_SIZE);
5597         if (dev->loop_id >= LOOPID_MAP_SIZE ||
5598             qla2x00_is_reserved_id(vha, dev->loop_id)) {
5599                 dev->loop_id = FC_NO_LOOP_ID;
5600                 rval = QLA_FUNCTION_FAILED;
5601         } else
5602                 set_bit(dev->loop_id, ha->loop_id_map);
5603
5604         spin_unlock_irqrestore(&ha->vport_slock, flags);
5605
5606         if (rval == QLA_SUCCESS)
5607                 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5608                     "Assigning new loopid=%x, portid=%x.\n",
5609                     dev->loop_id, dev->d_id.b24);
5610         else
5611                 ql_log(ql_log_warn, dev->vha, 0x2087,
5612                     "No loop_id's available, portid=%x.\n",
5613                     dev->d_id.b24);
5614
5615         return (rval);
5616 }
5617
5618
5619 /* FW does not set aside Loop id for MGMT Server/FFFFFAh */
5620 int
5621 qla2x00_reserve_mgmt_server_loop_id(scsi_qla_host_t *vha)
5622 {
5623         int loop_id = FC_NO_LOOP_ID;
5624         int lid = NPH_MGMT_SERVER - vha->vp_idx;
5625         unsigned long flags;
5626         struct qla_hw_data *ha = vha->hw;
5627
5628         if (vha->vp_idx == 0) {
5629                 set_bit(NPH_MGMT_SERVER, ha->loop_id_map);
5630                 return NPH_MGMT_SERVER;
5631         }
5632
5633         /* pick id from high and work down to low */
5634         spin_lock_irqsave(&ha->vport_slock, flags);
5635         for (; lid > 0; lid--) {
5636                 if (!test_bit(lid, vha->hw->loop_id_map)) {
5637                         set_bit(lid, vha->hw->loop_id_map);
5638                         loop_id = lid;
5639                         break;
5640                 }
5641         }
5642         spin_unlock_irqrestore(&ha->vport_slock, flags);
5643
5644         return loop_id;
5645 }
5646
5647 /*
5648  * qla2x00_fabric_login
5649  *      Issue fabric login command.
5650  *
5651  * Input:
5652  *      ha = adapter block pointer.
5653  *      device = pointer to FC device type structure.
5654  *
5655  * Returns:
5656  *      0 - Login successfully
5657  *      1 - Login failed
5658  *      2 - Initiator device
5659  *      3 - Fatal error
5660  */
5661 int
5662 qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
5663     uint16_t *next_loopid)
5664 {
5665         int     rval;
5666         int     retry;
5667         uint16_t tmp_loopid;
5668         uint16_t mb[MAILBOX_REGISTER_COUNT];
5669         struct qla_hw_data *ha = vha->hw;
5670
5671         retry = 0;
5672         tmp_loopid = 0;
5673
5674         for (;;) {
5675                 ql_dbg(ql_dbg_disc, vha, 0x2000,
5676                     "Trying Fabric Login w/loop id 0x%04x for port "
5677                     "%02x%02x%02x.\n",
5678                     fcport->loop_id, fcport->d_id.b.domain,
5679                     fcport->d_id.b.area, fcport->d_id.b.al_pa);
5680
5681                 /* Login fcport on switch. */
5682                 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
5683                     fcport->d_id.b.domain, fcport->d_id.b.area,
5684                     fcport->d_id.b.al_pa, mb, BIT_0);
5685                 if (rval != QLA_SUCCESS) {
5686                         return rval;
5687                 }
5688                 if (mb[0] == MBS_PORT_ID_USED) {
5689                         /*
5690                          * Device has another loop ID.  The firmware team
5691                          * recommends the driver perform an implicit login with
5692                          * the specified ID again. The ID we just used is save
5693                          * here so we return with an ID that can be tried by
5694                          * the next login.
5695                          */
5696                         retry++;
5697                         tmp_loopid = fcport->loop_id;
5698                         fcport->loop_id = mb[1];
5699
5700                         ql_dbg(ql_dbg_disc, vha, 0x2001,
5701                             "Fabric Login: port in use - next loop "
5702                             "id=0x%04x, port id= %02x%02x%02x.\n",
5703                             fcport->loop_id, fcport->d_id.b.domain,
5704                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
5705
5706                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5707                         /*
5708                          * Login succeeded.
5709                          */
5710                         if (retry) {
5711                                 /* A retry occurred before. */
5712                                 *next_loopid = tmp_loopid;
5713                         } else {
5714                                 /*
5715                                  * No retry occurred before. Just increment the
5716                                  * ID value for next login.
5717                                  */
5718                                 *next_loopid = (fcport->loop_id + 1);
5719                         }
5720
5721                         if (mb[1] & BIT_0) {
5722                                 fcport->port_type = FCT_INITIATOR;
5723                         } else {
5724                                 fcport->port_type = FCT_TARGET;
5725                                 if (mb[1] & BIT_1) {
5726                                         fcport->flags |= FCF_FCP2_DEVICE;
5727                                 }
5728                         }
5729
5730                         if (mb[10] & BIT_0)
5731                                 fcport->supported_classes |= FC_COS_CLASS2;
5732                         if (mb[10] & BIT_1)
5733                                 fcport->supported_classes |= FC_COS_CLASS3;
5734
5735                         if (IS_FWI2_CAPABLE(ha)) {
5736                                 if (mb[10] & BIT_7)
5737                                         fcport->flags |=
5738                                             FCF_CONF_COMP_SUPPORTED;
5739                         }
5740
5741                         rval = QLA_SUCCESS;
5742                         break;
5743                 } else if (mb[0] == MBS_LOOP_ID_USED) {
5744                         /*
5745                          * Loop ID already used, try next loop ID.
5746                          */
5747                         fcport->loop_id++;
5748                         rval = qla2x00_find_new_loop_id(vha, fcport);
5749                         if (rval != QLA_SUCCESS) {
5750                                 /* Ran out of loop IDs to use */
5751                                 break;
5752                         }
5753                 } else if (mb[0] == MBS_COMMAND_ERROR) {
5754                         /*
5755                          * Firmware possibly timed out during login. If NO
5756                          * retries are left to do then the device is declared
5757                          * dead.
5758                          */
5759                         *next_loopid = fcport->loop_id;
5760                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
5761                             fcport->d_id.b.domain, fcport->d_id.b.area,
5762                             fcport->d_id.b.al_pa);
5763                         qla2x00_mark_device_lost(vha, fcport, 1, 0);
5764
5765                         rval = 1;
5766                         break;
5767                 } else {
5768                         /*
5769                          * unrecoverable / not handled error
5770                          */
5771                         ql_dbg(ql_dbg_disc, vha, 0x2002,
5772                             "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5773                             "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5774                             fcport->d_id.b.area, fcport->d_id.b.al_pa,
5775                             fcport->loop_id, jiffies);
5776
5777                         *next_loopid = fcport->loop_id;
5778                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
5779                             fcport->d_id.b.domain, fcport->d_id.b.area,
5780                             fcport->d_id.b.al_pa);
5781                         qla2x00_clear_loop_id(fcport);
5782                         fcport->login_retry = 0;
5783
5784                         rval = 3;
5785                         break;
5786                 }
5787         }
5788
5789         return (rval);
5790 }
5791
5792 /*
5793  * qla2x00_local_device_login
5794  *      Issue local device login command.
5795  *
5796  * Input:
5797  *      ha = adapter block pointer.
5798  *      loop_id = loop id of device to login to.
5799  *
5800  * Returns (Where's the #define!!!!):
5801  *      0 - Login successfully
5802  *      1 - Login failed
5803  *      3 - Fatal error
5804  */
5805 int
5806 qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
5807 {
5808         int             rval;
5809         uint16_t        mb[MAILBOX_REGISTER_COUNT];
5810
5811         memset(mb, 0, sizeof(mb));
5812         rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
5813         if (rval == QLA_SUCCESS) {
5814                 /* Interrogate mailbox registers for any errors */
5815                 if (mb[0] == MBS_COMMAND_ERROR)
5816                         rval = 1;
5817                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5818                         /* device not in PCB table */
5819                         rval = 3;
5820         }
5821
5822         return (rval);
5823 }
5824
5825 /*
5826  *  qla2x00_loop_resync
5827  *      Resync with fibre channel devices.
5828  *
5829  * Input:
5830  *      ha = adapter block pointer.
5831  *
5832  * Returns:
5833  *      0 = success
5834  */
5835 int
5836 qla2x00_loop_resync(scsi_qla_host_t *vha)
5837 {
5838         int rval = QLA_SUCCESS;
5839         uint32_t wait_time;
5840         struct req_que *req;
5841         struct rsp_que *rsp;
5842
5843         req = vha->req;
5844         rsp = req->rsp;
5845
5846         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5847         if (vha->flags.online) {
5848                 if (!(rval = qla2x00_fw_ready(vha))) {
5849                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
5850                         wait_time = 256;
5851                         do {
5852                                 if (!IS_QLAFX00(vha->hw)) {
5853                                         /*
5854                                          * Issue a marker after FW becomes
5855                                          * ready.
5856                                          */
5857                                         qla2x00_marker(vha, req, rsp, 0, 0,
5858                                                 MK_SYNC_ALL);
5859                                         vha->marker_needed = 0;
5860                                 }
5861
5862                                 /* Remap devices on Loop. */
5863                                 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5864
5865                                 if (IS_QLAFX00(vha->hw))
5866                                         qlafx00_configure_devices(vha);
5867                                 else
5868                                         qla2x00_configure_loop(vha);
5869
5870                                 wait_time--;
5871                         } while (!atomic_read(&vha->loop_down_timer) &&
5872                                 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5873                                 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5874                                 &vha->dpc_flags)));
5875                 }
5876         }
5877
5878         if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5879                 return (QLA_FUNCTION_FAILED);
5880
5881         if (rval)
5882                 ql_dbg(ql_dbg_disc, vha, 0x206c,
5883                     "%s *** FAILED ***.\n", __func__);
5884
5885         return (rval);
5886 }
5887
5888 /*
5889 * qla2x00_perform_loop_resync
5890 * Description: This function will set the appropriate flags and call
5891 *              qla2x00_loop_resync. If successful loop will be resynced
5892 * Arguments : scsi_qla_host_t pointer
5893 * returm    : Success or Failure
5894 */
5895
5896 int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5897 {
5898         int32_t rval = 0;
5899
5900         if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5901                 /*Configure the flags so that resync happens properly*/
5902                 atomic_set(&ha->loop_down_timer, 0);
5903                 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5904                         atomic_set(&ha->loop_state, LOOP_UP);
5905                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5906                         set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5907                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5908
5909                         rval = qla2x00_loop_resync(ha);
5910                 } else
5911                         atomic_set(&ha->loop_state, LOOP_DEAD);
5912
5913                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5914         }
5915
5916         return rval;
5917 }
5918
5919 void
5920 qla2x00_update_fcports(scsi_qla_host_t *base_vha)
5921 {
5922         fc_port_t *fcport;
5923         struct scsi_qla_host *vha;
5924         struct qla_hw_data *ha = base_vha->hw;
5925         unsigned long flags;
5926
5927         spin_lock_irqsave(&ha->vport_slock, flags);
5928         /* Go with deferred removal of rport references. */
5929         list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5930                 atomic_inc(&vha->vref_count);
5931                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5932                         if (fcport->drport &&
5933                             atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5934                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
5935                                 qla2x00_rport_del(fcport);
5936
5937                                 spin_lock_irqsave(&ha->vport_slock, flags);
5938                         }
5939                 }
5940                 atomic_dec(&vha->vref_count);
5941                 wake_up(&vha->vref_waitq);
5942         }
5943         spin_unlock_irqrestore(&ha->vport_slock, flags);
5944 }
5945
5946 /* Assumes idc_lock always held on entry */
5947 void
5948 qla83xx_reset_ownership(scsi_qla_host_t *vha)
5949 {
5950         struct qla_hw_data *ha = vha->hw;
5951         uint32_t drv_presence, drv_presence_mask;
5952         uint32_t dev_part_info1, dev_part_info2, class_type;
5953         uint32_t class_type_mask = 0x3;
5954         uint16_t fcoe_other_function = 0xffff, i;
5955
5956         if (IS_QLA8044(ha)) {
5957                 drv_presence = qla8044_rd_direct(vha,
5958                     QLA8044_CRB_DRV_ACTIVE_INDEX);
5959                 dev_part_info1 = qla8044_rd_direct(vha,
5960                     QLA8044_CRB_DEV_PART_INFO_INDEX);
5961                 dev_part_info2 = qla8044_rd_direct(vha,
5962                     QLA8044_CRB_DEV_PART_INFO2);
5963         } else {
5964                 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5965                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5966                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5967         }
5968         for (i = 0; i < 8; i++) {
5969                 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5970                 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5971                     (i != ha->portnum)) {
5972                         fcoe_other_function = i;
5973                         break;
5974                 }
5975         }
5976         if (fcoe_other_function == 0xffff) {
5977                 for (i = 0; i < 8; i++) {
5978                         class_type = ((dev_part_info2 >> (i * 4)) &
5979                             class_type_mask);
5980                         if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5981                             ((i + 8) != ha->portnum)) {
5982                                 fcoe_other_function = i + 8;
5983                                 break;
5984                         }
5985                 }
5986         }
5987         /*
5988          * Prepare drv-presence mask based on fcoe functions present.
5989          * However consider only valid physical fcoe function numbers (0-15).
5990          */
5991         drv_presence_mask = ~((1 << (ha->portnum)) |
5992                         ((fcoe_other_function == 0xffff) ?
5993                          0 : (1 << (fcoe_other_function))));
5994
5995         /* We are the reset owner iff:
5996          *    - No other protocol drivers present.
5997          *    - This is the lowest among fcoe functions. */
5998         if (!(drv_presence & drv_presence_mask) &&
5999                         (ha->portnum < fcoe_other_function)) {
6000                 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6001                     "This host is Reset owner.\n");
6002                 ha->flags.nic_core_reset_owner = 1;
6003         }
6004 }
6005
6006 static int
6007 __qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6008 {
6009         int rval = QLA_SUCCESS;
6010         struct qla_hw_data *ha = vha->hw;
6011         uint32_t drv_ack;
6012
6013         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6014         if (rval == QLA_SUCCESS) {
6015                 drv_ack |= (1 << ha->portnum);
6016                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6017         }
6018
6019         return rval;
6020 }
6021
6022 static int
6023 __qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6024 {
6025         int rval = QLA_SUCCESS;
6026         struct qla_hw_data *ha = vha->hw;
6027         uint32_t drv_ack;
6028
6029         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6030         if (rval == QLA_SUCCESS) {
6031                 drv_ack &= ~(1 << ha->portnum);
6032                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6033         }
6034
6035         return rval;
6036 }
6037
6038 static const char *
6039 qla83xx_dev_state_to_string(uint32_t dev_state)
6040 {
6041         switch (dev_state) {
6042         case QLA8XXX_DEV_COLD:
6043                 return "COLD/RE-INIT";
6044         case QLA8XXX_DEV_INITIALIZING:
6045                 return "INITIALIZING";
6046         case QLA8XXX_DEV_READY:
6047                 return "READY";
6048         case QLA8XXX_DEV_NEED_RESET:
6049                 return "NEED RESET";
6050         case QLA8XXX_DEV_NEED_QUIESCENT:
6051                 return "NEED QUIESCENT";
6052         case QLA8XXX_DEV_FAILED:
6053                 return "FAILED";
6054         case QLA8XXX_DEV_QUIESCENT:
6055                 return "QUIESCENT";
6056         default:
6057                 return "Unknown";
6058         }
6059 }
6060
6061 /* Assumes idc-lock always held on entry */
6062 void
6063 qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6064 {
6065         struct qla_hw_data *ha = vha->hw;
6066         uint32_t idc_audit_reg = 0, duration_secs = 0;
6067
6068         switch (audit_type) {
6069         case IDC_AUDIT_TIMESTAMP:
6070                 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6071                 idc_audit_reg = (ha->portnum) |
6072                     (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6073                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6074                 break;
6075
6076         case IDC_AUDIT_COMPLETION:
6077                 duration_secs = ((jiffies_to_msecs(jiffies) -
6078                     jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6079                 idc_audit_reg = (ha->portnum) |
6080                     (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6081                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6082                 break;
6083
6084         default:
6085                 ql_log(ql_log_warn, vha, 0xb078,
6086                     "Invalid audit type specified.\n");
6087                 break;
6088         }
6089 }
6090
6091 /* Assumes idc_lock always held on entry */
6092 static int
6093 qla83xx_initiating_reset(scsi_qla_host_t *vha)
6094 {
6095         struct qla_hw_data *ha = vha->hw;
6096         uint32_t  idc_control, dev_state;
6097
6098         __qla83xx_get_idc_control(vha, &idc_control);
6099         if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6100                 ql_log(ql_log_info, vha, 0xb080,
6101                     "NIC Core reset has been disabled. idc-control=0x%x\n",
6102                     idc_control);
6103                 return QLA_FUNCTION_FAILED;
6104         }
6105
6106         /* Set NEED-RESET iff in READY state and we are the reset-owner */
6107         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6108         if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6109                 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6110                     QLA8XXX_DEV_NEED_RESET);
6111                 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6112                 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6113         } else {
6114                 const char *state = qla83xx_dev_state_to_string(dev_state);
6115                 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6116
6117                 /* SV: XXX: Is timeout required here? */
6118                 /* Wait for IDC state change READY -> NEED_RESET */
6119                 while (dev_state == QLA8XXX_DEV_READY) {
6120                         qla83xx_idc_unlock(vha, 0);
6121                         msleep(200);
6122                         qla83xx_idc_lock(vha, 0);
6123                         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6124                 }
6125         }
6126
6127         /* Send IDC ack by writing to drv-ack register */
6128         __qla83xx_set_drv_ack(vha);
6129
6130         return QLA_SUCCESS;
6131 }
6132
6133 int
6134 __qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6135 {
6136         return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6137 }
6138
6139 int
6140 __qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6141 {
6142         return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6143 }
6144
6145 static int
6146 qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6147 {
6148         uint32_t drv_presence = 0;
6149         struct qla_hw_data *ha = vha->hw;
6150
6151         qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6152         if (drv_presence & (1 << ha->portnum))
6153                 return QLA_SUCCESS;
6154         else
6155                 return QLA_TEST_FAILED;
6156 }
6157
6158 int
6159 qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6160 {
6161         int rval = QLA_SUCCESS;
6162         struct qla_hw_data *ha = vha->hw;
6163
6164         ql_dbg(ql_dbg_p3p, vha, 0xb058,
6165             "Entered  %s().\n", __func__);
6166
6167         if (vha->device_flags & DFLG_DEV_FAILED) {
6168                 ql_log(ql_log_warn, vha, 0xb059,
6169                     "Device in unrecoverable FAILED state.\n");
6170                 return QLA_FUNCTION_FAILED;
6171         }
6172
6173         qla83xx_idc_lock(vha, 0);
6174
6175         if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6176                 ql_log(ql_log_warn, vha, 0xb05a,
6177                     "Function=0x%x has been removed from IDC participation.\n",
6178                     ha->portnum);
6179                 rval = QLA_FUNCTION_FAILED;
6180                 goto exit;
6181         }
6182
6183         qla83xx_reset_ownership(vha);
6184
6185         rval = qla83xx_initiating_reset(vha);
6186
6187         /*
6188          * Perform reset if we are the reset-owner,
6189          * else wait till IDC state changes to READY/FAILED.
6190          */
6191         if (rval == QLA_SUCCESS) {
6192                 rval = qla83xx_idc_state_handler(vha);
6193
6194                 if (rval == QLA_SUCCESS)
6195                         ha->flags.nic_core_hung = 0;
6196                 __qla83xx_clear_drv_ack(vha);
6197         }
6198
6199 exit:
6200         qla83xx_idc_unlock(vha, 0);
6201
6202         ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6203
6204         return rval;
6205 }
6206
6207 int
6208 qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6209 {
6210         struct qla_hw_data *ha = vha->hw;
6211         int rval = QLA_FUNCTION_FAILED;
6212
6213         if (!IS_MCTP_CAPABLE(ha)) {
6214                 /* This message can be removed from the final version */
6215                 ql_log(ql_log_info, vha, 0x506d,
6216                     "This board is not MCTP capable\n");
6217                 return rval;
6218         }
6219
6220         if (!ha->mctp_dump) {
6221                 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6222                     MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6223
6224                 if (!ha->mctp_dump) {
6225                         ql_log(ql_log_warn, vha, 0x506e,
6226                             "Failed to allocate memory for mctp dump\n");
6227                         return rval;
6228                 }
6229         }
6230
6231 #define MCTP_DUMP_STR_ADDR      0x00000000
6232         rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6233             MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6234         if (rval != QLA_SUCCESS) {
6235                 ql_log(ql_log_warn, vha, 0x506f,
6236                     "Failed to capture mctp dump\n");
6237         } else {
6238                 ql_log(ql_log_info, vha, 0x5070,
6239                     "Mctp dump capture for host (%ld/%p).\n",
6240                     vha->host_no, ha->mctp_dump);
6241                 ha->mctp_dumped = 1;
6242         }
6243
6244         if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
6245                 ha->flags.nic_core_reset_hdlr_active = 1;
6246                 rval = qla83xx_restart_nic_firmware(vha);
6247                 if (rval)
6248                         /* NIC Core reset failed. */
6249                         ql_log(ql_log_warn, vha, 0x5071,
6250                             "Failed to restart nic firmware\n");
6251                 else
6252                         ql_dbg(ql_dbg_p3p, vha, 0xb084,
6253                             "Restarted NIC firmware successfully.\n");
6254                 ha->flags.nic_core_reset_hdlr_active = 0;
6255         }
6256
6257         return rval;
6258
6259 }
6260
6261 /*
6262 * qla2x00_quiesce_io
6263 * Description: This function will block the new I/Os
6264 *              Its not aborting any I/Os as context
6265 *              is not destroyed during quiescence
6266 * Arguments: scsi_qla_host_t
6267 * return   : void
6268 */
6269 void
6270 qla2x00_quiesce_io(scsi_qla_host_t *vha)
6271 {
6272         struct qla_hw_data *ha = vha->hw;
6273         struct scsi_qla_host *vp;
6274
6275         ql_dbg(ql_dbg_dpc, vha, 0x401d,
6276             "Quiescing I/O - ha=%p.\n", ha);
6277
6278         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6279         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6280                 atomic_set(&vha->loop_state, LOOP_DOWN);
6281                 qla2x00_mark_all_devices_lost(vha, 0);
6282                 list_for_each_entry(vp, &ha->vp_list, list)
6283                         qla2x00_mark_all_devices_lost(vp, 0);
6284         } else {
6285                 if (!atomic_read(&vha->loop_down_timer))
6286                         atomic_set(&vha->loop_down_timer,
6287                                         LOOP_DOWN_TIME);
6288         }
6289         /* Wait for pending cmds to complete */
6290         qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6291 }
6292
6293 void
6294 qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6295 {
6296         struct qla_hw_data *ha = vha->hw;
6297         struct scsi_qla_host *vp;
6298         unsigned long flags;
6299         fc_port_t *fcport;
6300         u16 i;
6301
6302         /* For ISP82XX, driver waits for completion of the commands.
6303          * online flag should be set.
6304          */
6305         if (!(IS_P3P_TYPE(ha)))
6306                 vha->flags.online = 0;
6307         ha->flags.chip_reset_done = 0;
6308         clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6309         vha->qla_stats.total_isp_aborts++;
6310
6311         ql_log(ql_log_info, vha, 0x00af,
6312             "Performing ISP error recovery - ha=%p.\n", ha);
6313
6314         ha->flags.purge_mbox = 1;
6315         /* For ISP82XX, reset_chip is just disabling interrupts.
6316          * Driver waits for the completion of the commands.
6317          * the interrupts need to be enabled.
6318          */
6319         if (!(IS_P3P_TYPE(ha)))
6320                 ha->isp_ops->reset_chip(vha);
6321
6322         SAVE_TOPO(ha);
6323         ha->flags.rida_fmt2 = 0;
6324         ha->flags.n2n_ae = 0;
6325         ha->flags.lip_ae = 0;
6326         ha->current_topology = 0;
6327         ha->flags.fw_started = 0;
6328         ha->flags.fw_init_done = 0;
6329         ha->chip_reset++;
6330         ha->base_qpair->chip_reset = ha->chip_reset;
6331         for (i = 0; i < ha->max_qpairs; i++) {
6332                 if (ha->queue_pair_map[i])
6333                         ha->queue_pair_map[i]->chip_reset =
6334                                 ha->base_qpair->chip_reset;
6335         }
6336
6337         /* purge MBox commands */
6338         if (atomic_read(&ha->num_pend_mbx_stage3)) {
6339                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
6340                 complete(&ha->mbx_intr_comp);
6341         }
6342
6343         i = 0;
6344         while (atomic_read(&ha->num_pend_mbx_stage3) ||
6345             atomic_read(&ha->num_pend_mbx_stage2) ||
6346             atomic_read(&ha->num_pend_mbx_stage1)) {
6347                 msleep(20);
6348                 i++;
6349                 if (i > 50)
6350                         break;
6351         }
6352         ha->flags.purge_mbox = 0;
6353
6354         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6355         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6356                 atomic_set(&vha->loop_state, LOOP_DOWN);
6357                 qla2x00_mark_all_devices_lost(vha, 0);
6358
6359                 spin_lock_irqsave(&ha->vport_slock, flags);
6360                 list_for_each_entry(vp, &ha->vp_list, list) {
6361                         atomic_inc(&vp->vref_count);
6362                         spin_unlock_irqrestore(&ha->vport_slock, flags);
6363
6364                         qla2x00_mark_all_devices_lost(vp, 0);
6365
6366                         spin_lock_irqsave(&ha->vport_slock, flags);
6367                         atomic_dec(&vp->vref_count);
6368                 }
6369                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6370         } else {
6371                 if (!atomic_read(&vha->loop_down_timer))
6372                         atomic_set(&vha->loop_down_timer,
6373                             LOOP_DOWN_TIME);
6374         }
6375
6376         /* Clear all async request states across all VPs. */
6377         list_for_each_entry(fcport, &vha->vp_fcports, list)
6378                 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6379         spin_lock_irqsave(&ha->vport_slock, flags);
6380         list_for_each_entry(vp, &ha->vp_list, list) {
6381                 atomic_inc(&vp->vref_count);
6382                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6383
6384                 list_for_each_entry(fcport, &vp->vp_fcports, list)
6385                         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6386
6387                 spin_lock_irqsave(&ha->vport_slock, flags);
6388                 atomic_dec(&vp->vref_count);
6389         }
6390         spin_unlock_irqrestore(&ha->vport_slock, flags);
6391
6392         if (!ha->flags.eeh_busy) {
6393                 /* Make sure for ISP 82XX IO DMA is complete */
6394                 if (IS_P3P_TYPE(ha)) {
6395                         qla82xx_chip_reset_cleanup(vha);
6396                         ql_log(ql_log_info, vha, 0x00b4,
6397                             "Done chip reset cleanup.\n");
6398
6399                         /* Done waiting for pending commands.
6400                          * Reset the online flag.
6401                          */
6402                         vha->flags.online = 0;
6403                 }
6404
6405                 /* Requeue all commands in outstanding command list. */
6406                 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6407         }
6408         /* memory barrier */
6409         wmb();
6410 }
6411
6412 /*
6413 *  qla2x00_abort_isp
6414 *      Resets ISP and aborts all outstanding commands.
6415 *
6416 * Input:
6417 *      ha           = adapter block pointer.
6418 *
6419 * Returns:
6420 *      0 = success
6421 */
6422 int
6423 qla2x00_abort_isp(scsi_qla_host_t *vha)
6424 {
6425         int rval;
6426         uint8_t        status = 0;
6427         struct qla_hw_data *ha = vha->hw;
6428         struct scsi_qla_host *vp;
6429         struct req_que *req = ha->req_q_map[0];
6430         unsigned long flags;
6431
6432         if (vha->flags.online) {
6433                 qla2x00_abort_isp_cleanup(vha);
6434
6435                 if (IS_QLA8031(ha)) {
6436                         ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6437                             "Clearing fcoe driver presence.\n");
6438                         if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6439                                 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6440                                     "Error while clearing DRV-Presence.\n");
6441                 }
6442
6443                 if (unlikely(pci_channel_offline(ha->pdev) &&
6444                     ha->flags.pci_channel_io_perm_failure)) {
6445                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6446                         status = 0;
6447                         return status;
6448                 }
6449
6450                 ha->isp_ops->get_flash_version(vha, req->ring);
6451
6452                 ha->isp_ops->nvram_config(vha);
6453
6454                 if (!qla2x00_restart_isp(vha)) {
6455                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
6456
6457                         if (!atomic_read(&vha->loop_down_timer)) {
6458                                 /*
6459                                  * Issue marker command only when we are going
6460                                  * to start the I/O .
6461                                  */
6462                                 vha->marker_needed = 1;
6463                         }
6464
6465                         vha->flags.online = 1;
6466
6467                         ha->isp_ops->enable_intrs(ha);
6468
6469                         ha->isp_abort_cnt = 0;
6470                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6471
6472                         if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6473                                 qla2x00_get_fw_version(vha);
6474                         if (ha->fce) {
6475                                 ha->flags.fce_enabled = 1;
6476                                 memset(ha->fce, 0,
6477                                     fce_calc_size(ha->fce_bufs));
6478                                 rval = qla2x00_enable_fce_trace(vha,
6479                                     ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6480                                     &ha->fce_bufs);
6481                                 if (rval) {
6482                                         ql_log(ql_log_warn, vha, 0x8033,
6483                                             "Unable to reinitialize FCE "
6484                                             "(%d).\n", rval);
6485                                         ha->flags.fce_enabled = 0;
6486                                 }
6487                         }
6488
6489                         if (ha->eft) {
6490                                 memset(ha->eft, 0, EFT_SIZE);
6491                                 rval = qla2x00_enable_eft_trace(vha,
6492                                     ha->eft_dma, EFT_NUM_BUFFERS);
6493                                 if (rval) {
6494                                         ql_log(ql_log_warn, vha, 0x8034,
6495                                             "Unable to reinitialize EFT "
6496                                             "(%d).\n", rval);
6497                                 }
6498                         }
6499                 } else {        /* failed the ISP abort */
6500                         vha->flags.online = 1;
6501                         if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
6502                                 if (ha->isp_abort_cnt == 0) {
6503                                         ql_log(ql_log_fatal, vha, 0x8035,
6504                                             "ISP error recover failed - "
6505                                             "board disabled.\n");
6506                                         /*
6507                                          * The next call disables the board
6508                                          * completely.
6509                                          */
6510                                         ha->isp_ops->reset_adapter(vha);
6511                                         vha->flags.online = 0;
6512                                         clear_bit(ISP_ABORT_RETRY,
6513                                             &vha->dpc_flags);
6514                                         status = 0;
6515                                 } else { /* schedule another ISP abort */
6516                                         ha->isp_abort_cnt--;
6517                                         ql_dbg(ql_dbg_taskm, vha, 0x8020,
6518                                             "ISP abort - retry remaining %d.\n",
6519                                             ha->isp_abort_cnt);
6520                                         status = 1;
6521                                 }
6522                         } else {
6523                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
6524                                 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6525                                     "ISP error recovery - retrying (%d) "
6526                                     "more times.\n", ha->isp_abort_cnt);
6527                                 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6528                                 status = 1;
6529                         }
6530                 }
6531
6532         }
6533
6534         if (!status) {
6535                 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
6536                 qla2x00_configure_hba(vha);
6537                 spin_lock_irqsave(&ha->vport_slock, flags);
6538                 list_for_each_entry(vp, &ha->vp_list, list) {
6539                         if (vp->vp_idx) {
6540                                 atomic_inc(&vp->vref_count);
6541                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6542
6543                                 qla2x00_vp_abort_isp(vp);
6544
6545                                 spin_lock_irqsave(&ha->vport_slock, flags);
6546                                 atomic_dec(&vp->vref_count);
6547                         }
6548                 }
6549                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6550
6551                 if (IS_QLA8031(ha)) {
6552                         ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6553                             "Setting back fcoe driver presence.\n");
6554                         if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6555                                 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6556                                     "Error while setting DRV-Presence.\n");
6557                 }
6558         } else {
6559                 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6560                        __func__);
6561         }
6562
6563         return(status);
6564 }
6565
6566 /*
6567 *  qla2x00_restart_isp
6568 *      restarts the ISP after a reset
6569 *
6570 * Input:
6571 *      ha = adapter block pointer.
6572 *
6573 * Returns:
6574 *      0 = success
6575 */
6576 static int
6577 qla2x00_restart_isp(scsi_qla_host_t *vha)
6578 {
6579         int status = 0;
6580         struct qla_hw_data *ha = vha->hw;
6581         struct req_que *req = ha->req_q_map[0];
6582         struct rsp_que *rsp = ha->rsp_q_map[0];
6583
6584         /* If firmware needs to be loaded */
6585         if (qla2x00_isp_firmware(vha)) {
6586                 vha->flags.online = 0;
6587                 status = ha->isp_ops->chip_diag(vha);
6588                 if (!status)
6589                         status = qla2x00_setup_chip(vha);
6590         }
6591
6592         if (!status && !(status = qla2x00_init_rings(vha))) {
6593                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
6594                 ha->flags.chip_reset_done = 1;
6595
6596                 /* Initialize the queues in use */
6597                 qla25xx_init_queues(ha);
6598
6599                 status = qla2x00_fw_ready(vha);
6600                 if (!status) {
6601                         /* Issue a marker after FW becomes ready. */
6602                         qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
6603                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6604                 }
6605
6606                 /* if no cable then assume it's good */
6607                 if ((vha->device_flags & DFLG_NO_CABLE))
6608                         status = 0;
6609         }
6610         return (status);
6611 }
6612
6613 static int
6614 qla25xx_init_queues(struct qla_hw_data *ha)
6615 {
6616         struct rsp_que *rsp = NULL;
6617         struct req_que *req = NULL;
6618         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6619         int ret = -1;
6620         int i;
6621
6622         for (i = 1; i < ha->max_rsp_queues; i++) {
6623                 rsp = ha->rsp_q_map[i];
6624                 if (rsp && test_bit(i, ha->rsp_qid_map)) {
6625                         rsp->options &= ~BIT_0;
6626                         ret = qla25xx_init_rsp_que(base_vha, rsp);
6627                         if (ret != QLA_SUCCESS)
6628                                 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6629                                     "%s Rsp que: %d init failed.\n",
6630                                     __func__, rsp->id);
6631                         else
6632                                 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6633                                     "%s Rsp que: %d inited.\n",
6634                                     __func__, rsp->id);
6635                 }
6636         }
6637         for (i = 1; i < ha->max_req_queues; i++) {
6638                 req = ha->req_q_map[i];
6639                 if (req && test_bit(i, ha->req_qid_map)) {
6640                         /* Clear outstanding commands array. */
6641                         req->options &= ~BIT_0;
6642                         ret = qla25xx_init_req_que(base_vha, req);
6643                         if (ret != QLA_SUCCESS)
6644                                 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6645                                     "%s Req que: %d init failed.\n",
6646                                     __func__, req->id);
6647                         else
6648                                 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6649                                     "%s Req que: %d inited.\n",
6650                                     __func__, req->id);
6651                 }
6652         }
6653         return ret;
6654 }
6655
6656 /*
6657 * qla2x00_reset_adapter
6658 *      Reset adapter.
6659 *
6660 * Input:
6661 *      ha = adapter block pointer.
6662 */
6663 void
6664 qla2x00_reset_adapter(scsi_qla_host_t *vha)
6665 {
6666         unsigned long flags = 0;
6667         struct qla_hw_data *ha = vha->hw;
6668         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
6669
6670         vha->flags.online = 0;
6671         ha->isp_ops->disable_intrs(ha);
6672
6673         spin_lock_irqsave(&ha->hardware_lock, flags);
6674         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6675         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
6676         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6677         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
6678         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6679 }
6680
6681 void
6682 qla24xx_reset_adapter(scsi_qla_host_t *vha)
6683 {
6684         unsigned long flags = 0;
6685         struct qla_hw_data *ha = vha->hw;
6686         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6687
6688         if (IS_P3P_TYPE(ha))
6689                 return;
6690
6691         vha->flags.online = 0;
6692         ha->isp_ops->disable_intrs(ha);
6693
6694         spin_lock_irqsave(&ha->hardware_lock, flags);
6695         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6696         RD_REG_DWORD(&reg->hccr);
6697         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6698         RD_REG_DWORD(&reg->hccr);
6699         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6700
6701         if (IS_NOPOLLING_TYPE(ha))
6702                 ha->isp_ops->enable_intrs(ha);
6703 }
6704
6705 /* On sparc systems, obtain port and node WWN from firmware
6706  * properties.
6707  */
6708 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6709         struct nvram_24xx *nv)
6710 {
6711 #ifdef CONFIG_SPARC
6712         struct qla_hw_data *ha = vha->hw;
6713         struct pci_dev *pdev = ha->pdev;
6714         struct device_node *dp = pci_device_to_OF_node(pdev);
6715         const u8 *val;
6716         int len;
6717
6718         val = of_get_property(dp, "port-wwn", &len);
6719         if (val && len >= WWN_SIZE)
6720                 memcpy(nv->port_name, val, WWN_SIZE);
6721
6722         val = of_get_property(dp, "node-wwn", &len);
6723         if (val && len >= WWN_SIZE)
6724                 memcpy(nv->node_name, val, WWN_SIZE);
6725 #endif
6726 }
6727
6728 int
6729 qla24xx_nvram_config(scsi_qla_host_t *vha)
6730 {
6731         int   rval;
6732         struct init_cb_24xx *icb;
6733         struct nvram_24xx *nv;
6734         uint32_t *dptr;
6735         uint8_t  *dptr1, *dptr2;
6736         uint32_t chksum;
6737         uint16_t cnt;
6738         struct qla_hw_data *ha = vha->hw;
6739
6740         rval = QLA_SUCCESS;
6741         icb = (struct init_cb_24xx *)ha->init_cb;
6742         nv = ha->nvram;
6743
6744         /* Determine NVRAM starting address. */
6745         if (ha->port_no == 0) {
6746                 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6747                 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6748         } else {
6749                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
6750                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6751         }
6752
6753         ha->nvram_size = sizeof(struct nvram_24xx);
6754         ha->vpd_size = FA_NVRAM_VPD_SIZE;
6755
6756         /* Get VPD data into cache */
6757         ha->vpd = ha->nvram + VPD_OFFSET;
6758         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
6759             ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6760
6761         /* Get NVRAM data into cache and calculate checksum. */
6762         dptr = (uint32_t *)nv;
6763         ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
6764             ha->nvram_size);
6765         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6766                 chksum += le32_to_cpu(*dptr);
6767
6768         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6769             "Contents of NVRAM\n");
6770         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6771             (uint8_t *)nv, ha->nvram_size);
6772
6773         /* Bad NVRAM data, set defaults parameters. */
6774         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6775             || nv->id[3] != ' ' ||
6776             nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
6777                 /* Reset NVRAM data. */
6778                 ql_log(ql_log_warn, vha, 0x006b,
6779                     "Inconsistent NVRAM detected: checksum=0x%x id=%c "
6780                     "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6781                 ql_log(ql_log_warn, vha, 0x006c,
6782                     "Falling back to functioning (yet invalid -- WWPN) "
6783                     "defaults.\n");
6784
6785                 /*
6786                  * Set default initialization control block.
6787                  */
6788                 memset(nv, 0, ha->nvram_size);
6789                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6790                 nv->version = cpu_to_le16(ICB_VERSION);
6791                 nv->frame_payload_size = 2048;
6792                 nv->execution_throttle = cpu_to_le16(0xFFFF);
6793                 nv->exchange_count = cpu_to_le16(0);
6794                 nv->hard_address = cpu_to_le16(124);
6795                 nv->port_name[0] = 0x21;
6796                 nv->port_name[1] = 0x00 + ha->port_no + 1;
6797                 nv->port_name[2] = 0x00;
6798                 nv->port_name[3] = 0xe0;
6799                 nv->port_name[4] = 0x8b;
6800                 nv->port_name[5] = 0x1c;
6801                 nv->port_name[6] = 0x55;
6802                 nv->port_name[7] = 0x86;
6803                 nv->node_name[0] = 0x20;
6804                 nv->node_name[1] = 0x00;
6805                 nv->node_name[2] = 0x00;
6806                 nv->node_name[3] = 0xe0;
6807                 nv->node_name[4] = 0x8b;
6808                 nv->node_name[5] = 0x1c;
6809                 nv->node_name[6] = 0x55;
6810                 nv->node_name[7] = 0x86;
6811                 qla24xx_nvram_wwn_from_ofw(vha, nv);
6812                 nv->login_retry_count = cpu_to_le16(8);
6813                 nv->interrupt_delay_timer = cpu_to_le16(0);
6814                 nv->login_timeout = cpu_to_le16(0);
6815                 nv->firmware_options_1 =
6816                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6817                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6818                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6819                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6820                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6821                 nv->efi_parameters = cpu_to_le32(0);
6822                 nv->reset_delay = 5;
6823                 nv->max_luns_per_target = cpu_to_le16(128);
6824                 nv->port_down_retry_count = cpu_to_le16(30);
6825                 nv->link_down_timeout = cpu_to_le16(30);
6826
6827                 rval = 1;
6828         }
6829
6830         if (qla_tgt_mode_enabled(vha)) {
6831                 /* Don't enable full login after initial LIP */
6832                 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6833                 /* Don't enable LIP full login for initiator */
6834                 nv->host_p &= cpu_to_le32(~BIT_10);
6835         }
6836
6837         qlt_24xx_config_nvram_stage1(vha, nv);
6838
6839         /* Reset Initialization control block */
6840         memset(icb, 0, ha->init_cb_size);
6841
6842         /* Copy 1st segment. */
6843         dptr1 = (uint8_t *)icb;
6844         dptr2 = (uint8_t *)&nv->version;
6845         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6846         while (cnt--)
6847                 *dptr1++ = *dptr2++;
6848
6849         icb->login_retry_count = nv->login_retry_count;
6850         icb->link_down_on_nos = nv->link_down_on_nos;
6851
6852         /* Copy 2nd segment. */
6853         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6854         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6855         cnt = (uint8_t *)&icb->reserved_3 -
6856             (uint8_t *)&icb->interrupt_delay_timer;
6857         while (cnt--)
6858                 *dptr1++ = *dptr2++;
6859
6860         /*
6861          * Setup driver NVRAM options.
6862          */
6863         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
6864             "QLA2462");
6865
6866         qlt_24xx_config_nvram_stage2(vha, icb);
6867
6868         if (nv->host_p & cpu_to_le32(BIT_15)) {
6869                 /* Use alternate WWN? */
6870                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6871                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6872         }
6873
6874         /* Prepare nodename */
6875         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
6876                 /*
6877                  * Firmware will apply the following mask if the nodename was
6878                  * not provided.
6879                  */
6880                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6881                 icb->node_name[0] &= 0xF0;
6882         }
6883
6884         /* Set host adapter parameters. */
6885         ha->flags.disable_risc_code_load = 0;
6886         ha->flags.enable_lip_reset = 0;
6887         ha->flags.enable_lip_full_login =
6888             le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6889         ha->flags.enable_target_reset =
6890             le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
6891         ha->flags.enable_led_scheme = 0;
6892         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
6893
6894         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6895             (BIT_6 | BIT_5 | BIT_4)) >> 4;
6896
6897         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6898             sizeof(ha->fw_seriallink_options24));
6899
6900         /* save HBA serial number */
6901         ha->serial0 = icb->port_name[5];
6902         ha->serial1 = icb->port_name[6];
6903         ha->serial2 = icb->port_name[7];
6904         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6905         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
6906
6907         icb->execution_throttle = cpu_to_le16(0xFFFF);
6908
6909         ha->retry_count = le16_to_cpu(nv->login_retry_count);
6910
6911         /* Set minimum login_timeout to 4 seconds. */
6912         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6913                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6914         if (le16_to_cpu(nv->login_timeout) < 4)
6915                 nv->login_timeout = cpu_to_le16(4);
6916         ha->login_timeout = le16_to_cpu(nv->login_timeout);
6917
6918         /* Set minimum RATOV to 100 tenths of a second. */
6919         ha->r_a_tov = 100;
6920
6921         ha->loop_reset_delay = nv->reset_delay;
6922
6923         /* Link Down Timeout = 0:
6924          *
6925          *      When Port Down timer expires we will start returning
6926          *      I/O's to OS with "DID_NO_CONNECT".
6927          *
6928          * Link Down Timeout != 0:
6929          *
6930          *       The driver waits for the link to come up after link down
6931          *       before returning I/Os to OS with "DID_NO_CONNECT".
6932          */
6933         if (le16_to_cpu(nv->link_down_timeout) == 0) {
6934                 ha->loop_down_abort_time =
6935                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6936         } else {
6937                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6938                 ha->loop_down_abort_time =
6939                     (LOOP_DOWN_TIME - ha->link_down_timeout);
6940         }
6941
6942         /* Need enough time to try and get the port back. */
6943         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6944         if (qlport_down_retry)
6945                 ha->port_down_retry_count = qlport_down_retry;
6946
6947         /* Set login_retry_count */
6948         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
6949         if (ha->port_down_retry_count ==
6950             le16_to_cpu(nv->port_down_retry_count) &&
6951             ha->port_down_retry_count > 3)
6952                 ha->login_retry_count = ha->port_down_retry_count;
6953         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6954                 ha->login_retry_count = ha->port_down_retry_count;
6955         if (ql2xloginretrycount)
6956                 ha->login_retry_count = ql2xloginretrycount;
6957
6958         /* Enable ZIO. */
6959         if (!vha->flags.init_done) {
6960                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6961                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6962                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6963                     le16_to_cpu(icb->interrupt_delay_timer): 2;
6964         }
6965         icb->firmware_options_2 &= cpu_to_le32(
6966             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
6967         vha->flags.process_response_queue = 0;
6968         if (ha->zio_mode != QLA_ZIO_DISABLED) {
6969                 ha->zio_mode = QLA_ZIO_MODE_6;
6970
6971                 ql_log(ql_log_info, vha, 0x006f,
6972                     "ZIO mode %d enabled; timer delay (%d us).\n",
6973                     ha->zio_mode, ha->zio_timer * 100);
6974
6975                 icb->firmware_options_2 |= cpu_to_le32(
6976                     (uint32_t)ha->zio_mode);
6977                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
6978                 vha->flags.process_response_queue = 1;
6979         }
6980
6981         if (rval) {
6982                 ql_log(ql_log_warn, vha, 0x0070,
6983                     "NVRAM configuration failed.\n");
6984         }
6985         return (rval);
6986 }
6987
6988 uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6989 {
6990         struct qla27xx_image_status pri_image_status, sec_image_status;
6991         uint8_t valid_pri_image, valid_sec_image;
6992         uint32_t *wptr;
6993         uint32_t cnt, chksum, size;
6994         struct qla_hw_data *ha = vha->hw;
6995
6996         valid_pri_image = valid_sec_image = 1;
6997         ha->active_image = 0;
6998         size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6999
7000         if (!ha->flt_region_img_status_pri) {
7001                 valid_pri_image = 0;
7002                 goto check_sec_image;
7003         }
7004
7005         qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
7006             ha->flt_region_img_status_pri, size);
7007
7008         if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7009                 ql_dbg(ql_dbg_init, vha, 0x018b,
7010                     "Primary image signature (0x%x) not valid\n",
7011                     pri_image_status.signature);
7012                 valid_pri_image = 0;
7013                 goto check_sec_image;
7014         }
7015
7016         wptr = (uint32_t *)(&pri_image_status);
7017         cnt = size;
7018
7019         for (chksum = 0; cnt--; wptr++)
7020                 chksum += le32_to_cpu(*wptr);
7021
7022         if (chksum) {
7023                 ql_dbg(ql_dbg_init, vha, 0x018c,
7024                     "Checksum validation failed for primary image (0x%x)\n",
7025                     chksum);
7026                 valid_pri_image = 0;
7027         }
7028
7029 check_sec_image:
7030         if (!ha->flt_region_img_status_sec) {
7031                 valid_sec_image = 0;
7032                 goto check_valid_image;
7033         }
7034
7035         qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7036             ha->flt_region_img_status_sec, size);
7037
7038         if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7039                 ql_dbg(ql_dbg_init, vha, 0x018d,
7040                     "Secondary image signature(0x%x) not valid\n",
7041                     sec_image_status.signature);
7042                 valid_sec_image = 0;
7043                 goto check_valid_image;
7044         }
7045
7046         wptr = (uint32_t *)(&sec_image_status);
7047         cnt = size;
7048         for (chksum = 0; cnt--; wptr++)
7049                 chksum += le32_to_cpu(*wptr);
7050         if (chksum) {
7051                 ql_dbg(ql_dbg_init, vha, 0x018e,
7052                     "Checksum validation failed for secondary image (0x%x)\n",
7053                     chksum);
7054                 valid_sec_image = 0;
7055         }
7056
7057 check_valid_image:
7058         if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7059                 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7060         if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7061                 if (!ha->active_image ||
7062                     pri_image_status.generation_number <
7063                     sec_image_status.generation_number)
7064                         ha->active_image = QLA27XX_SECONDARY_IMAGE;
7065         }
7066
7067         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x018f, "%s image\n",
7068             ha->active_image == 0 ? "default bootld and fw" :
7069             ha->active_image == 1 ? "primary" :
7070             ha->active_image == 2 ? "secondary" :
7071             "Invalid");
7072
7073         return ha->active_image;
7074 }
7075
7076 static int
7077 qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7078     uint32_t faddr)
7079 {
7080         int     rval = QLA_SUCCESS;
7081         int     segments, fragment;
7082         uint32_t *dcode, dlen;
7083         uint32_t risc_addr;
7084         uint32_t risc_size;
7085         uint32_t i;
7086         struct qla_hw_data *ha = vha->hw;
7087         struct req_que *req = ha->req_q_map[0];
7088
7089         ql_dbg(ql_dbg_init, vha, 0x008b,
7090             "FW: Loading firmware from flash (%x).\n", faddr);
7091
7092         rval = QLA_SUCCESS;
7093
7094         segments = FA_RISC_CODE_SEGMENTS;
7095         dcode = (uint32_t *)req->ring;
7096         *srisc_addr = 0;
7097
7098         if (IS_QLA27XX(ha) &&
7099             qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7100                 faddr = ha->flt_region_fw_sec;
7101
7102         /* Validate firmware image by checking version. */
7103         qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
7104         for (i = 0; i < 4; i++)
7105                 dcode[i] = be32_to_cpu(dcode[i]);
7106         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7107             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7108             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7109                 dcode[3] == 0)) {
7110                 ql_log(ql_log_fatal, vha, 0x008c,
7111                     "Unable to verify the integrity of flash firmware "
7112                     "image.\n");
7113                 ql_log(ql_log_fatal, vha, 0x008d,
7114                     "Firmware data: %08x %08x %08x %08x.\n",
7115                     dcode[0], dcode[1], dcode[2], dcode[3]);
7116
7117                 return QLA_FUNCTION_FAILED;
7118         }
7119
7120         while (segments && rval == QLA_SUCCESS) {
7121                 /* Read segment's load information. */
7122                 qla24xx_read_flash_data(vha, dcode, faddr, 4);
7123
7124                 risc_addr = be32_to_cpu(dcode[2]);
7125                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7126                 risc_size = be32_to_cpu(dcode[3]);
7127
7128                 fragment = 0;
7129                 while (risc_size > 0 && rval == QLA_SUCCESS) {
7130                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7131                         if (dlen > risc_size)
7132                                 dlen = risc_size;
7133
7134                         ql_dbg(ql_dbg_init, vha, 0x008e,
7135                             "Loading risc segment@ risc addr %x "
7136                             "number of dwords 0x%x offset 0x%x.\n",
7137                             risc_addr, dlen, faddr);
7138
7139                         qla24xx_read_flash_data(vha, dcode, faddr, dlen);
7140                         for (i = 0; i < dlen; i++)
7141                                 dcode[i] = swab32(dcode[i]);
7142
7143                         rval = qla2x00_load_ram(vha, req->dma, risc_addr,
7144                             dlen);
7145                         if (rval) {
7146                                 ql_log(ql_log_fatal, vha, 0x008f,
7147                                     "Failed to load segment %d of firmware.\n",
7148                                     fragment);
7149                                 return QLA_FUNCTION_FAILED;
7150                         }
7151
7152                         faddr += dlen;
7153                         risc_addr += dlen;
7154                         risc_size -= dlen;
7155                         fragment++;
7156                 }
7157
7158                 /* Next segment. */
7159                 segments--;
7160         }
7161
7162         if (!IS_QLA27XX(ha))
7163                 return rval;
7164
7165         if (ha->fw_dump_template)
7166                 vfree(ha->fw_dump_template);
7167         ha->fw_dump_template = NULL;
7168         ha->fw_dump_template_len = 0;
7169
7170         ql_dbg(ql_dbg_init, vha, 0x0161,
7171             "Loading fwdump template from %x\n", faddr);
7172         qla24xx_read_flash_data(vha, dcode, faddr, 7);
7173         risc_size = be32_to_cpu(dcode[2]);
7174         ql_dbg(ql_dbg_init, vha, 0x0162,
7175             "-> array size %x dwords\n", risc_size);
7176         if (risc_size == 0 || risc_size == ~0)
7177                 goto default_template;
7178
7179         dlen = (risc_size - 8) * sizeof(*dcode);
7180         ql_dbg(ql_dbg_init, vha, 0x0163,
7181             "-> template allocating %x bytes...\n", dlen);
7182         ha->fw_dump_template = vmalloc(dlen);
7183         if (!ha->fw_dump_template) {
7184                 ql_log(ql_log_warn, vha, 0x0164,
7185                     "Failed fwdump template allocate %x bytes.\n", risc_size);
7186                 goto default_template;
7187         }
7188
7189         faddr += 7;
7190         risc_size -= 8;
7191         dcode = ha->fw_dump_template;
7192         qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7193         for (i = 0; i < risc_size; i++)
7194                 dcode[i] = le32_to_cpu(dcode[i]);
7195
7196         if (!qla27xx_fwdt_template_valid(dcode)) {
7197                 ql_log(ql_log_warn, vha, 0x0165,
7198                     "Failed fwdump template validate\n");
7199                 goto default_template;
7200         }
7201
7202         dlen = qla27xx_fwdt_template_size(dcode);
7203         ql_dbg(ql_dbg_init, vha, 0x0166,
7204             "-> template size %x bytes\n", dlen);
7205         if (dlen > risc_size * sizeof(*dcode)) {
7206                 ql_log(ql_log_warn, vha, 0x0167,
7207                     "Failed fwdump template exceeds array by %zx bytes\n",
7208                     (size_t)(dlen - risc_size * sizeof(*dcode)));
7209                 goto default_template;
7210         }
7211         ha->fw_dump_template_len = dlen;
7212         return rval;
7213
7214 default_template:
7215         ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7216         if (ha->fw_dump_template)
7217                 vfree(ha->fw_dump_template);
7218         ha->fw_dump_template = NULL;
7219         ha->fw_dump_template_len = 0;
7220
7221         dlen = qla27xx_fwdt_template_default_size();
7222         ql_dbg(ql_dbg_init, vha, 0x0169,
7223             "-> template allocating %x bytes...\n", dlen);
7224         ha->fw_dump_template = vmalloc(dlen);
7225         if (!ha->fw_dump_template) {
7226                 ql_log(ql_log_warn, vha, 0x016a,
7227                     "Failed fwdump template allocate %x bytes.\n", risc_size);
7228                 goto failed_template;
7229         }
7230
7231         dcode = ha->fw_dump_template;
7232         risc_size = dlen / sizeof(*dcode);
7233         memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7234         for (i = 0; i < risc_size; i++)
7235                 dcode[i] = be32_to_cpu(dcode[i]);
7236
7237         if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7238                 ql_log(ql_log_warn, vha, 0x016b,
7239                     "Failed fwdump template validate\n");
7240                 goto failed_template;
7241         }
7242
7243         dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7244         ql_dbg(ql_dbg_init, vha, 0x016c,
7245             "-> template size %x bytes\n", dlen);
7246         ha->fw_dump_template_len = dlen;
7247         return rval;
7248
7249 failed_template:
7250         ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7251         if (ha->fw_dump_template)
7252                 vfree(ha->fw_dump_template);
7253         ha->fw_dump_template = NULL;
7254         ha->fw_dump_template_len = 0;
7255         return rval;
7256 }
7257
7258 #define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
7259
7260 int
7261 qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7262 {
7263         int     rval;
7264         int     i, fragment;
7265         uint16_t *wcode, *fwcode;
7266         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7267         struct fw_blob *blob;
7268         struct qla_hw_data *ha = vha->hw;
7269         struct req_que *req = ha->req_q_map[0];
7270
7271         /* Load firmware blob. */
7272         blob = qla2x00_request_firmware(vha);
7273         if (!blob) {
7274                 ql_log(ql_log_info, vha, 0x0083,
7275                     "Firmware image unavailable.\n");
7276                 ql_log(ql_log_info, vha, 0x0084,
7277                     "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
7278                 return QLA_FUNCTION_FAILED;
7279         }
7280
7281         rval = QLA_SUCCESS;
7282
7283         wcode = (uint16_t *)req->ring;
7284         *srisc_addr = 0;
7285         fwcode = (uint16_t *)blob->fw->data;
7286         fwclen = 0;
7287
7288         /* Validate firmware image by checking version. */
7289         if (blob->fw->size < 8 * sizeof(uint16_t)) {
7290                 ql_log(ql_log_fatal, vha, 0x0085,
7291                     "Unable to verify integrity of firmware image (%zd).\n",
7292                     blob->fw->size);
7293                 goto fail_fw_integrity;
7294         }
7295         for (i = 0; i < 4; i++)
7296                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7297         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7298             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7299                 wcode[2] == 0 && wcode[3] == 0)) {
7300                 ql_log(ql_log_fatal, vha, 0x0086,
7301                     "Unable to verify integrity of firmware image.\n");
7302                 ql_log(ql_log_fatal, vha, 0x0087,
7303                     "Firmware data: %04x %04x %04x %04x.\n",
7304                     wcode[0], wcode[1], wcode[2], wcode[3]);
7305                 goto fail_fw_integrity;
7306         }
7307
7308         seg = blob->segs;
7309         while (*seg && rval == QLA_SUCCESS) {
7310                 risc_addr = *seg;
7311                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7312                 risc_size = be16_to_cpu(fwcode[3]);
7313
7314                 /* Validate firmware image size. */
7315                 fwclen += risc_size * sizeof(uint16_t);
7316                 if (blob->fw->size < fwclen) {
7317                         ql_log(ql_log_fatal, vha, 0x0088,
7318                             "Unable to verify integrity of firmware image "
7319                             "(%zd).\n", blob->fw->size);
7320                         goto fail_fw_integrity;
7321                 }
7322
7323                 fragment = 0;
7324                 while (risc_size > 0 && rval == QLA_SUCCESS) {
7325                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7326                         if (wlen > risc_size)
7327                                 wlen = risc_size;
7328                         ql_dbg(ql_dbg_init, vha, 0x0089,
7329                             "Loading risc segment@ risc addr %x number of "
7330                             "words 0x%x.\n", risc_addr, wlen);
7331
7332                         for (i = 0; i < wlen; i++)
7333                                 wcode[i] = swab16(fwcode[i]);
7334
7335                         rval = qla2x00_load_ram(vha, req->dma, risc_addr,
7336                             wlen);
7337                         if (rval) {
7338                                 ql_log(ql_log_fatal, vha, 0x008a,
7339                                     "Failed to load segment %d of firmware.\n",
7340                                     fragment);
7341                                 break;
7342                         }
7343
7344                         fwcode += wlen;
7345                         risc_addr += wlen;
7346                         risc_size -= wlen;
7347                         fragment++;
7348                 }
7349
7350                 /* Next segment. */
7351                 seg++;
7352         }
7353         return rval;
7354
7355 fail_fw_integrity:
7356         return QLA_FUNCTION_FAILED;
7357 }
7358
7359 static int
7360 qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7361 {
7362         int     rval;
7363         int     segments, fragment;
7364         uint32_t *dcode, dlen;
7365         uint32_t risc_addr;
7366         uint32_t risc_size;
7367         uint32_t i;
7368         struct fw_blob *blob;
7369         const uint32_t *fwcode;
7370         uint32_t fwclen;
7371         struct qla_hw_data *ha = vha->hw;
7372         struct req_que *req = ha->req_q_map[0];
7373
7374         /* Load firmware blob. */
7375         blob = qla2x00_request_firmware(vha);
7376         if (!blob) {
7377                 ql_log(ql_log_warn, vha, 0x0090,
7378                     "Firmware image unavailable.\n");
7379                 ql_log(ql_log_warn, vha, 0x0091,
7380                     "Firmware images can be retrieved from: "
7381                     QLA_FW_URL ".\n");
7382
7383                 return QLA_FUNCTION_FAILED;
7384         }
7385
7386         ql_dbg(ql_dbg_init, vha, 0x0092,
7387             "FW: Loading via request-firmware.\n");
7388
7389         rval = QLA_SUCCESS;
7390
7391         segments = FA_RISC_CODE_SEGMENTS;
7392         dcode = (uint32_t *)req->ring;
7393         *srisc_addr = 0;
7394         fwcode = (uint32_t *)blob->fw->data;
7395         fwclen = 0;
7396
7397         /* Validate firmware image by checking version. */
7398         if (blob->fw->size < 8 * sizeof(uint32_t)) {
7399                 ql_log(ql_log_fatal, vha, 0x0093,
7400                     "Unable to verify integrity of firmware image (%zd).\n",
7401                     blob->fw->size);
7402                 return QLA_FUNCTION_FAILED;
7403         }
7404         for (i = 0; i < 4; i++)
7405                 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7406         if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7407             dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7408             (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7409                 dcode[3] == 0)) {
7410                 ql_log(ql_log_fatal, vha, 0x0094,
7411                     "Unable to verify integrity of firmware image (%zd).\n",
7412                     blob->fw->size);
7413                 ql_log(ql_log_fatal, vha, 0x0095,
7414                     "Firmware data: %08x %08x %08x %08x.\n",
7415                     dcode[0], dcode[1], dcode[2], dcode[3]);
7416                 return QLA_FUNCTION_FAILED;
7417         }
7418
7419         while (segments && rval == QLA_SUCCESS) {
7420                 risc_addr = be32_to_cpu(fwcode[2]);
7421                 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7422                 risc_size = be32_to_cpu(fwcode[3]);
7423
7424                 /* Validate firmware image size. */
7425                 fwclen += risc_size * sizeof(uint32_t);
7426                 if (blob->fw->size < fwclen) {
7427                         ql_log(ql_log_fatal, vha, 0x0096,
7428                             "Unable to verify integrity of firmware image "
7429                             "(%zd).\n", blob->fw->size);
7430                         return QLA_FUNCTION_FAILED;
7431                 }
7432
7433                 fragment = 0;
7434                 while (risc_size > 0 && rval == QLA_SUCCESS) {
7435                         dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7436                         if (dlen > risc_size)
7437                                 dlen = risc_size;
7438
7439                         ql_dbg(ql_dbg_init, vha, 0x0097,
7440                             "Loading risc segment@ risc addr %x "
7441                             "number of dwords 0x%x.\n", risc_addr, dlen);
7442
7443                         for (i = 0; i < dlen; i++)
7444                                 dcode[i] = swab32(fwcode[i]);
7445
7446                         rval = qla2x00_load_ram(vha, req->dma, risc_addr,
7447                             dlen);
7448                         if (rval) {
7449                                 ql_log(ql_log_fatal, vha, 0x0098,
7450                                     "Failed to load segment %d of firmware.\n",
7451                                     fragment);
7452                                 return QLA_FUNCTION_FAILED;
7453                         }
7454
7455                         fwcode += dlen;
7456                         risc_addr += dlen;
7457                         risc_size -= dlen;
7458                         fragment++;
7459                 }
7460
7461                 /* Next segment. */
7462                 segments--;
7463         }
7464
7465         if (!IS_QLA27XX(ha))
7466                 return rval;
7467
7468         if (ha->fw_dump_template)
7469                 vfree(ha->fw_dump_template);
7470         ha->fw_dump_template = NULL;
7471         ha->fw_dump_template_len = 0;
7472
7473         ql_dbg(ql_dbg_init, vha, 0x171,
7474             "Loading fwdump template from %x\n",
7475             (uint32_t)((void *)fwcode - (void *)blob->fw->data));
7476         risc_size = be32_to_cpu(fwcode[2]);
7477         ql_dbg(ql_dbg_init, vha, 0x172,
7478             "-> array size %x dwords\n", risc_size);
7479         if (risc_size == 0 || risc_size == ~0)
7480                 goto default_template;
7481
7482         dlen = (risc_size - 8) * sizeof(*fwcode);
7483         ql_dbg(ql_dbg_init, vha, 0x0173,
7484             "-> template allocating %x bytes...\n", dlen);
7485         ha->fw_dump_template = vmalloc(dlen);
7486         if (!ha->fw_dump_template) {
7487                 ql_log(ql_log_warn, vha, 0x0174,
7488                     "Failed fwdump template allocate %x bytes.\n", risc_size);
7489                 goto default_template;
7490         }
7491
7492         fwcode += 7;
7493         risc_size -= 8;
7494         dcode = ha->fw_dump_template;
7495         for (i = 0; i < risc_size; i++)
7496                 dcode[i] = le32_to_cpu(fwcode[i]);
7497
7498         if (!qla27xx_fwdt_template_valid(dcode)) {
7499                 ql_log(ql_log_warn, vha, 0x0175,
7500                     "Failed fwdump template validate\n");
7501                 goto default_template;
7502         }
7503
7504         dlen = qla27xx_fwdt_template_size(dcode);
7505         ql_dbg(ql_dbg_init, vha, 0x0176,
7506             "-> template size %x bytes\n", dlen);
7507         if (dlen > risc_size * sizeof(*fwcode)) {
7508                 ql_log(ql_log_warn, vha, 0x0177,
7509                     "Failed fwdump template exceeds array by %zx bytes\n",
7510                     (size_t)(dlen - risc_size * sizeof(*fwcode)));
7511                 goto default_template;
7512         }
7513         ha->fw_dump_template_len = dlen;
7514         return rval;
7515
7516 default_template:
7517         ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7518         if (ha->fw_dump_template)
7519                 vfree(ha->fw_dump_template);
7520         ha->fw_dump_template = NULL;
7521         ha->fw_dump_template_len = 0;
7522
7523         dlen = qla27xx_fwdt_template_default_size();
7524         ql_dbg(ql_dbg_init, vha, 0x0179,
7525             "-> template allocating %x bytes...\n", dlen);
7526         ha->fw_dump_template = vmalloc(dlen);
7527         if (!ha->fw_dump_template) {
7528                 ql_log(ql_log_warn, vha, 0x017a,
7529                     "Failed fwdump template allocate %x bytes.\n", risc_size);
7530                 goto failed_template;
7531         }
7532
7533         dcode = ha->fw_dump_template;
7534         risc_size = dlen / sizeof(*fwcode);
7535         fwcode = qla27xx_fwdt_template_default();
7536         for (i = 0; i < risc_size; i++)
7537                 dcode[i] = be32_to_cpu(fwcode[i]);
7538
7539         if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7540                 ql_log(ql_log_warn, vha, 0x017b,
7541                     "Failed fwdump template validate\n");
7542                 goto failed_template;
7543         }
7544
7545         dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7546         ql_dbg(ql_dbg_init, vha, 0x017c,
7547             "-> template size %x bytes\n", dlen);
7548         ha->fw_dump_template_len = dlen;
7549         return rval;
7550
7551 failed_template:
7552         ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7553         if (ha->fw_dump_template)
7554                 vfree(ha->fw_dump_template);
7555         ha->fw_dump_template = NULL;
7556         ha->fw_dump_template_len = 0;
7557         return rval;
7558 }
7559
7560 int
7561 qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7562 {
7563         int rval;
7564
7565         if (ql2xfwloadbin == 1)
7566                 return qla81xx_load_risc(vha, srisc_addr);
7567
7568         /*
7569          * FW Load priority:
7570          * 1) Firmware via request-firmware interface (.bin file).
7571          * 2) Firmware residing in flash.
7572          */
7573         rval = qla24xx_load_risc_blob(vha, srisc_addr);
7574         if (rval == QLA_SUCCESS)
7575                 return rval;
7576
7577         return qla24xx_load_risc_flash(vha, srisc_addr,
7578             vha->hw->flt_region_fw);
7579 }
7580
7581 int
7582 qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7583 {
7584         int rval;
7585         struct qla_hw_data *ha = vha->hw;
7586
7587         if (ql2xfwloadbin == 2)
7588                 goto try_blob_fw;
7589
7590         /*
7591          * FW Load priority:
7592          * 1) Firmware residing in flash.
7593          * 2) Firmware via request-firmware interface (.bin file).
7594          * 3) Golden-Firmware residing in flash -- limited operation.
7595          */
7596         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
7597         if (rval == QLA_SUCCESS)
7598                 return rval;
7599
7600 try_blob_fw:
7601         rval = qla24xx_load_risc_blob(vha, srisc_addr);
7602         if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7603                 return rval;
7604
7605         ql_log(ql_log_info, vha, 0x0099,
7606             "Attempting to fallback to golden firmware.\n");
7607         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7608         if (rval != QLA_SUCCESS)
7609                 return rval;
7610
7611         ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
7612         ha->flags.running_gold_fw = 1;
7613         return rval;
7614 }
7615
7616 void
7617 qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
7618 {
7619         int ret, retries;
7620         struct qla_hw_data *ha = vha->hw;
7621
7622         if (ha->flags.pci_channel_io_perm_failure)
7623                 return;
7624         if (!IS_FWI2_CAPABLE(ha))
7625                 return;
7626         if (!ha->fw_major_version)
7627                 return;
7628         if (!ha->flags.fw_started)
7629                 return;
7630
7631         ret = qla2x00_stop_firmware(vha);
7632         for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
7633             ret != QLA_INVALID_COMMAND && retries ; retries--) {
7634                 ha->isp_ops->reset_chip(vha);
7635                 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
7636                         continue;
7637                 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
7638                         continue;
7639                 ql_log(ql_log_info, vha, 0x8015,
7640                     "Attempting retry of stop-firmware command.\n");
7641                 ret = qla2x00_stop_firmware(vha);
7642         }
7643
7644         QLA_FW_STOPPED(ha);
7645         ha->flags.fw_init_done = 0;
7646 }
7647
7648 int
7649 qla24xx_configure_vhba(scsi_qla_host_t *vha)
7650 {
7651         int rval = QLA_SUCCESS;
7652         int rval2;
7653         uint16_t mb[MAILBOX_REGISTER_COUNT];
7654         struct qla_hw_data *ha = vha->hw;
7655         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7656         struct req_que *req;
7657         struct rsp_que *rsp;
7658
7659         if (!vha->vp_idx)
7660                 return -EINVAL;
7661
7662         rval = qla2x00_fw_ready(base_vha);
7663         if (vha->qpair)
7664                 req = vha->qpair->req;
7665         else
7666                 req = ha->req_q_map[0];
7667         rsp = req->rsp;
7668
7669         if (rval == QLA_SUCCESS) {
7670                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7671                 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
7672         }
7673
7674         vha->flags.management_server_logged_in = 0;
7675
7676         /* Login to SNS first */
7677         rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7678             BIT_1);
7679         if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7680                 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7681                         ql_dbg(ql_dbg_init, vha, 0x0120,
7682                             "Failed SNS login: loop_id=%x, rval2=%d\n",
7683                             NPH_SNS, rval2);
7684                 else
7685                         ql_dbg(ql_dbg_init, vha, 0x0103,
7686                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7687                             "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7688                             NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
7689                 return (QLA_FUNCTION_FAILED);
7690         }
7691
7692         atomic_set(&vha->loop_down_timer, 0);
7693         atomic_set(&vha->loop_state, LOOP_UP);
7694         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7695         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7696         rval = qla2x00_loop_resync(base_vha);
7697
7698         return rval;
7699 }
7700
7701 /* 84XX Support **************************************************************/
7702
7703 static LIST_HEAD(qla_cs84xx_list);
7704 static DEFINE_MUTEX(qla_cs84xx_mutex);
7705
7706 static struct qla_chip_state_84xx *
7707 qla84xx_get_chip(struct scsi_qla_host *vha)
7708 {
7709         struct qla_chip_state_84xx *cs84xx;
7710         struct qla_hw_data *ha = vha->hw;
7711
7712         mutex_lock(&qla_cs84xx_mutex);
7713
7714         /* Find any shared 84xx chip. */
7715         list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7716                 if (cs84xx->bus == ha->pdev->bus) {
7717                         kref_get(&cs84xx->kref);
7718                         goto done;
7719                 }
7720         }
7721
7722         cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7723         if (!cs84xx)
7724                 goto done;
7725
7726         kref_init(&cs84xx->kref);
7727         spin_lock_init(&cs84xx->access_lock);
7728         mutex_init(&cs84xx->fw_update_mutex);
7729         cs84xx->bus = ha->pdev->bus;
7730
7731         list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7732 done:
7733         mutex_unlock(&qla_cs84xx_mutex);
7734         return cs84xx;
7735 }
7736
7737 static void
7738 __qla84xx_chip_release(struct kref *kref)
7739 {
7740         struct qla_chip_state_84xx *cs84xx =
7741             container_of(kref, struct qla_chip_state_84xx, kref);
7742
7743         mutex_lock(&qla_cs84xx_mutex);
7744         list_del(&cs84xx->list);
7745         mutex_unlock(&qla_cs84xx_mutex);
7746         kfree(cs84xx);
7747 }
7748
7749 void
7750 qla84xx_put_chip(struct scsi_qla_host *vha)
7751 {
7752         struct qla_hw_data *ha = vha->hw;
7753         if (ha->cs84xx)
7754                 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7755 }
7756
7757 static int
7758 qla84xx_init_chip(scsi_qla_host_t *vha)
7759 {
7760         int rval;
7761         uint16_t status[2];
7762         struct qla_hw_data *ha = vha->hw;
7763
7764         mutex_lock(&ha->cs84xx->fw_update_mutex);
7765
7766         rval = qla84xx_verify_chip(vha, status);
7767
7768         mutex_unlock(&ha->cs84xx->fw_update_mutex);
7769
7770         return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7771             QLA_SUCCESS;
7772 }
7773
7774 /* 81XX Support **************************************************************/
7775
7776 int
7777 qla81xx_nvram_config(scsi_qla_host_t *vha)
7778 {
7779         int   rval;
7780         struct init_cb_81xx *icb;
7781         struct nvram_81xx *nv;
7782         uint32_t *dptr;
7783         uint8_t  *dptr1, *dptr2;
7784         uint32_t chksum;
7785         uint16_t cnt;
7786         struct qla_hw_data *ha = vha->hw;
7787
7788         rval = QLA_SUCCESS;
7789         icb = (struct init_cb_81xx *)ha->init_cb;
7790         nv = ha->nvram;
7791
7792         /* Determine NVRAM starting address. */
7793         ha->nvram_size = sizeof(struct nvram_81xx);
7794         ha->vpd_size = FA_NVRAM_VPD_SIZE;
7795         if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7796                 ha->vpd_size = FA_VPD_SIZE_82XX;
7797
7798         /* Get VPD data into cache */
7799         ha->vpd = ha->nvram + VPD_OFFSET;
7800         ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7801             ha->vpd_size);
7802
7803         /* Get NVRAM data into cache and calculate checksum. */
7804         ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
7805             ha->nvram_size);
7806         dptr = (uint32_t *)nv;
7807         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7808                 chksum += le32_to_cpu(*dptr);
7809
7810         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7811             "Contents of NVRAM:\n");
7812         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7813             (uint8_t *)nv, ha->nvram_size);
7814
7815         /* Bad NVRAM data, set defaults parameters. */
7816         if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7817             || nv->id[3] != ' ' ||
7818             nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
7819                 /* Reset NVRAM data. */
7820                 ql_log(ql_log_info, vha, 0x0073,
7821                     "Inconsistent NVRAM detected: checksum=0x%x id=%c "
7822                     "version=0x%x.\n", chksum, nv->id[0],
7823                     le16_to_cpu(nv->nvram_version));
7824                 ql_log(ql_log_info, vha, 0x0074,
7825                     "Falling back to functioning (yet invalid -- WWPN) "
7826                     "defaults.\n");
7827
7828                 /*
7829                  * Set default initialization control block.
7830                  */
7831                 memset(nv, 0, ha->nvram_size);
7832                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7833                 nv->version = cpu_to_le16(ICB_VERSION);
7834                 nv->frame_payload_size = 2048;
7835                 nv->execution_throttle = cpu_to_le16(0xFFFF);
7836                 nv->exchange_count = cpu_to_le16(0);
7837                 nv->port_name[0] = 0x21;
7838                 nv->port_name[1] = 0x00 + ha->port_no + 1;
7839                 nv->port_name[2] = 0x00;
7840                 nv->port_name[3] = 0xe0;
7841                 nv->port_name[4] = 0x8b;
7842                 nv->port_name[5] = 0x1c;
7843                 nv->port_name[6] = 0x55;
7844                 nv->port_name[7] = 0x86;
7845                 nv->node_name[0] = 0x20;
7846                 nv->node_name[1] = 0x00;
7847                 nv->node_name[2] = 0x00;
7848                 nv->node_name[3] = 0xe0;
7849                 nv->node_name[4] = 0x8b;
7850                 nv->node_name[5] = 0x1c;
7851                 nv->node_name[6] = 0x55;
7852                 nv->node_name[7] = 0x86;
7853                 nv->login_retry_count = cpu_to_le16(8);
7854                 nv->interrupt_delay_timer = cpu_to_le16(0);
7855                 nv->login_timeout = cpu_to_le16(0);
7856                 nv->firmware_options_1 =
7857                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7858                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7859                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7860                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7861                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7862                 nv->efi_parameters = cpu_to_le32(0);
7863                 nv->reset_delay = 5;
7864                 nv->max_luns_per_target = cpu_to_le16(128);
7865                 nv->port_down_retry_count = cpu_to_le16(30);
7866                 nv->link_down_timeout = cpu_to_le16(180);
7867                 nv->enode_mac[0] = 0x00;
7868                 nv->enode_mac[1] = 0xC0;
7869                 nv->enode_mac[2] = 0xDD;
7870                 nv->enode_mac[3] = 0x04;
7871                 nv->enode_mac[4] = 0x05;
7872                 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
7873
7874                 rval = 1;
7875         }
7876
7877         if (IS_T10_PI_CAPABLE(ha))
7878                 nv->frame_payload_size &= ~7;
7879
7880         qlt_81xx_config_nvram_stage1(vha, nv);
7881
7882         /* Reset Initialization control block */
7883         memset(icb, 0, ha->init_cb_size);
7884
7885         /* Copy 1st segment. */
7886         dptr1 = (uint8_t *)icb;
7887         dptr2 = (uint8_t *)&nv->version;
7888         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7889         while (cnt--)
7890                 *dptr1++ = *dptr2++;
7891
7892         icb->login_retry_count = nv->login_retry_count;
7893
7894         /* Copy 2nd segment. */
7895         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7896         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7897         cnt = (uint8_t *)&icb->reserved_5 -
7898             (uint8_t *)&icb->interrupt_delay_timer;
7899         while (cnt--)
7900                 *dptr1++ = *dptr2++;
7901
7902         memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7903         /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7904         if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
7905                 icb->enode_mac[0] = 0x00;
7906                 icb->enode_mac[1] = 0xC0;
7907                 icb->enode_mac[2] = 0xDD;
7908                 icb->enode_mac[3] = 0x04;
7909                 icb->enode_mac[4] = 0x05;
7910                 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
7911         }
7912
7913         /* Use extended-initialization control block. */
7914         memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7915
7916         /*
7917          * Setup driver NVRAM options.
7918          */
7919         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
7920             "QLE8XXX");
7921
7922         qlt_81xx_config_nvram_stage2(vha, icb);
7923
7924         /* Use alternate WWN? */
7925         if (nv->host_p & cpu_to_le32(BIT_15)) {
7926                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7927                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7928         }
7929
7930         /* Prepare nodename */
7931         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
7932                 /*
7933                  * Firmware will apply the following mask if the nodename was
7934                  * not provided.
7935                  */
7936                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7937                 icb->node_name[0] &= 0xF0;
7938         }
7939
7940         /* Set host adapter parameters. */
7941         ha->flags.disable_risc_code_load = 0;
7942         ha->flags.enable_lip_reset = 0;
7943         ha->flags.enable_lip_full_login =
7944             le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7945         ha->flags.enable_target_reset =
7946             le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7947         ha->flags.enable_led_scheme = 0;
7948         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7949
7950         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7951             (BIT_6 | BIT_5 | BIT_4)) >> 4;
7952
7953         /* save HBA serial number */
7954         ha->serial0 = icb->port_name[5];
7955         ha->serial1 = icb->port_name[6];
7956         ha->serial2 = icb->port_name[7];
7957         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7958         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7959
7960         icb->execution_throttle = cpu_to_le16(0xFFFF);
7961
7962         ha->retry_count = le16_to_cpu(nv->login_retry_count);
7963
7964         /* Set minimum login_timeout to 4 seconds. */
7965         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7966                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7967         if (le16_to_cpu(nv->login_timeout) < 4)
7968                 nv->login_timeout = cpu_to_le16(4);
7969         ha->login_timeout = le16_to_cpu(nv->login_timeout);
7970
7971         /* Set minimum RATOV to 100 tenths of a second. */
7972         ha->r_a_tov = 100;
7973
7974         ha->loop_reset_delay = nv->reset_delay;
7975
7976         /* Link Down Timeout = 0:
7977          *
7978          *      When Port Down timer expires we will start returning
7979          *      I/O's to OS with "DID_NO_CONNECT".
7980          *
7981          * Link Down Timeout != 0:
7982          *
7983          *       The driver waits for the link to come up after link down
7984          *       before returning I/Os to OS with "DID_NO_CONNECT".
7985          */
7986         if (le16_to_cpu(nv->link_down_timeout) == 0) {
7987                 ha->loop_down_abort_time =
7988                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7989         } else {
7990                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7991                 ha->loop_down_abort_time =
7992                     (LOOP_DOWN_TIME - ha->link_down_timeout);
7993         }
7994
7995         /* Need enough time to try and get the port back. */
7996         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7997         if (qlport_down_retry)
7998                 ha->port_down_retry_count = qlport_down_retry;
7999
8000         /* Set login_retry_count */
8001         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
8002         if (ha->port_down_retry_count ==
8003             le16_to_cpu(nv->port_down_retry_count) &&
8004             ha->port_down_retry_count > 3)
8005                 ha->login_retry_count = ha->port_down_retry_count;
8006         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
8007                 ha->login_retry_count = ha->port_down_retry_count;
8008         if (ql2xloginretrycount)
8009                 ha->login_retry_count = ql2xloginretrycount;
8010
8011         /* if not running MSI-X we need handshaking on interrupts */
8012         if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
8013                 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
8014
8015         /* Enable ZIO. */
8016         if (!vha->flags.init_done) {
8017                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8018                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8019                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8020                     le16_to_cpu(icb->interrupt_delay_timer): 2;
8021         }
8022         icb->firmware_options_2 &= cpu_to_le32(
8023             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8024         vha->flags.process_response_queue = 0;
8025         if (ha->zio_mode != QLA_ZIO_DISABLED) {
8026                 ha->zio_mode = QLA_ZIO_MODE_6;
8027
8028                 ql_log(ql_log_info, vha, 0x0075,
8029                     "ZIO mode %d enabled; timer delay (%d us).\n",
8030                     ha->zio_mode,
8031                     ha->zio_timer * 100);
8032
8033                 icb->firmware_options_2 |= cpu_to_le32(
8034                     (uint32_t)ha->zio_mode);
8035                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8036                 vha->flags.process_response_queue = 1;
8037         }
8038
8039          /* enable RIDA Format2 */
8040         icb->firmware_options_3 |= BIT_0;
8041
8042         if (IS_QLA27XX(ha)) {
8043                 icb->firmware_options_3 |= BIT_8;
8044                 ql_dbg(ql_log_info, vha, 0x0075,
8045                     "Enabling direct connection.\n");
8046         }
8047
8048         if (rval) {
8049                 ql_log(ql_log_warn, vha, 0x0076,
8050                     "NVRAM configuration failed.\n");
8051         }
8052         return (rval);
8053 }
8054
8055 int
8056 qla82xx_restart_isp(scsi_qla_host_t *vha)
8057 {
8058         int status, rval;
8059         struct qla_hw_data *ha = vha->hw;
8060         struct req_que *req = ha->req_q_map[0];
8061         struct rsp_que *rsp = ha->rsp_q_map[0];
8062         struct scsi_qla_host *vp;
8063         unsigned long flags;
8064
8065         status = qla2x00_init_rings(vha);
8066         if (!status) {
8067                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8068                 ha->flags.chip_reset_done = 1;
8069
8070                 status = qla2x00_fw_ready(vha);
8071                 if (!status) {
8072                         /* Issue a marker after FW becomes ready. */
8073                         qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
8074                         vha->flags.online = 1;
8075                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8076                 }
8077
8078                 /* if no cable then assume it's good */
8079                 if ((vha->device_flags & DFLG_NO_CABLE))
8080                         status = 0;
8081         }
8082
8083         if (!status) {
8084                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8085
8086                 if (!atomic_read(&vha->loop_down_timer)) {
8087                         /*
8088                          * Issue marker command only when we are going
8089                          * to start the I/O .
8090                          */
8091                         vha->marker_needed = 1;
8092                 }
8093
8094                 ha->isp_ops->enable_intrs(ha);
8095
8096                 ha->isp_abort_cnt = 0;
8097                 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8098
8099                 /* Update the firmware version */
8100                 status = qla82xx_check_md_needed(vha);
8101
8102                 if (ha->fce) {
8103                         ha->flags.fce_enabled = 1;
8104                         memset(ha->fce, 0,
8105                             fce_calc_size(ha->fce_bufs));
8106                         rval = qla2x00_enable_fce_trace(vha,
8107                             ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8108                             &ha->fce_bufs);
8109                         if (rval) {
8110                                 ql_log(ql_log_warn, vha, 0x8001,
8111                                     "Unable to reinitialize FCE (%d).\n",
8112                                     rval);
8113                                 ha->flags.fce_enabled = 0;
8114                         }
8115                 }
8116
8117                 if (ha->eft) {
8118                         memset(ha->eft, 0, EFT_SIZE);
8119                         rval = qla2x00_enable_eft_trace(vha,
8120                             ha->eft_dma, EFT_NUM_BUFFERS);
8121                         if (rval) {
8122                                 ql_log(ql_log_warn, vha, 0x8010,
8123                                     "Unable to reinitialize EFT (%d).\n",
8124                                     rval);
8125                         }
8126                 }
8127         }
8128
8129         if (!status) {
8130                 ql_dbg(ql_dbg_taskm, vha, 0x8011,
8131                     "qla82xx_restart_isp succeeded.\n");
8132
8133                 spin_lock_irqsave(&ha->vport_slock, flags);
8134                 list_for_each_entry(vp, &ha->vp_list, list) {
8135                         if (vp->vp_idx) {
8136                                 atomic_inc(&vp->vref_count);
8137                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
8138
8139                                 qla2x00_vp_abort_isp(vp);
8140
8141                                 spin_lock_irqsave(&ha->vport_slock, flags);
8142                                 atomic_dec(&vp->vref_count);
8143                         }
8144                 }
8145                 spin_unlock_irqrestore(&ha->vport_slock, flags);
8146
8147         } else {
8148                 ql_log(ql_log_warn, vha, 0x8016,
8149                     "qla82xx_restart_isp **** FAILED ****.\n");
8150         }
8151
8152         return status;
8153 }
8154
8155 void
8156 qla81xx_update_fw_options(scsi_qla_host_t *vha)
8157 {
8158         struct qla_hw_data *ha = vha->hw;
8159
8160         /*  Hold status IOCBs until ABTS response received. */
8161         if (ql2xfwholdabts)
8162                 ha->fw_options[3] |= BIT_12;
8163
8164         /* Set Retry FLOGI in case of P2P connection */
8165         if (ha->operating_mode == P2P) {
8166                 ha->fw_options[2] |= BIT_3;
8167                 ql_dbg(ql_dbg_disc, vha, 0x2103,
8168                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8169                         __func__, ha->fw_options[2]);
8170         }
8171
8172         /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8173         if (ql2xmvasynctoatio) {
8174                 if (qla_tgt_mode_enabled(vha) ||
8175                     qla_dual_mode_enabled(vha))
8176                         ha->fw_options[2] |= BIT_11;
8177                 else
8178                         ha->fw_options[2] &= ~BIT_11;
8179         }
8180
8181         if (qla_tgt_mode_enabled(vha) ||
8182             qla_dual_mode_enabled(vha)) {
8183                 /* FW auto send SCSI status during */
8184                 ha->fw_options[1] |= BIT_8;
8185                 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8186
8187                 /* FW perform Exchange validation */
8188                 ha->fw_options[2] |= BIT_4;
8189         } else {
8190                 ha->fw_options[1]  &= ~BIT_8;
8191                 ha->fw_options[10] &= 0x00ff;
8192
8193                 ha->fw_options[2] &= ~BIT_4;
8194         }
8195
8196         if (ql2xetsenable) {
8197                 /* Enable ETS Burst. */
8198                 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8199                 ha->fw_options[2] |= BIT_9;
8200         }
8201
8202         ql_dbg(ql_dbg_init, vha, 0x00e9,
8203             "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8204             __func__, ha->fw_options[1], ha->fw_options[2],
8205             ha->fw_options[3], vha->host->active_mode);
8206
8207         qla2x00_set_fw_options(vha, ha->fw_options);
8208 }
8209
8210 /*
8211  * qla24xx_get_fcp_prio
8212  *      Gets the fcp cmd priority value for the logged in port.
8213  *      Looks for a match of the port descriptors within
8214  *      each of the fcp prio config entries. If a match is found,
8215  *      the tag (priority) value is returned.
8216  *
8217  * Input:
8218  *      vha = scsi host structure pointer.
8219  *      fcport = port structure pointer.
8220  *
8221  * Return:
8222  *      non-zero (if found)
8223  *      -1 (if not found)
8224  *
8225  * Context:
8226  *      Kernel context
8227  */
8228 static int
8229 qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8230 {
8231         int i, entries;
8232         uint8_t pid_match, wwn_match;
8233         int priority;
8234         uint32_t pid1, pid2;
8235         uint64_t wwn1, wwn2;
8236         struct qla_fcp_prio_entry *pri_entry;
8237         struct qla_hw_data *ha = vha->hw;
8238
8239         if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
8240                 return -1;
8241
8242         priority = -1;
8243         entries = ha->fcp_prio_cfg->num_entries;
8244         pri_entry = &ha->fcp_prio_cfg->entry[0];
8245
8246         for (i = 0; i < entries; i++) {
8247                 pid_match = wwn_match = 0;
8248
8249                 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8250                         pri_entry++;
8251                         continue;
8252                 }
8253
8254                 /* check source pid for a match */
8255                 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8256                         pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8257                         pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8258                         if (pid1 == INVALID_PORT_ID)
8259                                 pid_match++;
8260                         else if (pid1 == pid2)
8261                                 pid_match++;
8262                 }
8263
8264                 /* check destination pid for a match */
8265                 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8266                         pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8267                         pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8268                         if (pid1 == INVALID_PORT_ID)
8269                                 pid_match++;
8270                         else if (pid1 == pid2)
8271                                 pid_match++;
8272                 }
8273
8274                 /* check source WWN for a match */
8275                 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8276                         wwn1 = wwn_to_u64(vha->port_name);
8277                         wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8278                         if (wwn2 == (uint64_t)-1)
8279                                 wwn_match++;
8280                         else if (wwn1 == wwn2)
8281                                 wwn_match++;
8282                 }
8283
8284                 /* check destination WWN for a match */
8285                 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8286                         wwn1 = wwn_to_u64(fcport->port_name);
8287                         wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8288                         if (wwn2 == (uint64_t)-1)
8289                                 wwn_match++;
8290                         else if (wwn1 == wwn2)
8291                                 wwn_match++;
8292                 }
8293
8294                 if (pid_match == 2 || wwn_match == 2) {
8295                         /* Found a matching entry */
8296                         if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8297                                 priority = pri_entry->tag;
8298                         break;
8299                 }
8300
8301                 pri_entry++;
8302         }
8303
8304         return priority;
8305 }
8306
8307 /*
8308  * qla24xx_update_fcport_fcp_prio
8309  *      Activates fcp priority for the logged in fc port
8310  *
8311  * Input:
8312  *      vha = scsi host structure pointer.
8313  *      fcp = port structure pointer.
8314  *
8315  * Return:
8316  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
8317  *
8318  * Context:
8319  *      Kernel context.
8320  */
8321 int
8322 qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8323 {
8324         int ret;
8325         int priority;
8326         uint16_t mb[5];
8327
8328         if (fcport->port_type != FCT_TARGET ||
8329             fcport->loop_id == FC_NO_LOOP_ID)
8330                 return QLA_FUNCTION_FAILED;
8331
8332         priority = qla24xx_get_fcp_prio(vha, fcport);
8333         if (priority < 0)
8334                 return QLA_FUNCTION_FAILED;
8335
8336         if (IS_P3P_TYPE(vha->hw)) {
8337                 fcport->fcp_prio = priority & 0xf;
8338                 return QLA_SUCCESS;
8339         }
8340
8341         ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
8342         if (ret == QLA_SUCCESS) {
8343                 if (fcport->fcp_prio != priority)
8344                         ql_dbg(ql_dbg_user, vha, 0x709e,
8345                             "Updated FCP_CMND priority - value=%d loop_id=%d "
8346                             "port_id=%02x%02x%02x.\n", priority,
8347                             fcport->loop_id, fcport->d_id.b.domain,
8348                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
8349                 fcport->fcp_prio = priority & 0xf;
8350         } else
8351                 ql_dbg(ql_dbg_user, vha, 0x704f,
8352                     "Unable to update FCP_CMND priority - ret=0x%x for "
8353                     "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8354                     fcport->d_id.b.domain, fcport->d_id.b.area,
8355                     fcport->d_id.b.al_pa);
8356         return  ret;
8357 }
8358
8359 /*
8360  * qla24xx_update_all_fcp_prio
8361  *      Activates fcp priority for all the logged in ports
8362  *
8363  * Input:
8364  *      ha = adapter block pointer.
8365  *
8366  * Return:
8367  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
8368  *
8369  * Context:
8370  *      Kernel context.
8371  */
8372 int
8373 qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8374 {
8375         int ret;
8376         fc_port_t *fcport;
8377
8378         ret = QLA_FUNCTION_FAILED;
8379         /* We need to set priority for all logged in ports */
8380         list_for_each_entry(fcport, &vha->vp_fcports, list)
8381                 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8382
8383         return ret;
8384 }
8385
8386 struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8387         int vp_idx, bool startqp)
8388 {
8389         int rsp_id = 0;
8390         int  req_id = 0;
8391         int i;
8392         struct qla_hw_data *ha = vha->hw;
8393         uint16_t qpair_id = 0;
8394         struct qla_qpair *qpair = NULL;
8395         struct qla_msix_entry *msix;
8396
8397         if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8398                 ql_log(ql_log_warn, vha, 0x00181,
8399                     "FW/Driver is not multi-queue capable.\n");
8400                 return NULL;
8401         }
8402
8403         if (ql2xmqsupport || ql2xnvmeenable) {
8404                 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8405                 if (qpair == NULL) {
8406                         ql_log(ql_log_warn, vha, 0x0182,
8407                             "Failed to allocate memory for queue pair.\n");
8408                         return NULL;
8409                 }
8410                 memset(qpair, 0, sizeof(struct qla_qpair));
8411
8412                 qpair->hw = vha->hw;
8413                 qpair->vha = vha;
8414                 qpair->qp_lock_ptr = &qpair->qp_lock;
8415                 spin_lock_init(&qpair->qp_lock);
8416                 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
8417
8418                 /* Assign available que pair id */
8419                 mutex_lock(&ha->mq_lock);
8420                 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
8421                 if (ha->num_qpairs >= ha->max_qpairs) {
8422                         mutex_unlock(&ha->mq_lock);
8423                         ql_log(ql_log_warn, vha, 0x0183,
8424                             "No resources to create additional q pair.\n");
8425                         goto fail_qid_map;
8426                 }
8427                 ha->num_qpairs++;
8428                 set_bit(qpair_id, ha->qpair_qid_map);
8429                 ha->queue_pair_map[qpair_id] = qpair;
8430                 qpair->id = qpair_id;
8431                 qpair->vp_idx = vp_idx;
8432                 qpair->fw_started = ha->flags.fw_started;
8433                 INIT_LIST_HEAD(&qpair->hints_list);
8434                 qpair->chip_reset = ha->base_qpair->chip_reset;
8435                 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8436                 qpair->enable_explicit_conf =
8437                     ha->base_qpair->enable_explicit_conf;
8438
8439                 for (i = 0; i < ha->msix_count; i++) {
8440                         msix = &ha->msix_entries[i];
8441                         if (msix->in_use)
8442                                 continue;
8443                         qpair->msix = msix;
8444                         ql_dbg(ql_dbg_multiq, vha, 0xc00f,
8445                             "Vector %x selected for qpair\n", msix->vector);
8446                         break;
8447                 }
8448                 if (!qpair->msix) {
8449                         ql_log(ql_log_warn, vha, 0x0184,
8450                             "Out of MSI-X vectors!.\n");
8451                         goto fail_msix;
8452                 }
8453
8454                 qpair->msix->in_use = 1;
8455                 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
8456                 qpair->pdev = ha->pdev;
8457                 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8458                         qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
8459
8460                 mutex_unlock(&ha->mq_lock);
8461
8462                 /* Create response queue first */
8463                 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
8464                 if (!rsp_id) {
8465                         ql_log(ql_log_warn, vha, 0x0185,
8466                             "Failed to create response queue.\n");
8467                         goto fail_rsp;
8468                 }
8469
8470                 qpair->rsp = ha->rsp_q_map[rsp_id];
8471
8472                 /* Create request queue */
8473                 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8474                     startqp);
8475                 if (!req_id) {
8476                         ql_log(ql_log_warn, vha, 0x0186,
8477                             "Failed to create request queue.\n");
8478                         goto fail_req;
8479                 }
8480
8481                 qpair->req = ha->req_q_map[req_id];
8482                 qpair->rsp->req = qpair->req;
8483                 qpair->rsp->qpair = qpair;
8484                 /* init qpair to this cpu. Will adjust at run time. */
8485                 qla_cpu_update(qpair, smp_processor_id());
8486
8487                 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8488                         if (ha->fw_attributes & BIT_4)
8489                                 qpair->difdix_supported = 1;
8490                 }
8491
8492                 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8493                 if (!qpair->srb_mempool) {
8494                         ql_log(ql_log_warn, vha, 0xd036,
8495                             "Failed to create srb mempool for qpair %d\n",
8496                             qpair->id);
8497                         goto fail_mempool;
8498                 }
8499
8500                 /* Mark as online */
8501                 qpair->online = 1;
8502
8503                 if (!vha->flags.qpairs_available)
8504                         vha->flags.qpairs_available = 1;
8505
8506                 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8507                     "Request/Response queue pair created, id %d\n",
8508                     qpair->id);
8509                 ql_dbg(ql_dbg_init, vha, 0x0187,
8510                     "Request/Response queue pair created, id %d\n",
8511                     qpair->id);
8512         }
8513         return qpair;
8514
8515 fail_mempool:
8516 fail_req:
8517         qla25xx_delete_rsp_que(vha, qpair->rsp);
8518 fail_rsp:
8519         mutex_lock(&ha->mq_lock);
8520         qpair->msix->in_use = 0;
8521         list_del(&qpair->qp_list_elem);
8522         if (list_empty(&vha->qp_list))
8523                 vha->flags.qpairs_available = 0;
8524 fail_msix:
8525         ha->queue_pair_map[qpair_id] = NULL;
8526         clear_bit(qpair_id, ha->qpair_qid_map);
8527         ha->num_qpairs--;
8528         mutex_unlock(&ha->mq_lock);
8529 fail_qid_map:
8530         kfree(qpair);
8531         return NULL;
8532 }
8533
8534 int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8535 {
8536         int ret = QLA_FUNCTION_FAILED;
8537         struct qla_hw_data *ha = qpair->hw;
8538
8539         qpair->delete_in_progress = 1;
8540         while (atomic_read(&qpair->ref_count))
8541                 msleep(500);
8542
8543         ret = qla25xx_delete_req_que(vha, qpair->req);
8544         if (ret != QLA_SUCCESS)
8545                 goto fail;
8546
8547         ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8548         if (ret != QLA_SUCCESS)
8549                 goto fail;
8550
8551         mutex_lock(&ha->mq_lock);
8552         ha->queue_pair_map[qpair->id] = NULL;
8553         clear_bit(qpair->id, ha->qpair_qid_map);
8554         ha->num_qpairs--;
8555         list_del(&qpair->qp_list_elem);
8556         if (list_empty(&vha->qp_list)) {
8557                 vha->flags.qpairs_available = 0;
8558                 vha->flags.qpairs_req_created = 0;
8559                 vha->flags.qpairs_rsp_created = 0;
8560         }
8561         mempool_destroy(qpair->srb_mempool);
8562         kfree(qpair);
8563         mutex_unlock(&ha->mq_lock);
8564
8565         return QLA_SUCCESS;
8566 fail:
8567         return ret;
8568 }