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