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