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