[SCSI] qla2xxx: Further generalization of SRB CTX infrastructure.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / qla2xxx / qla_os.c
CommitLineData
1da177e4 1/*
fa90c54f 2 * QLogic Fibre Channel HBA Driver
01e58d8e 3 * Copyright (c) 2003-2008 QLogic Corporation
1da177e4 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
1da177e4
LT
6 */
7#include "qla_def.h"
8
9#include <linux/moduleparam.h>
10#include <linux/vmalloc.h>
1da177e4 11#include <linux/delay.h>
39a11240 12#include <linux/kthread.h>
e1e82b6f 13#include <linux/mutex.h>
3420d36c 14#include <linux/kobject.h>
5a0e3ad6 15#include <linux/slab.h>
1da177e4
LT
16
17#include <scsi/scsi_tcq.h>
18#include <scsi/scsicam.h>
19#include <scsi/scsi_transport.h>
20#include <scsi/scsi_transport_fc.h>
21
22/*
23 * Driver version
24 */
25char qla2x00_version_str[40];
26
6a03b4cd
HZ
27static int apidev_major;
28
1da177e4
LT
29/*
30 * SRB allocation cache
31 */
e18b890b 32static struct kmem_cache *srb_cachep;
1da177e4 33
a9083016
GM
34/*
35 * CT6 CTX allocation cache
36 */
37static struct kmem_cache *ctx_cachep;
38
1da177e4
LT
39int ql2xlogintimeout = 20;
40module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
41MODULE_PARM_DESC(ql2xlogintimeout,
42 "Login timeout value in seconds.");
43
a7b61842 44int qlport_down_retry;
1da177e4
LT
45module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
46MODULE_PARM_DESC(qlport_down_retry,
900d9f98 47 "Maximum number of command retries to a port that returns "
1da177e4
LT
48 "a PORT-DOWN status.");
49
1da177e4
LT
50int ql2xplogiabsentdevice;
51module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(ql2xplogiabsentdevice,
53 "Option to enable PLOGI to devices that are not present after "
900d9f98 54 "a Fabric scan. This is needed for several broken switches. "
1da177e4
LT
55 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
56
1da177e4
LT
57int ql2xloginretrycount = 0;
58module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
59MODULE_PARM_DESC(ql2xloginretrycount,
60 "Specify an alternate value for the NVRAM login retry count.");
61
a7a167bf
AV
62int ql2xallocfwdump = 1;
63module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
64MODULE_PARM_DESC(ql2xallocfwdump,
65 "Option to enable allocation of memory for a firmware dump "
66 "during HBA initialization. Memory allocation requirements "
67 "vary by ISP type. Default is 1 - allocate memory.");
68
11010fec 69int ql2xextended_error_logging;
27d94035 70module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
11010fec 71MODULE_PARM_DESC(ql2xextended_error_logging,
0181944f
AV
72 "Option to enable extended error logging, "
73 "Default is 0 - no logging. 1 - log errors.");
74
a9083016
GM
75int ql2xshiftctondsd = 6;
76module_param(ql2xshiftctondsd, int, S_IRUGO|S_IRUSR);
77MODULE_PARM_DESC(ql2xshiftctondsd,
78 "Set to control shifting of command type processing "
79 "based on total number of SG elements.");
80
1da177e4
LT
81static void qla2x00_free_device(scsi_qla_host_t *);
82
7e47e5ca 83int ql2xfdmienable=1;
cca5335c
AV
84module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
85MODULE_PARM_DESC(ql2xfdmienable,
7794a5af
FW
86 "Enables FDMI registrations. "
87 "0 - no FDMI. Default is 1 - perform FDMI.");
cca5335c 88
df7baa50
AV
89#define MAX_Q_DEPTH 32
90static int ql2xmaxqdepth = MAX_Q_DEPTH;
91module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
92MODULE_PARM_DESC(ql2xmaxqdepth,
93 "Maximum queue depth to report for target devices.");
94
e5896bd5
AV
95int ql2xiidmaenable=1;
96module_param(ql2xiidmaenable, int, S_IRUGO|S_IRUSR);
97MODULE_PARM_DESC(ql2xiidmaenable,
98 "Enables iIDMA settings "
99 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
100
73208dfd
AC
101int ql2xmaxqueues = 1;
102module_param(ql2xmaxqueues, int, S_IRUGO|S_IRUSR);
103MODULE_PARM_DESC(ql2xmaxqueues,
104 "Enables MQ settings "
105 "Default is 1 for single queue. Set it to number \
106 of queues in MQ mode.");
68ca949c
AC
107
108int ql2xmultique_tag;
109module_param(ql2xmultique_tag, int, S_IRUGO|S_IRUSR);
110MODULE_PARM_DESC(ql2xmultique_tag,
111 "Enables CPU affinity settings for the driver "
112 "Default is 0 for no affinity of request and response IO. "
113 "Set it to 1 to turn on the cpu affinity.");
e337d907
AV
114
115int ql2xfwloadbin;
116module_param(ql2xfwloadbin, int, S_IRUGO|S_IRUSR);
117MODULE_PARM_DESC(ql2xfwloadbin,
118 "Option to specify location from which to load ISP firmware:\n"
119 " 2 -- load firmware via the request_firmware() (hotplug)\n"
120 " interface.\n"
121 " 1 -- load firmware from flash.\n"
122 " 0 -- use default semantics.\n");
123
ae97c91e
AV
124int ql2xetsenable;
125module_param(ql2xetsenable, int, S_IRUGO|S_IRUSR);
126MODULE_PARM_DESC(ql2xetsenable,
127 "Enables firmware ETS burst."
128 "Default is 0 - skip ETS enablement.");
129
a9083016
GM
130int ql2xdbwr;
131module_param(ql2xdbwr, int, S_IRUGO|S_IRUSR);
132MODULE_PARM_DESC(ql2xdbwr,
133 "Option to specify scheme for request queue posting\n"
134 " 0 -- Regular doorbell.\n"
135 " 1 -- CAMRAM doorbell (faster).\n");
136
137int ql2xdontresethba;
138module_param(ql2xdontresethba, int, S_IRUGO|S_IRUSR);
139MODULE_PARM_DESC(ql2xdontresethba,
140 "Option to specify reset behaviour\n"
141 " 0 (Default) -- Reset on failure.\n"
142 " 1 -- Do not reset on failure.\n");
143
144
1da177e4 145/*
fa2a1ce5 146 * SCSI host template entry points
1da177e4
LT
147 */
148static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051eb 149static int qla2xxx_slave_alloc(struct scsi_device *);
1e99e33a
AV
150static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
151static void qla2xxx_scan_start(struct Scsi_Host *);
f4f051eb 152static void qla2xxx_slave_destroy(struct scsi_device *);
a5326f86 153static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
fca29703 154 void (*fn)(struct scsi_cmnd *));
1da177e4
LT
155static int qla2xxx_eh_abort(struct scsi_cmnd *);
156static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
523ec773 157static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
1da177e4
LT
158static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
159static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
1da177e4 160
e881a172 161static int qla2x00_change_queue_depth(struct scsi_device *, int, int);
ce7e4af7
AV
162static int qla2x00_change_queue_type(struct scsi_device *, int);
163
a5326f86 164struct scsi_host_template qla2xxx_driver_template = {
1da177e4 165 .module = THIS_MODULE,
cb63067a 166 .name = QLA2XXX_DRIVER_NAME,
a5326f86 167 .queuecommand = qla2xxx_queuecommand,
fca29703
AV
168
169 .eh_abort_handler = qla2xxx_eh_abort,
170 .eh_device_reset_handler = qla2xxx_eh_device_reset,
523ec773 171 .eh_target_reset_handler = qla2xxx_eh_target_reset,
fca29703
AV
172 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
173 .eh_host_reset_handler = qla2xxx_eh_host_reset,
174
175 .slave_configure = qla2xxx_slave_configure,
176
177 .slave_alloc = qla2xxx_slave_alloc,
178 .slave_destroy = qla2xxx_slave_destroy,
ed677086
AV
179 .scan_finished = qla2xxx_scan_finished,
180 .scan_start = qla2xxx_scan_start,
ce7e4af7
AV
181 .change_queue_depth = qla2x00_change_queue_depth,
182 .change_queue_type = qla2x00_change_queue_type,
fca29703
AV
183 .this_id = -1,
184 .cmd_per_lun = 3,
185 .use_clustering = ENABLE_CLUSTERING,
186 .sg_tablesize = SG_ALL,
187
188 .max_sectors = 0xFFFF,
afb046e2 189 .shost_attrs = qla2x00_host_attrs,
fca29703
AV
190};
191
1da177e4 192static struct scsi_transport_template *qla2xxx_transport_template = NULL;
2c3dfe3f 193struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
1da177e4 194
1da177e4
LT
195/* TODO Convert to inlines
196 *
197 * Timer routines
198 */
1da177e4 199
2c3dfe3f 200__inline__ void
e315cd28 201qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
1da177e4 202{
e315cd28
AC
203 init_timer(&vha->timer);
204 vha->timer.expires = jiffies + interval * HZ;
205 vha->timer.data = (unsigned long)vha;
206 vha->timer.function = (void (*)(unsigned long))func;
207 add_timer(&vha->timer);
208 vha->timer_active = 1;
1da177e4
LT
209}
210
211static inline void
e315cd28 212qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
1da177e4 213{
a9083016
GM
214 /* Currently used for 82XX only. */
215 if (vha->device_flags & DFLG_DEV_FAILED)
216 return;
217
e315cd28 218 mod_timer(&vha->timer, jiffies + interval * HZ);
1da177e4
LT
219}
220
a824ebb3 221static __inline__ void
e315cd28 222qla2x00_stop_timer(scsi_qla_host_t *vha)
1da177e4 223{
e315cd28
AC
224 del_timer_sync(&vha->timer);
225 vha->timer_active = 0;
1da177e4
LT
226}
227
1da177e4
LT
228static int qla2x00_do_dpc(void *data);
229
230static void qla2x00_rst_aen(scsi_qla_host_t *);
231
73208dfd
AC
232static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
233 struct req_que **, struct rsp_que **);
e315cd28
AC
234static void qla2x00_mem_free(struct qla_hw_data *);
235static void qla2x00_sp_free_dma(srb_t *);
1da177e4 236
1da177e4 237/* -------------------------------------------------------------------------- */
73208dfd
AC
238static int qla2x00_alloc_queues(struct qla_hw_data *ha)
239{
2afa19a9 240 ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
73208dfd
AC
241 GFP_KERNEL);
242 if (!ha->req_q_map) {
243 qla_printk(KERN_WARNING, ha,
244 "Unable to allocate memory for request queue ptrs\n");
245 goto fail_req_map;
246 }
247
2afa19a9 248 ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
73208dfd
AC
249 GFP_KERNEL);
250 if (!ha->rsp_q_map) {
251 qla_printk(KERN_WARNING, ha,
252 "Unable to allocate memory for response queue ptrs\n");
253 goto fail_rsp_map;
254 }
255 set_bit(0, ha->rsp_qid_map);
256 set_bit(0, ha->req_qid_map);
257 return 1;
258
259fail_rsp_map:
260 kfree(ha->req_q_map);
261 ha->req_q_map = NULL;
262fail_req_map:
263 return -ENOMEM;
264}
265
2afa19a9 266static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
73208dfd 267{
73208dfd
AC
268 if (req && req->ring)
269 dma_free_coherent(&ha->pdev->dev,
270 (req->length + 1) * sizeof(request_t),
271 req->ring, req->dma);
272
273 kfree(req);
274 req = NULL;
275}
276
2afa19a9
AC
277static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
278{
279 if (rsp && rsp->ring)
280 dma_free_coherent(&ha->pdev->dev,
281 (rsp->length + 1) * sizeof(response_t),
282 rsp->ring, rsp->dma);
283
284 kfree(rsp);
285 rsp = NULL;
286}
287
73208dfd
AC
288static void qla2x00_free_queues(struct qla_hw_data *ha)
289{
290 struct req_que *req;
291 struct rsp_que *rsp;
292 int cnt;
293
2afa19a9 294 for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
73208dfd 295 req = ha->req_q_map[cnt];
2afa19a9 296 qla2x00_free_req_que(ha, req);
73208dfd 297 }
73208dfd
AC
298 kfree(ha->req_q_map);
299 ha->req_q_map = NULL;
2afa19a9
AC
300
301 for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
302 rsp = ha->rsp_q_map[cnt];
303 qla2x00_free_rsp_que(ha, rsp);
304 }
305 kfree(ha->rsp_q_map);
306 ha->rsp_q_map = NULL;
73208dfd
AC
307}
308
68ca949c
AC
309static int qla25xx_setup_mode(struct scsi_qla_host *vha)
310{
311 uint16_t options = 0;
312 int ques, req, ret;
313 struct qla_hw_data *ha = vha->hw;
314
7163ea81
AC
315 if (!(ha->fw_attributes & BIT_6)) {
316 qla_printk(KERN_INFO, ha,
317 "Firmware is not multi-queue capable\n");
318 goto fail;
319 }
68ca949c 320 if (ql2xmultique_tag) {
68ca949c
AC
321 /* create a request queue for IO */
322 options |= BIT_7;
323 req = qla25xx_create_req_que(ha, options, 0, 0, -1,
324 QLA_DEFAULT_QUE_QOS);
325 if (!req) {
326 qla_printk(KERN_WARNING, ha,
327 "Can't create request queue\n");
328 goto fail;
329 }
7163ea81 330 ha->wq = create_workqueue("qla2xxx_wq");
68ca949c
AC
331 vha->req = ha->req_q_map[req];
332 options |= BIT_1;
333 for (ques = 1; ques < ha->max_rsp_queues; ques++) {
334 ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
335 if (!ret) {
336 qla_printk(KERN_WARNING, ha,
337 "Response Queue create failed\n");
338 goto fail2;
339 }
340 }
7163ea81
AC
341 ha->flags.cpu_affinity_enabled = 1;
342
68ca949c
AC
343 DEBUG2(qla_printk(KERN_INFO, ha,
344 "CPU affinity mode enabled, no. of response"
345 " queues:%d, no. of request queues:%d\n",
346 ha->max_rsp_queues, ha->max_req_queues));
347 }
348 return 0;
349fail2:
350 qla25xx_delete_queues(vha);
7163ea81
AC
351 destroy_workqueue(ha->wq);
352 ha->wq = NULL;
68ca949c
AC
353fail:
354 ha->mqenable = 0;
7163ea81
AC
355 kfree(ha->req_q_map);
356 kfree(ha->rsp_q_map);
357 ha->max_req_queues = ha->max_rsp_queues = 1;
68ca949c
AC
358 return 1;
359}
360
1da177e4 361static char *
e315cd28 362qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
1da177e4 363{
e315cd28 364 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
365 static char *pci_bus_modes[] = {
366 "33", "66", "100", "133",
367 };
368 uint16_t pci_bus;
369
370 strcpy(str, "PCI");
371 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
372 if (pci_bus) {
373 strcat(str, "-X (");
374 strcat(str, pci_bus_modes[pci_bus]);
375 } else {
376 pci_bus = (ha->pci_attr & BIT_8) >> 8;
377 strcat(str, " (");
378 strcat(str, pci_bus_modes[pci_bus]);
379 }
380 strcat(str, " MHz)");
381
382 return (str);
383}
384
fca29703 385static char *
e315cd28 386qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
fca29703
AV
387{
388 static char *pci_bus_modes[] = { "33", "66", "100", "133", };
e315cd28 389 struct qla_hw_data *ha = vha->hw;
fca29703
AV
390 uint32_t pci_bus;
391 int pcie_reg;
392
393 pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
394 if (pcie_reg) {
395 char lwstr[6];
396 uint16_t pcie_lstat, lspeed, lwidth;
397
398 pcie_reg += 0x12;
399 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
400 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
401 lwidth = (pcie_lstat &
402 (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
403
404 strcpy(str, "PCIe (");
405 if (lspeed == 1)
c87a0d8c 406 strcat(str, "2.5GT/s ");
c3a2f0df 407 else if (lspeed == 2)
c87a0d8c 408 strcat(str, "5.0GT/s ");
fca29703
AV
409 else
410 strcat(str, "<unknown> ");
411 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
412 strcat(str, lwstr);
413
414 return str;
415 }
416
417 strcpy(str, "PCI");
418 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
419 if (pci_bus == 0 || pci_bus == 8) {
420 strcat(str, " (");
421 strcat(str, pci_bus_modes[pci_bus >> 3]);
422 } else {
423 strcat(str, "-X ");
424 if (pci_bus & BIT_2)
425 strcat(str, "Mode 2");
426 else
427 strcat(str, "Mode 1");
428 strcat(str, " (");
429 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
430 }
431 strcat(str, " MHz)");
432
433 return str;
434}
435
e5f82ab8 436static char *
e315cd28 437qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
1da177e4
LT
438{
439 char un_str[10];
e315cd28 440 struct qla_hw_data *ha = vha->hw;
fa2a1ce5 441
1da177e4
LT
442 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
443 ha->fw_minor_version,
444 ha->fw_subminor_version);
445
446 if (ha->fw_attributes & BIT_9) {
447 strcat(str, "FLX");
448 return (str);
449 }
450
451 switch (ha->fw_attributes & 0xFF) {
452 case 0x7:
453 strcat(str, "EF");
454 break;
455 case 0x17:
456 strcat(str, "TP");
457 break;
458 case 0x37:
459 strcat(str, "IP");
460 break;
461 case 0x77:
462 strcat(str, "VI");
463 break;
464 default:
465 sprintf(un_str, "(%x)", ha->fw_attributes);
466 strcat(str, un_str);
467 break;
468 }
469 if (ha->fw_attributes & 0x100)
470 strcat(str, "X");
471
472 return (str);
473}
474
e5f82ab8 475static char *
e315cd28 476qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
fca29703 477{
e315cd28 478 struct qla_hw_data *ha = vha->hw;
f0883ac6 479
3a03eb79
AV
480 sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
481 ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
fca29703 482 return str;
fca29703
AV
483}
484
485static inline srb_t *
e315cd28 486qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
fca29703
AV
487 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
488{
489 srb_t *sp;
e315cd28 490 struct qla_hw_data *ha = vha->hw;
fca29703
AV
491
492 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
493 if (!sp)
494 return sp;
495
fca29703
AV
496 sp->fcport = fcport;
497 sp->cmd = cmd;
498 sp->flags = 0;
499 CMD_SP(cmd) = (void *)sp;
500 cmd->scsi_done = done;
cf53b069 501 sp->ctx = NULL;
fca29703
AV
502
503 return sp;
504}
505
1da177e4 506static int
a5326f86 507qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
fca29703 508{
e315cd28 509 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
fca29703 510 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
19a7b4ae 511 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
e315cd28
AC
512 struct qla_hw_data *ha = vha->hw;
513 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
fca29703
AV
514 srb_t *sp;
515 int rval;
516
85880801
AV
517 if (ha->flags.eeh_busy) {
518 if (ha->flags.pci_channel_io_perm_failure)
b9b12f73 519 cmd->result = DID_NO_CONNECT << 16;
85880801
AV
520 else
521 cmd->result = DID_REQUEUE << 16;
14e660e6
SJ
522 goto qc24_fail_command;
523 }
524
19a7b4ae
JSEC
525 rval = fc_remote_port_chkready(rport);
526 if (rval) {
527 cmd->result = rval;
fca29703
AV
528 goto qc24_fail_command;
529 }
530
387f96b4 531 /* Close window on fcport/rport state-transitioning. */
7b594131
MC
532 if (fcport->drport)
533 goto qc24_target_busy;
387f96b4 534
fca29703
AV
535 if (atomic_read(&fcport->state) != FCS_ONLINE) {
536 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
e315cd28 537 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
fca29703
AV
538 cmd->result = DID_NO_CONNECT << 16;
539 goto qc24_fail_command;
540 }
7b594131 541 goto qc24_target_busy;
fca29703
AV
542 }
543
e315cd28 544 spin_unlock_irq(vha->host->host_lock);
fca29703 545
e315cd28 546 sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
fca29703
AV
547 if (!sp)
548 goto qc24_host_busy_lock;
549
e315cd28 550 rval = ha->isp_ops->start_scsi(sp);
fca29703
AV
551 if (rval != QLA_SUCCESS)
552 goto qc24_host_busy_free_sp;
553
e315cd28 554 spin_lock_irq(vha->host->host_lock);
fca29703
AV
555
556 return 0;
557
558qc24_host_busy_free_sp:
e315cd28
AC
559 qla2x00_sp_free_dma(sp);
560 mempool_free(sp, ha->srb_mempool);
fca29703
AV
561
562qc24_host_busy_lock:
e315cd28 563 spin_lock_irq(vha->host->host_lock);
fca29703
AV
564 return SCSI_MLQUEUE_HOST_BUSY;
565
7b594131
MC
566qc24_target_busy:
567 return SCSI_MLQUEUE_TARGET_BUSY;
568
fca29703
AV
569qc24_fail_command:
570 done(cmd);
571
572 return 0;
573}
574
575
1da177e4
LT
576/*
577 * qla2x00_eh_wait_on_command
578 * Waits for the command to be returned by the Firmware for some
579 * max time.
580 *
581 * Input:
1da177e4 582 * cmd = Scsi Command to wait on.
1da177e4
LT
583 *
584 * Return:
585 * Not Found : 0
586 * Found : 1
587 */
588static int
e315cd28 589qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1da177e4 590{
fe74c71f
AV
591#define ABORT_POLLING_PERIOD 1000
592#define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
f4f051eb 593 unsigned long wait_iter = ABORT_WAIT_ITER;
85880801
AV
594 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
595 struct qla_hw_data *ha = vha->hw;
f4f051eb 596 int ret = QLA_SUCCESS;
1da177e4 597
85880801
AV
598 if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
599 DEBUG17(qla_printk(KERN_WARNING, ha, "return:eh_wait\n"));
600 return ret;
601 }
602
d970432c 603 while (CMD_SP(cmd) && wait_iter--) {
fe74c71f 604 msleep(ABORT_POLLING_PERIOD);
f4f051eb
AV
605 }
606 if (CMD_SP(cmd))
607 ret = QLA_FUNCTION_FAILED;
1da177e4 608
f4f051eb 609 return ret;
1da177e4
LT
610}
611
612/*
613 * qla2x00_wait_for_hba_online
fa2a1ce5 614 * Wait till the HBA is online after going through
1da177e4
LT
615 * <= MAX_RETRIES_OF_ISP_ABORT or
616 * finally HBA is disabled ie marked offline
617 *
618 * Input:
619 * ha - pointer to host adapter structure
fa2a1ce5
AV
620 *
621 * Note:
1da177e4
LT
622 * Does context switching-Release SPIN_LOCK
623 * (if any) before calling this routine.
624 *
625 * Return:
626 * Success (Adapter is online) : 0
627 * Failed (Adapter is offline/disabled) : 1
628 */
854165f4 629int
e315cd28 630qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1da177e4 631{
fca29703
AV
632 int return_status;
633 unsigned long wait_online;
e315cd28
AC
634 struct qla_hw_data *ha = vha->hw;
635 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 636
fa2a1ce5 637 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
e315cd28
AC
638 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
639 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
640 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
641 ha->dpc_active) && time_before(jiffies, wait_online)) {
1da177e4
LT
642
643 msleep(1000);
644 }
e315cd28 645 if (base_vha->flags.online)
fa2a1ce5 646 return_status = QLA_SUCCESS;
1da177e4
LT
647 else
648 return_status = QLA_FUNCTION_FAILED;
649
1da177e4
LT
650 return (return_status);
651}
652
2533cf67
LC
653int
654qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
655{
656 int return_status;
657 unsigned long wait_reset;
658 struct qla_hw_data *ha = vha->hw;
659 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
660
661 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
662 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
663 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
664 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
665 ha->dpc_active) && time_before(jiffies, wait_reset)) {
666
667 msleep(1000);
668
669 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
670 ha->flags.chip_reset_done)
671 break;
672 }
673 if (ha->flags.chip_reset_done)
674 return_status = QLA_SUCCESS;
675 else
676 return_status = QLA_FUNCTION_FAILED;
677
678 return return_status;
679}
680
1da177e4
LT
681/*
682 * qla2x00_wait_for_loop_ready
683 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
fa2a1ce5 684 * to be in LOOP_READY state.
1da177e4
LT
685 * Input:
686 * ha - pointer to host adapter structure
fa2a1ce5
AV
687 *
688 * Note:
1da177e4
LT
689 * Does context switching-Release SPIN_LOCK
690 * (if any) before calling this routine.
fa2a1ce5 691 *
1da177e4
LT
692 *
693 * Return:
694 * Success (LOOP_READY) : 0
695 * Failed (LOOP_NOT_READY) : 1
696 */
fa2a1ce5 697static inline int
e315cd28 698qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
1da177e4
LT
699{
700 int return_status = QLA_SUCCESS;
701 unsigned long loop_timeout ;
e315cd28
AC
702 struct qla_hw_data *ha = vha->hw;
703 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4
LT
704
705 /* wait for 5 min at the max for loop to be ready */
fa2a1ce5 706 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1da177e4 707
e315cd28
AC
708 while ((!atomic_read(&base_vha->loop_down_timer) &&
709 atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
710 atomic_read(&base_vha->loop_state) != LOOP_READY) {
711 if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
57680080
RA
712 return_status = QLA_FUNCTION_FAILED;
713 break;
714 }
1da177e4
LT
715 msleep(1000);
716 if (time_after_eq(jiffies, loop_timeout)) {
717 return_status = QLA_FUNCTION_FAILED;
718 break;
719 }
720 }
fa2a1ce5 721 return (return_status);
1da177e4
LT
722}
723
724/**************************************************************************
725* qla2xxx_eh_abort
726*
727* Description:
728* The abort function will abort the specified command.
729*
730* Input:
731* cmd = Linux SCSI command packet to be aborted.
732*
733* Returns:
734* Either SUCCESS or FAILED.
735*
736* Note:
2ea00202 737* Only return FAILED if command not returned by firmware.
1da177e4 738**************************************************************************/
e5f82ab8 739static int
1da177e4
LT
740qla2xxx_eh_abort(struct scsi_cmnd *cmd)
741{
e315cd28 742 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
f4f051eb 743 srb_t *sp;
17d98630 744 int ret, i;
f4f051eb
AV
745 unsigned int id, lun;
746 unsigned long serial;
18e144d3 747 unsigned long flags;
2ea00202 748 int wait = 0;
e315cd28 749 struct qla_hw_data *ha = vha->hw;
67c2e93a 750 struct req_que *req = vha->req;
17d98630 751 srb_t *spt;
1da177e4 752
65d430fa 753 fc_block_scsi_eh(cmd);
07db5183 754
f4f051eb 755 if (!CMD_SP(cmd))
2ea00202 756 return SUCCESS;
1da177e4 757
2ea00202 758 ret = SUCCESS;
1da177e4 759
f4f051eb
AV
760 id = cmd->device->id;
761 lun = cmd->device->lun;
762 serial = cmd->serial_number;
17d98630
AC
763 spt = (srb_t *) CMD_SP(cmd);
764 if (!spt)
765 return SUCCESS;
1da177e4 766
f4f051eb 767 /* Check active list for command command. */
e315cd28 768 spin_lock_irqsave(&ha->hardware_lock, flags);
17d98630
AC
769 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
770 sp = req->outstanding_cmds[i];
1da177e4 771
17d98630
AC
772 if (sp == NULL)
773 continue;
a9083016 774 if ((sp->ctx) && !(sp->flags & SRB_FCP_CMND_DMA_VALID))
cf53b069 775 continue;
17d98630
AC
776 if (sp->cmd != cmd)
777 continue;
1da177e4 778
17d98630
AC
779 DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
780 " pid=%ld.\n", __func__, vha->host_no, sp, serial));
781
782 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2afa19a9 783 if (ha->isp_ops->abort_command(sp)) {
17d98630
AC
784 DEBUG2(printk("%s(%ld): abort_command "
785 "mbx failed.\n", __func__, vha->host_no));
2ac4b64f 786 ret = FAILED;
17d98630
AC
787 } else {
788 DEBUG3(printk("%s(%ld): abort_command "
789 "mbx success.\n", __func__, vha->host_no));
790 wait = 1;
73208dfd 791 }
17d98630
AC
792 spin_lock_irqsave(&ha->hardware_lock, flags);
793 break;
f4f051eb 794 }
e315cd28 795 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4 796
f4f051eb 797 /* Wait for the command to be returned. */
2ea00202 798 if (wait) {
e315cd28 799 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
fa2a1ce5 800 qla_printk(KERN_ERR, ha,
f4f051eb 801 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
e315cd28 802 "%x.\n", vha->host_no, id, lun, serial, ret);
2ea00202 803 ret = FAILED;
f4f051eb 804 }
1da177e4 805 }
1da177e4 806
fa2a1ce5 807 qla_printk(KERN_INFO, ha,
2ea00202 808 "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
e315cd28 809 vha->host_no, id, lun, wait, serial, ret);
1da177e4 810
f4f051eb
AV
811 return ret;
812}
1da177e4 813
523ec773
AV
814enum nexus_wait_type {
815 WAIT_HOST = 0,
816 WAIT_TARGET,
817 WAIT_LUN,
818};
819
f4f051eb 820static int
e315cd28 821qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
17d98630 822 unsigned int l, srb_t *sp, enum nexus_wait_type type)
f4f051eb 823{
17d98630 824 int cnt, match, status;
18e144d3 825 unsigned long flags;
e315cd28 826 struct qla_hw_data *ha = vha->hw;
73208dfd 827 struct req_que *req;
1da177e4 828
523ec773 829 status = QLA_SUCCESS;
17d98630
AC
830 if (!sp)
831 return status;
832
e315cd28 833 spin_lock_irqsave(&ha->hardware_lock, flags);
67c2e93a 834 req = vha->req;
17d98630
AC
835 for (cnt = 1; status == QLA_SUCCESS &&
836 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
837 sp = req->outstanding_cmds[cnt];
838 if (!sp)
523ec773 839 continue;
cf53b069
AV
840 if (sp->ctx)
841 continue;
17d98630
AC
842 if (vha->vp_idx != sp->fcport->vha->vp_idx)
843 continue;
844 match = 0;
845 switch (type) {
846 case WAIT_HOST:
847 match = 1;
848 break;
849 case WAIT_TARGET:
850 match = sp->cmd->device->id == t;
851 break;
852 case WAIT_LUN:
853 match = (sp->cmd->device->id == t &&
854 sp->cmd->device->lun == l);
855 break;
73208dfd 856 }
17d98630
AC
857 if (!match)
858 continue;
859
860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
861 status = qla2x00_eh_wait_on_command(sp->cmd);
862 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 863 }
e315cd28 864 spin_unlock_irqrestore(&ha->hardware_lock, flags);
523ec773
AV
865
866 return status;
1da177e4
LT
867}
868
a9083016
GM
869void qla82xx_wait_for_pending_commands(scsi_qla_host_t *vha)
870{
871 int cnt;
872 srb_t *sp;
873 struct req_que *req = vha->req;
874
875 DEBUG2(qla_printk(KERN_INFO, vha->hw,
876 "Waiting for pending commands\n"));
877 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
878 sp = req->outstanding_cmds[cnt];
879 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
880 sp, WAIT_HOST) == QLA_SUCCESS) {
881 DEBUG2(qla_printk(KERN_INFO, vha->hw,
882 "Done wait for pending commands\n"));
883 }
884 }
885}
886
523ec773
AV
887static char *reset_errors[] = {
888 "HBA not online",
889 "HBA not ready",
890 "Task management failed",
891 "Waiting for command completions",
892};
1da177e4 893
e5f82ab8 894static int
523ec773 895__qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
2afa19a9 896 struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
1da177e4 897{
e315cd28 898 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 899 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
523ec773 900 int err;
1da177e4 901
65d430fa 902 fc_block_scsi_eh(cmd);
07db5183 903
b0328bee 904 if (!fcport)
523ec773 905 return FAILED;
1da177e4 906
e315cd28
AC
907 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
908 vha->host_no, cmd->device->id, cmd->device->lun, name);
1da177e4 909
523ec773 910 err = 0;
e315cd28 911 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
523ec773
AV
912 goto eh_reset_failed;
913 err = 1;
e315cd28 914 if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
523ec773
AV
915 goto eh_reset_failed;
916 err = 2;
2afa19a9
AC
917 if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
918 != QLA_SUCCESS)
523ec773
AV
919 goto eh_reset_failed;
920 err = 3;
e315cd28 921 if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
17d98630 922 cmd->device->lun, (srb_t *) CMD_SP(cmd), type) != QLA_SUCCESS)
523ec773
AV
923 goto eh_reset_failed;
924
e315cd28
AC
925 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
926 vha->host_no, cmd->device->id, cmd->device->lun, name);
523ec773
AV
927
928 return SUCCESS;
929
930 eh_reset_failed:
e315cd28
AC
931 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
932 , vha->host_no, cmd->device->id, cmd->device->lun, name,
523ec773
AV
933 reset_errors[err]);
934 return FAILED;
935}
1da177e4 936
523ec773
AV
937static int
938qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
939{
e315cd28
AC
940 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
941 struct qla_hw_data *ha = vha->hw;
1da177e4 942
523ec773
AV
943 return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
944 ha->isp_ops->lun_reset);
1da177e4
LT
945}
946
1da177e4 947static int
523ec773 948qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1da177e4 949{
e315cd28
AC
950 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
951 struct qla_hw_data *ha = vha->hw;
1da177e4 952
523ec773
AV
953 return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
954 ha->isp_ops->target_reset);
1da177e4
LT
955}
956
1da177e4
LT
957/**************************************************************************
958* qla2xxx_eh_bus_reset
959*
960* Description:
961* The bus reset function will reset the bus and abort any executing
962* commands.
963*
964* Input:
965* cmd = Linux SCSI command packet of the command that cause the
966* bus reset.
967*
968* Returns:
969* SUCCESS/FAILURE (defined as macro in scsi.h).
970*
971**************************************************************************/
e5f82ab8 972static int
1da177e4
LT
973qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
974{
e315cd28 975 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 976 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
2c3dfe3f 977 int ret = FAILED;
f4f051eb
AV
978 unsigned int id, lun;
979 unsigned long serial;
17d98630 980 srb_t *sp = (srb_t *) CMD_SP(cmd);
f4f051eb 981
65d430fa 982 fc_block_scsi_eh(cmd);
07db5183 983
f4f051eb
AV
984 id = cmd->device->id;
985 lun = cmd->device->lun;
986 serial = cmd->serial_number;
1da177e4 987
b0328bee 988 if (!fcport)
f4f051eb 989 return ret;
1da177e4 990
e315cd28 991 qla_printk(KERN_INFO, vha->hw,
749af3d5 992 "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 993
e315cd28 994 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1da177e4 995 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051eb 996 goto eh_bus_reset_done;
1da177e4
LT
997 }
998
e315cd28
AC
999 if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
1000 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
f4f051eb 1001 ret = SUCCESS;
1da177e4 1002 }
f4f051eb
AV
1003 if (ret == FAILED)
1004 goto eh_bus_reset_done;
1da177e4 1005
9a41a62b 1006 /* Flush outstanding commands. */
17d98630 1007 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) !=
523ec773 1008 QLA_SUCCESS)
9a41a62b 1009 ret = FAILED;
1da177e4 1010
f4f051eb 1011eh_bus_reset_done:
e315cd28 1012 qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
f4f051eb 1013 (ret == FAILED) ? "failed" : "succeded");
1da177e4 1014
f4f051eb 1015 return ret;
1da177e4
LT
1016}
1017
1018/**************************************************************************
1019* qla2xxx_eh_host_reset
1020*
1021* Description:
1022* The reset function will reset the Adapter.
1023*
1024* Input:
1025* cmd = Linux SCSI command packet of the command that cause the
1026* adapter reset.
1027*
1028* Returns:
1029* Either SUCCESS or FAILED.
1030*
1031* Note:
1032**************************************************************************/
e5f82ab8 1033static int
1da177e4
LT
1034qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1035{
e315cd28 1036 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 1037 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
e315cd28 1038 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 1039 int ret = FAILED;
f4f051eb
AV
1040 unsigned int id, lun;
1041 unsigned long serial;
17d98630 1042 srb_t *sp = (srb_t *) CMD_SP(cmd);
e315cd28 1043 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 1044
65d430fa 1045 fc_block_scsi_eh(cmd);
07db5183 1046
f4f051eb
AV
1047 id = cmd->device->id;
1048 lun = cmd->device->lun;
1049 serial = cmd->serial_number;
1050
b0328bee 1051 if (!fcport)
f4f051eb 1052 return ret;
1da177e4 1053
1da177e4 1054 qla_printk(KERN_INFO, ha,
e315cd28 1055 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 1056
e315cd28 1057 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
f4f051eb 1058 goto eh_host_reset_lock;
1da177e4
LT
1059
1060 /*
1061 * Fixme-may be dpc thread is active and processing
fa2a1ce5 1062 * loop_resync,so wait a while for it to
1da177e4
LT
1063 * be completed and then issue big hammer.Otherwise
1064 * it may cause I/O failure as big hammer marks the
1065 * devices as lost kicking of the port_down_timer
1066 * while dpc is stuck for the mailbox to complete.
1067 */
e315cd28
AC
1068 qla2x00_wait_for_loop_ready(vha);
1069 if (vha != base_vha) {
1070 if (qla2x00_vp_abort_isp(vha))
f4f051eb 1071 goto eh_host_reset_lock;
e315cd28 1072 } else {
a9083016
GM
1073 if (IS_QLA82XX(vha->hw)) {
1074 if (!qla82xx_fcoe_ctx_reset(vha)) {
1075 /* Ctx reset success */
1076 ret = SUCCESS;
1077 goto eh_host_reset_lock;
1078 }
1079 /* fall thru if ctx reset failed */
1080 }
68ca949c
AC
1081 if (ha->wq)
1082 flush_workqueue(ha->wq);
1083
e315cd28 1084 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
a9083016 1085 if (ha->isp_ops->abort_isp(base_vha)) {
e315cd28
AC
1086 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1087 /* failed. schedule dpc to try */
1088 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1089
1090 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1091 goto eh_host_reset_lock;
1092 }
1093 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
fa2a1ce5 1094 }
1da177e4 1095
e315cd28 1096 /* Waiting for command to be returned to OS.*/
17d98630 1097 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) ==
e315cd28 1098 QLA_SUCCESS)
f4f051eb 1099 ret = SUCCESS;
1da177e4 1100
f4f051eb 1101eh_host_reset_lock:
f4f051eb
AV
1102 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1103 (ret == FAILED) ? "failed" : "succeded");
1da177e4 1104
f4f051eb
AV
1105 return ret;
1106}
1da177e4
LT
1107
1108/*
1109* qla2x00_loop_reset
1110* Issue loop reset.
1111*
1112* Input:
1113* ha = adapter block pointer.
1114*
1115* Returns:
1116* 0 = success
1117*/
a4722cf2 1118int
e315cd28 1119qla2x00_loop_reset(scsi_qla_host_t *vha)
1da177e4 1120{
0c8c39af 1121 int ret;
bdf79621 1122 struct fc_port *fcport;
e315cd28 1123 struct qla_hw_data *ha = vha->hw;
1da177e4 1124
55e5ed27
AV
1125 if (ha->flags.enable_target_reset) {
1126 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1127 if (fcport->port_type != FCT_TARGET)
1128 continue;
1129
1130 ret = ha->isp_ops->target_reset(fcport, 0, 0);
1131 if (ret != QLA_SUCCESS) {
1132 DEBUG2_3(printk("%s(%ld): bus_reset failed: "
1133 "target_reset=%d d_id=%x.\n", __func__,
1134 vha->host_no, ret, fcport->d_id.b24));
1135 }
1136 }
1137 }
1138
a9083016 1139 if (ha->flags.enable_lip_full_login && !IS_QLA8XXX_TYPE(ha)) {
e315cd28 1140 ret = qla2x00_full_login_lip(vha);
0c8c39af 1141 if (ret != QLA_SUCCESS) {
749af3d5 1142 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28 1143 "full_login_lip=%d.\n", __func__, vha->host_no,
0c8c39af 1144 ret));
749af3d5
AC
1145 }
1146 atomic_set(&vha->loop_state, LOOP_DOWN);
1147 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1148 qla2x00_mark_all_devices_lost(vha, 0);
1149 qla2x00_wait_for_loop_ready(vha);
0c8c39af
AV
1150 }
1151
0d6e61bc 1152 if (ha->flags.enable_lip_reset) {
e315cd28 1153 ret = qla2x00_lip_reset(vha);
0c8c39af 1154 if (ret != QLA_SUCCESS) {
749af3d5 1155 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28
AC
1156 "lip_reset=%d.\n", __func__, vha->host_no, ret));
1157 } else
1158 qla2x00_wait_for_loop_ready(vha);
1da177e4
LT
1159 }
1160
1da177e4 1161 /* Issue marker command only when we are going to start the I/O */
e315cd28 1162 vha->marker_needed = 1;
1da177e4 1163
0c8c39af 1164 return QLA_SUCCESS;
1da177e4
LT
1165}
1166
df4bf0bb 1167void
e315cd28 1168qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
df4bf0bb 1169{
73208dfd 1170 int que, cnt;
df4bf0bb
AV
1171 unsigned long flags;
1172 srb_t *sp;
ac280b67 1173 struct srb_ctx *ctx;
e315cd28 1174 struct qla_hw_data *ha = vha->hw;
73208dfd 1175 struct req_que *req;
df4bf0bb
AV
1176
1177 spin_lock_irqsave(&ha->hardware_lock, flags);
2afa19a9 1178 for (que = 0; que < ha->max_req_queues; que++) {
29bdccbe 1179 req = ha->req_q_map[que];
73208dfd
AC
1180 if (!req)
1181 continue;
1182 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1183 sp = req->outstanding_cmds[cnt];
e612d465 1184 if (sp) {
73208dfd 1185 req->outstanding_cmds[cnt] = NULL;
a9083016
GM
1186 if (!sp->ctx ||
1187 (sp->flags & SRB_FCP_CMND_DMA_VALID)) {
ac280b67
AV
1188 sp->cmd->result = res;
1189 qla2x00_sp_compl(ha, sp);
1190 } else {
1191 ctx = sp->ctx;
6c452a45
AV
1192 if (ctx->type == SRB_LOGIN_CMD ||
1193 ctx->type == SRB_LOGOUT_CMD) {
db3ad7f8
GM
1194 del_timer_sync(&ctx->timer);
1195 ctx->free(sp);
1196 } else {
6c452a45
AV
1197 struct srb_bsg *sp_bsg =
1198 (struct srb_bsg *)sp->ctx;
1199 struct fc_bsg_job *bsg_job =
1200 sp_bsg->bsg_job;
1201
1202 if (bsg_job->request->msgcode
1203 == FC_BSG_HST_CT)
db3ad7f8 1204 kfree(sp->fcport);
6c452a45
AV
1205 bsg_job->req->errors = 0;
1206 bsg_job->reply->result = res;
1207 bsg_job->job_done(
1208 sp_bsg->bsg_job);
db3ad7f8 1209 kfree(sp->ctx);
6c452a45
AV
1210 mempool_free(sp,
1211 ha->srb_mempool);
db3ad7f8 1212 }
ac280b67 1213 }
73208dfd 1214 }
df4bf0bb
AV
1215 }
1216 }
1217 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1218}
1219
f4f051eb
AV
1220static int
1221qla2xxx_slave_alloc(struct scsi_device *sdev)
1da177e4 1222{
bdf79621 1223 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1da177e4 1224
19a7b4ae 1225 if (!rport || fc_remote_port_chkready(rport))
f4f051eb 1226 return -ENXIO;
bdf79621 1227
19a7b4ae 1228 sdev->hostdata = *(fc_port_t **)rport->dd_data;
1da177e4 1229
f4f051eb
AV
1230 return 0;
1231}
1da177e4 1232
f4f051eb
AV
1233static int
1234qla2xxx_slave_configure(struct scsi_device *sdev)
1235{
e315cd28
AC
1236 scsi_qla_host_t *vha = shost_priv(sdev->host);
1237 struct qla_hw_data *ha = vha->hw;
8482e118 1238 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2afa19a9 1239 struct req_que *req = vha->req;
8482e118 1240
f4f051eb 1241 if (sdev->tagged_supported)
73208dfd 1242 scsi_activate_tcq(sdev, req->max_q_depth);
f4f051eb 1243 else
73208dfd 1244 scsi_deactivate_tcq(sdev, req->max_q_depth);
1da177e4 1245
85821c90 1246 rport->dev_loss_tmo = ha->port_down_retry_count;
8482e118 1247
f4f051eb
AV
1248 return 0;
1249}
1da177e4 1250
f4f051eb
AV
1251static void
1252qla2xxx_slave_destroy(struct scsi_device *sdev)
1253{
1254 sdev->hostdata = NULL;
1da177e4
LT
1255}
1256
c45dd305
GM
1257static void qla2x00_handle_queue_full(struct scsi_device *sdev, int qdepth)
1258{
1259 fc_port_t *fcport = (struct fc_port *) sdev->hostdata;
1260
1261 if (!scsi_track_queue_full(sdev, qdepth))
1262 return;
1263
1264 DEBUG2(qla_printk(KERN_INFO, fcport->vha->hw,
1265 "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
1266 fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1267 sdev->queue_depth));
1268}
1269
1270static void qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, int qdepth)
1271{
1272 fc_port_t *fcport = sdev->hostdata;
1273 struct scsi_qla_host *vha = fcport->vha;
1274 struct qla_hw_data *ha = vha->hw;
1275 struct req_que *req = NULL;
1276
1277 req = vha->req;
1278 if (!req)
1279 return;
1280
1281 if (req->max_q_depth <= sdev->queue_depth || req->max_q_depth < qdepth)
1282 return;
1283
1284 if (sdev->ordered_tags)
1285 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, qdepth);
1286 else
1287 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, qdepth);
1288
1289 DEBUG2(qla_printk(KERN_INFO, ha,
1290 "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
1291 fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
1292 sdev->queue_depth));
1293}
1294
ce7e4af7 1295static int
e881a172 1296qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
ce7e4af7 1297{
c45dd305
GM
1298 switch (reason) {
1299 case SCSI_QDEPTH_DEFAULT:
1300 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1301 break;
1302 case SCSI_QDEPTH_QFULL:
1303 qla2x00_handle_queue_full(sdev, qdepth);
1304 break;
1305 case SCSI_QDEPTH_RAMP_UP:
1306 qla2x00_adjust_sdev_qdepth_up(sdev, qdepth);
1307 break;
1308 default:
08002af2 1309 return -EOPNOTSUPP;
c45dd305 1310 }
e881a172 1311
ce7e4af7
AV
1312 return sdev->queue_depth;
1313}
1314
1315static int
1316qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1317{
1318 if (sdev->tagged_supported) {
1319 scsi_set_tag_type(sdev, tag_type);
1320 if (tag_type)
1321 scsi_activate_tcq(sdev, sdev->queue_depth);
1322 else
1323 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1324 } else
1325 tag_type = 0;
1326
1327 return tag_type;
1328}
1329
1da177e4
LT
1330/**
1331 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1332 * @ha: HA context
1333 *
1334 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1335 * supported addressing method.
1336 */
1337static void
53303c42 1338qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1da177e4 1339{
7524f9b9 1340 /* Assume a 32bit DMA mask. */
1da177e4 1341 ha->flags.enable_64bit_addressing = 0;
1da177e4 1342
6a35528a 1343 if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
7524f9b9
AV
1344 /* Any upper-dword bits set? */
1345 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
6a35528a 1346 !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
7524f9b9 1347 /* Ok, a 64bit DMA mask is applicable. */
1da177e4 1348 ha->flags.enable_64bit_addressing = 1;
fd34f556
AV
1349 ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1350 ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
7524f9b9 1351 return;
1da177e4 1352 }
1da177e4 1353 }
7524f9b9 1354
284901a9
YH
1355 dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1356 pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1da177e4
LT
1357}
1358
fd34f556 1359static void
e315cd28 1360qla2x00_enable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1361{
1362 unsigned long flags = 0;
1363 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1364
1365 spin_lock_irqsave(&ha->hardware_lock, flags);
1366 ha->interrupts_on = 1;
1367 /* enable risc and host interrupts */
1368 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1369 RD_REG_WORD(&reg->ictrl);
1370 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1371
1372}
1373
1374static void
e315cd28 1375qla2x00_disable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1376{
1377 unsigned long flags = 0;
1378 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1379
1380 spin_lock_irqsave(&ha->hardware_lock, flags);
1381 ha->interrupts_on = 0;
1382 /* disable risc and host interrupts */
1383 WRT_REG_WORD(&reg->ictrl, 0);
1384 RD_REG_WORD(&reg->ictrl);
1385 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1386}
1387
1388static void
e315cd28 1389qla24xx_enable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1390{
1391 unsigned long flags = 0;
1392 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1393
1394 spin_lock_irqsave(&ha->hardware_lock, flags);
1395 ha->interrupts_on = 1;
1396 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1397 RD_REG_DWORD(&reg->ictrl);
1398 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1399}
1400
1401static void
e315cd28 1402qla24xx_disable_intrs(struct qla_hw_data *ha)
fd34f556
AV
1403{
1404 unsigned long flags = 0;
1405 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1406
124f85e6
AV
1407 if (IS_NOPOLLING_TYPE(ha))
1408 return;
fd34f556
AV
1409 spin_lock_irqsave(&ha->hardware_lock, flags);
1410 ha->interrupts_on = 0;
1411 WRT_REG_DWORD(&reg->ictrl, 0);
1412 RD_REG_DWORD(&reg->ictrl);
1413 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1414}
1415
1416static struct isp_operations qla2100_isp_ops = {
1417 .pci_config = qla2100_pci_config,
1418 .reset_chip = qla2x00_reset_chip,
1419 .chip_diag = qla2x00_chip_diag,
1420 .config_rings = qla2x00_config_rings,
1421 .reset_adapter = qla2x00_reset_adapter,
1422 .nvram_config = qla2x00_nvram_config,
1423 .update_fw_options = qla2x00_update_fw_options,
1424 .load_risc = qla2x00_load_risc,
1425 .pci_info_str = qla2x00_pci_info_str,
1426 .fw_version_str = qla2x00_fw_version_str,
1427 .intr_handler = qla2100_intr_handler,
1428 .enable_intrs = qla2x00_enable_intrs,
1429 .disable_intrs = qla2x00_disable_intrs,
1430 .abort_command = qla2x00_abort_command,
523ec773
AV
1431 .target_reset = qla2x00_abort_target,
1432 .lun_reset = qla2x00_lun_reset,
fd34f556
AV
1433 .fabric_login = qla2x00_login_fabric,
1434 .fabric_logout = qla2x00_fabric_logout,
1435 .calc_req_entries = qla2x00_calc_iocbs_32,
1436 .build_iocbs = qla2x00_build_scsi_iocbs_32,
1437 .prep_ms_iocb = qla2x00_prep_ms_iocb,
1438 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
1439 .read_nvram = qla2x00_read_nvram_data,
1440 .write_nvram = qla2x00_write_nvram_data,
1441 .fw_dump = qla2100_fw_dump,
1442 .beacon_on = NULL,
1443 .beacon_off = NULL,
1444 .beacon_blink = NULL,
1445 .read_optrom = qla2x00_read_optrom_data,
1446 .write_optrom = qla2x00_write_optrom_data,
1447 .get_flash_version = qla2x00_get_flash_version,
e315cd28 1448 .start_scsi = qla2x00_start_scsi,
a9083016 1449 .abort_isp = qla2x00_abort_isp,
fd34f556
AV
1450};
1451
1452static struct isp_operations qla2300_isp_ops = {
1453 .pci_config = qla2300_pci_config,
1454 .reset_chip = qla2x00_reset_chip,
1455 .chip_diag = qla2x00_chip_diag,
1456 .config_rings = qla2x00_config_rings,
1457 .reset_adapter = qla2x00_reset_adapter,
1458 .nvram_config = qla2x00_nvram_config,
1459 .update_fw_options = qla2x00_update_fw_options,
1460 .load_risc = qla2x00_load_risc,
1461 .pci_info_str = qla2x00_pci_info_str,
1462 .fw_version_str = qla2x00_fw_version_str,
1463 .intr_handler = qla2300_intr_handler,
1464 .enable_intrs = qla2x00_enable_intrs,
1465 .disable_intrs = qla2x00_disable_intrs,
1466 .abort_command = qla2x00_abort_command,
523ec773
AV
1467 .target_reset = qla2x00_abort_target,
1468 .lun_reset = qla2x00_lun_reset,
fd34f556
AV
1469 .fabric_login = qla2x00_login_fabric,
1470 .fabric_logout = qla2x00_fabric_logout,
1471 .calc_req_entries = qla2x00_calc_iocbs_32,
1472 .build_iocbs = qla2x00_build_scsi_iocbs_32,
1473 .prep_ms_iocb = qla2x00_prep_ms_iocb,
1474 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
1475 .read_nvram = qla2x00_read_nvram_data,
1476 .write_nvram = qla2x00_write_nvram_data,
1477 .fw_dump = qla2300_fw_dump,
1478 .beacon_on = qla2x00_beacon_on,
1479 .beacon_off = qla2x00_beacon_off,
1480 .beacon_blink = qla2x00_beacon_blink,
1481 .read_optrom = qla2x00_read_optrom_data,
1482 .write_optrom = qla2x00_write_optrom_data,
1483 .get_flash_version = qla2x00_get_flash_version,
e315cd28 1484 .start_scsi = qla2x00_start_scsi,
a9083016 1485 .abort_isp = qla2x00_abort_isp,
fd34f556
AV
1486};
1487
1488static struct isp_operations qla24xx_isp_ops = {
1489 .pci_config = qla24xx_pci_config,
1490 .reset_chip = qla24xx_reset_chip,
1491 .chip_diag = qla24xx_chip_diag,
1492 .config_rings = qla24xx_config_rings,
1493 .reset_adapter = qla24xx_reset_adapter,
1494 .nvram_config = qla24xx_nvram_config,
1495 .update_fw_options = qla24xx_update_fw_options,
1496 .load_risc = qla24xx_load_risc,
1497 .pci_info_str = qla24xx_pci_info_str,
1498 .fw_version_str = qla24xx_fw_version_str,
1499 .intr_handler = qla24xx_intr_handler,
1500 .enable_intrs = qla24xx_enable_intrs,
1501 .disable_intrs = qla24xx_disable_intrs,
1502 .abort_command = qla24xx_abort_command,
523ec773
AV
1503 .target_reset = qla24xx_abort_target,
1504 .lun_reset = qla24xx_lun_reset,
fd34f556
AV
1505 .fabric_login = qla24xx_login_fabric,
1506 .fabric_logout = qla24xx_fabric_logout,
1507 .calc_req_entries = NULL,
1508 .build_iocbs = NULL,
1509 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1510 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
1511 .read_nvram = qla24xx_read_nvram_data,
1512 .write_nvram = qla24xx_write_nvram_data,
1513 .fw_dump = qla24xx_fw_dump,
1514 .beacon_on = qla24xx_beacon_on,
1515 .beacon_off = qla24xx_beacon_off,
1516 .beacon_blink = qla24xx_beacon_blink,
1517 .read_optrom = qla24xx_read_optrom_data,
1518 .write_optrom = qla24xx_write_optrom_data,
1519 .get_flash_version = qla24xx_get_flash_version,
e315cd28 1520 .start_scsi = qla24xx_start_scsi,
a9083016 1521 .abort_isp = qla2x00_abort_isp,
fd34f556
AV
1522};
1523
c3a2f0df
AV
1524static struct isp_operations qla25xx_isp_ops = {
1525 .pci_config = qla25xx_pci_config,
1526 .reset_chip = qla24xx_reset_chip,
1527 .chip_diag = qla24xx_chip_diag,
1528 .config_rings = qla24xx_config_rings,
1529 .reset_adapter = qla24xx_reset_adapter,
1530 .nvram_config = qla24xx_nvram_config,
1531 .update_fw_options = qla24xx_update_fw_options,
1532 .load_risc = qla24xx_load_risc,
1533 .pci_info_str = qla24xx_pci_info_str,
1534 .fw_version_str = qla24xx_fw_version_str,
1535 .intr_handler = qla24xx_intr_handler,
1536 .enable_intrs = qla24xx_enable_intrs,
1537 .disable_intrs = qla24xx_disable_intrs,
1538 .abort_command = qla24xx_abort_command,
523ec773
AV
1539 .target_reset = qla24xx_abort_target,
1540 .lun_reset = qla24xx_lun_reset,
c3a2f0df
AV
1541 .fabric_login = qla24xx_login_fabric,
1542 .fabric_logout = qla24xx_fabric_logout,
1543 .calc_req_entries = NULL,
1544 .build_iocbs = NULL,
1545 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1546 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
1547 .read_nvram = qla25xx_read_nvram_data,
1548 .write_nvram = qla25xx_write_nvram_data,
1549 .fw_dump = qla25xx_fw_dump,
1550 .beacon_on = qla24xx_beacon_on,
1551 .beacon_off = qla24xx_beacon_off,
1552 .beacon_blink = qla24xx_beacon_blink,
338c9161 1553 .read_optrom = qla25xx_read_optrom_data,
c3a2f0df
AV
1554 .write_optrom = qla24xx_write_optrom_data,
1555 .get_flash_version = qla24xx_get_flash_version,
e315cd28 1556 .start_scsi = qla24xx_start_scsi,
a9083016 1557 .abort_isp = qla2x00_abort_isp,
c3a2f0df
AV
1558};
1559
3a03eb79
AV
1560static struct isp_operations qla81xx_isp_ops = {
1561 .pci_config = qla25xx_pci_config,
1562 .reset_chip = qla24xx_reset_chip,
1563 .chip_diag = qla24xx_chip_diag,
1564 .config_rings = qla24xx_config_rings,
1565 .reset_adapter = qla24xx_reset_adapter,
1566 .nvram_config = qla81xx_nvram_config,
1567 .update_fw_options = qla81xx_update_fw_options,
eaac30be 1568 .load_risc = qla81xx_load_risc,
3a03eb79
AV
1569 .pci_info_str = qla24xx_pci_info_str,
1570 .fw_version_str = qla24xx_fw_version_str,
1571 .intr_handler = qla24xx_intr_handler,
1572 .enable_intrs = qla24xx_enable_intrs,
1573 .disable_intrs = qla24xx_disable_intrs,
1574 .abort_command = qla24xx_abort_command,
1575 .target_reset = qla24xx_abort_target,
1576 .lun_reset = qla24xx_lun_reset,
1577 .fabric_login = qla24xx_login_fabric,
1578 .fabric_logout = qla24xx_fabric_logout,
1579 .calc_req_entries = NULL,
1580 .build_iocbs = NULL,
1581 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1582 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
3d79038f
AV
1583 .read_nvram = NULL,
1584 .write_nvram = NULL,
3a03eb79
AV
1585 .fw_dump = qla81xx_fw_dump,
1586 .beacon_on = qla24xx_beacon_on,
1587 .beacon_off = qla24xx_beacon_off,
1588 .beacon_blink = qla24xx_beacon_blink,
1589 .read_optrom = qla25xx_read_optrom_data,
1590 .write_optrom = qla24xx_write_optrom_data,
1591 .get_flash_version = qla24xx_get_flash_version,
1592 .start_scsi = qla24xx_start_scsi,
a9083016
GM
1593 .abort_isp = qla2x00_abort_isp,
1594};
1595
1596static struct isp_operations qla82xx_isp_ops = {
1597 .pci_config = qla82xx_pci_config,
1598 .reset_chip = qla82xx_reset_chip,
1599 .chip_diag = qla24xx_chip_diag,
1600 .config_rings = qla82xx_config_rings,
1601 .reset_adapter = qla24xx_reset_adapter,
1602 .nvram_config = qla81xx_nvram_config,
1603 .update_fw_options = qla24xx_update_fw_options,
1604 .load_risc = qla82xx_load_risc,
1605 .pci_info_str = qla82xx_pci_info_str,
1606 .fw_version_str = qla24xx_fw_version_str,
1607 .intr_handler = qla82xx_intr_handler,
1608 .enable_intrs = qla82xx_enable_intrs,
1609 .disable_intrs = qla82xx_disable_intrs,
1610 .abort_command = qla24xx_abort_command,
1611 .target_reset = qla24xx_abort_target,
1612 .lun_reset = qla24xx_lun_reset,
1613 .fabric_login = qla24xx_login_fabric,
1614 .fabric_logout = qla24xx_fabric_logout,
1615 .calc_req_entries = NULL,
1616 .build_iocbs = NULL,
1617 .prep_ms_iocb = qla24xx_prep_ms_iocb,
1618 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
1619 .read_nvram = qla24xx_read_nvram_data,
1620 .write_nvram = qla24xx_write_nvram_data,
1621 .fw_dump = qla24xx_fw_dump,
1622 .beacon_on = qla24xx_beacon_on,
1623 .beacon_off = qla24xx_beacon_off,
1624 .beacon_blink = qla24xx_beacon_blink,
1625 .read_optrom = qla82xx_read_optrom_data,
1626 .write_optrom = qla82xx_write_optrom_data,
1627 .get_flash_version = qla24xx_get_flash_version,
1628 .start_scsi = qla82xx_start_scsi,
1629 .abort_isp = qla82xx_abort_isp,
3a03eb79
AV
1630};
1631
ea5b6382 1632static inline void
e315cd28 1633qla2x00_set_isp_flags(struct qla_hw_data *ha)
ea5b6382
AV
1634{
1635 ha->device_type = DT_EXTENDED_IDS;
1636 switch (ha->pdev->device) {
1637 case PCI_DEVICE_ID_QLOGIC_ISP2100:
1638 ha->device_type |= DT_ISP2100;
1639 ha->device_type &= ~DT_EXTENDED_IDS;
441d1072 1640 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
ea5b6382
AV
1641 break;
1642 case PCI_DEVICE_ID_QLOGIC_ISP2200:
1643 ha->device_type |= DT_ISP2200;
1644 ha->device_type &= ~DT_EXTENDED_IDS;
441d1072 1645 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
ea5b6382
AV
1646 break;
1647 case PCI_DEVICE_ID_QLOGIC_ISP2300:
1648 ha->device_type |= DT_ISP2300;
4a59f71d 1649 ha->device_type |= DT_ZIO_SUPPORTED;
441d1072 1650 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1651 break;
1652 case PCI_DEVICE_ID_QLOGIC_ISP2312:
1653 ha->device_type |= DT_ISP2312;
4a59f71d 1654 ha->device_type |= DT_ZIO_SUPPORTED;
441d1072 1655 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1656 break;
1657 case PCI_DEVICE_ID_QLOGIC_ISP2322:
1658 ha->device_type |= DT_ISP2322;
4a59f71d 1659 ha->device_type |= DT_ZIO_SUPPORTED;
ea5b6382
AV
1660 if (ha->pdev->subsystem_vendor == 0x1028 &&
1661 ha->pdev->subsystem_device == 0x0170)
1662 ha->device_type |= DT_OEM_001;
441d1072 1663 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1664 break;
1665 case PCI_DEVICE_ID_QLOGIC_ISP6312:
1666 ha->device_type |= DT_ISP6312;
441d1072 1667 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1668 break;
1669 case PCI_DEVICE_ID_QLOGIC_ISP6322:
1670 ha->device_type |= DT_ISP6322;
441d1072 1671 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
ea5b6382
AV
1672 break;
1673 case PCI_DEVICE_ID_QLOGIC_ISP2422:
1674 ha->device_type |= DT_ISP2422;
4a59f71d 1675 ha->device_type |= DT_ZIO_SUPPORTED;
e428924c 1676 ha->device_type |= DT_FWI2;
c76f2c01 1677 ha->device_type |= DT_IIDMA;
441d1072 1678 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382
AV
1679 break;
1680 case PCI_DEVICE_ID_QLOGIC_ISP2432:
1681 ha->device_type |= DT_ISP2432;
4a59f71d 1682 ha->device_type |= DT_ZIO_SUPPORTED;
e428924c 1683 ha->device_type |= DT_FWI2;
c76f2c01 1684 ha->device_type |= DT_IIDMA;
441d1072 1685 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1686 break;
4d4df193
HK
1687 case PCI_DEVICE_ID_QLOGIC_ISP8432:
1688 ha->device_type |= DT_ISP8432;
1689 ha->device_type |= DT_ZIO_SUPPORTED;
1690 ha->device_type |= DT_FWI2;
1691 ha->device_type |= DT_IIDMA;
1692 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1693 break;
044cc6c8
AV
1694 case PCI_DEVICE_ID_QLOGIC_ISP5422:
1695 ha->device_type |= DT_ISP5422;
e428924c 1696 ha->device_type |= DT_FWI2;
441d1072 1697 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1698 break;
044cc6c8
AV
1699 case PCI_DEVICE_ID_QLOGIC_ISP5432:
1700 ha->device_type |= DT_ISP5432;
e428924c 1701 ha->device_type |= DT_FWI2;
441d1072 1702 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1703 break;
c3a2f0df
AV
1704 case PCI_DEVICE_ID_QLOGIC_ISP2532:
1705 ha->device_type |= DT_ISP2532;
1706 ha->device_type |= DT_ZIO_SUPPORTED;
1707 ha->device_type |= DT_FWI2;
1708 ha->device_type |= DT_IIDMA;
441d1072 1709 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
ea5b6382 1710 break;
3a03eb79
AV
1711 case PCI_DEVICE_ID_QLOGIC_ISP8001:
1712 ha->device_type |= DT_ISP8001;
1713 ha->device_type |= DT_ZIO_SUPPORTED;
1714 ha->device_type |= DT_FWI2;
1715 ha->device_type |= DT_IIDMA;
1716 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1717 break;
a9083016
GM
1718 case PCI_DEVICE_ID_QLOGIC_ISP8021:
1719 ha->device_type |= DT_ISP8021;
1720 ha->device_type |= DT_ZIO_SUPPORTED;
1721 ha->device_type |= DT_FWI2;
1722 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1723 /* Initialize 82XX ISP flags */
1724 qla82xx_init_flags(ha);
1725 break;
ea5b6382 1726 }
e5b68a61 1727
a9083016
GM
1728 if (IS_QLA82XX(ha))
1729 ha->port_no = !(ha->portnum & 1);
1730 else
1731 /* Get adapter physical port no from interrupt pin register. */
1732 pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
1733
e5b68a61
AC
1734 if (ha->port_no & 1)
1735 ha->flags.port0 = 1;
1736 else
1737 ha->flags.port0 = 0;
ea5b6382
AV
1738}
1739
1da177e4 1740static int
e315cd28 1741qla2x00_iospace_config(struct qla_hw_data *ha)
1da177e4 1742{
3776541d 1743 resource_size_t pio;
73208dfd 1744 uint16_t msix;
68ca949c 1745 int cpus;
1da177e4 1746
a9083016
GM
1747 if (IS_QLA82XX(ha))
1748 return qla82xx_iospace_config(ha);
1749
285d0321
AV
1750 if (pci_request_selected_regions(ha->pdev, ha->bars,
1751 QLA2XXX_DRIVER_NAME)) {
1752 qla_printk(KERN_WARNING, ha,
1753 "Failed to reserve PIO/MMIO regions (%s)\n",
1754 pci_name(ha->pdev));
1755
1756 goto iospace_error_exit;
1757 }
1758 if (!(ha->bars & 1))
1759 goto skip_pio;
1760
1da177e4
LT
1761 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1762 pio = pci_resource_start(ha->pdev, 0);
3776541d
AV
1763 if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1764 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1da177e4
LT
1765 qla_printk(KERN_WARNING, ha,
1766 "Invalid PCI I/O region size (%s)...\n",
1767 pci_name(ha->pdev));
1768 pio = 0;
1769 }
1770 } else {
1771 qla_printk(KERN_WARNING, ha,
1772 "region #0 not a PIO resource (%s)...\n",
1773 pci_name(ha->pdev));
1774 pio = 0;
1775 }
285d0321 1776 ha->pio_address = pio;
1da177e4 1777
285d0321 1778skip_pio:
1da177e4 1779 /* Use MMIO operations for all accesses. */
3776541d 1780 if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1da177e4 1781 qla_printk(KERN_ERR, ha,
3776541d 1782 "region #1 not an MMIO resource (%s), aborting\n",
1da177e4
LT
1783 pci_name(ha->pdev));
1784 goto iospace_error_exit;
1785 }
3776541d 1786 if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1da177e4
LT
1787 qla_printk(KERN_ERR, ha,
1788 "Invalid PCI mem region size (%s), aborting\n",
1789 pci_name(ha->pdev));
1790 goto iospace_error_exit;
1791 }
1792
3776541d 1793 ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1da177e4
LT
1794 if (!ha->iobase) {
1795 qla_printk(KERN_ERR, ha,
1796 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1797
1798 goto iospace_error_exit;
1799 }
1800
73208dfd 1801 /* Determine queue resources */
2afa19a9 1802 ha->max_req_queues = ha->max_rsp_queues = 1;
d84a47c2
MH
1803 if ((ql2xmaxqueues <= 1 && !ql2xmultique_tag) ||
1804 (ql2xmaxqueues > 1 && ql2xmultique_tag) ||
2afa19a9 1805 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
17d98630 1806 goto mqiobase_exit;
d84a47c2 1807
17d98630
AC
1808 ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1809 pci_resource_len(ha->pdev, 3));
1810 if (ha->mqiobase) {
1811 /* Read MSIX vector size of the board */
1812 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1813 ha->msix_count = msix;
68ca949c
AC
1814 /* Max queues are bounded by available msix vectors */
1815 /* queue 0 uses two msix vectors */
1816 if (ql2xmultique_tag) {
1817 cpus = num_online_cpus();
27dc9c5a 1818 ha->max_rsp_queues = (ha->msix_count - 1 > cpus) ?
68ca949c
AC
1819 (cpus + 1) : (ha->msix_count - 1);
1820 ha->max_req_queues = 2;
1821 } else if (ql2xmaxqueues > 1) {
2afa19a9
AC
1822 ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1823 QLA_MQ_SIZE : ql2xmaxqueues;
1824 DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1825 " of request queues:%d\n", ha->max_req_queues));
1826 }
68ca949c
AC
1827 qla_printk(KERN_INFO, ha,
1828 "MSI-X vector count: %d\n", msix);
2afa19a9
AC
1829 } else
1830 qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
17d98630
AC
1831
1832mqiobase_exit:
2afa19a9 1833 ha->msix_count = ha->max_rsp_queues + 1;
1da177e4
LT
1834 return (0);
1835
1836iospace_error_exit:
1837 return (-ENOMEM);
1838}
1839
1e99e33a
AV
1840static void
1841qla2xxx_scan_start(struct Scsi_Host *shost)
1842{
e315cd28 1843 scsi_qla_host_t *vha = shost_priv(shost);
1e99e33a 1844
cbc8eb67
AV
1845 if (vha->hw->flags.running_gold_fw)
1846 return;
1847
e315cd28
AC
1848 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1849 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1850 set_bit(RSCN_UPDATE, &vha->dpc_flags);
1851 set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1e99e33a
AV
1852}
1853
1854static int
1855qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1856{
e315cd28 1857 scsi_qla_host_t *vha = shost_priv(shost);
1e99e33a 1858
e315cd28 1859 if (!vha->host)
1e99e33a 1860 return 1;
e315cd28 1861 if (time > vha->hw->loop_reset_delay * HZ)
1e99e33a
AV
1862 return 1;
1863
e315cd28 1864 return atomic_read(&vha->loop_state) == LOOP_READY;
1e99e33a
AV
1865}
1866
1da177e4
LT
1867/*
1868 * PCI driver interface
1869 */
7ee61397
AV
1870static int __devinit
1871qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1da177e4 1872{
a1541d5a 1873 int ret = -ENODEV;
1da177e4 1874 struct Scsi_Host *host;
e315cd28
AC
1875 scsi_qla_host_t *base_vha = NULL;
1876 struct qla_hw_data *ha;
29856e28 1877 char pci_info[30];
1da177e4 1878 char fw_str[30];
5433383e 1879 struct scsi_host_template *sht;
c51da4ec 1880 int bars, max_id, mem_only = 0;
e315cd28 1881 uint16_t req_length = 0, rsp_length = 0;
73208dfd
AC
1882 struct req_que *req = NULL;
1883 struct rsp_que *rsp = NULL;
1da177e4 1884
285d0321 1885 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
a5326f86 1886 sht = &qla2xxx_driver_template;
5433383e 1887 if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
8bc69e7d 1888 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
4d4df193 1889 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
8bc69e7d 1890 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
c3a2f0df 1891 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
3a03eb79 1892 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
a9083016
GM
1893 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
1894 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021) {
285d0321 1895 bars = pci_select_bars(pdev, IORESOURCE_MEM);
09483916 1896 mem_only = 1;
285d0321
AV
1897 }
1898
09483916
BH
1899 if (mem_only) {
1900 if (pci_enable_device_mem(pdev))
1901 goto probe_out;
1902 } else {
1903 if (pci_enable_device(pdev))
1904 goto probe_out;
1905 }
285d0321 1906
0927678f
JB
1907 /* This may fail but that's ok */
1908 pci_enable_pcie_error_reporting(pdev);
285d0321 1909
e315cd28
AC
1910 ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1911 if (!ha) {
1912 DEBUG(printk("Unable to allocate memory for ha\n"));
1913 goto probe_out;
1da177e4 1914 }
e315cd28 1915 ha->pdev = pdev;
1da177e4
LT
1916
1917 /* Clear our data area */
285d0321 1918 ha->bars = bars;
09483916 1919 ha->mem_only = mem_only;
df4bf0bb 1920 spin_lock_init(&ha->hardware_lock);
1da177e4 1921
ea5b6382
AV
1922 /* Set ISP-type information. */
1923 qla2x00_set_isp_flags(ha);
ca79cf66
DG
1924
1925 /* Set EEH reset type to fundamental if required by hba */
1926 if ( IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha)) {
1927 pdev->needs_freset = 1;
ca79cf66
DG
1928 }
1929
1da177e4
LT
1930 /* Configure PCI I/O space */
1931 ret = qla2x00_iospace_config(ha);
a1541d5a 1932 if (ret)
e315cd28 1933 goto probe_hw_failed;
1da177e4 1934
1da177e4 1935 qla_printk(KERN_INFO, ha,
5433383e
AV
1936 "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1937 ha->iobase);
1da177e4 1938
1da177e4 1939 ha->prev_topology = 0;
fca29703 1940 ha->init_cb_size = sizeof(init_cb_t);
d8b45213 1941 ha->link_data_rate = PORT_SPEED_UNKNOWN;
854165f4 1942 ha->optrom_size = OPTROM_SIZE_2300;
1da177e4 1943
abbd8870 1944 /* Assign ISP specific operations. */
e315cd28 1945 max_id = MAX_TARGETS_2200;
1da177e4 1946 if (IS_QLA2100(ha)) {
e315cd28 1947 max_id = MAX_TARGETS_2100;
1da177e4 1948 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
e315cd28
AC
1949 req_length = REQUEST_ENTRY_CNT_2100;
1950 rsp_length = RESPONSE_ENTRY_CNT_2100;
1951 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
abbd8870 1952 ha->gid_list_info_size = 4;
3a03eb79
AV
1953 ha->flash_conf_off = ~0;
1954 ha->flash_data_off = ~0;
1955 ha->nvram_conf_off = ~0;
1956 ha->nvram_data_off = ~0;
fd34f556 1957 ha->isp_ops = &qla2100_isp_ops;
1da177e4 1958 } else if (IS_QLA2200(ha)) {
1da177e4 1959 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1960 req_length = REQUEST_ENTRY_CNT_2200;
1961 rsp_length = RESPONSE_ENTRY_CNT_2100;
1962 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
abbd8870 1963 ha->gid_list_info_size = 4;
3a03eb79
AV
1964 ha->flash_conf_off = ~0;
1965 ha->flash_data_off = ~0;
1966 ha->nvram_conf_off = ~0;
1967 ha->nvram_data_off = ~0;
fd34f556 1968 ha->isp_ops = &qla2100_isp_ops;
fca29703 1969 } else if (IS_QLA23XX(ha)) {
1da177e4 1970 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1971 req_length = REQUEST_ENTRY_CNT_2200;
1972 rsp_length = RESPONSE_ENTRY_CNT_2300;
1973 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
abbd8870 1974 ha->gid_list_info_size = 6;
854165f4
AV
1975 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1976 ha->optrom_size = OPTROM_SIZE_2322;
3a03eb79
AV
1977 ha->flash_conf_off = ~0;
1978 ha->flash_data_off = ~0;
1979 ha->nvram_conf_off = ~0;
1980 ha->nvram_data_off = ~0;
fd34f556 1981 ha->isp_ops = &qla2300_isp_ops;
4d4df193 1982 } else if (IS_QLA24XX_TYPE(ha)) {
fca29703 1983 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1984 req_length = REQUEST_ENTRY_CNT_24XX;
1985 rsp_length = RESPONSE_ENTRY_CNT_2300;
1986 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2c3dfe3f 1987 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
fca29703 1988 ha->gid_list_info_size = 8;
854165f4 1989 ha->optrom_size = OPTROM_SIZE_24XX;
73208dfd 1990 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
fd34f556 1991 ha->isp_ops = &qla24xx_isp_ops;
3a03eb79
AV
1992 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1993 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1994 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1995 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
c3a2f0df 1996 } else if (IS_QLA25XX(ha)) {
c3a2f0df 1997 ha->mbx_count = MAILBOX_REGISTER_COUNT;
e315cd28
AC
1998 req_length = REQUEST_ENTRY_CNT_24XX;
1999 rsp_length = RESPONSE_ENTRY_CNT_2300;
2000 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
c3a2f0df 2001 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
c3a2f0df
AV
2002 ha->gid_list_info_size = 8;
2003 ha->optrom_size = OPTROM_SIZE_25XX;
73208dfd 2004 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
c3a2f0df 2005 ha->isp_ops = &qla25xx_isp_ops;
3a03eb79
AV
2006 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2007 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2008 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2009 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2010 } else if (IS_QLA81XX(ha)) {
2011 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2012 req_length = REQUEST_ENTRY_CNT_24XX;
2013 rsp_length = RESPONSE_ENTRY_CNT_2300;
2014 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2015 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2016 ha->gid_list_info_size = 8;
2017 ha->optrom_size = OPTROM_SIZE_81XX;
40859ae5 2018 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3a03eb79
AV
2019 ha->isp_ops = &qla81xx_isp_ops;
2020 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2021 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2022 ha->nvram_conf_off = ~0;
2023 ha->nvram_data_off = ~0;
a9083016
GM
2024 } else if (IS_QLA82XX(ha)) {
2025 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2026 req_length = REQUEST_ENTRY_CNT_82XX;
2027 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2028 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2029 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2030 ha->gid_list_info_size = 8;
2031 ha->optrom_size = OPTROM_SIZE_82XX;
2032 ha->isp_ops = &qla82xx_isp_ops;
2033 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2034 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2035 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2036 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1da177e4 2037 }
1da177e4 2038
6c2f527c 2039 mutex_init(&ha->vport_lock);
0b05a1f0
MB
2040 init_completion(&ha->mbx_cmd_comp);
2041 complete(&ha->mbx_cmd_comp);
2042 init_completion(&ha->mbx_intr_comp);
1da177e4 2043
2c3dfe3f 2044 set_bit(0, (unsigned long *) ha->vp_idx_map);
1da177e4 2045
53303c42 2046 qla2x00_config_dma_addressing(ha);
73208dfd 2047 ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
e315cd28 2048 if (!ret) {
1da177e4
LT
2049 qla_printk(KERN_WARNING, ha,
2050 "[ERROR] Failed to allocate memory for adapter\n");
2051
e315cd28
AC
2052 goto probe_hw_failed;
2053 }
2054
73208dfd 2055 req->max_q_depth = MAX_Q_DEPTH;
e315cd28 2056 if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
73208dfd
AC
2057 req->max_q_depth = ql2xmaxqdepth;
2058
e315cd28
AC
2059
2060 base_vha = qla2x00_create_host(sht, ha);
2061 if (!base_vha) {
2062 qla_printk(KERN_WARNING, ha,
2063 "[ERROR] Failed to allocate memory for scsi_host\n");
2064
a1541d5a 2065 ret = -ENOMEM;
6e9f21f3 2066 qla2x00_mem_free(ha);
2afa19a9
AC
2067 qla2x00_free_req_que(ha, req);
2068 qla2x00_free_rsp_que(ha, rsp);
e315cd28 2069 goto probe_hw_failed;
1da177e4
LT
2070 }
2071
e315cd28
AC
2072 pci_set_drvdata(pdev, base_vha);
2073
e315cd28 2074 host = base_vha->host;
2afa19a9 2075 base_vha->req = req;
73208dfd
AC
2076 host->can_queue = req->length + 128;
2077 if (IS_QLA2XXX_MIDTYPE(ha))
e315cd28 2078 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
73208dfd 2079 else
e315cd28
AC
2080 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
2081 base_vha->vp_idx;
e315cd28
AC
2082 if (IS_QLA2100(ha))
2083 host->sg_tablesize = 32;
2084 host->max_id = max_id;
2085 host->this_id = 255;
2086 host->cmd_per_lun = 3;
2087 host->unique_id = host->host_no;
2088 host->max_cmd_len = MAX_CMDSZ;
2089 host->max_channel = MAX_BUSES - 1;
2090 host->max_lun = MAX_LUNS;
2091 host->transportt = qla2xxx_transport_template;
9a069e19 2092 sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
e315cd28 2093
73208dfd
AC
2094 /* Set up the irqs */
2095 ret = qla2x00_request_irqs(ha, rsp);
2096 if (ret)
6e9f21f3 2097 goto probe_init_failed;
90a86fc0
JC
2098
2099 pci_save_state(pdev);
2100
73208dfd 2101 /* Alloc arrays of request and response ring ptrs */
7163ea81 2102que_init:
73208dfd
AC
2103 if (!qla2x00_alloc_queues(ha)) {
2104 qla_printk(KERN_WARNING, ha,
2105 "[ERROR] Failed to allocate memory for queue"
2106 " pointers\n");
6e9f21f3 2107 goto probe_init_failed;
73208dfd 2108 }
a9083016 2109
73208dfd
AC
2110 ha->rsp_q_map[0] = rsp;
2111 ha->req_q_map[0] = req;
2afa19a9
AC
2112 rsp->req = req;
2113 req->rsp = rsp;
2114 set_bit(0, ha->req_qid_map);
2115 set_bit(0, ha->rsp_qid_map);
08029990
AV
2116 /* FWI2-capable only. */
2117 req->req_q_in = &ha->iobase->isp24.req_q_in;
2118 req->req_q_out = &ha->iobase->isp24.req_q_out;
2119 rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
2120 rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
17d98630 2121 if (ha->mqenable) {
08029990
AV
2122 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
2123 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
2124 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
2125 rsp->rsp_q_out = &ha->mqiobase->isp25mq.rsp_q_out;
17d98630
AC
2126 }
2127
a9083016
GM
2128 if (IS_QLA82XX(ha)) {
2129 req->req_q_out = &ha->iobase->isp82.req_q_out[0];
2130 rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
2131 rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
2132 }
2133
e315cd28 2134 if (qla2x00_initialize_adapter(base_vha)) {
1da177e4
LT
2135 qla_printk(KERN_WARNING, ha,
2136 "Failed to initialize adapter\n");
2137
2138 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
2139 "Adapter flags %x.\n",
e315cd28 2140 base_vha->host_no, base_vha->device_flags));
1da177e4 2141
a9083016
GM
2142 if (IS_QLA82XX(ha)) {
2143 qla82xx_idc_lock(ha);
2144 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2145 QLA82XX_DEV_FAILED);
2146 qla82xx_idc_unlock(ha);
2147 qla_printk(KERN_INFO, ha, "HW State: FAILED\n");
2148 }
2149
a1541d5a 2150 ret = -ENODEV;
1da177e4
LT
2151 goto probe_failed;
2152 }
2153
7163ea81
AC
2154 if (ha->mqenable) {
2155 if (qla25xx_setup_mode(base_vha)) {
68ca949c
AC
2156 qla_printk(KERN_WARNING, ha,
2157 "Can't create queues, falling back to single"
2158 " queue mode\n");
7163ea81
AC
2159 goto que_init;
2160 }
2161 }
68ca949c 2162
cbc8eb67
AV
2163 if (ha->flags.running_gold_fw)
2164 goto skip_dpc;
2165
1da177e4
LT
2166 /*
2167 * Startup the kernel thread for this host adapter
2168 */
39a11240 2169 ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
e315cd28 2170 "%s_dpc", base_vha->host_str);
39a11240 2171 if (IS_ERR(ha->dpc_thread)) {
1da177e4
LT
2172 qla_printk(KERN_WARNING, ha,
2173 "Unable to start DPC thread!\n");
39a11240 2174 ret = PTR_ERR(ha->dpc_thread);
1da177e4
LT
2175 goto probe_failed;
2176 }
1da177e4 2177
cbc8eb67 2178skip_dpc:
e315cd28
AC
2179 list_add_tail(&base_vha->list, &ha->vp_list);
2180 base_vha->host->irq = ha->pdev->irq;
1da177e4
LT
2181
2182 /* Initialized the timer */
e315cd28 2183 qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
1da177e4
LT
2184
2185 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
e315cd28 2186 base_vha->host_no, ha));
d19044c3 2187
a9083016
GM
2188 ha->isp_ops->enable_intrs(ha);
2189
a1541d5a
AV
2190 ret = scsi_add_host(host, &pdev->dev);
2191 if (ret)
2192 goto probe_failed;
2193
1486400f
MR
2194 base_vha->flags.init_done = 1;
2195 base_vha->flags.online = 1;
2196
1e99e33a
AV
2197 scsi_scan_host(host);
2198
e315cd28 2199 qla2x00_alloc_sysfs_attr(base_vha);
a1541d5a 2200
e315cd28 2201 qla2x00_init_host_attr(base_vha);
a1541d5a 2202
e315cd28 2203 qla2x00_dfs_setup(base_vha);
df613b96 2204
1da177e4
LT
2205 qla_printk(KERN_INFO, ha, "\n"
2206 " QLogic Fibre Channel HBA Driver: %s\n"
2207 " QLogic %s - %s\n"
5433383e
AV
2208 " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
2209 qla2x00_version_str, ha->model_number,
e315cd28
AC
2210 ha->model_desc ? ha->model_desc : "", pdev->device,
2211 ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
2212 ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
2213 ha->isp_ops->fw_version_str(base_vha, fw_str));
1da177e4 2214
1da177e4
LT
2215 return 0;
2216
6e9f21f3 2217probe_init_failed:
2afa19a9
AC
2218 qla2x00_free_req_que(ha, req);
2219 qla2x00_free_rsp_que(ha, rsp);
2220 ha->max_req_queues = ha->max_rsp_queues = 0;
6e9f21f3 2221
1da177e4 2222probe_failed:
b9978769
AV
2223 if (base_vha->timer_active)
2224 qla2x00_stop_timer(base_vha);
2225 base_vha->flags.online = 0;
2226 if (ha->dpc_thread) {
2227 struct task_struct *t = ha->dpc_thread;
2228
2229 ha->dpc_thread = NULL;
2230 kthread_stop(t);
2231 }
2232
e315cd28 2233 qla2x00_free_device(base_vha);
1da177e4 2234
e315cd28 2235 scsi_host_put(base_vha->host);
1da177e4 2236
e315cd28 2237probe_hw_failed:
a9083016
GM
2238 if (IS_QLA82XX(ha)) {
2239 qla82xx_idc_lock(ha);
2240 qla82xx_clear_drv_active(ha);
2241 qla82xx_idc_unlock(ha);
2242 iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2243 if (!ql2xdbwr)
2244 iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2245 } else {
2246 if (ha->iobase)
2247 iounmap(ha->iobase);
2248 }
e315cd28
AC
2249 pci_release_selected_regions(ha->pdev, ha->bars);
2250 kfree(ha);
2251 ha = NULL;
1da177e4 2252
a1541d5a 2253probe_out:
e315cd28 2254 pci_disable_device(pdev);
a1541d5a 2255 return ret;
1da177e4 2256}
1da177e4 2257
4c993f76 2258static void
7ee61397 2259qla2x00_remove_one(struct pci_dev *pdev)
1da177e4 2260{
e315cd28
AC
2261 scsi_qla_host_t *base_vha, *vha, *temp;
2262 struct qla_hw_data *ha;
2263
2264 base_vha = pci_get_drvdata(pdev);
2265 ha = base_vha->hw;
2266
2267 list_for_each_entry_safe(vha, temp, &ha->vp_list, list) {
2268 if (vha && vha->fc_vport)
2269 fc_vport_terminate(vha->fc_vport);
2270 }
1da177e4 2271
e315cd28 2272 set_bit(UNLOADING, &base_vha->dpc_flags);
1da177e4 2273
b9978769
AV
2274 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
2275
e315cd28 2276 qla2x00_dfs_remove(base_vha);
c795c1e4 2277
e315cd28 2278 qla84xx_put_chip(base_vha);
c795c1e4 2279
b9978769
AV
2280 /* Disable timer */
2281 if (base_vha->timer_active)
2282 qla2x00_stop_timer(base_vha);
2283
2284 base_vha->flags.online = 0;
2285
68ca949c
AC
2286 /* Flush the work queue and remove it */
2287 if (ha->wq) {
2288 flush_workqueue(ha->wq);
2289 destroy_workqueue(ha->wq);
2290 ha->wq = NULL;
2291 }
2292
b9978769
AV
2293 /* Kill the kernel thread for this host */
2294 if (ha->dpc_thread) {
2295 struct task_struct *t = ha->dpc_thread;
2296
2297 /*
2298 * qla2xxx_wake_dpc checks for ->dpc_thread
2299 * so we need to zero it out.
2300 */
2301 ha->dpc_thread = NULL;
2302 kthread_stop(t);
2303 }
2304
e315cd28 2305 qla2x00_free_sysfs_attr(base_vha);
df613b96 2306
e315cd28 2307 fc_remove_host(base_vha->host);
4d4df193 2308
e315cd28 2309 scsi_remove_host(base_vha->host);
1da177e4 2310
e315cd28 2311 qla2x00_free_device(base_vha);
bdf79621 2312
e315cd28 2313 scsi_host_put(base_vha->host);
1da177e4 2314
a9083016
GM
2315 if (IS_QLA82XX(ha)) {
2316 iounmap((device_reg_t __iomem *)ha->nx_pcibase);
2317 if (!ql2xdbwr)
2318 iounmap((device_reg_t __iomem *)ha->nxdb_wr_ptr);
2319 } else {
2320 if (ha->iobase)
2321 iounmap(ha->iobase);
1da177e4 2322
a9083016
GM
2323 if (ha->mqiobase)
2324 iounmap(ha->mqiobase);
2325 }
73208dfd 2326
e315cd28
AC
2327 pci_release_selected_regions(ha->pdev, ha->bars);
2328 kfree(ha);
2329 ha = NULL;
1da177e4 2330
90a86fc0
JC
2331 pci_disable_pcie_error_reporting(pdev);
2332
665db93b 2333 pci_disable_device(pdev);
1da177e4
LT
2334 pci_set_drvdata(pdev, NULL);
2335}
1da177e4
LT
2336
2337static void
e315cd28 2338qla2x00_free_device(scsi_qla_host_t *vha)
1da177e4 2339{
e315cd28 2340 struct qla_hw_data *ha = vha->hw;
1da177e4 2341
85880801
AV
2342 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
2343
2344 /* Disable timer */
2345 if (vha->timer_active)
2346 qla2x00_stop_timer(vha);
2347
2348 /* Kill the kernel thread for this host */
2349 if (ha->dpc_thread) {
2350 struct task_struct *t = ha->dpc_thread;
2351
2352 /*
2353 * qla2xxx_wake_dpc checks for ->dpc_thread
2354 * so we need to zero it out.
2355 */
2356 ha->dpc_thread = NULL;
2357 kthread_stop(t);
2358 }
2359
2afa19a9
AC
2360 qla25xx_delete_queues(vha);
2361
df613b96 2362 if (ha->flags.fce_enabled)
e315cd28 2363 qla2x00_disable_fce_trace(vha, NULL, NULL);
df613b96 2364
a7a167bf 2365 if (ha->eft)
e315cd28 2366 qla2x00_disable_eft_trace(vha);
a7a167bf 2367
f6ef3b18 2368 /* Stop currently executing firmware. */
e315cd28 2369 qla2x00_try_to_stop_firmware(vha);
1da177e4 2370
85880801
AV
2371 vha->flags.online = 0;
2372
f6ef3b18 2373 /* turn-off interrupts on the card */
a9083016
GM
2374 if (ha->interrupts_on) {
2375 vha->flags.init_done = 0;
fd34f556 2376 ha->isp_ops->disable_intrs(ha);
a9083016 2377 }
f6ef3b18 2378
e315cd28 2379 qla2x00_free_irqs(vha);
1da177e4 2380
e315cd28 2381 qla2x00_mem_free(ha);
73208dfd
AC
2382
2383 qla2x00_free_queues(ha);
1da177e4
LT
2384}
2385
d97994dc 2386static inline void
e315cd28 2387qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
d97994dc
AV
2388 int defer)
2389{
d97994dc 2390 struct fc_rport *rport;
67becc00 2391 scsi_qla_host_t *base_vha;
d97994dc
AV
2392
2393 if (!fcport->rport)
2394 return;
2395
2396 rport = fcport->rport;
2397 if (defer) {
67becc00 2398 base_vha = pci_get_drvdata(vha->hw->pdev);
e315cd28 2399 spin_lock_irq(vha->host->host_lock);
d97994dc 2400 fcport->drport = rport;
e315cd28 2401 spin_unlock_irq(vha->host->host_lock);
67becc00
AV
2402 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2403 qla2xxx_wake_dpc(base_vha);
5f3a9a20 2404 } else
d97994dc 2405 fc_remote_port_delete(rport);
d97994dc
AV
2406}
2407
1da177e4
LT
2408/*
2409 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2410 *
2411 * Input: ha = adapter block pointer. fcport = port structure pointer.
2412 *
2413 * Return: None.
2414 *
2415 * Context:
2416 */
e315cd28 2417void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
d97994dc 2418 int do_login, int defer)
1da177e4 2419{
2c3dfe3f 2420 if (atomic_read(&fcport->state) == FCS_ONLINE &&
e315cd28
AC
2421 vha->vp_idx == fcport->vp_idx) {
2422 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2423 qla2x00_schedule_rport_del(vha, fcport, defer);
2424 }
fa2a1ce5 2425 /*
1da177e4
LT
2426 * We may need to retry the login, so don't change the state of the
2427 * port but do the retries.
2428 */
2429 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2430 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2431
2432 if (!do_login)
2433 return;
2434
2435 if (fcport->login_retry == 0) {
e315cd28
AC
2436 fcport->login_retry = vha->hw->login_retry_count;
2437 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1da177e4
LT
2438
2439 DEBUG(printk("scsi(%ld): Port login retry: "
2440 "%02x%02x%02x%02x%02x%02x%02x%02x, "
2441 "id = 0x%04x retry cnt=%d\n",
e315cd28 2442 vha->host_no,
1da177e4
LT
2443 fcport->port_name[0],
2444 fcport->port_name[1],
2445 fcport->port_name[2],
2446 fcport->port_name[3],
2447 fcport->port_name[4],
2448 fcport->port_name[5],
2449 fcport->port_name[6],
2450 fcport->port_name[7],
2451 fcport->loop_id,
2452 fcport->login_retry));
2453 }
2454}
2455
2456/*
2457 * qla2x00_mark_all_devices_lost
2458 * Updates fcport state when device goes offline.
2459 *
2460 * Input:
2461 * ha = adapter block pointer.
2462 * fcport = port structure pointer.
2463 *
2464 * Return:
2465 * None.
2466 *
2467 * Context:
2468 */
2469void
e315cd28 2470qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
1da177e4
LT
2471{
2472 fc_port_t *fcport;
2473
e315cd28 2474 list_for_each_entry(fcport, &vha->vp_fcports, list) {
0d6e61bc 2475 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vp_idx)
1da177e4 2476 continue;
0d6e61bc 2477
1da177e4
LT
2478 /*
2479 * No point in marking the device as lost, if the device is
2480 * already DEAD.
2481 */
2482 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2483 continue;
e315cd28 2484 if (atomic_read(&fcport->state) == FCS_ONLINE) {
0d6e61bc
AV
2485 if (defer)
2486 qla2x00_schedule_rport_del(vha, fcport, defer);
2487 else if (vha->vp_idx == fcport->vp_idx)
2488 qla2x00_schedule_rport_del(vha, fcport, defer);
2489 }
2490 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1da177e4
LT
2491 }
2492}
2493
2494/*
2495* qla2x00_mem_alloc
2496* Allocates adapter memory.
2497*
2498* Returns:
2499* 0 = success.
e8711085 2500* !0 = failure.
1da177e4 2501*/
e8711085 2502static int
73208dfd
AC
2503qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2504 struct req_que **req, struct rsp_que **rsp)
1da177e4
LT
2505{
2506 char name[16];
1da177e4 2507
e8711085 2508 ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
e315cd28 2509 &ha->init_cb_dma, GFP_KERNEL);
e8711085 2510 if (!ha->init_cb)
e315cd28 2511 goto fail;
e8711085 2512
e315cd28
AC
2513 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2514 &ha->gid_list_dma, GFP_KERNEL);
2515 if (!ha->gid_list)
e8711085 2516 goto fail_free_init_cb;
1da177e4 2517
e8711085
AV
2518 ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2519 if (!ha->srb_mempool)
e315cd28 2520 goto fail_free_gid_list;
e8711085 2521
a9083016
GM
2522 if (IS_QLA82XX(ha)) {
2523 /* Allocate cache for CT6 Ctx. */
2524 if (!ctx_cachep) {
2525 ctx_cachep = kmem_cache_create("qla2xxx_ctx",
2526 sizeof(struct ct6_dsd), 0,
2527 SLAB_HWCACHE_ALIGN, NULL);
2528 if (!ctx_cachep)
2529 goto fail_free_gid_list;
2530 }
2531 ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
2532 ctx_cachep);
2533 if (!ha->ctx_mempool)
2534 goto fail_free_srb_mempool;
2535 }
2536
e8711085
AV
2537 /* Get memory for cached NVRAM */
2538 ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2539 if (!ha->nvram)
a9083016 2540 goto fail_free_ctx_mempool;
e8711085 2541
e315cd28
AC
2542 snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2543 ha->pdev->device);
2544 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2545 DMA_POOL_SIZE, 8, 0);
2546 if (!ha->s_dma_pool)
2547 goto fail_free_nvram;
2548
a9083016
GM
2549 if (IS_QLA82XX(ha)) {
2550 ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2551 DSD_LIST_DMA_POOL_SIZE, 8, 0);
2552 if (!ha->dl_dma_pool) {
2553 qla_printk(KERN_WARNING, ha,
2554 "Memory Allocation failed - dl_dma_pool\n");
2555 goto fail_s_dma_pool;
2556 }
2557
2558 ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2559 FCP_CMND_DMA_POOL_SIZE, 8, 0);
2560 if (!ha->fcp_cmnd_dma_pool) {
2561 qla_printk(KERN_WARNING, ha,
2562 "Memory Allocation failed - fcp_cmnd_dma_pool\n");
2563 goto fail_dl_dma_pool;
2564 }
2565 }
2566
e8711085
AV
2567 /* Allocate memory for SNS commands */
2568 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
e315cd28 2569 /* Get consistent memory allocated for SNS commands */
e8711085 2570 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
e315cd28 2571 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
e8711085 2572 if (!ha->sns_cmd)
e315cd28 2573 goto fail_dma_pool;
e8711085 2574 } else {
e315cd28 2575 /* Get consistent memory allocated for MS IOCB */
e8711085 2576 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
e315cd28 2577 &ha->ms_iocb_dma);
e8711085 2578 if (!ha->ms_iocb)
e315cd28
AC
2579 goto fail_dma_pool;
2580 /* Get consistent memory allocated for CT SNS commands */
e8711085 2581 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
e315cd28 2582 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
e8711085
AV
2583 if (!ha->ct_sns)
2584 goto fail_free_ms_iocb;
1da177e4
LT
2585 }
2586
e315cd28 2587 /* Allocate memory for request ring */
73208dfd
AC
2588 *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2589 if (!*req) {
e315cd28
AC
2590 DEBUG(printk("Unable to allocate memory for req\n"));
2591 goto fail_req;
2592 }
73208dfd
AC
2593 (*req)->length = req_len;
2594 (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2595 ((*req)->length + 1) * sizeof(request_t),
2596 &(*req)->dma, GFP_KERNEL);
2597 if (!(*req)->ring) {
e315cd28
AC
2598 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2599 goto fail_req_ring;
2600 }
2601 /* Allocate memory for response ring */
73208dfd
AC
2602 *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2603 if (!*rsp) {
2604 qla_printk(KERN_WARNING, ha,
2605 "Unable to allocate memory for rsp\n");
e315cd28
AC
2606 goto fail_rsp;
2607 }
73208dfd
AC
2608 (*rsp)->hw = ha;
2609 (*rsp)->length = rsp_len;
2610 (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2611 ((*rsp)->length + 1) * sizeof(response_t),
2612 &(*rsp)->dma, GFP_KERNEL);
2613 if (!(*rsp)->ring) {
2614 qla_printk(KERN_WARNING, ha,
2615 "Unable to allocate memory for rsp_ring\n");
e315cd28
AC
2616 goto fail_rsp_ring;
2617 }
73208dfd
AC
2618 (*req)->rsp = *rsp;
2619 (*rsp)->req = *req;
2620 /* Allocate memory for NVRAM data for vports */
2621 if (ha->nvram_npiv_size) {
2622 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2623 ha->nvram_npiv_size, GFP_KERNEL);
2624 if (!ha->npiv_info) {
2625 qla_printk(KERN_WARNING, ha,
2626 "Unable to allocate memory for npiv info\n");
2627 goto fail_npiv_info;
2628 }
2629 } else
2630 ha->npiv_info = NULL;
e8711085 2631
b64b0e8f 2632 /* Get consistent memory allocated for EX-INIT-CB. */
a9083016 2633 if (IS_QLA8XXX_TYPE(ha)) {
b64b0e8f
AV
2634 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2635 &ha->ex_init_cb_dma);
2636 if (!ha->ex_init_cb)
2637 goto fail_ex_init_cb;
2638 }
2639
a9083016
GM
2640 INIT_LIST_HEAD(&ha->gbl_dsd_list);
2641
e315cd28
AC
2642 INIT_LIST_HEAD(&ha->vp_list);
2643 return 1;
2644
b64b0e8f
AV
2645fail_ex_init_cb:
2646 kfree(ha->npiv_info);
73208dfd
AC
2647fail_npiv_info:
2648 dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2649 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2650 (*rsp)->ring = NULL;
2651 (*rsp)->dma = 0;
e315cd28 2652fail_rsp_ring:
73208dfd 2653 kfree(*rsp);
e315cd28 2654fail_rsp:
73208dfd
AC
2655 dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2656 sizeof(request_t), (*req)->ring, (*req)->dma);
2657 (*req)->ring = NULL;
2658 (*req)->dma = 0;
e315cd28 2659fail_req_ring:
73208dfd 2660 kfree(*req);
e315cd28
AC
2661fail_req:
2662 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2663 ha->ct_sns, ha->ct_sns_dma);
2664 ha->ct_sns = NULL;
2665 ha->ct_sns_dma = 0;
e8711085
AV
2666fail_free_ms_iocb:
2667 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2668 ha->ms_iocb = NULL;
2669 ha->ms_iocb_dma = 0;
e315cd28 2670fail_dma_pool:
a9083016
GM
2671 if (IS_QLA82XX(ha)) {
2672 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2673 ha->fcp_cmnd_dma_pool = NULL;
2674 }
2675fail_dl_dma_pool:
2676 if (IS_QLA82XX(ha)) {
2677 dma_pool_destroy(ha->dl_dma_pool);
2678 ha->dl_dma_pool = NULL;
2679 }
2680fail_s_dma_pool:
e315cd28
AC
2681 dma_pool_destroy(ha->s_dma_pool);
2682 ha->s_dma_pool = NULL;
e8711085
AV
2683fail_free_nvram:
2684 kfree(ha->nvram);
2685 ha->nvram = NULL;
a9083016
GM
2686fail_free_ctx_mempool:
2687 mempool_destroy(ha->ctx_mempool);
2688 ha->ctx_mempool = NULL;
e8711085
AV
2689fail_free_srb_mempool:
2690 mempool_destroy(ha->srb_mempool);
2691 ha->srb_mempool = NULL;
e8711085
AV
2692fail_free_gid_list:
2693 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2694 ha->gid_list_dma);
e8711085
AV
2695 ha->gid_list = NULL;
2696 ha->gid_list_dma = 0;
e315cd28
AC
2697fail_free_init_cb:
2698 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2699 ha->init_cb_dma);
2700 ha->init_cb = NULL;
2701 ha->init_cb_dma = 0;
e8711085 2702fail:
e315cd28 2703 DEBUG(printk("%s: Memory allocation failure\n", __func__));
e8711085 2704 return -ENOMEM;
1da177e4
LT
2705}
2706
2707/*
2708* qla2x00_mem_free
2709* Frees all adapter allocated memory.
2710*
2711* Input:
2712* ha = adapter block pointer.
2713*/
a824ebb3 2714static void
e315cd28 2715qla2x00_mem_free(struct qla_hw_data *ha)
1da177e4 2716{
e8711085
AV
2717 if (ha->srb_mempool)
2718 mempool_destroy(ha->srb_mempool);
1da177e4 2719
df613b96
AV
2720 if (ha->fce)
2721 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
e315cd28 2722 ha->fce_dma);
df613b96 2723
a7a167bf
AV
2724 if (ha->fw_dump) {
2725 if (ha->eft)
2726 dma_free_coherent(&ha->pdev->dev,
e315cd28 2727 ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
a7a167bf
AV
2728 vfree(ha->fw_dump);
2729 }
2730
11bbc1d8
AV
2731 if (ha->dcbx_tlv)
2732 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2733 ha->dcbx_tlv, ha->dcbx_tlv_dma);
2734
ce0423f4
AV
2735 if (ha->xgmac_data)
2736 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2737 ha->xgmac_data, ha->xgmac_data_dma);
2738
1da177e4
LT
2739 if (ha->sns_cmd)
2740 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
e315cd28 2741 ha->sns_cmd, ha->sns_cmd_dma);
1da177e4
LT
2742
2743 if (ha->ct_sns)
2744 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
e315cd28 2745 ha->ct_sns, ha->ct_sns_dma);
1da177e4 2746
88729e53
AV
2747 if (ha->sfp_data)
2748 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2749
ad0ecd61
JC
2750 if (ha->edc_data)
2751 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2752
1da177e4
LT
2753 if (ha->ms_iocb)
2754 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2755
b64b0e8f 2756 if (ha->ex_init_cb)
a9083016
GM
2757 dma_pool_free(ha->s_dma_pool,
2758 ha->ex_init_cb, ha->ex_init_cb_dma);
b64b0e8f 2759
1da177e4
LT
2760 if (ha->s_dma_pool)
2761 dma_pool_destroy(ha->s_dma_pool);
2762
1da177e4
LT
2763 if (ha->gid_list)
2764 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2765 ha->gid_list_dma);
1da177e4 2766
a9083016
GM
2767 if (IS_QLA82XX(ha)) {
2768 if (!list_empty(&ha->gbl_dsd_list)) {
2769 struct dsd_dma *dsd_ptr, *tdsd_ptr;
2770
2771 /* clean up allocated prev pool */
2772 list_for_each_entry_safe(dsd_ptr,
2773 tdsd_ptr, &ha->gbl_dsd_list, list) {
2774 dma_pool_free(ha->dl_dma_pool,
2775 dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
2776 list_del(&dsd_ptr->list);
2777 kfree(dsd_ptr);
2778 }
2779 }
2780 }
2781
2782 if (ha->dl_dma_pool)
2783 dma_pool_destroy(ha->dl_dma_pool);
2784
2785 if (ha->fcp_cmnd_dma_pool)
2786 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2787
2788 if (ha->ctx_mempool)
2789 mempool_destroy(ha->ctx_mempool);
2790
e315cd28
AC
2791 if (ha->init_cb)
2792 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
a9083016 2793 ha->init_cb, ha->init_cb_dma);
e315cd28
AC
2794 vfree(ha->optrom_buffer);
2795 kfree(ha->nvram);
73208dfd 2796 kfree(ha->npiv_info);
1da177e4 2797
e8711085 2798 ha->srb_mempool = NULL;
a9083016 2799 ha->ctx_mempool = NULL;
a7a167bf
AV
2800 ha->eft = NULL;
2801 ha->eft_dma = 0;
1da177e4
LT
2802 ha->sns_cmd = NULL;
2803 ha->sns_cmd_dma = 0;
2804 ha->ct_sns = NULL;
2805 ha->ct_sns_dma = 0;
2806 ha->ms_iocb = NULL;
2807 ha->ms_iocb_dma = 0;
1da177e4
LT
2808 ha->init_cb = NULL;
2809 ha->init_cb_dma = 0;
b64b0e8f
AV
2810 ha->ex_init_cb = NULL;
2811 ha->ex_init_cb_dma = 0;
1da177e4
LT
2812
2813 ha->s_dma_pool = NULL;
a9083016
GM
2814 ha->dl_dma_pool = NULL;
2815 ha->fcp_cmnd_dma_pool = NULL;
1da177e4 2816
1da177e4
LT
2817 ha->gid_list = NULL;
2818 ha->gid_list_dma = 0;
2819
e315cd28
AC
2820 ha->fw_dump = NULL;
2821 ha->fw_dumped = 0;
2822 ha->fw_dump_reading = 0;
e315cd28 2823}
1da177e4 2824
e315cd28
AC
2825struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2826 struct qla_hw_data *ha)
2827{
2828 struct Scsi_Host *host;
2829 struct scsi_qla_host *vha = NULL;
854165f4 2830
e315cd28
AC
2831 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2832 if (host == NULL) {
2833 printk(KERN_WARNING
2834 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2835 goto fail;
2836 }
2837
2838 /* Clear our data area */
2839 vha = shost_priv(host);
2840 memset(vha, 0, sizeof(scsi_qla_host_t));
2841
2842 vha->host = host;
2843 vha->host_no = host->host_no;
2844 vha->hw = ha;
2845
2846 INIT_LIST_HEAD(&vha->vp_fcports);
2847 INIT_LIST_HEAD(&vha->work_list);
2848 INIT_LIST_HEAD(&vha->list);
2849
f999f4c1
AV
2850 spin_lock_init(&vha->work_lock);
2851
e315cd28
AC
2852 sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2853 return vha;
2854
2855fail:
2856 return vha;
1da177e4
LT
2857}
2858
01ef66bb 2859static struct qla_work_evt *
f999f4c1 2860qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
0971de7f
AV
2861{
2862 struct qla_work_evt *e;
2863
f999f4c1 2864 e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
0971de7f
AV
2865 if (!e)
2866 return NULL;
2867
2868 INIT_LIST_HEAD(&e->list);
2869 e->type = type;
2870 e->flags = QLA_EVT_FLAG_FREE;
2871 return e;
2872}
2873
01ef66bb 2874static int
f999f4c1 2875qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
0971de7f 2876{
f999f4c1 2877 unsigned long flags;
0971de7f 2878
f999f4c1 2879 spin_lock_irqsave(&vha->work_lock, flags);
e315cd28 2880 list_add_tail(&e->list, &vha->work_list);
f999f4c1 2881 spin_unlock_irqrestore(&vha->work_lock, flags);
e315cd28 2882 qla2xxx_wake_dpc(vha);
f999f4c1 2883
0971de7f
AV
2884 return QLA_SUCCESS;
2885}
2886
2887int
e315cd28 2888qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
0971de7f
AV
2889 u32 data)
2890{
2891 struct qla_work_evt *e;
2892
f999f4c1 2893 e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
0971de7f
AV
2894 if (!e)
2895 return QLA_FUNCTION_FAILED;
2896
2897 e->u.aen.code = code;
2898 e->u.aen.data = data;
f999f4c1 2899 return qla2x00_post_work(vha, e);
0971de7f
AV
2900}
2901
8a659571
AV
2902int
2903qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2904{
2905 struct qla_work_evt *e;
2906
f999f4c1 2907 e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
8a659571
AV
2908 if (!e)
2909 return QLA_FUNCTION_FAILED;
2910
2911 memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
f999f4c1 2912 return qla2x00_post_work(vha, e);
8a659571
AV
2913}
2914
ac280b67
AV
2915#define qla2x00_post_async_work(name, type) \
2916int qla2x00_post_async_##name##_work( \
2917 struct scsi_qla_host *vha, \
2918 fc_port_t *fcport, uint16_t *data) \
2919{ \
2920 struct qla_work_evt *e; \
2921 \
2922 e = qla2x00_alloc_work(vha, type); \
2923 if (!e) \
2924 return QLA_FUNCTION_FAILED; \
2925 \
2926 e->u.logio.fcport = fcport; \
2927 if (data) { \
2928 e->u.logio.data[0] = data[0]; \
2929 e->u.logio.data[1] = data[1]; \
2930 } \
2931 return qla2x00_post_work(vha, e); \
2932}
2933
2934qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2935qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2936qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2937qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
2938
3420d36c
AV
2939int
2940qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2941{
2942 struct qla_work_evt *e;
2943
2944 e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2945 if (!e)
2946 return QLA_FUNCTION_FAILED;
2947
2948 e->u.uevent.code = code;
2949 return qla2x00_post_work(vha, e);
2950}
2951
2952static void
2953qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2954{
2955 char event_string[40];
2956 char *envp[] = { event_string, NULL };
2957
2958 switch (code) {
2959 case QLA_UEVENT_CODE_FW_DUMP:
2960 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2961 vha->host_no);
2962 break;
2963 default:
2964 /* do nothing */
2965 break;
2966 }
2967 kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2968}
2969
ac280b67 2970void
e315cd28 2971qla2x00_do_work(struct scsi_qla_host *vha)
0971de7f 2972{
f999f4c1
AV
2973 struct qla_work_evt *e, *tmp;
2974 unsigned long flags;
2975 LIST_HEAD(work);
0971de7f 2976
f999f4c1
AV
2977 spin_lock_irqsave(&vha->work_lock, flags);
2978 list_splice_init(&vha->work_list, &work);
2979 spin_unlock_irqrestore(&vha->work_lock, flags);
2980
2981 list_for_each_entry_safe(e, tmp, &work, list) {
0971de7f 2982 list_del_init(&e->list);
0971de7f
AV
2983
2984 switch (e->type) {
2985 case QLA_EVT_AEN:
e315cd28 2986 fc_host_post_event(vha->host, fc_get_event_number(),
0971de7f
AV
2987 e->u.aen.code, e->u.aen.data);
2988 break;
8a659571
AV
2989 case QLA_EVT_IDC_ACK:
2990 qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2991 break;
ac280b67
AV
2992 case QLA_EVT_ASYNC_LOGIN:
2993 qla2x00_async_login(vha, e->u.logio.fcport,
2994 e->u.logio.data);
2995 break;
2996 case QLA_EVT_ASYNC_LOGIN_DONE:
2997 qla2x00_async_login_done(vha, e->u.logio.fcport,
2998 e->u.logio.data);
2999 break;
3000 case QLA_EVT_ASYNC_LOGOUT:
3001 qla2x00_async_logout(vha, e->u.logio.fcport);
3002 break;
3003 case QLA_EVT_ASYNC_LOGOUT_DONE:
3004 qla2x00_async_logout_done(vha, e->u.logio.fcport,
3005 e->u.logio.data);
3006 break;
3420d36c
AV
3007 case QLA_EVT_UEVENT:
3008 qla2x00_uevent_emit(vha, e->u.uevent.code);
3009 break;
0971de7f
AV
3010 }
3011 if (e->flags & QLA_EVT_FLAG_FREE)
3012 kfree(e);
e315cd28 3013 }
e315cd28 3014}
f999f4c1 3015
e315cd28
AC
3016/* Relogins all the fcports of a vport
3017 * Context: dpc thread
3018 */
3019void qla2x00_relogin(struct scsi_qla_host *vha)
3020{
3021 fc_port_t *fcport;
c6b2fca8 3022 int status;
e315cd28
AC
3023 uint16_t next_loopid = 0;
3024 struct qla_hw_data *ha = vha->hw;
ac280b67 3025 uint16_t data[2];
e315cd28
AC
3026
3027 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3028 /*
3029 * If the port is not ONLINE then try to login
3030 * to it if we haven't run out of retries.
3031 */
3032 if (atomic_read(&fcport->state) !=
3033 FCS_ONLINE && fcport->login_retry) {
3034
ac280b67 3035 fcport->login_retry--;
e315cd28 3036 if (fcport->flags & FCF_FABRIC_DEVICE) {
f08b7251 3037 if (fcport->flags & FCF_FCP2_DEVICE)
e315cd28
AC
3038 ha->isp_ops->fabric_logout(vha,
3039 fcport->loop_id,
3040 fcport->d_id.b.domain,
3041 fcport->d_id.b.area,
3042 fcport->d_id.b.al_pa);
3043
ac280b67
AV
3044 if (IS_ALOGIO_CAPABLE(ha)) {
3045 data[0] = 0;
3046 data[1] = QLA_LOGIO_LOGIN_RETRIED;
3047 status = qla2x00_post_async_login_work(
3048 vha, fcport, data);
3049 if (status == QLA_SUCCESS)
3050 continue;
3051 /* Attempt a retry. */
3052 status = 1;
3053 } else
3054 status = qla2x00_fabric_login(vha,
3055 fcport, &next_loopid);
e315cd28
AC
3056 } else
3057 status = qla2x00_local_device_login(vha,
3058 fcport);
3059
e315cd28
AC
3060 if (status == QLA_SUCCESS) {
3061 fcport->old_loop_id = fcport->loop_id;
3062
3063 DEBUG(printk("scsi(%ld): port login OK: logged "
3064 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
3065
3066 qla2x00_update_fcport(vha, fcport);
3067
3068 } else if (status == 1) {
3069 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3070 /* retry the login again */
3071 DEBUG(printk("scsi(%ld): Retrying"
3072 " %d login again loop_id 0x%x\n",
3073 vha->host_no, fcport->login_retry,
3074 fcport->loop_id));
3075 } else {
3076 fcport->login_retry = 0;
3077 }
3078
3079 if (fcport->login_retry == 0 && status != QLA_SUCCESS)
3080 fcport->loop_id = FC_NO_LOOP_ID;
3081 }
3082 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3083 break;
0971de7f 3084 }
0971de7f
AV
3085}
3086
1da177e4
LT
3087/**************************************************************************
3088* qla2x00_do_dpc
3089* This kernel thread is a task that is schedule by the interrupt handler
3090* to perform the background processing for interrupts.
3091*
3092* Notes:
3093* This task always run in the context of a kernel thread. It
3094* is kick-off by the driver's detect code and starts up
3095* up one per adapter. It immediately goes to sleep and waits for
3096* some fibre event. When either the interrupt handler or
3097* the timer routine detects a event it will one of the task
3098* bits then wake us up.
3099**************************************************************************/
3100static int
3101qla2x00_do_dpc(void *data)
3102{
2c3dfe3f 3103 int rval;
e315cd28
AC
3104 scsi_qla_host_t *base_vha;
3105 struct qla_hw_data *ha;
1da177e4 3106
e315cd28
AC
3107 ha = (struct qla_hw_data *)data;
3108 base_vha = pci_get_drvdata(ha->pdev);
1da177e4 3109
1da177e4
LT
3110 set_user_nice(current, -20);
3111
39a11240 3112 while (!kthread_should_stop()) {
1da177e4
LT
3113 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
3114
39a11240
CH
3115 set_current_state(TASK_INTERRUPTIBLE);
3116 schedule();
3117 __set_current_state(TASK_RUNNING);
1da177e4
LT
3118
3119 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
3120
3121 /* Initialization not yet finished. Don't do anything yet. */
e315cd28 3122 if (!base_vha->flags.init_done)
1da177e4
LT
3123 continue;
3124
85880801
AV
3125 if (ha->flags.eeh_busy) {
3126 DEBUG17(qla_printk(KERN_WARNING, ha,
3127 "qla2x00_do_dpc: dpc_flags: %lx\n",
3128 base_vha->dpc_flags));
3129 continue;
3130 }
3131
e315cd28 3132 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
1da177e4
LT
3133
3134 ha->dpc_active = 1;
3135
1da177e4 3136 if (ha->flags.mbox_busy) {
1da177e4
LT
3137 ha->dpc_active = 0;
3138 continue;
3139 }
3140
e315cd28 3141 qla2x00_do_work(base_vha);
0971de7f 3142
a9083016
GM
3143 if (IS_QLA82XX(ha)) {
3144 if (test_and_clear_bit(ISP_UNRECOVERABLE,
3145 &base_vha->dpc_flags)) {
3146 qla82xx_idc_lock(ha);
3147 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3148 QLA82XX_DEV_FAILED);
3149 qla82xx_idc_unlock(ha);
3150 qla_printk(KERN_INFO, ha,
3151 "HW State: FAILED\n");
3152 qla82xx_device_state_handler(base_vha);
3153 continue;
3154 }
3155
3156 if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
3157 &base_vha->dpc_flags)) {
3158
3159 DEBUG(printk(KERN_INFO
3160 "scsi(%ld): dpc: sched "
3161 "qla82xx_fcoe_ctx_reset ha = %p\n",
3162 base_vha->host_no, ha));
3163 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3164 &base_vha->dpc_flags))) {
3165 if (qla82xx_fcoe_ctx_reset(base_vha)) {
3166 /* FCoE-ctx reset failed.
3167 * Escalate to chip-reset
3168 */
3169 set_bit(ISP_ABORT_NEEDED,
3170 &base_vha->dpc_flags);
3171 }
3172 clear_bit(ABORT_ISP_ACTIVE,
3173 &base_vha->dpc_flags);
3174 }
3175
3176 DEBUG(printk("scsi(%ld): dpc:"
3177 " qla82xx_fcoe_ctx_reset end\n",
3178 base_vha->host_no));
3179 }
3180 }
3181
e315cd28
AC
3182 if (test_and_clear_bit(ISP_ABORT_NEEDED,
3183 &base_vha->dpc_flags)) {
1da177e4
LT
3184
3185 DEBUG(printk("scsi(%ld): dpc: sched "
3186 "qla2x00_abort_isp ha = %p\n",
e315cd28 3187 base_vha->host_no, ha));
1da177e4 3188 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
e315cd28 3189 &base_vha->dpc_flags))) {
1da177e4 3190
a9083016 3191 if (ha->isp_ops->abort_isp(base_vha)) {
1da177e4
LT
3192 /* failed. retry later */
3193 set_bit(ISP_ABORT_NEEDED,
e315cd28 3194 &base_vha->dpc_flags);
99363ef8 3195 }
e315cd28
AC
3196 clear_bit(ABORT_ISP_ACTIVE,
3197 &base_vha->dpc_flags);
99363ef8
SJ
3198 }
3199
1da177e4 3200 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
e315cd28 3201 base_vha->host_no));
1da177e4
LT
3202 }
3203
e315cd28
AC
3204 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
3205 qla2x00_update_fcports(base_vha);
3206 clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
c9c5ced9 3207 }
d97994dc 3208
e315cd28
AC
3209 if (test_and_clear_bit(RESET_MARKER_NEEDED,
3210 &base_vha->dpc_flags) &&
3211 (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
1da177e4
LT
3212
3213 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
e315cd28 3214 base_vha->host_no));
1da177e4 3215
e315cd28
AC
3216 qla2x00_rst_aen(base_vha);
3217 clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
1da177e4
LT
3218 }
3219
3220 /* Retry each device up to login retry count */
e315cd28
AC
3221 if ((test_and_clear_bit(RELOGIN_NEEDED,
3222 &base_vha->dpc_flags)) &&
3223 !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
3224 atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
1da177e4
LT
3225
3226 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
e315cd28
AC
3227 base_vha->host_no));
3228 qla2x00_relogin(base_vha);
3229
1da177e4 3230 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
e315cd28 3231 base_vha->host_no));
1da177e4
LT
3232 }
3233
e315cd28
AC
3234 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
3235 &base_vha->dpc_flags)) {
1da177e4
LT
3236
3237 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
e315cd28 3238 base_vha->host_no));
1da177e4
LT
3239
3240 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
e315cd28 3241 &base_vha->dpc_flags))) {
1da177e4 3242
e315cd28 3243 rval = qla2x00_loop_resync(base_vha);
1da177e4 3244
e315cd28
AC
3245 clear_bit(LOOP_RESYNC_ACTIVE,
3246 &base_vha->dpc_flags);
1da177e4
LT
3247 }
3248
3249 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
e315cd28 3250 base_vha->host_no));
1da177e4
LT
3251 }
3252
e315cd28
AC
3253 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
3254 atomic_read(&base_vha->loop_state) == LOOP_READY) {
3255 clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
3256 qla2xxx_flash_npiv_conf(base_vha);
272976ca
AV
3257 }
3258
1da177e4 3259 if (!ha->interrupts_on)
fd34f556 3260 ha->isp_ops->enable_intrs(ha);
1da177e4 3261
e315cd28
AC
3262 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
3263 &base_vha->dpc_flags))
3264 ha->isp_ops->beacon_blink(base_vha);
f6df144c 3265
e315cd28 3266 qla2x00_do_dpc_all_vps(base_vha);
2c3dfe3f 3267
1da177e4
LT
3268 ha->dpc_active = 0;
3269 } /* End of while(1) */
3270
e315cd28 3271 DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
1da177e4
LT
3272
3273 /*
3274 * Make sure that nobody tries to wake us up again.
3275 */
1da177e4
LT
3276 ha->dpc_active = 0;
3277
ac280b67
AV
3278 /* Cleanup any residual CTX SRBs. */
3279 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3280
39a11240
CH
3281 return 0;
3282}
3283
3284void
e315cd28 3285qla2xxx_wake_dpc(struct scsi_qla_host *vha)
39a11240 3286{
e315cd28 3287 struct qla_hw_data *ha = vha->hw;
c795c1e4
AV
3288 struct task_struct *t = ha->dpc_thread;
3289
e315cd28 3290 if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
c795c1e4 3291 wake_up_process(t);
1da177e4
LT
3292}
3293
1da177e4
LT
3294/*
3295* qla2x00_rst_aen
3296* Processes asynchronous reset.
3297*
3298* Input:
3299* ha = adapter block pointer.
3300*/
3301static void
e315cd28 3302qla2x00_rst_aen(scsi_qla_host_t *vha)
1da177e4 3303{
e315cd28
AC
3304 if (vha->flags.online && !vha->flags.reset_active &&
3305 !atomic_read(&vha->loop_down_timer) &&
3306 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
1da177e4 3307 do {
e315cd28 3308 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1da177e4
LT
3309
3310 /*
3311 * Issue marker command only when we are going to start
3312 * the I/O.
3313 */
e315cd28
AC
3314 vha->marker_needed = 1;
3315 } while (!atomic_read(&vha->loop_down_timer) &&
3316 (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
1da177e4
LT
3317 }
3318}
3319
f4f051eb 3320static void
e315cd28 3321qla2x00_sp_free_dma(srb_t *sp)
f4f051eb
AV
3322{
3323 struct scsi_cmnd *cmd = sp->cmd;
3324
3325 if (sp->flags & SRB_DMA_VALID) {
385d70b4 3326 scsi_dma_unmap(cmd);
f4f051eb
AV
3327 sp->flags &= ~SRB_DMA_VALID;
3328 }
fca29703 3329 CMD_SP(cmd) = NULL;
f4f051eb
AV
3330}
3331
3332void
73208dfd 3333qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
f4f051eb
AV
3334{
3335 struct scsi_cmnd *cmd = sp->cmd;
3336
e315cd28 3337 qla2x00_sp_free_dma(sp);
f4f051eb 3338
a9083016
GM
3339 if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
3340 struct ct6_dsd *ctx = sp->ctx;
3341 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd,
3342 ctx->fcp_cmnd_dma);
3343 list_splice(&ctx->dsd_list, &ha->gbl_dsd_list);
3344 ha->gbl_dsd_inuse -= ctx->dsd_use_cnt;
3345 ha->gbl_dsd_avail += ctx->dsd_use_cnt;
3346 mempool_free(sp->ctx, ha->ctx_mempool);
3347 sp->ctx = NULL;
3348 }
f4f051eb 3349
a9083016 3350 mempool_free(sp, ha->srb_mempool);
f4f051eb
AV
3351 cmd->scsi_done(cmd);
3352}
bdf79621 3353
1da177e4
LT
3354/**************************************************************************
3355* qla2x00_timer
3356*
3357* Description:
3358* One second timer
3359*
3360* Context: Interrupt
3361***************************************************************************/
2c3dfe3f 3362void
e315cd28 3363qla2x00_timer(scsi_qla_host_t *vha)
1da177e4 3364{
1da177e4
LT
3365 unsigned long cpu_flags = 0;
3366 fc_port_t *fcport;
1da177e4
LT
3367 int start_dpc = 0;
3368 int index;
3369 srb_t *sp;
f4f051eb 3370 int t;
85880801 3371 uint16_t w;
e315cd28 3372 struct qla_hw_data *ha = vha->hw;
73208dfd 3373 struct req_que *req;
85880801 3374
a9083016
GM
3375 if (IS_QLA82XX(ha))
3376 qla82xx_watchdog(vha);
3377
85880801
AV
3378 /* Hardware read to raise pending EEH errors during mailbox waits. */
3379 if (!pci_channel_offline(ha->pdev))
3380 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
1da177e4
LT
3381 /*
3382 * Ports - Port down timer.
3383 *
3384 * Whenever, a port is in the LOST state we start decrementing its port
3385 * down timer every second until it reaches zero. Once it reaches zero
fa2a1ce5 3386 * the port it marked DEAD.
1da177e4
LT
3387 */
3388 t = 0;
e315cd28 3389 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
3390 if (fcport->port_type != FCT_TARGET)
3391 continue;
3392
3393 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3394
3395 if (atomic_read(&fcport->port_down_timer) == 0)
3396 continue;
3397
fa2a1ce5 3398 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
1da177e4 3399 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
fa2a1ce5 3400
1da177e4 3401 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
fca29703 3402 "%d remaining\n",
e315cd28 3403 vha->host_no,
1da177e4
LT
3404 t, atomic_read(&fcport->port_down_timer)));
3405 }
3406 t++;
3407 } /* End of for fcport */
3408
1da177e4
LT
3409
3410 /* Loop down handler. */
e315cd28
AC
3411 if (atomic_read(&vha->loop_down_timer) > 0 &&
3412 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3413 && vha->flags.online) {
1da177e4 3414
e315cd28
AC
3415 if (atomic_read(&vha->loop_down_timer) ==
3416 vha->loop_down_abort_time) {
1da177e4
LT
3417
3418 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3419 "queues before time expire\n",
e315cd28 3420 vha->host_no));
1da177e4 3421
e315cd28
AC
3422 if (!IS_QLA2100(ha) && vha->link_down_timeout)
3423 atomic_set(&vha->loop_state, LOOP_DEAD);
1da177e4 3424
f08b7251
AV
3425 /*
3426 * Schedule an ISP abort to return any FCP2-device
3427 * commands.
3428 */
2c3dfe3f 3429 /* NPIV - scan physical port only */
e315cd28 3430 if (!vha->vp_idx) {
2c3dfe3f
SJ
3431 spin_lock_irqsave(&ha->hardware_lock,
3432 cpu_flags);
73208dfd 3433 req = ha->req_q_map[0];
2c3dfe3f
SJ
3434 for (index = 1;
3435 index < MAX_OUTSTANDING_COMMANDS;
3436 index++) {
3437 fc_port_t *sfcp;
3438
e315cd28 3439 sp = req->outstanding_cmds[index];
2c3dfe3f
SJ
3440 if (!sp)
3441 continue;
cf53b069
AV
3442 if (sp->ctx)
3443 continue;
2c3dfe3f 3444 sfcp = sp->fcport;
f08b7251 3445 if (!(sfcp->flags & FCF_FCP2_DEVICE))
2c3dfe3f 3446 continue;
bdf79621 3447
2c3dfe3f 3448 set_bit(ISP_ABORT_NEEDED,
e315cd28 3449 &vha->dpc_flags);
2c3dfe3f
SJ
3450 break;
3451 }
3452 spin_unlock_irqrestore(&ha->hardware_lock,
e315cd28 3453 cpu_flags);
1da177e4 3454 }
1da177e4
LT
3455 start_dpc++;
3456 }
3457
3458 /* if the loop has been down for 4 minutes, reinit adapter */
e315cd28 3459 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
0d6e61bc 3460 if (!(vha->device_flags & DFLG_NO_CABLE)) {
1da177e4
LT
3461 DEBUG(printk("scsi(%ld): Loop down - "
3462 "aborting ISP.\n",
e315cd28 3463 vha->host_no));
1da177e4
LT
3464 qla_printk(KERN_WARNING, ha,
3465 "Loop down - aborting ISP.\n");
3466
e315cd28 3467 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1da177e4
LT
3468 }
3469 }
fca29703 3470 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
e315cd28
AC
3471 vha->host_no,
3472 atomic_read(&vha->loop_down_timer)));
1da177e4
LT
3473 }
3474
f6df144c
AV
3475 /* Check if beacon LED needs to be blinked */
3476 if (ha->beacon_blink_led == 1) {
e315cd28 3477 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
f6df144c
AV
3478 start_dpc++;
3479 }
3480
550bf57d 3481 /* Process any deferred work. */
e315cd28 3482 if (!list_empty(&vha->work_list))
550bf57d
AV
3483 start_dpc++;
3484
1da177e4 3485 /* Schedule the DPC routine if needed */
e315cd28
AC
3486 if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3487 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3488 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
1da177e4 3489 start_dpc ||
e315cd28
AC
3490 test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3491 test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
a9083016
GM
3492 test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
3493 test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
e315cd28
AC
3494 test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3495 test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3496 qla2xxx_wake_dpc(vha);
1da177e4 3497
e315cd28 3498 qla2x00_restart_timer(vha, WATCH_INTERVAL);
1da177e4
LT
3499}
3500
5433383e
AV
3501/* Firmware interface routines. */
3502
a9083016 3503#define FW_BLOBS 8
5433383e
AV
3504#define FW_ISP21XX 0
3505#define FW_ISP22XX 1
3506#define FW_ISP2300 2
3507#define FW_ISP2322 3
48c02fde 3508#define FW_ISP24XX 4
c3a2f0df 3509#define FW_ISP25XX 5
3a03eb79 3510#define FW_ISP81XX 6
a9083016 3511#define FW_ISP82XX 7
5433383e 3512
bb8ee499
AV
3513#define FW_FILE_ISP21XX "ql2100_fw.bin"
3514#define FW_FILE_ISP22XX "ql2200_fw.bin"
3515#define FW_FILE_ISP2300 "ql2300_fw.bin"
3516#define FW_FILE_ISP2322 "ql2322_fw.bin"
3517#define FW_FILE_ISP24XX "ql2400_fw.bin"
c3a2f0df 3518#define FW_FILE_ISP25XX "ql2500_fw.bin"
3a03eb79 3519#define FW_FILE_ISP81XX "ql8100_fw.bin"
a9083016 3520#define FW_FILE_ISP82XX "ql8200_fw.bin"
bb8ee499 3521
e1e82b6f 3522static DEFINE_MUTEX(qla_fw_lock);
5433383e
AV
3523
3524static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
bb8ee499
AV
3525 { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3526 { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3527 { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3528 { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3529 { .name = FW_FILE_ISP24XX, },
c3a2f0df 3530 { .name = FW_FILE_ISP25XX, },
3a03eb79 3531 { .name = FW_FILE_ISP81XX, },
a9083016 3532 { .name = FW_FILE_ISP82XX, },
5433383e
AV
3533};
3534
3535struct fw_blob *
e315cd28 3536qla2x00_request_firmware(scsi_qla_host_t *vha)
5433383e 3537{
e315cd28 3538 struct qla_hw_data *ha = vha->hw;
5433383e
AV
3539 struct fw_blob *blob;
3540
3541 blob = NULL;
3542 if (IS_QLA2100(ha)) {
3543 blob = &qla_fw_blobs[FW_ISP21XX];
3544 } else if (IS_QLA2200(ha)) {
3545 blob = &qla_fw_blobs[FW_ISP22XX];
48c02fde 3546 } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
5433383e 3547 blob = &qla_fw_blobs[FW_ISP2300];
48c02fde 3548 } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
5433383e 3549 blob = &qla_fw_blobs[FW_ISP2322];
4d4df193 3550 } else if (IS_QLA24XX_TYPE(ha)) {
5433383e 3551 blob = &qla_fw_blobs[FW_ISP24XX];
c3a2f0df
AV
3552 } else if (IS_QLA25XX(ha)) {
3553 blob = &qla_fw_blobs[FW_ISP25XX];
3a03eb79
AV
3554 } else if (IS_QLA81XX(ha)) {
3555 blob = &qla_fw_blobs[FW_ISP81XX];
a9083016
GM
3556 } else if (IS_QLA82XX(ha)) {
3557 blob = &qla_fw_blobs[FW_ISP82XX];
5433383e
AV
3558 }
3559
e1e82b6f 3560 mutex_lock(&qla_fw_lock);
5433383e
AV
3561 if (blob->fw)
3562 goto out;
3563
3564 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3565 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
e315cd28 3566 "(%s).\n", vha->host_no, blob->name));
5433383e
AV
3567 blob->fw = NULL;
3568 blob = NULL;
3569 goto out;
3570 }
3571
3572out:
e1e82b6f 3573 mutex_unlock(&qla_fw_lock);
5433383e
AV
3574 return blob;
3575}
3576
3577static void
3578qla2x00_release_firmware(void)
3579{
3580 int idx;
3581
e1e82b6f 3582 mutex_lock(&qla_fw_lock);
5433383e
AV
3583 for (idx = 0; idx < FW_BLOBS; idx++)
3584 if (qla_fw_blobs[idx].fw)
3585 release_firmware(qla_fw_blobs[idx].fw);
e1e82b6f 3586 mutex_unlock(&qla_fw_lock);
5433383e
AV
3587}
3588
14e660e6
SJ
3589static pci_ers_result_t
3590qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3591{
85880801
AV
3592 scsi_qla_host_t *vha = pci_get_drvdata(pdev);
3593 struct qla_hw_data *ha = vha->hw;
3594
3595 DEBUG2(qla_printk(KERN_WARNING, ha, "error_detected:state %x\n",
3596 state));
b9b12f73 3597
14e660e6
SJ
3598 switch (state) {
3599 case pci_channel_io_normal:
85880801 3600 ha->flags.eeh_busy = 0;
14e660e6
SJ
3601 return PCI_ERS_RESULT_CAN_RECOVER;
3602 case pci_channel_io_frozen:
85880801 3603 ha->flags.eeh_busy = 1;
90a86fc0 3604 qla2x00_free_irqs(vha);
14e660e6
SJ
3605 pci_disable_device(pdev);
3606 return PCI_ERS_RESULT_NEED_RESET;
3607 case pci_channel_io_perm_failure:
85880801
AV
3608 ha->flags.pci_channel_io_perm_failure = 1;
3609 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
14e660e6
SJ
3610 return PCI_ERS_RESULT_DISCONNECT;
3611 }
3612 return PCI_ERS_RESULT_NEED_RESET;
3613}
3614
3615static pci_ers_result_t
3616qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3617{
3618 int risc_paused = 0;
3619 uint32_t stat;
3620 unsigned long flags;
e315cd28
AC
3621 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3622 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3623 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3624 struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3625
3626 spin_lock_irqsave(&ha->hardware_lock, flags);
3627 if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3628 stat = RD_REG_DWORD(&reg->hccr);
3629 if (stat & HCCR_RISC_PAUSE)
3630 risc_paused = 1;
3631 } else if (IS_QLA23XX(ha)) {
3632 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3633 if (stat & HSR_RISC_PAUSED)
3634 risc_paused = 1;
3635 } else if (IS_FWI2_CAPABLE(ha)) {
3636 stat = RD_REG_DWORD(&reg24->host_status);
3637 if (stat & HSRX_RISC_PAUSED)
3638 risc_paused = 1;
3639 }
3640 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3641
3642 if (risc_paused) {
3643 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3644 "Dumping firmware!\n");
e315cd28 3645 ha->isp_ops->fw_dump(base_vha, 0);
14e660e6
SJ
3646
3647 return PCI_ERS_RESULT_NEED_RESET;
3648 } else
3649 return PCI_ERS_RESULT_RECOVERED;
3650}
3651
3652static pci_ers_result_t
3653qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3654{
3655 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
e315cd28
AC
3656 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3657 struct qla_hw_data *ha = base_vha->hw;
90a86fc0
JC
3658 struct rsp_que *rsp;
3659 int rc, retries = 10;
09483916 3660
85880801
AV
3661 DEBUG17(qla_printk(KERN_WARNING, ha, "slot_reset\n"));
3662
90a86fc0
JC
3663 /* Workaround: qla2xxx driver which access hardware earlier
3664 * needs error state to be pci_channel_io_online.
3665 * Otherwise mailbox command timesout.
3666 */
3667 pdev->error_state = pci_channel_io_normal;
3668
3669 pci_restore_state(pdev);
3670
8c1496bd
RL
3671 /* pci_restore_state() clears the saved_state flag of the device
3672 * save restored state which resets saved_state flag
3673 */
3674 pci_save_state(pdev);
3675
09483916
BH
3676 if (ha->mem_only)
3677 rc = pci_enable_device_mem(pdev);
3678 else
3679 rc = pci_enable_device(pdev);
14e660e6 3680
09483916 3681 if (rc) {
14e660e6
SJ
3682 qla_printk(KERN_WARNING, ha,
3683 "Can't re-enable PCI device after reset.\n");
14e660e6
SJ
3684 return ret;
3685 }
14e660e6 3686
90a86fc0
JC
3687 rsp = ha->rsp_q_map[0];
3688 if (qla2x00_request_irqs(ha, rsp))
3689 return ret;
3690
e315cd28 3691 if (ha->isp_ops->pci_config(base_vha))
14e660e6
SJ
3692 return ret;
3693
90a86fc0
JC
3694 while (ha->flags.mbox_busy && retries--)
3695 msleep(1000);
85880801 3696
e315cd28 3697 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
a9083016 3698 if (ha->isp_ops->abort_isp(base_vha) == QLA_SUCCESS)
14e660e6 3699 ret = PCI_ERS_RESULT_RECOVERED;
e315cd28 3700 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
14e660e6 3701
90a86fc0 3702
85880801
AV
3703 DEBUG17(qla_printk(KERN_WARNING, ha,
3704 "slot_reset-return:ret=%x\n", ret));
3705
14e660e6
SJ
3706 return ret;
3707}
3708
3709static void
3710qla2xxx_pci_resume(struct pci_dev *pdev)
3711{
e315cd28
AC
3712 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3713 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3714 int ret;
3715
85880801
AV
3716 DEBUG17(qla_printk(KERN_WARNING, ha, "pci_resume\n"));
3717
e315cd28 3718 ret = qla2x00_wait_for_hba_online(base_vha);
14e660e6
SJ
3719 if (ret != QLA_SUCCESS) {
3720 qla_printk(KERN_ERR, ha,
3721 "the device failed to resume I/O "
3722 "from slot/link_reset");
3723 }
85880801 3724
3e46f031
LC
3725 pci_cleanup_aer_uncorrect_error_status(pdev);
3726
85880801 3727 ha->flags.eeh_busy = 0;
14e660e6
SJ
3728}
3729
3730static struct pci_error_handlers qla2xxx_err_handler = {
3731 .error_detected = qla2xxx_pci_error_detected,
3732 .mmio_enabled = qla2xxx_pci_mmio_enabled,
3733 .slot_reset = qla2xxx_pci_slot_reset,
3734 .resume = qla2xxx_pci_resume,
3735};
3736
5433383e 3737static struct pci_device_id qla2xxx_pci_tbl[] = {
47f5e069
AV
3738 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3739 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3740 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3741 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3742 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3743 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3744 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3745 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3746 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
4d4df193 3747 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
47f5e069
AV
3748 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3749 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
c3a2f0df 3750 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3a03eb79 3751 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
a9083016 3752 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
5433383e
AV
3753 { 0 },
3754};
3755MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3756
fca29703 3757static struct pci_driver qla2xxx_pci_driver = {
cb63067a 3758 .name = QLA2XXX_DRIVER_NAME,
0a21ef1e
JB
3759 .driver = {
3760 .owner = THIS_MODULE,
3761 },
fca29703 3762 .id_table = qla2xxx_pci_tbl,
7ee61397 3763 .probe = qla2x00_probe_one,
4c993f76 3764 .remove = qla2x00_remove_one,
14e660e6 3765 .err_handler = &qla2xxx_err_handler,
fca29703
AV
3766};
3767
6a03b4cd
HZ
3768static struct file_operations apidev_fops = {
3769 .owner = THIS_MODULE,
3770};
3771
1da177e4
LT
3772/**
3773 * qla2x00_module_init - Module initialization.
3774 **/
3775static int __init
3776qla2x00_module_init(void)
3777{
fca29703
AV
3778 int ret = 0;
3779
1da177e4 3780 /* Allocate cache for SRBs. */
354d6b21 3781 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
20c2df83 3782 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
3783 if (srb_cachep == NULL) {
3784 printk(KERN_ERR
3785 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3786 return -ENOMEM;
3787 }
3788
3789 /* Derive version string. */
3790 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
11010fec 3791 if (ql2xextended_error_logging)
0181944f
AV
3792 strcat(qla2x00_version_str, "-debug");
3793
1c97a12a
AV
3794 qla2xxx_transport_template =
3795 fc_attach_transport(&qla2xxx_transport_functions);
2c3dfe3f
SJ
3796 if (!qla2xxx_transport_template) {
3797 kmem_cache_destroy(srb_cachep);
1da177e4 3798 return -ENODEV;
2c3dfe3f 3799 }
6a03b4cd
HZ
3800
3801 apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
3802 if (apidev_major < 0) {
3803 printk(KERN_WARNING "qla2xxx: Unable to register char device "
3804 "%s\n", QLA2XXX_APIDEV);
3805 }
3806
2c3dfe3f
SJ
3807 qla2xxx_transport_vport_template =
3808 fc_attach_transport(&qla2xxx_transport_vport_functions);
3809 if (!qla2xxx_transport_vport_template) {
3810 kmem_cache_destroy(srb_cachep);
3811 fc_release_transport(qla2xxx_transport_template);
1da177e4 3812 return -ENODEV;
2c3dfe3f 3813 }
1da177e4 3814
fd9a29f0
AV
3815 printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3816 qla2x00_version_str);
7ee61397 3817 ret = pci_register_driver(&qla2xxx_pci_driver);
fca29703
AV
3818 if (ret) {
3819 kmem_cache_destroy(srb_cachep);
3820 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3821 fc_release_transport(qla2xxx_transport_vport_template);
fca29703
AV
3822 }
3823 return ret;
1da177e4
LT
3824}
3825
3826/**
3827 * qla2x00_module_exit - Module cleanup.
3828 **/
3829static void __exit
3830qla2x00_module_exit(void)
3831{
6a03b4cd 3832 unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
7ee61397 3833 pci_unregister_driver(&qla2xxx_pci_driver);
5433383e 3834 qla2x00_release_firmware();
354d6b21 3835 kmem_cache_destroy(srb_cachep);
a9083016
GM
3836 if (ctx_cachep)
3837 kmem_cache_destroy(ctx_cachep);
1da177e4 3838 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3839 fc_release_transport(qla2xxx_transport_vport_template);
1da177e4
LT
3840}
3841
3842module_init(qla2x00_module_init);
3843module_exit(qla2x00_module_exit);
3844
3845MODULE_AUTHOR("QLogic Corporation");
3846MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3847MODULE_LICENSE("GPL");
3848MODULE_VERSION(QLA2XXX_VERSION);
bb8ee499
AV
3849MODULE_FIRMWARE(FW_FILE_ISP21XX);
3850MODULE_FIRMWARE(FW_FILE_ISP22XX);
3851MODULE_FIRMWARE(FW_FILE_ISP2300);
3852MODULE_FIRMWARE(FW_FILE_ISP2322);
3853MODULE_FIRMWARE(FW_FILE_ISP24XX);
61623fc3 3854MODULE_FIRMWARE(FW_FILE_ISP25XX);