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