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