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