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