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