[SCSI] qla2xxx: Support for asynchronous TM and Marker IOCBs.
[deliverable/linux.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
3822263e
MI
145int ql2xasynctmfenable;
146module_param(ql2xasynctmfenable, int, S_IRUGO|S_IRUSR);
147MODULE_PARM_DESC(ql2xasynctmfenable,
148 "Enables issue of TM IOCBs asynchronously via IOCB mechanism"
149 "Default is 0 - Issue TM IOCBs via mailbox mechanism.");
1da177e4 150/*
fa2a1ce5 151 * SCSI host template entry points
1da177e4
LT
152 */
153static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051eb 154static int qla2xxx_slave_alloc(struct scsi_device *);
1e99e33a
AV
155static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
156static void qla2xxx_scan_start(struct Scsi_Host *);
f4f051eb 157static void qla2xxx_slave_destroy(struct scsi_device *);
a5326f86 158static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
fca29703 159 void (*fn)(struct scsi_cmnd *));
1da177e4
LT
160static int qla2xxx_eh_abort(struct scsi_cmnd *);
161static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
523ec773 162static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
1da177e4
LT
163static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
164static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
1da177e4 165
e881a172 166static int qla2x00_change_queue_depth(struct scsi_device *, int, int);
ce7e4af7
AV
167static int qla2x00_change_queue_type(struct scsi_device *, int);
168
a5326f86 169struct scsi_host_template qla2xxx_driver_template = {
1da177e4 170 .module = THIS_MODULE,
cb63067a 171 .name = QLA2XXX_DRIVER_NAME,
a5326f86 172 .queuecommand = qla2xxx_queuecommand,
fca29703
AV
173
174 .eh_abort_handler = qla2xxx_eh_abort,
175 .eh_device_reset_handler = qla2xxx_eh_device_reset,
523ec773 176 .eh_target_reset_handler = qla2xxx_eh_target_reset,
fca29703
AV
177 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
178 .eh_host_reset_handler = qla2xxx_eh_host_reset,
179
180 .slave_configure = qla2xxx_slave_configure,
181
182 .slave_alloc = qla2xxx_slave_alloc,
183 .slave_destroy = qla2xxx_slave_destroy,
ed677086
AV
184 .scan_finished = qla2xxx_scan_finished,
185 .scan_start = qla2xxx_scan_start,
ce7e4af7
AV
186 .change_queue_depth = qla2x00_change_queue_depth,
187 .change_queue_type = qla2x00_change_queue_type,
fca29703
AV
188 .this_id = -1,
189 .cmd_per_lun = 3,
190 .use_clustering = ENABLE_CLUSTERING,
191 .sg_tablesize = SG_ALL,
192
193 .max_sectors = 0xFFFF,
afb046e2 194 .shost_attrs = qla2x00_host_attrs,
fca29703
AV
195};
196
1da177e4 197static struct scsi_transport_template *qla2xxx_transport_template = NULL;
2c3dfe3f 198struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
1da177e4 199
1da177e4
LT
200/* TODO Convert to inlines
201 *
202 * Timer routines
203 */
1da177e4 204
2c3dfe3f 205__inline__ void
e315cd28 206qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
1da177e4 207{
e315cd28
AC
208 init_timer(&vha->timer);
209 vha->timer.expires = jiffies + interval * HZ;
210 vha->timer.data = (unsigned long)vha;
211 vha->timer.function = (void (*)(unsigned long))func;
212 add_timer(&vha->timer);
213 vha->timer_active = 1;
1da177e4
LT
214}
215
216static inline void
e315cd28 217qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
1da177e4 218{
a9083016
GM
219 /* Currently used for 82XX only. */
220 if (vha->device_flags & DFLG_DEV_FAILED)
221 return;
222
e315cd28 223 mod_timer(&vha->timer, jiffies + interval * HZ);
1da177e4
LT
224}
225
a824ebb3 226static __inline__ void
e315cd28 227qla2x00_stop_timer(scsi_qla_host_t *vha)
1da177e4 228{
e315cd28
AC
229 del_timer_sync(&vha->timer);
230 vha->timer_active = 0;
1da177e4
LT
231}
232
1da177e4
LT
233static int qla2x00_do_dpc(void *data);
234
235static void qla2x00_rst_aen(scsi_qla_host_t *);
236
73208dfd
AC
237static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
238 struct req_que **, struct rsp_que **);
e315cd28
AC
239static void qla2x00_mem_free(struct qla_hw_data *);
240static void qla2x00_sp_free_dma(srb_t *);
1da177e4 241
1da177e4 242/* -------------------------------------------------------------------------- */
73208dfd
AC
243static int qla2x00_alloc_queues(struct qla_hw_data *ha)
244{
2afa19a9 245 ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
73208dfd
AC
246 GFP_KERNEL);
247 if (!ha->req_q_map) {
248 qla_printk(KERN_WARNING, ha,
249 "Unable to allocate memory for request queue ptrs\n");
250 goto fail_req_map;
251 }
252
2afa19a9 253 ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
73208dfd
AC
254 GFP_KERNEL);
255 if (!ha->rsp_q_map) {
256 qla_printk(KERN_WARNING, ha,
257 "Unable to allocate memory for response queue ptrs\n");
258 goto fail_rsp_map;
259 }
260 set_bit(0, ha->rsp_qid_map);
261 set_bit(0, ha->req_qid_map);
262 return 1;
263
264fail_rsp_map:
265 kfree(ha->req_q_map);
266 ha->req_q_map = NULL;
267fail_req_map:
268 return -ENOMEM;
269}
270
2afa19a9 271static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
73208dfd 272{
73208dfd
AC
273 if (req && req->ring)
274 dma_free_coherent(&ha->pdev->dev,
275 (req->length + 1) * sizeof(request_t),
276 req->ring, req->dma);
277
278 kfree(req);
279 req = NULL;
280}
281
2afa19a9
AC
282static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
283{
284 if (rsp && rsp->ring)
285 dma_free_coherent(&ha->pdev->dev,
286 (rsp->length + 1) * sizeof(response_t),
287 rsp->ring, rsp->dma);
288
289 kfree(rsp);
290 rsp = NULL;
291}
292
73208dfd
AC
293static void qla2x00_free_queues(struct qla_hw_data *ha)
294{
295 struct req_que *req;
296 struct rsp_que *rsp;
297 int cnt;
298
2afa19a9 299 for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
73208dfd 300 req = ha->req_q_map[cnt];
2afa19a9 301 qla2x00_free_req_que(ha, req);
73208dfd 302 }
73208dfd
AC
303 kfree(ha->req_q_map);
304 ha->req_q_map = NULL;
2afa19a9
AC
305
306 for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
307 rsp = ha->rsp_q_map[cnt];
308 qla2x00_free_rsp_que(ha, rsp);
309 }
310 kfree(ha->rsp_q_map);
311 ha->rsp_q_map = NULL;
73208dfd
AC
312}
313
68ca949c
AC
314static int qla25xx_setup_mode(struct scsi_qla_host *vha)
315{
316 uint16_t options = 0;
317 int ques, req, ret;
318 struct qla_hw_data *ha = vha->hw;
319
7163ea81
AC
320 if (!(ha->fw_attributes & BIT_6)) {
321 qla_printk(KERN_INFO, ha,
322 "Firmware is not multi-queue capable\n");
323 goto fail;
324 }
68ca949c 325 if (ql2xmultique_tag) {
68ca949c
AC
326 /* create a request queue for IO */
327 options |= BIT_7;
328 req = qla25xx_create_req_que(ha, options, 0, 0, -1,
329 QLA_DEFAULT_QUE_QOS);
330 if (!req) {
331 qla_printk(KERN_WARNING, ha,
332 "Can't create request queue\n");
333 goto fail;
334 }
7163ea81 335 ha->wq = create_workqueue("qla2xxx_wq");
68ca949c
AC
336 vha->req = ha->req_q_map[req];
337 options |= BIT_1;
338 for (ques = 1; ques < ha->max_rsp_queues; ques++) {
339 ret = qla25xx_create_rsp_que(ha, options, 0, 0, req);
340 if (!ret) {
341 qla_printk(KERN_WARNING, ha,
342 "Response Queue create failed\n");
343 goto fail2;
344 }
345 }
7163ea81
AC
346 ha->flags.cpu_affinity_enabled = 1;
347
68ca949c
AC
348 DEBUG2(qla_printk(KERN_INFO, ha,
349 "CPU affinity mode enabled, no. of response"
350 " queues:%d, no. of request queues:%d\n",
351 ha->max_rsp_queues, ha->max_req_queues));
352 }
353 return 0;
354fail2:
355 qla25xx_delete_queues(vha);
7163ea81
AC
356 destroy_workqueue(ha->wq);
357 ha->wq = NULL;
68ca949c
AC
358fail:
359 ha->mqenable = 0;
7163ea81
AC
360 kfree(ha->req_q_map);
361 kfree(ha->rsp_q_map);
362 ha->max_req_queues = ha->max_rsp_queues = 1;
68ca949c
AC
363 return 1;
364}
365
1da177e4 366static char *
e315cd28 367qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
1da177e4 368{
e315cd28 369 struct qla_hw_data *ha = vha->hw;
1da177e4
LT
370 static char *pci_bus_modes[] = {
371 "33", "66", "100", "133",
372 };
373 uint16_t pci_bus;
374
375 strcpy(str, "PCI");
376 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
377 if (pci_bus) {
378 strcat(str, "-X (");
379 strcat(str, pci_bus_modes[pci_bus]);
380 } else {
381 pci_bus = (ha->pci_attr & BIT_8) >> 8;
382 strcat(str, " (");
383 strcat(str, pci_bus_modes[pci_bus]);
384 }
385 strcat(str, " MHz)");
386
387 return (str);
388}
389
fca29703 390static char *
e315cd28 391qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
fca29703
AV
392{
393 static char *pci_bus_modes[] = { "33", "66", "100", "133", };
e315cd28 394 struct qla_hw_data *ha = vha->hw;
fca29703
AV
395 uint32_t pci_bus;
396 int pcie_reg;
397
398 pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
399 if (pcie_reg) {
400 char lwstr[6];
401 uint16_t pcie_lstat, lspeed, lwidth;
402
403 pcie_reg += 0x12;
404 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
405 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
406 lwidth = (pcie_lstat &
407 (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
408
409 strcpy(str, "PCIe (");
410 if (lspeed == 1)
c87a0d8c 411 strcat(str, "2.5GT/s ");
c3a2f0df 412 else if (lspeed == 2)
c87a0d8c 413 strcat(str, "5.0GT/s ");
fca29703
AV
414 else
415 strcat(str, "<unknown> ");
416 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
417 strcat(str, lwstr);
418
419 return str;
420 }
421
422 strcpy(str, "PCI");
423 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
424 if (pci_bus == 0 || pci_bus == 8) {
425 strcat(str, " (");
426 strcat(str, pci_bus_modes[pci_bus >> 3]);
427 } else {
428 strcat(str, "-X ");
429 if (pci_bus & BIT_2)
430 strcat(str, "Mode 2");
431 else
432 strcat(str, "Mode 1");
433 strcat(str, " (");
434 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
435 }
436 strcat(str, " MHz)");
437
438 return str;
439}
440
e5f82ab8 441static char *
e315cd28 442qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
1da177e4
LT
443{
444 char un_str[10];
e315cd28 445 struct qla_hw_data *ha = vha->hw;
fa2a1ce5 446
1da177e4
LT
447 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
448 ha->fw_minor_version,
449 ha->fw_subminor_version);
450
451 if (ha->fw_attributes & BIT_9) {
452 strcat(str, "FLX");
453 return (str);
454 }
455
456 switch (ha->fw_attributes & 0xFF) {
457 case 0x7:
458 strcat(str, "EF");
459 break;
460 case 0x17:
461 strcat(str, "TP");
462 break;
463 case 0x37:
464 strcat(str, "IP");
465 break;
466 case 0x77:
467 strcat(str, "VI");
468 break;
469 default:
470 sprintf(un_str, "(%x)", ha->fw_attributes);
471 strcat(str, un_str);
472 break;
473 }
474 if (ha->fw_attributes & 0x100)
475 strcat(str, "X");
476
477 return (str);
478}
479
e5f82ab8 480static char *
e315cd28 481qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
fca29703 482{
e315cd28 483 struct qla_hw_data *ha = vha->hw;
f0883ac6 484
3a03eb79
AV
485 sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
486 ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
fca29703 487 return str;
fca29703
AV
488}
489
490static inline srb_t *
e315cd28 491qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
fca29703
AV
492 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
493{
494 srb_t *sp;
e315cd28 495 struct qla_hw_data *ha = vha->hw;
fca29703
AV
496
497 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
498 if (!sp)
499 return sp;
500
fca29703
AV
501 sp->fcport = fcport;
502 sp->cmd = cmd;
503 sp->flags = 0;
504 CMD_SP(cmd) = (void *)sp;
505 cmd->scsi_done = done;
cf53b069 506 sp->ctx = NULL;
fca29703
AV
507
508 return sp;
509}
510
1da177e4 511static int
a5326f86 512qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
fca29703 513{
e315cd28 514 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
fca29703 515 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
19a7b4ae 516 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
e315cd28
AC
517 struct qla_hw_data *ha = vha->hw;
518 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
fca29703
AV
519 srb_t *sp;
520 int rval;
521
85880801
AV
522 if (ha->flags.eeh_busy) {
523 if (ha->flags.pci_channel_io_perm_failure)
b9b12f73 524 cmd->result = DID_NO_CONNECT << 16;
85880801
AV
525 else
526 cmd->result = DID_REQUEUE << 16;
14e660e6
SJ
527 goto qc24_fail_command;
528 }
529
19a7b4ae
JSEC
530 rval = fc_remote_port_chkready(rport);
531 if (rval) {
532 cmd->result = rval;
fca29703
AV
533 goto qc24_fail_command;
534 }
535
387f96b4 536 /* Close window on fcport/rport state-transitioning. */
7b594131
MC
537 if (fcport->drport)
538 goto qc24_target_busy;
387f96b4 539
fca29703
AV
540 if (atomic_read(&fcport->state) != FCS_ONLINE) {
541 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
e315cd28 542 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
fca29703
AV
543 cmd->result = DID_NO_CONNECT << 16;
544 goto qc24_fail_command;
545 }
7b594131 546 goto qc24_target_busy;
fca29703
AV
547 }
548
e315cd28 549 spin_unlock_irq(vha->host->host_lock);
fca29703 550
e315cd28 551 sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
fca29703
AV
552 if (!sp)
553 goto qc24_host_busy_lock;
554
e315cd28 555 rval = ha->isp_ops->start_scsi(sp);
fca29703
AV
556 if (rval != QLA_SUCCESS)
557 goto qc24_host_busy_free_sp;
558
e315cd28 559 spin_lock_irq(vha->host->host_lock);
fca29703
AV
560
561 return 0;
562
563qc24_host_busy_free_sp:
e315cd28
AC
564 qla2x00_sp_free_dma(sp);
565 mempool_free(sp, ha->srb_mempool);
fca29703
AV
566
567qc24_host_busy_lock:
e315cd28 568 spin_lock_irq(vha->host->host_lock);
fca29703
AV
569 return SCSI_MLQUEUE_HOST_BUSY;
570
7b594131
MC
571qc24_target_busy:
572 return SCSI_MLQUEUE_TARGET_BUSY;
573
fca29703
AV
574qc24_fail_command:
575 done(cmd);
576
577 return 0;
578}
579
580
1da177e4
LT
581/*
582 * qla2x00_eh_wait_on_command
583 * Waits for the command to be returned by the Firmware for some
584 * max time.
585 *
586 * Input:
1da177e4 587 * cmd = Scsi Command to wait on.
1da177e4
LT
588 *
589 * Return:
590 * Not Found : 0
591 * Found : 1
592 */
593static int
e315cd28 594qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1da177e4 595{
fe74c71f
AV
596#define ABORT_POLLING_PERIOD 1000
597#define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
f4f051eb 598 unsigned long wait_iter = ABORT_WAIT_ITER;
85880801
AV
599 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
600 struct qla_hw_data *ha = vha->hw;
f4f051eb 601 int ret = QLA_SUCCESS;
1da177e4 602
85880801
AV
603 if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
604 DEBUG17(qla_printk(KERN_WARNING, ha, "return:eh_wait\n"));
605 return ret;
606 }
607
d970432c 608 while (CMD_SP(cmd) && wait_iter--) {
fe74c71f 609 msleep(ABORT_POLLING_PERIOD);
f4f051eb 610 }
611 if (CMD_SP(cmd))
612 ret = QLA_FUNCTION_FAILED;
1da177e4 613
f4f051eb 614 return ret;
1da177e4
LT
615}
616
617/*
618 * qla2x00_wait_for_hba_online
fa2a1ce5 619 * Wait till the HBA is online after going through
1da177e4
LT
620 * <= MAX_RETRIES_OF_ISP_ABORT or
621 * finally HBA is disabled ie marked offline
622 *
623 * Input:
624 * ha - pointer to host adapter structure
fa2a1ce5
AV
625 *
626 * Note:
1da177e4
LT
627 * Does context switching-Release SPIN_LOCK
628 * (if any) before calling this routine.
629 *
630 * Return:
631 * Success (Adapter is online) : 0
632 * Failed (Adapter is offline/disabled) : 1
633 */
854165f4 634int
e315cd28 635qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1da177e4 636{
fca29703
AV
637 int return_status;
638 unsigned long wait_online;
e315cd28
AC
639 struct qla_hw_data *ha = vha->hw;
640 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 641
fa2a1ce5 642 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
e315cd28
AC
643 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
644 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
645 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
646 ha->dpc_active) && time_before(jiffies, wait_online)) {
1da177e4
LT
647
648 msleep(1000);
649 }
e315cd28 650 if (base_vha->flags.online)
fa2a1ce5 651 return_status = QLA_SUCCESS;
1da177e4
LT
652 else
653 return_status = QLA_FUNCTION_FAILED;
654
1da177e4
LT
655 return (return_status);
656}
657
2533cf67
LC
658int
659qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
660{
661 int return_status;
662 unsigned long wait_reset;
663 struct qla_hw_data *ha = vha->hw;
664 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
665
666 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
667 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
668 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
669 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
670 ha->dpc_active) && time_before(jiffies, wait_reset)) {
671
672 msleep(1000);
673
674 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
675 ha->flags.chip_reset_done)
676 break;
677 }
678 if (ha->flags.chip_reset_done)
679 return_status = QLA_SUCCESS;
680 else
681 return_status = QLA_FUNCTION_FAILED;
682
683 return return_status;
684}
685
1da177e4
LT
686/*
687 * qla2x00_wait_for_loop_ready
688 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
fa2a1ce5 689 * to be in LOOP_READY state.
1da177e4
LT
690 * Input:
691 * ha - pointer to host adapter structure
fa2a1ce5
AV
692 *
693 * Note:
1da177e4
LT
694 * Does context switching-Release SPIN_LOCK
695 * (if any) before calling this routine.
fa2a1ce5 696 *
1da177e4
LT
697 *
698 * Return:
699 * Success (LOOP_READY) : 0
700 * Failed (LOOP_NOT_READY) : 1
701 */
fa2a1ce5 702static inline int
e315cd28 703qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
1da177e4
LT
704{
705 int return_status = QLA_SUCCESS;
706 unsigned long loop_timeout ;
e315cd28
AC
707 struct qla_hw_data *ha = vha->hw;
708 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4
LT
709
710 /* wait for 5 min at the max for loop to be ready */
fa2a1ce5 711 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1da177e4 712
e315cd28
AC
713 while ((!atomic_read(&base_vha->loop_down_timer) &&
714 atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
715 atomic_read(&base_vha->loop_state) != LOOP_READY) {
716 if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
57680080
RA
717 return_status = QLA_FUNCTION_FAILED;
718 break;
719 }
1da177e4
LT
720 msleep(1000);
721 if (time_after_eq(jiffies, loop_timeout)) {
722 return_status = QLA_FUNCTION_FAILED;
723 break;
724 }
725 }
fa2a1ce5 726 return (return_status);
1da177e4
LT
727}
728
729/**************************************************************************
730* qla2xxx_eh_abort
731*
732* Description:
733* The abort function will abort the specified command.
734*
735* Input:
736* cmd = Linux SCSI command packet to be aborted.
737*
738* Returns:
739* Either SUCCESS or FAILED.
740*
741* Note:
2ea00202 742* Only return FAILED if command not returned by firmware.
1da177e4 743**************************************************************************/
e5f82ab8 744static int
1da177e4
LT
745qla2xxx_eh_abort(struct scsi_cmnd *cmd)
746{
e315cd28 747 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
f4f051eb 748 srb_t *sp;
17d98630 749 int ret, i;
f4f051eb 750 unsigned int id, lun;
751 unsigned long serial;
18e144d3 752 unsigned long flags;
2ea00202 753 int wait = 0;
e315cd28 754 struct qla_hw_data *ha = vha->hw;
67c2e93a 755 struct req_que *req = vha->req;
17d98630 756 srb_t *spt;
1da177e4 757
65d430fa 758 fc_block_scsi_eh(cmd);
07db5183 759
f4f051eb 760 if (!CMD_SP(cmd))
2ea00202 761 return SUCCESS;
1da177e4 762
2ea00202 763 ret = SUCCESS;
1da177e4 764
f4f051eb 765 id = cmd->device->id;
766 lun = cmd->device->lun;
767 serial = cmd->serial_number;
17d98630
AC
768 spt = (srb_t *) CMD_SP(cmd);
769 if (!spt)
770 return SUCCESS;
1da177e4 771
f4f051eb 772 /* Check active list for command command. */
e315cd28 773 spin_lock_irqsave(&ha->hardware_lock, flags);
17d98630
AC
774 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
775 sp = req->outstanding_cmds[i];
1da177e4 776
17d98630
AC
777 if (sp == NULL)
778 continue;
a9083016 779 if ((sp->ctx) && !(sp->flags & SRB_FCP_CMND_DMA_VALID))
cf53b069 780 continue;
17d98630
AC
781 if (sp->cmd != cmd)
782 continue;
1da177e4 783
17d98630
AC
784 DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
785 " pid=%ld.\n", __func__, vha->host_no, sp, serial));
786
787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2afa19a9 788 if (ha->isp_ops->abort_command(sp)) {
17d98630
AC
789 DEBUG2(printk("%s(%ld): abort_command "
790 "mbx failed.\n", __func__, vha->host_no));
2ac4b64f 791 ret = FAILED;
17d98630
AC
792 } else {
793 DEBUG3(printk("%s(%ld): abort_command "
794 "mbx success.\n", __func__, vha->host_no));
795 wait = 1;
73208dfd 796 }
17d98630
AC
797 spin_lock_irqsave(&ha->hardware_lock, flags);
798 break;
f4f051eb 799 }
e315cd28 800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4 801
f4f051eb 802 /* Wait for the command to be returned. */
2ea00202 803 if (wait) {
e315cd28 804 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
fa2a1ce5 805 qla_printk(KERN_ERR, ha,
f4f051eb 806 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
e315cd28 807 "%x.\n", vha->host_no, id, lun, serial, ret);
2ea00202 808 ret = FAILED;
f4f051eb 809 }
1da177e4 810 }
1da177e4 811
fa2a1ce5 812 qla_printk(KERN_INFO, ha,
2ea00202 813 "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
e315cd28 814 vha->host_no, id, lun, wait, serial, ret);
1da177e4 815
f4f051eb 816 return ret;
817}
1da177e4 818
523ec773
AV
819enum nexus_wait_type {
820 WAIT_HOST = 0,
821 WAIT_TARGET,
822 WAIT_LUN,
823};
824
f4f051eb 825static int
e315cd28 826qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
17d98630 827 unsigned int l, srb_t *sp, enum nexus_wait_type type)
f4f051eb 828{
17d98630 829 int cnt, match, status;
18e144d3 830 unsigned long flags;
e315cd28 831 struct qla_hw_data *ha = vha->hw;
73208dfd 832 struct req_que *req;
1da177e4 833
523ec773 834 status = QLA_SUCCESS;
17d98630
AC
835 if (!sp)
836 return status;
837
e315cd28 838 spin_lock_irqsave(&ha->hardware_lock, flags);
67c2e93a 839 req = vha->req;
17d98630
AC
840 for (cnt = 1; status == QLA_SUCCESS &&
841 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
842 sp = req->outstanding_cmds[cnt];
843 if (!sp)
523ec773 844 continue;
cf53b069
AV
845 if (sp->ctx)
846 continue;
17d98630
AC
847 if (vha->vp_idx != sp->fcport->vha->vp_idx)
848 continue;
849 match = 0;
850 switch (type) {
851 case WAIT_HOST:
852 match = 1;
853 break;
854 case WAIT_TARGET:
855 match = sp->cmd->device->id == t;
856 break;
857 case WAIT_LUN:
858 match = (sp->cmd->device->id == t &&
859 sp->cmd->device->lun == l);
860 break;
73208dfd 861 }
17d98630
AC
862 if (!match)
863 continue;
864
865 spin_unlock_irqrestore(&ha->hardware_lock, flags);
866 status = qla2x00_eh_wait_on_command(sp->cmd);
867 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4 868 }
e315cd28 869 spin_unlock_irqrestore(&ha->hardware_lock, flags);
523ec773
AV
870
871 return status;
1da177e4
LT
872}
873
a9083016
GM
874void qla82xx_wait_for_pending_commands(scsi_qla_host_t *vha)
875{
876 int cnt;
877 srb_t *sp;
878 struct req_que *req = vha->req;
879
880 DEBUG2(qla_printk(KERN_INFO, vha->hw,
881 "Waiting for pending commands\n"));
882 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
883 sp = req->outstanding_cmds[cnt];
884 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
885 sp, WAIT_HOST) == QLA_SUCCESS) {
886 DEBUG2(qla_printk(KERN_INFO, vha->hw,
887 "Done wait for pending commands\n"));
888 }
889 }
890}
891
523ec773
AV
892static char *reset_errors[] = {
893 "HBA not online",
894 "HBA not ready",
895 "Task management failed",
896 "Waiting for command completions",
897};
1da177e4 898
e5f82ab8 899static int
523ec773 900__qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
2afa19a9 901 struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
1da177e4 902{
e315cd28 903 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 904 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
523ec773 905 int err;
1da177e4 906
65d430fa 907 fc_block_scsi_eh(cmd);
07db5183 908
b0328bee 909 if (!fcport)
523ec773 910 return FAILED;
1da177e4 911
e315cd28
AC
912 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
913 vha->host_no, cmd->device->id, cmd->device->lun, name);
1da177e4 914
523ec773 915 err = 0;
e315cd28 916 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
523ec773
AV
917 goto eh_reset_failed;
918 err = 1;
e315cd28 919 if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
523ec773
AV
920 goto eh_reset_failed;
921 err = 2;
2afa19a9
AC
922 if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
923 != QLA_SUCCESS)
523ec773
AV
924 goto eh_reset_failed;
925 err = 3;
e315cd28 926 if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
17d98630 927 cmd->device->lun, (srb_t *) CMD_SP(cmd), type) != QLA_SUCCESS)
523ec773
AV
928 goto eh_reset_failed;
929
e315cd28
AC
930 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
931 vha->host_no, cmd->device->id, cmd->device->lun, name);
523ec773
AV
932
933 return SUCCESS;
934
935 eh_reset_failed:
e315cd28
AC
936 qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
937 , vha->host_no, cmd->device->id, cmd->device->lun, name,
523ec773
AV
938 reset_errors[err]);
939 return FAILED;
940}
1da177e4 941
523ec773
AV
942static int
943qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
944{
e315cd28
AC
945 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
946 struct qla_hw_data *ha = vha->hw;
1da177e4 947
523ec773
AV
948 return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
949 ha->isp_ops->lun_reset);
1da177e4
LT
950}
951
1da177e4 952static int
523ec773 953qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1da177e4 954{
e315cd28
AC
955 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
956 struct qla_hw_data *ha = vha->hw;
1da177e4 957
523ec773
AV
958 return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
959 ha->isp_ops->target_reset);
1da177e4
LT
960}
961
1da177e4
LT
962/**************************************************************************
963* qla2xxx_eh_bus_reset
964*
965* Description:
966* The bus reset function will reset the bus and abort any executing
967* commands.
968*
969* Input:
970* cmd = Linux SCSI command packet of the command that cause the
971* bus reset.
972*
973* Returns:
974* SUCCESS/FAILURE (defined as macro in scsi.h).
975*
976**************************************************************************/
e5f82ab8 977static int
1da177e4
LT
978qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
979{
e315cd28 980 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 981 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
2c3dfe3f 982 int ret = FAILED;
f4f051eb 983 unsigned int id, lun;
984 unsigned long serial;
17d98630 985 srb_t *sp = (srb_t *) CMD_SP(cmd);
f4f051eb 986
65d430fa 987 fc_block_scsi_eh(cmd);
07db5183 988
f4f051eb 989 id = cmd->device->id;
990 lun = cmd->device->lun;
991 serial = cmd->serial_number;
1da177e4 992
b0328bee 993 if (!fcport)
f4f051eb 994 return ret;
1da177e4 995
e315cd28 996 qla_printk(KERN_INFO, vha->hw,
749af3d5 997 "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 998
e315cd28 999 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1da177e4 1000 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051eb 1001 goto eh_bus_reset_done;
1da177e4
LT
1002 }
1003
e315cd28
AC
1004 if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
1005 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
f4f051eb 1006 ret = SUCCESS;
1da177e4 1007 }
f4f051eb 1008 if (ret == FAILED)
1009 goto eh_bus_reset_done;
1da177e4 1010
9a41a62b 1011 /* Flush outstanding commands. */
17d98630 1012 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) !=
523ec773 1013 QLA_SUCCESS)
9a41a62b 1014 ret = FAILED;
1da177e4 1015
f4f051eb 1016eh_bus_reset_done:
e315cd28 1017 qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
f4f051eb 1018 (ret == FAILED) ? "failed" : "succeded");
1da177e4 1019
f4f051eb 1020 return ret;
1da177e4
LT
1021}
1022
1023/**************************************************************************
1024* qla2xxx_eh_host_reset
1025*
1026* Description:
1027* The reset function will reset the Adapter.
1028*
1029* Input:
1030* cmd = Linux SCSI command packet of the command that cause the
1031* adapter reset.
1032*
1033* Returns:
1034* Either SUCCESS or FAILED.
1035*
1036* Note:
1037**************************************************************************/
e5f82ab8 1038static int
1da177e4
LT
1039qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1040{
e315cd28 1041 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
bdf79621 1042 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
e315cd28 1043 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 1044 int ret = FAILED;
f4f051eb 1045 unsigned int id, lun;
1046 unsigned long serial;
17d98630 1047 srb_t *sp = (srb_t *) CMD_SP(cmd);
e315cd28 1048 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 1049
65d430fa 1050 fc_block_scsi_eh(cmd);
07db5183 1051
f4f051eb 1052 id = cmd->device->id;
1053 lun = cmd->device->lun;
1054 serial = cmd->serial_number;
1055
b0328bee 1056 if (!fcport)
f4f051eb 1057 return ret;
1da177e4 1058
1da177e4 1059 qla_printk(KERN_INFO, ha,
e315cd28 1060 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
1da177e4 1061
e315cd28 1062 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
f4f051eb 1063 goto eh_host_reset_lock;
1da177e4
LT
1064
1065 /*
1066 * Fixme-may be dpc thread is active and processing
fa2a1ce5 1067 * loop_resync,so wait a while for it to
1da177e4
LT
1068 * be completed and then issue big hammer.Otherwise
1069 * it may cause I/O failure as big hammer marks the
1070 * devices as lost kicking of the port_down_timer
1071 * while dpc is stuck for the mailbox to complete.
1072 */
e315cd28
AC
1073 qla2x00_wait_for_loop_ready(vha);
1074 if (vha != base_vha) {
1075 if (qla2x00_vp_abort_isp(vha))
f4f051eb 1076 goto eh_host_reset_lock;
e315cd28 1077 } else {
a9083016
GM
1078 if (IS_QLA82XX(vha->hw)) {
1079 if (!qla82xx_fcoe_ctx_reset(vha)) {
1080 /* Ctx reset success */
1081 ret = SUCCESS;
1082 goto eh_host_reset_lock;
1083 }
1084 /* fall thru if ctx reset failed */
1085 }
68ca949c
AC
1086 if (ha->wq)
1087 flush_workqueue(ha->wq);
1088
e315cd28 1089 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
a9083016 1090 if (ha->isp_ops->abort_isp(base_vha)) {
e315cd28
AC
1091 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1092 /* failed. schedule dpc to try */
1093 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1094
1095 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1096 goto eh_host_reset_lock;
1097 }
1098 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
fa2a1ce5 1099 }
1da177e4 1100
e315cd28 1101 /* Waiting for command to be returned to OS.*/
17d98630 1102 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) ==
e315cd28 1103 QLA_SUCCESS)
f4f051eb 1104 ret = SUCCESS;
1da177e4 1105
f4f051eb 1106eh_host_reset_lock:
f4f051eb 1107 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1108 (ret == FAILED) ? "failed" : "succeded");
1da177e4 1109
f4f051eb 1110 return ret;
1111}
1da177e4
LT
1112
1113/*
1114* qla2x00_loop_reset
1115* Issue loop reset.
1116*
1117* Input:
1118* ha = adapter block pointer.
1119*
1120* Returns:
1121* 0 = success
1122*/
a4722cf2 1123int
e315cd28 1124qla2x00_loop_reset(scsi_qla_host_t *vha)
1da177e4 1125{
0c8c39af 1126 int ret;
bdf79621 1127 struct fc_port *fcport;
e315cd28 1128 struct qla_hw_data *ha = vha->hw;
1da177e4 1129
55e5ed27
AV
1130 if (ha->flags.enable_target_reset) {
1131 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1132 if (fcport->port_type != FCT_TARGET)
1133 continue;
1134
1135 ret = ha->isp_ops->target_reset(fcport, 0, 0);
1136 if (ret != QLA_SUCCESS) {
1137 DEBUG2_3(printk("%s(%ld): bus_reset failed: "
1138 "target_reset=%d d_id=%x.\n", __func__,
1139 vha->host_no, ret, fcport->d_id.b24));
1140 }
1141 }
1142 }
1143
a9083016 1144 if (ha->flags.enable_lip_full_login && !IS_QLA8XXX_TYPE(ha)) {
e315cd28 1145 ret = qla2x00_full_login_lip(vha);
0c8c39af 1146 if (ret != QLA_SUCCESS) {
749af3d5 1147 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28 1148 "full_login_lip=%d.\n", __func__, vha->host_no,
0c8c39af 1149 ret));
749af3d5
AC
1150 }
1151 atomic_set(&vha->loop_state, LOOP_DOWN);
1152 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1153 qla2x00_mark_all_devices_lost(vha, 0);
1154 qla2x00_wait_for_loop_ready(vha);
0c8c39af
AV
1155 }
1156
0d6e61bc 1157 if (ha->flags.enable_lip_reset) {
e315cd28 1158 ret = qla2x00_lip_reset(vha);
0c8c39af 1159 if (ret != QLA_SUCCESS) {
749af3d5 1160 DEBUG2_3(printk("%s(%ld): failed: "
e315cd28
AC
1161 "lip_reset=%d.\n", __func__, vha->host_no, ret));
1162 } else
1163 qla2x00_wait_for_loop_ready(vha);
1da177e4
LT
1164 }
1165
1da177e4 1166 /* Issue marker command only when we are going to start the I/O */
e315cd28 1167 vha->marker_needed = 1;
1da177e4 1168
0c8c39af 1169 return QLA_SUCCESS;
1da177e4
LT
1170}
1171
df4bf0bb 1172void
e315cd28 1173qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
df4bf0bb 1174{
73208dfd 1175 int que, cnt;
df4bf0bb
AV
1176 unsigned long flags;
1177 srb_t *sp;
ac280b67 1178 struct srb_ctx *ctx;
e315cd28 1179 struct qla_hw_data *ha = vha->hw;
73208dfd 1180 struct req_que *req;
df4bf0bb
AV
1181
1182 spin_lock_irqsave(&ha->hardware_lock, flags);
2afa19a9 1183 for (que = 0; que < ha->max_req_queues; que++) {
29bdccbe 1184 req = ha->req_q_map[que];
73208dfd
AC
1185 if (!req)
1186 continue;
1187 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1188 sp = req->outstanding_cmds[cnt];
e612d465 1189 if (sp) {
73208dfd 1190 req->outstanding_cmds[cnt] = NULL;
a9083016
GM
1191 if (!sp->ctx ||
1192 (sp->flags & SRB_FCP_CMND_DMA_VALID)) {
ac280b67
AV
1193 sp->cmd->result = res;
1194 qla2x00_sp_compl(ha, sp);
1195 } else {
1196 ctx = sp->ctx;
6c452a45
AV
1197 if (ctx->type == SRB_LOGIN_CMD ||
1198 ctx->type == SRB_LOGOUT_CMD) {
4916392b 1199 ctx->u.iocb_cmd->free(sp);
db3ad7f8 1200 } else {
6c452a45 1201 struct fc_bsg_job *bsg_job =
4916392b 1202 ctx->u.bsg_job;
6c452a45
AV
1203 if (bsg_job->request->msgcode
1204 == FC_BSG_HST_CT)
db3ad7f8 1205 kfree(sp->fcport);
6c452a45
AV
1206 bsg_job->req->errors = 0;
1207 bsg_job->reply->result = res;
4916392b 1208 bsg_job->job_done(bsg_job);
db3ad7f8 1209 kfree(sp->ctx);
6c452a45 1210 mempool_free(sp,
4916392b 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 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 1230 return 0;
1231}
1da177e4 1232
f4f051eb 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 1248 return 0;
1249}
1da177e4 1250
f4f051eb 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 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 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 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 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 1656 break;
1657 case PCI_DEVICE_ID_QLOGIC_ISP2322:
1658 ha->device_type |= DT_ISP2322;
4a59f71d 1659 ha->device_type |= DT_ZIO_SUPPORTED;
ea5b6382 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 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 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 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 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 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 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 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 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 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 2388 int defer)
2389{
d97994dc 2390 struct fc_rport *rport;
67becc00 2391 scsi_qla_host_t *base_vha;
d97994dc 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 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
5ff1d584
AV
2642 /* Get consistent memory allocated for Async Port-Database. */
2643 if (!IS_FWI2_CAPABLE(ha)) {
2644 ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2645 &ha->async_pd_dma);
2646 if (!ha->async_pd)
2647 goto fail_async_pd;
2648 }
2649
e315cd28
AC
2650 INIT_LIST_HEAD(&ha->vp_list);
2651 return 1;
2652
5ff1d584
AV
2653fail_async_pd:
2654 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
b64b0e8f
AV
2655fail_ex_init_cb:
2656 kfree(ha->npiv_info);
73208dfd
AC
2657fail_npiv_info:
2658 dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2659 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2660 (*rsp)->ring = NULL;
2661 (*rsp)->dma = 0;
e315cd28 2662fail_rsp_ring:
73208dfd 2663 kfree(*rsp);
e315cd28 2664fail_rsp:
73208dfd
AC
2665 dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2666 sizeof(request_t), (*req)->ring, (*req)->dma);
2667 (*req)->ring = NULL;
2668 (*req)->dma = 0;
e315cd28 2669fail_req_ring:
73208dfd 2670 kfree(*req);
e315cd28
AC
2671fail_req:
2672 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2673 ha->ct_sns, ha->ct_sns_dma);
2674 ha->ct_sns = NULL;
2675 ha->ct_sns_dma = 0;
e8711085
AV
2676fail_free_ms_iocb:
2677 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2678 ha->ms_iocb = NULL;
2679 ha->ms_iocb_dma = 0;
e315cd28 2680fail_dma_pool:
a9083016
GM
2681 if (IS_QLA82XX(ha)) {
2682 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2683 ha->fcp_cmnd_dma_pool = NULL;
2684 }
2685fail_dl_dma_pool:
2686 if (IS_QLA82XX(ha)) {
2687 dma_pool_destroy(ha->dl_dma_pool);
2688 ha->dl_dma_pool = NULL;
2689 }
2690fail_s_dma_pool:
e315cd28
AC
2691 dma_pool_destroy(ha->s_dma_pool);
2692 ha->s_dma_pool = NULL;
e8711085
AV
2693fail_free_nvram:
2694 kfree(ha->nvram);
2695 ha->nvram = NULL;
a9083016
GM
2696fail_free_ctx_mempool:
2697 mempool_destroy(ha->ctx_mempool);
2698 ha->ctx_mempool = NULL;
e8711085
AV
2699fail_free_srb_mempool:
2700 mempool_destroy(ha->srb_mempool);
2701 ha->srb_mempool = NULL;
e8711085
AV
2702fail_free_gid_list:
2703 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2704 ha->gid_list_dma);
e8711085
AV
2705 ha->gid_list = NULL;
2706 ha->gid_list_dma = 0;
e315cd28
AC
2707fail_free_init_cb:
2708 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2709 ha->init_cb_dma);
2710 ha->init_cb = NULL;
2711 ha->init_cb_dma = 0;
e8711085 2712fail:
e315cd28 2713 DEBUG(printk("%s: Memory allocation failure\n", __func__));
e8711085 2714 return -ENOMEM;
1da177e4
LT
2715}
2716
2717/*
2718* qla2x00_mem_free
2719* Frees all adapter allocated memory.
2720*
2721* Input:
2722* ha = adapter block pointer.
2723*/
a824ebb3 2724static void
e315cd28 2725qla2x00_mem_free(struct qla_hw_data *ha)
1da177e4 2726{
e8711085
AV
2727 if (ha->srb_mempool)
2728 mempool_destroy(ha->srb_mempool);
1da177e4 2729
df613b96
AV
2730 if (ha->fce)
2731 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
e315cd28 2732 ha->fce_dma);
df613b96 2733
a7a167bf
AV
2734 if (ha->fw_dump) {
2735 if (ha->eft)
2736 dma_free_coherent(&ha->pdev->dev,
e315cd28 2737 ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
a7a167bf
AV
2738 vfree(ha->fw_dump);
2739 }
2740
11bbc1d8
AV
2741 if (ha->dcbx_tlv)
2742 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
2743 ha->dcbx_tlv, ha->dcbx_tlv_dma);
2744
ce0423f4
AV
2745 if (ha->xgmac_data)
2746 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
2747 ha->xgmac_data, ha->xgmac_data_dma);
2748
1da177e4
LT
2749 if (ha->sns_cmd)
2750 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
e315cd28 2751 ha->sns_cmd, ha->sns_cmd_dma);
1da177e4
LT
2752
2753 if (ha->ct_sns)
2754 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
e315cd28 2755 ha->ct_sns, ha->ct_sns_dma);
1da177e4 2756
88729e53
AV
2757 if (ha->sfp_data)
2758 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2759
ad0ecd61
JC
2760 if (ha->edc_data)
2761 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2762
1da177e4
LT
2763 if (ha->ms_iocb)
2764 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2765
b64b0e8f 2766 if (ha->ex_init_cb)
a9083016
GM
2767 dma_pool_free(ha->s_dma_pool,
2768 ha->ex_init_cb, ha->ex_init_cb_dma);
b64b0e8f 2769
5ff1d584
AV
2770 if (ha->async_pd)
2771 dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
2772
1da177e4
LT
2773 if (ha->s_dma_pool)
2774 dma_pool_destroy(ha->s_dma_pool);
2775
1da177e4
LT
2776 if (ha->gid_list)
2777 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
e315cd28 2778 ha->gid_list_dma);
1da177e4 2779
a9083016
GM
2780 if (IS_QLA82XX(ha)) {
2781 if (!list_empty(&ha->gbl_dsd_list)) {
2782 struct dsd_dma *dsd_ptr, *tdsd_ptr;
2783
2784 /* clean up allocated prev pool */
2785 list_for_each_entry_safe(dsd_ptr,
2786 tdsd_ptr, &ha->gbl_dsd_list, list) {
2787 dma_pool_free(ha->dl_dma_pool,
2788 dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
2789 list_del(&dsd_ptr->list);
2790 kfree(dsd_ptr);
2791 }
2792 }
2793 }
2794
2795 if (ha->dl_dma_pool)
2796 dma_pool_destroy(ha->dl_dma_pool);
2797
2798 if (ha->fcp_cmnd_dma_pool)
2799 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
2800
2801 if (ha->ctx_mempool)
2802 mempool_destroy(ha->ctx_mempool);
2803
e315cd28
AC
2804 if (ha->init_cb)
2805 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
a9083016 2806 ha->init_cb, ha->init_cb_dma);
e315cd28
AC
2807 vfree(ha->optrom_buffer);
2808 kfree(ha->nvram);
73208dfd 2809 kfree(ha->npiv_info);
1da177e4 2810
e8711085 2811 ha->srb_mempool = NULL;
a9083016 2812 ha->ctx_mempool = NULL;
a7a167bf
AV
2813 ha->eft = NULL;
2814 ha->eft_dma = 0;
1da177e4
LT
2815 ha->sns_cmd = NULL;
2816 ha->sns_cmd_dma = 0;
2817 ha->ct_sns = NULL;
2818 ha->ct_sns_dma = 0;
2819 ha->ms_iocb = NULL;
2820 ha->ms_iocb_dma = 0;
1da177e4
LT
2821 ha->init_cb = NULL;
2822 ha->init_cb_dma = 0;
b64b0e8f
AV
2823 ha->ex_init_cb = NULL;
2824 ha->ex_init_cb_dma = 0;
5ff1d584
AV
2825 ha->async_pd = NULL;
2826 ha->async_pd_dma = 0;
1da177e4
LT
2827
2828 ha->s_dma_pool = NULL;
a9083016
GM
2829 ha->dl_dma_pool = NULL;
2830 ha->fcp_cmnd_dma_pool = NULL;
1da177e4 2831
1da177e4
LT
2832 ha->gid_list = NULL;
2833 ha->gid_list_dma = 0;
2834
e315cd28
AC
2835 ha->fw_dump = NULL;
2836 ha->fw_dumped = 0;
2837 ha->fw_dump_reading = 0;
e315cd28 2838}
1da177e4 2839
e315cd28
AC
2840struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2841 struct qla_hw_data *ha)
2842{
2843 struct Scsi_Host *host;
2844 struct scsi_qla_host *vha = NULL;
854165f4 2845
e315cd28
AC
2846 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2847 if (host == NULL) {
2848 printk(KERN_WARNING
2849 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2850 goto fail;
2851 }
2852
2853 /* Clear our data area */
2854 vha = shost_priv(host);
2855 memset(vha, 0, sizeof(scsi_qla_host_t));
2856
2857 vha->host = host;
2858 vha->host_no = host->host_no;
2859 vha->hw = ha;
2860
2861 INIT_LIST_HEAD(&vha->vp_fcports);
2862 INIT_LIST_HEAD(&vha->work_list);
2863 INIT_LIST_HEAD(&vha->list);
2864
f999f4c1
AV
2865 spin_lock_init(&vha->work_lock);
2866
e315cd28
AC
2867 sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2868 return vha;
2869
2870fail:
2871 return vha;
1da177e4
LT
2872}
2873
01ef66bb 2874static struct qla_work_evt *
f999f4c1 2875qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
0971de7f
AV
2876{
2877 struct qla_work_evt *e;
2878
f999f4c1 2879 e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
0971de7f
AV
2880 if (!e)
2881 return NULL;
2882
2883 INIT_LIST_HEAD(&e->list);
2884 e->type = type;
2885 e->flags = QLA_EVT_FLAG_FREE;
2886 return e;
2887}
2888
01ef66bb 2889static int
f999f4c1 2890qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
0971de7f 2891{
f999f4c1 2892 unsigned long flags;
0971de7f 2893
f999f4c1 2894 spin_lock_irqsave(&vha->work_lock, flags);
e315cd28 2895 list_add_tail(&e->list, &vha->work_list);
f999f4c1 2896 spin_unlock_irqrestore(&vha->work_lock, flags);
e315cd28 2897 qla2xxx_wake_dpc(vha);
f999f4c1 2898
0971de7f
AV
2899 return QLA_SUCCESS;
2900}
2901
2902int
e315cd28 2903qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
0971de7f
AV
2904 u32 data)
2905{
2906 struct qla_work_evt *e;
2907
f999f4c1 2908 e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
0971de7f
AV
2909 if (!e)
2910 return QLA_FUNCTION_FAILED;
2911
2912 e->u.aen.code = code;
2913 e->u.aen.data = data;
f999f4c1 2914 return qla2x00_post_work(vha, e);
0971de7f
AV
2915}
2916
8a659571
AV
2917int
2918qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2919{
2920 struct qla_work_evt *e;
2921
f999f4c1 2922 e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
8a659571
AV
2923 if (!e)
2924 return QLA_FUNCTION_FAILED;
2925
2926 memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
f999f4c1 2927 return qla2x00_post_work(vha, e);
8a659571
AV
2928}
2929
ac280b67
AV
2930#define qla2x00_post_async_work(name, type) \
2931int qla2x00_post_async_##name##_work( \
2932 struct scsi_qla_host *vha, \
2933 fc_port_t *fcport, uint16_t *data) \
2934{ \
2935 struct qla_work_evt *e; \
2936 \
2937 e = qla2x00_alloc_work(vha, type); \
2938 if (!e) \
2939 return QLA_FUNCTION_FAILED; \
2940 \
2941 e->u.logio.fcport = fcport; \
2942 if (data) { \
2943 e->u.logio.data[0] = data[0]; \
2944 e->u.logio.data[1] = data[1]; \
2945 } \
2946 return qla2x00_post_work(vha, e); \
2947}
2948
2949qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
2950qla2x00_post_async_work(login_done, QLA_EVT_ASYNC_LOGIN_DONE);
2951qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
2952qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
5ff1d584
AV
2953qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
2954qla2x00_post_async_work(adisc_done, QLA_EVT_ASYNC_ADISC_DONE);
ac280b67 2955
3420d36c
AV
2956int
2957qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
2958{
2959 struct qla_work_evt *e;
2960
2961 e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
2962 if (!e)
2963 return QLA_FUNCTION_FAILED;
2964
2965 e->u.uevent.code = code;
2966 return qla2x00_post_work(vha, e);
2967}
2968
2969static void
2970qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
2971{
2972 char event_string[40];
2973 char *envp[] = { event_string, NULL };
2974
2975 switch (code) {
2976 case QLA_UEVENT_CODE_FW_DUMP:
2977 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2978 vha->host_no);
2979 break;
2980 default:
2981 /* do nothing */
2982 break;
2983 }
2984 kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
2985}
2986
ac280b67 2987void
e315cd28 2988qla2x00_do_work(struct scsi_qla_host *vha)
0971de7f 2989{
f999f4c1
AV
2990 struct qla_work_evt *e, *tmp;
2991 unsigned long flags;
2992 LIST_HEAD(work);
0971de7f 2993
f999f4c1
AV
2994 spin_lock_irqsave(&vha->work_lock, flags);
2995 list_splice_init(&vha->work_list, &work);
2996 spin_unlock_irqrestore(&vha->work_lock, flags);
2997
2998 list_for_each_entry_safe(e, tmp, &work, list) {
0971de7f 2999 list_del_init(&e->list);
0971de7f
AV
3000
3001 switch (e->type) {
3002 case QLA_EVT_AEN:
e315cd28 3003 fc_host_post_event(vha->host, fc_get_event_number(),
0971de7f
AV
3004 e->u.aen.code, e->u.aen.data);
3005 break;
8a659571
AV
3006 case QLA_EVT_IDC_ACK:
3007 qla81xx_idc_ack(vha, e->u.idc_ack.mb);
3008 break;
ac280b67
AV
3009 case QLA_EVT_ASYNC_LOGIN:
3010 qla2x00_async_login(vha, e->u.logio.fcport,
3011 e->u.logio.data);
3012 break;
3013 case QLA_EVT_ASYNC_LOGIN_DONE:
3014 qla2x00_async_login_done(vha, e->u.logio.fcport,
3015 e->u.logio.data);
3016 break;
3017 case QLA_EVT_ASYNC_LOGOUT:
3018 qla2x00_async_logout(vha, e->u.logio.fcport);
3019 break;
3020 case QLA_EVT_ASYNC_LOGOUT_DONE:
3021 qla2x00_async_logout_done(vha, e->u.logio.fcport,
3022 e->u.logio.data);
3023 break;
5ff1d584
AV
3024 case QLA_EVT_ASYNC_ADISC:
3025 qla2x00_async_adisc(vha, e->u.logio.fcport,
3026 e->u.logio.data);
3027 break;
3028 case QLA_EVT_ASYNC_ADISC_DONE:
3029 qla2x00_async_adisc_done(vha, e->u.logio.fcport,
3030 e->u.logio.data);
3031 break;
3420d36c
AV
3032 case QLA_EVT_UEVENT:
3033 qla2x00_uevent_emit(vha, e->u.uevent.code);
3034 break;
0971de7f
AV
3035 }
3036 if (e->flags & QLA_EVT_FLAG_FREE)
3037 kfree(e);
e315cd28 3038 }
e315cd28 3039}
f999f4c1 3040
e315cd28
AC
3041/* Relogins all the fcports of a vport
3042 * Context: dpc thread
3043 */
3044void qla2x00_relogin(struct scsi_qla_host *vha)
3045{
3046 fc_port_t *fcport;
c6b2fca8 3047 int status;
e315cd28
AC
3048 uint16_t next_loopid = 0;
3049 struct qla_hw_data *ha = vha->hw;
ac280b67 3050 uint16_t data[2];
e315cd28
AC
3051
3052 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3053 /*
3054 * If the port is not ONLINE then try to login
3055 * to it if we haven't run out of retries.
3056 */
5ff1d584
AV
3057 if (atomic_read(&fcport->state) != FCS_ONLINE &&
3058 fcport->login_retry && !(fcport->flags & FCF_ASYNC_SENT)) {
ac280b67 3059 fcport->login_retry--;
e315cd28 3060 if (fcport->flags & FCF_FABRIC_DEVICE) {
f08b7251 3061 if (fcport->flags & FCF_FCP2_DEVICE)
e315cd28
AC
3062 ha->isp_ops->fabric_logout(vha,
3063 fcport->loop_id,
3064 fcport->d_id.b.domain,
3065 fcport->d_id.b.area,
3066 fcport->d_id.b.al_pa);
3067
ac280b67 3068 if (IS_ALOGIO_CAPABLE(ha)) {
5ff1d584 3069 fcport->flags |= FCF_ASYNC_SENT;
ac280b67
AV
3070 data[0] = 0;
3071 data[1] = QLA_LOGIO_LOGIN_RETRIED;
3072 status = qla2x00_post_async_login_work(
3073 vha, fcport, data);
3074 if (status == QLA_SUCCESS)
3075 continue;
3076 /* Attempt a retry. */
3077 status = 1;
3078 } else
3079 status = qla2x00_fabric_login(vha,
3080 fcport, &next_loopid);
e315cd28
AC
3081 } else
3082 status = qla2x00_local_device_login(vha,
3083 fcport);
3084
e315cd28
AC
3085 if (status == QLA_SUCCESS) {
3086 fcport->old_loop_id = fcport->loop_id;
3087
3088 DEBUG(printk("scsi(%ld): port login OK: logged "
3089 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
3090
3091 qla2x00_update_fcport(vha, fcport);
3092
3093 } else if (status == 1) {
3094 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3095 /* retry the login again */
3096 DEBUG(printk("scsi(%ld): Retrying"
3097 " %d login again loop_id 0x%x\n",
3098 vha->host_no, fcport->login_retry,
3099 fcport->loop_id));
3100 } else {
3101 fcport->login_retry = 0;
3102 }
3103
3104 if (fcport->login_retry == 0 && status != QLA_SUCCESS)
3105 fcport->loop_id = FC_NO_LOOP_ID;
3106 }
3107 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
3108 break;
0971de7f 3109 }
0971de7f
AV
3110}
3111
1da177e4
LT
3112/**************************************************************************
3113* qla2x00_do_dpc
3114* This kernel thread is a task that is schedule by the interrupt handler
3115* to perform the background processing for interrupts.
3116*
3117* Notes:
3118* This task always run in the context of a kernel thread. It
3119* is kick-off by the driver's detect code and starts up
3120* up one per adapter. It immediately goes to sleep and waits for
3121* some fibre event. When either the interrupt handler or
3122* the timer routine detects a event it will one of the task
3123* bits then wake us up.
3124**************************************************************************/
3125static int
3126qla2x00_do_dpc(void *data)
3127{
2c3dfe3f 3128 int rval;
e315cd28
AC
3129 scsi_qla_host_t *base_vha;
3130 struct qla_hw_data *ha;
1da177e4 3131
e315cd28
AC
3132 ha = (struct qla_hw_data *)data;
3133 base_vha = pci_get_drvdata(ha->pdev);
1da177e4 3134
1da177e4
LT
3135 set_user_nice(current, -20);
3136
39a11240 3137 while (!kthread_should_stop()) {
1da177e4
LT
3138 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
3139
39a11240
CH
3140 set_current_state(TASK_INTERRUPTIBLE);
3141 schedule();
3142 __set_current_state(TASK_RUNNING);
1da177e4
LT
3143
3144 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
3145
3146 /* Initialization not yet finished. Don't do anything yet. */
e315cd28 3147 if (!base_vha->flags.init_done)
1da177e4
LT
3148 continue;
3149
85880801
AV
3150 if (ha->flags.eeh_busy) {
3151 DEBUG17(qla_printk(KERN_WARNING, ha,
3152 "qla2x00_do_dpc: dpc_flags: %lx\n",
3153 base_vha->dpc_flags));
3154 continue;
3155 }
3156
e315cd28 3157 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
1da177e4
LT
3158
3159 ha->dpc_active = 1;
3160
1da177e4 3161 if (ha->flags.mbox_busy) {
1da177e4
LT
3162 ha->dpc_active = 0;
3163 continue;
3164 }
3165
e315cd28 3166 qla2x00_do_work(base_vha);
0971de7f 3167
a9083016
GM
3168 if (IS_QLA82XX(ha)) {
3169 if (test_and_clear_bit(ISP_UNRECOVERABLE,
3170 &base_vha->dpc_flags)) {
3171 qla82xx_idc_lock(ha);
3172 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3173 QLA82XX_DEV_FAILED);
3174 qla82xx_idc_unlock(ha);
3175 qla_printk(KERN_INFO, ha,
3176 "HW State: FAILED\n");
3177 qla82xx_device_state_handler(base_vha);
3178 continue;
3179 }
3180
3181 if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
3182 &base_vha->dpc_flags)) {
3183
3184 DEBUG(printk(KERN_INFO
3185 "scsi(%ld): dpc: sched "
3186 "qla82xx_fcoe_ctx_reset ha = %p\n",
3187 base_vha->host_no, ha));
3188 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3189 &base_vha->dpc_flags))) {
3190 if (qla82xx_fcoe_ctx_reset(base_vha)) {
3191 /* FCoE-ctx reset failed.
3192 * Escalate to chip-reset
3193 */
3194 set_bit(ISP_ABORT_NEEDED,
3195 &base_vha->dpc_flags);
3196 }
3197 clear_bit(ABORT_ISP_ACTIVE,
3198 &base_vha->dpc_flags);
3199 }
3200
3201 DEBUG(printk("scsi(%ld): dpc:"
3202 " qla82xx_fcoe_ctx_reset end\n",
3203 base_vha->host_no));
3204 }
3205 }
3206
e315cd28
AC
3207 if (test_and_clear_bit(ISP_ABORT_NEEDED,
3208 &base_vha->dpc_flags)) {
1da177e4
LT
3209
3210 DEBUG(printk("scsi(%ld): dpc: sched "
3211 "qla2x00_abort_isp ha = %p\n",
e315cd28 3212 base_vha->host_no, ha));
1da177e4 3213 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
e315cd28 3214 &base_vha->dpc_flags))) {
1da177e4 3215
a9083016 3216 if (ha->isp_ops->abort_isp(base_vha)) {
1da177e4
LT
3217 /* failed. retry later */
3218 set_bit(ISP_ABORT_NEEDED,
e315cd28 3219 &base_vha->dpc_flags);
99363ef8 3220 }
e315cd28
AC
3221 clear_bit(ABORT_ISP_ACTIVE,
3222 &base_vha->dpc_flags);
99363ef8
SJ
3223 }
3224
1da177e4 3225 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
e315cd28 3226 base_vha->host_no));
1da177e4
LT
3227 }
3228
e315cd28
AC
3229 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
3230 qla2x00_update_fcports(base_vha);
3231 clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
c9c5ced9 3232 }
d97994dc 3233
e315cd28
AC
3234 if (test_and_clear_bit(RESET_MARKER_NEEDED,
3235 &base_vha->dpc_flags) &&
3236 (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
1da177e4
LT
3237
3238 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
e315cd28 3239 base_vha->host_no));
1da177e4 3240
e315cd28
AC
3241 qla2x00_rst_aen(base_vha);
3242 clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
1da177e4
LT
3243 }
3244
3245 /* Retry each device up to login retry count */
e315cd28
AC
3246 if ((test_and_clear_bit(RELOGIN_NEEDED,
3247 &base_vha->dpc_flags)) &&
3248 !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
3249 atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
1da177e4
LT
3250
3251 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
e315cd28
AC
3252 base_vha->host_no));
3253 qla2x00_relogin(base_vha);
3254
1da177e4 3255 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
e315cd28 3256 base_vha->host_no));
1da177e4
LT
3257 }
3258
e315cd28
AC
3259 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
3260 &base_vha->dpc_flags)) {
1da177e4
LT
3261
3262 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
e315cd28 3263 base_vha->host_no));
1da177e4
LT
3264
3265 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
e315cd28 3266 &base_vha->dpc_flags))) {
1da177e4 3267
e315cd28 3268 rval = qla2x00_loop_resync(base_vha);
1da177e4 3269
e315cd28
AC
3270 clear_bit(LOOP_RESYNC_ACTIVE,
3271 &base_vha->dpc_flags);
1da177e4
LT
3272 }
3273
3274 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
e315cd28 3275 base_vha->host_no));
1da177e4
LT
3276 }
3277
e315cd28
AC
3278 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
3279 atomic_read(&base_vha->loop_state) == LOOP_READY) {
3280 clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
3281 qla2xxx_flash_npiv_conf(base_vha);
272976ca
AV
3282 }
3283
1da177e4 3284 if (!ha->interrupts_on)
fd34f556 3285 ha->isp_ops->enable_intrs(ha);
1da177e4 3286
e315cd28
AC
3287 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
3288 &base_vha->dpc_flags))
3289 ha->isp_ops->beacon_blink(base_vha);
f6df144c 3290
e315cd28 3291 qla2x00_do_dpc_all_vps(base_vha);
2c3dfe3f 3292
1da177e4
LT
3293 ha->dpc_active = 0;
3294 } /* End of while(1) */
3295
e315cd28 3296 DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
1da177e4
LT
3297
3298 /*
3299 * Make sure that nobody tries to wake us up again.
3300 */
1da177e4
LT
3301 ha->dpc_active = 0;
3302
ac280b67
AV
3303 /* Cleanup any residual CTX SRBs. */
3304 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3305
39a11240
CH
3306 return 0;
3307}
3308
3309void
e315cd28 3310qla2xxx_wake_dpc(struct scsi_qla_host *vha)
39a11240 3311{
e315cd28 3312 struct qla_hw_data *ha = vha->hw;
c795c1e4
AV
3313 struct task_struct *t = ha->dpc_thread;
3314
e315cd28 3315 if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
c795c1e4 3316 wake_up_process(t);
1da177e4
LT
3317}
3318
1da177e4
LT
3319/*
3320* qla2x00_rst_aen
3321* Processes asynchronous reset.
3322*
3323* Input:
3324* ha = adapter block pointer.
3325*/
3326static void
e315cd28 3327qla2x00_rst_aen(scsi_qla_host_t *vha)
1da177e4 3328{
e315cd28
AC
3329 if (vha->flags.online && !vha->flags.reset_active &&
3330 !atomic_read(&vha->loop_down_timer) &&
3331 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
1da177e4 3332 do {
e315cd28 3333 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1da177e4
LT
3334
3335 /*
3336 * Issue marker command only when we are going to start
3337 * the I/O.
3338 */
e315cd28
AC
3339 vha->marker_needed = 1;
3340 } while (!atomic_read(&vha->loop_down_timer) &&
3341 (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
1da177e4
LT
3342 }
3343}
3344
f4f051eb 3345static void
e315cd28 3346qla2x00_sp_free_dma(srb_t *sp)
f4f051eb 3347{
3348 struct scsi_cmnd *cmd = sp->cmd;
3349
3350 if (sp->flags & SRB_DMA_VALID) {
385d70b4 3351 scsi_dma_unmap(cmd);
f4f051eb 3352 sp->flags &= ~SRB_DMA_VALID;
3353 }
fca29703 3354 CMD_SP(cmd) = NULL;
f4f051eb 3355}
3356
3357void
73208dfd 3358qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
f4f051eb 3359{
3360 struct scsi_cmnd *cmd = sp->cmd;
3361
e315cd28 3362 qla2x00_sp_free_dma(sp);
f4f051eb 3363
a9083016
GM
3364 if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
3365 struct ct6_dsd *ctx = sp->ctx;
3366 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd,
3367 ctx->fcp_cmnd_dma);
3368 list_splice(&ctx->dsd_list, &ha->gbl_dsd_list);
3369 ha->gbl_dsd_inuse -= ctx->dsd_use_cnt;
3370 ha->gbl_dsd_avail += ctx->dsd_use_cnt;
3371 mempool_free(sp->ctx, ha->ctx_mempool);
3372 sp->ctx = NULL;
3373 }
f4f051eb 3374
a9083016 3375 mempool_free(sp, ha->srb_mempool);
f4f051eb 3376 cmd->scsi_done(cmd);
3377}
bdf79621 3378
1da177e4
LT
3379/**************************************************************************
3380* qla2x00_timer
3381*
3382* Description:
3383* One second timer
3384*
3385* Context: Interrupt
3386***************************************************************************/
2c3dfe3f 3387void
e315cd28 3388qla2x00_timer(scsi_qla_host_t *vha)
1da177e4 3389{
1da177e4
LT
3390 unsigned long cpu_flags = 0;
3391 fc_port_t *fcport;
1da177e4
LT
3392 int start_dpc = 0;
3393 int index;
3394 srb_t *sp;
f4f051eb 3395 int t;
85880801 3396 uint16_t w;
e315cd28 3397 struct qla_hw_data *ha = vha->hw;
73208dfd 3398 struct req_que *req;
85880801 3399
a9083016
GM
3400 if (IS_QLA82XX(ha))
3401 qla82xx_watchdog(vha);
3402
85880801
AV
3403 /* Hardware read to raise pending EEH errors during mailbox waits. */
3404 if (!pci_channel_offline(ha->pdev))
3405 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
1da177e4
LT
3406 /*
3407 * Ports - Port down timer.
3408 *
3409 * Whenever, a port is in the LOST state we start decrementing its port
3410 * down timer every second until it reaches zero. Once it reaches zero
fa2a1ce5 3411 * the port it marked DEAD.
1da177e4
LT
3412 */
3413 t = 0;
e315cd28 3414 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1da177e4
LT
3415 if (fcport->port_type != FCT_TARGET)
3416 continue;
3417
3418 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3419
3420 if (atomic_read(&fcport->port_down_timer) == 0)
3421 continue;
3422
fa2a1ce5 3423 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
1da177e4 3424 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
fa2a1ce5 3425
1da177e4 3426 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
fca29703 3427 "%d remaining\n",
e315cd28 3428 vha->host_no,
1da177e4
LT
3429 t, atomic_read(&fcport->port_down_timer)));
3430 }
3431 t++;
3432 } /* End of for fcport */
3433
1da177e4
LT
3434
3435 /* Loop down handler. */
e315cd28
AC
3436 if (atomic_read(&vha->loop_down_timer) > 0 &&
3437 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3438 && vha->flags.online) {
1da177e4 3439
e315cd28
AC
3440 if (atomic_read(&vha->loop_down_timer) ==
3441 vha->loop_down_abort_time) {
1da177e4
LT
3442
3443 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3444 "queues before time expire\n",
e315cd28 3445 vha->host_no));
1da177e4 3446
e315cd28
AC
3447 if (!IS_QLA2100(ha) && vha->link_down_timeout)
3448 atomic_set(&vha->loop_state, LOOP_DEAD);
1da177e4 3449
f08b7251
AV
3450 /*
3451 * Schedule an ISP abort to return any FCP2-device
3452 * commands.
3453 */
2c3dfe3f 3454 /* NPIV - scan physical port only */
e315cd28 3455 if (!vha->vp_idx) {
2c3dfe3f
SJ
3456 spin_lock_irqsave(&ha->hardware_lock,
3457 cpu_flags);
73208dfd 3458 req = ha->req_q_map[0];
2c3dfe3f
SJ
3459 for (index = 1;
3460 index < MAX_OUTSTANDING_COMMANDS;
3461 index++) {
3462 fc_port_t *sfcp;
3463
e315cd28 3464 sp = req->outstanding_cmds[index];
2c3dfe3f
SJ
3465 if (!sp)
3466 continue;
cf53b069
AV
3467 if (sp->ctx)
3468 continue;
2c3dfe3f 3469 sfcp = sp->fcport;
f08b7251 3470 if (!(sfcp->flags & FCF_FCP2_DEVICE))
2c3dfe3f 3471 continue;
bdf79621 3472
2c3dfe3f 3473 set_bit(ISP_ABORT_NEEDED,
e315cd28 3474 &vha->dpc_flags);
2c3dfe3f
SJ
3475 break;
3476 }
3477 spin_unlock_irqrestore(&ha->hardware_lock,
e315cd28 3478 cpu_flags);
1da177e4 3479 }
1da177e4
LT
3480 start_dpc++;
3481 }
3482
3483 /* if the loop has been down for 4 minutes, reinit adapter */
e315cd28 3484 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
0d6e61bc 3485 if (!(vha->device_flags & DFLG_NO_CABLE)) {
1da177e4
LT
3486 DEBUG(printk("scsi(%ld): Loop down - "
3487 "aborting ISP.\n",
e315cd28 3488 vha->host_no));
1da177e4
LT
3489 qla_printk(KERN_WARNING, ha,
3490 "Loop down - aborting ISP.\n");
3491
e315cd28 3492 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1da177e4
LT
3493 }
3494 }
fca29703 3495 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
e315cd28
AC
3496 vha->host_no,
3497 atomic_read(&vha->loop_down_timer)));
1da177e4
LT
3498 }
3499
f6df144c 3500 /* Check if beacon LED needs to be blinked */
3501 if (ha->beacon_blink_led == 1) {
e315cd28 3502 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
f6df144c 3503 start_dpc++;
3504 }
3505
550bf57d 3506 /* Process any deferred work. */
e315cd28 3507 if (!list_empty(&vha->work_list))
550bf57d
AV
3508 start_dpc++;
3509
1da177e4 3510 /* Schedule the DPC routine if needed */
e315cd28
AC
3511 if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
3512 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
3513 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
1da177e4 3514 start_dpc ||
e315cd28
AC
3515 test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
3516 test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
a9083016
GM
3517 test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
3518 test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
e315cd28
AC
3519 test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
3520 test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
3521 qla2xxx_wake_dpc(vha);
1da177e4 3522
e315cd28 3523 qla2x00_restart_timer(vha, WATCH_INTERVAL);
1da177e4
LT
3524}
3525
5433383e
AV
3526/* Firmware interface routines. */
3527
a9083016 3528#define FW_BLOBS 8
5433383e
AV
3529#define FW_ISP21XX 0
3530#define FW_ISP22XX 1
3531#define FW_ISP2300 2
3532#define FW_ISP2322 3
48c02fde 3533#define FW_ISP24XX 4
c3a2f0df 3534#define FW_ISP25XX 5
3a03eb79 3535#define FW_ISP81XX 6
a9083016 3536#define FW_ISP82XX 7
5433383e 3537
bb8ee499
AV
3538#define FW_FILE_ISP21XX "ql2100_fw.bin"
3539#define FW_FILE_ISP22XX "ql2200_fw.bin"
3540#define FW_FILE_ISP2300 "ql2300_fw.bin"
3541#define FW_FILE_ISP2322 "ql2322_fw.bin"
3542#define FW_FILE_ISP24XX "ql2400_fw.bin"
c3a2f0df 3543#define FW_FILE_ISP25XX "ql2500_fw.bin"
3a03eb79 3544#define FW_FILE_ISP81XX "ql8100_fw.bin"
a9083016 3545#define FW_FILE_ISP82XX "ql8200_fw.bin"
bb8ee499 3546
e1e82b6f 3547static DEFINE_MUTEX(qla_fw_lock);
5433383e
AV
3548
3549static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
bb8ee499
AV
3550 { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
3551 { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
3552 { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
3553 { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
3554 { .name = FW_FILE_ISP24XX, },
c3a2f0df 3555 { .name = FW_FILE_ISP25XX, },
3a03eb79 3556 { .name = FW_FILE_ISP81XX, },
a9083016 3557 { .name = FW_FILE_ISP82XX, },
5433383e
AV
3558};
3559
3560struct fw_blob *
e315cd28 3561qla2x00_request_firmware(scsi_qla_host_t *vha)
5433383e 3562{
e315cd28 3563 struct qla_hw_data *ha = vha->hw;
5433383e
AV
3564 struct fw_blob *blob;
3565
3566 blob = NULL;
3567 if (IS_QLA2100(ha)) {
3568 blob = &qla_fw_blobs[FW_ISP21XX];
3569 } else if (IS_QLA2200(ha)) {
3570 blob = &qla_fw_blobs[FW_ISP22XX];
48c02fde 3571 } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
5433383e 3572 blob = &qla_fw_blobs[FW_ISP2300];
48c02fde 3573 } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
5433383e 3574 blob = &qla_fw_blobs[FW_ISP2322];
4d4df193 3575 } else if (IS_QLA24XX_TYPE(ha)) {
5433383e 3576 blob = &qla_fw_blobs[FW_ISP24XX];
c3a2f0df
AV
3577 } else if (IS_QLA25XX(ha)) {
3578 blob = &qla_fw_blobs[FW_ISP25XX];
3a03eb79
AV
3579 } else if (IS_QLA81XX(ha)) {
3580 blob = &qla_fw_blobs[FW_ISP81XX];
a9083016
GM
3581 } else if (IS_QLA82XX(ha)) {
3582 blob = &qla_fw_blobs[FW_ISP82XX];
5433383e
AV
3583 }
3584
e1e82b6f 3585 mutex_lock(&qla_fw_lock);
5433383e
AV
3586 if (blob->fw)
3587 goto out;
3588
3589 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
3590 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
e315cd28 3591 "(%s).\n", vha->host_no, blob->name));
5433383e
AV
3592 blob->fw = NULL;
3593 blob = NULL;
3594 goto out;
3595 }
3596
3597out:
e1e82b6f 3598 mutex_unlock(&qla_fw_lock);
5433383e
AV
3599 return blob;
3600}
3601
3602static void
3603qla2x00_release_firmware(void)
3604{
3605 int idx;
3606
e1e82b6f 3607 mutex_lock(&qla_fw_lock);
5433383e
AV
3608 for (idx = 0; idx < FW_BLOBS; idx++)
3609 if (qla_fw_blobs[idx].fw)
3610 release_firmware(qla_fw_blobs[idx].fw);
e1e82b6f 3611 mutex_unlock(&qla_fw_lock);
5433383e
AV
3612}
3613
14e660e6
SJ
3614static pci_ers_result_t
3615qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3616{
85880801
AV
3617 scsi_qla_host_t *vha = pci_get_drvdata(pdev);
3618 struct qla_hw_data *ha = vha->hw;
3619
3620 DEBUG2(qla_printk(KERN_WARNING, ha, "error_detected:state %x\n",
3621 state));
b9b12f73 3622
14e660e6
SJ
3623 switch (state) {
3624 case pci_channel_io_normal:
85880801 3625 ha->flags.eeh_busy = 0;
14e660e6
SJ
3626 return PCI_ERS_RESULT_CAN_RECOVER;
3627 case pci_channel_io_frozen:
85880801 3628 ha->flags.eeh_busy = 1;
90a86fc0 3629 qla2x00_free_irqs(vha);
14e660e6
SJ
3630 pci_disable_device(pdev);
3631 return PCI_ERS_RESULT_NEED_RESET;
3632 case pci_channel_io_perm_failure:
85880801
AV
3633 ha->flags.pci_channel_io_perm_failure = 1;
3634 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
14e660e6
SJ
3635 return PCI_ERS_RESULT_DISCONNECT;
3636 }
3637 return PCI_ERS_RESULT_NEED_RESET;
3638}
3639
3640static pci_ers_result_t
3641qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3642{
3643 int risc_paused = 0;
3644 uint32_t stat;
3645 unsigned long flags;
e315cd28
AC
3646 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3647 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3648 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3649 struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3650
3651 spin_lock_irqsave(&ha->hardware_lock, flags);
3652 if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3653 stat = RD_REG_DWORD(&reg->hccr);
3654 if (stat & HCCR_RISC_PAUSE)
3655 risc_paused = 1;
3656 } else if (IS_QLA23XX(ha)) {
3657 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3658 if (stat & HSR_RISC_PAUSED)
3659 risc_paused = 1;
3660 } else if (IS_FWI2_CAPABLE(ha)) {
3661 stat = RD_REG_DWORD(&reg24->host_status);
3662 if (stat & HSRX_RISC_PAUSED)
3663 risc_paused = 1;
3664 }
3665 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3666
3667 if (risc_paused) {
3668 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3669 "Dumping firmware!\n");
e315cd28 3670 ha->isp_ops->fw_dump(base_vha, 0);
14e660e6
SJ
3671
3672 return PCI_ERS_RESULT_NEED_RESET;
3673 } else
3674 return PCI_ERS_RESULT_RECOVERED;
3675}
3676
3677static pci_ers_result_t
3678qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3679{
3680 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
e315cd28
AC
3681 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3682 struct qla_hw_data *ha = base_vha->hw;
90a86fc0
JC
3683 struct rsp_que *rsp;
3684 int rc, retries = 10;
09483916 3685
85880801
AV
3686 DEBUG17(qla_printk(KERN_WARNING, ha, "slot_reset\n"));
3687
90a86fc0
JC
3688 /* Workaround: qla2xxx driver which access hardware earlier
3689 * needs error state to be pci_channel_io_online.
3690 * Otherwise mailbox command timesout.
3691 */
3692 pdev->error_state = pci_channel_io_normal;
3693
3694 pci_restore_state(pdev);
3695
8c1496bd
RL
3696 /* pci_restore_state() clears the saved_state flag of the device
3697 * save restored state which resets saved_state flag
3698 */
3699 pci_save_state(pdev);
3700
09483916
BH
3701 if (ha->mem_only)
3702 rc = pci_enable_device_mem(pdev);
3703 else
3704 rc = pci_enable_device(pdev);
14e660e6 3705
09483916 3706 if (rc) {
14e660e6
SJ
3707 qla_printk(KERN_WARNING, ha,
3708 "Can't re-enable PCI device after reset.\n");
14e660e6
SJ
3709 return ret;
3710 }
14e660e6 3711
90a86fc0
JC
3712 rsp = ha->rsp_q_map[0];
3713 if (qla2x00_request_irqs(ha, rsp))
3714 return ret;
3715
e315cd28 3716 if (ha->isp_ops->pci_config(base_vha))
14e660e6
SJ
3717 return ret;
3718
90a86fc0
JC
3719 while (ha->flags.mbox_busy && retries--)
3720 msleep(1000);
85880801 3721
e315cd28 3722 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
a9083016 3723 if (ha->isp_ops->abort_isp(base_vha) == QLA_SUCCESS)
14e660e6 3724 ret = PCI_ERS_RESULT_RECOVERED;
e315cd28 3725 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
14e660e6 3726
90a86fc0 3727
85880801
AV
3728 DEBUG17(qla_printk(KERN_WARNING, ha,
3729 "slot_reset-return:ret=%x\n", ret));
3730
14e660e6
SJ
3731 return ret;
3732}
3733
3734static void
3735qla2xxx_pci_resume(struct pci_dev *pdev)
3736{
e315cd28
AC
3737 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3738 struct qla_hw_data *ha = base_vha->hw;
14e660e6
SJ
3739 int ret;
3740
85880801
AV
3741 DEBUG17(qla_printk(KERN_WARNING, ha, "pci_resume\n"));
3742
e315cd28 3743 ret = qla2x00_wait_for_hba_online(base_vha);
14e660e6
SJ
3744 if (ret != QLA_SUCCESS) {
3745 qla_printk(KERN_ERR, ha,
3746 "the device failed to resume I/O "
3747 "from slot/link_reset");
3748 }
85880801 3749
3e46f031
LC
3750 pci_cleanup_aer_uncorrect_error_status(pdev);
3751
85880801 3752 ha->flags.eeh_busy = 0;
14e660e6
SJ
3753}
3754
3755static struct pci_error_handlers qla2xxx_err_handler = {
3756 .error_detected = qla2xxx_pci_error_detected,
3757 .mmio_enabled = qla2xxx_pci_mmio_enabled,
3758 .slot_reset = qla2xxx_pci_slot_reset,
3759 .resume = qla2xxx_pci_resume,
3760};
3761
5433383e 3762static struct pci_device_id qla2xxx_pci_tbl[] = {
47f5e069
AV
3763 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3764 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3765 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3766 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3767 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3768 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3769 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3770 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3771 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
4d4df193 3772 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
47f5e069
AV
3773 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3774 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
c3a2f0df 3775 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3a03eb79 3776 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
a9083016 3777 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
5433383e
AV
3778 { 0 },
3779};
3780MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3781
fca29703 3782static struct pci_driver qla2xxx_pci_driver = {
cb63067a 3783 .name = QLA2XXX_DRIVER_NAME,
0a21ef1e
JB
3784 .driver = {
3785 .owner = THIS_MODULE,
3786 },
fca29703 3787 .id_table = qla2xxx_pci_tbl,
7ee61397 3788 .probe = qla2x00_probe_one,
4c993f76 3789 .remove = qla2x00_remove_one,
14e660e6 3790 .err_handler = &qla2xxx_err_handler,
fca29703
AV
3791};
3792
6a03b4cd
HZ
3793static struct file_operations apidev_fops = {
3794 .owner = THIS_MODULE,
3795};
3796
1da177e4
LT
3797/**
3798 * qla2x00_module_init - Module initialization.
3799 **/
3800static int __init
3801qla2x00_module_init(void)
3802{
fca29703
AV
3803 int ret = 0;
3804
1da177e4 3805 /* Allocate cache for SRBs. */
354d6b21 3806 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
20c2df83 3807 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
3808 if (srb_cachep == NULL) {
3809 printk(KERN_ERR
3810 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3811 return -ENOMEM;
3812 }
3813
3814 /* Derive version string. */
3815 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
11010fec 3816 if (ql2xextended_error_logging)
0181944f
AV
3817 strcat(qla2x00_version_str, "-debug");
3818
1c97a12a
AV
3819 qla2xxx_transport_template =
3820 fc_attach_transport(&qla2xxx_transport_functions);
2c3dfe3f
SJ
3821 if (!qla2xxx_transport_template) {
3822 kmem_cache_destroy(srb_cachep);
1da177e4 3823 return -ENODEV;
2c3dfe3f 3824 }
6a03b4cd
HZ
3825
3826 apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
3827 if (apidev_major < 0) {
3828 printk(KERN_WARNING "qla2xxx: Unable to register char device "
3829 "%s\n", QLA2XXX_APIDEV);
3830 }
3831
2c3dfe3f
SJ
3832 qla2xxx_transport_vport_template =
3833 fc_attach_transport(&qla2xxx_transport_vport_functions);
3834 if (!qla2xxx_transport_vport_template) {
3835 kmem_cache_destroy(srb_cachep);
3836 fc_release_transport(qla2xxx_transport_template);
1da177e4 3837 return -ENODEV;
2c3dfe3f 3838 }
1da177e4 3839
fd9a29f0
AV
3840 printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3841 qla2x00_version_str);
7ee61397 3842 ret = pci_register_driver(&qla2xxx_pci_driver);
fca29703
AV
3843 if (ret) {
3844 kmem_cache_destroy(srb_cachep);
3845 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3846 fc_release_transport(qla2xxx_transport_vport_template);
fca29703
AV
3847 }
3848 return ret;
1da177e4
LT
3849}
3850
3851/**
3852 * qla2x00_module_exit - Module cleanup.
3853 **/
3854static void __exit
3855qla2x00_module_exit(void)
3856{
6a03b4cd 3857 unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
7ee61397 3858 pci_unregister_driver(&qla2xxx_pci_driver);
5433383e 3859 qla2x00_release_firmware();
354d6b21 3860 kmem_cache_destroy(srb_cachep);
a9083016
GM
3861 if (ctx_cachep)
3862 kmem_cache_destroy(ctx_cachep);
1da177e4 3863 fc_release_transport(qla2xxx_transport_template);
2c3dfe3f 3864 fc_release_transport(qla2xxx_transport_vport_template);
1da177e4
LT
3865}
3866
3867module_init(qla2x00_module_init);
3868module_exit(qla2x00_module_exit);
3869
3870MODULE_AUTHOR("QLogic Corporation");
3871MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3872MODULE_LICENSE("GPL");
3873MODULE_VERSION(QLA2XXX_VERSION);
bb8ee499
AV
3874MODULE_FIRMWARE(FW_FILE_ISP21XX);
3875MODULE_FIRMWARE(FW_FILE_ISP22XX);
3876MODULE_FIRMWARE(FW_FILE_ISP2300);
3877MODULE_FIRMWARE(FW_FILE_ISP2322);
3878MODULE_FIRMWARE(FW_FILE_ISP24XX);
61623fc3 3879MODULE_FIRMWARE(FW_FILE_ISP25XX);
This page took 0.764013 seconds and 5 git commands to generate.